@capybara-hq/kernel 0.1.0-rc.42 → 0.1.0-rc.44
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/catalog/marketplaces.service.d.ts +1 -1
- package/dist/catalog/scope-sync.d.ts +3 -2
- package/dist/catalog/scope-sync.js +3 -2
- package/dist/client/client-access.service.js +4 -4
- package/dist/config/schema.d.ts +3 -0
- package/dist/config/schema.js +30 -0
- package/dist/conversations/events.d.ts +3 -0
- package/dist/conversations/events.js +7 -3
- package/dist/ctx/create-ctx.d.ts +13 -6
- package/dist/ctx/create-ctx.js +21 -5
- package/dist/db/kernel-schema.d.ts +10 -0
- package/dist/extensions/enabled.d.ts +24 -4
- package/dist/extensions/enabled.js +47 -10
- package/dist/extensions/host.d.ts +7 -3
- package/dist/extensions/host.js +13 -8
- package/dist/extensions/org-config.d.ts +5 -2
- package/dist/extensions/org-config.js +32 -22
- package/dist/infra/config/kernel-config.service.js +4 -0
- package/dist/models/enablement.d.ts +1 -1
- package/dist/models/enablement.js +1 -1
- package/dist/models/image-usable.d.ts +22 -0
- package/dist/models/image-usable.js +29 -0
- package/dist/models/images.d.ts +125 -0
- package/dist/models/images.js +440 -0
- package/dist/models/models.module.d.ts +3 -2
- package/dist/models/models.module.js +16 -5
- package/dist/models/org-models.schemas.js +1 -0
- package/dist/models/purposes.controller.d.ts +7 -0
- package/dist/models/purposes.controller.js +9 -0
- package/dist/models/purposes.d.ts +28 -3
- package/dist/models/purposes.js +40 -13
- package/dist/platform/ops/models.ops.js +10 -0
- package/dist/runtime/hook-calls.d.ts +11 -1
- package/dist/runtime/hook-calls.js +24 -1
- package/dist/runtime/providers.d.ts +10 -6
- package/dist/runtime/providers.js +23 -4
- package/dist/runtime/usage-report.d.ts +10 -6
- package/dist/runtime/usage-report.js +31 -11
- package/dist/sandbox/review/permission-candidate.js +22 -6
- package/dist/sandbox/review/review.service.d.ts +1 -1
- package/dist/sandbox/review/review.service.js +43 -12
- package/dist/tools/builtin-names.d.ts +3 -1
- package/dist/tools/builtin-names.js +5 -1
- package/dist/tools/builtins/artifacts.d.ts +43 -0
- package/dist/tools/builtins/artifacts.js +67 -0
- package/dist/tools/builtins/builtin-tools.d.ts +4 -0
- package/dist/tools/builtins/builtin-tools.js +28 -5
- package/dist/tools/builtins/image/generate.d.ts +59 -0
- package/dist/tools/builtins/image/generate.js +535 -0
- package/dist/tools/builtins/image/index.d.ts +2 -0
- package/dist/tools/builtins/image/index.js +2 -0
- package/dist/tools/builtins/image/paths.d.ts +12 -0
- package/dist/tools/builtins/image/paths.js +26 -0
- package/dist/tools/builtins/sandbox/deliver.js +7 -49
- package/dist/tools/pipeline.js +15 -7
- package/dist/tools/registry.d.ts +2 -2
- package/dist/tools/registry.js +2 -2
- package/dist/tools/tools.module.js +3 -0
- package/package.json +4 -4
- package/src/migrations/0160_model_usage_image_kind.sql +10 -0
|
@@ -24,7 +24,7 @@ import { OrgLock } from "../orgs/org-lock.js";
|
|
|
24
24
|
import { toIssues } from "./config-issues.js";
|
|
25
25
|
import { configMessages, safeConfigFormSchema } from "./config-json-schema.js";
|
|
26
26
|
import { EffectiveConfigs } from "./effective-config.js";
|
|
27
|
-
import { enabledExtensionsQuery, requiredExtensions } from "./enabled.js";
|
|
27
|
+
import { alwaysOnExtensions, enabledExtensionsQuery, requiredExtensions } from "./enabled.js";
|
|
28
28
|
import { ExtensionHost } from "./host.js";
|
|
29
29
|
import { ExtensionInstalls } from "./installs.js";
|
|
30
30
|
import { fieldLevels } from "./org-levels.js";
|
|
@@ -216,8 +216,10 @@ let OrgExtensionConfigs = class OrgExtensionConfigs {
|
|
|
216
216
|
.execute();
|
|
217
217
|
await this.afterConfigRowRead?.();
|
|
218
218
|
const setKeys = await this.setSecretKeys(t, orgId);
|
|
219
|
-
// Required bootstrap plugins are listed enabled (no org
|
|
219
|
+
// Required bootstrap plugins and builtin plugins' extensions are listed enabled (no org
|
|
220
|
+
// config row needed, spec §7; enabled.ts)
|
|
220
221
|
const required = await requiredExtensions(t);
|
|
222
|
+
const on = await alwaysOnExtensions(t);
|
|
221
223
|
const loaded = this.host.manifests();
|
|
222
224
|
const items = await pluginItemsOf(t, [
|
|
223
225
|
...loaded.map((m) => m.name),
|
|
@@ -228,13 +230,13 @@ let OrgExtensionConfigs = class OrgExtensionConfigs {
|
|
|
228
230
|
const listed = await scopeListedFor(t, orgId);
|
|
229
231
|
const seen = (name) => offeredToOrg({
|
|
230
232
|
required: required.has(name),
|
|
231
|
-
enabled: rows.find((r) => r.plugin === name)?.enabled ?? false,
|
|
233
|
+
enabled: on.has(name) || (rows.find((r) => r.plugin === name)?.enabled ?? false),
|
|
232
234
|
items: items.get(name),
|
|
233
235
|
listed,
|
|
234
236
|
});
|
|
235
237
|
const views = loaded
|
|
236
238
|
.filter((m) => seen(m.name))
|
|
237
|
-
.map((m) => this.view(m, rows.find((r) => r.plugin === m.name), setKeys, required.has(m.name), items.get(m.name)));
|
|
239
|
+
.map((m) => this.view(m, rows.find((r) => r.plugin === m.name), setKeys, required.has(m.name), items.get(m.name), on.has(m.name)));
|
|
238
240
|
const names = new Set(loaded.map((m) => m.name));
|
|
239
241
|
const unavailable = installed
|
|
240
242
|
.filter((i) => !names.has(i.name) && seen(i.name))
|
|
@@ -242,7 +244,7 @@ let OrgExtensionConfigs = class OrgExtensionConfigs {
|
|
|
242
244
|
.map((i) => ({
|
|
243
245
|
plugin: i.name,
|
|
244
246
|
version: i.version,
|
|
245
|
-
enabled:
|
|
247
|
+
enabled: on.has(i.name) || (rows.find((r) => r.plugin === i.name)?.enabled ?? false),
|
|
246
248
|
configurable: false,
|
|
247
249
|
config: null,
|
|
248
250
|
secrets: [],
|
|
@@ -312,18 +314,22 @@ let OrgExtensionConfigs = class OrgExtensionConfigs {
|
|
|
312
314
|
.where("org_id", "=", orgId)
|
|
313
315
|
.executeTakeFirst();
|
|
314
316
|
const required = (await requiredExtensions(t)).has(plugin);
|
|
317
|
+
// Always on (required, or its plugin is builtin): enabled in every org (enabled.ts)
|
|
318
|
+
const on = required || (await alwaysOnExtensions(t)).has(plugin);
|
|
315
319
|
const item = (await pluginItemsOf(t, [plugin])).get(plugin);
|
|
316
320
|
if (required && input.enabled === false)
|
|
317
321
|
throw requiredError(plugin);
|
|
318
|
-
if (
|
|
322
|
+
if (on && input.enabled === false)
|
|
323
|
+
throw scopeLockedError();
|
|
324
|
+
if (opts.ifDisabled && (row?.enabled === true || on)) {
|
|
319
325
|
opts.ifDisabled.skipped = true;
|
|
320
|
-
return this.view(manifest, row, await this.setSecretKeys(t, orgId), required, item);
|
|
326
|
+
return this.view(manifest, row, await this.setSecretKeys(t, orgId), required, item, on);
|
|
321
327
|
}
|
|
322
|
-
//
|
|
323
|
-
const enabled =
|
|
328
|
+
// An always-on extension is enabled whatever the row said (it may predate `required` / builtin)
|
|
329
|
+
const enabled = on || (input.enabled ?? row?.enabled ?? false);
|
|
324
330
|
// validateOnly: the rules for (newly) enabling and disabling belong to the write that follows
|
|
325
331
|
if (!opts.validateOnly) {
|
|
326
|
-
await this.assertSwitchRules(t, orgId, manifest, input, row,
|
|
332
|
+
await this.assertSwitchRules(t, orgId, manifest, input, row, on, opts);
|
|
327
333
|
}
|
|
328
334
|
let plain = row?.config ?? null;
|
|
329
335
|
let changed = [];
|
|
@@ -376,7 +382,7 @@ let OrgExtensionConfigs = class OrgExtensionConfigs {
|
|
|
376
382
|
}
|
|
377
383
|
// Also without an org config schema: nothing is written
|
|
378
384
|
if (opts.validateOnly) {
|
|
379
|
-
return this.view(manifest, row, await this.setSecretKeys(t, orgId), required, item);
|
|
385
|
+
return this.view(manifest, row, await this.setSecretKeys(t, orgId), required, item, on);
|
|
380
386
|
}
|
|
381
387
|
// 当前 schema 中已不存在的密钥路径:同一事务内清掉遗留的密文行
|
|
382
388
|
const live = new Set((schema ? secretPaths(schema) : []).map((p) => secretKey(plugin, p)));
|
|
@@ -421,7 +427,7 @@ let OrgExtensionConfigs = class OrgExtensionConfigs {
|
|
|
421
427
|
.where("plugin", "=", plugin)
|
|
422
428
|
.where("org_id", "=", orgId)
|
|
423
429
|
.executeTakeFirst();
|
|
424
|
-
const view = this.view(manifest, saved, await this.setSecretKeys(t, orgId), required, item);
|
|
430
|
+
const view = this.view(manifest, saved, await this.setSecretKeys(t, orgId), required, item, on);
|
|
425
431
|
// Only once the root transaction committed (an outer caller's too): a rolled-back write
|
|
426
432
|
// registers no secrets and disconnects nothing
|
|
427
433
|
afterCommit(async () => {
|
|
@@ -435,9 +441,9 @@ let OrgExtensionConfigs = class OrgExtensionConfigs {
|
|
|
435
441
|
* re-checks the item policy and grants (C22; one already enabled is unaffected), enabling needs
|
|
436
442
|
* the dependencies enabled, disabling (without `force`) no enabled dependents.
|
|
437
443
|
*/
|
|
438
|
-
async assertSwitchRules(t, orgId, manifest, input, row,
|
|
444
|
+
async assertSwitchRules(t, orgId, manifest, input, row, alwaysOn, opts) {
|
|
439
445
|
const plugin = manifest.name;
|
|
440
|
-
if (input.enabled === true && row?.enabled !== true && !
|
|
446
|
+
if (input.enabled === true && row?.enabled !== true && !alwaysOn) {
|
|
441
447
|
// 插件自己的条目 disabled 是总开关,任何途径都不能新启用(裁决 M-5)
|
|
442
448
|
const killed = await disabledPluginItems(t, [plugin]);
|
|
443
449
|
if (killed.length > 0)
|
|
@@ -457,7 +463,8 @@ let OrgExtensionConfigs = class OrgExtensionConfigs {
|
|
|
457
463
|
}
|
|
458
464
|
/**
|
|
459
465
|
* 读取并解密某插件在该组织下的完整配置(供运行时按组织构造插件实例)。
|
|
460
|
-
*
|
|
466
|
+
* An always-on extension (a required bootstrap plugin, or its plugin is builtin: `enabled.ts`) is
|
|
467
|
+
* enabled without a row (its stored config, or `{}`, is validated).
|
|
461
468
|
* 无记录 → undefined;插件无 `config.org` 或已停用(`enabled=false`,不校验、不读密钥)时 `config` 为 null。
|
|
462
469
|
* 配置行与密钥在同一个 `REPEATABLE READ` 只读事务里读取。
|
|
463
470
|
* A stored config that no longer matches the schema (e.g. after a plugin upgrade) throws
|
|
@@ -483,10 +490,10 @@ let OrgExtensionConfigs = class OrgExtensionConfigs {
|
|
|
483
490
|
.where("org_id", "=", orgId)
|
|
484
491
|
.executeTakeFirst();
|
|
485
492
|
await this.afterConfigRowRead?.();
|
|
486
|
-
const
|
|
487
|
-
if (!row && !
|
|
493
|
+
const on = (await alwaysOnExtensions(t)).has(plugin);
|
|
494
|
+
if (!row && !on)
|
|
488
495
|
return undefined;
|
|
489
|
-
const enabled =
|
|
496
|
+
const enabled = on || row?.enabled === true;
|
|
490
497
|
const schema = manifest.config?.org;
|
|
491
498
|
// 已停用的插件不会被实例化:不校验(可能是升级前存下的旧配置),也不解密密钥
|
|
492
499
|
if (!schema || !enabled)
|
|
@@ -544,8 +551,8 @@ let OrgExtensionConfigs = class OrgExtensionConfigs {
|
|
|
544
551
|
*/
|
|
545
552
|
async disableIn(orgId, plugin, actorAccountId, reason) {
|
|
546
553
|
const t = this.db;
|
|
547
|
-
//
|
|
548
|
-
if ((await
|
|
554
|
+
// An always-on extension (required, or its plugin is builtin) stays enabled in every org
|
|
555
|
+
if ((await alwaysOnExtensions(t)).has(plugin))
|
|
549
556
|
return false;
|
|
550
557
|
const r = await t
|
|
551
558
|
.updateTable("extension_org_configs")
|
|
@@ -635,7 +642,9 @@ let OrgExtensionConfigs = class OrgExtensionConfigs {
|
|
|
635
642
|
throw new ApiError("extension.not_found", `Extension ${plugin} is not installed`);
|
|
636
643
|
return set;
|
|
637
644
|
}
|
|
638
|
-
view(m, row, setKeys, required, items
|
|
645
|
+
view(m, row, setKeys, required, items,
|
|
646
|
+
/** Always on in every org (enabled.ts): required, or its plugin is builtin. */
|
|
647
|
+
alwaysOn) {
|
|
639
648
|
const schema = m.config?.org;
|
|
640
649
|
const paths = schema ? secretPaths(schema) : [];
|
|
641
650
|
let config = null;
|
|
@@ -647,7 +656,7 @@ let OrgExtensionConfigs = class OrgExtensionConfigs {
|
|
|
647
656
|
return {
|
|
648
657
|
plugin: m.name,
|
|
649
658
|
version: m.version,
|
|
650
|
-
enabled:
|
|
659
|
+
enabled: alwaysOn || (row?.enabled ?? false),
|
|
651
660
|
configurable: schema !== undefined,
|
|
652
661
|
config,
|
|
653
662
|
secrets: paths.map((p) => ({ path: p, set: setKeys.has(secretKey(m.name, p)) })),
|
|
@@ -753,4 +762,5 @@ function orgConfigIncomplete(schema, plain, secretSet) {
|
|
|
753
762
|
const secrets = secretPaths(schema).filter(secretSet);
|
|
754
763
|
return toIssues(r.error).some((i) => !secrets.some((p) => i.path === p || i.path.startsWith(`${p}.`)));
|
|
755
764
|
}
|
|
765
|
+
const scopeLockedError = () => new ApiError("plugin.scope_locked", "This plugin is built in: it is on for every organization and cannot be switched off here");
|
|
756
766
|
const requiredError = (plugin) => new ApiError("extension.required", `Extension ${plugin} is required by this deployment (a bootstrap plugin): it is enabled in every organization and cannot be disabled`);
|
|
@@ -35,6 +35,10 @@ const HOT_APPLIERS = {
|
|
|
35
35
|
// The provider registry reads the list time limit at every listModels call.
|
|
36
36
|
"models.listTimeoutMs": () => { },
|
|
37
37
|
"models.embedTimeoutMs": () => { },
|
|
38
|
+
// ModelImages reads the image limits at every prepare and run.
|
|
39
|
+
"models.imageTimeoutMs": () => { },
|
|
40
|
+
"models.imageMaxPerCall": () => { },
|
|
41
|
+
"models.imagePromptMaxChars": () => { },
|
|
38
42
|
// SandboxService reads the shared-lease cap at every acquisition.
|
|
39
43
|
"sandbox.maxConcurrentCommands": () => { },
|
|
40
44
|
// The command-review service reads the classifier's bounds at every classifier call.
|
|
@@ -65,7 +65,7 @@ export declare class ModelEnablement {
|
|
|
65
65
|
private rowsOf;
|
|
66
66
|
/**
|
|
67
67
|
* The extensions enabled in `orgId` ({@link enabledExtensionsQuery}: the org's own switch on, or
|
|
68
|
-
* a required bootstrap plugin): a model provider is on in an org only then, like every other
|
|
68
|
+
* always on: a required bootstrap plugin or a builtin plugin's extension): a model provider is on in an org only then, like every other
|
|
69
69
|
* extension. Read from PG on every call (one small query; the switch has its own writers, nothing
|
|
70
70
|
* to evict).
|
|
71
71
|
*/
|
|
@@ -75,7 +75,7 @@ let ModelEnablement = class ModelEnablement {
|
|
|
75
75
|
}
|
|
76
76
|
/**
|
|
77
77
|
* The extensions enabled in `orgId` ({@link enabledExtensionsQuery}: the org's own switch on, or
|
|
78
|
-
* a required bootstrap plugin): a model provider is on in an org only then, like every other
|
|
78
|
+
* always on: a required bootstrap plugin or a builtin plugin's extension): a model provider is on in an org only then, like every other
|
|
79
79
|
* extension. Read from PG on every call (one small query; the switch has its own writers, nothing
|
|
80
80
|
* to evict).
|
|
81
81
|
*/
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { Logger } from "@capybara-hq/plugin-sdk";
|
|
2
|
+
import { type ProviderRegistry } from "../runtime/providers.js";
|
|
3
|
+
import type { StorageService } from "../storage/storage.service.js";
|
|
4
|
+
import type { ModelPurposesService } from "./purposes.js";
|
|
5
|
+
/** The model purpose `GenerateImage` calls (image generation spec §5.4; no fallback). */
|
|
6
|
+
export declare const IMAGE_PURPOSE = "image";
|
|
7
|
+
/** Whether `GenerateImage` can run in an org now, and if not, why. */
|
|
8
|
+
export type ImageUsable = "usable" | "no-model" | "no-storage";
|
|
9
|
+
/**
|
|
10
|
+
* The one predicate of `GenerateImage`'s availability (image generation spec §5.3), shared by the
|
|
11
|
+
* tool's listing and the member-readable purpose availability the agent form reads, so the two
|
|
12
|
+
* never disagree: the org's `image` purpose resolves to an image model the org can call now (an
|
|
13
|
+
* unmapped, disabled or unavailable model, or a provider whose model list fails, is `no-model`),
|
|
14
|
+
* and an object store is there to deliver the images (`no-storage`). Any other failure is logged
|
|
15
|
+
* and rethrown: it is a kernel fault, not an unavailable model.
|
|
16
|
+
*/
|
|
17
|
+
export declare function imagePurposeUsable(deps: {
|
|
18
|
+
purposes: Pick<ModelPurposesService, "resolve">;
|
|
19
|
+
providers: Pick<ProviderRegistry, "resolve">;
|
|
20
|
+
storage: Pick<StorageService, "available">;
|
|
21
|
+
log: Pick<Logger, "warn">;
|
|
22
|
+
}, orgId: string): Promise<ImageUsable>;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { ApiError } from "../errors.js";
|
|
2
|
+
import { ProviderListError } from "../runtime/providers.js";
|
|
3
|
+
/** The model purpose `GenerateImage` calls (image generation spec §5.4; no fallback). */
|
|
4
|
+
export const IMAGE_PURPOSE = "image";
|
|
5
|
+
/**
|
|
6
|
+
* The one predicate of `GenerateImage`'s availability (image generation spec §5.3), shared by the
|
|
7
|
+
* tool's listing and the member-readable purpose availability the agent form reads, so the two
|
|
8
|
+
* never disagree: the org's `image` purpose resolves to an image model the org can call now (an
|
|
9
|
+
* unmapped, disabled or unavailable model, or a provider whose model list fails, is `no-model`),
|
|
10
|
+
* and an object store is there to deliver the images (`no-storage`). Any other failure is logged
|
|
11
|
+
* and rethrown: it is a kernel fault, not an unavailable model.
|
|
12
|
+
*/
|
|
13
|
+
export async function imagePurposeUsable(deps, orgId) {
|
|
14
|
+
try {
|
|
15
|
+
const target = await deps.purposes.resolve(orgId, IMAGE_PURPOSE);
|
|
16
|
+
await deps.providers.resolve(orgId, target.model, { kind: "image_generation" });
|
|
17
|
+
}
|
|
18
|
+
catch (e) {
|
|
19
|
+
if (e instanceof ProviderListError)
|
|
20
|
+
return "no-model";
|
|
21
|
+
if (e instanceof ApiError &&
|
|
22
|
+
(e.code === "model.unconfigured" || e.code === "agent.model_unavailable")) {
|
|
23
|
+
return "no-model";
|
|
24
|
+
}
|
|
25
|
+
deps.log.warn("image purpose availability check failed", { orgId, err: e });
|
|
26
|
+
throw e;
|
|
27
|
+
}
|
|
28
|
+
return deps.storage.available ? "usable" : "no-storage";
|
|
29
|
+
}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { type ExtensionImageGenerateRequest, type ExtensionImageGenerateResult, type ExtensionModelTarget, type HookScope, type ImageAspectRatio, type ImageGenerateRequest, type ImageGenerateResult, type ImageMediaType, type ImageSize, type Logger, type ModelImageGenerationInfo } from "@capybara-hq/plugin-sdk";
|
|
2
|
+
import { KernelConfigService } from "../infra/config/kernel-config.service.js";
|
|
3
|
+
import { KernelDb } from "../infra/database/kernel-db.js";
|
|
4
|
+
import { TransactionHost } from "../infra/database/transaction-host.js";
|
|
5
|
+
import { type HookDenial, type HookRunner } from "../runtime/hook-calls.js";
|
|
6
|
+
import { ProviderRegistry } from "../runtime/providers.js";
|
|
7
|
+
import { UsageReportJobs } from "../runtime/usage-report-jobs.js";
|
|
8
|
+
import type { DeclaredModelPurposes } from "./embeddings.js";
|
|
9
|
+
import { ModelPurposesService } from "./purposes.js";
|
|
10
|
+
/** What a caller asks for (the tool or `ctx.models.generateImage`); references are bytes already. */
|
|
11
|
+
export interface ImageCallInput {
|
|
12
|
+
prompt: string;
|
|
13
|
+
/** An exact size (only `ctx.models`); must be one the model declares. */
|
|
14
|
+
size?: ImageSize;
|
|
15
|
+
aspectRatio?: ImageAspectRatio;
|
|
16
|
+
/** Images to produce (default 1). */
|
|
17
|
+
n?: number;
|
|
18
|
+
/** Base64 reference images. */
|
|
19
|
+
referenceImages?: {
|
|
20
|
+
mediaType: ImageMediaType;
|
|
21
|
+
data: string;
|
|
22
|
+
}[];
|
|
23
|
+
}
|
|
24
|
+
/** A validated call, ready for {@link ModelImages.run}. */
|
|
25
|
+
export interface PreparedImageCall {
|
|
26
|
+
ref: {
|
|
27
|
+
provider: string;
|
|
28
|
+
model: string;
|
|
29
|
+
};
|
|
30
|
+
info: ModelImageGenerationInfo;
|
|
31
|
+
request: ImageGenerateRequest;
|
|
32
|
+
}
|
|
33
|
+
/** Who makes the call, as the hook payloads name it. */
|
|
34
|
+
export type ImageHookIdentity = {
|
|
35
|
+
kind: "conversation";
|
|
36
|
+
scope: HookScope;
|
|
37
|
+
step: number;
|
|
38
|
+
callId: string;
|
|
39
|
+
} | {
|
|
40
|
+
kind: "extension";
|
|
41
|
+
extension: string;
|
|
42
|
+
imagePurpose: string;
|
|
43
|
+
};
|
|
44
|
+
/** A `model.request` policy hook denied the call; nothing was sent. The caller records it. */
|
|
45
|
+
export declare class ImageCallDenied extends Error {
|
|
46
|
+
readonly verdict: HookDenial;
|
|
47
|
+
constructor(verdict: HookDenial);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Image generation calls (image generation spec §5.1, §5.2, §5.6, §6), shared by the
|
|
51
|
+
* `GenerateImage` tool and `ctx.models.generateImage`:
|
|
52
|
+
* - {@link prepare} resolves the org's target for a purpose of kind `image_generation` (no
|
|
53
|
+
* fallback: unmapped is `model.unconfigured`), checks the prompt (code points, at most
|
|
54
|
+
* `models.imagePromptMaxChars`), the count (at most `models.imageMaxPerCall` and the model's
|
|
55
|
+
* `maxImages`), the reference images (count, media type, `MEDIA_LIMITS.imageBytes`), picks the
|
|
56
|
+
* size ({@link pickImageSize}) and sanitizes the mapping's params against the model's
|
|
57
|
+
* declarations. Every refusal is `ApiError("validation_failed")` with an actionable text.
|
|
58
|
+
* - {@link run} fires `model.request` (placeholders for references; a denial throws
|
|
59
|
+
* {@link ImageCallDenied} and sends nothing), calls `generateImage` within
|
|
60
|
+
* `models.imageTimeoutMs`, checks the result ({@link validateImageGenerateResult}: invalid is a
|
|
61
|
+
* `ModelError("server")`) and fires `model.response` (observe; media types and byte sizes, never
|
|
62
|
+
* bytes), also when the call failed, but not when the caller's signal aborted it.
|
|
63
|
+
* They store nothing and record no usage: the callers do. For extensions (spec §5.5, §8, §9),
|
|
64
|
+
* {@link generateForExtension} is `ctx.models.generateImage`: the admission of `ctx.models.embed`
|
|
65
|
+
* for kind `image_generation` (a purpose the extension declared with that kind, otherwise
|
|
66
|
+
* `model.purpose_unknown`; an org that enabled the extension, otherwise `extension.not_enabled`),
|
|
67
|
+
* then {@link prepare} and {@link run}; a policy denial writes one `hook.denied` audit row and
|
|
68
|
+
* rejects with `ApiError("model.hook_denied")`; a successful call writes one `model_usage` row
|
|
69
|
+
* (`kind = 'image_generation'`, its input tokens and images) and owes its `usage.report` delivery
|
|
70
|
+
* in the same transaction ({@link recordExtensionUsage}). A call that fails or is denied records
|
|
71
|
+
* no usage. The kernel stores no image of an extension call: the extension gets the bytes.
|
|
72
|
+
*/
|
|
73
|
+
export declare class ModelImages {
|
|
74
|
+
private readonly purposes;
|
|
75
|
+
private readonly providers;
|
|
76
|
+
private readonly log;
|
|
77
|
+
/** `models.imageTimeoutMs`, `imageMaxPerCall`, `imagePromptMaxChars` (hot, read per call). */
|
|
78
|
+
private readonly config;
|
|
79
|
+
private readonly kdb;
|
|
80
|
+
readonly txHost: TransactionHost;
|
|
81
|
+
/** An extension call's `usage.report` delivery, owed with its `model_usage` row. */
|
|
82
|
+
private readonly usage;
|
|
83
|
+
constructor(purposes: Pick<ModelPurposesService, "resolve">, providers: Pick<ProviderRegistry, "resolve">, log: Pick<Logger, "warn">,
|
|
84
|
+
/** `models.imageTimeoutMs`, `imageMaxPerCall`, `imagePromptMaxChars` (hot, read per call). */
|
|
85
|
+
config: Pick<KernelConfigService, "get">, kdb: KernelDb, txHost: TransactionHost,
|
|
86
|
+
/** An extension call's `usage.report` delivery, owed with its `model_usage` row. */
|
|
87
|
+
usage: Pick<UsageReportJobs, "oweModelUsage">);
|
|
88
|
+
/** Refuses a purpose `plugin` did not declare with kind `image_generation`, or an org it is off in. */
|
|
89
|
+
private admit;
|
|
90
|
+
/**
|
|
91
|
+
* `ctx.models.generateImage` for `plugin` (its declared purposes), aborted by `signal` (the
|
|
92
|
+
* instance's) and the request's own signal; `hooks` is the host's hook registry
|
|
93
|
+
* (`model.request`, `model.response`), absent only for a ctx built without a host (tests).
|
|
94
|
+
*/
|
|
95
|
+
generateForExtension(plugin: string, declared: DeclaredModelPurposes, request: ExtensionImageGenerateRequest, signal: AbortSignal, hooks: HookRunner | undefined): Promise<ExtensionImageGenerateResult>;
|
|
96
|
+
/**
|
|
97
|
+
* `ctx.models.describe` of an image generation purpose for `plugin`: the org's effective target
|
|
98
|
+
* with its `imageGeneration` facts, or null when the purpose maps to no model the org can use.
|
|
99
|
+
*/
|
|
100
|
+
describeForExtension(plugin: string, declared: DeclaredModelPurposes, req: {
|
|
101
|
+
orgId: string;
|
|
102
|
+
purpose: string;
|
|
103
|
+
}): Promise<ExtensionModelTarget | null>;
|
|
104
|
+
/**
|
|
105
|
+
* One `model_usage` row of an extension call (`kind = 'image_generation'`, the result's tokens
|
|
106
|
+
* and images) with its owed `usage.report` delivery, in one transaction.
|
|
107
|
+
*/
|
|
108
|
+
private recordExtensionUsage;
|
|
109
|
+
prepare(orgId: string, purpose: string, input: ImageCallInput): Promise<PreparedImageCall>;
|
|
110
|
+
run(orgId: string, prepared: PreparedImageCall, who: ImageHookIdentity, signal: AbortSignal, hooks: HookRunner | undefined): Promise<ImageGenerateResult & {
|
|
111
|
+
latencyMs: number;
|
|
112
|
+
}>;
|
|
113
|
+
/**
|
|
114
|
+
* One `generateImage` call, limited by `models.imageTimeoutMs` (hot, read per call): at the limit
|
|
115
|
+
* the call's signal aborts and the call fails with `ModelError("timeout")`, even when the
|
|
116
|
+
* provider ignores the signal (its late answer is dropped).
|
|
117
|
+
*/
|
|
118
|
+
private callWithTimeout;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* The images a validated answer is billed for: the answer's own `usage.outputImages`, else the
|
|
122
|
+
* images it carries, never more than it carries (a vendor count above them is not trusted). The
|
|
123
|
+
* one rule of the tool's `usage` event and an extension call's `model_usage` row.
|
|
124
|
+
*/
|
|
125
|
+
export declare function billedImages(res: Pick<ImageGenerateResult, "images" | "usage">): number;
|