Go implementation of DeepSeek Harness and self-hosted personal agent.
Go
fix(llm): encode text Responses tool outputs as strings
xycadd6f5739fc0
diff --git a/docs/providers.md b/docs/providers.mdindex 6da63ed..e26f397 100644--- a/docs/providers.md+++ b/docs/providers.md@@ -42,6 +42,14 @@ is the endpoint default. The account default endpoint applies to new chats;existing chats keep their selection and can switch endpoints/models independently.A switch during generation applies to the next turn.+Responses requests encode text-only function tool outputs as strings, including+outputs replayed from older sessions, for compatibility with providers that do+not accept content arrays in `function_call_output.output`. Multiple text blocks+are joined with newlines; empty results use an empty string. Tool outputs with+images retain their ordered content arrays at the wire encoder. The main model+adapter continues to send tool images in a following user message. Stored session+content and Anthropic encoding are unchanged.+For Anthropic, use `https://api.anthropic.com/v1` or your provider's compatiblebase URL; do not append `/messages`. The Host appends `/messages`, sends`x-api-key` and `anthropic-version: 2023-06-01`, and normalizes text, tool use,diff --git a/tests/integration/browser_use_docker_test.go b/tests/integration/browser_use_docker_test.goindex e812929..0f1e270 100644--- a/tests/integration/browser_use_docker_test.go+++ b/tests/integration/browser_use_docker_test.go@@ -46,9 +46,10 @@ func TestDockerBrowserSkillAndSessionIsolation(t *testing.T) {mux.HandleFunc("/responses", func(w http.ResponseWriter, r *http.Request) {var body struct {Input []struct {- Role, Type string- CallID string `json:"call_id"`- Content, Output []struct{ Text string }+ Role, Type string+ CallID string `json:"call_id"`+ Content []struct{ Text string }+ Output string}Tools []struct{ Name string }}@@ -72,8 +73,8 @@ func TestDockerBrowserSkillAndSessionIsolation(t *testing.T) {!strings.HasPrefix(item.Content[0].Text, "Current runtime context.") {userMessage = item.Content[0].Text}- if item.Type == "function_call_output" && len(item.Output) > 0 {- outputs[item.CallID] = item.Output[0].Text+ if item.Type == "function_call_output" {+ outputs[item.CallID] = item.Output}}var output map[string]anydiff --git a/tests/integration/memory_docker_test.go b/tests/integration/memory_docker_test.goindex dfbb229..f37644e 100644--- a/tests/integration/memory_docker_test.go+++ b/tests/integration/memory_docker_test.go@@ -78,7 +78,7 @@ func testMemoryAndHistoryRebuild(t *testing.T, profile, account, fixtureURL striRole stringCallID string `json:"call_id"`Content []struct{ Text string }- Output []struct{ Text string }+ Output string}}raw, err := io.ReadAll(r.Body)@@ -134,10 +134,10 @@ func testMemoryAndHistoryRebuild(t *testing.T, profile, account, fixtureURL stri}var result *sessionsearch.Resultfor _, item := range body.Input {- if item.Type == "function_call_output" && item.CallID == callID && len(item.Output) == 1 {+ if item.Type == "function_call_output" && item.CallID == callID {var decoded sessionsearch.Result- if err := json.Unmarshal([]byte(item.Output[0].Text), &decoded); err != nil {- t.Errorf("session_search output = %q", item.Output[0].Text)+ if err := json.Unmarshal([]byte(item.Output), &decoded); err != nil {+ t.Errorf("session_search output = %q", item.Output)}result = &decoded}diff --git a/tests/integration/web_parity_docker_test.go b/tests/integration/web_parity_docker_test.goindex 37dc279..3ad90af 100644--- a/tests/integration/web_parity_docker_test.go+++ b/tests/integration/web_parity_docker_test.go@@ -87,7 +87,7 @@ func TestDockerWebToolParity(t *testing.T) {Input []struct {Type stringCallID string `json:"call_id"`- Output []struct{ Type, Text string }+ Output string}}if err := json.NewDecoder(r.Body).Decode(&body); err != nil {@@ -105,11 +105,7 @@ func TestDockerWebToolParity(t *testing.T) {observed := map[string]string{}for _, item := range body.Input {if item.Type == "function_call_output" {- if len(item.Output) != 1 || item.Output[0].Type != "input_text" {- t.Errorf("unexpected tool result content blocks: %v", item.Output)- continue- }- observed[item.CallID] = item.Output[0].Text+ observed[item.CallID] = item.Output}}var output []map[string]anydiff --git a/thirdparty/llm/client_test.go b/thirdparty/llm/client_test.goindex f843a47..0f7cf95 100644--- a/thirdparty/llm/client_test.go+++ b/thirdparty/llm/client_test.go@@ -4,6 +4,7 @@ import ("context""encoding/json""errors"+ "fmt""net/http""net/http/httptest""reflect"@@ -211,7 +212,7 @@ func TestResponsesToolResultKeepsBlockOrder(t *testing.T) {}}-func TestResponsesHistoricalTextToolResultStaysAnArray(t *testing.T) {+func TestResponsesHistoricalTextToolResultUsesString(t *testing.T) {input, err := encodeResponsesInput([]Item{{Type: "tool_call", CallID: "call-1", Name: "lookup", Arguments: `{}`},{Type: "tool_result", CallID: "call-1", Content: []ContentBlock{{Type: "text", Text: "first"}, {Type: "text", Text: " second"}}},@@ -223,15 +224,84 @@ func TestResponsesHistoricalTextToolResultStaysAnArray(t *testing.T) {if err != nil {t.Fatal(err)}- if got := input[1].(map[string]any)["output"].([]any); len(got) != 2 ||- got[0].(map[string]string)["text"] != "first" || got[1].(map[string]string)["text"] != " second" {- t.Fatalf("historical text tool result lost its block order: %#v", got)+ if got := input[1].(map[string]any)["output"]; got != "first\n second" {+ t.Fatalf("historical text tool result = %#v", got)}if parts, ok := input[2].(map[string]any)["content"].([]any); !ok || len(parts) != 2 {t.Fatalf("generated image message lost its ordered blocks: %#v", input[2])}}+func TestResponsesTextToolOutputsWorkWithStringOnlyProvider(t *testing.T) {+ for _, stream := range []bool{false, true} {+ for _, test := range []struct {+ name string+ content []ContentBlock+ want string+ }{+ {name: "nil"},+ {name: "empty", content: []ContentBlock{}},+ {name: "empty text", content: []ContentBlock{{Type: "text"}}},+ {name: "escaped text", content: []ContentBlock{{Type: "text", Text: "搜索结果:\n\"quoted\" \\ path\tend"}}, want: "搜索结果:\n\"quoted\" \\ path\tend"},+ {name: "JSON array data", content: []ContentBlock{{Type: "text", Text: `[{"title":"Go","url":"https://go.dev/"}]`}}, want: `[{"title":"Go","url":"https://go.dev/"}]`},+ {name: "multiple blocks", content: []ContentBlock{{Type: "text", Text: "first"}, {Type: "text", Text: "second"}}, want: "first\nsecond"},+ } {+ t.Run(fmt.Sprintf("stream=%t/%s", stream, test.name), func(t *testing.T) {+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ // Decode into a string field to model a provider that rejects arrays.+ var request struct {+ Input []struct {+ Type string+ CallID string `json:"call_id"`+ Output string+ }+ }+ if err := json.NewDecoder(r.Body).Decode(&request); err != nil {+ t.Errorf("decode string-only tool output: %v", err)+ http.Error(w, `{"error":{"message":"invalid JSON body"}}`, http.StatusBadRequest)+ return+ }+ outputs := map[string]string{}+ for _, item := range request.Input {+ if item.Type == "function_call_output" {+ outputs[item.CallID] = item.Output+ }+ }+ if !reflect.DeepEqual(outputs, map[string]string{"old-call": "old\nresult", "new-call": test.want}) {+ t.Errorf("tool outputs = %#v", outputs)+ }+ if stream {+ w.Header().Set("Content-Type", "text/event-stream")+ fmt.Fprint(w, "data: {\"type\":\"response.completed\",\"response\":{\"status\":\"completed\",\"output\":[]}}\n\n")+ } else {+ fmt.Fprint(w, `{"status":"completed","output":[]}`)+ }+ }))+ defer server.Close()+ client, err := NewClient(Endpoint{BaseURL: server.URL}, "model", server.Client())+ if err != nil {+ t.Fatal(err)+ }+ request := Request{Input: []Item{+ {Type: "tool_call", CallID: "old-call", Name: "lookup", Arguments: `{}`},+ {Type: "tool_result", CallID: "old-call", Content: []ContentBlock{{Type: "text", Text: "old"}, {Type: "text", Text: "result"}}},+ {Type: "message", Role: "user", Content: []ContentBlock{{Type: "text", Text: "search again"}}},+ {Type: "tool_call", CallID: "new-call", Name: "lookup", Arguments: `{}`},+ {Type: "tool_result", CallID: "new-call", Content: test.content},+ }}+ if stream {+ _, err = client.Stream(context.Background(), request, nil)+ } else {+ _, err = client.Generate(context.Background(), request)+ }+ if err != nil {+ t.Fatal(err)+ }+ })+ }+ }+}+func TestChatCompletionsExplicitAndImageRole(t *testing.T) {var path stringvar request map[string]anydiff --git a/thirdparty/llm/encode.go b/thirdparty/llm/encode.goindex e62954b..97a93e2 100644--- a/thirdparty/llm/encode.go+++ b/thirdparty/llm/encode.go@@ -42,11 +42,11 @@ func encodeResponsesInput(input []Item) ([]any, error) {}out = append(out, value)case "tool_result":- parts, err := responsesParts("user", item.Content)+ output, err := responsesToolOutput(item.Content)if err != nil {return nil, fmt.Errorf("tool result %q: %w", item.CallID, err)}- out = append(out, map[string]any{"type": "function_call_output", "call_id": item.CallID, "output": parts})+ out = append(out, map[string]any{"type": "function_call_output", "call_id": item.CallID, "output": output})default:if len(item.Raw) > 0 {out = append(out, json.RawMessage(item.Raw))@@ -56,6 +56,19 @@ func encodeResponsesInput(input []Item) ([]any, error) {return out, nil}+func responsesToolOutput(blocks []ContentBlock) (any, error) {+ texts := make([]string, 0, len(blocks))+ for _, block := range blocks {+ if block.Type != "text" {+ return responsesParts("user", blocks)+ }+ texts = append(texts, block.Text)+ }+ // Strings also work with Responses providers that do not accept content+ // arrays in tool outputs. Keep multimodal results as ordered content blocks.+ return strings.Join(texts, "\n"), nil+}+func responsesParts(role string, blocks []ContentBlock) ([]any, error) {parts := make([]any, 0, len(blocks))for _, block := range blocks {