@magpie-community/opencode-workbuddy-auth 0.1.14 → 0.1.15

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 (3) hide show
  1. package/README.md +5 -0
  2. package/index.mjs +60 -4
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -82,6 +82,11 @@ Each chat carries WorkBuddy's headers:
82
82
  A chat that doesn't open with a system message is given WorkBuddy's
83
83
  default one, as the app does.
84
84
 
85
+ The loader also translates OpenAI-compatible request spellings WorkBuddy
86
+ doesn't take: `max_completion_tokens` becomes `max_tokens`, `developer`
87
+ messages become `system`, and an object `tool_choice` becomes WorkBuddy's
88
+ string form.
89
+
85
90
  A chat's tools are given shapes WorkBuddy's models take. Without them
86
91
  WorkBuddy AI answers 400 "Invalid request parameters" (11133), DeepSeek's
87
92
  models to each shape below and Kimi's, GPT's, Gemini's and GLM's to some:
package/index.mjs CHANGED
@@ -487,8 +487,11 @@ function unflag(messages) {
487
487
  }
488
488
 
489
489
  // withSystem gives a chat that doesn't open with a system message
490
- // WorkBuddy's default one, as its app does, and takes FLAGGED's words out
491
- // of it.
490
+ // WorkBuddy's default one, as its app does, takes FLAGGED's words out of it,
491
+ // and applies the request spellings WorkBuddy's chat endpoint accepts. The
492
+ // OpenAI-compatible client normally uses max_completion_tokens and may call a
493
+ // reasoning prompt developer; WorkBuddy's endpoint takes max_tokens and
494
+ // system instead.
492
495
  function withSystem(body) {
493
496
  // a body magpie or OpenCode hands over as bytes is read as the text it is
494
497
  if (body instanceof ArrayBuffer || ArrayBuffer.isView(body)) body = new TextDecoder().decode(body)
@@ -497,6 +500,16 @@ function withSystem(body) {
497
500
  const b = JSON.parse(body)
498
501
  if (!Array.isArray(b?.messages) || !b.messages.length) return body
499
502
  let changed = unflag(b.messages)
503
+ if (b.max_completion_tokens !== undefined) {
504
+ if (b.max_tokens === undefined) b.max_tokens = b.max_completion_tokens
505
+ delete b.max_completion_tokens
506
+ changed = true
507
+ }
508
+ for (const m of b.messages) {
509
+ if (m?.role !== "developer") continue
510
+ m.role = "system"
511
+ changed = true
512
+ }
500
513
  if (b.messages[0]?.role !== "system") {
501
514
  b.messages.unshift({ role: "system", content: "You are a helpful assistant." })
502
515
  changed = true
@@ -507,6 +520,49 @@ function withSystem(body) {
507
520
  }
508
521
  }
509
522
 
523
+ // normalizeToolChoice converts the object form emitted by OpenAI clients to
524
+ // WorkBuddy's string form. It runs after fitted(), so a named choice follows a
525
+ // tool name that was made safe for WorkBuddy.
526
+ function normalizeToolChoice(body) {
527
+ if (typeof body !== "string") return body
528
+ try {
529
+ const b = JSON.parse(body)
530
+ if (!b || typeof b !== "object" || Array.isArray(b) || !("tool_choice" in b)) return body
531
+ const choice = b.tool_choice
532
+ if (typeof choice === "string") {
533
+ if (choice.trim().toLowerCase() === "none") {
534
+ delete b.tool_choice
535
+ delete b.tools
536
+ delete b.functions
537
+ }
538
+ return JSON.stringify(b)
539
+ }
540
+ if (!choice || typeof choice !== "object" || Array.isArray(choice)) {
541
+ delete b.tool_choice
542
+ return JSON.stringify(b)
543
+ }
544
+ const type = typeof choice.type === "string" ? choice.type.trim().toLowerCase() : ""
545
+ if (type === "none") {
546
+ delete b.tool_choice
547
+ delete b.tools
548
+ delete b.functions
549
+ } else if (type === "auto" || type === "required") {
550
+ b.tool_choice = type
551
+ } else if (type === "function") {
552
+ const fn = choice.function && typeof choice.function === "object" && !Array.isArray(choice.function)
553
+ ? choice.function
554
+ : undefined
555
+ const name = typeof fn?.name === "string" ? fn.name.trim() : ""
556
+ b.tool_choice = name || "auto"
557
+ } else {
558
+ delete b.tool_choice
559
+ }
560
+ return JSON.stringify(b)
561
+ } catch {
562
+ return body
563
+ }
564
+ }
565
+
510
566
  // ---- tools WorkBuddy takes ----------------------------------------------------
511
567
 
512
568
  // WorkBuddy AI answers 400 code 11133 "Invalid request parameters" (the
@@ -1220,7 +1276,7 @@ function makePlugin(site) {
1220
1276
  if (body === undefined && req) body = await req.clone().text()
1221
1277
  body = withSystem(body)
1222
1278
  const fit = fitted(body)
1223
- body = fit.body
1279
+ body = normalizeToolChoice(fit.body)
1224
1280
  attend(headers, body, session)
1225
1281
  const res = await fetch(req ? req.url : input, { ...init, method: init?.method ?? req?.method, headers, body })
1226
1282
  return res.status >= 400 ? explained(res) : kept(await renamed(res, fit.names))
@@ -1338,4 +1394,4 @@ export const WorkBuddyAuthPlugin = makePlugin(SITES.workbuddy)
1338
1394
  export const WorkBuddyAIAuthPlugin = makePlugin(SITES["workbuddy-ai"])
1339
1395
 
1340
1396
  // for tests
1341
- export const _internal = { runsOutOf, packagesOf, endOf, enterpriseHeaders, enterpriseOf, liveModels, browserSignIn, withSystem, fitTools, fitted, renamed, unflagged, usageOf, desktopHeld, explained, REFUSED_HINT, EXHAUSTED_HINT, freeCredits, creditsOf, fresh, SITES, renewing, renewed, SESSION, attend, turnKey, conversationKey, signatureOf }
1397
+ export const _internal = { runsOutOf, packagesOf, endOf, enterpriseHeaders, enterpriseOf, liveModels, browserSignIn, withSystem, normalizeToolChoice, fitTools, fitted, renamed, unflagged, usageOf, desktopHeld, explained, REFUSED_HINT, EXHAUSTED_HINT, freeCredits, creditsOf, fresh, SITES, renewing, renewed, SESSION, attend, turnKey, conversationKey, signatureOf }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magpie-community/opencode-workbuddy-auth",
3
- "version": "0.1.14",
3
+ "version": "0.1.15",
4
4
  "description": "OpenCode provider plugins for WorkBuddy's plans: WorkBuddy (CodeBuddy, copilot.tencent.com) and WorkBuddy AI (www.workbuddy.ai)",
5
5
  "type": "module",
6
6
  "main": "./index.mjs",