MCPcopy Create free account
hub / github.com/MoonshotAI/kimi-code / runTurn

Function runTurn

packages/agent-core/src/loop/run-turn.ts:54–137  ·  view source on GitHub ↗
(input: RunTurnInput)

Source from the content-addressed store, hash-verified

52}
53
54export async function runTurn(input: RunTurnInput): Promise<TurnResult> {
55 const {
56 turnId,
57 signal,
58 llm,
59 buildMessages,
60 buildMessagesStrict,
61 dispatchEvent,
62 tools,
63 hooks,
64 log,
65 maxSteps,
66 maxRetryAttempts,
67 recordStepUsage: hostRecordStepUsage,
68 } = input;
69 let usage: TokenUsage = emptyUsage();
70 let steps = 0;
71 // Normal exits overwrite this with the completed step's stop reason.
72 let stopReason: LoopTurnStopReason = 'end_turn';
73 let activeStep: number | undefined;
74 const recordStepUsage = async (
75 stepUsage: TokenUsage,
76 ): Promise<RecordStepUsageResult | void> => {
77 usage = addUsage(usage, stepUsage);
78 return hostRecordStepUsage?.(stepUsage);
79 };
80
81 try {
82 while (true) {
83 signal.throwIfAborted();
84
85 if (maxSteps !== undefined && maxSteps > 0 && steps >= maxSteps) {
86 throw createMaxStepsExceededError(maxSteps);
87 }
88
89 steps += 1;
90 activeStep = steps;
91 const stepResult = await executeLoopStep({
92 turnId,
93 signal,
94 buildMessages,
95 buildMessagesStrict,
96 dispatchEvent,
97 llm,
98 tools,
99 hooks,
100 log,
101 currentStep: steps,
102 maxRetryAttempts,
103 recordUsage: recordStepUsage,
104 });
105 activeStep = undefined;
106
107 if (stepResult.stopReason === 'tool_use') {
108 continue;
109 }
110
111 const terminalStopReason: LoopTerminalStepStopReason = stepResult.stopReason;

Callers 1

runStepLoopMethod · 0.50

Calls 9

emptyUsageFunction · 0.90
executeLoopStepFunction · 0.90
isAbortErrorFunction · 0.90
isMaxStepsExceededErrorFunction · 0.90
errorMessageFunction · 0.90
dispatchEventFunction · 0.85
makeInterruptedEventFunction · 0.85
throwIfAbortedMethod · 0.80

Tested by

no test coverage detected