dsh-keys-manager 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-keys-manager",
3
- "version": "0.3.1",
3
+ "version": "0.3.2",
4
4
  "type": "module",
5
5
  "description": "Multi-key management for DSH providers: sticky per-session round-robin, retry failover, cooldown recovery, and searchable extra providers.",
6
6
  "repository": {
@@ -78,7 +78,7 @@
78
78
  "react": "^18.2.0"
79
79
  },
80
80
  "dependencies": {
81
- "dsh-llm-omp-transports": "^0.3.1"
81
+ "dsh-llm-omp-transports": "^0.3.2"
82
82
  },
83
83
  "devDependencies": {
84
84
  "@deepseek-ai/cordis": "~4.1.0",
@@ -98,6 +98,7 @@
98
98
  "build": "node scripts/omp-oauth/build-oauth.mjs && tsc -p tsconfig.build.json && tsc -p tsconfig.client.json && node scripts/build-client.mjs && node scripts/verify-dist.mjs",
99
99
  "build:client": "tsc -p tsconfig.client.json && node scripts/build-client.mjs",
100
100
  "test": "vitest run",
101
+ "generate:capabilities": "node scripts/generate-extra-capabilities.mjs",
101
102
  "release:prepare": "node scripts/prepare-release.mjs"
102
103
  }
103
104
  }
