@capybara-hq/kernel 0.1.0-rc.61 → 0.1.0-rc.63

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/bootstrap.json +3 -3
  2. package/dist/app.d.ts +2 -0
  3. package/dist/catalog/catalog.service.d.ts +13 -0
  4. package/dist/catalog/catalog.service.js +17 -8
  5. package/dist/catalog/git-source.d.ts +8 -0
  6. package/dist/catalog/git-source.js +14 -6
  7. package/dist/catalog/marketplaces.controller.d.ts +3 -2
  8. package/dist/catalog/marketplaces.controller.js +5 -2
  9. package/dist/catalog/marketplaces.schemas.d.ts +7 -0
  10. package/dist/catalog/marketplaces.schemas.js +13 -0
  11. package/dist/catalog/marketplaces.service.d.ts +65 -11
  12. package/dist/catalog/marketplaces.service.js +208 -30
  13. package/dist/config/schema.d.ts +2 -0
  14. package/dist/config/schema.js +21 -1
  15. package/dist/conversations/attachments.controller.d.ts +11 -3
  16. package/dist/conversations/attachments.controller.js +18 -5
  17. package/dist/conversations/conversations.controller.d.ts +11 -2
  18. package/dist/conversations/conversations.controller.js +17 -4
  19. package/dist/conversations/conversations.schemas.d.ts +8 -0
  20. package/dist/conversations/conversations.schemas.js +6 -0
  21. package/dist/db/kernel-schema.d.ts +4 -0
  22. package/dist/errors.d.ts +7 -0
  23. package/dist/errors.js +7 -0
  24. package/dist/extensions/bootstrap-defaults.d.ts +24 -0
  25. package/dist/extensions/bootstrap-defaults.js +78 -0
  26. package/dist/extensions/bootstrap-extra.d.ts +74 -0
  27. package/dist/extensions/bootstrap-extra.js +131 -0
  28. package/dist/extensions/bootstrap.d.ts +77 -13
  29. package/dist/extensions/bootstrap.js +282 -48
  30. package/dist/identity/settings.d.ts +10 -0
  31. package/dist/identity/settings.js +46 -0
  32. package/dist/index.d.ts +2 -0
  33. package/dist/index.js +3 -0
  34. package/dist/kernel.module.js +3 -0
  35. package/dist/lifecycle/kernel-lifecycle.d.ts +7 -1
  36. package/dist/lifecycle/kernel-lifecycle.js +69 -16
  37. package/dist/platform/ops/marketplaces.ops.js +27 -4
  38. package/package.json +4 -4
  39. package/src/migrations/0174_marketplace_managed_username.sql +4 -0
@@ -11,6 +11,7 @@ import { PromptSuggestionQueue } from "../conversations/prompt-suggestions.js";
11
11
  import { TitleQueue } from "../conversations/titles.js";
12
12
  import { ExtensionBlobs } from "../extensions/blobs.js";
13
13
  import { BootstrapService } from "../extensions/bootstrap.js";
14
+ import { BootstrapDefaultsService } from "../extensions/bootstrap-defaults.js";
14
15
  import { ExtensionHost } from "../extensions/host.js";
15
16
  import { ExtensionInstalls } from "../extensions/installs.js";
16
17
  import { ExtensionLifecycle } from "../extensions/lifecycle.js";
@@ -80,6 +81,7 @@ export declare class KernelLifecycle implements OnApplicationBootstrap, BeforeAp
80
81
  private readonly extensionLifecycle;
81
82
  private readonly replicaStatus;
82
83
  private readonly bootstrap;
84
+ private readonly bootstrapDefaults;
83
85
  private readonly marketplaces;
84
86
  private readonly pluginImport;
85
87
  private readonly bus;
@@ -105,7 +107,9 @@ export declare class KernelLifecycle implements OnApplicationBootstrap, BeforeAp
105
107
  private bootstrapCarry;
106
108
  /** The packages the first bootstrap pass obtained, so the second downloads nothing again. */
107
109
  private bootstrapPackages;
