@sixzjd/dsh-workbuddy-connect 0.0.0-stage → 0.3.1
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/LICENSE +21 -0
- package/README.en.md +162 -0
- package/README.md +202 -2
- package/assets/icon.svg +12 -0
- package/client.js +595 -0
- package/cordis.patch.yml +13 -0
- package/lib/adapter.js +340 -0
- package/lib/atrest.js +382 -0
- package/lib/auth.js +540 -0
- package/lib/catalog.js +226 -0
- package/lib/identity.js +309 -0
- package/lib/index.js +819 -0
- package/lib/paths.js +156 -0
- package/lib/shim.js +531 -0
- package/lib/upstream.js +1918 -0
- package/lib/variants.js +91 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +59 -4
package/lib/adapter.js
ADDED
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The WorkBuddy pi-ai provider: one loopback-backed adapter per variant route,
|
|
3
|
+
* registered into the Harness LLM seam.
|
|
4
|
+
*
|
|
5
|
+
* The profile is assembled by hand rather than through dsh-llm-pi-ai's internal
|
|
6
|
+
* `resolveProfiles()`: that helper is not part of the package's public export
|
|
7
|
+
* surface, so hand-assembly is the only supported path and every newly required
|
|
8
|
+
* field must be adopted here explicitly.
|
|
9
|
+
*
|
|
10
|
+
* @module dsh-workbuddy-connect/adapter
|
|
11
|
+
*/
|
|
12
|
+
import { createProvider } from '@earendil-works/pi-ai'
|
|
13
|
+
import { openAICompletionsApi } from '@earendil-works/pi-ai/api/openai-completions.lazy'
|
|
14
|
+
import { resolveRetryPolicy } from '@deepseek-ai/dsh-llm'
|
|
15
|
+
import { PiAiAdapter } from '@deepseek-ai/dsh-llm-pi-ai'
|
|
16
|
+
import { normalizeCredits } from './upstream.js'
|
|
17
|
+
import { WORKBUDDY_MODEL_IDENTITY, modelIdentityKeysOf } from './variants.js'
|
|
18
|
+
|
|
19
|
+
export { WORKBUDDY_MODEL_IDENTITY, modelIdentityKeysOf }
|
|
20
|
+
|
|
21
|
+
/** Provider idle ceiling while one stream read is outstanding. */
|
|
22
|
+
export const WORKBUDDY_STREAM_IDLE_TIMEOUT_MS = 300_000
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* The reasoning tier a request that names no effort resolves to.
|
|
26
|
+
*
|
|
27
|
+
* `dsh-llm-pi-ai` resolves every request's tier as
|
|
28
|
+
* `options.reasoningEffort ?? profile.reasoning`, so an absent default here
|
|
29
|
+
* means "whatever pi-ai fills in" — and for a model whose level map carried
|
|
30
|
+
* `off → null`, that was "no reasoning effort at all", which nine models answer
|
|
31
|
+
* with HTTP 400 / code 11133. A compaction summary is exactly such a caller.
|
|
32
|
+
*/
|
|
33
|
+
export const WORKBUDDY_DEFAULT_EFFORT = 'high'
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Image-request budgets at the dsh-llm-pi-ai defaults.
|
|
37
|
+
*
|
|
38
|
+
* They bound requests to models whose catalog entry declares `supportsImages`;
|
|
39
|
+
* text-only models never receive images.
|
|
40
|
+
*/
|
|
41
|
+
const REQUEST_IMAGE_BUDGETS = {
|
|
42
|
+
maxRequestImageBytes: 20971520,
|
|
43
|
+
requestImagePixelBudget: 4194304,
|
|
44
|
+
requestImageMaxBytes: 1048576,
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Inert pi-ai auth plane.
|
|
49
|
+
*
|
|
50
|
+
* The WorkBuddy routes authenticate only through the shim shared secret
|
|
51
|
+
* resolved per request by `resolveApiKey`, so pi-ai's own credential lifecycle
|
|
52
|
+
* and ambient discovery must never manufacture a credential for them.
|
|
53
|
+
*/
|
|
54
|
+
const INERT_AUTH = {
|
|
55
|
+
credentials: {
|
|
56
|
+
async read() {},
|
|
57
|
+
async list() {
|
|
58
|
+
return []
|
|
59
|
+
},
|
|
60
|
+
async modify() {
|
|
61
|
+
throw new Error('dsh-workbuddy-connect: the WorkBuddy routes have no pi-ai credential lifecycle')
|
|
62
|
+
},
|
|
63
|
+
async delete() {},
|
|
64
|
+
},
|
|
65
|
+
authContext: {
|
|
66
|
+
async env() {},
|
|
67
|
+
async fileExists() {
|
|
68
|
+
return false
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** No per-token pricing is knowable for a subscription quota; report zero. */
|
|
74
|
+
const NO_COST = { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* The suffix appended to a model's display name so its billing rate is visible
|
|
78
|
+
* wherever the name is shown.
|
|
79
|
+
*
|
|
80
|
+
* The separator is a middle dot rather than a hyphen or colon: model names
|
|
81
|
+
* already contain hyphens (`GLM-5.3-Flash`, `Deepseek-V4.1-Flash`), so a hyphen
|
|
82
|
+
* separator would be ambiguous about where the name ends and the rate begins.
|
|
83
|
+
*/
|
|
84
|
+
const RATE_SEPARATOR = ' · '
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* What the model seat shows in place of a rate the plugin cannot stand behind.
|
|
88
|
+
*
|
|
89
|
+
* The seat has no locale service (the adapter is a host seam), so this is a
|
|
90
|
+
* literal; it matches the settings card's own wording for the same state.
|
|
91
|
+
* Rendering it as WORDS rather than an empty slot: every other model in the
|
|
92
|
+
* seat shows `· x0.3`, so a bare name where the rate should be reads as a
|
|
93
|
+
* rendering bug.
|
|
94
|
+
*/
|
|
95
|
+
export const RATE_UNAVAILABLE = '价格暂不可用'
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* The catalog display suffix: the billing rate followed by the declared promo
|
|
99
|
+
* badges, or undefined when the row carries neither.
|
|
100
|
+
*/
|
|
101
|
+
function displaySuffix(info) {
|
|
102
|
+
if (info.billing?.rateUnknown === true) return RATE_UNAVAILABLE
|
|
103
|
+
const parts = [
|
|
104
|
+
normalizeCredits(info.billing?.credits),
|
|
105
|
+
...(info.billing?.badges ?? []),
|
|
106
|
+
].filter(part => part !== undefined && part !== '')
|
|
107
|
+
// De-duplicated: a promotion's label is layered onto the row's own badge
|
|
108
|
+
// list, and a row that already carried the same label would otherwise show
|
|
109
|
+
// it twice (`Hy3 · x0.00 · Free now · Free now`).
|
|
110
|
+
return parts.length === 0 ? undefined : [...new Set(parts)].join(RATE_SEPARATOR)
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** Append the catalog display suffix to one model's display name. */
|
|
114
|
+
function withCatalogDisplay(name, info) {
|
|
115
|
+
const suffix = displaySuffix(info)
|
|
116
|
+
return suffix === undefined ? name : `${name}${RATE_SEPARATOR}${suffix}`
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Resolve a model's reasoning capability into pi-ai's `thinkingLevelMap`.
|
|
121
|
+
*
|
|
122
|
+
* The upstream's declared set wins outright: when it names a non-empty
|
|
123
|
+
* `supportedEfforts`, exactly those values are offered and nothing else. A
|
|
124
|
+
* row the upstream left undeclared gets no control at all — its selectable set
|
|
125
|
+
* is client-side knowledge the catalog does not carry.
|
|
126
|
+
*
|
|
127
|
+
* `off` is offered only when the upstream declares `canDisableThinking: true`,
|
|
128
|
+
* and it maps to `null` (no `reasoning_effort` on the wire) because the
|
|
129
|
+
* upstream has no `off` spelling to send.
|
|
130
|
+
*/
|
|
131
|
+
export function reasoningFields(info) {
|
|
132
|
+
const reasoning = info.reasoning
|
|
133
|
+
if (reasoning === undefined || reasoning.supports !== true) {
|
|
134
|
+
// Not a reasoning model: pi-ai reads a falsy `reasoning` as "off only".
|
|
135
|
+
return { reasoning: false }
|
|
136
|
+
}
|
|
137
|
+
const declared = reasoning.supportedEfforts
|
|
138
|
+
if (declared === undefined || declared.length === 0) {
|
|
139
|
+
// Undeclared: no thinking control, and no `reasoning_effort` on the wire.
|
|
140
|
+
return { reasoning: false }
|
|
141
|
+
}
|
|
142
|
+
const fallback = typeof reasoning.defaultEffort === 'string' ? reasoning.defaultEffort : undefined
|
|
143
|
+
return {
|
|
144
|
+
reasoning: true,
|
|
145
|
+
thinkingLevelMap: {
|
|
146
|
+
off: fallback ?? null,
|
|
147
|
+
minimal: null,
|
|
148
|
+
low: declared.includes('low') ? 'low' : null,
|
|
149
|
+
medium: declared.includes('medium') ? 'medium' : null,
|
|
150
|
+
high: declared.includes('high') ? 'high' : null,
|
|
151
|
+
xhigh: declared.includes('xhigh') ? 'xhigh' : null,
|
|
152
|
+
max: declared.includes('max') ? 'max' : null,
|
|
153
|
+
},
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** Build one pi-ai model descriptor pointing at the loopback shim. */
|
|
158
|
+
function toPiModel(info, baseUrl, modelIdentity) {
|
|
159
|
+
const reasoning = reasoningFields(info)
|
|
160
|
+
return {
|
|
161
|
+
id: info.id,
|
|
162
|
+
name: info.name,
|
|
163
|
+
api: 'openai-completions',
|
|
164
|
+
provider: modelIdentity,
|
|
165
|
+
baseUrl,
|
|
166
|
+
input: info.supportsImages === true ? ['text', 'image'] : ['text'],
|
|
167
|
+
...reasoning,
|
|
168
|
+
cost: NO_COST,
|
|
169
|
+
contextWindow: info.contextWindow,
|
|
170
|
+
maxTokens: info.maxTokens,
|
|
171
|
+
compat: {
|
|
172
|
+
// pi-ai cannot infer WorkBuddy's field spelling from the shim's random
|
|
173
|
+
// loopback URL, so name the upstream-required field explicitly.
|
|
174
|
+
maxTokensField: 'max_tokens',
|
|
175
|
+
// pi-ai only emits `reasoning_effort` when this is set. Without it the
|
|
176
|
+
// effort selector in the composer would silently do nothing.
|
|
177
|
+
supportsReasoningEffort: reasoning.thinkingLevelMap !== undefined,
|
|
178
|
+
},
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* The WorkBuddy route's adapter: `PiAiAdapter` with the billing rate folded
|
|
184
|
+
* into the catalog answers it returns to the DSH model pickers.
|
|
185
|
+
*
|
|
186
|
+
* `listModels()` and `resolveModel()` build their answers straight from the
|
|
187
|
+
* pi-ai descriptors, which carry no billing fact, so the rate is layered on
|
|
188
|
+
* here by looking the model up in the live catalog. Both overrides delegate to
|
|
189
|
+
* `super` and then rewrite only the display fields, so streaming, capability
|
|
190
|
+
* resolution, and effort mapping stay exactly as `dsh-llm-pi-ai` implements
|
|
191
|
+
* them.
|
|
192
|
+
*/
|
|
193
|
+
class WorkBuddyPiAiAdapter extends PiAiAdapter {
|
|
194
|
+
constructor(catalog, adapterOptions) {
|
|
195
|
+
super(adapterOptions)
|
|
196
|
+
this.catalog = catalog
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/** Catalog entry for one model id, or undefined when the catalog omits it. */
|
|
200
|
+
infoFor(model) {
|
|
201
|
+
return this.catalog.current().find(entry => entry.id === model)
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
async listModels(provider) {
|
|
205
|
+
const models = await super.listModels(provider)
|
|
206
|
+
return models.map(model => {
|
|
207
|
+
const info = this.infoFor(model.id)
|
|
208
|
+
if (info === undefined) return model
|
|
209
|
+
return { ...model, name: withCatalogDisplay(model.name, info) }
|
|
210
|
+
})
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
async resolveModel(provider, model, signal) {
|
|
214
|
+
const resolved = await super.resolveModel(provider, model, signal)
|
|
215
|
+
const info = this.infoFor(model)
|
|
216
|
+
if (info === undefined) return resolved
|
|
217
|
+
return { ...resolved, name: withCatalogDisplay(resolved.name, info) }
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Assemble the adapter.
|
|
223
|
+
*
|
|
224
|
+
* The provider's `getModels` reads the live catalog, and every model's
|
|
225
|
+
* `baseUrl` is re-resolved per read so the shim's port applies from the first
|
|
226
|
+
* snapshot after startup.
|
|
227
|
+
*
|
|
228
|
+
* @param {object} options
|
|
229
|
+
* @param {object} options.shim The running loopback shim.
|
|
230
|
+
* @param {object} options.catalog The live catalog.
|
|
231
|
+
* @param {string} [options.providerId] Provider id to register.
|
|
232
|
+
* @param {string} [options.displayName] Human-facing provider name.
|
|
233
|
+
* @param {string} [options.modelIdentity] Identity every model of this route advertises.
|
|
234
|
+
* @param {() => object|undefined} [options.resolveAttachments] Attachment service lookup.
|
|
235
|
+
*/
|
|
236
|
+
export function createWorkBuddyAdapter(options) {
|
|
237
|
+
const { shim, catalog } = options
|
|
238
|
+
const providerId = options.providerId ?? 'workbuddy'
|
|
239
|
+
const displayName = options.displayName ?? 'WorkBuddy'
|
|
240
|
+
// The model identity is shared by both regions on purpose: pi-ai keeps a
|
|
241
|
+
// historical thinking block only when the message names the same provider,
|
|
242
|
+
// api AND model id, so a transcript that crosses the region boundary would
|
|
243
|
+
// otherwise be replayed as plain text — and a replayed transcript is a much
|
|
244
|
+
// larger one, which is what makes it stop fitting its window.
|
|
245
|
+
const modelIdentity = options.modelIdentity ?? WORKBUDDY_MODEL_IDENTITY
|
|
246
|
+
// The registry must publish the identity as an EXTRA key of this route: the
|
|
247
|
+
// stamped `model.provider` is what pi-ai resolves the provider by, so a
|
|
248
|
+
// region that does not publish it answers every request with
|
|
249
|
+
// `Unknown provider: workbuddy` before a byte is sent.
|
|
250
|
+
const registryKeys = modelIdentityKeysOf(providerId, modelIdentity)
|
|
251
|
+
|
|
252
|
+
const buildModels = () => {
|
|
253
|
+
// The OpenAI SDK pi-ai drives appends `/chat/completions` to baseURL,
|
|
254
|
+
// so the shim's routes line up with the `/v1` prefix in place.
|
|
255
|
+
const baseUrl = `${shim.baseUrl()}/v1`
|
|
256
|
+
return catalog.current().map(info => toPiModel(info, baseUrl, modelIdentity))
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
const base = createProvider({
|
|
260
|
+
id: providerId,
|
|
261
|
+
name: displayName,
|
|
262
|
+
auth: {
|
|
263
|
+
apiKey: {
|
|
264
|
+
name: 'WorkBuddy shim shared secret',
|
|
265
|
+
async resolve({ credential }) {
|
|
266
|
+
const apiKey = credential?.key
|
|
267
|
+
return apiKey === undefined || apiKey.length === 0 ? undefined : { auth: { apiKey }, source: displayName }
|
|
268
|
+
},
|
|
269
|
+
},
|
|
270
|
+
},
|
|
271
|
+
models: buildModels(),
|
|
272
|
+
api: openAICompletionsApi(),
|
|
273
|
+
})
|
|
274
|
+
|
|
275
|
+
// `getModels` is delegated to a live read: stream dispatch still runs through
|
|
276
|
+
// the constructed provider, while the catalog answer tracks the refresh.
|
|
277
|
+
const provider = { ...base, getModels: () => buildModels() }
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* One profile per published registry key.
|
|
281
|
+
*
|
|
282
|
+
* The `piProvider` is what actually lands in pi-ai's registry, and that map
|
|
283
|
+
* is keyed on `piProvider.id` — NOT on the profile's own `provider` field. So
|
|
284
|
+
* the identity key needs a provider object that *claims* that id: the
|
|
285
|
+
* route's own provider under a second id, so the models, baseUrl, api and
|
|
286
|
+
* auth the request runs with stay this region's and nothing about the
|
|
287
|
+
* gateway is duplicated.
|
|
288
|
+
*
|
|
289
|
+
* Sharing the very same object under both keys would register the route id
|
|
290
|
+
* twice and leave the stamped identity unresolved — every request in that
|
|
291
|
+
* region would fail with `Unknown provider: workbuddy` before a byte is
|
|
292
|
+
* sent.
|
|
293
|
+
*/
|
|
294
|
+
const profileFor = (key) => ({
|
|
295
|
+
provider: key,
|
|
296
|
+
displayName,
|
|
297
|
+
streamIdleTimeoutMs: WORKBUDDY_STREAM_IDLE_TIMEOUT_MS,
|
|
298
|
+
retryPolicy: resolveRetryPolicy(undefined, 'dsh-workbuddy-connect retryPolicy'),
|
|
299
|
+
configuredMaxTokens: new Map(),
|
|
300
|
+
// Required since 0.1.5-alpha.2; the live catalog only exposes models the
|
|
301
|
+
// upstream actually served, so there is never a per-model failure to report.
|
|
302
|
+
modelErrors: new Map(),
|
|
303
|
+
// The default tier, pinned to the same value pi-ai's level map would fill
|
|
304
|
+
// in for an absent one. Without it a request that names no effort (a
|
|
305
|
+
// compaction summary is exactly such a caller) resolves to "off" — which
|
|
306
|
+
// nine models refuse with HTTP 400 / code 11133.
|
|
307
|
+
reasoning: WORKBUDDY_DEFAULT_EFFORT,
|
|
308
|
+
...REQUEST_IMAGE_BUDGETS,
|
|
309
|
+
piProvider: key === providerId ? provider : { ...provider, id: key },
|
|
310
|
+
})
|
|
311
|
+
|
|
312
|
+
// A single source for BOTH the initial map and `invalidate`: an `invalidate`
|
|
313
|
+
// that rebuilt only the route key would silently re-break the identity key.
|
|
314
|
+
const buildProfiles = () => new Map(registryKeys.map(key => [key, profileFor(key)]))
|
|
315
|
+
|
|
316
|
+
let profiles = buildProfiles()
|
|
317
|
+
|
|
318
|
+
const adapter = new WorkBuddyPiAiAdapter(catalog, {
|
|
319
|
+
profiles: () => profiles,
|
|
320
|
+
auth: INERT_AUTH,
|
|
321
|
+
// Resolve the shim's per-process shared secret as the OpenAI apiKey so
|
|
322
|
+
// pi-ai sends it as `Authorization: Bearer <shared-secret>`. The shim
|
|
323
|
+
// validates this before forwarding and resolves the real WorkBuddy token
|
|
324
|
+
// itself via the store, so the secret never reaches upstream.
|
|
325
|
+
resolveApiKey: async () => shim.token(),
|
|
326
|
+
// Image inputs reach the wire through the host's durable attachment
|
|
327
|
+
// service: dsh-llm-pi-ai refuses any request carrying an image without
|
|
328
|
+
// it, so the adapter must be handed the service the same way the sibling
|
|
329
|
+
// connectors hand it. The lookup is lazy so a host without one never
|
|
330
|
+
// fails plugin startup.
|
|
331
|
+
...options.resolveAttachments === undefined ? {} : { resolveAttachments: options.resolveAttachments },
|
|
332
|
+
})
|
|
333
|
+
|
|
334
|
+
return {
|
|
335
|
+
adapter,
|
|
336
|
+
invalidate: () => {
|
|
337
|
+
profiles = buildProfiles()
|
|
338
|
+
},
|
|
339
|
+
}
|
|
340
|
+
}
|