@@ -0,0 +1,127 @@
1
+ /**
2
+ * Generate the committed image-capability index for the extra-provider routes.
3
+ *
4
+ * DSH's remote model discovery (`readListing`) reports no input modalities, so
5
+ * every row an extra-provider profile installs looks text-only. The pinned
6
+ * `@oh-my-pi/pi-catalog` does know which of those models accept images; this
7
+ * script copies that answer into a small committed module instead of importing
8
+ * a 12 MB catalog at runtime.
9
+ *
10
+ * Only routes the pinned catalog describes are emitted: an absent route means
11
+ * "the pin says nothing about it", which is not the same as "no image models",
12
+ * and the runtime merge leaves such a route's curated seed modalities alone.
13
+ *
14
+ * The output is deterministic — providers and ids sorted, fixed formatting, and
15
+ * a provenance header naming the package version and the sha256 of its
16
+ * `src/models.json` — so regenerating an unchanged pin is byte-identical.
17
+ *
18
+ * @module dsh-keys-manager/generate-extra-capabilities
19
+ */
20
+ import { createHash } from 'node:crypto'
21
+ import { existsSync, readFileSync, realpathSync, writeFileSync } from 'node:fs'
22
+ import { dirname, join } from 'node:path'
23
+ import { fileURLToPath } from 'node:url'
24
+
25
+ const scriptsDir = dirname(fileURLToPath(import.meta.url))
26
+ const repoRoot = dirname(scriptsDir)
27
+
28
+ /** Resolve the installed root of a package by walking up to its own manifest. */
29
+ export function packageRootOf(name) {
30
+ let dir = dirname(fileURLToPath(import.meta.resolve(name)))
31
+ while (dir !== dirname(dir)) {
32
+ const manifestPath = join(dir, 'package.json')
33
+ if (existsSync(manifestPath)) {
34
+ const manifest = JSON.parse(readFileSync(manifestPath, 'utf8'))
35
+ if (manifest.name === name) return dir
36
+ }
37
+ dir = dirname(dir)
38
+ }
39
+ throw new Error(`cannot locate the installed package ${name}`)
40
+ }
41
+
42
+ /**
43
+ * The extra-provider route ids, read out of the seed catalog module. The array
44
+ * literal is plain JSON, so slicing it out keeps one source of truth instead of
45
+ * a second hand-maintained id list that could drift.
46
+ */
47
+ export function readProviderIds(root = repoRoot) {
48
+ const source = readFileSync(join(root, 'src/providers.ts'), 'utf8')
49
+ const start = source.indexOf('deepFreeze([\n')
50
+ const end = source.indexOf('\n])', start)
51
+ if (start < 0 || end < 0) throw new Error('cannot find the EXTRA_PROVIDERS array in src/providers.ts')
52
+ const providers = JSON.parse(source.slice(start + 'deepFreeze('.length, end + 2))
53
+ return providers.map(provider => provider.id)
54
+ }
55
+
56
+ /** Per route, the catalog model ids whose entry accepts image input. */
57
+ export function buildImageIndex(catalog, providerIds) {
58
+ const index = {}
59
+ for (const providerId of [...providerIds].sort()) {
60
+ const rows = catalog[providerId]
61
+ if (rows === null || typeof rows !== 'object') continue
62
+ index[providerId] = Object.values(rows)
63
+ .filter(row => Array.isArray(row.input) && row.input.includes('image'))
64
+ .map(row => row.id)
65
+ .sort()
66
+ }
67
+ return index
68
+ }
69
+
70
+ /** Render the committed module. Pure: the same index always produces the same bytes. */
71
+ export function renderCapabilitiesSource(index, provenance) {
72
+ const lines = [
73
+ '/**',
74
+ ' * Generated by scripts/generate-extra-capabilities.mjs — do not edit by hand;',
75
+ ' * run `pnpm run generate:capabilities` after bumping @oh-my-pi/pi-catalog.',
76
+ ' *',
77
+ ` * Source: @oh-my-pi/pi-catalog@${provenance.version} src/models.json (sha256:${provenance.hash}).`,
78
+ ' *',
79
+ ' * The model ids the pinned catalog marks image-capable, per extra-provider',
80
+ ' * route. A route the catalog does not describe is absent: the pin has no',
81
+ ' * answer for it, so its rows keep the modality the seed catalog gives them.',
82
+ ' */',
83
+ 'export const EXTRA_IMAGE_MODELS: Readonly<Record<string, readonly string[]>> = {',
84
+ ]
85
+ for (const providerId of Object.keys(index).sort()) {
86
+ lines.push(` ${JSON.stringify(providerId)}: [`)
87
+ for (const id of index[providerId]) lines.push(` ${JSON.stringify(id)},`)
88
+ lines.push(' ],')
89
+ }
90
+ lines.push('}', '')
91
+ return lines.join('\n')
92
+ }
93
+
94
+ /** The full committed source for one installed catalog and one route list. */
95
+ export function generateCapabilitiesSource(catalogRoot, providerIds) {
96
+ const manifest = JSON.parse(readFileSync(join(catalogRoot, 'package.json'), 'utf8'))
97
+ const bytes = readFileSync(join(catalogRoot, 'src/models.json'))
98
+ const catalog = JSON.parse(bytes.toString('utf8'))
99
+ return renderCapabilitiesSource(
100
+ buildImageIndex(catalog, providerIds),
101
+ { version: manifest.version, hash: createHash('sha256').update(bytes).digest('hex') },
102
+ )
103
+ }
104
+
105
+ function main() {
106
+ const catalogRoot = packageRootOf('@oh-my-pi/pi-catalog')
107
+ const source = generateCapabilitiesSource(catalogRoot, readProviderIds())
108
+ writeFileSync(join(repoRoot, 'src/extra-capabilities.generated.ts'), source, 'utf8')
109
+ console.log(`wrote src/extra-capabilities.generated.ts (${source.length} bytes)`)
110
+ }
111
+
112
+ /**
113
+ * Whether this module is the entry point of the running process. Compared by
114
+ * real path, not by string: a symlinked checkout runs `argv[1]` through a
115
+ * different path than `import.meta.url`.
116
+ */
117
+ function isDirectExecution() {
118
+ if (process.argv[1] === undefined) return false
119
+ try {
120
+ return realpathSync(process.argv[1]) === realpathSync(fileURLToPath(import.meta.url))
121
+ } catch {
122
+ return false
123
+ }
124
+ }
125
+
126
+ // Importing the module (tests) must not write the generated file.
127
+ if (isDirectExecution()) main()