@capybara-hq/kernel 0.1.0-rc.65 → 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 (177) 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/attribution/platform-attribution.d.ts +15 -6
  19. package/dist/attribution/platform-attribution.js +31 -14
  20. package/dist/background/poll.job.d.ts +1 -1
  21. package/dist/background/poll.job.js +8 -6
  22. package/dist/background/tasks.service.js +1 -1
  23. package/dist/catalog/catalog-content.d.ts +6 -3
  24. package/dist/catalog/catalog-content.js +7 -8
  25. package/dist/catalog/claude/translate.js +1 -1
  26. package/dist/catalog/marketplaces.service.d.ts +1 -1
  27. package/dist/catalog/marketplaces.service.js +2 -1
  28. package/dist/catalog/plugin-package.d.ts +3 -2
  29. package/dist/channels/file-delivery.d.ts +2 -0
  30. package/dist/channels/file-delivery.js +1 -1
  31. package/dist/channels/inbound.d.ts +8 -4
  32. package/dist/channels/inbound.js +14 -7
  33. package/dist/channels/outbound.js +11 -5
  34. package/dist/channels/phrases.d.ts +21 -13
  35. package/dist/channels/phrases.js +21 -14
  36. package/dist/client/client-files.js +1 -1
  37. package/dist/config/product-name.d.ts +8 -0
  38. package/dist/config/product-name.js +9 -0
  39. package/dist/config/schema.d.ts +1 -0
  40. package/dist/config/schema.js +17 -4
  41. package/dist/conversations/conversations.service.d.ts +4 -3
  42. package/dist/conversations/conversations.service.js +15 -2
  43. package/dist/ctx/create-ctx.js +5 -2
  44. package/dist/ctx/device-provider-check.d.ts +7 -0
  45. package/dist/ctx/device-provider-check.js +21 -0
  46. package/dist/db/kernel-schema.d.ts +7 -0
  47. package/dist/devices/access.d.ts +16 -46
  48. package/dist/devices/access.js +26 -62
  49. package/dist/devices/access.module.js +2 -2
  50. package/dist/devices/conversation-devices.d.ts +38 -18
  51. package/dist/devices/conversation-devices.js +69 -51
  52. package/dist/devices/default-provider-setting.d.ts +45 -0
  53. package/dist/devices/default-provider-setting.js +59 -0
  54. package/dist/devices/device-projects.service.js +7 -2
  55. package/dist/devices/device-provider-selection.d.ts +37 -0
  56. package/dist/devices/device-provider-selection.js +86 -0
  57. package/dist/devices/device-provider-selection.module.d.ts +3 -0
  58. package/dist/devices/device-provider-selection.module.js +22 -0
  59. package/dist/devices/devices.controller.d.ts +8 -5
  60. package/dist/devices/devices.controller.js +7 -4
  61. package/dist/devices/devices.module.js +3 -1
  62. package/dist/devices/devices.schemas.js +2 -2
  63. package/dist/devices/devices.service.d.ts +72 -83
  64. package/dist/devices/devices.service.js +139 -248
  65. package/dist/devices/dto.d.ts +9 -0
  66. package/dist/devices/dto.js +15 -2
  67. package/dist/devices/label.d.ts +3 -5
  68. package/dist/devices/label.js +3 -14
  69. package/dist/devices/legacy-cleanup.job.d.ts +2 -2
  70. package/dist/devices/legacy-cleanup.job.js +6 -6
  71. package/dist/errors.d.ts +15 -1
  72. package/dist/errors.js +15 -1
  73. package/dist/extensions/guide-places.d.ts +2 -1
  74. package/dist/extensions/guide-places.js +8 -2
  75. package/dist/extensions/locks.d.ts +1 -1
  76. package/dist/extensions/locks.js +1 -1
  77. package/dist/extensions/org-config.d.ts +20 -4
  78. package/dist/extensions/org-config.js +23 -6
  79. package/dist/extensions/permission-view.d.ts +2 -2
  80. package/dist/extensions/permission-view.js +2 -2
  81. package/dist/extensions/plugin-guides.d.ts +1 -1
  82. package/dist/extensions/plugin-guides.js +11 -3
  83. package/dist/extensions/point-defaults.d.ts +14 -0
  84. package/dist/extensions/point-defaults.js +70 -47
  85. package/dist/extensions/resolve.js +7 -2
  86. package/dist/git/gateway.d.ts +9 -0
  87. package/dist/git/gateway.js +36 -17
  88. package/dist/git/gateway.route.d.ts +1 -1
  89. package/dist/git/gateway.route.js +1 -3
  90. package/dist/http/app.d.ts +1 -1
  91. package/dist/identity/app-auth.d.ts +4 -3
  92. package/dist/identity/app-auth.js +7 -8
  93. package/dist/identity/settings.d.ts +8 -0
  94. package/dist/identity/settings.js +30 -3
  95. package/dist/identity/sso.d.ts +13 -7
  96. package/dist/identity/sso.js +21 -7
  97. package/dist/infra/config/kernel-config.service.js +3 -1
  98. package/dist/lifecycle/kernel-lifecycle.js +7 -6
  99. package/dist/local-devices/device-routes.d.ts +1 -1
  100. package/dist/local-devices/device-routes.js +2 -2
  101. package/dist/local-devices/local-device-provider.d.ts +14 -11
  102. package/dist/local-devices/local-device-provider.js +14 -0
  103. package/dist/local-devices/local-devices.module.d.ts +4 -4
  104. package/dist/local-devices/local-devices.module.js +5 -3
  105. package/dist/local-devices/owner-report.js +2 -1
  106. package/dist/local-devices/targets.d.ts +2 -0
  107. package/dist/local-devices/targets.js +10 -0
  108. package/dist/orgs/offboarding.js +9 -0
  109. package/dist/orgs/settings.service.d.ts +26 -1
  110. package/dist/orgs/settings.service.js +44 -3
  111. package/dist/platform/ops/admin.ops.js +8 -3
  112. package/dist/platform/ops/agents.ops.d.ts +6 -1
  113. package/dist/platform/ops/agents.ops.js +38 -11
  114. package/dist/platform/ops/devices.ops.js +25 -20
  115. package/dist/platform/ops/orgs.ops.js +1 -0
  116. package/dist/platform/ops/platform-icons.ops.js +4 -4
  117. package/dist/platform/ops/platform.ops.js +2 -1
  118. package/dist/platform/ops/themes.ops.js +1 -1
  119. package/dist/runtime/plugin-context.service.js +1 -12
  120. package/dist/sandbox/cloud-permission-snapshot.d.ts +2 -2
  121. package/dist/sandbox/cloud-permission-snapshot.js +3 -3
  122. package/dist/sandbox/device-reports.d.ts +50 -0
  123. package/dist/sandbox/device-reports.js +157 -0
  124. package/dist/sandbox/held-operations.d.ts +8 -8
  125. package/dist/sandbox/leases.d.ts +1 -1
  126. package/dist/sandbox/leases.js +1 -1
  127. package/dist/sandbox/operation-authority.d.ts +5 -2
  128. package/dist/sandbox/operation-authority.js +12 -11
  129. package/dist/sandbox/operation-reconcile.job.d.ts +2 -2
  130. package/dist/sandbox/operation-reconcile.job.js +5 -5
  131. package/dist/sandbox/permission-availability.d.ts +2 -2
  132. package/dist/sandbox/permission-call-types.d.ts +5 -0
  133. package/dist/sandbox/permission-facts.d.ts +2 -2
  134. package/dist/sandbox/permission-facts.js +5 -5
  135. package/dist/sandbox/providers.d.ts +23 -24
  136. package/dist/sandbox/providers.js +38 -46
  137. package/dist/sandbox/providers.module.js +3 -3
  138. package/dist/sandbox/review/classifier.d.ts +6 -0
  139. package/dist/sandbox/review/classifier.js +12 -1
  140. package/dist/sandbox/review/index.d.ts +1 -1
  141. package/dist/sandbox/review/index.js +1 -1
  142. package/dist/sandbox/sandbox.module.d.ts +1 -1
  143. package/dist/sandbox/sandbox.module.js +7 -2
  144. package/dist/sandbox/sandbox.service.d.ts +95 -68
  145. package/dist/sandbox/sandbox.service.js +212 -208
  146. package/dist/setup/setup-guides.service.d.ts +4 -1
  147. package/dist/setup/setup-guides.service.js +6 -1
  148. package/dist/themes/avatar-preset-key.d.ts +6 -0
  149. package/dist/themes/avatar-preset-key.js +9 -0
  150. package/dist/themes/platform-icon.service.js +1 -1
  151. package/dist/themes/theme-delivery.service.d.ts +10 -2
  152. package/dist/themes/theme-delivery.service.js +26 -11
  153. package/dist/themes/theme-state.d.ts +43 -2
  154. package/dist/themes/theme-state.js +61 -4
  155. package/dist/themes/themes-public.controller.d.ts +3 -3
  156. package/dist/themes/themes-public.controller.js +3 -3
  157. package/dist/themes/themes.service.d.ts +28 -1
  158. package/dist/themes/themes.service.js +36 -2
  159. package/dist/tools/builtins/image/generate.d.ts +3 -0
  160. package/dist/tools/builtins/image/generate.js +21 -11
  161. package/dist/tools/builtins/image/index.d.ts +1 -1
  162. package/dist/tools/builtins/image/index.js +1 -1
  163. package/dist/tools/builtins/platform.js +25 -17
  164. package/dist/tools/builtins/sandbox/common.d.ts +37 -13
  165. package/dist/tools/builtins/sandbox/common.js +63 -19
  166. package/dist/tools/builtins/sandbox/read.d.ts +5 -2
  167. package/dist/tools/builtins/sandbox/read.js +9 -6
  168. package/dist/tools/builtins/sandbox/task-output.d.ts +2 -2
  169. package/dist/tools/builtins/sandbox/task-output.js +6 -5
  170. package/dist/tools/builtins/skill-load.js +13 -4
  171. package/dist/tools/connectors.service.js +2 -1
  172. package/dist/tools/oauth/discovery.d.ts +2 -0
  173. package/dist/tools/oauth/discovery.js +3 -1
  174. package/dist/until-aborted.d.ts +5 -0
  175. package/dist/until-aborted.js +16 -0
  176. package/package.json +4 -4
  177. package/src/migrations/0176_device_report.sql +6 -0
