@capybara-hq/kernel 0.1.0-rc.66 → 0.1.0-rc.67

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 (139) hide show
  1. package/bootstrap.json +0 -8
  2. package/dist/admin/admin.schemas.d.ts +3 -0
  3. package/dist/agents/admin-agents.d.ts +2 -2
  4. package/dist/agents/admin-agents.js +2 -1
  5. package/dist/agents/agent-lookup.js +2 -1
  6. package/dist/agents/agent-profile.service.d.ts +5 -2
  7. package/dist/agents/agent-profile.service.js +13 -2
  8. package/dist/agents/agents.controller.d.ts +4 -3
  9. package/dist/agents/agents.controller.js +7 -3
  10. package/dist/agents/agents.module.js +3 -0
  11. package/dist/agents/agents.schemas.d.ts +6 -6
  12. package/dist/agents/agents.service.d.ts +23 -2
  13. package/dist/agents/agents.service.js +61 -6
  14. package/dist/agents/avatar-view.d.ts +8 -0
  15. package/dist/agents/avatar-view.js +12 -0
  16. package/dist/agents/profile.d.ts +38 -13
  17. package/dist/agents/profile.js +31 -20
  18. package/dist/background/poll.job.d.ts +1 -1
  19. package/dist/background/poll.job.js +8 -6
  20. package/dist/background/tasks.service.js +1 -1
  21. package/dist/catalog/marketplaces.service.d.ts +1 -1
  22. package/dist/channels/inbound.d.ts +2 -3
  23. package/dist/channels/inbound.js +2 -3
  24. package/dist/channels/phrases.d.ts +3 -3
  25. package/dist/channels/phrases.js +3 -3
  26. package/dist/config/schema.d.ts +1 -0
  27. package/dist/config/schema.js +14 -1
  28. package/dist/conversations/conversations.service.d.ts +4 -3
  29. package/dist/conversations/conversations.service.js +4 -2
  30. package/dist/ctx/create-ctx.js +5 -2
  31. package/dist/ctx/device-provider-check.d.ts +7 -0
  32. package/dist/ctx/device-provider-check.js +21 -0
  33. package/dist/db/kernel-schema.d.ts +7 -0
  34. package/dist/devices/access.d.ts +16 -46
  35. package/dist/devices/access.js +26 -62
  36. package/dist/devices/access.module.js +2 -2
  37. package/dist/devices/conversation-devices.d.ts +38 -18
  38. package/dist/devices/conversation-devices.js +69 -51
  39. package/dist/devices/default-provider-setting.d.ts +45 -0
  40. package/dist/devices/default-provider-setting.js +59 -0
  41. package/dist/devices/device-projects.service.js +7 -2
  42. package/dist/devices/device-provider-selection.d.ts +37 -0
  43. package/dist/devices/device-provider-selection.js +86 -0
  44. package/dist/devices/device-provider-selection.module.d.ts +3 -0
  45. package/dist/devices/device-provider-selection.module.js +22 -0
  46. package/dist/devices/devices.controller.d.ts +8 -5
  47. package/dist/devices/devices.controller.js +7 -4
  48. package/dist/devices/devices.module.js +3 -1
  49. package/dist/devices/devices.schemas.js +2 -2
  50. package/dist/devices/devices.service.d.ts +72 -83
  51. package/dist/devices/devices.service.js +139 -248
  52. package/dist/devices/dto.d.ts +9 -0
  53. package/dist/devices/dto.js +15 -2
  54. package/dist/devices/label.d.ts +3 -5
  55. package/dist/devices/label.js +3 -14
  56. package/dist/devices/legacy-cleanup.job.d.ts +2 -2
  57. package/dist/devices/legacy-cleanup.job.js +6 -6
  58. package/dist/errors.d.ts +15 -1
  59. package/dist/errors.js +15 -1
  60. package/dist/extensions/guide-places.d.ts +2 -1
  61. package/dist/extensions/guide-places.js +8 -2
  62. package/dist/extensions/locks.d.ts +1 -1
  63. package/dist/extensions/locks.js +1 -1
  64. package/dist/extensions/org-config.d.ts +20 -4
  65. package/dist/extensions/org-config.js +23 -6
  66. package/dist/extensions/permission-view.d.ts +2 -2
  67. package/dist/extensions/permission-view.js +2 -2
  68. package/dist/extensions/plugin-guides.d.ts +1 -1
  69. package/dist/extensions/plugin-guides.js +11 -3
  70. package/dist/extensions/point-defaults.d.ts +14 -0
  71. package/dist/extensions/point-defaults.js +70 -47
  72. package/dist/extensions/resolve.js +7 -2
  73. package/dist/identity/settings.d.ts +8 -0
  74. package/dist/identity/settings.js +24 -1
  75. package/dist/infra/config/kernel-config.service.js +2 -0
  76. package/dist/lifecycle/kernel-lifecycle.js +7 -6
  77. package/dist/local-devices/device-routes.d.ts +1 -1
  78. package/dist/local-devices/device-routes.js +1 -1
  79. package/dist/local-devices/local-device-provider.d.ts +14 -11
  80. package/dist/local-devices/local-device-provider.js +14 -0
  81. package/dist/local-devices/local-devices.module.d.ts +4 -4
  82. package/dist/local-devices/local-devices.module.js +5 -3
  83. package/dist/local-devices/owner-report.js +2 -1
  84. package/dist/local-devices/targets.d.ts +2 -0
  85. package/dist/local-devices/targets.js +10 -0
  86. package/dist/orgs/settings.service.d.ts +26 -1
  87. package/dist/orgs/settings.service.js +44 -3
  88. package/dist/platform/ops/admin.ops.js +8 -3
  89. package/dist/platform/ops/agents.ops.d.ts +6 -1
  90. package/dist/platform/ops/agents.ops.js +38 -11
  91. package/dist/platform/ops/devices.ops.js +25 -20
  92. package/dist/platform/ops/orgs.ops.js +1 -0
  93. package/dist/platform/ops/themes.ops.js +1 -1
  94. package/dist/runtime/plugin-context.service.js +1 -12
  95. package/dist/sandbox/cloud-permission-snapshot.d.ts +2 -2
  96. package/dist/sandbox/cloud-permission-snapshot.js +3 -3
  97. package/dist/sandbox/device-reports.d.ts +50 -0
  98. package/dist/sandbox/device-reports.js +157 -0
  99. package/dist/sandbox/held-operations.d.ts +8 -8
  100. package/dist/sandbox/leases.d.ts +1 -1
  101. package/dist/sandbox/leases.js +1 -1
  102. package/dist/sandbox/operation-authority.d.ts +5 -2
  103. package/dist/sandbox/operation-authority.js +12 -11
  104. package/dist/sandbox/operation-reconcile.job.d.ts +2 -2
  105. package/dist/sandbox/operation-reconcile.job.js +5 -5
  106. package/dist/sandbox/permission-availability.d.ts +2 -2
  107. package/dist/sandbox/permission-call-types.d.ts +5 -0
  108. package/dist/sandbox/permission-facts.d.ts +2 -2
  109. package/dist/sandbox/permission-facts.js +5 -5
  110. package/dist/sandbox/providers.d.ts +23 -24
  111. package/dist/sandbox/providers.js +38 -46
  112. package/dist/sandbox/providers.module.js +3 -3
  113. package/dist/sandbox/sandbox.module.d.ts +1 -1
  114. package/dist/sandbox/sandbox.module.js +7 -2
  115. package/dist/sandbox/sandbox.service.d.ts +95 -68
  116. package/dist/sandbox/sandbox.service.js +212 -208
  117. package/dist/setup/setup-guides.service.d.ts +4 -1
  118. package/dist/setup/setup-guides.service.js +6 -1
  119. package/dist/themes/avatar-preset-key.d.ts +6 -0
  120. package/dist/themes/avatar-preset-key.js +9 -0
  121. package/dist/themes/theme-state.d.ts +29 -1
  122. package/dist/themes/theme-state.js +40 -1
  123. package/dist/themes/themes.service.d.ts +28 -1
  124. package/dist/themes/themes.service.js +36 -2
  125. package/dist/tools/builtins/image/generate.d.ts +3 -0
  126. package/dist/tools/builtins/image/generate.js +21 -11
  127. package/dist/tools/builtins/image/index.d.ts +1 -1
  128. package/dist/tools/builtins/image/index.js +1 -1
  129. package/dist/tools/builtins/sandbox/common.d.ts +36 -12
  130. package/dist/tools/builtins/sandbox/common.js +62 -18
  131. package/dist/tools/builtins/sandbox/read.d.ts +5 -2
  132. package/dist/tools/builtins/sandbox/read.js +9 -6
  133. package/dist/tools/builtins/sandbox/task-output.d.ts +2 -2
  134. package/dist/tools/builtins/sandbox/task-output.js +6 -5
  135. package/dist/tools/builtins/skill-load.js +13 -4
  136. package/dist/until-aborted.d.ts +5 -0
  137. package/dist/until-aborted.js +16 -0
  138. package/package.json +4 -4
  139. package/src/migrations/0176_device_report.sql +6 -0
