diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..d7a3ca1 --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,5 @@ +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": ["dbaeumer.vscode-eslint", "connor4312.esbuild-problem-matchers", "ms-vscode.extension-test-runner"] +} diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..e3746b7 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,41 @@ +// A launch configuration that compiles the extension and then opens it inside a new window +// Use IntelliSense to learn about possible attributes. +// Hover to view descriptions of existing attributes. +// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 +{ + "version": "0.2.0", + "configurations": [ + { + "name": "Run Extension", + "type": "extensionHost", + "request": "launch", + "args": [ + "--extensionDevelopmentPath=${workspaceFolder}", + "--new-window", + "--disable-extensions", + "--trace-deprecation" + ], + "outFiles": [ + "${workspaceFolder}/dist/**/*.js" + ], + "preLaunchTask": "${defaultBuildTask}" + }, + { + "name": "Run Extension (clean profile)", + "type": "extensionHost", + "request": "launch", + "args": [ + "--extensionDevelopmentPath=${workspaceFolder}", + "--user-data-dir=${workspaceFolder}/.vscode-dev-profile", + "--extensions-dir=${workspaceFolder}/.vscode-dev-extensions", + "--profile-temp", + "--new-window", + "--disable-extensions" + ], + "outFiles": [ + "${workspaceFolder}/dist/**/*.js" + ], + "preLaunchTask": "${defaultBuildTask}" + } + ] +} diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..1bcc8be --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,13 @@ +// Place your settings in this file to overwrite default and user settings. +{ + "files.exclude": { + "out": false, // set this to true to hide the "out" folder with the compiled JS files + "dist": false // set this to true to hide the "dist" folder with the compiled JS files + }, + "search.exclude": { + "out": true, // set this to false to include "out" folder in search results + "dist": true // set this to false to include "dist" folder in search results + }, + // Turn off tsc task auto detection since we have the necessary tasks as npm scripts + "js/ts.tsc.autoDetect": "off" +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..297c96e --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,79 @@ +// See https://go.microsoft.com/fwlink/?LinkId=733558 +// for the documentation about the tasks.json format +{ + "version": "2.0.0", + "tasks": [ + { + "label": "watch", + "dependsOn": [ + "npm: watch:tsc", + "npm: watch:esbuild" + ], + "presentation": { + "reveal": "never" + }, + "group": { + "kind": "build", + "isDefault": true + } + }, + { + "type": "npm", + "script": "watch:esbuild", + "group": "build", + "problemMatcher": { + "owner": "esbuild", + "fileLocation": "absolute", + "pattern": { + "regexp": "^✘ \\[ERROR\\] (.+)\\n\\n\\s+(.+):(\\d+):(\\d+):$", + "message": 1, + "file": 2, + "line": 3, + "column": 4 + }, + "background": { + "activeOnStart": true, + "beginsPattern": "\\[watch\\] build started", + "endsPattern": "\\[watch\\] build finished" + } + }, + "isBackground": true, + "label": "npm: watch:esbuild", + "presentation": { + "group": "watch", + "reveal": "never" + } + }, + { + "type": "npm", + "script": "watch:tsc", + "group": "build", + "problemMatcher": "$tsc-watch", + "isBackground": true, + "label": "npm: watch:tsc", + "presentation": { + "group": "watch", + "reveal": "never" + } + }, + { + "type": "npm", + "script": "watch-tests", + "problemMatcher": "$tsc-watch", + "isBackground": true, + "presentation": { + "reveal": "never", + "group": "watchers" + }, + "group": "build" + }, + { + "label": "tasks: watch-tests", + "dependsOn": [ + "npm: watch", + "npm: watch-tests" + ], + "problemMatcher": [] + } + ] +} diff --git a/src/agent/provider.ts b/src/agent/provider.ts index d6ed377..b993b22 100644 --- a/src/agent/provider.ts +++ b/src/agent/provider.ts @@ -170,28 +170,76 @@ async function* streamWithRetry( } } -/** - * OpenAI's `tool` role content is string-only, so a tool result that carries an - * image is split: the tool message keeps the text, and the image is forwarded - * in a trailing `user` message right after it. - */ -function normalizeOpenAIMessages(messages: WireMessage[]): WireMessage[] { - const out: WireMessage[] = []; - for (const m of messages) { - if (m.role === "tool" && Array.isArray(m.content)) { - const texts = m.content.filter((p): p is Extract => p.type === "text"); - const images = m.content.filter((p): p is Extract => p.type === "image_url"); - out.push({ role: "tool", tool_call_id: m.tool_call_id, content: texts.map((t) => t.text).join("\n") || "(image)" }); - if (images.length) { - out.push({ role: "user", content: images as WireContentPart[] }); - } +/** Drop Anthropic-only `cache_control` and empty text parts (xAI/Grok 400: Empty content block). */ +function stripOpenAIParts(parts: WireContentPart[]): WireContentPart[] { + const out: WireContentPart[] = []; + for (const p of parts) { + if (p.type === "text") { + if (!p.text) continue; + out.push({ type: "text", text: p.text }); } else { - out.push(m); + out.push({ type: "image_url", image_url: p.image_url }); } } return out; } +function openAIContent(content: string | WireContentPart[] | null | undefined): string | WireContentPart[] { + if (content == null) return ""; + if (typeof content === "string") return content; + const parts = stripOpenAIParts(content); + if (!parts.length) return ""; + if (parts.length === 1 && parts[0].type === "text") return parts[0].text; + return parts; +} + +/** + * OpenAI chat shape: string tool content, no Anthropic cache_control, no null/empty + * content blocks. xAI/Grok rejects those with `Empty content block`. + * Tool images → tool text + trailing user image message. + * Returns plain objects (not only WireMessage) so tool-only assistant can omit `content`. + */ +function normalizeOpenAIMessages(messages: WireMessage[]): Record[] { + const out: Record[] = []; + for (const m of messages) { + if (m.role === "tool") { + if (Array.isArray(m.content)) { + const texts = m.content.filter((p): p is Extract => p.type === "text"); + const images = m.content.filter((p): p is Extract => p.type === "image_url"); + out.push({ + role: "tool", + tool_call_id: m.tool_call_id, + content: texts.map((t) => t.text).join("\n") || (images.length ? "(image)" : "(empty)"), + }); + if (images.length) { + out.push({ role: "user", content: stripOpenAIParts(images) }); + } + } else { + out.push({ role: "tool", tool_call_id: m.tool_call_id, content: m.content || "(empty)" }); + } + continue; + } + if (m.role === "assistant") { + const text = (typeof m.content === "string" ? m.content : "") || ""; + const msg: Record = { role: "assistant" }; + // Never send null/empty content — Grok 400 "Empty content block". + if (text) msg.content = text; + else if (!m.tool_calls?.length) msg.content = "(empty)"; + if (m.tool_calls?.length) msg.tool_calls = m.tool_calls; + out.push(msg); + continue; + } + const content = openAIContent(m.content); + if (content === "" || (Array.isArray(content) && content.length === 0)) { + if (m.role === "system") continue; + out.push({ role: "user", content: "(empty)" }); + continue; + } + out.push({ role: m.role, content }); + } + return out; +} + /** Generate a short conversation title from the first user message using the model. */ export async function generateTitle(apiBaseUrl: string, apiKey: string, model: string, userText: string, anthropic?: boolean, oauthKind?: OAuthKind): Promise { const sys = "Generate a concise 3-6 word title for a chat that starts with the user's message. The title must summarize the topic, not repeat the message.";