@sixzjd/dsh-workbuddy-connect 0.3.0 → 0.3.2
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/README.md +2 -0
- package/client.js +275 -198
- package/lib/accounts.js +361 -0
- package/lib/adapter.js +1 -38
- package/lib/atrest.js +48 -2
- package/lib/auth.js +48 -7
- package/lib/cachestats.js +85 -0
- package/lib/index.js +171 -62
- package/lib/shim.js +43 -4
- package/lib/thinking.js +39 -0
- package/lib/upstream.js +46 -7
- package/package.json +1 -1
- package/lib/updates.js +0 -325
package/lib/index.js
CHANGED
|
@@ -27,10 +27,10 @@ import { WorkBuddyCredentialStore, WorkBuddyLoginClient, readJsonFile, writeJson
|
|
|
27
27
|
import { WorkBuddyCatalog, fallbackFor } from './catalog.js'
|
|
28
28
|
import { createWorkBuddyAdapter } from './adapter.js'
|
|
29
29
|
import { createWorkBuddyShim } from './shim.js'
|
|
30
|
-
import {
|
|
31
|
-
import {
|
|
30
|
+
import { accumulateCacheUsage, emptyCacheUsage } from './cachestats.js'
|
|
31
|
+
import { WorkBuddyUpstreamClient, modelWithCurrentPromotion, prepareChatBody, prepareInternationalChatBody, rateIsPaid, realmOf, regionOf } from './upstream.js'
|
|
32
|
+
import { readDesktopCredential, readDesktopCredentials, findDesktopExecutable } from './atrest.js'
|
|
32
33
|
import { discoverProfileDir, workbuddyPluginDataDir, workbuddyStateDir } from './paths.js'
|
|
33
|
-
import { checkForUpdate, updateSpecFrom } from './updates.js'
|
|
34
34
|
import { WORKBUDDY_VARIANTS, variantFor } from './variants.js'
|
|
35
35
|
|
|
36
36
|
export const name = 'llm-workbuddy'
|
|
@@ -72,6 +72,7 @@ function controlKeys() {
|
|
|
72
72
|
models: randomBytes(24).toString('hex'),
|
|
73
73
|
auth: randomBytes(24).toString('hex'),
|
|
74
74
|
update: randomBytes(24).toString('hex'),
|
|
75
|
+
accounts: randomBytes(24).toString('hex'),
|
|
75
76
|
}
|
|
76
77
|
return processKeys
|
|
77
78
|
}
|
|
@@ -145,6 +146,13 @@ class VariantRuntime {
|
|
|
145
146
|
this.catalog = new WorkBuddyCatalog({ region: variant.region, initial: fallbackFor(variant.region) })
|
|
146
147
|
this.store = new WorkBuddyCredentialStore({
|
|
147
148
|
variant,
|
|
149
|
+
// `dataDir` MUST be passed through. The store falls back to the *discovered
|
|
150
|
+
// profile's* directory when it is not told where to live, so omitting it
|
|
151
|
+
// makes a runtime constructed against any other directory write its
|
|
152
|
+
// credentials into the real profile — and, because saving claims the active
|
|
153
|
+
// slot, re-point that profile's active account. That is exactly how a test
|
|
154
|
+
// run against a temporary directory signed the real account out.
|
|
155
|
+
dataDir: this.dataDir,
|
|
148
156
|
refresh: credential => this.client.refreshToken(credential),
|
|
149
157
|
})
|
|
150
158
|
this.settings = readJsonFile(join(this.dataDir, variant.settingsFilename)) ?? {}
|
|
@@ -152,6 +160,14 @@ class VariantRuntime {
|
|
|
152
160
|
if (this.settings.useMaximumContextWindow === true) this.catalog.setUseMaximumContextWindow(true)
|
|
153
161
|
this.credits = undefined
|
|
154
162
|
this.creditsError = undefined
|
|
163
|
+
/**
|
|
164
|
+
* Running cache-hit accounting for this process' calls.
|
|
165
|
+
*
|
|
166
|
+
* In memory on purpose: it describes this session's traffic, and a rate
|
|
167
|
+
* carried over from last week would answer a different question than the one
|
|
168
|
+
* the card asks ("is my cache working right now").
|
|
169
|
+
*/
|
|
170
|
+
this.cacheUsage = emptyCacheUsage()
|
|
155
171
|
/**
|
|
156
172
|
* Whether the upstream has refused this credential since the last
|
|
157
173
|
* successful call.
|
|
@@ -280,6 +296,23 @@ class VariantRuntime {
|
|
|
280
296
|
*/
|
|
281
297
|
async status() {
|
|
282
298
|
const auth = await this.store.status()
|
|
299
|
+
// The account list is a summary (no tokens, no full uid — see
|
|
300
|
+
// WorkBuddyAccountStore#list) plus the count, which is what the card needs to
|
|
301
|
+
// decide whether a switcher is worth showing at all.
|
|
302
|
+
let accounts = []
|
|
303
|
+
try {
|
|
304
|
+
accounts = (await this.store.accounts.list()).map(account => ({
|
|
305
|
+
uidShort: account.uidShort,
|
|
306
|
+
hash: account.hash,
|
|
307
|
+
...account.nickname === undefined ? {} : { nickname: account.nickname },
|
|
308
|
+
expired: account.expired,
|
|
309
|
+
active: account.active,
|
|
310
|
+
}))
|
|
311
|
+
} catch {
|
|
312
|
+
// An unreadable account list is not worth failing the card over: it
|
|
313
|
+
// degrades to "no switcher", which is the pre-upgrade behaviour.
|
|
314
|
+
accounts = []
|
|
315
|
+
}
|
|
283
316
|
// `catalog.all()` already re-derives the current promotion; applying it a
|
|
284
317
|
// second time here would repeat its badge.
|
|
285
318
|
const disabled = this.catalog.disabledModels()
|
|
@@ -292,6 +325,12 @@ class VariantRuntime {
|
|
|
292
325
|
supportedContextWindows: current.supportedContextWindows,
|
|
293
326
|
supportsImages: current.supportsImages === true,
|
|
294
327
|
free: current.billing?.free === true,
|
|
328
|
+
// "Does this model draw on the credit balance". Re-derived from the
|
|
329
|
+
// stored rate rather than read back as a flag: the saved catalog holds
|
|
330
|
+
// records written by whatever version produced them, and one written
|
|
331
|
+
// before this flag existed has no `paid` key — reading it would report
|
|
332
|
+
// every rated model as free. See rateIsPaid.
|
|
333
|
+
paid: rateIsPaid(current.billing),
|
|
295
334
|
rate: current.billing?.rateUnknown === true ? null : current.billing?.credits,
|
|
296
335
|
rateUnknown: current.billing?.rateUnknown === true,
|
|
297
336
|
badges: current.billing?.badges ?? [],
|
|
@@ -309,6 +348,7 @@ class VariantRuntime {
|
|
|
309
348
|
// spawns the app, so it is cheap enough for a status poll.
|
|
310
349
|
desktopAvailable: findDesktopExecutable() !== undefined,
|
|
311
350
|
auth: { ...auth, sessionStale: this.sessionStale, accountRestricted: this.accountRestricted },
|
|
351
|
+
accounts,
|
|
312
352
|
catalog: {
|
|
313
353
|
source: this.catalog.source,
|
|
314
354
|
fetchedAt: this.catalog.fetchedAtMs,
|
|
@@ -317,6 +357,10 @@ class VariantRuntime {
|
|
|
317
357
|
},
|
|
318
358
|
credits: this.credits ?? null,
|
|
319
359
|
creditsError: this.creditsError ?? null,
|
|
360
|
+
// What the prefix cache actually did this session. `turns === 0` means
|
|
361
|
+
// no call has been made since the process started, which is a different
|
|
362
|
+
// statement from "the cache is missing everything".
|
|
363
|
+
cache: { ...this.cacheUsage },
|
|
320
364
|
useMaximumContextWindow: this.catalog.useMaximumContextWindow === true,
|
|
321
365
|
}
|
|
322
366
|
}
|
|
@@ -337,7 +381,7 @@ export async function apply(ctx, config = {}) {
|
|
|
337
381
|
const profileDir = discoverProfileDir()
|
|
338
382
|
const client = new WorkBuddyUpstreamClient()
|
|
339
383
|
const login = new WorkBuddyLoginClient()
|
|
340
|
-
const { login: loginKey, models: modelsKey, auth: authKey, update: updateKey } = controlKeys()
|
|
384
|
+
const { login: loginKey, models: modelsKey, auth: authKey, update: updateKey, accounts: accountsKey } = controlKeys()
|
|
341
385
|
|
|
342
386
|
const runtimes = new Map()
|
|
343
387
|
const adapters = new Map()
|
|
@@ -380,6 +424,12 @@ export async function apply(ctx, config = {}) {
|
|
|
380
424
|
// The upstream refusing the stored credential is the only trustworthy
|
|
381
425
|
// signal that the session was rotated away; the card turns it into a
|
|
382
426
|
// visible hint instead of a stale "signed in" badge.
|
|
427
|
+
// Accounting only: the shim reports each turn's usage so the card can show
|
|
428
|
+
// what the prefix cache actually did, instead of leaving the number to be
|
|
429
|
+
// inferred from a bill.
|
|
430
|
+
onUsage: usage => {
|
|
431
|
+
runtime.cacheUsage = accumulateCacheUsage(runtime.cacheUsage, usage)
|
|
432
|
+
},
|
|
383
433
|
onSessionDead: () => runtime.markSessionStale(),
|
|
384
434
|
// A restriction is a different failure with a different fix, so it gets
|
|
385
435
|
// its own mark rather than sharing the stale-session one.
|
|
@@ -474,63 +524,6 @@ export async function apply(ctx, config = {}) {
|
|
|
474
524
|
})
|
|
475
525
|
|
|
476
526
|
//#region card routes
|
|
477
|
-
/**
|
|
478
|
-
* The last update report plus when it was taken.
|
|
479
|
-
*
|
|
480
|
-
* A `git ls-remote` costs a round trip, and the card polls its status for
|
|
481
|
-
* unrelated reasons (credits, catalog), so the answer is cached for a while
|
|
482
|
-
* and refreshed only when the user asks or the cache ages out. Both callers
|
|
483
|
-
* share one in-flight promise: two panels opening at once must not launch two
|
|
484
|
-
* checks.
|
|
485
|
-
*/
|
|
486
|
-
const updateCache = { report: undefined, at: 0, inFlight: undefined }
|
|
487
|
-
const UPDATE_TTL_MS = 6 * 60 * 60 * 1000
|
|
488
|
-
|
|
489
|
-
async function updateReport(profileDir, force) {
|
|
490
|
-
const fresh = updateCache.report !== undefined && Date.now() - updateCache.at < UPDATE_TTL_MS
|
|
491
|
-
if (fresh && force !== true) return updateCache.report
|
|
492
|
-
if (updateCache.inFlight !== undefined) return updateCache.inFlight
|
|
493
|
-
updateCache.inFlight = (async () => {
|
|
494
|
-
try {
|
|
495
|
-
// The profile that hosts this plugin, not the one the card happens to
|
|
496
|
-
// be drawn in: the two are the same for a normal install, and the
|
|
497
|
-
// discovered directory is the one whose lockfile records this copy.
|
|
498
|
-
const report = await checkForUpdate(profileDir)
|
|
499
|
-
updateCache.report = report
|
|
500
|
-
updateCache.at = Date.now()
|
|
501
|
-
return report
|
|
502
|
-
} finally {
|
|
503
|
-
updateCache.inFlight = undefined
|
|
504
|
-
}
|
|
505
|
-
})()
|
|
506
|
-
return updateCache.inFlight
|
|
507
|
-
}
|
|
508
|
-
|
|
509
|
-
const updateHandler = async (req, res) => {
|
|
510
|
-
if (req.method !== 'GET' || !routeGuard(req, updateKey)) {
|
|
511
|
-
res.statusCode = req.method === 'GET' ? 403 : 405
|
|
512
|
-
res.end()
|
|
513
|
-
return
|
|
514
|
-
}
|
|
515
|
-
let report
|
|
516
|
-
try {
|
|
517
|
-
report = await updateReport(profileDir, new URL(req.url ?? '/', 'http://127.0.0.1').searchParams.get('refresh') === '1')
|
|
518
|
-
} catch (error) {
|
|
519
|
-
report = { state: 'unknown', reason: safeMessage(error) }
|
|
520
|
-
}
|
|
521
|
-
// No key is echoed here: this route only *reads*, and the install it
|
|
522
|
-
// enables goes through the plugin manager's own remote service, which
|
|
523
|
-
// carries its own authorization. Returning a control key would hand any
|
|
524
|
-
// reader of the card a credential it does not need.
|
|
525
|
-
const spec = updateSpecFrom(report)
|
|
526
|
-
const body = JSON.stringify({
|
|
527
|
-
...report,
|
|
528
|
-
...spec === undefined ? {} : { targetSpec: spec },
|
|
529
|
-
})
|
|
530
|
-
res.writeHead(200, { 'content-type': 'application/json', 'cache-control': 'no-store' })
|
|
531
|
-
res.end(body)
|
|
532
|
-
}
|
|
533
|
-
|
|
534
527
|
const statusHandler = (req, res) => {
|
|
535
528
|
if (req.method !== 'GET') {
|
|
536
529
|
res.statusCode = 405
|
|
@@ -563,6 +556,7 @@ export async function apply(ctx, config = {}) {
|
|
|
563
556
|
// other card route: the key travels with the status document the card
|
|
564
557
|
// already fetches, which is how `loginKey`/`modelsKey` reach it too.
|
|
565
558
|
updateKey,
|
|
559
|
+
accountsKey,
|
|
566
560
|
})
|
|
567
561
|
res.writeHead(200, { 'content-type': 'application/json', 'cache-control': 'no-store' })
|
|
568
562
|
res.end(payload)
|
|
@@ -756,6 +750,121 @@ export async function apply(ctx, config = {}) {
|
|
|
756
750
|
res.end()
|
|
757
751
|
}
|
|
758
752
|
|
|
753
|
+
/**
|
|
754
|
+
* The account switcher's write path.
|
|
755
|
+
*
|
|
756
|
+
* Every action re-reads the disk and then refreshes what the switch changed —
|
|
757
|
+
* catalog and credits belong to the account, so serving the previous account's
|
|
758
|
+
* roster after a switch would be a lie the card cannot detect.
|
|
759
|
+
*/
|
|
760
|
+
const accountsHandler = async (req, res) => {
|
|
761
|
+
if (req.method !== 'POST' || !routeGuard(req, accountsKey)) {
|
|
762
|
+
res.statusCode = req.method === 'POST' ? 403 : 405
|
|
763
|
+
res.end()
|
|
764
|
+
return
|
|
765
|
+
}
|
|
766
|
+
const text = await readRouteBody(req)
|
|
767
|
+
let action
|
|
768
|
+
try {
|
|
769
|
+
action = JSON.parse(text ?? '')
|
|
770
|
+
} catch {
|
|
771
|
+
res.statusCode = 400
|
|
772
|
+
res.end()
|
|
773
|
+
return
|
|
774
|
+
}
|
|
775
|
+
const provider = typeof action?.provider === 'string' ? action.provider : ''
|
|
776
|
+
const runtime = runtimes.get(provider)
|
|
777
|
+
if (runtime === undefined) {
|
|
778
|
+
res.statusCode = 400
|
|
779
|
+
res.end()
|
|
780
|
+
return
|
|
781
|
+
}
|
|
782
|
+
const reply = (status, body) => {
|
|
783
|
+
res.writeHead(status, { 'content-type': 'application/json', 'cache-control': 'no-store' })
|
|
784
|
+
res.end(JSON.stringify(body))
|
|
785
|
+
}
|
|
786
|
+
try {
|
|
787
|
+
if (action?.action === 'activate') {
|
|
788
|
+
const handle = typeof action.hash === 'string' ? action.hash : ''
|
|
789
|
+
await runtime.store.accounts.activate(handle)
|
|
790
|
+
// A switch replaces the credential, so the stale mark describes the old
|
|
791
|
+
// account and must not survive it. An account restriction is kept: it
|
|
792
|
+
// belongs to the account, and the new one is only cleared by its own
|
|
793
|
+
// successful call.
|
|
794
|
+
runtime.markSessionFresh()
|
|
795
|
+
runtime.catalog.setVisible(true)
|
|
796
|
+
runtime.credits = undefined
|
|
797
|
+
runtime.creditsError = undefined
|
|
798
|
+
await runtime.refreshCatalog()
|
|
799
|
+
await runtime.refreshCredits()
|
|
800
|
+
refresh()
|
|
801
|
+
reply(200, { ok: true })
|
|
802
|
+
return
|
|
803
|
+
}
|
|
804
|
+
if (action?.action === 'remove') {
|
|
805
|
+
const handle = typeof action.hash === 'string' ? action.hash : ''
|
|
806
|
+
await runtime.store.accounts.remove(handle)
|
|
807
|
+
// `remove` promotes another account when it took the active slot; if none
|
|
808
|
+
// is left the variant really is signed out.
|
|
809
|
+
if (await runtime.store.accounts.active() === undefined) {
|
|
810
|
+
// The account list is empty: also drop the downgrade copy, or the next
|
|
811
|
+
// read migrates the just-deleted account straight back. logout()
|
|
812
|
+
// clears both; it is idempotent, so it is safe after remove().
|
|
813
|
+
await runtime.store.logout()
|
|
814
|
+
runtime.catalog.setVisible(false)
|
|
815
|
+
runtime.credits = undefined
|
|
816
|
+
} else {
|
|
817
|
+
await runtime.refreshCatalog()
|
|
818
|
+
await runtime.refreshCredits()
|
|
819
|
+
}
|
|
820
|
+
refresh()
|
|
821
|
+
reply(200, { ok: true })
|
|
822
|
+
return
|
|
823
|
+
}
|
|
824
|
+
if (action?.action === 'clear') {
|
|
825
|
+
await runtime.store.logout()
|
|
826
|
+
runtime.catalog.setVisible(false)
|
|
827
|
+
runtime.credits = undefined
|
|
828
|
+
runtime.creditsError = undefined
|
|
829
|
+
runtime.markSessionFresh()
|
|
830
|
+
refresh()
|
|
831
|
+
reply(200, { ok: true })
|
|
832
|
+
return
|
|
833
|
+
}
|
|
834
|
+
if (action?.action === 'adopt-desktop') {
|
|
835
|
+
// Import one of the sessions the desktop app already holds.
|
|
836
|
+
const wanted = typeof action.uid === 'string' ? action.uid : ''
|
|
837
|
+
const found = await readDesktopCredentials()
|
|
838
|
+
const candidates = found.filter(credential => realmOf(credential) === runtime.variant.region)
|
|
839
|
+
if (candidates.length === 0) {
|
|
840
|
+
reply(200, { ok: false, error: 'no desktop session found for this edition' })
|
|
841
|
+
return
|
|
842
|
+
}
|
|
843
|
+
const chosen = wanted === ''
|
|
844
|
+
? candidates[0]
|
|
845
|
+
: candidates.find(credential => credential.uid === wanted)
|
|
846
|
+
if (chosen === undefined) {
|
|
847
|
+
reply(200, { ok: false, error: 'that desktop account is no longer stored' })
|
|
848
|
+
return
|
|
849
|
+
}
|
|
850
|
+
await runtime.store.save({ ...chosen, region: runtime.variant.region })
|
|
851
|
+
runtime.markSessionFresh()
|
|
852
|
+
runtime.catalog.setVisible(true)
|
|
853
|
+
await runtime.refreshCatalog()
|
|
854
|
+
await runtime.refreshCredits()
|
|
855
|
+
refresh()
|
|
856
|
+
reply(200, { ok: true })
|
|
857
|
+
return
|
|
858
|
+
}
|
|
859
|
+
res.statusCode = 400
|
|
860
|
+
res.end()
|
|
861
|
+
} catch (error) {
|
|
862
|
+
// 200 with `ok: false`: the card shows the reason, and a transport-level
|
|
863
|
+
// error status would surface as a generic failure with no explanation.
|
|
864
|
+
reply(200, { ok: false, error: safeMessage(error) })
|
|
865
|
+
}
|
|
866
|
+
}
|
|
867
|
+
|
|
759
868
|
const authHandler = async (req, res) => {
|
|
760
869
|
if (req.method !== 'POST' || !routeGuard(req, authKey)) {
|
|
761
870
|
res.statusCode = req.method === 'POST' ? 403 : 405
|
|
@@ -841,7 +950,7 @@ export async function apply(ctx, config = {}) {
|
|
|
841
950
|
const b = webCtx.webServer.register({ kind: 'exact', path: `${ROUTE_BASE}/login`, handler: loginHandler })
|
|
842
951
|
const c = webCtx.webServer.register({ kind: 'exact', path: `${ROUTE_BASE}/models`, handler: modelsHandler })
|
|
843
952
|
const d = webCtx.webServer.register({ kind: 'exact', path: `${ROUTE_BASE}/auth`, handler: authHandler })
|
|
844
|
-
const e = webCtx.webServer.register({ kind: 'exact', path: `${ROUTE_BASE}/
|
|
953
|
+
const e = webCtx.webServer.register({ kind: 'exact', path: `${ROUTE_BASE}/accounts`, handler: accountsHandler })
|
|
845
954
|
return () => {
|
|
846
955
|
a()
|
|
847
956
|
b()
|
package/lib/shim.js
CHANGED
|
@@ -24,6 +24,7 @@ import { createHash, randomBytes, timingSafeEqual } from 'node:crypto'
|
|
|
24
24
|
import { createServer } from 'node:http'
|
|
25
25
|
import { writeFileSync } from 'node:fs'
|
|
26
26
|
import { describeAccountRestriction, describeUpstreamFailure } from './upstream.js'
|
|
27
|
+
import { usageFromSseText } from './cachestats.js'
|
|
27
28
|
|
|
28
29
|
/** The shim's preferred fixed loopback port (env-overridable; ephemeral on collision). */
|
|
29
30
|
const SHIM_PORT = Number(process.env.DSH_WORKBUDDY_CONNECT_PORT ?? 39319)
|
|
@@ -225,8 +226,25 @@ function mergeToolCallDeltas(deltas) {
|
|
|
225
226
|
return merged
|
|
226
227
|
}
|
|
227
228
|
|
|
229
|
+
/**
|
|
230
|
+
* Hand a usage block to the caller, never letting accounting break a response.
|
|
231
|
+
*
|
|
232
|
+
* This runs inside the relay loop while the client is watching tokens arrive, so
|
|
233
|
+
* any failure here would truncate a live answer. The card's numbers are worth
|
|
234
|
+
* nothing next to that.
|
|
235
|
+
*/
|
|
236
|
+
function reportUsage(text, onUsage) {
|
|
237
|
+
if (typeof onUsage !== 'function') return
|
|
238
|
+
try {
|
|
239
|
+
const usage = usageFromSseText(text)
|
|
240
|
+
if (usage !== undefined) onUsage(usage)
|
|
241
|
+
} catch {
|
|
242
|
+
// Accounting is best-effort by design.
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
228
246
|
/** Pump an upstream SSE stream straight through to the client. */
|
|
229
|
-
async function relayStream(res, upstreamRes, signal) {
|
|
247
|
+
async function relayStream(res, upstreamRes, signal, onUsage) {
|
|
230
248
|
res.writeHead(200, {
|
|
231
249
|
'Content-Type': 'text/event-stream',
|
|
232
250
|
'Cache-Control': 'no-cache',
|
|
@@ -255,11 +273,20 @@ async function relayStream(res, upstreamRes, signal) {
|
|
|
255
273
|
// block never gets rewritten wrongly.
|
|
256
274
|
const cut = buf.lastIndexOf('\n\n')
|
|
257
275
|
if (cut !== -1) {
|
|
258
|
-
|
|
276
|
+
const block = buf.slice(0, cut + 2)
|
|
277
|
+
res.write(block)
|
|
278
|
+
// Scanned from the block that is handed to the client, so the accounting
|
|
279
|
+
// sees exactly what the response contained, and read before the write
|
|
280
|
+
// can throw. This is the only path the upstream serves, so without it
|
|
281
|
+
// the card could never show a hit rate at all.
|
|
282
|
+
reportUsage(block, onUsage)
|
|
259
283
|
buf = buf.slice(cut + 2)
|
|
260
284
|
}
|
|
261
285
|
}
|
|
262
|
-
if (buf !== '')
|
|
286
|
+
if (buf !== '') {
|
|
287
|
+
res.write(buf)
|
|
288
|
+
reportUsage(buf, onUsage)
|
|
289
|
+
}
|
|
263
290
|
} catch (error) {
|
|
264
291
|
options.logger?.warn?.('dsh-workbuddy-connect: upstream stream failed mid-flight', error)
|
|
265
292
|
} finally {
|
|
@@ -281,6 +308,7 @@ async function relayStream(res, upstreamRes, signal) {
|
|
|
281
308
|
* @param {() => void} [options.onSessionDead] Called when the upstream refuses the stored credential.
|
|
282
309
|
* @param {(reason: string) => void} [options.onAccountRestricted] Called when the upstream restricts the account.
|
|
283
310
|
* @param {() => void} [options.onSessionFresh] Called after a call the upstream accepted.
|
|
311
|
+
* @param {(usage: object) => void} [options.onUsage] Called with each turn's `usage` block.
|
|
284
312
|
* @param {string} [options.dumpBodyPath] Write the exact outbound body here (diagnostics; off by default).
|
|
285
313
|
*/
|
|
286
314
|
export function createWorkBuddyShim(options) {
|
|
@@ -432,10 +460,21 @@ export function createWorkBuddyShim(options) {
|
|
|
432
460
|
}
|
|
433
461
|
})()
|
|
434
462
|
if (wantsStream) {
|
|
435
|
-
await relayStream(res, result.response, controller.signal)
|
|
463
|
+
await relayStream(res, result.response, controller.signal, options.onUsage)
|
|
436
464
|
return
|
|
437
465
|
}
|
|
438
466
|
const content = await collectCompletion(result.response, controller.signal)
|
|
467
|
+
// Passed straight through: `reportUsage` parses SSE text, and this usage
|
|
468
|
+
// is already an object. Routing it through the SSE scanner found nothing,
|
|
469
|
+
// which the integration check caught — the non-streaming path silently
|
|
470
|
+
// reported no usage at all.
|
|
471
|
+
if (content?.usage !== undefined && typeof options.onUsage === 'function') {
|
|
472
|
+
try {
|
|
473
|
+
options.onUsage(content.usage)
|
|
474
|
+
} catch {
|
|
475
|
+
// Accounting must never cost the caller their response.
|
|
476
|
+
}
|
|
477
|
+
}
|
|
439
478
|
writeJson(res, 200, content)
|
|
440
479
|
} finally {
|
|
441
480
|
res.removeListener('close', onClientClose)
|
package/lib/thinking.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Resolve a model's reasoning capability into pi-ai's `thinkingLevelMap`.
|
|
3
|
+
*
|
|
4
|
+
* The upstream's declared set wins outright: when it names a non-empty
|
|
5
|
+
* `supportedEfforts`, exactly those values are offered and nothing else. A
|
|
6
|
+
* row the upstream left undeclared gets no control at all — its selectable set
|
|
7
|
+
* is client-side knowledge the catalog does not carry.
|
|
8
|
+
*
|
|
9
|
+
* The `off` slot is pi-ai's default level, and it is offered only when the
|
|
10
|
+
* upstream declares `canDisableThinking: true`. Without that declaration the
|
|
11
|
+
* slot is `null`, which hides it: a model that cannot disable thinking would
|
|
12
|
+
* otherwise present "off" next to its only real level, with both putting the
|
|
13
|
+
* same value on the wire — a control that looks like a choice and is not one.
|
|
14
|
+
*/
|
|
15
|
+
export function reasoningFields(info) {
|
|
16
|
+
const reasoning = info.reasoning
|
|
17
|
+
if (reasoning === undefined || reasoning.supports !== true) {
|
|
18
|
+
// Not a reasoning model: pi-ai reads a falsy `reasoning` as "off only".
|
|
19
|
+
return { reasoning: false }
|
|
20
|
+
}
|
|
21
|
+
const declared = reasoning.supportedEfforts
|
|
22
|
+
if (declared === undefined || declared.length === 0) {
|
|
23
|
+
// Undeclared: no thinking control, and no `reasoning_effort` on the wire.
|
|
24
|
+
return { reasoning: false }
|
|
25
|
+
}
|
|
26
|
+
const fallback = typeof reasoning.defaultEffort === 'string' ? reasoning.defaultEffort : undefined
|
|
27
|
+
return {
|
|
28
|
+
reasoning: true,
|
|
29
|
+
thinkingLevelMap: {
|
|
30
|
+
off: reasoning.canDisableThinking === true ? fallback ?? null : null,
|
|
31
|
+
minimal: null,
|
|
32
|
+
low: declared.includes('low') ? 'low' : null,
|
|
33
|
+
medium: declared.includes('medium') ? 'medium' : null,
|
|
34
|
+
high: declared.includes('high') ? 'high' : null,
|
|
35
|
+
xhigh: declared.includes('xhigh') ? 'xhigh' : null,
|
|
36
|
+
max: declared.includes('max') ? 'max' : null,
|
|
37
|
+
},
|
|
38
|
+
}
|
|
39
|
+
}
|
package/lib/upstream.js
CHANGED
|
@@ -236,7 +236,16 @@ function resolveUpstreamReasoning(wrapped) {
|
|
|
236
236
|
/** Parse the upstream's promotional badge list into plain labels. */
|
|
237
237
|
function resolveUpstreamBadges(wrapped) {
|
|
238
238
|
const raw = wrapped['promotions'] ?? wrapped['promotionTags']
|
|
239
|
-
|
|
239
|
+
// Copied, never the upstream's own array: pushing into that would mutate the
|
|
240
|
+
// parsed document, so parsing the same catalog twice would accumulate labels.
|
|
241
|
+
const list = Array.isArray(raw) ? [...raw] : typeof raw === 'string' ? [raw] : []
|
|
242
|
+
// The live CN documents publish their labels in `tags`, not in `promotions`
|
|
243
|
+
// (measured 2026-11-16: hy3 and hy4free arrive as ["craft",
|
|
244
|
+
// "badge:限时免费:#FF0000"]). Only the `badge:`-prefixed entries are labels
|
|
245
|
+
// meant for a human — `craft`, `text-to-image` and `enterprise:selfhost` say
|
|
246
|
+
// what a model does — so the rest is deliberately left out.
|
|
247
|
+
const tags = Array.isArray(wrapped['tags']) ? wrapped['tags'] : []
|
|
248
|
+
list.push(...tags.filter(tag => typeof tag === 'string' && tag.startsWith(BADGE_PREFIX)))
|
|
240
249
|
const badges = []
|
|
241
250
|
for (const entry of list) {
|
|
242
251
|
if (typeof entry !== 'string') continue
|
|
@@ -246,18 +255,44 @@ function resolveUpstreamBadges(wrapped) {
|
|
|
246
255
|
return badges
|
|
247
256
|
}
|
|
248
257
|
|
|
258
|
+
/**
|
|
259
|
+
* Whether a billing record describes a model that draws on the credit balance.
|
|
260
|
+
*
|
|
261
|
+
* The upstream publishes no membership or tier field per model — `/v3/config`,
|
|
262
|
+
* both legacy personal-models documents, the billing packages and the account
|
|
263
|
+
* JWT were all read on 2026-11-16 and carry none — so the rate is the only
|
|
264
|
+
* per-model statement of "this costs credits" that exists. Deriving the answer
|
|
265
|
+
* from the rate keeps it a fact the upstream states instead of a hand-kept list
|
|
266
|
+
* that silently rots.
|
|
267
|
+
*
|
|
268
|
+
* Takes the billing record rather than a parsed model so the *saved* catalog can
|
|
269
|
+
* use it too: that file holds normalized records written by whatever version
|
|
270
|
+
* produced them, and an older one has no `paid` key at all. Re-deriving from the
|
|
271
|
+
* `credits` that IS stored is what keeps a x0.29 model from reading as free
|
|
272
|
+
* after a downgrade-to-previous-version.
|
|
273
|
+
*/
|
|
274
|
+
export function rateIsPaid(billing) {
|
|
275
|
+
// A lapsed promotion leaves its stale rate behind alongside `rateUnknown`.
|
|
276
|
+
// That number is not a current price, so it cannot support a tier claim.
|
|
277
|
+
if (billing?.rateUnknown === true) return false
|
|
278
|
+
const credits = billing?.credits
|
|
279
|
+
if (typeof credits !== 'string' || !credits.startsWith('x')) return false
|
|
280
|
+
const value = Number(credits.slice(1))
|
|
281
|
+
return Number.isFinite(value) && value > 0
|
|
282
|
+
}
|
|
283
|
+
|
|
249
284
|
/** Parse the upstream's billing block into a normalized billing record. */
|
|
250
285
|
function resolveUpstreamBilling(wrapped) {
|
|
251
286
|
const rawCredits = typeof wrapped['credits'] === 'string' ? wrapped['credits'] : undefined
|
|
252
287
|
const rate = normalizeCredits(rawCredits)
|
|
253
288
|
const badges = resolveUpstreamBadges(wrapped)
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
},
|
|
289
|
+
const value = rate === undefined ? Number.NaN : Number(rate.slice(1))
|
|
290
|
+
const billing = {
|
|
291
|
+
...rate === undefined ? {} : { credits: rate },
|
|
292
|
+
...badges.length === 0 ? {} : { badges },
|
|
293
|
+
free: rate !== undefined && value === 0,
|
|
260
294
|
}
|
|
295
|
+
return { billing: { ...billing, paid: rateIsPaid(billing) } }
|
|
261
296
|
}
|
|
262
297
|
|
|
263
298
|
/**
|
|
@@ -1523,6 +1558,9 @@ export function modelWithCurrentPromotion(model, now = Date.now()) {
|
|
|
1523
1558
|
...model,
|
|
1524
1559
|
billing: {
|
|
1525
1560
|
free: false,
|
|
1561
|
+
// The price is not known, so neither is "paid": the surface shows the
|
|
1562
|
+
// unknown-price notice instead of guessing at a tier.
|
|
1563
|
+
paid: false,
|
|
1526
1564
|
rateUnknown: true,
|
|
1527
1565
|
...staleRate === undefined ? {} : { credits: staleRate },
|
|
1528
1566
|
...expiredLabels.length === 0 ? {} : { expiredPromotions: expiredLabels },
|
|
@@ -1542,6 +1580,7 @@ export function modelWithCurrentPromotion(model, now = Date.now()) {
|
|
|
1542
1580
|
...model.billing,
|
|
1543
1581
|
credits: `x${value.toFixed(2)}`,
|
|
1544
1582
|
free: value === 0,
|
|
1583
|
+
paid: value > 0,
|
|
1545
1584
|
// De-duplicated, so applying the promotion to an already-promoted row is
|
|
1546
1585
|
// a no-op: the catalog re-derives the promotion on every read, and a
|
|
1547
1586
|
// label that accumulated would read `Free now · Free now` in the picker.
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"displayName": "WorkBuddy",
|
|
4
4
|
"description": "将 WorkBuddy(国际版 / 国内版)的模型接入 DeepSeek Harness — bring WorkBuddy (Global & CN) models into DeepSeek Harness.",
|
|
5
5
|
"icon": "./assets/icon.svg",
|
|
6
|
-
"version": "0.3.
|
|
6
|
+
"version": "0.3.2",
|
|
7
7
|
"author": "sunzhengnan",
|
|
8
8
|
"keywords": [
|
|
9
9
|
"dsh-plugin",
|