@sixzjd/dsh-workbuddy-connect 0.3.1 → 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 +274 -22
- 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 +172 -3
- 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/index.js
CHANGED
|
@@ -27,8 +27,9 @@ 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
34
|
import { WORKBUDDY_VARIANTS, variantFor } from './variants.js'
|
|
34
35
|
|
|
@@ -71,6 +72,7 @@ function controlKeys() {
|
|
|
71
72
|
models: randomBytes(24).toString('hex'),
|
|
72
73
|
auth: randomBytes(24).toString('hex'),
|
|
73
74
|
update: randomBytes(24).toString('hex'),
|
|
75
|
+
accounts: randomBytes(24).toString('hex'),
|
|
74
76
|
}
|
|
75
77
|
return processKeys
|
|
76
78
|
}
|
|
@@ -144,6 +146,13 @@ class VariantRuntime {
|
|
|
144
146
|
this.catalog = new WorkBuddyCatalog({ region: variant.region, initial: fallbackFor(variant.region) })
|
|
145
147
|
this.store = new WorkBuddyCredentialStore({
|
|
146
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,
|
|
147
156
|
refresh: credential => this.client.refreshToken(credential),
|
|
148
157
|
})
|
|
149
158
|
this.settings = readJsonFile(join(this.dataDir, variant.settingsFilename)) ?? {}
|
|
@@ -151,6 +160,14 @@ class VariantRuntime {
|
|
|
151
160
|
if (this.settings.useMaximumContextWindow === true) this.catalog.setUseMaximumContextWindow(true)
|
|
152
161
|
this.credits = undefined
|
|
153
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()
|
|
154
171
|
/**
|
|
155
172
|
* Whether the upstream has refused this credential since the last
|
|
156
173
|
* successful call.
|
|
@@ -279,6 +296,23 @@ class VariantRuntime {
|
|
|
279
296
|
*/
|
|
280
297
|
async status() {
|
|
281
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
|
+
}
|
|
282
316
|
// `catalog.all()` already re-derives the current promotion; applying it a
|
|
283
317
|
// second time here would repeat its badge.
|
|
284
318
|
const disabled = this.catalog.disabledModels()
|
|
@@ -291,6 +325,12 @@ class VariantRuntime {
|
|
|
291
325
|
supportedContextWindows: current.supportedContextWindows,
|
|
292
326
|
supportsImages: current.supportsImages === true,
|
|
293
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),
|
|
294
334
|
rate: current.billing?.rateUnknown === true ? null : current.billing?.credits,
|
|
295
335
|
rateUnknown: current.billing?.rateUnknown === true,
|
|
296
336
|
badges: current.billing?.badges ?? [],
|
|
@@ -308,6 +348,7 @@ class VariantRuntime {
|
|
|
308
348
|
// spawns the app, so it is cheap enough for a status poll.
|
|
309
349
|
desktopAvailable: findDesktopExecutable() !== undefined,
|
|
310
350
|
auth: { ...auth, sessionStale: this.sessionStale, accountRestricted: this.accountRestricted },
|
|
351
|
+
accounts,
|
|
311
352
|
catalog: {
|
|
312
353
|
source: this.catalog.source,
|
|
313
354
|
fetchedAt: this.catalog.fetchedAtMs,
|
|
@@ -316,6 +357,10 @@ class VariantRuntime {
|
|
|
316
357
|
},
|
|
317
358
|
credits: this.credits ?? null,
|
|
318
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 },
|
|
319
364
|
useMaximumContextWindow: this.catalog.useMaximumContextWindow === true,
|
|
320
365
|
}
|
|
321
366
|
}
|
|
@@ -336,7 +381,7 @@ export async function apply(ctx, config = {}) {
|
|
|
336
381
|
const profileDir = discoverProfileDir()
|
|
337
382
|
const client = new WorkBuddyUpstreamClient()
|
|
338
383
|
const login = new WorkBuddyLoginClient()
|
|
339
|
-
const { login: loginKey, models: modelsKey, auth: authKey, update: updateKey } = controlKeys()
|
|
384
|
+
const { login: loginKey, models: modelsKey, auth: authKey, update: updateKey, accounts: accountsKey } = controlKeys()
|
|
340
385
|
|
|
341
386
|
const runtimes = new Map()
|
|
342
387
|
const adapters = new Map()
|
|
@@ -379,6 +424,12 @@ export async function apply(ctx, config = {}) {
|
|
|
379
424
|
// The upstream refusing the stored credential is the only trustworthy
|
|
380
425
|
// signal that the session was rotated away; the card turns it into a
|
|
381
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
|
+
},
|
|
382
433
|
onSessionDead: () => runtime.markSessionStale(),
|
|
383
434
|
// A restriction is a different failure with a different fix, so it gets
|
|
384
435
|
// its own mark rather than sharing the stale-session one.
|
|
@@ -505,6 +556,7 @@ export async function apply(ctx, config = {}) {
|
|
|
505
556
|
// other card route: the key travels with the status document the card
|
|
506
557
|
// already fetches, which is how `loginKey`/`modelsKey` reach it too.
|
|
507
558
|
updateKey,
|
|
559
|
+
accountsKey,
|
|
508
560
|
})
|
|
509
561
|
res.writeHead(200, { 'content-type': 'application/json', 'cache-control': 'no-store' })
|
|
510
562
|
res.end(payload)
|
|
@@ -698,6 +750,121 @@ export async function apply(ctx, config = {}) {
|
|
|
698
750
|
res.end()
|
|
699
751
|
}
|
|
700
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
|
+
|
|
701
868
|
const authHandler = async (req, res) => {
|
|
702
869
|
if (req.method !== 'POST' || !routeGuard(req, authKey)) {
|
|
703
870
|
res.statusCode = req.method === 'POST' ? 403 : 405
|
|
@@ -783,11 +950,13 @@ export async function apply(ctx, config = {}) {
|
|
|
783
950
|
const b = webCtx.webServer.register({ kind: 'exact', path: `${ROUTE_BASE}/login`, handler: loginHandler })
|
|
784
951
|
const c = webCtx.webServer.register({ kind: 'exact', path: `${ROUTE_BASE}/models`, handler: modelsHandler })
|
|
785
952
|
const d = webCtx.webServer.register({ kind: 'exact', path: `${ROUTE_BASE}/auth`, handler: authHandler })
|
|
953
|
+
const e = webCtx.webServer.register({ kind: 'exact', path: `${ROUTE_BASE}/accounts`, handler: accountsHandler })
|
|
786
954
|
return () => {
|
|
787
955
|
a()
|
|
788
956
|
b()
|
|
789
957
|
c()
|
|
790
958
|
d()
|
|
959
|
+
e()
|
|
791
960
|
}
|
|
792
961
|
}, 'dsh-workbuddy-connect: web routes')
|
|
793
962
|
})
|
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",
|