Extract shared tool batch adapter helper

This commit is contained in:
2026-05-18 19:18:22 +03:00
parent 9352ade19f
commit 13df2a1c23
5 changed files with 312 additions and 243 deletions
+2 -2
View File
@@ -82,8 +82,8 @@
- [ ] Stage `model_call` должен делать только один model request.
- [x] Stage `model_call` должен возвращать normalized model output.
- [x] Stage `tool_loop` должен решать, есть ли tool calls.
- [ ] Stage `tool_loop` должен выполнять tools через общий `executeToolBatch`.
- [ ] Stage `tool_loop` должен добавлять tool results в provider adapter.
- [x] Stage `tool_loop` должен выполнять tools через общий `executeToolBatch`.
- [x] Stage `tool_loop` должен добавлять tool results в provider adapter.
- [ ] Stage `tool_loop` должен управлять max rounds.
- [ ] Stage `tool_loop` должен сохранять tool result artifacts.
- [x] Stage `tool_loop` должен уметь завершаться без tools как `skipped`.
+28
View File
@@ -0,0 +1,28 @@
import type {AiProviderAdapter} from "./provider-adapters.js";
import {executeToolBatch, type ToolCallData, type ToolExecutionMemory} from "./unified-ai-runner.shared.js";
import type {TelegramStreamMessage} from "./telegram-stream-message.js";
import type {ToolRuntimeContext} from "./tools/runtime.js";
export async function executeToolBatchWithAdapter(params: {
userId: number | undefined | null;
toolCalls: ToolCallData[];
streamMessage: TelegramStreamMessage;
toolContext: ToolRuntimeContext;
toolMemory: ToolExecutionMemory;
adapter: AiProviderAdapter;
appendTargets?: unknown[][];
}): Promise<string[]> {
const results = await executeToolBatch(
params.userId,
params.toolCalls,
params.streamMessage,
params.toolContext,
params.toolMemory,
);
for (const target of params.appendTargets ?? []) {
params.adapter.appendToolResults(target, params.toolCalls, results);
}
return results;
}
+19 -7
View File
@@ -9,7 +9,6 @@ import {getProviderAdapter} from "./provider-adapters";
import {runToolRankStage} from "./tool-rank-stage";
import {
executeToolBatch,
MAX_TOOL_ROUNDS,
MistralDocumentReference,
roundStatus,
@@ -18,6 +17,7 @@ import {
ToolCallData,
ToolExecutionMemory
} from "./unified-ai-runner.shared";
import {executeToolBatchWithAdapter} from "./tool-batch-runner";
import {Message} from "typescript-telegram-bot-api";
export async function runMistral(
@@ -102,9 +102,15 @@ export async function runMistral(
function: {name: call.name, arguments: call.argumentsText},
})),
});
const toolResults = await executeToolBatch(msg.from?.id, calls, streamMessage, toolContext, toolMemory);
adapter.appendToolResults(messages, calls, toolResults);
adapter.appendToolResults(requestMessages, calls, toolResults);
await executeToolBatchWithAdapter({
userId: msg.from?.id,
toolCalls: calls,
streamMessage,
toolContext,
toolMemory,
adapter,
appendTargets: [messages, requestMessages],
});
continue;
}
@@ -153,9 +159,15 @@ export async function runMistral(
content: roundText,
toolCalls: calls.map(c => ({id: c.id, function: {name: c.name, arguments: c.argumentsText}}))
});
const toolResults = await executeToolBatch(msg.from?.id, calls, streamMessage, toolContext, toolMemory);
adapter.appendToolResults(messages, calls, toolResults);
adapter.appendToolResults(requestMessages, calls, toolResults);
await executeToolBatchWithAdapter({
userId: msg.from?.id,
toolCalls: calls,
streamMessage,
toolContext,
toolMemory,
adapter,
appendTargets: [messages, requestMessages],
});
}
} finally {
await adapter.finalize().catch(() => undefined);
+19 -4
View File
@@ -20,7 +20,6 @@ import {
allToolSchemaNames,
dedupeToolCalls,
DEFAULT_OLLAMA_CONTEXT_SIZE,
executeToolBatch,
isOllamaModelActive,
isRecord,
MAX_OLLAMA_CONTEXT_SIZE,
@@ -33,6 +32,7 @@ import {
ToolCallData,
ToolExecutionMemory
} from "./unified-ai-runner.shared";
import {executeToolBatchWithAdapter} from "./tool-batch-runner";
import {getToolPrompts} from "./tools/registry";
import {GetNoteFileResult, GetNoteFileResultSchema} from "./tools/notes";
import {getModelCapabilities} from "./provider-model-runtime";
@@ -286,7 +286,15 @@ export async function runOllama(
})),
});
adapter.appendToolResults(messages, calls, await executeToolBatch(msg.from?.id, calls, streamMessage, toolContext, toolMemory));
await executeToolBatchWithAdapter({
userId: msg.from?.id,
toolCalls: calls,
streamMessage,
toolContext,
toolMemory,
adapter,
appendTargets: [messages],
});
continue;
}
@@ -396,7 +404,15 @@ export async function runOllama(
})),
});
const toolResults = await executeToolBatch(msg.from?.id, calls, streamMessage, toolContext, toolMemory);
const toolResults = await executeToolBatchWithAdapter({
userId: msg.from?.id,
toolCalls: calls,
streamMessage,
toolContext,
toolMemory,
adapter,
appendTargets: [messages],
});
let successGetNoteFileResult: GetNoteFileResult | undefined = undefined;
@@ -428,7 +444,6 @@ export async function runOllama(
}).catch(logError);
}
adapter.appendToolResults(messages, calls, toolResults);
}
} finally {
if (interval) clearInterval(interval);
+19 -5
View File
@@ -19,7 +19,6 @@ import {
collectOpenAiResponseCodeInterpreterCalls,
collectOpenAiResponseImages,
collectOpenAiResponseText,
executeToolBatch,
MAX_TOOL_ROUNDS,
OPENAI_IMAGE_PARTIALS,
openAiResponseItemCallId,
@@ -33,6 +32,7 @@ import {
errorMessage,
allToolSchemaNames
} from "./unified-ai-runner.shared";
import {executeToolBatchWithAdapter} from "./tool-batch-runner";
import {bot} from "../index";
import fs from "node:fs";
import path from "node:path";
@@ -175,9 +175,16 @@ export async function runOpenAi(
name: call.name,
argumentsText: call.argumentsText,
}));
const toolResults = await executeToolBatch(msg.from?.id, toolCalls, streamMessage, toolContext, toolMemory);
const toolOutputs: Array<{type: "function_call_output"; call_id: string; output: string}> = [];
adapter.appendToolResults(toolOutputs, calls, toolResults);
const toolResults = await executeToolBatchWithAdapter({
userId: msg.from?.id,
toolCalls,
streamMessage,
toolContext,
toolMemory,
adapter,
appendTargets: [toolOutputs],
});
const uploadFilesResult = await tryToUploadFiles(msg, toolResults);
if (uploadFilesResult.found) {
@@ -364,9 +371,16 @@ export async function runOpenAi(
name: call.name,
argumentsText: call.argumentsText,
}));
const toolResults = await executeToolBatch(msg.from?.id, toolCalls, streamMessage, toolContext, toolMemory);
const toolOutputs: Array<{type: "function_call_output"; call_id: string; output: string}> = [];
adapter.appendToolResults(toolOutputs, calls, toolResults);
const toolResults = await executeToolBatchWithAdapter({
userId: msg.from?.id,
toolCalls,
streamMessage,
toolContext,
toolMemory,
adapter,
appendTargets: [toolOutputs],
});
const uploadFilesResult = await tryToUploadFiles(msg, toolResults);
if (uploadFilesResult.found) {