Skip to content

Self-hosted workflows for coding-agent delivery. Orchestrate CodeBuddy, Codex and Claude Code with scripts and human approval gates. Includes Local workflows for coding assistants.

【架构优化】从 antigravityStream.translate 中拆出 translateStep/responseStep/antigravityToolStep (#800)

fx-kurumi0599fc382627
diff --git a/internal/runtime/agent_cli_antigravity_stream_test.go b/internal/runtime/agent_cli_antigravity_stream_test.go
new file mode 100644
index 00000000..38b4c3c2
--- /dev/null
+++ b/internal/runtime/agent_cli_antigravity_stream_test.go
@@ -0,0 +1,77 @@
+package runtime
+
+import (
+ "strings"
+ "testing"
+)
+
+func translateAntigravity(raws ...string) []string {
+ translate := NewAgentCLITranslator(AgentCLIAntigravity)
+ var lines []string
+ for _, raw := range raws {
+ lines = append(lines, translate(raw)...)
+ }
+ return lines
+}
+
+func TestAntigravityStreamAgentResponseAccumulatesUntilDone(t *testing.T) {
+ lines := translateAntigravity(
+ `{"event":"init","conversation_id":"c-1","init":{"model":"gemini-x"}}`,
+ `{"event":"step_update","step_update":{"step_index":0,"step_type":"agent_response","text_delta":"你好,"}}`,
+ `{"event":"step_update","step_update":{"step_index":0,"step_type":"agent_response","text_delta":"世界","state":"DONE","usage":{"input_tokens":3,"output_tokens":4}}}`,
+ `{"event":"result","result":{"status":"SUCCESS","response":"你好,世界"}}`,
+ )
+ joined := strings.Join(lines, "\n")
+ if !strings.Contains(lines[0], `"session_id":"c-1"`) || !strings.Contains(lines[0], `"model":"gemini-x"`) {
+ t.Fatalf("init line: %s", lines[0])
+ }
+ if !strings.Contains(joined, `"text":"你好,世界"`) {
+ t.Fatalf("accumulated text block missing:\n%s", joined)
+ }
+ last := lines[len(lines)-1]
+ if !strings.Contains(last, `"type":"result"`) || !strings.Contains(last, `"is_error":false`) || !strings.Contains(last, `"input_tokens":3`) {
+ t.Fatalf("result: %s", last)
+ }
+}
+
+func TestAntigravityStreamToolStepMapsMCPAndResults(t *testing.T) {
+ lines := translateAntigravity(
+ `{"event":"step_update","step_update":{"step_index":2,"conversation_id":"c-1","step_type":"tool","tool_info":{"name":"call_mcp_tool","parameters":{"ServerName":"github","ToolName":"get_me","Arguments":{"q":1}}}}}`,
+ `{"event":"step_update","step_update":{"step_index":2,"conversation_id":"c-1","step_type":"tool","state":"DONE","tool_info":{"name":"call_mcp_tool","parameters":{"ServerName":"github","ToolName":"get_me","Arguments":{"q":1}},"output":"ok-output"}}}`,
+ `{"event":"step_update","step_update":{"step_index":3,"conversation_id":"c-1","step_type":"tool","state":"DONE","tool_info":{"name":"run_command","parameters":{"cmd":"ls"},"error":"permission denied"}}}`,
+ )
+ joined := strings.Join(lines, "\n")
+ for _, want := range []string{
+ `"name":"mcp__github__get_me"`,
+ `"input":{"q":1}`,
+ `"tool_use_id":"c-1#2"`,
+ "ok-output",
+ `"name":"run_command"`,
+ `"tool_use_id":"c-1#3"`,
+ "permission denied",
+ } {
+ if !strings.Contains(joined, want) {
+ t.Fatalf("missing %s in:\n%s", want, joined)
+ }
+ }
+ if strings.Count(joined, `"tool_use_id":"c-1#2"`) != 1 {
+ t.Fatalf("tool result for an unfinished step must not be emitted:\n%s", joined)
+ }
+}
+
+func TestAntigravityStreamFailureFallsBackToStepError(t *testing.T) {
+ lines := translateAntigravity(
+ `{"event":"step_update","step_update":{"step_index":1,"step_type":"error_message","error":"quota exceeded"}}`,
+ `{"event":"result","result":{"status":"FAILED"}}`,
+ )
+ if last := lines[len(lines)-1]; !strings.Contains(last, `"is_error":true`) || !strings.Contains(last, "quota exceeded") {
+ t.Fatalf("result: %s", last)
+ }
+ lines = translateAntigravity(`{"event":"result","result":{"status":"FAILED"}}`)
+ if last := lines[len(lines)-1]; !strings.Contains(last, "Antigravity 运行失败") {
+ t.Fatalf("default failure text: %s", last)
+ }
+ if lines := translateAntigravity(`{"event":"step_update","step_update":{"step_type":"unknown"}}`); len(lines) != 0 {
+ t.Fatalf("unknown step should emit nothing: %v", lines)
+ }
+}
diff --git a/internal/runtime/agent_cli_stream.go b/internal/runtime/agent_cli_stream.go
index d3a94805..079798ca 100644
--- a/internal/runtime/agent_cli_stream.go
+++ b/internal/runtime/agent_cli_stream.go
@@ -420,44 +420,7 @@ func (a *antigravityStream) translate(s *ClaudeStream, raw map[string]any) []str
return s.Init(stringValue(raw["conversation_id"]), stringValue(init["model"]))
case "step_update":
step, _ := raw["step_update"].(map[string]any)
- index := int64Value(step["step_index"])
- done := stringValue(step["state"]) == "DONE"
- switch stringValue(step["step_type"]) {
- case "agent_response":
- delta := stringValue(step["text_delta"])
- a.text.WriteString(delta)
- out := s.Delta(fmt.Sprintf("text#%d", index), "text", delta)
- if done {
- if usage, ok := step["usage"].(map[string]any); ok {
- s.AddUsage(int64Value(usage["input_tokens"]), int64Value(usage["output_tokens"]), int64Value(usage["cache_read_tokens"]), 0, int64Value(usage["thinking_tokens"]))
- }
- out = append(out, s.Text(a.text.String())...)
- a.text.Reset()
- }
- return out
- case "tool":
- info, _ := step["tool_info"].(map[string]any)
- id := fmt.Sprintf("%s#%d", stringValue(step["conversation_id"]), index)
- name := stringValue(info["name"])
- input := info["parameters"]
- if params, ok := input.(map[string]any); ok && name == "call_mcp_tool" {
- name, input = "mcp__"+stringValue(params["ServerName"])+"__"+stringValue(params["ToolName"]), params["Arguments"]
- }
- out := s.ToolUse(id, name, input)
- if done {
- failure := errorText(info["error"])
- if failure != "" {
- out = append(out, s.ToolResult(id, true, failure)...)
- } else {
- out = append(out, s.ToolResult(id, false, jsonText(info["output"]))...)
- }
- }
- return out
- case "error_message":
- if text := errorText(step["error"]); text != "" {
- a.failure = text
- }
- }
+ return a.translateStep(s, step)
case "result":
result, _ := raw["result"].(map[string]any)
failed := stringValue(result["status"]) != "SUCCESS"
@@ -478,6 +441,62 @@ func (a *antigravityStream) translate(s *ClaudeStream, raw map[string]any) []str
return nil
}
+// translateStep dispatches one `step_update` payload by its step_type.
+// error_message steps only remember the failure for the final result.
+func (a *antigravityStream) translateStep(s *ClaudeStream, step map[string]any) []string {
+ index := int64Value(step["step_index"])
+ done := stringValue(step["state"]) == "DONE"
+ switch stringValue(step["step_type"]) {
+ case "agent_response":
+ return a.responseStep(s, step, index, done)
+ case "tool":
+ return antigravityToolStep(s, step, index, done)
+ case "error_message":
+ if text := errorText(step["error"]); text != "" {
+ a.failure = text
+ }
+ }
+ return nil
+}
+
+// responseStep streams the text delta and, once the step is DONE, records its
+// usage and emits the accumulated text as one block.
+func (a *antigravityStream) responseStep(s *ClaudeStream, step map[string]any, index int64, done bool) []string {
+ delta := stringValue(step["text_delta"])
+ a.text.WriteString(delta)
+ out := s.Delta(fmt.Sprintf("text#%d", index), "text", delta)
+ if done {
+ if usage, ok := step["usage"].(map[string]any); ok {
+ s.AddUsage(int64Value(usage["input_tokens"]), int64Value(usage["output_tokens"]), int64Value(usage["cache_read_tokens"]), 0, int64Value(usage["thinking_tokens"]))
+ }
+ out = append(out, s.Text(a.text.String())...)
+ a.text.Reset()
+ }
+ return out
+}
+
+// antigravityToolStep reports the tool call (unwrapping call_mcp_tool into the
+// mcp__<server>__<tool> name) and, once the step is DONE, its result.
+func antigravityToolStep(s *ClaudeStream, step map[string]any, index int64, done bool) []string {
+ info, _ := step["tool_info"].(map[string]any)
+ id := fmt.Sprintf("%s#%d", stringValue(step["conversation_id"]), index)
+ name := stringValue(info["name"])
+ input := info["parameters"]
+ if params, ok := input.(map[string]any); ok && name == "call_mcp_tool" {
+ name, input = "mcp__"+stringValue(params["ServerName"])+"__"+stringValue(params["ToolName"]), params["Arguments"]
+ }
+ out := s.ToolUse(id, name, input)
+ if done {
+ failure := errorText(info["error"])
+ if failure != "" {
+ out = append(out, s.ToolResult(id, true, failure)...)
+ } else {
+ out = append(out, s.ToolResult(id, false, jsonText(info["output"]))...)
+ }
+ }
+ return out
+}
+
// kimiStream: `kimi -p --output-format stream-json` prints whole messages;
// the session ID only arrives with the closing resume hint.
type kimiStream struct{ started bool }