Unified · Split
| @@ -6,7 +6,7 @@ cliproxy · 5 unmodified lines | ||
| 6 | 6 | # Configuration |
| 7 | 7 | config.yaml |
| 8 | 8 | .env |
| 9 | − | |
| 9 | +.mcp.json | |
| 10 | 10 | # Generated content |
| 11 | 11 | bin/* |
| 12 | 12 | logs/* |
| @@ -796,10 +796,10 @@ func (h *Handler) DeleteOAuthModelAlias(c *gin.Context) { · 795 unmodified lines | ||
| 796 | 796 | c.JSON(404, gin.H{"error": "channel not found"}) |
| 797 | 797 | return |
| 798 | 798 | } |
| 799 | − delete(h.cfg.OAuthModelAlias, channel) | |
| 800 | − if len(h.cfg.OAuthModelAlias) == 0 { | |
| 801 | − h.cfg.OAuthModelAlias = nil | |
| 802 | − } | |
| 799 | + // Set to nil instead of deleting the key so that the "explicitly disabled" | |
| 800 | + // marker survives config reload and prevents SanitizeOAuthModelAlias from | |
| 801 | + // re-injecting default aliases (fixes #222). | |
| 802 | + h.cfg.OAuthModelAlias[channel] = nil | |
| 803 | 803 | h.persist(c) |
| 804 | 804 | } |
| 805 | 805 | |
| @@ -767,7 +767,13 @@ func (cfg *Config) SanitizeOAuthModelAlias() { · 766 unmodified lines | ||
| 767 | 767 | out := make(map[string][]OAuthModelAlias, len(cfg.OAuthModelAlias)) |
| 768 | 768 | for rawChannel, aliases := range cfg.OAuthModelAlias { |
| 769 | 769 | channel := strings.ToLower(strings.TrimSpace(rawChannel)) |
| 770 | − if channel == "" || len(aliases) == 0 { | |
| 770 | + if channel == "" { | |
| 771 | + continue | |
| 772 | + } | |
| 773 | + // Preserve channels that were explicitly set to empty/nil – they act | |
| 774 | + // as "disabled" markers so default injection won't re-add them (#222). | |
| 775 | + if len(aliases) == 0 { | |
| 776 | + out[channel] = nil | |
| 771 | 777 | continue |
| 772 | 778 | } |
| 773 | 779 | seenAlias := make(map[string]struct{}, len(aliases)) |
| @@ -128,6 +128,50 @@ func TestSanitizeOAuthModelAlias_DoesNotOverrideUserKiroAliases(t *testing.T) { · 127 unmodified lines | ||
| 128 | 128 | } |
| 129 | 129 | } |
| 130 | 130 | |
| 131 | +func TestSanitizeOAuthModelAlias_DoesNotReinjectAfterExplicitDeletion(t *testing.T) { | |
| 132 | + // When user explicitly deletes kiro aliases (key exists with nil value), | |
| 133 | + // defaults should NOT be re-injected on subsequent sanitize calls (#222). | |
| 134 | + cfg := &Config{ | |
| 135 | + OAuthModelAlias: map[string][]OAuthModelAlias{ | |
| 136 | + "kiro": nil, // explicitly deleted | |
| 137 | + "codex": {{Name: "gpt-5", Alias: "g5"}}, | |
| 138 | + }, | |
| 139 | + } | |
| 140 | + | |
| 141 | + cfg.SanitizeOAuthModelAlias() | |
| 142 | + | |
| 143 | + kiroAliases := cfg.OAuthModelAlias["kiro"] | |
| 144 | + if len(kiroAliases) != 0 { | |
| 145 | + t.Fatalf("expected kiro aliases to remain empty after explicit deletion, got %d aliases", len(kiroAliases)) | |
| 146 | + } | |
| 147 | + // The key itself must still be present to prevent re-injection on next reload | |
| 148 | + if _, exists := cfg.OAuthModelAlias["kiro"]; !exists { | |
| 149 | + t.Fatal("expected kiro key to be preserved as nil marker after sanitization") | |
| 150 | + } | |
| 151 | + // Other channels should be unaffected | |
| 152 | + if len(cfg.OAuthModelAlias["codex"]) != 1 { | |
| 153 | + t.Fatal("expected codex aliases to be preserved") | |
| 154 | + } | |
| 155 | +} | |
| 156 | + | |
| 157 | +func TestSanitizeOAuthModelAlias_DoesNotReinjectAfterExplicitDeletionEmpty(t *testing.T) { | |
| 158 | + // Same as above but with empty slice instead of nil (PUT with empty body). | |
| 159 | + cfg := &Config{ | |
| 160 | + OAuthModelAlias: map[string][]OAuthModelAlias{ | |
| 161 | + "kiro": {}, // explicitly set to empty | |
| 162 | + }, | |
| 163 | + } | |
| 164 | + | |
| 165 | + cfg.SanitizeOAuthModelAlias() | |
| 166 | + | |
| 167 | + if len(cfg.OAuthModelAlias["kiro"]) != 0 { | |
| 168 | + t.Fatalf("expected kiro aliases to remain empty, got %d aliases", len(cfg.OAuthModelAlias["kiro"])) | |
| 169 | + } | |
| 170 | + if _, exists := cfg.OAuthModelAlias["kiro"]; !exists { | |
| 171 | + t.Fatal("expected kiro key to be preserved") | |
| 172 | + } | |
| 173 | +} | |
| 174 | + | |
| 131 | 175 | func TestSanitizeOAuthModelAlias_InjectsDefaultKiroWhenEmpty(t *testing.T) { |
| 132 | 176 | // When OAuthModelAlias is nil, kiro defaults should still be injected |
| 133 | 177 | cfg := &Config{} |
| @@ -1007,7 +1007,12 @@ func (e *AntigravityExecutor) CountTokens(ctx context.Context, auth *cliproxyaut · 1006 unmodified lines | ||
| 1007 | 1007 | func FetchAntigravityModels(ctx context.Context, auth *cliproxyauth.Auth, cfg *config.Config) []*registry.ModelInfo { |
| 1008 | 1008 | exec := &AntigravityExecutor{cfg: cfg} |
| 1009 | 1009 | token, updatedAuth, errToken := exec.ensureAccessToken(ctx, auth) |
| 1010 | − if errToken != nil || token == "" { | |
| 1010 | + if errToken != nil { | |
| 1011 | + log.Warnf("antigravity executor: fetch models failed for %s: token error: %v", auth.ID, errToken) | |
| 1012 | + return nil | |
| 1013 | + } | |
| 1014 | + if token == "" { | |
| 1015 | + log.Warnf("antigravity executor: fetch models failed for %s: got empty token", auth.ID) | |
| 1011 | 1016 | return nil |
| 1012 | 1017 | } |
| 1013 | 1018 | if updatedAuth != nil { |
| @@ -1021,6 +1026,7 @@ func FetchAntigravityModels(ctx context.Context, auth *cliproxyauth.Auth, cfg *c · 7 unmodified lines | ||
| 1021 | 1026 | modelsURL := baseURL + antigravityModelsPath |
| 1022 | 1027 | httpReq, errReq := http.NewRequestWithContext(ctx, http.MethodPost, modelsURL, bytes.NewReader([]byte(`{}`))) |
| 1023 | 1028 | if errReq != nil { |
| 1029 | + log.Warnf("antigravity executor: fetch models failed for %s: create request error: %v", auth.ID, errReq) | |
| 1024 | 1030 | return nil |
| 1025 | 1031 | } |
| 1026 | 1032 | httpReq.Header.Set("Content-Type", "application/json") |
| @@ -1033,12 +1039,14 @@ func FetchAntigravityModels(ctx context.Context, auth *cliproxyauth.Auth, cfg *c · 6 unmodified lines | ||
| 1033 | 1039 | httpResp, errDo := httpClient.Do(httpReq) |
| 1034 | 1040 | if errDo != nil { |
| 1035 | 1041 | if errors.Is(errDo, context.Canceled) || errors.Is(errDo, context.DeadlineExceeded) { |
| 1042 | + log.Warnf("antigravity executor: fetch models failed for %s: context canceled: %v", auth.ID, errDo) | |
| 1036 | 1043 | return nil |
| 1037 | 1044 | } |
| 1038 | 1045 | if idx+1 < len(baseURLs) { |
| 1039 | 1046 | log.Debugf("antigravity executor: models request error on base url %s, retrying with fallback base url: %s", baseURL, baseURLs[idx+1]) |
| 1040 | 1047 | continue |
| 1041 | 1048 | } |
| 1049 | + log.Warnf("antigravity executor: fetch models failed for %s: request error: %v", auth.ID, errDo) | |
| 1042 | 1050 | return nil |
| 1043 | 1051 | } |
| 1044 | 1052 | |
| @@ -1051,6 +1059,7 @@ func FetchAntigravityModels(ctx context.Context, auth *cliproxyauth.Auth, cfg *c · 6 unmodified lines | ||
| 1051 | 1059 | log.Debugf("antigravity executor: models read error on base url %s, retrying with fallback base url: %s", baseURL, baseURLs[idx+1]) |
| 1052 | 1060 | continue |
| 1053 | 1061 | } |
| 1062 | + log.Warnf("antigravity executor: fetch models failed for %s: read body error: %v", auth.ID, errRead) | |
| 1054 | 1063 | return nil |
| 1055 | 1064 | } |
| 1056 | 1065 | if httpResp.StatusCode < http.StatusOK || httpResp.StatusCode >= http.StatusMultipleChoices { |
| @@ -1058,11 +1067,13 @@ func FetchAntigravityModels(ctx context.Context, auth *cliproxyauth.Auth, cfg *c · 1 unmodified line | ||
| 1058 | 1067 | log.Debugf("antigravity executor: models request rate limited on base url %s, retrying with fallback base url: %s", baseURL, baseURLs[idx+1]) |
| 1059 | 1068 | continue |
| 1060 | 1069 | } |
| 1070 | + log.Warnf("antigravity executor: fetch models failed for %s: unexpected status %d, body: %s", auth.ID, httpResp.StatusCode, string(bodyBytes)) | |
| 1061 | 1071 | return nil |
| 1062 | 1072 | } |
| 1063 | 1073 | |
| 1064 | 1074 | result := gjson.GetBytes(bodyBytes, "models") |
| 1065 | 1075 | if !result.Exists() { |
| 1076 | + log.Warnf("antigravity executor: fetch models failed for %s: no models field in response, body: %s", auth.ID, string(bodyBytes)) | |
| 1066 | 1077 | return nil |
| 1067 | 1078 | } |
| 1068 | 1079 | |
| @@ -110,7 +110,7 @@ func (e *GitHubCopilotExecutor) Execute(ctx context.Context, auth *cliproxyauth. · 109 unmodified lines | ||
| 110 | 110 | defer reporter.trackFailure(ctx, &err) |
| 111 | 111 | |
| 112 | 112 | from := opts.SourceFormat |
| 113 | − useResponses := useGitHubCopilotResponsesEndpoint(from) | |
| 113 | + useResponses := useGitHubCopilotResponsesEndpoint(from, req.Model) | |
| 114 | 114 | to := sdktranslator.FromString("openai") |
| 115 | 115 | if useResponses { |
| 116 | 116 | to = sdktranslator.FromString("openai-response") |
| @@ -133,6 +133,12 @@ func (e *GitHubCopilotExecutor) Execute(ctx context.Context, auth *cliproxyauth. · 16 unmodified lines | ||
| 133 | 133 | return resp, err |
| 134 | 134 | } |
| 135 | 135 | |
| 136 | + if useResponses { | |
| 137 | + body = normalizeGitHubCopilotResponsesInput(body) | |
| 138 | + body = normalizeGitHubCopilotResponsesTools(body) | |
| 139 | + } else { | |
| 140 | + body = normalizeGitHubCopilotChatTools(body) | |
| 141 | + } | |
| 136 | 142 | requestedModel := payloadRequestedModel(opts, req.Model) |
| 137 | 143 | body = applyPayloadConfigWithRoot(e.cfg, req.Model, to.String(), "", body, originalTranslated, requestedModel) |
| 138 | 144 | body, _ = sjson.SetBytes(body, "stream", false) |
| @@ -209,7 +215,12 @@ func (e *GitHubCopilotExecutor) Execute(ctx context.Context, auth *cliproxyauth. · 70 unmodified lines | ||
| 209 | 215 | } |
| 210 | 216 | |
| 211 | 217 | var param any |
| 212 | − converted := sdktranslator.TranslateNonStream(ctx, to, from, req.Model, bytes.Clone(opts.OriginalRequest), body, data, ¶m) | |
| 218 | + converted := "" | |
| 219 | + if useResponses && from.String() == "claude" { | |
| 220 | + converted = translateGitHubCopilotResponsesNonStreamToClaude(data) | |
| 221 | + } else { | |
| 222 | + converted = sdktranslator.TranslateNonStream(ctx, to, from, req.Model, bytes.Clone(opts.OriginalRequest), body, data, ¶m) | |
| 223 | + } | |
| 213 | 224 | resp = cliproxyexecutor.Response{Payload: []byte(converted)} |
| 214 | 225 | reporter.ensurePublished(ctx) |
| 215 | 226 | return resp, nil |
| @@ -226,7 +237,7 @@ func (e *GitHubCopilotExecutor) ExecuteStream(ctx context.Context, auth *cliprox · 10 unmodified lines | ||
| 226 | 237 | defer reporter.trackFailure(ctx, &err) |
| 227 | 238 | |
| 228 | 239 | from := opts.SourceFormat |
| 229 | − useResponses := useGitHubCopilotResponsesEndpoint(from) | |
| 240 | + useResponses := useGitHubCopilotResponsesEndpoint(from, req.Model) | |
| 230 | 241 | to := sdktranslator.FromString("openai") |
| 231 | 242 | if useResponses { |
| 232 | 243 | to = sdktranslator.FromString("openai-response") |
| @@ -249,6 +260,12 @@ func (e *GitHubCopilotExecutor) ExecuteStream(ctx context.Context, auth *cliprox · 16 unmodified lines | ||
| 249 | 260 | return nil, err |
| 250 | 261 | } |
| 251 | 262 | |
| 263 | + if useResponses { | |
| 264 | + body = normalizeGitHubCopilotResponsesInput(body) | |
| 265 | + body = normalizeGitHubCopilotResponsesTools(body) | |
| 266 | + } else { | |
| 267 | + body = normalizeGitHubCopilotChatTools(body) | |
| 268 | + } | |
| 252 | 269 | requestedModel := payloadRequestedModel(opts, req.Model) |
| 253 | 270 | body = applyPayloadConfigWithRoot(e.cfg, req.Model, to.String(), "", body, originalTranslated, requestedModel) |
| 254 | 271 | body, _ = sjson.SetBytes(body, "stream", true) |
| @@ -349,7 +366,12 @@ func (e *GitHubCopilotExecutor) ExecuteStream(ctx context.Context, auth *cliprox · 94 unmodified lines | ||
| 349 | 366 | } |
| 350 | 367 | } |
| 351 | 368 | |
| 352 | − chunks := sdktranslator.TranslateStream(ctx, to, from, req.Model, bytes.Clone(opts.OriginalRequest), body, bytes.Clone(line), ¶m) | |
| 369 | + var chunks []string | |
| 370 | + if useResponses && from.String() == "claude" { | |
| 371 | + chunks = translateGitHubCopilotResponsesStreamToClaude(bytes.Clone(line), ¶m) | |
| 372 | + } else { | |
| 373 | + chunks = sdktranslator.TranslateStream(ctx, to, from, req.Model, bytes.Clone(opts.OriginalRequest), body, bytes.Clone(line), ¶m) | |
| 374 | + } | |
| 353 | 375 | for i := range chunks { |
| 354 | 376 | out <- cliproxyexecutor.StreamChunk{Payload: []byte(chunks[i])} |
| 355 | 377 | } |
| @@ -503,8 +525,12 @@ func (e *GitHubCopilotExecutor) normalizeModel(model string, body []byte) []byte · 147 unmodified lines | ||
| 503 | 525 | return body |
| 504 | 526 | } |
| 505 | 527 | |
| 506 | −func useGitHubCopilotResponsesEndpoint(sourceFormat sdktranslator.Format) bool { | |
| 507 | − return sourceFormat.String() == "openai-response" | |
| 528 | +func useGitHubCopilotResponsesEndpoint(sourceFormat sdktranslator.Format, model string) bool { | |
| 529 | + if sourceFormat.String() == "openai-response" { | |
| 530 | + return true | |
| 531 | + } | |
| 532 | + baseModel := strings.ToLower(thinking.ParseSuffix(model).ModelName) | |
| 533 | + return strings.Contains(baseModel, "codex") | |
| 508 | 534 | } |
| 509 | 535 | |
| 510 | 536 | // flattenAssistantContent converts assistant message content from array format |
| @@ -539,6 +565,411 @@ func flattenAssistantContent(body []byte) []byte { · 28 unmodified lines | ||
| 539 | 565 | return result |
| 540 | 566 | } |
| 541 | 567 | |
| 568 | +func normalizeGitHubCopilotChatTools(body []byte) []byte { | |
| 569 | + tools := gjson.GetBytes(body, "tools") | |
| 570 | + if tools.Exists() { | |
| 571 | + filtered := "[]" | |
| 572 | + if tools.IsArray() { | |
| 573 | + for _, tool := range tools.Array() { | |
| 574 | + if tool.Get("type").String() != "function" { | |
| 575 | + continue | |
| 576 | + } | |
| 577 | + filtered, _ = sjson.SetRaw(filtered, "-1", tool.Raw) | |
| 578 | + } | |
| 579 | + } | |
| 580 | + body, _ = sjson.SetRawBytes(body, "tools", []byte(filtered)) | |
| 581 | + } | |
| 582 | + | |
| 583 | + toolChoice := gjson.GetBytes(body, "tool_choice") | |
| 584 | + if !toolChoice.Exists() { | |
| 585 | + return body | |
| 586 | + } | |
| 587 | + if toolChoice.Type == gjson.String { | |
| 588 | + switch toolChoice.String() { | |
| 589 | + case "auto", "none", "required": | |
| 590 | + return body | |
| 591 | + } | |
| 592 | + } | |
| 593 | + body, _ = sjson.SetBytes(body, "tool_choice", "auto") | |
| 594 | + return body | |
| 595 | +} | |
| 596 | + | |
| 597 | +func normalizeGitHubCopilotResponsesInput(body []byte) []byte { | |
| 598 | + input := gjson.GetBytes(body, "input") | |
| 599 | + if input.Exists() { | |
| 600 | + if input.Type == gjson.String { | |
| 601 | + return body | |
| 602 | + } | |
| 603 | + inputString := input.Raw | |
| 604 | + if input.Type != gjson.JSON { | |
| 605 | + inputString = input.String() | |
| 606 | + } | |
| 607 | + body, _ = sjson.SetBytes(body, "input", inputString) | |
| 608 | + return body | |
| 609 | + } | |
| 610 | + | |
| 611 | + var parts []string | |
| 612 | + if system := gjson.GetBytes(body, "system"); system.Exists() { | |
| 613 | + if text := strings.TrimSpace(collectTextFromNode(system)); text != "" { | |
| 614 | + parts = append(parts, text) | |
| 615 | + } | |
| 616 | + } | |
| 617 | + if messages := gjson.GetBytes(body, "messages"); messages.Exists() && messages.IsArray() { | |
| 618 | + for _, msg := range messages.Array() { | |
| 619 | + if text := strings.TrimSpace(collectTextFromNode(msg.Get("content"))); text != "" { | |
| 620 | + parts = append(parts, text) | |
| 621 | + } | |
| 622 | + } | |
| 623 | + } | |
| 624 | + body, _ = sjson.SetBytes(body, "input", strings.Join(parts, "\n")) | |
| 625 | + return body | |
| 626 | +} | |
| 627 | + | |
| 628 | +func normalizeGitHubCopilotResponsesTools(body []byte) []byte { | |
| 629 | + tools := gjson.GetBytes(body, "tools") | |
| 630 | + if tools.Exists() { | |
| 631 | + filtered := "[]" | |
| 632 | + if tools.IsArray() { | |
| 633 | + for _, tool := range tools.Array() { | |
| 634 | + toolType := tool.Get("type").String() | |
| 635 | + // Accept OpenAI format (type="function") and Claude format | |
| 636 | + // (no type field, but has top-level name + input_schema). | |
| 637 | + if toolType != "" && toolType != "function" { | |
| 638 | + continue | |
| 639 | + } | |
| 640 | + name := tool.Get("name").String() | |
| 641 | + if name == "" { | |
| 642 | + name = tool.Get("function.name").String() | |
| 643 | + } | |
| 644 | + if name == "" { | |
| 645 | + continue | |
| 646 | + } | |
| 647 | + normalized := `{"type":"function","name":""}` | |
| 648 | + normalized, _ = sjson.Set(normalized, "name", name) | |
| 649 | + if desc := tool.Get("description").String(); desc != "" { | |
| 650 | + normalized, _ = sjson.Set(normalized, "description", desc) | |
| 651 | + } else if desc = tool.Get("function.description").String(); desc != "" { | |
| 652 | + normalized, _ = sjson.Set(normalized, "description", desc) | |
| 653 | + } | |
| 654 | + if params := tool.Get("parameters"); params.Exists() { | |
| 655 | + normalized, _ = sjson.SetRaw(normalized, "parameters", params.Raw) | |
| 656 | + } else if params = tool.Get("function.parameters"); params.Exists() { | |
| 657 | + normalized, _ = sjson.SetRaw(normalized, "parameters", params.Raw) | |
| 658 | + } else if params = tool.Get("input_schema"); params.Exists() { | |
| 659 | + normalized, _ = sjson.SetRaw(normalized, "parameters", params.Raw) | |
| 660 | + } | |
| 661 | + filtered, _ = sjson.SetRaw(filtered, "-1", normalized) | |
| 662 | + } | |
| 663 | + } | |
| 664 | + body, _ = sjson.SetRawBytes(body, "tools", []byte(filtered)) | |
| 665 | + } | |
| 666 | + | |
| 667 | + toolChoice := gjson.GetBytes(body, "tool_choice") | |
| 668 | + if !toolChoice.Exists() { | |
| 669 | + return body | |
| 670 | + } | |
| 671 | + if toolChoice.Type == gjson.String { | |
| 672 | + switch toolChoice.String() { | |
| 673 | + case "auto", "none", "required": | |
| 674 | + return body | |
| 675 | + default: | |
| 676 | + body, _ = sjson.SetBytes(body, "tool_choice", "auto") | |
| 677 | + return body | |
| 678 | + } | |
| 679 | + } | |
| 680 | + if toolChoice.Type == gjson.JSON { | |
| 681 | + choiceType := toolChoice.Get("type").String() | |
| 682 | + if choiceType == "function" { | |
| 683 | + name := toolChoice.Get("name").String() | |
| 684 | + if name == "" { | |
| 685 | + name = toolChoice.Get("function.name").String() | |
| 686 | + } | |
| 687 | + if name != "" { | |
| 688 | + normalized := `{"type":"function","name":""}` | |
| 689 | + normalized, _ = sjson.Set(normalized, "name", name) | |
| 690 | + body, _ = sjson.SetRawBytes(body, "tool_choice", []byte(normalized)) | |
| 691 | + return body | |
| 692 | + } | |
| 693 | + } | |
| 694 | + } | |
| 695 | + body, _ = sjson.SetBytes(body, "tool_choice", "auto") | |
| 696 | + return body | |
| 697 | +} | |
| 698 | + | |
| 699 | +func collectTextFromNode(node gjson.Result) string { | |
| 700 | + if !node.Exists() { | |
| 701 | + return "" | |
| 702 | + } | |
| 703 | + if node.Type == gjson.String { | |
| 704 | + return node.String() | |
| 705 | + } | |
| 706 | + if node.IsArray() { | |
| 707 | + var parts []string | |
| 708 | + for _, item := range node.Array() { | |
| 709 | + if item.Type == gjson.String { | |
| 710 | + if text := item.String(); text != "" { | |
| 711 | + parts = append(parts, text) | |
| 712 | + } | |
| 713 | + continue | |
| 714 | + } | |
| 715 | + if text := item.Get("text").String(); text != "" { | |
| 716 | + parts = append(parts, text) | |
| 717 | + continue | |
| 718 | + } | |
| 719 | + if nested := collectTextFromNode(item.Get("content")); nested != "" { | |
| 720 | + parts = append(parts, nested) | |
| 721 | + } | |
| 722 | + } | |
| 723 | + return strings.Join(parts, "\n") | |
| 724 | + } | |
| 725 | + if node.Type == gjson.JSON { | |
| 726 | + if text := node.Get("text").String(); text != "" { | |
| 727 | + return text | |
| 728 | + } | |
| 729 | + if nested := collectTextFromNode(node.Get("content")); nested != "" { | |
| 730 | + return nested | |
| 731 | + } | |
| 732 | + return node.Raw | |
| 733 | + } | |
| 734 | + return node.String() | |
| 735 | +} | |
| 736 | + | |
| 737 | +type githubCopilotResponsesStreamToolState struct { | |
| 738 | + Index int | |
| 739 | + ID string | |
| 740 | + Name string | |
| 741 | +} | |
| 742 | + | |
| 743 | +type githubCopilotResponsesStreamState struct { | |
| 744 | + MessageStarted bool | |
| 745 | + MessageStopSent bool | |
| 746 | + TextBlockStarted bool | |
| 747 | + TextBlockIndex int | |
| 748 | + NextContentIndex int | |
| 749 | + HasToolUse bool | |
| 750 | + OutputIndexToTool map[int]*githubCopilotResponsesStreamToolState | |
| 751 | + ItemIDToTool map[string]*githubCopilotResponsesStreamToolState | |
| 752 | +} | |
| 753 | + | |
| 754 | +func translateGitHubCopilotResponsesNonStreamToClaude(data []byte) string { | |
| 755 | + root := gjson.ParseBytes(data) | |
| 756 | + out := `{"id":"","type":"message","role":"assistant","model":"","content":[],"stop_reason":null,"stop_sequence":null,"usage":{"input_tokens":0,"output_tokens":0}}` | |
| 757 | + out, _ = sjson.Set(out, "id", root.Get("id").String()) | |
| 758 | + out, _ = sjson.Set(out, "model", root.Get("model").String()) | |
| 759 | + | |
| 760 | + hasToolUse := false | |
| 761 | + if output := root.Get("output"); output.Exists() && output.IsArray() { | |
| 762 | + for _, item := range output.Array() { | |
| 763 | + switch item.Get("type").String() { | |
| 764 | + case "message": | |
| 765 | + if content := item.Get("content"); content.Exists() && content.IsArray() { | |
| 766 | + for _, part := range content.Array() { | |
| 767 | + if part.Get("type").String() != "output_text" { | |
| 768 | + continue | |
| 769 | + } | |
| 770 | + text := part.Get("text").String() | |
| 771 | + if text == "" { | |
| 772 | + continue | |
| 773 | + } | |
| 774 | + block := `{"type":"text","text":""}` | |
| 775 | + block, _ = sjson.Set(block, "text", text) | |
| 776 | + out, _ = sjson.SetRaw(out, "content.-1", block) | |
| 777 | + } | |
| 778 | + } | |
| 779 | + case "function_call": | |
| 780 | + hasToolUse = true | |
| 781 | + toolUse := `{"type":"tool_use","id":"","name":"","input":{}}` | |
| 782 | + toolID := item.Get("call_id").String() | |
| 783 | + if toolID == "" { | |
| 784 | + toolID = item.Get("id").String() | |
| 785 | + } | |
| 786 | + toolUse, _ = sjson.Set(toolUse, "id", toolID) | |
| 787 | + toolUse, _ = sjson.Set(toolUse, "name", item.Get("name").String()) | |
| 788 | + if args := item.Get("arguments").String(); args != "" && gjson.Valid(args) { | |
| 789 | + argObj := gjson.Parse(args) | |
| 790 | + if argObj.IsObject() { | |
| 791 | + toolUse, _ = sjson.SetRaw(toolUse, "input", argObj.Raw) | |
| 792 | + } | |
| 793 | + } | |
| 794 | + out, _ = sjson.SetRaw(out, "content.-1", toolUse) | |
| 795 | + } | |
| 796 | + } | |
| 797 | + } | |
| 798 | + | |
| 799 | + inputTokens := root.Get("usage.input_tokens").Int() | |
| 800 | + outputTokens := root.Get("usage.output_tokens").Int() | |
| 801 | + out, _ = sjson.Set(out, "usage.input_tokens", inputTokens) | |
| 802 | + out, _ = sjson.Set(out, "usage.output_tokens", outputTokens) | |
| 803 | + if hasToolUse { | |
| 804 | + out, _ = sjson.Set(out, "stop_reason", "tool_use") | |
| 805 | + } else { | |
| 806 | + out, _ = sjson.Set(out, "stop_reason", "end_turn") | |
| 807 | + } | |
| 808 | + return out | |
| 809 | +} | |
| 810 | + | |
| 811 | +func translateGitHubCopilotResponsesStreamToClaude(line []byte, param *any) []string { | |
| 812 | + if *param == nil { | |
| 813 | + *param = &githubCopilotResponsesStreamState{ | |
| 814 | + TextBlockIndex: -1, | |
| 815 | + OutputIndexToTool: make(map[int]*githubCopilotResponsesStreamToolState), | |
| 816 | + ItemIDToTool: make(map[string]*githubCopilotResponsesStreamToolState), | |
| 817 | + } | |
| 818 | + } | |
| 819 | + state := (*param).(*githubCopilotResponsesStreamState) | |
| 820 | + | |
| 821 | + if !bytes.HasPrefix(line, dataTag) { | |
| 822 | + return nil | |
| 823 | + } | |
| 824 | + payload := bytes.TrimSpace(line[5:]) | |
| 825 | + if bytes.Equal(payload, []byte("[DONE]")) { | |
| 826 | + return nil | |
| 827 | + } | |
| 828 | + if !gjson.ValidBytes(payload) { | |
| 829 | + return nil | |
| 830 | + } | |
| 831 | + | |
| 832 | + event := gjson.GetBytes(payload, "type").String() | |
| 833 | + results := make([]string, 0, 4) | |
| 834 | + ensureMessageStart := func() { | |
| 835 | + if state.MessageStarted { | |
| 836 | + return | |
| 837 | + } | |
| 838 | + messageStart := `{"type":"message_start","message":{"id":"","type":"message","role":"assistant","model":"","content":[],"stop_reason":null,"stop_sequence":null,"usage":{"input_tokens":0,"output_tokens":0}}}` | |
| 839 | + messageStart, _ = sjson.Set(messageStart, "message.id", gjson.GetBytes(payload, "response.id").String()) | |
| 840 | + messageStart, _ = sjson.Set(messageStart, "message.model", gjson.GetBytes(payload, "response.model").String()) | |
| 841 | + results = append(results, "event: message_start\ndata: "+messageStart+"\n\n") | |
| 842 | + state.MessageStarted = true | |
| 843 | + } | |
| 844 | + startTextBlockIfNeeded := func() { | |
| 845 | + if state.TextBlockStarted { | |
| 846 | + return | |
| 847 | + } | |
| 848 | + if state.TextBlockIndex < 0 { | |
| 849 | + state.TextBlockIndex = state.NextContentIndex | |
| 850 | + state.NextContentIndex++ | |
| 851 | + } | |
| 852 | + contentBlockStart := `{"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}` | |
| 853 | + contentBlockStart, _ = sjson.Set(contentBlockStart, "index", state.TextBlockIndex) | |
| 854 | + results = append(results, "event: content_block_start\ndata: "+contentBlockStart+"\n\n") | |
| 855 | + state.TextBlockStarted = true | |
| 856 | + } | |
| 857 | + stopTextBlockIfNeeded := func() { | |
| 858 | + if !state.TextBlockStarted { | |
| 859 | + return | |
| 860 | + } | |
| 861 | + contentBlockStop := `{"type":"content_block_stop","index":0}` | |
| 862 | + contentBlockStop, _ = sjson.Set(contentBlockStop, "index", state.TextBlockIndex) | |
| 863 | + results = append(results, "event: content_block_stop\ndata: "+contentBlockStop+"\n\n") | |
| 864 | + state.TextBlockStarted = false | |
| 865 | + state.TextBlockIndex = -1 | |
| 866 | + } | |
| 867 | + resolveTool := func(itemID string, outputIndex int) *githubCopilotResponsesStreamToolState { | |
| 868 | + if itemID != "" { | |
| 869 | + if tool, ok := state.ItemIDToTool[itemID]; ok { | |
| 870 | + return tool | |
| 871 | + } | |
| 872 | + } | |
| 873 | + if tool, ok := state.OutputIndexToTool[outputIndex]; ok { | |
| 874 | + if itemID != "" { | |
| 875 | + state.ItemIDToTool[itemID] = tool | |
| 876 | + } | |
| 877 | + return tool | |
| 878 | + } | |
| 879 | + return nil | |
| 880 | + } | |
| 881 | + | |
| 882 | + switch event { | |
| 883 | + case "response.created": | |
| 884 | + ensureMessageStart() | |
| 885 | + case "response.output_text.delta": | |
| 886 | + ensureMessageStart() | |
| 887 | + startTextBlockIfNeeded() | |
| 888 | + delta := gjson.GetBytes(payload, "delta").String() | |
| 889 | + if delta != "" { | |
| 890 | + contentDelta := `{"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":""}}` | |
| 891 | + contentDelta, _ = sjson.Set(contentDelta, "index", state.TextBlockIndex) | |
| 892 | + contentDelta, _ = sjson.Set(contentDelta, "delta.text", delta) | |
| 893 | + results = append(results, "event: content_block_delta\ndata: "+contentDelta+"\n\n") | |
| 894 | + } | |
| 895 | + case "response.output_item.added": | |
| 896 | + if gjson.GetBytes(payload, "item.type").String() != "function_call" { | |
| 897 | + break | |
| 898 | + } | |
| 899 | + ensureMessageStart() | |
| 900 | + stopTextBlockIfNeeded() | |
| 901 | + state.HasToolUse = true | |
| 902 | + tool := &githubCopilotResponsesStreamToolState{ | |
| 903 | + Index: state.NextContentIndex, | |
| 904 | + ID: gjson.GetBytes(payload, "item.call_id").String(), | |
| 905 | + Name: gjson.GetBytes(payload, "item.name").String(), | |
| 906 | + } | |
| 907 | + if tool.ID == "" { | |
| 908 | + tool.ID = gjson.GetBytes(payload, "item.id").String() | |
| 909 | + } | |
| 910 | + state.NextContentIndex++ | |
| 911 | + outputIndex := int(gjson.GetBytes(payload, "output_index").Int()) | |
| 912 | + state.OutputIndexToTool[outputIndex] = tool | |
| 913 | + if itemID := gjson.GetBytes(payload, "item.id").String(); itemID != "" { | |
| 914 | + state.ItemIDToTool[itemID] = tool | |
| 915 | + } | |
| 916 | + contentBlockStart := `{"type":"content_block_start","index":0,"content_block":{"type":"tool_use","id":"","name":"","input":{}}}` | |
| 917 | + contentBlockStart, _ = sjson.Set(contentBlockStart, "index", tool.Index) | |
| 918 | + contentBlockStart, _ = sjson.Set(contentBlockStart, "content_block.id", tool.ID) | |
| 919 | + contentBlockStart, _ = sjson.Set(contentBlockStart, "content_block.name", tool.Name) | |
| 920 | + results = append(results, "event: content_block_start\ndata: "+contentBlockStart+"\n\n") | |
| 921 | + case "response.output_item.delta": | |
| 922 | + item := gjson.GetBytes(payload, "item") | |
| 923 | + if item.Get("type").String() != "function_call" { | |
| 924 | + break | |
| 925 | + } | |
| 926 | + tool := resolveTool(item.Get("id").String(), int(gjson.GetBytes(payload, "output_index").Int())) | |
| 927 | + if tool == nil { | |
| 928 | + break | |
| 929 | + } | |
| 930 | + partial := gjson.GetBytes(payload, "delta").String() | |
| 931 | + if partial == "" { | |
| 932 | + partial = item.Get("arguments").String() | |
| 933 | + } | |
| 934 | + if partial == "" { | |
| 935 | + break | |
| 936 | + } | |
| 937 | + inputDelta := `{"type":"content_block_delta","index":0,"delta":{"type":"input_json_delta","partial_json":""}}` | |
| 938 | + inputDelta, _ = sjson.Set(inputDelta, "index", tool.Index) | |
| 939 | + inputDelta, _ = sjson.Set(inputDelta, "delta.partial_json", partial) | |
| 940 | + results = append(results, "event: content_block_delta\ndata: "+inputDelta+"\n\n") | |
| 941 | + case "response.output_item.done": | |
| 942 | + if gjson.GetBytes(payload, "item.type").String() != "function_call" { | |
| 943 | + break | |
| 944 | + } | |
| 945 | + tool := resolveTool(gjson.GetBytes(payload, "item.id").String(), int(gjson.GetBytes(payload, "output_index").Int())) | |
| 946 | + if tool == nil { | |
| 947 | + break | |
| 948 | + } | |
| 949 | + contentBlockStop := `{"type":"content_block_stop","index":0}` | |
| 950 | + contentBlockStop, _ = sjson.Set(contentBlockStop, "index", tool.Index) | |
| 951 | + results = append(results, "event: content_block_stop\ndata: "+contentBlockStop+"\n\n") | |
| 952 | + case "response.completed": | |
| 953 | + ensureMessageStart() | |
| 954 | + stopTextBlockIfNeeded() | |
| 955 | + if !state.MessageStopSent { | |
| 956 | + stopReason := "end_turn" | |
| 957 | + if state.HasToolUse { | |
| 958 | + stopReason = "tool_use" | |
| 959 | + } | |
| 960 | + messageDelta := `{"type":"message_delta","delta":{"stop_reason":"","stop_sequence":null},"usage":{"input_tokens":0,"output_tokens":0}}` | |
| 961 | + messageDelta, _ = sjson.Set(messageDelta, "delta.stop_reason", stopReason) | |
| 962 | + messageDelta, _ = sjson.Set(messageDelta, "usage.input_tokens", gjson.GetBytes(payload, "response.usage.input_tokens").Int()) | |
| 963 | + messageDelta, _ = sjson.Set(messageDelta, "usage.output_tokens", gjson.GetBytes(payload, "response.usage.output_tokens").Int()) | |
| 964 | + results = append(results, "event: message_delta\ndata: "+messageDelta+"\n\n") | |
| 965 | + results = append(results, "event: message_stop\ndata: {\"type\":\"message_stop\"}\n\n") | |
| 966 | + state.MessageStopSent = true | |
| 967 | + } | |
| 968 | + } | |
| 969 | + | |
| 970 | + return results | |
| 971 | +} | |
| 972 | + | |
| 542 | 973 | // isHTTPSuccess checks if the status code indicates success (2xx). |
| 543 | 974 | func isHTTPSuccess(statusCode int) bool { |
| 544 | 975 | return statusCode >= 200 && statusCode < 300 |
| @@ -1,8 +1,10 @@ | ||
| 1 | 1 | package executor |
| 2 | 2 | |
| 3 | 3 | import ( |
| 4 | + "strings" | |
| 4 | 5 | "testing" |
| 5 | 6 | |
| 7 | + sdktranslator "github.com/router-for-me/CLIProxyAPI/v6/sdk/translator" | |
| 6 | 8 | "github.com/tidwall/gjson" |
| 7 | 9 | ) |
| 8 | 10 | |
| @@ -52,3 +54,189 @@ func TestGitHubCopilotNormalizeModel_StripsSuffix(t *testing.T) { · 43 unmodified lines | ||
| 52 | 54 | }) |
| 53 | 55 | } |
| 54 | 56 | } |
| 57 | + | |
| 58 | +func TestUseGitHubCopilotResponsesEndpoint_OpenAIResponseSource(t *testing.T) { | |
| 59 | + t.Parallel() | |
| 60 | + if !useGitHubCopilotResponsesEndpoint(sdktranslator.FromString("openai-response"), "claude-3-5-sonnet") { | |
| 61 | + t.Fatal("expected openai-response source to use /responses") | |
| 62 | + } | |
| 63 | +} | |
| 64 | + | |
| 65 | +func TestUseGitHubCopilotResponsesEndpoint_CodexModel(t *testing.T) { | |
| 66 | + t.Parallel() | |
| 67 | + if !useGitHubCopilotResponsesEndpoint(sdktranslator.FromString("openai"), "gpt-5-codex") { | |
| 68 | + t.Fatal("expected codex model to use /responses") | |
| 69 | + } | |
| 70 | +} | |
| 71 | + | |
| 72 | +func TestUseGitHubCopilotResponsesEndpoint_DefaultChat(t *testing.T) { | |
| 73 | + t.Parallel() | |
| 74 | + if useGitHubCopilotResponsesEndpoint(sdktranslator.FromString("openai"), "claude-3-5-sonnet") { | |
| 75 | + t.Fatal("expected default openai source with non-codex model to use /chat/completions") | |
| 76 | + } | |
| 77 | +} | |
| 78 | + | |
| 79 | +func TestNormalizeGitHubCopilotChatTools_KeepFunctionOnly(t *testing.T) { | |
| 80 | + t.Parallel() | |
| 81 | + body := []byte(`{"tools":[{"type":"function","function":{"name":"ok"}},{"type":"code_interpreter"}],"tool_choice":"auto"}`) | |
| 82 | + got := normalizeGitHubCopilotChatTools(body) | |
| 83 | + tools := gjson.GetBytes(got, "tools").Array() | |
| 84 | + if len(tools) != 1 { | |
| 85 | + t.Fatalf("tools len = %d, want 1", len(tools)) | |
| 86 | + } | |
| 87 | + if tools[0].Get("type").String() != "function" { | |
| 88 | + t.Fatalf("tool type = %q, want function", tools[0].Get("type").String()) | |
| 89 | + } | |
| 90 | +} | |
| 91 | + | |
| 92 | +func TestNormalizeGitHubCopilotChatTools_InvalidToolChoiceDowngradeToAuto(t *testing.T) { | |
| 93 | + t.Parallel() | |
| 94 | + body := []byte(`{"tools":[],"tool_choice":{"type":"function","function":{"name":"x"}}}`) | |
| 95 | + got := normalizeGitHubCopilotChatTools(body) | |
| 96 | + if gjson.GetBytes(got, "tool_choice").String() != "auto" { | |
| 97 | + t.Fatalf("tool_choice = %s, want auto", gjson.GetBytes(got, "tool_choice").Raw) | |
| 98 | + } | |
| 99 | +} | |
| 100 | + | |
| 101 | +func TestNormalizeGitHubCopilotResponsesInput_MissingInputExtractedFromSystemAndMessages(t *testing.T) { | |
| 102 | + t.Parallel() | |
| 103 | + body := []byte(`{"system":"sys text","messages":[{"role":"user","content":"user text"},{"role":"assistant","content":[{"type":"text","text":"assistant text"}]}]}`) | |
| 104 | + got := normalizeGitHubCopilotResponsesInput(body) | |
| 105 | + in := gjson.GetBytes(got, "input") | |
| 106 | + if in.Type != gjson.String { | |
| 107 | + t.Fatalf("input type = %v, want string", in.Type) | |
| 108 | + } | |
| 109 | + if !strings.Contains(in.String(), "sys text") || !strings.Contains(in.String(), "user text") || !strings.Contains(in.String(), "assistant text") { | |
| 110 | + t.Fatalf("input = %q, want merged text", in.String()) | |
| 111 | + } | |
| 112 | +} | |
| 113 | + | |
| 114 | +func TestNormalizeGitHubCopilotResponsesInput_NonStringInputStringified(t *testing.T) { | |
| 115 | + t.Parallel() | |
| 116 | + body := []byte(`{"input":{"foo":"bar"}}`) | |
| 117 | + got := normalizeGitHubCopilotResponsesInput(body) | |
| 118 | + in := gjson.GetBytes(got, "input") | |
| 119 | + if in.Type != gjson.String { | |
| 120 | + t.Fatalf("input type = %v, want string", in.Type) | |
| 121 | + } | |
| 122 | + if !strings.Contains(in.String(), "foo") { | |
| 123 | + t.Fatalf("input = %q, want stringified object", in.String()) | |
| 124 | + } | |
| 125 | +} | |
| 126 | + | |
| 127 | +func TestNormalizeGitHubCopilotResponsesTools_FlattenFunctionTools(t *testing.T) { | |
| 128 | + t.Parallel() | |
| 129 | + body := []byte(`{"tools":[{"type":"function","function":{"name":"sum","description":"d","parameters":{"type":"object"}}},{"type":"web_search"}]}`) | |
| 130 | + got := normalizeGitHubCopilotResponsesTools(body) | |
| 131 | + tools := gjson.GetBytes(got, "tools").Array() | |
| 132 | + if len(tools) != 1 { | |
| 133 | + t.Fatalf("tools len = %d, want 1", len(tools)) | |
| 134 | + } | |
| 135 | + if tools[0].Get("name").String() != "sum" { | |
| 136 | + t.Fatalf("tools[0].name = %q, want sum", tools[0].Get("name").String()) | |
| 137 | + } | |
| 138 | + if !tools[0].Get("parameters").Exists() { | |
| 139 | + t.Fatal("expected parameters to be preserved") | |
| 140 | + } | |
| 141 | +} | |
| 142 | + | |
| 143 | +func TestNormalizeGitHubCopilotResponsesTools_ClaudeFormatTools(t *testing.T) { | |
| 144 | + t.Parallel() | |
| 145 | + body := []byte(`{"tools":[{"name":"Bash","description":"Run commands","input_schema":{"type":"object","properties":{"command":{"type":"string"}},"required":["command"]}},{"name":"Read","description":"Read files","input_schema":{"type":"object","properties":{"path":{"type":"string"}}}}]}`) | |
| 146 | + got := normalizeGitHubCopilotResponsesTools(body) | |
| 147 | + tools := gjson.GetBytes(got, "tools").Array() | |
| 148 | + if len(tools) != 2 { | |
| 149 | + t.Fatalf("tools len = %d, want 2", len(tools)) | |
| 150 | + } | |
| 151 | + if tools[0].Get("type").String() != "function" { | |
| 152 | + t.Fatalf("tools[0].type = %q, want function", tools[0].Get("type").String()) | |
| 153 | + } | |
| 154 | + if tools[0].Get("name").String() != "Bash" { | |
| 155 | + t.Fatalf("tools[0].name = %q, want Bash", tools[0].Get("name").String()) | |
| 156 | + } | |
| 157 | + if tools[0].Get("description").String() != "Run commands" { | |
| 158 | + t.Fatalf("tools[0].description = %q, want 'Run commands'", tools[0].Get("description").String()) | |
| 159 | + } | |
| 160 | + if !tools[0].Get("parameters").Exists() { | |
| 161 | + t.Fatal("expected parameters to be set from input_schema") | |
| 162 | + } | |
| 163 | + if tools[0].Get("parameters.properties.command").Exists() != true { | |
| 164 | + t.Fatal("expected parameters.properties.command to exist") | |
| 165 | + } | |
| 166 | + if tools[1].Get("name").String() != "Read" { | |
| 167 | + t.Fatalf("tools[1].name = %q, want Read", tools[1].Get("name").String()) | |
| 168 | + } | |
| 169 | +} | |
| 170 | + | |
| 171 | +func TestNormalizeGitHubCopilotResponsesTools_FlattenToolChoiceFunctionObject(t *testing.T) { | |
| 172 | + t.Parallel() | |
| 173 | + body := []byte(`{"tool_choice":{"type":"function","function":{"name":"sum"}}}`) | |
| 174 | + got := normalizeGitHubCopilotResponsesTools(body) | |
| 175 | + if gjson.GetBytes(got, "tool_choice.type").String() != "function" { | |
| 176 | + t.Fatalf("tool_choice.type = %q, want function", gjson.GetBytes(got, "tool_choice.type").String()) | |
| 177 | + } | |
| 178 | + if gjson.GetBytes(got, "tool_choice.name").String() != "sum" { | |
| 179 | + t.Fatalf("tool_choice.name = %q, want sum", gjson.GetBytes(got, "tool_choice.name").String()) | |
| 180 | + } | |
| 181 | +} | |
| 182 | + | |
| 183 | +func TestNormalizeGitHubCopilotResponsesTools_InvalidToolChoiceDowngradeToAuto(t *testing.T) { | |
| 184 | + t.Parallel() | |
| 185 | + body := []byte(`{"tool_choice":{"type":"function"}}`) | |
| 186 | + got := normalizeGitHubCopilotResponsesTools(body) | |
| 187 | + if gjson.GetBytes(got, "tool_choice").String() != "auto" { | |
| 188 | + t.Fatalf("tool_choice = %s, want auto", gjson.GetBytes(got, "tool_choice").Raw) | |
| 189 | + } | |
| 190 | +} | |
| 191 | + | |
| 192 | +func TestTranslateGitHubCopilotResponsesNonStreamToClaude_TextMapping(t *testing.T) { | |
| 193 | + t.Parallel() | |
| 194 | + resp := []byte(`{"id":"resp_1","model":"gpt-5-codex","output":[{"type":"message","content":[{"type":"output_text","text":"hello"}]}],"usage":{"input_tokens":3,"output_tokens":5}}`) | |
| 195 | + out := translateGitHubCopilotResponsesNonStreamToClaude(resp) | |
| 196 | + if gjson.Get(out, "type").String() != "message" { | |
| 197 | + t.Fatalf("type = %q, want message", gjson.Get(out, "type").String()) | |
| 198 | + } | |
| 199 | + if gjson.Get(out, "content.0.type").String() != "text" { | |
| 200 | + t.Fatalf("content.0.type = %q, want text", gjson.Get(out, "content.0.type").String()) | |
| 201 | + } | |
| 202 | + if gjson.Get(out, "content.0.text").String() != "hello" { | |
| 203 | + t.Fatalf("content.0.text = %q, want hello", gjson.Get(out, "content.0.text").String()) | |
| 204 | + } | |
| 205 | +} | |
| 206 | + | |
| 207 | +func TestTranslateGitHubCopilotResponsesNonStreamToClaude_ToolUseMapping(t *testing.T) { | |
| 208 | + t.Parallel() | |
| 209 | + resp := []byte(`{"id":"resp_2","model":"gpt-5-codex","output":[{"type":"function_call","id":"fc_1","call_id":"call_1","name":"sum","arguments":"{\"a\":1}"}],"usage":{"input_tokens":1,"output_tokens":2}}`) | |
| 210 | + out := translateGitHubCopilotResponsesNonStreamToClaude(resp) | |
| 211 | + if gjson.Get(out, "content.0.type").String() != "tool_use" { | |
| 212 | + t.Fatalf("content.0.type = %q, want tool_use", gjson.Get(out, "content.0.type").String()) | |
| 213 | + } | |
| 214 | + if gjson.Get(out, "content.0.name").String() != "sum" { | |
| 215 | + t.Fatalf("content.0.name = %q, want sum", gjson.Get(out, "content.0.name").String()) | |
| 216 | + } | |
| 217 | + if gjson.Get(out, "stop_reason").String() != "tool_use" { | |
| 218 | + t.Fatalf("stop_reason = %q, want tool_use", gjson.Get(out, "stop_reason").String()) | |
| 219 | + } | |
| 220 | +} | |
| 221 | + | |
| 222 | +func TestTranslateGitHubCopilotResponsesStreamToClaude_TextLifecycle(t *testing.T) { | |
| 223 | + t.Parallel() | |
| 224 | + var param any | |
| 225 | + | |
| 226 | + created := translateGitHubCopilotResponsesStreamToClaude([]byte(`data: {"type":"response.created","response":{"id":"resp_1","model":"gpt-5-codex"}}`), ¶m) | |
| 227 | + if len(created) == 0 || !strings.Contains(created[0], "message_start") { | |
| 228 | + t.Fatalf("created events = %#v, want message_start", created) | |
| 229 | + } | |
| 230 | + | |
| 231 | + delta := translateGitHubCopilotResponsesStreamToClaude([]byte(`data: {"type":"response.output_text.delta","delta":"he"}`), ¶m) | |
| 232 | + joinedDelta := strings.Join(delta, "") | |
| 233 | + if !strings.Contains(joinedDelta, "content_block_start") || !strings.Contains(joinedDelta, "text_delta") { | |
| 234 | + t.Fatalf("delta events = %#v, want content_block_start + text_delta", delta) | |
| 235 | + } | |
| 236 | + | |
| 237 | + completed := translateGitHubCopilotResponsesStreamToClaude([]byte(`data: {"type":"response.completed","response":{"usage":{"input_tokens":7,"output_tokens":9}}}`), ¶m) | |
| 238 | + joinedCompleted := strings.Join(completed, "") | |
| 239 | + if !strings.Contains(joinedCompleted, "message_delta") || !strings.Contains(joinedCompleted, "message_stop") { | |
| 240 | + t.Fatalf("completed events = %#v, want message_delta + message_stop", completed) | |
| 241 | + } | |
| 242 | +} | |
| @@ -16,6 +16,7 @@ import ( · 15 unmodified lines | ||
| 16 | 16 | "path/filepath" |
| 17 | 17 | "strings" |
| 18 | 18 | "sync" |
| 19 | + "sync/atomic" | |
| 19 | 20 | "syscall" |
| 20 | 21 | "time" |
| 21 | 22 | |
| @@ -385,6 +386,35 @@ func buildKiroEndpointConfigs(region string) []kiroEndpointConfig { · 363 unmodified lines | ||
| 385 | 386 | } |
| 386 | 387 | } |
| 387 | 388 | |
| 389 | +// resolveKiroAPIRegion determines the AWS region for Kiro API calls. | |
| 390 | +// Region priority: | |
| 391 | +// 1. auth.Metadata["api_region"] - explicit API region override | |
| 392 | +// 2. ProfileARN region - extracted from arn:aws:service:REGION:account:resource | |
| 393 | +// 3. kiroDefaultRegion (us-east-1) - fallback | |
| 394 | +// Note: OIDC "region" is NOT used - it's for token refresh, not API calls | |
| 395 | +func resolveKiroAPIRegion(auth *cliproxyauth.Auth) string { | |
| 396 | + if auth == nil || auth.Metadata == nil { | |
| 397 | + return kiroDefaultRegion | |
| 398 | + } | |
| 399 | + // Priority 1: Explicit api_region override | |
| 400 | + if r, ok := auth.Metadata["api_region"].(string); ok && r != "" { | |
| 401 | + log.Debugf("kiro: using region %s (source: api_region)", r) | |
| 402 | + return r | |
| 403 | + } | |
| 404 | + // Priority 2: Extract from ProfileARN | |
| 405 | + if profileArn, ok := auth.Metadata["profile_arn"].(string); ok && profileArn != "" { | |
| 406 | + if arnRegion := extractRegionFromProfileARN(profileArn); arnRegion != "" { | |
| 407 | + log.Debugf("kiro: using region %s (source: profile_arn)", arnRegion) | |
| 408 | + return arnRegion | |
| 409 | + } | |
| 410 | + } | |
| 411 | + // Note: OIDC "region" field is NOT used for API endpoint | |
| 412 | + // Kiro API only exists in us-east-1, while OIDC region can vary (e.g., ap-northeast-2) | |
| 413 | + // Using OIDC region for API calls causes DNS failures | |
| 414 | + log.Debugf("kiro: using region %s (source: default)", kiroDefaultRegion) | |
| 415 | + return kiroDefaultRegion | |
| 416 | +} | |
| 417 | + | |
| 388 | 418 | // kiroEndpointConfigs is kept for backward compatibility with default us-east-1 region. |
| 389 | 419 | // Prefer using buildKiroEndpointConfigs(region) for dynamic region support. |
| 390 | 420 | var kiroEndpointConfigs = buildKiroEndpointConfigs(kiroDefaultRegion) |
| @@ -403,30 +433,8 @@ func getKiroEndpointConfigs(auth *cliproxyauth.Auth) []kiroEndpointConfig { · 12 unmodified lines | ||
| 403 | 433 | return kiroEndpointConfigs |
| 404 | 434 | } |
| 405 | 435 | |
| 406 | − // Determine API region with priority: api_region > profile_arn > region > default | |
| 407 | − region := kiroDefaultRegion | |
| 408 | − regionSource := "default" | |
| 409 | − | |
| 410 | − if auth.Metadata != nil { | |
| 411 | − // Priority 1: Explicit api_region override | |
| 412 | − if r, ok := auth.Metadata["api_region"].(string); ok && r != "" { | |
| 413 | − region = r | |
| 414 | − regionSource = "api_region" | |
| 415 | − } else { | |
| 416 | − // Priority 2: Extract from ProfileARN | |
| 417 | − if profileArn, ok := auth.Metadata["profile_arn"].(string); ok && profileArn != "" { | |
| 418 | − if arnRegion := extractRegionFromProfileARN(profileArn); arnRegion != "" { | |
| 419 | − region = arnRegion | |
| 420 | − regionSource = "profile_arn" | |
| 421 | − } | |
| 422 | − } | |
| 423 | − // Note: OIDC "region" field is NOT used for API endpoint | |
| 424 | − // Kiro API only exists in us-east-1, while OIDC region can vary (e.g., ap-northeast-2) | |
| 425 | − // Using OIDC region for API calls causes DNS failures | |
| 426 | − } | |
| 427 | − } | |
| 428 | − | |
| 429 | − log.Debugf("kiro: using region %s (source: %s)", region, regionSource) | |
| 436 | + // Determine API region using shared resolution logic | |
| 437 | + region := resolveKiroAPIRegion(auth) | |
| 430 | 438 | |
| 431 | 439 | // Build endpoint configs for the specified region |
| 432 | 440 | endpointConfigs := buildKiroEndpointConfigs(region) |
| @@ -520,7 +528,7 @@ func buildKiroPayloadForFormat(body []byte, modelID, profileArn, origin string, · 87 unmodified lines | ||
| 520 | 528 | log.Debugf("kiro: using OpenAI payload builder for source format: %s", sourceFormat.String()) |
| 521 | 529 | return kiroopenai.BuildKiroPayloadFromOpenAI(body, modelID, profileArn, origin, isAgentic, isChatOnly, headers, nil) |
| 522 | 530 | case "kiro": |
| 523 | − // Body is already in Kiro format — pass through directly (used by callKiroRawAndBuffer) | |
| 531 | + // Body is already in Kiro format — pass through directly | |
| 524 | 532 | log.Debugf("kiro: body already in Kiro format, passing through directly") |
| 525 | 533 | return body, false |
| 526 | 534 | default: |
| @@ -640,17 +648,7 @@ func (e *KiroExecutor) Execute(ctx context.Context, auth *cliproxyauth.Auth, req · 113 unmodified lines | ||
| 640 | 648 | rateLimiter.WaitForToken(tokenKey) |
| 641 | 649 | log.Debugf("kiro: rate limiter cleared for token %s", tokenKey) |
| 642 | 650 | |
| 643 | − // Check for pure web_search request | |
| 644 | − // Route to MCP endpoint instead of normal Kiro API | |
| 645 | − if kiroclaude.HasWebSearchTool(req.Payload) { | |
| 646 | − log.Infof("kiro: detected pure web_search request (non-stream), routing to MCP endpoint") | |
| 647 | − return e.handleWebSearch(ctx, auth, req, opts, accessToken, profileArn) | |
| 648 | − } | |
| 649 | − | |
| 650 | − reporter := newUsageReporter(ctx, e.Identifier(), req.Model, auth) | |
| 651 | − defer reporter.trackFailure(ctx, &err) | |
| 652 | − | |
| 653 | − // Check if token is expired before making request | |
| 651 | + // Check if token is expired before making request (covers both normal and web_search paths) | |
| 654 | 652 | if e.isTokenExpired(accessToken) { |
| 655 | 653 | log.Infof("kiro: access token expired, attempting recovery") |
| 656 | 654 | |
| @@ -679,6 +677,16 @@ func (e *KiroExecutor) Execute(ctx context.Context, auth *cliproxyauth.Auth, req · 22 unmodified lines | ||
| 679 | 677 | } |
| 680 | 678 | } |
| 681 | 679 | |
| 680 | + // Check for pure web_search request | |
| 681 | + // Route to MCP endpoint instead of normal Kiro API | |
| 682 | + if kiroclaude.HasWebSearchTool(req.Payload) { | |
| 683 | + log.Infof("kiro: detected pure web_search request (non-stream), routing to MCP endpoint") | |
| 684 | + return e.handleWebSearch(ctx, auth, req, opts, accessToken, profileArn) | |
| 685 | + } | |
| 686 | + | |
| 687 | + reporter := newUsageReporter(ctx, e.Identifier(), req.Model, auth) | |
| 688 | + defer reporter.trackFailure(ctx, &err) | |
| 689 | + | |
| 682 | 690 | from := opts.SourceFormat |
| 683 | 691 | to := sdktranslator.FromString("kiro") |
| 684 | 692 | body := sdktranslator.TranslateRequest(from, to, req.Model, bytes.Clone(req.Payload), true) |
| @@ -1025,8 +1033,9 @@ func (e *KiroExecutor) executeWithRetry(ctx context.Context, auth *cliproxyauth. · 340 unmodified lines | ||
| 1025 | 1033 | |
| 1026 | 1034 | // Build response in Claude format for Kiro translator |
| 1027 | 1035 | // stopReason is extracted from upstream response by parseEventStream |
| 1028 | − kiroResponse := kiroclaude.BuildClaudeResponse(content, toolUses, req.Model, usageInfo, stopReason) | |
| 1029 | − out := sdktranslator.TranslateNonStream(ctx, to, from, req.Model, bytes.Clone(opts.OriginalRequest), body, kiroResponse, nil) | |
| 1036 | + requestedModel := payloadRequestedModel(opts, req.Model) | |
| 1037 | + kiroResponse := kiroclaude.BuildClaudeResponse(content, toolUses, requestedModel, usageInfo, stopReason) | |
| 1038 | + out := sdktranslator.TranslateNonStream(ctx, to, from, requestedModel, bytes.Clone(opts.OriginalRequest), body, kiroResponse, nil) | |
| 1030 | 1039 | resp = cliproxyexecutor.Response{Payload: []byte(out)} |
| 1031 | 1040 | return resp, nil |
| 1032 | 1041 | } |
| @@ -1068,17 +1077,7 @@ func (e *KiroExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.Aut · 35 unmodified lines | ||
| 1068 | 1077 | rateLimiter.WaitForToken(tokenKey) |
| 1069 | 1078 | log.Debugf("kiro: stream rate limiter cleared for token %s", tokenKey) |
| 1070 | 1079 | |
| 1071 | − // Check for pure web_search request | |
| 1072 | − // Route to MCP endpoint instead of normal Kiro API | |
| 1073 | − if kiroclaude.HasWebSearchTool(req.Payload) { | |
| 1074 | − log.Infof("kiro: detected pure web_search request, routing to MCP endpoint") | |
| 1075 | − return e.handleWebSearchStream(ctx, auth, req, opts, accessToken, profileArn) | |
| 1076 | − } | |
| 1077 | − | |
| 1078 | − reporter := newUsageReporter(ctx, e.Identifier(), req.Model, auth) | |
| 1079 | − defer reporter.trackFailure(ctx, &err) | |
| 1080 | − | |
| 1081 | − // Check if token is expired before making request | |
| 1080 | + // Check if token is expired before making request (covers both normal and web_search paths) | |
| 1082 | 1081 | if e.isTokenExpired(accessToken) { |
| 1083 | 1082 | log.Infof("kiro: access token expired, attempting recovery before stream request") |
| 1084 | 1083 | |
| @@ -1107,6 +1106,16 @@ func (e *KiroExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.Aut · 22 unmodified lines | ||
| 1107 | 1106 | } |
| 1108 | 1107 | } |
| 1109 | 1108 | |
| 1109 | + // Check for pure web_search request | |
| 1110 | + // Route to MCP endpoint instead of normal Kiro API | |
| 1111 | + if kiroclaude.HasWebSearchTool(req.Payload) { | |
| 1112 | + log.Infof("kiro: detected pure web_search request, routing to MCP endpoint") | |
| 1113 | + return e.handleWebSearchStream(ctx, auth, req, opts, accessToken, profileArn) | |
| 1114 | + } | |
| 1115 | + | |
| 1116 | + reporter := newUsageReporter(ctx, e.Identifier(), req.Model, auth) | |
| 1117 | + defer reporter.trackFailure(ctx, &err) | |
| 1118 | + | |
| 1110 | 1119 | from := opts.SourceFormat |
| 1111 | 1120 | to := sdktranslator.FromString("kiro") |
| 1112 | 1121 | body := sdktranslator.TranslateRequest(from, to, req.Model, bytes.Clone(req.Payload), true) |
| @@ -1423,7 +1432,7 @@ func (e *KiroExecutor) executeStreamWithRetry(ctx context.Context, auth *cliprox · 310 unmodified lines | ||
| 1423 | 1432 | // So we always enable thinking parsing for Kiro responses |
| 1424 | 1433 | log.Debugf("kiro: stream thinkingEnabled = %v (always true for Kiro)", thinkingEnabled) |
| 1425 | 1434 | |
| 1426 | − e.streamToChannel(ctx, resp.Body, out, from, req.Model, opts.OriginalRequest, body, reporter, thinkingEnabled) | |
| 1435 | + e.streamToChannel(ctx, resp.Body, out, from, payloadRequestedModel(opts, req.Model), opts.OriginalRequest, body, reporter, thinkingEnabled) | |
| 1427 | 1436 | }(httpResp, thinkingEnabled) |
| 1428 | 1437 | |
| 1429 | 1438 | return out, nil |
| @@ -4114,6 +4123,238 @@ func (e *KiroExecutor) isTokenExpired(accessToken string) bool { · 2684 unmodified lines | ||
| 4114 | 4123 | return isExpired |
| 4115 | 4124 | } |
| 4116 | 4125 | |
| 4126 | +// ══════════════════════════════════════════════════════════════════════════════ | |
| 4127 | +// Web Search Handler (MCP API) | |
| 4128 | +// ══════════════════════════════════════════════════════════════════════════════ | |
| 4129 | + | |
| 4130 | +// fetchToolDescription caching: | |
| 4131 | +// Uses a mutex + fetched flag to ensure only one goroutine fetches at a time, | |
| 4132 | +// with automatic retry on failure: | |
| 4133 | +// - On failure, fetched stays false so subsequent calls will retry | |
| 4134 | +// - On success, fetched is set to true — subsequent calls skip immediately (mutex-free fast path) | |
| 4135 | +// The cached description is stored in the translator package via kiroclaude.SetWebSearchDescription(), | |
| 4136 | +// enabling the translator's convertClaudeToolsToKiro to read it when building Kiro requests. | |
| 4137 | +var ( | |
| 4138 | + toolDescMu sync.Mutex | |
| 4139 | + toolDescFetched atomic.Bool | |
| 4140 | +) | |
| 4141 | + | |
| 4142 | +// fetchToolDescription calls MCP tools/list to get the web_search tool description | |
| 4143 | +// and caches it. Safe to call concurrently — only one goroutine fetches at a time. | |
| 4144 | +// If the fetch fails, subsequent calls will retry. On success, no further fetches occur. | |
| 4145 | +// The httpClient parameter allows reusing a shared pooled HTTP client. | |
| 4146 | +func fetchToolDescription(ctx context.Context, mcpEndpoint, authToken string, httpClient *http.Client, auth *cliproxyauth.Auth, authAttrs map[string]string) { | |
| 4147 | + // Fast path: already fetched successfully, no lock needed | |
| 4148 | + if toolDescFetched.Load() { | |
| 4149 | + return | |
| 4150 | + } | |
| 4151 | + | |
| 4152 | + toolDescMu.Lock() | |
| 4153 | + defer toolDescMu.Unlock() | |
| 4154 | + | |
| 4155 | + // Double-check after acquiring lock | |
| 4156 | + if toolDescFetched.Load() { | |
| 4157 | + return | |
| 4158 | + } | |
| 4159 | + | |
| 4160 | + handler := newWebSearchHandler(ctx, mcpEndpoint, authToken, httpClient, auth, authAttrs) | |
| 4161 | + reqBody := []byte(`{"id":"tools_list","jsonrpc":"2.0","method":"tools/list"}`) | |
| 4162 | + log.Debugf("kiro/websearch MCP tools/list request: %d bytes", len(reqBody)) | |
| 4163 | + | |
| 4164 | + req, err := http.NewRequestWithContext(ctx, "POST", mcpEndpoint, bytes.NewReader(reqBody)) | |
| 4165 | + if err != nil { | |
| 4166 | + log.Warnf("kiro/websearch: failed to create tools/list request: %v", err) | |
| 4167 | + return | |
| 4168 | + } | |
| 4169 | + | |
| 4170 | + // Reuse same headers as callMcpAPI | |
| 4171 | + handler.setMcpHeaders(req) | |
| 4172 | + | |
| 4173 | + resp, err := handler.httpClient.Do(req) | |
| 4174 | + if err != nil { | |
| 4175 | + log.Warnf("kiro/websearch: tools/list request failed: %v", err) | |
| 4176 | + return | |
| 4177 | + } | |
| 4178 | + defer resp.Body.Close() | |
| 4179 | + | |
| 4180 | + body, err := io.ReadAll(resp.Body) | |
| 4181 | + if err != nil || resp.StatusCode != http.StatusOK { | |
| 4182 | + log.Warnf("kiro/websearch: tools/list returned status %d", resp.StatusCode) | |
| 4183 | + return | |
| 4184 | + } | |
| 4185 | + log.Debugf("kiro/websearch MCP tools/list response: [%d] %d bytes", resp.StatusCode, len(body)) | |
| 4186 | + | |
| 4187 | + // Parse: {"result":{"tools":[{"name":"web_search","description":"..."}]}} | |
| 4188 | + var result struct { | |
| 4189 | + Result *struct { | |
| 4190 | + Tools []struct { | |
| 4191 | + Name string `json:"name"` | |
| 4192 | + Description string `json:"description"` | |
| 4193 | + } `json:"tools"` | |
| 4194 | + } `json:"result"` | |
| 4195 | + } | |
| 4196 | + if err := json.Unmarshal(body, &result); err != nil || result.Result == nil { | |
| 4197 | + log.Warnf("kiro/websearch: failed to parse tools/list response") | |
| 4198 | + return | |
| 4199 | + } | |
| 4200 | + | |
| 4201 | + for _, tool := range result.Result.Tools { | |
| 4202 | + if tool.Name == "web_search" && tool.Description != "" { | |
| 4203 | + kiroclaude.SetWebSearchDescription(tool.Description) | |
| 4204 | + toolDescFetched.Store(true) // success — no more fetches | |
| 4205 | + log.Infof("kiro/websearch: cached web_search description from tools/list (%d bytes)", len(tool.Description)) | |
| 4206 | + return | |
| 4207 | + } | |
| 4208 | + } | |
| 4209 | + | |
| 4210 | + // web_search tool not found in response | |
| 4211 | + log.Warnf("kiro/websearch: web_search tool not found in tools/list response") | |
| 4212 | +} | |
| 4213 | + | |
| 4214 | +// webSearchHandler handles web search requests via Kiro MCP API | |
| 4215 | +type webSearchHandler struct { | |
| 4216 | + ctx context.Context | |
| 4217 | + mcpEndpoint string | |
| 4218 | + httpClient *http.Client | |
| 4219 | + authToken string | |
| 4220 | + auth *cliproxyauth.Auth // for applyDynamicFingerprint | |
| 4221 | + authAttrs map[string]string // optional, for custom headers from auth.Attributes | |
| 4222 | +} | |
| 4223 | + | |
| 4224 | +// newWebSearchHandler creates a new webSearchHandler. | |
| 4225 | +// If httpClient is nil, a default client with 30s timeout is used. | |
| 4226 | +// Pass a shared pooled client (e.g. from getKiroPooledHTTPClient) for connection reuse. | |
| 4227 | +func newWebSearchHandler(ctx context.Context, mcpEndpoint, authToken string, httpClient *http.Client, auth *cliproxyauth.Auth, authAttrs map[string]string) *webSearchHandler { | |
| 4228 | + if httpClient == nil { | |
| 4229 | + httpClient = &http.Client{ | |
| 4230 | + Timeout: 30 * time.Second, | |
| 4231 | + } | |
| 4232 | + } | |
| 4233 | + return &webSearchHandler{ | |
| 4234 | + ctx: ctx, | |
| 4235 | + mcpEndpoint: mcpEndpoint, | |
| 4236 | + httpClient: httpClient, | |
| 4237 | + authToken: authToken, | |
| 4238 | + auth: auth, | |
| 4239 | + authAttrs: authAttrs, | |
| 4240 | + } | |
| 4241 | +} | |
| 4242 | + | |
| 4243 | +// setMcpHeaders sets standard MCP API headers on the request, | |
| 4244 | +// aligned with the GAR request pattern. | |
| 4245 | +func (h *webSearchHandler) setMcpHeaders(req *http.Request) { | |
| 4246 | + // 1. Content-Type & Accept (aligned with GAR) | |
| 4247 | + req.Header.Set("Content-Type", "application/json") | |
| 4248 | + req.Header.Set("Accept", "*/*") | |
| 4249 | + | |
| 4250 | + // 2. Kiro-specific headers (aligned with GAR) | |
| 4251 | + req.Header.Set("x-amzn-kiro-agent-mode", "vibe") | |
| 4252 | + req.Header.Set("x-amzn-codewhisperer-optout", "true") | |
| 4253 | + | |
| 4254 | + // 3. User-Agent: Reuse applyDynamicFingerprint for consistency | |
| 4255 | + applyDynamicFingerprint(req, h.auth) | |
| 4256 | + | |
| 4257 | + // 4. AWS SDK identifiers | |
| 4258 | + req.Header.Set("Amz-Sdk-Request", "attempt=1; max=3") | |
| 4259 | + req.Header.Set("Amz-Sdk-Invocation-Id", uuid.New().String()) | |
| 4260 | + | |
| 4261 | + // 5. Authentication | |
| 4262 | + req.Header.Set("Authorization", "Bearer "+h.authToken) | |
| 4263 | + | |
| 4264 | + // 6. Custom headers from auth attributes | |
| 4265 | + util.ApplyCustomHeadersFromAttrs(req, h.authAttrs) | |
| 4266 | +} | |
| 4267 | + | |
| 4268 | +// mcpMaxRetries is the maximum number of retries for MCP API calls. | |
| 4269 | +const mcpMaxRetries = 2 | |
| 4270 | + | |
| 4271 | +// callMcpAPI calls the Kiro MCP API with the given request. | |
| 4272 | +// Includes retry logic with exponential backoff for retryable errors. | |
| 4273 | +func (h *webSearchHandler) callMcpAPI(request *kiroclaude.McpRequest) (*kiroclaude.McpResponse, error) { | |
| 4274 | + requestBody, err := json.Marshal(request) | |
| 4275 | + if err != nil { | |
| 4276 | + return nil, fmt.Errorf("failed to marshal MCP request: %w", err) | |
| 4277 | + } | |
| 4278 | + log.Debugf("kiro/websearch MCP request → %s (%d bytes)", h.mcpEndpoint, len(requestBody)) | |
| 4279 | + | |
| 4280 | + var lastErr error | |
| 4281 | + for attempt := 0; attempt <= mcpMaxRetries; attempt++ { | |
| 4282 | + if attempt > 0 { | |
| 4283 | + backoff := time.Duration(1<<attempt) * time.Second | |
| 4284 | + if backoff > 10*time.Second { | |
| 4285 | + backoff = 10 * time.Second | |
| 4286 | + } | |
| 4287 | + log.Warnf("kiro/websearch: MCP retry %d/%d after %v (last error: %v)", attempt, mcpMaxRetries, backoff, lastErr) | |
| 4288 | + select { | |
| 4289 | + case <-h.ctx.Done(): | |
| 4290 | + return nil, h.ctx.Err() | |
| 4291 | + case <-time.After(backoff): | |
| 4292 | + } | |
| 4293 | + } | |
| 4294 | + | |
| 4295 | + req, err := http.NewRequestWithContext(h.ctx, "POST", h.mcpEndpoint, bytes.NewReader(requestBody)) | |
| 4296 | + if err != nil { | |
| 4297 | + return nil, fmt.Errorf("failed to create HTTP request: %w", err) | |
| 4298 | + } | |
| 4299 | + | |
| 4300 | + h.setMcpHeaders(req) | |
| 4301 | + | |
| 4302 | + resp, err := h.httpClient.Do(req) | |
| 4303 | + if err != nil { | |
| 4304 | + lastErr = fmt.Errorf("MCP API request failed: %w", err) | |
| 4305 | + continue // network error → retry | |
| 4306 | + } | |
| 4307 | + | |
| 4308 | + body, err := io.ReadAll(resp.Body) | |
| 4309 | + resp.Body.Close() | |
| 4310 | + if err != nil { | |
| 4311 | + lastErr = fmt.Errorf("failed to read MCP response: %w", err) | |
| 4312 | + continue // read error → retry | |
| 4313 | + } | |
| 4314 | + log.Debugf("kiro/websearch MCP response ← [%d] (%d bytes)", resp.StatusCode, len(body)) | |
| 4315 | + | |
| 4316 | + // Retryable HTTP status codes (aligned with GAR: 502, 503, 504) | |
| 4317 | + if resp.StatusCode >= 502 && resp.StatusCode <= 504 { | |
| 4318 | + lastErr = fmt.Errorf("MCP API returned retryable status %d: %s", resp.StatusCode, string(body)) | |
| 4319 | + continue | |
| 4320 | + } | |
| 4321 | + | |
| 4322 | + if resp.StatusCode != http.StatusOK { | |
| 4323 | + return nil, fmt.Errorf("MCP API returned status %d: %s", resp.StatusCode, string(body)) | |
| 4324 | + } | |
| 4325 | + | |
| 4326 | + var mcpResponse kiroclaude.McpResponse | |
| 4327 | + if err := json.Unmarshal(body, &mcpResponse); err != nil { | |
| 4328 | + return nil, fmt.Errorf("failed to parse MCP response: %w", err) | |
| 4329 | + } | |
| 4330 | + | |
| 4331 | + if mcpResponse.Error != nil { | |
| 4332 | + code := -1 | |
| 4333 | + if mcpResponse.Error.Code != nil { | |
| 4334 | + code = *mcpResponse.Error.Code | |
| 4335 | + } | |
| 4336 | + msg := "Unknown error" | |
| 4337 | + if mcpResponse.Error.Message != nil { | |
| 4338 | + msg = *mcpResponse.Error.Message | |
| 4339 | + } | |
| 4340 | + return nil, fmt.Errorf("MCP error %d: %s", code, msg) | |
| 4341 | + } | |
| 4342 | + | |
| 4343 | + return &mcpResponse, nil | |
| 4344 | + } | |
| 4345 | + | |
| 4346 | + return nil, lastErr | |
| 4347 | +} | |
| 4348 | + | |
| 4349 | +// webSearchAuthAttrs extracts auth attributes for MCP calls. | |
| 4350 | +// Used by handleWebSearch and handleWebSearchStream to pass custom headers. | |
| 4351 | +func webSearchAuthAttrs(auth *cliproxyauth.Auth) map[string]string { | |
| 4352 | + if auth != nil { | |
| 4353 | + return auth.Attributes | |
| 4354 | + } | |
| 4355 | + return nil | |
| 4356 | +} | |
| 4357 | + | |
| 4117 | 4358 | const maxWebSearchIterations = 5 |
| 4118 | 4359 | |
| 4119 | 4360 | // handleWebSearchStream handles web_search requests: |
| @@ -4136,58 +4377,63 @@ func (e *KiroExecutor) handleWebSearchStream( · 16 unmodified lines | ||
| 4136 | 4377 | return e.callKiroDirectStream(ctx, auth, req, opts, accessToken, profileArn) |
| 4137 | 4378 | } |
| 4138 | 4379 | |
| 4139 | − // Build MCP endpoint based on region | |
| 4140 | − region := kiroDefaultRegion | |
| 4141 | − if auth != nil && auth.Metadata != nil { | |
| 4142 | − if r, ok := auth.Metadata["api_region"].(string); ok && r != "" { | |
| 4143 | − region = r | |
| 4144 | − } | |
| 4145 | − } | |
| 4146 | − mcpEndpoint := fmt.Sprintf("https://q.%s.amazonaws.com/mcp", region) | |
| 4380 | + // Build MCP endpoint using shared region resolution (supports api_region + ProfileARN fallback) | |
| 4381 | + region := resolveKiroAPIRegion(auth) | |
| 4382 | + mcpEndpoint := kiroclaude.BuildMcpEndpoint(region) | |
| 4147 | 4383 | |
| 4148 | 4384 | // ── Step 1: tools/list (SYNC) — cache tool description ── |
| 4149 | 4385 | { |
| 4150 | − tokenKey := getTokenKey(auth) | |
| 4151 | − fp := getGlobalFingerprintManager().GetFingerprint(tokenKey) | |
| 4152 | − var authAttrs map[string]string | |
| 4153 | − if auth != nil { | |
| 4154 | − authAttrs = auth.Attributes | |
| 4155 | − } | |
| 4156 | − kiroclaude.FetchToolDescription(mcpEndpoint, accessToken, newKiroHTTPClientWithPooling(ctx, e.cfg, auth, 30*time.Second), fp, authAttrs) | |
| 4386 | + authAttrs := webSearchAuthAttrs(auth) | |
| 4387 | + fetchToolDescription(ctx, mcpEndpoint, accessToken, newKiroHTTPClientWithPooling(ctx, e.cfg, auth, 30*time.Second), auth, authAttrs) | |
| 4157 | 4388 | } |
| 4158 | 4389 | |
| 4159 | 4390 | // Create output channel |
| 4160 | 4391 | out := make(chan cliproxyexecutor.StreamChunk) |
| 4161 | 4392 | |
| 4393 | + // Usage reporting: track web search requests like normal streaming requests | |
| 4394 | + reporter := newUsageReporter(ctx, e.Identifier(), req.Model, auth) | |
| 4395 | + | |
| 4162 | 4396 | go func() { |
| 4397 | + var wsErr error | |
| 4398 | + defer reporter.trackFailure(ctx, &wsErr) | |
| 4163 | 4399 | defer close(out) |
| 4164 | 4400 | |
| 4165 | − // Send message_start event to client | |
| 4166 | − messageStartEvent := kiroclaude.SseEvent{ | |
| 4167 | − Event: "message_start", | |
| 4168 | − Data: map[string]interface{}{ | |
| 4169 | − "type": "message_start", | |
| 4170 | − "message": map[string]interface{}{ | |
| 4171 | − "id": kiroclaude.GenerateMessageID(), | |
| 4172 | − "type": "message", | |
| 4173 | − "role": "assistant", | |
| 4174 | − "model": req.Model, | |
| 4175 | − "content": []interface{}{}, | |
| 4176 | − "stop_reason": nil, | |
| 4177 | − "stop_sequence": nil, | |
| 4178 | − "usage": map[string]interface{}{ | |
| 4179 | − "input_tokens": len(req.Payload) / 4, | |
| 4180 | − "output_tokens": 0, | |
| 4181 | − "cache_creation_input_tokens": 0, | |
| 4182 | − "cache_read_input_tokens": 0, | |
| 4183 | − }, | |
| 4184 | − }, | |
| 4185 | − }, | |
| 4401 | + // Estimate input tokens using tokenizer (matching streamToChannel pattern) | |
| 4402 | + var totalUsage usage.Detail | |
| 4403 | + if enc, tokErr := getTokenizer(req.Model); tokErr == nil { | |
| 4404 | + if inp, e := countClaudeChatTokens(enc, req.Payload); e == nil && inp > 0 { | |
| 4405 | + totalUsage.InputTokens = inp | |
| 4406 | + } else { | |
| 4407 | + totalUsage.InputTokens = int64(len(req.Payload) / 4) | |
| 4408 | + } | |
| 4409 | + } else { | |
| 4410 | + totalUsage.InputTokens = int64(len(req.Payload) / 4) | |
| 4186 | 4411 | } |
| 4412 | + if totalUsage.InputTokens == 0 && len(req.Payload) > 0 { | |
| 4413 | + totalUsage.InputTokens = 1 | |
| 4414 | + } | |
| 4415 | + var accumulatedOutputLen int | |
| 4416 | + defer func() { | |
| 4417 | + if wsErr != nil { | |
| 4418 | + return // let trackFailure handle failure reporting | |
| 4419 | + } | |
| 4420 | + totalUsage.OutputTokens = int64(accumulatedOutputLen / 4) | |
| 4421 | + if accumulatedOutputLen > 0 && totalUsage.OutputTokens == 0 { | |
| 4422 | + totalUsage.OutputTokens = 1 | |
| 4423 | + } | |
| 4424 | + reporter.publish(ctx, totalUsage) | |
| 4425 | + }() | |
| 4426 | + | |
| 4427 | + // Send message_start event to client (aligned with streamToChannel pattern) | |
| 4428 | + // Use payloadRequestedModel to return user's original model alias | |
| 4429 | + msgStart := kiroclaude.BuildClaudeMessageStartEvent( | |
| 4430 | + payloadRequestedModel(opts, req.Model), | |
| 4431 | + totalUsage.InputTokens, | |
| 4432 | + ) | |
| 4187 | 4433 | select { |
| 4188 | 4434 | case <-ctx.Done(): |
| 4189 | 4435 | return |
| 4190 | − case out <- cliproxyexecutor.StreamChunk{Payload: []byte(messageStartEvent.ToSSEString())}: | |
| 4436 | + case out <- cliproxyexecutor.StreamChunk{Payload: append(msgStart, '\n', '\n')}: | |
| 4191 | 4437 | } |
| 4192 | 4438 | |
| 4193 | 4439 | // ── Step 2+: MCP search → InjectToolResultsClaude → callKiroAndBuffer loop ── |
| @@ -4211,19 +4457,15 @@ func (e *KiroExecutor) handleWebSearchStream( · 17 unmodified lines | ||
| 4211 | 4457 | currentToolUseId := fmt.Sprintf("srvtoolu_%s", kiroclaude.GenerateToolUseID()) |
| 4212 | 4458 | |
| 4213 | 4459 | for iteration := 0; iteration < maxWebSearchIterations; iteration++ { |
| 4214 | − log.Infof("kiro/websearch: search iteration %d/%d — query: %s", | |
| 4215 | − iteration+1, maxWebSearchIterations, currentQuery) | |
| 4460 | + log.Infof("kiro/websearch: search iteration %d/%d", | |
| 4461 | + iteration+1, maxWebSearchIterations) | |
| 4216 | 4462 | |
| 4217 | 4463 | // MCP search |
| 4218 | 4464 | _, mcpRequest := kiroclaude.CreateMcpRequest(currentQuery) |
| 4219 | − tokenKey := getTokenKey(auth) | |
| 4220 | − fp := getGlobalFingerprintManager().GetFingerprint(tokenKey) | |
| 4221 | − var authAttrs map[string]string | |
| 4222 | − if auth != nil { | |
| 4223 | − authAttrs = auth.Attributes | |
| 4224 | − } | |
| 4225 | − handler := kiroclaude.NewWebSearchHandler(mcpEndpoint, accessToken, newKiroHTTPClientWithPooling(ctx, e.cfg, auth, 30*time.Second), fp, authAttrs) | |
| 4226 | − mcpResponse, mcpErr := handler.CallMcpAPI(mcpRequest) | |
| 4465 | + | |
| 4466 | + authAttrs := webSearchAuthAttrs(auth) | |
| 4467 | + handler := newWebSearchHandler(ctx, mcpEndpoint, accessToken, newKiroHTTPClientWithPooling(ctx, e.cfg, auth, 30*time.Second), auth, authAttrs) | |
| 4468 | + mcpResponse, mcpErr := handler.callMcpAPI(mcpRequest) | |
| 4227 | 4469 | |
| 4228 | 4470 | var searchResults *kiroclaude.WebSearchResults |
| 4229 | 4471 | if mcpErr != nil { |
| @@ -4245,7 +4487,7 @@ func (e *KiroExecutor) handleWebSearchStream( · 15 unmodified lines | ||
| 4245 | 4487 | select { |
| 4246 | 4488 | case <-ctx.Done(): |
| 4247 | 4489 | return |
| 4248 | − case out <- cliproxyexecutor.StreamChunk{Payload: []byte(event.ToSSEString())}: | |
| 4490 | + case out <- cliproxyexecutor.StreamChunk{Payload: event}: | |
| 4249 | 4491 | } |
| 4250 | 4492 | } |
| 4251 | 4493 | contentBlockIndex += 2 |
| @@ -4255,8 +4497,9 @@ func (e *KiroExecutor) handleWebSearchStream( · 3 unmodified lines | ||
| 4255 | 4497 | currentClaudePayload, err = kiroclaude.InjectToolResultsClaude(currentClaudePayload, currentToolUseId, currentQuery, searchResults) |
| 4256 | 4498 | if err != nil { |
| 4257 | 4499 | log.Warnf("kiro/websearch: failed to inject tool results: %v", err) |
| 4500 | + wsErr = fmt.Errorf("failed to inject tool results: %w", err) | |
| 4258 | 4501 | e.sendFallbackText(ctx, out, contentBlockIndex, currentQuery, searchResults) |
| 4259 | − break | |
| 4502 | + return | |
| 4260 | 4503 | } |
| 4261 | 4504 | |
| 4262 | 4505 | // Call GAR with modified Claude payload (full translation pipeline) |
| @@ -4265,14 +4508,15 @@ func (e *KiroExecutor) handleWebSearchStream( · 2 unmodified lines | ||
| 4265 | 4508 | kiroChunks, kiroErr := e.callKiroAndBuffer(ctx, auth, modifiedReq, opts, accessToken, profileArn) |
| 4266 | 4509 | if kiroErr != nil { |
| 4267 | 4510 | log.Warnf("kiro/websearch: Kiro API failed at iteration %d: %v", iteration+1, kiroErr) |
| 4511 | + wsErr = fmt.Errorf("Kiro API failed at iteration %d: %w", iteration+1, kiroErr) | |
| 4268 | 4512 | e.sendFallbackText(ctx, out, contentBlockIndex, currentQuery, searchResults) |
| 4269 | − break | |
| 4513 | + return | |
| 4270 | 4514 | } |
| 4271 | 4515 | |
| 4272 | 4516 | // Analyze response |
| 4273 | 4517 | analysis := kiroclaude.AnalyzeBufferedStream(kiroChunks) |
| 4274 | − log.Infof("kiro/websearch: iteration %d — stop_reason: %s, has_tool_use: %v, query: %s, toolUseId: %s", | |
| 4275 | − iteration+1, analysis.StopReason, analysis.HasWebSearchToolUse, analysis.WebSearchQuery, analysis.WebSearchToolUseId) | |
| 4518 | + log.Infof("kiro/websearch: iteration %d — stop_reason: %s, has_tool_use: %v", | |
| 4519 | + iteration+1, analysis.StopReason, analysis.HasWebSearchToolUse) | |
| 4276 | 4520 | |
| 4277 | 4521 | if analysis.HasWebSearchToolUse && analysis.WebSearchQuery != "" && iteration+1 < maxWebSearchIterations { |
| 4278 | 4522 | // Model wants another search |
| @@ -4297,12 +4541,14 @@ func (e *KiroExecutor) handleWebSearchStream( · 18 unmodified lines | ||
| 4297 | 4541 | if !shouldForward { |
| 4298 | 4542 | continue |
| 4299 | 4543 | } |
| 4544 | + accumulatedOutputLen += len(adjusted) | |
| 4300 | 4545 | select { |
| 4301 | 4546 | case <-ctx.Done(): |
| 4302 | 4547 | return |
| 4303 | 4548 | case out <- cliproxyexecutor.StreamChunk{Payload: adjusted}: |
| 4304 | 4549 | } |
| 4305 | 4550 | } else { |
| 4551 | + accumulatedOutputLen += len(chunk) | |
| 4306 | 4552 | select { |
| 4307 | 4553 | case <-ctx.Done(): |
| 4308 | 4554 | return |
| @@ -4320,77 +4566,124 @@ func (e *KiroExecutor) handleWebSearchStream( · 11 unmodified lines | ||
| 4320 | 4566 | return out, nil |
| 4321 | 4567 | } |
| 4322 | 4568 | |
| 4323 | −// callKiroAndBuffer calls the Kiro API and buffers all response chunks. | |
| 4324 | −// Returns the buffered chunks for analysis before forwarding to client. | |
| 4325 | −func (e *KiroExecutor) callKiroAndBuffer( | |
| 4569 | +// handleWebSearch handles web_search requests for non-streaming Execute path. | |
| 4570 | +// Performs MCP search synchronously, injects results into the request payload, | |
| 4571 | +// then calls the normal non-streaming Kiro API path which returns a proper | |
| 4572 | +// Claude JSON response (not SSE chunks). | |
| 4573 | +func (e *KiroExecutor) handleWebSearch( | |
| 4326 | 4574 | ctx context.Context, |
| 4327 | 4575 | auth *cliproxyauth.Auth, |
| 4328 | 4576 | req cliproxyexecutor.Request, |
| 4329 | 4577 | opts cliproxyexecutor.Options, |
| 4330 | 4578 | accessToken, profileArn string, |
| 4331 | −) ([][]byte, error) { | |
| 4332 | − from := opts.SourceFormat | |
| 4333 | − to := sdktranslator.FromString("kiro") | |
| 4334 | − body := sdktranslator.TranslateRequest(from, to, req.Model, bytes.Clone(req.Payload), true) | |
| 4335 | − log.Debugf("kiro/websearch GAR request: %d bytes", len(body)) | |
| 4579 | +) (cliproxyexecutor.Response, error) { | |
| 4580 | + // Extract search query from Claude Code's web_search tool_use | |
| 4581 | + query := kiroclaude.ExtractSearchQuery(req.Payload) | |
| 4582 | + if query == "" { | |
| 4583 | + log.Warnf("kiro/websearch: non-stream: failed to extract search query, falling back to normal Execute") | |
| 4584 | + // Fall through to normal non-streaming path | |
| 4585 | + return e.executeNonStreamFallback(ctx, auth, req, opts, accessToken, profileArn) | |
| 4586 | + } | |
| 4336 | 4587 | |
| 4337 | − kiroModelID := e.mapModelToKiro(req.Model) | |
| 4338 | − isAgentic, isChatOnly := determineAgenticMode(req.Model) | |
| 4339 | − effectiveProfileArn := getEffectiveProfileArnWithWarning(auth, profileArn) | |
| 4588 | + // Build MCP endpoint using shared region resolution (supports api_region + ProfileARN fallback) | |
| 4589 | + region := resolveKiroAPIRegion(auth) | |
| 4590 | + mcpEndpoint := kiroclaude.BuildMcpEndpoint(region) | |
| 4340 | 4591 | |
| 4341 | − tokenKey := "" | |
| 4342 | − if auth != nil { | |
| 4343 | − tokenKey = auth.ID | |
| 4592 | + // Step 1: Fetch/cache tool description (sync) | |
| 4593 | + { | |
| 4594 | + authAttrs := webSearchAuthAttrs(auth) | |
| 4595 | + fetchToolDescription(ctx, mcpEndpoint, accessToken, newKiroHTTPClientWithPooling(ctx, e.cfg, auth, 30*time.Second), auth, authAttrs) | |
| 4344 | 4596 | } |
| 4345 | 4597 | |
| 4346 | − kiroStream, err := e.executeStreamWithRetry( | |
| 4347 | − ctx, auth, req, opts, accessToken, effectiveProfileArn, | |
| 4348 | − nil, body, from, nil, "", kiroModelID, isAgentic, isChatOnly, tokenKey, | |
| 4349 | − ) | |
| 4598 | + // Step 2: Perform MCP search | |
| 4599 | + _, mcpRequest := kiroclaude.CreateMcpRequest(query) | |
| 4600 | + | |
| 4601 | + authAttrs := webSearchAuthAttrs(auth) | |
| 4602 | + handler := newWebSearchHandler(ctx, mcpEndpoint, accessToken, newKiroHTTPClientWithPooling(ctx, e.cfg, auth, 30*time.Second), auth, authAttrs) | |
| 4603 | + mcpResponse, mcpErr := handler.callMcpAPI(mcpRequest) | |
| 4604 | + | |
| 4605 | + var searchResults *kiroclaude.WebSearchResults | |
| 4606 | + if mcpErr != nil { | |
| 4607 | + log.Warnf("kiro/websearch: non-stream: MCP API call failed: %v, continuing with empty results", mcpErr) | |
| 4608 | + } else { | |
| 4609 | + searchResults = kiroclaude.ParseSearchResults(mcpResponse) | |
| 4610 | + } | |
| 4611 | + | |
| 4612 | + resultCount := 0 | |
| 4613 | + if searchResults != nil { | |
| 4614 | + resultCount = len(searchResults.Results) | |
| 4615 | + } | |
| 4616 | + log.Infof("kiro/websearch: non-stream: got %d search results", resultCount) | |
| 4617 | + | |
| 4618 | + // Step 3: Replace restrictive web_search tool description (align with streaming path) | |
| 4619 | + simplifiedPayload, simplifyErr := kiroclaude.ReplaceWebSearchToolDescription(bytes.Clone(req.Payload)) | |
| 4620 | + if simplifyErr != nil { | |
| 4621 | + log.Warnf("kiro/websearch: non-stream: failed to simplify web_search tool: %v, using original payload", simplifyErr) | |
| 4622 | + simplifiedPayload = bytes.Clone(req.Payload) | |
| 4623 | + } | |
| 4624 | + | |
| 4625 | + // Step 4: Inject search tool_use + tool_result into Claude payload | |
| 4626 | + currentToolUseId := fmt.Sprintf("srvtoolu_%s", kiroclaude.GenerateToolUseID()) | |
| 4627 | + modifiedPayload, err := kiroclaude.InjectToolResultsClaude(simplifiedPayload, currentToolUseId, query, searchResults) | |
| 4350 | 4628 | if err != nil { |
| 4351 | − return nil, err | |
| 4629 | + log.Warnf("kiro/websearch: non-stream: failed to inject tool results: %v, falling back", err) | |
| 4630 | + return e.executeNonStreamFallback(ctx, auth, req, opts, accessToken, profileArn) | |
| 4352 | 4631 | } |
| 4353 | 4632 | |
| 4354 | − // Buffer all chunks | |
| 4355 | − var chunks [][]byte | |
| 4356 | − for chunk := range kiroStream { | |
| 4357 | − if chunk.Err != nil { | |
| 4358 | − return chunks, chunk.Err | |
| 4359 | − } | |
| 4360 | − if len(chunk.Payload) > 0 { | |
| 4361 | − chunks = append(chunks, bytes.Clone(chunk.Payload)) | |
| 4362 | − } | |
| 4633 | + // Step 5: Call Kiro API via the normal non-streaming path (executeWithRetry) | |
| 4634 | + // This path uses parseEventStream → BuildClaudeResponse → TranslateNonStream | |
| 4635 | + // to produce a proper Claude JSON response | |
| 4636 | + modifiedReq := req | |
| 4637 | + modifiedReq.Payload = modifiedPayload | |
| 4638 | + | |
| 4639 | + resp, err := e.executeNonStreamFallback(ctx, auth, modifiedReq, opts, accessToken, profileArn) | |
| 4640 | + if err != nil { | |
| 4641 | + return resp, err | |
| 4363 | 4642 | } |
| 4364 | 4643 | |
| 4365 | − log.Debugf("kiro/websearch GAR response: %d chunks buffered", len(chunks)) | |
| 4644 | + // Step 6: Inject server_tool_use + web_search_tool_result into response | |
| 4645 | + // so Claude Code can display "Did X searches in Ys" | |
| 4646 | + indicators := []kiroclaude.SearchIndicator{ | |
| 4647 | + { | |
| 4648 | + ToolUseID: currentToolUseId, | |
| 4649 | + Query: query, | |
| 4650 | + Results: searchResults, | |
| 4651 | + }, | |
| 4652 | + } | |
| 4653 | + injectedPayload, injErr := kiroclaude.InjectSearchIndicatorsInResponse(resp.Payload, indicators) | |
| 4654 | + if injErr != nil { | |
| 4655 | + log.Warnf("kiro/websearch: non-stream: failed to inject search indicators: %v", injErr) | |
| 4656 | + } else { | |
| 4657 | + resp.Payload = injectedPayload | |
| 4658 | + } | |
| 4366 | 4659 | |
| 4367 | − return chunks, nil | |
| 4660 | + return resp, nil | |
| 4368 | 4661 | } |
| 4369 | 4662 | |
| 4370 | −// callKiroRawAndBuffer calls the Kiro API with a pre-built Kiro payload (no translation). | |
| 4371 | −// Used in the web search loop where the payload is modified directly in Kiro format. | |
| 4372 | −func (e *KiroExecutor) callKiroRawAndBuffer( | |
| 4663 | +// callKiroAndBuffer calls the Kiro API and buffers all response chunks. | |
| 4664 | +// Returns the buffered chunks for analysis before forwarding to client. | |
| 4665 | +// Usage reporting is NOT done here — the caller (handleWebSearchStream) manages its own reporter. | |
| 4666 | +func (e *KiroExecutor) callKiroAndBuffer( | |
| 4373 | 4667 | ctx context.Context, |
| 4374 | 4668 | auth *cliproxyauth.Auth, |
| 4375 | 4669 | req cliproxyexecutor.Request, |
| 4376 | 4670 | opts cliproxyexecutor.Options, |
| 4377 | 4671 | accessToken, profileArn string, |
| 4378 | − kiroBody []byte, | |
| 4379 | 4672 | ) ([][]byte, error) { |
| 4673 | + from := opts.SourceFormat | |
| 4674 | + to := sdktranslator.FromString("kiro") | |
| 4675 | + body := sdktranslator.TranslateRequest(from, to, req.Model, bytes.Clone(req.Payload), true) | |
| 4676 | + log.Debugf("kiro/websearch GAR request: %d bytes", len(body)) | |
| 4677 | + | |
| 4380 | 4678 | kiroModelID := e.mapModelToKiro(req.Model) |
| 4381 | 4679 | isAgentic, isChatOnly := determineAgenticMode(req.Model) |
| 4382 | 4680 | effectiveProfileArn := getEffectiveProfileArnWithWarning(auth, profileArn) |
| 4383 | 4681 | |
| 4384 | − tokenKey := "" | |
| 4385 | − if auth != nil { | |
| 4386 | − tokenKey = auth.ID | |
| 4387 | − } | |
| 4388 | − log.Debugf("kiro/websearch GAR raw request: %d bytes", len(kiroBody)) | |
| 4682 | + tokenKey := getTokenKey(auth) | |
| 4389 | 4683 | |
| 4390 | − kiroFormat := sdktranslator.FromString("kiro") | |
| 4391 | 4684 | kiroStream, err := e.executeStreamWithRetry( |
| 4392 | 4685 | ctx, auth, req, opts, accessToken, effectiveProfileArn, |
| 4393 | − nil, kiroBody, kiroFormat, nil, "", kiroModelID, isAgentic, isChatOnly, tokenKey, | |
| 4686 | + nil, body, from, nil, "", kiroModelID, isAgentic, isChatOnly, tokenKey, | |
| 4394 | 4687 | ) |
| 4395 | 4688 | if err != nil { |
| 4396 | 4689 | return nil, err |
| @@ -4407,7 +4700,7 @@ func (e *KiroExecutor) callKiroRawAndBuffer( · 10 unmodified lines | ||
| 4407 | 4700 | } |
| 4408 | 4701 | } |
| 4409 | 4702 | |
| 4410 | − log.Debugf("kiro/websearch GAR raw response: %d chunks buffered", len(chunks)) | |
| 4703 | + log.Debugf("kiro/websearch GAR response: %d chunks buffered", len(chunks)) | |
| 4411 | 4704 | |
| 4412 | 4705 | return chunks, nil |
| 4413 | 4706 | } |
| @@ -4428,18 +4721,22 @@ func (e *KiroExecutor) callKiroDirectStream( · 14 unmodified lines | ||
| 4428 | 4721 | isAgentic, isChatOnly := determineAgenticMode(req.Model) |
| 4429 | 4722 | effectiveProfileArn := getEffectiveProfileArnWithWarning(auth, profileArn) |
| 4430 | 4723 | |
| 4431 | − tokenKey := "" | |
| 4432 | − if auth != nil { | |
| 4433 | − tokenKey = auth.ID | |
| 4434 | − } | |
| 4724 | + tokenKey := getTokenKey(auth) | |
| 4725 | + | |
| 4726 | + reporter := newUsageReporter(ctx, e.Identifier(), req.Model, auth) | |
| 4727 | + var streamErr error | |
| 4728 | + defer reporter.trackFailure(ctx, &streamErr) | |
| 4435 | 4729 | |
| 4436 | − return e.executeStreamWithRetry( | |
| 4730 | + stream, streamErr := e.executeStreamWithRetry( | |
| 4437 | 4731 | ctx, auth, req, opts, accessToken, effectiveProfileArn, |
| 4438 | − nil, body, from, nil, "", kiroModelID, isAgentic, isChatOnly, tokenKey, | |
| 4732 | + nil, body, from, reporter, "", kiroModelID, isAgentic, isChatOnly, tokenKey, | |
| 4439 | 4733 | ) |
| 4734 | + return stream, streamErr | |
| 4440 | 4735 | } |
| 4441 | 4736 | |
| 4442 | 4737 | // sendFallbackText sends a simple text response when the Kiro API fails during the search loop. |
| 4738 | +// Delegates SSE event construction to kiroclaude.BuildFallbackTextEvents() for alignment | |
| 4739 | +// with how streamToChannel() uses BuildClaude*Event() functions. | |
| 4443 | 4740 | func (e *KiroExecutor) sendFallbackText( |
| 4444 | 4741 | ctx context.Context, |
| 4445 | 4742 | out chan<- cliproxyexecutor.StreamChunk, |
| @@ -4447,182 +4744,14 @@ func (e *KiroExecutor) sendFallbackText( · 1 unmodified line | ||
| 4447 | 4744 | query string, |
| 4448 | 4745 | searchResults *kiroclaude.WebSearchResults, |
| 4449 | 4746 | ) { |
| 4450 | − // Generate a simple text summary from search results | |
| 4451 | − summary := kiroclaude.FormatSearchContextPrompt(query, searchResults) | |
| 4452 | − | |
| 4453 | − events := []kiroclaude.SseEvent{ | |
| 4454 | − { | |
| 4455 | − Event: "content_block_start", | |
| 4456 | − Data: map[string]interface{}{ | |
| 4457 | − "type": "content_block_start", | |
| 4458 | − "index": contentBlockIndex, | |
| 4459 | − "content_block": map[string]interface{}{ | |
| 4460 | − "type": "text", | |
| 4461 | − "text": "", | |
| 4462 | − }, | |
| 4463 | − }, | |
| 4464 | − }, | |
| 4465 | − { | |
| 4466 | − Event: "content_block_delta", | |
| 4467 | − Data: map[string]interface{}{ | |
| 4468 | − "type": "content_block_delta", | |
| 4469 | − "index": contentBlockIndex, | |
| 4470 | − "delta": map[string]interface{}{ | |
| 4471 | − "type": "text_delta", | |
| 4472 | − "text": summary, | |
| 4473 | − }, | |
| 4474 | − }, | |
| 4475 | − }, | |
| 4476 | − { | |
| 4477 | − Event: "content_block_stop", | |
| 4478 | − Data: map[string]interface{}{ | |
| 4479 | − "type": "content_block_stop", | |
| 4480 | − "index": contentBlockIndex, | |
| 4481 | − }, | |
| 4482 | − }, | |
| 4483 | − } | |
| 4484 | − | |
| 4747 | + events := kiroclaude.BuildFallbackTextEvents(contentBlockIndex, query, searchResults) | |
| 4485 | 4748 | for _, event := range events { |
| 4486 | 4749 | select { |
| 4487 | 4750 | case <-ctx.Done(): |
| 4488 | 4751 | return |
| 4489 | − case out <- cliproxyexecutor.StreamChunk{Payload: []byte(event.ToSSEString())}: | |
| 4490 | − } | |
| 4491 | − } | |
| 4492 | − | |
| 4493 | − // Send message_delta with end_turn and message_stop | |
| 4494 | − msgDelta := kiroclaude.SseEvent{ | |
| 4495 | − Event: "message_delta", | |
| 4496 | − Data: map[string]interface{}{ | |
| 4497 | − "type": "message_delta", | |
| 4498 | − "delta": map[string]interface{}{ | |
| 4499 | − "stop_reason": "end_turn", | |
| 4500 | − "stop_sequence": nil, | |
| 4501 | − }, | |
| 4502 | − "usage": map[string]interface{}{ | |
| 4503 | − "output_tokens": len(summary) / 4, | |
| 4504 | − }, | |
| 4505 | − }, | |
| 4506 | − } | |
| 4507 | − select { | |
| 4508 | − case <-ctx.Done(): | |
| 4509 | − return | |
| 4510 | − case out <- cliproxyexecutor.StreamChunk{Payload: []byte(msgDelta.ToSSEString())}: | |
| 4511 | − } | |
| 4512 | − | |
| 4513 | − msgStop := kiroclaude.SseEvent{ | |
| 4514 | − Event: "message_stop", | |
| 4515 | − Data: map[string]interface{}{ | |
| 4516 | − "type": "message_stop", | |
| 4517 | − }, | |
| 4518 | − } | |
| 4519 | − select { | |
| 4520 | − case <-ctx.Done(): | |
| 4521 | − return | |
| 4522 | − case out <- cliproxyexecutor.StreamChunk{Payload: []byte(msgStop.ToSSEString())}: | |
| 4523 | − } | |
| 4524 | − | |
| 4525 | −} | |
| 4526 | − | |
| 4527 | −// handleWebSearch handles web_search requests for non-streaming Execute path. | |
| 4528 | −// Performs MCP search synchronously, injects results into the request payload, | |
| 4529 | −// then calls the normal non-streaming Kiro API path which returns a proper | |
| 4530 | −// Claude JSON response (not SSE chunks). | |
| 4531 | −func (e *KiroExecutor) handleWebSearch( | |
| 4532 | − ctx context.Context, | |
| 4533 | − auth *cliproxyauth.Auth, | |
| 4534 | − req cliproxyexecutor.Request, | |
| 4535 | − opts cliproxyexecutor.Options, | |
| 4536 | − accessToken, profileArn string, | |
| 4537 | −) (cliproxyexecutor.Response, error) { | |
| 4538 | − // Extract search query from Claude Code's web_search tool_use | |
| 4539 | − query := kiroclaude.ExtractSearchQuery(req.Payload) | |
| 4540 | − if query == "" { | |
| 4541 | − log.Warnf("kiro/websearch: non-stream: failed to extract search query, falling back to normal Execute") | |
| 4542 | − // Fall through to normal non-streaming path | |
| 4543 | − return e.executeNonStreamFallback(ctx, auth, req, opts, accessToken, profileArn) | |
| 4544 | − } | |
| 4545 | − | |
| 4546 | − // Build MCP endpoint based on region | |
| 4547 | − region := kiroDefaultRegion | |
| 4548 | − if auth != nil && auth.Metadata != nil { | |
| 4549 | − if r, ok := auth.Metadata["api_region"].(string); ok && r != "" { | |
| 4550 | − region = r | |
| 4752 | + case out <- cliproxyexecutor.StreamChunk{Payload: append(event, '\n', '\n')}: | |
| 4551 | 4753 | } |
| 4552 | 4754 | } |
| 4553 | − mcpEndpoint := fmt.Sprintf("https://q.%s.amazonaws.com/mcp", region) | |
| 4554 | − | |
| 4555 | − // Step 1: Fetch/cache tool description (sync) | |
| 4556 | − { | |
| 4557 | − tokenKey := getTokenKey(auth) | |
| 4558 | − fp := getGlobalFingerprintManager().GetFingerprint(tokenKey) | |
| 4559 | − var authAttrs map[string]string | |
| 4560 | − if auth != nil { | |
| 4561 | − authAttrs = auth.Attributes | |
| 4562 | − } | |
| 4563 | − kiroclaude.FetchToolDescription(mcpEndpoint, accessToken, newKiroHTTPClientWithPooling(ctx, e.cfg, auth, 30*time.Second), fp, authAttrs) | |
| 4564 | − } | |
| 4565 | − | |
| 4566 | − // Step 2: Perform MCP search | |
| 4567 | − _, mcpRequest := kiroclaude.CreateMcpRequest(query) | |
| 4568 | − tokenKey := getTokenKey(auth) | |
| 4569 | − fp := getGlobalFingerprintManager().GetFingerprint(tokenKey) | |
| 4570 | − var authAttrs map[string]string | |
| 4571 | − if auth != nil { | |
| 4572 | − authAttrs = auth.Attributes | |
| 4573 | − } | |
| 4574 | − handler := kiroclaude.NewWebSearchHandler(mcpEndpoint, accessToken, newKiroHTTPClientWithPooling(ctx, e.cfg, auth, 30*time.Second), fp, authAttrs) | |
| 4575 | − mcpResponse, mcpErr := handler.CallMcpAPI(mcpRequest) | |
| 4576 | − | |
| 4577 | − var searchResults *kiroclaude.WebSearchResults | |
| 4578 | − if mcpErr != nil { | |
| 4579 | − log.Warnf("kiro/websearch: non-stream: MCP API call failed: %v, continuing with empty results", mcpErr) | |
| 4580 | − } else { | |
| 4581 | − searchResults = kiroclaude.ParseSearchResults(mcpResponse) | |
| 4582 | − } | |
| 4583 | − | |
| 4584 | − resultCount := 0 | |
| 4585 | − if searchResults != nil { | |
| 4586 | − resultCount = len(searchResults.Results) | |
| 4587 | − } | |
| 4588 | − log.Infof("kiro/websearch: non-stream: got %d search results for query: %s", resultCount, query) | |
| 4589 | − | |
| 4590 | − // Step 3: Inject search tool_use + tool_result into Claude payload | |
| 4591 | − currentToolUseId := fmt.Sprintf("srvtoolu_%s", kiroclaude.GenerateToolUseID()) | |
| 4592 | − modifiedPayload, err := kiroclaude.InjectToolResultsClaude(bytes.Clone(req.Payload), currentToolUseId, query, searchResults) | |
| 4593 | − if err != nil { | |
| 4594 | − log.Warnf("kiro/websearch: non-stream: failed to inject tool results: %v, falling back", err) | |
| 4595 | − return e.executeNonStreamFallback(ctx, auth, req, opts, accessToken, profileArn) | |
| 4596 | − } | |
| 4597 | − | |
| 4598 | − // Step 4: Call Kiro API via the normal non-streaming path (executeWithRetry) | |
| 4599 | − // This path uses parseEventStream → BuildClaudeResponse → TranslateNonStream | |
| 4600 | − // to produce a proper Claude JSON response | |
| 4601 | − modifiedReq := req | |
| 4602 | − modifiedReq.Payload = modifiedPayload | |
| 4603 | − | |
| 4604 | − resp, err := e.executeNonStreamFallback(ctx, auth, modifiedReq, opts, accessToken, profileArn) | |
| 4605 | − if err != nil { | |
| 4606 | − return resp, err | |
| 4607 | − } | |
| 4608 | − | |
| 4609 | − // Step 5: Inject server_tool_use + web_search_tool_result into response | |
| 4610 | − // so Claude Code can display "Did X searches in Ys" | |
| 4611 | − indicators := []kiroclaude.SearchIndicator{ | |
| 4612 | − { | |
| 4613 | − ToolUseID: currentToolUseId, | |
| 4614 | − Query: query, | |
| 4615 | − Results: searchResults, | |
| 4616 | − }, | |
| 4617 | − } | |
| 4618 | − injectedPayload, injErr := kiroclaude.InjectSearchIndicatorsInResponse(resp.Payload, indicators) | |
| 4619 | − if injErr != nil { | |
| 4620 | − log.Warnf("kiro/websearch: non-stream: failed to inject search indicators: %v", injErr) | |
| 4621 | − } else { | |
| 4622 | − resp.Payload = injectedPayload | |
| 4623 | − } | |
| 4624 | − | |
| 4625 | − return resp, nil | |
| 4626 | 4755 | } |
| 4627 | 4756 | |
| 4628 | 4757 | // executeNonStreamFallback runs the standard non-streaming Execute path for a request. |
| @@ -183,4 +183,124 @@ func PendingTagSuffix(buffer, tag string) int { · 182 unmodified lines | ||
| 183 | 183 | } |
| 184 | 184 | } |
| 185 | 185 | return 0 |
| 186 | −} | |
| 186 | +} | |
| 187 | + | |
| 188 | +// GenerateSearchIndicatorEvents generates ONLY the search indicator SSE events | |
| 189 | +// (server_tool_use + web_search_tool_result) without text summary or message termination. | |
| 190 | +// These events trigger Claude Code's search indicator UI. | |
| 191 | +// The caller is responsible for sending message_start before and message_delta/stop after. | |
| 192 | +func GenerateSearchIndicatorEvents( | |
| 193 | + query string, | |
| 194 | + toolUseID string, | |
| 195 | + searchResults *WebSearchResults, | |
| 196 | + startIndex int, | |
| 197 | +) [][]byte { | |
| 198 | + events := make([][]byte, 0, 5) | |
| 199 | + | |
| 200 | + // 1. content_block_start (server_tool_use) | |
| 201 | + event1 := map[string]interface{}{ | |
| 202 | + "type": "content_block_start", | |
| 203 | + "index": startIndex, | |
| 204 | + "content_block": map[string]interface{}{ | |
| 205 | + "id": toolUseID, | |
| 206 | + "type": "server_tool_use", | |
| 207 | + "name": "web_search", | |
| 208 | + "input": map[string]interface{}{}, | |
| 209 | + }, | |
| 210 | + } | |
| 211 | + data1, _ := json.Marshal(event1) | |
| 212 | + events = append(events, []byte("event: content_block_start\ndata: "+string(data1)+"\n\n")) | |
| 213 | + | |
| 214 | + // 2. content_block_delta (input_json_delta) | |
| 215 | + inputJSON, _ := json.Marshal(map[string]string{"query": query}) | |
| 216 | + event2 := map[string]interface{}{ | |
| 217 | + "type": "content_block_delta", | |
| 218 | + "index": startIndex, | |
| 219 | + "delta": map[string]interface{}{ | |
| 220 | + "type": "input_json_delta", | |
| 221 | + "partial_json": string(inputJSON), | |
| 222 | + }, | |
| 223 | + } | |
| 224 | + data2, _ := json.Marshal(event2) | |
| 225 | + events = append(events, []byte("event: content_block_delta\ndata: "+string(data2)+"\n\n")) | |
| 226 | + | |
| 227 | + // 3. content_block_stop (server_tool_use) | |
| 228 | + event3 := map[string]interface{}{ | |
| 229 | + "type": "content_block_stop", | |
| 230 | + "index": startIndex, | |
| 231 | + } | |
| 232 | + data3, _ := json.Marshal(event3) | |
| 233 | + events = append(events, []byte("event: content_block_stop\ndata: "+string(data3)+"\n\n")) | |
| 234 | + | |
| 235 | + // 4. content_block_start (web_search_tool_result) | |
| 236 | + searchContent := make([]map[string]interface{}, 0) | |
| 237 | + if searchResults != nil { | |
| 238 | + for _, r := range searchResults.Results { | |
| 239 | + snippet := "" | |
| 240 | + if r.Snippet != nil { | |
| 241 | + snippet = *r.Snippet | |
| 242 | + } | |
| 243 | + searchContent = append(searchContent, map[string]interface{}{ | |
| 244 | + "type": "web_search_result", | |
| 245 | + "title": r.Title, | |
| 246 | + "url": r.URL, | |
| 247 | + "encrypted_content": snippet, | |
| 248 | + "page_age": nil, | |
| 249 | + }) | |
| 250 | + } | |
| 251 | + } | |
| 252 | + event4 := map[string]interface{}{ | |
| 253 | + "type": "content_block_start", | |
| 254 | + "index": startIndex + 1, | |
| 255 | + "content_block": map[string]interface{}{ | |
| 256 | + "type": "web_search_tool_result", | |
| 257 | + "tool_use_id": toolUseID, | |
| 258 | + "content": searchContent, | |
| 259 | + }, | |
| 260 | + } | |
| 261 | + data4, _ := json.Marshal(event4) | |
| 262 | + events = append(events, []byte("event: content_block_start\ndata: "+string(data4)+"\n\n")) | |
| 263 | + | |
| 264 | + // 5. content_block_stop (web_search_tool_result) | |
| 265 | + event5 := map[string]interface{}{ | |
| 266 | + "type": "content_block_stop", | |
| 267 | + "index": startIndex + 1, | |
| 268 | + } | |
| 269 | + data5, _ := json.Marshal(event5) | |
| 270 | + events = append(events, []byte("event: content_block_stop\ndata: "+string(data5)+"\n\n")) | |
| 271 | + | |
| 272 | + return events | |
| 273 | +} | |
| 274 | + | |
| 275 | +// BuildFallbackTextEvents generates SSE events for a fallback text response | |
| 276 | +// when the Kiro API fails during the search loop. Uses BuildClaude*Event() | |
| 277 | +// functions to align with streamToChannel patterns. | |
| 278 | +// Returns raw SSE byte slices ready to be sent to the client channel. | |
| 279 | +func BuildFallbackTextEvents(contentBlockIndex int, query string, results *WebSearchResults) [][]byte { | |
| 280 | + summary := FormatSearchContextPrompt(query, results) | |
| 281 | + outputTokens := len(summary) / 4 | |
| 282 | + if len(summary) > 0 && outputTokens == 0 { | |
| 283 | + outputTokens = 1 | |
| 284 | + } | |
| 285 | + | |
| 286 | + var events [][]byte | |
| 287 | + | |
| 288 | + // content_block_start (text) | |
| 289 | + events = append(events, BuildClaudeContentBlockStartEvent(contentBlockIndex, "text", "", "")) | |
| 290 | + | |
| 291 | + // content_block_delta (text_delta) | |
| 292 | + events = append(events, BuildClaudeStreamEvent(summary, contentBlockIndex)) | |
| 293 | + | |
| 294 | + // content_block_stop | |
| 295 | + events = append(events, BuildClaudeContentBlockStopEvent(contentBlockIndex)) | |
| 296 | + | |
| 297 | + // message_delta with end_turn | |
| 298 | + events = append(events, BuildClaudeMessageDeltaEvent("end_turn", usage.Detail{ | |
| 299 | + OutputTokens: int64(outputTokens), | |
| 300 | + })) | |
| 301 | + | |
| 302 | + // message_stop | |
| 303 | + events = append(events, BuildClaudeMessageStopOnlyEvent()) | |
| 304 | + | |
| 305 | + return events | |
| 306 | +} | |
| @@ -0,0 +1,350 @@ | ||
| 1 | +package claude | |
| 2 | + | |
| 3 | +import ( | |
| 4 | + "encoding/json" | |
| 5 | + "strings" | |
| 6 | + | |
| 7 | + log "github.com/sirupsen/logrus" | |
| 8 | +) | |
| 9 | + | |
| 10 | +// sseEvent represents a Server-Sent Event | |
| 11 | +type sseEvent struct { | |
| 12 | + Event string | |
| 13 | + Data interface{} | |
| 14 | +} | |
| 15 | + | |
| 16 | +// ToSSEString converts the event to SSE wire format | |
| 17 | +func (e *sseEvent) ToSSEString() string { | |
| 18 | + dataBytes, _ := json.Marshal(e.Data) | |
| 19 | + return "event: " + e.Event + "\ndata: " + string(dataBytes) + "\n\n" | |
| 20 | +} | |
| 21 | + | |
| 22 | +// AdjustStreamIndices adjusts content block indices in SSE event data by adding an offset. | |
| 23 | +// It also suppresses duplicate message_start events (returns shouldForward=false). | |
| 24 | +// This is used to combine search indicator events (indices 0,1) with Kiro model response events. | |
| 25 | +// | |
| 26 | +// The data parameter is a single SSE "data:" line payload (JSON). | |
| 27 | +// Returns: adjusted data, shouldForward (false = skip this event). | |
| 28 | +func AdjustStreamIndices(data []byte, offset int) ([]byte, bool) { | |
| 29 | + if len(data) == 0 { | |
| 30 | + return data, true | |
| 31 | + } | |
| 32 | + | |
| 33 | + // Quick check: parse the JSON | |
| 34 | + var event map[string]interface{} | |
| 35 | + if err := json.Unmarshal(data, &event); err != nil { | |
| 36 | + // Not valid JSON, pass through | |
| 37 | + return data, true | |
| 38 | + } | |
| 39 | + | |
| 40 | + eventType, _ := event["type"].(string) | |
| 41 | + | |
| 42 | + // Suppress duplicate message_start events | |
| 43 | + if eventType == "message_start" { | |
| 44 | + return data, false | |
| 45 | + } | |
| 46 | + | |
| 47 | + // Adjust index for content_block events | |
| 48 | + switch eventType { | |
| 49 | + case "content_block_start", "content_block_delta", "content_block_stop": | |
| 50 | + if idx, ok := event["index"].(float64); ok { | |
| 51 | + event["index"] = int(idx) + offset | |
| 52 | + adjusted, err := json.Marshal(event) | |
| 53 | + if err != nil { | |
| 54 | + return data, true | |
| 55 | + } | |
| 56 | + return adjusted, true | |
| 57 | + } | |
| 58 | + } | |
| 59 | + | |
| 60 | + // Pass through all other events unchanged (message_delta, message_stop, ping, etc.) | |
| 61 | + return data, true | |
| 62 | +} | |
| 63 | + | |
| 64 | +// AdjustSSEChunk processes a raw SSE chunk (potentially containing multiple "event:/data:" pairs) | |
| 65 | +// and adjusts content block indices. Suppresses duplicate message_start events. | |
| 66 | +// Returns the adjusted chunk and whether it should be forwarded. | |
| 67 | +func AdjustSSEChunk(chunk []byte, offset int) ([]byte, bool) { | |
| 68 | + chunkStr := string(chunk) | |
| 69 | + | |
| 70 | + // Fast path: if no "data:" prefix, pass through | |
| 71 | + if !strings.Contains(chunkStr, "data: ") { | |
| 72 | + return chunk, true | |
| 73 | + } | |
| 74 | + | |
| 75 | + var result strings.Builder | |
| 76 | + hasContent := false | |
| 77 | + | |
| 78 | + lines := strings.Split(chunkStr, "\n") | |
| 79 | + for i := 0; i < len(lines); i++ { | |
| 80 | + line := lines[i] | |
| 81 | + | |
| 82 | + if strings.HasPrefix(line, "data: ") { | |
| 83 | + dataPayload := strings.TrimPrefix(line, "data: ") | |
| 84 | + dataPayload = strings.TrimSpace(dataPayload) | |
| 85 | + | |
| 86 | + if dataPayload == "[DONE]" { | |
| 87 | + result.WriteString(line + "\n") | |
| 88 | + hasContent = true | |
| 89 | + continue | |
| 90 | + } | |
| 91 | + | |
| 92 | + adjusted, shouldForward := AdjustStreamIndices([]byte(dataPayload), offset) | |
| 93 | + if !shouldForward { | |
| 94 | + // Skip this event and its preceding "event:" line | |
| 95 | + // Also skip the trailing empty line | |
| 96 | + continue | |
| 97 | + } | |
| 98 | + | |
| 99 | + result.WriteString("data: " + string(adjusted) + "\n") | |
| 100 | + hasContent = true | |
| 101 | + } else if strings.HasPrefix(line, "event: ") { | |
| 102 | + // Check if the next data line will be suppressed | |
| 103 | + if i+1 < len(lines) && strings.HasPrefix(lines[i+1], "data: ") { | |
| 104 | + dataPayload := strings.TrimPrefix(lines[i+1], "data: ") | |
| 105 | + dataPayload = strings.TrimSpace(dataPayload) | |
| 106 | + | |
| 107 | + var event map[string]interface{} | |
| 108 | + if err := json.Unmarshal([]byte(dataPayload), &event); err == nil { | |
| 109 | + if eventType, ok := event["type"].(string); ok && eventType == "message_start" { | |
| 110 | + // Skip both the event: and data: lines | |
| 111 | + i++ // skip the data: line too | |
| 112 | + continue | |
| 113 | + } | |
| 114 | + } | |
| 115 | + } | |
| 116 | + result.WriteString(line + "\n") | |
| 117 | + hasContent = true | |
| 118 | + } else { | |
| 119 | + result.WriteString(line + "\n") | |
| 120 | + if strings.TrimSpace(line) != "" { | |
| 121 | + hasContent = true | |
| 122 | + } | |
| 123 | + } | |
| 124 | + } | |
| 125 | + | |
| 126 | + if !hasContent { | |
| 127 | + return nil, false | |
| 128 | + } | |
| 129 | + | |
| 130 | + return []byte(result.String()), true | |
| 131 | +} | |
| 132 | + | |
| 133 | +// BufferedStreamResult contains the analysis of buffered SSE chunks from a Kiro API response. | |
| 134 | +type BufferedStreamResult struct { | |
| 135 | + // StopReason is the detected stop_reason from the stream (e.g., "end_turn", "tool_use") | |
| 136 | + StopReason string | |
| 137 | + // WebSearchQuery is the extracted query if the model requested another web_search | |
| 138 | + WebSearchQuery string | |
| 139 | + // WebSearchToolUseId is the tool_use ID from the model's response (needed for toolResults) | |
| 140 | + WebSearchToolUseId string | |
| 141 | + // HasWebSearchToolUse indicates whether the model requested web_search | |
| 142 | + HasWebSearchToolUse bool | |
| 143 | + // WebSearchToolUseIndex is the content_block index of the web_search tool_use | |
| 144 | + WebSearchToolUseIndex int | |
| 145 | +} | |
| 146 | + | |
| 147 | +// AnalyzeBufferedStream scans buffered SSE chunks to detect stop_reason and web_search tool_use. | |
| 148 | +// This is used in the search loop to determine if the model wants another search round. | |
| 149 | +func AnalyzeBufferedStream(chunks [][]byte) BufferedStreamResult { | |
| 150 | + result := BufferedStreamResult{WebSearchToolUseIndex: -1} | |
| 151 | + | |
| 152 | + // Track tool use state across chunks | |
| 153 | + var currentToolName string | |
| 154 | + var currentToolIndex int = -1 | |
| 155 | + var toolInputBuilder strings.Builder | |
| 156 | + | |
| 157 | + for _, chunk := range chunks { | |
| 158 | + chunkStr := string(chunk) | |
| 159 | + lines := strings.Split(chunkStr, "\n") | |
| 160 | + for _, line := range lines { | |
| 161 | + if !strings.HasPrefix(line, "data: ") { | |
| 162 | + continue | |
| 163 | + } | |
| 164 | + dataPayload := strings.TrimPrefix(line, "data: ") | |
| 165 | + dataPayload = strings.TrimSpace(dataPayload) | |
| 166 | + if dataPayload == "[DONE]" || dataPayload == "" { | |
| 167 | + continue | |
| 168 | + } | |
| 169 | + | |
| 170 | + var event map[string]interface{} | |
| 171 | + if err := json.Unmarshal([]byte(dataPayload), &event); err != nil { | |
| 172 | + continue | |
| 173 | + } | |
| 174 | + | |
| 175 | + eventType, _ := event["type"].(string) | |
| 176 | + | |
| 177 | + switch eventType { | |
| 178 | + case "message_delta": | |
| 179 | + // Extract stop_reason from message_delta | |
| 180 | + if delta, ok := event["delta"].(map[string]interface{}); ok { | |
| 181 | + if sr, ok := delta["stop_reason"].(string); ok && sr != "" { | |
| 182 | + result.StopReason = sr | |
| 183 | + } | |
| 184 | + } | |
| 185 | + | |
| 186 | + case "content_block_start": | |
| 187 | + // Detect tool_use content blocks | |
| 188 | + if cb, ok := event["content_block"].(map[string]interface{}); ok { | |
| 189 | + if cbType, ok := cb["type"].(string); ok && cbType == "tool_use" { | |
| 190 | + if name, ok := cb["name"].(string); ok { | |
| 191 | + currentToolName = strings.ToLower(name) | |
| 192 | + if idx, ok := event["index"].(float64); ok { | |
| 193 | + currentToolIndex = int(idx) | |
| 194 | + } | |
| 195 | + // Capture tool use ID for toolResults handshake | |
| 196 | + if id, ok := cb["id"].(string); ok { | |
| 197 | + result.WebSearchToolUseId = id | |
| 198 | + } | |
| 199 | + toolInputBuilder.Reset() | |
| 200 | + } | |
| 201 | + } | |
| 202 | + } | |
| 203 | + | |
| 204 | + case "content_block_delta": | |
| 205 | + // Accumulate tool input JSON | |
| 206 | + if currentToolName != "" { | |
| 207 | + if delta, ok := event["delta"].(map[string]interface{}); ok { | |
| 208 | + if deltaType, ok := delta["type"].(string); ok && deltaType == "input_json_delta" { | |
| 209 | + if partial, ok := delta["partial_json"].(string); ok { | |
| 210 | + toolInputBuilder.WriteString(partial) | |
| 211 | + } | |
| 212 | + } | |
| 213 | + } | |
| 214 | + } | |
| 215 | + | |
| 216 | + case "content_block_stop": | |
| 217 | + // Finalize tool use detection | |
| 218 | + if currentToolName == "web_search" || currentToolName == "websearch" || currentToolName == "remote_web_search" { | |
| 219 | + result.HasWebSearchToolUse = true | |
| 220 | + result.WebSearchToolUseIndex = currentToolIndex | |
| 221 | + // Extract query from accumulated input JSON | |
| 222 | + inputJSON := toolInputBuilder.String() | |
| 223 | + var input map[string]string | |
| 224 | + if err := json.Unmarshal([]byte(inputJSON), &input); err == nil { | |
| 225 | + if q, ok := input["query"]; ok { | |
| 226 | + result.WebSearchQuery = q | |
| 227 | + } | |
| 228 | + } | |
| 229 | + log.Debugf("kiro/websearch: detected web_search tool_use") | |
| 230 | + } | |
| 231 | + currentToolName = "" | |
| 232 | + currentToolIndex = -1 | |
| 233 | + toolInputBuilder.Reset() | |
| 234 | + } | |
| 235 | + } | |
| 236 | + } | |
| 237 | + | |
| 238 | + return result | |
| 239 | +} | |
| 240 | + | |
| 241 | +// FilterChunksForClient processes buffered SSE chunks and removes web_search tool_use | |
| 242 | +// content blocks. This prevents the client from seeing "Tool use" prompts for web_search | |
| 243 | +// when the proxy is handling the search loop internally. | |
| 244 | +// Also suppresses message_start and message_delta/message_stop events since those | |
| 245 | +// are managed by the outer handleWebSearchStream. | |
| 246 | +func FilterChunksForClient(chunks [][]byte, wsToolIndex int, indexOffset int) [][]byte { | |
| 247 | + var filtered [][]byte | |
| 248 | + | |
| 249 | + for _, chunk := range chunks { | |
| 250 | + chunkStr := string(chunk) | |
| 251 | + lines := strings.Split(chunkStr, "\n") | |
| 252 | + | |
| 253 | + var resultBuilder strings.Builder | |
| 254 | + hasContent := false | |
| 255 | + | |
| 256 | + for i := 0; i < len(lines); i++ { | |
| 257 | + line := lines[i] | |
| 258 | + | |
| 259 | + if strings.HasPrefix(line, "data: ") { | |
| 260 | + dataPayload := strings.TrimPrefix(line, "data: ") | |
| 261 | + dataPayload = strings.TrimSpace(dataPayload) | |
| 262 | + | |
| 263 | + if dataPayload == "[DONE]" { | |
| 264 | + // Skip [DONE] — the outer loop manages stream termination | |
| 265 | + continue | |
| 266 | + } | |
| 267 | + | |
| 268 | + var event map[string]interface{} | |
| 269 | + if err := json.Unmarshal([]byte(dataPayload), &event); err != nil { | |
| 270 | + resultBuilder.WriteString(line + "\n") | |
| 271 | + hasContent = true | |
| 272 | + continue | |
| 273 | + } | |
| 274 | + | |
| 275 | + eventType, _ := event["type"].(string) | |
| 276 | + | |
| 277 | + // Skip message_start (outer loop sends its own) | |
| 278 | + if eventType == "message_start" { | |
| 279 | + continue | |
| 280 | + } | |
| 281 | + | |
| 282 | + // Skip message_delta and message_stop (outer loop manages these) | |
| 283 | + if eventType == "message_delta" || eventType == "message_stop" { | |
| 284 | + continue | |
| 285 | + } | |
| 286 | + | |
| 287 | + // Check if this event belongs to the web_search tool_use block | |
| 288 | + if wsToolIndex >= 0 { | |
| 289 | + if idx, ok := event["index"].(float64); ok && int(idx) == wsToolIndex { | |
| 290 | + // Skip events for the web_search tool_use block | |
| 291 | + continue | |
| 292 | + } | |
| 293 | + } | |
| 294 | + | |
| 295 | + // Apply index offset for remaining events | |
| 296 | + if indexOffset > 0 { | |
| 297 | + switch eventType { | |
| 298 | + case "content_block_start", "content_block_delta", "content_block_stop": | |
| 299 | + if idx, ok := event["index"].(float64); ok { | |
| 300 | + event["index"] = int(idx) + indexOffset | |
| 301 | + adjusted, err := json.Marshal(event) | |
| 302 | + if err == nil { | |
| 303 | + resultBuilder.WriteString("data: " + string(adjusted) + "\n") | |
| 304 | + hasContent = true | |
| 305 | + continue | |
| 306 | + } | |
| 307 | + } | |
| 308 | + } | |
| 309 | + } | |
| 310 | + | |
| 311 | + resultBuilder.WriteString(line + "\n") | |
| 312 | + hasContent = true | |
| 313 | + } else if strings.HasPrefix(line, "event: ") { | |
| 314 | + // Check if the next data line will be suppressed | |
| 315 | + if i+1 < len(lines) && strings.HasPrefix(lines[i+1], "data: ") { | |
| 316 | + nextData := strings.TrimPrefix(lines[i+1], "data: ") | |
| 317 | + nextData = strings.TrimSpace(nextData) | |
| 318 | + | |
| 319 | + var nextEvent map[string]interface{} | |
| 320 | + if err := json.Unmarshal([]byte(nextData), &nextEvent); err == nil { | |
| 321 | + nextType, _ := nextEvent["type"].(string) | |
| 322 | + if nextType == "message_start" || nextType == "message_delta" || nextType == "message_stop" { | |
| 323 | + i++ // skip the data line | |
| 324 | + continue | |
| 325 | + } | |
| 326 | + if wsToolIndex >= 0 { | |
| 327 | + if idx, ok := nextEvent["index"].(float64); ok && int(idx) == wsToolIndex { | |
| 328 | + i++ // skip the data line | |
| 329 | + continue | |
| 330 | + } | |
| 331 | + } | |
| 332 | + } | |
| 333 | + } | |
| 334 | + resultBuilder.WriteString(line + "\n") | |
| 335 | + hasContent = true | |
| 336 | + } else { | |
| 337 | + resultBuilder.WriteString(line + "\n") | |
| 338 | + if strings.TrimSpace(line) != "" { | |
| 339 | + hasContent = true | |
| 340 | + } | |
| 341 | + } | |
| 342 | + } | |
| 343 | + | |
| 344 | + if hasContent { | |
| 345 | + filtered = append(filtered, []byte(resultBuilder.String())) | |
| 346 | + } | |
| 347 | + } | |
| 348 | + | |
| 349 | + return filtered | |
| 350 | +} | |
| @@ -1,11 +1,14 @@ | ||
| 1 | 1 | // Package claude provides web search functionality for Kiro translator. |
| 2 | −// This file implements detection and MCP request/response types for web search. | |
| 2 | +// This file implements detection, MCP request/response types, and pure data | |
| 3 | +// transformation utilities for web search. SSE event generation, stream analysis, | |
| 4 | +// and HTTP I/O logic reside in the executor package (kiro_executor.go). | |
| 3 | 5 | package claude |
| 4 | 6 | |
| 5 | 7 | import ( |
| 6 | 8 | "encoding/json" |
| 7 | 9 | "fmt" |
| 8 | 10 | "strings" |
| 11 | + "sync/atomic" | |
| 9 | 12 | "time" |
| 10 | 13 | |
| 11 | 14 | "github.com/google/uuid" |
| @@ -14,6 +17,26 @@ import ( · 2 unmodified lines | ||
| 14 | 17 | "github.com/tidwall/sjson" |
| 15 | 18 | ) |
| 16 | 19 | |
| 20 | +// cachedToolDescription stores the dynamically-fetched web_search tool description. | |
| 21 | +// Written by the executor via SetWebSearchDescription, read by the translator | |
| 22 | +// when building the remote_web_search tool for Kiro API requests. | |
| 23 | +var cachedToolDescription atomic.Value // stores string | |
| 24 | + | |
| 25 | +// GetWebSearchDescription returns the cached web_search tool description, | |
| 26 | +// or empty string if not yet fetched. Lock-free via atomic.Value. | |
| 27 | +func GetWebSearchDescription() string { | |
| 28 | + if v := cachedToolDescription.Load(); v != nil { | |
| 29 | + return v.(string) | |
| 30 | + } | |
| 31 | + return "" | |
| 32 | +} | |
| 33 | + | |
| 34 | +// SetWebSearchDescription stores the dynamically-fetched web_search tool description. | |
| 35 | +// Called by the executor after fetching from MCP tools/list. | |
| 36 | +func SetWebSearchDescription(desc string) { | |
| 37 | + cachedToolDescription.Store(desc) | |
| 38 | +} | |
| 39 | + | |
| 17 | 40 | // McpRequest represents a JSON-RPC 2.0 request to Kiro MCP API |
| 18 | 41 | type McpRequest struct { |
| 19 | 42 | ID string `json:"id"` |
| @@ -191,36 +214,11 @@ func CreateMcpRequest(query string) (string, *McpRequest) { · 171 unmodified lines | ||
| 191 | 214 | return toolUseID, request |
| 192 | 215 | } |
| 193 | 216 | |
| 194 | −// GenerateMessageID generates a Claude-style message ID | |
| 195 | −func GenerateMessageID() string { | |
| 196 | − return "msg_" + strings.ReplaceAll(uuid.New().String(), "-", "")[:24] | |
| 197 | −} | |
| 198 | − | |
| 199 | 217 | // GenerateToolUseID generates a Kiro-style tool use ID (base62-like UUID) |
| 200 | 218 | func GenerateToolUseID() string { |
| 201 | 219 | return strings.ReplaceAll(uuid.New().String(), "-", "")[:22] |
| 202 | 220 | } |
| 203 | 221 | |
| 204 | −// ContainsWebSearchTool checks if the request contains a web_search tool (among any tools). | |
| 205 | −// Unlike HasWebSearchTool, this detects web_search even in mixed-tool arrays. | |
| 206 | −func ContainsWebSearchTool(body []byte) bool { | |
| 207 | − tools := gjson.GetBytes(body, "tools") | |
| 208 | − if !tools.IsArray() { | |
| 209 | − return false | |
| 210 | − } | |
| 211 | − | |
| 212 | − for _, tool := range tools.Array() { | |
| 213 | − name := strings.ToLower(tool.Get("name").String()) | |
| 214 | − toolType := strings.ToLower(tool.Get("type").String()) | |
| 215 | − | |
| 216 | − if isWebSearchTool(name, toolType) { | |
| 217 | − return true | |
| 218 | − } | |
| 219 | − } | |
| 220 | − | |
| 221 | − return false | |
| 222 | −} | |
| 223 | − | |
| 224 | 222 | // ReplaceWebSearchToolDescription replaces the web_search tool description with |
| 225 | 223 | // a minimal version that allows re-search without the restrictive "do not search |
| 226 | 224 | // non-coding topics" instruction from the original Kiro tools/list response. |
| @@ -275,48 +273,6 @@ func ReplaceWebSearchToolDescription(body []byte) ([]byte, error) { · 48 unmodified lines | ||
| 275 | 273 | return result, nil |
| 276 | 274 | } |
| 277 | 275 | |
| 278 | −// StripWebSearchTool removes web_search tool entries from the request's tools array. | |
| 279 | −// If the tools array becomes empty after removal, it is removed entirely. | |
| 280 | −func StripWebSearchTool(body []byte) ([]byte, error) { | |
| 281 | − tools := gjson.GetBytes(body, "tools") | |
| 282 | − if !tools.IsArray() { | |
| 283 | − return body, nil | |
| 284 | − } | |
| 285 | − | |
| 286 | − var filtered []json.RawMessage | |
| 287 | − for _, tool := range tools.Array() { | |
| 288 | − name := strings.ToLower(tool.Get("name").String()) | |
| 289 | − toolType := strings.ToLower(tool.Get("type").String()) | |
| 290 | − | |
| 291 | − if !isWebSearchTool(name, toolType) { | |
| 292 | − filtered = append(filtered, json.RawMessage(tool.Raw)) | |
| 293 | − } | |
| 294 | − } | |
| 295 | − | |
| 296 | − var result []byte | |
| 297 | − var err error | |
| 298 | − | |
| 299 | − if len(filtered) == 0 { | |
| 300 | − // Remove tools array entirely | |
| 301 | − result, err = sjson.DeleteBytes(body, "tools") | |
| 302 | − if err != nil { | |
| 303 | − return body, fmt.Errorf("failed to delete tools: %w", err) | |
| 304 | − } | |
| 305 | − } else { | |
| 306 | − // Replace with filtered array | |
| 307 | − filteredJSON, marshalErr := json.Marshal(filtered) | |
| 308 | − if marshalErr != nil { | |
| 309 | − return body, fmt.Errorf("failed to marshal filtered tools: %w", marshalErr) | |
| 310 | − } | |
| 311 | − result, err = sjson.SetRawBytes(body, "tools", filteredJSON) | |
| 312 | − if err != nil { | |
| 313 | − return body, fmt.Errorf("failed to set filtered tools: %w", err) | |
| 314 | − } | |
| 315 | − } | |
| 316 | − | |
| 317 | − return result, nil | |
| 318 | −} | |
| 319 | − | |
| 320 | 276 | // FormatSearchContextPrompt formats search results as a structured text block |
| 321 | 277 | // for injection into the system prompt. |
| 322 | 278 | func FormatSearchContextPrompt(query string, results *WebSearchResults) string { |
| @@ -365,7 +321,7 @@ func FormatToolResultText(results *WebSearchResults) string { · 42 unmodified lines | ||
| 365 | 321 | // |
| 366 | 322 | // This produces the exact same GAR request format as the Kiro IDE (HAR captures). |
| 367 | 323 | // IMPORTANT: The web_search tool must remain in the "tools" array for this to work. |
| 368 | −// Use ReplaceWebSearchToolDescription (not StripWebSearchTool) to keep the tool available. | |
| 324 | +// Use ReplaceWebSearchToolDescription to keep the tool available with a minimal description. | |
| 369 | 325 | func InjectToolResultsClaude(claudePayload []byte, toolUseId, query string, results *WebSearchResults) ([]byte, error) { |
| 370 | 326 | var payload map[string]interface{} |
| 371 | 327 | if err := json.Unmarshal(claudePayload, &payload); err != nil { |
| @@ -432,8 +388,8 @@ Do NOT apologize for bad results without first attempting a re-search. · 60 unmodified lines | ||
| 432 | 388 | return claudePayload, fmt.Errorf("failed to marshal updated payload: %w", err) |
| 433 | 389 | } |
| 434 | 390 | |
| 435 | − log.Infof("kiro/websearch: injected tool_use+tool_result (toolUseId=%s, query=%s, messages=%d)", | |
| 436 | − toolUseId, query, len(messages)) | |
| 391 | + log.Infof("kiro/websearch: injected tool_use+tool_result (toolUseId=%s, messages=%d)", | |
| 392 | + toolUseId, len(messages)) | |
| 437 | 393 | |
| 438 | 394 | return result, nil |
| 439 | 395 | } |
| @@ -512,658 +468,28 @@ type SearchIndicator struct { · 72 unmodified lines | ||
| 512 | 468 | Results *WebSearchResults |
| 513 | 469 | } |
| 514 | 470 | |
| 515 | −// ══════════════════════════════════════════════════════════════════════════════ | |
| 516 | −// SSE Event Generation | |
| 517 | −// ══════════════════════════════════════════════════════════════════════════════ | |
| 518 | − | |
| 519 | −// SseEvent represents a Server-Sent Event | |
| 520 | −type SseEvent struct { | |
| 521 | − Event string | |
| 522 | − Data interface{} | |
| 523 | −} | |
| 524 | − | |
| 525 | −// ToSSEString converts the event to SSE wire format | |
| 526 | −func (e *SseEvent) ToSSEString() string { | |
| 527 | − dataBytes, _ := json.Marshal(e.Data) | |
| 528 | − return fmt.Sprintf("event: %s\ndata: %s\n\n", e.Event, string(dataBytes)) | |
| 471 | +// BuildMcpEndpoint constructs the MCP endpoint URL for the given AWS region. | |
| 472 | +// Centralizes the URL pattern used by both handleWebSearch and handleWebSearchStream. | |
| 473 | +func BuildMcpEndpoint(region string) string { | |
| 474 | + return fmt.Sprintf("https://q.%s.amazonaws.com/mcp", region) | |
| 529 | 475 | } |
| 530 | 476 | |
| 531 | −// GenerateWebSearchEvents generates the 11-event SSE sequence for web search. | |
| 532 | −// Events: message_start, content_block_start(server_tool_use), content_block_delta(input_json), | |
| 533 | −// content_block_stop, content_block_start(web_search_tool_result), content_block_stop, | |
| 534 | −// content_block_start(text), content_block_delta(text), content_block_stop, message_delta, message_stop | |
| 535 | −func GenerateWebSearchEvents( | |
| 536 | − model string, | |
| 537 | − query string, | |
| 538 | − toolUseID string, | |
| 539 | − searchResults *WebSearchResults, | |
| 540 | − inputTokens int, | |
| 541 | −) []SseEvent { | |
| 542 | − events := make([]SseEvent, 0, 15) | |
| 543 | − messageID := GenerateMessageID() | |
| 544 | − | |
| 545 | − // 1. message_start | |
| 546 | − events = append(events, SseEvent{ | |
| 547 | − Event: "message_start", | |
| 548 | − Data: map[string]interface{}{ | |
| 549 | − "type": "message_start", | |
| 550 | − "message": map[string]interface{}{ | |
| 551 | − "id": messageID, | |
| 552 | − "type": "message", | |
| 553 | − "role": "assistant", | |
| 554 | − "model": model, | |
| 555 | − "content": []interface{}{}, | |
| 556 | − "stop_reason": nil, | |
| 557 | − "stop_sequence": nil, | |
| 558 | − "usage": map[string]interface{}{ | |
| 559 | − "input_tokens": inputTokens, | |
| 560 | − "output_tokens": 0, | |
| 561 | − "cache_creation_input_tokens": 0, | |
| 562 | − "cache_read_input_tokens": 0, | |
| 563 | − }, | |
| 564 | − }, | |
| 565 | − }, | |
| 566 | − }) | |
| 567 | − | |
| 568 | − // 2. content_block_start (server_tool_use) | |
| 569 | − events = append(events, SseEvent{ | |
| 570 | − Event: "content_block_start", | |
| 571 | − Data: map[string]interface{}{ | |
| 572 | − "type": "content_block_start", | |
| 573 | − "index": 0, | |
| 574 | − "content_block": map[string]interface{}{ | |
| 575 | − "id": toolUseID, | |
| 576 | − "type": "server_tool_use", | |
| 577 | − "name": "web_search", | |
| 578 | − "input": map[string]interface{}{}, | |
| 579 | − }, | |
| 580 | − }, | |
| 581 | − }) | |
| 582 | − | |
| 583 | − // 3. content_block_delta (input_json_delta) | |
| 584 | − inputJSON, _ := json.Marshal(map[string]string{"query": query}) | |
| 585 | − events = append(events, SseEvent{ | |
| 586 | − Event: "content_block_delta", | |
| 587 | − Data: map[string]interface{}{ | |
| 588 | − "type": "content_block_delta", | |
| 589 | − "index": 0, | |
| 590 | − "delta": map[string]interface{}{ | |
| 591 | − "type": "input_json_delta", | |
| 592 | − "partial_json": string(inputJSON), | |
| 593 | − }, | |
| 594 | − }, | |
| 595 | − }) | |
| 596 | − | |
| 597 | − // 4. content_block_stop (server_tool_use) | |
| 598 | − events = append(events, SseEvent{ | |
| 599 | − Event: "content_block_stop", | |
| 600 | − Data: map[string]interface{}{ | |
| 601 | − "type": "content_block_stop", | |
| 602 | − "index": 0, | |
| 603 | − }, | |
| 604 | − }) | |
| 605 | − | |
| 606 | − // 5. content_block_start (web_search_tool_result) | |
| 607 | − searchContent := make([]map[string]interface{}, 0) | |
| 608 | − if searchResults != nil { | |
| 609 | − for _, r := range searchResults.Results { | |
| 610 | − snippet := "" | |
| 611 | − if r.Snippet != nil { | |
| 612 | − snippet = *r.Snippet | |
| 613 | − } | |
| 614 | − searchContent = append(searchContent, map[string]interface{}{ | |
| 615 | − "type": "web_search_result", | |
| 616 | − "title": r.Title, | |
| 617 | − "url": r.URL, | |
| 618 | − "encrypted_content": snippet, | |
| 619 | − "page_age": nil, | |
| 620 | − }) | |
| 621 | − } | |
| 622 | − } | |
| 623 | − events = append(events, SseEvent{ | |
| 624 | − Event: "content_block_start", | |
| 625 | − Data: map[string]interface{}{ | |
| 626 | − "type": "content_block_start", | |
| 627 | − "index": 1, | |
| 628 | − "content_block": map[string]interface{}{ | |
| 629 | − "type": "web_search_tool_result", | |
| 630 | − "tool_use_id": toolUseID, | |
| 631 | − "content": searchContent, | |
| 632 | − }, | |
| 633 | − }, | |
| 634 | − }) | |
| 635 | − | |
| 636 | − // 6. content_block_stop (web_search_tool_result) | |
| 637 | − events = append(events, SseEvent{ | |
| 638 | − Event: "content_block_stop", | |
| 639 | − Data: map[string]interface{}{ | |
| 640 | − "type": "content_block_stop", | |
| 641 | − "index": 1, | |
| 642 | − }, | |
| 643 | − }) | |
| 644 | − | |
| 645 | − // 7. content_block_start (text) | |
| 646 | − events = append(events, SseEvent{ | |
| 647 | − Event: "content_block_start", | |
| 648 | − Data: map[string]interface{}{ | |
| 649 | − "type": "content_block_start", | |
| 650 | − "index": 2, | |
| 651 | − "content_block": map[string]interface{}{ | |
| 652 | − "type": "text", | |
| 653 | − "text": "", | |
| 654 | − }, | |
| 655 | − }, | |
| 656 | − }) | |
| 657 | − | |
| 658 | − // 8. content_block_delta (text_delta) - generate search summary | |
| 659 | − summary := generateSearchSummary(query, searchResults) | |
| 660 | − | |
| 661 | − // Split text into chunks for streaming effect | |
| 662 | − chunkSize := 100 | |
| 663 | − runes := []rune(summary) | |
| 664 | − for i := 0; i < len(runes); i += chunkSize { | |
| 665 | − end := i + chunkSize | |
| 666 | − if end > len(runes) { | |
| 667 | − end = len(runes) | |
| 668 | − } | |
| 669 | − chunk := string(runes[i:end]) | |
| 670 | − events = append(events, SseEvent{ | |
| 671 | − Event: "content_block_delta", | |
| 672 | − Data: map[string]interface{}{ | |
| 673 | − "type": "content_block_delta", | |
| 674 | − "index": 2, | |
| 675 | − "delta": map[string]interface{}{ | |
| 676 | − "type": "text_delta", | |
| 677 | − "text": chunk, | |
| 678 | − }, | |
| 679 | − }, | |
| 680 | − }) | |
| 477 | +// ParseSearchResults extracts WebSearchResults from MCP response | |
| 478 | +func ParseSearchResults(response *McpResponse) *WebSearchResults { | |
| 479 | + if response == nil || response.Result == nil || len(response.Result.Content) == 0 { | |
| 480 | + return nil | |
| 681 | 481 | } |
| 682 | 482 | |
| 683 | − // 9. content_block_stop (text) | |
| 684 | − events = append(events, SseEvent{ | |
| 685 | − Event: "content_block_stop", | |
| 686 | − Data: map[string]interface{}{ | |
| 687 | − "type": "content_block_stop", | |
| 688 | − "index": 2, | |
| 689 | − }, | |
| 690 | − }) | |
| 691 | − | |
| 692 | − // 10. message_delta | |
| 693 | − outputTokens := (len(summary) + 3) / 4 // Simple estimation | |
| 694 | − events = append(events, SseEvent{ | |
| 695 | − Event: "message_delta", | |
| 696 | − Data: map[string]interface{}{ | |
| 697 | − "type": "message_delta", | |
| 698 | − "delta": map[string]interface{}{ | |
| 699 | − "stop_reason": "end_turn", | |
| 700 | − "stop_sequence": nil, | |
| 701 | − }, | |
| 702 | − "usage": map[string]interface{}{ | |
| 703 | − "output_tokens": outputTokens, | |
| 704 | − }, | |
| 705 | − }, | |
| 706 | − }) | |
| 707 | − | |
| 708 | − // 11. message_stop | |
| 709 | − events = append(events, SseEvent{ | |
| 710 | − Event: "message_stop", | |
| 711 | − Data: map[string]interface{}{ | |
| 712 | − "type": "message_stop", | |
| 713 | − }, | |
| 714 | − }) | |
| 715 | − | |
| 716 | − return events | |
| 717 | −} | |
| 718 | − | |
| 719 | −// generateSearchSummary generates a text summary of search results | |
| 720 | −func generateSearchSummary(query string, results *WebSearchResults) string { | |
| 721 | − var sb strings.Builder | |
| 722 | − sb.WriteString(fmt.Sprintf("Here are the search results for \"%s\":\n\n", query)) | |
| 723 | − | |
| 724 | − if results != nil && len(results.Results) > 0 { | |
| 725 | − for i, r := range results.Results { | |
| 726 | − sb.WriteString(fmt.Sprintf("%d. **%s**\n", i+1, r.Title)) | |
| 727 | − if r.Snippet != nil { | |
| 728 | − snippet := *r.Snippet | |
| 729 | − if len(snippet) > 200 { | |
| 730 | − snippet = snippet[:200] + "..." | |
| 731 | − } | |
| 732 | − sb.WriteString(fmt.Sprintf(" %s\n", snippet)) | |
| 733 | − } | |
| 734 | − sb.WriteString(fmt.Sprintf(" Source: %s\n\n", r.URL)) | |
| 735 | − } | |
| 736 | − } else { | |
| 737 | − sb.WriteString("No results found.\n") | |
| 483 | + content := response.Result.Content[0] | |
| 484 | + if content.ContentType != "text" { | |
| 485 | + return nil | |
| 738 | 486 | } |
| 739 | 487 | |
| 740 | − sb.WriteString("\nPlease note that these are web search results and may not be fully accurate or up-to-date.") | |
| 741 | − | |
| 742 | − return sb.String() | |
| 743 | −} | |
| 744 | − | |
| 745 | −// GenerateSearchIndicatorEvents generates ONLY the search indicator SSE events | |
| 746 | −// (server_tool_use + web_search_tool_result) without text summary or message termination. | |
| 747 | −// These events trigger Claude Code's search indicator UI. | |
| 748 | −// The caller is responsible for sending message_start before and message_delta/stop after. | |
| 749 | −func GenerateSearchIndicatorEvents( | |
| 750 | − query string, | |
| 751 | − toolUseID string, | |
| 752 | − searchResults *WebSearchResults, | |
| 753 | − startIndex int, | |
| 754 | −) []SseEvent { | |
| 755 | − events := make([]SseEvent, 0, 4) | |
| 756 | − | |
| 757 | − // 1. content_block_start (server_tool_use) | |
| 758 | − events = append(events, SseEvent{ | |
| 759 | − Event: "content_block_start", | |
| 760 | − Data: map[string]interface{}{ | |
| 761 | − "type": "content_block_start", | |
| 762 | − "index": startIndex, | |
| 763 | − "content_block": map[string]interface{}{ | |
| 764 | − "id": toolUseID, | |
| 765 | − "type": "server_tool_use", | |
| 766 | − "name": "web_search", | |
| 767 | − "input": map[string]interface{}{}, | |
| 768 | − }, | |
| 769 | − }, | |
| 770 | − }) | |
| 771 | − | |
| 772 | − // 2. content_block_delta (input_json_delta) | |
| 773 | − inputJSON, _ := json.Marshal(map[string]string{"query": query}) | |
| 774 | − events = append(events, SseEvent{ | |
| 775 | − Event: "content_block_delta", | |
| 776 | − Data: map[string]interface{}{ | |
| 777 | − "type": "content_block_delta", | |
| 778 | − "index": startIndex, | |
| 779 | − "delta": map[string]interface{}{ | |
| 780 | − "type": "input_json_delta", | |
| 781 | − "partial_json": string(inputJSON), | |
| 782 | − }, | |
| 783 | − }, | |
| 784 | − }) | |
| 785 | − | |
| 786 | − // 3. content_block_stop (server_tool_use) | |
| 787 | − events = append(events, SseEvent{ | |
| 788 | − Event: "content_block_stop", | |
| 789 | − Data: map[string]interface{}{ | |
| 790 | − "type": "content_block_stop", | |
| 791 | − "index": startIndex, | |
| 792 | − }, | |
| 793 | − }) | |
| 794 | − | |
| 795 | − // 4. content_block_start (web_search_tool_result) | |
| 796 | − searchContent := make([]map[string]interface{}, 0) | |
| 797 | − if searchResults != nil { | |
| 798 | − for _, r := range searchResults.Results { | |
| 799 | − snippet := "" | |
| 800 | − if r.Snippet != nil { | |
| 801 | − snippet = *r.Snippet | |
| 802 | − } | |
| 803 | − searchContent = append(searchContent, map[string]interface{}{ | |
| 804 | − "type": "web_search_result", | |
| 805 | − "title": r.Title, | |
| 806 | − "url": r.URL, | |
| 807 | − "encrypted_content": snippet, | |
| 808 | − "page_age": nil, | |
| 809 | − }) | |
| 810 | − } | |
| 811 | − } | |
| 812 | − events = append(events, SseEvent{ | |
| 813 | − Event: "content_block_start", | |
| 814 | − Data: map[string]interface{}{ | |
| 815 | − "type": "content_block_start", | |
| 816 | − "index": startIndex + 1, | |
| 817 | − "content_block": map[string]interface{}{ | |
| 818 | − "type": "web_search_tool_result", | |
| 819 | − "tool_use_id": toolUseID, | |
| 820 | − "content": searchContent, | |
| 821 | − }, | |
| 822 | − }, | |
| 823 | − }) | |
| 824 | − | |
| 825 | − // 5. content_block_stop (web_search_tool_result) | |
| 826 | − events = append(events, SseEvent{ | |
| 827 | − Event: "content_block_stop", | |
| 828 | − Data: map[string]interface{}{ | |
| 829 | − "type": "content_block_stop", | |
| 830 | − "index": startIndex + 1, | |
| 831 | − }, | |
| 832 | − }) | |
| 833 | − | |
| 834 | − return events | |
| 835 | −} | |
| 836 | − | |
| 837 | −// ══════════════════════════════════════════════════════════════════════════════ | |
| 838 | −// Stream Analysis & Manipulation | |
| 839 | −// ══════════════════════════════════════════════════════════════════════════════ | |
| 840 | − | |
| 841 | −// AdjustStreamIndices adjusts content block indices in SSE event data by adding an offset. | |
| 842 | −// It also suppresses duplicate message_start events (returns shouldForward=false). | |
| 843 | −// This is used to combine search indicator events (indices 0,1) with Kiro model response events. | |
| 844 | −// | |
| 845 | −// The data parameter is a single SSE "data:" line payload (JSON). | |
| 846 | −// Returns: adjusted data, shouldForward (false = skip this event). | |
| 847 | −func AdjustStreamIndices(data []byte, offset int) ([]byte, bool) { | |
| 848 | − if len(data) == 0 { | |
| 849 | − return data, true | |
| 850 | − } | |
| 851 | − | |
| 852 | − // Quick check: parse the JSON | |
| 853 | − var event map[string]interface{} | |
| 854 | − if err := json.Unmarshal(data, &event); err != nil { | |
| 855 | − // Not valid JSON, pass through | |
| 856 | − return data, true | |
| 857 | − } | |
| 858 | − | |
| 859 | − eventType, _ := event["type"].(string) | |
| 860 | − | |
| 861 | − // Suppress duplicate message_start events | |
| 862 | − if eventType == "message_start" { | |
| 863 | − return data, false | |
| 864 | − } | |
| 865 | − | |
| 866 | − // Adjust index for content_block events | |
| 867 | − switch eventType { | |
| 868 | − case "content_block_start", "content_block_delta", "content_block_stop": | |
| 869 | − if idx, ok := event["index"].(float64); ok { | |
| 870 | − event["index"] = int(idx) + offset | |
| 871 | − adjusted, err := json.Marshal(event) | |
| 872 | − if err != nil { | |
| 873 | − return data, true | |
| 874 | − } | |
| 875 | − return adjusted, true | |
| 876 | − } | |
| 877 | − } | |
| 878 | − | |
| 879 | − // Pass through all other events unchanged (message_delta, message_stop, ping, etc.) | |
| 880 | − return data, true | |
| 881 | −} | |
| 882 | − | |
| 883 | −// AdjustSSEChunk processes a raw SSE chunk (potentially containing multiple "event:/data:" pairs) | |
| 884 | −// and adjusts content block indices. Suppresses duplicate message_start events. | |
| 885 | −// Returns the adjusted chunk and whether it should be forwarded. | |
| 886 | −func AdjustSSEChunk(chunk []byte, offset int) ([]byte, bool) { | |
| 887 | − chunkStr := string(chunk) | |
| 888 | − | |
| 889 | − // Fast path: if no "data:" prefix, pass through | |
| 890 | − if !strings.Contains(chunkStr, "data: ") { | |
| 891 | − return chunk, true | |
| 892 | − } | |
| 893 | − | |
| 894 | − var result strings.Builder | |
| 895 | − hasContent := false | |
| 896 | − | |
| 897 | − lines := strings.Split(chunkStr, "\n") | |
| 898 | − for i := 0; i < len(lines); i++ { | |
| 899 | − line := lines[i] | |
| 900 | − | |
| 901 | − if strings.HasPrefix(line, "data: ") { | |
| 902 | − dataPayload := strings.TrimPrefix(line, "data: ") | |
| 903 | − dataPayload = strings.TrimSpace(dataPayload) | |
| 904 | − | |
| 905 | − if dataPayload == "[DONE]" { | |
| 906 | − result.WriteString(line + "\n") | |
| 907 | − hasContent = true | |
| 908 | − continue | |
| 909 | − } | |
| 910 | − | |
| 911 | − adjusted, shouldForward := AdjustStreamIndices([]byte(dataPayload), offset) | |
| 912 | − if !shouldForward { | |
| 913 | − // Skip this event and its preceding "event:" line | |
| 914 | − // Also skip the trailing empty line | |
| 915 | − continue | |
| 916 | − } | |
| 917 | − | |
| 918 | − result.WriteString("data: " + string(adjusted) + "\n") | |
| 919 | − hasContent = true | |
| 920 | − } else if strings.HasPrefix(line, "event: ") { | |
| 921 | − // Check if the next data line will be suppressed | |
| 922 | − if i+1 < len(lines) && strings.HasPrefix(lines[i+1], "data: ") { | |
| 923 | − dataPayload := strings.TrimPrefix(lines[i+1], "data: ") | |
| 924 | − dataPayload = strings.TrimSpace(dataPayload) | |
| 925 | − | |
| 926 | − var event map[string]interface{} | |
| 927 | − if err := json.Unmarshal([]byte(dataPayload), &event); err == nil { | |
| 928 | − if eventType, ok := event["type"].(string); ok && eventType == "message_start" { | |
| 929 | − // Skip both the event: and data: lines | |
| 930 | − i++ // skip the data: line too | |
| 931 | − continue | |
| 932 | − } | |
| 933 | − } | |
| 934 | − } | |
| 935 | − result.WriteString(line + "\n") | |
| 936 | − hasContent = true | |
| 937 | − } else { | |
| 938 | − result.WriteString(line + "\n") | |
| 939 | − if strings.TrimSpace(line) != "" { | |
| 940 | − hasContent = true | |
| 941 | − } | |
| 942 | − } | |
| 943 | − } | |
| 944 | − | |
| 945 | − if !hasContent { | |
| 946 | − return nil, false | |
| 947 | − } | |
| 948 | − | |
| 949 | − return []byte(result.String()), true | |
| 950 | −} | |
| 951 | − | |
| 952 | −// BufferedStreamResult contains the analysis of buffered SSE chunks from a Kiro API response. | |
| 953 | −type BufferedStreamResult struct { | |
| 954 | − // StopReason is the detected stop_reason from the stream (e.g., "end_turn", "tool_use") | |
| 955 | − StopReason string | |
| 956 | − // WebSearchQuery is the extracted query if the model requested another web_search | |
| 957 | − WebSearchQuery string | |
| 958 | − // WebSearchToolUseId is the tool_use ID from the model's response (needed for toolResults) | |
| 959 | − WebSearchToolUseId string | |
| 960 | − // HasWebSearchToolUse indicates whether the model requested web_search | |
| 961 | − HasWebSearchToolUse bool | |
| 962 | − // WebSearchToolUseIndex is the content_block index of the web_search tool_use | |
| 963 | − WebSearchToolUseIndex int | |
| 964 | −} | |
| 965 | − | |
| 966 | −// AnalyzeBufferedStream scans buffered SSE chunks to detect stop_reason and web_search tool_use. | |
| 967 | −// This is used in the search loop to determine if the model wants another search round. | |
| 968 | −func AnalyzeBufferedStream(chunks [][]byte) BufferedStreamResult { | |
| 969 | − result := BufferedStreamResult{WebSearchToolUseIndex: -1} | |
| 970 | − | |
| 971 | − // Track tool use state across chunks | |
| 972 | − var currentToolName string | |
| 973 | − var currentToolIndex int = -1 | |
| 974 | − var toolInputBuilder strings.Builder | |
| 975 | − | |
| 976 | − for _, chunk := range chunks { | |
| 977 | − chunkStr := string(chunk) | |
| 978 | − lines := strings.Split(chunkStr, "\n") | |
| 979 | − for _, line := range lines { | |
| 980 | − if !strings.HasPrefix(line, "data: ") { | |
| 981 | − continue | |
| 982 | − } | |
| 983 | − dataPayload := strings.TrimPrefix(line, "data: ") | |
| 984 | − dataPayload = strings.TrimSpace(dataPayload) | |
| 985 | − if dataPayload == "[DONE]" || dataPayload == "" { | |
| 986 | − continue | |
| 987 | − } | |
| 988 | − | |
| 989 | − var event map[string]interface{} | |
| 990 | − if err := json.Unmarshal([]byte(dataPayload), &event); err != nil { | |
| 991 | − continue | |
| 992 | − } | |
| 993 | − | |
| 994 | − eventType, _ := event["type"].(string) | |
| 995 | − | |
| 996 | − switch eventType { | |
| 997 | − case "message_delta": | |
| 998 | − // Extract stop_reason from message_delta | |
| 999 | − if delta, ok := event["delta"].(map[string]interface{}); ok { | |
| 1000 | − if sr, ok := delta["stop_reason"].(string); ok && sr != "" { | |
| 1001 | − result.StopReason = sr | |
| 1002 | − } | |
| 1003 | − } | |
| 1004 | − | |
| 1005 | − case "content_block_start": | |
| 1006 | − // Detect tool_use content blocks | |
| 1007 | − if cb, ok := event["content_block"].(map[string]interface{}); ok { | |
| 1008 | − if cbType, ok := cb["type"].(string); ok && cbType == "tool_use" { | |
| 1009 | − if name, ok := cb["name"].(string); ok { | |
| 1010 | − currentToolName = strings.ToLower(name) | |
| 1011 | − if idx, ok := event["index"].(float64); ok { | |
| 1012 | − currentToolIndex = int(idx) | |
| 1013 | − } | |
| 1014 | − // Capture tool use ID for toolResults handshake | |
| 1015 | − if id, ok := cb["id"].(string); ok { | |
| 1016 | − result.WebSearchToolUseId = id | |
| 1017 | − } | |
| 1018 | − toolInputBuilder.Reset() | |
| 1019 | − } | |
| 1020 | − } | |
| 1021 | − } | |
| 1022 | − | |
| 1023 | − case "content_block_delta": | |
| 1024 | − // Accumulate tool input JSON | |
| 1025 | − if currentToolName != "" { | |
| 1026 | − if delta, ok := event["delta"].(map[string]interface{}); ok { | |
| 1027 | − if deltaType, ok := delta["type"].(string); ok && deltaType == "input_json_delta" { | |
| 1028 | − if partial, ok := delta["partial_json"].(string); ok { | |
| 1029 | − toolInputBuilder.WriteString(partial) | |
| 1030 | − } | |
| 1031 | − } | |
| 1032 | − } | |
| 1033 | − } | |
| 1034 | − | |
| 1035 | − case "content_block_stop": | |
| 1036 | − // Finalize tool use detection | |
| 1037 | − if currentToolName == "web_search" || currentToolName == "websearch" || currentToolName == "remote_web_search" { | |
| 1038 | − result.HasWebSearchToolUse = true | |
| 1039 | − result.WebSearchToolUseIndex = currentToolIndex | |
| 1040 | − // Extract query from accumulated input JSON | |
| 1041 | − inputJSON := toolInputBuilder.String() | |
| 1042 | − var input map[string]string | |
| 1043 | − if err := json.Unmarshal([]byte(inputJSON), &input); err == nil { | |
| 1044 | − if q, ok := input["query"]; ok { | |
| 1045 | − result.WebSearchQuery = q | |
| 1046 | − } | |
| 1047 | − } | |
| 1048 | − log.Debugf("kiro/websearch: detected web_search tool_use, query: %s", result.WebSearchQuery) | |
| 1049 | − } | |
| 1050 | − currentToolName = "" | |
| 1051 | − currentToolIndex = -1 | |
| 1052 | − toolInputBuilder.Reset() | |
| 1053 | − } | |
| 1054 | − } | |
| 1055 | − } | |
| 1056 | − | |
| 1057 | − return result | |
| 1058 | −} | |
| 1059 | − | |
| 1060 | −// FilterChunksForClient processes buffered SSE chunks and removes web_search tool_use | |
| 1061 | −// content blocks. This prevents the client from seeing "Tool use" prompts for web_search | |
| 1062 | −// when the proxy is handling the search loop internally. | |
| 1063 | −// Also suppresses message_start and message_delta/message_stop events since those | |
| 1064 | −// are managed by the outer handleWebSearchStream. | |
| 1065 | −func FilterChunksForClient(chunks [][]byte, wsToolIndex int, indexOffset int) [][]byte { | |
| 1066 | − var filtered [][]byte | |
| 1067 | − | |
| 1068 | − for _, chunk := range chunks { | |
| 1069 | − chunkStr := string(chunk) | |
| 1070 | − lines := strings.Split(chunkStr, "\n") | |
| 1071 | − | |
| 1072 | − var resultBuilder strings.Builder | |
| 1073 | − hasContent := false | |
| 1074 | − | |
| 1075 | − for i := 0; i < len(lines); i++ { | |
| 1076 | − line := lines[i] | |
| 1077 | − | |
| 1078 | − if strings.HasPrefix(line, "data: ") { | |
| 1079 | − dataPayload := strings.TrimPrefix(line, "data: ") | |
| 1080 | − dataPayload = strings.TrimSpace(dataPayload) | |
| 1081 | − | |
| 1082 | − if dataPayload == "[DONE]" { | |
| 1083 | − // Skip [DONE] — the outer loop manages stream termination | |
| 1084 | − continue | |
| 1085 | − } | |
| 1086 | − | |
| 1087 | − var event map[string]interface{} | |
| 1088 | − if err := json.Unmarshal([]byte(dataPayload), &event); err != nil { | |
| 1089 | − resultBuilder.WriteString(line + "\n") | |
| 1090 | − hasContent = true | |
| 1091 | − continue | |
| 1092 | − } | |
| 1093 | − | |
| 1094 | − eventType, _ := event["type"].(string) | |
| 1095 | − | |
| 1096 | − // Skip message_start (outer loop sends its own) | |
| 1097 | − if eventType == "message_start" { | |
| 1098 | − continue | |
| 1099 | − } | |
| 1100 | − | |
| 1101 | − // Skip message_delta and message_stop (outer loop manages these) | |
| 1102 | − if eventType == "message_delta" || eventType == "message_stop" { | |
| 1103 | − continue | |
| 1104 | − } | |
| 1105 | − | |
| 1106 | − // Check if this event belongs to the web_search tool_use block | |
| 1107 | − if wsToolIndex >= 0 { | |
| 1108 | − if idx, ok := event["index"].(float64); ok && int(idx) == wsToolIndex { | |
| 1109 | − // Skip events for the web_search tool_use block | |
| 1110 | − continue | |
| 1111 | − } | |
| 1112 | − } | |
| 1113 | − | |
| 1114 | − // Apply index offset for remaining events | |
| 1115 | − if indexOffset > 0 { | |
| 1116 | − switch eventType { | |
| 1117 | − case "content_block_start", "content_block_delta", "content_block_stop": | |
| 1118 | − if idx, ok := event["index"].(float64); ok { | |
| 1119 | − event["index"] = int(idx) + indexOffset | |
| 1120 | − adjusted, err := json.Marshal(event) | |
| 1121 | − if err == nil { | |
| 1122 | − resultBuilder.WriteString("data: " + string(adjusted) + "\n") | |
| 1123 | − hasContent = true | |
| 1124 | − continue | |
| 1125 | − } | |
| 1126 | − } | |
| 1127 | − } | |
| 1128 | − } | |
| 1129 | − | |
| 1130 | − resultBuilder.WriteString(line + "\n") | |
| 1131 | − hasContent = true | |
| 1132 | − } else if strings.HasPrefix(line, "event: ") { | |
| 1133 | − // Check if the next data line will be suppressed | |
| 1134 | − if i+1 < len(lines) && strings.HasPrefix(lines[i+1], "data: ") { | |
| 1135 | − nextData := strings.TrimPrefix(lines[i+1], "data: ") | |
| 1136 | − nextData = strings.TrimSpace(nextData) | |
| 1137 | − | |
| 1138 | − var nextEvent map[string]interface{} | |
| 1139 | − if err := json.Unmarshal([]byte(nextData), &nextEvent); err == nil { | |
| 1140 | − nextType, _ := nextEvent["type"].(string) | |
| 1141 | − if nextType == "message_start" || nextType == "message_delta" || nextType == "message_stop" { | |
| 1142 | − i++ // skip the data line | |
| 1143 | − continue | |
| 1144 | − } | |
| 1145 | − if wsToolIndex >= 0 { | |
| 1146 | − if idx, ok := nextEvent["index"].(float64); ok && int(idx) == wsToolIndex { | |
| 1147 | − i++ // skip the data line | |
| 1148 | − continue | |
| 1149 | − } | |
| 1150 | − } | |
| 1151 | − } | |
| 1152 | − } | |
| 1153 | − resultBuilder.WriteString(line + "\n") | |
| 1154 | − hasContent = true | |
| 1155 | − } else { | |
| 1156 | − resultBuilder.WriteString(line + "\n") | |
| 1157 | − if strings.TrimSpace(line) != "" { | |
| 1158 | − hasContent = true | |
| 1159 | − } | |
| 1160 | − } | |
| 1161 | − } | |
| 1162 | − | |
| 1163 | − if hasContent { | |
| 1164 | − filtered = append(filtered, []byte(resultBuilder.String())) | |
| 1165 | − } | |
| 488 | + var results WebSearchResults | |
| 489 | + if err := json.Unmarshal([]byte(content.Text), &results); err != nil { | |
| 490 | + log.Warnf("kiro/websearch: failed to parse search results: %v", err) | |
| 491 | + return nil | |
| 1166 | 492 | } |
| 1167 | 493 | |
| 1168 | − return filtered | |
| 494 | + return &results | |
| 1169 | 495 | } |
| @@ -1,270 +0,0 @@ | ||
| 1 | −// Package claude provides web search handler for Kiro translator. | |
| 2 | −// This file implements the MCP API call and response handling. | |
| 3 | −package claude | |
| 4 | − | |
| 5 | −import ( | |
| 6 | − "bytes" | |
| 7 | − "encoding/json" | |
| 8 | − "fmt" | |
| 9 | − "io" | |
| 10 | − "net/http" | |
| 11 | − "sync" | |
| 12 | − "sync/atomic" | |
| 13 | − "time" | |
| 14 | − | |
| 15 | − "github.com/google/uuid" | |
| 16 | − kiroauth "github.com/router-for-me/CLIProxyAPI/v6/internal/auth/kiro" | |
| 17 | − "github.com/router-for-me/CLIProxyAPI/v6/internal/util" | |
| 18 | − log "github.com/sirupsen/logrus" | |
| 19 | −) | |
| 20 | − | |
| 21 | −// Cached web_search tool description fetched from MCP tools/list. | |
| 22 | −// Uses atomic.Pointer[sync.Once] for lock-free reads with retry-on-failure: | |
| 23 | −// - sync.Once prevents race conditions and deduplicates concurrent calls | |
| 24 | −// - On failure, a fresh sync.Once is swapped in to allow retry on next call | |
| 25 | −// - On success, sync.Once stays "done" forever — zero overhead for subsequent calls | |
| 26 | −var ( | |
| 27 | − cachedToolDescription atomic.Value // stores string | |
| 28 | − toolDescOnce atomic.Pointer[sync.Once] | |
| 29 | − fallbackFpOnce sync.Once | |
| 30 | − fallbackFp *kiroauth.Fingerprint | |
| 31 | −) | |
| 32 | − | |
| 33 | −func init() { | |
| 34 | − toolDescOnce.Store(&sync.Once{}) | |
| 35 | −} | |
| 36 | − | |
| 37 | −// FetchToolDescription calls MCP tools/list to get the web_search tool description | |
| 38 | −// and caches it. Safe to call concurrently — only one goroutine fetches at a time. | |
| 39 | −// If the fetch fails, subsequent calls will retry. On success, no further fetches occur. | |
| 40 | −// The httpClient parameter allows reusing a shared pooled HTTP client. | |
| 41 | −func FetchToolDescription(mcpEndpoint, authToken string, httpClient *http.Client, fp *kiroauth.Fingerprint, authAttrs map[string]string) { | |
| 42 | − toolDescOnce.Load().Do(func() { | |
| 43 | − handler := NewWebSearchHandler(mcpEndpoint, authToken, httpClient, fp, authAttrs) | |
| 44 | − reqBody := []byte(`{"id":"tools_list","jsonrpc":"2.0","method":"tools/list"}`) | |
| 45 | − log.Debugf("kiro/websearch MCP tools/list request: %d bytes", len(reqBody)) | |
| 46 | − | |
| 47 | − req, err := http.NewRequest("POST", mcpEndpoint, bytes.NewReader(reqBody)) | |
| 48 | − if err != nil { | |
| 49 | − log.Warnf("kiro/websearch: failed to create tools/list request: %v", err) | |
| 50 | − toolDescOnce.Store(&sync.Once{}) // allow retry | |
| 51 | − return | |
| 52 | − } | |
| 53 | − | |
| 54 | − // Reuse same headers as CallMcpAPI | |
| 55 | − handler.setMcpHeaders(req) | |
| 56 | − | |
| 57 | − resp, err := handler.HTTPClient.Do(req) | |
| 58 | − if err != nil { | |
| 59 | − log.Warnf("kiro/websearch: tools/list request failed: %v", err) | |
| 60 | − toolDescOnce.Store(&sync.Once{}) // allow retry | |
| 61 | − return | |
| 62 | − } | |
| 63 | − defer resp.Body.Close() | |
| 64 | − | |
| 65 | − body, err := io.ReadAll(resp.Body) | |
| 66 | − if err != nil || resp.StatusCode != http.StatusOK { | |
| 67 | − log.Warnf("kiro/websearch: tools/list returned status %d", resp.StatusCode) | |
| 68 | − toolDescOnce.Store(&sync.Once{}) // allow retry | |
| 69 | − return | |
| 70 | − } | |
| 71 | − log.Debugf("kiro/websearch MCP tools/list response: [%d] %d bytes", resp.StatusCode, len(body)) | |
| 72 | − | |
| 73 | − // Parse: {"result":{"tools":[{"name":"web_search","description":"..."}]}} | |
| 74 | − var result struct { | |
| 75 | − Result *struct { | |
| 76 | − Tools []struct { | |
| 77 | − Name string `json:"name"` | |
| 78 | − Description string `json:"description"` | |
| 79 | − } `json:"tools"` | |
| 80 | − } `json:"result"` | |
| 81 | − } | |
| 82 | − if err := json.Unmarshal(body, &result); err != nil || result.Result == nil { | |
| 83 | − log.Warnf("kiro/websearch: failed to parse tools/list response") | |
| 84 | − toolDescOnce.Store(&sync.Once{}) // allow retry | |
| 85 | − return | |
| 86 | − } | |
| 87 | − | |
| 88 | − for _, tool := range result.Result.Tools { | |
| 89 | − if tool.Name == "web_search" && tool.Description != "" { | |
| 90 | − cachedToolDescription.Store(tool.Description) | |
| 91 | − log.Infof("kiro/websearch: cached web_search description from tools/list (%d bytes)", len(tool.Description)) | |
| 92 | − return // success — sync.Once stays "done", no more fetches | |
| 93 | − } | |
| 94 | − } | |
| 95 | − | |
| 96 | − // web_search tool not found in response | |
| 97 | − toolDescOnce.Store(&sync.Once{}) // allow retry | |
| 98 | − }) | |
| 99 | −} | |
| 100 | − | |
| 101 | −// GetWebSearchDescription returns the cached web_search tool description, | |
| 102 | −// or empty string if not yet fetched. Lock-free via atomic.Value. | |
| 103 | −func GetWebSearchDescription() string { | |
| 104 | − if v := cachedToolDescription.Load(); v != nil { | |
| 105 | − return v.(string) | |
| 106 | − } | |
| 107 | − return "" | |
| 108 | −} | |
| 109 | − | |
| 110 | −// WebSearchHandler handles web search requests via Kiro MCP API | |
| 111 | −type WebSearchHandler struct { | |
| 112 | − McpEndpoint string | |
| 113 | − HTTPClient *http.Client | |
| 114 | − AuthToken string | |
| 115 | − Fingerprint *kiroauth.Fingerprint // optional, for dynamic headers | |
| 116 | − AuthAttrs map[string]string // optional, for custom headers from auth.Attributes | |
| 117 | −} | |
| 118 | − | |
| 119 | −// NewWebSearchHandler creates a new WebSearchHandler. | |
| 120 | −// If httpClient is nil, a default client with 30s timeout is used. | |
| 121 | −// If fingerprint is nil, a random one-off fingerprint is generated. | |
| 122 | −// Pass a shared pooled client (e.g. from getKiroPooledHTTPClient) for connection reuse. | |
| 123 | −func NewWebSearchHandler(mcpEndpoint, authToken string, httpClient *http.Client, fp *kiroauth.Fingerprint, authAttrs map[string]string) *WebSearchHandler { | |
| 124 | − if httpClient == nil { | |
| 125 | − httpClient = &http.Client{ | |
| 126 | − Timeout: 30 * time.Second, | |
| 127 | − } | |
| 128 | − } | |
| 129 | − if fp == nil { | |
| 130 | − // Use a shared fallback fingerprint for callers without token context | |
| 131 | − fallbackFpOnce.Do(func() { | |
| 132 | − mgr := kiroauth.NewFingerprintManager() | |
| 133 | − fallbackFp = mgr.GetFingerprint("mcp-fallback") | |
| 134 | − }) | |
| 135 | − fp = fallbackFp | |
| 136 | − } | |
| 137 | − return &WebSearchHandler{ | |
| 138 | − McpEndpoint: mcpEndpoint, | |
| 139 | − HTTPClient: httpClient, | |
| 140 | − AuthToken: authToken, | |
| 141 | − Fingerprint: fp, | |
| 142 | − AuthAttrs: authAttrs, | |
| 143 | − } | |
| 144 | −} | |
| 145 | − | |
| 146 | −// setMcpHeaders sets standard MCP API headers on the request, | |
| 147 | −// aligned with the GAR request pattern in kiro_executor.go. | |
| 148 | −func (h *WebSearchHandler) setMcpHeaders(req *http.Request) { | |
| 149 | − fp := h.Fingerprint | |
| 150 | − | |
| 151 | − // 1. Content-Type & Accept (aligned with GAR) | |
| 152 | − req.Header.Set("Content-Type", "application/json") | |
| 153 | − req.Header.Set("Accept", "*/*") | |
| 154 | − | |
| 155 | − // 2. Kiro-specific headers (aligned with GAR) | |
| 156 | − req.Header.Set("x-amzn-kiro-agent-mode", "vibe") | |
| 157 | − req.Header.Set("x-amzn-codewhisperer-optout", "true") | |
| 158 | − | |
| 159 | − // 3. Dynamic fingerprint headers | |
| 160 | − req.Header.Set("User-Agent", fp.BuildUserAgent()) | |
| 161 | − req.Header.Set("X-Amz-User-Agent", fp.BuildAmzUserAgent()) | |
| 162 | − | |
| 163 | − // 4. AWS SDK identifiers (casing aligned with GAR) | |
| 164 | − req.Header.Set("Amz-Sdk-Request", "attempt=1; max=3") | |
| 165 | − req.Header.Set("Amz-Sdk-Invocation-Id", uuid.New().String()) | |
| 166 | − | |
| 167 | − // 5. Authentication | |
| 168 | − req.Header.Set("Authorization", "Bearer "+h.AuthToken) | |
| 169 | − | |
| 170 | − // 6. Custom headers from auth attributes | |
| 171 | − util.ApplyCustomHeadersFromAttrs(req, h.AuthAttrs) | |
| 172 | −} | |
| 173 | − | |
| 174 | −// mcpMaxRetries is the maximum number of retries for MCP API calls. | |
| 175 | −const mcpMaxRetries = 2 | |
| 176 | − | |
| 177 | −// CallMcpAPI calls the Kiro MCP API with the given request. | |
| 178 | −// Includes retry logic with exponential backoff for retryable errors, | |
| 179 | −// aligned with the GAR request retry pattern. | |
| 180 | −func (h *WebSearchHandler) CallMcpAPI(request *McpRequest) (*McpResponse, error) { | |
| 181 | − requestBody, err := json.Marshal(request) | |
| 182 | − if err != nil { | |
| 183 | − return nil, fmt.Errorf("failed to marshal MCP request: %w", err) | |
| 184 | − } | |
| 185 | − log.Debugf("kiro/websearch MCP request → %s (%d bytes)", h.McpEndpoint, len(requestBody)) | |
| 186 | − | |
| 187 | − var lastErr error | |
| 188 | − for attempt := 0; attempt <= mcpMaxRetries; attempt++ { | |
| 189 | − if attempt > 0 { | |
| 190 | − backoff := time.Duration(1<<attempt) * time.Second | |
| 191 | − if backoff > 10*time.Second { | |
| 192 | − backoff = 10 * time.Second | |
| 193 | − } | |
| 194 | − log.Warnf("kiro/websearch: MCP retry %d/%d after %v (last error: %v)", attempt, mcpMaxRetries, backoff, lastErr) | |
| 195 | − time.Sleep(backoff) | |
| 196 | − } | |
| 197 | − | |
| 198 | − req, err := http.NewRequest("POST", h.McpEndpoint, bytes.NewReader(requestBody)) | |
| 199 | − if err != nil { | |
| 200 | − return nil, fmt.Errorf("failed to create HTTP request: %w", err) | |
| 201 | − } | |
| 202 | − | |
| 203 | − h.setMcpHeaders(req) | |
| 204 | − | |
| 205 | − resp, err := h.HTTPClient.Do(req) | |
| 206 | − if err != nil { | |
| 207 | − lastErr = fmt.Errorf("MCP API request failed: %w", err) | |
| 208 | − continue // network error → retry | |
| 209 | − } | |
| 210 | − | |
| 211 | − body, err := io.ReadAll(resp.Body) | |
| 212 | − resp.Body.Close() | |
| 213 | − if err != nil { | |
| 214 | − lastErr = fmt.Errorf("failed to read MCP response: %w", err) | |
| 215 | − continue // read error → retry | |
| 216 | − } | |
| 217 | − log.Debugf("kiro/websearch MCP response ← [%d] (%d bytes)", resp.StatusCode, len(body)) | |
| 218 | − | |
| 219 | − // Retryable HTTP status codes (aligned with GAR: 502, 503, 504) | |
| 220 | − if resp.StatusCode >= 502 && resp.StatusCode <= 504 { | |
| 221 | − lastErr = fmt.Errorf("MCP API returned retryable status %d: %s", resp.StatusCode, string(body)) | |
| 222 | − continue | |
| 223 | − } | |
| 224 | − | |
| 225 | − if resp.StatusCode != http.StatusOK { | |
| 226 | − return nil, fmt.Errorf("MCP API returned status %d: %s", resp.StatusCode, string(body)) | |
| 227 | − } | |
| 228 | − | |
| 229 | − var mcpResponse McpResponse | |
| 230 | − if err := json.Unmarshal(body, &mcpResponse); err != nil { | |
| 231 | − return nil, fmt.Errorf("failed to parse MCP response: %w", err) | |
| 232 | − } | |
| 233 | − | |
| 234 | − if mcpResponse.Error != nil { | |
| 235 | − code := -1 | |
| 236 | − if mcpResponse.Error.Code != nil { | |
| 237 | − code = *mcpResponse.Error.Code | |
| 238 | − } | |
| 239 | − msg := "Unknown error" | |
| 240 | − if mcpResponse.Error.Message != nil { | |
| 241 | − msg = *mcpResponse.Error.Message | |
| 242 | − } | |
| 243 | − return nil, fmt.Errorf("MCP error %d: %s", code, msg) | |
| 244 | − } | |
| 245 | − | |
| 246 | − return &mcpResponse, nil | |
| 247 | − } | |
| 248 | − | |
| 249 | − return nil, lastErr | |
| 250 | −} | |
| 251 | − | |
| 252 | −// ParseSearchResults extracts WebSearchResults from MCP response | |
| 253 | −func ParseSearchResults(response *McpResponse) *WebSearchResults { | |
| 254 | − if response == nil || response.Result == nil || len(response.Result.Content) == 0 { | |
| 255 | − return nil | |
| 256 | − } | |
| 257 | − | |
| 258 | − content := response.Result.Content[0] | |
| 259 | − if content.ContentType != "text" { | |
| 260 | − return nil | |
| 261 | − } | |
| 262 | − | |
| 263 | − var results WebSearchResults | |
| 264 | − if err := json.Unmarshal([]byte(content.Text), &results); err != nil { | |
| 265 | − log.Warnf("kiro/websearch: failed to parse search results: %v", err) | |
| 266 | − return nil | |
| 267 | − } | |
| 268 | − | |
| 269 | − return &results | |
| 270 | −} | |