@magpie-community/opencode-factory-auth 0.1.33 → 0.1.34
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 +18 -0
- package/index.mjs +52 -8
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -229,6 +229,24 @@ token marker. Changed-file text supplied by hooks, quoted or incomplete
|
|
|
229
229
|
notifications, and notices that omit the snippet are left alone. Model and
|
|
230
230
|
environment metadata after non-startup hooks retain their existing adaptation.
|
|
231
231
|
|
|
232
|
+
Claude Code's instructions reminder names `~/.claude/CLAUDE.md` "(user's
|
|
233
|
+
private global instructions for all projects)", which Factory refuses; in
|
|
234
|
+
the generated reminder it becomes "(global instructions)". Claude Code
|
|
235
|
+
renders that reminder two ways: the first copy opens with "Codebase and user
|
|
236
|
+
instructions are shown below", and a copy re-read after the files changed
|
|
237
|
+
(after compaction, a settings sync and the like) opens with "Instruction
|
|
238
|
+
files were re-read …; these differ from their earlier copies" or "These
|
|
239
|
+
instruction files changed". Both are adapted, in text blocks, string user
|
|
240
|
+
turns and chat completions' joined user text; text after the reminder and
|
|
241
|
+
a user's own mention of the phrase stay as written. A tool result that
|
|
242
|
+
quotes the phrase (reading Claude Code's renderer, say) gets the lossless
|
|
243
|
+
JSON encoding above.
|
|
244
|
+
|
|
245
|
+
When Factory still answers 403, the error magpie and the agent see keeps
|
|
246
|
+
Factory's request id (from `x-request-id`, `request-id` or `cf-ray`, or the
|
|
247
|
+
body) and its error code and type, and the Anthropic-route body carries
|
|
248
|
+
`request_id`, so the refusal can be named to Factory.
|
|
249
|
+
|
|
232
250
|
Use magpie's Claude Code integration to select the Factory provider.
|
|
233
251
|
magpie manages the provider-specific client settings, including capability
|
|
234
252
|
and permission configuration. Fields such as `safeguards` and
|
package/index.mjs
CHANGED
|
@@ -410,6 +410,28 @@ function explain(status, text) {
|
|
|
410
410
|
return "Factory refused the request. magpie adapts fixed client metadata on both OpenAI and Anthropic routes. Factory may still refuse unsupported fields or wrappers; check the upstream error, the organization's model policy, plan and regional provider availability, and compare the same model in Droid"
|
|
411
411
|
}
|
|
412
412
|
|
|
413
|
+
// upstreamIds are what Factory's refusal says to tell it apart when asking
|
|
414
|
+
// Factory about it (plugins#243): the request id from its headers or body,
|
|
415
|
+
// and the error's own code and type. The headers go back as they came too.
|
|
416
|
+
const REQUEST_ID_HEADERS = ["x-request-id", "request-id", "x-amzn-requestid", "cf-ray"]
|
|
417
|
+
function upstreamIds(text, headers) {
|
|
418
|
+
let v
|
|
419
|
+
try {
|
|
420
|
+
v = JSON.parse(text)
|
|
421
|
+
} catch {}
|
|
422
|
+
const err = v && typeof v === "object" && v.error && typeof v.error === "object" ? v.error : {}
|
|
423
|
+
const str = (x) => (typeof x === "string" && /^[\x21-\x7e]{1,128}$/.test(x) ? x : undefined)
|
|
424
|
+
const id = str(v?.request_id) ?? str(v?.requestId) ?? str(err.request_id) ?? str(err.requestId) ??
|
|
425
|
+
REQUEST_ID_HEADERS.map((h) => str(headers?.get?.(h) ?? undefined)).find(Boolean)
|
|
426
|
+
const parts = []
|
|
427
|
+
if (id) parts.push("request id " + id)
|
|
428
|
+
const code = str(err.code) ?? str(v?.code)
|
|
429
|
+
if (code) parts.push("code " + code)
|
|
430
|
+
const type = str(err.type)
|
|
431
|
+
if (type) parts.push("type " + type)
|
|
432
|
+
return { id, said: parts.length ? " (upstream " + parts.join(", ") + ")" : "" }
|
|
433
|
+
}
|
|
434
|
+
|
|
413
435
|
// ---- opening as droid ---------------------------------------------------------
|
|
414
436
|
//
|
|
415
437
|
// Factory takes a subscription's model requests only from Droid (magpie
|
|
@@ -698,12 +720,14 @@ function tooLarge(text, left) {
|
|
|
698
720
|
|
|
699
721
|
// errorReply is an error as the API the request was for words one:
|
|
700
722
|
// Anthropic's on /llm/a/, OpenAI's on the others.
|
|
701
|
-
function errorReply(url, status, type, message, headers = new Headers()) {
|
|
723
|
+
function errorReply(url, status, type, message, headers = new Headers(), requestId) {
|
|
702
724
|
headers.set("content-type", "application/json")
|
|
703
725
|
headers.delete("content-length")
|
|
704
726
|
headers.delete("content-encoding")
|
|
705
727
|
const anthropic = new URL(url).pathname.includes("/llm/a/")
|
|
706
728
|
const body = anthropic ? { type: "error", error: { type, message } } : { error: { message, type, code: null } }
|
|
729
|
+
// Anthropic's error body carries the request id beside the error
|
|
730
|
+
if (anthropic && requestId) body.request_id = requestId
|
|
707
731
|
return new Response(JSON.stringify(body), { status, headers })
|
|
708
732
|
}
|
|
709
733
|
|
|
@@ -1159,8 +1183,24 @@ function systemModelLine(text) {
|
|
|
1159
1183
|
|
|
1160
1184
|
// The reminder carrying CLAUDE.md files names the global one with a phrase
|
|
1161
1185
|
// Factory refuses; only that phrase in the generated reminder changes.
|
|
1162
|
-
|
|
1186
|
+
// Claude Code renders the instructions attachment two ways (n_t in
|
|
1187
|
+
// 2.1.292/2.1.303): the first copy opens with the preamble, and a copy
|
|
1188
|
+
// re-read after the files changed (changed: true, with or without a
|
|
1189
|
+
// reason such as "compaction") opens with its own sentence before the same
|
|
1190
|
+
// "Contents of … (user's private global instructions for all projects):"
|
|
1191
|
+
// lines (plugins#243).
|
|
1192
|
+
const INSTRUCTIONS_REMINDER = /^<system-reminder>\n(?:Codebase and user instructions are shown below|(?:Instruction files were re-read [^\n]+; these differ from their earlier copies|These instruction files changed) \u2014 each replaces its earlier copy, and the instructions preamble above still applies:\nContents of )/
|
|
1163
1193
|
const GLOBAL_INSTRUCTIONS = "(user's private global instructions for all projects)"
|
|
1194
|
+
// Only the reminder itself changes: text joined after its close (a chat
|
|
1195
|
+
// user message is a turn's blocks joined) stays as the user wrote it.
|
|
1196
|
+
function instructionsText(text) {
|
|
1197
|
+
if (!INSTRUCTIONS_REMINDER.test(text)) return text
|
|
1198
|
+
const close = text.indexOf("\n</system-reminder>")
|
|
1199
|
+
const end = close < 0 ? text.length : close + "\n</system-reminder>".length
|
|
1200
|
+
const reminder = text.slice(0, end)
|
|
1201
|
+
if (!reminder.includes(GLOBAL_INSTRUCTIONS)) return text
|
|
1202
|
+
return reminder.replaceAll(GLOBAL_INSTRUCTIONS, "(global instructions)") + text.slice(end)
|
|
1203
|
+
}
|
|
1164
1204
|
|
|
1165
1205
|
// Claude Code (2.1.291 and later) restores skills invoked before compaction
|
|
1166
1206
|
// as one generated reminder. Factory refuses that fixed opening, and the restored
|
|
@@ -1296,9 +1336,10 @@ function quotedToolText(text, prefix = QUOTED_TOOL_PREFIX) {
|
|
|
1296
1336
|
if (![...CLAUDE_IDENTITIES].some((identity) => text.includes(identity)) &&
|
|
1297
1337
|
!text.includes("You have been invoked in the following environment:") &&
|
|
1298
1338
|
!text.includes("x-anthropic-billing-header: cc_version=") &&
|
|
1299
|
-
!text.includes(COMPACT_OPENING)
|
|
1339
|
+
!text.includes(COMPACT_OPENING) &&
|
|
1340
|
+
!text.includes(GLOBAL_INSTRUCTIONS)) return text
|
|
1300
1341
|
let encoded = JSON.stringify(text)
|
|
1301
|
-
for (const phrase of ["You are", "You have", "x-anthropic-billing-header", "system-reminder", COMPACT_OPENING]) {
|
|
1342
|
+
for (const phrase of ["You are", "You have", "x-anthropic-billing-header", "system-reminder", COMPACT_OPENING, GLOBAL_INSTRUCTIONS]) {
|
|
1302
1343
|
encoded = encoded.replaceAll(phrase, "\\u" + phrase.charCodeAt(0).toString(16).padStart(4, "0") + phrase.slice(1))
|
|
1303
1344
|
}
|
|
1304
1345
|
return prefix + encoded
|
|
@@ -1332,7 +1373,8 @@ function systemMessageText(text) {
|
|
|
1332
1373
|
function userBlockText(text) {
|
|
1333
1374
|
const compacted = compactContext(text)
|
|
1334
1375
|
if (compacted !== text) return compacted
|
|
1335
|
-
|
|
1376
|
+
const instructions = instructionsText(text)
|
|
1377
|
+
if (instructions !== text) return instructions
|
|
1336
1378
|
// a system message folded into the user's turn on its way here
|
|
1337
1379
|
if (HOOK_OUTPUT.test(text) || SYSTEM_ENV_CONTEXT.test(text)) return systemContext(text)
|
|
1338
1380
|
if (ENV_REMINDER.test(text)) {
|
|
@@ -1446,7 +1488,8 @@ function anthropicBody(body) {
|
|
|
1446
1488
|
}
|
|
1447
1489
|
if (message?.role !== "user") continue
|
|
1448
1490
|
if (typeof message.content === "string") {
|
|
1449
|
-
const
|
|
1491
|
+
const compacted = compactContext(message.content)
|
|
1492
|
+
const adapted = compacted !== message.content ? compacted : instructionsText(message.content)
|
|
1450
1493
|
if (adapted !== message.content) {
|
|
1451
1494
|
message.content = adapted
|
|
1452
1495
|
changed = true
|
|
@@ -1868,8 +1911,9 @@ export const FactoryAuthPlugin = async ({ client }) => {
|
|
|
1868
1911
|
}
|
|
1869
1912
|
const why = explain(res.status, text)
|
|
1870
1913
|
// the refusal, then what to do about it, as provider.Explain joins them
|
|
1871
|
-
const
|
|
1872
|
-
|
|
1914
|
+
const ids = upstreamIds(text, res.headers)
|
|
1915
|
+
const msg = `${vendorError(text, statusLine(res.status))}${ids.said} — ${why}`
|
|
1916
|
+
return answer(errorReply(url, res.status, "permission_error", msg, new Headers(res.headers), ids.id))
|
|
1873
1917
|
} catch (e) {
|
|
1874
1918
|
// Factory refused to renew the sign-in: the built-in failed the
|
|
1875
1919
|
// request (magpie's 502) and marked the account lapsed
|