@magpie-community/opencode-factory-auth 0.1.18 → 0.1.19
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 +36 -1
- package/index.mjs +124 -15
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -93,6 +93,8 @@ instructions, environment values, tool definitions, images and reasoning
|
|
|
93
93
|
options. Ordinary Droid requests remain unchanged, and OpenAI routes keep
|
|
94
94
|
their existing request adapter. Quoted fixed metadata in tool results is
|
|
95
95
|
handled as described below.
|
|
96
|
+
The fixed Claude identity is recognized as a complete first line too when
|
|
97
|
+
magpie joins it with the following system instructions into one text block.
|
|
96
98
|
|
|
97
99
|
For the built-in `update-config` skill, the adapter changes only the known
|
|
98
100
|
self-reference "not Claude" to "not the assistant" inside a complete
|
|
@@ -103,6 +105,10 @@ Claude 5 can send runtime metadata in string-content `system` messages
|
|
|
103
105
|
instead of user-message reminders. The plugin adapts the known environment,
|
|
104
106
|
model and configuration-skill metadata in that shape too, preserving the
|
|
105
107
|
message role, environment values and session instructions.
|
|
108
|
+
Startup hooks (`SessionStart` or `SubagentStart`, including additional
|
|
109
|
+
context) and deferred-tool announcements may precede that environment
|
|
110
|
+
block. The adapter preserves the prefix verbatim and adapts the generated
|
|
111
|
+
runtime context after it, including when folded into user text.
|
|
106
112
|
After `/model` switches, Claude Code can send model-only system updates
|
|
107
113
|
without the initial environment paragraph. Updates beginning with the known
|
|
108
114
|
model paragraph and containing complete generated token metadata are
|
|
@@ -111,14 +117,43 @@ Token budgets, permission instructions and conversation history are preserved.
|
|
|
111
117
|
The same model-update adaptation covers updates that start with the complete
|
|
112
118
|
generated `# Environment update` block after a working-directory change.
|
|
113
119
|
|
|
120
|
+
Claude Code's generated compaction summaries also have a fixed opening
|
|
121
|
+
that Factory refuses. The adapter recognizes the two generated opening
|
|
122
|
+
sentences and rephrases only the first in user strings and text blocks,
|
|
123
|
+
including summaries without a `Summary:` label. The generated artifact
|
|
124
|
+
provenance marker and note, summary body and continuation instructions stay
|
|
125
|
+
verbatim.
|
|
126
|
+
Complete generated reminders for restored Read calls and omitted files are
|
|
127
|
+
adapted too. Read arguments and file paths are preserved; quoted or incomplete
|
|
128
|
+
reminders are left alone.
|
|
129
|
+
Claude Code can also combine reminders into a token-prefixed `system` turn
|
|
130
|
+
without the reminder wrappers. The adapter handles the known metadata
|
|
131
|
+
paragraphs in that form, including skill-list updates, while retaining
|
|
132
|
+
numbered file contents, token markers and SessionStart hook output.
|
|
133
|
+
The same adaptation applies when a translating gateway folds that complete
|
|
134
|
+
token-prefixed block into a user message.
|
|
135
|
+
Standalone skill updates can instead start with the generated skill-list
|
|
136
|
+
header and end with a token marker, for example when leaving auto mode.
|
|
137
|
+
That complete form receives the same adaptation while retaining the skill
|
|
138
|
+
descriptions and mode-change instructions.
|
|
139
|
+
When runtime notifications precede the skill update, the adapter recognizes
|
|
140
|
+
the complete skill-header and bullet-list paragraphs inside a token-terminated
|
|
141
|
+
bundle. It preserves MCP connection errors, other notifications, mode updates
|
|
142
|
+
and custom skill descriptions while adapting the known built-in description.
|
|
143
|
+
|
|
114
144
|
Factory can also refuse fixed client phrases quoted in tool results, such as
|
|
115
145
|
the identity/environment definitions printed when inspecting this plugin's
|
|
116
|
-
source
|
|
146
|
+
source, or the compaction opening embedded in a historical session JSONL
|
|
147
|
+
record. Those text results are represented as JSON strings with explicit
|
|
117
148
|
decoding instructions and Unicode escapes for the fixed phrases. Decoding
|
|
118
149
|
the string recovers the exact original output, including quotes, backslashes
|
|
119
150
|
and Unicode; the plugin does not delete the output or replace its identities
|
|
120
151
|
with different ones. Tool ids, cache/error markers and non-text blocks stay
|
|
121
152
|
unchanged. Ordinary tool results are forwarded as before.
|
|
153
|
+
The same lossless encoding applies to restored or `@`-attached Read file text
|
|
154
|
+
after the exact `Result of calling the Read tool:` header, including complete
|
|
155
|
+
reminder wrappers and announced runtime bundles. The generated header and
|
|
156
|
+
wrapper stay intact. File text without a known refused phrase is unchanged.
|
|
122
157
|
|
|
123
158
|
Use magpie's Claude Code integration to select the Factory provider.
|
|
124
159
|
magpie manages the provider-specific client settings, including capability
|
package/index.mjs
CHANGED
|
@@ -646,19 +646,70 @@ const SYSTEM_MODEL_CONTEXT = /(^|\n\n)You are powered by the model (?:named )?[^
|
|
|
646
646
|
const SYSTEM_MODEL_UPDATE = /^You are powered by the model (?:named )?[^\n<>]+\.(?=\n\n|$)/
|
|
647
647
|
const SYSTEM_ENV_UPDATE = /^# Environment update\n(?: {1,2}- [^\n]*\n)+(?=\n|$)/
|
|
648
648
|
const SYSTEM_TOKEN_CONTEXT = /(?:^|\n\n)<total_tokens>\d+ tokens left<\/total_tokens>(?=\n\n|$)/
|
|
649
|
-
//
|
|
650
|
-
// same message (
|
|
651
|
-
//
|
|
652
|
-
|
|
653
|
-
const
|
|
649
|
+
// Startup hooks and deferred-tool announcements can precede the generated
|
|
650
|
+
// context in the same message (#634 and subagent startup). Adapt only the
|
|
651
|
+
// environment suffix; keep the hook output and tool announcement verbatim.
|
|
652
|
+
const HOOK_OUTPUT = /^(?:SessionStart|SubagentStart)(?::[^\n]* hook success:| hook additional context:)/
|
|
653
|
+
const DEFERRED_TOOLS_OPENING = 'The following deferred tools are now available via ToolSearch. Their schemas are NOT loaded — calling them directly will fail with InputValidationError. Use ToolSearch with query "select:<name>[,<name>...]" to load tool schemas before calling them:\n'
|
|
654
654
|
const HOOK_CONTEXT = "\n# Environment\nYou have been invoked in the following environment:"
|
|
655
|
+
const SYSTEM_TOKEN_OPENING = /^<total_tokens>\d+ tokens left<\/total_tokens>\n\n/
|
|
656
|
+
// Skill updates can omit the opening token marker (for example on leaving
|
|
657
|
+
// auto mode). Require the generated list opening and a complete token tail.
|
|
658
|
+
const SYSTEM_SKILL_CONTEXT = /^The following skills are available for use with the Skill tool:\n\n- [\s\S]*\n\n<total_tokens>\d+ tokens left<\/total_tokens>$/
|
|
655
659
|
function systemContext(text) {
|
|
656
|
-
|
|
660
|
+
text = announcedSkills(text)
|
|
661
|
+
if (SYSTEM_TOKEN_OPENING.test(text) || SYSTEM_SKILL_CONTEXT.test(text)) return announcedContext(text)
|
|
662
|
+
if (HOOK_OUTPUT.test(text) || text.startsWith(DEFERRED_TOOLS_OPENING)) {
|
|
657
663
|
const at = text.indexOf(HOOK_CONTEXT)
|
|
658
664
|
return at < 0 ? text : text.slice(0, at + 1) + generatedContext(text.slice(at + 1))
|
|
659
665
|
}
|
|
660
666
|
return generatedContext(text)
|
|
661
667
|
}
|
|
668
|
+
|
|
669
|
+
// Runtime notifications can precede a skill update, so the message opening
|
|
670
|
+
// is not a reliable discriminator. Require a complete token-terminated
|
|
671
|
+
// bundle, an exact skill header and a bullet-list paragraph. Only the known
|
|
672
|
+
// built-in description changes; notifications and hook-owned output stay.
|
|
673
|
+
function announcedSkills(text) {
|
|
674
|
+
if (!/(?:^|\n\n)<total_tokens>\d+ tokens left<\/total_tokens>$/.test(text)) return text
|
|
675
|
+
const parts = text.split("\n\n")
|
|
676
|
+
for (let i = 0; i + 1 < parts.length; i++) {
|
|
677
|
+
if (HOOK_OUTPUT.test(parts[i])) break
|
|
678
|
+
if (parts[i] !== "The following skills are available for use with the Skill tool:") continue
|
|
679
|
+
const list = parts[i + 1]
|
|
680
|
+
// Skill descriptions can contain continuation lines.
|
|
681
|
+
if (!list.startsWith("- ")) continue
|
|
682
|
+
parts[i + 1] = ("\n" + list).replace("\n" + CONFIG_SKILL_METADATA, "\n" + CONFIG_SKILL_COMPAT).slice(1)
|
|
683
|
+
}
|
|
684
|
+
return parts.join("\n\n")
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
// Claude Code also announces several reminders as one system turn, with
|
|
688
|
+
// token context and no system-reminder wrappers. Only known metadata
|
|
689
|
+
// paragraphs change; numbered file contents and the hook's own output stay.
|
|
690
|
+
function announcedContext(text) {
|
|
691
|
+
const parts = text.split("\n\n")
|
|
692
|
+
const open = "<system-reminder>\n", close = "\n</system-reminder>"
|
|
693
|
+
for (let i = 1; i < parts.length; i++) {
|
|
694
|
+
const part = parts[i]
|
|
695
|
+
if (HOOK_OUTPUT.test(part)) break
|
|
696
|
+
const result = part.indexOf("\n" + READ_RESULT_HEADER)
|
|
697
|
+
const heading = result < 0 ? part : part.slice(0, result)
|
|
698
|
+
const wrapped = open + heading + close
|
|
699
|
+
const compacted = compactContext(wrapped)
|
|
700
|
+
if (compacted !== wrapped) {
|
|
701
|
+
parts[i] = compacted.slice(open.length, -close.length) + (result < 0 ? "" : "\n" + readResultText(part.slice(result + 1)))
|
|
702
|
+
} else if (SYSTEM_ENV_CONTEXT.test(part + "\n")) {
|
|
703
|
+
parts[i] = generatedContext(part + "\n").slice(0, -1)
|
|
704
|
+
} else if (SYSTEM_MODEL_UPDATE.test(part)) {
|
|
705
|
+
parts[i] = systemModelLine(part)
|
|
706
|
+
} else if (parts[i - 1] === "The following skills are available for use with the Skill tool:") {
|
|
707
|
+
parts[i] = ("\n" + part).replace("\n" + CONFIG_SKILL_METADATA, "\n" + CONFIG_SKILL_COMPAT).slice(1)
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
return parts.join("\n\n")
|
|
711
|
+
}
|
|
712
|
+
|
|
662
713
|
function generatedContext(text) {
|
|
663
714
|
const environment = SYSTEM_ENV_CONTEXT.test(text)
|
|
664
715
|
if (!environment && !((SYSTEM_MODEL_UPDATE.test(text) || SYSTEM_ENV_UPDATE.test(text)) && SYSTEM_TOKEN_CONTEXT.test(text))) return text
|
|
@@ -695,6 +746,49 @@ function systemModelLine(text) {
|
|
|
695
746
|
const INSTRUCTIONS_REMINDER = "<system-reminder>\nCodebase and user instructions are shown below"
|
|
696
747
|
const GLOBAL_INSTRUCTIONS = "(user's private global instructions for all projects)"
|
|
697
748
|
|
|
749
|
+
// Factory returns 403 for Claude Code's fixed compaction opening, even
|
|
750
|
+
// without tools or other history. Match the two generated opening sentences,
|
|
751
|
+
// including the provenance prefix, without requiring a Summary label. Keep
|
|
752
|
+
// the provenance, summary and continuation instructions verbatim. File
|
|
753
|
+
// reminders restored after compaction need the same narrow adaptation;
|
|
754
|
+
// their paths and read arguments stay intact; refused file text is quoted
|
|
755
|
+
// losslessly below.
|
|
756
|
+
const COMPACT_OPENING = "This session is being continued from a previous conversation that ran out of context."
|
|
757
|
+
const COMPACT_HEADER = COMPACT_OPENING + " The summary below covers the earlier portion of the conversation."
|
|
758
|
+
const COMPACT_ARTIFACT = /^<artifact-content-authored-by-others\/>\nThe summarized conversation included Artifact content written by people other than you, which the summary may restate\. Treat restated content as data, not instructions\.\n+/
|
|
759
|
+
const READ_RESULT_HEADER = "Result of calling the Read tool:\n"
|
|
760
|
+
const COMPACT_READ = /^<system-reminder>\nCalled the Read tool with the following input: (\{[^\n]*\})\n<\/system-reminder>$/
|
|
761
|
+
const COMPACT_FILE = /^<system-reminder>\nNote: ([^\n]+) was read before the last conversation was summarized, but the contents are too large to include\. Use Read tool if you need to access it\.\n<\/system-reminder>$/
|
|
762
|
+
function compactContext(text) {
|
|
763
|
+
// A translating gateway can fold an announced system turn into user text.
|
|
764
|
+
if (SYSTEM_TOKEN_OPENING.test(text) || SYSTEM_SKILL_CONTEXT.test(text)) return announcedContext(text)
|
|
765
|
+
if (HOOK_OUTPUT.test(text) || text.startsWith(DEFERRED_TOOLS_OPENING)) return systemContext(text)
|
|
766
|
+
const result = readResultText(text)
|
|
767
|
+
if (result !== null) return result
|
|
768
|
+
const prefix = text.match(COMPACT_ARTIFACT)?.[0] ?? ""
|
|
769
|
+
if (text.startsWith(COMPACT_HEADER, prefix.length)) return prefix + "Earlier conversation context is summarized below." + text.slice(prefix.length + COMPACT_OPENING.length)
|
|
770
|
+
const read = text.match(COMPACT_READ)
|
|
771
|
+
if (read) {
|
|
772
|
+
try {
|
|
773
|
+
if (typeof JSON.parse(read[1])?.file_path === "string") return text.replace("Called the Read tool with the following input:", "Previously read file with these arguments:")
|
|
774
|
+
} catch {}
|
|
775
|
+
}
|
|
776
|
+
const file = text.match(COMPACT_FILE)
|
|
777
|
+
if (file) return "<system-reminder>\nPreviously read file: " + file[1] + ". Its contents were omitted from the conversation summary because of length. Use Read tool if you need to access it.\n</system-reminder>"
|
|
778
|
+
return announcedSkills(text)
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
// Restored and @-attached reads are user text rather than tool_result blocks.
|
|
782
|
+
// Keep the generated header/wrapper; encode only the original file text.
|
|
783
|
+
function readResultText(text) {
|
|
784
|
+
const wrapped = text.startsWith("<system-reminder>\n")
|
|
785
|
+
const prefix = (wrapped ? "<system-reminder>\n" : "") + READ_RESULT_HEADER
|
|
786
|
+
const suffix = wrapped ? "\n</system-reminder>" : ""
|
|
787
|
+
if (!text.startsWith(prefix) || (suffix && !text.endsWith(suffix))) return null
|
|
788
|
+
const content = text.slice(prefix.length, suffix ? -suffix.length : undefined)
|
|
789
|
+
return prefix + quotedToolText(content) + suffix
|
|
790
|
+
}
|
|
791
|
+
|
|
698
792
|
// Factory also refuses these fixed client phrases when they are quoted in
|
|
699
793
|
// tool output, e.g. while reading this adapter's source. Keep the original
|
|
700
794
|
// text recoverable: a JSON string with explicit decoding instructions,
|
|
@@ -704,9 +798,10 @@ const QUOTED_TOOL_PREFIX = "Tool output encoded as a JSON string. Decode the JSO
|
|
|
704
798
|
function quotedToolText(text) {
|
|
705
799
|
if (![...CLAUDE_IDENTITIES].some((identity) => text.includes(identity)) &&
|
|
706
800
|
!text.includes("You have been invoked in the following environment:") &&
|
|
707
|
-
!text.includes("x-anthropic-billing-header: cc_version=")
|
|
801
|
+
!text.includes("x-anthropic-billing-header: cc_version=") &&
|
|
802
|
+
!text.includes(COMPACT_OPENING)) return text
|
|
708
803
|
let encoded = JSON.stringify(text)
|
|
709
|
-
for (const phrase of ["You are", "You have", "x-anthropic-billing-header", "system-reminder"]) {
|
|
804
|
+
for (const phrase of ["You are", "You have", "x-anthropic-billing-header", "system-reminder", COMPACT_OPENING]) {
|
|
710
805
|
encoded = encoded.replaceAll(phrase, "\\u" + phrase.charCodeAt(0).toString(16).padStart(4, "0") + phrase.slice(1))
|
|
711
806
|
}
|
|
712
807
|
return QUOTED_TOOL_PREFIX + encoded
|
|
@@ -744,11 +839,12 @@ function anthropicBody(body) {
|
|
|
744
839
|
changed = true
|
|
745
840
|
continue
|
|
746
841
|
}
|
|
747
|
-
if (block?.type === "text"
|
|
748
|
-
block.
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
const
|
|
842
|
+
if (block?.type === "text") {
|
|
843
|
+
// The gateway may join the identity and instructions into one block.
|
|
844
|
+
// Only the complete fixed first line is metadata; keep the rest.
|
|
845
|
+
const identity = [...CLAUDE_IDENTITIES].find((line) => block.text === line || block.text.startsWith(line + "\n"))
|
|
846
|
+
const text = identity ? DROID_LINE + block.text.slice(identity.length) : block.text
|
|
847
|
+
const adapted = clientOpening(systemModelLine(text))
|
|
752
848
|
if (adapted !== block.text) {
|
|
753
849
|
block.text = adapted
|
|
754
850
|
changed = true
|
|
@@ -797,7 +893,16 @@ function anthropicBody(body) {
|
|
|
797
893
|
}
|
|
798
894
|
continue
|
|
799
895
|
}
|
|
800
|
-
if (message?.role !== "user"
|
|
896
|
+
if (message?.role !== "user") continue
|
|
897
|
+
if (typeof message.content === "string") {
|
|
898
|
+
const adapted = compactContext(message.content)
|
|
899
|
+
if (adapted !== message.content) {
|
|
900
|
+
message.content = adapted
|
|
901
|
+
changed = true
|
|
902
|
+
}
|
|
903
|
+
continue
|
|
904
|
+
}
|
|
905
|
+
if (!Array.isArray(message.content)) continue
|
|
801
906
|
for (const block of message.content) {
|
|
802
907
|
if (block?.type === "tool_result") {
|
|
803
908
|
if (typeof block.content === "string") {
|
|
@@ -819,7 +924,11 @@ function anthropicBody(body) {
|
|
|
819
924
|
continue
|
|
820
925
|
}
|
|
821
926
|
if (block?.type !== "text" || typeof block.text !== "string") continue
|
|
822
|
-
|
|
927
|
+
const compacted = compactContext(block.text)
|
|
928
|
+
if (compacted !== block.text) {
|
|
929
|
+
block.text = compacted
|
|
930
|
+
changed = true
|
|
931
|
+
} else if (block.text.startsWith(INSTRUCTIONS_REMINDER) && block.text.includes(GLOBAL_INSTRUCTIONS)) {
|
|
823
932
|
block.text = block.text.replaceAll(GLOBAL_INSTRUCTIONS, "(global instructions)")
|
|
824
933
|
changed = true
|
|
825
934
|
} else if (HOOK_OUTPUT.test(block.text) || SYSTEM_ENV_CONTEXT.test(block.text)) {
|