|
| 1 | +package agents |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "strings" |
| 8 | +) |
| 9 | + |
| 10 | +const ( |
| 11 | + // ACPUpdateTypeMessageChunk streams agent text deltas. |
| 12 | + ACPUpdateTypeMessageChunk = "agent_message_chunk" |
| 13 | + // ACPUpdateTypePlan replaces the current agent plan entries. |
| 14 | + ACPUpdateTypePlan = "plan" |
| 15 | +) |
| 16 | + |
| 17 | +// PlanEntry is one ACP plan entry shown to the user. |
| 18 | +type PlanEntry struct { |
| 19 | + Content string `json:"content"` |
| 20 | + Status string `json:"status,omitempty"` |
| 21 | + Priority string `json:"priority,omitempty"` |
| 22 | +} |
| 23 | + |
| 24 | +// ACPUpdate is one normalized ACP session/update payload. |
| 25 | +type ACPUpdate struct { |
| 26 | + Type string |
| 27 | + Delta string |
| 28 | + PlanEntries []PlanEntry |
| 29 | +} |
| 30 | + |
| 31 | +// ParseACPUpdate normalizes provider-specific session/update payloads. |
| 32 | +func ParseACPUpdate(raw json.RawMessage) (ACPUpdate, error) { |
| 33 | + if len(raw) == 0 { |
| 34 | + return ACPUpdate{}, nil |
| 35 | + } |
| 36 | + |
| 37 | + var payload struct { |
| 38 | + Delta string `json:"delta"` |
| 39 | + Update struct { |
| 40 | + SessionUpdate string `json:"sessionUpdate"` |
| 41 | + Content struct { |
| 42 | + Type string `json:"type"` |
| 43 | + Text string `json:"text"` |
| 44 | + } `json:"content"` |
| 45 | + Entries []PlanEntry `json:"entries"` |
| 46 | + } `json:"update"` |
| 47 | + } |
| 48 | + if err := json.Unmarshal(raw, &payload); err != nil { |
| 49 | + return ACPUpdate{}, fmt.Errorf("decode ACP session/update payload: %w", err) |
| 50 | + } |
| 51 | + |
| 52 | + switch strings.TrimSpace(payload.Update.SessionUpdate) { |
| 53 | + case "": |
| 54 | + if payload.Delta == "" { |
| 55 | + return ACPUpdate{}, nil |
| 56 | + } |
| 57 | + return ACPUpdate{ |
| 58 | + Type: ACPUpdateTypeMessageChunk, |
| 59 | + Delta: payload.Delta, |
| 60 | + }, nil |
| 61 | + case ACPUpdateTypeMessageChunk: |
| 62 | + if contentType := strings.TrimSpace(payload.Update.Content.Type); contentType != "" && contentType != "text" { |
| 63 | + return ACPUpdate{Type: ACPUpdateTypeMessageChunk}, nil |
| 64 | + } |
| 65 | + return ACPUpdate{ |
| 66 | + Type: ACPUpdateTypeMessageChunk, |
| 67 | + Delta: payload.Update.Content.Text, |
| 68 | + }, nil |
| 69 | + case ACPUpdateTypePlan: |
| 70 | + return ACPUpdate{ |
| 71 | + Type: ACPUpdateTypePlan, |
| 72 | + PlanEntries: normalizePlanEntries(payload.Update.Entries), |
| 73 | + }, nil |
| 74 | + default: |
| 75 | + return ACPUpdate{Type: strings.TrimSpace(payload.Update.SessionUpdate)}, nil |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +// ClonePlanEntries returns a trimmed deep copy of the provided entries. |
| 80 | +func ClonePlanEntries(entries []PlanEntry) []PlanEntry { |
| 81 | + return normalizePlanEntries(entries) |
| 82 | +} |
| 83 | + |
| 84 | +func normalizePlanEntries(entries []PlanEntry) []PlanEntry { |
| 85 | + if len(entries) == 0 { |
| 86 | + return nil |
| 87 | + } |
| 88 | + |
| 89 | + normalized := make([]PlanEntry, 0, len(entries)) |
| 90 | + for _, entry := range entries { |
| 91 | + content := strings.TrimSpace(entry.Content) |
| 92 | + if content == "" { |
| 93 | + continue |
| 94 | + } |
| 95 | + normalized = append(normalized, PlanEntry{ |
| 96 | + Content: content, |
| 97 | + Status: strings.TrimSpace(entry.Status), |
| 98 | + Priority: strings.TrimSpace(entry.Priority), |
| 99 | + }) |
| 100 | + } |
| 101 | + if len(normalized) == 0 { |
| 102 | + return nil |
| 103 | + } |
| 104 | + return normalized |
| 105 | +} |
| 106 | + |
| 107 | +// PlanHandler receives ACP plan replacements for the active turn. |
| 108 | +type PlanHandler func(ctx context.Context, entries []PlanEntry) error |
| 109 | + |
| 110 | +type planHandlerContextKey struct{} |
| 111 | + |
| 112 | +// WithPlanHandler binds one per-turn plan callback to context. |
| 113 | +func WithPlanHandler(ctx context.Context, handler PlanHandler) context.Context { |
| 114 | + if handler == nil { |
| 115 | + return ctx |
| 116 | + } |
| 117 | + return context.WithValue(ctx, planHandlerContextKey{}, handler) |
| 118 | +} |
| 119 | + |
| 120 | +// PlanHandlerFromContext gets plan callback from context, if present. |
| 121 | +func PlanHandlerFromContext(ctx context.Context) (PlanHandler, bool) { |
| 122 | + if ctx == nil { |
| 123 | + return nil, false |
| 124 | + } |
| 125 | + handler, ok := ctx.Value(planHandlerContextKey{}).(PlanHandler) |
| 126 | + if !ok || handler == nil { |
| 127 | + return nil, false |
| 128 | + } |
| 129 | + return handler, true |
| 130 | +} |
0 commit comments