@aiwayds/dsh-model-sync 0.4.1 → 0.6.0
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 +13 -10
- package/README.zh-CN.md +14 -9
- package/cordis.patch.yml +5 -4
- package/icon.svg +6 -0
- package/lib/builtin-catalog-snapshot.d.ts +9 -0
- package/lib/builtin-catalog-snapshot.js +2 -2
- package/lib/builtin-catalog-snapshot.js.map +1 -1
- package/lib/index.d.ts +153 -1
- package/lib/index.js +139 -42
- package/lib/index.js.map +1 -1
- package/lib/provider-native.d.ts +124 -0
- package/lib/provider-native.js +202 -0
- package/lib/provider-native.js.map +1 -0
- package/lib/route-credential.d.ts +119 -0
- package/lib/route-credential.js +127 -0
- package/lib/route-credential.js.map +1 -0
- package/lib/translate.d.ts +8 -0
- package/lib/translate.js.map +1 -1
- package/lib/writer.d.ts +2 -1
- package/lib/writer.js.map +1 -1
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +32 -19
- package/src/builtin-catalog-snapshot.ts +11 -2
- package/src/index.ts +186 -54
- package/src/provider-native.ts +293 -0
- package/src/route-credential.ts +206 -0
- package/src/translate.ts +8 -0
- package/src/writer.ts +2 -1
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
// Provider-native model-list fetch: union the pi.dev catalog with each
|
|
2
|
+
// provider's own /models endpoint — the fresher source.
|
|
3
|
+
//
|
|
4
|
+
// Why this exists: pi.dev (and the pi-ai builtin snapshot behind it) lags
|
|
5
|
+
// behind the providers. Measured 2026-09-22 against the native endpoints:
|
|
6
|
+
// DeepSeek's listing had already renamed deepseek-v4-flash → deepseek-flash
|
|
7
|
+
// while pi-ai still carried the retired id; Zhipu served glm-5.3-flashx that
|
|
8
|
+
// pi-ai did not know; Xiaomi's platform announced the V2.6 series while pi-ai
|
|
9
|
+
// stopped at v2.5. Every provider above exposes an OpenAI-shaped
|
|
10
|
+
// `GET /models` listing that answers "which ids exist" straight from the
|
|
11
|
+
// horse's mouth.
|
|
12
|
+
//
|
|
13
|
+
// Merge policy (user-mandated 2026-09-22): the listing may only ADD ids —
|
|
14
|
+
// never remove. A pi.dev entry keeps its richer metadata untouched; a
|
|
15
|
+
// native-only id is synthesized as a minimal entry. Capacities follow the
|
|
16
|
+
// same "don't guess" rule: contextWindow is written only when the listing
|
|
17
|
+
// itself states one (Moonshot's `context_length`), maxTokens never — omitting
|
|
18
|
+
// both lets dsh-llm-pi-ai fall back to the route's defaultContextWindow /
|
|
19
|
+
// defaultMaxTokens (its resolve chain `entry.x ?? base?.x ?? request.default`
|
|
20
|
+
// is exactly this policy made native).
|
|
21
|
+
//
|
|
22
|
+
// Failure policy: a failed native fetch must never lose models. It degrades
|
|
23
|
+
// to the pi.dev result unchanged and surfaces one report line; the round
|
|
24
|
+
// proceeds either way.
|
|
25
|
+
|
|
26
|
+
import type { RemoteCatalogEntry } from './remote-catalog.ts'
|
|
27
|
+
import { deriveKeyRef, getRawUserApiKeyEnv, type RouteCredentialSettingsDescriptor } from './route-credential.ts'
|
|
28
|
+
|
|
29
|
+
// ---------------------------------------------------------------------------
|
|
30
|
+
// Endpoint table
|
|
31
|
+
// ---------------------------------------------------------------------------
|
|
32
|
+
|
|
33
|
+
/** How the /models request authenticates. */
|
|
34
|
+
export type ProviderNativeAuth = 'bearer' | 'api-key-header'
|
|
35
|
+
|
|
36
|
+
/** One provider route's native model-listing endpoint. */
|
|
37
|
+
export interface ProviderNativeEndpoint {
|
|
38
|
+
/** Full URL of the OpenAI-shaped `GET` listing. */
|
|
39
|
+
url: string
|
|
40
|
+
/** Authentication style (Xiaomi accepts either; its docs lead with the header). */
|
|
41
|
+
auth: ProviderNativeAuth
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Provider routes with a known first-party model-listing API. Routes absent
|
|
46
|
+
* from this table (gateways like `opencode-go`, unverified providers) simply
|
|
47
|
+
* keep the pi.dev-only behavior.
|
|
48
|
+
*
|
|
49
|
+
* All endpoints verified 2026-09-22: DeepSeek/Zhipu with a live key, the rest
|
|
50
|
+
* probe-confirmed (401 vs 404 — the endpoint exists, the key was not ours to
|
|
51
|
+
* spend) plus official docs for Xiaomi (mimo.mi.com list-models) and
|
|
52
|
+
* Moonshot (platform.kimi.com OpenAPI, whose response uniquely adds
|
|
53
|
+
* `context_length` and capability flags we can lift).
|
|
54
|
+
*/
|
|
55
|
+
export const PROVIDER_NATIVE_ENDPOINTS: Readonly<Record<string, ProviderNativeEndpoint>> = {
|
|
56
|
+
deepseek: { url: 'https://api.deepseek.com/models', auth: 'bearer' },
|
|
57
|
+
'moonshotai-cn': { url: 'https://api.moonshot.cn/v1/models', auth: 'bearer' },
|
|
58
|
+
moonshotai: { url: 'https://api.moonshot.ai/v1/models', auth: 'bearer' },
|
|
59
|
+
'kimi-coding': { url: 'https://api.kimi.com/coding/v1/models', auth: 'bearer' },
|
|
60
|
+
zai: { url: 'https://api.z.ai/api/paas/v4/models', auth: 'bearer' },
|
|
61
|
+
'zai-coding-cn': { url: 'https://open.bigmodel.cn/api/coding/paas/v4/models', auth: 'bearer' },
|
|
62
|
+
xiaomi: { url: 'https://api.xiaomimimo.com/v1/models', auth: 'api-key-header' },
|
|
63
|
+
'xiaomi-token-plan-cn': { url: 'https://token-plan-cn.xiaomimimo.com/v1/models', auth: 'api-key-header' },
|
|
64
|
+
'xiaomi-token-plan-ams': { url: 'https://token-plan-ams.xiaomimimo.com/v1/models', auth: 'api-key-header' },
|
|
65
|
+
'xiaomi-token-plan-sgp': { url: 'https://token-plan-sgp.xiaomimimo.com/v1/models', auth: 'api-key-header' },
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// ---------------------------------------------------------------------------
|
|
69
|
+
// Credential value resolution
|
|
70
|
+
// ---------------------------------------------------------------------------
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* The credentials seam surface this module needs beyond the gate's
|
|
74
|
+
* `describe`: `resolve` returns the value itself (the gate only asks whether
|
|
75
|
+
* it is configured). Optional peer — callers tolerate its absence via the
|
|
76
|
+
* launch-environment fallback.
|
|
77
|
+
*/
|
|
78
|
+
export interface CredentialsResolveSeam {
|
|
79
|
+
resolve(ref: string): Promise<{ value: string } | undefined>
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** The launch-environment snapshot surface (same as route-credential.ts). */
|
|
83
|
+
export interface LaunchEnvironmentSnapshotLike {
|
|
84
|
+
get(name: string): { value: string } | undefined
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** The subset of `Context` the credential-value resolver reads. */
|
|
88
|
+
export interface NativeCredentialContext {
|
|
89
|
+
get(name: 'credentials'): CredentialsResolveSeam | undefined
|
|
90
|
+
get(name: 'launchEnvironment'): LaunchEnvironmentSnapshotLike | undefined
|
|
91
|
+
get(name: string): unknown
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Resolve one route's credential reference to its current VALUE — the same
|
|
96
|
+
* two layers `checkRouteCredential` gates on, but reading through instead of
|
|
97
|
+
* asking whether:
|
|
98
|
+
* 1. the credentials seam's `resolve(ref)` (the same per-operation read the
|
|
99
|
+
* harness itself uses before a request);
|
|
100
|
+
* 2. the launch-environment snapshot (process env fallback when the service
|
|
101
|
+
* is absent).
|
|
102
|
+
*
|
|
103
|
+
* Returns undefined when neither layer has a value; callers skip the native
|
|
104
|
+
* fetch (the pi.dev result stands) rather than fail the round.
|
|
105
|
+
*/
|
|
106
|
+
export async function resolveRouteCredentialValue(
|
|
107
|
+
ctx: NativeCredentialContext,
|
|
108
|
+
desc: RouteCredentialSettingsDescriptor | undefined,
|
|
109
|
+
route: string,
|
|
110
|
+
): Promise<string | undefined> {
|
|
111
|
+
const declared = getRawUserApiKeyEnv(desc, route)
|
|
112
|
+
const ref = declared ?? deriveKeyRef(route)
|
|
113
|
+
const REF_PATTERN = /^[A-Za-z_][A-Za-z0-9_]*$/
|
|
114
|
+
|
|
115
|
+
if (REF_PATTERN.test(ref)) {
|
|
116
|
+
const credentials = ctx.get('credentials')
|
|
117
|
+
if (credentials !== undefined) {
|
|
118
|
+
try {
|
|
119
|
+
const resolved = await credentials.resolve(ref)
|
|
120
|
+
if (resolved !== undefined && typeof resolved.value === 'string' && resolved.value.length > 0) {
|
|
121
|
+
return resolved.value
|
|
122
|
+
}
|
|
123
|
+
} catch {
|
|
124
|
+
// Same tolerance as the gate: a half-broken seam reads as "no value
|
|
125
|
+
// here" and falls through to the environment.
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const snapshot = ctx.get('launchEnvironment') ?? {
|
|
131
|
+
get(name: string) {
|
|
132
|
+
const value = process.env[name]
|
|
133
|
+
return value !== undefined && value !== '' ? { value, source: 'process' } : undefined
|
|
134
|
+
},
|
|
135
|
+
}
|
|
136
|
+
const entry = snapshot.get(ref)
|
|
137
|
+
if (entry !== undefined && typeof entry.value === 'string' && entry.value.length > 0) {
|
|
138
|
+
return entry.value
|
|
139
|
+
}
|
|
140
|
+
return undefined
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// ---------------------------------------------------------------------------
|
|
144
|
+
// Fetch + parse
|
|
145
|
+
// ---------------------------------------------------------------------------
|
|
146
|
+
|
|
147
|
+
/** One provider's native listing outcome: the fresh truth, or why not. */
|
|
148
|
+
export type ProviderNativeResult =
|
|
149
|
+
| {
|
|
150
|
+
ok: true
|
|
151
|
+
/** Model ids exactly as the provider lists them. */
|
|
152
|
+
ids: string[]
|
|
153
|
+
/** contextWindow values the listing itself states (Moonshot `context_length`). */
|
|
154
|
+
contextById: Map<string, number>
|
|
155
|
+
}
|
|
156
|
+
| { ok: false; error: string }
|
|
157
|
+
|
|
158
|
+
/** A minimal fetch compatible enough for tests to stub. */
|
|
159
|
+
export type FetchLike = (url: string, init: { headers: Record<string, string>; signal: AbortSignal }) => Promise<{
|
|
160
|
+
ok: boolean
|
|
161
|
+
status: number
|
|
162
|
+
json(): Promise<unknown>
|
|
163
|
+
}>
|
|
164
|
+
|
|
165
|
+
function isPositiveInt(value: unknown): value is number {
|
|
166
|
+
return typeof value === 'number' && Number.isInteger(value) && value > 0
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Parse a native listing body. Accepts the OpenAI list shape
|
|
171
|
+
* `{"object":"list","data":[{"id":...}]}` and tolerates the per-provider
|
|
172
|
+
* extras (Moonshot's `context_length`; Zhipu's `created`/`owned_by`) — only
|
|
173
|
+
* string `id`s count, and only a positive-integer `context_length` is kept.
|
|
174
|
+
*/
|
|
175
|
+
export function parseProviderNativeBody(value: unknown): { ids: string[]; contextById: Map<string, number> } {
|
|
176
|
+
const ids: string[] = []
|
|
177
|
+
const contextById = new Map<string, number>()
|
|
178
|
+
if (typeof value !== 'object' || value === null) throw new Error('body is not an object')
|
|
179
|
+
const data = (value as Record<string, unknown>).data
|
|
180
|
+
if (!Array.isArray(data)) throw new Error('body has no data array')
|
|
181
|
+
for (const item of data) {
|
|
182
|
+
if (typeof item !== 'object' || item === null) continue
|
|
183
|
+
const id = (item as Record<string, unknown>).id
|
|
184
|
+
if (typeof id !== 'string' || id.length === 0) continue
|
|
185
|
+
ids.push(id)
|
|
186
|
+
const context = (item as Record<string, unknown>).context_length
|
|
187
|
+
if (isPositiveInt(context)) contextById.set(id, context)
|
|
188
|
+
}
|
|
189
|
+
if (ids.length === 0) throw new Error('data array lists no usable model ids')
|
|
190
|
+
return { ids, contextById }
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Fetch one route's native model listing. Any failure (non-200, network,
|
|
195
|
+
* unparseable body) resolves to `{ ok: false, error }` — never throws, so the
|
|
196
|
+
* caller's pi.dev result is untouchable.
|
|
197
|
+
*
|
|
198
|
+
* @param route Provider route id; must have an entry in the endpoint table.
|
|
199
|
+
* @param apiKey The credential value (Bearer token / api-key header value).
|
|
200
|
+
* @param timeoutMs Abort budget.
|
|
201
|
+
* @param fetchImpl Injectable fetch (tests); defaults to the global fetch.
|
|
202
|
+
*/
|
|
203
|
+
export async function fetchProviderNativeModels(
|
|
204
|
+
route: string,
|
|
205
|
+
apiKey: string,
|
|
206
|
+
timeoutMs: number,
|
|
207
|
+
fetchImpl: FetchLike = fetch as unknown as FetchLike,
|
|
208
|
+
): Promise<ProviderNativeResult> {
|
|
209
|
+
const endpoint = PROVIDER_NATIVE_ENDPOINTS[route]
|
|
210
|
+
if (endpoint === undefined) {
|
|
211
|
+
return { ok: false, error: `no native endpoint mapped for route "${route}"` }
|
|
212
|
+
}
|
|
213
|
+
const headers: Record<string, string> = { accept: 'application/json' }
|
|
214
|
+
if (endpoint.auth === 'bearer') headers.authorization = `Bearer ${apiKey}`
|
|
215
|
+
else headers['api-key'] = apiKey
|
|
216
|
+
|
|
217
|
+
const controller = new AbortController()
|
|
218
|
+
const timer = setTimeout(() => controller.abort(), timeoutMs)
|
|
219
|
+
try {
|
|
220
|
+
const response = await fetchImpl(endpoint.url, { headers, signal: controller.signal })
|
|
221
|
+
if (!response.ok) {
|
|
222
|
+
return { ok: false, error: `HTTP ${response.status} from ${new URL(endpoint.url).host}` }
|
|
223
|
+
}
|
|
224
|
+
const parsed = parseProviderNativeBody(await response.json())
|
|
225
|
+
return { ok: true, ...parsed }
|
|
226
|
+
} catch (err: unknown) {
|
|
227
|
+
const message = err instanceof Error ? err.message : String(err)
|
|
228
|
+
return { ok: false, error: `native listing fetch failed: ${message}` }
|
|
229
|
+
} finally {
|
|
230
|
+
clearTimeout(timer)
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// ---------------------------------------------------------------------------
|
|
235
|
+
// Union merge
|
|
236
|
+
// ---------------------------------------------------------------------------
|
|
237
|
+
|
|
238
|
+
/** Outcome of merging a native listing into a route's pi.dev entries. */
|
|
239
|
+
export interface MergedNativeResult {
|
|
240
|
+
/** pi.dev entries plus one synthesized entry per native-only id. */
|
|
241
|
+
entries: RemoteCatalogEntry[]
|
|
242
|
+
/** Native ids that were not already present and got synthesized. */
|
|
243
|
+
addedIds: string[]
|
|
244
|
+
/** Why some or all native ids could not be synthesized (routeApi unknown). */
|
|
245
|
+
skippedReason?: string
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Union a native listing into the pi.dev entries — additions only.
|
|
250
|
+
*
|
|
251
|
+
* A native id the pi.dev list already carries is ignored (pi.dev's richer
|
|
252
|
+
* metadata wins). A native-only id becomes a minimal entry: no maxTokens ever
|
|
253
|
+
* (no source states one), contextWindow only when the listing itself did
|
|
254
|
+
* (`contextById`). `routeApi` is the route's single builtin api — required so
|
|
255
|
+
* the entry can survive translate's base-less gate; routes with no resolvable
|
|
256
|
+
* api (unknown route, mixed-protocol gateway) skip synthesis wholesale with a
|
|
257
|
+
* `skippedReason` instead of emitting entries the runtime would reject.
|
|
258
|
+
*/
|
|
259
|
+
export function mergeProviderNativeEntries(
|
|
260
|
+
piDevEntries: readonly RemoteCatalogEntry[],
|
|
261
|
+
native: { ids: readonly string[]; contextById: ReadonlyMap<string, number> },
|
|
262
|
+
route: string,
|
|
263
|
+
routeApi: string | undefined,
|
|
264
|
+
): MergedNativeResult {
|
|
265
|
+
const known = new Set(piDevEntries.map((e) => e.id))
|
|
266
|
+
const addedIds: string[] = []
|
|
267
|
+
const entries: RemoteCatalogEntry[] = [...piDevEntries]
|
|
268
|
+
|
|
269
|
+
for (const id of native.ids) {
|
|
270
|
+
if (known.has(id)) continue
|
|
271
|
+
if (routeApi === undefined) {
|
|
272
|
+
return {
|
|
273
|
+
entries,
|
|
274
|
+
addedIds,
|
|
275
|
+
skippedReason: `cannot place ${native.ids.length - addedIds.length} new id(s) — route has no single builtin api to address them`,
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
const context = native.contextById.get(id)
|
|
279
|
+
entries.push({
|
|
280
|
+
id,
|
|
281
|
+
name: id,
|
|
282
|
+
api: routeApi,
|
|
283
|
+
provider: route,
|
|
284
|
+
baseUrl: '',
|
|
285
|
+
reasoning: false,
|
|
286
|
+
input: ['text'],
|
|
287
|
+
...(context !== undefined ? { contextWindow: context } : {}),
|
|
288
|
+
})
|
|
289
|
+
addedIds.push(id)
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
return { entries, addedIds }
|
|
293
|
+
}
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
// Login gate for the settings-mode sync: skip routes whose credential
|
|
2
|
+
// reference is not configured, so a logged-out profile never silently gets
|
|
3
|
+
// its `models` rewritten by the sync.
|
|
4
|
+
//
|
|
5
|
+
// Layer 1 follows llm-pi-ai's `resolveApiKey`: prefer the credentials seam.
|
|
6
|
+
// Layer 2 follows the same package's `authContextFrom(ctx).env()` semantics:
|
|
7
|
+
// consult the launch environment, or process.env when that service is absent.
|
|
8
|
+
// A hit on either allows sync; only when BOTH miss do we skip the route.
|
|
9
|
+
// Known gap: after credential removal, a same-name shell export still allows
|
|
10
|
+
// writes, but requests with the seam present still fail with MISSING_CREDENTIAL.
|
|
11
|
+
//
|
|
12
|
+
// deriveKeyRef replicates the dsh web UI's same helper at
|
|
13
|
+
// packages/client/ui-settings-models/src/client/store.ts:111-113 (uppercase +
|
|
14
|
+
// collapse non-alphanumerics to `_` + `_API_KEY` suffix). The web UI uses it
|
|
15
|
+
// to mint the reference name a typed key stores under; we use it as the
|
|
16
|
+
// fallback when a route has no explicit `apiKeyEnv` (shell-environment users
|
|
17
|
+
// who `export OPENCODE_API_KEY=…` without going through the UI). The two
|
|
18
|
+
// conversions are byte-for-byte identical, so a value the web UI wrote
|
|
19
|
+
// resolves through the same name here.
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The credentials seam surface this module needs. The seam is an optional
|
|
23
|
+
* peer (`@deepseek-ai/dsh-credentials`), so the gate must tolerate its
|
|
24
|
+
* absence — `checkRouteCredential` falls through to the launch-environment
|
|
25
|
+
* check in that case.
|
|
26
|
+
*
|
|
27
|
+
* The actual `CredentialProvider.describe` takes a branded `CredentialRef`
|
|
28
|
+
* (`Branded<'CredentialRef'>` from `@deepseek-ai/dsh-brand`), but the brand is
|
|
29
|
+
* a structural type only — every branded string is also a plain `string` at
|
|
30
|
+
* runtime. Declaring the parameter as `string` here keeps the local interface
|
|
31
|
+
* dependency-free (we don't import the brand module just to name one
|
|
32
|
+
* parameter) while remaining assignable from any value the seam emits:
|
|
33
|
+
* `credentialRef(value)` returns `value as CredentialRef`, which is the same
|
|
34
|
+
* `value` retyped, so passing it to `describe(ref: string)` works under both
|
|
35
|
+
* the host and any plain-JS seam test double.
|
|
36
|
+
*/
|
|
37
|
+
export interface CredentialsSeam {
|
|
38
|
+
describe(ref: string): Promise<{ configured: boolean }>
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* The launch-environment snapshot surface (`@deepseek-ai/dsh-launch-environment`).
|
|
43
|
+
* Cordis `ctx.get` returns undefined for an unregistered service; it does not
|
|
44
|
+
* synthesize a snapshot. This module supplies the `process.env` fallback.
|
|
45
|
+
*/
|
|
46
|
+
export interface LaunchEnvironmentSnapshot {
|
|
47
|
+
get(name: string): { value: string } | undefined
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* The subset of `Context` this module reads. Both services are optional:
|
|
52
|
+
* `credentials` is an optional peer and may be absent, `launchEnvironment` is
|
|
53
|
+
* provided by the launcher but the helper synthesizes a process-env snapshot
|
|
54
|
+
* when it is not.
|
|
55
|
+
*/
|
|
56
|
+
export interface RouteCredentialContext {
|
|
57
|
+
get(name: 'credentials'): CredentialsSeam | undefined
|
|
58
|
+
get(name: 'launchEnvironment'): LaunchEnvironmentSnapshot | undefined
|
|
59
|
+
get(name: string): unknown
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* The settings descriptor shape this module needs (matches writer.ts's
|
|
64
|
+
* `SettingsDescriptor`). Only `user.providers.<route>.apiKeyEnv` is read —
|
|
65
|
+
* the value the user explicitly declared in the llm-pi-ai provider profile
|
|
66
|
+
* (settings.yaml up to dsh 0.1.6, the profile patch on 0.1.7+).
|
|
67
|
+
*/
|
|
68
|
+
export interface RouteCredentialSettingsDescriptor {
|
|
69
|
+
user?: Record<string, unknown>
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Outcome of one route's credential check.
|
|
74
|
+
*/
|
|
75
|
+
export interface RouteCredentialCheck {
|
|
76
|
+
/** True when either source confirms the route is configured. */
|
|
77
|
+
ok: boolean
|
|
78
|
+
/**
|
|
79
|
+
* The credential reference name consulted (e.g. `OPENCODE_API_KEY`,
|
|
80
|
+
* `MINIMAX_CN_API_KEY`). Always present, even when `ok` is false, so the
|
|
81
|
+
* report can name what is missing.
|
|
82
|
+
*/
|
|
83
|
+
ref: string
|
|
84
|
+
/**
|
|
85
|
+
* Which source confirmed the route, when `ok` is true:
|
|
86
|
+
* - 'credentials' — the seam's `describe()` returned `configured: true`
|
|
87
|
+
* - 'env' — the launch-environment snapshot (process env / .env) had a
|
|
88
|
+
* non-empty value, with no seam hit
|
|
89
|
+
*/
|
|
90
|
+
via?: 'credentials' | 'env'
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Derive the conventional credential reference for a provider route. This is
|
|
95
|
+
* a byte-for-byte replica of `deriveKeyRef` from the dsh web UI's settings
|
|
96
|
+
* models package (see ~/github/deepseek-harness/packages/client/ui-
|
|
97
|
+
* settings-models/src/client/store.ts:111-113). The web UI uses it as the
|
|
98
|
+
* default name a typed key stores under; we use it as the fallback when the
|
|
99
|
+
* user has not declared an explicit `apiKeyEnv` in their provider profile.
|
|
100
|
+
*
|
|
101
|
+
* @param provider - provider route id (e.g. `opencode-go`, `minimax-cn`).
|
|
102
|
+
* @returns the derived reference name (e.g. `OPENCODE_GO_API_KEY`).
|
|
103
|
+
*/
|
|
104
|
+
export function deriveKeyRef(provider: string): string {
|
|
105
|
+
return `${provider.toUpperCase().replace(/[^A-Z0-9]+/g, '_')}_API_KEY`
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Read the explicit `apiKeyEnv` the user declared for a route in the
|
|
110
|
+
* `llm-pi-ai` provider profile. Returns the trimmed string when present and
|
|
111
|
+
* non-empty, otherwise `undefined` (callers fall through to `deriveKeyRef`).
|
|
112
|
+
*
|
|
113
|
+
* @param desc - the settings descriptor (may be undefined when the host has
|
|
114
|
+
* no `llm-pi-ai` namespace registered yet).
|
|
115
|
+
* @param route - the provider route id.
|
|
116
|
+
*/
|
|
117
|
+
export function getRawUserApiKeyEnv(
|
|
118
|
+
desc: RouteCredentialSettingsDescriptor | undefined,
|
|
119
|
+
route: string,
|
|
120
|
+
): string | undefined {
|
|
121
|
+
if (desc === undefined) return undefined
|
|
122
|
+
const providers = desc.user?.providers as Record<string, unknown> | undefined
|
|
123
|
+
if (providers === undefined) return undefined
|
|
124
|
+
const routeData = providers[route] as Record<string, unknown> | undefined
|
|
125
|
+
if (routeData === undefined) return undefined
|
|
126
|
+
const raw = routeData.apiKeyEnv
|
|
127
|
+
if (typeof raw !== 'string') return undefined
|
|
128
|
+
const trimmed = raw.trim()
|
|
129
|
+
return trimmed.length > 0 ? trimmed : undefined
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Run the per-route credential gate. Returns `{ ok, ref, via? }` where `ok`
|
|
134
|
+
* is true iff either source confirms the route is configured.
|
|
135
|
+
*
|
|
136
|
+
* Resolution layers (not an exact request-time credential resolver):
|
|
137
|
+
* 1. `credentials.describe(ref).configured === true` when the seam is
|
|
138
|
+
* provided AND the ref is a valid `CredentialRef` (POSIX identifier).
|
|
139
|
+
* The strict pattern check matters: a profile removed during logout leaves
|
|
140
|
+
* no `apiKeyEnv`, so we always derive; and a stray ref that does not match
|
|
141
|
+
* the brand grammar (e.g. the seam `resolve` would throw) must read as
|
|
142
|
+
* "not configured" rather than crash the round.
|
|
143
|
+
* 2. `launchEnvironment.get(ref).value` returns a non-empty string, following
|
|
144
|
+
* llm-pi-ai's `authContextFrom(ctx).env()` semantics. Like
|
|
145
|
+
* `launchEnvironmentOf`, we synthesize a `process.env` snapshot when the
|
|
146
|
+
* launch-environment service is absent, even without a launcher.
|
|
147
|
+
*
|
|
148
|
+
* Both misses → `ok: false`. The gate then skips fetch / translate /
|
|
149
|
+
* `settings.mutate` for the route and emits a report line naming the ref.
|
|
150
|
+
*
|
|
151
|
+
* @param ctx - the plugin's cordis context.
|
|
152
|
+
* @param desc - the resolved `llm-pi-ai` settings descriptor (may be undefined
|
|
153
|
+
* when the host has not registered the namespace yet — same shape as the
|
|
154
|
+
* descriptor the writer already reads).
|
|
155
|
+
* @param route - the provider route id being checked.
|
|
156
|
+
*/
|
|
157
|
+
export async function checkRouteCredential(
|
|
158
|
+
ctx: RouteCredentialContext,
|
|
159
|
+
desc: RouteCredentialSettingsDescriptor | undefined,
|
|
160
|
+
route: string,
|
|
161
|
+
): Promise<RouteCredentialCheck> {
|
|
162
|
+
// 1. The user-declared ref (when present) wins over the derived one. A
|
|
163
|
+
// profile that names its own ref is the authoritative choice; the
|
|
164
|
+
// derivation is only a fallback for shell-environment users.
|
|
165
|
+
const declared = getRawUserApiKeyEnv(desc, route)
|
|
166
|
+
const ref = declared ?? deriveKeyRef(route)
|
|
167
|
+
|
|
168
|
+
// 2. Credentials seam first. The seam is optional: skip the whole branch
|
|
169
|
+
// when the host did not register it. Inside the branch, only consult the
|
|
170
|
+
// seam when the ref matches the brand's POSIX-identifier grammar — the
|
|
171
|
+
// seam's own `describe` throws on an invalid ref, which would crash the
|
|
172
|
+
// sync round. Mirroring `isCredentialRefName` from
|
|
173
|
+
// @deepseek-ai/dsh-credentials keeps the gate quiet on malformed input.
|
|
174
|
+
const REF_PATTERN = /^[A-Za-z_][A-Za-z0-9_]*$/
|
|
175
|
+
if (REF_PATTERN.test(ref)) {
|
|
176
|
+
const credentials = ctx.get('credentials')
|
|
177
|
+
if (credentials !== undefined) {
|
|
178
|
+
try {
|
|
179
|
+
const info = await credentials.describe(ref)
|
|
180
|
+
if (info.configured) {
|
|
181
|
+
return { ok: true, ref, via: 'credentials' }
|
|
182
|
+
}
|
|
183
|
+
} catch {
|
|
184
|
+
// Treat any throw from describe (provider missing, seam half-broken)
|
|
185
|
+
// as "not configured here" and fall through to the env check.
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// 3. Cordis `ctx.get` returns undefined for absent services, without throwing
|
|
191
|
+
// or falling through to process.env. Reproduce `launchEnvironmentOf`'s
|
|
192
|
+
// fallback locally, without importing the optional peer. A provided
|
|
193
|
+
// snapshot remains authoritative; empty values do not count as configured.
|
|
194
|
+
const snapshot = ctx.get('launchEnvironment') ?? {
|
|
195
|
+
get(name: string) {
|
|
196
|
+
const value = process.env[name]
|
|
197
|
+
return value !== undefined && value !== '' ? { value, source: 'process' } : undefined
|
|
198
|
+
},
|
|
199
|
+
}
|
|
200
|
+
const entry = snapshot.get(ref)
|
|
201
|
+
if (entry !== undefined && typeof entry.value === 'string' && entry.value.length > 0) {
|
|
202
|
+
return { ok: true, ref, via: 'env' }
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
return { ok: false, ref }
|
|
206
|
+
}
|
package/src/translate.ts
CHANGED
|
@@ -108,6 +108,14 @@ export interface BuiltinModelData {
|
|
|
108
108
|
id: string
|
|
109
109
|
api: string
|
|
110
110
|
maxTokens?: number
|
|
111
|
+
/**
|
|
112
|
+
* The id left the official default model list (dsh 0.1.7 removed
|
|
113
|
+
* deepseek-v4-flash / deepseek-v4-flash-vision-exp) but the data is kept
|
|
114
|
+
* for historical user configurations. Deprecated entries still serve
|
|
115
|
+
* base-matching classification, but the keepBuiltinOnly emission skips
|
|
116
|
+
* them unless explicitly opted back in (see index.ts getBuiltinOnlyEntries).
|
|
117
|
+
*/
|
|
118
|
+
deprecated?: boolean
|
|
111
119
|
}
|
|
112
120
|
|
|
113
121
|
// ---------------------------------------------------------------------------
|
package/src/writer.ts
CHANGED
|
@@ -31,7 +31,8 @@
|
|
|
31
31
|
* stored ones (a re-added key is the user's latest intent).
|
|
32
32
|
* - Revision conflict retry: catch SETTINGS_CONFLICT, re-read, re-translate,
|
|
33
33
|
* re-write once
|
|
34
|
-
* - Never touches
|
|
34
|
+
* - Never touches the settings document directly (settings.yaml up to
|
|
35
|
+
* dsh 0.1.6, the profile patch on 0.1.7+) — only through settings.mutate
|
|
35
36
|
* - Store read failure: when the models-store read fails (corrupt JSON,
|
|
36
37
|
* EACCES, …) AND settings carries no overrides key, skip the round with
|
|
37
38
|
* reason 'store-unavailable' and leave settings.models untouched — the
|