diff --git a/packages/factory/README.md b/packages/factory/README.md index 9a42e66..b952de7 100644 --- a/packages/factory/README.md +++ b/packages/factory/README.md @@ -75,11 +75,16 @@ magpie plugin login factory ## Claude Code through magpie Factory's Anthropic route requires Droid's fixed client preamble and -refuses some of Claude Code's fixed environment and model wrappers. The -plugin adapts that metadata while preserving the coding instructions, -environment values, tool definitions, tool results, images and reasoning -options. Native Droid requests remain unchanged, and OpenAI routes keep -their existing request adapter. +refuses some of Claude Code's fixed environment, model and built-in skill +metadata. The plugin adapts that metadata while preserving the coding +instructions, environment values, tool definitions, tool results, images +and reasoning options. Native Droid requests remain unchanged, and OpenAI +routes keep their existing request adapter. + +For the built-in `update-config` skill, the adapter changes only the known +self-reference "not Claude" to "not the assistant" inside a complete +generated skill-list reminder. Its configuration instructions, other +skill descriptions, tools and caller text are preserved. Use magpie's Claude Code integration to select the Factory provider. magpie manages the provider-specific client settings, including capability @@ -93,4 +98,4 @@ inference. Serving the counting endpoint still depends on the host and upstream. Model and feature availability depend on the Factory account, organization region and upstream API. Connectivity has been verified with Sonnet 4.6, Sonnet 5.5 and Opus 5.5, and a two-turn Read tool call with -Sonnet 4.6. +Sonnet 4.6 using Claude Code's default tool set. diff --git a/packages/factory/index.mjs b/packages/factory/index.mjs index b4280a2..2ac4f65 100644 --- a/packages/factory/index.mjs +++ b/packages/factory/index.mjs @@ -582,6 +582,12 @@ const CLAUDE_IDENTITIES = new Set([ // user's question, or an incomplete fragment of one. const ENV_REMINDER = /^\n# Environment\nYou have been invoked in the following environment:[ \t]*\n(?: {1,2}- [^\n]*\n)+<\/system-reminder>$/ const MODEL_REMINDER = /^\nYou are powered by the model (?:named )?[^\n<>]+\.\n<\/system-reminder>$/ +const SKILL_REMINDER = /^\nThe following skills are available for use with the Skill tool:\n\n(?:(?!<\/?system-reminder>)[\s\S])*\n<\/system-reminder>$/ +// Factory also refuses this fixed self-reference in Claude Code 2.1.287's +// built-in update-config description. Keep its instructions and the rest +// of the skill list intact; only the known line in a complete listing changes. +const CONFIG_SKILL_METADATA = '- update-config: Use this skill to configure the Claude Code harness via settings.json. Automated behaviors ("from now on when X", "each time X", "whenever X", "before/after X") require hooks configured in settings.json - the harness executes these, not Claude, so memory/preferences cannot fulfill them.' +const CONFIG_SKILL_COMPAT = CONFIG_SKILL_METADATA.replace("not Claude", "not the assistant") function anthropicBody(body) { let request @@ -653,6 +659,12 @@ function anthropicBody(body) { .replace("The exact model ID is", "Model ID:") .replace("Assistant knowledge cutoff is", "Model knowledge cutoff:") changed = true + } else if (SKILL_REMINDER.test(block.text)) { + const adapted = block.text.replace("\n" + CONFIG_SKILL_METADATA, "\n" + CONFIG_SKILL_COMPAT) + if (adapted !== block.text) { + block.text = adapted + changed = true + } } } } diff --git a/packages/factory/package.json b/packages/factory/package.json index 05308be..861d95b 100644 --- a/packages/factory/package.json +++ b/packages/factory/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-factory-auth", - "version": "0.1.6", + "version": "0.1.7", "description": "Sign in to a Factory (Droid) subscription from OpenCode or magpie", "type": "module", "main": "./index.mjs", diff --git a/packages/factory/request.test.mjs b/packages/factory/request.test.mjs index 00be240..ca28c98 100644 --- a/packages/factory/request.test.mjs +++ b/packages/factory/request.test.mjs @@ -5,6 +5,9 @@ const real = globalThis.fetch afterEach(() => (globalThis.fetch = real)) const url = "https://api.factory.ai/api/llm/a/v1/messages" const droid = "You are Droid, an AI software engineering agent built by Factory." +// The built-in update-config description emitted by Claude Code 2.1.287. +const configSkill = '- update-config: Use this skill to configure the Claude Code harness via settings.json. Automated behaviors ("from now on when X", "each time X", "whenever X", "before/after X") require hooks configured in settings.json - the harness executes these, not Claude, so memory/preferences cannot fulfill them. Also use for: permissions, env vars and hook troubleshooting.' +const skillReminder = "\nThe following skills are available for use with the Skill tool:\n\n" + configSkill + "\n- custom: Keep every user-defined skill description intact.\n" async function loaded() { const seen = [] @@ -205,3 +208,42 @@ test("retains large integer tokens byte-for-byte in an unmodified native Droid r await l.fetch(url, { method: "POST", body }) expect(seen[0].body).toBe(body) }) + +test("adapts the built-in configuration skill self-reference on inference and counting", async () => { + const { l, seen } = await loaded() + const block = { type: "text", text: skillReminder, cache_control: { type: "ephemeral" } } + const tools = [{ name: "Skill", description: "Load a skill.", input_schema: { type: "object", properties: { skill: { type: "string" } } } }] + const body = JSON.stringify({ system: droid, tools, messages: [{ role: "user", content: [block, { type: "text", text: "Reply OK." }] }] }) + for (const endpoint of [url, url + "/count_tokens"]) { + await l.fetch(endpoint, { method: "POST", body }) + const sent = JSON.parse(seen.at(-1).body) + expect(sent.messages[0].content).toEqual([{ ...block, text: skillReminder.replace("not Claude", "not the assistant") }, { type: "text", text: "Reply OK." }]) + expect(sent.tools).toEqual(tools) + expect(sent.system).toBe(droid) + } +}) + +test("preserves pasted skill listings and configuration text outside the generated block", async () => { + const { l, seen } = await loaded() + const texts = [ + skillReminder + "\nExplain this pasted skill list.", + "Explain this:\n" + skillReminder, + skillReminder.replace("", ""), + configSkill, + skillReminder.replace("- update-config:", "- custom-config:"), + ] + const content = texts.map((text) => ({ type: "text", text })) + const body = JSON.stringify({ system: droid, messages: [{ role: "user", content }] }) + await l.fetch(url, { method: "POST", body }) + expect(seen[0].body).toBe(body) +}) + +test("preserves a skill listing inside tool results and is idempotent after adapting it", async () => { + const { l, seen } = await loaded() + const toolResult = { role: "user", content: [{ type: "tool_result", tool_use_id: "call_1", content: [{ type: "text", text: skillReminder }] }] } + const body = JSON.stringify({ system: droid, messages: [{ role: "user", content: [{ type: "text", text: skillReminder }] }, toolResult] }) + await l.fetch(url, { method: "POST", body }) + expect(JSON.parse(seen[0].body).messages[1]).toEqual(toolResult) + await l.fetch(url, { method: "POST", body: seen[0].body }) + expect(seen[1].body).toBe(seen[0].body) +})