Extract shared tool batch adapter helper
This commit is contained in:
+2
-2
@@ -82,8 +82,8 @@
|
|||||||
- [ ] Stage `model_call` должен делать только один model request.
|
- [ ] Stage `model_call` должен делать только один model request.
|
||||||
- [x] Stage `model_call` должен возвращать normalized model output.
|
- [x] Stage `model_call` должен возвращать normalized model output.
|
||||||
- [x] Stage `tool_loop` должен решать, есть ли tool calls.
|
- [x] Stage `tool_loop` должен решать, есть ли tool calls.
|
||||||
- [ ] Stage `tool_loop` должен выполнять tools через общий `executeToolBatch`.
|
- [x] Stage `tool_loop` должен выполнять tools через общий `executeToolBatch`.
|
||||||
- [ ] Stage `tool_loop` должен добавлять tool results в provider adapter.
|
- [x] Stage `tool_loop` должен добавлять tool results в provider adapter.
|
||||||
- [ ] Stage `tool_loop` должен управлять max rounds.
|
- [ ] Stage `tool_loop` должен управлять max rounds.
|
||||||
- [ ] Stage `tool_loop` должен сохранять tool result artifacts.
|
- [ ] Stage `tool_loop` должен сохранять tool result artifacts.
|
||||||
- [x] Stage `tool_loop` должен уметь завершаться без tools как `skipped`.
|
- [x] Stage `tool_loop` должен уметь завершаться без tools как `skipped`.
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
@@ -9,7 +9,6 @@ import {getProviderAdapter} from "./provider-adapters";
|
|||||||
import {runToolRankStage} from "./tool-rank-stage";
|
import {runToolRankStage} from "./tool-rank-stage";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
executeToolBatch,
|
|
||||||
MAX_TOOL_ROUNDS,
|
MAX_TOOL_ROUNDS,
|
||||||
MistralDocumentReference,
|
MistralDocumentReference,
|
||||||
roundStatus,
|
roundStatus,
|
||||||
@@ -18,6 +17,7 @@ import {
|
|||||||
ToolCallData,
|
ToolCallData,
|
||||||
ToolExecutionMemory
|
ToolExecutionMemory
|
||||||
} from "./unified-ai-runner.shared";
|
} from "./unified-ai-runner.shared";
|
||||||
|
import {executeToolBatchWithAdapter} from "./tool-batch-runner";
|
||||||
import {Message} from "typescript-telegram-bot-api";
|
import {Message} from "typescript-telegram-bot-api";
|
||||||
|
|
||||||
export async function runMistral(
|
export async function runMistral(
|
||||||
@@ -102,9 +102,15 @@ export async function runMistral(
|
|||||||
function: {name: call.name, arguments: call.argumentsText},
|
function: {name: call.name, arguments: call.argumentsText},
|
||||||
})),
|
})),
|
||||||
});
|
});
|
||||||
const toolResults = await executeToolBatch(msg.from?.id, calls, streamMessage, toolContext, toolMemory);
|
await executeToolBatchWithAdapter({
|
||||||
adapter.appendToolResults(messages, calls, toolResults);
|
userId: msg.from?.id,
|
||||||
adapter.appendToolResults(requestMessages, calls, toolResults);
|
toolCalls: calls,
|
||||||
|
streamMessage,
|
||||||
|
toolContext,
|
||||||
|
toolMemory,
|
||||||
|
adapter,
|
||||||
|
appendTargets: [messages, requestMessages],
|
||||||
|
});
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -153,9 +159,15 @@ export async function runMistral(
|
|||||||
content: roundText,
|
content: roundText,
|
||||||
toolCalls: calls.map(c => ({id: c.id, function: {name: c.name, arguments: c.argumentsText}}))
|
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);
|
await executeToolBatchWithAdapter({
|
||||||
adapter.appendToolResults(messages, calls, toolResults);
|
userId: msg.from?.id,
|
||||||
adapter.appendToolResults(requestMessages, calls, toolResults);
|
toolCalls: calls,
|
||||||
|
streamMessage,
|
||||||
|
toolContext,
|
||||||
|
toolMemory,
|
||||||
|
adapter,
|
||||||
|
appendTargets: [messages, requestMessages],
|
||||||
|
});
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
await adapter.finalize().catch(() => undefined);
|
await adapter.finalize().catch(() => undefined);
|
||||||
|
|||||||
@@ -20,7 +20,6 @@ import {
|
|||||||
allToolSchemaNames,
|
allToolSchemaNames,
|
||||||
dedupeToolCalls,
|
dedupeToolCalls,
|
||||||
DEFAULT_OLLAMA_CONTEXT_SIZE,
|
DEFAULT_OLLAMA_CONTEXT_SIZE,
|
||||||
executeToolBatch,
|
|
||||||
isOllamaModelActive,
|
isOllamaModelActive,
|
||||||
isRecord,
|
isRecord,
|
||||||
MAX_OLLAMA_CONTEXT_SIZE,
|
MAX_OLLAMA_CONTEXT_SIZE,
|
||||||
@@ -33,6 +32,7 @@ import {
|
|||||||
ToolCallData,
|
ToolCallData,
|
||||||
ToolExecutionMemory
|
ToolExecutionMemory
|
||||||
} from "./unified-ai-runner.shared";
|
} from "./unified-ai-runner.shared";
|
||||||
|
import {executeToolBatchWithAdapter} from "./tool-batch-runner";
|
||||||
import {getToolPrompts} from "./tools/registry";
|
import {getToolPrompts} from "./tools/registry";
|
||||||
import {GetNoteFileResult, GetNoteFileResultSchema} from "./tools/notes";
|
import {GetNoteFileResult, GetNoteFileResultSchema} from "./tools/notes";
|
||||||
import {getModelCapabilities} from "./provider-model-runtime";
|
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;
|
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;
|
let successGetNoteFileResult: GetNoteFileResult | undefined = undefined;
|
||||||
|
|
||||||
@@ -428,7 +444,6 @@ export async function runOllama(
|
|||||||
}).catch(logError);
|
}).catch(logError);
|
||||||
}
|
}
|
||||||
|
|
||||||
adapter.appendToolResults(messages, calls, toolResults);
|
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
if (interval) clearInterval(interval);
|
if (interval) clearInterval(interval);
|
||||||
|
|||||||
@@ -19,7 +19,6 @@ import {
|
|||||||
collectOpenAiResponseCodeInterpreterCalls,
|
collectOpenAiResponseCodeInterpreterCalls,
|
||||||
collectOpenAiResponseImages,
|
collectOpenAiResponseImages,
|
||||||
collectOpenAiResponseText,
|
collectOpenAiResponseText,
|
||||||
executeToolBatch,
|
|
||||||
MAX_TOOL_ROUNDS,
|
MAX_TOOL_ROUNDS,
|
||||||
OPENAI_IMAGE_PARTIALS,
|
OPENAI_IMAGE_PARTIALS,
|
||||||
openAiResponseItemCallId,
|
openAiResponseItemCallId,
|
||||||
@@ -33,6 +32,7 @@ import {
|
|||||||
errorMessage,
|
errorMessage,
|
||||||
allToolSchemaNames
|
allToolSchemaNames
|
||||||
} from "./unified-ai-runner.shared";
|
} from "./unified-ai-runner.shared";
|
||||||
|
import {executeToolBatchWithAdapter} from "./tool-batch-runner";
|
||||||
import {bot} from "../index";
|
import {bot} from "../index";
|
||||||
import fs from "node:fs";
|
import fs from "node:fs";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
@@ -175,9 +175,16 @@ export async function runOpenAi(
|
|||||||
name: call.name,
|
name: call.name,
|
||||||
argumentsText: call.argumentsText,
|
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}> = [];
|
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);
|
const uploadFilesResult = await tryToUploadFiles(msg, toolResults);
|
||||||
if (uploadFilesResult.found) {
|
if (uploadFilesResult.found) {
|
||||||
@@ -364,9 +371,16 @@ export async function runOpenAi(
|
|||||||
name: call.name,
|
name: call.name,
|
||||||
argumentsText: call.argumentsText,
|
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}> = [];
|
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);
|
const uploadFilesResult = await tryToUploadFiles(msg, toolResults);
|
||||||
if (uploadFilesResult.found) {
|
if (uploadFilesResult.found) {
|
||||||
|
|||||||
Reference in New Issue
Block a user