@@ -131,7 +131,7 @@ func (a *sessionAgent) Run(ctx context.Context, call SessionAgentCall) (*fantasy
133 if len(a.tools) > 0 {
133 if len(a.tools) > 0 {
134− // add anthropic caching to the last tool
134+ // Add anthropic caching to the last tool.
135 a.tools[len(a.tools)-1].SetProviderOptions(a.getCacheControlOptions())
135 a.tools[len(a.tools)-1].SetProviderOptions(a.getCacheControlOptions())
@@ -153,7 +153,7 @@ func (a *sessionAgent) Run(ctx context.Context, call SessionAgentCall) (*fantasy
155 var wg sync.WaitGroup
155 var wg sync.WaitGroup
156− // Generate title if first message
156+ // Generate title if first message.
157 if len(msgs) == 0 {
157 if len(msgs) == 0 {
158 wg.Go(func() {
158 wg.Go(func() {
159 sessionLock.Lock()
159 sessionLock.Lock()
@@ -162,13 +162,13 @@ func (a *sessionAgent) Run(ctx context.Context, call SessionAgentCall) (*fantasy
165− // Add the user message to the session
165+ // Add the user message to the session.
166 _, err = a.createUserMessage(ctx, call)
166 _, err = a.createUserMessage(ctx, call)
167 if err != nil {
167 if err != nil {
168 return nil, err
168 return nil, err
171− // add the session to the context
171+ // Add the session to the context.
172 ctx = context.WithValue(ctx, tools.SessionIDContextKey, call.SessionID)
172 ctx = context.WithValue(ctx, tools.SessionIDContextKey, call.SessionID)
174 genCtx, cancel := context.WithCancel(ctx)
174 genCtx, cancel := context.WithCancel(ctx)
@@ -195,10 +195,10 @@ func (a *sessionAgent) Run(ctx context.Context, call SessionAgentCall) (*fantasy
195 PresencePenalty: call.PresencePenalty,
195 PresencePenalty: call.PresencePenalty,
196 TopK: call.TopK,
196 TopK: call.TopK,
197 FrequencyPenalty: call.FrequencyPenalty,
197 FrequencyPenalty: call.FrequencyPenalty,
198− // Before each step create the new assistant message
198+ // Before each step create a new assistant message.
199 PrepareStep: func(callContext context.Context, options fantasy.PrepareStepFunctionOptions) (_ context.Context, prepared fantasy.PrepareStepResult, err error) {
199 PrepareStep: func(callContext context.Context, options fantasy.PrepareStepFunctionOptions) (_ context.Context, prepared fantasy.PrepareStepResult, err error) {
200 prepared.Messages = options.Messages
200 prepared.Messages = options.Messages
201− // reset all cached items
201+ // Reset all cached items.
202 for i := range prepared.Messages {
202 for i := range prepared.Messages {
203 prepared.Messages[i].ProviderOptions = nil
203 prepared.Messages[i].ProviderOptions = nil
@@ -216,14 +216,14 @@ func (a *sessionAgent) Run(ctx context.Context, call SessionAgentCall) (*fantasy
216 lastSystemRoleInx := 0
216 lastSystemRoleInx := 0
217 systemMessageUpdated := false
217 systemMessageUpdated := false
218 for i, msg := range prepared.Messages {
218 for i, msg := range prepared.Messages {
219− // only add cache control to the last message
219+ // Only add cache control to the last message.
220 if msg.Role == fantasy.MessageRoleSystem {
220 if msg.Role == fantasy.MessageRoleSystem {
221 lastSystemRoleInx = i
221 lastSystemRoleInx = i
222 } else if !systemMessageUpdated {
222 } else if !systemMessageUpdated {
223 prepared.Messages[lastSystemRoleInx].ProviderOptions = a.getCacheControlOptions()
223 prepared.Messages[lastSystemRoleInx].ProviderOptions = a.getCacheControlOptions()
224 systemMessageUpdated = true
224 systemMessageUpdated = true
226− // than add cache control to the last 2 messages
226+ // Than add cache control to the last 2 messages.
227 if i > len(prepared.Messages)-3 {
227 if i > len(prepared.Messages)-3 {
228 prepared.Messages[i].ProviderOptions = a.getCacheControlOptions()
228 prepared.Messages[i].ProviderOptions = a.getCacheControlOptions()
@@ -394,10 +394,10 @@ func (a *sessionAgent) Run(ctx context.Context, call SessionAgentCall) (*fantasy
394 if currentAssistant == nil {
394 if currentAssistant == nil {
395 return result, err
395 return result, err
397− // Ensure we finish thinking on error to close the reasoning state
397+ // Ensure we finish thinking on error to close the reasoning state.
398 currentAssistant.FinishThinking()
398 currentAssistant.FinishThinking()
399 toolCalls := currentAssistant.ToolCalls()
399 toolCalls := currentAssistant.ToolCalls()
400− // INFO: we use the parent context here because the genCtx has been cancelled
400+ // INFO: we use the parent context here because the genCtx has been cancelled.
401 msgs, createErr := a.messages.List(ctx, currentAssistant.SessionID)
401 msgs, createErr := a.messages.List(ctx, currentAssistant.SessionID)
402 if createErr != nil {
402 if createErr != nil {
403 return nil, createErr
403 return nil, createErr
@@ -459,7 +459,8 @@ func (a *sessionAgent) Run(ctx context.Context, call SessionAgentCall) (*fantasy
460 currentAssistant.AddFinish(message.FinishReasonError, "API Error", err.Error())
460 currentAssistant.AddFinish(message.FinishReasonError, "API Error", err.Error())
462− // INFO: we use the parent context here because the genCtx has been cancelled
462+ // Note: we use the parent context here because the genCtx has been
463 updateErr := a.messages.Update(ctx, *currentAssistant)
464 updateErr := a.messages.Update(ctx, *currentAssistant)
464 if updateErr != nil {
465 if updateErr != nil {
465 return nil, updateErr
466 return nil, updateErr
@@ -473,7 +474,7 @@ func (a *sessionAgent) Run(ctx context.Context, call SessionAgentCall) (*fantasy
473 if summarizeErr := a.Summarize(genCtx, call.SessionID, call.ProviderOptions); summarizeErr != nil {
474 if summarizeErr := a.Summarize(genCtx, call.SessionID, call.ProviderOptions); summarizeErr != nil {
474 return nil, summarizeErr
475 return nil, summarizeErr
476− // if the agent was not done...
477+ // If the agent wasn't done...
477 if len(currentAssistant.ToolCalls()) > 0 {
478 if len(currentAssistant.ToolCalls()) > 0 {
478 existing, ok := a.messageQueue.Get(call.SessionID)
479 existing, ok := a.messageQueue.Get(call.SessionID)
@@ -485,7 +486,7 @@ func (a *sessionAgent) Run(ctx context.Context, call SessionAgentCall) (*fantasy
488− // release active request before processing queued messages
489+ // Release active request before processing queued messages.
489 a.activeRequests.Del(call.SessionID)
490 a.activeRequests.Del(call.SessionID)
@@ -493,7 +494,7 @@ func (a *sessionAgent) Run(ctx context.Context, call SessionAgentCall) (*fantasy
493 if !ok || len(queuedMessages) == 0 {
494 if !ok || len(queuedMessages) == 0 {
494 return result, err
495 return result, err
496− // there are queued messages restart the loop
497+ // There are queued messages restart the loop.
497 firstQueuedMessage := queuedMessages[0]
498 firstQueuedMessage := queuedMessages[0]
498 a.messageQueue.Set(call.SessionID, queuedMessages[1:])
499 a.messageQueue.Set(call.SessionID, queuedMessages[1:])
499 return a.Run(ctx, firstQueuedMessage)
500 return a.Run(ctx, firstQueuedMessage)
@@ -513,7 +514,7 @@ func (a *sessionAgent) Summarize(ctx context.Context, sessionID string, opts fan
513 return err
514 return err
515 if len(msgs) == 0 {
516 if len(msgs) == 0 {
516− // nothing to summarize
517+ // Nothing to summarize.
517 return nil
518 return nil
@@ -553,7 +554,7 @@ func (a *sessionAgent) Summarize(ctx context.Context, sessionID string, opts fan
553 return a.messages.Update(genCtx, summaryMessage)
554 return a.messages.Update(genCtx, summaryMessage)
555 OnReasoningEnd: func(id string, reasoning fantasy.ReasoningContent) error {
556 OnReasoningEnd: func(id string, reasoning fantasy.ReasoningContent) error {
556− // handle anthropic signature
557+ // Handle anthropic signature.
557 if anthropicData, ok := reasoning.ProviderMetadata["anthropic"]; ok {
558 if anthropicData, ok := reasoning.ProviderMetadata["anthropic"]; ok {
558 if signature, ok := anthropicData.(*anthropic.ReasoningOptionMetadata); ok && signature.Signature != "" {
559 if signature, ok := anthropicData.(*anthropic.ReasoningOptionMetadata); ok && signature.Signature != "" {
559 summaryMessage.AppendReasoningSignature(signature.Signature)
560 summaryMessage.AppendReasoningSignature(signature.Signature)
@@ -570,7 +571,7 @@ func (a *sessionAgent) Summarize(ctx context.Context, sessionID string, opts fan
570 if err != nil {
571 if err != nil {
571 isCancelErr := errors.Is(err, context.Canceled)
572 isCancelErr := errors.Is(err, context.Canceled)
572 if isCancelErr {
573 if isCancelErr {
573− // User cancelled summarize we need to remove the summary message
574+ // User cancelled summarize we need to remove the summary message.
574 deleteErr := a.messages.Delete(ctx, summaryMessage.ID)
575 deleteErr := a.messages.Delete(ctx, summaryMessage.ID)
575 return deleteErr
576 return deleteErr
@@ -597,7 +598,7 @@ func (a *sessionAgent) Summarize(ctx context.Context, sessionID string, opts fan
598 a.updateSessionUsage(a.largeModel, ¤tSession, resp.TotalUsage, openrouterCost)
599 a.updateSessionUsage(a.largeModel, ¤tSession, resp.TotalUsage, openrouterCost)
600− // just in case get just the last usage
601+ // Just in case, get just the last usage info.
601 usage := resp.Response.Usage
602 usage := resp.Response.Usage
602 currentSession.SummaryMessageID = summaryMessage.ID
603 currentSession.SummaryMessageID = summaryMessage.ID
603 currentSession.CompletionTokens = usage.OutputTokens
604 currentSession.CompletionTokens = usage.OutputTokens
@@ -643,7 +644,8 @@ func (a *sessionAgent) preparePrompt(msgs []message.Message, attachments ...mess
643 if len(m.Parts) == 0 {
644 if len(m.Parts) == 0 {
646− // Assistant message without content or tool calls (cancelled before it returned anything)
647+ // Assistant message without content or tool calls (cancelled before it
648+ // returned anything).
647 if m.Role == message.Assistant && len(m.ToolCalls()) == 0 && m.Content().Text == "" && m.ReasoningContent().String() == "" {
649 if m.Role == message.Assistant && len(m.ToolCalls()) == 0 && m.Content().Text == "" && m.ReasoningContent().String() == "" {
@@ -718,7 +720,7 @@ func (a *sessionAgent) generateTitle(ctx context.Context, session *session.Sessi
719 title = strings.ReplaceAll(title, "\n", " ")
721 title = strings.ReplaceAll(title, "\n", " ")
721− // remove thinking tags if present
723+ // Remove thinking tags if present.
722 if idx := strings.Index(title, "</think>"); idx > 0 {
724 if idx := strings.Index(title, "</think>"); idx > 0 {
723 title = title[idx+len("</think>"):]
725 title = title[idx+len("</think>"):]
@@ -784,13 +786,13 @@ func (a *sessionAgent) updateSessionUsage(model Model, session *session.Session,
786func (a *sessionAgent) Cancel(sessionID string) {
788func (a *sessionAgent) Cancel(sessionID string) {
787− // Cancel regular requests
789+ // Cancel regular requests.
788 if cancel, ok := a.activeRequests.Take(sessionID); ok && cancel != nil {
790 if cancel, ok := a.activeRequests.Take(sessionID); ok && cancel != nil {
789 slog.Info("Request cancellation initiated", "session_id", sessionID)
791 slog.Info("Request cancellation initiated", "session_id", sessionID)
793− // Also check for summarize requests
795+ // Also check for summarize requests.
794 if cancel, ok := a.activeRequests.Take(sessionID + "-summarize"); ok && cancel != nil {
796 if cancel, ok := a.activeRequests.Take(sessionID + "-summarize"); ok && cancel != nil {
795 slog.Info("Summarize cancellation initiated", "session_id", sessionID)
797 slog.Info("Summarize cancellation initiated", "session_id", sessionID)