@capybara-hq/kernel 0.1.0-rc.135 → 0.1.0-rc.136

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 (92) hide show
  1. package/dist/admin/admin.module.js +3 -0
  2. package/dist/admin/health.service.d.ts +2 -2
  3. package/dist/admin/health.service.js +6 -2
  4. package/dist/catalog/marketplaces.service.d.ts +1 -1
  5. package/dist/conversations/access.d.ts +15 -7
  6. package/dist/conversations/access.js +59 -10
  7. package/dist/conversations/conversation-agent.d.ts +18 -0
  8. package/dist/conversations/conversation-agent.js +23 -0
  9. package/dist/conversations/conversation-ref.d.ts +3 -2
  10. package/dist/conversations/conversation-ref.js +6 -1
  11. package/dist/conversations/conversations.controller.d.ts +28 -5
  12. package/dist/conversations/conversations.controller.js +99 -10
  13. package/dist/conversations/conversations.module.js +10 -0
  14. package/dist/conversations/conversations.schemas.d.ts +6 -0
  15. package/dist/conversations/conversations.schemas.js +15 -0
  16. package/dist/conversations/conversations.service.d.ts +125 -21
  17. package/dist/conversations/conversations.service.js +260 -90
  18. package/dist/conversations/event-log.d.ts +9 -1
  19. package/dist/conversations/event-log.js +2 -0
  20. package/dist/conversations/events.d.ts +57 -0
  21. package/dist/conversations/events.js +73 -1
  22. package/dist/conversations/external/external-calls.d.ts +75 -0
  23. package/dist/conversations/external/external-calls.js +91 -0
  24. package/dist/conversations/external/external-calls.processor.d.ts +45 -0
  25. package/dist/conversations/external/external-calls.processor.js +144 -0
  26. package/dist/conversations/external/external-conversations.service.d.ts +266 -0
  27. package/dist/conversations/external/external-conversations.service.js +1138 -0
  28. package/dist/conversations/external/external.schemas.d.ts +63 -0
  29. package/dist/conversations/external/external.schemas.js +52 -0
  30. package/dist/conversations/external/token.d.ts +6 -0
  31. package/dist/conversations/external/token.js +6 -0
  32. package/dist/conversations/permission-profile-store.d.ts +4 -1
  33. package/dist/conversations/permission-profiles.controller.d.ts +33 -9
  34. package/dist/conversations/permission-profiles.controller.js +86 -6
  35. package/dist/conversations/prompt-suggestions.js +2 -1
  36. package/dist/conversations/queue.js +2 -1
  37. package/dist/conversations/titles.js +4 -3
  38. package/dist/ctx/create-ctx.d.ts +7 -0
  39. package/dist/ctx/create-ctx.js +45 -2
  40. package/dist/db/kernel-schema.d.ts +31 -4
  41. package/dist/device-access/host-services.js +3 -2
  42. package/dist/device-access/person-handles.d.ts +16 -7
  43. package/dist/device-access/person-handles.js +14 -9
  44. package/dist/devices/conversation-devices.d.ts +5 -0
  45. package/dist/devices/conversation-devices.js +16 -0
  46. package/dist/erasure/org-catalog.js +2 -2
  47. package/dist/errors.d.ts +34 -0
  48. package/dist/errors.js +34 -0
  49. package/dist/evals/snapshot.js +3 -1
  50. package/dist/evals/trajectory.service.d.ts +2 -2
  51. package/dist/evals/trajectory.service.js +11 -7
  52. package/dist/extensions/extensions.module.js +3 -0
  53. package/dist/extensions/host.d.ts +7 -0
  54. package/dist/extensions/host.js +11 -0
  55. package/dist/extensions/permission-view.d.ts +3 -1
  56. package/dist/extensions/permission-view.js +4 -1
  57. package/dist/extensions/point-defaults.d.ts +13 -21
  58. package/dist/extensions/point-defaults.js +32 -72
  59. package/dist/extensions/point-serving.d.ts +56 -0
  60. package/dist/extensions/point-serving.js +120 -0
  61. package/dist/inbox/inbox-items.service.d.ts +11 -0
  62. package/dist/inbox/inbox-items.service.js +39 -0
  63. package/dist/infra/outbox/registry.d.ts +70 -0
  64. package/dist/infra/outbox/registry.js +44 -0
  65. package/dist/infra/queue/queue-names.d.ts +1 -1
  66. package/dist/infra/queue/queue-names.js +1 -0
  67. package/dist/kernel.module.js +4 -0
  68. package/dist/platform/exclusions.js +10 -0
  69. package/dist/platform/ops/conversations.ops.d.ts +8 -1
  70. package/dist/platform/ops/conversations.ops.js +52 -14
  71. package/dist/plugin-reads/plugin-reads.service.js +4 -1
  72. package/dist/push/recipients.js +5 -2
  73. package/dist/retention/worker.js +2 -1
  74. package/dist/roles/worker-role.d.ts +3 -1
  75. package/dist/roles/worker-role.js +8 -2
  76. package/dist/roles/worker.module.js +3 -0
  77. package/dist/runtime/context/build.js +8 -0
  78. package/dist/runtime/context/compaction.js +7 -0
  79. package/dist/runtime/idle-arm.js +3 -1
  80. package/dist/runtime/idle-compaction.js +2 -1
  81. package/dist/runtime/run-finish.js +2 -1
  82. package/dist/runtime/run-jobs.js +2 -1
  83. package/dist/runtime/run-queue.js +4 -2
  84. package/dist/runtime/run-worker.js +5 -2
  85. package/dist/runtime/stream.js +10 -0
  86. package/dist/runtime/usage-report.js +2 -1
  87. package/dist/runtime/waits.js +2 -1
  88. package/dist/tools/approvals.service.js +6 -2
  89. package/dist/tools/builtins/skill-load.js +3 -2
  90. package/dist/tools/tool-watch.service.js +3 -1
  91. package/package.json +3 -3
  92. package/src/migrations/0227_external_conversations.sql +33 -0
