@magpie-community/opencode-factory-auth 0.1.20 → 0.1.21

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 +30 -5
  2. package/package.json +1 -1
package/index.mjs CHANGED
@@ -286,20 +286,43 @@ function clientOpening(text) {
286
286
  return typeof text === "string" ? text.replace(PI_OPENING, (_, at) => at + PI_COMPAT) : text
287
287
  }
288
288
 
289
+ // pi turns a compaction, and a branch it came back from, into a user
290
+ // message of one text: a fixed sentence, then the summary in <summary> tags
291
+ // (pi-coding-agent 1.0.4 and 1.1.0, dist/core/messages.js:
292
+ // COMPACTION_SUMMARY_PREFIX and BRANCH_SUMMARY_PREFIX). Factory refuses the
293
+ // compaction sentence whole (yetone/magpie#1316): after a compaction every
294
+ // turn was 403, and the reporter's replays passed with the sentence removed,
295
+ // reworded or only half of it. The branch sentence is pi's other fixed
296
+ // summary opening and is reworded the same way. Only a text that is pi's
297
+ // whole summary, sentence first and </summary> last, changes; the summary
298
+ // itself goes on as it is.
299
+ const PI_SUMMARIES = [
300
+ ["The conversation history before this point was compacted into the following summary:\n\n<summary>\n", "Earlier conversation context is summarized below:\n\n<summary>\n"],
301
+ ["The following is a summary of a branch that this conversation came back from:\n\n<summary>\n", "Summary of an earlier conversation branch:\n\n<summary>\n"],
302
+ ]
303
+ function piSummary(text) {
304
+ if (typeof text !== "string" || !text.endsWith("</summary>")) return text
305
+ for (const [from, to] of PI_SUMMARIES) if (text.startsWith(from)) return to + text.slice(from.length)
306
+ return text
307
+ }
308
+
289
309
  // promptOpenings adapts clientOpening in the system and developer messages
290
- // of msgs (chat completions' messages, Responses' input), their string
291
- // content or text parts: false when nothing changed.
310
+ // of msgs (chat completions' messages, Responses' input), and piSummary in
311
+ // their user messages, their string content or text parts: false when
312
+ // nothing changed.
292
313
  function promptOpenings(msgs) {
293
314
  let changed = false
294
315
  for (const m of Array.isArray(msgs) ? msgs : []) {
295
- if (!m || typeof m !== "object" || (m.role !== "system" && m.role !== "developer")) continue
316
+ if (!m || typeof m !== "object") continue
317
+ const adapt = m.role === "system" || m.role === "developer" ? clientOpening : m.role === "user" ? piSummary : null
318
+ if (!adapt) continue
296
319
  if (typeof m.content === "string") {
297
- const adapted = clientOpening(m.content)
320
+ const adapted = adapt(m.content)
298
321
  if (adapted !== m.content) (m.content = adapted), (changed = true)
299
322
  } else if (Array.isArray(m.content)) {
300
323
  for (const p of m.content) {
301
324
  if (!p || typeof p !== "object" || typeof p.text !== "string") continue
302
- const adapted = clientOpening(p.text)
325
+ const adapted = adapt(p.text)
303
326
  if (adapted !== p.text) (p.text = adapted), (changed = true)
304
327
  }
305
328
  }
@@ -789,6 +812,8 @@ function compactContext(text, quoteChangedFiles = true) {
789
812
  const quoted = quotedToolText(context, "Conversation context encoded as a JSON string. Decode the JSON string to recover the exact original context before continuing:\n")
790
813
  return header + (quoted === context ? context : "\n\n" + quoted)
791
814
  }
815
+ const summary = piSummary(text)
816
+ if (summary !== text) return summary
792
817
  const read = text.match(COMPACT_READ)
793
818
  if (read) {
794
819
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magpie-community/opencode-factory-auth",
3
- "version": "0.1.20",
3
+ "version": "0.1.21",
4
4
  "description": "Sign in to a Factory (Droid) subscription from OpenCode or magpie",
5
5
  "type": "module",
6
6
  "main": "./index.mjs",