@capybara-hq/kernel 0.1.0-rc.82 → 0.1.0-rc.83
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/errors.d.ts +2 -0
- package/dist/errors.js +2 -0
- package/dist/orgs/directory-sync.d.ts +10 -6
- package/dist/orgs/directory-sync.js +5 -5
- package/dist/platform/exclusions.js +0 -5
- package/dist/platform/operation.d.ts +2 -0
- package/dist/platform/ops/extensions.ops.d.ts +9 -1
- package/dist/platform/ops/extensions.ops.js +131 -4
- package/package.json +1 -1
package/dist/errors.d.ts
CHANGED
|
@@ -244,6 +244,8 @@ export declare const ERROR_CODES: {
|
|
|
244
244
|
*/
|
|
245
245
|
readonly "client.not_found": 404;
|
|
246
246
|
readonly "directory.not_configured": 409;
|
|
247
|
+
/** A manual directory sync could not read the extension's state for the org (transient). */
|
|
248
|
+
readonly "directory.unavailable": 503;
|
|
247
249
|
readonly "catalog.item_disabled": 403;
|
|
248
250
|
readonly "plugin.disabled": 409;
|
|
249
251
|
readonly "plugin.scope_locked": 409;
|
package/dist/errors.js
CHANGED
|
@@ -244,6 +244,8 @@ export const ERROR_CODES = {
|
|
|
244
244
|
*/
|
|
245
245
|
"client.not_found": 404,
|
|
246
246
|
"directory.not_configured": 409,
|
|
247
|
+
/** A manual directory sync could not read the extension's state for the org (transient). */
|
|
248
|
+
"directory.unavailable": 503,
|
|
247
249
|
"catalog.item_disabled": 403,
|
|
248
250
|
"plugin.disabled": 409,
|
|
249
251
|
"plugin.scope_locked": 409,
|
|
@@ -35,12 +35,16 @@ export declare const DIRECTORY_MAX_PAGES = 10000;
|
|
|
35
35
|
/** 触发来源:每小时扫描、入站 `directory_changed` 提示、管理员手动。 */
|
|
36
36
|
export type DirectorySyncTrigger = "schedule" | "changed" | "manual";
|
|
37
37
|
/**
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
38
|
+
* Why a sync does not run: a stale job (the extension is unloaded or no longer enabled), the org was
|
|
39
|
+
* deleted, another sync holds the lock. `state_unreadable`: whether the extension is enabled in the
|
|
40
|
+
* org, or its effective config, could not be read (a transient failure such as a database error;
|
|
41
|
+
* a manual sync answers 503 `directory.unavailable`). `not_configured`: the extension's
|
|
42
|
+
* `Directory.configured` says the org's config does not set up the directory (Ruling A28, skipped
|
|
43
|
+
* silently). `needs_config`: the extension's effective deployment config for the org is not valid
|
|
44
|
+
* (layering spec A §6): skipped silently like `not_configured`; the org admin sees the extension as
|
|
45
|
+
* needing configuration.
|
|
42
46
|
*/
|
|
43
|
-
export type DirectorySkipReason = "plugin_unavailable" | "org_not_found" | "not_enabled" | "
|
|
47
|
+
export type DirectorySkipReason = "plugin_unavailable" | "org_not_found" | "not_enabled" | "state_unreadable" | "not_configured" | "needs_config" | "locked";
|
|
44
48
|
export type DirectorySyncResult = {
|
|
45
49
|
status: "skipped";
|
|
46
50
|
reason: DirectorySkipReason;
|
|
@@ -156,7 +160,7 @@ export declare class DirectorySync {
|
|
|
156
160
|
/**
|
|
157
161
|
* Manual trigger by an admin: re-checks `org.members.manage` live under the org row lock, then that
|
|
158
162
|
* the extension can sync (does not provide a directory -> `extension.not_found`, not enabled ->
|
|
159
|
-
* `extension.not_enabled`, its state cannot be read -> `
|
|
163
|
+
* `extension.not_enabled`, its state cannot be read (transient) -> 503 `directory.unavailable`, the extension says
|
|
160
164
|
* the directory is not configured -> `directory.not_configured`, Ruling A28; the effective config
|
|
161
165
|
* is not valid in the org -> `extension.needs_config`, layering spec A §6), and enqueues a sync
|
|
162
166
|
* job with a unique id.
|
|
@@ -194,7 +194,7 @@ let DirectorySync = class DirectorySync {
|
|
|
194
194
|
plugin,
|
|
195
195
|
err,
|
|
196
196
|
});
|
|
197
|
-
return { ok: false, reason: "
|
|
197
|
+
return { ok: false, reason: "state_unreadable" };
|
|
198
198
|
}
|
|
199
199
|
if (!cfg?.enabled)
|
|
200
200
|
return { ok: false, reason: "not_enabled" };
|
|
@@ -204,7 +204,7 @@ let DirectorySync = class DirectorySync {
|
|
|
204
204
|
}
|
|
205
205
|
catch (err) {
|
|
206
206
|
this.log.warn("directory sync: cannot read the effective config", { orgId, plugin, err });
|
|
207
|
-
return { ok: false, reason: "
|
|
207
|
+
return { ok: false, reason: "state_unreadable" };
|
|
208
208
|
}
|
|
209
209
|
if (!effective.ok)
|
|
210
210
|
return { ok: false, reason: "needs_config" };
|
|
@@ -255,7 +255,7 @@ let DirectorySync = class DirectorySync {
|
|
|
255
255
|
/**
|
|
256
256
|
* Manual trigger by an admin: re-checks `org.members.manage` live under the org row lock, then that
|
|
257
257
|
* the extension can sync (does not provide a directory -> `extension.not_found`, not enabled ->
|
|
258
|
-
* `extension.not_enabled`, its state cannot be read -> `
|
|
258
|
+
* `extension.not_enabled`, its state cannot be read (transient) -> 503 `directory.unavailable`, the extension says
|
|
259
259
|
* the directory is not configured -> `directory.not_configured`, Ruling A28; the effective config
|
|
260
260
|
* is not valid in the org -> `extension.needs_config`, layering spec A §6), and enqueues a sync
|
|
261
261
|
* job with a unique id.
|
|
@@ -271,8 +271,8 @@ let DirectorySync = class DirectorySync {
|
|
|
271
271
|
throw new ApiError("org.not_found", "Organization not found");
|
|
272
272
|
case "not_enabled":
|
|
273
273
|
throw new ApiError("extension.not_enabled", `Extension ${plugin} is not enabled for this organization`);
|
|
274
|
-
case "
|
|
275
|
-
throw new ApiError("
|
|
274
|
+
case "state_unreadable":
|
|
275
|
+
throw new ApiError("directory.unavailable", `Cannot read the state of extension ${plugin} for this organization right now; try again`);
|
|
276
276
|
case "not_configured":
|
|
277
277
|
throw new ApiError("directory.not_configured", `The directory of extension ${plugin} is not configured for this organization`);
|
|
278
278
|
case "needs_config":
|
|
@@ -512,11 +512,6 @@ export const PLATFORM_EXCLUSIONS = [
|
|
|
512
512
|
{ route: "GET /readyz", category: "transport", reason: "Readiness probe for the orchestrator" },
|
|
513
513
|
// models
|
|
514
514
|
// orgs
|
|
515
|
-
{
|
|
516
|
-
route: "PUT /api/v1/orgs/:orgId/extensions/:extension",
|
|
517
|
-
category: "internal",
|
|
518
|
-
reason: "The web's switch of an extension. For an extension shipped in one catalog plugin it is that plugin's switch, which plugins.set-enabled covers through the catalog switch route (with its uninstall preview). A direct-path extension (config-declared, or sourced from several catalog items) has no operation: an agent cannot switch it, a known gap (a required bootstrap plugin is on everywhere and has no switch); the org's config of any extension is its copy of the overridable fields (extensions.save-org-config)",
|
|
519
|
-
},
|
|
520
515
|
{
|
|
521
516
|
route: "POST /api/v1/orgs/:orgId/extensions/:extension/directory/sync",
|
|
522
517
|
category: "human-only",
|
|
@@ -86,6 +86,8 @@ export interface OperationError {
|
|
|
86
86
|
* `marketplaces.add` duplicate check is tool policy; the web accepts a duplicate source),
|
|
87
87
|
* `plugin.needs_input` (input only a person enters on the web), `agent.visibility_web_only`,
|
|
88
88
|
* `catalog.org_owned` (`plugins.set-shown` leaves an org's own plugin to that org),
|
|
89
|
+
* `extension.switch_via_plugin` (`extensions.set-enabled` leaves a catalog plugin's extension to
|
|
90
|
+
* `plugins.set-enabled`),
|
|
89
91
|
* `account.self` / `account.no_change` (disabling your own account, a status that is set already),
|
|
90
92
|
* and `platform.secret_record_web_only` (a secret record the card cannot ask for; ruling R1).
|
|
91
93
|
*/
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { type OnModuleInit } from "@nestjs/common";
|
|
2
|
+
import { OrgExtensionSwitch } from "../../catalog/org-extension-switch.js";
|
|
2
3
|
import { ExtensionDisplayNames } from "../../extensions/display-name.js";
|
|
3
4
|
import { OrgExtensionConfigs } from "../../extensions/org-config.js";
|
|
4
5
|
import { OrgOverrides, type OrgOverrideView } from "../../extensions/org-overrides.js";
|
|
@@ -28,7 +29,14 @@ export declare class ExtensionsOperations implements OnModuleInit {
|
|
|
28
29
|
private readonly switches;
|
|
29
30
|
private readonly pointDefaults;
|
|
30
31
|
private readonly overrides;
|
|
31
|
-
|
|
32
|
+
private readonly extSwitch;
|
|
33
|
+
constructor(registry: PlatformOperations, configs: OrgExtensionConfigs, displayNames: ExtensionDisplayNames, kdb: KernelDb, switches: OrgToolSwitches, pointDefaults: ExtensionPointDefaults, overrides: OrgOverrides, extSwitch: OrgExtensionSwitch);
|
|
34
|
+
/**
|
|
35
|
+
* Why `extensions.set-enabled` refuses the switch before writing, or null: an extension of one
|
|
36
|
+
* catalog plugin is switched through its plugin; a required or builtin one cannot go off. The
|
|
37
|
+
* write itself repeats every rule ({@link OrgExtensionSwitch.put}).
|
|
38
|
+
*/
|
|
39
|
+
private switchRefusal;
|
|
32
40
|
/** The org view of one extension, or `extension.not_found` (not installed or not offered). */
|
|
33
41
|
private viewOf;
|
|
34
42
|
/** An extension's display name for a card (its plugin's), else the name, clipped. */
|
|
@@ -7,10 +7,11 @@ 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 { EXTENSION_POINTS, parseExtensionPoint } from "@capybara-hq/plugin-sdk";
|
|
10
|
+
import { EXTENSION_POINTS, parseExtensionPoint, ToolError } from "@capybara-hq/plugin-sdk";
|
|
11
11
|
import { Inject, Injectable } from "@nestjs/common";
|
|
12
12
|
import { z } from "zod";
|
|
13
|
-
import { OrgExtensionParams, OrgExtensionToolParams, OrgOverridesPut, OrgOverridesTake, OrgToolSwitchPut, } from "../../catalog/org-extension.schemas.js";
|
|
13
|
+
import { OrgExtensionParams, OrgExtensionPut, OrgExtensionToolParams, OrgOverridesPut, OrgOverridesTake, OrgToolSwitchPut, } from "../../catalog/org-extension.schemas.js";
|
|
14
|
+
import { OrgExtensionSwitch } from "../../catalog/org-extension-switch.js";
|
|
14
15
|
import { ApiError } from "../../errors.js";
|
|
15
16
|
import { ExtensionDisplayNames } from "../../extensions/display-name.js";
|
|
16
17
|
import { OrgExtensionConfigs, providesOf, } from "../../extensions/org-config.js";
|
|
@@ -34,11 +35,19 @@ const NoInput = z.object({}).strict();
|
|
|
34
35
|
const Extension = OrgExtensionParams.shape.extension;
|
|
35
36
|
const Tool = OrgExtensionToolParams.shape.tool;
|
|
36
37
|
const SwitchConfirm = OrgToolSwitchPut.shape.confirm.unwrap();
|
|
38
|
+
const Force = OrgExtensionPut.shape.force;
|
|
39
|
+
/**
|
|
40
|
+
* Why `extensions.set-enabled` does not switch an extension that ships in one catalog plugin: that
|
|
41
|
+
* switch is the plugin's (with its uninstall preview), `plugins.set-enabled`.
|
|
42
|
+
*/
|
|
43
|
+
const viaPlugin = (extension, itemId) => `Extension ${extension} ships in a catalog plugin, whose switch it is: switch the plugin with plugins.set-enabled (itemId ${itemId}; switching off takes the confirm value of plugins.disable-preview).`;
|
|
37
44
|
/** The next step of an extension that needs its org config. */
|
|
38
45
|
const NEEDS_ORG_CONFIG = {
|
|
39
46
|
orgAdmin: "Read what is missing with extensions.org-config, then fill in the org's own copy of the overridable fields with extensions.save-org-config; the person enters secrets in a secure input request.",
|
|
40
47
|
member: "Ask an org admin to fill in its org config on the Extensions page.",
|
|
41
48
|
};
|
|
49
|
+
/** What `extensions.set-enabled` adds when the extension is on but its org config is not valid. */
|
|
50
|
+
const CONFIG_NEEDED = `The extension is on, but has no valid config for this org yet and gets no calls until it does: ${NEEDS_ORG_CONFIG.orgAdmin} If only platform-only fields are missing, the super admin must set them.`;
|
|
42
51
|
/**
|
|
43
52
|
* Secret fields of a target the schema walk cannot address (no form schema, a record): web only,
|
|
44
53
|
* so a value for one in `values` is refused as well.
|
|
@@ -90,7 +99,8 @@ let ExtensionsOperations = class ExtensionsOperations {
|
|
|
90
99
|
switches;
|
|
91
100
|
pointDefaults;
|
|
92
101
|
overrides;
|
|
93
|
-
|
|
102
|
+
extSwitch;
|
|
103
|
+
constructor(registry, configs, displayNames, kdb, switches, pointDefaults, overrides, extSwitch) {
|
|
94
104
|
this.registry = registry;
|
|
95
105
|
this.configs = configs;
|
|
96
106
|
this.displayNames = displayNames;
|
|
@@ -98,6 +108,32 @@ let ExtensionsOperations = class ExtensionsOperations {
|
|
|
98
108
|
this.switches = switches;
|
|
99
109
|
this.pointDefaults = pointDefaults;
|
|
100
110
|
this.overrides = overrides;
|
|
111
|
+
this.extSwitch = extSwitch;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Why `extensions.set-enabled` refuses the switch before writing, or null: an extension of one
|
|
115
|
+
* catalog plugin is switched through its plugin; a required or builtin one cannot go off. The
|
|
116
|
+
* write itself repeats every rule ({@link OrgExtensionSwitch.put}).
|
|
117
|
+
*/
|
|
118
|
+
async switchRefusal(orgId, extension, enabled) {
|
|
119
|
+
const v = await this.viewOf(orgId, extension);
|
|
120
|
+
const plan = await this.configs.switchPlan(extension, enabled);
|
|
121
|
+
if (plan.kind === "item") {
|
|
122
|
+
return { code: "extension.switch_via_plugin", message: viaPlugin(extension, plan.itemId) };
|
|
123
|
+
}
|
|
124
|
+
if (!enabled && v.required) {
|
|
125
|
+
return {
|
|
126
|
+
code: "extension.required",
|
|
127
|
+
message: `Extension ${extension} is required: it is on in every organization`,
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
if (!enabled && plan.kind === "locked") {
|
|
131
|
+
return {
|
|
132
|
+
code: "plugin.scope_locked",
|
|
133
|
+
message: `Extension ${extension} is built in: it is on in every organization`,
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
return null;
|
|
101
137
|
}
|
|
102
138
|
/** The org view of one extension, or `extension.not_found` (not installed or not offered). */
|
|
103
139
|
async viewOf(orgId, name) {
|
|
@@ -146,6 +182,96 @@ let ExtensionsOperations = class ExtensionsOperations {
|
|
|
146
182
|
}), startOf(input.cursor), 50);
|
|
147
183
|
},
|
|
148
184
|
}),
|
|
185
|
+
defineOperation({
|
|
186
|
+
id: "extensions.set-enabled",
|
|
187
|
+
description: "Switch an extension that is not a catalog plugin's on or off in this org (org admins): one declared in the deployment config, or one several catalog plugins bring. An extension shipped in one catalog plugin is switched with plugins.set-enabled; extensions.list shows its catalogItemId. Switching on never waits for config: an extension with no valid config for the org is switched on anyway and the result's configNeeded says how to fill in the org's copy (extensions.save-org-config). Switching off is refused while other enabled extensions depend on it, unless force is true (they lose what they depend on).",
|
|
188
|
+
input: z.object({ extension: Extension, enabled: z.boolean(), force: Force }).strict(),
|
|
189
|
+
example: { extension: "web-search-tavily", enabled: true },
|
|
190
|
+
authz: { orgAction: "org.extensions.manage" },
|
|
191
|
+
mirrors: ["PUT /api/v1/orgs/:orgId/extensions/:extension"],
|
|
192
|
+
// the confirm of a catalog plugin's switch-off: that switch is plugins.set-enabled's
|
|
193
|
+
omits: ["confirm"],
|
|
194
|
+
confirm: "org",
|
|
195
|
+
errors: [
|
|
196
|
+
{
|
|
197
|
+
code: "extension.not_found",
|
|
198
|
+
next: "The extension is not running or not visible to this org; check the name with extensions.list.",
|
|
199
|
+
},
|
|
200
|
+
{
|
|
201
|
+
code: "extension.switch_via_plugin",
|
|
202
|
+
next: "Switch its plugin with plugins.set-enabled (the itemId is extensions.list's catalogItemId).",
|
|
203
|
+
},
|
|
204
|
+
{
|
|
205
|
+
code: "extension.required",
|
|
206
|
+
next: "The extension is required on the platform and stays on in every org.",
|
|
207
|
+
},
|
|
208
|
+
{
|
|
209
|
+
code: "plugin.scope_locked",
|
|
210
|
+
next: "The extension's plugin is built in: it is on in every org and cannot be switched off in one org.",
|
|
211
|
+
},
|
|
212
|
+
{
|
|
213
|
+
code: "extension.dependency_missing",
|
|
214
|
+
next: "The extension needs an extension point no enabled extension provides in this org (the details name it); switch on a provider first (extensions.points, extensions.list).",
|
|
215
|
+
},
|
|
216
|
+
{
|
|
217
|
+
code: "extension.has_dependents",
|
|
218
|
+
next: "Other enabled extensions depend on it (the details name them): switch those off first, or call again with force: true after the person agreed that they lose it.",
|
|
219
|
+
},
|
|
220
|
+
{
|
|
221
|
+
code: "plugin.disabled",
|
|
222
|
+
next: {
|
|
223
|
+
superAdmin: "The plugin is stopped on the platform: turn it back on in the admin console under Plugins, then call again.",
|
|
224
|
+
orgAdmin: "The super admin stopped this plugin on the platform; ask them.",
|
|
225
|
+
member: "The super admin stopped this plugin on the platform; tell your org admin, who can ask the super admin.",
|
|
226
|
+
},
|
|
227
|
+
},
|
|
228
|
+
{
|
|
229
|
+
code: "catalog.approval_required",
|
|
230
|
+
next: "A plugin that brings the extension is not offered to this org: create an access request with requests.create.",
|
|
231
|
+
},
|
|
232
|
+
],
|
|
233
|
+
precheck: async (i, ctx) => {
|
|
234
|
+
const found = { refusal: null };
|
|
235
|
+
const r = await precheckBy(async () => {
|
|
236
|
+
found.refusal = await this.switchRefusal(ctx.orgId, i.extension, i.enabled);
|
|
237
|
+
});
|
|
238
|
+
if (!r.ok)
|
|
239
|
+
return r;
|
|
240
|
+
return found.refusal === null ? { ok: true } : { ok: false, ...found.refusal };
|
|
241
|
+
},
|
|
242
|
+
summary: async (i) => {
|
|
243
|
+
const x = await this.nameOf(i.extension);
|
|
244
|
+
if (i.enabled) {
|
|
245
|
+
return {
|
|
246
|
+
zh: `在本组织启用扩展「${x.zh}」`,
|
|
247
|
+
en: `Enable extension "${x.en}" in the org`,
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
return i.force === true
|
|
251
|
+
? {
|
|
252
|
+
zh: `在本组织强制停用扩展「${x.zh}」:依赖它的扩展将失去依赖`,
|
|
253
|
+
en: `Force-disable extension "${x.en}" in the org: extensions that depend on it lose it`,
|
|
254
|
+
}
|
|
255
|
+
: { zh: `在本组织停用扩展「${x.zh}」`, en: `Disable extension "${x.en}" in the org` };
|
|
256
|
+
},
|
|
257
|
+
run: async (input, ctx) => {
|
|
258
|
+
const refusal = await this.switchRefusal(ctx.orgId, input.extension, input.enabled);
|
|
259
|
+
if (refusal?.code === "extension.switch_via_plugin")
|
|
260
|
+
throw new ToolError(refusal.message);
|
|
261
|
+
const r = await this.extSwitch.put(ctx.orgId, ctx.actorAccountId, input.extension, { enabled: input.enabled, ...(input.force === true && { force: true }) }, undefined, viaAgent("extensions.set-enabled", ctx));
|
|
262
|
+
// unreachable: only a catalog plugin's switch-off answers a preview, refused above
|
|
263
|
+
if (r.kind === "preview")
|
|
264
|
+
throw new Error("extensions.set-enabled: unexpected preview");
|
|
265
|
+
const v = r.view;
|
|
266
|
+
return {
|
|
267
|
+
extension: v.plugin,
|
|
268
|
+
state: stateOf(v),
|
|
269
|
+
enabled: v.enabled,
|
|
270
|
+
needsConfig: v.needsConfig,
|
|
271
|
+
...(v.enabled && v.needsConfig && { configNeeded: CONFIG_NEEDED }),
|
|
272
|
+
};
|
|
273
|
+
},
|
|
274
|
+
}),
|
|
149
275
|
defineOperation({
|
|
150
276
|
id: "extensions.tools",
|
|
151
277
|
description: "List the tools an extension registers, each with this org's switch (on unless an org admin turned it off).",
|
|
@@ -538,6 +664,7 @@ ExtensionsOperations = __decorate([
|
|
|
538
664
|
__param(3, Inject(KernelDb)),
|
|
539
665
|
__param(4, Inject(OrgToolSwitches)),
|
|
540
666
|
__param(5, Inject(ExtensionPointDefaults)),
|
|
541
|
-
__param(6, Inject(OrgOverrides))
|
|
667
|
+
__param(6, Inject(OrgOverrides)),
|
|
668
|
+
__param(7, Inject(OrgExtensionSwitch))
|
|
542
669
|
], ExtensionsOperations);
|
|
543
670
|
export { ExtensionsOperations };
|
package/package.json
CHANGED