From 329a6ffc9fe7d697cbf64913c8d2721e7d487648 Mon Sep 17 00:00:00 2001 From: Dmitry Ng <19asdek91@gmail.com> Date: Thu, 19 Mar 2026 19:21:19 +0300 Subject: [PATCH] fix: empty flow assistant logs when no updates occur - Added a mechanism to track updates in the flow assistant log worker. - Implemented a new database function to delete flow assistant logs if no updates are made during processing. - Updated the querier interface to include the new delete function. - Enhanced SQL model to support the deletion of flow assistant logs. --- backend/pkg/controller/aslog.go | 10 +++++++++- backend/pkg/database/assistantlogs.sql.go | 10 ++++++++++ backend/pkg/database/querier.go | 1 + backend/pkg/tools/executor.go | 2 +- backend/sqlc/models/assistantlogs.sql | 4 ++++ 5 files changed, 25 insertions(+), 2 deletions(-) diff --git a/backend/pkg/controller/aslog.go b/backend/pkg/controller/aslog.go index 788aa69a..a7b3f68e 100644 --- a/backend/pkg/controller/aslog.go +++ b/backend/pkg/controller/aslog.go @@ -292,6 +292,7 @@ func (aslw *flowAssistantLogWorker) workerMsgUpdater( contentBuf := bytes.NewBuffer(contentData) thinkingData := make([]byte, 0, defaultMaxMessageLength) thinkingBuf := bytes.NewBuffer(thinkingData) + wasUpdated := false // track if we actually updated the record msgLog, err := aslw.db.GetFlowAssistantLog(ctx, msgID) if err != nil { @@ -336,15 +337,18 @@ func (aslw *flowAssistantLogWorker) workerMsgUpdater( ID: msgID, }) if err == nil { + wasUpdated = true aslw.pub.AssistantLogUpdated(ctx, msgLog, false) } case providers.StreamMessageChunkTypeContent: contentBuf.WriteString(chunk.Content) + wasUpdated = true aslw.pub.AssistantLogUpdated(ctx, newLog(chunk.MsgType, chunk.Content, ""), true) case providers.StreamMessageChunkTypeThinking: thinkingBuf.WriteString(aslw.getThinkingString(chunk.Thinking)) + wasUpdated = true aslw.pub.AssistantLogUpdated(ctx, newLog(chunk.MsgType, "", aslw.getThinkingString(chunk.Thinking)), true) case providers.StreamMessageChunkTypeResult: @@ -360,6 +364,7 @@ func (aslw *flowAssistantLogWorker) workerMsgUpdater( ID: msgID, }) if err == nil { + wasUpdated = true aslw.pub.AssistantLogUpdated(ctx, msgLog, false) } } @@ -375,7 +380,10 @@ func (aslw *flowAssistantLogWorker) workerMsgUpdater( processChunk(<-ch) } - if msgLog, err = aslw.db.GetFlowAssistantLog(ctx, msgID); err == nil { + // If record was never updated, delete it (empty message case) + if !wasUpdated { + _ = aslw.db.DeleteFlowAssistantLog(ctx, msgID) + } else if msgLog, err = aslw.db.GetFlowAssistantLog(ctx, msgID); err == nil { content, thinking := contentBuf.String(), thinkingBuf.String() _, _ = aslw.db.UpdateAssistantLog(ctx, database.UpdateAssistantLogParams{ Type: msgLog.Type, diff --git a/backend/pkg/database/assistantlogs.sql.go b/backend/pkg/database/assistantlogs.sql.go index a3cb70a6..bbf824ec 100644 --- a/backend/pkg/database/assistantlogs.sql.go +++ b/backend/pkg/database/assistantlogs.sql.go @@ -106,6 +106,16 @@ func (q *Queries) CreateResultAssistantLog(ctx context.Context, arg CreateResult return i, err } +const deleteFlowAssistantLog = `-- name: DeleteFlowAssistantLog :exec +DELETE FROM assistantlogs +WHERE id = $1 +` + +func (q *Queries) DeleteFlowAssistantLog(ctx context.Context, id int64) error { + _, err := q.db.ExecContext(ctx, deleteFlowAssistantLog, id) + return err +} + const getFlowAssistantLog = `-- name: GetFlowAssistantLog :one SELECT al.id, al.type, al.message, al.result, al.result_format, al.flow_id, al.assistant_id, al.created_at, al.thinking diff --git a/backend/pkg/database/querier.go b/backend/pkg/database/querier.go index 1113b806..28888e5a 100644 --- a/backend/pkg/database/querier.go +++ b/backend/pkg/database/querier.go @@ -36,6 +36,7 @@ type Querier interface { DeleteAssistant(ctx context.Context, id int64) (Assistant, error) DeleteFavoriteFlow(ctx context.Context, arg DeleteFavoriteFlowParams) (UserPreference, error) DeleteFlow(ctx context.Context, id int64) (Flow, error) + DeleteFlowAssistantLog(ctx context.Context, id int64) error DeletePrompt(ctx context.Context, id int64) error DeleteProvider(ctx context.Context, id int64) (Provider, error) DeleteSubtask(ctx context.Context, id int64) error diff --git a/backend/pkg/tools/executor.go b/backend/pkg/tools/executor.go index 9908afb5..c885b401 100644 --- a/backend/pkg/tools/executor.go +++ b/backend/pkg/tools/executor.go @@ -280,7 +280,7 @@ func (ce *customExecutor) Execute( var err error msgID, msg := int64(0), ce.getMessage(args) - if msg != "" { + if strings.Trim(msg, " \t\n\r") != "" { msgType := getMessageType(name) msgID, err = ce.mlp.PutMsg(ctx, msgType, ce.taskID, ce.subtaskID, streamID, thinking, msg) if err != nil { diff --git a/backend/sqlc/models/assistantlogs.sql b/backend/sqlc/models/assistantlogs.sql index 606776b5..51e9d805 100644 --- a/backend/sqlc/models/assistantlogs.sql +++ b/backend/sqlc/models/assistantlogs.sql @@ -70,3 +70,7 @@ UPDATE assistantlogs SET result = $1, result_format = $2 WHERE id = $3 RETURNING *; + +-- name: DeleteFlowAssistantLog :exec +DELETE FROM assistantlogs +WHERE id = $1;