| 90 | |
| 91 | func (s *SubAgent) Run(ctx context.Context, prompt string) (string, error) { |
| 92 | if ctx == nil { |
| 93 | ctx = context.Background() |
| 94 | } |
| 95 | runCtx, cancel := s.subagentRunContext(ctx) |
| 96 | defer cancel() |
| 97 | sessionState := s.createSessionState(runCtx, prompt) |
| 98 | result := s.ExecuteOneRequest(runCtx, prompt, sessionState) |
| 99 | if result.TimedOut && ctx.Err() == nil { |
| 100 | finalizeCtx, finalizeCancel := context.WithTimeout(ctx, time.Duration(SubagentFinalizeSeconds)*time.Second) |
| 101 | result = s.finalizeTimedOutRun(finalizeCtx, prompt, sessionState, result.FinalText) |
| 102 | finalizeCancel() |
| 103 | } |
| 104 | if result.TimedOut { |
| 105 | result.FinalText = s.wrapTimedOutResult(result.FinalText) |
| 106 | } |
| 107 | s.TotalInputTokens += result.TotalInputTokens |
| 108 | s.TotalOutputTokens += result.TotalOutputTokens |
| 109 | return result.FinalText, nil |
| 110 | } |
| 111 | |
| 112 | func (s *SubAgent) CreateSessionState(prompt string) *SubAgentSessionState { |
| 113 | return s.createSessionState(context.Background(), prompt) |
| 114 | } |