@capybara-hq/kernel 0.1.0-rc.5 → 0.1.0-rc.6
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/dist/admin/admin.controller.d.ts +13 -2
- package/dist/admin/admin.controller.js +29 -4
- package/dist/agents/agents.service.d.ts +3 -1
- package/dist/agents/agents.service.js +8 -6
- package/dist/agents/from-template.service.js +3 -3
- package/dist/agents/models.controller.d.ts +34 -5
- package/dist/agents/models.controller.js +50 -6
- package/dist/agents/readiness.controller.d.ts +5 -5
- package/dist/agents/readiness.controller.js +6 -6
- package/dist/catalog/marketplaces.service.d.ts +1 -1
- package/dist/config/schema.d.ts +4 -0
- package/dist/config/schema.js +24 -0
- package/dist/conversations/conversations.service.d.ts +4 -3
- package/dist/conversations/conversations.service.js +9 -8
- package/dist/ctx/create-ctx.d.ts +15 -1
- package/dist/ctx/create-ctx.js +23 -2
- package/dist/db/kernel-schema.d.ts +24 -0
- package/dist/errors.d.ts +3 -0
- package/dist/errors.js +3 -0
- package/dist/evals/run-replay.js +4 -2
- package/dist/evals/runs.service.d.ts +2 -1
- package/dist/evals/runs.service.js +13 -12
- package/dist/evals/step-replay.d.ts +2 -1
- package/dist/evals/step-replay.js +3 -2
- package/dist/extensions/config-hash.d.ts +10 -0
- package/dist/extensions/config-hash.js +38 -0
- package/dist/extensions/extensions.module.js +6 -12
- package/dist/extensions/host.d.ts +6 -10
- package/dist/extensions/host.js +22 -50
- package/dist/extensions/state.module.d.ts +9 -0
- package/dist/extensions/state.module.js +38 -0
- package/dist/http/api.module.js +2 -0
- package/dist/http/plugin-dispatch.d.ts +4 -1
- package/dist/http/plugin-dispatch.js +14 -2
- package/dist/infra/config/kernel-config.service.js +3 -0
- package/dist/infra/events/kernel-events.d.ts +8 -0
- package/dist/models/embeddings.d.ts +82 -0
- package/dist/models/embeddings.js +326 -0
- package/dist/models/enablement.controller.d.ts +40 -0
- package/dist/models/enablement.controller.js +74 -0
- package/dist/models/enablement.d.ts +99 -0
- package/dist/models/enablement.js +263 -0
- package/dist/models/models.module.d.ts +5 -1
- package/dist/models/models.module.js +16 -4
- package/dist/models/purposes.d.ts +18 -4
- package/dist/models/purposes.js +46 -9
- package/dist/platform/exclusions.js +18 -0
- package/dist/runtime/hook-calls.d.ts +8 -1
- package/dist/runtime/hook-calls.js +40 -9
- package/dist/runtime/model-call.d.ts +8 -3
- package/dist/runtime/model-call.js +28 -5
- package/dist/runtime/providers.d.ts +146 -32
- package/dist/runtime/providers.js +321 -67
- package/dist/runtime/run-worker.d.ts +1 -1
- package/dist/runtime/run-worker.js +4 -4
- package/dist/runtime/subagents/definition.js +3 -3
- package/dist/runtime/usage-report-jobs.d.ts +88 -0
- package/dist/runtime/usage-report-jobs.js +158 -0
- package/dist/runtime/usage-report.d.ts +33 -38
- package/dist/runtime/usage-report.js +152 -84
- package/dist/tools/pipeline.js +6 -0
- package/package.json +3 -3
- package/src/migrations/0089_model_kinds_usage.sql +38 -0
|
@@ -7,38 +7,95 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
|
|
|
7
7
|
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
8
8
|
return function (target, key) { decorator(target, key, paramIndex); }
|
|
9
9
|
};
|
|
10
|
+
import { createHash } from "node:crypto";
|
|
10
11
|
import { assertValidDeclarations, EFFORT_VALUES, parseExtensionPoint, } from "@capybara-hq/plugin-sdk";
|
|
11
12
|
import { Inject, Injectable, Module, Optional } from "@nestjs/common";
|
|
12
13
|
import { ApiError } from "../errors.js";
|
|
13
|
-
import {
|
|
14
|
+
import { deploymentConfigHash } from "../extensions/config-hash.js";
|
|
15
|
+
import { EffectiveConfigs } from "../extensions/effective-config.js";
|
|
16
|
+
import { missingOverridable } from "../extensions/org-levels.js";
|
|
17
|
+
import { ExtensionRegistry } from "../extensions/registry.js";
|
|
18
|
+
import { ExtensionStateModule } from "../extensions/state.module.js";
|
|
14
19
|
import { CacheService } from "../infra/cache/cache.service.js";
|
|
15
20
|
import { Cacheable } from "../infra/cache/cacheable.decorator.js";
|
|
21
|
+
import { KernelConfigService } from "../infra/config/kernel-config.service.js";
|
|
16
22
|
import { KERNEL_LOGGER, REDIS } from "../infra/tokens.js";
|
|
23
|
+
import { ModelEnablement, ModelEnablementModule, modelKey } from "../models/enablement.js";
|
|
17
24
|
/** 供应商模型列表在 Redis 中的缓存时长(秒)。 */
|
|
18
25
|
const MODELS_TTL_SECONDS = 600;
|
|
19
|
-
/**
|
|
20
|
-
|
|
21
|
-
|
|
26
|
+
/**
|
|
27
|
+
* Optional provider: the `listModels()` time limit (ms) instead of the config's
|
|
28
|
+
* `models.listTimeoutMs` (tests).
|
|
29
|
+
*/
|
|
22
30
|
export const LIST_MODELS_TIMEOUT = Symbol("LIST_MODELS_TIMEOUT");
|
|
23
31
|
/**
|
|
24
32
|
* 列表视图(后台模型页、组织模型页)里列模型失败的供应商在这么多秒内直接标 error、不再请求(Redis,多副本共享)。
|
|
25
33
|
* 只用于列表;`resolve`(运行时)照常每次请求,失败由降级梯子重试。
|
|
26
34
|
*/
|
|
27
35
|
export const LIST_FAILURE_TTL_SECONDS = 15;
|
|
36
|
+
/**
|
|
37
|
+
* How long the "model dropped from the list" warn is not repeated for the same (provider, model,
|
|
38
|
+
* config digest): the list is re-read every {@link MODELS_TTL_SECONDS}, the warn is not.
|
|
39
|
+
*/
|
|
40
|
+
const DROPPED_WARN_TTL_SECONDS = 24 * 3600;
|
|
28
41
|
/**
|
|
29
42
|
* The model list's cache key (`@Cacheable` stores it as `cache:<key>`). It carries the provider's
|
|
30
43
|
* contract major: after a plugin moves from `@1` to `@2` it never reads the list written through
|
|
31
44
|
* the `@1` adapter (both are `@2`-shaped, the shape cannot tell them apart). It also carries the
|
|
32
|
-
* plugin version and the
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
* hash, never config text (no secret in the
|
|
36
|
-
* version / digest (test doubles) use `-`.
|
|
45
|
+
* plugin version and the digest of the effective config the list was read with (see
|
|
46
|
+
* {@link ProviderRegistry}): after an upgrade (the parameter declarations may have changed) or a
|
|
47
|
+
* config change (models added or removed) every replica validates the new list at once instead of
|
|
48
|
+
* waiting for the old entry's TTL. The digest is only a hash, never config text (no secret in the
|
|
49
|
+
* key). Old keys expire by TTL. Entries without a version / digest (test doubles) use `-`.
|
|
37
50
|
*/
|
|
38
|
-
const modelsKey = (p) => `capk:models:${p.plugin}:v${p.major}:${p.version ?? "-"}:${
|
|
51
|
+
const modelsKey = (p, hash) => `capk:models:${p.plugin}:v${p.major}:${p.version ?? "-"}:${hash ?? "-"}`;
|
|
39
52
|
/** 列表视图的失败标记({@link LIST_FAILURE_TTL_SECONDS}),与模型列表同样按版本与配置摘要区分。 */
|
|
40
|
-
const listFailedKey = (p) => `capk:models-failed:${p.plugin}:v${p.major}:${p.version ?? "-"}:${
|
|
53
|
+
const listFailedKey = (p, hash) => `capk:models-failed:${p.plugin}:v${p.major}:${p.version ?? "-"}:${hash ?? "-"}`;
|
|
54
|
+
const droppedKey = (plugin, hash, model) => `capk:models-dropped:${plugin}:${hash ?? "-"}:${model}`;
|
|
55
|
+
/**
|
|
56
|
+
* The digest of an org's own copy of the config. `deploymentConfigHash` masks secrets, and a copy
|
|
57
|
+
* usually differs from the platform only by its key, so the org id goes into the digest: a copy
|
|
58
|
+
* never shares the platform's list (or another org's), whose key may see other models.
|
|
59
|
+
*/
|
|
60
|
+
const copyHash = (orgId, schema, config) => createHash("sha256")
|
|
61
|
+
.update(`${orgId}\0${deploymentConfigHash(schema, config)}`)
|
|
62
|
+
.digest("hex")
|
|
63
|
+
.slice(0, 16);
|
|
41
64
|
const CAPABILITY = "model-provider";
|
|
65
|
+
/** The model kinds this kernel knows; another kind (a newer plugin) is listed as `other`. */
|
|
66
|
+
const KNOWN_KINDS = new Set([
|
|
67
|
+
"chat",
|
|
68
|
+
"embedding",
|
|
69
|
+
"rerank",
|
|
70
|
+
"image_generation",
|
|
71
|
+
"video_generation",
|
|
72
|
+
"speech_to_text",
|
|
73
|
+
"text_to_speech",
|
|
74
|
+
]);
|
|
75
|
+
function bindProvider(impl, call) {
|
|
76
|
+
const bound = {
|
|
77
|
+
stream: (req, opts) => impl.stream(req, { signal: opts.signal, ...call }),
|
|
78
|
+
};
|
|
79
|
+
if (typeof impl.embed === "function") {
|
|
80
|
+
bound.embed = (req, opts) => impl.embed(req, { signal: opts.signal, ...call });
|
|
81
|
+
}
|
|
82
|
+
return bound;
|
|
83
|
+
}
|
|
84
|
+
const positiveInt = (n) => typeof n === "number" && Number.isInteger(n) && n > 0;
|
|
85
|
+
/**
|
|
86
|
+
* Whether an embedding model's info is usable: at least one dimension, every dimension and the
|
|
87
|
+
* limits positive integers, and `defaultDimensions` one of `dimensions`. A model failing this is
|
|
88
|
+
* dropped from the list (every default call on it would fail validation).
|
|
89
|
+
*/
|
|
90
|
+
function isValidEmbeddingInfo(e) {
|
|
91
|
+
return (Array.isArray(e.dimensions) &&
|
|
92
|
+
e.dimensions.length > 0 &&
|
|
93
|
+
e.dimensions.every(positiveInt) &&
|
|
94
|
+
positiveInt(e.defaultDimensions) &&
|
|
95
|
+
e.dimensions.includes(e.defaultDimensions) &&
|
|
96
|
+
positiveInt(e.maxInputTokens) &&
|
|
97
|
+
positiveInt(e.maxBatch));
|
|
98
|
+
}
|
|
42
99
|
/**
|
|
43
100
|
* 供应商的 `listModels()` 失败(上游不可用等)。仍是 `agent.model_unavailable`,HTTP 调用方照常
|
|
44
101
|
* 得到该错误码;运行时(`runModelStep`)据此类型把它当作可重试的失败交给降级梯子,
|
|
@@ -66,14 +123,16 @@ const V1_EFFORT_KNOB = {
|
|
|
66
123
|
/**
|
|
67
124
|
* 把 `model-provider@1` 实现包成 `@2`:
|
|
68
125
|
* - `listModels`:`parameters` 由 `capabilities.effort` 推导(为真时只有 effort 旋钮,否则为空);
|
|
126
|
+
* an `@1` model is always a chat model: `kind` and `embedding` are dropped (an `@1` provider has
|
|
127
|
+
* no `embed`); the per-call options are not passed (an `@1` provider reads `ctx.config`);
|
|
69
128
|
* - `stream`:`params.effort` 映射为 `req.effort`,其余参数丢弃(`@1` 没有对应字段);
|
|
70
|
-
* 没有 `params.effort` 时保留请求自带的 `effort
|
|
129
|
+
* 没有 `params.effort` 时保留请求自带的 `effort`;only the signal is passed.
|
|
71
130
|
*/
|
|
72
131
|
export function adaptModelProviderV1(impl) {
|
|
73
132
|
return {
|
|
74
133
|
async listModels() {
|
|
75
134
|
const models = await impl.listModels();
|
|
76
|
-
return models.map((m) => ({
|
|
135
|
+
return models.map(({ kind: _kind, embedding: _embedding, ...m }) => ({
|
|
77
136
|
...m,
|
|
78
137
|
parameters: m.capabilities.effort ? [structuredClone(V1_EFFORT_KNOB)] : [],
|
|
79
138
|
}));
|
|
@@ -86,43 +145,61 @@ export function adaptModelProviderV1(impl) {
|
|
|
86
145
|
if (typeof effort === "string" && EFFORT_VALUES.includes(effort)) {
|
|
87
146
|
out.effort = effort;
|
|
88
147
|
}
|
|
89
|
-
return impl.stream(out, opts);
|
|
148
|
+
return impl.stream(out, { signal: opts.signal });
|
|
90
149
|
},
|
|
91
150
|
};
|
|
92
151
|
}
|
|
93
152
|
/**
|
|
94
153
|
* 模型供应商注册表:供应商即提供 `model-provider` 能力的插件(以插件名为供应商名)。
|
|
95
154
|
* 同时支持 `model-provider@1`(经 {@link adaptModelProviderV1} 适配)与 `@2`;
|
|
96
|
-
* 同一插件两者都提供时用 `@2
|
|
97
|
-
*
|
|
98
|
-
*
|
|
155
|
+
* 同一插件两者都提供时用 `@2`。
|
|
156
|
+
*
|
|
157
|
+
* Org-bound (spec B §5.5, A §6): every list and call is made for an org with the provider's
|
|
158
|
+
* effective config there (`EffectiveConfigs.get`), passed as `{ orgId, config }`; a provider whose
|
|
159
|
+
* effective config is not valid in the org offers no models there, and neither does one the org
|
|
160
|
+
* explicitly turned off (its own extension switch row off, spec A §8; a provider with no switch row
|
|
161
|
+
* stays usable). The admin view (no org) lists with the platform config.
|
|
162
|
+
*
|
|
163
|
+
* Each list is validated per model (spec B §5.1): kinds are normalised (absent = `chat`, unknown =
|
|
164
|
+
* `other`); a chat model failing `assertValidDeclarations`, or an `embedding` model without
|
|
165
|
+
* `embedding` info or from a provider without `embed`, is dropped with a warn, the rest of the
|
|
166
|
+
* provider stays usable. Only `chat` and `embedding` models resolve.
|
|
167
|
+
*
|
|
168
|
+
* Each list (`@2`-shaped) is cached in Redis (`@Cacheable`, key
|
|
99
169
|
* `cache:capk:models:<provider>:v<major>:<plugin version>:<config digest>`, 600 s), never in
|
|
100
|
-
* process memory: every replica shares it.
|
|
170
|
+
* process memory: every replica shares it. The digest is the platform config's for every org that
|
|
171
|
+
* follows the platform (one shared list), and a per-org digest for an org's own copy.
|
|
101
172
|
*/
|
|
102
173
|
let ProviderRegistry = class ProviderRegistry {
|
|
103
174
|
host;
|
|
175
|
+
effective;
|
|
104
176
|
cacheService;
|
|
105
177
|
kv;
|
|
178
|
+
config;
|
|
179
|
+
enablement;
|
|
106
180
|
log;
|
|
107
|
-
|
|
108
|
-
constructor(host, cacheService, kv, log,
|
|
109
|
-
/**
|
|
110
|
-
|
|
181
|
+
listTimeoutOverride;
|
|
182
|
+
constructor(host, effective, cacheService, kv, config, enablement, log,
|
|
183
|
+
/** Overrides `models.listTimeoutMs` (tests). */
|
|
184
|
+
listTimeoutOverride) {
|
|
111
185
|
this.host = host;
|
|
186
|
+
this.effective = effective;
|
|
112
187
|
this.cacheService = cacheService;
|
|
113
188
|
this.kv = kv;
|
|
189
|
+
this.config = config;
|
|
190
|
+
this.enablement = enablement;
|
|
114
191
|
this.log = log;
|
|
115
|
-
this.
|
|
192
|
+
this.listTimeoutOverride = listTimeoutOverride;
|
|
116
193
|
}
|
|
117
194
|
/** `listModels()` 加时限(到时按失败处理,供应商的调用留在后台、结果丢弃)。 */
|
|
118
|
-
async listModelsWithTimeout(impl) {
|
|
119
|
-
const ms = this.listTimeoutMs;
|
|
195
|
+
async listModelsWithTimeout(impl, call) {
|
|
196
|
+
const ms = this.listTimeoutOverride ?? this.config.get().models.listTimeoutMs;
|
|
120
197
|
let timer;
|
|
121
|
-
const call = Promise.resolve().then(() => impl.listModels());
|
|
122
|
-
call
|
|
198
|
+
const call$ = Promise.resolve().then(() => impl.listModels(call));
|
|
199
|
+
call$.catch(() => { });
|
|
123
200
|
try {
|
|
124
201
|
return await Promise.race([
|
|
125
|
-
call
|
|
202
|
+
call$,
|
|
126
203
|
new Promise((_, reject) => {
|
|
127
204
|
timer = setTimeout(() => reject(new Error(`listModels timed out after ${ms}ms`)), ms);
|
|
128
205
|
}),
|
|
@@ -134,11 +211,17 @@ let ProviderRegistry = class ProviderRegistry {
|
|
|
134
211
|
}
|
|
135
212
|
/**
|
|
136
213
|
* Whether at least one model-provider extension is loaded on this replica (enabled, configured,
|
|
137
|
-
* set up)
|
|
138
|
-
* is down still counts (org
|
|
214
|
+
* set up) with a valid effective config in `orgId`. Read from extension state and the org's
|
|
215
|
+
* config only: never calls a vendor, so a provider whose vendor is down still counts (org
|
|
216
|
+
* readiness, web-order spec §3.8).
|
|
139
217
|
*/
|
|
140
|
-
hasProvider() {
|
|
141
|
-
|
|
218
|
+
async hasProvider(orgId) {
|
|
219
|
+
const off = await this.enablement.switchedOff(orgId);
|
|
220
|
+
for (const p of this.providers()) {
|
|
221
|
+
if ((await this.target(p, orgId, off)).ok)
|
|
222
|
+
return true;
|
|
223
|
+
}
|
|
224
|
+
return false;
|
|
142
225
|
}
|
|
143
226
|
/** 每个插件取一个实现(`@2` 优先),按插件首次出现的顺序;不支持的主版本记 warn 后忽略。 */
|
|
144
227
|
providers() {
|
|
@@ -170,53 +253,174 @@ let ProviderRegistry = class ProviderRegistry {
|
|
|
170
253
|
impl: major === 2 ? impl : adaptModelProviderV1(impl),
|
|
171
254
|
}));
|
|
172
255
|
}
|
|
173
|
-
|
|
174
|
-
|
|
256
|
+
/**
|
|
257
|
+
* The call options and list digest of `p` for `orgId`, or for the platform when `orgId` is
|
|
258
|
+
* undefined (the admin view). An org: its effective config (not valid -> `needs_config`); the
|
|
259
|
+
* platform's digest while it follows the platform, a per-org one for its copy. The platform: its
|
|
260
|
+
* own config (a required overridable field left empty -> `org_configured`). `off`: the
|
|
261
|
+
* extensions the org explicitly turned off (`ModelEnablement.switchedOff`) -> `switched_off`.
|
|
262
|
+
*/
|
|
263
|
+
async target(p, orgId, off = new Set()) {
|
|
264
|
+
const inst = this.host.current?.(p.plugin);
|
|
265
|
+
const schema = inst?.manifest.config?.deployment;
|
|
266
|
+
if (orgId === undefined) {
|
|
267
|
+
if (schema && missingOverridable(schema, inst?.config).length > 0) {
|
|
268
|
+
return { ok: false, reason: "org_configured" };
|
|
269
|
+
}
|
|
270
|
+
return { ok: true, call: { config: inst?.config }, hash: p.configHash };
|
|
271
|
+
}
|
|
272
|
+
if (off.has(p.plugin))
|
|
273
|
+
return { ok: false, reason: "switched_off" };
|
|
274
|
+
const eff = await this.effective.get(orgId, p.plugin);
|
|
275
|
+
if (!eff.ok)
|
|
276
|
+
return { ok: false, reason: "needs_config" };
|
|
277
|
+
// an `@1` provider ignores the per-call options (it reads `ctx.config`): the org's own copy
|
|
278
|
+
// could not be honoured, its calls would go out with the platform's config
|
|
279
|
+
if (eff.source === "copy" && p.major === 1)
|
|
280
|
+
return { ok: false, reason: "needs_config" };
|
|
281
|
+
return {
|
|
282
|
+
ok: true,
|
|
283
|
+
call: { orgId, config: eff.config },
|
|
284
|
+
hash: eff.source === "platform" ? p.configHash : copyHash(orgId, schema, eff.config),
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
models(p, t) {
|
|
288
|
+
return this.validatedModels(modelsKey(p, t.hash), p, t.call, t.hash);
|
|
175
289
|
}
|
|
176
290
|
/** The provider's validated model list, cached under `key` (see {@link modelsKey}). */
|
|
177
|
-
async validatedModels(_key, p) {
|
|
178
|
-
const { plugin: provider,
|
|
291
|
+
async validatedModels(_key, p, call, hash) {
|
|
292
|
+
const { plugin: provider, impl } = p;
|
|
179
293
|
let models;
|
|
180
294
|
try {
|
|
181
|
-
models = await this.listModelsWithTimeout(impl);
|
|
295
|
+
models = await this.listModelsWithTimeout(impl, call);
|
|
182
296
|
}
|
|
183
297
|
catch (err) {
|
|
184
298
|
throw new ProviderListError(provider, err);
|
|
185
299
|
}
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
300
|
+
const out = [];
|
|
301
|
+
for (const m of models) {
|
|
302
|
+
const kind = m.kind === undefined ? "chat" : KNOWN_KINDS.has(m.kind) ? m.kind : "other";
|
|
303
|
+
let problem;
|
|
304
|
+
let cause;
|
|
305
|
+
if (kind === "chat") {
|
|
306
|
+
try {
|
|
189
307
|
assertValidDeclarations(provider, m.parameters);
|
|
308
|
+
}
|
|
309
|
+
catch (err) {
|
|
310
|
+
problem = "invalid parameter declarations";
|
|
311
|
+
cause = err;
|
|
312
|
+
}
|
|
190
313
|
}
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
314
|
+
else if (kind === "embedding") {
|
|
315
|
+
if (!m.embedding)
|
|
316
|
+
problem = "an embedding model without embedding info";
|
|
317
|
+
else if (!isValidEmbeddingInfo(m.embedding))
|
|
318
|
+
problem = "malformed embedding info";
|
|
319
|
+
else if (typeof impl.embed !== "function") {
|
|
320
|
+
problem = "an embedding model from a provider without embed";
|
|
321
|
+
}
|
|
195
322
|
}
|
|
323
|
+
if (problem !== undefined) {
|
|
324
|
+
await this.warnDropped(provider, m.id, hash, problem, cause);
|
|
325
|
+
continue;
|
|
326
|
+
}
|
|
327
|
+
out.push({ ...m, kind });
|
|
196
328
|
}
|
|
197
|
-
return
|
|
329
|
+
return out;
|
|
330
|
+
}
|
|
331
|
+
/** One warn per (provider, model, config digest) within {@link DROPPED_WARN_TTL_SECONDS}. */
|
|
332
|
+
async warnDropped(provider, model, hash, problem, cause) {
|
|
333
|
+
const key = droppedKey(provider, hash, model);
|
|
334
|
+
if ((await this.kv.get(key)) !== null)
|
|
335
|
+
return;
|
|
336
|
+
await this.kv.set(key, "1", "EX", DROPPED_WARN_TTL_SECONDS);
|
|
337
|
+
this.log?.warn("model dropped from the provider's list", {
|
|
338
|
+
provider,
|
|
339
|
+
model,
|
|
340
|
+
problem,
|
|
341
|
+
...(cause !== undefined && { err: cause }),
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
/**
|
|
345
|
+
* 每个供应商及其模型(按插件加载顺序),for `orgId`:列模型失败或在该组织需要配置的供应商跳过。Only
|
|
346
|
+
* enabled models (neither the platform nor the org disabled them, spec B §5.3), optionally of
|
|
347
|
+
* one `kind`.
|
|
348
|
+
*/
|
|
349
|
+
async list(orgId, opts = {}) {
|
|
350
|
+
return (await this.listWithStatus(orgId)).flatMap((p) => p.status === "ok"
|
|
351
|
+
? [
|
|
352
|
+
{
|
|
353
|
+
provider: p.provider,
|
|
354
|
+
models: opts.kind === undefined ? p.models : p.models.filter((m) => m.kind === opts.kind),
|
|
355
|
+
},
|
|
356
|
+
]
|
|
357
|
+
: []);
|
|
198
358
|
}
|
|
199
|
-
/**
|
|
200
|
-
|
|
201
|
-
|
|
359
|
+
/**
|
|
360
|
+
* An org's catalog (the org settings page, spec B §5.3): every provider as in
|
|
361
|
+
* {@link listWithStatus} for the org, with every model the platform left on, each carrying
|
|
362
|
+
* `disabled` for the org's own list. Reads the same cached lists (no extra provider call).
|
|
363
|
+
*/
|
|
364
|
+
async catalog(orgId) {
|
|
365
|
+
const [statuses, platform, org] = await Promise.all([
|
|
366
|
+
this.statuses(orgId),
|
|
367
|
+
this.enablement.disabled(null),
|
|
368
|
+
this.enablement.disabled(orgId),
|
|
369
|
+
]);
|
|
370
|
+
return statuses.map((p) => ({
|
|
371
|
+
...p,
|
|
372
|
+
models: p.models
|
|
373
|
+
.filter((m) => !platform.has(modelKey(p.provider, m.id)))
|
|
374
|
+
.map((m) => ({ ...m, disabled: org.has(modelKey(p.provider, m.id)) })),
|
|
375
|
+
}));
|
|
202
376
|
}
|
|
203
377
|
/**
|
|
204
|
-
*
|
|
205
|
-
*
|
|
378
|
+
* 每个已安装供应商及其状态(按插件加载顺序),for `orgId` (enabled models only), or the platform
|
|
379
|
+
* view when undefined (`/admin/models`: every model, `disabled` from the platform's list only).
|
|
380
|
+
* See {@link statuses} for the statuses.
|
|
206
381
|
*/
|
|
207
|
-
async listWithStatus() {
|
|
382
|
+
async listWithStatus(orgId) {
|
|
383
|
+
const [statuses, platform, org] = await Promise.all([
|
|
384
|
+
this.statuses(orgId),
|
|
385
|
+
this.enablement.disabled(null),
|
|
386
|
+
orgId === undefined ? Promise.resolve(new Set()) : this.enablement.disabled(orgId),
|
|
387
|
+
]);
|
|
388
|
+
return statuses.map((p) => ({
|
|
389
|
+
...p,
|
|
390
|
+
models: orgId === undefined
|
|
391
|
+
? p.models.map((m) => ({ ...m, disabled: platform.has(modelKey(p.provider, m.id)) }))
|
|
392
|
+
: p.models.filter((m) => {
|
|
393
|
+
const key = modelKey(p.provider, m.id);
|
|
394
|
+
return !platform.has(key) && !org.has(key);
|
|
395
|
+
}),
|
|
396
|
+
}));
|
|
397
|
+
}
|
|
398
|
+
/**
|
|
399
|
+
* 每个已安装供应商的完整列表与状态,for `orgId` or the platform: 列模型失败(含超过
|
|
400
|
+
* `models.listTimeoutMs`)的标 `error`、`models` 为空,不影响其他供应商;列模型失败的在
|
|
401
|
+
* {@link LIST_FAILURE_TTL_SECONDS} 秒内不再请求。An org where the provider needs configuration gets
|
|
402
|
+
* `error`; the platform view of a provider whose platform config leaves a required overridable
|
|
403
|
+
* field empty gets `org_configured`. `error` 只是内核自己的英文说明(不透出上游错误原文,原文记 warn
|
|
404
|
+
* 日志)。No disabled list applied.
|
|
405
|
+
*/
|
|
406
|
+
async statuses(orgId) {
|
|
208
407
|
const providers = this.providers();
|
|
408
|
+
const off = orgId === undefined ? new Set() : await this.enablement.switchedOff(orgId);
|
|
209
409
|
const settled = await Promise.allSettled(providers.map(async (p) => {
|
|
410
|
+
const t = await this.target(p, orgId, off);
|
|
411
|
+
if (!t.ok)
|
|
412
|
+
return t.reason;
|
|
413
|
+
const failed = listFailedKey(p, t.hash);
|
|
210
414
|
// 刚失败过的供应商:短时间内直接标 error,不再等它(每个供应商另有时限)
|
|
211
|
-
if ((await this.kv.get(
|
|
415
|
+
if ((await this.kv.get(failed)) !== null) {
|
|
212
416
|
throw new ProviderListError(p.plugin, new Error("listModels failed recently"));
|
|
213
417
|
}
|
|
214
418
|
try {
|
|
215
|
-
return await this.models(p);
|
|
419
|
+
return await this.models(p, t);
|
|
216
420
|
}
|
|
217
421
|
catch (err) {
|
|
218
422
|
if (err instanceof ProviderListError) {
|
|
219
|
-
await this.kv.set(
|
|
423
|
+
await this.kv.set(failed, "1", "EX", LIST_FAILURE_TTL_SECONDS);
|
|
220
424
|
}
|
|
221
425
|
throw err;
|
|
222
426
|
}
|
|
@@ -224,6 +428,17 @@ let ProviderRegistry = class ProviderRegistry {
|
|
|
224
428
|
return settled.map((r, i) => {
|
|
225
429
|
const provider = providers[i].plugin;
|
|
226
430
|
if (r.status === "fulfilled") {
|
|
431
|
+
if (r.value === "org_configured") {
|
|
432
|
+
return { provider, plugin: provider, status: "org_configured", models: [] };
|
|
433
|
+
}
|
|
434
|
+
if (r.value === "needs_config") {
|
|
435
|
+
const error = `Model provider ${provider} needs configuration in this organization`;
|
|
436
|
+
return { provider, plugin: provider, status: "error", error, models: [] };
|
|
437
|
+
}
|
|
438
|
+
if (r.value === "switched_off") {
|
|
439
|
+
const error = `Model provider ${provider} is turned off in this organization`;
|
|
440
|
+
return { provider, plugin: provider, status: "error", error, models: [] };
|
|
441
|
+
}
|
|
227
442
|
return { provider, plugin: provider, status: "ok", models: r.value };
|
|
228
443
|
}
|
|
229
444
|
this.log?.warn("model provider listModels failed; skipped", { provider, err: r.reason });
|
|
@@ -234,17 +449,53 @@ let ProviderRegistry = class ProviderRegistry {
|
|
|
234
449
|
});
|
|
235
450
|
}
|
|
236
451
|
/**
|
|
237
|
-
*
|
|
238
|
-
*
|
|
452
|
+
* 解析模型引用 for `orgId`,返回绑定到该组织调用选项的供应商与模型信息。
|
|
453
|
+
* 供应商未安装、在该组织需要配置、模型不存在(或被丢弃)、不是 `opts.kind`(default `chat`: a
|
|
454
|
+
* streaming caller never gets an embedding model)或被平台 / 该组织停用 → `agent.model_unavailable`;
|
|
239
455
|
* 供应商 `listModels()` 失败 → `ProviderListError`(同一错误码,运行时视为可重试)。
|
|
240
456
|
*/
|
|
241
|
-
async resolve(ref) {
|
|
457
|
+
async resolve(orgId, ref, opts = {}) {
|
|
458
|
+
const { p, t, info } = await this.lookup(orgId, ref, opts.kind ?? "chat");
|
|
459
|
+
return { provider: bindProvider(p.impl, t.call), info };
|
|
460
|
+
}
|
|
461
|
+
/**
|
|
462
|
+
* The model's info in the platform view (deployment-level settings such as a purpose's
|
|
463
|
+
* deployment default; no call is ever made there), by {@link resolve}'s rules. A provider each
|
|
464
|
+
* org configures itself has no platform list: `agent.model_unavailable`. Only the platform's
|
|
465
|
+
* disabled list applies.
|
|
466
|
+
*/
|
|
467
|
+
async platformInfo(ref, opts = {}) {
|
|
468
|
+
return (await this.lookup(undefined, ref, opts.kind ?? "chat")).info;
|
|
469
|
+
}
|
|
470
|
+
async lookup(orgId, ref, kind) {
|
|
471
|
+
const unavailable = () => new ApiError("agent.model_unavailable", `Model ${ref.provider}/${ref.model} is not available`);
|
|
242
472
|
const p = this.providers().find((x) => x.plugin === ref.provider);
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
473
|
+
if (!p)
|
|
474
|
+
throw unavailable();
|
|
475
|
+
const off = orgId === undefined ? new Set() : await this.enablement.switchedOff(orgId);
|
|
476
|
+
const t = await this.target(p, orgId, off);
|
|
477
|
+
if (!t.ok) {
|
|
478
|
+
throw new ApiError("agent.model_unavailable", t.reason === "org_configured"
|
|
479
|
+
? `Model provider ${ref.provider} is configured by each organization`
|
|
480
|
+
: t.reason === "switched_off"
|
|
481
|
+
? `Model provider ${ref.provider} is turned off in this organization`
|
|
482
|
+
: `Model provider ${ref.provider} needs configuration in this organization`);
|
|
483
|
+
}
|
|
484
|
+
const info = (await this.models(p, t)).find((m) => m.id === ref.model);
|
|
485
|
+
if (!info || (info.kind !== "chat" && info.kind !== "embedding"))
|
|
486
|
+
throw unavailable();
|
|
487
|
+
if (kind !== "callable" && info.kind !== kind) {
|
|
488
|
+
throw new ApiError("agent.model_unavailable", `Model ${ref.provider}/${ref.model} is not ${kind === "embedding" ? "an" : "a"} ${kind} model`);
|
|
489
|
+
}
|
|
490
|
+
const key = modelKey(ref.provider, ref.model);
|
|
491
|
+
const [platform, org] = await Promise.all([
|
|
492
|
+
this.enablement.disabled(null),
|
|
493
|
+
orgId === undefined ? Promise.resolve(new Set()) : this.enablement.disabled(orgId),
|
|
494
|
+
]);
|
|
495
|
+
if (platform.has(key) || org.has(key)) {
|
|
496
|
+
throw new ApiError("agent.model_unavailable", `Model ${ref.provider}/${ref.model} is disabled`);
|
|
246
497
|
}
|
|
247
|
-
return {
|
|
498
|
+
return { p, t, info: info };
|
|
248
499
|
}
|
|
249
500
|
};
|
|
250
501
|
__decorate([
|
|
@@ -253,11 +504,14 @@ __decorate([
|
|
|
253
504
|
ProviderRegistry = __decorate([
|
|
254
505
|
Injectable(),
|
|
255
506
|
__param(0, Inject(ExtensionRegistry)),
|
|
256
|
-
__param(1, Inject(
|
|
257
|
-
__param(2, Inject(
|
|
258
|
-
__param(3, Inject(
|
|
259
|
-
__param(4,
|
|
260
|
-
__param(
|
|
507
|
+
__param(1, Inject(EffectiveConfigs)),
|
|
508
|
+
__param(2, Inject(CacheService)),
|
|
509
|
+
__param(3, Inject(REDIS)),
|
|
510
|
+
__param(4, Inject(KernelConfigService)),
|
|
511
|
+
__param(5, Inject(ModelEnablement)),
|
|
512
|
+
__param(6, Inject(KERNEL_LOGGER)),
|
|
513
|
+
__param(7, Optional()),
|
|
514
|
+
__param(7, Inject(LIST_MODELS_TIMEOUT))
|
|
261
515
|
], ProviderRegistry);
|
|
262
516
|
export { ProviderRegistry };
|
|
263
517
|
/** The model provider registry (one instance; the models and agents domains read it). */
|
|
@@ -265,7 +519,7 @@ let ProvidersModule = class ProvidersModule {
|
|
|
265
519
|
};
|
|
266
520
|
ProvidersModule = __decorate([
|
|
267
521
|
Module({
|
|
268
|
-
imports: [
|
|
522
|
+
imports: [ExtensionStateModule, ModelEnablementModule],
|
|
269
523
|
providers: [ProviderRegistry],
|
|
270
524
|
exports: [ProviderRegistry],
|
|
271
525
|
})
|
|
@@ -170,7 +170,7 @@ export declare class RunWorker {
|
|
|
170
170
|
* model removed), writes `model_switched { model: null, effort: null, reason: "unavailable" }`
|
|
171
171
|
* for this run (idempotent) and clears the cached selection: the run and the next ones use the
|
|
172
172
|
* agent's model. A provider that only failed to list its models is not "gone" (the run's ladder
|
|
173
|
-
* handles it).
|
|
173
|
+
* handles it). Resolved for the conversation's org (its effective provider config).
|
|
174
174
|
*/
|
|
175
175
|
private fallBackIfUnavailable;
|
|
176
176
|
/**
|
|
@@ -581,7 +581,7 @@ let RunWorker = class RunWorker {
|
|
|
581
581
|
if (run.status === "queued" || startedAt === null) {
|
|
582
582
|
if (!existingStart) {
|
|
583
583
|
if (saved !== undefined)
|
|
584
|
-
await this.fallBackIfUnavailable(conversationId, runId, saved);
|
|
584
|
+
await this.fallBackIfUnavailable(conv.org_id, conversationId, runId, saved);
|
|
585
585
|
const started = await this.appendRunStarted(conversationId, run, agent, saved);
|
|
586
586
|
agent = started.agent;
|
|
587
587
|
const toSeq = started.toSeq;
|
|
@@ -789,14 +789,14 @@ let RunWorker = class RunWorker {
|
|
|
789
789
|
* model removed), writes `model_switched { model: null, effort: null, reason: "unavailable" }`
|
|
790
790
|
* for this run (idempotent) and clears the cached selection: the run and the next ones use the
|
|
791
791
|
* agent's model. A provider that only failed to list its models is not "gone" (the run's ladder
|
|
792
|
-
* handles it).
|
|
792
|
+
* handles it). Resolved for the conversation's org (its effective provider config).
|
|
793
793
|
*/
|
|
794
|
-
async fallBackIfUnavailable(conversationId, runId, agent) {
|
|
794
|
+
async fallBackIfUnavailable(orgId, conversationId, runId, agent) {
|
|
795
795
|
const sel = await readSelection(this.db, conversationId);
|
|
796
796
|
if (sel.model === null || sameModel(sel.model, agent.model))
|
|
797
797
|
return;
|
|
798
798
|
try {
|
|
799
|
-
await this.models.resolve(sel.model);
|
|
799
|
+
await this.models.resolve(orgId, sel.model);
|
|
800
800
|
return;
|
|
801
801
|
}
|
|
802
802
|
catch (e) {
|
|
@@ -184,9 +184,9 @@ selected = []) {
|
|
|
184
184
|
return { pinned, warnings };
|
|
185
185
|
}
|
|
186
186
|
/** The `effort` declaration of `model`, or null (or when the model cannot be resolved). */
|
|
187
|
-
async function effortDecl(registry, model) {
|
|
187
|
+
async function effortDecl(registry, orgId, model) {
|
|
188
188
|
try {
|
|
189
|
-
const { info } = await registry.resolve(model);
|
|
189
|
+
const { info } = await registry.resolve(orgId, model);
|
|
190
190
|
return info.parameters.find((p) => p.standard === "effort") ?? null;
|
|
191
191
|
}
|
|
192
192
|
catch {
|
|
@@ -306,7 +306,7 @@ export async function effectiveDefinition(d, a) {
|
|
|
306
306
|
const m = await resolveTier(d, a.orgId, parent, tier);
|
|
307
307
|
let modelParams = m.modelParams;
|
|
308
308
|
if (template?.effort !== undefined) {
|
|
309
|
-
const decl = await effortDecl(d.registry, m.model);
|
|
309
|
+
const decl = await effortDecl(d.registry, a.orgId, m.model);
|
|
310
310
|
if (decl) {
|
|
311
311
|
const v = sanitizeParams([decl], { [decl.key]: template.effort }).value[decl.key];
|
|
312
312
|
if (v !== undefined)
|