@magpie-community/opencode-factory-auth 0.1.8 → 0.1.10

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.
Files changed (3) hide show
  1. package/README.md +20 -3
  2. package/index.mjs +52 -8
  3. package/package.json +3 -2
package/README.md CHANGED
@@ -77,9 +77,10 @@ magpie plugin login factory
77
77
  Factory's Anthropic route requires Droid's fixed client preamble and
78
78
  refuses some of Claude Code's fixed environment, model and built-in skill
79
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.
80
+ instructions, environment values, tool definitions, images and reasoning
81
+ options. Ordinary Droid requests remain unchanged, and OpenAI routes keep
82
+ their existing request adapter. Quoted fixed metadata in tool results is
83
+ handled as described below.
83
84
 
84
85
  For the built-in `update-config` skill, the adapter changes only the known
85
86
  self-reference "not Claude" to "not the assistant" inside a complete
@@ -90,6 +91,22 @@ Claude 5 can send runtime metadata in string-content `system` messages
90
91
  instead of user-message reminders. The plugin adapts the known environment,
91
92
  model and configuration-skill metadata in that shape too, preserving the
92
93
  message role, environment values and session instructions.
94
+ After `/model` switches, Claude Code can send model-only system updates
95
+ without the initial environment paragraph. Updates beginning with the known
96
+ model paragraph and containing complete generated token metadata are
97
+ adapted too, including accumulated switches when resuming a conversation.
98
+ Token budgets, permission instructions and conversation history are preserved.
99
+ The same model-update adaptation covers updates that start with the complete
100
+ generated `# Environment update` block after a working-directory change.
101
+
102
+ Factory can also refuse fixed client phrases quoted in tool results, such as
103
+ the identity/environment definitions printed when inspecting this plugin's
104
+ source. Those text results are represented as JSON strings with explicit
105
+ decoding instructions and Unicode escapes for the fixed phrases. Decoding
106
+ the string recovers the exact original output, including quotes, backslashes
107
+ and Unicode; the plugin does not delete the output or replace its identities
108
+ with different ones. Tool ids, cache/error markers and non-text blocks stay
109
+ unchanged. Ordinary tool results are forwarded as before.
93
110
 
94
111
  Use magpie's Claude Code integration to select the Factory provider.
95
112
  magpie manages the provider-specific client settings, including capability
