@@ -575,6 +575,7 @@ const handle_ask = async (do_ask_gpt = true, message = null) => {
575
575
}
576
576
} else {
577
577
await safe_load_conversation ( window . conversation_id , true ) ;
578
+ await load_conversations ( ) ;
578
579
}
579
580
} ;
580
581
@@ -1330,7 +1331,12 @@ const delete_conversation = async (conversation_id) => {
1330
1331
}
1331
1332
}
1332
1333
}
1333
-
1334
+ if ( window . share_id && conversation_id == window . start_id ) {
1335
+ const url = `${ window . share_url } /backend-api/v2/files/${ window . share_id } ` ;
1336
+ await fetch ( url , {
1337
+ method : 'DELETE'
1338
+ } ) ;
1339
+ }
1334
1340
appStorage . removeItem ( `conversation:${ conversation_id } ` ) ;
1335
1341
const item = document . getElementById ( `convo-${ conversation_id } ` ) ;
1336
1342
item . remove ( ) ;
@@ -1360,7 +1366,9 @@ const set_conversation = async (conversation_id) => {
1360
1366
} ;
1361
1367
1362
1368
const new_conversation = async ( ) => {
1363
- history . pushState ( { } , null , `/chat/` ) ;
1369
+ if ( ! / \/ c h a t \/ ( s h a r e | \? | $ ) / . test ( window . location . href ) ) {
1370
+ history . pushState ( { } , null , `/chat/` ) ;
1371
+ }
1364
1372
window . conversation_id = generateUUID ( ) ;
1365
1373
document . title = window . title || document . title ;
1366
1374
document . querySelector ( ".chat-top-panel .convo-title" ) . innerText = "New Conversation" ;
@@ -1429,16 +1437,16 @@ const load_conversation = async (conversation, scroll=true) => {
1429
1437
let messages = conversation ?. items || [ ] ;
1430
1438
console . debug ( "Conversation:" , conversation . id )
1431
1439
1432
- let title = conversation . new_title || conversation . title ;
1433
- title = title ? `${ title } - G4F` : window . title ;
1440
+ let conversation_title = conversation . new_title || conversation . title ;
1441
+ title = conversation_title ? `${ conversation_title } - G4F` : window . title ;
1434
1442
if ( title ) {
1435
1443
document . title = title ;
1436
1444
}
1437
- const convoTitle = document . querySelector ( ".chat-top-panel .convo-title" ) ;
1445
+ const chatHeader = document . querySelector ( ".chat-top-panel .convo-title" ) ;
1438
1446
if ( window . share_id && conversation . id == window . start_id ) {
1439
- convoTitle . innerHTML = '<i class="fa-solid fa-qrcode"></i> ' + escapeHtml ( title ) ;
1447
+ chatHeader . innerHTML = '<i class="fa-solid fa-qrcode"></i> ' + escapeHtml ( conversation_title ) ;
1440
1448
} else {
1441
- convoTitle . innerText = title ;
1449
+ chatHeader . innerText = conversation_title ;
1442
1450
}
1443
1451
1444
1452
if ( chatPrompt ) {
@@ -2248,11 +2256,20 @@ window.addEventListener('load', async function() {
2248
2256
return await load_conversation ( JSON . parse ( appStorage . getItem ( `conversation:${ window . conversation_id } ` ) ) ) ;
2249
2257
}
2250
2258
let conversation = await response . json ( ) ;
2251
- if ( ! window . conversation_id || conversation . id == window . conversation_id ) {
2252
- window . conversation_id = conversation . id ;
2253
- await load_conversation ( conversation ) ;
2254
- await save_conversation ( window . conversation_id , JSON . stringify ( conversation ) ) ;
2259
+ if ( ! appStorage . getItem ( `conversation:${ window . conversation_id } ` ) || conversation . id == window . conversation_id ) {
2260
+ // Copy conversation from share
2261
+ if ( conversation . id != window . conversation_id ) {
2262
+ window . conversation_id = conversation . id ;
2263
+ conversation . updated = Date . now ( ) ;
2264
+ window . share_id = null ;
2265
+ }
2266
+ await load_conversation ( conversation ) ;
2267
+ await save_conversation ( conversation . id , JSON . stringify ( conversation ) ) ;
2255
2268
await load_conversations ( ) ;
2269
+ if ( ! window . share_id ) {
2270
+ // Continue after copy conversation
2271
+ return ;
2272
+ }
2256
2273
let refreshOnHide = true ;
2257
2274
document . addEventListener ( "visibilitychange" , ( ) => {
2258
2275
if ( document . hidden ) {
@@ -2261,6 +2278,7 @@ window.addEventListener('load', async function() {
2261
2278
refreshOnHide = true ;
2262
2279
}
2263
2280
} ) ;
2281
+ // Start chat mode (QRCode)
2264
2282
var refreshIntervalId = setInterval ( async ( ) => {
2265
2283
if ( ! window . share_id ) {
2266
2284
clearInterval ( refreshIntervalId ) ;
@@ -2297,29 +2315,6 @@ window.addEventListener('load', async function() {
2297
2315
await safe_load_conversation ( window . conversation_id , false ) ;
2298
2316
} ) ;
2299
2317
2300
- document . addEventListener ( "DOMContentLoaded" , function ( ) {
2301
- document . querySelectorAll ( ".collapsible-header" ) . forEach ( header => {
2302
- header . addEventListener ( "click" , function ( ) {
2303
- const content = this . nextElementSibling ;
2304
-
2305
- const isOpen = ! content . classList . contains ( "hidden" ) ;
2306
-
2307
- document . querySelectorAll ( ".collapsible-content" ) . forEach ( section => {
2308
- section . style . display = "none" ;
2309
- section . style . maxHeight = "0" ;
2310
- section . classList . add ( "hidden" ) ;
2311
- } ) ;
2312
-
2313
- if ( ! isOpen ) {
2314
- content . style . display = "block" ;
2315
- content . style . maxHeight = content . scrollHeight + "px" ;
2316
- content . classList . remove ( "hidden" ) ;
2317
- }
2318
- } ) ;
2319
- } ) ;
2320
- } ) ;
2321
-
2322
-
2323
2318
window . addEventListener ( 'DOMContentLoaded' , async function ( ) {
2324
2319
await on_load ( ) ;
2325
2320
if ( window . conversation_id == "{{conversation_id}}" ) {
@@ -2332,16 +2327,6 @@ window.addEventListener('DOMContentLoaded', async function() {
2332
2327
await loadAllProviders ( ) ;
2333
2328
} ) ;
2334
2329
2335
- // Also try loading when the settings panel is opened
2336
- document . addEventListener ( 'DOMContentLoaded' , function ( ) {
2337
- const settingsIcon = document . querySelector ( '.settings_icon' ) ;
2338
- if ( settingsIcon ) {
2339
- settingsIcon . addEventListener ( 'click' , function ( ) {
2340
- setTimeout ( loadAllProviders , 500 ) ;
2341
- } ) ;
2342
- }
2343
- } ) ;
2344
-
2345
2330
window . addEventListener ( 'pywebviewready' , async function ( ) {
2346
2331
await on_api ( ) ;
2347
2332
} ) ;
@@ -2350,22 +2335,17 @@ async function on_load() {
2350
2335
count_input ( ) ;
2351
2336
if ( / \/ s e t t i n g s \/ / . test ( window . location . href ) ) {
2352
2337
open_settings ( ) ;
2353
- } else if ( / \/ c h a t \/ s h a r e / . test ( window . location . href ) ) {
2338
+ } else if ( / \/ c h a t \/ ( s h a r e | \? | $ ) / . test ( window . location . href ) ) {
2354
2339
chatPrompt . value = document . getElementById ( "systemPrompt" ) ?. value || "" ;
2355
2340
let chat_url = new URL ( window . location . href )
2356
2341
let chat_params = new URLSearchParams ( chat_url . search ) ;
2357
2342
if ( chat_params . get ( "prompt" ) ) {
2358
2343
userInput . value = chat_params . get ( "prompt" ) ;
2359
2344
userInput . style . height = userInput . scrollHeight + "px" ;
2360
2345
userInput . focus ( ) ;
2361
- //await handle_ask();
2346
+ } else {
2347
+ new_conversation ( ) ;
2362
2348
}
2363
- } else if ( / \/ c h a t \/ [ ? $ ] / . test ( window . location . href ) ) {
2364
- chatPrompt . value = document . getElementById ( "systemPrompt" ) ?. value || "" ;
2365
- say_hello ( ) ;
2366
- } else if ( window . location . pathname === "/chat/" ) {
2367
- // When the URL is exactly "/chat/" with trailing slash, create a new conversation
2368
- await new_conversation ( ) ;
2369
2349
} else {
2370
2350
//load_conversation(window.conversation_id);
2371
2351
}
@@ -2414,7 +2394,6 @@ const load_provider_option = (input, provider_name) => {
2414
2394
async function on_api ( ) {
2415
2395
load_version ( ) ;
2416
2396
let prompt_lock = false ;
2417
- setupCollapsibleFields ( ) ;
2418
2397
userInput . addEventListener ( "keydown" , async ( evt ) => {
2419
2398
if ( prompt_lock ) return ;
2420
2399
// If not mobile and not shift enter
@@ -2550,23 +2529,8 @@ async function on_api() {
2550
2529
} ) ;
2551
2530
2552
2531
providersContainer . querySelector ( ".collapsible-header" ) . addEventListener ( 'click' , ( e ) => {
2553
- const header = providersContainer . querySelector ( ".collapsible-header" ) ;
2554
- const content = providersContainer . querySelector ( ".collapsible-content" ) ;
2555
-
2556
- header . classList . toggle ( 'active' ) ;
2557
- if ( content . classList . contains ( 'hidden' ) ) {
2558
- content . classList . remove ( 'hidden' ) ;
2559
- content . style . display = "block" ;
2560
- setTimeout ( ( ) => {
2561
- content . style . maxHeight = ( content . scrollHeight + 100 ) + "px" ;
2562
- } , 10 ) ;
2563
- } else {
2564
- content . style . maxHeight = "0" ;
2565
- setTimeout ( ( ) => {
2566
- content . classList . add ( 'hidden' ) ;
2567
- content . style . display = "none" ;
2568
- } , 300 ) ;
2569
- }
2532
+ providersContainer . querySelector ( ".collapsible-content" ) . classList . toggle ( 'hidden' ) ;
2533
+ providersContainer . querySelector ( ".collapsible-header" ) . classList . toggle ( 'active' ) ;
2570
2534
} ) ;
2571
2535
}
2572
2536
@@ -2610,23 +2574,8 @@ async function on_api() {
2610
2574
}
2611
2575
2612
2576
providersListContainer . querySelector ( ".collapsible-header" ) . addEventListener ( 'click' , ( e ) => {
2613
- const header = providersListContainer . querySelector ( ".collapsible-header" ) ;
2614
- const content = providersListContainer . querySelector ( ".collapsible-content" ) ;
2615
-
2616
- header . classList . toggle ( 'active' ) ;
2617
- if ( content . classList . contains ( 'hidden' ) ) {
2618
- content . classList . remove ( 'hidden' ) ;
2619
- content . style . display = "block" ;
2620
- setTimeout ( ( ) => {
2621
- content . style . maxHeight = ( content . scrollHeight + 100 ) + "px" ;
2622
- } , 10 ) ;
2623
- } else {
2624
- content . style . maxHeight = "0" ;
2625
- setTimeout ( ( ) => {
2626
- content . classList . add ( 'hidden' ) ;
2627
- content . style . display = "none" ;
2628
- } , 300 ) ;
2629
- }
2577
+ providersListContainer . querySelector ( ".collapsible-content" ) . classList . toggle ( 'hidden' ) ;
2578
+ providersListContainer . querySelector ( ".collapsible-header" ) . classList . toggle ( 'active' ) ;
2630
2579
} ) ;
2631
2580
2632
2581
register_settings_storage ( ) ;
@@ -2653,14 +2602,19 @@ async function on_api() {
2653
2602
2654
2603
// Handle checkbox change
2655
2604
hide_systemPrompt . addEventListener ( 'change' , async ( event ) => {
2656
- updateSystemPromptVisibility ( event . target . checked ) ;
2605
+ if ( event . target . checked ) {
2606
+ chatPrompt . classList . add ( "hidden" ) ;
2607
+ } else {
2608
+ chatPrompt . classList . remove ( "hidden" ) ;
2609
+ }
2657
2610
} ) ;
2658
2611
2659
2612
// Handle slide-header click
2660
2613
document . querySelector ( ".slide-header" ) ?. addEventListener ( "click" , ( ) => {
2661
- const isCurrentlyVisible = ! slide_systemPrompt_icon . classList . contains ( "fa-angles-down" ) ;
2662
- document . querySelector ( ".chat-top-panel" ) . classList [ isCurrentlyVisible ? "add" : "remove" ] ( "hidden" ) ;
2663
- updateSystemPromptVisibility ( isCurrentlyVisible ) ;
2614
+ const checked = slide_systemPrompt_icon . classList . contains ( "fa-angles-up" ) ;
2615
+ chatPrompt . classList [ checked ? "add" : "remove" ] ( "hidden" ) ;
2616
+ slide_systemPrompt_icon . classList [ checked ? "remove" : "add" ] ( "fa-angles-up" ) ;
2617
+ slide_systemPrompt_icon . classList [ checked ? "add" : "remove" ] ( "fa-angles-down" ) ;
2664
2618
} ) ;
2665
2619
const userInputHeight = document . getElementById ( "message-input-height" ) ;
2666
2620
if ( userInputHeight ) {
@@ -2819,7 +2773,7 @@ function connectToSSE(url, do_refine, bucket_id) {
2819
2773
} else if ( data . action == "media" ) {
2820
2774
inputCount . innerText = `File: ${ data . filename } ` ;
2821
2775
const url = `/files/${ bucket_id } /media/${ data . filename } ` ;
2822
- const media = [ { bucket_id : bucket_id , url : url } ] ;
2776
+ const media = [ { bucket_id : bucket_id , url : url , name : data . filename } ] ;
2823
2777
await handle_ask ( false , media ) ;
2824
2778
} else if ( data . action == "load" ) {
2825
2779
inputCount . innerText = `Read data: ${ formatFileSize ( data . size ) } ` ;
0 commit comments