-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathhelpers.go
More file actions
259 lines (224 loc) · 8.02 KB
/
helpers.go
File metadata and controls
259 lines (224 loc) · 8.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package acp
// TextBlock constructs a text content block.
func TextBlock(text string) ContentBlock {
return ContentBlock{Text: &ContentBlockText{
Text: text,
Type: "text",
}}
}
// ImageBlock constructs an inline image content block with base64-encoded data.
func ImageBlock(data string, mimeType string) ContentBlock {
return ContentBlock{Image: &ContentBlockImage{
Data: data,
MimeType: mimeType,
Type: "image",
}}
}
// AudioBlock constructs an inline audio content block with base64-encoded data.
func AudioBlock(data string, mimeType string) ContentBlock {
return ContentBlock{Audio: &ContentBlockAudio{
Data: data,
MimeType: mimeType,
Type: "audio",
}}
}
// ResourceLinkBlock constructs a resource_link content block with a name and URI.
func ResourceLinkBlock(name string, uri string) ContentBlock {
return ContentBlock{ResourceLink: &ContentBlockResourceLink{
Name: name,
Type: "resource_link",
Uri: uri,
}}
}
// ResourceBlock wraps an embedded resource as a content block.
func ResourceBlock(res EmbeddedResourceResource) ContentBlock {
return ContentBlock{Resource: &ContentBlockResource{
Resource: res,
Type: "resource",
}}
}
// ToolContent wraps a content block as tool-call content.
func ToolContent(block ContentBlock) ToolCallContent {
return ToolCallContent{Content: &ToolCallContentContent{
Content: block,
Type: "content",
}}
}
// ToolDiffContent constructs a diff tool-call content. If oldText is omitted, the field is left empty.
func ToolDiffContent(path string, newText string, oldText ...string) ToolCallContent {
var o *string
if len(oldText) > 0 {
o = &oldText[0]
}
return ToolCallContent{Diff: &ToolCallContentDiff{
NewText: newText,
OldText: o,
Path: path,
Type: "diff",
}}
}
// ToolTerminalRef constructs a terminal reference tool-call content.
func ToolTerminalRef(terminalID string) ToolCallContent {
return ToolCallContent{Terminal: &ToolCallContentTerminal{
TerminalId: terminalID,
Type: "terminal",
}}
}
// Ptr returns a pointer to v.
func Ptr[T any](v T) *T {
return &v
}
// UpdateUserMessage constructs a user_message_chunk update with the given content.
func UpdateUserMessage(content ContentBlock) SessionUpdate {
return SessionUpdate{UserMessageChunk: &SessionUpdateUserMessageChunk{Content: content}}
}
// UpdateUserMessageText constructs a user_message_chunk update from text.
func UpdateUserMessageText(text string) SessionUpdate {
return UpdateUserMessage(TextBlock(text))
}
// UpdateAgentMessage constructs an agent_message_chunk update with the given content.
func UpdateAgentMessage(content ContentBlock) SessionUpdate {
return SessionUpdate{AgentMessageChunk: &SessionUpdateAgentMessageChunk{Content: content}}
}
// UpdateAgentMessageText constructs an agent_message_chunk update from text.
func UpdateAgentMessageText(text string) SessionUpdate {
return UpdateAgentMessage(TextBlock(text))
}
// UpdateAgentThought constructs an agent_thought_chunk update with the given content.
func UpdateAgentThought(content ContentBlock) SessionUpdate {
return SessionUpdate{AgentThoughtChunk: &SessionUpdateAgentThoughtChunk{Content: content}}
}
// UpdateAgentThoughtText constructs an agent_thought_chunk update from text.
func UpdateAgentThoughtText(text string) SessionUpdate {
return UpdateAgentThought(TextBlock(text))
}
// UpdatePlan constructs a plan update with the provided entries.
func UpdatePlan(entries ...PlanEntry) SessionUpdate {
return SessionUpdate{Plan: &SessionUpdatePlan{Entries: entries}}
}
type ToolCallStartOpt func(tc *SessionUpdateToolCall)
// StartToolCall constructs a tool_call update with required fields and applies optional modifiers.
func StartToolCall(id ToolCallId, title string, opts ...ToolCallStartOpt) SessionUpdate {
tc := SessionUpdateToolCall{
Title: title,
ToolCallId: id,
}
for _, opt := range opts {
opt(&tc)
}
return SessionUpdate{ToolCall: &tc}
}
// WithStartKind sets the kind for a tool_call start update.
func WithStartKind(k ToolKind) ToolCallStartOpt {
return func(tc *SessionUpdateToolCall) {
tc.Kind = k
}
}
// WithStartStatus sets the status for a tool_call start update.
func WithStartStatus(s ToolCallStatus) ToolCallStartOpt {
return func(tc *SessionUpdateToolCall) {
tc.Status = s
}
}
// WithStartContent sets the initial content for a tool_call start update.
func WithStartContent(c []ToolCallContent) ToolCallStartOpt {
return func(tc *SessionUpdateToolCall) {
tc.Content = c
}
}
// WithStartLocations sets file locations and, if a single path is provided and rawInput is empty, mirrors it as rawInput.path.
func WithStartLocations(l []ToolCallLocation) ToolCallStartOpt {
return func(tc *SessionUpdateToolCall) {
tc.Locations = l
if len(l) == 1 && l[0].Path != "" {
if tc.RawInput == nil {
tc.RawInput = map[string]any{"path": l[0].Path}
} else {
m, ok := tc.RawInput.(map[string]any)
if ok {
if _, exists := m["path"]; !exists {
m["path"] = l[0].Path
}
}
}
}
}
}
// WithStartRawInput sets rawInput for a tool_call start update.
func WithStartRawInput(v any) ToolCallStartOpt {
return func(tc *SessionUpdateToolCall) {
tc.RawInput = v
}
}
// WithStartRawOutput sets rawOutput for a tool_call start update.
func WithStartRawOutput(v any) ToolCallStartOpt {
return func(tc *SessionUpdateToolCall) {
tc.RawOutput = v
}
}
type ToolCallUpdateOpt func(tu *SessionToolCallUpdate)
// UpdateToolCall constructs a tool_call_update with the given ID and applies optional modifiers.
func UpdateToolCall(id ToolCallId, opts ...ToolCallUpdateOpt) SessionUpdate {
tu := SessionToolCallUpdate{ToolCallId: id}
for _, opt := range opts {
opt(&tu)
}
return SessionUpdate{ToolCallUpdate: &tu}
}
// WithUpdateTitle sets the title for a tool_call_update.
func WithUpdateTitle(t string) ToolCallUpdateOpt {
return func(tu *SessionToolCallUpdate) {
tu.Title = Ptr(t)
}
}
// WithUpdateKind sets the kind for a tool_call_update.
func WithUpdateKind(k ToolKind) ToolCallUpdateOpt {
return func(tu *SessionToolCallUpdate) {
tu.Kind = Ptr(k)
}
}
// WithUpdateStatus sets the status for a tool_call_update.
func WithUpdateStatus(s ToolCallStatus) ToolCallUpdateOpt {
return func(tu *SessionToolCallUpdate) {
tu.Status = Ptr(s)
}
}
// WithUpdateContent replaces the content collection for a tool_call_update.
func WithUpdateContent(c []ToolCallContent) ToolCallUpdateOpt {
return func(tu *SessionToolCallUpdate) {
tu.Content = c
}
}
// WithUpdateLocations replaces the locations collection for a tool_call_update.
func WithUpdateLocations(l []ToolCallLocation) ToolCallUpdateOpt {
return func(tu *SessionToolCallUpdate) {
tu.Locations = l
}
}
// WithUpdateRawInput sets rawInput for a tool_call_update.
func WithUpdateRawInput(v any) ToolCallUpdateOpt {
return func(tu *SessionToolCallUpdate) {
tu.RawInput = v
}
}
// WithUpdateRawOutput sets rawOutput for a tool_call_update.
func WithUpdateRawOutput(v any) ToolCallUpdateOpt {
return func(tu *SessionToolCallUpdate) {
tu.RawOutput = v
}
}
// StartReadToolCall constructs a 'tool_call' update for reading a file: kind=read, status=pending, locations=[{path}], rawInput={path}.
func StartReadToolCall(id ToolCallId, title string, path string, opts ...ToolCallStartOpt) SessionUpdate {
base := []ToolCallStartOpt{WithStartKind(ToolKindRead), WithStartStatus(ToolCallStatusPending), WithStartLocations([]ToolCallLocation{{Path: path}}), WithStartRawInput(map[string]any{"path": path})}
args := append(base, opts...)
return StartToolCall(id, title, args...)
}
// StartEditToolCall constructs a 'tool_call' update for editing content: kind=edit, status=pending, locations=[{path}], rawInput={path, content}.
func StartEditToolCall(id ToolCallId, title string, path string, content any, opts ...ToolCallStartOpt) SessionUpdate {
base := []ToolCallStartOpt{WithStartKind(ToolKindEdit), WithStartStatus(ToolCallStatusPending), WithStartLocations([]ToolCallLocation{{Path: path}}), WithStartRawInput(map[string]any{
"content": content,
"path": path,
})}
args := append(base, opts...)
return StartToolCall(id, title, args...)
}