typeclaw 0.48.0 → 0.48.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.
Files changed (41) hide show
  1. package/package.json +1 -1
  2. package/src/agent/plugin-tools.ts +68 -9
  3. package/src/agent/system-prompt.ts +8 -0
  4. package/src/agent-messenger/config-dir.ts +131 -0
  5. package/src/agent-messenger/host-config-dir.ts +121 -0
  6. package/src/bundled-plugins/guard/policies/managed-config.ts +7 -20
  7. package/src/bundled-plugins/guard/policies/non-workspace-write.ts +7 -20
  8. package/src/bundled-plugins/guard/policies/skill-authoring.ts +8 -20
  9. package/src/bundled-plugins/memory/README.md +2 -0
  10. package/src/bundled-plugins/memory/dreaming.ts +2 -0
  11. package/src/bundled-plugins/security/policies/private-surface-read.ts +10 -23
  12. package/src/cli/channel.ts +15 -16
  13. package/src/cli/hostd.ts +8 -13
  14. package/src/cli/init.ts +21 -7
  15. package/src/cli/model.ts +6 -3
  16. package/src/cli/restart.ts +13 -15
  17. package/src/compose/restart.ts +14 -6
  18. package/src/config/providers.ts +106 -143
  19. package/src/container/agent-operation-lock.ts +85 -0
  20. package/src/container/controller.ts +6 -0
  21. package/src/container/index.ts +8 -0
  22. package/src/container/restart.ts +55 -0
  23. package/src/container/shared.ts +5 -0
  24. package/src/container/start.ts +112 -4
  25. package/src/container/stop.ts +17 -1
  26. package/src/hostd/daemon.ts +1 -1
  27. package/src/hostd/paths.ts +17 -0
  28. package/src/hostd/supervisor.ts +27 -10
  29. package/src/init/dockerfile.ts +6 -5
  30. package/src/init/env-file.ts +42 -18
  31. package/src/init/instagram-auth.ts +55 -31
  32. package/src/init/kakaotalk-auth.ts +1 -5
  33. package/src/init/line-auth.ts +45 -29
  34. package/src/init/models-dev.ts +114 -7
  35. package/src/init/opengateway-catalog.ts +120 -0
  36. package/src/path-safety/real-intended-path.ts +111 -0
  37. package/src/sandbox/canonical-secrets.ts +10 -1
  38. package/src/sandbox/errors.ts +0 -32
  39. package/src/sandbox/privileged-runtime.ts +8 -94
  40. package/src/test-helpers/fake-agent-operation-lock.ts +66 -0
  41. package/typeclaw.schema.json +10 -30
@@ -3,10 +3,16 @@ import { join } from 'node:path'
3
3
 
4
4
  const ENV_FILE = '.env'
5
5
 