@@ -0,0 +1,266 @@
1
+ import { type ConversationTargetInfo, type LocalizedName } from "@capybara-hq/plugin-sdk";
2
+ import { ConversationDevices } from "../../devices/conversation-devices.js";
3
+ import { DeviceProjectsService } from "../../devices/device-projects.service.js";
4
+ import { ExtensionDisplayNames } from "../../extensions/display-name.js";
5
+ import { ExtensionPointDefaults } from "../../extensions/point-defaults.js";
6
+ import { InboxItemsService } from "../../inbox/inbox-items.service.js";
7
+ import { KernelConfigService } from "../../infra/config/kernel-config.service.js";
8
+ import { KernelDb } from "../../infra/database/kernel-db.js";
9
+ import { TransactionHost } from "../../infra/database/transaction-host.js";
10
+ import { LockService } from "../../infra/lock/lock.service.js";
11
+ import { OutboxService } from "../../infra/outbox/outbox.service.js";
12
+ import type { KernelLogger } from "../../log.js";
13
+ import { type ConversationView } from "../conversations.service.js";
14
+ import { type EventBus, EventLog } from "../event-log.js";
15
+ import type { ConversationEvent } from "../events.js";
16
+ import { type ExternalCallJobData, type OwnedExternal } from "./external-calls.js";
17
+ /** One target the new conversation page lists (`GET …/conversation-targets`). */
18
+ export interface ConversationTargetItem {
19
+ /** The extension that provides it. */
20
+ extension: string;
21
+ /** The extension's name for people (its English display name, else its id). */
22
+ label: string;
23
+ /** The extension's display name, for `lib/extension-label.ts`; null when it has none. */
24
+ displayName: LocalizedName | null;
25
+ target: ConversationTargetInfo;
26
+ }
27
+ /** A first message that starts an external conversation (`POST …/conversations/first-message`). */
28
+ export interface ExternalFirstMessage {
29
+ extension: string;
30
+ targetKey: string;
31
+ deviceId?: string;
32
+ project?: string;
33
+ model?: string;
34
+ effort?: string;
35
+ mode?: string;
36
+ text: string;
37
+ clientMessageId: string;
38
+ /** The sender's UI language: the language of the stored target label. */
39
+ locale?: "zh" | "en";
40
+ }
41
+ /** What the first message answers (as for a chat conversation). */
42
+ export interface ExternalCreated {
43
+ conversation: ConversationView;
44
+ event: ConversationEvent;
45
+ events: ConversationEvent[];
46
+ created: boolean;
47
+ }
48
+ /** The longest title an external conversation takes from its first prompt (characters). */
49
+ export declare const EXTERNAL_TITLE_CHARS = 80;
50
+ /** What a failed turn says when a call could not reach the target within its retries. */
51
+ export declare const TARGET_UNAVAILABLE_TEXT = "The local agent is unavailable";
52
+ /** What the failed turn says when the conversation's `start` was dropped after its retries. */
53
+ export declare const START_FAILED_TEXT = "The local agent could not start this conversation.";
54
+ /**
55
+ * How old a conversation's next undelivered message may get before the stall sweep owes its
56
+ * delivery again (a lost job, or one whose retries ran out before its cursor moved).
57
+ */
58
+ export declare const EXTERNAL_STALL_MS: number;
59
+ /**
60
+ * External conversations (local agents spec Part 2, calls 6, 7, 10a, 11): conversations a person
61
+ * holds with a `conversation-target@1` target (a local agent on their device) instead of an agent.
62
+ * The kernel log mirrors them; the kernel never runs a model on them.
63
+ *
64
+ * - **Targets** ({@link targets}): each serving provider's `targets()` for the person.
65
+ * - **Writes** ({@link createWithMessage}, {@link send}, {@link answer}, {@link stop}): the
66
+ * person's message or answer is written to the log and the target call is owed through the
67
+ * outbox in the same transaction (`cap-external-calls`), at least once.
68
+ * - **Delivery** ({@link deliver}, the worker's `ExternalCallsProcessor`): `start` and `send` go
69
+ * strictly by `deliverySeq` after `external_delivered_seq`; the cursor moves in the transaction
70
+ * that marks a message delivered, which also owes the next one. A call that keeps failing (the
71
+ * target errors or no longer serves the org) is dropped after its retries with an
72
+ * `external_turn` `failed` in the log.
73
+ * - **Mirror** (`ctx.conversations.appendExternal` / `setExternalState` / `streamExternalText`):
74
+ * only the providing extension, on its own conversations, in their org.
75
+ *
76
+ * Every target call carries a person handle the kernel issued for the conversation's owner (bound
77
+ * to the conversation's device), revoked when the call settles.
78
+ */
79
+ export declare class ExternalConversationsService {
80
+ private readonly db;
81
+ readonly txHost: TransactionHost;
82
+ private readonly eventLog;
83
+ private readonly outbox;
84
+ private readonly points;
85
+ private readonly displayNames;
86
+ private readonly devices;
87
+ private readonly projects;
88
+ private readonly config;
89
+ private readonly locks;
90
+ private readonly log;
91
+ /** The owner's inbox item for each request (spec call 8). */
92
+ private readonly inbox;
93
+ private readonly bus;
94
+ constructor(db: KernelDb, txHost: TransactionHost, eventLog: Pick<EventLog, "appendIn" | "appendInReport" | "notify" | "publishChanged" | "findByIdem">, outbox: Pick<OutboxService, "add">, points: Pick<ExtensionPointDefaults, "providersFor" | "providerOf" | "serves">, displayNames: Pick<ExtensionDisplayNames, "many">, devices: Pick<ConversationDevices, "forNew" | "existingDefault">, projects: Pick<DeviceProjectsService, "resolveForBinding">, config: KernelConfigService, locks: Pick<LockService, "acquire">, log: KernelLogger,
95
+ /** The owner's inbox item for each request (spec call 8). */
96
+ inbox: Pick<InboxItemsService, "post" | "resolveKernelItem">, bus: EventBus | undefined);
97
+ /**
98
+ * The targets `accountId` can start an external conversation with: every provider serving the
99
+ * point in the org, asked within `tools.timeoutMs`; a provider that fails, times out or answers
100
+ * nothing usable is left out (logged), a target that does not parse too.
101
+ */
102
+ targets(orgId: string, accountId: string): Promise<ConversationTargetItem[]>;
103
+ /** The targets of one provider's answer that parse, each key once. */
104
+ private validTargets;
105
+ /**
106
+ * One call to a provider within `tools.timeoutMs`, with a person handle for `who` (bound to
107
+ * `who.deviceId` when given; null binds it to no device) that is revoked once the call settles.
108
+ */
109
+ private call;
110
+ /**
111
+ * Creates an external conversation with its first message: the target must be listed by its
112
+ * provider serving the org now (`conversation.target_unavailable`), and the device is one the
113
+ * target lists ({@link deviceFor}). The project binds as for a chat conversation. Writes the row
114
+ * (`kind = 'external'`, the title from the prompt), the `external_user_message` (deliverySeq 1)
115
+ * and the owed `start` in one transaction; a retry with the same `clientMessageId` answers with
116
+ * the conversation it created (`created: false`).
117
+ */
118
+ createWithMessage(orgId: string, accountId: string, req: ExternalFirstMessage): Promise<ExternalCreated>;
119
+ /**
120
+ * The device a new external conversation binds to: always one the target lists. A chosen one
121
+ * must be listed; without a choice, the person's existing default device when the target lists
122
+ * it, else the target's first listed device that is not `ready: false` (checked for the person as
123
+ * a choice would be). None → `conversation.target_device`. Never makes a device: a default device
124
+ * is not created as a side effect of choosing a target.
125
+ */
126
+ private deviceFor;
127
+ /** The row, its first message and the owed `start` (undefined: a concurrent retry was first). */
128
+ private createIn;
129
+ /**
130
+ * What an earlier first message with this `clientMessageId` created; the id used for another
131
+ * conversation (another kind or target) → `conflict`.
132
+ */
133
+ private findCreated;
134
+ private view;
135
+ /**
136
+ * The person's external conversation, if `conversationId` is one (null: another kind, or not
137
+ * theirs: the caller treats it as any conversation).
138
+ */
139
+ find(orgId: string, conversationId: string, accountId: string): Promise<OwnedExternal | null>;
140
+ /** {@link find} for a write: not an external conversation of theirs → not found; not served → unavailable. */
141
+ private writableExternal;
142
+ /**
143
+ * A later message (`POST …/messages` on an external conversation): written as an
144
+ * `external_user_message` with the next `deliverySeq`, `send` owed in the same transaction. A
145
+ * retry with the same `clientMessageId` answers with the message it wrote and owes nothing.
146
+ */
147
+ send(orgId: string, conversationId: string, accountId: string, input: {
148
+ text: string;
149
+ interrupt: boolean;
150
+ clientMessageId?: string;
151
+ }): Promise<ConversationEvent>;
152
+ private sendIn;
153
+ /**
154
+ * The person's answer to a target's `external_request`: `external_request_answered` written once
155
+ * (a second answer → 409 `conversation.request_answered`), `answer` owed. An unknown request →
156
+ * 404 `conversation.request_not_found`; an option it does not offer → 400.
157
+ */
158
+ answer(orgId: string, conversationId: string, accountId: string, requestId: string, optionId: string): Promise<ConversationEvent>;
159
+ private answerIn;
160
+ /** Stop (`POST …/abort` on an external conversation): `stop` owed. */
161
+ stop(orgId: string, conversationId: string, accountId: string): Promise<void>;
162
+ private stopIn;
163
+ /** Locks the live external conversation's row; returns its last event seq and delivery state. */
164
+ private lockLive;
165
+ /**
166
+ * One owed call. `start` / `send` go one at a time per conversation (a lock, only tried: a
167
+ * delivery that finds it held ends at once without spending an attempt; the holder looks again
168
+ * after releasing it and owes the next message as a `-tail` job), and only when their
169
+ * `deliverySeq` is the next after the conversation's cursor: an earlier one is a repeat (done),
170
+ * a later one waits (the delivery of the one before owes it again; the stall sweep backs that
171
+ * up). `answer` and `stop` wait for the start. Only `deleted` goes to a deleted conversation or
172
+ * one whose start was dropped. A provider that does not serve the org, or a call that fails,
173
+ * fails the job (retried); on the `last` attempt the call is dropped (logged) and, but for
174
+ * `deleted`, the log gets an `external_turn` `failed` ({@link dropIn}).
175
+ */
176
+ deliver(d: ExternalCallJobData, last: boolean, signal: AbortSignal): Promise<void>;
177
+ /**
178
+ * After a delivery released the conversation's lock: when the next message after the cursor is
179
+ * written (a delivery of it may have found the lock held and ended), owes it once more as a
180
+ * `-tail` job. Nothing for a deleted conversation or one whose start was dropped.
181
+ */
182
+ private oweTailIfDeliverable;
183
+ /** Owes the delivery of message `seq` (the start for seq 1) under the given id suffix. */
184
+ private oweMessage;
185
+ /**
186
+ * The stall sweep (`ExternalCallsSweepJob`): every live external conversation whose next
187
+ * undelivered message is older than {@link EXTERNAL_STALL_MS} (its job lost, or failed for good
188
+ * before the cursor moved) gets that delivery owed again, under one id per stall window. Returns
189
+ * how many were owed.
190
+ */
191
+ resumeStalled(signal?: AbortSignal): Promise<number>;
192
+ private deliverOne;
193
+ /** The `external_user_message` of `deliverySeq`, as the target receives it. */
194
+ private userMessage;
195
+ /**
196
+ * Marks message `seq` delivered: the cursor moves to it (only from `seq - 1`), then, under the
197
+ * row lock that move took (a send takes it before choosing its seq), the next message, if one is
198
+ * written, is owed again under `-next`: its own job may have run and waited before this commit.
199
+ */
200
+ private markDelivered;
201
+ /**
202
+ * A call dropped after its retries. A dropped `send` still moves the cursor (the next message is
203
+ * not held behind it), the state goes back to `idle`. A dropped `start` moves the cursor from 0
204
+ * to {@link START_DROPPED_SEQ} and ends the conversation (`ended`): no later message is ever
205
+ * delivered as a `send` to a session that never started, and the person's sends are refused
206
+ * (`conversation.external_ended`).
207
+ * The log gets an `external_turn` `failed` saying which. Returns the seq to notify (null:
208
+ * nothing written).
209
+ */
210
+ private dropIn;
211
+ /**
212
+ * `ctx.conversations.appendExternal` of `extension`: the events are copied, checked
213
+ * (`parseExternalEvents`) and appended in one transaction, each idempotent by its own id (an
214
+ * activity by id and status); refused (`CtxReadError`) unless the conversation is a live
215
+ * external conversation of `extension` in `orgId` and `extension` serves the org.
216
+ */
217
+ appendExternal(extension: string, args: {
218
+ orgId: string;
219
+ conversationId: string;
220
+ events: unknown;
221
+ }): Promise<void>;
222
+ private appendIn;
223
+ /**
224
+ * The owner's personal inbox item for a target's request (local agents spec call 8): a kernel
225
+ * item of kind `external_request` (title = the request's title, body = its detail, each cut to
226
+ * the inbox limits) linking to the conversation, where the request is answered; its push is owed
227
+ * with it. One per request (dedupe key), resolved when the request is answered ({@link answerIn})
228
+ * or the conversation is deleted (`ConversationsService.markDeleted`). An item the inbox refuses is
229
+ * posted again in its shortest form (the fixed title, no body); only an owner who cannot take
230
+ * items at all (no longer an active member) gets none, logged: the card in the conversation stays.
231
+ */
232
+ private postRequestItem;
233
+ /** `ctx.conversations.setExternalState` of `extension` (refused as {@link appendExternal}). */
234
+ setExternalState(extension: string, args: {
235
+ orgId: string;
236
+ conversationId: string;
237
+ state: unknown;
238
+ }): Promise<void>;
239
+ private setStateIn;
240
+ /**
241
+ * `ctx.conversations.streamExternalText` of `extension` (the ctx checks the arguments): a bus
242
+ * hint `external_delta` on the conversation's topic, never stored; dropped (logged) for a
243
+ * conversation that is not a live one of `extension` in the org.
244
+ */
245
+ streamExternalText(extension: string, args: {
246
+ orgId: string;
247
+ conversationId: string;
248
+ messageId: string;
249
+ text: string;
250
+ }): Promise<void>;
251
+ /** Refuses an extension that does not serve the point in the org. */
252
+ private assertServes;
253
+ /**
254
+ * The live external conversation of `extension` in `orgId` (row-locked when `lock`), its state;
255
+ * anything else is `invalid_args`, alike for another extension's, another org's or none.
256
+ */
257
+ private ownRow;
258
+ }
259
+ /** What `ctx.conversations`' external methods call through (found at the time of use). */
260
+ export type ExternalConversationsBackend = Pick<ExternalConversationsService, "appendExternal" | "setExternalState" | "streamExternalText">;
261
+ /**
262
+ * External conversations on every role: the service (routes, the ctx mirror through
263
+ * {@link EXTERNAL_CONVERSATIONS_BACKEND}) and the queue the outbox adds their calls to.
264
+ */
265
+ export declare class ExternalConversationsModule {
266
+ }