typeclaw 0.47.2 → 0.48.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/package.json +1 -1
- package/src/agent/index.ts +1 -2
- package/src/agent/loop-guard.ts +71 -41
- package/src/agent/plugin-tools.ts +234 -58
- package/src/agent/system-prompt.ts +1 -1
- package/src/agent/turn-runner.ts +12 -2
- package/src/agent-messenger/config-dir.ts +131 -0
- package/src/agent-messenger/host-config-dir.ts +121 -0
- package/src/bundled-plugins/agent-browser/index.ts +0 -38
- package/src/bundled-plugins/agent-browser/skills/agent-browser/SKILL.md +1 -1
- package/src/bundled-plugins/guard/policies/non-workspace-write.ts +16 -1
- package/src/bundled-plugins/memory/append-tool.ts +75 -2
- package/src/bundled-plugins/memory/frontmatter.ts +13 -2
- package/src/bundled-plugins/memory/load-memory.ts +1 -1
- package/src/bundled-plugins/memory/memory-logger.ts +5 -1
- package/src/bundled-plugins/memory/secret-detector.ts +7 -0
- package/src/bundled-plugins/memory/vector/hybrid.ts +3 -1
- package/src/bundled-plugins/memory/vector/passages.ts +5 -1
- package/src/bundled-plugins/security/index.ts +148 -98
- package/src/bundled-plugins/security/policies/private-surface-read.ts +43 -28
- package/src/channels/adapters/discord-bot-edit.ts +3 -5
- package/src/channels/adapters/discord-bot-reactions.ts +4 -6
- package/src/channels/adapters/discord-bot-slash-commands.ts +3 -1
- package/src/channels/adapters/discord-bot.ts +20 -23
- package/src/channels/adapters/discord-edit.ts +3 -5
- package/src/channels/adapters/discord-reactions.ts +4 -6
- package/src/channels/adapters/discord.ts +1 -1
- package/src/channels/adapters/github/decoy-reviewer.ts +2 -1
- package/src/channels/adapters/github/history.ts +2 -1
- package/src/channels/adapters/github/inbound.ts +4 -11
- package/src/channels/adapters/github/index.ts +6 -9
- package/src/channels/adapters/github/outbound.ts +3 -6
- package/src/channels/adapters/github/reactions.ts +3 -2
- package/src/channels/adapters/github/reconcile-cooldown-store.ts +4 -6
- package/src/channels/adapters/github/reconcile-open-prs.ts +5 -4
- package/src/channels/adapters/github/recover-failed-deliveries.ts +2 -5
- package/src/channels/adapters/github/review-state.ts +3 -2
- package/src/channels/adapters/github/review-thread-resolver.ts +3 -2
- package/src/channels/adapters/github/webhook-register.ts +5 -8
- package/src/channels/adapters/instagram-channel-resolver.ts +3 -5
- package/src/channels/adapters/instagram.ts +1 -1
- package/src/channels/adapters/kakaotalk-author-resolver.ts +2 -5
- package/src/channels/adapters/kakaotalk-channel-resolver.ts +3 -5
- package/src/channels/adapters/kakaotalk-fetch-attachment.ts +2 -1
- package/src/channels/adapters/kakaotalk-membership.ts +2 -5
- package/src/channels/adapters/kakaotalk-typing.ts +1 -1
- package/src/channels/adapters/kakaotalk.ts +1 -1
- package/src/channels/adapters/line-channel-resolver.ts +3 -5
- package/src/channels/adapters/line.ts +1 -1
- package/src/channels/adapters/slack-bot-edit.ts +1 -1
- package/src/channels/adapters/slack-bot-reactions.ts +4 -6
- package/src/channels/adapters/slack-bot.ts +8 -8
- package/src/channels/adapters/slack-edit.ts +3 -5
- package/src/channels/adapters/slack-reactions.ts +4 -6
- package/src/channels/adapters/slack.ts +1 -1
- package/src/channels/adapters/teams-edit.ts +2 -5
- package/src/channels/adapters/teams.ts +1 -1
- package/src/channels/adapters/telegram-bot-edit.ts +4 -7
- package/src/channels/adapters/telegram-bot.ts +1 -1
- package/src/channels/adapters/webex-bot.ts +1 -1
- package/src/channels/adapters/webex-edit.ts +3 -5
- package/src/channels/adapters/webex.ts +1 -1
- package/src/channels/github-review-claim.ts +89 -4
- package/src/channels/github-token-bridge.ts +2 -1
- package/src/channels/manager.ts +478 -64
- package/src/channels/membership-cache.ts +2 -5
- package/src/channels/persistence.ts +3 -6
- package/src/channels/reloadable.ts +23 -6
- package/src/channels/router.ts +134 -56
- package/src/channels/tunnel-bridge.ts +2 -1
- package/src/channels/types.ts +6 -3
- package/src/cli/channel.ts +15 -16
- package/src/cli/hostd.ts +61 -38
- package/src/cli/init.ts +21 -7
- package/src/cli/model.ts +6 -3
- package/src/cli/restart.ts +13 -15
- package/src/compose/restart.ts +14 -6
- package/src/config/config.ts +10 -0
- package/src/config/providers.ts +68 -0
- package/src/container/agent-operation-lock.ts +85 -0
- package/src/container/controller.ts +6 -0
- package/src/container/index.ts +8 -0
- package/src/container/restart.ts +55 -0
- package/src/container/shared.ts +5 -0
- package/src/container/start.ts +70 -4
- package/src/container/stop.ts +17 -1
- package/src/dependencies/bin-resolver.ts +88 -0
- package/src/dependencies/bins.ts +227 -0
- package/src/dependencies/index.ts +17 -0
- package/src/doctor/checks.ts +2 -0
- package/src/doctor/operational-incidents.ts +32 -0
- package/src/hostd/credential-rotation-apply.ts +94 -0
- package/src/hostd/daemon.ts +1 -1
- package/src/hostd/paths.ts +17 -0
- package/src/hostd/supervisor.ts +27 -10
- package/src/init/dockerfile.ts +6 -5
- package/src/init/env-file.ts +42 -18
- package/src/init/instagram-auth.ts +55 -31
- package/src/init/kakaotalk-auth.ts +1 -5
- package/src/init/line-auth.ts +45 -29
- package/src/init/models-dev.ts +131 -10
- package/src/init/opengateway-catalog.ts +120 -0
- package/src/init/validate-api-key.ts +26 -4
- package/src/operations/classify-tool-outcome.ts +151 -0
- package/src/operations/incidents.ts +274 -0
- package/src/operations/index.ts +3 -0
- package/src/operations/remediations.ts +41 -0
- package/src/reload/client.ts +8 -2
- package/src/reload/docker-exec-client.ts +10 -2
- package/src/reload/index.ts +1 -1
- package/src/reload/recover.ts +5 -3
- package/src/reload/registry.ts +5 -5
- package/src/reload/types.ts +9 -1
- package/src/run/index.ts +5 -0
- package/src/sandbox/canonical-secrets.ts +10 -1
- package/src/sandbox/dependency-bins.ts +125 -0
- package/src/sandbox/hidden-paths.ts +4 -1
- package/src/sandbox/index.ts +8 -0
- package/src/sandbox/policy.ts +1 -1
- package/src/sandbox/privileged-runtime.ts +7 -12
- package/src/sandbox/runtime-private.ts +4 -0
- package/src/sandbox/writable-zones.ts +4 -4
- package/src/server/index.ts +7 -5
- package/src/shared/protocol.ts +6 -1
- package/src/test-helpers/fake-agent-operation-lock.ts +66 -0
- package/typeclaw.schema.json +10 -5
- package/src/bundled-plugins/agent-browser/shim-install.ts +0 -179
- package/src/bundled-plugins/agent-browser/shim.ts +0 -108
- /package/src/channels/{adapters/describe-error.ts → describe-error.ts} +0 -0
package/src/init/models-dev.ts
CHANGED
|
@@ -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
|
|
|
@@ -16,7 +18,11 @@ const REQUEST_TIMEOUT_MS = 10_000
|
|
|
16
18
|
// KnownProviderId. Specifically, they ship Fireworks under `fireworks-ai`.
|
|
17
19
|
// This map is the single place that bridges the two namespaces; every other
|
|
18
20
|
// helper in this file works in OUR namespace.
|
|
19
|
-
|
|
21
|
+
// `null` means "never merge upstream models into this provider". Reserved for
|
|
22
|
+
// gateways whose ids are creator-qualified: models.dev lists bare ids like
|
|
23
|
+
// `gpt-5.5`, but OpenGateway only accepts `openai/gpt-5.5`, so merging would
|
|
24
|
+
// synthesize refs that pass `isModelRef()` and then 404 at request time.
|
|
25
|
+
const PROVIDER_TO_MODELS_DEV: Record<KnownProviderId, string | null> = {
|
|
20
26
|
openai: 'openai',
|
|
21
27
|
// openai-codex models live under the `openai` namespace on models.dev too
|
|
22
28
|
// (Codex is a backend, not a separate provider in their taxonomy). Curated
|
|
@@ -42,6 +48,16 @@ const PROVIDER_TO_MODELS_DEV: Record<KnownProviderId, string> = {
|
|
|
42
48
|
// metadata under `moonshot`, so we route lookups there; the curated
|
|
43
49
|
// `kimi-for-coding` alias is surfaced regardless of upstream membership.
|
|
44
50
|
'moonshot-coding': 'moonshot',
|
|
51
|
+
opengateway: null,
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function upstreamProviderFor(
|
|
55
|
+
data: Record<string, ModelsDevProvider>,
|
|
56
|
+
providerId: KnownProviderId,
|
|
57
|
+
): ModelsDevProvider | undefined {
|
|
58
|
+
const modelsDevId = PROVIDER_TO_MODELS_DEV[providerId]
|
|
59
|
+
if (modelsDevId === null) return undefined
|
|
60
|
+
return data[modelsDevId]
|
|
45
61
|
}
|
|
46
62
|
|
|
47
63
|
export type ModelOption = {
|
|
@@ -99,12 +115,17 @@ type ModelsDevProvider = {
|
|
|
99
115
|
export type FetchModelsResult = {
|
|
100
116
|
options: ModelOption[]
|
|
101
117
|
source: 'models.dev' | 'curated'
|
|
118
|
+
sources: {
|
|
119
|
+
modelsDev: 'live' | 'unavailable'
|
|
120
|
+
openGatewayCatalog: 'live' | 'unavailable'
|
|
121
|
+
openGatewayPrices: 'live' | 'unavailable'
|
|
122
|
+
}
|
|
123
|
+
warnings: string[]
|
|
102
124
|
warning?: string
|
|
103
125
|
}
|
|
104
126
|
|
|
105
|
-
// Pulls
|
|
106
|
-
// curated
|
|
107
|
-
// (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.
|
|
108
129
|
//
|
|
109
130
|
// Falls back to the curated list alone if the network is unreachable, the
|
|
110
131
|
// response is malformed, or any unexpected error fires — the wizard MUST
|
|
@@ -115,16 +136,58 @@ export async function fetchModelOptions(
|
|
|
115
136
|
): Promise<FetchModelsResult> {
|
|
116
137
|
const fetchImpl = options.fetchImpl ?? fetch
|
|
117
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> {
|
|
118
180
|
try {
|
|
119
181
|
const res = await fetchImpl(MODELS_DEV_URL, { signal: AbortSignal.timeout(timeoutMs) })
|
|
120
182
|
if (!res.ok) {
|
|
121
|
-
return {
|
|
183
|
+
return { status: 'unavailable', warning: `models.dev returned ${res.status}` }
|
|
122
184
|
}
|
|
123
|
-
const data =
|
|
124
|
-
return {
|
|
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> }
|
|
125
188
|
} catch (error) {
|
|
126
189
|
const reason = error instanceof Error ? error.message : String(error)
|
|
127
|
-
return {
|
|
190
|
+
return { status: 'unavailable', warning: reason }
|
|
128
191
|
}
|
|
129
192
|
}
|
|
130
193
|
|
|
@@ -156,7 +219,7 @@ function mergeWithCurated(data: Record<string, ModelsDevProvider>): ModelOption[
|
|
|
156
219
|
const seen = new Set<string>()
|
|
157
220
|
for (const providerId of Object.keys(KNOWN_PROVIDERS) as KnownProviderId[]) {
|
|
158
221
|
const known = KNOWN_PROVIDERS[providerId]
|
|
159
|
-
const upstream = data
|
|
222
|
+
const upstream = upstreamProviderFor(data, providerId)
|
|
160
223
|
const upstreamModels = upstream?.models ?? {}
|
|
161
224
|
for (const modelId of Object.keys(known.models)) {
|
|
162
225
|
const upstreamModel = upstreamModels[modelId]
|
|
@@ -167,9 +230,10 @@ function mergeWithCurated(data: Record<string, ModelsDevProvider>): ModelOption[
|
|
|
167
230
|
}
|
|
168
231
|
|
|
169
232
|
for (const providerId of Object.keys(KNOWN_PROVIDERS) as KnownProviderId[]) {
|
|
170
|
-
const upstream = data
|
|
233
|
+
const upstream = upstreamProviderFor(data, providerId)
|
|
171
234
|
const upstreamModels = upstream?.models ?? {}
|
|
172
235
|
for (const [fallbackModelId, upstreamModel] of Object.entries(upstreamModels)) {
|
|
236
|
+
if (!isRecord(upstreamModel)) continue
|
|
173
237
|
const modelId = upstreamModel.id ?? fallbackModelId
|
|
174
238
|
if (modelId.trim().length === 0) continue
|
|
175
239
|
const ref = `${providerId}/${modelId}`
|
|
@@ -181,6 +245,59 @@ function mergeWithCurated(data: Record<string, ModelsDevProvider>): ModelOption[
|
|
|
181
245
|
return out
|
|
182
246
|
}
|
|
183
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
|
+
|
|
184
301
|
type BuildOptionOpts = {
|
|
185
302
|
curated: boolean
|
|
186
303
|
upstream?: ModelsDevModel
|
|
@@ -236,3 +353,7 @@ function resolveCost(cost: ModelsDevModel['cost']): ModelOptionCost | null {
|
|
|
236
353
|
cacheWrite: cost.cacheWrite ?? cost.cache_write ?? 0,
|
|
237
354
|
}
|
|
238
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
|
+
}
|
|
@@ -1,7 +1,20 @@
|
|
|
1
1
|
import { effectiveBaseUrl } from '@/agent/model-overrides'
|
|
2
2
|
import { KNOWN_PROVIDERS, type KnownProviderId } from '@/config/providers'
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
// `successShape` is what a 200 must look like to count as a validated key.
|
|
5
|
+
// Default `models-list` requires `{ data: [...] }`, which every /v1/models probe
|
|
6
|
+
// below returns. `json-object` is the weaker check for probes that answer with a
|
|
7
|
+
// bare object instead of a list — it still rejects the captive-portal HTML case
|
|
8
|
+
// that motivated shape-checking in the first place.
|
|
9
|
+
type ProbeSuccessShape = 'models-list' | 'json-object'
|
|
10
|
+
|
|
11
|
+
type Probe = {
|
|
12
|
+
url: string
|
|
13
|
+
authHeader: 'bearer' | 'x-api-key'
|
|
14
|
+
successShape?: ProbeSuccessShape
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const PROVIDER_PROBE: Partial<Record<KnownProviderId, Probe>> = {
|
|
5
18
|
openai: { url: 'https://api.openai.com/v1/models', authHeader: 'bearer' },
|
|
6
19
|
anthropic: { url: 'https://api.anthropic.com/v1/models', authHeader: 'x-api-key' },
|
|
7
20
|
fireworks: { url: 'https://api.fireworks.ai/inference/v1/models', authHeader: 'bearer' },
|
|
@@ -13,6 +26,12 @@ const PROVIDER_PROBE: Partial<Record<KnownProviderId, { url: string; authHeader:
|
|
|
13
26
|
upstage: { url: 'https://api.upstage.ai/v1/models', authHeader: 'bearer' },
|
|
14
27
|
moonshot: { url: 'https://api.moonshot.ai/v1/models', authHeader: 'bearer' },
|
|
15
28
|
'moonshot-coding': { url: 'https://api.kimi.com/coding/v1/models', authHeader: 'bearer' },
|
|
29
|
+
// NOT /v1/models: OpenGateway serves its catalog PUBLICLY (verified — HTTP 200
|
|
30
|
+
// with `{data:[...]}` for a missing key and for a garbage key), so probing it
|
|
31
|
+
// would hand back `ok` for any string the user pastes. That is worse than no
|
|
32
|
+
// validation, because init would vouch for a key that cannot complete a single
|
|
33
|
+
// request. /v1/me is the cheapest endpoint that actually 401s on a bad key.
|
|
34
|
+
opengateway: { url: 'https://apis.opengateway.ai/v1/me', authHeader: 'bearer', successShape: 'json-object' },
|
|
16
35
|
}
|
|
17
36
|
|
|
18
37
|
// When a base-URL override (ANTHROPIC_BASE_URL / OPENAI_BASE_URL) points at a
|
|
@@ -75,7 +94,7 @@ export async function validateApiKey(
|
|
|
75
94
|
// Treat the response as "ok" only if it parses as the expected
|
|
76
95
|
// JSON shape (`{ data: [...] }` for /v1/models on every probed
|
|
77
96
|
// provider).
|
|
78
|
-
const shapeOk = await
|
|
97
|
+
const shapeOk = await hasExpectedShape(res, probe.successShape ?? 'models-list')
|
|
79
98
|
if (shapeOk) return { kind: 'ok' }
|
|
80
99
|
return { kind: 'skipped', reason: 'network-error', detail: 'unexpected response shape' }
|
|
81
100
|
}
|
|
@@ -125,12 +144,14 @@ function isFireworksFirePassForbidden(body: string): boolean {
|
|
|
125
144
|
}
|
|
126
145
|
}
|
|
127
146
|
|
|
128
|
-
async function
|
|
147
|
+
async function hasExpectedShape(res: Response, shape: ProbeSuccessShape): Promise<boolean> {
|
|
129
148
|
const text = await readCapped(res, MAX_BODY_BYTES)
|
|
130
149
|
if (text === null) return false
|
|
131
150
|
try {
|
|
132
151
|
const parsed = JSON.parse(text) as unknown
|
|
133
|
-
|
|
152
|
+
if (typeof parsed !== 'object' || parsed === null || Array.isArray(parsed)) return false
|
|
153
|
+
if (shape === 'json-object') return true
|
|
154
|
+
return Array.isArray((parsed as { data?: unknown }).data)
|
|
134
155
|
} catch {
|
|
135
156
|
return false
|
|
136
157
|
}
|
|
@@ -172,6 +193,7 @@ export const API_KEY_DASHBOARD_URL: Partial<Record<KnownProviderId, string>> = {
|
|
|
172
193
|
upstage: 'https://console.upstage.ai/api-keys',
|
|
173
194
|
moonshot: 'https://platform.moonshot.ai/console/api-keys',
|
|
174
195
|
'moonshot-coding': 'https://www.kimi.com/code/console',
|
|
196
|
+
opengateway: 'https://opengateway.ai/docs/reference/guides/authentication',
|
|
175
197
|
}
|
|
176
198
|
|
|
177
199
|
// MiniMax sells the same `minimax` provider under two billing surfaces that
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { isSecretShapedToken } from '@/bundled-plugins/memory/secret-detector'
|
|
2
|
+
|
|
3
|
+
export type OperationalIncidentFact =
|
|
4
|
+
| { kind: 'bash-command-not-found'; bin: string }
|
|
5
|
+
| { kind: 'declared-skill-bin-unresolved'; bin: string }
|
|
6
|
+
| { kind: 'sandbox-proc-unavailable' }
|
|
7
|
+
|
|
8
|
+
export type OperationalIncidentResolutionSignal = 'successful-bin' | 'successful-sandbox-real-proc'
|
|
9
|
+
|
|
10
|
+
export type OperationalIncidentSuccessInput = {
|
|
11
|
+
tool: string
|
|
12
|
+
args: Record<string, unknown>
|
|
13
|
+
sandboxedRealProcSucceeded: boolean
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export const OPERATIONAL_INCIDENT_RESOLUTION_SIGNAL_BY_KIND = {
|
|
17
|
+
'bash-command-not-found': 'successful-bin',
|
|
18
|
+
'declared-skill-bin-unresolved': 'successful-bin',
|
|
19
|
+
'sandbox-proc-unavailable': 'successful-sandbox-real-proc',
|
|
20
|
+
} as const satisfies Record<OperationalIncidentFact['kind'], OperationalIncidentResolutionSignal>
|
|
21
|
+
|
|
22
|
+
export class DeclaredSkillBinUnresolvedError extends Error {
|
|
23
|
+
override readonly name = 'DeclaredSkillBinUnresolvedError'
|
|
24
|
+
|
|
25
|
+
constructor(readonly bin: string) {
|
|
26
|
+
super(`declared skill bin could not be resolved: ${bin}`)
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const SAFE_BIN = '[A-Za-z0-9][A-Za-z0-9._+-]{0,127}'
|
|
31
|
+
const COMMAND_NOT_FOUND = new RegExp(
|
|
32
|
+
`^(?:/bin/bash: |bash: line [1-9][0-9]{0,5}: )(${SAFE_BIN}): command not found\\n(?:\\n){1,2}Command exited with code 127$`,
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
export function classifyToolOutcome(input: { tool: string; error: unknown }): OperationalIncidentFact | null {
|
|
36
|
+
if (input.error instanceof DeclaredSkillBinUnresolvedError && isSafeBin(input.error.bin)) {
|
|
37
|
+
return { kind: 'declared-skill-bin-unresolved', bin: input.error.bin }
|
|
38
|
+
}
|
|
39
|
+
if (input.tool !== 'bash' || !(input.error instanceof Error)) return null
|
|
40
|
+
if (input.error.name === 'SandboxDegradedProcError') return { kind: 'sandbox-proc-unavailable' }
|
|
41
|
+
|
|
42
|
+
const match = COMMAND_NOT_FOUND.exec(input.error.message)
|
|
43
|
+
const bin = match?.[1]
|
|
44
|
+
return bin === undefined ? null : bashCommandNotFoundFact(bin)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function deriveOperationalIncidentFactsForSuccess(
|
|
48
|
+
input: OperationalIncidentSuccessInput,
|
|
49
|
+
): OperationalIncidentFact[] {
|
|
50
|
+
if (input.tool !== 'bash') return []
|
|
51
|
+
const command = input.args.command
|
|
52
|
+
if (typeof command !== 'string') return []
|
|
53
|
+
// False resolution hides a live defect; under-resolution only leaves a visible stale warning.
|
|
54
|
+
// Accept only syntax where aggregate exit zero proves this exact invocation succeeded.
|
|
55
|
+
const commandWord = parseSingleUnconditionalInvocation(command)
|
|
56
|
+
if (commandWord === null) return []
|
|
57
|
+
|
|
58
|
+
const facts = successfulBinFacts(commandWord)
|
|
59
|
+
if (input.sandboxedRealProcSucceeded) facts.push({ kind: 'sandbox-proc-unavailable' })
|
|
60
|
+
return facts
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function bashCommandNotFoundFact(bin: string): OperationalIncidentFact | null {
|
|
64
|
+
return isSafeBin(bin) ? { kind: 'bash-command-not-found', bin } : null
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function successfulBinFacts(bin: string): OperationalIncidentFact[] {
|
|
68
|
+
if (!isSafeBin(bin)) return []
|
|
69
|
+
return [
|
|
70
|
+
{ kind: 'bash-command-not-found', bin },
|
|
71
|
+
{ kind: 'declared-skill-bin-unresolved', bin },
|
|
72
|
+
]
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function isSafeBin(bin: string): boolean {
|
|
76
|
+
return new RegExp(`^${SAFE_BIN}$`).test(bin) && !isSecretShapedToken(bin)
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const SHELL_CONSTRUCTS = new Set([
|
|
80
|
+
'!',
|
|
81
|
+
'case',
|
|
82
|
+
'coproc',
|
|
83
|
+
'do',
|
|
84
|
+
'done',
|
|
85
|
+
'elif',
|
|
86
|
+
'else',
|
|
87
|
+
'esac',
|
|
88
|
+
'fi',
|
|
89
|
+
'for',
|
|
90
|
+
'function',
|
|
91
|
+
'if',
|
|
92
|
+
'in',
|
|
93
|
+
'select',
|
|
94
|
+
'then',
|
|
95
|
+
'time',
|
|
96
|
+
'until',
|
|
97
|
+
'while',
|
|
98
|
+
])
|
|
99
|
+
|
|
100
|
+
function parseSingleUnconditionalInvocation(command: string): string | null {
|
|
101
|
+
const words: string[] = []
|
|
102
|
+
let word = ''
|
|
103
|
+
let quote: '"' | "'" | null = null
|
|
104
|
+
let active = false
|
|
105
|
+
const endWord = (): void => {
|
|
106
|
+
if (!active) return
|
|
107
|
+
words.push(word)
|
|
108
|
+
word = ''
|
|
109
|
+
active = false
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
for (let i = 0; i < command.length; i++) {
|
|
113
|
+
const character = command[i]!
|
|
114
|
+
if (quote !== null) {
|
|
115
|
+
if (character === quote) {
|
|
116
|
+
quote = null
|
|
117
|
+
} else {
|
|
118
|
+
if (quote === '"' && (character === '`' || (character === '$' && command[i + 1] === '('))) return null
|
|
119
|
+
if (character === '\\' && quote === '"' && i + 1 < command.length) word += command[++i]
|
|
120
|
+
else word += character
|
|
121
|
+
}
|
|
122
|
+
active = true
|
|
123
|
+
continue
|
|
124
|
+
}
|
|
125
|
+
if (character === '"' || character === "'") {
|
|
126
|
+
quote = character
|
|
127
|
+
active = true
|
|
128
|
+
continue
|
|
129
|
+
}
|
|
130
|
+
if (character === '\\') {
|
|
131
|
+
if (i + 1 >= command.length) return null
|
|
132
|
+
word += command[++i]
|
|
133
|
+
active = true
|
|
134
|
+
continue
|
|
135
|
+
}
|
|
136
|
+
if (';&|<>\n\r()'.includes(character) || character === '`' || (character === '$' && command[i + 1] === '('))
|
|
137
|
+
return null
|
|
138
|
+
if (character === '#' && !active) return null
|
|
139
|
+
if (character === ' ' || character === '\t') {
|
|
140
|
+
endWord()
|
|
141
|
+
continue
|
|
142
|
+
}
|
|
143
|
+
word += character
|
|
144
|
+
active = true
|
|
145
|
+
}
|
|
146
|
+
if (quote !== null) return null
|
|
147
|
+
endWord()
|
|
148
|
+
const commandWord = words.find((candidate) => !/^[A-Za-z_][A-Za-z0-9_]*=/.test(candidate))
|
|
149
|
+
if (commandWord === undefined || SHELL_CONSTRUCTS.has(commandWord)) return null
|
|
150
|
+
return commandWord
|
|
151
|
+
}
|