mirror of
https://github.com/chaitin/MonkeyCode.git
synced 2026-11-30 16:47:16 +08:00
Merge pull request #570 from chaitin/fix-clickhouse-user-input-plaintext
fix: 兼容明文 user-input 日志
This commit is contained in:
5 files changed
+181
-27
No files matched your search
@@ -476,9 +476,23 @@ func buildTaskStreamsFromLogEntries(entries []tasklog.Entry, logger *slog.Logger
|
||||
return streams, ended
|
||||
}
|
||||
|
||||
type taskUserInputStoragePayload struct {
|
||||
Encoding string `json:"encoding"`
|
||||
Content string `json:"content"`
|
||||
Attachments []domain.TaskAttachment `json:"attachments"`
|
||||
}
|
||||
|
||||
func parseUserInputData(data []byte) domain.ContinueTaskReq {
|
||||
var stored taskUserInputStoragePayload
|
||||
if err := json.Unmarshal(data, &stored); err == nil && stored.Encoding == "plaintext" {
|
||||
return domain.ContinueTaskReq{
|
||||
Content: []byte(stored.Content),
|
||||
Attachments: stored.Attachments,
|
||||
}
|
||||
}
|
||||
|
||||
var payload domain.TaskUserInputPayload
|
||||
if err := json.Unmarshal(data, &payload); err == nil && strings.TrimSpace(string(payload.Content)) != "" {
|
||||
if err := json.Unmarshal(data, &payload); err == nil && (len(payload.Content) > 0 || len(payload.Attachments) > 0) {
|
||||
return domain.ContinueTaskReq{
|
||||
Content: payload.Content,
|
||||
Attachments: payload.Attachments,
|
||||
|
||||
@@ -1,13 +1,16 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"log/slog"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"github.com/chaitin/MonkeyCode/backend/domain"
|
||||
"github.com/chaitin/MonkeyCode/backend/pkg/tasklog"
|
||||
)
|
||||
|
||||
@@ -47,14 +50,45 @@ func TestBuildTaskStreamsFromLogEntriesKeepsStreamingWhenNotEnded(t *testing.T)
|
||||
|
||||
func TestNormalizeUserInputDataWrapsLegacyText(t *testing.T) {
|
||||
got := normalizeUserInputData([]byte("旧输入"))
|
||||
if string(got) != `{"content":"5pen6L6T5YWl","attachments":[]}` {
|
||||
t.Fatalf("normalized = %s", got)
|
||||
}
|
||||
assertUserInputPayload(t, got, "旧输入", []domain.TaskAttachment{})
|
||||
}
|
||||
|
||||
func TestNormalizeUserInputDataKeepsPayloadShape(t *testing.T) {
|
||||
got := normalizeUserInputData([]byte(`{"content":"5paw6L6T5YWl","attachments":[{"url":"https://oss.example.com/temp/a.txt","filename":"a.txt"}]}`))
|
||||
if string(got) != `{"content":"5paw6L6T5YWl","attachments":[{"url":"https://oss.example.com/temp/a.txt","filename":"a.txt"}]}` {
|
||||
t.Fatalf("normalized = %s", got)
|
||||
assertUserInputPayload(t, got, "新输入", []domain.TaskAttachment{{URL: "https://oss.example.com/temp/a.txt", Filename: "a.txt"}})
|
||||
}
|
||||
|
||||
func TestNormalizeUserInputDataKeepsLegacyPayloadAttachmentsWhenContentEmpty(t *testing.T) {
|
||||
got := normalizeUserInputData([]byte(`{"content":"","attachments":[{"url":"https://oss.example.com/temp/empty.txt","filename":"empty.txt"}]}`))
|
||||
assertUserInputPayload(t, got, "", []domain.TaskAttachment{{URL: "https://oss.example.com/temp/empty.txt", Filename: "empty.txt"}})
|
||||
}
|
||||
|
||||
func TestNormalizeUserInputDataConvertsPlaintextStoragePayloadToFrontendPayload(t *testing.T) {
|
||||
got := normalizeUserInputData([]byte(`{"encoding":"plaintext","content":"继续处理","attachments":[{"url":"https://oss.example.com/temp/a.txt","filename":"a.txt"}]}`))
|
||||
assertUserInputPayload(t, got, "继续处理", []domain.TaskAttachment{{URL: "https://oss.example.com/temp/a.txt", Filename: "a.txt"}})
|
||||
}
|
||||
|
||||
func TestNormalizeUserInputDataKeepsPlaintextBase64LookingContent(t *testing.T) {
|
||||
got := normalizeUserInputData([]byte(`{"encoding":"plaintext","content":"aGVsbG8="}`))
|
||||
assertUserInputPayload(t, got, "aGVsbG8=", []domain.TaskAttachment{})
|
||||
}
|
||||
|
||||
func TestNormalizeUserInputDataKeepsPlaintextStoragePayloadAttachmentsWhenContentEmpty(t *testing.T) {
|
||||
got := normalizeUserInputData([]byte(`{"encoding":"plaintext","content":"","attachments":[{"url":"https://oss.example.com/temp/empty.txt","filename":"empty.txt"}]}`))
|
||||
assertUserInputPayload(t, got, "", []domain.TaskAttachment{{URL: "https://oss.example.com/temp/empty.txt", Filename: "empty.txt"}})
|
||||
}
|
||||
|
||||
func assertUserInputPayload(t *testing.T, data []byte, wantContent string, wantAttachments []domain.TaskAttachment) {
|
||||
t.Helper()
|
||||
|
||||
var payload domain.TaskUserInputPayload
|
||||
if err := json.Unmarshal(data, &payload); err != nil {
|
||||
t.Fatalf("unmarshal normalized payload: %v, data = %s", err, data)
|
||||
}
|
||||
if string(payload.Content) != wantContent {
|
||||
t.Fatalf("content = %q, want %q, data = %s", string(payload.Content), wantContent, data)
|
||||
}
|
||||
if !reflect.DeepEqual(payload.Attachments, wantAttachments) {
|
||||
t.Fatalf("attachments = %#v, want %#v, data = %s", payload.Attachments, wantAttachments, data)
|
||||
}
|
||||
}
|
||||
@@ -356,14 +356,9 @@ func buildSummaryConversation(ctx context.Context, logger *slog.Logger, taskID u
|
||||
continue
|
||||
}
|
||||
|
||||
decoded, err := base64.StdEncoding.DecodeString(string(chunk.Data))
|
||||
if err != nil {
|
||||
decoded = chunk.Data
|
||||
}
|
||||
|
||||
switch chunk.Event {
|
||||
case "user-input":
|
||||
userInputText := userInputContent(decoded)
|
||||
userInputText := userInputContent(chunk.Data)
|
||||
|
||||
if len(agentMsg) > 0 {
|
||||
agentContent := strings.Join(agentMsg, "")
|
||||
@@ -376,10 +371,10 @@ func buildSummaryConversation(ctx context.Context, logger *slog.Logger, taskID u
|
||||
case "reply-question":
|
||||
var userInputText string
|
||||
var ur userReply
|
||||
if err := json.Unmarshal(decoded, &ur); err != nil {
|
||||
userInputText = string(decoded)
|
||||
} else {
|
||||
if decodeJSONPayload(chunk.Data, &ur) {
|
||||
userInputText = ur.AnswersJSON
|
||||
} else {
|
||||
userInputText = string(chunk.Data)
|
||||
}
|
||||
|
||||
if len(agentMsg) > 0 {
|
||||
@@ -392,7 +387,7 @@ func buildSummaryConversation(ctx context.Context, logger *slog.Logger, taskID u
|
||||
|
||||
case "task-running":
|
||||
var taskMsg wsData
|
||||
if err := json.Unmarshal(decoded, &taskMsg); err != nil {
|
||||
if !decodeJSONPayload(chunk.Data, &taskMsg) {
|
||||
continue
|
||||
}
|
||||
if taskMsg.Update.SessionUpdate == "agent_message_chunk" {
|
||||
@@ -433,12 +428,41 @@ func formatSummaryConversation(messages []llm.Message) []map[string]any {
|
||||
return conversation
|
||||
}
|
||||
|
||||
func userInputContent(decoded []byte) string {
|
||||
var payload userInputPayload
|
||||
if err := json.Unmarshal(decoded, &payload); err == nil && len(payload.Content) > 0 {
|
||||
return string(payload.Content)
|
||||
func decodeJSONPayload(data []byte, v any) bool {
|
||||
if err := json.Unmarshal(data, v); err == nil {
|
||||
return true
|
||||
}
|
||||
return string(decoded)
|
||||
decoded, err := base64.StdEncoding.DecodeString(string(data))
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return json.Unmarshal(decoded, v) == nil
|
||||
}
|
||||
|
||||
func userInputContent(data []byte) string {
|
||||
if content, ok := parseUserInputPayload(data); ok {
|
||||
return content
|
||||
}
|
||||
decoded, err := base64.StdEncoding.DecodeString(string(data))
|
||||
if err == nil {
|
||||
if content, ok := parseUserInputPayload(decoded); ok {
|
||||
return content
|
||||
}
|
||||
}
|
||||
return string(data)
|
||||
}
|
||||
|
||||
func parseUserInputPayload(data []byte) (string, bool) {
|
||||
var stored userInputStoragePayload
|
||||
if err := json.Unmarshal(data, &stored); err == nil && stored.Encoding == "plaintext" {
|
||||
return stored.Content, true
|
||||
}
|
||||
|
||||
var payload userInputPayload
|
||||
if err := json.Unmarshal(data, &payload); err == nil && (len(payload.Content) > 0 || len(payload.Attachments) > 0) {
|
||||
return string(payload.Content), true
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
|
||||
func normalizeSummaryLogStore(store *consts.LogStore) consts.LogStore {
|
||||
|
||||
@@ -40,7 +40,7 @@ func TestTasklogConversationReaderFetchClickHouseChunks(t *testing.T) {
|
||||
gateway := &fakeTasklogGateway{responses: []*tasklog.QueryTurnsResp{{
|
||||
Chunks: []*tasklog.TurnChunk{
|
||||
{Event: "task-running", Data: []byte(base64.StdEncoding.EncodeToString(runningData)), Timestamp: 2},
|
||||
{Event: "user-input", Data: []byte(base64.StdEncoding.EncodeToString([]byte("请帮我修复测试"))), Timestamp: 1},
|
||||
{Event: "user-input", Data: []byte(`{"encoding":"plaintext","content":"请帮我修复测试"}`), Timestamp: 1},
|
||||
},
|
||||
}}}
|
||||
reader := newTasklogConversationReader(gateway, slog.New(slog.NewTextHandler(io.Discard, nil)))
|
||||
@@ -94,19 +94,95 @@ func TestBuildSummaryConversationUsesUserInputPayloadContent(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildSummaryConversationKeepsRawBase64LookingUserInput(t *testing.T) {
|
||||
taskID := uuid.New()
|
||||
messages, err := buildSummaryConversation(
|
||||
context.Background(),
|
||||
slog.New(slog.NewTextHandler(io.Discard, nil)),
|
||||
taskID,
|
||||
[]*tasklog.TurnChunk{{Event: "user-input", Data: []byte("aGVsbG8="), Timestamp: 1}},
|
||||
1,
|
||||
1,
|
||||
"",
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("buildSummaryConversation() error = %v", err)
|
||||
}
|
||||
if len(messages) != 1 || messages[0].Content != "aGVsbG8=" {
|
||||
t.Fatalf("messages = %#v", messages)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildSummaryConversationUsesPlaintextUserInputPayloadContent(t *testing.T) {
|
||||
taskID := uuid.New()
|
||||
messages, err := buildSummaryConversation(
|
||||
context.Background(),
|
||||
slog.New(slog.NewTextHandler(io.Discard, nil)),
|
||||
taskID,
|
||||
[]*tasklog.TurnChunk{{Event: "user-input", Data: []byte(`{"encoding":"plaintext","content":"继续实现"}`), Timestamp: 1}},
|
||||
1,
|
||||
1,
|
||||
"",
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("buildSummaryConversation() error = %v", err)
|
||||
}
|
||||
if len(messages) != 1 || messages[0].Content != "继续实现" {
|
||||
t.Fatalf("messages = %#v", messages)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildSummaryConversationKeepsPlaintextBase64LookingContent(t *testing.T) {
|
||||
taskID := uuid.New()
|
||||
messages, err := buildSummaryConversation(
|
||||
context.Background(),
|
||||
slog.New(slog.NewTextHandler(io.Discard, nil)),
|
||||
taskID,
|
||||
[]*tasklog.TurnChunk{{Event: "user-input", Data: []byte(`{"encoding":"plaintext","content":"aGVsbG8="}`), Timestamp: 1}},
|
||||
1,
|
||||
1,
|
||||
"",
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("buildSummaryConversation() error = %v", err)
|
||||
}
|
||||
if len(messages) != 1 || messages[0].Content != "aGVsbG8=" {
|
||||
t.Fatalf("messages = %#v", messages)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildSummaryConversationKeepsEmptyPlaintextUserInputPayloadContent(t *testing.T) {
|
||||
taskID := uuid.New()
|
||||
messages, err := buildSummaryConversation(
|
||||
context.Background(),
|
||||
slog.New(slog.NewTextHandler(io.Discard, nil)),
|
||||
taskID,
|
||||
[]*tasklog.TurnChunk{{Event: "user-input", Data: []byte(`{"encoding":"plaintext","content":""}`), Timestamp: 1}},
|
||||
1,
|
||||
1,
|
||||
"",
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("buildSummaryConversation() error = %v", err)
|
||||
}
|
||||
if len(messages) != 1 || messages[0].Content != "" {
|
||||
t.Fatalf("messages = %#v", messages)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTasklogConversationReaderStopsWhenMaxRoundsReached(t *testing.T) {
|
||||
runningData := mustJSON(t, wsData{Update: wsUpdate{SessionUpdate: "agent_message_chunk", Content: wsContent{Text: "助手回复"}}})
|
||||
gateway := &fakeTasklogGateway{responses: []*tasklog.QueryTurnsResp{
|
||||
{
|
||||
Chunks: []*tasklog.TurnChunk{
|
||||
{Event: "user-input", Data: []byte(base64.StdEncoding.EncodeToString([]byte("第一轮"))), Timestamp: 1},
|
||||
{Event: "user-input", Data: []byte("第一轮"), Timestamp: 1},
|
||||
{Event: "task-running", Data: []byte(base64.StdEncoding.EncodeToString(runningData)), Timestamp: 2},
|
||||
},
|
||||
HasMore: true,
|
||||
NextCursor: "next",
|
||||
},
|
||||
{
|
||||
Chunks: []*tasklog.TurnChunk{{Event: "user-input", Data: []byte(base64.StdEncoding.EncodeToString([]byte("不应读取"))), Timestamp: 0}},
|
||||
Chunks: []*tasklog.TurnChunk{{Event: "user-input", Data: []byte("不应读取"), Timestamp: 0}},
|
||||
},
|
||||
}}
|
||||
reader := newTasklogConversationReader(gateway, slog.New(slog.NewTextHandler(io.Discard, nil)))
|
||||
@@ -130,9 +206,9 @@ func TestTasklogConversationReaderKeepsOnlyRecentMaxRoundsInSinglePage(t *testin
|
||||
olderRunningData := mustJSON(t, wsData{Update: wsUpdate{SessionUpdate: "agent_message_chunk", Content: wsContent{Text: "更旧助手"}}})
|
||||
gateway := &fakeTasklogGateway{responses: []*tasklog.QueryTurnsResp{{
|
||||
Chunks: []*tasklog.TurnChunk{
|
||||
{Event: "user-input", Data: []byte(base64.StdEncoding.EncodeToString([]byte("最新用户"))), Timestamp: 1},
|
||||
{Event: "user-input", Data: []byte("最新用户"), Timestamp: 1},
|
||||
{Event: "task-running", Data: []byte(base64.StdEncoding.EncodeToString(latestRunningData)), Timestamp: 2},
|
||||
{Event: "user-input", Data: []byte(base64.StdEncoding.EncodeToString([]byte("更旧用户"))), Timestamp: 3},
|
||||
{Event: "user-input", Data: []byte("更旧用户"), Timestamp: 3},
|
||||
{Event: "task-running", Data: []byte(base64.StdEncoding.EncodeToString(olderRunningData)), Timestamp: 4},
|
||||
},
|
||||
}}}
|
||||
@@ -169,7 +245,7 @@ func TestTasklogConversationReaderUsesInitialContentWhenLogRoundsAreInsufficient
|
||||
func TestTasklogConversationReaderDoesNotUseInitialContentWhenLogRoundsAreEnough(t *testing.T) {
|
||||
gateway := &fakeTasklogGateway{responses: []*tasklog.QueryTurnsResp{{
|
||||
Chunks: []*tasklog.TurnChunk{
|
||||
{Event: "user-input", Data: []byte(base64.StdEncoding.EncodeToString([]byte("实现登录页"))), Timestamp: 1},
|
||||
{Event: "user-input", Data: []byte("实现登录页"), Timestamp: 1},
|
||||
},
|
||||
}}}
|
||||
reader := newTasklogConversationReader(gateway, slog.New(slog.NewTextHandler(io.Discard, nil)))
|
||||
|
||||
@@ -16,6 +16,12 @@ type userInputPayload struct {
|
||||
Attachments []taskAttachment `json:"attachments"`
|
||||
}
|
||||
|
||||
type userInputStoragePayload struct {
|
||||
Encoding string `json:"encoding"`
|
||||
Content string `json:"content"`
|
||||
Attachments []taskAttachment `json:"attachments"`
|
||||
}
|
||||
|
||||
type taskAttachment struct {
|
||||
URL string `json:"url"`
|
||||
Filename string `json:"filename"`
|
||||
|
||||
Reference in new issue
Block a user