108
- constructor(permissionConfig: PermissionConfigPublication, accounts: AccountsService, configService: KernelConfigService, log: KernelLogger, options: KernelAppOptions, pool: Kysely<any>, redis: Redis, kdb: KernelDb, shutdown: GracefulShutdown, host: ExtensionHost, registry: ExtensionRegistry, installs: ExtensionInstalls, blobs: ExtensionBlobs, extensionLifecycle: ExtensionLifecycle, replicaStatus: ReplicaStatusStore, bootstrap: BootstrapService, marketplaces: MarketplacesService, pluginImport: PluginImport, bus: Pick<ExtensionContext["bus"], "publish" | "subscribe">, usageReport: UsageReporter, titles: TitleQueue, suggestions: PromptSuggestionQueue, channelJobs: ChannelJobs, channelOutbound: ChannelOutbound, presence: ReplicaPresence, worker?: WorkerRole | undefined, gateway?: GatewayRole | undefined);
110
+ /** The merged bootstrap manifest, read once per start. */
111
+ private mergedManifest;
112
+ constructor(permissionConfig: PermissionConfigPublication, accounts: AccountsService, configService: KernelConfigService, log: KernelLogger, options: KernelAppOptions, pool: Kysely<any>, redis: Redis, kdb: KernelDb, shutdown: GracefulShutdown, host: ExtensionHost, registry: ExtensionRegistry, installs: ExtensionInstalls, blobs: ExtensionBlobs, extensionLifecycle: ExtensionLifecycle, replicaStatus: ReplicaStatusStore, bootstrap: BootstrapService, bootstrapDefaults: BootstrapDefaultsService, marketplaces: MarketplacesService, pluginImport: PluginImport, bus: Pick<ExtensionContext["bus"], "publish" | "subscribe">, usageReport: UsageReporter, titles: TitleQueue, suggestions: PromptSuggestionQueue, channelJobs: ChannelJobs, channelOutbound: ChannelOutbound, presence: ReplicaPresence, worker?: WorkerRole | undefined, gateway?: GatewayRole | undefined);
109
113
  /** The kernel HTTP application (set up by `buildKernelApp` before `init()`). */
110
114
  get http(): KernelHttpApp;
111
115
  attachHttp(http: KernelHttpApp): void;
@@ -122,6 +126,8 @@ export declare class KernelLifecycle implements OnApplicationBootstrap, BeforeAp
122
126
  * `/readyz`) after the start sequence; returns the actual port.
123
127
  */
124
128
  listen(port: number): Promise<number>;
129
+ /** The built-in manifest (or `options.bootstrapManifest`) merged with the extra manifest, if any. */
130
+ private manifest;
125
131
  /**
126
132
  * Runs the bootstrap step unless it is done for the manifest. A required entry that cannot be
127
133
  * completed (or a missing / tampered package: then nothing was installed) stops the start, so the
@@ -18,7 +18,9 @@ import { PromptSuggestionQueue } from "../conversations/prompt-suggestions.js";
18
18
  import { TitleQueue } from "../conversations/titles.js";
19
19
  import { migrate } from "../db/migrator.js";
20
20
  import { ExtensionBlobs } from "../extensions/blobs.js";
21
- import { BootstrapFailedError, BootstrapService, bootstrapManifestSha256, readBootstrapManifest, } from "../extensions/bootstrap.js";
21
+ import { BootstrapFailedError, BootstrapService, bootstrapManifestSha256, } from "../extensions/bootstrap.js";
22
+ import { BootstrapDefaultsService } from "../extensions/bootstrap-defaults.js";
23
+ import { BootstrapExtraMismatchError, hasExtraManifestTraces, loadBootstrapManifest, missingExtraManifestMessage, syncExtraMarketplaces, } from "../extensions/bootstrap-extra.js";
22
24
  import { ExtensionHost } from "../extensions/host.js";
23
25
  import { ExtensionInstalls } from "../extensions/installs.js";
24
26
  import { ExtensionLifecycle } from "../extensions/lifecycle.js";
@@ -100,6 +102,7 @@ let KernelLifecycle = class KernelLifecycle {
100
102
  extensionLifecycle;
101
103
  replicaStatus;
102
104
  bootstrap;
105
+ bootstrapDefaults;
103
106
  marketplaces;
104
107
  pluginImport;
105
108
  bus;
@@ -125,9 +128,11 @@ let KernelLifecycle = class KernelLifecycle {
125
128
  bootstrapCarry;
126
129
  /** The packages the first bootstrap pass obtained, so the second downloads nothing again. */
