@magpie-community/opencode-factory-auth 0.1.17 → 0.1.18

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 +44 -7
  2. package/package.json +1 -1
package/index.mjs CHANGED
@@ -274,6 +274,39 @@ function explain(status, text) {
274
274
 
275
275
  const DROID_LINE = "You are Droid, an AI software engineering agent built by Factory."
276
276
 
277
+ // pi (@earendil-works/pi-coding-agent 1.0.4) opens its system prompt with
278
+ // a sentence Factory refuses whole, on the OpenAI and Anthropic routes
279
+ // alike (yetone/magpie#952): either half of it passes, and pi's full
280
+ // request with the sentence cut to "You are an expert coding assistant."
281
+ // is answered. Only that sentence, at the start of a line of a system or
282
+ // developer prompt, changes; the rest of pi's prompt goes on as it is.
283
+ const PI_OPENING = /(^|\n)You are an expert coding assistant operating inside pi, a coding agent harness\./
284
+ const PI_COMPAT = "You are an expert coding assistant."
285
+ function clientOpening(text) {
286
+ return typeof text === "string" ? text.replace(PI_OPENING, (_, at) => at + PI_COMPAT) : text
287
+ }
288
+
289
+ // 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.
292
+ function promptOpenings(msgs) {
293
+ let changed = false
294
+ for (const m of Array.isArray(msgs) ? msgs : []) {
295
+ if (!m || typeof m !== "object" || (m.role !== "system" && m.role !== "developer")) continue
296
+ if (typeof m.content === "string") {
297
+ const adapted = clientOpening(m.content)
298
+ if (adapted !== m.content) (m.content = adapted), (changed = true)
299
+ } else if (Array.isArray(m.content)) {
300
+ for (const p of m.content) {
301
+ if (!p || typeof p !== "object" || typeof p.text !== "string") continue
302
+ const adapted = clientOpening(p.text)
303
+ if (adapted !== p.text) (p.text = adapted), (changed = true)
304
+ }
305
+ }
306
+ }
307
+ return changed
308
+ }
309
+
277
310
  // droidChat opens msgs' first system message with droid's line, or puts one
278
311
  // before them with the line alone: false when it opens so already.
279
312
  function droidChat(msgs) {
@@ -326,11 +359,15 @@ function droidBody(path, body) {
326
359
  if (path.endsWith("/responses")) {
327
360
  const v = m.instructions
328
361
  if (v !== undefined && v !== null && typeof v !== "string") return body // not something droid sends
329
- const ins = v ?? ""
330
- if (ins.startsWith(DROID_LINE)) return body
331
- m.instructions = ins.trim() === "" ? DROID_LINE : DROID_LINE + "\n" + ins
362
+ const opened = promptOpenings(m.input)
363
+ const ins = clientOpening(v ?? "")
364
+ if (!ins.startsWith(DROID_LINE)) m.instructions = ins.trim() === "" ? DROID_LINE : DROID_LINE + "\n" + ins
365
+ else if (ins !== v || opened) m.instructions = ins
366
+ else return body
332
367
  } else if (path.endsWith("/chat/completions")) {
333
- if (!Array.isArray(m.messages) || !droidChat(m.messages)) return body
368
+ if (!Array.isArray(m.messages)) return body
369
+ const opened = promptOpenings(m.messages)
370
+ if (!droidChat(m.messages) && !opened) return body
334
371
  } else return body
335
372
  return JSON.stringify(m)
336
373
  }
@@ -711,7 +748,7 @@ function anthropicBody(body) {
711
748
  block.text = DROID_LINE
712
749
  changed = true
713
750
  } else if (block?.type === "text") {
714
- const adapted = systemModelLine(block.text)
751
+ const adapted = clientOpening(systemModelLine(block.text))
715
752
  if (adapted !== block.text) {
716
753
  block.text = adapted
717
754
  changed = true
@@ -739,7 +776,7 @@ function anthropicBody(body) {
739
776
 
740
777
  for (const message of request.messages) {
741
778
  if (message?.role === "system" && typeof message.content === "string") {
742
- const adapted = systemContext(message.content)
779
+ const adapted = clientOpening(systemContext(message.content))
743
780
  if (adapted !== message.content) {
744
781
  message.content = adapted
745
782
  changed = true
@@ -752,7 +789,7 @@ function anthropicBody(body) {
752
789
  if (message?.role === "system" && Array.isArray(message.content)) {
753
790
  for (const block of message.content) {
754
791
  if (block?.type !== "text" || typeof block.text !== "string") continue
755
- const adapted = systemContext(block.text)
792
+ const adapted = clientOpening(systemContext(block.text))
756
793
  if (adapted !== block.text) {
757
794
  block.text = adapted
758
795
  changed = true
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magpie-community/opencode-factory-auth",
3
- "version": "0.1.17",
3
+ "version": "0.1.18",
4
4
  "description": "Sign in to a Factory (Droid) subscription from OpenCode or magpie",
5
5
  "type": "module",
6
6
  "main": "./index.mjs",