@capybara-hq/kernel 0.1.0-rc.84 → 0.1.0-rc.86

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 (157) hide show
  1. package/dist/admin/admin.module.js +5 -0
  2. package/dist/admin/health.service.d.ts +2 -2
  3. package/dist/admin/health.service.js +10 -2
  4. package/dist/catalog/admin-catalog.controller.d.ts +2 -1
  5. package/dist/catalog/admin-catalog.controller.js +2 -1
  6. package/dist/catalog/catalog-content.d.ts +5 -15
  7. package/dist/catalog/catalog-content.js +5 -56
  8. package/dist/catalog/catalog-scope.service.d.ts +3 -3
  9. package/dist/catalog/catalog-scope.service.js +4 -3
  10. package/dist/catalog/catalog.module.d.ts +3 -1
  11. package/dist/catalog/catalog.module.js +14 -1
  12. package/dist/catalog/component-scope.service.d.ts +6 -1
  13. package/dist/catalog/component-scope.service.js +16 -2
  14. package/dist/catalog/job-ids.d.ts +29 -0
  15. package/dist/catalog/job-ids.js +29 -0
  16. package/dist/catalog/jobs.d.ts +12 -3
  17. package/dist/catalog/jobs.js +28 -7
  18. package/dist/catalog/marketplaces.service.d.ts +1 -1
  19. package/dist/catalog/org-catalog.controller.d.ts +2 -30
  20. package/dist/catalog/org-catalog.controller.js +3 -29
  21. package/dist/catalog/org-catalog.schemas.d.ts +9 -46
  22. package/dist/catalog/org-catalog.schemas.js +9 -23
  23. package/dist/catalog/org-catalog.service.js +14 -23
  24. package/dist/catalog/plugin-import.d.ts +21 -0
  25. package/dist/catalog/plugin-import.js +33 -3
  26. package/dist/catalog/plugin-install.d.ts +69 -76
  27. package/dist/catalog/plugin-install.js +156 -234
  28. package/dist/catalog/plugin-upgrade.d.ts +71 -11
  29. package/dist/catalog/plugin-upgrade.js +222 -43
  30. package/dist/catalog/scope-sync.d.ts +22 -20
  31. package/dist/catalog/scope-sync.js +57 -42
  32. package/dist/catalog/template-connector-ensure.d.ts +100 -0
  33. package/dist/catalog/template-connector-ensure.js +285 -0
  34. package/dist/catalog/template-connector-jobs.d.ts +28 -0
  35. package/dist/catalog/template-connector-jobs.js +54 -0
  36. package/dist/catalog/template-connector-reconcile.d.ts +44 -0
  37. package/dist/catalog/template-connector-reconcile.js +114 -0
  38. package/dist/catalog/template-connectors.processor.d.ts +32 -0
  39. package/dist/catalog/template-connectors.processor.js +101 -0
  40. package/dist/channels/cards.d.ts +7 -3
  41. package/dist/channels/cards.js +9 -4
  42. package/dist/channels/phrases.d.ts +10 -0
  43. package/dist/channels/phrases.js +10 -0
  44. package/dist/config/docs.js +1 -1
  45. package/dist/config/schema.d.ts +3 -0
  46. package/dist/config/schema.js +18 -12
  47. package/dist/ctx/create-ctx.d.ts +10 -1
  48. package/dist/ctx/create-ctx.js +42 -2
  49. package/dist/db/kernel-schema.d.ts +2 -1
  50. package/dist/device-access/device-access.module.d.ts +7 -0
  51. package/dist/device-access/device-access.module.js +41 -0
  52. package/dist/device-access/device-access.service.d.ts +142 -0
  53. package/dist/device-access/device-access.service.js +464 -0
  54. package/dist/device-access/token.d.ts +6 -0
  55. package/dist/device-access/token.js +6 -0
  56. package/dist/device-access/viewer-routes.d.ts +45 -0
  57. package/dist/device-access/viewer-routes.js +81 -0
  58. package/dist/device-access/viewer.d.ts +244 -0
  59. package/dist/device-access/viewer.js +844 -0
  60. package/dist/dial-in/access.d.ts +19 -0
  61. package/dist/dial-in/access.js +23 -0
  62. package/dist/dial-in/gateway.d.ts +48 -6
  63. package/dist/dial-in/gateway.js +145 -10
  64. package/dist/dial-in/keys.d.ts +7 -0
  65. package/dist/dial-in/keys.js +7 -0
  66. package/dist/dial-in/routes.d.ts +11 -3
  67. package/dist/dial-in/routes.js +15 -4
  68. package/dist/dial-in/streams.d.ts +58 -0
  69. package/dist/dial-in/streams.js +283 -0
  70. package/dist/errors.d.ts +2 -0
  71. package/dist/errors.js +2 -0
  72. package/dist/extensions/host.d.ts +13 -1
  73. package/dist/extensions/host.js +20 -1
  74. package/dist/extensions/org-config.d.ts +6 -1
  75. package/dist/extensions/org-config.js +15 -2
  76. package/dist/extensions/permission-set.d.ts +15 -1
  77. package/dist/extensions/permission-set.js +13 -1
  78. package/dist/extensions/permission-view.d.ts +6 -4
  79. package/dist/extensions/permission-view.js +6 -1
  80. package/dist/git/gateway.js +3 -1
  81. package/dist/http/error.filter.js +2 -0
  82. package/dist/http/gateway-routes.d.ts +8 -4
  83. package/dist/http/gateway-routes.js +4 -3
  84. package/dist/infra/lock/lock.service.d.ts +5 -1
  85. package/dist/infra/lock/lock.service.js +10 -4
  86. package/dist/infra/outbox/registry.d.ts +28 -0
  87. package/dist/infra/outbox/registry.js +33 -0
  88. package/dist/infra/queue/queue-names.d.ts +1 -1
  89. package/dist/infra/queue/queue-names.js +2 -0
  90. package/dist/infra/throttler/named-throttlers.d.ts +3 -0
  91. package/dist/infra/throttler/named-throttlers.js +16 -0
  92. package/dist/kernel.module.js +3 -0
  93. package/dist/lifecycle/kernel-lifecycle.d.ts +8 -1
  94. package/dist/lifecycle/kernel-lifecycle.js +37 -11
  95. package/dist/orgs/org-owner.d.ts +7 -0
  96. package/dist/orgs/org-owner.js +16 -0
  97. package/dist/platform/operation.d.ts +3 -4
  98. package/dist/platform/ops/connectors.ops.js +8 -1
  99. package/dist/platform/ops/org-plugins.ops.d.ts +0 -9
  100. package/dist/platform/ops/org-plugins.ops.js +18 -235
  101. package/dist/platform/ops/plugins.ops.d.ts +0 -1
  102. package/dist/platform/ops/plugins.ops.js +3 -31
  103. package/dist/roles/gateway-role.d.ts +13 -6
  104. package/dist/roles/gateway-role.js +27 -9
  105. package/dist/roles/gateway.module.d.ts +2 -1
  106. package/dist/roles/gateway.module.js +13 -3
  107. package/dist/roles/worker-role.d.ts +9 -4
  108. package/dist/roles/worker-role.js +18 -5
  109. package/dist/roles/worker.module.d.ts +1 -1
  110. package/dist/roles/worker.module.js +8 -1
  111. package/dist/sandbox/device-services.d.ts +62 -0
  112. package/dist/sandbox/device-services.js +183 -0
  113. package/dist/sandbox/leases.d.ts +18 -1
  114. package/dist/sandbox/leases.js +75 -1
  115. package/dist/sandbox/network-grants.js +5 -2
  116. package/dist/sandbox/sandbox.module.d.ts +2 -1
  117. package/dist/sandbox/sandbox.module.js +5 -2
  118. package/dist/sandbox/sandbox.service.d.ts +44 -1
  119. package/dist/sandbox/sandbox.service.js +142 -5
  120. package/dist/setup/setup-guides.service.d.ts +9 -1
  121. package/dist/setup/setup-guides.service.js +38 -8
  122. package/dist/tools/admin-connectors.d.ts +3 -2
  123. package/dist/tools/admin-connectors.js +5 -4
  124. package/dist/tools/approvals.service.d.ts +45 -20
  125. package/dist/tools/approvals.service.js +19 -9
  126. package/dist/tools/call-scope.d.ts +60 -0
  127. package/dist/tools/call-scope.js +23 -0
  128. package/dist/tools/connector-setup.d.ts +28 -0
  129. package/dist/tools/connector-setup.js +33 -0
  130. package/dist/tools/connector-tools-jobs.d.ts +35 -0
  131. package/dist/tools/connector-tools-jobs.js +40 -0
  132. package/dist/tools/connector-tools.processor.d.ts +32 -0
  133. package/dist/tools/connector-tools.processor.js +97 -0
  134. package/dist/tools/connectors.controller.d.ts +1 -0
  135. package/dist/tools/connectors.schemas.d.ts +1 -0
  136. package/dist/tools/connectors.schemas.js +5 -0
  137. package/dist/tools/connectors.service.d.ts +138 -14
  138. package/dist/tools/connectors.service.js +410 -50
  139. package/dist/tools/credentials.d.ts +35 -0
  140. package/dist/tools/credentials.js +89 -1
  141. package/dist/tools/drift.d.ts +5 -0
  142. package/dist/tools/drift.js +21 -2
  143. package/dist/tools/editable-endpoint.d.ts +23 -0
  144. package/dist/tools/editable-endpoint.js +92 -0
  145. package/dist/tools/network-asks.d.ts +13 -0
  146. package/dist/tools/network-asks.js +26 -2
  147. package/dist/tools/oauth/flow.d.ts +1 -1
  148. package/dist/tools/oauth/flow.js +3 -2
  149. package/dist/tools/pipeline.d.ts +30 -1
  150. package/dist/tools/pipeline.js +299 -59
  151. package/dist/tools/questions.service.d.ts +24 -3
  152. package/dist/tools/questions.service.js +3 -1
  153. package/dist/tools/registry.d.ts +14 -2
  154. package/dist/tools/registry.js +27 -6
  155. package/dist/tools/tools.module.js +4 -0
  156. package/package.json +3 -3
  157. package/src/migrations/0183_connectors_created_by_nullable.sql +5 -0
