@@ -38,7 +38,7 @@ func (e *AgentEvent) Response() message.Message {
38
38
}
39
39
40
40
type Service interface {
41
- Run (ctx context.Context , sessionID string , content string ) (<- chan AgentEvent , error )
41
+ Run (ctx context.Context , sessionID string , content string , attachments ... message. Attachment ) (<- chan AgentEvent , error )
42
42
Cancel (sessionID string )
43
43
IsSessionBusy (sessionID string ) bool
44
44
IsBusy () bool
@@ -117,23 +117,23 @@ func (a *agent) IsSessionBusy(sessionID string) bool {
117
117
}
118
118
119
119
func (a * agent ) generateTitle (ctx context.Context , sessionID string , content string ) error {
120
+ if content == "" {
121
+ return nil
122
+ }
120
123
if a .titleProvider == nil {
121
124
return nil
122
125
}
123
126
session , err := a .sessions .Get (ctx , sessionID )
124
127
if err != nil {
125
128
return err
126
129
}
130
+ parts := []message.ContentPart {message.TextContent {Text : content }}
127
131
response , err := a .titleProvider .SendMessages (
128
132
ctx ,
129
133
[]message.Message {
130
134
{
131
- Role : message .User ,
132
- Parts : []message.ContentPart {
133
- message.TextContent {
134
- Text : content ,
135
- },
136
- },
135
+ Role : message .User ,
136
+ Parts : parts ,
137
137
},
138
138
},
139
139
make ([]tools.BaseTool , 0 ),
@@ -158,7 +158,10 @@ func (a *agent) err(err error) AgentEvent {
158
158
}
159
159
}
160
160
161
- func (a * agent ) Run (ctx context.Context , sessionID string , content string ) (<- chan AgentEvent , error ) {
161
+ func (a * agent ) Run (ctx context.Context , sessionID string , content string , attachments ... message.Attachment ) (<- chan AgentEvent , error ) {
162
+ if ! a .provider .Model ().SupportsAttachments && attachments != nil {
163
+ attachments = nil
164
+ }
162
165
events := make (chan AgentEvent )
163
166
if a .IsSessionBusy (sessionID ) {
164
167
return nil , ErrSessionBusy
@@ -172,10 +175,13 @@ func (a *agent) Run(ctx context.Context, sessionID string, content string) (<-ch
172
175
defer logging .RecoverPanic ("agent.Run" , func () {
173
176
events <- a .err (fmt .Errorf ("panic while running the agent" ))
174
177
})
175
-
176
- result := a .processGeneration (genCtx , sessionID , content )
178
+ var attachmentParts []message.ContentPart
179
+ for _ , attachment := range attachments {
180
+ attachmentParts = append (attachmentParts , message.BinaryContent {Path : attachment .FilePath , MIMEType : attachment .MimeType , Data : attachment .Content })
181
+ }
182
+ result := a .processGeneration (genCtx , sessionID , content , attachmentParts )
177
183
if result .Err () != nil && ! errors .Is (result .Err (), ErrRequestCancelled ) && ! errors .Is (result .Err (), context .Canceled ) {
178
- logging .ErrorPersist (fmt . Sprintf ( "Generation error for session %s: %v" , sessionID , result ))
184
+ logging .ErrorPersist (result . Err (). Error ( ))
179
185
}
180
186
logging .Debug ("Request completed" , "sessionID" , sessionID )
181
187
a .activeRequests .Delete (sessionID )
@@ -186,7 +192,7 @@ func (a *agent) Run(ctx context.Context, sessionID string, content string) (<-ch
186
192
return events , nil
187
193
}
188
194
189
- func (a * agent ) processGeneration (ctx context.Context , sessionID , content string ) AgentEvent {
195
+ func (a * agent ) processGeneration (ctx context.Context , sessionID , content string , attachmentParts []message. ContentPart ) AgentEvent {
190
196
// List existing messages; if none, start title generation asynchronously.
191
197
msgs , err := a .messages .List (ctx , sessionID )
192
198
if err != nil {
@@ -204,13 +210,13 @@ func (a *agent) processGeneration(ctx context.Context, sessionID, content string
204
210
}()
205
211
}
206
212
207
- userMsg , err := a .createUserMessage (ctx , sessionID , content )
213
+ userMsg , err := a .createUserMessage (ctx , sessionID , content , attachmentParts )
208
214
if err != nil {
209
215
return a .err (fmt .Errorf ("failed to create user message: %w" , err ))
210
216
}
211
-
212
217
// Append the new user message to the conversation history.
213
218
msgHistory := append (msgs , userMsg )
219
+
214
220
for {
215
221
// Check for cancellation before each iteration
216
222
select {
@@ -240,12 +246,12 @@ func (a *agent) processGeneration(ctx context.Context, sessionID, content string
240
246
}
241
247
}
242
248
243
- func (a * agent ) createUserMessage (ctx context.Context , sessionID , content string ) (message.Message , error ) {
249
+ func (a * agent ) createUserMessage (ctx context.Context , sessionID , content string , attachmentParts []message.ContentPart ) (message.Message , error ) {
250
+ parts := []message.ContentPart {message.TextContent {Text : content }}
251
+ parts = append (parts , attachmentParts ... )
244
252
return a .messages .Create (ctx , sessionID , message.CreateMessageParams {
245
- Role : message .User ,
246
- Parts : []message.ContentPart {
247
- message.TextContent {Text : content },
248
- },
253
+ Role : message .User ,
254
+ Parts : parts ,
249
255
})
250
256
}
251
257
@@ -310,7 +316,6 @@ func (a *agent) streamAndHandleEvents(ctx context.Context, sessionID string, msg
310
316
}
311
317
continue
312
318
}
313
-
314
319
toolResult , toolErr := tool .Run (ctx , tools.ToolCall {
315
320
ID : toolCall .ID ,
316
321
Name : toolCall .Name ,
0 commit comments