|
| 1 | +package llm |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/base64" |
| 6 | + "testing" |
| 7 | + |
| 8 | + genai "google.golang.org/genai" |
| 9 | +) |
| 10 | + |
| 11 | +func TestGoogleClient_ToolCallThoughtSignatureRoundTrip(t *testing.T) { |
| 12 | + signature := []byte{0xde, 0xad, 0xbe, 0xef} |
| 13 | + |
| 14 | + part := genai.NewPartFromFunctionCall("do_stuff", map[string]any{"value": "x"}) |
| 15 | + part.Thought = true |
| 16 | + part.ThoughtSignature = signature |
| 17 | + |
| 18 | + content := genai.NewContentFromParts([]*genai.Part{part}, genai.RoleModel) |
| 19 | + |
| 20 | + toolCalls := convertToolCallsFromContent(content) |
| 21 | + if len(toolCalls) != 1 { |
| 22 | + t.Fatalf("expected 1 tool call, got %d", len(toolCalls)) |
| 23 | + } |
| 24 | + |
| 25 | + tc := toolCalls[0] |
| 26 | + sig, ok := tc["thought_signature"].(string) |
| 27 | + if !ok || sig == "" { |
| 28 | + t.Fatalf("expected thought_signature to be captured, got %#v", tc["thought_signature"]) |
| 29 | + } |
| 30 | + if sig != base64.StdEncoding.EncodeToString(signature) { |
| 31 | + t.Fatalf("expected signature %q, got %q", base64.StdEncoding.EncodeToString(signature), sig) |
| 32 | + } |
| 33 | + |
| 34 | + assistantMsg, err := convertAssistantMessage(&Message{Role: "assistant", ToolCalls: toolCalls}) |
| 35 | + if err != nil { |
| 36 | + t.Fatalf("convertAssistantMessage returned error: %v", err) |
| 37 | + } |
| 38 | + |
| 39 | + if len(assistantMsg.Parts) != 1 { |
| 40 | + t.Fatalf("expected 1 part after round-trip, got %d", len(assistantMsg.Parts)) |
| 41 | + } |
| 42 | + |
| 43 | + resultPart := assistantMsg.Parts[0] |
| 44 | + if !resultPart.Thought { |
| 45 | + t.Fatalf("expected Thought to be preserved") |
| 46 | + } |
| 47 | + if !bytes.Equal(resultPart.ThoughtSignature, signature) { |
| 48 | + t.Fatalf("expected signature %v, got %v", signature, resultPart.ThoughtSignature) |
| 49 | + } |
| 50 | + if resultPart.FunctionCall == nil || resultPart.FunctionCall.Name != "do_stuff" { |
| 51 | + t.Fatalf("expected function call to be preserved, got %+v", resultPart.FunctionCall) |
| 52 | + } |
| 53 | +} |
0 commit comments