@@ -11,7 +11,7 @@ import { Inject, Injectable } from "@nestjs/common";
11
11
  import { z } from "zod";
12
12
  import { AppearanceService } from "../../catalog/appearance.service.js";
13
13
  import { CatalogScopeService } from "../../catalog/catalog-scope.service.js";
14
- import { CatalogConnectorInput, CatalogItemPathParams, OrgPluginInstallBody, PluginInstallPathParams, } from "../../catalog/org-catalog.schemas.js";
14
+ import { CatalogItemPathParams, OrgPluginInstallBody, PluginInstallPathParams, } from "../../catalog/org-catalog.schemas.js";
15
15
  import { OrgCatalogService } from "../../catalog/org-catalog.service.js";
16
16
  import { OrgExtensionParams } from "../../catalog/org-extension.schemas.js";
17
17
  import { OrgPluginUploads } from "../../catalog/org-plugin-uploads.js";
@@ -22,102 +22,24 @@ import { ApiError } from "../../errors.js";
22
22
  import { KernelConfigService } from "../../infra/config/kernel-config.service.js";
23
23
  import { KernelDb } from "../../infra/database/kernel-db.js";
24
24
  import { ThemesService } from "../../themes/themes.service.js";
25
- import { clientProblem, headersWebOnly, templateConnectorInput, templatePersonFields, } from "../connector-person-fields.js";
26
25
  import { FileRefSchema } from "../file-ref.js";
27
- import { defineOperation, } from "../operation.js";
28
- import { personFieldParams } from "../person-fields.js";
26
+ import { defineOperation } from "../operation.js";
29
27
  import { PlatformOperations } from "../registry.js";
30
28
  import { appearanceSummary, reviewBrief } from "./catalog-admin.ops.js";
