diff --git a/packages/zen-free/README.md b/packages/zen-free/README.md index 000a890..666cad1 100644 --- a/packages/zen-free/README.md +++ b/packages/zen-free/README.md @@ -88,8 +88,13 @@ gets a small allowance a day and then `429 FreeUsageLimitError` ("Free models work in OpenCode") until 00:00 UTC, while OpenCode's go through. So a request whose system messages have no such block gets one added after the agent's own prompt (Anthropic `system` and Responses -`instructions` likewise), its working directory said as unknown; a -prompt that has one, as OpenCode's does, goes as it is. +`instructions` likewise); a prompt that has one, as OpenCode's does, goes +as it is. The block added is OpenCode's own, with the working directory +the agent's prompt names (Codex's ``, a `Working directory:` line, +"the current working directory is ...") or the user's home folder when it +names none. A fixed placeholder there is refused: since 2026-10-29 Zen +answers `Working directory: unknown`, which 0.1.15 to 0.1.17 sent, with +that 429 at once. Now and then Zen closes a streamed Chat Completions reply before its `[DONE]`. When the reply has said something and no tool call of it is diff --git a/packages/zen-free/index.test.mjs b/packages/zen-free/index.test.mjs index 1318da4..689b7ea 100644 --- a/packages/zen-free/index.test.mjs +++ b/packages/zen-free/index.test.mjs @@ -4,6 +4,7 @@ import { once } from "node:events"; import { readFileSync } from "node:fs"; import test from "node:test"; import { protocols, declaredTools, toolReply } from "./tool-fixtures.mjs"; +import { addEnvironment } from "./provider.mjs"; const subject = import("./index.mjs").then((module) => ({ default: { server: module.ZenFreePlugin }, @@ -711,3 +712,43 @@ for (const { protocol, path, model } of protocols) { assert.equal(systemText(protocol, sent), own); }); } + +// Zen answers 429 FreeUsageLimitError at once to the block 0.1.15 to +// 0.1.17 added, `Working directory: unknown`, and serves the same request +// with a path there (shanmao_10826 on magpie's Discord, 2026-10-29). The +// block names the directory the agent's prompt names, or the home folder. +test("the environment block names a real directory, never a fixed placeholder", () => { + const home = "/home/someone"; + const added = (protocol, body) => { + addEnvironment(body, protocol, home); + return systemText(protocol, body); + }; + const line = (text) => /Working directory: (.*)/.exec(text)?.[1]; + // none named: the home folder, in OpenCode's own block + let text = added("chat", { messages: [{ role: "user", content: "hi" }] }); + assert.match(text, ENV); + assert.equal(line(text), home); + assert.doesNotMatch(text, /unknown/); + assert.match(text, /^Here is some useful information about the environment you are running in:\n\n/); + assert.match(text, new RegExp(`Workspace root folder: ${home}\\n Is directory a git repo: (yes|no)\\n Platform: ${process.platform}\\n$`)); + // Codex's in its first user message + text = added("responses", { + instructions: "You are Codex.", + input: [{ role: "user", content: [{ type: "input_text", text: "\n /Users/me/app\n" }] }], + }); + assert.equal(line(text), "/Users/me/app"); + // Kimi's sentence, a Windows path, in the system prompt + text = added("chat", { + messages: [ + { role: "system", content: "You are Kimi. The current working directory is `C:\\work\\repo`." }, + { role: "user", content: "hi" }, + ], + }); + assert.equal(line(text.split("")[1]), "C:\\work\\repo"); + // Anthropic: a "Working directory:" line outside an block + text = added("anthropic", { system: "Working directory: /srv/proj.\nBe brief.", messages: [] }); + assert.equal(line(text.split("")[1]), "/srv/proj"); + // a relative or bare word is not a directory + text = added("chat", { messages: [{ role: "system", content: "Working directory: unknown" }, { role: "user", content: "hi" }] }); + assert.equal(line(text.split("")[1]), home); +}); diff --git a/packages/zen-free/package.json b/packages/zen-free/package.json index 333d4af..0da8405 100644 --- a/packages/zen-free/package.json +++ b/packages/zen-free/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-zen-free-auth", - "version": "0.1.17", + "version": "0.1.18", "description": "OpenCode Zen free models through the public credential, with native streaming protocols", "type": "module", "main": "./index.mjs", diff --git a/packages/zen-free/provider.mjs b/packages/zen-free/provider.mjs index 78ec85a..d7cb428 100644 --- a/packages/zen-free/provider.mjs +++ b/packages/zen-free/provider.mjs @@ -1,4 +1,7 @@ import { createHash, randomBytes } from "node:crypto"; +import { existsSync } from "node:fs"; +import { homedir } from "node:os"; +import { join } from "node:path"; import { PROVIDER, SDK, @@ -123,11 +126,26 @@ function normalizeTools(body, protocol) { // say; the same request with the block added is served (tried against // opencode.ai on 2026-10-25, deepseek-v4.1-flash-free and // glm-5.3-flash-free). In a user message or a tool description it doesn't -// count, and `` or `working directory:` don't either. The directory -// isn't known here, and the agent's own prompt says it, so it is told as -// unknown rather than as a path that would mislead the model. -const ENVIRONMENT = "\n Working directory: unknown\n"; +// count, and `` or `working directory:` don't either. +// +// Since 2026-10-29 Zen answers 429 FreeUsageLimitError at once to the +// literal line 0.1.15 to 0.1.17 added, `Working directory: unknown`, while +// the same request with a path there is served (shanmao_10826 on magpie's +// Discord): fixed text that only this plugin sends is its fingerprint, as +// its old User-Agent was. So the block is OpenCode 2.2.0's own +// (packages/core/src/instructions/builtins.ts, without its line about +// OpenCode's temp folder), with the directory the agent's own prompt +// names, as OpenCode names its own, or the user's home folder when the +// prompt names none. const HAS_ENVIRONMENT = /[\s\S]*Working directory:[\s\S]*<\/env>/; +// How agents name their working directory: Codex's ``, Claude Code's +// and others' `Working directory:`, Kimi's "The current working directory +// is `...`". Only an absolute path counts. +const ABSOLUTE = String.raw`((?:~?/|[A-Za-z]:[\\/]|\\\\)[^\s\x60'"<>]*)`; +const STATED_DIRECTORY = [ + new RegExp(String.raw`\s*${ABSOLUTE}\s*`), + new RegExp(String.raw`working directory(?: is)?:?\s+\x60?${ABSOLUTE}`, "i"), +]; function textOf(content) { if (typeof content === "string") return content; @@ -135,17 +153,56 @@ function textOf(content) { return content.map((part) => (typeof part?.text === "string" ? part.text : "")).join("\n"); } -function withEnvironment(content) { - if (typeof content === "string") return content ? `${content}\n\n${ENVIRONMENT}` : ENVIRONMENT; - if (Array.isArray(content)) return [...content, { type: "text", text: ENVIRONMENT }]; - return ENVIRONMENT; -} - const SYSTEM_ROLES = ["system", "developer"]; -export function addEnvironment(body, protocol) { +// The agent's working directory as its own prompt (system messages, +// instructions) or its first user message says it. +export function statedDirectory(body, protocol) { + const messages = protocol === "responses" ? body.input : body.messages; + const texts = [textOf(protocol === "anthropic" ? body.system : body.instructions)]; + if (Array.isArray(messages)) { + for (const message of messages) + if (SYSTEM_ROLES.includes(message?.role)) texts.push(textOf(message.content)); + texts.push(textOf(messages.find((message) => message?.role === "user")?.content)); + } else if (typeof messages === "string") { + texts.push(messages); + } + for (const text of texts) { + for (const pattern of STATED_DIRECTORY) { + const found = pattern.exec(text)?.[1]?.replace(/[.,;:)\]]+$/, ""); + if (found && found !== "/" && found !== "~/") return found; + } + } + return undefined; +} + +// OpenCode's block, said for `directory`. +export function environment(directory) { + let git = false; + try { + git = existsSync(join(directory.replace(/^~(?=\/)/, homedir()), ".git")); + } catch {} + return [ + "Here is some useful information about the environment you are running in:", + "", + ` Working directory: ${directory}`, + ` Workspace root folder: ${directory}`, + ` Is directory a git repo: ${git ? "yes" : "no"}`, + ` Platform: ${process.platform}`, + "", + ].join("\n"); +} + +function withEnvironment(content, block) { + if (typeof content === "string") return content ? `${content}\n\n${block}` : block; + if (Array.isArray(content)) return [...content, { type: "text", text: block }]; + return block; +} + +export function addEnvironment(body, protocol, home = homedir()) { + const block = () => environment(statedDirectory(body, protocol) ?? home); if (protocol === "anthropic") { - if (!HAS_ENVIRONMENT.test(textOf(body.system))) body.system = withEnvironment(body.system); + if (!HAS_ENVIRONMENT.test(textOf(body.system))) body.system = withEnvironment(body.system, block()); return; } const messages = protocol === "chat" ? body.messages : body.input; @@ -155,16 +212,16 @@ export function addEnvironment(body, protocol) { if (system.some((message) => HAS_ENVIRONMENT.test(textOf(message.content)))) return; if (protocol === "responses") { if (!HAS_ENVIRONMENT.test(textOf(body.instructions))) - body.instructions = withEnvironment(body.instructions); + body.instructions = withEnvironment(body.instructions, block()); return; } // Added to the agent's own system message, so the prompt stays one // system message, the same on every turn (its cache holds); one is put // first when the agent sent none. const first = system.find((message) => message.role === "system"); - if (first) first.content = withEnvironment(first.content); - else if (Array.isArray(messages)) messages.unshift({ role: "system", content: ENVIRONMENT }); - else body.messages = [{ role: "system", content: ENVIRONMENT }]; + if (first) first.content = withEnvironment(first.content, block()); + else if (Array.isArray(messages)) messages.unshift({ role: "system", content: block() }); + else body.messages = [{ role: "system", content: block() }]; } const ALPHABET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";