127
130
  bootstrapPackages;
131
+ /** The merged bootstrap manifest, read once per start. */
132
+ mergedManifest;
128
133
  constructor(permissionConfig, accounts, configService, log, options,
129
134
  // biome-ignore lint/suspicious/noExplicitAny: the pool carries no schema types
130
- pool, redis, kdb, shutdown, host, registry, installs, blobs, extensionLifecycle, replicaStatus, bootstrap, marketplaces, pluginImport, bus,
135
+ pool, redis, kdb, shutdown, host, registry, installs, blobs, extensionLifecycle, replicaStatus, bootstrap, bootstrapDefaults, marketplaces, pluginImport, bus,
131
136
  // the producers closed on every role (their consumers are the worker role's)
132
137
  usageReport, titles, suggestions, channelJobs, channelOutbound,
133
138
  // this process's presence beat, on every role (admin overview spec §5.1)
@@ -151,6 +156,7 @@ let KernelLifecycle = class KernelLifecycle {
151
156
  this.extensionLifecycle = extensionLifecycle;
152
157
  this.replicaStatus = replicaStatus;
153
158
  this.bootstrap = bootstrap;
159
+ this.bootstrapDefaults = bootstrapDefaults;
154
160
  this.marketplaces = marketplaces;
155
161
  this.pluginImport = pluginImport;
156
162
  this.bus = bus;
@@ -229,6 +235,25 @@ let KernelLifecycle = class KernelLifecycle {
229
235
  this.checkpoint();
230
236
  // official 清单来源按配置同步(幂等;每个副本启动时都做)
231
237
  await this.marketplaces.syncOfficialFromConfig();
238
+ // No extra manifest here while one left traces (config-managed marketplaces, or entries the
239
+ // last bootstrap run took from one): this service is configured differently from the one
240
+ // that wrote them. Syncing would demote them, and the bootstrap step
241
+ // would clear the extra entries' required / builtin under the built-in manifest's sha, which
242
+ // the correctly configured services then find done and never repair. So both are skipped
243
+ // (decided from the database before either runs); the start goes on with the state as it is
244
+ const traces = opts.bootstrap?.extraManifest === undefined
245
+ ? await this.bootstrap.extraManifestTraces()
246
+ : { marketplaces: [], plugins: [] };
247
+ const mismatch = hasExtraManifestTraces(traces);
248
+ if (mismatch) {
249
+ this.log.error(`${missingExtraManifestMessage(traces)}. The bootstrap step and the marketplace sync are skipped in this start`, { ...traces });
250
+ }
251
+ else {
252
+ // The extra manifest's marketplaces (an empty list demotes the config-managed rows). Without
253
+ // an extra manifest nothing is synced or demoted: the check above may have run before
254
+ // another replica's sync committed (ruling R16)
255
+ await syncExtraMarketplaces((m) => this.marketplaces.syncExtraFromConfig(m), opts.bootstrap !== undefined ? (await this.manifest()).extra?.marketplaces : undefined, this.log);
256
+ }
232
257
  // The bootstrap plugins (spec §7), once per manifest across replicas (the Redis lock
233
258
  // `lock:bootstrap`); a bootstrap entry named like a config-declared extension is kept
234
259
  if (opts.bootstrap !== undefined) {
@@ -236,7 +261,7 @@ let KernelLifecycle = class KernelLifecycle {
236
261
  ...(opts.resolveFrom !== undefined && { resolveFrom: opts.resolveFrom }),
237
262
  }));
238
263
  }
239
- const bootstrapAgain = opts.bootstrap !== undefined && (await this.runBootstrap(opts.bootstrap.dir));
264
+ const bootstrapAgain = opts.bootstrap !== undefined && !mismatch && (await this.runBootstrap(opts.bootstrap.dir));
240
265
  this.checkpoint();
241
266
  // 配置声明 → 安装记录(C15 / C13)。安装记录的包在本副本同步到 extensionHost.dir 后导入,
242
267
  // 与运行中的 reconcile 走同一条路径;迁移由一个副本持锁执行(ensureMigrated,C16)
@@ -288,6 +313,13 @@ let KernelLifecycle = class KernelLifecycle {
288
313
  await this.runBootstrap(opts.bootstrap.dir, true);
289
314
  this.checkpoint();
290
315
  }
316
+ // The extra manifest's defaults (the platform default theme), at every start whether or not
317
+ // the step ran in it (compose's `init` usually ran it): its themes exist only once the second
318
+ // pass imported them. A no-op once applied or once a super admin chose; never fails the start
319
+ if (opts.bootstrap !== undefined) {
320
+ await this.bootstrapDefaults.apply((await this.manifest()).extra?.defaultTheme);
321
+ this.checkpoint();
322
+ }
291
323
  if (opts.source?.watch) {
292
324
  // 热更新:重新校验;只应用 hot 字段,其余变化 warn 并列出需重启的键;校验失败保持原配置
293
325
  let reload = Promise.resolve();
@@ -322,6 +354,18 @@ let KernelLifecycle = class KernelLifecycle {
322
354
  await this.warnIfSetupPending();
323
355
  return this.listening;
324
356
  }
357
+ /** The built-in manifest (or `options.bootstrapManifest`) merged with the extra manifest, if any. */
358
+ manifest() {
359
+ this.mergedManifest ??= loadBootstrapManifest({
360
+ ...(this.options.bootstrapManifest !== undefined && {
361
+ builtin: this.options.bootstrapManifest,
362
+ }),
363
+ ...(this.options.bootstrap?.extraManifest !== undefined && {
364
+ extraManifest: this.options.bootstrap.extraManifest,
365
+ }),
366
+ });
367
+ return this.mergedManifest;
368
+ }
325
369
  /**
326
370
  * Runs the bootstrap step unless it is done for the manifest. A required entry that cannot be
327
371
  * completed (or a missing / tampered package: then nothing was installed) stops the start, so the
@@ -333,7 +377,7 @@ let KernelLifecycle = class KernelLifecycle {
333
377
  * them too; the final pass imports them, and one that fails is logged like an optional entry.
334
378
  */
335
379
  async runBootstrap(dir, final = false) {
336
- const manifest = this.options.bootstrapManifest ?? (await readBootstrapManifest());
380
+ const manifest = await this.manifest();
337
381
  const bootstrap = this.bootstrap;
338
382
  if (await bootstrap.isDone(bootstrapManifestSha256(manifest)))
339
383
  return false;
@@ -351,6 +395,14 @@ let KernelLifecycle = class KernelLifecycle {
351
395
  return false;
352
396
  }
353
397
  catch (e) {
398
+ // The step's own check under the lock (ruling R16): another replica's sync committed after
399
+ // this start's check. Nothing was written; the start goes on like an R14 mismatch
400
+ if (e instanceof BootstrapExtraMismatchError) {
401
+ this.log.error(`${e.message}. The bootstrap step is skipped in this start`, {
402
+ ...e.traces,
403
+ });
404
+ return false;
405
+ }
354
406
  if (e instanceof BootstrapFailedError && !e.requiredFailed) {
355
407
  this.bootstrapCarry = e.results;
356
408
  this.bootstrapPackages = e.packages;
@@ -501,18 +553,19 @@ KernelLifecycle = __decorate([
501
553
  __param(13, Inject(ExtensionLifecycle)),
502
554
  __param(14, Inject(ReplicaStatusStore)),
503
555
  __param(15, Inject(BootstrapService)),
504
- __param(16, Inject(MarketplacesService)),
505
- __param(17, Inject(PluginImport)),
506
- __param(18, Inject(KERNEL_BUS)),
507
- __param(19, Inject(UsageReporter)),
508
- __param(20, Inject(TitleQueue)),
509
- __param(21, Inject(PromptSuggestionQueue)),
510
- __param(22, Inject(ChannelJobs)),
511
- __param(23, Inject(ChannelOutbound)),
512
- __param(24, Inject(ReplicaPresence)),
513
- __param(25, Optional()),
514
- __param(25, Inject(WorkerRole)),
556
+ __param(16, Inject(BootstrapDefaultsService)),
557
+ __param(17, Inject(MarketplacesService)),
558
+ __param(18, Inject(PluginImport)),
559
+ __param(19, Inject(KERNEL_BUS)),
560
+ __param(20, Inject(UsageReporter)),
561
+ __param(21, Inject(TitleQueue)),
562
+ __param(22, Inject(PromptSuggestionQueue)),
563
+ __param(23, Inject(ChannelJobs)),
564
+ __param(24, Inject(ChannelOutbound)),
565
+ __param(25, Inject(ReplicaPresence)),
515
566
  __param(26, Optional()),
516
- __param(26, Inject(GatewayRole))
567
+ __param(26, Inject(WorkerRole)),
568
+ __param(27, Optional()),
569
+ __param(27, Inject(GatewayRole))
517
570
  ], KernelLifecycle);
518
571
  export { KernelLifecycle };
@@ -34,6 +34,8 @@ const source = (m) => ({
34
34
  form: m.form,
35
35
  ref: m.ref,
36
36
  builtin: m.builtin,
37
+ managed: m.managed,
38
+ username: m.username,
37
39
  enabled: m.enabled,
38
40
  disabledByConfig: m.disabledByConfig,
39
41
  lastStatus: m.lastStatus,
@@ -91,6 +93,13 @@ let MarketplacesOperations = class MarketplacesOperations {
91
93
  message: "The official marketplace can only be enabled or disabled",
92
94
  };
93
95
  }
96
+ if (m.managed === "config" && !onlyEnabled) {
97
+ return {
98
+ ok: false,
99
+ code: "marketplace.config_managed",
100
+ message: "This marketplace is configured by the deployment and can only be enabled or disabled",
101
+ };
102
+ }
94
103
  return { ok: true, source: m };
95
104
  }
96
105
  onModuleInit() {
@@ -151,8 +160,13 @@ let MarketplacesOperations = class MarketplacesOperations {
151
160
  }),
152
161
  defineOperation({
153
162
  id: "marketplaces.add",
154
- description: "Add a custom marketplace source to the platform (super admin): a name and an https URL of a marketplace.json, or a git repository (…/*.git or owner/repo) with an optional ref. Sources that need a token or mirrors are added on the web.",
155
- input: MarketplaceAddBody.pick({ name: true, url: true, ref: true }).strict(),
163
+ description: "Add a custom marketplace source to the platform (super admin): a name and an https URL of a marketplace.json, or a git repository (…/*.git or owner/repo) with an optional ref and the username its git token is sent with (empty: x-access-token). Sources that need a token or mirrors are added on the web.",
164
+ input: MarketplaceAddBody.pick({
165
+ name: true,
166
+ url: true,
167
+ ref: true,
168
+ username: true,
169
+ }).strict(),
156
170
  example: { name: "Team plugins", url: "https://plugins.example.com/marketplace.json" },
157
171
  authz: { superAdmin: true },
158
172
  mirrors: ["POST /api/v1/admin/marketplaces"],
@@ -200,6 +214,7 @@ let MarketplacesOperations = class MarketplacesOperations {
200
214
  name: input.name,
201
215
  url: input.url,
202
216
  ...(input.ref !== undefined && { ref: input.ref }),
217
+ ...(input.username !== undefined && { username: input.username }),
203
218
  }, { auditDetail: viaAgent("marketplaces.add", ctx) })),
204
219
  }),
205
220
  }),
@@ -232,7 +247,7 @@ let MarketplacesOperations = class MarketplacesOperations {
232
247
  }),