31
29
  import { precheckBy, viaAgent } from "./errors.js";
32
30
  import { Cursor, pageOf, startOf } from "./paging.js";
33
- import { afterTaken, takePersonInputs } from "./person-inputs.js";
34
31
  import { localized, named, orElse } from "./summary.js";
35
32
  const InstallId = PluginInstallPathParams.shape.installId.max(100);
36
33
  /** An org-owned plugin's catalog item id (`:id` of the owned-plugins routes). */
37
34
  const OwnedId = OrgOwnedPluginParams.shape.id;
38
35
  const EXAMPLE_ID = "3f1c2a9e-5b7d-4e21-9c0a-1d2e3f4a5b6c";
39
36
  const ItemId = CatalogItemPathParams.shape.itemId.max(100);
40
- /**
41
- * The model's input for one connector template (the install's and configure's `connectors` entry
42
- * without its secrets): name, slug, the endpoint fields the template lets the org edit, and an
43
- * OAuth client's id. The credential and the client secret are person fields.
44
- */
45
- const TemplateInput = CatalogConnectorInput.pick({ name: true, slug: true, endpoint: true })
46
- .extend({
47
- client: z
48
- .object({ clientId: z.string().min(1).max(500) })
49
- .strict()
50
- .optional(),
51
- })
52
- .strict();
53
- const Templates = z
54
- .record(z.string().min(1).max(100), TemplateInput)
55
- .refine((v) => Object.keys(v).length <= 50, { message: "Too many connector templates" });
56
37
  const InstallInput = z
57
38
  .object({
58
39
  itemId: ItemId,
59
- templates: Templates.optional().describe("Per connector template id (plugins.get lists them), the connector's name, slug, endpoint fields and OAuth client id; never a credential."),
60
- templatesOn: OrgPluginInstallBody.shape.templatesOn.describe("The connector template ids to switch on with the install (the web wizard's choose step): exactly these make connectors, the plugin's other templates are switched off in the org and stay available on the connectors page. Omit to take the templates on in the org now (default-on ones, and default-off ones an admin switched on)."),
61
- askPerson: personFieldParams.askPerson,
62
- })
63
- .strict();
64
- const ConfigureInput = z
65
- .object({
66
- itemId: ItemId,
67
- templates: Templates.describe("Per missing connector template id (plugins.get shows the plugin needs_config), the connector's name, slug, endpoint fields and OAuth client id; never a credential. Name every template to create, even with {}."),
68
- askPerson: personFieldParams.askPerson,
40
+ templatesOn: OrgPluginInstallBody.shape.templatesOn.describe("The connector template ids to switch on with the install: exactly these make connectors, the plugin's other templates are switched off in the org and stay available on the connectors page. Omit to take the templates on in the org now (default-on ones, and default-off ones an admin switched on)."),
69
41
  })
70
42
  .strict();
71
- /** The card's person-field paths, from the plan (`personAsk`). */
72
- const entersText = (ctx) => {
73
- const ask = ctx.personAsk ?? [];
74
- return ask.length === 0
75
- ? { zh: "", en: "" }
76
- : { zh: `;由本人填写 ${ask.join("、")}`, en: `; the person enters ${ask.join(", ")}` };
77
- };
78
- /** A template as the person-field helpers take it. */
79
- const targetOf = (t) => ({
80
- id: t.id,
81
- name: {
82
- zh: named(t.displayName?.zh ?? t.displayName?.en ?? t.id),
83
- en: named(t.displayName?.en ?? t.id),
84
- },
85
- auth: t.auth,
86
- });
87
- /**
88
- * The refusal of a template input the card cannot complete, or ok: a credential the card cannot
89
- * ask for (custom headers), an OAuth client without its id, or a client for a template that takes
90
- * none.
91
- */
92
- function templatesRefusal(templates, params) {
93
- for (const t of templates) {
94
- const where = `Connector template ${t.name.en} (${t.id})`;
95
- const why = headersWebOnly(t.auth, where);
96
- if (why !== null)
97
- return { ok: false, code: "platform.secret_record_web_only", message: why };
98
- const client = clientProblem(t.auth, params?.[t.id]?.client, where);
99
- if (client !== null)
100
- return { ok: false, code: "validation_failed", message: client };
101
- }
102
- return { ok: true };
103
- }
104
- /** The `connectors` body of the install and configure routes: params plus entered values. */
105
- function connectorInputs(templates, params, entered) {
106
- const out = {};
107
- for (const t of templates) {
108
- const given = params?.[t.id];
109
- const hasEntered = Object.keys(entered).some((p) => p.startsWith(`connectors.${t.id}.`));
110
- if (given === undefined && !hasEntered)
111
- continue;
112
- out[t.id] = templateConnectorInput(t, given, entered);
113
- }
114
- // a template the plan does not know (not on here) goes to the service as given: it refuses it
115
- for (const [id, given] of Object.entries(params ?? {})) {
116
- if (!(id in out) && !templates.some((t) => t.id === id))
117
- out[id] = { ...given };
118
- }
119
- return Object.keys(out).length === 0 ? undefined : out;
120
- }
121
43
  /** One of the org's plugin installs as `plugins.installs` shows it: a compact row. */
