Isolate tool rank stage pipeline
This commit is contained in:
@@ -10,7 +10,9 @@ export async function storeToolRankAudit(params: {
|
||||
round: number;
|
||||
startedAt: number;
|
||||
startedAtIso: string;
|
||||
availableTools: string[];
|
||||
selectedTools?: string[];
|
||||
usedRanker?: boolean;
|
||||
error?: unknown;
|
||||
}): Promise<void> {
|
||||
const event: PipelineAuditEvent = {
|
||||
@@ -23,7 +25,16 @@ export async function storeToolRankAudit(params: {
|
||||
model: params.model,
|
||||
details: {
|
||||
round: params.round,
|
||||
availableTools: params.availableTools,
|
||||
selectedTools: params.selectedTools ?? [],
|
||||
usedRanker: params.usedRanker ?? false,
|
||||
toolRankDecision: {
|
||||
provider: params.provider,
|
||||
round: params.round,
|
||||
availableTools: params.availableTools,
|
||||
selectedTools: params.selectedTools ?? [],
|
||||
usedRanker: params.usedRanker ?? false,
|
||||
},
|
||||
},
|
||||
error: params.error instanceof Error ? params.error.message : params.error ? String(params.error) : undefined,
|
||||
};
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
import {Environment} from "../common/environment.js";
|
||||
import {AiProvider} from "../model/ai-provider.js";
|
||||
import type {BoundaryValue} from "../common/boundary-types.js";
|
||||
import type {TelegramStreamMessage} from "./telegram-stream-message.js";
|
||||
import type {RuntimeConfigSnapshot} from "./unified-ai-runner.shared.js";
|
||||
import {filterRankedTools} from "./tool-ranker-pipeline.js";
|
||||
import {ToolRanker} from "./unified-ai-runner.tool-ranker.js";
|
||||
import {storeToolRankAudit} from "./tool-rank-audit.js";
|
||||
import {allToolSchemaNames, toolSchemaNames} from "./tool-schema-utils.js";
|
||||
import type {ToolRanker} from "./unified-ai-runner.tool-ranker.js";
|
||||
|
||||
function latestUserText(messages: readonly { role?: string; content?: unknown }[]): string {
|
||||
for (let i = messages.length - 1; i >= 0; i--) {
|
||||
@@ -35,16 +33,33 @@ export async function runToolRankStage(params: {
|
||||
streamMessage: TelegramStreamMessage;
|
||||
signal: AbortSignal;
|
||||
toolRanker?: ToolRanker;
|
||||
storeAudit?: (params: {
|
||||
streamMessage: TelegramStreamMessage;
|
||||
provider: AiProvider;
|
||||
model: string;
|
||||
round: number;
|
||||
startedAt: number;
|
||||
startedAtIso: string;
|
||||
availableTools: string[];
|
||||
selectedTools?: string[];
|
||||
usedRanker?: boolean;
|
||||
error?: unknown;
|
||||
}) => Promise<void>;
|
||||
}): Promise<{
|
||||
filteredTools: BoundaryValue[];
|
||||
selectedToolNames: string[];
|
||||
usedRanker: boolean;
|
||||
}> {
|
||||
const toolRanker = params.toolRanker ?? new ToolRanker(params.config);
|
||||
const toolRanker = params.toolRanker ?? new (await import("./unified-ai-runner.tool-ranker.js")).ToolRanker(params.config);
|
||||
const startedAt = Date.now();
|
||||
const startedAtIso = new Date().toISOString();
|
||||
const storeAudit = params.storeAudit ?? (await import("./tool-rank-audit.js")).storeToolRankAudit;
|
||||
const filterSelectedTools = (selectedToolNames: readonly string[]): BoundaryValue[] => {
|
||||
const selected = new Set(selectedToolNames);
|
||||
return params.availableTools.filter(tool => toolSchemaNames(tool).some(name => selected.has(name)));
|
||||
};
|
||||
|
||||
params.streamMessage.setStatus(Environment.getSelectingToolsText());
|
||||
params.streamMessage.setStatus("🧩 Выбираю подходящие инструменты...");
|
||||
await params.streamMessage.flush();
|
||||
|
||||
try {
|
||||
@@ -58,31 +73,34 @@ export async function runToolRankStage(params: {
|
||||
|
||||
params.streamMessage.clearStatus();
|
||||
await params.streamMessage.flush();
|
||||
await storeToolRankAudit({
|
||||
await storeAudit({
|
||||
streamMessage: params.streamMessage,
|
||||
provider: params.provider,
|
||||
model: params.model,
|
||||
round: params.round,
|
||||
startedAt,
|
||||
startedAtIso,
|
||||
availableTools: allToolSchemaNames(params.availableTools),
|
||||
selectedTools: selection.toolNames,
|
||||
usedRanker: selection.usedRanker,
|
||||
});
|
||||
|
||||
return {
|
||||
filteredTools: filterRankedTools(params.availableTools, selection.toolNames),
|
||||
filteredTools: filterSelectedTools(selection.toolNames),
|
||||
selectedToolNames: selection.toolNames,
|
||||
usedRanker: selection.usedRanker,
|
||||
};
|
||||
} catch (error) {
|
||||
params.streamMessage.clearStatus();
|
||||
await params.streamMessage.flush();
|
||||
await storeToolRankAudit({
|
||||
await storeAudit({
|
||||
streamMessage: params.streamMessage,
|
||||
provider: params.provider,
|
||||
model: params.model,
|
||||
round: params.round,
|
||||
startedAt,
|
||||
startedAtIso,
|
||||
availableTools: allToolSchemaNames(params.availableTools),
|
||||
error,
|
||||
});
|
||||
throw error;
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
import type {BoundaryValue} from "../common/boundary-types.js";
|
||||
|
||||
function isRecord(value: BoundaryValue): value is Record<string, BoundaryValue> {
|
||||
return value !== null && typeof value === "object" && !Array.isArray(value);
|
||||
}
|
||||
|
||||
function asOptionalString(value: BoundaryValue): string | undefined {
|
||||
return typeof value === "string" && value.trim().length > 0 ? value.trim() : undefined;
|
||||
}
|
||||
|
||||
export function toolSchemaName(tool: BoundaryValue): string | undefined {
|
||||
if (!isRecord(tool)) return undefined;
|
||||
const fn = isRecord(tool.function) ? tool.function : undefined;
|
||||
const directName = fn?.name ?? tool.name ?? (typeof tool.type === "string" && tool.type !== "function" ? tool.type : undefined);
|
||||
return asOptionalString(directName);
|
||||
}
|
||||
|
||||
export function toolSchemaNames(tool: BoundaryValue): string[] {
|
||||
if (!isRecord(tool)) return [];
|
||||
|
||||
if (Array.isArray(tool.functionDeclarations)) {
|
||||
return tool.functionDeclarations
|
||||
.map(declaration => isRecord(declaration) ? asOptionalString(declaration.name) : undefined)
|
||||
.filter((name): name is string => !!name);
|
||||
}
|
||||
|
||||
const name = toolSchemaName(tool);
|
||||
return name ? [name] : [];
|
||||
}
|
||||
|
||||
export function allToolSchemaNames(tools: readonly BoundaryValue[]): string[] {
|
||||
return [...new Set(tools.flatMap(toolSchemaNames))];
|
||||
}
|
||||
@@ -5,7 +5,6 @@ import type {BoundaryValue} from "../common/boundary-types";
|
||||
import {AiProvider} from "../model/ai-provider.js";
|
||||
import {ToolRankerFallbackPolicy} from "../common/policies.js";
|
||||
import {Environment} from "../common/environment.js";
|
||||
import {photoGenDir} from "../index.js";
|
||||
import {delay, logError, replyToMessage} from "../util/utils.js";
|
||||
import {MessageStore} from "../common/message-store.js";
|
||||
import type {OpenAiResponseTool} from "./tool-mappers.js";
|
||||
@@ -72,6 +71,10 @@ export const MAX_OLLAMA_CONTEXT_SIZE = 262144;
|
||||
export const DEFAULT_OLLAMA_CONTEXT_SIZE = 32768;
|
||||
export const toolResourceLocks = new KeyedAsyncLock();
|
||||
|
||||
function photoGenDir(): string {
|
||||
return path.join(Environment.DATA_PATH, "cache", "photo", "gen");
|
||||
}
|
||||
|
||||
export type UnifiedRunOptions = {
|
||||
provider: AiProvider;
|
||||
msg: Message;
|
||||
@@ -1523,7 +1526,7 @@ export function writeOpenAiGeneratedImage(sourceMessage: Message, b64: string, l
|
||||
} {
|
||||
const buffer = Buffer.from(b64, "base64");
|
||||
const fileName = `${sourceMessage.chat.id}_${sourceMessage.message_id}_${Date.now()}_${label}.png`;
|
||||
const cachePath = path.join(photoGenDir, fileName);
|
||||
const cachePath = path.join(photoGenDir(), fileName);
|
||||
fs.writeFileSync(cachePath, buffer);
|
||||
return {buffer, cachePath, fileName};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user