@magpie-community/opencode-factory-auth 0.1.35 → 0.1.37
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/index.mjs +31 -7
- package/package.json +1 -1
package/index.mjs
CHANGED
|
@@ -403,10 +403,28 @@ async function reconcile(c) {
|
|
|
403
403
|
// "Requested active organization is not accessible by this user".
|
|
404
404
|
const orgRefused = (status, text) => status === 403 && text.toLowerCase().includes("active organization is not accessible")
|
|
405
405
|
|
|
406
|
+
// contentRefused is Factory's usage policy check refusing what the request
|
|
407
|
+
// says, not who sent it: a 403 with code "content_policy_rejected" (droid
|
|
408
|
+
// 0.245.0 tells it apart the same way, and only when Factory sent no
|
|
409
|
+
// x-factory-upstream-error header). Its check reads the whole conversation,
|
|
410
|
+
// so sending the request again, under any org, is refused again.
|
|
411
|
+
function contentRefused(status, text, headers) {
|
|
412
|
+
if (status !== 403 || headers?.get?.("x-factory-upstream-error")) return false
|
|
413
|
+
let v
|
|
414
|
+
try {
|
|
415
|
+
v = JSON.parse(text)
|
|
416
|
+
} catch {
|
|
417
|
+
return false
|
|
418
|
+
}
|
|
419
|
+
const err = v && typeof v === "object" && v.error && typeof v.error === "object" ? v.error : {}
|
|
420
|
+
return v?.code === "content_policy_rejected" || err.code === "content_policy_rejected"
|
|
421
|
+
}
|
|
422
|
+
|
|
406
423
|
// explain is what the user can do about a 403 Factory still answers once
|
|
407
424
|
// the request carries what droid sends.
|
|
408
|
-
function explain(status, text) {
|
|
425
|
+
function explain(status, text, headers) {
|
|
409
426
|
if (status !== 403) return ""
|
|
427
|
+
if (contentRefused(status, text, headers)) return "Factory's usage policy check reads the whole conversation, so this session's requests are refused again until the text that tripped it is gone: start a new session, or remove or reword what the conversation quotes (another agent's system prompt or instructions pasted in a message or a tool's output are the usual cause)"
|
|
410
428
|
if (orgRefused(status, text)) return "the Factory account's organization changed; remove the account in magpie and sign in to it again"
|
|
411
429
|
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"
|
|
412
430
|
}
|
|
@@ -1545,10 +1563,16 @@ function anthropicBody(body) {
|
|
|
1545
1563
|
continue
|
|
1546
1564
|
}
|
|
1547
1565
|
// Attribution is consumed here; Factory does not strip Anthropic's
|
|
1548
|
-
// billing block and requires the Droid identity to be first.
|
|
1549
|
-
|
|
1566
|
+
// billing block and requires the Droid identity to be first. Only the
|
|
1567
|
+
// header's own line goes: a block (or string system) that carries
|
|
1568
|
+
// instructions after it, such as Claude Code's auto-mode classifier
|
|
1569
|
+
// policy, keeps them and its other fields (#302).
|
|
1570
|
+
if (block?.type === "text" && block.text.startsWith("x-anthropic-billing-header: cc_version=")) {
|
|
1550
1571
|
changed = true
|
|
1551
|
-
|
|
1572
|
+
const end = block.text.indexOf("\n")
|
|
1573
|
+
const rest = end < 0 ? "" : block.text.slice(end + 1)
|
|
1574
|
+
if (rest.trim() === "") continue
|
|
1575
|
+
block.text = rest
|
|
1552
1576
|
}
|
|
1553
1577
|
if (block?.type === "text") {
|
|
1554
1578
|
// The gateway may join the identity and instructions into one block.
|
|
@@ -2020,12 +2044,12 @@ export const FactoryAuthPlugin = async ({ client }) => {
|
|
|
2020
2044
|
if (res.status === 413) return answer(await smaller(res))
|
|
2021
2045
|
if (res.status !== 403) return answer(res)
|
|
2022
2046
|
let text = await res.text()
|
|
2023
|
-
if (await mendOrg(res.status, text, onRenew).catch(() => false)) {
|
|
2047
|
+
if (!contentRefused(res.status, text, res.headers) && (await mendOrg(res.status, text, onRenew).catch(() => false))) {
|
|
2024
2048
|
res = await send(input, init, body, onRenew)
|
|
2025
2049
|
if (res.status !== 403) return answer(res)
|
|
2026
2050
|
text = await res.text()
|
|
2027
2051
|
}
|
|
2028
|
-
const why = explain(res.status, text)
|
|
2052
|
+
const why = explain(res.status, text, res.headers)
|
|
2029
2053
|
// the refusal, then what to do about it, as provider.Explain joins them
|
|
2030
2054
|
const ids = upstreamIds(text, res.headers)
|
|
2031
2055
|
const msg = `${vendorError(text, statusLine(res.status))}${ids.said} — ${why}`
|
|
@@ -2044,4 +2068,4 @@ export const FactoryAuthPlugin = async ({ client }) => {
|
|
|
2044
2068
|
}
|
|
2045
2069
|
|
|
2046
2070
|
// for tests
|
|
2047
|
-
export const _internal = { fastBody, FAST_BETA, limitWindows, windowEnd, dollars, vendorError, CORE, droidBody, DROID_LINE, geminiBody, leaveOutAttachments, LEFT_OUT, TOO_LARGE_HINT, MODELS, PROMOTIONS, rateOf, configModels, providerModel, providerModels }
|
|
2071
|
+
export const _internal = { contentRefused, explain, fastBody, FAST_BETA, limitWindows, windowEnd, dollars, vendorError, CORE, droidBody, DROID_LINE, geminiBody, leaveOutAttachments, LEFT_OUT, TOO_LARGE_HINT, MODELS, PROMOTIONS, rateOf, configModels, providerModel, providerModels }
|