233
248
  defineOperation({
234
249
  id: "marketplaces.update",
235
- description: "Edit a marketplace source (super admin): name, url, form, ref (null: follow the remote HEAD), mirrors, enabled. The official one takes only enabled. A token is set on the web.",
250
+ description: "Edit a marketplace source (super admin): name, url, form, ref (null: follow the remote HEAD), mirrors, username (the git token's username; null: x-access-token), enabled. The official one and those configured by the deployment take only enabled. A token is set on the web.",
236
251
  input: z
237
252
  .object({
238
253
  marketplaceId: MarketplaceId,
@@ -252,6 +267,10 @@ let MarketplacesOperations = class MarketplacesOperations {
252
267
  code: "marketplace.builtin",
253
268
  next: "The official marketplace can only be enabled or disabled; it cannot be edited or removed.",
254
269
  },
270
+ {
271
+ code: "marketplace.config_managed",
272
+ next: "This source is configured by the deployment (its extra bootstrap manifest); it can only be enabled or disabled here. Whoever runs the deployment changes the rest.",
273
+ },
255
274
  {
256
275
  code: "validation_failed",
257
276
  next: "Fix the field the message names (https, no user info), then call again.",
@@ -283,7 +302,7 @@ let MarketplacesOperations = class MarketplacesOperations {
283
302
  }),
284
303
  defineOperation({
285
304
  id: "marketplaces.remove",
286
- description: "Remove a custom marketplace source (super admin): its entries and token go; plugins imported from it stay in the catalog. The official one cannot be removed (disable it with marketplaces.update).",
305
+ description: "Remove a custom marketplace source (super admin): its entries and token go; plugins imported from it stay in the catalog. The official one and those configured by the deployment cannot be removed (disable them with marketplaces.update).",
287
306
  input: z.object({ marketplaceId: MarketplaceId }).strict(),
288
307
  example: { marketplaceId: "3f1c2a9e-5b7d-4e21-9c0a-1d2e3f4a5b6c" },
289
308
  authz: { superAdmin: true },
@@ -296,6 +315,10 @@ let MarketplacesOperations = class MarketplacesOperations {
296
315
  code: "marketplace.builtin",
297
316
  next: "The official marketplace can only be enabled or disabled; it cannot be edited or removed.",
298
317
  },
318
+ {
319
+ code: "marketplace.config_managed",
320
+ next: "This source is configured by the deployment (its extra bootstrap manifest); it can only be enabled or disabled here. Whoever runs the deployment changes the rest.",
321
+ },
299
322
  ],
300
323
  precheck: async (i) => {
301
324
  const r = await this.editable(i.marketplaceId, false);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capybara-hq/kernel",
3
- "version": "0.1.0-rc.61",
3
+ "version": "0.1.0-rc.63",
4
4
  "description": "Capybara kernel: config, database, plugin host and HTTP API of the Capybara agent platform",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -66,7 +66,7 @@
66
66
  "yaml": "^2.9.1",
67
67
  "yauzl": "3.4.0",
68
68
  "zod": "^4.6.7",
69
- "@capybara-hq/plugin-sdk": "^0.1.0-rc.46"
69
+ "@capybara-hq/plugin-sdk": "^0.1.0-rc.48"
70
70
  },
71
71
  "devDependencies": {
72
72
  "@langchain/langgraph-checkpoint-validation": "^1.2.0",
@@ -75,8 +75,8 @@
75
75
  "@types/ws": "^8.18.2",
76
76
  "@types/yauzl": "3.4.0",
77
77
  "ws": "^8.22.0",
78
- "@capybara-hq/device": "0.1.0-rc.25",
79
- "@capybara-hq/testing": "0.1.0-rc.30"
78
+ "@capybara-hq/testing": "0.1.0-rc.31",
79
+ "@capybara-hq/device": "0.1.0-rc.25"
80
80
  },
81
81
  "scripts": {
82
82
  "build": "tsc -b"
@@ -0,0 +1,4 @@
1
+ -- Marketplaces owned by the deployment config (bootstrap.extraManifest; 'config', else NULL) and
2
+ -- the Basic auth username sent with a marketplace's git token (NULL: x-access-token).
3
+ ALTER TABLE marketplaces ADD COLUMN IF NOT EXISTS managed text NULL;
4
+ ALTER TABLE marketplaces ADD COLUMN IF NOT EXISTS username text NULL;