@@ -5,8 +5,8 @@ import type { DeviceModeLevel } from "../runtime/context/device-paths.js";
5
5
  import type { SandboxMode } from "../sandbox/modes.js";
6
6
  import { PermissionCeilingWrites } from "../sandbox/permission-ceiling-writes.js";
7
7
  import { PermissionFacts, type WorkingFacts } from "../sandbox/permission-facts.js";
8
- import { SandboxProviders } from "../sandbox/providers.js";
9
8
  import { DeviceAccess } from "./access.js";
9
+ import { DeviceProviderSelection } from "./device-provider-selection.js";
10
10
  import { DevicesService } from "./devices.service.js";
11
11
  import { type WorkingProject } from "./working-root.js";
12
12
  export type { WorkingFacts } from "../sandbox/permission-facts.js";
@@ -14,6 +14,19 @@ export type { WorkingFacts } from "../sandbox/permission-facts.js";
14
14
  export declare const ANONYMOUS_NO_DEVICE = "anonymous";
15
15
  /** Anonymous IM senders have no device (spec §4.2 item 8): their sandbox tools are off. */
16
16
  export declare const anonymousNoDevice: () => ApiError;
17
+ /** `details[0].reason` of the error a conversation with no device gets while providers are on. */
18
+ export declare const NO_DEVICE = "no_device";
19
+ /**
20
+ * No device to bind although device providers are on in the org (503 `sandbox.unavailable`): the
21
+ * person has no default device and the org has no default device provider to make one.
22
+ */
23
+ export declare const noDeviceToBind: () => ApiError;
24
+ /**
25
+ * Why a sandbox tool call cannot run ({@link ConversationDevices.unusableReason}): the bound
26
+ * device's provider is off (`provider-off`), no device to bind while providers are on
27
+ * (`no-device`), or no device provider on in the org (`none`).
28
+ */
29
+ export type DeviceUnusableReason = "provider-off" | "no-device" | "none";
17
30
  /** How a new conversation picks its device. */
