@@ -38,7 +38,7 @@ func (e *AgentEvent) Response() message.Message {
3838}
3939
4040type 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 )
4242 Cancel (sessionID string )
4343 IsSessionBusy (sessionID string ) bool
4444 IsBusy () bool
@@ -117,23 +117,23 @@ func (a *agent) IsSessionBusy(sessionID string) bool {
117117}
118118
119119func (a * agent ) generateTitle (ctx context.Context , sessionID string , content string ) error {
120+ if content == "" {
121+ return nil
122+ }
120123 if a .titleProvider == nil {
121124 return nil
122125 }
123126 session , err := a .sessions .Get (ctx , sessionID )
124127 if err != nil {
125128 return err
126129 }
130+ parts := []message.ContentPart {message.TextContent {Text : content }}
127131 response , err := a .titleProvider .SendMessages (
128132 ctx ,
129133 []message.Message {
130134 {
131- Role : message .User ,
132- Parts : []message.ContentPart {
133- message.TextContent {
134- Text : content ,
135- },
136- },
135+ Role : message .User ,
136+ Parts : parts ,
137137 },
138138 },
139139 make ([]tools.BaseTool , 0 ),
@@ -158,7 +158,10 @@ func (a *agent) err(err error) AgentEvent {
158158 }
159159}
160160
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+ }
162165 events := make (chan AgentEvent )
163166 if a .IsSessionBusy (sessionID ) {
164167 return nil , ErrSessionBusy
@@ -172,10 +175,13 @@ func (a *agent) Run(ctx context.Context, sessionID string, content string) (<-ch
172175 defer logging .RecoverPanic ("agent.Run" , func () {
173176 events <- a .err (fmt .Errorf ("panic while running the agent" ))
174177 })
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 )
177183 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 ( ))
179185 }
180186 logging .Debug ("Request completed" , "sessionID" , sessionID )
181187 a .activeRequests .Delete (sessionID )
@@ -186,7 +192,7 @@ func (a *agent) Run(ctx context.Context, sessionID string, content string) (<-ch
186192 return events , nil
187193}
188194
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 {
190196 // List existing messages; if none, start title generation asynchronously.
191197 msgs , err := a .messages .List (ctx , sessionID )
192198 if err != nil {
@@ -204,13 +210,13 @@ func (a *agent) processGeneration(ctx context.Context, sessionID, content string
204210 }()
205211 }
206212
207- userMsg , err := a .createUserMessage (ctx , sessionID , content )
213+ userMsg , err := a .createUserMessage (ctx , sessionID , content , attachmentParts )
208214 if err != nil {
209215 return a .err (fmt .Errorf ("failed to create user message: %w" , err ))
210216 }
211-
212217 // Append the new user message to the conversation history.
213218 msgHistory := append (msgs , userMsg )
219+
214220 for {
215221 // Check for cancellation before each iteration
216222 select {
@@ -240,12 +246,12 @@ func (a *agent) processGeneration(ctx context.Context, sessionID, content string
240246 }
241247}
242248
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 ... )
244252 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 ,
249255 })
250256}
251257
@@ -310,7 +316,6 @@ func (a *agent) streamAndHandleEvents(ctx context.Context, sessionID string, msg
310316 }
311317 continue
312318 }
313-
314319 toolResult , toolErr := tool .Run (ctx , tools.ToolCall {
315320 ID : toolCall .ID ,
316321 Name : toolCall .Name ,
0 commit comments