@magpie-community/opencode-factory-auth 0.1.49 → 0.1.50
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +16 -0
- package/index.mjs +6 -2
- package/package.json +2 -2
- package/tool-names.mjs +234 -0
package/README.md
CHANGED
|
@@ -170,6 +170,22 @@ from Core or Extra usage. Readings lag, so the last requests before a
|
|
|
170
170
|
window fills still go out; to never spend Extra usage, turn it off in
|
|
171
171
|
Factory.
|
|
172
172
|
|
|
173
|
+
## Tool names Factory refuses
|
|
174
|
+
|
|
175
|
+
Since 2026-11-17 Factory refuses (403 `content_policy_rejected`) any request
|
|
176
|
+
that declares a tool under certain exact names, on every route, and the list
|
|
177
|
+
moves. As of 2026-11-18 it holds Claude Code's `Bash`, `Write`, `Agent` and
|
|
178
|
+
`TaskCreate`/`TaskGet`/`TaskList`/`TaskUpdate`, OpenCode's and pi's `bash`,
|
|
179
|
+
`read`, `edit`, `write`, `glob`, `grep` and `task` (dsh declares six of
|
|
180
|
+
these), `run_shell`, `Shell`, `StrReplace`, and the names first used in their
|
|
181
|
+
place (`BashTool`, `bash_tool`, ...). The plugin declares such a tool under
|
|
182
|
+
droid's own name for the job (`Execute` for `Bash`, `Create` for `Write`,
|
|
183
|
+
`Task` for `Agent`, `Read` for `read`, ...), or, when droid has none or the
|
|
184
|
+
request already declares it, under its own name with a suffix drawn from the
|
|
185
|
+
process (`TaskCreate_3f9a`). It renames the tool's earlier calls and
|
|
186
|
+
`tool_choice` to match and gives the caller its own name back in every reply,
|
|
187
|
+
streamed or not. The lists are in `tool-names.mjs`.
|
|
188
|
+
|
|
173
189
|
## Claude Code through magpie
|
|
174
190
|
|
|
175
191
|
Factory's Anthropic route requires Droid's fixed client preamble and
|
package/index.mjs
CHANGED
|
@@ -7,6 +7,7 @@ import { STATUS_CODES } from "node:http"
|
|
|
7
7
|
import { createHash } from "node:crypto"
|
|
8
8
|
import { readFileSync } from "node:fs"
|
|
9
9
|
import { parseToolJSON, wrapAnthropicTools, unwrapAnthropicTools } from "./anthropic-tools.mjs"
|
|
10
|
+
import { aliasToolNames, restoreToolNames } from "./tool-names.mjs"
|
|
10
11
|
|
|
11
12
|
const PROVIDER = "factory"
|
|
12
13
|
|
|
@@ -2211,12 +2212,15 @@ export const FactoryAuthPlugin = async ({ client, magpie }) => {
|
|
|
2211
2212
|
if (path.includes("/llm/a/")) h.set("X-Api-Key", "placeholder")
|
|
2212
2213
|
// another agent's request opens as droid's does (droidBody)
|
|
2213
2214
|
const anthropic = path.includes("/llm/a/") && (path.endsWith("/messages") || path.endsWith("/messages/count_tokens"))
|
|
2214
|
-
|
|
2215
|
+
// Factory refuses some tool names outright (tool-names.mjs): those
|
|
2216
|
+
// go out under another name and come back as the caller's
|
|
2217
|
+
const named = aliasToolNames(anthropic ? anthropicBody(body) : droidBody(path, body))
|
|
2218
|
+
const adapted = anthropic ? wrapAnthropicTools(named.body) : { body: named.body, names: new Set() }
|
|
2215
2219
|
const out = sanitizeBody(adapted.body)
|
|
2216
2220
|
// the caller's length is of the body it sent, not of this one
|
|
2217
2221
|
if (out !== init?.body) h.delete("content-length")
|
|
2218
2222
|
const res = await fetch(url, { ...init, method: init?.method ?? (input instanceof Request ? input.method : "POST"), headers: h, body: out })
|
|
2219
|
-
return path.endsWith("/messages") ? unwrapAnthropicTools(res, adapted.names) : res
|
|
2223
|
+
return restoreToolNames(path.endsWith("/messages") ? await unwrapAnthropicTools(res, adapted.names) : res, named.aliases)
|
|
2220
2224
|
}
|
|
2221
2225
|
|
|
2222
2226
|
return {
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@magpie-community/opencode-factory-auth",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.50",
|
|
4
4
|
"description": "Sign in to a Factory (Droid) subscription from OpenCode or magpie",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./opencode.mjs",
|
|
7
7
|
"exports": { ".": "./opencode2.mjs" },
|
|
8
|
-
"files": ["index.mjs", "opencode.mjs", "opencode2.mjs", "anthropic-tools.mjs", "client-phrases.json", "README.md"],
|
|
8
|
+
"files": ["index.mjs", "opencode.mjs", "opencode2.mjs", "anthropic-tools.mjs", "tool-names.mjs", "client-phrases.json", "README.md"],
|
|
9
9
|
"keywords": ["opencode", "opencode-plugin", "magpie", "factory", "droid"],
|
|
10
10
|
"license": "MIT",
|
|
11
11
|
"repository": { "type": "git", "url": "git+https://github.com/magpie-community/plugins.git", "directory": "packages/factory" }
|
package/tool-names.mjs
ADDED
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
import { parseToolJSON } from "./anthropic-tools.mjs"
|
|
2
|
+
|
|
3
|
+
// Factory refuses a request whose tool list declares certain names, with a
|
|
4
|
+
// 403 content_policy_rejected, on every route (Anthropic Messages, OpenAI
|
|
5
|
+
// Responses and chat completions) and on both api.factory.ai and
|
|
6
|
+
// prem.factory.ai. Probed one tool at a time with an empty description: only
|
|
7
|
+
// the declared name decides, matched exactly and case-sensitively. A name
|
|
8
|
+
// that appears only in history (tool_use, function_call), in tool_choice or
|
|
9
|
+
// in text is not refused. The list moves: from 2026-11-17 22:30 PST it held
|
|
10
|
+
// Claude Code's, OpenCode's and Codex's names; from 2026-11-18 06:00 UTC it
|
|
11
|
+
// also holds the names first used in their place (BashTool, bash_tool, ...)
|
|
12
|
+
// and Claude Code's task tools, and no longer Codex's exec_command,
|
|
13
|
+
// write_stdin and Shell or NotebookEdit, EnterPlanMode, ExitPlanMode and
|
|
14
|
+
// get_weather. plugins#1291 also saw agent, Shell and StrReplace refused
|
|
15
|
+
// on 2026-11-18 07:04 UTC; they are kept, since a name renamed that Factory
|
|
16
|
+
// takes costs nothing and one missed refuses the whole request.
|
|
17
|
+
export const REFUSED_TOOL_NAMES = new Set([
|
|
18
|
+
"Agent", "Bash", "TaskCreate", "TaskGet", "TaskList", "TaskUpdate", "Write",
|
|
19
|
+
"Shell", "StrReplace",
|
|
20
|
+
"agent", "bash", "edit", "glob", "grep", "read", "run_shell", "task", "write",
|
|
21
|
+
"AgentTool", "BashTool", "WriteTool", "bash_tool", "edit_tool", "read_tool", "write_tool",
|
|
22
|
+
])
|
|
23
|
+
|
|
24
|
+
// DROID_NAMES is the tool of Factory's own client, droid, that does the
|
|
25
|
+
// same job, which Factory takes as declared: a refused tool goes out under
|
|
26
|
+
// it, unless the request already declares that name or another refused
|
|
27
|
+
// tool took it first.
|
|
28
|
+
export const DROID_NAMES = new Map([
|
|
29
|
+
["Bash", "Execute"], ["bash", "Execute"], ["run_shell", "Execute"], ["BashTool", "Execute"], ["bash_tool", "Execute"],
|
|
30
|
+
["Write", "Create"], ["write", "Create"], ["WriteTool", "Create"], ["write_tool", "Create"],
|
|
31
|
+
["Shell", "Execute"],
|
|
32
|
+
["Agent", "Task"], ["agent", "Task"], ["AgentTool", "Task"], ["task", "Task"],
|
|
33
|
+
["read", "Read"], ["read_tool", "Read"],
|
|
34
|
+
["edit", "Edit"], ["edit_tool", "Edit"],
|
|
35
|
+
["glob", "Glob"], ["grep", "Grep"],
|
|
36
|
+
])
|
|
37
|
+
|
|
38
|
+
// A refused tool droid has no name for (Claude Code's TaskCreate, ...), or
|
|
39
|
+
// one whose droid name is taken, goes out as its own name with a suffix
|
|
40
|
+
// drawn from this process (TaskCreate_3f9a): the same throughout the
|
|
41
|
+
// process, so a conversation reads one name and its prompt cache holds, but
|
|
42
|
+
// no fixed name a list could hold.
|
|
43
|
+
const SALT = crypto.randomUUID()
|
|
44
|
+
export function suffixed(name) {
|
|
45
|
+
let h = 0x811c9dc5
|
|
46
|
+
for (const c of SALT + "\0" + name) h = Math.imul(h ^ c.charCodeAt(0), 0x01000193) >>> 0
|
|
47
|
+
return name + "_" + (h & 0xffff).toString(16).padStart(4, "0")
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const isObject = (v) => v != null && typeof v === "object" && !Array.isArray(v)
|
|
51
|
+
|
|
52
|
+
function text(body) {
|
|
53
|
+
if (typeof body === "string") return body
|
|
54
|
+
if (body instanceof ArrayBuffer) return Buffer.from(body).toString("utf8")
|
|
55
|
+
if (ArrayBuffer.isView(body)) return Buffer.from(body.buffer, body.byteOffset, body.byteLength).toString("utf8")
|
|
56
|
+
return null
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// declared lists every place a request declares a tool by name, as
|
|
60
|
+
// [holder, key] pairs: Anthropic's custom tools, OpenAI's function tools
|
|
61
|
+
// (Responses' flat ones and chat completions' nested ones) and Gemini's
|
|
62
|
+
// function declarations. Anthropic's server tools (a type other than
|
|
63
|
+
// custom) keep the names Anthropic gives them.
|
|
64
|
+
function declared(request) {
|
|
65
|
+
const out = []
|
|
66
|
+
for (const tool of Array.isArray(request.tools) ? request.tools : []) {
|
|
67
|
+
if (!isObject(tool)) continue
|
|
68
|
+
if (tool.type === "function" && isObject(tool.function)) out.push([tool.function, "name"])
|
|
69
|
+
else if (typeof tool.name === "string" && (tool.type == null || tool.type === "custom" || tool.type === "function")) out.push([tool, "name"])
|
|
70
|
+
if (Array.isArray(tool.functionDeclarations)) for (const d of tool.functionDeclarations) if (isObject(d)) out.push([d, "name"])
|
|
71
|
+
}
|
|
72
|
+
return out
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// uses lists where a request names a declared tool again: tool_choice,
|
|
76
|
+
// earlier calls in the history and Gemini's function responses and
|
|
77
|
+
// allowed names, so the model reads one name throughout.
|
|
78
|
+
function uses(request) {
|
|
79
|
+
const out = []
|
|
80
|
+
const choice = request.tool_choice
|
|
81
|
+
if (isObject(choice)) {
|
|
82
|
+
if (isObject(choice.function)) out.push([choice.function, "name"])
|
|
83
|
+
else if (typeof choice.name === "string") out.push([choice, "name"])
|
|
84
|
+
// Responses' allowed_tools lists tools as they are declared
|
|
85
|
+
for (const t of Array.isArray(choice.tools) ? choice.tools : []) {
|
|
86
|
+
if (isObject(t?.function)) out.push([t.function, "name"])
|
|
87
|
+
else if (isObject(t) && typeof t.name === "string") out.push([t, "name"])
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
for (const m of Array.isArray(request.messages) ? request.messages : []) {
|
|
91
|
+
if (!isObject(m)) continue
|
|
92
|
+
if (Array.isArray(m.content)) for (const b of m.content) if (isObject(b) && b.type === "tool_use") out.push([b, "name"])
|
|
93
|
+
if (Array.isArray(m.tool_calls)) for (const c of m.tool_calls) if (isObject(c?.function)) out.push([c.function, "name"])
|
|
94
|
+
}
|
|
95
|
+
for (const item of Array.isArray(request.input) ? request.input : []) {
|
|
96
|
+
if (isObject(item) && (item.type === "function_call" || item.type === "custom_tool_call")) out.push([item, "name"])
|
|
97
|
+
}
|
|
98
|
+
for (const c of Array.isArray(request.contents) ? request.contents : []) {
|
|
99
|
+
for (const p of Array.isArray(c?.parts) ? c.parts : []) {
|
|
100
|
+
if (isObject(p?.functionCall)) out.push([p.functionCall, "name"])
|
|
101
|
+
if (isObject(p?.functionResponse)) out.push([p.functionResponse, "name"])
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
const allowed = request.toolConfig?.functionCallingConfig?.allowedFunctionNames
|
|
105
|
+
if (Array.isArray(allowed)) for (let i = 0; i < allowed.length; i++) out.push([allowed, i])
|
|
106
|
+
return out
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// aliasToolNames renames each declared tool Factory refuses, and every
|
|
110
|
+
// later mention of it in the request, to a name it takes: droid's own for
|
|
111
|
+
// the job (Execute for Bash, Create for Write, ...), else a suffixed one,
|
|
112
|
+
// never one the request already declares. It returns the body and the names
|
|
113
|
+
// to restore in the reply (alias → original). A body with nothing to rename
|
|
114
|
+
// goes on byte for byte.
|
|
115
|
+
export function aliasToolNames(body) {
|
|
116
|
+
const aliases = new Map()
|
|
117
|
+
const raw = text(body)
|
|
118
|
+
if (raw == null) return { body, aliases }
|
|
119
|
+
let request
|
|
120
|
+
try { request = parseToolJSON(raw) } catch { return { body, aliases } }
|
|
121
|
+
if (!isObject(request)) return { body, aliases }
|
|
122
|
+
const decl = declared(request)
|
|
123
|
+
if (!decl.some(([h, k]) => REFUSED_TOOL_NAMES.has(h[k]))) return { body, aliases }
|
|
124
|
+
const taken = new Set(decl.map(([h, k]) => h[k]))
|
|
125
|
+
const renamed = new Map()
|
|
126
|
+
for (const [holder, key] of decl) {
|
|
127
|
+
const name = holder[key]
|
|
128
|
+
if (!REFUSED_TOOL_NAMES.has(name)) continue
|
|
129
|
+
if (!renamed.has(name)) {
|
|
130
|
+
let alias = DROID_NAMES.get(name)
|
|
131
|
+
if (alias == null || taken.has(alias)) alias = suffixed(name)
|
|
132
|
+
while (taken.has(alias) || REFUSED_TOOL_NAMES.has(alias)) alias += "_"
|
|
133
|
+
taken.add(alias)
|
|
134
|
+
renamed.set(name, alias)
|
|
135
|
+
aliases.set(alias, name)
|
|
136
|
+
}
|
|
137
|
+
holder[key] = renamed.get(name)
|
|
138
|
+
}
|
|
139
|
+
for (const [holder, key] of uses(request)) if (renamed.has(holder[key])) holder[key] = renamed.get(holder[key])
|
|
140
|
+
return { body: JSON.stringify(request), aliases }
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// restore puts the original names back wherever a reply names a tool: a
|
|
144
|
+
// "name" field holding an alias (Anthropic's tool_use, OpenAI's function
|
|
145
|
+
// calls in Responses and chat completions, Gemini's functionCall). A call's
|
|
146
|
+
// own arguments are the model's data and stay as they are, as they do in a
|
|
147
|
+
// stream, where they arrive as JSON text.
|
|
148
|
+
const ARGUMENTS = new Set(["input", "args", "arguments"])
|
|
149
|
+
function restore(value, aliases) {
|
|
150
|
+
let changed = false
|
|
151
|
+
const walk = (v) => {
|
|
152
|
+
if (Array.isArray(v)) { for (const x of v) walk(x); return }
|
|
153
|
+
if (!isObject(v)) return
|
|
154
|
+
for (const [k, x] of Object.entries(v)) {
|
|
155
|
+
if (k === "name" && typeof x === "string" && aliases.has(x)) { v[k] = aliases.get(x); changed = true }
|
|
156
|
+
else if (!ARGUMENTS.has(k)) walk(x)
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
walk(value)
|
|
160
|
+
return changed
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function headersOf(res) {
|
|
164
|
+
const headers = new Headers(res.headers)
|
|
165
|
+
headers.delete("content-length")
|
|
166
|
+
headers.delete("content-encoding")
|
|
167
|
+
return { status: res.status, statusText: res.statusText, headers }
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function eventStream(body, aliases) {
|
|
171
|
+
const reader = body.getReader()
|
|
172
|
+
// Bytes Factory sends that are not UTF-8 go on as U+FFFD rather than
|
|
173
|
+
// failing the reply.
|
|
174
|
+
const decoder = new TextDecoder("utf-8")
|
|
175
|
+
const encoder = new TextEncoder()
|
|
176
|
+
let pending = "", ended = false
|
|
177
|
+
const frame = (raw) => {
|
|
178
|
+
const lines = raw.split(/\r\n|\n|\r/)
|
|
179
|
+
const data = lines.filter((l) => l.startsWith("data:")).map((l) => l.slice(5).replace(/^ /, "")).join("\n")
|
|
180
|
+
if (!data.includes("\"name\"")) return raw
|
|
181
|
+
let event
|
|
182
|
+
try { event = parseToolJSON(data) } catch { return raw }
|
|
183
|
+
if (!restore(event, aliases)) return raw
|
|
184
|
+
// the event's data lines become one; its other fields and ending stay
|
|
185
|
+
const eol = /\r\n|\n|\r/.exec(raw)?.[0] ?? "\n"
|
|
186
|
+
const ending = /(?:\r\n|\n|\r)+$/.exec(raw)?.[0] ?? ""
|
|
187
|
+
const body = raw.slice(0, raw.length - ending.length).split(/\r\n|\n|\r/)
|
|
188
|
+
let first = true
|
|
189
|
+
const out = []
|
|
190
|
+
for (const l of body) {
|
|
191
|
+
if (!l.startsWith("data:")) out.push(l)
|
|
192
|
+
else if (first) { out.push("data: " + JSON.stringify(event)); first = false }
|
|
193
|
+
}
|
|
194
|
+
return out.join(eol) + ending
|
|
195
|
+
}
|
|
196
|
+
return new ReadableStream({
|
|
197
|
+
async pull(controller) {
|
|
198
|
+
try {
|
|
199
|
+
while (true) {
|
|
200
|
+
const boundary = /\r\n\r\n|\r\n\n|\n\r\n|\n\n|\r\r/.exec(pending)
|
|
201
|
+
if (boundary || (ended && pending)) {
|
|
202
|
+
const end = boundary ? boundary.index + boundary[0].length : pending.length
|
|
203
|
+
const raw = pending.slice(0, end)
|
|
204
|
+
pending = pending.slice(end)
|
|
205
|
+
controller.enqueue(encoder.encode(frame(raw)))
|
|
206
|
+
return
|
|
207
|
+
}
|
|
208
|
+
if (ended) { controller.close(); return }
|
|
209
|
+
const chunk = await reader.read()
|
|
210
|
+
pending += decoder.decode(chunk.value, { stream: !chunk.done })
|
|
211
|
+
ended = chunk.done
|
|
212
|
+
}
|
|
213
|
+
} catch (e) {
|
|
214
|
+
controller.error(e)
|
|
215
|
+
await reader.cancel(e).catch(() => {})
|
|
216
|
+
}
|
|
217
|
+
},
|
|
218
|
+
cancel(reason) { return reader.cancel(reason) },
|
|
219
|
+
})
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// restoreToolNames gives the caller its own tool names back in Factory's
|
|
223
|
+
// reply, streamed or whole. Without aliases, or for a failed or bodiless
|
|
224
|
+
// reply, the response goes on untouched.
|
|
225
|
+
export async function restoreToolNames(res, aliases) {
|
|
226
|
+
if (!aliases.size || !res.ok || !res.body) return res
|
|
227
|
+
const type = res.headers.get("content-type") ?? ""
|
|
228
|
+
if (type.includes("text/event-stream")) return new Response(eventStream(res.body, aliases), headersOf(res))
|
|
229
|
+
if (!type.includes("json")) return res
|
|
230
|
+
const raw = await res.text()
|
|
231
|
+
let value
|
|
232
|
+
try { value = parseToolJSON(raw) } catch { return new Response(raw, headersOf(res)) }
|
|
233
|
+
return new Response(restore(value, aliases) ? JSON.stringify(value) : raw, headersOf(res))
|
|
234
|
+
}
|