From a2684eda6303c28173cad5f9caf604e58f6ae0de Mon Sep 17 00:00:00 2001 From: Pabloader Date: Tue, 14 Apr 2026 20:43:46 +0000 Subject: [PATCH] Chat tools --- src/games/storywriter/utils/prompt.ts | 1 + src/games/storywriter/utils/tools.ts | 31 ++++++++++++++++++--------- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/games/storywriter/utils/prompt.ts b/src/games/storywriter/utils/prompt.ts index b3f6e8c..dbf304d 100644 --- a/src/games/storywriter/utils/prompt.ts +++ b/src/games/storywriter/utils/prompt.ts @@ -409,6 +409,7 @@ namespace Prompt { return { model: model.id, messages: applyVars(formattedMessages), + tools: Tools.getTools([...Tools.CHAT_ONLY_TOOLS]), max_tokens: model.top_provider.max_completion_tokens || 2048, banned_tokens: state.bannedTokens, ...state.generationSettings, diff --git a/src/games/storywriter/utils/tools.ts b/src/games/storywriter/utils/tools.ts index 5ab6461..cab23f0 100644 --- a/src/games/storywriter/utils/tools.ts +++ b/src/games/storywriter/utils/tools.ts @@ -16,7 +16,7 @@ export namespace Tools { const tool = (t: Tool): Tool => t; - const TOOLS: Record = { + const TOOLS = { 'get_character': tool({ handler: async (args, appState) => { if (!appState.currentStory) { @@ -507,17 +507,24 @@ export namespace Tools { }), }; - export function getTools(): LLM.Tool[] { - return Object.entries(TOOLS).map(([key, tool]) => ({ - type: 'function', - function: { - name: key, - description: tool.description, - parameters: tool.parameters, - }, - })); + export type ToolName = keyof typeof TOOLS; + export const isToolName = (name: string): name is ToolName => name in TOOLS; + + export function getTools(only?: ToolName[]): LLM.Tool[] { + return Object.entries(TOOLS) + .filter(([key]) => !only || only.includes(key as ToolName)) + .map(([key, tool]) => ({ + type: 'function', + function: { + name: key, + description: tool.description, + parameters: tool.parameters, + }, + })); } + export const CHAT_ONLY_TOOLS = ['generate_image'] as const; + function parseArg(arg: unknown): unknown { if (typeof arg !== 'string') return arg; @@ -541,6 +548,10 @@ export namespace Tools { const { function: fn } = toolCall; const args = parseArg(fn.arguments); + if (!isToolName(fn.name)) { + return `Unknown tool: ${fn.name}`; + } + const tool = TOOLS[fn.name]; if (!tool) { return `Unknown tool: ${fn.name}`;