@magpie-community/opencode-factory-auth 0.1.5 → 0.1.8
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 +37 -3
- package/index.mjs +138 -4
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -44,9 +44,9 @@ droid sends opens its system prompt with "You are Droid, an AI software
|
|
|
44
44
|
engineering agent built by Factory.", so another agent's request to
|
|
45
45
|
Factory's OpenAI-shaped API (`/api/llm/o`: GPT and Grok on Responses, the
|
|
46
46
|
open models on chat completions) opens with that line too, the agent's own
|
|
47
|
-
prompt after it.
|
|
48
|
-
|
|
49
|
-
|
|
47
|
+
prompt after it. On Anthropic's Messages (`/api/llm/a`), the plugin adapts
|
|
48
|
+
fixed client metadata while preserving the task instructions and history;
|
|
49
|
+
see [Claude Code through magpie](#claude-code-through-magpie) below.
|
|
50
50
|
|
|
51
51
|
## Models
|
|
52
52
|
|
|
@@ -71,3 +71,37 @@ The list is not included:
|
|
|
71
71
|
magpie plugin add @magpie-community/opencode-factory-auth
|
|
72
72
|
magpie plugin login factory
|
|
73
73
|
```
|
|
74
|
+
|
|
75
|
+
## Claude Code through magpie
|
|
76
|
+
|
|
77
|
+
Factory's Anthropic route requires Droid's fixed client preamble and
|
|
78
|
+
refuses some of Claude Code's fixed environment, model and built-in skill
|
|
79
|
+
metadata. The plugin adapts that metadata while preserving the coding
|
|
80
|
+
instructions, environment values, tool definitions, tool results, images
|
|
81
|
+
and reasoning options. Native Droid requests remain unchanged, and OpenAI
|
|
82
|
+
routes keep their existing request adapter.
|
|
83
|
+
|
|
84
|
+
For the built-in `update-config` skill, the adapter changes only the known
|
|
85
|
+
self-reference "not Claude" to "not the assistant" inside a complete
|
|
86
|
+
generated skill-list reminder. Its configuration instructions, other
|
|
87
|
+
skill descriptions, tools and caller text are preserved.
|
|
88
|
+
|
|
89
|
+
Claude 5 can send runtime metadata in string-content `system` messages
|
|
90
|
+
instead of user-message reminders. The plugin adapts the known environment,
|
|
91
|
+
model and configuration-skill metadata in that shape too, preserving the
|
|
92
|
+
message role, environment values and session instructions.
|
|
93
|
+
|
|
94
|
+
Use magpie's Claude Code integration to select the Factory provider.
|
|
95
|
+
magpie manages the provider-specific client settings, including capability
|
|
96
|
+
and permission configuration. Fields such as `safeguards` and
|
|
97
|
+
`context_management` are forwarded for Factory to validate; the request
|
|
98
|
+
adapter preserves them along with the other request options.
|
|
99
|
+
|
|
100
|
+
The same metadata adaptation applies to `/messages` and
|
|
101
|
+
`/messages/count_tokens`, so token counting sees the prompt used for
|
|
102
|
+
inference. Serving the counting endpoint still depends on the host and
|
|
103
|
+
upstream. Model and feature availability depend on the Factory account,
|
|
104
|
+
organization region and upstream API. Connectivity has been verified with
|
|
105
|
+
Sonnet 4.6, Sonnet 5.5 and Opus 5.5. Two-turn Read tool calls with Claude
|
|
106
|
+
Code's default tool set have been verified with Sonnet 4.6 and Sonnet 5.5,
|
|
107
|
+
including Sonnet 5.5 selected through a routing group.
|
package/index.mjs
CHANGED
|
@@ -245,7 +245,7 @@ const orgRefused = (status, text) => status === 403 && text.toLowerCase().includ
|
|
|
245
245
|
function explain(status, text) {
|
|
246
246
|
if (status !== 403) return ""
|
|
247
247
|
if (orgRefused(status, text)) return "the Factory account's organization changed; remove the account in magpie and sign in to it again"
|
|
248
|
-
return "Factory
|
|
248
|
+
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"
|
|
249
249
|
}
|
|
250
250
|
|
|
251
251
|
// ---- opening as droid ---------------------------------------------------------
|
|
@@ -260,8 +260,8 @@ function explain(status, text) {
|
|
|
260
260
|
// system message first, joined the same way). So another agent's request to
|
|
261
261
|
// /api/llm/o opens with the line too, as the built-in's factoryDroidBody
|
|
262
262
|
// (internal/provider/factory_client.go) has it. Anthropic's Messages
|
|
263
|
-
// (/api/llm/a)
|
|
264
|
-
//
|
|
263
|
+
// (/api/llm/a) uses anthropicBody below to adapt fixed client metadata.
|
|
264
|
+
// A native Droid request goes on byte for byte.
|
|
265
265
|
|
|
266
266
|
const DROID_LINE = "You are Droid, an AI software engineering agent built by Factory."
|
|
267
267
|
|
|
@@ -569,6 +569,139 @@ function bodyModel(body) {
|
|
|
569
569
|
}
|
|
570
570
|
}
|
|
571
571
|
|
|
572
|
+
// Factory's Anthropic route accepts Droid's client preamble. Keep the
|
|
573
|
+
// caller's instructions, tools and history, adapting only fixed client
|
|
574
|
+
// metadata that Factory refuses (including Claude Code's wrappers).
|
|
575
|
+
const CLAUDE_IDENTITIES = new Set([
|
|
576
|
+
"You are Claude Code, Anthropic's official CLI for Claude.",
|
|
577
|
+
"You are Claude Code, Anthropic's official CLI for Claude, running within the Claude Agent SDK.",
|
|
578
|
+
"You are a Claude agent, built on Anthropic's Claude Agent SDK.",
|
|
579
|
+
])
|
|
580
|
+
|
|
581
|
+
// Match a complete generated block, not a pasted reminder followed by a
|
|
582
|
+
// user's question, or an incomplete fragment of one.
|
|
583
|
+
const ENV_REMINDER = /^<system-reminder>\n# Environment\nYou have been invoked in the following environment:[ \t]*\n(?: {1,2}- [^\n]*\n)+<\/system-reminder>$/
|
|
584
|
+
const MODEL_REMINDER = /^<system-reminder>\nYou are powered by the model (?:named )?[^\n<>]+\.\n<\/system-reminder>$/
|
|
585
|
+
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>$/
|
|
586
|
+
// Factory also refuses this fixed self-reference in Claude Code 2.1.287's
|
|
587
|
+
// built-in update-config description. Keep its instructions and the rest
|
|
588
|
+
// of the skill list intact; only the known line in a complete listing changes.
|
|
589
|
+
const CONFIG_SKILL_METADATA = '- update-config: Use this skill to configure the Claude Code harness via settings.json. Automated behaviors ("from now on when X", "each time X", "whenever X", "before/after X") require hooks configured in settings.json - the harness executes these, not Claude, so memory/preferences cannot fulfill them.'
|
|
590
|
+
const CONFIG_SKILL_COMPAT = CONFIG_SKILL_METADATA.replace("not Claude", "not the assistant")
|
|
591
|
+
|
|
592
|
+
// Claude 5 also sends its runtime metadata as a string-content system
|
|
593
|
+
// message, instead of user-message reminders. Preserve that role and all
|
|
594
|
+
// instructions; only the known environment paragraph opens this adapter.
|
|
595
|
+
const SYSTEM_ENV_CONTEXT = /^# Environment\nYou have been invoked in the following environment:[ \t]*\n(?: {1,2}- [^\n]*\n)+(?=\n|$)/
|
|
596
|
+
const SYSTEM_MODEL_CONTEXT = /(^|\n\n)You are powered by the model (?:named )?[^\n<>]+\.(?=\n\n|$)/g
|
|
597
|
+
function systemContext(text) {
|
|
598
|
+
if (!SYSTEM_ENV_CONTEXT.test(text)) return text
|
|
599
|
+
let out = text.replace("# Environment", "# Runtime context")
|
|
600
|
+
.replace("You have been invoked in the following environment:", "The session environment is:")
|
|
601
|
+
.replace(SYSTEM_MODEL_CONTEXT, (paragraph) => paragraph
|
|
602
|
+
.replace("You are powered by the model named", "Current model name:")
|
|
603
|
+
.replace("You are powered by the model", "Current model:")
|
|
604
|
+
.replace("The exact model ID is", "Model ID:")
|
|
605
|
+
.replace("Assistant knowledge cutoff is", "Model knowledge cutoff:"))
|
|
606
|
+
const skillHeader = "\n\nThe following skills are available for use with the Skill tool:\n\n"
|
|
607
|
+
const skills = out.indexOf(skillHeader)
|
|
608
|
+
if (skills >= 0) {
|
|
609
|
+
const start = skills + skillHeader.length - 1
|
|
610
|
+
out = out.slice(0, start) + out.slice(start).replace("\n" + CONFIG_SKILL_METADATA, "\n" + CONFIG_SKILL_COMPAT)
|
|
611
|
+
}
|
|
612
|
+
return out
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
function anthropicBody(body) {
|
|
616
|
+
let request
|
|
617
|
+
try {
|
|
618
|
+
const text = typeof body === "string" ? body
|
|
619
|
+
: body instanceof ArrayBuffer ? Buffer.from(body).toString("utf8")
|
|
620
|
+
: ArrayBuffer.isView(body) ? Buffer.from(body.buffer, body.byteOffset, body.byteLength).toString("utf8") : ""
|
|
621
|
+
request = JSON.parse(text)
|
|
622
|
+
} catch {
|
|
623
|
+
return body
|
|
624
|
+
}
|
|
625
|
+
// As in droidBody, JSON round-tripping uses JavaScript Numbers: integer
|
|
626
|
+
// tokens above 2^53 can lose precision when a body needs adapting. A
|
|
627
|
+
// body that needs no changes is returned byte-for-byte instead.
|
|
628
|
+
if (!request || !Array.isArray(request.messages)) return body
|
|
629
|
+
if (request.system != null && typeof request.system !== "string" && !Array.isArray(request.system)) return body
|
|
630
|
+
if (Array.isArray(request.system) && request.system.some((b) => b?.type === "text" && typeof b.text !== "string")) return body
|
|
631
|
+
|
|
632
|
+
let changed = false
|
|
633
|
+
const system = typeof request.system === "string" ? [{ type: "text", text: request.system }] : request.system ?? []
|
|
634
|
+
if (typeof request.system === "string" && !request.system.startsWith(DROID_LINE)) changed = true
|
|
635
|
+
const kept = []
|
|
636
|
+
for (const block of system) {
|
|
637
|
+
if (block?.type === "text" && block.text.trim() === "") {
|
|
638
|
+
changed = true
|
|
639
|
+
continue
|
|
640
|
+
}
|
|
641
|
+
// Attribution is consumed here; Factory does not strip Anthropic's
|
|
642
|
+
// billing block and requires the Droid identity to be first.
|
|
643
|
+
if (block?.type === "text" && block.text?.startsWith("x-anthropic-billing-header: cc_version=")) {
|
|
644
|
+
changed = true
|
|
645
|
+
continue
|
|
646
|
+
}
|
|
647
|
+
if (block?.type === "text" && CLAUDE_IDENTITIES.has(block.text)) {
|
|
648
|
+
block.text = DROID_LINE
|
|
649
|
+
changed = true
|
|
650
|
+
}
|
|
651
|
+
kept.push(block)
|
|
652
|
+
}
|
|
653
|
+
const identity = kept.findIndex((b) => b?.type === "text" && b.text.startsWith(DROID_LINE))
|
|
654
|
+
if (identity < 0) {
|
|
655
|
+
kept.unshift({ type: "text", text: DROID_LINE })
|
|
656
|
+
changed = true
|
|
657
|
+
} else if (identity > 0) {
|
|
658
|
+
kept.unshift(...kept.splice(identity, 1))
|
|
659
|
+
changed = true
|
|
660
|
+
}
|
|
661
|
+
// Drop only duplicate identity-only blocks. A block that also contains
|
|
662
|
+
// task instructions stays intact.
|
|
663
|
+
for (let i = kept.length - 1; i > 0; i--) {
|
|
664
|
+
if (kept[i]?.type === "text" && kept[i].text === DROID_LINE) {
|
|
665
|
+
kept.splice(i, 1)
|
|
666
|
+
changed = true
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
if (changed) request.system = kept
|
|
670
|
+
|
|
671
|
+
for (const message of request.messages) {
|
|
672
|
+
if (message?.role === "system" && typeof message.content === "string") {
|
|
673
|
+
const adapted = systemContext(message.content)
|
|
674
|
+
if (adapted !== message.content) {
|
|
675
|
+
message.content = adapted
|
|
676
|
+
changed = true
|
|
677
|
+
}
|
|
678
|
+
continue
|
|
679
|
+
}
|
|
680
|
+
if (message?.role !== "user" || !Array.isArray(message.content)) continue
|
|
681
|
+
for (const block of message.content) {
|
|
682
|
+
if (block?.type !== "text" || typeof block.text !== "string") continue
|
|
683
|
+
if (ENV_REMINDER.test(block.text)) {
|
|
684
|
+
block.text = block.text.replace("# Environment", "# Runtime context")
|
|
685
|
+
.replace("You have been invoked in the following environment:", "The session environment is:")
|
|
686
|
+
changed = true
|
|
687
|
+
} else if (MODEL_REMINDER.test(block.text)) {
|
|
688
|
+
block.text = block.text.replace("You are powered by the model named", "Current model name:")
|
|
689
|
+
.replace("You are powered by the model", "Current model:")
|
|
690
|
+
.replace("The exact model ID is", "Model ID:")
|
|
691
|
+
.replace("Assistant knowledge cutoff is", "Model knowledge cutoff:")
|
|
692
|
+
changed = true
|
|
693
|
+
} else if (SKILL_REMINDER.test(block.text)) {
|
|
694
|
+
const adapted = block.text.replace("\n" + CONFIG_SKILL_METADATA, "\n" + CONFIG_SKILL_COMPAT)
|
|
695
|
+
if (adapted !== block.text) {
|
|
696
|
+
block.text = adapted
|
|
697
|
+
changed = true
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
return changed ? JSON.stringify(request) : body
|
|
703
|
+
}
|
|
704
|
+
|
|
572
705
|
export const FactoryAuthPlugin = async ({ client }) => {
|
|
573
706
|
// the session id this process's requests carry
|
|
574
707
|
const session = crypto.randomUUID()
|
|
@@ -806,7 +939,8 @@ export const FactoryAuthPlugin = async ({ client }) => {
|
|
|
806
939
|
// which Anthropic's SDK sends beside the bearer token
|
|
807
940
|
if (path.includes("/llm/a/")) h.set("X-Api-Key", "placeholder")
|
|
808
941
|
// another agent's request opens as droid's does (droidBody)
|
|
809
|
-
const out =
|
|
942
|
+
const out = path.includes("/llm/a/") && (path.endsWith("/messages") || path.endsWith("/messages/count_tokens"))
|
|
943
|
+
? anthropicBody(body) : droidBody(path, body)
|
|
810
944
|
if (out !== body) h.delete("content-length")
|
|
811
945
|
return fetch(url, { ...init, method: init?.method ?? (input instanceof Request ? input.method : "POST"), headers: h, body: out })
|
|
812
946
|
}
|