From a375b2ffc8dd30cd795f453e3143454f612054f0 Mon Sep 17 00:00:00 2001 From: tsingliu <410869548@qq.com> Date: Sun, 8 Feb 2026 16:03:19 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=8F=90=E7=A4=BA=E8=AF=8D?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=B7=A5=E5=85=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/agent.ts | 1 + src/tools/index.ts | 2 + src/tools/prompt-optimizer.ts | 69 +++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+) create mode 100644 src/tools/prompt-optimizer.ts diff --git a/src/agent.ts b/src/agent.ts index c645f3f..7593138 100644 --- a/src/agent.ts +++ b/src/agent.ts @@ -49,6 +49,7 @@ GUIDELINES: 2. ROBUSTNESS: Use standard Linux/Unix tools found in minimal images (Alpine/Debian). 3. TOOLS: Use 'execute_shell_command' for actions, 'write_file' for code generation. 4. CLARITY: Output concise logs. You are a worker unit, not a chat bot. +5. OPTIMIZATION: When asked to generate creative content (images, stories, complex code), use 'optimize_prompt' first to ensure the best possible output quality. ` } ]; diff --git a/src/tools/index.ts b/src/tools/index.ts index 6c598fb..1333730 100644 --- a/src/tools/index.ts +++ b/src/tools/index.ts @@ -6,6 +6,7 @@ import { NotifyTool } from './notify.js'; import { BrowserTool } from './browser.js'; import { ScreenshotTool } from './screenshot.js'; import { ImageTool } from './image.js'; +import { PromptOptimizerTool } from './prompt-optimizer.js'; // Central Registry of all available tools export const toolRegistry: ToolModule[] = [ @@ -13,6 +14,7 @@ export const toolRegistry: ToolModule[] = [ ReadFileTool, WriteFileTool, DateTimeTool, + PromptOptimizerTool, EmailTool, SearchTool, NotifyTool, diff --git a/src/tools/prompt-optimizer.ts b/src/tools/prompt-optimizer.ts new file mode 100644 index 0000000..da54fb7 --- /dev/null +++ b/src/tools/prompt-optimizer.ts @@ -0,0 +1,69 @@ +import OpenAI from 'openai'; +import { ToolModule } from './interface.js'; + +export const PromptOptimizerTool: ToolModule = { + name: "Prompt Optimizer", + definition: { + type: "function", + function: { + name: "optimize_prompt", + description: "Optimize a user's raw task description or prompt to be more professional, structured, and effective. STRONGLY RECOMMENDED for creative tasks (like image generation) or complex scripts to ensure high-quality results.", + parameters: { + type: "object", + properties: { + raw_prompt: { + type: "string", + description: "The original, raw prompt or task description provided by the user." + }, + context: { + type: "string", + description: "Optional context about the goal, audience, or specific requirements (e.g., 'for an image generator', 'for a code reviewer')." + } + }, + required: ["raw_prompt"] + } + } + }, + handler: async (args: any, config: any) => { + if (!config?.apiKey) { + return "Error: OpenAI API Key is missing in the configuration. Please run 'autoclaw setup' or check your .env file."; + } + + const client = new OpenAI({ + apiKey: config.apiKey, + baseURL: config.baseUrl + }); + + const contextMsg = args.context ? `Context: ${args.context}` : "Context: General AI Assistant interaction."; + + try { + const completion = await client.chat.completions.create({ + model: config.model || 'gpt-4o', + messages: [ + { + role: "system", + content: `You are an expert Prompt Engineer. Your goal is to rewrite the user's raw prompt to be clear, precise, and highly effective for LLMs or professional communication. + +RULES: +1. Preserve the original intent. +2. Structure the prompt logically (e.g., Role, Context, Task, Constraints, Output Format). +3. Use professional and concise language. +4. Return ONLY the optimized prompt. Do not add conversational filler.` + }, + { + role: "user", + content: `Raw Prompt: "${args.raw_prompt}" + +${contextMsg} + +Please optimize this prompt.` + } + ] + }); + + return completion.choices[0].message?.content || "Error: Failed to generate optimized prompt."; + } catch (error: any) { + return `Error optimizing prompt: ${error.message}`; + } + } +};