122
44
  const installRow = (i) => ({
123
45
  id: i.id,
@@ -176,41 +98,6 @@ let OrgPluginsOperations = class OrgPluginsOperations {
176
98
  this.themes = themes;
177
99
  this.config = config;
178
100
  }
179
- /**
180
- * What installing a plugin asks the person for (platform web parity spec §7.2): the connector
181
- * templates whose connector the install creates (on in the org, none kept from an earlier
182
- * switch-off, as the install wizard asks). The extension's config for the org is its copy of the
183
- * overridable fields, saved apart (`extensions.save-org-config`).
184
- */
185
- async installTargets(ctx, itemId, templatesOn) {
186
- const d = await this.orgCatalog.detail(ctx.orgId, ctx.actorAccountId, itemId);
187
- const templates = d.templates
188
- .filter((t) => (templatesOn === undefined ? t.on : templatesOn.includes(t.id)))
189
- .filter((t) => !t.connected)
190
- .map(targetOf);
191
- return {
192
- item: d.item,
193
- templates,
194
- target: {
195
- target: {
196
- zh: `插件「${localized(d.item.displayName, d.item.name).zh}」的安装`,
197
- en: `${localized(d.item.displayName, d.item.name).en}: install`,
198
- },
199
- fields: templatePersonFields(templates),
200
- recordOnly: [],
201
- },
202
- };
203
- }
204
- /** The missing connectors of an installed plugin that a configure call names. */
205
- async configureTargets(ctx, itemId, named) {
206
- const missing = await this.installer.missingConnectors(ctx.orgId, itemId);
207
- if (missing === undefined) {
208
- throw new ApiError("plugin.install_not_found", "The plugin is not installed in this org");
209
- }
210
- return missing.templates
211
- .filter((t) => named.includes(t.id))
212
- .map((t) => targetOf({ id: t.id, displayName: t.file.displayName, auth: t.file.template.auth }));
213
- }
214
101
  /** The display name of the live plugin of `extension`, or the name when it cannot be read. */
215
102
  pluginNames(extension) {
216
103
  return orElse(async () => {
@@ -584,20 +471,13 @@ let OrgPluginsOperations = class OrgPluginsOperations {
584
471
  }),
585
472
  defineOperation({
586
473
  id: "plugins.install",
587
- description: "Install a plugin in this org with its inputs (org admins; the web's install wizard): per connector template the connector's name, slug, endpoint fields and OAuth client id (templates). Secrets are never params: the person enters them in a secure input request, at connectors.<templateId>.credential.token (or .credential.username / .credential.password, .client.clientSecret). The extension's config for the org is its copy of the overridable fields (extensions.save-org-config). Required ones are asked even when not listed in askPerson. A plugin that needs no input is switched on with plugins.set-enabled instead.",
474
+ description: "Install a plugin in this org (org admins), optionally choosing which of its connector templates to switch on (templatesOn). It asks for no input: each connector template on becomes a connector, and one that needs an org credential or OAuth client waits as needs_config until an org admin configures it on the connectors page (connectors.list shows setup). The extension's config for the org is its copy of the overridable fields (extensions.save-org-config). plugins.set-enabled switches a plugin on with the templates on in the org now.",
588
475
  input: InstallInput,
589
- example: {
590
- itemId: EXAMPLE_ID,
591
- templates: { github: { endpoint: { url: "https://api.githubcopilot.com/mcp/" } } },
592
- askPerson: ["connectors.github.credential.token"],
593
- },
476
+ example: { itemId: EXAMPLE_ID },
594
477
  // the route needs catalog.read; the service re-checks the plugin's component actions
595
478
  authz: { orgAction: "org.extensions.manage" },
596
479
  authzNote: "service-enforced",
597
480
  mirrors: ["POST /api/v1/orgs/:orgId/catalog/:itemId/install"],
598
- // the route's connectors carry credentials: templates holds the rest, secrets are person fields
599
- omits: ["connectors"],
600
- extras: ["templates", "askPerson"],
601
481
  confirm: "org",
602
482
  errors: [
603
483
  { code: "catalog.item_not_found", next: "Check the plugin id with plugins.list." },
@@ -607,7 +487,7 @@ let OrgPluginsOperations = class OrgPluginsOperations {
607
487
  },
608
488
  {
609
489
  code: "plugin.install_failed",
610
- next: "The install failed at the step its details name and was rolled back; fix that (often the connector's endpoint or credential) and call again: the person enters the credentials again on a new card.",
490
+ next: "The install failed at the step its details name and was rolled back; tell the person what failed.",
611
491
  },
612
492
  {
613
493
  code: "catalog.not_deployed",
@@ -615,126 +495,29 @@ let OrgPluginsOperations = class OrgPluginsOperations {
615
495
  },
616
496
  {
617
497
  code: "validation_failed",
618
- next: "The error's details name the fields (connectors.<templateId>.…); correct the params and call again.",
619
- },
620
- {
621
- code: "platform.secret_record_web_only",
622
- next: "Tell the person to finish this on the Plugins page on the web.",
498
+ next: "The error's details name the field (templatesOn); correct the params and call again.",
623
499
  },
624
500
  ],
625
- precheck: async (i, ctx) => {
626
- let r;
627
- const read = await precheckBy(async () => {
628
- r = await this.installTargets(ctx, i.itemId, i.templatesOn);
629
- });
630
- if (!read.ok || r === undefined)
631
- return read;
632
- if (r.item.installed !== null) {
633
- return {
634
- ok: false,
635
- code: "plugin.already_installed",
636
- message: `Plugin ${r.item.name} is installed in this org already`,
637
- };
501
+ precheck: async (i, ctx) => precheckBy(async () => {
502
+ const d = await this.orgCatalog.detail(ctx.orgId, ctx.actorAccountId, i.itemId);
503
+ if (d.item.installed !== null) {
504
+ throw new ApiError("plugin.already_installed", `Plugin ${d.item.name} is installed in this org already`);
638
505
  }
639
- return templatesRefusal(r.templates, i.templates);
640
- },
641
- personFields: async (i, ctx) => (await this.installTargets(ctx, i.itemId, i.templatesOn)).target,
506
+ }),
642
507
  summary: async (i, ctx) => {
643
- const { item, templates } = await this.installTargets(ctx, i.itemId, i.templatesOn);
644
- const n = localized(item.displayName, item.name);
645
- const e = entersText(ctx);
508
+ const d = await this.orgCatalog.detail(ctx.orgId, ctx.actorAccountId, i.itemId);
509
+ const n = localized(d.item.displayName, d.item.name);
510
+ const templates = d.templates.filter((t) => i.templatesOn === undefined ? t.on : i.templatesOn.includes(t.id)).length;
646
511
  return {
647
- zh: `在本组织安装插件「${n.zh}」${item.latestVersion}(${templates.length} 个连接器)${e.zh}`,
648
- en: `Install plugin "${n.en}" ${item.latestVersion} in the org (${templates.length} connectors)${e.en}`,
512
+ zh: `在本组织安装插件「${n.zh}」${d.item.latestVersion}(${templates} 个连接器)`,
513
+ en: `Install plugin "${n.en}" ${d.item.latestVersion} in the org (${templates} connectors)`,
649
514
  };
650
515
  },
651
516
  run: async (input, ctx) => {
652
- const { templates } = await this.installTargets(ctx, input.itemId, input.templatesOn);
653
- // the install runs steps in transactions of their own under the org row lock, so the
654
- // values are taken first (takePersonInputs)
655
- const { entered, auditDetail } = await takePersonInputs("plugins.install", ctx);
656
- const connectors = connectorInputs(templates, input.templates, entered);
657
- const r = await this.orgCatalog
658
- .install(ctx.orgId, ctx.actorAccountId, input.itemId, {
659
- ...(connectors !== undefined && { connectors }),
660
- ...(input.templatesOn !== undefined && { templatesOn: input.templatesOn }),
661
- }, undefined, auditDetail)
662
- .catch((e) => {
663
- throw afterTaken(e, entered);
664
- });
517
+ const r = await this.orgCatalog.install(ctx.orgId, ctx.actorAccountId, input.itemId, input.templatesOn !== undefined ? { templatesOn: input.templatesOn } : {}, undefined, viaAgent("plugins.install", ctx));
665
518
  return { installed: r.installed, install: installRow(r.install) };
666
519
  },
667
520
  }),
668
- defineOperation({
669
- id: "plugins.configure-connectors",
670
- description: "Create the connectors an installed plugin is missing (org admins; the plugin shows needs_config, e.g. after a platform update added a connector template): per missing template its name, slug, endpoint fields and OAuth client id (templates). Credentials are never params: the person enters them in a secure input request at connectors.<templateId>.credential.token (or .credential.username / .credential.password, .client.clientSecret). Required ones are asked even when not listed in askPerson.",
671
- input: ConfigureInput,
672
- example: {
673
- itemId: EXAMPLE_ID,
674
- templates: { github: {} },
675
- askPerson: ["connectors.github.credential.token"],
676
- },
677
- authz: { orgAction: "connectors.manage" },
678
- mirrors: ["POST /api/v1/orgs/:orgId/catalog/:itemId/connectors"],
679
- // the route's inputs.connectors carry credentials: templates holds the rest
680
- omits: ["inputs"],
681
- extras: ["templates", "askPerson"],
682
- confirm: "org",
683
- errors: [
684
- {
685
- code: "plugin.install_not_found",
686
- next: "The plugin is not installed in this org; install it with plugins.install.",
687
- },
688
- {
689
- code: "validation_failed",
690
- next: "The error's details name the fields (connectors.<templateId>.…); correct the params and call again.",
691
- },
692
- {
693
- code: "platform.secret_record_web_only",
694
- next: "Tell the person to finish this on the Plugins page on the web.",
695
- },
696
- ],
697
- precheck: async (i, ctx) => {
698
- let templates = [];
699
- const read = await precheckBy(async () => {
700
- templates = await this.configureTargets(ctx, i.itemId, Object.keys(i.templates));
701
- });
702
- return read.ok ? templatesRefusal(templates, i.templates) : read;
703
- },
704
- personFields: async (i, ctx) => {
705
- const templates = await this.configureTargets(ctx, i.itemId, Object.keys(i.templates));
706
- return {
707
- target: { zh: "插件缺少的连接器", en: "Plugin: missing connectors" },
708
- fields: templatePersonFields(templates),
709
- };
710
- },
711
- summary: async (i, ctx) => {
712
- const n = await orElse(async () => {
713
- const d = await this.orgCatalog.detail(ctx.orgId, ctx.actorAccountId, i.itemId);
714
- return localized(d.item.displayName, d.item.name);
715
- }, { zh: named(i.itemId), en: named(i.itemId) });
716
- const ids = Object.keys(i.templates).map(named).join(", ");
717
- const e = entersText(ctx);
718
- return {
719
- zh: `为插件「${n.zh}」创建缺少的连接器 ${ids}${e.zh}`,
720
- en: `Create the missing connectors ${ids} of plugin "${n.en}"${e.en}`,
721
- };
722
- },
723
- run: async (input, ctx) => {
724
- const templates = await this.configureTargets(ctx, input.itemId, Object.keys(input.templates));
725
- // each connector is created and recorded in transactions of its own (as the route)
726
- const { entered, auditDetail } = await takePersonInputs("plugins.configure-connectors", ctx);
727
- return {
728
- connectors: await this.installer
729
- .configureConnectors(ctx.orgId, ctx.actorAccountId, input.itemId, {
730
- connectors: connectorInputs(templates, input.templates, entered) ?? {},
731
- }, { auditDetail })
732
- .catch((e) => {
733
- throw afterTaken(e, entered);
734
- }),
735
- };
736
- },
737
- }),
738
521
  ]);
739
522
  }
740
523
  };
@@ -8,7 +8,6 @@ import { ExtensionAvailability } from "../../extensions/unavailable.js";
8
8
  import { KernelDb } from "../../infra/database/kernel-db.js";
9
9
  import { PlatformOperations } from "../registry.js";
10
10
  export { OTHER_ORG } from "./other-orgs.js";
11
- export declare const NEEDS_INPUT = "This plugin needs connector input (endpoints, credentials): install it with plugins.install, or for an installed plugin missing connectors create them with plugins.configure-connectors; the person enters the credentials in a secure input request.";
12
11
  /**
13
12
  * What `plugins.set-enabled` adds to its result when the plugin is on but its extension's
14
13
  * effective config for the org is not valid yet (switching on never waits for it, as on the web).
@@ -40,7 +40,6 @@ export { OTHER_ORG } from "./other-orgs.js";
40
40
  const LOCKED = "This plugin is on for every org (or shipped with the platform) and cannot be switched off in one org.";
41
41
  /** The confirm value of switching off a plugin with nothing installed in the org. */
42
42
  const NOTHING_INSTALLED = "nothing-installed";
43
- export const NEEDS_INPUT = "This plugin needs connector input (endpoints, credentials): install it with plugins.install, or for an installed plugin missing connectors create them with plugins.configure-connectors; the person enters the credentials in a secure input request.";
44
43
  /**
45
44
  * What `plugins.set-enabled` adds to its result when the plugin is on but its extension's
46
45
  * effective config for the org is not valid yet (switching on never waits for it, as on the web).
@@ -84,19 +83,6 @@ const row = (i, role) => ({
84
83
  switchable: i.switchable,
85
84
  request: i.request,
86
85
  });
87
- /**
88
- * Why switching the plugin on needs input first, as the `plugin.needs_input` message, or null:
89
- * connector input only (an installed plugin whose connector template misses its connector, or a
90
- * plugin not installed yet that has connector templates: `plugins.install`,
91
- * `plugins.configure-connectors`). Its extension's config never blocks the switch, as on the web
92
- * and the route: the plugin goes on and its extension stays `needsConfig` until the org's copy
93
- * (or the platform config) is valid ({@link configStillNeeded}).
94
- */
95
- function needsInput(item) {
96
- if (item.installed !== null)
97
- return item.state === "needs_config" ? NEEDS_INPUT : null;
98
- return item.componentCounts.connectorTemplates > 0 ? NEEDS_INPUT : null;
99
- }
100
86
  /** The note for a plugin switched on whose extension's effective config is not valid, or null. */
101
87
  function configStillNeeded(item, extensions) {
102
88
  const own = extensions.find((e) => e.catalogItem?.itemId === item.id);
@@ -213,7 +199,7 @@ let PluginsOperations = class PluginsOperations {
213
199
  }),
214
200
  defineOperation({
215
201
  id: "plugins.set-enabled",
216
- description: "Switch a plugin on or off in this org. Switching on installs it; a plugin whose enabling needs connector input is refused: install it with plugins.install instead. A plugin whose extension has no valid config for the org yet is switched on anyway, and the result's configNeeded says how to fill in the org's copy (extensions.save-org-config). Switching off removes what the plugin brought and needs confirm: the value plugins.disable-preview returned with the preview the person saw.",
202
+ description: "Switch a plugin on or off in this org. Switching on installs it without asking for input: each connector template on becomes a connector, and one that needs an org credential or OAuth client waits as needs_config until an org admin configures it on the connectors page. A plugin whose extension has no valid config for the org yet is switched on anyway, and the result's configNeeded says how to fill in the org's copy (extensions.save-org-config). Switching off removes what the plugin brought and needs confirm: the value plugins.disable-preview returned with the preview the person saw.",
217
203
  input: z.discriminatedUnion("enabled", [
218
204
  z.object({ itemId: ItemId, enabled: z.literal(true) }).strict(),
219
205
  z.object({ itemId: ItemId, enabled: z.literal(false), confirm: Confirm }).strict(),
@@ -224,8 +210,8 @@ let PluginsOperations = class PluginsOperations {
224
210
  authz: { orgAction: "org.extensions.manage" },
225
211
  authzNote: "service-enforced",
226
212
  mirrors: ["PUT /api/v1/orgs/:orgId/catalog/:itemId/switch"],
213
+ // the route's inputs choose the connector templates: plugins.install takes that choice
227
214
  omits: ["inputs"],
228
- withoutSecrets: "a plugin that needs install inputs is refused here; plugins.install takes them, its secrets entered by the person on the card",
229
215
  loosens: {
230
216
  confirm: "also takes `nothing-installed` (plugins.disable-preview's value when nothing is installed), which the route's hash pattern rejects; the service checks confirm only when something is installed",
231
217
  },
@@ -251,10 +237,6 @@ let PluginsOperations = class PluginsOperations {
251
237
  code: "catalog.item_not_found",
252
238
  next: "Check the plugin id with plugins.list.",
253
239
  },
254
- {
255
- code: "plugin.needs_input",
256
- next: "The plugin needs connector input: install it with plugins.install (or plugins.configure-connectors once installed), the person entering the credentials on the card.",
257
- },
258
240
  {
259
241
  code: "catalog.approval_required",
260
242
  next: "The plugin is not offered to this org: create an access request with requests.create.",
@@ -273,8 +255,7 @@ let PluginsOperations = class PluginsOperations {
273
255
  },
274
256
  ],
275
257
  // what is known to fail before the card: an unknown plugin, switching off a locked one,
276
- // switching on one whose extension cannot run (with the role's next step naming it) or
277
- // that needs a person's input
258
+ // switching on one whose extension cannot run (with the role's next step naming it)
278
259
  precheck: async (i, ctx) => {
279
260
  const [item] = UUID_RE.test(i.itemId)
280
261
  ? await this.orgCatalog.list(ctx.orgId, ctx.actorAccountId, i.itemId)
@@ -294,9 +275,6 @@ let PluginsOperations = class PluginsOperations {
294
275
  next: unavailableNext(item.reason, name),
295
276
  };
296
277
  }
297
- const missing = i.enabled ? needsInput(item) : null;
298
- if (missing !== null)
299
- return { ok: false, code: "plugin.needs_input", message: missing };
300
278
  return { ok: true };
301
279
  },
302
280
  summary: async (i, ctx) => {
@@ -322,12 +300,6 @@ let PluginsOperations = class PluginsOperations {
322
300
  };
323
301
  },
324
302
  run: async (input, ctx) => {
325
- if (input.enabled) {
326
- const [item] = await this.orgCatalog.list(ctx.orgId, ctx.actorAccountId, input.itemId);
327
- const missing = item ? needsInput(item) : null;
328
- if (missing !== null)
329
- throw new ToolError(missing);
330
- }
331
303
  const r = await this.orgCatalog.setSwitch(ctx.orgId, ctx.actorAccountId, input.itemId, input.enabled ? { enabled: true } : { enabled: false, confirm: input.confirm }, undefined, viaAgent("plugins.set-enabled", ctx));
332
304
  if (r.kind === "done") {
333
305
  const configNeeded = input.enabled
@@ -1,15 +1,18 @@
1
1
  import type { ExtensionContext } from "@capybara-hq/plugin-sdk";
2
2
  import { ChannelSupervisor } from "../channels/supervisor.js";
3
3
  import { ChannelWebhooks } from "../channels/webhook.js";
4
+ import { DeviceStreamViewers } from "../device-access/viewer.js";
4
5
  import { DialInGateway } from "../dial-in/gateway.js";
5
6
  import { ExtensionHost } from "../extensions/host.js";
6
7
  import type { GatewayRoutes } from "../http/gateway-routes.js";
7
8
  import { KernelConfigService } from "../infra/config/kernel-config.service.js";
9
+ import { CredentialThrottler } from "../infra/throttler/credential-throttler.js";
8
10
  /**
9
11
  * The gateway role's parts, driven by `KernelLifecycle` at fixed points of its start order and
10
12
  * stop phases: the channel long-connection supervisor (leader election and share) and the webhook
11
13
  * inbound (its raw Fastify route, {@link routes}, and its runtime cache's invalidation), and the
12
- * device connections ({@link DialInGateway}, `GET /device/connect`). Provided
14
+ * device connections ({@link DialInGateway}, `GET /device/connect`) and the device stream viewers
15
+ * ({@link DeviceStreamViewers}, `GET /device/stream`). Provided
13
16
  * only by `GatewayModule`, so a process without the gateway role has none of them.
14
17
  */
15
18
  export declare class GatewayRole {
@@ -19,6 +22,8 @@ export declare class GatewayRole {
19
22
  private readonly supervisor;
20
23
  private readonly webhooks;
21
24
  private readonly devices;
25
+ private readonly viewers;
26
+ private readonly throttler;
22
27
  /** The supervisor's subscription to extension instance changes. */
23
28
  private unwatchPlugins;
24
29
  private unwatchWebhooks;
@@ -26,10 +31,10 @@ export declare class GatewayRole {
26
31
  private unsubscribeWebhooks;
27
32
  /** The supervisor was started: the runs phase stops it. */
28
33
  private supervisorStarted;
29
- constructor(config: KernelConfigService, host: ExtensionHost, bus: Pick<ExtensionContext["bus"], "subscribe">, supervisor: ChannelSupervisor, webhooks: ChannelWebhooks, devices: DialInGateway);
34
+ constructor(config: KernelConfigService, host: ExtensionHost, bus: Pick<ExtensionContext["bus"], "subscribe">, supervisor: ChannelSupervisor, webhooks: ChannelWebhooks, devices: DialInGateway, viewers: DeviceStreamViewers, throttler: CredentialThrottler);
30
35
  /**
31
- * The gateway role's HTTP routes (the channel webhook, the device connection), registered when
32
- * the app is built.
36
+ * The gateway role's HTTP routes (the channel webhook, the device connection, the device stream
37
+ * viewer), registered when the app is built.
33
38
  */
34
39
  routes(): GatewayRoutes;
35
40
  /**
@@ -46,8 +51,10 @@ export declare class GatewayRole {
46
51
  /** Ends the webhook cache's `channel.changed` subscription. */
47
52
  unsubscribe(): Promise<void>;
48
53
  /**
49
- * Closes the device connections (1001) and hands their leases back, before the HTTP server
50
- * closes (the machines reconnect to another gateway replica).
54
+ * Closes the device stream viewers (1001: the panels reconnect with a new ticket), then the
55
+ * device connections (1001) and hands their leases back, before the HTTP server closes (the
56
+ * machines reconnect to another gateway replica). Viewers first, so a viewer of a local device
57
+ * learns of the shutdown rather than of its machine going offline.
51
58
  */
52
59
  stopDevices(): Promise<void>;
53
60
  /**
@@ -11,15 +11,19 @@ import { Inject, Injectable } from "@nestjs/common";
11
11
  import { CHANNEL_CHANGED_TOPIC } from "../channels/connections.service.js";
12
12
  import { ChannelSupervisor } from "../channels/supervisor.js";
13
13
  import { ChannelWebhooks } from "../channels/webhook.js";
14
+ import { DeviceStreamViewers } from "../device-access/viewer.js";
15
+ import { viewerThrottleOn } from "../device-access/viewer-routes.js";
14
16
  import { DialInGateway } from "../dial-in/gateway.js";
15
17
  import { ExtensionHost } from "../extensions/host.js";
16
18
  import { KernelConfigService } from "../infra/config/kernel-config.service.js";
19
+ import { CredentialThrottler } from "../infra/throttler/credential-throttler.js";
17
20
  import { KERNEL_BUS } from "../infra/tokens.js";
18
21
  /**
19
22
  * The gateway role's parts, driven by `KernelLifecycle` at fixed points of its start order and
20
23
  * stop phases: the channel long-connection supervisor (leader election and share) and the webhook
21
24
  * inbound (its raw Fastify route, {@link routes}, and its runtime cache's invalidation), and the
22
- * device connections ({@link DialInGateway}, `GET /device/connect`). Provided
25
+ * device connections ({@link DialInGateway}, `GET /device/connect`) and the device stream viewers
26
+ * ({@link DeviceStreamViewers}, `GET /device/stream`). Provided
23
27
  * only by `GatewayModule`, so a process without the gateway role has none of them.
24
28
  */
25
29
  let GatewayRole = class GatewayRole {
@@ -29,6 +33,8 @@ let GatewayRole = class GatewayRole {
29
33
  supervisor;
30
34
  webhooks;
31
35
  devices;
36
+ viewers;
37
+ throttler;
32
38
  /** The supervisor's subscription to extension instance changes. */
33
39
  unwatchPlugins;
34
40
  unwatchWebhooks;
@@ -36,17 +42,19 @@ let GatewayRole = class GatewayRole {
36
42
  unsubscribeWebhooks;
37
43
  /** The supervisor was started: the runs phase stops it. */
38
44
  supervisorStarted = false;
39
- constructor(config, host, bus, supervisor, webhooks, devices) {
45
+ constructor(config, host, bus, supervisor, webhooks, devices, viewers, throttler) {
40
46
  this.config = config;
41
47
  this.host = host;
42
48
  this.bus = bus;
43
49
  this.supervisor = supervisor;
44
50
  this.webhooks = webhooks;
45
51
  this.devices = devices;
52
+ this.viewers = viewers;
53
+ this.throttler = throttler;
46
54
  }
47
55
  /**
48
- * The gateway role's HTTP routes (the channel webhook, the device connection), registered when
49
- * the app is built.
56
+ * The gateway role's HTTP routes (the channel webhook, the device connection, the device stream
57
+ * viewer), registered when the app is built.
50
58
  */
51
59
  routes() {
52
60
  const config = this.config.get();
@@ -56,6 +64,11 @@ let GatewayRole = class GatewayRole {
56
64
  devices: {
57
65
  gateway: this.devices,
58
66
  maxFrameBytes: config.devices.dialIn.maxFrameBytes,
67
+ streams: {
68
+ viewers: this.viewers,
69
+ // per-client limit only when the client address is known (ruling R23)
70
+ ...(viewerThrottleOn(config.http.trustProxy) && { throttler: this.throttler }),
71
+ },
59
72
  },
60
73
  };
61
74
  }
@@ -100,11 +113,14 @@ let GatewayRole = class GatewayRole {
100
113
  return this.unsubscribeWebhooks?.() ?? Promise.resolve();
101
114
  }
102
115
  /**
103
- * Closes the device connections (1001) and hands their leases back, before the HTTP server
104
- * closes (the machines reconnect to another gateway replica).
116
+ * Closes the device stream viewers (1001: the panels reconnect with a new ticket), then the
117
+ * device connections (1001) and hands their leases back, before the HTTP server closes (the
118
+ * machines reconnect to another gateway replica). Viewers first, so a viewer of a local device
119
+ * learns of the shutdown rather than of its machine going offline.
105
120
  */
106
- stopDevices() {
107
- return this.devices.stop();
121
+ async stopDevices() {
122
+ await this.viewers.stop();
123
+ await this.devices.stop();
108
124
  }
109
125
  /**
110
126
  * The runs phase: the supervisor aborts its connects and hands back its leases (undefined: never
@@ -121,6 +137,8 @@ GatewayRole = __decorate([
121
137
  __param(2, Inject(KERNEL_BUS)),
122
138
  __param(3, Inject(ChannelSupervisor)),
123
139
  __param(4, Inject(ChannelWebhooks)),
124
- __param(5, Inject(DialInGateway))
140
+ __param(5, Inject(DialInGateway)),
141
+ __param(6, Inject(DeviceStreamViewers)),
142
+ __param(7, Inject(CredentialThrottler))
125
143
  ], GatewayRole);
126
144
  export { GatewayRole };
@@ -2,7 +2,8 @@
2
2
  * The gateway role (nest kernel spec §2), imported by `KernelModule` only when `roles` includes
3
3
  * `gateway`: the channel long-connection supervisor, the webhook inbound (served as a raw Fastify
4
4
  * route, `http/gateway-routes.ts`), the device connections ({@link DialInGateway}, `GET
5
- * /device/connect`) and {@link GatewayRole}, which `KernelLifecycle` drives. It runs
5
+ * /device/connect`), the device stream viewers ({@link DeviceStreamViewers}, `GET /device/stream`)
6
+ * and {@link GatewayRole}, which `KernelLifecycle` drives. It runs
6
7
  * no cron and no queue consumer (those are the worker role's).
7
8
  */
8
9
  export declare class GatewayModule {
@@ -8,23 +8,33 @@ import { Module } from "@nestjs/common";
8
8
  import { ChannelsModule } from "../channels/channels.module.js";
9
9
  import { ChannelSupervisor } from "../channels/supervisor.js";
10
10
  import { ChannelWebhooks } from "../channels/webhook.js";
11
+ import { ExtensionDeviceAccessModule } from "../device-access/device-access.module.js";
12
+ import { DeviceStreamViewers } from "../device-access/viewer.js";
11
13
  import { DialInModule } from "../dial-in/dial-in.module.js";
12
14
  import { DialInGateway } from "../dial-in/gateway.js";
13
15
  import { ExtensionsModule } from "../extensions/extensions.module.js";
16
+ import { SandboxModule } from "../sandbox/sandbox.module.js";
14
17
  import { GatewayRole } from "./gateway-role.js";
15
18
  /**
16
19
  * The gateway role (nest kernel spec §2), imported by `KernelModule` only when `roles` includes
17
20
  * `gateway`: the channel long-connection supervisor, the webhook inbound (served as a raw Fastify
18
21
  * route, `http/gateway-routes.ts`), the device connections ({@link DialInGateway}, `GET
19
- * /device/connect`) and {@link GatewayRole}, which `KernelLifecycle` drives. It runs
22
+ * /device/connect`), the device stream viewers ({@link DeviceStreamViewers}, `GET /device/stream`)
23
+ * and {@link GatewayRole}, which `KernelLifecycle` drives. It runs
20
24
  * no cron and no queue consumer (those are the worker role's).
21
25
  */
22
26
  let GatewayModule = class GatewayModule {
23
27
  };
24
28
  GatewayModule = __decorate([
25
29
  Module({
26
- imports: [ChannelsModule, ExtensionsModule, DialInModule],
27
- providers: [ChannelSupervisor, ChannelWebhooks, DialInGateway, GatewayRole],
30
+ imports: [
31
+ ChannelsModule,
32
+ ExtensionsModule,
33
+ DialInModule,
34
+ SandboxModule,
35
+ ExtensionDeviceAccessModule,
36
+ ],
37
+ providers: [ChannelSupervisor, ChannelWebhooks, DialInGateway, DeviceStreamViewers, GatewayRole],
28
38
  exports: [GatewayRole],
29
39
  })
30
40
  ], GatewayModule);