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

Function step

packages/kosong/test/fixtures/step.ts:70–146  ·  view source on GitHub ↗
(
  provider: ChatProvider,
  systemPrompt: string,
  toolset: Toolset,
  history: Message[],
  callbacks?: StepCallbacks,
  options?: GenerateOptions,
)

Source from the content-addressed store, hash-verified

68 * @throws {ChatProviderError} (and subtypes) on provider failures.
69 */
70export async function step(
71 provider: ChatProvider,
72 systemPrompt: string,
73 toolset: Toolset,
74 history: Message[],
75 callbacks?: StepCallbacks,
76 options?: GenerateOptions,
77): Promise<StepResult> {
78 const toolCalls: ToolCall[] = [];
79 const toolResultPromises = new Map<string, Promise<ToolResult>>();
80
81 async function onToolCall(toolCall: ToolCall): Promise<void> {
82 toolCalls.push(toolCall);
83
84 const handleResult = toolset.handle(toolCall);
85
86 // Normalise to a Promise regardless of whether handle() returned sync.
87 const promise: Promise<ToolResult> =
88 handleResult instanceof Promise ? handleResult : Promise.resolve(handleResult);
89
90 // When the promise resolves, fire the onToolResult callback.
91 const tracked = promise.then((result) => {
92 if (callbacks?.onToolResult !== undefined) {
93 callbacks.onToolResult(result);
94 }
95 return result;
96 });
97
98 toolResultPromises.set(toolCall.id, tracked);
99 void tracked.catch(() => {});
100 }
101
102 let result: GenerateResult;
103 try {
104 const generateCallbacks: GenerateCallbacks = { onToolCall };
105 if (callbacks?.onMessagePart !== undefined) {
106 generateCallbacks.onMessagePart = callbacks.onMessagePart;
107 }
108 result = await generate(
109 provider,
110 systemPrompt,
111 toolset.tools,
112 history,
113 generateCallbacks,
114 options,
115 );
116 } catch (error: unknown) {
117 // On provider or cancellation errors, cancel/await all pending tool
118 // result promises to avoid dangling work.
119 await cleanupPromises(toolResultPromises);
120 throw error;
121 }
122
123 return {
124 id: result.id,
125 message: result.message,
126 usage: result.usage,
127 finishReason: result.finishReason,

Calls 2

generateFunction · 0.90
cleanupPromisesFunction · 0.85

Tested by 2

runAgentLoopFunction · 0.72
runTwoStepLoopFunction · 0.72