@@ -50,16 +50,91 @@ function submitSystemPrompt(event) {
50
50
51
51
var image = "" ;
52
52
var audio = "" ;
53
+ var fileContent = "" ;
54
+ var currentFileName = "" ;
55
+
56
+ async function extractTextFromPDF ( pdfData ) {
57
+ try {
58
+ const pdf = await pdfjsLib . getDocument ( { data : pdfData } ) . promise ;
59
+ let fullText = '' ;
60
+
61
+ for ( let i = 1 ; i <= pdf . numPages ; i ++ ) {
62
+ const page = await pdf . getPage ( i ) ;
63
+ const textContent = await page . getTextContent ( ) ;
64
+ const pageText = textContent . items . map ( item => item . str ) . join ( ' ' ) ;
65
+ fullText += pageText + '\n' ;
66
+ }
67
+
68
+ return fullText ;
69
+ } catch ( error ) {
70
+ console . error ( 'Error extracting text from PDF:' , error ) ;
71
+ throw error ;
72
+ }
73
+ }
74
+
75
+ function readInputFile ( ) {
76
+ if ( ! this . files || ! this . files [ 0 ] ) return ;
77
+
78
+ const file = this . files [ 0 ] ;
79
+ const FR = new FileReader ( ) ;
80
+ currentFileName = file . name ;
81
+ const fileExtension = file . name . split ( '.' ) . pop ( ) . toLowerCase ( ) ;
82
+
83
+ FR . addEventListener ( "load" , async function ( evt ) {
84
+ if ( fileExtension === 'pdf' ) {
85
+ try {
86
+ fileContent = await extractTextFromPDF ( evt . target . result ) ;
87
+ } catch ( error ) {
88
+ console . error ( 'Error processing PDF:' , error ) ;
89
+ fileContent = "Error processing PDF file" ;
90
+ }
91
+ } else {
92
+ // For text and markdown files
93
+ fileContent = evt . target . result ;
94
+ }
95
+ } ) ;
96
+
97
+ if ( fileExtension === 'pdf' ) {
98
+ FR . readAsArrayBuffer ( file ) ;
99
+ } else {
100
+ FR . readAsText ( file ) ;
101
+ }
102
+ }
53
103
54
104
function submitPrompt ( event ) {
55
105
event . preventDefault ( ) ;
56
106
57
107
const input = document . getElementById ( "input" ) . value ;
58
- Alpine . store ( "chat" ) . add ( "user" , input , image , audio ) ;
108
+ let fullInput = input ;
109
+
110
+ // If there's file content, append it to the input for the LLM
111
+ if ( fileContent ) {
112
+ fullInput += "\n\nFile content:\n" + fileContent ;
113
+ }
114
+
115
+ // Show file icon in chat if there's a file
116
+ let displayContent = input ;
117
+ if ( currentFileName ) {
118
+ displayContent += `\n\n<i class="fa-solid fa-file"></i> Attached file: ${ currentFileName } ` ;
119
+ }
120
+
121
+ // Add the message to the chat UI with just the icon
122
+ Alpine . store ( "chat" ) . add ( "user" , displayContent , image , audio ) ;
123
+
124
+ // Update the last message in the store with the full content
125
+ const history = Alpine . store ( "chat" ) . history ;
126
+ if ( history . length > 0 ) {
127
+ history [ history . length - 1 ] . content = fullInput ;
128
+ }
129
+
59
130
document . getElementById ( "input" ) . value = "" ;
60
131
const systemPrompt = localStorage . getItem ( "system_prompt" ) ;
61
132
Alpine . nextTick ( ( ) => { document . getElementById ( 'messages' ) . scrollIntoView ( false ) ; } ) ;
62
- promptGPT ( systemPrompt , input ) ;
133
+ promptGPT ( systemPrompt , fullInput ) ;
134
+
135
+ // Reset file content and name after sending
136
+ fileContent = "" ;
137
+ currentFileName = "" ;
63
138
}
64
139
65
140
function readInputImage ( ) {
@@ -88,9 +163,6 @@ function readInputAudio() {
88
163
89
164
async function promptGPT ( systemPrompt , input ) {
90
165
const model = document . getElementById ( "chat-model" ) . value ;
91
- // Set class "loader" to the element with "loader" id
92
- //document.getElementById("loader").classList.add("loader");
93
- // Make the "loader" visible
94
166
toggleLoader ( true ) ;
95
167
96
168
messages = Alpine . store ( "chat" ) . messages ( ) ;
@@ -261,6 +333,7 @@ document.getElementById("prompt").addEventListener("submit", submitPrompt);
261
333
document . getElementById ( "input" ) . focus ( ) ;
262
334
document . getElementById ( "input_image" ) . addEventListener ( "change" , readInputImage ) ;
263
335
document . getElementById ( "input_audio" ) . addEventListener ( "change" , readInputAudio ) ;
336
+ document . getElementById ( "input_file" ) . addEventListener ( "change" , readInputFile ) ;
264
337
265
338
storesystemPrompt = localStorage . getItem ( "system_prompt" ) ;
266
339
if ( storesystemPrompt ) {
0 commit comments