@magpie-community/opencode-factory-auth 0.1.32 → 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 +26 -3
- package/index.mjs +81 -19
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -128,9 +128,14 @@ The fixed Claude identity is recognized as a complete first line too when
|
|
|
128
128
|
magpie joins it with the following system instructions into one text block.
|
|
129
129
|
|
|
130
130
|
For the built-in `update-config` skill, the adapter changes only the known
|
|
131
|
-
self-reference "not Claude" to "not the assistant" inside
|
|
132
|
-
generated skill
|
|
133
|
-
|
|
131
|
+
self-reference "not Claude" to "not the assistant" inside that skill's entry
|
|
132
|
+
in a generated skill list. Claude Code renders the entry two ways: as a
|
|
133
|
+
system message (`- update-config: …`) and, in the `claude-code-guide` agent's
|
|
134
|
+
own system prompt under `# User's Current Configuration`, with the slash that
|
|
135
|
+
invokes it (`- /update-config: …`). Both spellings are adapted, and only the
|
|
136
|
+
sentence Factory refuses changes, so the entry's name and the rest of the list
|
|
137
|
+
stay. Its configuration instructions, other skill descriptions, tools and
|
|
138
|
+
caller text are preserved.
|
|
134
139
|
|
|
135
140
|
Claude 5 can send runtime metadata in string-content `system` messages
|
|
136
141
|
instead of user-message reminders. The plugin adapts the known environment,
|
|
@@ -224,6 +229,24 @@ token marker. Changed-file text supplied by hooks, quoted or incomplete
|
|
|
224
229
|
notifications, and notices that omit the snippet are left alone. Model and
|
|
225
230
|
environment metadata after non-startup hooks retain their existing adaptation.
|
|
226
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
|
+
|
|
227
250
|
Use magpie's Claude Code integration to select the Factory provider.
|
|
228
251
|
magpie manages the provider-specific client settings, including capability
|
|
229
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
|
|
|
@@ -953,11 +977,29 @@ const CLAUDE_IDENTITIES = new Set([
|
|
|
953
977
|
const ENV_REMINDER = /^<system-reminder>\n# Environment\nYou have been invoked in the following environment:[ \t]*\n(?: {1,2}- [^\n]*\n)+<\/system-reminder>$/
|
|
954
978
|
const MODEL_REMINDER = /^<system-reminder>\nYou are powered by the model (?:named )?[^\n<>]+\.\n<\/system-reminder>$/
|
|
955
979
|
const SKILL_REMINDER = /^<system-reminder>\nThe following skills are available for use with the Skill tool:\n\n(?:(?!<\/?system-reminder>)[\s\S])*\n<\/system-reminder>$/
|
|
956
|
-
// Factory also refuses this fixed self-reference in Claude Code
|
|
957
|
-
//
|
|
958
|
-
//
|
|
959
|
-
|
|
960
|
-
|
|
980
|
+
// Factory also refuses this fixed self-reference in Claude Code's built-in
|
|
981
|
+
// update-config description. Keep its instructions and the rest of the skill
|
|
982
|
+
// list intact; only that entry changes.
|
|
983
|
+
//
|
|
984
|
+
// Claude Code renders the entry two ways: as a system message ("- update-
|
|
985
|
+
// config: …") and, in the claude-code-guide agent's own system prompt under
|
|
986
|
+
// "# User's Current Configuration", with the slash that invokes it
|
|
987
|
+
// ("- /update-config: …"). Match the entry by its name in either spelling and
|
|
988
|
+
// change only the refused sentence in it, so a rewording of the rest of the
|
|
989
|
+
// description cannot defeat the match. The sentence is the built-in
|
|
990
|
+
// description's own text, so no other entry carries it by accident.
|
|
991
|
+
const CONFIG_SKILL_ENTRY = /^- \/?update-config: /
|
|
992
|
+
const CONFIG_SKILL_REFUSED = "the harness executes these, not Claude, so memory/preferences cannot fulfill them."
|
|
993
|
+
const CONFIG_SKILL_SAFE = CONFIG_SKILL_REFUSED.replace("not Claude", "not the assistant")
|
|
994
|
+
|
|
995
|
+
// configSkillText repairs that entry wherever the text carries a skill list.
|
|
996
|
+
function configSkillText(text) {
|
|
997
|
+
if (!text.includes(CONFIG_SKILL_REFUSED)) return text
|
|
998
|
+
return text
|
|
999
|
+
.split("\n")
|
|
1000
|
+
.map((line) => (CONFIG_SKILL_ENTRY.test(line) ? line.replace(CONFIG_SKILL_REFUSED, CONFIG_SKILL_SAFE) : line))
|
|
1001
|
+
.join("\n")
|
|
1002
|
+
}
|
|
961
1003
|
|
|
962
1004
|
// Claude Code also sends the skill list alone as a system message: the
|
|
963
1005
|
// entries only, no header, no <system-reminder> wrapper and no token tail,
|
|
@@ -965,7 +1007,7 @@ const CONFIG_SKILL_COMPAT = CONFIG_SKILL_METADATA.replace("not Claude", "not the
|
|
|
965
1007
|
// list (it opens with an entry) changes, and in it only the known line.
|
|
966
1008
|
function bareSkills(text) {
|
|
967
1009
|
if (!text.startsWith("- ")) return text
|
|
968
|
-
return (
|
|
1010
|
+
return configSkillText(text)
|
|
969
1011
|
}
|
|
970
1012
|
|
|
971
1013
|
// Claude 5 also sends its runtime metadata as a string-content system
|
|
@@ -1018,7 +1060,7 @@ function announcedSkills(text) {
|
|
|
1018
1060
|
const list = parts[i + 1]
|
|
1019
1061
|
// Skill descriptions can contain continuation lines.
|
|
1020
1062
|
if (!list.startsWith("- ")) continue
|
|
1021
|
-
parts[i + 1] = (
|
|
1063
|
+
parts[i + 1] = configSkillText(list)
|
|
1022
1064
|
}
|
|
1023
1065
|
return parts.join("\n\n")
|
|
1024
1066
|
}
|
|
@@ -1069,7 +1111,7 @@ function announcedContext(text, start = 1) {
|
|
|
1069
1111
|
} else if (SYSTEM_MODEL_UPDATE.test(part)) {
|
|
1070
1112
|
parts[i] = systemModelLine(part)
|
|
1071
1113
|
} else if (parts[i - 1] === "The following skills are available for use with the Skill tool:") {
|
|
1072
|
-
parts[i] = (
|
|
1114
|
+
parts[i] = configSkillText(part)
|
|
1073
1115
|
}
|
|
1074
1116
|
}
|
|
1075
1117
|
return parts.join("\n\n")
|
|
@@ -1123,7 +1165,7 @@ function generatedContext(text) {
|
|
|
1123
1165
|
const skills = out.indexOf(skillHeader)
|
|
1124
1166
|
if (skills >= 0) {
|
|
1125
1167
|
const start = skills + skillHeader.length - 1
|
|
1126
|
-
out = out.slice(0, start) + out.slice(start)
|
|
1168
|
+
out = out.slice(0, start) + configSkillText(out.slice(start))
|
|
1127
1169
|
}
|
|
1128
1170
|
return out
|
|
1129
1171
|
}
|
|
@@ -1141,8 +1183,24 @@ function systemModelLine(text) {
|
|
|
1141
1183
|
|
|
1142
1184
|
// The reminder carrying CLAUDE.md files names the global one with a phrase
|
|
1143
1185
|
// Factory refuses; only that phrase in the generated reminder changes.
|
|
1144
|
-
|
|
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 )/
|
|
1145
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
|
+
}
|
|
1146
1204
|
|
|
1147
1205
|
// Claude Code (2.1.291 and later) restores skills invoked before compaction
|
|
1148
1206
|
// as one generated reminder. Factory refuses that fixed opening, and the restored
|
|
@@ -1278,9 +1336,10 @@ function quotedToolText(text, prefix = QUOTED_TOOL_PREFIX) {
|
|
|
1278
1336
|
if (![...CLAUDE_IDENTITIES].some((identity) => text.includes(identity)) &&
|
|
1279
1337
|
!text.includes("You have been invoked in the following environment:") &&
|
|
1280
1338
|
!text.includes("x-anthropic-billing-header: cc_version=") &&
|
|
1281
|
-
!text.includes(COMPACT_OPENING)
|
|
1339
|
+
!text.includes(COMPACT_OPENING) &&
|
|
1340
|
+
!text.includes(GLOBAL_INSTRUCTIONS)) return text
|
|
1282
1341
|
let encoded = JSON.stringify(text)
|
|
1283
|
-
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]) {
|
|
1284
1343
|
encoded = encoded.replaceAll(phrase, "\\u" + phrase.charCodeAt(0).toString(16).padStart(4, "0") + phrase.slice(1))
|
|
1285
1344
|
}
|
|
1286
1345
|
return prefix + encoded
|
|
@@ -1302,7 +1361,7 @@ function claudeIdentity(text) {
|
|
|
1302
1361
|
|
|
1303
1362
|
// promptText adapts a system prompt's text.
|
|
1304
1363
|
function promptText(text) {
|
|
1305
|
-
return clientOpening(systemModelLine(claudeIdentity(text)))
|
|
1364
|
+
return configSkillText(clientOpening(systemModelLine(claudeIdentity(text))))
|
|
1306
1365
|
}
|
|
1307
1366
|
|
|
1308
1367
|
// systemMessageText adapts a system message's text after the prompt.
|
|
@@ -1314,7 +1373,8 @@ function systemMessageText(text) {
|
|
|
1314
1373
|
function userBlockText(text) {
|
|
1315
1374
|
const compacted = compactContext(text)
|
|
1316
1375
|
if (compacted !== text) return compacted
|
|
1317
|
-
|
|
1376
|
+
const instructions = instructionsText(text)
|
|
1377
|
+
if (instructions !== text) return instructions
|
|
1318
1378
|
// a system message folded into the user's turn on its way here
|
|
1319
1379
|
if (HOOK_OUTPUT.test(text) || SYSTEM_ENV_CONTEXT.test(text)) return systemContext(text)
|
|
1320
1380
|
if (ENV_REMINDER.test(text)) {
|
|
@@ -1327,7 +1387,7 @@ function userBlockText(text) {
|
|
|
1327
1387
|
.replace("The exact model ID is", "Model ID:")
|
|
1328
1388
|
.replace("Assistant knowledge cutoff is", "Model knowledge cutoff:")
|
|
1329
1389
|
}
|
|
1330
|
-
if (SKILL_REMINDER.test(text)) return text
|
|
1390
|
+
if (SKILL_REMINDER.test(text)) return configSkillText(text)
|
|
1331
1391
|
return text
|
|
1332
1392
|
}
|
|
1333
1393
|
|
|
@@ -1428,7 +1488,8 @@ function anthropicBody(body) {
|
|
|
1428
1488
|
}
|
|
1429
1489
|
if (message?.role !== "user") continue
|
|
1430
1490
|
if (typeof message.content === "string") {
|
|
1431
|
-
const
|
|
1491
|
+
const compacted = compactContext(message.content)
|
|
1492
|
+
const adapted = compacted !== message.content ? compacted : instructionsText(message.content)
|
|
1432
1493
|
if (adapted !== message.content) {
|
|
1433
1494
|
message.content = adapted
|
|
1434
1495
|
changed = true
|
|
@@ -1850,8 +1911,9 @@ export const FactoryAuthPlugin = async ({ client }) => {
|
|
|
1850
1911
|
}
|
|
1851
1912
|
const why = explain(res.status, text)
|
|
1852
1913
|
// the refusal, then what to do about it, as provider.Explain joins them
|
|
1853
|
-
const
|
|
1854
|
-
|
|
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))
|
|
1855
1917
|
} catch (e) {
|
|
1856
1918
|
// Factory refused to renew the sign-in: the built-in failed the
|
|
1857
1919
|
// request (magpie's 502) and marked the account lapsed
|