18
31
  export interface NewConversationDevice {
19
32
  /** The device the person chose (the composer's picker); checked with `usable`. */
@@ -65,12 +78,12 @@ export interface CommandReviewFacts {
65
78
  export declare class ConversationDevices {
66
79
  private readonly db;
67
80
  readonly txHost: TransactionHost;
68
- private readonly providers;
81
+ private readonly selection;
69
82
  private readonly access;
70
83
  private readonly devices;
71
84
  private readonly facts;
72
85
  private readonly ceilings;
73
- constructor(db: KernelDb, txHost: TransactionHost, providers: Pick<SandboxProviders, "cloud"> & Partial<Pick<SandboxProviders, "forName">>, access: Pick<DeviceAccess, "usable">, devices: Pick<DevicesService, "ensureDefault" | "modeLevel" | "projectReach">, facts: Pick<PermissionFacts, "readWorkingFacts" | "workingProject">, ceilings: PermissionCeilingWrites);
86
+ constructor(db: KernelDb, txHost: TransactionHost, selection: Pick<DeviceProviderSelection, "onInOrg" | "isOnInOrg" | "defaultFor">, access: Pick<DeviceAccess, "usable">, devices: Pick<DevicesService, "ensureDefault" | "modeLevel" | "projectReach">, facts: Pick<PermissionFacts, "readWorkingFacts" | "workingProject">, ceilings: PermissionCeilingWrites);
74
87
  /**
75
88
  * The mode and network a call in the device runs at now ({@link DevicesService.modeLevel}),
76
89
  * recorded for the model in `device_paths` (auto mode spec §4.2).
@@ -99,8 +112,8 @@ export declare class ConversationDevices {
99
112
  * The device a new conversation of `accountId` is bound to: the chosen one when it is
100
113
  * usable by the person (else `device.not_found` / `device.unavailable`, and nothing
101
114
  * is created); without a choice the IM default (IM only) or the person's default device.
102
- * Null (unbound) for anonymous IM senders, and without a cloud sandbox provider when no usable
103
- * IM default (a connected machine's device, say) applies.
115
+ * Null (unbound) for anonymous IM senders, and when no usable IM default applies and the person
116
+ * has no default device and none can be made (no default provider on in the org).
104
117
  */
105
118
  forNew(orgId: string, accountId: string, pick?: NewConversationDevice): Promise<string | null>;
106
119
  /**
@@ -108,31 +121,38 @@ export declare class ConversationDevices {
108
121
  * enters, checked for its owner now. A conversation still unbound (created before
109
122
  * devices, or while no provider existed) is bound first, to the owner's IM default when it
110
123
  * is an IM thread, else to the owner's default device; a racing call binds it once.
111
- * Throws `sandbox.unavailable` for a cloud device (or none to bind) without a cloud
112
- * provider and for an anonymous IM conversation, and `device.not_found` /
113
- * `device.unavailable` when the owner may not use it now. A conversation bound to a
114
- * connected machine's device (`@local`) does not need a cloud provider.
124
+ * Throws `sandbox.unavailable` when there is no device to bind (`details[0].reason`
125
+ * {@link NO_DEVICE} while providers are on in the org) and for an anonymous IM conversation, and `device.not_found` / `device.unavailable` when the owner may not use it
126
+ * now ({@link DeviceAccess.usable}: the device's own provider must be on in the org).
115
127
  */
116
128
  forTool(orgId: string, rootConversationId: string): Promise<string>;
117
129
  /**
118
130
  * Whether a sandbox tool call of the tree rooted at `rootConversationId` has a provider to run
119
- * on, for tool listings (ruling R8; no binding, no access check, never throws): a cloud
120
- * provider is loaded, or the device the conversation is bound to (an unbound IM thread:
121
- * its owner's IM default) runs on a registered kernel provider (`@local`). A forbidden or gone
122
- * device still counts: the call then says why.
131
+ * on, for tool listings (ruling R8; no binding, no access check): {@link unusableReason} is
132
+ * null.
133
+ */
134
+ usableNow(orgId: string, rootConversationId?: string): Promise<boolean>;
135
+ /**
136
+ * Why a sandbox tool call of the tree rooted at `rootConversationId` cannot run now, or null when
137
+ * it can (no binding, no access check): `provider-off` when the device the conversation is bound
138
+ * to (an unbound IM thread: its owner's IM default; any unbound one: its owner's default device)
139
+ * has its provider off in the org or not installed (its files are kept); `no-device` when there
140
+ * is no device and no default provider to make one while device providers are on in the org;
141
+ * `none` when no device provider is on in the org at all. A forbidden or gone device still counts as usable: the call
142
+ * then says why.
123
143
  */
124
- usableNow(orgId: string, rootConversationId: string): Promise<boolean>;
144
+ unusableReason(orgId: string, rootConversationId?: string): Promise<DeviceUnusableReason | null>;
145
+ /** The device of `orgId` has its provider on in the org now. */
146
+ private providerOn;
125
147
  /**
126
148
  * The person's IM default when `im` and it is still theirs, live, in this org and usable (the
127
149
  * table has no such guard), else their default device (created when missing); null when
128
- * that is needed and there is no cloud provider to make or use it. The owner having left the org
129
- * reads as `device.not_found`.
150
+ * that is needed and there is none and no default provider on in the org to make it. The owner
151
+ * having left the org reads as `device.not_found`.
130
152
  */
131
153
  private defaultFor;
132
154
  /** Binds an unbound conversation to `deviceId`; returns what it is bound to now. */
133
155
  private bind;
134
- /** The device runs on a kernel-internal provider (`@local`). */
135
- private isInternal;
136
156
  /** The account is the org's anonymous IM guest (no person behind it). */
137
157
  private isAnonymous;
138
158
  /** The conversation is (or was) an IM thread's. */
@@ -14,16 +14,23 @@ import { TransactionHost, withTransactionFencePrelude, } from "../infra/database
14
14
  import { Transactional } from "../infra/database/transactional.decorator.js";
15
15
  import { PermissionCeilingWrites } from "../sandbox/permission-ceiling-writes.js";
16
16
  import { PermissionFacts } from "../sandbox/permission-facts.js";
17
- import { isInternalProvider, SandboxProviders } from "../sandbox/providers.js";
18
17
  import { homeOf } from "../sandbox/review/rules.js";
19
18
  import { deviceNotFound, sandboxUnavailable } from "../sandbox/sandbox.service.js";
20
19
  import { DeviceAccess } from "./access.js";
20
+ import { DeviceProviderSelection } from "./device-provider-selection.js";
21
21
  import { DevicesService } from "./devices.service.js";
22
22
  import { workingRootOf } from "./working-root.js";
23
23
  /** `details[0].reason` of the error an anonymous IM conversation's sandbox call gets. */
24
24
  export const ANONYMOUS_NO_DEVICE = "anonymous";
25
25
  /** Anonymous IM senders have no device (spec §4.2 item 8): their sandbox tools are off. */
26
26
  export const anonymousNoDevice = () => new ApiError("sandbox.unavailable", "Anonymous IM senders have no device, so the sandbox is not available", [{ reason: ANONYMOUS_NO_DEVICE }]);
27
+ /** `details[0].reason` of the error a conversation with no device gets while providers are on. */
28
+ export const NO_DEVICE = "no_device";
29
+ /**
30
+ * No device to bind although device providers are on in the org (503 `sandbox.unavailable`): the
31
+ * person has no default device and the org has no default device provider to make one.
32
+ */
33
+ export const noDeviceToBind = () => new ApiError("sandbox.unavailable", "This conversation has no device: its owner has no default device and the organization has no default device provider to make one", [{ reason: NO_DEVICE }]);
27
34
  /**
28
35
  * Which device a conversation works in (spec 2026-11-07-environments-design §4.1 items 1, 3;
29
36
  * §4.2 items 8, 9, 12). A conversation is bound once, to a device of its owner
@@ -35,15 +42,15 @@ export const anonymousNoDevice = () => new ApiError("sandbox.unavailable", "Anon
35
42
  let ConversationDevices = class ConversationDevices {
36
43
  db;
37
44
  txHost;
38
- providers;
45
+ selection;
39
46
  access;
40
47
  devices;
41
48
  facts;
42
49
  ceilings;
43
- constructor(db, txHost, providers, access, devices, facts, ceilings) {
50
+ constructor(db, txHost, selection, access, devices, facts, ceilings) {
44
51
  this.db = db;
45
52
  this.txHost = txHost;
46
- this.providers = providers;
53
+ this.selection = selection;
47
54
  this.access = access;
48
55
  this.devices = devices;
49
56
  this.facts = facts;
@@ -134,15 +141,13 @@ let ConversationDevices = class ConversationDevices {
134
141
  * The device a new conversation of `accountId` is bound to: the chosen one when it is
135
142
  * usable by the person (else `device.not_found` / `device.unavailable`, and nothing
136
143
  * is created); without a choice the IM default (IM only) or the person's default device.
137
- * Null (unbound) for anonymous IM senders, and without a cloud sandbox provider when no usable
138
- * IM default (a connected machine's device, say) applies.
144
+ * Null (unbound) for anonymous IM senders, and when no usable IM default applies and the person
145
+ * has no default device and none can be made (no default provider on in the org).
139
146
  */
140
147
  async forNew(orgId, accountId, pick = {}) {
141
148
  if (pick.deviceId !== undefined) {
142
149
  return (await this.access.usable(orgId, accountId, pick.deviceId)).id;
143
150
  }
144
- if (pick.im !== true && this.providers.cloud() === undefined)
145
- return null;
146
151
  if (await this.isAnonymous(orgId, accountId))
147
152
  return null;
148
153
  return this.defaultFor(orgId, accountId, pick.im === true);
@@ -152,10 +157,9 @@ let ConversationDevices = class ConversationDevices {
152
157
  * enters, checked for its owner now. A conversation still unbound (created before
153
158
  * devices, or while no provider existed) is bound first, to the owner's IM default when it
154
159
  * is an IM thread, else to the owner's default device; a racing call binds it once.
155
- * Throws `sandbox.unavailable` for a cloud device (or none to bind) without a cloud
156
- * provider and for an anonymous IM conversation, and `device.not_found` /
157
- * `device.unavailable` when the owner may not use it now. A conversation bound to a
158
- * connected machine's device (`@local`) does not need a cloud provider.
160
+ * Throws `sandbox.unavailable` when there is no device to bind (`details[0].reason`
161
+ * {@link NO_DEVICE} while providers are on in the org) and for an anonymous IM conversation, and `device.not_found` / `device.unavailable` when the owner may not use it
162
+ * now ({@link DeviceAccess.usable}: the device's own provider must be on in the org).
159
163
  */
160
164
  async forTool(orgId, rootConversationId) {
161
165
  const conv = await this.db
@@ -172,25 +176,36 @@ let ConversationDevices = class ConversationDevices {
172
176
  if (deviceId === null) {
173
177
  const im = await this.isImThread(rootConversationId);
174
178
  const chosen = await this.defaultFor(orgId, conv.account_id, im);
175
- if (chosen === null)
176
- throw sandboxUnavailable();
179
+ if (chosen === null) {
180
+ throw (await this.selection.onInOrg(orgId)).length > 0
181
+ ? noDeviceToBind()
182
+ : sandboxUnavailable();
183
+ }
177
184
  deviceId = await withTransactionFencePrelude(() => this.ceilings.lock({ orgId, deviceId: chosen, rootConversationId }, "root"), () => this.bind(orgId, rootConversationId, chosen));
178
185
  }
179
- if (this.providers.cloud() === undefined && !(await this.isInternal(deviceId))) {
180
- throw sandboxUnavailable();
181
- }
182
186
  return (await this.access.usable(orgId, conv.account_id, deviceId)).id;
183
187
  }
184
188
  /**
185
189
  * Whether a sandbox tool call of the tree rooted at `rootConversationId` has a provider to run
186
- * on, for tool listings (ruling R8; no binding, no access check, never throws): a cloud
187
- * provider is loaded, or the device the conversation is bound to (an unbound IM thread:
188
- * its owner's IM default) runs on a registered kernel provider (`@local`). A forbidden or gone
189
- * device still counts: the call then says why.
190
+ * on, for tool listings (ruling R8; no binding, no access check): {@link unusableReason} is
191
+ * null.
190
192
  */
191
193
  async usableNow(orgId, rootConversationId) {
192
- if (this.providers.cloud() !== undefined)
193
- return true;
194
+ return (await this.unusableReason(orgId, rootConversationId)) === null;
195
+ }
196
+ /**
197
+ * Why a sandbox tool call of the tree rooted at `rootConversationId` cannot run now, or null when
198
+ * it can (no binding, no access check): `provider-off` when the device the conversation is bound
199
+ * to (an unbound IM thread: its owner's IM default; any unbound one: its owner's default device)
200
+ * has its provider off in the org or not installed (its files are kept); `no-device` when there
201
+ * is no device and no default provider to make one while device providers are on in the org;
202
+ * `none` when no device provider is on in the org at all. A forbidden or gone device still counts as usable: the call
203
+ * then says why.
204
+ */
205
+ async unusableReason(orgId, rootConversationId) {
206
+ if (rootConversationId === undefined) {
207
+ return (await this.selection.onInOrg(orgId)).length > 0 ? null : "none";
208
+ }
194
209
  const conv = await this.db
195
210
  .selectFrom("conversations")
196
211
  .select(["account_id", "device_id"])
@@ -198,36 +213,51 @@ let ConversationDevices = class ConversationDevices {
198
213
  .where("org_id", "=", orgId)
199
214
  .executeTakeFirst();
200
215
  if (!conv)
201
- return false;
202
- let deviceId = conv.device_id;
203
- if (deviceId === null) {
204
- if (!(await this.isImThread(rootConversationId)))
205
- return false;
216
+ return "none";
217
+ const onOrOff = async (deviceId) => (await this.providerOn(orgId, deviceId)) ? null : "provider-off";
218
+ if (conv.device_id !== null)
219
+ return onOrOff(conv.device_id);
220
+ if (await this.isImThread(rootConversationId)) {
206
221
  const row = await this.db
207
222
  .selectFrom("device_defaults")
208
223
  .select("device_id")
209
224
  .where("org_id", "=", orgId)
210
225
  .where("account_id", "=", conv.account_id)
211
226
  .executeTakeFirst();
212
- if (!row)
213
- return false;
214
- deviceId = row.device_id;
227
+ if (row && (await this.providerOn(orgId, row.device_id)))
228
+ return null;
215
229
  }
216
- const env = await this.db
230
+ const own = await this.db
231
+ .selectFrom("devices")
232
+ .select("id")
233
+ .where("org_id", "=", orgId)
234
+ .where("owner_kind", "=", "personal")
235
+ .where("owner_account_id", "=", conv.account_id)
236
+ .where("is_default", "=", true)
237
+ .where("deleted_at", "is", null)
238
+ .executeTakeFirst();
239
+ if (own)
240
+ return onOrOff(own.id);
241
+ if ((await this.selection.defaultFor(orgId)) !== undefined)
242
+ return null;
243
+ return (await this.selection.onInOrg(orgId)).length > 0 ? "no-device" : "none";
244
+ }
245
+ /** The device of `orgId` has its provider on in the org now. */
246
+ async providerOn(orgId, deviceId) {
247
+ const row = await this.db
217
248
  .selectFrom("devices")
218
249
  .select("provider")
219
250
  .where("id", "=", deviceId)
251
+ .where("org_id", "=", orgId)
220
252
  .executeTakeFirst();
221
- const provider = env?.provider ?? null;
222
- return (provider !== null &&
223
- isInternalProvider(provider) &&
224
- this.providers.forName?.(provider) !== undefined);
253
+ const provider = row?.provider ?? null;
254
+ return provider !== null && (await this.selection.isOnInOrg(orgId, provider));
225
255
  }
226
256
  /**
227
257
  * The person's IM default when `im` and it is still theirs, live, in this org and usable (the
228
258
  * table has no such guard), else their default device (created when missing); null when
229
- * that is needed and there is no cloud provider to make or use it. The owner having left the org
230
- * reads as `device.not_found`.
259
+ * that is needed and there is none and no default provider on in the org to make it. The owner
260
+ * having left the org reads as `device.not_found`.
231
261
  */
232
262
  async defaultFor(orgId, accountId, im) {
233
263
  if (im) {
@@ -247,8 +277,6 @@ let ConversationDevices = class ConversationDevices {
247
277
  }
248
278
  }
249
279
  }
250
- if (this.providers.cloud() === undefined)
251
- return null;
252
280
  try {
253
281
  return await this.devices.ensureDefault(orgId, accountId);
254
282
  }
@@ -283,16 +311,6 @@ let ConversationDevices = class ConversationDevices {
283
311
  .executeTakeFirstOrThrow();
284
312
  return row.device_id ?? deviceId;
285
313
  }
286
- /** The device runs on a kernel-internal provider (`@local`). */
287
- async isInternal(deviceId) {
288
- const row = await this.db
289
- .selectFrom("devices")
290
- .select("provider")
291
- .where("id", "=", deviceId)
292
- .executeTakeFirst();
293
- const provider = row?.provider ?? null;
294
- return provider !== null && isInternalProvider(provider);
295
- }
296
314
  /** The account is the org's anonymous IM guest (no person behind it). */
297
315
  async isAnonymous(orgId, accountId) {
298
316
  const row = await this.db
@@ -321,7 +339,7 @@ ConversationDevices = __decorate([
321
339
  Injectable(),
322
340
  __param(0, Inject(KernelDb)),
323
341
  __param(1, Inject(TransactionHost)),
324
- __param(2, Inject(SandboxProviders)),
342
+ __param(2, Inject(DeviceProviderSelection)),
325
343
  __param(3, Inject(DeviceAccess)),
326
344
  __param(4, Inject(DevicesService)),
327
345
  __param(5, Inject(PermissionFacts)),
@@ -0,0 +1,45 @@
1
+ import { z } from "zod";
2
+ import type { KernelSchemaDb } from "../db/kernel-schema.js";
3
+ import type { KernelConfigService } from "../infra/config/kernel-config.service.js";
4
+ import type { KernelLogger } from "../log.js";
5
+ import type { DeviceSandboxSource } from "../sandbox/device-sandbox-settings.js";
6
+ /**
7
+ * The platform's default device provider: the `device-provider@1` extension that makes a person's
8
+ * default device when several coexist. Both a startup config field (`devices.defaultProvider`) and
9
+ * a deployment setting ({@link DEVICES_DEFAULT_PROVIDER_SETTING}), read live at every use:
10
+ * environment > config file / Nacos > the super admin's choice in the admin console > none. An
11
+ * empty string in the startup config means none. A field set in the startup config is read-only in
12
+ * the admin console. An invalid stored value reads as none, with a warning.
13
+ */
14
+ /** The `deployment_settings` key of the super admin's choice (stored {@link DevicesDefaultProviderStored}). */
15
+ export declare const DEVICES_DEFAULT_PROVIDER_SETTING = "devicesDefaultProvider";
16
+ /** An extension name. */
17
+ export declare const DeviceProviderName: z.ZodString;
18
+ /** The stored choice: `provider` absent follows the default (none). */
19
+ export declare const DevicesDefaultProviderStoredSchema: z.ZodObject<{
20
+ provider: z.ZodOptional<z.ZodString>;
21
+ }, z.core.$strict>;
22
+ export type DevicesDefaultProviderStored = z.infer<typeof DevicesDefaultProviderStoredSchema>;
23
+ /**
24
+ * A change of the stored choice (`PUT /api/v1/admin/settings` `devicesDefaultProvider`):
25
+ * `provider: null` clears it.
26
+ */
27
+ export declare const DEVICES_DEFAULT_PROVIDER_PATCH: z.ZodObject<{
28
+ provider: z.ZodNullable<z.ZodString>;
29
+ }, z.core.$strict>;
30
+ export type DevicesDefaultProviderPatch = z.infer<typeof DEVICES_DEFAULT_PROVIDER_PATCH>;
31
+ /** The admin console's view: the provider in effect (null: none) and where it comes from. */
32
+ export interface DevicesDefaultProviderView {
33
+ provider: string | null;
34
+ source: DeviceSandboxSource;
35
+ }
36
+ type ConfigReader = Pick<KernelConfigService, "get" | "fieldSource">;
37
+ /** The stored choice's provider, read live; invalid reads as none with a warning. */
38
+ export declare function readStoredDefaultProvider(db: KernelSchemaDb, log?: Pick<KernelLogger, "warn">): Promise<string | null | undefined>;
39
+ /**
40
+ * The platform's default device provider with its source, read live from PG (in the caller's
41
+ * transaction when there is one): the startup config (`""` reads none), else the stored choice,
42
+ * else none.
43
+ */
44
+ export declare function readPlatformDefaultProvider(cfg: ConfigReader, db: KernelSchemaDb, log?: Pick<KernelLogger, "warn">): Promise<DevicesDefaultProviderView>;
45
+ export {};
@@ -0,0 +1,59 @@
1
+ import { EXTENSION_NAME_RE } from "@capybara-hq/plugin-sdk";
2
+ import { z } from "zod";
3
+ /**
4
+ * The platform's default device provider: the `device-provider@1` extension that makes a person's
5
+ * default device when several coexist. Both a startup config field (`devices.defaultProvider`) and
6
+ * a deployment setting ({@link DEVICES_DEFAULT_PROVIDER_SETTING}), read live at every use:
7
+ * environment > config file / Nacos > the super admin's choice in the admin console > none. An
8
+ * empty string in the startup config means none. A field set in the startup config is read-only in
9
+ * the admin console. An invalid stored value reads as none, with a warning.
10
+ */
11
+ /** The `deployment_settings` key of the super admin's choice (stored {@link DevicesDefaultProviderStored}). */
12
+ export const DEVICES_DEFAULT_PROVIDER_SETTING = "devicesDefaultProvider";
13
+ /** An extension name. */
14
+ export const DeviceProviderName = z.string().regex(EXTENSION_NAME_RE, "must be an extension name");
15
+ /** The stored choice: `provider` absent follows the default (none). */
16
+ export const DevicesDefaultProviderStoredSchema = z
17
+ .object({ provider: DeviceProviderName.optional() })
18
+ .strict();
19
+ /**
20
+ * A change of the stored choice (`PUT /api/v1/admin/settings` `devicesDefaultProvider`):
21
+ * `provider: null` clears it.
22
+ */
23
+ export const DEVICES_DEFAULT_PROVIDER_PATCH = z
24
+ .object({ provider: DeviceProviderName.nullable() })
25
+ .strict();
26
+ /** The stored choice's provider, read live; invalid reads as none with a warning. */
27
+ export async function readStoredDefaultProvider(db, log) {
28
+ const row = await db
29
+ .selectFrom("deployment_settings")
30
+ .select("value")
31
+ .where("key", "=", DEVICES_DEFAULT_PROVIDER_SETTING)
32
+ .executeTakeFirst();
33
+ if (row === undefined)
34
+ return undefined;
35
+ const parsed = DevicesDefaultProviderStoredSchema.safeParse(row.value);
36
+ if (parsed.success)
37
+ return parsed.data.provider ?? null;
38
+ log?.warn("invalid deployment setting; it reads as no default device provider", {
39
+ key: DEVICES_DEFAULT_PROVIDER_SETTING,
40
+ });
41
+ return null;
42
+ }
43
+ /**
44
+ * The platform's default device provider with its source, read live from PG (in the caller's
45
+ * transaction when there is one): the startup config (`""` reads none), else the stored choice,
46
+ * else none.
47
+ */
48
+ export async function readPlatformDefaultProvider(cfg, db, log) {
49
+ // optional chaining: partial config stubs (tests, the CLI) may leave the section out
50
+ const configured = cfg.get().devices
51
+ ?.defaultProvider;
52
+ const source = cfg.fieldSource("devices.defaultProvider");
53
+ if (configured !== undefined && source !== undefined)
54
+ return { provider: configured === "" ? null : configured, source };
55
+ const stored = await readStoredDefaultProvider(db, log);
56
+ if (stored === undefined)
57
+ return { provider: null, source: "default" };
58
+ return { provider: stored, source: "db" };
59
+ }
@@ -8,7 +8,7 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
8
8
  return function (target, key) { decorator(target, key, paramIndex); }
9
9
  };
10
10
  import { posix } from "node:path";
11
- import { DeviceProtocolError, isDeviceProjectName, isFileSystemGoneError, isSandboxGoneError, isSandboxScopeDeniedError, MAX_DEVICE_SCAN_RESULTS, ToolError, validateSandboxPathShape, } from "@capybara-hq/plugin-sdk";
11
+ import { DeviceProtocolError, isDeviceProjectName, isFileSystemGoneError, isProjectPathRefusedError, isSandboxGoneError, isSandboxScopeDeniedError, MAX_DEVICE_SCAN_RESULTS, ToolError, validateSandboxPathShape, } from "@capybara-hq/plugin-sdk";
12
12
  import { Inject, Injectable, Optional } from "@nestjs/common";
13
13
  import { sql } from "kysely";
14
14
  import { ApiError } from "../errors.js";
@@ -481,7 +481,8 @@ function unavailable(e) {
481
481
  const pathMissing = (path) => new ApiError("device.project_path_missing", `${clipped(path)} does not exist on the device; check the path`);
482
482
  /**
483
483
  * What a failed `list(path)` becomes. A machine's policy refusal is the device refusing the path
484
- * in its mode (`device.project_outside_mode`). Any other error alone cannot tell a file or an
484
+ * in its mode (`device.project_outside_mode`); a provider's `ProjectPathRefusedError` is
485
+ * `device.project_refused` with its reason. Any other error alone cannot tell a file or an
485
486
  * unreadable directory from a failure on the device, so the device is asked `stat(path)`: a file
486
487
  * or a directory it cannot list is the path's problem (422 `device.project_path_unreadable`), a
487
488
  * missing path `device.project_path_missing`, and a `stat` that fails too means the device could
@@ -491,6 +492,10 @@ async function listFailure(e, r, path, mode, signal, logUnavailable) {
491
492
  const passed = passThrough(e);
492
493
  if (passed !== undefined)
493
494
  return passed;
495
+ // the provider refusing the directory states its reason (shown to the person, en + zh)
496
+ if (isProjectPathRefusedError(e)) {
497
+ return new ApiError("device.project_refused", e.reason.en, [{ reason: e.reason }]);
498
+ }
494
499
  const why = e instanceof ToolError ? ` (${clipped(e.message)})` : "";
495
500
  // the owner browse scope refusing the path is the device refusing it (gate on only)
496
501
  if ((e instanceof ToolError && e.message.startsWith(POLICY_REFUSAL_PREFIX)) ||
@@ -0,0 +1,37 @@
1
+ import { ExtensionPointDefaults } from "../extensions/point-defaults.js";
2
+ import { KernelConfigService } from "../infra/config/kernel-config.service.js";
3
+ import { KernelDb } from "../infra/database/kernel-db.js";
4
+ import type { KernelLogger } from "../log.js";
5
+ import { OrgSettingsService } from "../orgs/settings.service.js";
6
+ import { type DeviceProviderEntry, DeviceProviders } from "../sandbox/providers.js";
7
+ /**
8
+ * Which device providers serve an org: the loaded `device-provider@1` extensions switched on in it
9
+ * (valid config), and the provider a new device gets by default. Everything is read at every call
10
+ * (PG; no in-process cache). Internal providers (`@local`) are never listed here.
11
+ */
12
+ export declare class DeviceProviderSelection {
13
+ private readonly providers;
14
+ private readonly points;
15
+ private readonly orgSettings;
16
+ private readonly config;
17
+ private readonly kdb;
18
+ private readonly log;
19
+ constructor(providers: Pick<DeviceProviders, "forDevice">, points: Pick<ExtensionPointDefaults, "providersFor" | "serves">, orgSettings: Pick<OrgSettingsService, "get">, config: Pick<KernelConfigService, "get" | "fieldSource">, kdb: Pick<KernelDb, "db">, log: KernelLogger);
20
+ /** The providers that serve `orgId` now, in extension-name order. */
21
+ onInOrg(orgId: string): Promise<DeviceProviderEntry[]>;
22
+ /**
23
+ * Whether the provider `plugin` (a device's `devices.provider`) serves `orgId` now: an extension
24
+ * that {@link onInOrg} lists; a kernel-internal provider (`@local`) is no catalog item and has
25
+ * no org switch, so for it "on in the org" means registered in this process (until phase 2 of
26
+ * the device-provider plan moves local devices into the `device-local` plugin). Evaluates only
27
+ * that provider ({@link ExtensionPointDefaults.serves}, the criteria {@link onInOrg} applies).
28
+ */
29
+ isOnInOrg(orgId: string, plugin: string): Promise<boolean>;
30
+ /**
31
+ * The provider a new device of `orgId` gets: the org's `deviceDefaultProvider`, else the
32
+ * platform's `devices.defaultProvider`, returned only while it serves the org (the setting is
33
+ * validated at write time and can go stale: unknown, unloaded or off in the org is none, never
34
+ * another provider).
35
+ */
36
+ defaultFor(orgId: string): Promise<DeviceProviderEntry | undefined>;
37
+ }
@@ -0,0 +1,86 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ var __param = (this && this.__param) || function (paramIndex, decorator) {
8
+ return function (target, key) { decorator(target, key, paramIndex); }
9
+ };
10
+ import { Inject, Injectable } from "@nestjs/common";
11
+ import { ExtensionPointDefaults } from "../extensions/point-defaults.js";
12
+ import { KernelConfigService } from "../infra/config/kernel-config.service.js";
13
+ import { KernelDb } from "../infra/database/kernel-db.js";
14
+ import { KERNEL_LOGGER } from "../infra/tokens.js";
15
+ import { OrgSettingsService } from "../orgs/settings.service.js";
16
+ import { DeviceProviders, isInternalProvider, } from "../sandbox/providers.js";
17
+ import { readPlatformDefaultProvider } from "./default-provider-setting.js";
18
+ /**
19
+ * Which device providers serve an org: the loaded `device-provider@1` extensions switched on in it
20
+ * (valid config), and the provider a new device gets by default. Everything is read at every call
21
+ * (PG; no in-process cache). Internal providers (`@local`) are never listed here.
22
+ */
23
+ let DeviceProviderSelection = class DeviceProviderSelection {
24
+ providers;
25
+ points;
26
+ orgSettings;
27
+ config;
28
+ kdb;
29
+ log;
30
+ constructor(providers, points, orgSettings, config, kdb, log) {
31
+ this.providers = providers;
32
+ this.points = points;
33
+ this.orgSettings = orgSettings;
34
+ this.config = config;
35
+ this.kdb = kdb;
36
+ this.log = log;
37
+ }
38
+ /** The providers that serve `orgId` now, in extension-name order. */
39
+ async onInOrg(orgId) {
40
+ const { providers } = await this.points.providersFor(orgId, "device-provider");
41
+ const out = [];
42
+ for (const p of providers) {
43
+ const e = this.providers.forDevice(p.extension);
44
+ if (e && !isInternalProvider(e.plugin))
45
+ out.push(e);
46
+ }
47
+ return out;
48
+ }
49
+ /**
50
+ * Whether the provider `plugin` (a device's `devices.provider`) serves `orgId` now: an extension
51
+ * that {@link onInOrg} lists; a kernel-internal provider (`@local`) is no catalog item and has
52
+ * no org switch, so for it "on in the org" means registered in this process (until phase 2 of
53
+ * the device-provider plan moves local devices into the `device-local` plugin). Evaluates only
54
+ * that provider ({@link ExtensionPointDefaults.serves}, the criteria {@link onInOrg} applies).
55
+ */
56
+ async isOnInOrg(orgId, plugin) {
57
+ if (isInternalProvider(plugin))
58
+ return this.providers.forDevice(plugin) !== undefined;
59
+ if (this.providers.forDevice(plugin) === undefined)
60
+ return false;
61
+ return this.points.serves(orgId, "device-provider", plugin);
62
+ }
63
+ /**
64
+ * The provider a new device of `orgId` gets: the org's `deviceDefaultProvider`, else the
65
+ * platform's `devices.defaultProvider`, returned only while it serves the org (the setting is
66
+ * validated at write time and can go stale: unknown, unloaded or off in the org is none, never
67
+ * another provider).
68
+ */
69
+ async defaultFor(orgId) {
70
+ const name = (await this.orgSettings.get(orgId)).deviceDefaultProvider ??
71
+ (await readPlatformDefaultProvider(this.config, this.kdb.db, this.log)).provider;
72
+ if (name === null || name === undefined)
73
+ return undefined;
74
+ return (await this.onInOrg(orgId)).find((e) => e.plugin === name);
75
+ }
76
+ };
77
+ DeviceProviderSelection = __decorate([
78
+ Injectable(),
79
+ __param(0, Inject(DeviceProviders)),
80
+ __param(1, Inject(ExtensionPointDefaults)),
81
+ __param(2, Inject(OrgSettingsService)),
82
+ __param(3, Inject(KernelConfigService)),
83
+ __param(4, Inject(KernelDb)),
84
+ __param(5, Inject(KERNEL_LOGGER))
85
+ ], DeviceProviderSelection);
86
+ export { DeviceProviderSelection };
@@ -0,0 +1,3 @@
1
+ /** {@link DeviceProviderSelection}: which providers serve an org, and the default one. */
2
+ export declare class DeviceProviderSelectionModule {
3
+ }
@@ -0,0 +1,22 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ import { Module } from "@nestjs/common";
8
+ import { ExtensionsModule } from "../extensions/extensions.module.js";
9
+ import { OrgSettingsModule } from "../orgs/settings.service.js";
10
+ import { SandboxProviderRegistryModule } from "../sandbox/providers.module.js";
11
+ import { DeviceProviderSelection } from "./device-provider-selection.js";
12
+ /** {@link DeviceProviderSelection}: which providers serve an org, and the default one. */
13
+ let DeviceProviderSelectionModule = class DeviceProviderSelectionModule {
14
+ };
15
+ DeviceProviderSelectionModule = __decorate([
16
+ Module({
17
+ imports: [ExtensionsModule, OrgSettingsModule, SandboxProviderRegistryModule],
18
+ providers: [DeviceProviderSelection],
19
+ exports: [DeviceProviderSelection],
20
+ })
21
+ ], DeviceProviderSelectionModule);
22
+ export { DeviceProviderSelectionModule };