From 073d3dad1e69e716e1966d71c0a112fd63564626 Mon Sep 17 00:00:00 2001 From: DFFM-maker Date: Thu, 25 Dec 2025 19:00:13 +0100 Subject: [PATCH] moficica app.py --- .../062ab602-b92a-4799-aea5-a3a779f70f18.txt | 310 ------------------ .../69fae64f-b4b8-465e-b4ef-83e6bcf21fbe.txt | 109 ++++++ .../1d8acaad-0cea-45f6-998b-950a78140b01.txt | 109 ++++++ __pycache__/app.cpython-310.pyc | Bin 4402 -> 4424 bytes app.py | 243 +++++++------- workspaces/admin/error.txt | 109 ++++++ 6 files changed, 439 insertions(+), 441 deletions(-) delete mode 100644 .files/4a7c6ca1-4b01-4121-854f-a260fbd4e911/062ab602-b92a-4799-aea5-a3a779f70f18.txt create mode 100644 .files/7b4a28f6-17ec-4557-aa7a-eb5a68c4f8b7/69fae64f-b4b8-465e-b4ef-83e6bcf21fbe.txt create mode 100644 .files/fb8f5a48-1655-49e4-b9dd-238f9a463b25/1d8acaad-0cea-45f6-998b-950a78140b01.txt create mode 100644 workspaces/admin/error.txt diff --git a/.files/4a7c6ca1-4b01-4121-854f-a260fbd4e911/062ab602-b92a-4799-aea5-a3a779f70f18.txt b/.files/4a7c6ca1-4b01-4121-854f-a260fbd4e911/062ab602-b92a-4799-aea5-a3a779f70f18.txt deleted file mode 100644 index 0c1ca348..00000000 --- a/.files/4a7c6ca1-4b01-4121-854f-a260fbd4e911/062ab602-b92a-4799-aea5-a3a779f70f18.txt +++ /dev/null @@ -1,310 +0,0 @@ -VAR Internal -Idecon FB_IDECON_Client False False -Enable BOOL True False False -MsgFilter UINT 17 False False -fbTcpConnect SktTCPConnect False False -fbSocketSend SktTCPSend False False -fbSocketRcv SktTCPRcv False False -fbClose SktClose False False -Config _sSOCKET_ADDRESS (PortNo := 50000, IpAdr := '172.16.224.200') False False -SocketID_Int _sSOCKET False False -tmpIpAddress STRING[256] False False -tmpPort UINT False False -Connected BOOL False False -bConnectDone BOOL False False -bConnectBusy BOOL False False -bConnectError BOOL False False -wConnectErrorID WORD False False -bSendDone BOOL False False -bSendBusy BOOL False False -bSendError BOOL False False -wSendErrorID WORD False False -bRcvDone BOOL False False -bRcvBusy BOOL False False -bRcvError BOOL False False -wRcvErrorID WORD False False -RxChunk ARRAY[0..511] OF BYTE False False -RxChunkLen UINT False False -RxChunkValid BOOL False False -RcvTimeout UINT 50 False False -TxConsumed BOOL False False -prevTxOutValid BOOL False False -sendTrigger BOOL False False -Command STRING[255] False False -CommandTrigger BOOL False False -prevCommandTrigger BOOL False False -tonPollingSTATSV TON False False -tonPollingSTATP TON False False -prevPollSVQ BOOL False False -prevPollPQ BOOL False False -pollResetSV BOOL False False -pollResetP BOOL False False -prevLastMessage STRING[512] False False -newMsg BOOL False False -newWeightMsg BOOL False False -prevWeightMsg BOOL False False -totalRejected DINT False False -TxChunk ARRAY[0..511] OF BYTE False False -TxChunkLen UINT False False -iTx UINT False False -TxBuffer ARRAY[0..511] OF BYTE False False -tmpWeightData Idecon_WeightData False False -tmpStatPData Idecon_StatPData False False -rTrigRcv R_TRIG False False -bExecuteRcv BOOL False False -iRcvState INT False False - -END VAR - -VAR External -G_Idecon_Config False -G_System_Error False -G_ProgressiveID False -G_OPCUA_Idecon False -G_Idecon_StatP False -G_Idecon_LastWeight False -G_Idecon_LastSTATSV False -G_Idecon_WorkMode False -G_Idecon_BatchCode False -G_Idecon_ProductionOrder False -END VAR - -// ---------- Program ProcessIdecon---------------// - - -IF NOT Enable THEN - Connected := FALSE; - RxChunkLen := 0; - RxChunkValid := FALSE; - TxConsumed := FALSE; - Command := ''; - CommandTrigger := FALSE; - prevCommandTrigger := FALSE; - prevTxOutValid := FALSE; - pollResetSV := FALSE; - pollResetP := FALSE; -END_IF; - -tmpIpAddress := Config.IpAdr; -tmpPort := Config.PortNo; - -fbTcpConnect( - Execute := (Enable AND NOT Connected), - DstAdr := tmpIpAddress, // Passiamo la variabile semplice (FIX per "Member of structure") - DstTcpPort := tmpPort, // Passiamo la variabile semplice - SrcTcpPort := 0, // (Opzionale) 0 = Assegnazione automatica porta locale - Socket => SocketID_Int, - Done => bConnectDone, - Busy => bConnectBusy, - Error => bConnectError, - ErrorID => wConnectErrorID -); -IF bConnectDone THEN - Connected := TRUE; -END_IF; - -IF bConnectError THEN - Connected := FALSE; -END_IF; - -// --------------------------------------------------------- -// MACCHINA A STATI "INFINITY LOOP" (Per lettura continua) -// --------------------------------------------------------- - -CASE iRcvState OF - 0: // STATO IDLE: Pronti a ricevere? - // Se siamo connessi e il blocco non è occupato o in errore... - IF Connected AND NOT bRcvBusy AND NOT bRcvError THEN - bExecuteRcv := TRUE; // ALZA IL FRONTE DI SALITA - iRcvState := 10; // Passa in attesa - END_IF; - - 10: // STATO WAIT: Aspetta i dati - IF bRcvDone THEN - // Dati ricevuti! Andiamo a resettare - iRcvState := 20; - ELSIF bRcvError OR NOT Connected THEN - // Se cade la connessione o c'è errore socket - iRcvState := 99; - END_IF; - - 20: // STATO RESET (FONDAMENTALE PER LEGGERE IL SUCCESSIVO) - bExecuteRcv := FALSE; // Abbassa Execute - - // Aspettiamo che il blocco confermi di essersi spento (Done deve tornare False) - // Senza questo check, il riarmo è troppo veloce e fallisce! - IF NOT bRcvDone AND NOT bRcvBusy THEN - iRcvState := 0; // Torna all'inizio per leggere il prossimo - END_IF; - - 99: // STATO ERRORE - bExecuteRcv := FALSE; - // Se l'errore sparisce o la connessione cade, resetta la macchina - IF NOT bRcvError THEN - iRcvState := 0; - END_IF; -END_CASE; - -// --- Chiamata al Blocco (Fuori dal CASE) --- -fbSocketRcv( - Execute := bExecuteRcv, - Socket := SocketID_Int, - TimeOut := 1, //RcvTimeout, // Assicurati sia basso (es. 10 o 50 ms) - Size := UINT#512, - RcvDat := RxChunk[0], - Done => bRcvDone, - Busy => bRcvBusy, - Error => bRcvError, - ErrorID => wRcvErrorID, - RcvSize => RxChunkLen -); - -rTrigRcv(CLK := bRcvDone); -RxChunkValid := rTrigRcv.Q AND (RxChunkLen > 0); - -// Gestione disconnessione su errore grave -IF bRcvError THEN - Connected := FALSE; - iRcvState := 99; -END_IF; - -tonPollingSTATSV(IN := (Enable AND Connected AND NOT pollResetSV), PT := T#2S); -tonPollingSTATP(IN := (Enable AND Connected AND NOT pollResetP), PT := T#5S); - -pollResetSV := FALSE; -pollResetP := FALSE; - -CommandTrigger := FALSE; -IF tonPollingSTATSV.Q AND NOT prevPollSVQ THEN - Command := 'STATSV'; - CommandTrigger := TRUE; - pollResetSV := TRUE; -ELSIF tonPollingSTATP.Q AND NOT prevPollPQ THEN - Command := 'STATREQ'; - CommandTrigger := TRUE; - pollResetP := TRUE; -END_IF; -prevPollSVQ := tonPollingSTATSV.Q; -prevPollPQ := tonPollingSTATP.Q; - -Idecon( - Enable := Enable, - MsgFilter := MsgFilter, - RxIn := RxChunk, - RxInLen := RxChunkLen, - RxInValid := RxChunkValid, - TxConsumed := TxConsumed, - Command := Command, - CommandTrigger := CommandTrigger -); -tmpWeightData := Idecon.Weight; -tmpStatPData := Idecon.StatP; -// Gestione Messaggi -newMsg := (Idecon.LastMessage <> prevLastMessage); -prevLastMessage := Idecon.LastMessage; - -newWeightMsg := newMsg AND IDECON_StartsWith(Idecon.LastMessage, 'WEIGHT='); - -IF newWeightMsg AND NOT prevWeightMsg THEN - G_ProgressiveID := G_ProgressiveID + 1; -END_IF; -prevWeightMsg := newWeightMsg; -G_Idecon_LastSTATSV := Idecon.LastSTATSV; - - -IF tmpWeightData.Valid THEN - G_Idecon_LastWeight.DateTimeText := tmpWeightData.DateTimeText; - G_Idecon_LastWeight.ProductionOrder := tmpWeightData.ProductionOrder; - G_Idecon_LastWeight.BatchCode := tmpWeightData.BatchCode; - G_Idecon_LastWeight.RecipeName := tmpWeightData.RecipeName; - G_Idecon_LastWeight.LineCode := tmpWeightData.LineCode; - G_Idecon_LastWeight.SerialNumber := tmpWeightData.SerialNumber; - G_Idecon_LastWeight.WeightMg := tmpWeightData.WeightMg; - G_Idecon_LastWeight.DeltaToNominalMg:= tmpWeightData.DeltaToNominalMg; - G_Idecon_LastWeight.Classification := tmpWeightData.Classification; - G_Idecon_LastWeight.Valid := TRUE; -END_IF; - -// --- E anche qui usiamo tmpStatPData --- -IF tmpStatPData.Valid THEN - G_Idecon_StatP.TotalProducts := tmpStatPData.TotalProducts; - G_Idecon_StatP.TotalAccepted := tmpStatPData.TotalAccepted; - G_Idecon_StatP.RejectedMinus := tmpStatPData.RejectedMinus; - G_Idecon_StatP.RejectedMinusMinus := tmpStatPData.RejectedMinusMinus; - G_Idecon_StatP.RejectedPlus := tmpStatPData.RejectedPlus; - G_Idecon_StatP.RejectedPlusPlus := tmpStatPData.RejectedPlusPlus; - G_Idecon_StatP.CannotBeWeighed := tmpStatPData.CannotBeWeighed; - G_Idecon_StatP.MetalCategory := tmpStatPData.MetalCategory; - G_Idecon_StatP.Valid := TRUE; -END_IF; - - -G_System_Error := bConnectError OR bSendError OR bRcvError OR Idecon.Error; - -IF G_Idecon_StatP.Valid AND (G_Idecon_StatP.TotalProducts >= G_Idecon_StatP.TotalAccepted) THEN - totalRejected := G_Idecon_StatP.TotalProducts - G_Idecon_StatP.TotalAccepted; -ELSE - totalRejected := 0; -END_IF; - -// OPC UA Mapping -G_OPCUA_Idecon.Connected := Connected; -G_OPCUA_Idecon.WorkMode := G_Idecon_WorkMode; -G_OPCUA_Idecon.LastSTATSV := G_Idecon_LastSTATSV; -G_OPCUA_Idecon.BatchCode := G_Idecon_BatchCode; -G_OPCUA_Idecon.ProductionOrder := G_Idecon_ProductionOrder; -G_OPCUA_Idecon.Error := G_System_Error; -G_OPCUA_Idecon.ErrorId := UINT_TO_UDINT(WORD_TO_UINT(wConnectErrorID)); - -G_OPCUA_Idecon.LastWeight.ProgressiveId := G_ProgressiveID; -G_OPCUA_Idecon.LastWeight.TimestampText := G_Idecon_LastWeight.DateTimeText; -G_OPCUA_Idecon.LastWeight.ProductionOrder := G_Idecon_LastWeight.ProductionOrder; -G_OPCUA_Idecon.LastWeight.BatchCode := G_Idecon_LastWeight.BatchCode; -G_OPCUA_Idecon.LastWeight.RecipeName := G_Idecon_LastWeight.RecipeName; -G_OPCUA_Idecon.LastWeight.LineCode := G_Idecon_LastWeight.LineCode; -G_OPCUA_Idecon.LastWeight.SerialNumber := G_Idecon_LastWeight.SerialNumber; -G_OPCUA_Idecon.LastWeight.Weight_g := DINT_TO_LREAL(G_Idecon_LastWeight.WeightMg) / LREAL#1000.0; -G_OPCUA_Idecon.LastWeight.Delta_g := DINT_TO_LREAL(G_Idecon_LastWeight.DeltaToNominalMg) / LREAL#1000.0; -G_OPCUA_Idecon.LastWeight.Classification := G_Idecon_LastWeight.Classification; -G_OPCUA_Idecon.LastWeight.Expelled := FALSE; -G_OPCUA_Idecon.LastWeight.Valid := G_Idecon_LastWeight.Valid; - -G_OPCUA_Idecon.Stats.TotalProducts := G_Idecon_StatP.TotalProducts; -G_OPCUA_Idecon.Stats.TotalAccepted := G_Idecon_StatP.TotalAccepted; -G_OPCUA_Idecon.Stats.TotalRejected := totalRejected; -G_OPCUA_Idecon.Stats.RejectedPlusPlus := G_Idecon_StatP.RejectedPlusPlus; -G_OPCUA_Idecon.Stats.CannotBeWeighed := G_Idecon_StatP.CannotBeWeighed; -G_OPCUA_Idecon.Stats.MetalCategory := G_Idecon_StatP.MetalCategory; -G_OPCUA_Idecon.Stats.Valid := G_Idecon_StatP.Valid; - -// Invio Socket -sendTrigger := (Idecon.TxOutValid AND NOT prevTxOutValid); -prevTxOutValid := Idecon.TxOutValid; - -IF sendTrigger THEN - // Copiamo i dati dall'output dell'FB al buffer LOCALE - // Questo evita l'errore "Member of function block instance variable" - TxBuffer := Idecon.TxOut; -END_IF; - -fbSocketSend( - Execute := (Enable AND Connected AND sendTrigger), - Socket := SocketID_Int, - SendDat := TxBuffer[0], // --- CORREZIONE 2: Usa buffer locale --- - Size := Idecon.TxOutLen, - Done => bSendDone, - Busy => bSendBusy, - Error => bSendError, - ErrorID => wSendErrorID -); - -TxConsumed := bSendDone; - -IF bSendError THEN - Connected := FALSE; -END_IF; - -fbClose( - Execute := (Enable AND NOT Connected), - Socket := SocketID_Int -); diff --git a/.files/7b4a28f6-17ec-4557-aa7a-eb5a68c4f8b7/69fae64f-b4b8-465e-b4ef-83e6bcf21fbe.txt b/.files/7b4a28f6-17ec-4557-aa7a-eb5a68c4f8b7/69fae64f-b4b8-465e-b4ef-83e6bcf21fbe.txt new file mode 100644 index 00000000..334bbef9 --- /dev/null +++ b/.files/7b4a28f6-17ec-4557-aa7a-eb5a68c4f8b7/69fae64f-b4b8-465e-b4ef-83e6bcf21fbe.txt @@ -0,0 +1,109 @@ +chainlit-app-1 | 2025-12-25 17:32:55 - INFO - chainlit - Your app is available at http://0.0.0.0:8000 +chainlit-app-1 | 2025-12-25 17:36:13 - WARNING - chainlit - Translation file for it-IT not found. Using parent translation it. +chainlit-app-1 | 2025-12-25 17:36:13 - WARNING - chainlit - Translated markdown file for it-IT not found. Defaulting to chainlit.md. +chainlit-app-1 | 2025-12-25 17:36:13 - INFO - chainlit - Missing custom logo. Falling back to default logo. +chainlit-app-1 | 2025-12-25 17:39:46 - WARNING - chainlit - Translation file for it-IT not found. Using parent translation it. +chainlit-app-1 | 2025-12-25 17:39:47 - WARNING - chainlit - Translation file for it-IT not found. Using parent translation it. +chainlit-app-1 | 2025-12-25 17:39:47 - WARNING - chainlit - Translated markdown file for it-IT not found. Defaulting to chainlit.md. +chainlit-app-1 | 2025-12-25 17:39:51 - WARNING - chainlit - Translation file for it-IT not found. Using parent translation it. +chainlit-app-1 | 2025-12-25 17:39:56 - INFO - httpx - HTTP Request: POST http://192.168.1.243:11434/api/chat "HTTP/1.1 200 OK" +qdrant-1 | _ _ +qdrant-1 | __ _ __| |_ __ __ _ _ __ | |_ +qdrant-1 | / _` |/ _` | '__/ _` | '_ \| __| +qdrant-1 | | (_| | (_| | | | (_| | | | | |_ +qdrant-1 | \__, |\__,_|_| \__,_|_| |_|\__| +qdrant-1 | |_| +qdrant-1 | +qdrant-1 | Access web UI at https://ui.qdrant.tech/?v=v1.0.0 +qdrant-1 | +qdrant-1 | [2025-12-25T16:38:00.816Z INFO storage::content_manager::consensus::persistent] Initializing new raft state at ./storage/raft_state +qdrant-1 | [2025-12-25T16:38:00.861Z INFO qdrant] Distributed mode disabled +qdrant-1 | [2025-12-25T16:38:00.861Z INFO qdrant] Telemetry reporting enabled, id: e6113e43-627c-471d-8374-0f1b61799d76 +qdrant-1 | [2025-12-25T16:38:00.872Z INFO qdrant::tonic] Qdrant gRPC listening on 6334 +qdrant-1 | [2025-12-25T16:38:00.890Z INFO actix_server::builder] Starting 3 workers +qdrant-1 | [2025-12-25T16:38:00.890Z INFO actix_server::server] Actix runtime found; starting in Actix runtime +qdrant-1 | [2025-12-25T16:39:02.504Z INFO actix_server::server] SIGTERM received; starting graceful shutdown +qdrant-1 | [2025-12-25T16:39:02.505Z INFO actix_server::worker] Shutting down idle worker +qdrant-1 | [2025-12-25T16:39:02.508Z INFO actix_server::accept] Accept thread stopped +qdrant-1 | [2025-12-25T16:39:02.508Z INFO actix_server::worker] Shutting down idle worker +qdrant-1 | [2025-12-25T16:39:02.508Z INFO actix_server::worker] Shutting down idle worker +qdrant-1 | _ _ +qdrant-1 | __ _ __| |_ __ __ _ _ __ | |_ +qdrant-1 | / _` |/ _` | '__/ _` | '_ \| __| +qdrant-1 | | (_| | (_| | | | (_| | | | | |_ +qdrant-1 | \__, |\__,_|_| \__,_|_| |_|\__| +qdrant-1 | |_| +qdrant-1 | +qdrant-1 | Access web UI at https://ui.qdrant.tech/?v=v1.0.0 +qdrant-1 | +qdrant-1 | [2025-12-25T16:43:53.592Z INFO storage::content_manager::consensus::persistent] Loading raft state from ./storage/raft_state +qdrant-1 | [2025-12-25T16:43:53.612Z INFO qdrant] Distributed mode disabled +qdrant-1 | [2025-12-25T16:43:53.612Z INFO qdrant] Telemetry reporting enabled, id: 2a83356a-9770-47d3-a0bd-638f75769522 +qdrant-1 | [2025-12-25T16:43:53.615Z INFO qdrant::tonic] Qdrant gRPC listening on 6334 +qdrant-1 | [2025-12-25T16:43:53.616Z INFO actix_server::builder] Starting 3 workers +qdrant-1 | [2025-12-25T16:43:53.617Z INFO actix_server::server] Actix runtime found; starting in Actix runtime +qdrant-1 | [2025-12-25T16:56:42.005Z INFO actix_server::server] SIGTERM received; starting graceful shutdown +qdrant-1 | [2025-12-25T16:56:42.006Z INFO actix_server::worker] Shutting down idle worker +qdrant-1 | [2025-12-25T16:56:42.006Z INFO actix_server::worker] Shutting down idle worker +qdrant-1 | [2025-12-25T16:56:42.007Z INFO actix_server::worker] Shutting down idle worker +qdrant-1 | [2025-12-25T16:56:42.007Z INFO actix_server::accept] Accept thread stopped +qdrant-1 | _ _ +qdrant-1 | __ _ __| |_ __ __ _ _ __ | |_ +qdrant-1 | / _` |/ _` | '__/ _` | '_ \| __| +qdrant-1 | | (_| | (_| | | | (_| | | | | |_ +qdrant-1 | \__, |\__,_|_| \__,_|_| |_|\__| +qdrant-1 | |_| +qdrant-1 | +qdrant-1 | Access web UI at https://ui.qdrant.tech/?v=v1.0.0 +qdrant-1 | +qdrant-1 | [2025-12-25T16:56:52.790Z INFO storage::content_manager::consensus::persistent] Loading raft state from ./storage/raft_state +qdrant-1 | [2025-12-25T16:56:52.796Z INFO qdrant] Distributed mode disabled +qdrant-1 | [2025-12-25T16:56:52.796Z INFO qdrant] Telemetry reporting enabled, id: f821b8ea-9ee5-497e-a172-dfebf253f7b1 +qdrant-1 | [2025-12-25T16:56:52.797Z INFO qdrant::tonic] Qdrant gRPC listening on 6334 +qdrant-1 | [2025-12-25T16:56:52.798Z INFO actix_server::builder] Starting 3 workers +qdrant-1 | [2025-12-25T16:56:52.798Z INFO actix_server::server] Actix runtime found; starting in Actix runtime +postgres-1 | +postgres-1 | PostgreSQL Database directory appears to contain a database; Skipping initialization +postgres-1 | +postgres-1 | 2025-12-25 16:38:01.071 UTC [1] LOG: starting PostgreSQL 18.1 (Debian 18.1-1.pgdg13+2) on x86_64-pc-linux-gnu, compiled by gcc (Debian 14.2.0-19) 14.2.0, 64-bit +postgres-1 | 2025-12-25 16:38:01.072 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432 +postgres-1 | 2025-12-25 16:38:01.072 UTC [1] LOG: listening on IPv6 address "::", port 5432 +postgres-1 | 2025-12-25 16:38:01.093 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432" +postgres-1 | 2025-12-25 16:38:01.126 UTC [32] LOG: database system was shut down at 2025-12-25 14:34:55 UTC +postgres-1 | 2025-12-25 16:38:01.155 UTC [1] LOG: database system is ready to accept connections +postgres-1 | 2025-12-25 16:39:02.495 UTC [1] LOG: received fast shutdown request +postgres-1 | 2025-12-25 16:39:02.505 UTC [1] LOG: aborting any active transactions +postgres-1 | 2025-12-25 16:39:02.521 UTC [1] LOG: background worker "logical replication launcher" (PID 35) exited with exit code 1 +postgres-1 | 2025-12-25 16:39:02.521 UTC [30] LOG: shutting down +postgres-1 | 2025-12-25 16:39:02.533 UTC [30] LOG: checkpoint starting: shutdown immediate +postgres-1 | 2025-12-25 16:39:02.601 UTC [30] LOG: checkpoint complete: wrote 0 buffers (0.0%), wrote 3 SLRU buffers; 0 WAL file(s) added, 0 removed, 0 recycled; write=0.019 s, sync=0.009 s, total=0.079 s; sync files=2, longest=0.005 s, average=0.005 s; distance=0 kB, estimate=0 kB; lsn=0/1BEF980, redo lsn=0/1BEF980 +postgres-1 | 2025-12-25 16:39:02.644 UTC [1] LOG: database system is shut down +postgres-1 | +postgres-1 | PostgreSQL Database directory appears to contain a database; Skipping initialization +postgres-1 | +postgres-1 | 2025-12-25 16:43:53.946 UTC [1] LOG: starting PostgreSQL 18.1 (Debian 18.1-1.pgdg13+2) on x86_64-pc-linux-gnu, compiled by gcc (Debian 14.2.0-19) 14.2.0, 64-bit +postgres-1 | 2025-12-25 16:43:53.947 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432 +postgres-1 | 2025-12-25 16:43:53.947 UTC [1] LOG: listening on IPv6 address "::", port 5432 +postgres-1 | 2025-12-25 16:43:53.965 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432" +postgres-1 | 2025-12-25 16:43:53.990 UTC [32] LOG: database system was shut down at 2025-12-25 16:39:02 UTC +postgres-1 | 2025-12-25 16:43:54.013 UTC [1] LOG: database system is ready to accept connections +postgres-1 | 2025-12-25 16:48:54.089 UTC [30] LOG: checkpoint starting: time +postgres-1 | 2025-12-25 16:48:54.175 UTC [30] LOG: checkpoint complete: wrote 0 buffers (0.0%), wrote 3 SLRU buffers; 0 WAL file(s) added, 0 removed, 0 recycled; write=0.036 s, sync=0.009 s, total=0.088 s; sync files=2, longest=0.005 s, average=0.005 s; distance=0 kB, estimate=0 kB; lsn=0/1BEFA88, redo lsn=0/1BEFA30 +postgres-1 | 2025-12-25 16:56:42.002 UTC [1] LOG: received fast shutdown request +postgres-1 | 2025-12-25 16:56:42.018 UTC [1] LOG: aborting any active transactions +postgres-1 | 2025-12-25 16:56:42.026 UTC [1] LOG: background worker "logical replication launcher" (PID 35) exited with exit code 1 +postgres-1 | 2025-12-25 16:56:42.030 UTC [30] LOG: shutting down +postgres-1 | 2025-12-25 16:56:42.039 UTC [30] LOG: checkpoint starting: shutdown immediate +postgres-1 | 2025-12-25 16:56:42.086 UTC [30] LOG: checkpoint complete: wrote 0 buffers (0.0%), wrote 0 SLRU buffers; 0 WAL file(s) added, 0 removed, 0 recycled; write=0.004 s, sync=0.001 s, total=0.057 s; sync files=0, longest=0.000 s, average=0.000 s; distance=0 kB, estimate=0 kB; lsn=0/1BEFB38, redo lsn=0/1BEFB38 +postgres-1 | 2025-12-25 16:56:42.131 UTC [1] LOG: database system is shut down +postgres-1 | +postgres-1 | PostgreSQL Database directory appears to contain a database; Skipping initialization +postgres-1 | +postgres-1 | 2025-12-25 16:56:43.530 UTC [1] LOG: starting PostgreSQL 18.1 (Debian 18.1-1.pgdg13+2) on x86_64-pc-linux-gnu, compiled by gcc (Debian 14.2.0-19) 14.2.0, 64-bit +postgres-1 | 2025-12-25 16:56:43.532 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432 +postgres-1 | 2025-12-25 16:56:43.532 UTC [1] LOG: listening on IPv6 address "::", port 5432 +postgres-1 | 2025-12-25 16:56:43.552 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432" +postgres-1 | 2025-12-25 16:56:43.585 UTC [32] LOG: database system was shut down at 2025-12-25 16:56:42 UTC +postgres-1 | 2025-12-25 16:56:43.616 UTC [1] LOG: database system is ready to accept connections +postgres-1 | 2025-12-25 17:01:43.645 UTC [30] LOG: checkpoint starting: time +postgres-1 | 2025-12-25 17:01:43.712 UTC [30] LOG: checkpoint complete: wrote 0 buffers (0.0%), wrote 3 SLRU buffers; 0 WAL file(s) added, 0 removed, 0 recycled; write=0.019 s, sync=0.009 s, total=0.068 s; sync files=2, longest=0.005 s, average=0.005 s; distance=0 kB, estimate=0 kB; lsn=0/1BEFC40, redo lsn=0/1BEFBE8 diff --git a/.files/fb8f5a48-1655-49e4-b9dd-238f9a463b25/1d8acaad-0cea-45f6-998b-950a78140b01.txt b/.files/fb8f5a48-1655-49e4-b9dd-238f9a463b25/1d8acaad-0cea-45f6-998b-950a78140b01.txt new file mode 100644 index 00000000..334bbef9 --- /dev/null +++ b/.files/fb8f5a48-1655-49e4-b9dd-238f9a463b25/1d8acaad-0cea-45f6-998b-950a78140b01.txt @@ -0,0 +1,109 @@ +chainlit-app-1 | 2025-12-25 17:32:55 - INFO - chainlit - Your app is available at http://0.0.0.0:8000 +chainlit-app-1 | 2025-12-25 17:36:13 - WARNING - chainlit - Translation file for it-IT not found. Using parent translation it. +chainlit-app-1 | 2025-12-25 17:36:13 - WARNING - chainlit - Translated markdown file for it-IT not found. Defaulting to chainlit.md. +chainlit-app-1 | 2025-12-25 17:36:13 - INFO - chainlit - Missing custom logo. Falling back to default logo. +chainlit-app-1 | 2025-12-25 17:39:46 - WARNING - chainlit - Translation file for it-IT not found. Using parent translation it. +chainlit-app-1 | 2025-12-25 17:39:47 - WARNING - chainlit - Translation file for it-IT not found. Using parent translation it. +chainlit-app-1 | 2025-12-25 17:39:47 - WARNING - chainlit - Translated markdown file for it-IT not found. Defaulting to chainlit.md. +chainlit-app-1 | 2025-12-25 17:39:51 - WARNING - chainlit - Translation file for it-IT not found. Using parent translation it. +chainlit-app-1 | 2025-12-25 17:39:56 - INFO - httpx - HTTP Request: POST http://192.168.1.243:11434/api/chat "HTTP/1.1 200 OK" +qdrant-1 | _ _ +qdrant-1 | __ _ __| |_ __ __ _ _ __ | |_ +qdrant-1 | / _` |/ _` | '__/ _` | '_ \| __| +qdrant-1 | | (_| | (_| | | | (_| | | | | |_ +qdrant-1 | \__, |\__,_|_| \__,_|_| |_|\__| +qdrant-1 | |_| +qdrant-1 | +qdrant-1 | Access web UI at https://ui.qdrant.tech/?v=v1.0.0 +qdrant-1 | +qdrant-1 | [2025-12-25T16:38:00.816Z INFO storage::content_manager::consensus::persistent] Initializing new raft state at ./storage/raft_state +qdrant-1 | [2025-12-25T16:38:00.861Z INFO qdrant] Distributed mode disabled +qdrant-1 | [2025-12-25T16:38:00.861Z INFO qdrant] Telemetry reporting enabled, id: e6113e43-627c-471d-8374-0f1b61799d76 +qdrant-1 | [2025-12-25T16:38:00.872Z INFO qdrant::tonic] Qdrant gRPC listening on 6334 +qdrant-1 | [2025-12-25T16:38:00.890Z INFO actix_server::builder] Starting 3 workers +qdrant-1 | [2025-12-25T16:38:00.890Z INFO actix_server::server] Actix runtime found; starting in Actix runtime +qdrant-1 | [2025-12-25T16:39:02.504Z INFO actix_server::server] SIGTERM received; starting graceful shutdown +qdrant-1 | [2025-12-25T16:39:02.505Z INFO actix_server::worker] Shutting down idle worker +qdrant-1 | [2025-12-25T16:39:02.508Z INFO actix_server::accept] Accept thread stopped +qdrant-1 | [2025-12-25T16:39:02.508Z INFO actix_server::worker] Shutting down idle worker +qdrant-1 | [2025-12-25T16:39:02.508Z INFO actix_server::worker] Shutting down idle worker +qdrant-1 | _ _ +qdrant-1 | __ _ __| |_ __ __ _ _ __ | |_ +qdrant-1 | / _` |/ _` | '__/ _` | '_ \| __| +qdrant-1 | | (_| | (_| | | | (_| | | | | |_ +qdrant-1 | \__, |\__,_|_| \__,_|_| |_|\__| +qdrant-1 | |_| +qdrant-1 | +qdrant-1 | Access web UI at https://ui.qdrant.tech/?v=v1.0.0 +qdrant-1 | +qdrant-1 | [2025-12-25T16:43:53.592Z INFO storage::content_manager::consensus::persistent] Loading raft state from ./storage/raft_state +qdrant-1 | [2025-12-25T16:43:53.612Z INFO qdrant] Distributed mode disabled +qdrant-1 | [2025-12-25T16:43:53.612Z INFO qdrant] Telemetry reporting enabled, id: 2a83356a-9770-47d3-a0bd-638f75769522 +qdrant-1 | [2025-12-25T16:43:53.615Z INFO qdrant::tonic] Qdrant gRPC listening on 6334 +qdrant-1 | [2025-12-25T16:43:53.616Z INFO actix_server::builder] Starting 3 workers +qdrant-1 | [2025-12-25T16:43:53.617Z INFO actix_server::server] Actix runtime found; starting in Actix runtime +qdrant-1 | [2025-12-25T16:56:42.005Z INFO actix_server::server] SIGTERM received; starting graceful shutdown +qdrant-1 | [2025-12-25T16:56:42.006Z INFO actix_server::worker] Shutting down idle worker +qdrant-1 | [2025-12-25T16:56:42.006Z INFO actix_server::worker] Shutting down idle worker +qdrant-1 | [2025-12-25T16:56:42.007Z INFO actix_server::worker] Shutting down idle worker +qdrant-1 | [2025-12-25T16:56:42.007Z INFO actix_server::accept] Accept thread stopped +qdrant-1 | _ _ +qdrant-1 | __ _ __| |_ __ __ _ _ __ | |_ +qdrant-1 | / _` |/ _` | '__/ _` | '_ \| __| +qdrant-1 | | (_| | (_| | | | (_| | | | | |_ +qdrant-1 | \__, |\__,_|_| \__,_|_| |_|\__| +qdrant-1 | |_| +qdrant-1 | +qdrant-1 | Access web UI at https://ui.qdrant.tech/?v=v1.0.0 +qdrant-1 | +qdrant-1 | [2025-12-25T16:56:52.790Z INFO storage::content_manager::consensus::persistent] Loading raft state from ./storage/raft_state +qdrant-1 | [2025-12-25T16:56:52.796Z INFO qdrant] Distributed mode disabled +qdrant-1 | [2025-12-25T16:56:52.796Z INFO qdrant] Telemetry reporting enabled, id: f821b8ea-9ee5-497e-a172-dfebf253f7b1 +qdrant-1 | [2025-12-25T16:56:52.797Z INFO qdrant::tonic] Qdrant gRPC listening on 6334 +qdrant-1 | [2025-12-25T16:56:52.798Z INFO actix_server::builder] Starting 3 workers +qdrant-1 | [2025-12-25T16:56:52.798Z INFO actix_server::server] Actix runtime found; starting in Actix runtime +postgres-1 | +postgres-1 | PostgreSQL Database directory appears to contain a database; Skipping initialization +postgres-1 | +postgres-1 | 2025-12-25 16:38:01.071 UTC [1] LOG: starting PostgreSQL 18.1 (Debian 18.1-1.pgdg13+2) on x86_64-pc-linux-gnu, compiled by gcc (Debian 14.2.0-19) 14.2.0, 64-bit +postgres-1 | 2025-12-25 16:38:01.072 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432 +postgres-1 | 2025-12-25 16:38:01.072 UTC [1] LOG: listening on IPv6 address "::", port 5432 +postgres-1 | 2025-12-25 16:38:01.093 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432" +postgres-1 | 2025-12-25 16:38:01.126 UTC [32] LOG: database system was shut down at 2025-12-25 14:34:55 UTC +postgres-1 | 2025-12-25 16:38:01.155 UTC [1] LOG: database system is ready to accept connections +postgres-1 | 2025-12-25 16:39:02.495 UTC [1] LOG: received fast shutdown request +postgres-1 | 2025-12-25 16:39:02.505 UTC [1] LOG: aborting any active transactions +postgres-1 | 2025-12-25 16:39:02.521 UTC [1] LOG: background worker "logical replication launcher" (PID 35) exited with exit code 1 +postgres-1 | 2025-12-25 16:39:02.521 UTC [30] LOG: shutting down +postgres-1 | 2025-12-25 16:39:02.533 UTC [30] LOG: checkpoint starting: shutdown immediate +postgres-1 | 2025-12-25 16:39:02.601 UTC [30] LOG: checkpoint complete: wrote 0 buffers (0.0%), wrote 3 SLRU buffers; 0 WAL file(s) added, 0 removed, 0 recycled; write=0.019 s, sync=0.009 s, total=0.079 s; sync files=2, longest=0.005 s, average=0.005 s; distance=0 kB, estimate=0 kB; lsn=0/1BEF980, redo lsn=0/1BEF980 +postgres-1 | 2025-12-25 16:39:02.644 UTC [1] LOG: database system is shut down +postgres-1 | +postgres-1 | PostgreSQL Database directory appears to contain a database; Skipping initialization +postgres-1 | +postgres-1 | 2025-12-25 16:43:53.946 UTC [1] LOG: starting PostgreSQL 18.1 (Debian 18.1-1.pgdg13+2) on x86_64-pc-linux-gnu, compiled by gcc (Debian 14.2.0-19) 14.2.0, 64-bit +postgres-1 | 2025-12-25 16:43:53.947 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432 +postgres-1 | 2025-12-25 16:43:53.947 UTC [1] LOG: listening on IPv6 address "::", port 5432 +postgres-1 | 2025-12-25 16:43:53.965 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432" +postgres-1 | 2025-12-25 16:43:53.990 UTC [32] LOG: database system was shut down at 2025-12-25 16:39:02 UTC +postgres-1 | 2025-12-25 16:43:54.013 UTC [1] LOG: database system is ready to accept connections +postgres-1 | 2025-12-25 16:48:54.089 UTC [30] LOG: checkpoint starting: time +postgres-1 | 2025-12-25 16:48:54.175 UTC [30] LOG: checkpoint complete: wrote 0 buffers (0.0%), wrote 3 SLRU buffers; 0 WAL file(s) added, 0 removed, 0 recycled; write=0.036 s, sync=0.009 s, total=0.088 s; sync files=2, longest=0.005 s, average=0.005 s; distance=0 kB, estimate=0 kB; lsn=0/1BEFA88, redo lsn=0/1BEFA30 +postgres-1 | 2025-12-25 16:56:42.002 UTC [1] LOG: received fast shutdown request +postgres-1 | 2025-12-25 16:56:42.018 UTC [1] LOG: aborting any active transactions +postgres-1 | 2025-12-25 16:56:42.026 UTC [1] LOG: background worker "logical replication launcher" (PID 35) exited with exit code 1 +postgres-1 | 2025-12-25 16:56:42.030 UTC [30] LOG: shutting down +postgres-1 | 2025-12-25 16:56:42.039 UTC [30] LOG: checkpoint starting: shutdown immediate +postgres-1 | 2025-12-25 16:56:42.086 UTC [30] LOG: checkpoint complete: wrote 0 buffers (0.0%), wrote 0 SLRU buffers; 0 WAL file(s) added, 0 removed, 0 recycled; write=0.004 s, sync=0.001 s, total=0.057 s; sync files=0, longest=0.000 s, average=0.000 s; distance=0 kB, estimate=0 kB; lsn=0/1BEFB38, redo lsn=0/1BEFB38 +postgres-1 | 2025-12-25 16:56:42.131 UTC [1] LOG: database system is shut down +postgres-1 | +postgres-1 | PostgreSQL Database directory appears to contain a database; Skipping initialization +postgres-1 | +postgres-1 | 2025-12-25 16:56:43.530 UTC [1] LOG: starting PostgreSQL 18.1 (Debian 18.1-1.pgdg13+2) on x86_64-pc-linux-gnu, compiled by gcc (Debian 14.2.0-19) 14.2.0, 64-bit +postgres-1 | 2025-12-25 16:56:43.532 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432 +postgres-1 | 2025-12-25 16:56:43.532 UTC [1] LOG: listening on IPv6 address "::", port 5432 +postgres-1 | 2025-12-25 16:56:43.552 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432" +postgres-1 | 2025-12-25 16:56:43.585 UTC [32] LOG: database system was shut down at 2025-12-25 16:56:42 UTC +postgres-1 | 2025-12-25 16:56:43.616 UTC [1] LOG: database system is ready to accept connections +postgres-1 | 2025-12-25 17:01:43.645 UTC [30] LOG: checkpoint starting: time +postgres-1 | 2025-12-25 17:01:43.712 UTC [30] LOG: checkpoint complete: wrote 0 buffers (0.0%), wrote 3 SLRU buffers; 0 WAL file(s) added, 0 removed, 0 recycled; write=0.019 s, sync=0.009 s, total=0.068 s; sync files=2, longest=0.005 s, average=0.005 s; distance=0 kB, estimate=0 kB; lsn=0/1BEFC40, redo lsn=0/1BEFBE8 diff --git a/__pycache__/app.cpython-310.pyc b/__pycache__/app.cpython-310.pyc index 147f59238059a9172c8dba628bbe706cde2cdbe4..e9542507d2bb7f4c93f684c6cbc1cdef91563c75 100644 GIT binary patch delta 752 zcmX|;&rcLF6vx}nkDXn1W;fAbRv|$Qk`dhy(1?gXc7X!~M1lu{F~dOX?!fHMrc)5j ztZ0nhh=(4$>L1`iIT}xD;@{Ad-uxRp82i}7HvP=&x9`2a*ZDO3W7tbvw}ABM>@P>Z zD<8ej)`6wBre0t5N~y*ROff|<>SBT45lX1>o^(luw~dTY^AL9pVP@zhrq+(h2Qx$w zwO)}5>Y~ipwy3R@+#%_j)XB^}#H*RMDv06=T0`2-!`Cm|;aK0LO68TMrP^{WsNG!% z>b14`Kk`HIjO$}%Hj`9DKdT7I4g1{v0&8tDN~3loRr&w6Z!0^AkecOc6ltn+mokHYKc5|aM3uP*dvZn#17_DM^26q zyeRwfqj>IUGCyzF)7qI|lh2Bk(d%H20WJcdBYsvxnY$_DKHZ38({R0ni?w1VH`#GGJIk*tp~9ybU( zQLq{6o+jj9@8PAZaMrh#P!?>)$<|KVySM^z0~!iH>eVx@H}Xws+=7%1`K|P13@pFD z_q&{}W*D{ONc8%?r;j)RtO6d$X1R)Q$hYOe^HW+-Zqg2Pga}d*a>3_7D`>67(ll%^ Qz93b3HFu(b%>fVp1Ap|k=l}o! delta 713 zcmY+BO>YuG7{_;pWqDoT)RbBi#ZiiNugCGs4zo7iY&e0$O9@ z&5QE|?7@qP2kp_six)q`dZZf|-^X&8dpZVwck@}K~xw;-!#HZ0+YjyE+ z?5qE!Dy{)>zKi2k1zrIJkYX!SFabw^!uLJ|k?#ajbr!40e*#|vq`iexIDtog6qqO| zC~m%q4~3FSTQS0W1PG;0q`9+G*{yUN&Gx}q&u-?@+4YTdHk~Wv|NdL-wwn6~*2h&v zF(6k5u1C2#atUR}+NEj;4-YXymeV&huDP~t4o$ArYzsS-NGDwDV~U+8+-EtI`)B2b z%2kS=QIZh_?3b2L$Vzw|1St>$K|qS9jR1sTi`@-f0ku;vRQG9{qJ(U)Z_(tPk_bET zKTof;w!76zyFOkTP&z8Dt^Mbsl+70Mg_&7$QM7UOv4@EP$xN=yen;EjI@{N;f-;-x zZ=ABM31S-$g#m9Is1xFn(gkCM+?4ZgNo+|}B;+<`E=f*C(hkW=qy)HTjz-u)L~4>f ziHJm0LYLTPk7DhG+aeSr9ppW==zyyPn}`${kF9{lyZ92wuygVIg>_NYiS%5AyN9;h zd+5y-lTv3{Du4&GK|K6#BOdz@&-fd%k@5KRYcetx=35E&2BziM7M=TQOBpE9lIXA( wi8Q#uJ|)gyE{X)#U8gHLXxF1Ap`<2rVUZhR&old&G}zC?y?_=5>T(R60dr=s_5c6? diff --git a/app.py b/app.py index 93ae2319..db278c95 100644 --- a/app.py +++ b/app.py @@ -4,178 +4,159 @@ import re from datetime import datetime import shutil import uuid -from qdrant_client import QdrantClient -from qdrant_client.http.models import PointStruct +import ollama +from qdrant_client import QdrantClient, models -# Define user roles mapping +# --- CONFIGURAZIONE --- USER_ROLES = { 'moglie@esempio.com': 'business', 'ingegnere@esempio.com': 'engineering', 'architetto@esempio.com': 'architecture', 'admin@esempio.com': 'admin' } - -# Define the path for workspaces WORKSPACES_DIR = "./workspaces" +# URL Config +OLLAMA_URL = os.getenv('OLLAMA_API_BASE', 'http://192.168.1.243:11434') +QDRANT_URL = "http://qdrant:6333" # Nome del servizio nel docker-compose + +# Client Globali +aclient = ollama.AsyncClient(host=OLLAMA_URL) # Per la chat (veloce) +# Client sincrono per embedding (più stabile per operazioni batch) +embed_client = ollama.Client(host=OLLAMA_URL) + +# --- FUNZIONI UTILITY --- + def create_workspace(user_role): - workspace_path = os.path.join(WORKSPACES_DIR, user_role) - if not os.path.exists(workspace_path): - os.makedirs(workspace_path) + path = os.path.join(WORKSPACES_DIR, user_role) + os.makedirs(path, exist_ok=True) def save_code_to_file(code, user_role): timestamp = datetime.now().strftime("%Y%m%d%H%M%S") file_name = f"code_{timestamp}.py" file_path = os.path.join(WORKSPACES_DIR, user_role, file_name) - - with open(file_path, "w") as file: + with open(file_path, "w", encoding="utf-8") as file: file.write(code) - return file_path -def limit_history(history): - if len(history) > 20: - history = history[-20:] - return history +def get_qdrant_client(): + return QdrantClient(url=QDRANT_URL) -async def connect_to_qdrant(): - client = QdrantClient("http://qdrant:6333") - collection_name = "documents" - +def ensure_collection(client): try: - client.get_collection(collection_name) - except Exception as e: + client.get_collection("documents") + except: client.create_collection( - collection_name=collection_name, - vectors_config={"size": 768, "distance": "Cosine"} + collection_name="documents", + vectors_config=models.VectorParams(size=768, distance=models.Distance.COSINE) ) - - return client -async def get_embeddings(text): - import ollama - # Costruisci l'URL completo: http://IP:PORT - # È meglio usare la stessa logica usata nella funzione message (OLLAMA_API_BASE) - host_url = os.getenv('OLLAMA_API_BASE', 'http://192.168.1.243:11434') - - # Inizializza il client passando SOLO l'host completo - client = ollama.Client(host=host_url) - - # Aggiungi il controllo lunghezza per evitare crash su testi lunghi - if len(text) > 12000: - text = text[:12000] - - response = client.embed(model='nomic-embed-text', input=text) - - # Gestione compatibilità versioni +def get_embeddings(text): + # Taglia il testo se troppo lungo per evitare errori (max safe context) + text = text[:8000] + response = embed_client.embed(model='nomic-embed-text', input=text) + # Gestisce diversi formati di risposta delle versioni Ollama if 'embeddings' in response: return response['embeddings'][0] return response['embedding'] + +# --- LOGICA CHAT --- + @cl.on_chat_start async def chat_start(): - # Set the user's email to a hardcoded value for testing purposes - user_email = "admin@esempio.com" - - # Determine the user's role based on the email + user_email = "admin@esempio.com" # In prod, prendilo dagli header/auth user_role = USER_ROLES.get(user_email, 'guest') - # Create workspace directory if it doesn't exist create_workspace(user_role) - # Initialize history in the session cl.user_session.set("history", []) - - # Set the user's role in the session cl.user_session.set("role", user_role) - # Send a welcome message based on the user's role - if user_role == 'admin': - await cl.Message(content="Welcome, Admin!").send() - elif user_role == 'engineering': - await cl.Message(content="Welcome, Engineer!").send() - elif user_role == 'business': - await cl.Message(content="Welcome, Business User!").send() - elif user_role == 'architecture': - await cl.Message(content="Welcome, Architect!").send() - else: - await cl.Message(content="Welcome, Guest!").send() + # Inizializza Qdrant all'avvio + try: + q_client = get_qdrant_client() + ensure_collection(q_client) + status_msg = "✅ System ready. Qdrant connected." + except Exception as e: + status_msg = f"⚠️ System ready, but Qdrant error: {e}" + + await cl.Message(content=f"Welcome {user_role}! {status_msg}").send() @cl.on_message -async def message(message): - # Retrieve the user's role from the session +async def message(message: cl.Message): user_role = cl.user_session.get("role", 'guest') + history = cl.user_session.get("history", []) - if not user_role: - await cl.Message(content="User role not found").send() - return - - # Initialize the Ollama client using the environment variable - ollama_api_base = os.getenv('OLLAMA_API_BASE', 'http://192.168.1.243:11434') - - try: - import ollama - client = ollama.Client(ollama_api_base) + # 1. GESTIONE FILE CARICATI (RAG) + if message.elements: + processing_msg = cl.Message(content="⚙️ Elaborazione file in corso...") + await processing_msg.send() - # Retrieve the history from the session and limit it - history = cl.user_session.get("history", []) - history = limit_history(history) + q_client = get_qdrant_client() + uploaded_files = [] - # Append the new user message to the history - history.append({"role": "user", "content": message.content}) - - # Check if there are any elements in the message - if message.elements: - uploaded_files = [] - for element in message.elements: - try: - # Save the content of the file - temp_file_path = element.path - destination_file_path = os.path.join(WORKSPACES_DIR, user_role, element.name) - - with open(temp_file_path, 'rb') as src, open(destination_file_path, 'wb') as dst: - shutil.copyfileobj(src, dst) - - uploaded_files.append(element.name) - except Exception as e: - await cl.Message(content=f"Error saving {element.name}: {e}").send() - - if uploaded_files: - await cl.Message(content=f"Files uploaded and saved: {', '.join(uploaded_files)}").send() - - # Call the model and get the response - response = client.chat(model='qwen2.5-coder:7b', messages=history) - - # Extract code blocks from the AI response - code_blocks = re.findall(r"```python(.*?)```", response['message']['content'], re.DOTALL) - - elements = [] - if code_blocks: - for code in code_blocks: - file_path = save_code_to_file(code, user_role) - elements.append(cl.File(name=os.path.basename(file_path), path=file_path)) - - # Append the AI response to the history - history.append({"role": "assistant", "content": response['message']['content']}) - - # Save the updated history in the session - cl.user_session.set("history", history) - - # Handle text files and index them in Qdrant for element in message.elements: - if element.name.endswith('.txt'): - with open(element.path, 'r') as f: - content = f.read() - - embeddings = await get_embeddings(content) - - qdrant_client = await connect_to_qdrant() - point_id = uuid.uuid4() - point = PointStruct(id=point_id, vector=embeddings, payload={"file_name": element.name}) - qdrant_client.upsert(collection_name="documents", points=[point]) - - await cl.Message(content=f"Documento indicizzato con successo su Qdrant.").send() + # Salva su disco + dest_path = os.path.join(WORKSPACES_DIR, user_role, element.name) + shutil.copyfile(element.path, dest_path) + uploaded_files.append(element.name) + + # Se è testo, indicizza su Qdrant + if element.name.endswith('.txt') or element.name.endswith('.md') or element.name.endswith('.py'): + try: + with open(dest_path, 'r', encoding='utf-8') as f: + content = f.read() + + vector = get_embeddings(content) + + point_id = str(uuid.uuid4()) + q_client.upsert( + collection_name="documents", + points=[models.PointStruct( + id=point_id, + vector=vector, + payload={"filename": element.name, "text": content[:500]} # Salviamo un'anteprima + )] + ) + except Exception as e: + await cl.Message(content=f"❌ Errore indicizzazione {element.name}: {e}").send() + + await processing_msg.remove() + await cl.Message(content=f"📂 File salvati e indicizzati: {', '.join(uploaded_files)}").send() + + # 2. AGGIORNA STORIA E CHAT + history.append({"role": "user", "content": message.content}) + + msg = cl.Message(content="") + await msg.send() + + full_response = "" + + # Streaming della risposta + try: + async for part in await aclient.chat(model='qwen2.5-coder:7b', messages=history, stream=True): + token = part['message']['content'] + full_response += token + await msg.stream_token(token) - # Send the final message including both text and files - await cl.Message(content=response['message']['content'], elements=elements).send() + await msg.update() + + # 3. ESTRAZIONE CODICE (Salvataggio automatico) + code_blocks = re.findall(r"```python(.*?)```", full_response, re.DOTALL) + if code_blocks: + files_generated = [] + for code in code_blocks: + code = code.strip() + if code: + path = save_code_to_file(code, user_role) + files_generated.append(cl.File(name=os.path.basename(path), path=path)) + + if files_generated: + await cl.Message(content="💾 Ho estratto il codice per te:", elements=files_generated).send() + + # Aggiorna storia + history.append({"role": "assistant", "content": full_response}) + cl.user_session.set("history", history) + except Exception as e: - await cl.Message(content=f"Error: {e}").send() + await cl.Message(content=f"❌ Errore generazione AI: {e}").send() \ No newline at end of file diff --git a/workspaces/admin/error.txt b/workspaces/admin/error.txt new file mode 100644 index 00000000..334bbef9 --- /dev/null +++ b/workspaces/admin/error.txt @@ -0,0 +1,109 @@ +chainlit-app-1 | 2025-12-25 17:32:55 - INFO - chainlit - Your app is available at http://0.0.0.0:8000 +chainlit-app-1 | 2025-12-25 17:36:13 - WARNING - chainlit - Translation file for it-IT not found. Using parent translation it. +chainlit-app-1 | 2025-12-25 17:36:13 - WARNING - chainlit - Translated markdown file for it-IT not found. Defaulting to chainlit.md. +chainlit-app-1 | 2025-12-25 17:36:13 - INFO - chainlit - Missing custom logo. Falling back to default logo. +chainlit-app-1 | 2025-12-25 17:39:46 - WARNING - chainlit - Translation file for it-IT not found. Using parent translation it. +chainlit-app-1 | 2025-12-25 17:39:47 - WARNING - chainlit - Translation file for it-IT not found. Using parent translation it. +chainlit-app-1 | 2025-12-25 17:39:47 - WARNING - chainlit - Translated markdown file for it-IT not found. Defaulting to chainlit.md. +chainlit-app-1 | 2025-12-25 17:39:51 - WARNING - chainlit - Translation file for it-IT not found. Using parent translation it. +chainlit-app-1 | 2025-12-25 17:39:56 - INFO - httpx - HTTP Request: POST http://192.168.1.243:11434/api/chat "HTTP/1.1 200 OK" +qdrant-1 | _ _ +qdrant-1 | __ _ __| |_ __ __ _ _ __ | |_ +qdrant-1 | / _` |/ _` | '__/ _` | '_ \| __| +qdrant-1 | | (_| | (_| | | | (_| | | | | |_ +qdrant-1 | \__, |\__,_|_| \__,_|_| |_|\__| +qdrant-1 | |_| +qdrant-1 | +qdrant-1 | Access web UI at https://ui.qdrant.tech/?v=v1.0.0 +qdrant-1 | +qdrant-1 | [2025-12-25T16:38:00.816Z INFO storage::content_manager::consensus::persistent] Initializing new raft state at ./storage/raft_state +qdrant-1 | [2025-12-25T16:38:00.861Z INFO qdrant] Distributed mode disabled +qdrant-1 | [2025-12-25T16:38:00.861Z INFO qdrant] Telemetry reporting enabled, id: e6113e43-627c-471d-8374-0f1b61799d76 +qdrant-1 | [2025-12-25T16:38:00.872Z INFO qdrant::tonic] Qdrant gRPC listening on 6334 +qdrant-1 | [2025-12-25T16:38:00.890Z INFO actix_server::builder] Starting 3 workers +qdrant-1 | [2025-12-25T16:38:00.890Z INFO actix_server::server] Actix runtime found; starting in Actix runtime +qdrant-1 | [2025-12-25T16:39:02.504Z INFO actix_server::server] SIGTERM received; starting graceful shutdown +qdrant-1 | [2025-12-25T16:39:02.505Z INFO actix_server::worker] Shutting down idle worker +qdrant-1 | [2025-12-25T16:39:02.508Z INFO actix_server::accept] Accept thread stopped +qdrant-1 | [2025-12-25T16:39:02.508Z INFO actix_server::worker] Shutting down idle worker +qdrant-1 | [2025-12-25T16:39:02.508Z INFO actix_server::worker] Shutting down idle worker +qdrant-1 | _ _ +qdrant-1 | __ _ __| |_ __ __ _ _ __ | |_ +qdrant-1 | / _` |/ _` | '__/ _` | '_ \| __| +qdrant-1 | | (_| | (_| | | | (_| | | | | |_ +qdrant-1 | \__, |\__,_|_| \__,_|_| |_|\__| +qdrant-1 | |_| +qdrant-1 | +qdrant-1 | Access web UI at https://ui.qdrant.tech/?v=v1.0.0 +qdrant-1 | +qdrant-1 | [2025-12-25T16:43:53.592Z INFO storage::content_manager::consensus::persistent] Loading raft state from ./storage/raft_state +qdrant-1 | [2025-12-25T16:43:53.612Z INFO qdrant] Distributed mode disabled +qdrant-1 | [2025-12-25T16:43:53.612Z INFO qdrant] Telemetry reporting enabled, id: 2a83356a-9770-47d3-a0bd-638f75769522 +qdrant-1 | [2025-12-25T16:43:53.615Z INFO qdrant::tonic] Qdrant gRPC listening on 6334 +qdrant-1 | [2025-12-25T16:43:53.616Z INFO actix_server::builder] Starting 3 workers +qdrant-1 | [2025-12-25T16:43:53.617Z INFO actix_server::server] Actix runtime found; starting in Actix runtime +qdrant-1 | [2025-12-25T16:56:42.005Z INFO actix_server::server] SIGTERM received; starting graceful shutdown +qdrant-1 | [2025-12-25T16:56:42.006Z INFO actix_server::worker] Shutting down idle worker +qdrant-1 | [2025-12-25T16:56:42.006Z INFO actix_server::worker] Shutting down idle worker +qdrant-1 | [2025-12-25T16:56:42.007Z INFO actix_server::worker] Shutting down idle worker +qdrant-1 | [2025-12-25T16:56:42.007Z INFO actix_server::accept] Accept thread stopped +qdrant-1 | _ _ +qdrant-1 | __ _ __| |_ __ __ _ _ __ | |_ +qdrant-1 | / _` |/ _` | '__/ _` | '_ \| __| +qdrant-1 | | (_| | (_| | | | (_| | | | | |_ +qdrant-1 | \__, |\__,_|_| \__,_|_| |_|\__| +qdrant-1 | |_| +qdrant-1 | +qdrant-1 | Access web UI at https://ui.qdrant.tech/?v=v1.0.0 +qdrant-1 | +qdrant-1 | [2025-12-25T16:56:52.790Z INFO storage::content_manager::consensus::persistent] Loading raft state from ./storage/raft_state +qdrant-1 | [2025-12-25T16:56:52.796Z INFO qdrant] Distributed mode disabled +qdrant-1 | [2025-12-25T16:56:52.796Z INFO qdrant] Telemetry reporting enabled, id: f821b8ea-9ee5-497e-a172-dfebf253f7b1 +qdrant-1 | [2025-12-25T16:56:52.797Z INFO qdrant::tonic] Qdrant gRPC listening on 6334 +qdrant-1 | [2025-12-25T16:56:52.798Z INFO actix_server::builder] Starting 3 workers +qdrant-1 | [2025-12-25T16:56:52.798Z INFO actix_server::server] Actix runtime found; starting in Actix runtime +postgres-1 | +postgres-1 | PostgreSQL Database directory appears to contain a database; Skipping initialization +postgres-1 | +postgres-1 | 2025-12-25 16:38:01.071 UTC [1] LOG: starting PostgreSQL 18.1 (Debian 18.1-1.pgdg13+2) on x86_64-pc-linux-gnu, compiled by gcc (Debian 14.2.0-19) 14.2.0, 64-bit +postgres-1 | 2025-12-25 16:38:01.072 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432 +postgres-1 | 2025-12-25 16:38:01.072 UTC [1] LOG: listening on IPv6 address "::", port 5432 +postgres-1 | 2025-12-25 16:38:01.093 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432" +postgres-1 | 2025-12-25 16:38:01.126 UTC [32] LOG: database system was shut down at 2025-12-25 14:34:55 UTC +postgres-1 | 2025-12-25 16:38:01.155 UTC [1] LOG: database system is ready to accept connections +postgres-1 | 2025-12-25 16:39:02.495 UTC [1] LOG: received fast shutdown request +postgres-1 | 2025-12-25 16:39:02.505 UTC [1] LOG: aborting any active transactions +postgres-1 | 2025-12-25 16:39:02.521 UTC [1] LOG: background worker "logical replication launcher" (PID 35) exited with exit code 1 +postgres-1 | 2025-12-25 16:39:02.521 UTC [30] LOG: shutting down +postgres-1 | 2025-12-25 16:39:02.533 UTC [30] LOG: checkpoint starting: shutdown immediate +postgres-1 | 2025-12-25 16:39:02.601 UTC [30] LOG: checkpoint complete: wrote 0 buffers (0.0%), wrote 3 SLRU buffers; 0 WAL file(s) added, 0 removed, 0 recycled; write=0.019 s, sync=0.009 s, total=0.079 s; sync files=2, longest=0.005 s, average=0.005 s; distance=0 kB, estimate=0 kB; lsn=0/1BEF980, redo lsn=0/1BEF980 +postgres-1 | 2025-12-25 16:39:02.644 UTC [1] LOG: database system is shut down +postgres-1 | +postgres-1 | PostgreSQL Database directory appears to contain a database; Skipping initialization +postgres-1 | +postgres-1 | 2025-12-25 16:43:53.946 UTC [1] LOG: starting PostgreSQL 18.1 (Debian 18.1-1.pgdg13+2) on x86_64-pc-linux-gnu, compiled by gcc (Debian 14.2.0-19) 14.2.0, 64-bit +postgres-1 | 2025-12-25 16:43:53.947 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432 +postgres-1 | 2025-12-25 16:43:53.947 UTC [1] LOG: listening on IPv6 address "::", port 5432 +postgres-1 | 2025-12-25 16:43:53.965 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432" +postgres-1 | 2025-12-25 16:43:53.990 UTC [32] LOG: database system was shut down at 2025-12-25 16:39:02 UTC +postgres-1 | 2025-12-25 16:43:54.013 UTC [1] LOG: database system is ready to accept connections +postgres-1 | 2025-12-25 16:48:54.089 UTC [30] LOG: checkpoint starting: time +postgres-1 | 2025-12-25 16:48:54.175 UTC [30] LOG: checkpoint complete: wrote 0 buffers (0.0%), wrote 3 SLRU buffers; 0 WAL file(s) added, 0 removed, 0 recycled; write=0.036 s, sync=0.009 s, total=0.088 s; sync files=2, longest=0.005 s, average=0.005 s; distance=0 kB, estimate=0 kB; lsn=0/1BEFA88, redo lsn=0/1BEFA30 +postgres-1 | 2025-12-25 16:56:42.002 UTC [1] LOG: received fast shutdown request +postgres-1 | 2025-12-25 16:56:42.018 UTC [1] LOG: aborting any active transactions +postgres-1 | 2025-12-25 16:56:42.026 UTC [1] LOG: background worker "logical replication launcher" (PID 35) exited with exit code 1 +postgres-1 | 2025-12-25 16:56:42.030 UTC [30] LOG: shutting down +postgres-1 | 2025-12-25 16:56:42.039 UTC [30] LOG: checkpoint starting: shutdown immediate +postgres-1 | 2025-12-25 16:56:42.086 UTC [30] LOG: checkpoint complete: wrote 0 buffers (0.0%), wrote 0 SLRU buffers; 0 WAL file(s) added, 0 removed, 0 recycled; write=0.004 s, sync=0.001 s, total=0.057 s; sync files=0, longest=0.000 s, average=0.000 s; distance=0 kB, estimate=0 kB; lsn=0/1BEFB38, redo lsn=0/1BEFB38 +postgres-1 | 2025-12-25 16:56:42.131 UTC [1] LOG: database system is shut down +postgres-1 | +postgres-1 | PostgreSQL Database directory appears to contain a database; Skipping initialization +postgres-1 | +postgres-1 | 2025-12-25 16:56:43.530 UTC [1] LOG: starting PostgreSQL 18.1 (Debian 18.1-1.pgdg13+2) on x86_64-pc-linux-gnu, compiled by gcc (Debian 14.2.0-19) 14.2.0, 64-bit +postgres-1 | 2025-12-25 16:56:43.532 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432 +postgres-1 | 2025-12-25 16:56:43.532 UTC [1] LOG: listening on IPv6 address "::", port 5432 +postgres-1 | 2025-12-25 16:56:43.552 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432" +postgres-1 | 2025-12-25 16:56:43.585 UTC [32] LOG: database system was shut down at 2025-12-25 16:56:42 UTC +postgres-1 | 2025-12-25 16:56:43.616 UTC [1] LOG: database system is ready to accept connections +postgres-1 | 2025-12-25 17:01:43.645 UTC [30] LOG: checkpoint starting: time +postgres-1 | 2025-12-25 17:01:43.712 UTC [30] LOG: checkpoint complete: wrote 0 buffers (0.0%), wrote 3 SLRU buffers; 0 WAL file(s) added, 0 removed, 0 recycled; write=0.019 s, sync=0.009 s, total=0.068 s; sync files=2, longest=0.005 s, average=0.005 s; distance=0 kB, estimate=0 kB; lsn=0/1BEFC40, redo lsn=0/1BEFBE8