@magpie-community/opencode-factory-auth 0.1.40 → 0.1.41

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 (2) hide show
  1. package/index.mjs +55 -4
  2. package/package.json +1 -1
package/index.mjs CHANGED
@@ -682,7 +682,9 @@ function geminiBody(model, body) {
682
682
  if (typeof si === "string") si = { parts: [{ text: si }] }
683
683
  if (!si || typeof si !== "object") si = { parts: [] }
684
684
  delete si.role
685
- const parts = Array.isArray(si.parts) ? si.parts : []
685
+ const parts = Array.isArray(si.parts) ? si.parts.map((part) =>
686
+ part && typeof part.text === "string" ? { ...part, text: zcodeRuntimeContext(part.text) } : part) : []
687
+ si.parts = parts
686
688
  if (!(typeof parts[0]?.text === "string" && parts[0].text.startsWith(DROID_LINE))) {
687
689
  const texts = []
688
690
  const rest = []
@@ -1334,6 +1336,8 @@ const COMPACT_OPENING = "This session is being continued from a previous convers
1334
1336
  const COMPACT_HEADER = COMPACT_OPENING + " The summary below covers the earlier portion of the conversation."
1335
1337
  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+/
1336
1338
  const READ_RESULT_HEADER = "Result of calling the Read tool:\n"
1339
+ const READ_ARGS_OPENING = "Called the Read tool with the following input:"
1340
+ const READ_ARGS_RENAMED = "Previously read file with these arguments:"
1337
1341
  const COMPACT_READ = /^<system-reminder>\nCalled the Read tool with the following input: (\{[^\n]*\})\n<\/system-reminder>$/
1338
1342
  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>$/
1339
1343
  function compactContext(text, quoteChangedFiles = true) {
@@ -1357,10 +1361,12 @@ function compactContext(text, quoteChangedFiles = true) {
1357
1361
  if (summary !== text) return summary
1358
1362
  const skills = invokedSkills(text)
1359
1363
  if (skills !== text) return skills
1364
+ const restoredRead = restoredReadText(text)
1365
+ if (restoredRead !== null) return restoredRead
1360
1366
  const read = text.match(COMPACT_READ)
1361
1367
  if (read) {
1362
1368
  try {
1363
- 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:")
1369
+ if (typeof JSON.parse(read[1])?.file_path === "string") return text.replace(READ_ARGS_OPENING, READ_ARGS_RENAMED)
1364
1370
  } catch {}
1365
1371
  }
1366
1372
  const file = text.match(COMPACT_FILE)
@@ -1379,6 +1385,30 @@ function readResultText(text) {
1379
1385
  return prefix + quotedToolText(content) + suffix
1380
1386
  }
1381
1387
 
1388
+ // ZCode restores a file read from before compaction as one reminder carrying
1389
+ // both the read arguments and the file's text, where Claude Code's reminder
1390
+ // carries the arguments alone (COMPACT_READ above). Factory refuses the fixed
1391
+ // "Called the Read tool" opening, so this shape needs its own rename. The file
1392
+ // text is adapted as a read result, which encodes any refused client phrase it
1393
+ // happens to quote.
1394
+ const ZCODE_RESTORED_READ = /^Called the Read tool with the following input: (\{[^\n]*\})\nResult of calling the Read tool:\n([\s\S]*)$/
1395
+ function restoredReadText(text) {
1396
+ const open = "<system-reminder>\n", close = "\n</system-reminder>"
1397
+ const wrapped = text.startsWith(open)
1398
+ if (wrapped && !text.endsWith(close)) return null
1399
+ const match = (wrapped ? text.slice(open.length, -close.length) : text).match(ZCODE_RESTORED_READ)
1400
+ if (!match) return null
1401
+ try {
1402
+ if (typeof JSON.parse(match[1])?.file_path !== "string") return null
1403
+ } catch {
1404
+ return null
1405
+ }
1406
+ return (wrapped ? open : "")
1407
+ + READ_ARGS_RENAMED + " " + match[1] + "\n" + READ_RESULT_HEADER
1408
+ + quotedToolText(match[2])
1409
+ + (wrapped ? close : "")
1410
+ }
1411
+
1382
1412
  // Claude Code's edited_text_file attachment reports current file lines,
1383
1413
  // not a unified diff. Keep its path and instructions; quote only the lines.
1384
1414
  const CHANGED_FILE_HEADER = /^Note: [^\n]+ changed on disk since you last read it\. That's usually deliberate, so take it as the current state rather than reverting it; if the change looks wrong, say so rather than undoing it yourself — otherwise no need to call it out\. Here are the relevant changes \(shown with line numbers\):\n/
@@ -1512,14 +1542,35 @@ function claudeIdentity(text) {
1512
1542
  return identity ? DROID_LINE + text.slice(identity.length) : text
1513
1543
  }
1514
1544
 
1545
+ // ZCode embeds its generated environment paragraph, with unindented bullets,
1546
+ // in a larger system prompt. Factory refuses the fixed opening. Change only a
1547
+ // complete standalone generated paragraph, keeping every value and leaving
1548
+ // fenced, quoted or incomplete copies alone. Shell is optional: ZCode's own
1549
+ // prompt builder omits it, while the desktop prompt includes it.
1550
+ const ZCODE_ENV_CONTEXT = /(^|\n\n)(# Environment\nYou have been invoked in the following environment:\n(?:- Primary working directory: [^\n]+\n(?:- Is a git repository: (?:yes|no)\n)?)?- Platform: [^\n]+\n(?:- Shell: [^\n]+\n)?- OS Version: [^\n]+\n- You are powered by the model named [^\n]+\.)(?=\n(?:\n|$)|$)/g
1551
+ function insideFence(text, end) {
1552
+ let fenced = false
1553
+ for (const line of text.slice(0, end).split("\n")) if (/^ {0,3}(?:```|~~~)/.test(line)) fenced = !fenced
1554
+ return fenced
1555
+ }
1556
+ function zcodeRuntimeContext(text) {
1557
+ return text.replace(ZCODE_ENV_CONTEXT, (block, lead, paragraph, offset) => {
1558
+ if (insideFence(text, offset + lead.length)) return block
1559
+ return lead + paragraph
1560
+ .replace("# Environment", "# Runtime context")
1561
+ .replace("You have been invoked in the following environment:", "The session environment is:")
1562
+ .replace("- You are powered by the model named", "- Current model name:")
1563
+ })
1564
+ }
1565
+
1515
1566
  // promptText adapts a system prompt's text.
1516
1567
  function promptText(text) {
1517
- return configSkillText(clientOpening(systemModelLine(claudeIdentity(text))))
1568
+ return configSkillText(clientOpening(systemModelLine(claudeIdentity(zcodeRuntimeContext(text)))))
1518
1569
  }
1519
1570
 
1520
1571
  // systemMessageText adapts a system message's text after the prompt.
1521
1572
  function systemMessageText(text) {
1522
- return bareSkills(clientOpening(systemContext(text)))
1573
+ return bareSkills(clientOpening(systemContext(zcodeRuntimeContext(text))))
1523
1574
  }
1524
1575
 
1525
1576
  // userBlockText adapts one text block of a user turn.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magpie-community/opencode-factory-auth",
3
- "version": "0.1.40",
3
+ "version": "0.1.41",
4
4
  "description": "Sign in to a Factory (Droid) subscription from OpenCode or magpie",
5
5
  "type": "module",
6
6
  "main": "./index.mjs",