package/index.mjs CHANGED
@@ -591,18 +591,26 @@ const CONFIG_SKILL_COMPAT = CONFIG_SKILL_METADATA.replace("not Claude", "not the
591
591
 
592
592
  // Claude 5 also sends its runtime metadata as a string-content system
593
593
  // message, instead of user-message reminders. Preserve that role and all
594
- // instructions; only the known environment paragraph opens this adapter.
594
+ // instructions. A model switch sends a separate update without the
595
+ // environment paragraph, sometimes following a working-directory update;
596
+ // require the generated opening block and complete token context before
597
+ // adapting either update shape.
595
598
  const SYSTEM_ENV_CONTEXT = /^# Environment\nYou have been invoked in the following environment:[ \t]*\n(?: {1,2}- [^\n]*\n)+(?=\n|$)/
596
599
  const SYSTEM_MODEL_CONTEXT = /(^|\n\n)You are powered by the model (?:named )?[^\n<>]+\.(?=\n\n|$)/g
600
+ const SYSTEM_MODEL_UPDATE = /^You are powered by the model (?:named )?[^\n<>]+\.(?=\n\n|$)/
601
+ const SYSTEM_ENV_UPDATE = /^# Environment update\n(?: {1,2}- [^\n]*\n)+(?=\n|$)/
602
+ const SYSTEM_TOKEN_CONTEXT = /(?:^|\n\n)<total_tokens>\d+ tokens left<\/total_tokens>(?=\n\n|$)/
597
603
  function systemContext(text) {
598
- if (!SYSTEM_ENV_CONTEXT.test(text)) return text
599
- let out = text.replace("# Environment", "# Runtime context")
604
+ const environment = SYSTEM_ENV_CONTEXT.test(text)
605
+ if (!environment && !((SYSTEM_MODEL_UPDATE.test(text) || SYSTEM_ENV_UPDATE.test(text)) && SYSTEM_TOKEN_CONTEXT.test(text))) return text
606
+ let out = text.replace(SYSTEM_MODEL_CONTEXT, (paragraph) => paragraph
607
+ .replace("You are powered by the model named", "Current model name:")
608
+ .replace("You are powered by the model", "Current model:")
609
+ .replace("The exact model ID is", "Model ID:")
610
+ .replace("Assistant knowledge cutoff is", "Model knowledge cutoff:"))
611
+ if (!environment) return out
612
+ out = out.replace("# Environment", "# Runtime context")
600
613
  .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
614
  const skillHeader = "\n\nThe following skills are available for use with the Skill tool:\n\n"
607
615
  const skills = out.indexOf(skillHeader)
608
616
  if (skills >= 0) {
@@ -612,6 +620,23 @@ function systemContext(text) {
612
620
  return out
613
621
  }
614
622
 
623
+ // Factory also refuses these fixed client phrases when they are quoted in
624
+ // tool output, e.g. while reading this adapter's source. Keep the original
625
+ // text recoverable: a JSON string with explicit decoding instructions,
626
+ // rather than deleting or rewriting the file's contents. Other output and
627
+ // the tool's id, error/cache markers and non-text blocks stay untouched.
628
+ const QUOTED_TOOL_PREFIX = "Tool output encoded as a JSON string. Decode the JSON string to recover the exact original text before using it:\n"
629
+ function quotedToolText(text) {
630
+ if (![...CLAUDE_IDENTITIES].some((identity) => text.includes(identity)) &&
631
+ !text.includes("You have been invoked in the following environment:") &&
632
+ !text.includes("x-anthropic-billing-header: cc_version=")) return text
633
+ let encoded = JSON.stringify(text)
634
+ for (const phrase of ["You are", "You have", "x-anthropic-billing-header", "system-reminder"]) {
635
+ encoded = encoded.replaceAll(phrase, "\\u" + phrase.charCodeAt(0).toString(16).padStart(4, "0") + phrase.slice(1))
636
+ }
637
+ return QUOTED_TOOL_PREFIX + encoded
638
+ }
639
+
615
640
  function anthropicBody(body) {
616
641
  let request
617
642
  try {
@@ -679,6 +704,25 @@ function anthropicBody(body) {
679
704
  }
680
705
  if (message?.role !== "user" || !Array.isArray(message.content)) continue
681
706
  for (const block of message.content) {
707
+ if (block?.type === "tool_result") {
708
+ if (typeof block.content === "string") {
709
+ const adapted = quotedToolText(block.content)
710
+ if (adapted !== block.content) {
711
+ block.content = adapted
712
+ changed = true
713
+ }
714
+ } else if (Array.isArray(block.content)) {
715
+ for (const part of block.content) {
716
+ if (part?.type !== "text" || typeof part.text !== "string") continue
717
+ const adapted = quotedToolText(part.text)
718
+ if (adapted !== part.text) {
719
+ part.text = adapted
720
+ changed = true
721
+ }
722
+ }
723
+ }
724
+ continue
725
+ }
682
726
  if (block?.type !== "text" || typeof block.text !== "string") continue
683
727
  if (ENV_REMINDER.test(block.text)) {
684
728
  block.text = block.text.replace("# Environment", "# Runtime context")
package/package.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "name": "@magpie-community/opencode-factory-auth",
3
- "version": "0.1.8",
3
+ "version": "0.1.10",
4
4
  "description": "Sign in to a Factory (Droid) subscription from OpenCode or magpie",
5
5
  "type": "module",
6
6
  "main": "./index.mjs",
7
7
  "files": ["index.mjs", "README.md"],
8
8
  "keywords": ["opencode", "opencode-plugin", "magpie", "factory", "droid"],
9
- "license": "MIT"
9
+ "license": "MIT",
10
+ "repository": { "type": "git", "url": "git+https://github.com/magpie-community/plugins.git", "directory": "packages/factory" }
10
11
  }