gunk-dev/the-valley

agy.go

sourcec14b0e68e9c3main's published tip history
2.2 KiB
1package stream
2
3import "encoding/json"
4
5// normalizeAgy follows https://antigravity.google/docs/cli/headless.
6func normalizeAgy(line []byte) []byte {
7 var ev struct {
8 Event string `json:"event"`
9 ConversationID string `json:"conversation_id"`
10 Init struct {
11 Model string `json:"model"`
12 } `json:"init"`
13 Step struct {
14 Type string `json:"step_type"`
15 State string `json:"state"`
16 Text string `json:"text_delta"`
17 Tool struct {
18 Name string `json:"name"`
19 Parameters json.RawMessage `json:"parameters"`
20 Output string `json:"output"`
21 } `json:"tool_info"`
22 } `json:"step_update"`
23 Result struct {
24 ConversationID string `json:"conversation_id"`
25 Status string `json:"status"`
26 Response string `json:"response"`
27 Error string `json:"error"`
28 DurationSeconds float64 `json:"duration_seconds"`
29 } `json:"result"`
30 }
31 if json.Unmarshal(line, &ev) != nil || ev.Event == "" {
32 return line
33 }
34 var out any
35 switch ev.Event {
36 case "init":
37 out = map[string]any{"type": "system", "subtype": "init", "session_id": ev.ConversationID, "model": ev.Init.Model}
38 case "step_update":
39 if ev.Step.Type == "agent_response" && ev.Step.Text != "" {
40 out = map[string]any{"type": "text_delta", "content": ev.Step.Text}
41 } else if ev.Step.Type == "tool" && ev.Step.State == "DONE" {
42 out = map[string]any{"type": "tool_result", "content": ev.Step.Tool.Output}
43 } else if ev.Step.Type == "tool" && ev.Step.State == "ACTIVE" {
44 out = map[string]any{"type": "assistant", "message": map[string]any{"content": []any{map[string]any{"type": "tool_use", "name": ev.Step.Tool.Name, "input": ev.Step.Tool.Parameters}}}}
45 }
46 case "result":
47 subtype := "success"
48 failed := ev.Result.Status != "SUCCESS"
49 if failed {
50 subtype = "error_during_execution"
51 }
52 out = map[string]any{"type": "result", "subtype": subtype, "session_id": ev.Result.ConversationID, "is_error": failed, "errors": []string{ev.Result.Error}, "content": ev.Result.Response, "duration_ms": int64(ev.Result.DurationSeconds * 1000)}
53 }
54 if out == nil {
55 return line
56 }
57 data, err := json.Marshal(out)
58 if err != nil {
59 return line
60 }
61 return data
62}