@magpie-community/opencode-factory-auth 0.1.28 → 0.1.29
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/index.mjs +91 -52
- package/package.json +1 -1
package/index.mjs
CHANGED
|
@@ -382,27 +382,38 @@ function piSummary(text) {
|
|
|
382
382
|
return text
|
|
383
383
|
}
|
|
384
384
|
|
|
385
|
-
// promptOpenings adapts
|
|
386
|
-
//
|
|
387
|
-
//
|
|
388
|
-
//
|
|
389
|
-
|
|
385
|
+
// promptOpenings adapts the messages of msgs (chat completions' messages,
|
|
386
|
+
// Responses' input) as anthropicBody adapts a Messages request's: pi's
|
|
387
|
+
// opening and Claude Code's fixed phrases in the system and developer
|
|
388
|
+
// messages (the first of chat's as a system prompt, when prompt), pi's
|
|
389
|
+
// summary and Claude Code's reminders in user messages, and client phrases
|
|
390
|
+
// quoted in tool output (chat's tool messages, Responses'
|
|
391
|
+
// function_call_output). String content or text parts change, nothing else:
|
|
392
|
+
// false when nothing changed.
|
|
393
|
+
function promptOpenings(msgs, prompt = false) {
|
|
390
394
|
let changed = false
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
if (
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
395
|
+
const list = Array.isArray(msgs) ? msgs : []
|
|
396
|
+
list.forEach((m, i) => {
|
|
397
|
+
if (!m || typeof m !== "object") return
|
|
398
|
+
let adapt, field = "content"
|
|
399
|
+
if (m.role === "system" || m.role === "developer") adapt = prompt && i === 0 && m.role === "system" ? promptText : systemMessageText
|
|
400
|
+
else if (m.role === "user") adapt = (t) => userText(piSummary(t))
|
|
401
|
+
else if (m.role === "tool") adapt = (t) => quotedToolText(t)
|
|
402
|
+
else if (m.type === "function_call_output") (adapt = (t) => quotedToolText(t)), (field = "output")
|
|
403
|
+
else return
|
|
404
|
+
const c = m[field]
|
|
405
|
+
if (typeof c === "string") {
|
|
406
|
+
const adapted = adapt(c)
|
|
407
|
+
if (adapted !== c) (m[field] = adapted), (changed = true)
|
|
408
|
+
} else if (Array.isArray(c)) {
|
|
409
|
+
for (const p of c) {
|
|
400
410
|
if (!p || typeof p !== "object" || typeof p.text !== "string") continue
|
|
401
|
-
|
|
411
|
+
// a part is a block of its own: no joined reminders to look into
|
|
412
|
+
const adapted = m.role === "user" ? userBlockText(piSummary(p.text)) : adapt(p.text)
|
|
402
413
|
if (adapted !== p.text) (p.text = adapted), (changed = true)
|
|
403
414
|
}
|
|
404
415
|
}
|
|
405
|
-
}
|
|
416
|
+
})
|
|
406
417
|
return changed
|
|
407
418
|
}
|
|
408
419
|
|
|
@@ -459,13 +470,13 @@ function droidBody(path, body) {
|
|
|
459
470
|
const v = m.instructions
|
|
460
471
|
if (v !== undefined && v !== null && typeof v !== "string") return body // not something droid sends
|
|
461
472
|
const opened = promptOpenings(m.input)
|
|
462
|
-
const ins =
|
|
473
|
+
const ins = promptText(v ?? "")
|
|
463
474
|
if (!ins.startsWith(DROID_LINE)) m.instructions = ins.trim() === "" ? DROID_LINE : DROID_LINE + "\n" + ins
|
|
464
475
|
else if (ins !== v || opened) m.instructions = ins
|
|
465
476
|
else return body
|
|
466
477
|
} else if (path.endsWith("/chat/completions")) {
|
|
467
478
|
if (!Array.isArray(m.messages)) return body
|
|
468
|
-
const opened = promptOpenings(m.messages)
|
|
479
|
+
const opened = promptOpenings(m.messages, true)
|
|
469
480
|
if (!droidChat(m.messages) && !opened) return body
|
|
470
481
|
} else return body
|
|
471
482
|
return JSON.stringify(m)
|
|
@@ -1191,6 +1202,62 @@ function quotedToolText(text, prefix = QUOTED_TOOL_PREFIX) {
|
|
|
1191
1202
|
return prefix + encoded
|
|
1192
1203
|
}
|
|
1193
1204
|
|
|
1205
|
+
// The adapters below are shared by both of Factory's routes. Claude Code's
|
|
1206
|
+
// request reaches /api/llm/a as Messages, but a model Factory serves on
|
|
1207
|
+
// /api/llm/o (GLM, Kimi and other chat models) gets it translated by magpie
|
|
1208
|
+
// into chat completions or Responses, carrying the same fixed phrases.
|
|
1209
|
+
// Factory refuses them there too (Echoo on magpie's Discord: GLM-5.3-Flash,
|
|
1210
|
+
// Claude Code 403 while Codex worked on the same account).
|
|
1211
|
+
|
|
1212
|
+
// claudeIdentity opens text with droid's line in place of Claude Code's
|
|
1213
|
+
// fixed first line, when text is that line alone or opens with it.
|
|
1214
|
+
function claudeIdentity(text) {
|
|
1215
|
+
const identity = [...CLAUDE_IDENTITIES].find((line) => text === line || text.startsWith(line + "\n"))
|
|
1216
|
+
return identity ? DROID_LINE + text.slice(identity.length) : text
|
|
1217
|
+
}
|
|
1218
|
+
|
|
1219
|
+
// promptText adapts a system prompt's text.
|
|
1220
|
+
function promptText(text) {
|
|
1221
|
+
return clientOpening(systemModelLine(claudeIdentity(text)))
|
|
1222
|
+
}
|
|
1223
|
+
|
|
1224
|
+
// systemMessageText adapts a system message's text after the prompt.
|
|
1225
|
+
function systemMessageText(text) {
|
|
1226
|
+
return bareSkills(clientOpening(systemContext(text)))
|
|
1227
|
+
}
|
|
1228
|
+
|
|
1229
|
+
// userBlockText adapts one text block of a user turn.
|
|
1230
|
+
function userBlockText(text) {
|
|
1231
|
+
const compacted = compactContext(text)
|
|
1232
|
+
if (compacted !== text) return compacted
|
|
1233
|
+
if (text.startsWith(INSTRUCTIONS_REMINDER) && text.includes(GLOBAL_INSTRUCTIONS)) return text.replaceAll(GLOBAL_INSTRUCTIONS, "(global instructions)")
|
|
1234
|
+
// a system message folded into the user's turn on its way here
|
|
1235
|
+
if (HOOK_OUTPUT.test(text) || SYSTEM_ENV_CONTEXT.test(text)) return systemContext(text)
|
|
1236
|
+
if (ENV_REMINDER.test(text)) {
|
|
1237
|
+
return text.replace("# Environment", "# Runtime context")
|
|
1238
|
+
.replace("You have been invoked in the following environment:", "The session environment is:")
|
|
1239
|
+
}
|
|
1240
|
+
if (MODEL_REMINDER.test(text)) {
|
|
1241
|
+
return text.replace("You are powered by the model named", "Current model name:")
|
|
1242
|
+
.replace("You are powered by the model", "Current model:")
|
|
1243
|
+
.replace("The exact model ID is", "Model ID:")
|
|
1244
|
+
.replace("Assistant knowledge cutoff is", "Model knowledge cutoff:")
|
|
1245
|
+
}
|
|
1246
|
+
if (SKILL_REMINDER.test(text)) return text.replace("\n" + CONFIG_SKILL_METADATA, "\n" + CONFIG_SKILL_COMPAT)
|
|
1247
|
+
return text
|
|
1248
|
+
}
|
|
1249
|
+
|
|
1250
|
+
// A chat completions user message is one string: magpie joins a Messages
|
|
1251
|
+
// turn's text blocks with nothing between them, so a reminder block and the
|
|
1252
|
+
// user's words arrive as one text. Adapted whole first, then each complete
|
|
1253
|
+
// reminder in it as the block it was; the rest stays as it is.
|
|
1254
|
+
const REMINDER_BLOCK = /<system-reminder>\n[\s\S]*?\n<\/system-reminder>/g
|
|
1255
|
+
function userText(text) {
|
|
1256
|
+
const adapted = userBlockText(text)
|
|
1257
|
+
if (adapted !== text || !text.includes("<system-reminder>\n")) return adapted
|
|
1258
|
+
return text.replace(REMINDER_BLOCK, (block) => userBlockText(block))
|
|
1259
|
+
}
|
|
1260
|
+
|
|
1194
1261
|
function anthropicBody(body) {
|
|
1195
1262
|
let request
|
|
1196
1263
|
try {
|
|
@@ -1226,9 +1293,7 @@ function anthropicBody(body) {
|
|
|
1226
1293
|
if (block?.type === "text") {
|
|
1227
1294
|
// The gateway may join the identity and instructions into one block.
|
|
1228
1295
|
// Only the complete fixed first line is metadata; keep the rest.
|
|
1229
|
-
const
|
|
1230
|
-
const text = identity ? DROID_LINE + block.text.slice(identity.length) : block.text
|
|
1231
|
-
const adapted = clientOpening(systemModelLine(text))
|
|
1296
|
+
const adapted = promptText(block.text)
|
|
1232
1297
|
if (adapted !== block.text) {
|
|
1233
1298
|
block.text = adapted
|
|
1234
1299
|
changed = true
|
|
@@ -1256,7 +1321,7 @@ function anthropicBody(body) {
|
|
|
1256
1321
|
|
|
1257
1322
|
for (const message of request.messages) {
|
|
1258
1323
|
if (message?.role === "system" && typeof message.content === "string") {
|
|
1259
|
-
const adapted =
|
|
1324
|
+
const adapted = systemMessageText(message.content)
|
|
1260
1325
|
if (adapted !== message.content) {
|
|
1261
1326
|
message.content = adapted
|
|
1262
1327
|
changed = true
|
|
@@ -1269,7 +1334,7 @@ function anthropicBody(body) {
|
|
|
1269
1334
|
if (message?.role === "system" && Array.isArray(message.content)) {
|
|
1270
1335
|
for (const block of message.content) {
|
|
1271
1336
|
if (block?.type !== "text" || typeof block.text !== "string") continue
|
|
1272
|
-
const adapted =
|
|
1337
|
+
const adapted = systemMessageText(block.text)
|
|
1273
1338
|
if (adapted !== block.text) {
|
|
1274
1339
|
block.text = adapted
|
|
1275
1340
|
changed = true
|
|
@@ -1308,36 +1373,10 @@ function anthropicBody(body) {
|
|
|
1308
1373
|
continue
|
|
1309
1374
|
}
|
|
1310
1375
|
if (block?.type !== "text" || typeof block.text !== "string") continue
|
|
1311
|
-
const
|
|
1312
|
-
if (
|
|
1313
|
-
block.text =
|
|
1314
|
-
changed = true
|
|
1315
|
-
} else if (block.text.startsWith(INSTRUCTIONS_REMINDER) && block.text.includes(GLOBAL_INSTRUCTIONS)) {
|
|
1316
|
-
block.text = block.text.replaceAll(GLOBAL_INSTRUCTIONS, "(global instructions)")
|
|
1317
|
-
changed = true
|
|
1318
|
-
} else if (HOOK_OUTPUT.test(block.text) || SYSTEM_ENV_CONTEXT.test(block.text)) {
|
|
1319
|
-
// a system message folded into the user's turn on its way here
|
|
1320
|
-
const adapted = systemContext(block.text)
|
|
1321
|
-
if (adapted !== block.text) {
|
|
1322
|
-
block.text = adapted
|
|
1323
|
-
changed = true
|
|
1324
|
-
}
|
|
1325
|
-
} else if (ENV_REMINDER.test(block.text)) {
|
|
1326
|
-
block.text = block.text.replace("# Environment", "# Runtime context")
|
|
1327
|
-
.replace("You have been invoked in the following environment:", "The session environment is:")
|
|
1328
|
-
changed = true
|
|
1329
|
-
} else if (MODEL_REMINDER.test(block.text)) {
|
|
1330
|
-
block.text = block.text.replace("You are powered by the model named", "Current model name:")
|
|
1331
|
-
.replace("You are powered by the model", "Current model:")
|
|
1332
|
-
.replace("The exact model ID is", "Model ID:")
|
|
1333
|
-
.replace("Assistant knowledge cutoff is", "Model knowledge cutoff:")
|
|
1376
|
+
const adapted = userBlockText(block.text)
|
|
1377
|
+
if (adapted !== block.text) {
|
|
1378
|
+
block.text = adapted
|
|
1334
1379
|
changed = true
|
|
1335
|
-
} else if (SKILL_REMINDER.test(block.text)) {
|
|
1336
|
-
const adapted = block.text.replace("\n" + CONFIG_SKILL_METADATA, "\n" + CONFIG_SKILL_COMPAT)
|
|
1337
|
-
if (adapted !== block.text) {
|
|
1338
|
-
block.text = adapted
|
|
1339
|
-
changed = true
|
|
1340
|
-
}
|
|
1341
1380
|
}
|
|
1342
1381
|
}
|
|
1343
1382
|
}
|