@@ -46,7 +46,7 @@ export declare class BackgroundTaskPollJob {
46
46
  private readonly stopper;
47
47
  /** The marker that logs a left-alone legacy instance once across the replicas. */
48
48
  private readonly redis?;
49
- constructor(store: Pick<BackgroundTaskStore, "runningLegacy" | "runningManaged" | "owedCancels" | "pruneFinished" | "failAbandonedLaunches">, finisher: BackgroundTaskFinisher, eventLog: Pick<EventLog, "publishChanged">, sandbox: Pick<SandboxService, "cloudAvailable" | "availableFor" | "ifRunning">, config: KernelConfigService, lockService: LockService, log: KernelLogger, stopper: Pick<BackgroundTaskStopper, "stop">,
49
+ constructor(store: Pick<BackgroundTaskStore, "runningLegacy" | "runningManaged" | "owedCancels" | "pruneFinished" | "failAbandonedLaunches">, finisher: BackgroundTaskFinisher, eventLog: Pick<EventLog, "publishChanged">, sandbox: Pick<SandboxService, "availableFor" | "providerLoaded" | "ifRunning">, config: KernelConfigService, lockService: LockService, log: KernelLogger, stopper: Pick<BackgroundTaskStopper, "stop">,
50
50
  /** The marker that logs a left-alone legacy instance once across the replicas. */
51
51
  redis?: Pick<Redis, "set"> | undefined);
52
52
  poll(signal?: AbortSignal): Promise<void>;
@@ -106,10 +106,11 @@ let BackgroundTaskPollJob = class BackgroundTaskPollJob {
106
106
  if (signal.aborted)
107
107
  break;
108
108
  try {
109
- // the device's provider is not there (no cloud provider, `@local` not served):
110
- // its tasks stay as they are until it is
111
- const env = tasks[0]?.deviceId ?? null;
112
- if (env === null ? !this.sandbox.cloudAvailable : !(await this.sandbox.availableFor(env))) {
109
+ // the device's provider does not serve its org now (off in the org, not loaded, `@local`
110
+ // not registered): its tasks stay as they are until it does. A task without a device
111
+ // needs no provider: pollLegacy finishes it as gone.
112
+ const first = tasks[0];
113
+ if (first?.deviceId && !(await this.sandbox.availableFor(first.deviceId, first.orgId))) {
113
114
  continue;
114
115
  }
115
116
  finished += await this.pollLegacy(tasks, signal);
@@ -162,7 +163,7 @@ let BackgroundTaskPollJob = class BackgroundTaskPollJob {
162
163
  break;
163
164
  const [first] = tasks;
164
165
  try {
165
- if (!(await this.sandbox.availableFor(first.deviceId)))
166
+ if (!(await this.sandbox.availableFor(first.deviceId, first.orgId)))
166
167
  continue;
167
168
  n += await this.pollManagedInstance(tasks, signal);
168
169
  }
@@ -183,7 +184,8 @@ let BackgroundTaskPollJob = class BackgroundTaskPollJob {
183
184
  if (signal.aborted || attempts >= MANAGED_STOPS_PER_TICK)
184
185
  break;
185
186
  try {
186
- if (!(await this.sandbox.availableFor(t.deviceId)))
187
+ // revocation delivery needs the provider loaded only (not on in the org)
188
+ if (!(await this.sandbox.providerLoaded(t.deviceId)))
187
189
  continue;
188
190
  attempts++;
189
191
  const after = await this.stopper.stop(t, "revoked", signal);
@@ -205,7 +205,7 @@ let BackgroundTasks = class BackgroundTasks {
205
205
  reason: task.status === "running" ? "starting" : "not_started",
206
206
  };
207
207
  }
208
- if (!(await this.sandbox.availableFor(task.deviceId))) {
208
+ if (!(await this.sandbox.availableFor(task.deviceId, orgId))) {
209
209
  return { kind: "unavailable", reason: "unavailable" };
210
210
  }
211
211
  if (!(await this.deviceUsable(orgId, task.conversationId))) {
@@ -203,7 +203,7 @@ export declare function headsToResolve(entries: readonly MarketplaceEntry[], imp
203
203
  /** The host versions (an entry's `minKernel` is judged against them). */
204
204
  export declare const HOST_VERSIONS: {
205
205
  readonly kernel: string;
206
- readonly sdk: "0.1.0-rc.50";
206
+ readonly sdk: "0.1.0-rc.51";
207
207
  };
208
208
  /** Whether this deployment satisfies the entry's `minKernel`. */
209
209
  export declare const entryCompatible: (e: Pick<MarketplaceEntry, "minKernel">) => boolean;
@@ -77,9 +77,8 @@ export declare class ChannelInbound {
77
77
  /**
78
78
  * Direct-message `/device` (environments spec §4.2 item 8): lists the person's usable devices,
79
79
  * or with a name sets the IM default their next new conversation is bound to. The name matches
80
- * case-insensitively against each label (`deviceLabel`: a former default demoted by a
81
- * provider switch answers to e.g. "Personal (Docker sandbox)"); the default device also
82
- * answers to 个人设备 / Personal.
80
+ * case-insensitively against each label (`deviceLabel`); the default device also answers to
81
+ * its zh or en "Personal" label (`PERSONAL` in devices/label.ts).
83
82
  */
84
83
  private deviceCommand;
85
84
  /**
@@ -316,9 +316,8 @@ let ChannelInbound = class ChannelInbound {
316
316
  /**
317
317
  * Direct-message `/device` (environments spec §4.2 item 8): lists the person's usable devices,
318
318
  * or with a name sets the IM default their next new conversation is bound to. The name matches
319
- * case-insensitively against each label (`deviceLabel`: a former default demoted by a
320
- * provider switch answers to e.g. "Personal (Docker sandbox)"); the default device also
321
- * answers to 个人设备 / Personal.
319
+ * case-insensitively against each label (`deviceLabel`); the default device also answers to
320
+ * its zh or en "Personal" label (`PERSONAL` in devices/label.ts).
322
321
  */
323
322
  async deviceCommand(orgId, accountId, name, locale, replyText) {
324
323
  const { items, defaultId } = await this.devices.list(orgId, accountId);
@@ -29,10 +29,10 @@ export declare const CHANNEL_PHRASES: {
29
29
  };
30
30
  /** `/device <name>` names an existing device that is unavailable now (`{list}` as in deviceList). */
31
31
  readonly deviceUnavailable: {
32
- readonly zh: "设备「{name}」当前不可用(它的文件由未启用的沙箱提供方保存)。你的设备:\n{list}";
33
- readonly en: "The device “{name}” is unavailable: its files are kept by a sandbox provider that is not active. Your devices:\n{list}";
32
+ readonly zh: "设备「{name}」当前不可用(它的设备提供方在该组织未启用或未安装,文件仍保留)。你的设备:\n{list}";
33
+ readonly en: "The device “{name}” is unavailable: its device provider is off in this organization or not installed (its files are kept). Your devices:\n{list}";
34
34
  };
35
- /** Direct message `/device` when the person has no usable device (no cloud sandbox in the deployment). */
35
+ /** Direct message `/device` when the person has no usable device (no device provider on in the org). */
36
36
  readonly deviceNone: {
37
37
  readonly zh: "你目前没有可用的设备。";
38
38
  readonly en: "You have no usable devices.";
@@ -24,10 +24,10 @@ export const CHANNEL_PHRASES = {
24
24
  },
25
25
  /** `/device <name>` names an existing device that is unavailable now (`{list}` as in deviceList). */
26
26
  deviceUnavailable: {
27
- zh: "设备「{name}」当前不可用(它的文件由未启用的沙箱提供方保存)。你的设备:\n{list}",
28
- en: "The device \u201c{name}\u201d is unavailable: its files are kept by a sandbox provider that is not active. Your devices:\n{list}",
27
+ zh: "设备「{name}」当前不可用(它的设备提供方在该组织未启用或未安装,文件仍保留)。你的设备:\n{list}",
28
+ en: "The device \u201c{name}\u201d is unavailable: its device provider is off in this organization or not installed (its files are kept). Your devices:\n{list}",
29
29
  },
30
- /** Direct message `/device` when the person has no usable device (no cloud sandbox in the deployment). */
30
+ /** Direct message `/device` when the person has no usable device (no device provider on in the org). */
31
31
  deviceNone: {
32
32
  zh: "你目前没有可用的设备。",
33
33
  en: "You have no usable devices.",
@@ -283,6 +283,7 @@ export declare const KernelConfigSchema: z.ZodObject<{
283
283
  allowed: "allowed";
284
284
  off: "off";
285
285
  }>>;
286
+ defaultProvider: z.ZodOptional<z.ZodUnion<readonly [z.ZodLiteral<"">, z.ZodString]>>;
286
287
  network: z.ZodPrefault<z.ZodObject<{
287
288
  default: z.ZodOptional<z.ZodEnum<{
288
289
  allowlist: "allowlist";
@@ -1,5 +1,5 @@
1
1
  import { isIP } from "node:net";
2
- import { CATALOG_ITEM_NAME_RE, DOCUMENT_MEDIA_TYPES, IMAGE_MEDIA_TYPES, validateAllowHost, } from "@capybara-hq/plugin-sdk";
2
+ import { CATALOG_ITEM_NAME_RE, DOCUMENT_MEDIA_TYPES, EXTENSION_NAME_RE, IMAGE_MEDIA_TYPES, validateAllowHost, } from "@capybara-hq/plugin-sdk";
3
3
  import { z } from "zod";
4
4
  import { LOG_FORMATS, LOG_LEVELS } from "../log.js";
5
5
  import { noProxyEntryProblem, proxyUrlProblem } from "../net/proxy.js";
@@ -1659,6 +1659,19 @@ export const KernelConfigSchema = z.strictObject({
1659
1659
  hot: true,
1660
1660
  description: "Whether a device may be granted running a call outside its sandbox (default allowed): a person, the review model or Auto then approves such an escalation per call, within the organization's and the device owner's settings; off refuses every escalation, and devices only run in their sandbox. An organization may turn it off and follow the platform. Set here, it is read-only in the admin console. Replaces devices.fullAccess (CAPYBARA_DEVICES__FULL_ACCESS) and devices.autoMode, which are refused: rename them (the same values; off also needs devices.network.ceiling: allowlist to keep devices on the allowlist), or convert a config file with capybara config migrate-permissions",
1661
1661
  })),
1662
+ /**
1663
+ * The device provider that makes a person's default device (several `device-provider@1`
1664
+ * extensions may coexist). Also a deployment setting the super admin changes in the admin
1665
+ * console (`devicesDefaultProvider`); the startup config wins over it, so this field is
1666
+ * optional with no schema default. An empty string means none. An org may override it.
1667
+ */
1668
+ defaultProvider: z
1669
+ .union([z.literal(""), z.string().regex(EXTENSION_NAME_RE)])
1670
+ .optional()
1671
+ .meta(meta({
1672
+ hot: true,
1673
+ description: "The name of the device provider extension that makes a person's default device, needed with a single device provider too (default none: nobody gets a default device, and creating a device needs an explicit provider). An empty string means none. An organization may choose another provider that is on in it. Set here, it is read-only in the admin console",
1674
+ })),
1662
1675
  /**
1663
1676
  * The platform's device sandbox network (approval-only profiles design, "Settings model"):
1664
1677
  * the default mode and the widest mode an org or device may choose, `open` (the public
@@ -1,7 +1,7 @@
1
1
  import type { ConversationSource } from "@capybara-hq/plugin-sdk";
2
2
  import { AgentLookup } from "../agents/agent-lookup.js";
3
3
  import type { ModelRef } from "../agents/definition.js";
4
- import type { AgentAvatar } from "../agents/profile.js";
4
+ import type { AgentAvatarView } from "../agents/profile.js";
5
5
  import type { TitleSource } from "../db/kernel-schema.js";
6
6
  import { ConversationDevices, type NewConversationDevice } from "../devices/conversation-devices.js";
7
7
  import { DeviceProjectsService } from "../devices/device-projects.service.js";
@@ -97,7 +97,7 @@ export interface ConversationGroup {
97
97
  id: string;
98
98
  name: string;
99
99
  profile: {
100
- avatar: AgentAvatar;
100
+ avatar: AgentAvatarView;
101
101
  updatedAt: string;
102
102
  };
103
103
  };
@@ -371,7 +371,8 @@ export declare class ConversationsService {
371
371
  * checks the owner and the device's current mode (refused with `device.not_found`,
372
372
  * `device.projects_unsupported`, `device.project_not_found` or `device.project_outside_mode`).
373
373
  * Called inside the transaction that writes the conversation. A conversation without a device
374
- * (no cloud provider, an anonymous IM sender) cannot have one. Undefined name: free mode.
374
+ * (no default device provider on in the org, an anonymous IM sender) cannot have one. Undefined
375
+ * name: free mode.
375
376
  */
376
377
  private bindingFor;
377
378
  /**
@@ -11,6 +11,7 @@ import { randomUUID } from "node:crypto";
11
11
  import { Inject, Injectable } from "@nestjs/common";
12
12
  import { sql } from "kysely";
13
13
  import { AgentLookup, visibleTo } from "../agents/agent-lookup.js";
14
+ import { avatarView } from "../agents/avatar-view.js";
14
15
  import { unbindConversation } from "../channels/threads.js";
15
16
  import { ConversationDevices, } from "../devices/conversation-devices.js";
16
17
  import { DeviceProjectsService } from "../devices/device-projects.service.js";
@@ -344,7 +345,8 @@ let ConversationsService = class ConversationsService {
344
345
  * checks the owner and the device's current mode (refused with `device.not_found`,
345
346
  * `device.projects_unsupported`, `device.project_not_found` or `device.project_outside_mode`).
346
347
  * Called inside the transaction that writes the conversation. A conversation without a device
347
- * (no cloud provider, an anonymous IM sender) cannot have one. Undefined name: free mode.
348
+ * (no default device provider on in the org, an anonymous IM sender) cannot have one. Undefined
349
+ * name: free mode.
348
350
  */
349
351
  async bindingFor(orgId, accountId, deviceId, name) {
350
352
  if (name === undefined)
@@ -676,7 +678,7 @@ let ConversationsService = class ConversationsService {
676
678
  id: r.agent_id,
677
679
  name: r.agent_name ?? "",
678
680
  profile: {
679
- avatar: r.profile_avatar,
681
+ avatar: avatarView(r.profile_avatar),
680
682
  updatedAt: r.profile_updated_at.toISOString(),
681
683
  },
682
684
  },
@@ -1,4 +1,4 @@
1
- import { EXTENSION_POINTS, ExtensionPermissionError, ExtensionPointUnavailableError, FILE_PREVIEW_EXTENSION_POINT, OBJECT_STORE_EXTENSION_POINT, parseExtensionPoint, resolveUiSlots, SETUP_CHECK_ID_RE, SETUP_GUIDE_PLACES, validateFilePreviewProvider, validateToolSpec, } from "@capybara-hq/plugin-sdk";
1
+ import { DEVICE_PROVIDER_EXTENSION_POINT, EXTENSION_POINTS, ExtensionPermissionError, ExtensionPointUnavailableError, FILE_PREVIEW_EXTENSION_POINT, OBJECT_STORE_EXTENSION_POINT, parseExtensionPoint, resolveUiSlots, SETUP_CHECK_ID_RE, SETUP_GUIDE_PLACES, validateFilePreviewProvider, validateToolSpec, } from "@capybara-hq/plugin-sdk";
2
2
  import { Command, ConnectionBuilder, ControlledTransactionBuilder, Kysely, TransactionBuilder, } from "kysely";
3
3
  import { conversationTopic } from "../conversations/event-log.js";
4
4
  import { kernelDb } from "../db/kernel-schema.js";
@@ -10,6 +10,7 @@ import { audit } from "../identity/audit.js";
10
10
  import { PrefixedStorage } from "../storage/storage.service.js";
11
11
  import { canUseSlot, orgUiTopic, PLATFORM_UI_TOPIC, PluginUiRegistration, } from "../ui/registry.js";
12
12
  import { createBus } from "./bus.js";
13
+ import { checkDeviceProvider } from "./device-provider-check.js";
13
14
  import { createKv } from "./kv.js";
14
15
  import { createLock } from "./lock.js";
15
16
  import { createExtensionNet } from "./net.js";
@@ -658,7 +659,9 @@ export function createExtensionContext(deps, manifest, deploymentConfig, extensi
658
659
  }
659
660
  const served = extensionPoint === FILE_PREVIEW_EXTENSION_POINT
660
661
  ? checkFilePreviewProvider(manifest, impl, uiResolution, deps.log)
661
- : impl;
662
+ : extensionPoint === DEVICE_PROVIDER_EXTENSION_POINT
663
+ ? checkDeviceProvider(name, impl)
664
+ : impl;
662
665
  provided.set(extensionPoint, served);
663
666
  ledger.add({
664
667
  kind: "provide",
@@ -0,0 +1,7 @@
1
+ import type { DeviceProvider } from "@capybara-hq/plugin-sdk";
2
+ /**
3
+ * Checks a plugin's `device-provider@1` implementation when it is provided: the declarations
4
+ * (`networkAllowlist`, `concurrentExec`) must be `true` and the required methods must exist. A
5
+ * failure fails the plugin's setup, so it never loads.
6
+ */
7
+ export declare function checkDeviceProvider(plugin: string, impl: unknown): DeviceProvider;
@@ -0,0 +1,21 @@
1
+ const REQUIRED_FUNCTIONS = ["report", "killTagged", "exec", "create", "createFileSystem"];
2
+ /**
3
+ * Checks a plugin's `device-provider@1` implementation when it is provided: the declarations
4
+ * (`networkAllowlist`, `concurrentExec`) must be `true` and the required methods must exist. A
5
+ * failure fails the plugin's setup, so it never loads.
6
+ */
7
+ export function checkDeviceProvider(plugin, impl) {
8
+ const p = (impl ?? {});
9
+ const need = (member) => {
10
+ throw new Error(`plugin ${plugin}: device-provider@1 requires ${member}`);
11
+ };
12
+ if (p.networkAllowlist !== true)
13
+ need("networkAllowlist");
14
+ if (p.concurrentExec !== true)
15
+ need("concurrentExec");
16
+ for (const m of REQUIRED_FUNCTIONS) {
17
+ if (typeof p[m] !== "function")
18
+ need(m);
19
+ }
20
+ return impl;
21
+ }
@@ -1139,6 +1139,13 @@ export interface DevicesTable {
1139
1139
  network_mode: ColumnType<"open" | "allowlist" | "none" | null, "open" | "allowlist" | "none" | null | undefined, "open" | "allowlist" | "none" | null>;
1140
1140
  /** 0165: a cloud device's allowlist hosts (a JSON array), used while the mode is `allowlist`. */
1141
1141
  network_hosts: ColumnType<string[], string | undefined, string>;
1142
+ /**
1143
+ * 0176: what the device's provider last reported (a `DeviceReport` as JSON; read back through
1144
+ * `parseDeviceReport`, see `DeviceReports`); null until the first create or resume stores one.
1145
+ */
1146
+ report: ColumnType<unknown, string | null | undefined, string | null>;
1147
+ /** 0176: when {@link report} was stored. */
1148
+ report_at: TsNullable;
1142
1149
  created_at: Ts;
1143
1150
  updated_at: Ts;
1144
1151
  deleted_at: TsNullable;
@@ -1,49 +1,16 @@
1
1
  import { ApiError } from "../errors.js";
2
2
  import { KernelDb } from "../infra/database/kernel-db.js";
3
- import { SandboxProviders } from "../sandbox/providers.js";
4
- /** What decides a device's provider: its own record and its instance row's (if any). */
5
- export interface ProviderFacts {
6
- /** `devices.provider`: the provider current when it was created. */
7
- provider: string | null;
8
- /** `device_instances.provider`; null without an instance row. */
9
- instanceProvider: string | null;
10
- /** The instance row's `fs_status`; null without an instance row. */
11
- fsStatus: "none" | "ready" | "lost" | null;
12
- /** The instance row records an instance handle. */
13
- hasInstance: boolean;
14
- }
15
- /**
16
- * The provider a device works with: a kernel-internal one (`@local`) as recorded; for a
17
- * cloud device the one keeping its files or instance once it has any (they are never
18
- * migrated, spec §4.2 item 13); before that, the current cloud provider (its first use creates
19
- * them there), else the one recorded at creation. The same rule as `SandboxService`'s use.
20
- */
21
- export declare function deviceProvider(f: ProviderFacts, current: string | undefined): string | null;
22
- /** It has files or an instance at some provider (a file system, even a lost one, or a handle). */
23
- export declare const holdsResources: (f: ProviderFacts) => boolean;
24
- /** `device_instances.status`. */
25
- export type InstanceStatus = "none" | "creating" | "running" | "suspended" | "destroyed";
3
+ import { DeviceProviderSelection } from "./device-provider-selection.js";
26
4
  /**
27
- * For replacing a default (which one is the person's default): it holds healthy files or an
28
- * instance, or an instance row is under way (`creating`, `running`, `suspended`: a first use may
29
- * be creating the instance before any file system or handle is recorded). Lost files count as
30
- * none here, unlike {@link holdsResources}, which decides the provider.
5
+ * The device's provider is off in its org or not installed, or the device has none (503). Its
6
+ * files and instance are kept: nothing is demoted, recreated or moved to another provider.
31
7
  */
32
- export declare const inUseForDefault: (f: ProviderFacts & {
33
- status: InstanceStatus | null;
34
- }) => boolean;
35
- /**
36
- * The provider a former default belongs to (its label and the provider whose switch back restores
37
- * it): the one holding its resources once it holds any, else the one it was demoted from (the
38
- * marker). A former default demoted while empty and used afterwards keeps a stale marker; its
39
- * resources decide. null for a device that is not a former default.
40
- */
41
- export declare function demotedProvider(marker: string | null, f: ProviderFacts): string | null;
8
+ export declare const deviceUnavailable: (provider: string | null) => ApiError;
42
9
  /**
43
- * The device's provider is not the current one, or there is no provider (503); for an
44
- * internal provider (`@local`), this kernel does not serve it.
10
+ * The device's instance row names another provider than the device's own (503): never written by
11
+ * this kernel (a device is bound to its provider for life), so nothing is taken over.
45
12
  */
46
- export declare const deviceUnavailable: (provider: string | null) => ApiError;
13
+ export declare const deviceInstanceForeign: (provider: string, instanceProvider: string) => ApiError;
47
14
  /**
48
15
  * Who may use a device (spec §4.2 item 12): in phase 1 only the owner of a personal
49
16
  * device. Anyone else gets `device.not_found`, never `forbidden`, so ids cannot be
@@ -51,16 +18,19 @@ export declare const deviceUnavailable: (provider: string | null) => ApiError;
51
18
  */
52
19
  export declare class DeviceAccess {
53
20
  private readonly db;
54
- private readonly providers;
55
- constructor(db: KernelDb, providers: Pick<SandboxProviders, "cloud" | "forName">);
21
+ private readonly selection;
22
+ constructor(db: KernelDb, selection: Pick<DeviceProviderSelection, "isOnInOrg">);
56
23
  /**
57
24
  * The device if `accountId` may use it in `orgId` now: `device.not_found` when it is
58
- * missing, deleted, in another org or someone else's; `device.unavailable` when its
59
- * provider is not the current one (or there is none). A local device is usable while
60
- * `@local` is registered: whether its machine is online is the relay's concern, not access.
25
+ * missing, deleted, in another org or someone else's; `device.unavailable` when it has no
26
+ * provider, its provider (`devices.provider`, fixed at creation) does not serve the org now
27
+ * ({@link DeviceProviderSelection.isOnInOrg}: off in the org, not installed, invalid config;
28
+ * `@local` not registered), or its instance row names another provider (never written by this
29
+ * kernel). A local device is usable while `@local` is registered: whether its machine is online
30
+ * is the relay's concern, not access.
61
31
  */
62
32
  usable(orgId: string, accountId: string, deviceId: string): Promise<{
63
33
  id: string;
64
- provider: string | null;
34
+ provider: string;
65
35
  }>;
66
36
  }
@@ -10,52 +10,20 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
10
10
  import { Inject, Injectable } from "@nestjs/common";
11
11
  import { ApiError } from "../errors.js";
12
12
  import { KernelDb } from "../infra/database/kernel-db.js";
13
- import { isInternalProvider, SandboxProviders } from "../sandbox/providers.js";
13
+ import { DeviceProviderSelection } from "./device-provider-selection.js";
14
14
  import { deviceNotFound } from "./errors.js";
15
15
  /**
16
- * The provider a device works with: a kernel-internal one (`@local`) as recorded; for a
17
- * cloud device the one keeping its files or instance once it has any (they are never
18
- * migrated, spec §4.2 item 13); before that, the current cloud provider (its first use creates
19
- * them there), else the one recorded at creation. The same rule as `SandboxService`'s use.
16
+ * The device's provider is off in its org or not installed, or the device has none (503). Its
17
+ * files and instance are kept: nothing is demoted, recreated or moved to another provider.
20
18
  */
21
- export function deviceProvider(f, current) {
22
- if (f.provider !== null && isInternalProvider(f.provider))
23
- return f.provider;
24
- if (holdsResources(f))
25
- return f.instanceProvider;
26
- return current ?? f.provider;
27
- }
28
- /** It has files or an instance at some provider (a file system, even a lost one, or a handle). */
29
- export const holdsResources = (f) => f.fsStatus !== null && (f.fsStatus !== "none" || f.hasInstance);
30
- /**
31
- * For replacing a default (which one is the person's default): it holds healthy files or an
32
- * instance, or an instance row is under way (`creating`, `running`, `suspended`: a first use may
33
- * be creating the instance before any file system or handle is recorded). Lost files count as
34
- * none here, unlike {@link holdsResources}, which decides the provider.
35
- */
36
- export const inUseForDefault = (f) => f.fsStatus === "ready" ||
37
- f.hasInstance ||
38
- (f.status !== null && f.status !== "none" && f.status !== "destroyed");
39
- /**
40
- * The provider a former default belongs to (its label and the provider whose switch back restores
41
- * it): the one holding its resources once it holds any, else the one it was demoted from (the
42
- * marker). A former default demoted while empty and used afterwards keeps a stale marker; its
43
- * resources decide. null for a device that is not a former default.
44
- */
45
- export function demotedProvider(marker, f) {
46
- if (marker === null)
47
- return null;
48
- return (holdsResources(f) ? f.instanceProvider : null) ?? marker;
49
- }
19
+ export const deviceUnavailable = (provider) => new ApiError("device.unavailable", provider === null
20
+ ? "This device has no provider; delete it and create a new one"
21
+ : `This device's provider ${provider} is off in this organization or not installed; its files are kept, and the device works again once an org admin switches the provider back on`);
50
22
  /**
51
- * The device's provider is not the current one, or there is no provider (503); for an
52
- * internal provider (`@local`), this kernel does not serve it.
23
+ * The device's instance row names another provider than the device's own (503): never written by
24
+ * this kernel (a device is bound to its provider for life), so nothing is taken over.
53
25
  */
54
- export const deviceUnavailable = (provider) => new ApiError("device.unavailable", provider === null
55
- ? "No cloud sandbox is available in this deployment; ask the super admin to enable a sandbox plugin"
56
- : isInternalProvider(provider)
57
- ? `This device runs on a connected machine (${provider}), which this deployment does not serve`
58
- : `This device is kept by the sandbox provider ${provider}, which is not the current provider; it can be used again once ${provider} is the deployment's sandbox provider`);
26
+ export const deviceInstanceForeign = (provider, instanceProvider) => new ApiError("device.unavailable", `This device's provider is ${provider}, but its instance was made by ${instanceProvider}; it cannot be used as it is: delete it and create a new one`);
59
27
  /**
60
28
  * Who may use a device (spec §4.2 item 12): in phase 1 only the owner of a personal
61
29
  * device. Anyone else gets `device.not_found`, never `forbidden`, so ids cannot be
@@ -63,22 +31,25 @@ export const deviceUnavailable = (provider) => new ApiError("device.unavailable"
63
31
  */
64
32
  let DeviceAccess = class DeviceAccess {
65
33
  db;
66
- providers;
67
- constructor(db, providers) {
34
+ selection;
35
+ constructor(db, selection) {
68
36
  this.db = db;
69
- this.providers = providers;
37
+ this.selection = selection;
70
38
  }
71
39
  /**
72
40
  * The device if `accountId` may use it in `orgId` now: `device.not_found` when it is
73
- * missing, deleted, in another org or someone else's; `device.unavailable` when its
74
- * provider is not the current one (or there is none). A local device is usable while
75
- * `@local` is registered: whether its machine is online is the relay's concern, not access.
41
+ * missing, deleted, in another org or someone else's; `device.unavailable` when it has no
42
+ * provider, its provider (`devices.provider`, fixed at creation) does not serve the org now
43
+ * ({@link DeviceProviderSelection.isOnInOrg}: off in the org, not installed, invalid config;
44
+ * `@local` not registered), or its instance row names another provider (never written by this
45
+ * kernel). A local device is usable while `@local` is registered: whether its machine is online
46
+ * is the relay's concern, not access.
76
47
  */
77
48
  async usable(orgId, accountId, deviceId) {
78
49
  const row = await this.db
79
50
  .selectFrom("devices as e")
80
51
  .leftJoin("device_instances as i", "i.device_id", "e.id")
81
- .select(["e.id", "e.provider", "i.provider as instance_provider", "i.fs_status", "i.handle"])
52
+ .select(["e.id", "e.provider", "i.provider as instance_provider"])
82
53
  .where("e.id", "=", deviceId)
83
54
  .where("e.org_id", "=", orgId)
84
55
  .where("e.owner_kind", "=", "personal")
@@ -87,20 +58,13 @@ let DeviceAccess = class DeviceAccess {
87
58
  .executeTakeFirst();
88
59
  if (!row)
89
60
  throw deviceNotFound();
90
- if (row.provider !== null && isInternalProvider(row.provider)) {
91
- if (this.providers.forName(row.provider) === undefined) {
92
- throw deviceUnavailable(row.provider);
93
- }
94
- return { id: row.id, provider: row.provider };
61
+ const provider = row.provider;
62
+ if (provider === null)
63
+ throw deviceUnavailable(null);
64
+ if (row.instance_provider !== null && row.instance_provider !== provider) {
65
+ throw deviceInstanceForeign(provider, row.instance_provider);
95
66
  }
96
- const current = this.providers.cloud()?.plugin;
97
- const provider = deviceProvider({
98
- provider: row.provider,
99
- instanceProvider: row.instance_provider,
100
- fsStatus: row.fs_status,
101
- hasInstance: row.handle !== null,
102
- }, current);
103
- if (current === undefined || provider !== current)
67
+ if (!(await this.selection.isOnInOrg(orgId, provider)))
104
68
  throw deviceUnavailable(provider);
105
69
  return { id: row.id, provider };
106
70
  }
@@ -108,6 +72,6 @@ let DeviceAccess = class DeviceAccess {
108
72
  DeviceAccess = __decorate([
109
73
  Injectable(),
110
74
  __param(0, Inject(KernelDb)),
111
- __param(1, Inject(SandboxProviders))
75
+ __param(1, Inject(DeviceProviderSelection))
112
76
  ], DeviceAccess);
113
77
  export { DeviceAccess };
@@ -5,13 +5,13 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
5
5
  return c > 3 && r && Object.defineProperty(target, key, r), r;
6
6
  };
7
7
  import { Module } from "@nestjs/common";
8
- import { SandboxProviderRegistryModule } from "../sandbox/providers.module.js";
9
8
  import { DeviceAccess } from "./access.js";
9
+ import { DeviceProviderSelectionModule } from "./device-provider-selection.module.js";
10
10
  let DeviceAccessModule = class DeviceAccessModule {
11
11
  };
12
12
  DeviceAccessModule = __decorate([
13
13
  Module({
14
- imports: [SandboxProviderRegistryModule],
14
+ imports: [DeviceProviderSelectionModule],
15
15
  providers: [DeviceAccess],
16
16
  exports: [DeviceAccess],
17
17
  })