MCPcopy Create free account
hub / github.com/GongShichen/LuminaCode / finalizeTimedOutRun

Method finalizeTimedOutRun

agent/subagent.go:363–403  ·  view source on GitHub ↗
(ctx context.Context, prompt string, sessionState *SubAgentSessionState, fallback string)

Source from the content-addressed store, hash-verified

361
362func (s *SubAgent) finalizeTimedOutRun(ctx context.Context, prompt string, sessionState *SubAgentSessionState, fallback string) SubAgentRequestResult {
363 if sessionState == nil {
364 return SubAgentRequestResult{FinalText: fallback, TimedOut: true}
365 }
366 finalPrompt := fmt.Sprintf("You have reached the %d second sub-agent time limit. Stop using tools. Based only on the original task, conversation, tool results already available above, and the runtime partial-progress summary below, return the most useful final answer now. Be concise but include concrete findings, uncertainties, and next steps.\n\nOriginal task:\n%s\n\nRuntime partial-progress summary:\n%s", s.timeoutSeconds(), prompt, fallback)
367 messages := append([]map[string]any{}, sessionState.Messages...)
368 messages = append(messages, map[string]any{"role": "user", "content": []map[string]any{{"type": "text", "text": finalPrompt}}})
369 model := s.Model
370 if model == "" {
371 model = s.Config.APIModel
372 }
373 client, err := CreateConfiguredLLMClient(s.Config, model, s.Config.APIMaxTokens, s.ThinkingBudget, api.DefaultRetryConfigPtr())
374 if err != nil {
375 return SubAgentRequestResult{FinalText: fallback, TotalInputTokens: sessionState.TotalInputTokens, TotalOutputTokens: sessionState.TotalOutputTokens, TimedOut: true}
376 }
377 fullText := ""
378 streamCtx := api.ContextWithStreamIdleTimeout(ctx, time.Duration(s.Config.APIStreamIdleTimeoutSeconds*float64(time.Second)))
379 for result := range client.StreamChat(streamCtx, sessionState.SystemPrompt, prepareSubagentAPIMessages(messages), nil, nil) {
380 if result.Err != nil {
381 return SubAgentRequestResult{FinalText: fallback, TotalInputTokens: sessionState.TotalInputTokens, TotalOutputTokens: sessionState.TotalOutputTokens, TimedOut: true}
382 }
383 event := result.Event
384 switch stringFromAny(event["type"]) {
385 case "text_delta":
386 fullText += stringFromAny(event["text"])
387 case "usage":
388 sessionState.TotalInputTokens += intFromAny(event["input_tokens"])
389 sessionState.TotalOutputTokens += intFromAny(event["output_tokens"])
390 }
391 }
392 fullText = strings.TrimSpace(fullText)
393 if fullText == "" {
394 fullText = fallback
395 }
396 sessionState.Messages = append(messages, map[string]any{"role": "assistant", "content": []map[string]any{{"type": "text", "text": fullText}}})
397 return SubAgentRequestResult{
398 FinalText: fullText,
399 TotalInputTokens: sessionState.TotalInputTokens,
400 TotalOutputTokens: sessionState.TotalOutputTokens,
401 TimedOut: true,
402 }
403}
404
405func (s *SubAgent) partialTimeoutText(sessionState *SubAgentSessionState, currentText string) string {
406 var sections []string

Callers 2

runWorkerMethod · 0.95
RunMethod · 0.95

Calls 7

timeoutSecondsMethod · 0.95
CreateLLMClientFunction · 0.92
DefaultRetryConfigPtrFunction · 0.92
stringFromAnyFunction · 0.70
intFromAnyFunction · 0.70
StreamChatMethod · 0.65

Tested by

no test coverage detected