6
- // Parse the agent's `.env` into a key-value map, matching Docker's
7
- // `--env-file` parser semantics: blank lines and `#`-lines ignored, no
8
- // quote stripping, no shell expansion, no whitespace trimming around `=`.
9
- // Lines without `=` are skipped. Last value wins on duplicate keys.
6
+ // Parse the agent's `.env` into a key-value map, mirroring Docker's
7
+ // `--env-file` parser (docker/cli `pkg/kvfile.parseKeyValueFile`): a UTF-8 BOM
8
+ // is stripped from the FIRST line only, every line is left-trimmed of Unicode
9
+ // whitespace BEFORE the blank/`#` check, and nothing is trimmed around or after
10
+ // `=` (no quote stripping, no shell expansion). Last value wins on duplicates.
11
+ //
12
+ // The normalization is load-bearing, not cosmetic: Docker accepts a BOM-prefixed
13
+ // or indented `KEY=value` and passes it to the container, so a parser that missed
14
+ // those would report an operator-declared variable as absent — which callers read
15
+ // as "TypeClaw may manage this", e.g. migrating a deliberately pinned directory.
10
16
  export function readEnvFile(cwd: string): Map<string, string> {
11
17
  const out = new Map<string, string>()
12
18
  let raw: string
@@ -16,18 +22,35 @@ export function readEnvFile(cwd: string): Map<string, string> {
16
22
  if (err instanceof Error && 'code' in err && err.code === 'ENOENT') return out
17
23
  throw err
18
24
  }
19
- for (const line of raw.split(/\r?\n/)) {
20
- if (line.length === 0) continue
21
- if (line.startsWith('#')) continue
22
- const eq = line.indexOf('=')
23
- if (eq <= 0) continue
24
- const key = line.slice(0, eq)
25
- const value = line.slice(eq + 1)
26
- out.set(key, value)
25
+ const lines = raw.split(/\r?\n/)
26
+ for (const [index, rawLine] of lines.entries()) {
27
+ const parsed = parseEnvLine(rawLine, index)
28
+ if (parsed === null) continue
29
+ out.set(parsed.key, parsed.value)
27
30
  }
28
31
  return out
29
32
  }
30
33
 
34
+ // Shared by the reader and the writer so a declaration one of them recognizes
35
+ // can never be invisible to the other — an indented key that parsed as declared
36
+ // but did not match on write would append a duplicate instead of replacing.
37
+ function parseEnvLine(rawLine: string, index: number): { key: string; value: string } | null {
38
+ const withoutBom = index === 0 ? rawLine.replace(/^\uFEFF/, '') : rawLine
39
+ // \p{White_Space}, not \s: JS \s also matches U+FEFF (and misses U+0085),
40
+ // so \s would strip a BOM on EVERY line while Go's unicode.IsSpace — which
41
+ // Docker uses — strips it on line 1 only.
42
+ const line = withoutBom.replace(/^\p{White_Space}+/u, '')
43
+ if (line.length === 0) return null
44
+ if (line.startsWith('#')) return null
45
+ const eq = line.indexOf('=')
46
+ if (eq <= 0) return null
47
+ const key = line.slice(0, eq)
48
+ // Docker rejects the whole file when a key contains a space or tab, so such
49
+ // a line can never reach a container; skip it rather than inventing a key.
50
+ if (/[ \t]/.test(key)) return null
51
+ return { key, value: line.slice(eq + 1) }
52
+ }
53
+
31
54
  export function hasEnvKey(cwd: string, key: string): boolean {
32
55
  const value = readEnvFile(cwd).get(key)
33
56
  return value !== undefined && value.length > 0
@@ -52,13 +75,14 @@ export function appendOrReplaceEnvKey(cwd: string, key: string, value: string):
52
75
  // regardless of replace-vs-append path.
53
76
  if (lines.length > 0 && lines[lines.length - 1] === '') lines.pop()
54
77
  let replaced = false
55
- const next = lines.map((line) => {
56
- if (line.startsWith('#')) return line
57
- const eq = line.indexOf('=')
58
- if (eq <= 0) return line
59
- if (line.slice(0, eq) !== key) return line
78
+ const next = lines.map((line, index) => {
79
+ const parsed = parseEnvLine(line, index)
80
+ if (parsed === null || parsed.key !== key) return line
60
81
  replaced = true
61
- return `${key}=${value}`
82
+ // Re-emit the BOM so replacing the first line does not silently strip the
83
+ // file's encoding marker.
84
+ const bom = index === 0 && line.startsWith('\uFEFF') ? '\uFEFF' : ''
85
+ return `${bom}${key}=${value}`
62
86
  })
63
87
  if (!replaced) next.push(`${key}=${value}`)
64
88
  const out = `${next.join('\n')}\n`
@@ -8,10 +8,20 @@ import {
8
8
  type InstagramAccount,
9
9
  } from 'agent-messenger/instagram'
10
10
 
11
+ import {
12
+ prepareAgentMessengerHostConfigDir,
13
+ type HostAgentMessengerConfigResult,
14
+ } from '@/agent-messenger/host-config-dir'
15
+ import { type WithAgentOperationLock, withAgentOperationLock } from '@/container/agent-operation-lock'
11
16
  import { SecretsInstagramCredentialStore } from '@/secrets/instagram-store'
12
17
 
13
18
  export type InstagramBootstrapStatus = { ok: true } | { ok: false; reason: string }
14
19
 
20
+ export type AgentMessengerHostConfigDeps = {
21
+ prepareConfigDir?: (agentDir: string) => Promise<HostAgentMessengerConfigResult>
22
+ operationLock?: WithAgentOperationLock
23
+ }
24
+
15
25
  // Instagram gates a fresh login behind one of two interactive second factors:
16
26
  // a 2FA code (authenticator app / SMS the account already has enabled) or a
17
27
  // checkpoint challenge (a suspicious-login verification code sent to email or
@@ -78,44 +88,58 @@ export function instagramSecretsPath(agentDir: string): string {
78
88
  return join(agentDir, 'secrets.json')
79
89
  }
80
90
 
81
- export function instagramConfigDir(agentDir: string): string {
82
- return join(agentDir, 'workspace', '.agent-messenger')
91
+ export async function runInstagramBootstrap(
92
+ input: InstagramLoginInput,
93
+ deps: AgentMessengerHostConfigDeps = {},
94
+ ): Promise<InstagramBootstrapStatus> {
95
+ const operationLock = deps.operationLock ?? withAgentOperationLock
96
+ try {
97
+ const locked = await operationLock({ agentDir: input.agentDir, operation: 'instagram-auth' }, async () => {
98
+ return await runInstagramBootstrapLocked(input, deps)
99
+ })
100
+ return locked.ok ? locked.value : { ok: false, reason: locked.reason }
101
+ } catch (err) {
102
+ return { ok: false, reason: err instanceof Error ? err.message : String(err) }
103
+ }
83
104
  }
84
105
 
85
- export async function runInstagramBootstrap(input: InstagramLoginInput): Promise<InstagramBootstrapStatus> {
106
+ async function runInstagramBootstrapLocked(
107
+ input: InstagramLoginInput,
108
+ deps: AgentMessengerHostConfigDeps,
109
+ ): Promise<InstagramBootstrapStatus> {
86
110
  try {
111
+ const prepared = await (deps.prepareConfigDir ?? prepareAgentMessengerHostConfigDir)(input.agentDir)
112
+ if (!prepared.ok) return prepared
113
+
87
114
  const store = new SecretsInstagramCredentialStore({
88
115
  mode: 'host',
89
116
  secretsPath: instagramSecretsPath(input.agentDir),
90
117
  })
91
- const result = await withInstagramConfigDir(
92
- instagramConfigDir(input.agentDir),
93
- async (): Promise<InstagramBootstrapStatus> => {
94
- const sdkManager = new InstagramCredentialManager()
95
- const manager = input.credentialManager ?? sdkManager
96
- const accountId = createAccountId(input.username)
97
- const paths = await manager.ensureAccountPaths(accountId)
98
- const client = input.client ?? buildInstagramClient(sdkManager)
99
- client.setSessionPath(paths.session_path)
100
-
101
- // A checkpoint login persists a partial SDK session (challenge_path +
102
- // cookies) inside authenticate(), before we know whether the operator
103
- // can finish it. Snapshot the file up front and restore it on any
104
- // failure — whether completeLogin returns a non-ok status (fail-closed
105
- // / cancelled) or throws (e.g. challengeSubmitCode rejecting a wrong
106
- // code) — so a half-written session is never left behind. Rethrow so
107
- // the outer catch reports the error.
108
- const sessionSnapshot = await snapshotSessionFile(paths.session_path)
109
- try {
110
- const result = await completeLogin({ store, manager, client, accountId, input })
111
- if (!result.ok) await restoreSessionFile(paths.session_path, sessionSnapshot)
112
- return result
113
- } catch (err) {
114
- await restoreSessionFile(paths.session_path, sessionSnapshot)
115
- throw err
116
- }
117
- },
118
- )
118
+ const result = await withInstagramConfigDir(prepared.hostDir, async (): Promise<InstagramBootstrapStatus> => {
119
+ const sdkManager = new InstagramCredentialManager()
120
+ const manager = input.credentialManager ?? sdkManager
121
+ const accountId = createAccountId(input.username)
122
+ const paths = await manager.ensureAccountPaths(accountId)
123
+ const client = input.client ?? buildInstagramClient(sdkManager)
124
+ client.setSessionPath(paths.session_path)
125
+
126
+ // A checkpoint login persists a partial SDK session (challenge_path +
127
+ // cookies) inside authenticate(), before we know whether the operator
128
+ // can finish it. Snapshot the file up front and restore it on any
129
+ // failure — whether completeLogin returns a non-ok status (fail-closed
130
+ // / cancelled) or throws (e.g. challengeSubmitCode rejecting a wrong
131
+ // code) — so a half-written session is never left behind. Rethrow so
132
+ // the outer catch reports the error.
133
+ const sessionSnapshot = await snapshotSessionFile(paths.session_path)
134
+ try {
135
+ const result = await completeLogin({ store, manager, client, accountId, input })
136
+ if (!result.ok) await restoreSessionFile(paths.session_path, sessionSnapshot)
137
+ return result
138
+ } catch (err) {
139
+ await restoreSessionFile(paths.session_path, sessionSnapshot)
140
+ throw err
141
+ }
142
+ })
119
143
  return result
120
144
  } catch (err) {
121
145
  return { ok: false, reason: err instanceof Error ? err.message : String(err) }
@@ -1,4 +1,4 @@
1
- import { join, posix, resolve } from 'node:path'
1
+ import { join, resolve } from 'node:path'
2
2
 
3
3
  import { loginFlow as upstreamLoginFlow } from 'agent-messenger/kakaotalk'
4
4
 
@@ -33,10 +33,6 @@ export type LoginFlowFn = typeof upstreamLoginFlow
33
33
  export type LoginFlowOptions = Parameters<LoginFlowFn>[0]
34
34
  export type LoginFlowResult = Awaited<ReturnType<LoginFlowFn>>
35
35
 
36
- export function kakaotalkConfigDir(agentDir: string): string {
37
- return posix.join(agentDir, 'workspace', '.agent-messenger')
38
- }
39
-
40
36
  export function kakaotalkSecretsPath(agentDir: string): string {
41
37
  return join(agentDir, 'secrets.json')
42
38
  }
@@ -2,10 +2,20 @@ import { join } from 'node:path'
2
2
 
3
3
  import { LineClient as RealLineClient, LineCredentialManager, type LineLoginResult } from 'agent-messenger/line'
4
4
 
5
+ import {
6
+ prepareAgentMessengerHostConfigDir,
7
+ type HostAgentMessengerConfigResult,
8
+ } from '@/agent-messenger/host-config-dir'
9
+ import { type WithAgentOperationLock, withAgentOperationLock } from '@/container/agent-operation-lock'
5
10
  import { SecretsLineCredentialStore } from '@/secrets/line-store'
6
11
 
7
12
  export type LineBootstrapStatus = { ok: true } | { ok: false; reason: string }
8
13
 
14
+ export type AgentMessengerHostConfigDeps = {
15
+ prepareConfigDir?: (agentDir: string) => Promise<HostAgentMessengerConfigResult>
16
+ operationLock?: WithAgentOperationLock
17
+ }
18
+
9
19
  export type LineLoginCallbacks = {
10
20
  onQRUrl?: (url: string) => void | Promise<void>
11
21
  onPincode: (pin: string) => void
@@ -50,36 +60,41 @@ export function lineSecretsPath(agentDir: string): string {
50
60
  return join(agentDir, 'secrets.json')
51
61
  }
52
62
 
53
- // The SDK persists E2EE (Letter-Sealing) key material under
54
- // `<AGENT_MESSENGER_CONFIG_DIR>/line-storage/`. The container sets that env to
55
- // the agent workspace (src/init/dockerfile.ts), but a host-stage login (init /
56
- // `channel reauth line`) would otherwise fall back to `~/.config/agent-messenger`
57
- // — so the E2EE key gets written somewhere the container never reads, and inbound
58
- // Letter-Sealing messages stay undecryptable. Point the host login at the same
59
- // per-agent dir the container uses so the key lands where the runtime reads it.
60
- export function lineConfigDir(agentDir: string): string {
61
- return join(agentDir, 'workspace', '.agent-messenger')
63
+ export async function runLineBootstrap(
64
+ input: LineLoginInput,
65
+ deps: AgentMessengerHostConfigDeps = {},
66
+ ): Promise<LineBootstrapStatus> {
67
+ const operationLock = deps.operationLock ?? withAgentOperationLock
68
+ try {
69
+ const locked = await operationLock({ agentDir: input.agentDir, operation: 'line-auth' }, async () => {
70
+ return await runLineBootstrapLocked(input, deps)
71
+ })
72
+ return locked.ok ? locked.value : { ok: false, reason: locked.reason }
73
+ } catch (err) {
74
+ return { ok: false, reason: err instanceof Error ? err.message : String(err) }
75
+ }
62
76
  }
63
77
 
64
- export async function runLineBootstrap(input: LineLoginInput): Promise<LineBootstrapStatus> {
78
+ async function runLineBootstrapLocked(
79
+ input: LineLoginInput,
80
+ deps: AgentMessengerHostConfigDeps,
81
+ ): Promise<LineBootstrapStatus> {
65
82
  try {
83
+ const prepared = await (deps.prepareConfigDir ?? prepareAgentMessengerHostConfigDir)(input.agentDir)
84
+ if (!prepared.ok) return prepared
85
+
66
86
  const store = new SecretsLineCredentialStore({ mode: 'host', secretsPath: lineSecretsPath(input.agentDir) })
67
87
 
68
- // The env is set only for the duration of client construction + login (when
69
- // the SDK reads it to locate line-storage) and restored after, so a second
70
- // bootstrap for a different agent in the same process can't inherit the
71
- // first agent's path. An already-set value (the container's Dockerfile env)
72
- // is left untouched.
73
- const result = await withLineConfigDir(lineConfigDir(input.agentDir), () => {
74
- // The LINE SDK persists the minted auth_token + certificate by calling
75
- // setAccount() on whatever credential manager the client was built with.
76
- // Wiring our secrets.json-backed store in here means a successful login
77
- // writes straight to secrets.json#channels.line — no second copy in
78
- // ~/.config/agent-messenger to keep in sync.
79
- const client = input.client ?? buildLineClient(store)
80
-
81
- return suppressLineTokenInfoDump(() =>
82
- input.method === 'qr'
88
+ const result = await suppressLineTokenInfoDump(() =>
89
+ withLineConfigDir(prepared.hostDir, () => {
90
+ // The SDK writes Letter-Sealing keys under AGENT_MESSENGER_CONFIG_DIR.
91
+ // The prepared host path follows the live container when one is running,
92
+ // so reauth never strands E2EE material in a directory that runtime cannot read.
93
+ const client = input.client ?? buildLineClient(store)
94
+
95
+ // The LINE SDK persists the minted auth_token + certificate by calling
96
+ // setAccount() on the credential manager wired into the client.
97
+ return input.method === 'qr'
83
98
  ? client.loginWithQR({
84
99
  onQRUrl: async (url) => {
85
100
  await input.callbacks.onQRUrl?.(url)
@@ -90,9 +105,9 @@ export async function runLineBootstrap(input: LineLoginInput): Promise<LineBoots
90
105
  email: input.email,
91
106
  password: input.password,
92
107
  onPincode: input.callbacks.onPincode,
93
- }),
94
- )
95
- })
108
+ })
109
+ }),
110
+ )
96
111
 
97
112
  if (!result.authenticated || result.account_id === undefined) {
98
113
  const reason = result.message ?? result.error ?? 'LINE login did not authenticate'
@@ -126,11 +141,12 @@ function buildLineClient(store: SecretsLineCredentialStore): LineLoginClient {
126
141
 
127
142
  async function withLineConfigDir<T>(dir: string, fn: () => Promise<T>): Promise<T> {
128
143
  const previous = process.env.AGENT_MESSENGER_CONFIG_DIR
129
- if (previous === undefined) process.env.AGENT_MESSENGER_CONFIG_DIR = dir
144
+ process.env.AGENT_MESSENGER_CONFIG_DIR = dir
130
145
  try {
131
146
  return await fn()
132
147
  } finally {
133
148
  if (previous === undefined) delete process.env.AGENT_MESSENGER_CONFIG_DIR
149
+ else process.env.AGENT_MESSENGER_CONFIG_DIR = previous
134
150
  }
135
151
  }
136
152
 
@@ -9,6 +9,8 @@ import {
9
9
  type ModelRef,
10
10
  } from '@/config/providers'
11
11
 
12
+ import { fetchOpenGatewayCatalog, type OpenGatewayModel } from './opengateway-catalog'
13
+
12
14
  const MODELS_DEV_URL = 'https://models.dev/api.json'
13
15
  const REQUEST_TIMEOUT_MS = 10_000
14
16
 
@@ -113,12 +115,17 @@ type ModelsDevProvider = {
113
115
  export type FetchModelsResult = {
114
116
  options: ModelOption[]
115
117
  source: 'models.dev' | 'curated'
118
+ sources: {
119
+ modelsDev: 'live' | 'unavailable'
120
+ openGatewayCatalog: 'live' | 'unavailable'
121
+ openGatewayPrices: 'live' | 'unavailable'
122
+ }
123
+ warnings: string[]
116
124
  warning?: string
117
125
  }
118
126
 
119
- // Pulls the live model catalog from models.dev, intersects it with our
120
- // curated KNOWN_PROVIDERS allowlist, and returns one ModelOption per
121
- // (provider, model) pair the user is allowed to pick at init time.
127
+ // Pulls live catalogs from models.dev and OpenGateway, keeps every curated
128
+ // entry, and appends non-curated models whose provider refs TypeClaw can route.
122
129
  //
123
130
  // Falls back to the curated list alone if the network is unreachable, the
124
131
  // response is malformed, or any unexpected error fires — the wizard MUST
@@ -129,16 +136,58 @@ export async function fetchModelOptions(
129
136
  ): Promise<FetchModelsResult> {
130
137
  const fetchImpl = options.fetchImpl ?? fetch
131
138
  const timeoutMs = options.timeoutMs ?? REQUEST_TIMEOUT_MS
139
+ const [modelsDev, openGateway] = await Promise.all([
140
+ fetchModelsDev(fetchImpl, timeoutMs),
141
+ fetchOpenGatewayCatalog({ fetchImpl, timeoutMs }),
142
+ ])
143
+ // Composition is inside the guard because only the models.dev ROOT is shape-
144
+ // checked on fetch: a well-formed envelope can still carry a malformed nested
145
+ // model, and this function's contract is that `typeclaw init` keeps working
146
+ // on a fresh machine no matter what the network returns.
147
+ let baseOptions: ModelOption[]
148
+ let compositionWarning: string | undefined
149
+ try {
150
+ baseOptions = modelsDev.status === 'live' ? mergeWithCurated(modelsDev.data) : curatedOptions()
151
+ } catch (error) {
152
+ baseOptions = curatedOptions()
153
+ compositionWarning = `models.dev returned unusable data (${error instanceof Error ? error.message : String(error)})`
154
+ }
155
+ const mergedOptions = mergeOpenGateway(baseOptions, openGateway.models)
156
+ const warnings = [
157
+ ...(modelsDev.status === 'unavailable' ? [modelsDev.warning] : []),
158
+ ...(compositionWarning !== undefined ? [compositionWarning] : []),
159
+ ...openGateway.warnings,
160
+ ]
161
+ const warning = warnings.length > 0 ? warnings.join('; ') : undefined
162
+ return {
163
+ options: mergedOptions,
164
+ source: modelsDev.status === 'live' ? 'models.dev' : 'curated',
165
+ sources: {
166
+ modelsDev: modelsDev.status,
167
+ openGatewayCatalog: openGateway.catalog,
168
+ openGatewayPrices: openGateway.prices,
169
+ },
170
+ warnings,
171
+ ...(warning !== undefined ? { warning } : {}),
172
+ }
173
+ }
174
+
175
+ type ModelsDevResult =
176
+ | { status: 'live'; data: Record<string, ModelsDevProvider> }
177
+ | { status: 'unavailable'; warning: string }
178
+
179
+ async function fetchModelsDev(fetchImpl: typeof fetch, timeoutMs: number): Promise<ModelsDevResult> {
132
180
  try {
133
181
  const res = await fetchImpl(MODELS_DEV_URL, { signal: AbortSignal.timeout(timeoutMs) })
134
182
  if (!res.ok) {
135
- return { options: curatedOptions(), source: 'curated', warning: `models.dev returned ${res.status}` }
183
+ return { status: 'unavailable', warning: `models.dev returned ${res.status}` }
136
184
  }
137
- const data = (await res.json()) as Record<string, ModelsDevProvider>
138
- return { options: mergeWithCurated(data), source: 'models.dev' }
185
+ const data: unknown = await res.json()
186
+ if (!isRecord(data)) return { status: 'unavailable', warning: 'models.dev returned malformed JSON' }
187
+ return { status: 'live', data: data as Record<string, ModelsDevProvider> }
139
188
  } catch (error) {
140
189
  const reason = error instanceof Error ? error.message : String(error)
141
- return { options: curatedOptions(), source: 'curated', warning: reason }
190
+ return { status: 'unavailable', warning: reason }
142
191
  }
143
192
  }
144
193
 
@@ -184,6 +233,7 @@ function mergeWithCurated(data: Record<string, ModelsDevProvider>): ModelOption[
184
233
  const upstream = upstreamProviderFor(data, providerId)
185
234
  const upstreamModels = upstream?.models ?? {}
186
235
  for (const [fallbackModelId, upstreamModel] of Object.entries(upstreamModels)) {
236
+ if (!isRecord(upstreamModel)) continue
187
237
  const modelId = upstreamModel.id ?? fallbackModelId
188
238
  if (modelId.trim().length === 0) continue
189
239
  const ref = `${providerId}/${modelId}`
@@ -195,6 +245,59 @@ function mergeWithCurated(data: Record<string, ModelsDevProvider>): ModelOption[
195
245
  return out
196
246
  }
197
247
 
248
+ function mergeOpenGateway(options: ModelOption[], models: OpenGatewayModel[]): ModelOption[] {
249
+ const out = [...options]
250
+ const seen = new Set(options.map((option) => option.ref))
251
+ for (const model of models) {
252
+ const ref = `opengateway/${model.id}`
253
+ if (seen.has(ref) || !isModelRef(ref)) continue
254
+ const sibling = nativeSiblingFor(model)
255
+ out.push({
256
+ ref,
257
+ providerId: 'opengateway',
258
+ providerName: KNOWN_PROVIDERS.opengateway.name,
259
+ modelId: model.id,
260
+ modelName: sibling?.name ?? model.id,
261
+ reasoning: sibling?.reasoning ?? false,
262
+ contextWindow: sibling?.contextWindow ?? null,
263
+ maxTokens: sibling?.maxTokens ?? null,
264
+ cost: model.cost,
265
+ curated: false,
266
+ supportsVision: model.input.includes('image'),
267
+ })
268
+ seen.add(ref)
269
+ }
270
+ return out
271
+ }
272
+
273
+ type NativeSibling = {
274
+ name: string
275
+ reasoning?: boolean
276
+ contextWindow?: number
277
+ maxTokens?: number
278
+ }
279
+
280
+ function nativeSiblingFor(model: OpenGatewayModel): NativeSibling | undefined {
281
+ const slash = model.id.indexOf('/')
282
+ if (slash === -1) return undefined
283
+ const creator = model.id.slice(0, slash)
284
+ const modelId = model.id.slice(slash + 1)
285
+ const providerId = nativeProviderId(model.ownedBy) ?? nativeProviderId(creator)
286
+ if (providerId === undefined) return undefined
287
+ return (KNOWN_PROVIDERS[providerId].models as Record<string, NativeSibling>)[modelId]
288
+ }
289
+
290
+ // The gateway namespaces some creators differently from this registry's
291
+ // provider ids (`moonshotai` vs `moonshot`, `x-ai` vs `xai`, `z-ai` vs `zai`).
292
+ // Without the alias the sibling lookup misses and the model loses its
293
+ // contextWindow/maxTokens, which neither OpenGateway endpoint publishes.
294
+ function nativeProviderId(value: string): KnownProviderId | undefined {
295
+ const aliases: Record<string, KnownProviderId> = { moonshotai: 'moonshot', 'x-ai': 'xai', 'z-ai': 'zai' }
296
+ const candidate = aliases[value] ?? value
297
+ if (!(candidate in KNOWN_PROVIDERS) || candidate === 'opengateway') return undefined
298
+ return candidate as KnownProviderId
299
+ }
300
+
198
301
  type BuildOptionOpts = {
199
302
  curated: boolean
200
303
  upstream?: ModelsDevModel
@@ -250,3 +353,7 @@ function resolveCost(cost: ModelsDevModel['cost']): ModelOptionCost | null {
250
353
  cacheWrite: cost.cacheWrite ?? cost.cache_write ?? 0,
251
354
  }
252
355
  }
356
+
357
+ function isRecord(value: unknown): value is Record<string, unknown> {
358
+ return typeof value === 'object' && value !== null && !Array.isArray(value)
359
+ }
@@ -0,0 +1,120 @@
1
+ import type { ModelOptionCost } from './models-dev'
2
+
3
+ const CATALOG_URL = 'https://apis.opengateway.ai/v1/models'
4
+ const PRICES_URL = 'https://opengateway.ai/api/model-prices'
5
+ const REQUEST_TIMEOUT_MS = 10_000
6
+ const PER_MILLION = 1_000_000
7
+
8
+ export type OpenGatewayModel = {
9
+ id: string
10
+ ownedBy: string
11
+ input: string[]
12
+ cost: ModelOptionCost | null
13
+ }
14
+
15
+ export type OpenGatewayCatalogResult = {
16
+ models: OpenGatewayModel[]
17
+ catalog: 'live' | 'unavailable'
18
+ prices: 'live' | 'unavailable'
19
+ warnings: string[]
20
+ }
21
+
22
+ type SourceResult<T> = { status: 'live'; value: T } | { status: 'unavailable'; warning: string }
23
+
24
+ export async function fetchOpenGatewayCatalog(
25
+ options: { fetchImpl?: typeof fetch; timeoutMs?: number } = {},
26
+ ): Promise<OpenGatewayCatalogResult> {
27
+ const fetchImpl = options.fetchImpl ?? fetch
28
+ const timeoutMs = options.timeoutMs ?? REQUEST_TIMEOUT_MS
29
+ const [catalogResult, pricesResult] = await Promise.all([
30
+ fetchCatalog(fetchImpl, timeoutMs),
31
+ fetchPrices(fetchImpl, timeoutMs),
32
+ ])
33
+ const warnings = [
34
+ ...(catalogResult.status === 'unavailable' ? [catalogResult.warning] : []),
35
+ ...(pricesResult.status === 'unavailable' ? [pricesResult.warning] : []),
36
+ ]
37
+ const prices = pricesResult.status === 'live' ? pricesResult.value : {}
38
+ const models =
39
+ catalogResult.status === 'live'
40
+ ? catalogResult.value.map((model) => ({ ...model, cost: costForModel(prices[model.id]) }))
41
+ : []
42
+
43
+ return {
44
+ models,
45
+ catalog: catalogResult.status,
46
+ prices: pricesResult.status,
47
+ warnings,
48
+ }
49
+ }
50
+
51
+ async function fetchCatalog(fetchImpl: typeof fetch, timeoutMs: number): Promise<SourceResult<OpenGatewayModel[]>> {
52
+ try {
53
+ const response = await fetchImpl(CATALOG_URL, { signal: AbortSignal.timeout(timeoutMs) })
54
+ if (!response.ok) return unavailable('catalog', `returned ${response.status}`)
55
+ const value: unknown = await response.json()
56
+ if (!isRecord(value) || !Array.isArray(value.data)) return unavailable('catalog', 'returned malformed JSON')
57
+ return { status: 'live', value: value.data.flatMap(parseCatalogModel) }
58
+ } catch (error) {
59
+ return unavailable('catalog', reasonFrom(error))
60
+ }
61
+ }
62
+
63
+ async function fetchPrices(fetchImpl: typeof fetch, timeoutMs: number): Promise<SourceResult<Record<string, unknown>>> {
64
+ try {
65
+ const response = await fetchImpl(PRICES_URL, { signal: AbortSignal.timeout(timeoutMs) })
66
+ if (!response.ok) return unavailable('prices', `returned ${response.status}`)
67
+ const value: unknown = await response.json()
68
+ if (!isRecord(value)) return unavailable('prices', 'returned malformed JSON')
69
+ return { status: 'live', value }
70
+ } catch (error) {
71
+ return unavailable('prices', reasonFrom(error))
72
+ }
73
+ }
74
+
75
+ function parseCatalogModel(value: unknown): OpenGatewayModel[] {
76
+ if (!isRecord(value)) return []
77
+ if (typeof value.id !== 'string' || value.id.length === 0) return []
78
+ if (typeof value.owned_by !== 'string' || value.owned_by.length === 0) return []
79
+ if (value.status !== 'active') return []
80
+ if (!isRecord(value.modalities) || !stringArray(value.modalities.output).includes('text')) return []
81
+ if (!stringArray(value.endpoints).includes('chat_completions')) return []
82
+ const input = stringArray(value.modalities.input).filter((modality) => modality === 'text' || modality === 'image')
83
+ return [{ id: value.id, ownedBy: value.owned_by, input, cost: null }]
84
+ }
85
+
86
+ function costForModel(value: unknown): ModelOptionCost | null {
87
+ if (!isRecord(value)) return null
88
+ const input = requiredRate(value.inputCostPerToken)
89
+ const output = requiredRate(value.outputCostPerToken)
90
+ const cacheRead = optionalRate(value.cacheReadInputTokenCost)
91
+ const cacheWrite = optionalRate(value.cacheCreationInputTokenCost)
92
+ if (input === null || output === null || cacheRead === null || cacheWrite === null) return null
93
+ return { input, output, cacheRead, cacheWrite }
94
+ }
95
+
96
+ function requiredRate(value: unknown): number | null {
97
+ if (typeof value !== 'number' || !Number.isFinite(value) || value < 0) return null
98
+ return Number((value * PER_MILLION).toPrecision(15))
99
+ }
100
+
101
+ function optionalRate(value: unknown): number | null {
102
+ if (value === null || value === undefined) return 0
103
+ return requiredRate(value)
104
+ }
105
+
106
+ function unavailable<T>(source: 'catalog' | 'prices', reason: string): SourceResult<T> {
107
+ return { status: 'unavailable', warning: `OpenGateway ${source} ${reason}` }
108
+ }
109
+
110
+ function reasonFrom(error: unknown): string {
111
+ return error instanceof Error ? error.message : String(error)
112
+ }
113
+
114
+ function isRecord(value: unknown): value is Record<string, unknown> {
115
+ return typeof value === 'object' && value !== null && !Array.isArray(value)
116
+ }
117
+
118
+ function stringArray(value: unknown): string[] {
119
+ return Array.isArray(value) ? value.filter((entry): entry is string => typeof entry === 'string') : []
120
+ }