@magpie-community/opencode-factory-auth 0.1.28 → 0.1.30
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 +98 -54
- package/package.json +1 -1
package/index.mjs
CHANGED
|
@@ -13,8 +13,13 @@ const API = "https://api.factory.ai"
|
|
|
13
13
|
const API_EU = "https://api.eu.factory.ai"
|
|
14
14
|
// droid's WorkOS client, production
|
|
15
15
|
const CLIENT_ID = "client_01HNM792M5G5G1A2THWPXKFMXB"
|
|
16
|
-
// the droid release the requests say they are
|
|
17
|
-
|
|
16
|
+
// the droid release the requests say they are: the current one (droid
|
|
17
|
+
// 0.242.0's X-Client-Version and User-Agent). Its binary reads a
|
|
18
|
+
// server-side min_cli_version, and both reports of every model answered a
|
|
19
|
+
// bare 403 "Forbidden" (0.1.16, 0.1.30) came with this release behind
|
|
20
|
+
// droid's while every other header matched; whether Factory refuses an old
|
|
21
|
+
// release so is not confirmed
|
|
22
|
+
const VERSION = "0.242.0"
|
|
18
23
|
// how long before an access token lapses it is renewed (droid: a minute)
|
|
19
24
|
const REFRESH_LEAD = 2 * 60 * 1000
|
|
20
25
|
// and how long before, magpie renews it ahead of time (auth.refresh): a
|
|
@@ -382,27 +387,38 @@ function piSummary(text) {
|
|
|
382
387
|
return text
|
|
383
388
|
}
|
|
384
389
|
|
|
385
|
-
// promptOpenings adapts
|
|
386
|
-
//
|
|
387
|
-
//
|
|
388
|
-
//
|
|
389
|
-
|
|
390
|
+
// promptOpenings adapts the messages of msgs (chat completions' messages,
|
|
391
|
+
// Responses' input) as anthropicBody adapts a Messages request's: pi's
|
|
392
|
+
// opening and Claude Code's fixed phrases in the system and developer
|
|
393
|
+
// messages (the first of chat's as a system prompt, when prompt), pi's
|
|
394
|
+
// summary and Claude Code's reminders in user messages, and client phrases
|
|
395
|
+
// quoted in tool output (chat's tool messages, Responses'
|
|
396
|
+
// function_call_output). String content or text parts change, nothing else:
|
|
397
|
+
// false when nothing changed.
|
|
398
|
+
function promptOpenings(msgs, prompt = false) {
|
|
390
399
|
let changed = false
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
if (
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
+
const list = Array.isArray(msgs) ? msgs : []
|
|
401
|
+
list.forEach((m, i) => {
|
|
402
|
+
if (!m || typeof m !== "object") return
|
|
403
|
+
let adapt, field = "content"
|
|
404
|
+
if (m.role === "system" || m.role === "developer") adapt = prompt && i === 0 && m.role === "system" ? promptText : systemMessageText
|
|
405
|
+
else if (m.role === "user") adapt = (t) => userText(piSummary(t))
|
|
406
|
+
else if (m.role === "tool") adapt = (t) => quotedToolText(t)
|
|
407
|
+
else if (m.type === "function_call_output") (adapt = (t) => quotedToolText(t)), (field = "output")
|
|
408
|
+
else return
|
|
409
|
+
const c = m[field]
|
|
410
|
+
if (typeof c === "string") {
|
|
411
|
+
const adapted = adapt(c)
|
|
412
|
+
if (adapted !== c) (m[field] = adapted), (changed = true)
|
|
413
|
+
} else if (Array.isArray(c)) {
|
|
414
|
+
for (const p of c) {
|
|
400
415
|
if (!p || typeof p !== "object" || typeof p.text !== "string") continue
|
|
401
|
-
|
|
416
|
+
// a part is a block of its own: no joined reminders to look into
|
|
417
|
+
const adapted = m.role === "user" ? userBlockText(piSummary(p.text)) : adapt(p.text)
|
|
402
418
|
if (adapted !== p.text) (p.text = adapted), (changed = true)
|
|
403
419
|
}
|
|
404
420
|
}
|
|
405
|
-
}
|
|
421
|
+
})
|
|
406
422
|
return changed
|
|
407
423
|
}
|
|
408
424
|
|
|
@@ -459,13 +475,13 @@ function droidBody(path, body) {
|
|
|
459
475
|
const v = m.instructions
|
|
460
476
|
if (v !== undefined && v !== null && typeof v !== "string") return body // not something droid sends
|
|
461
477
|
const opened = promptOpenings(m.input)
|
|
462
|
-
const ins =
|
|
478
|
+
const ins = promptText(v ?? "")
|
|
463
479
|
if (!ins.startsWith(DROID_LINE)) m.instructions = ins.trim() === "" ? DROID_LINE : DROID_LINE + "\n" + ins
|
|
464
480
|
else if (ins !== v || opened) m.instructions = ins
|
|
465
481
|
else return body
|
|
466
482
|
} else if (path.endsWith("/chat/completions")) {
|
|
467
483
|
if (!Array.isArray(m.messages)) return body
|
|
468
|
-
const opened = promptOpenings(m.messages)
|
|
484
|
+
const opened = promptOpenings(m.messages, true)
|
|
469
485
|
if (!droidChat(m.messages) && !opened) return body
|
|
470
486
|
} else return body
|
|
471
487
|
return JSON.stringify(m)
|
|
@@ -1191,6 +1207,62 @@ function quotedToolText(text, prefix = QUOTED_TOOL_PREFIX) {
|
|
|
1191
1207
|
return prefix + encoded
|
|
1192
1208
|
}
|
|
1193
1209
|
|
|
1210
|
+
// The adapters below are shared by both of Factory's routes. Claude Code's
|
|
1211
|
+
// request reaches /api/llm/a as Messages, but a model Factory serves on
|
|
1212
|
+
// /api/llm/o (GLM, Kimi and other chat models) gets it translated by magpie
|
|
1213
|
+
// into chat completions or Responses, carrying the same fixed phrases.
|
|
1214
|
+
// Factory refuses them there too (Echoo on magpie's Discord: GLM-5.3-Flash,
|
|
1215
|
+
// Claude Code 403 while Codex worked on the same account).
|
|
1216
|
+
|
|
1217
|
+
// claudeIdentity opens text with droid's line in place of Claude Code's
|
|
1218
|
+
// fixed first line, when text is that line alone or opens with it.
|
|
1219
|
+
function claudeIdentity(text) {
|
|
1220
|
+
const identity = [...CLAUDE_IDENTITIES].find((line) => text === line || text.startsWith(line + "\n"))
|
|
1221
|
+
return identity ? DROID_LINE + text.slice(identity.length) : text
|
|
1222
|
+
}
|
|
1223
|
+
|
|
1224
|
+
// promptText adapts a system prompt's text.
|
|
1225
|
+
function promptText(text) {
|
|
1226
|
+
return clientOpening(systemModelLine(claudeIdentity(text)))
|
|
1227
|
+
}
|
|
1228
|
+
|
|
1229
|
+
// systemMessageText adapts a system message's text after the prompt.
|
|
1230
|
+
function systemMessageText(text) {
|
|
1231
|
+
return bareSkills(clientOpening(systemContext(text)))
|
|
1232
|
+
}
|
|
1233
|
+
|
|
1234
|
+
// userBlockText adapts one text block of a user turn.
|
|
1235
|
+
function userBlockText(text) {
|
|
1236
|
+
const compacted = compactContext(text)
|
|
1237
|
+
if (compacted !== text) return compacted
|
|
1238
|
+
if (text.startsWith(INSTRUCTIONS_REMINDER) && text.includes(GLOBAL_INSTRUCTIONS)) return text.replaceAll(GLOBAL_INSTRUCTIONS, "(global instructions)")
|
|
1239
|
+
// a system message folded into the user's turn on its way here
|
|
1240
|
+
if (HOOK_OUTPUT.test(text) || SYSTEM_ENV_CONTEXT.test(text)) return systemContext(text)
|
|
1241
|
+
if (ENV_REMINDER.test(text)) {
|
|
1242
|
+
return text.replace("# Environment", "# Runtime context")
|
|
1243
|
+
.replace("You have been invoked in the following environment:", "The session environment is:")
|
|
1244
|
+
}
|
|
1245
|
+
if (MODEL_REMINDER.test(text)) {
|
|
1246
|
+
return text.replace("You are powered by the model named", "Current model name:")
|
|
1247
|
+
.replace("You are powered by the model", "Current model:")
|
|
1248
|
+
.replace("The exact model ID is", "Model ID:")
|
|
1249
|
+
.replace("Assistant knowledge cutoff is", "Model knowledge cutoff:")
|
|
1250
|
+
}
|
|
1251
|
+
if (SKILL_REMINDER.test(text)) return text.replace("\n" + CONFIG_SKILL_METADATA, "\n" + CONFIG_SKILL_COMPAT)
|
|
1252
|
+
return text
|
|
1253
|
+
}
|
|
1254
|
+
|
|
1255
|
+
// A chat completions user message is one string: magpie joins a Messages
|
|
1256
|
+
// turn's text blocks with nothing between them, so a reminder block and the
|
|
1257
|
+
// user's words arrive as one text. Adapted whole first, then each complete
|
|
1258
|
+
// reminder in it as the block it was; the rest stays as it is.
|
|
1259
|
+
const REMINDER_BLOCK = /<system-reminder>\n[\s\S]*?\n<\/system-reminder>/g
|
|
1260
|
+
function userText(text) {
|
|
1261
|
+
const adapted = userBlockText(text)
|
|
1262
|
+
if (adapted !== text || !text.includes("<system-reminder>\n")) return adapted
|
|
1263
|
+
return text.replace(REMINDER_BLOCK, (block) => userBlockText(block))
|
|
1264
|
+
}
|
|
1265
|
+
|
|
1194
1266
|
function anthropicBody(body) {
|
|
1195
1267
|
let request
|
|
1196
1268
|
try {
|
|
@@ -1226,9 +1298,7 @@ function anthropicBody(body) {
|
|
|
1226
1298
|
if (block?.type === "text") {
|
|
1227
1299
|
// The gateway may join the identity and instructions into one block.
|
|
1228
1300
|
// 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))
|
|
1301
|
+
const adapted = promptText(block.text)
|
|
1232
1302
|
if (adapted !== block.text) {
|
|
1233
1303
|
block.text = adapted
|
|
1234
1304
|
changed = true
|
|
@@ -1256,7 +1326,7 @@ function anthropicBody(body) {
|
|
|
1256
1326
|
|
|
1257
1327
|
for (const message of request.messages) {
|
|
1258
1328
|
if (message?.role === "system" && typeof message.content === "string") {
|
|
1259
|
-
const adapted =
|
|
1329
|
+
const adapted = systemMessageText(message.content)
|
|
1260
1330
|
if (adapted !== message.content) {
|
|
1261
1331
|
message.content = adapted
|
|
1262
1332
|
changed = true
|
|
@@ -1269,7 +1339,7 @@ function anthropicBody(body) {
|
|
|
1269
1339
|
if (message?.role === "system" && Array.isArray(message.content)) {
|
|
1270
1340
|
for (const block of message.content) {
|
|
1271
1341
|
if (block?.type !== "text" || typeof block.text !== "string") continue
|
|
1272
|
-
const adapted =
|
|
1342
|
+
const adapted = systemMessageText(block.text)
|
|
1273
1343
|
if (adapted !== block.text) {
|
|
1274
1344
|
block.text = adapted
|
|
1275
1345
|
changed = true
|
|
@@ -1308,36 +1378,10 @@ function anthropicBody(body) {
|
|
|
1308
1378
|
continue
|
|
1309
1379
|
}
|
|
1310
1380
|
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:")
|
|
1381
|
+
const adapted = userBlockText(block.text)
|
|
1382
|
+
if (adapted !== block.text) {
|
|
1383
|
+
block.text = adapted
|
|
1334
1384
|
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
1385
|
}
|
|
1342
1386
|
}
|
|
1343
1387
|
}
|