@@ -88,6 +88,16 @@ CKEDITOR.plugins.add('openrouter', {
8888 return defaultModel ;
8989 }
9090
91+ // Recupera il prompt di sistema predefinito dalle impostazioni globali
92+ // Se non è definito, utilizza un prompt generico come fallback
93+ function getSystemPrompt ( ) {
94+ var defaultPrompt = 'Sei un assistente utile.' ; // Fallback predefinito
95+ if ( typeof globals !== 'undefined' && globals . AISystemPrompt && globals . AISystemPrompt . trim ( ) !== '' ) {
96+ defaultPrompt = globals . AISystemPrompt ;
97+ }
98+ return defaultPrompt ;
99+ }
100+
91101 // Gestisce la visualizzazione dell'indicatore di caricamento nel dialog
92102 // Mostra/nasconde l'animazione e gestisce gli stati dell'interfaccia utente
93103 function toggleLoadingIndicator ( dialog , show ) {
@@ -252,7 +262,7 @@ CKEDITOR.plugins.add('openrouter', {
252262
253263 // Effettua la chiamata API a OpenRouter
254264 // Gestisce l'invio del prompt, la configurazione della richiesta e il trattamento delle risposte
255- function callOpenRouterApi ( dialog , editor , prompt , selectedModel , selectedText , temperature , maxTokens ) {
265+ function callOpenRouterApi ( dialog , editor , systemPrompt , userPrompt , selectedText , selectedModel , temperature , maxTokens ) {
256266 // Verifica API Key
257267 if ( typeof globals === 'undefined' || ! globals . openRouterApiKey ) {
258268 if ( typeof toastr !== 'undefined' ) {
@@ -268,8 +278,8 @@ CKEDITOR.plugins.add('openrouter', {
268278 var requestBody = {
269279 model : selectedModel ,
270280 messages : [
271- { role : "system" , content : "Sei un assistente utile." } , // Puoi personalizzare il messaggio di sistema
272- { role : "user" , content : prompt + ( selectedText ? "\n\nRiscrivi o lavora su questo testo:\n" + selectedText : "" ) }
281+ { role : "system" , content : systemPrompt } , // Usa il prompt di sistema dal dialog
282+ { role : "user" , content : userPrompt + ( selectedText ? "\n\nRiscrivi o lavora su questo testo:\n" + selectedText : "" ) }
273283 ] ,
274284 temperature : parseFloat ( temperature ) , // Ensure temperature is a float
275285 max_tokens : parseInt ( maxTokens , 10 ) // Ensure max_tokens is an integer
@@ -316,7 +326,8 @@ CKEDITOR.plugins.add('openrouter', {
316326 // Definisce l'interfaccia utente, i campi di input e gestisce le interazioni dell'utente
317327 CKEDITOR . dialog . add ( 'openrouterDialog' , function ( editor ) {
318328 var defaultModel = getDefaultModel ( ) ; // Ottieni il modello predefinito
319- var lastUsedModel = getCookie ( 'ckeditorOpenRouterModel' ) || defaultModel ; // Leggi l'ultimo modello usato o usa il predefinito
329+ var defaultSystemPrompt = getSystemPrompt ( ) ; // Ottieni il prompt di sistema predefinito
330+ var lastUsedModel = getCookie ( 'ckeditorOpenRouterModel' ) || defaultModel ; // Letto all'inizio, ma setup lo sovrascrive
320331 var lastTemperature = getCookie ( 'ckeditorOpenRouterTemp' ) || '0.7' ; // Default temperature
321332 var lastMaxTokens = getCookie ( 'ckeditorOpenRouterTokens' ) || '1024' ; // Default max tokens
322333
@@ -349,6 +360,19 @@ CKEDITOR.plugins.add('openrouter', {
349360 '</div>' +
350361 '</div>'
351362 } ,
363+ {
364+ type : 'textarea' ,
365+ id : 'system_prompt' ,
366+ label : TRANSLATIONS . LABEL_SYSTEM_PROMPT , // Nuova label
367+ rows : 3 ,
368+ 'default' : defaultSystemPrompt ,
369+ setup : function ( ) {
370+ this . setValue ( defaultSystemPrompt ) ;
371+ } ,
372+ commit : function ( data ) {
373+ data . system_prompt = this . getValue ( ) ;
374+ }
375+ } ,
352376 {
353377 type : 'textarea' ,
354378 id : 'context' ,
@@ -359,7 +383,10 @@ CKEDITOR.plugins.add('openrouter', {
359383 var selectedText = selection ? selection . getSelectedText ( ) : '' ;
360384 this . setValue ( selectedText ) ;
361385 } ,
362- style : 'background-color: #f5f5f5;' // Sfondo grigio chiaro per distinguerlo
386+ // Rimosso lo style per renderlo editabile
387+ commit : function ( data ) {
388+ data . context = this . getValue ( ) ; // Recupera il contesto (potrebbe essere stato modificato)
389+ }
363390 } ,
364391 {
365392 type : 'textarea' ,
@@ -372,7 +399,7 @@ CKEDITOR.plugins.add('openrouter', {
372399 // Potresti pre-popolare il prompt se necessario
373400 } ,
374401 commit : function ( data ) {
375- data . prompt = this . getValue ( ) ;
402+ data . user_prompt = this . getValue ( ) ; // Rinominato per chiarezza
376403 }
377404 } ,
378405 {
@@ -394,15 +421,17 @@ CKEDITOR.plugins.add('openrouter', {
394421 [ 'Llama 3 8B Instruct (Meta)' , 'meta-llama/llama-3-8b-instruct' ]
395422 // Aggiungi altri modelli se necessario
396423 ] ,
397- 'default' : lastUsedModel , // Usa l'ultimo modello selezionato o il predefinito
424+ 'default' : lastUsedModel , // Valore predefinito iniziale
398425 setup : function ( ) {
399- // Leggi il cookie ogni volta che il dialog viene aperto
426+ // Leggi il cookie * ogni volta* che il dialog viene aperto
400427 var currentLastUsedModel = getCookie ( 'ckeditorOpenRouterModel' ) || defaultModel ;
428+ // Imposta il valore del dropdown basato sul cookie corrente
401429 this . setValue ( currentLastUsedModel ) ;
402430 } ,
403431 commit : function ( data ) {
432+ // Salva il valore selezionato nel cookie quando si preme OK
404433 data . model = this . getValue ( ) ;
405- setCookie ( 'ckeditorOpenRouterModel' , data . model , 30 ) ; // Salva il modello selezionato per 30 giorni
434+ setCookie ( 'ckeditorOpenRouterModel' , data . model , 30 ) ;
406435 }
407436 } ,
408437 {
@@ -414,13 +443,17 @@ CKEDITOR.plugins.add('openrouter', {
414443 html : '<div style="padding: 5px;">' +
415444 '<label style="display: block; margin-bottom: 5px;">' + TRANSLATIONS . LABEL_TEMPERATURE + ' <span id="tempValue">' + lastTemperature + '</span></label>' +
416445 '<input type="range" id="temperatureRange" min="0.1" max="1.0" step="0.1" value="' + lastTemperature + '" ' +
417- 'style="width: 100%;" oninput="document.getElementById(\'tempValue\').textContent = this.value"/>' +
446+ 'style="width: 100%;" oninput="document.getElementById(\'tempValue\').textContent = this.value"/>' + // Completed oninput
418447 '</div>' ,
419448 setup : function ( ) {
420449 var range = this . getElement ( ) . findOne ( 'input' ) ;
421450 if ( range ) {
422- range . $ . value = lastTemperature ;
423- range . $ . oninput ( ) ;
451+ // Leggi il cookie ogni volta che il dialog viene aperto
452+ var currentLastTemperature = getCookie ( 'ckeditorOpenRouterTemp' ) || '0.7' ;
453+ range . $ . value = currentLastTemperature ;
454+ // Aggiorna il valore visualizzato
455+ var span = this . getElement ( ) . findOne ( '#tempValue' ) ;
456+ if ( span ) span . setText ( currentLastTemperature ) ;
424457 }
425458 } ,
426459 commit : function ( data ) {
@@ -445,7 +478,9 @@ CKEDITOR.plugins.add('openrouter', {
445478 return true ;
446479 } ,
447480 setup : function ( ) {
448- this . setValue ( lastMaxTokens ) ;
481+ // Leggi il cookie ogni volta che il dialog viene aperto
482+ var currentLastMaxTokens = getCookie ( 'ckeditorOpenRouterTokens' ) || '1024' ;
483+ this . setValue ( currentLastMaxTokens ) ;
449484 } ,
450485 commit : function ( data ) {
451486 data . max_tokens = this . getValue ( ) ;
@@ -470,7 +505,7 @@ CKEDITOR.plugins.add('openrouter', {
470505 var dialog = this ;
471506 var selection = editor . getSelection ( ) ;
472507 this . selectedText = selection ? selection . getSelectedText ( ) : null ;
473-
508+
474509 // Forza il centramento del dialog
475510 var dialogElement = dialog . getElement ( ) ;
476511 dialogElement . setStyles ( {
@@ -480,102 +515,60 @@ CKEDITOR.plugins.add('openrouter', {
480515 'transform' : 'translate(-50%, -50%)' ,
481516 'margin' : '0'
482517 } ) ;
483-
484- // Leggi i cookie per le impostazioni più recenti
485- var currentLastUsedModel = getCookie ( 'ckeditorOpenRouterModel' ) || getDefaultModel ( ) ;
486- var currentTemperature = getCookie ( 'ckeditorOpenRouterTemp' ) || '0.7' ;
487- var currentMaxTokens = getCookie ( 'ckeditorOpenRouterTokens' ) || '1024' ;
488-
518+
489519 // Aggiorna il campo contesto
490520 var contextField = dialog . getContentElement ( 'tab-main' , 'context' ) ;
491521 if ( contextField ) {
492522 contextField . setValue ( this . selectedText || '' ) ;
493523 }
494-
495- // Aggiorna il modello selezionato
496- var modelField = dialog . getContentElement ( 'tab-main' , 'model' ) ;
497- if ( modelField ) {
498- modelField . setValue ( currentLastUsedModel ) ;
499- }
500-
501- // Aggiorna il valore della temperatura
502- var tempElement = dialogElement . findOne ( '#temperatureRange' ) ;
503- var tempValueElement = dialogElement . findOne ( '#tempValue' ) ;
504- if ( tempElement && tempValueElement ) {
505- tempElement . $ . value = currentTemperature ;
506- tempValueElement . setHtml ( currentTemperature ) ;
507- }
508-
509- // Aggiorna il valore dei max tokens
510- var maxTokensField = dialog . getContentElement ( 'tab-main' , 'max_tokens' ) ;
511- if ( maxTokensField ) {
512- maxTokensField . setValue ( currentMaxTokens ) ;
513- }
514-
515- toggleLoadingIndicator ( this , false ) ;
516-
517- // Verifica se l'API Key è configurata
518- var apiKeyConfigured = typeof globals !== 'undefined' && globals . openRouterApiKey ;
519-
520- // Trova il pulsante OK
521- var okButton = this . getButton ( 'ok' ) ;
522-
523- // Trova il container dell'avviso
524- var warningContainer = dialogElement . findOne ( '#api-key-warning-container' ) ;
525-
526- if ( ! apiKeyConfigured ) {
527- // Disabilita il pulsante OK ma mantieni visibile il pulsante di chiusura
528- if ( okButton ) {
529- okButton . disable ( ) ;
530- }
531-
532- // Mostra l'avviso
533- if ( warningContainer ) {
534- warningContainer . setStyle ( 'display' , 'flex' ) ;
535- }
524+
525+ // Mostra/nascondi l'avviso API Key
526+ var apiKeyWarningContainer = dialogElement . findOne ( '#api-key-warning-container' ) ;
527+ var mainContents = dialogElement . findOne ( '.cke_dialog_contents_body' ) ; // Selettore corretto
528+ var footer = dialogElement . findOne ( '.cke_dialog_footer' ) ;
529+
530+ if ( typeof globals === 'undefined' || ! globals . openRouterApiKey ) {
531+ if ( apiKeyWarningContainer ) apiKeyWarningContainer . setStyle ( 'display' , 'flex' ) ;
532+ if ( mainContents ) mainContents . setStyle ( 'visibility' , 'hidden' ) ; // Nascondi contenuti principali
533+ if ( footer ) footer . setStyle ( 'visibility' , 'hidden' ) ; // Nascondi footer
536534 } else {
537- // Abilita il pulsante OK
538- if ( okButton ) {
539- okButton . enable ( ) ;
540- }
541-
542- // Nascondi l'avviso
543- if ( warningContainer ) {
544- warningContainer . setStyle ( 'display' , 'none' ) ;
545- }
535+ if ( apiKeyWarningContainer ) apiKeyWarningContainer . setStyle ( 'display' , 'none' ) ;
536+ if ( mainContents ) mainContents . setStyle ( 'visibility' , 'visible' ) ; // Mostra contenuti principali
537+ if ( footer ) footer . setStyle ( 'visibility' , 'visible' ) ; // Mostra footer
546538 }
539+
540+ toggleLoadingIndicator ( this , false ) ;
547541 } ,
548542 onOk : function ( ) {
549543 var dialog = this ;
550544 var data = { } ;
551545 dialog . commitContent ( data ) ; // Raccoglie i dati dagli elementi del form
552546
553- // Recupera il testo selezionato (potrebbe essere cambiato)
554- var selection = editor . getSelection ( ) ;
555- var selectedText = selection ? selection . getSelectedText ( ) : null ;
547+ // Usa il contesto dal campo 'context' se presente, altrimenti il testo selezionato originale
548+ var contextText = data . context || this . selectedText ;
556549
557- // Chiama la funzione API
558- callOpenRouterApi ( dialog , editor , data . prompt , data . model , selectedText , data . temperature , data . max_tokens ) ;
550+ // Chiama la funzione API con il prompt di sistema
551+ callOpenRouterApi ( dialog , editor , data . system_prompt , data . user_prompt , contextText , data . model , data . temperature , data . max_tokens ) ;
559552
560553 // Impedisci la chiusura automatica del dialog; la chiusura avverrà in handleApiResponse
561554 return false ;
562555 }
563556 } ;
564557 } ) ;
565558
566- // Configurazione del comando e del pulsante nella toolbar dell'editor
559+ // Configurazione del comando e del pulsante nella toolbar
567560 editor . addCommand ( 'openrouterDialogCmd' , new CKEDITOR . dialogCommand ( 'openrouterDialog' ) ) ;
568561
569562 editor . ui . addButton ( 'OpenRouter' , {
570563 label : TRANSLATIONS . DIALOG_TITLE ,
571564 command : 'openrouterDialogCmd' ,
572565 toolbar : 'insert' ,
573- icon : this . path + 'icons/openrouter.png' // Percorso all 'icona del plugin
566+ icon : this . path + 'icons/openrouter.png' // Assicurati che l 'icona esista in questa posizione
574567 } ) ;
575568
576569 // Log di debug per confermare il corretto caricamento del plugin
577570 if ( typeof globals !== 'undefined' && globals . debug ) {
578571 console . log ( TRANSLATIONS . PLUGIN_LOADED ) ;
579572 }
580573 }
581- } ) ;
574+ } ) ;
0 commit comments