@capybara-hq/kernel 0.1.0-rc.134 → 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.
- package/bootstrap.json +1 -1
- package/dist/admin/admin.module.js +3 -0
- package/dist/admin/health.service.d.ts +2 -2
- package/dist/admin/health.service.js +6 -2
- package/dist/catalog/marketplaces.service.d.ts +1 -1
- package/dist/config/schema.d.ts +7 -0
- package/dist/config/schema.js +60 -2
- package/dist/conversations/access.d.ts +15 -7
- package/dist/conversations/access.js +59 -10
- package/dist/conversations/conversation-agent.d.ts +18 -0
- package/dist/conversations/conversation-agent.js +23 -0
- package/dist/conversations/conversation-ref.d.ts +3 -2
- package/dist/conversations/conversation-ref.js +6 -1
- package/dist/conversations/conversations.controller.d.ts +28 -5
- package/dist/conversations/conversations.controller.js +99 -10
- package/dist/conversations/conversations.module.js +10 -0
- package/dist/conversations/conversations.schemas.d.ts +6 -0
- package/dist/conversations/conversations.schemas.js +15 -0
- package/dist/conversations/conversations.service.d.ts +125 -21
- package/dist/conversations/conversations.service.js +260 -90
- package/dist/conversations/event-log.d.ts +9 -1
- package/dist/conversations/event-log.js +2 -0
- package/dist/conversations/events.d.ts +57 -0
- package/dist/conversations/events.js +73 -1
- package/dist/conversations/external/external-calls.d.ts +75 -0
- package/dist/conversations/external/external-calls.js +91 -0
- package/dist/conversations/external/external-calls.processor.d.ts +45 -0
- package/dist/conversations/external/external-calls.processor.js +144 -0
- package/dist/conversations/external/external-conversations.service.d.ts +266 -0
- package/dist/conversations/external/external-conversations.service.js +1138 -0
- package/dist/conversations/external/external.schemas.d.ts +63 -0
- package/dist/conversations/external/external.schemas.js +52 -0
- package/dist/conversations/external/token.d.ts +6 -0
- package/dist/conversations/external/token.js +6 -0
- package/dist/conversations/permission-profile-store.d.ts +4 -1
- package/dist/conversations/permission-profiles.controller.d.ts +33 -9
- package/dist/conversations/permission-profiles.controller.js +86 -6
- package/dist/conversations/prompt-suggestions.js +2 -1
- package/dist/conversations/queue.js +2 -1
- package/dist/conversations/titles.js +4 -3
- package/dist/ctx/create-ctx.d.ts +7 -0
- package/dist/ctx/create-ctx.js +45 -2
- package/dist/db/extension-db.d.ts +8 -3
- package/dist/db/extension-db.js +107 -7
- package/dist/db/extension-role-provision.d.ts +9 -0
- package/dist/db/extension-role-provision.js +89 -0
- package/dist/db/extension-schema-transfer.d.ts +2 -1
- package/dist/db/extension-schema-transfer.js +103 -19
- package/dist/db/kernel-schema.d.ts +31 -4
- package/dist/db/migrator.d.ts +2 -0
- package/dist/db/migrator.js +4 -3
- package/dist/device-access/host-services.js +3 -2
- package/dist/device-access/person-handles.d.ts +16 -7
- package/dist/device-access/person-handles.js +14 -9
- package/dist/devices/conversation-devices.d.ts +5 -0
- package/dist/devices/conversation-devices.js +16 -0
- package/dist/erasure/org-catalog.js +2 -2
- package/dist/errors.d.ts +34 -0
- package/dist/errors.js +34 -0
- package/dist/evals/snapshot.js +3 -1
- package/dist/evals/trajectory.service.d.ts +2 -2
- package/dist/evals/trajectory.service.js +11 -7
- package/dist/extensions/admin-ops.js +17 -1
- package/dist/extensions/extensions.module.js +3 -0
- package/dist/extensions/host.d.ts +9 -1
- package/dist/extensions/host.js +14 -2
- package/dist/extensions/migrate-lock.js +1 -0
- package/dist/extensions/permission-view.d.ts +3 -1
- package/dist/extensions/permission-view.js +4 -1
- package/dist/extensions/point-defaults.d.ts +13 -21
- package/dist/extensions/point-defaults.js +32 -72
- package/dist/extensions/point-serving.d.ts +56 -0
- package/dist/extensions/point-serving.js +120 -0
- package/dist/inbox/inbox-items.service.d.ts +11 -0
- package/dist/inbox/inbox-items.service.js +39 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.js +3 -1
- package/dist/infra/database/rls-role.d.ts +4 -11
- package/dist/infra/database/rls-role.js +71 -27
- package/dist/infra/outbox/registry.d.ts +70 -0
- package/dist/infra/outbox/registry.js +44 -0
- package/dist/infra/queue/queue-names.d.ts +1 -1
- package/dist/infra/queue/queue-names.js +1 -0
- package/dist/kernel.d.ts +2 -0
- package/dist/kernel.js +12 -2
- package/dist/kernel.module.js +4 -0
- package/dist/lifecycle/kernel-lifecycle.js +2 -3
- package/dist/platform/exclusions.js +10 -0
- package/dist/platform/ops/conversations.ops.d.ts +8 -1
- package/dist/platform/ops/conversations.ops.js +52 -14
- package/dist/plugin-reads/plugin-reads.service.js +4 -1
- package/dist/push/recipients.js +5 -2
- package/dist/retention/worker.js +2 -1
- package/dist/roles/worker-role.d.ts +3 -1
- package/dist/roles/worker-role.js +8 -2
- package/dist/roles/worker.module.js +3 -0
- package/dist/runtime/context/build.js +8 -0
- package/dist/runtime/context/compaction.js +7 -0
- package/dist/runtime/idle-arm.js +3 -1
- package/dist/runtime/idle-compaction.js +2 -1
- package/dist/runtime/run-finish.js +2 -1
- package/dist/runtime/run-jobs.js +2 -1
- package/dist/runtime/run-queue.js +4 -2
- package/dist/runtime/run-worker.js +5 -2
- package/dist/runtime/stream.js +10 -0
- package/dist/runtime/usage-report.js +2 -1
- package/dist/runtime/waits.js +2 -1
- package/dist/tools/approvals.service.js +6 -2
- package/dist/tools/builtins/skill-load.js +3 -2
- package/dist/tools/tool-watch.service.js +3 -1
- package/package.json +3 -3
- package/src/migrations/0226_enable_kernel_rls.sql +30 -0
- package/src/migrations/0227_external_conversations.sql +33 -0
|
@@ -11,6 +11,7 @@ import { isDeviceProjectName } from "@capybara-hq/plugin-sdk";
|
|
|
11
11
|
import { Controller, Delete, Get, Header, HttpCode, Inject, Param, Patch, Post, Res, } from "@nestjs/common";
|
|
12
12
|
import { z } from "zod";
|
|
13
13
|
import { ModelRef } from "../agents/definition.js";
|
|
14
|
+
import { ApiError } from "../errors.js";
|
|
14
15
|
import { requestSignal } from "../http/request-signal.js";
|
|
15
16
|
import { CurrentPrincipal, requireUser } from "../identity/principal.js";
|
|
16
17
|
import { createZodDto } from "../infra/validation/create-zod-dto.js";
|
|
@@ -20,9 +21,11 @@ import { MAX_ATTACHMENTS } from "../tools/connector-content.js";
|
|
|
20
21
|
import { ConversationToolInfo, TOOL_INFO_KEY_MAX, TOOL_INFO_MAX_KEYS, } from "../tools/info.js";
|
|
21
22
|
import { ARTIFACT_DOWNLOAD_CACHE_SECONDS, ConversationArtifacts, } from "./artifacts.js";
|
|
22
23
|
import { MAX_CONVERSATION_REFS } from "./conversation-ref.js";
|
|
23
|
-
import { CompactBody, DOWNLOAD_AS, EventsQuery, FilesQuery, IdParams, ListQuery, PatchBody, SubagentsQuery, searchFields, } from "./conversations.schemas.js";
|
|
24
|
+
import { CompactBody, DOWNLOAD_AS, EventsQuery, FilesQuery, IdParams, ListQuery, PatchBody, SubagentsQuery, searchFields, targetRefOf, } from "./conversations.schemas.js";
|
|
24
25
|
import { ConversationsService, MAX_PROJECT_FILES, } from "./conversations.service.js";
|
|
25
26
|
import { ConversationDeviceStatus } from "./device-status.js";
|
|
27
|
+
import { ExternalAnswerBody, ExternalRequestParams } from "./external/external.schemas.js";
|
|
28
|
+
import { ExternalConversationsService } from "./external/external-conversations.service.js";
|
|
26
29
|
import { ConversationFiles, FILE_CONTENT_MAX_BYTES } from "./files.js";
|
|
27
30
|
import { countMarkers, MAX_CONNECTOR_MENTIONS, MAX_TOKENS, tokenEntityKey, } from "./inline-tokens.js";
|
|
28
31
|
import { ConversationOutputs, OUTPUT_PAGE_MAX_CHARS } from "./outputs-read.js";
|
|
@@ -30,8 +33,11 @@ import { ConversationQueue } from "./queue.service.js";
|
|
|
30
33
|
const GroupsQuery = z.object({
|
|
31
34
|
perGroup: z.coerce.number().int().min(1).max(20).default(5),
|
|
32
35
|
limit: z.coerce.number().int().min(1).max(50).default(20),
|
|
33
|
-
/**
|
|
34
|
-
|
|
36
|
+
/**
|
|
37
|
+
* The last group's key from the previous page (`next`): an agent id, or
|
|
38
|
+
* `target/<extension>/<targetKey>`.
|
|
39
|
+
*/
|
|
40
|
+
after: z.string().min(1).max(200).optional(),
|
|
35
41
|
...searchFields,
|
|
36
42
|
});
|
|
37
43
|
/** `?keys=a,b`: 1–100 comma-separated tool keys of 1–200 characters each. */
|
|
@@ -173,9 +179,17 @@ function tokenRules(b, ctx) {
|
|
|
173
179
|
}
|
|
174
180
|
}
|
|
175
181
|
}
|
|
176
|
-
|
|
182
|
+
/**
|
|
183
|
+
* `POST …/messages`. On an external conversation only `text` (with `clientMessageId` and the
|
|
184
|
+
* browser context) and `interrupt` (stop the target's current turn first) are taken; the other
|
|
185
|
+
* fields are `conversation.external_fields`, and so is `interrupt` on any other conversation.
|
|
186
|
+
*/
|
|
187
|
+
const MessageBody = MessageFields.extend({ interrupt: z.boolean().optional() })
|
|
188
|
+
.strict()
|
|
177
189
|
.refine(...textRule)
|
|
178
190
|
.superRefine(tokenRules);
|
|
191
|
+
/** The message fields an external conversation does not take. */
|
|
192
|
+
const NOT_EXTERNAL_FIELDS = ["tokens", "files", "model", "effort", "retryOf", "skill"];
|
|
179
193
|
/**
|
|
180
194
|
* `POST /conversations`: `message` creates the conversation with its first message (create on
|
|
181
195
|
* first send; `clientMessageId` is required and makes a retry return the same conversation).
|
|
@@ -235,6 +249,10 @@ class ConversationQueueEditBodyDto extends createZodDto(QueueEditBody, "Conversa
|
|
|
235
249
|
}
|
|
236
250
|
class ConversationCompactBodyDto extends createZodDto(CompactBody, "ConversationCompactBody") {
|
|
237
251
|
}
|
|
252
|
+
class ExternalRequestParamsDto extends createZodDto(ExternalRequestParams, "ConversationExternalRequestParams") {
|
|
253
|
+
}
|
|
254
|
+
class ExternalAnswerBodyDto extends createZodDto(ExternalAnswerBody, "ConversationExternalAnswer") {
|
|
255
|
+
}
|
|
238
256
|
/**
|
|
239
257
|
* 会话(`/api/v1/orgs/:orgId/conversations`):`agents.use`(访客以上),只接受人类用户;
|
|
240
258
|
* 服务令牌 → 403 `forbidden`。只有会话所有者可见,其他人一律 `conversation.not_found`。
|
|
@@ -247,7 +265,8 @@ let ConversationsController = class ConversationsController {
|
|
|
247
265
|
toolInfo;
|
|
248
266
|
queue;
|
|
249
267
|
files;
|
|
250
|
-
|
|
268
|
+
external;
|
|
269
|
+
constructor(conversations, outputs, artifacts, deviceStatus, toolInfo, queue, files, external) {
|
|
251
270
|
this.conversations = conversations;
|
|
252
271
|
this.outputs = outputs;
|
|
253
272
|
this.artifacts = artifacts;
|
|
@@ -255,6 +274,7 @@ let ConversationsController = class ConversationsController {
|
|
|
255
274
|
this.toolInfo = toolInfo;
|
|
256
275
|
this.queue = queue;
|
|
257
276
|
this.files = files;
|
|
277
|
+
this.external = external;
|
|
258
278
|
}
|
|
259
279
|
/**
|
|
260
280
|
* 201 when created; 200 when `message.clientMessageId` repeats an earlier first send. With a
|
|
@@ -314,7 +334,8 @@ let ConversationsController = class ConversationsController {
|
|
|
314
334
|
await this.conversations.remove(org.orgId, id, p.account.id);
|
|
315
335
|
}
|
|
316
336
|
/**
|
|
317
|
-
* My conversations, most recently updated first, each with its agent
|
|
337
|
+
* My conversations, most recently updated first, each with its agent (null for an external
|
|
338
|
+
* conversation; `target` keeps one target's, local agents spec Part 2): `next` is the next page's
|
|
318
339
|
* cursor (passed back as `after`), null when there are no more; `total` counts every match of
|
|
319
340
|
* the filters and comes only when asked (`withTotal=true`) on the first page (no `after`).
|
|
320
341
|
*/
|
|
@@ -325,6 +346,7 @@ let ConversationsController = class ConversationsController {
|
|
|
325
346
|
total: q.withTotal === "true" && q.after === undefined,
|
|
326
347
|
...(q.after !== undefined && { after: q.after }),
|
|
327
348
|
...(q.agentId !== undefined && { agentId: q.agentId }),
|
|
349
|
+
...(q.target !== undefined && { target: targetRefOf(q.target) }),
|
|
328
350
|
...(q.q !== undefined && { q: q.q }),
|
|
329
351
|
...(q.state !== undefined && { state: q.state }),
|
|
330
352
|
...(q.archived !== undefined && { archived: true }),
|
|
@@ -334,8 +356,9 @@ let ConversationsController = class ConversationsController {
|
|
|
334
356
|
return { conversations, next, ...(total !== undefined && { total }) };
|
|
335
357
|
}
|
|
336
358
|
/**
|
|
337
|
-
* My conversations grouped by agent, for the sidebar: groups by
|
|
338
|
-
* newest conversations each with `more`; `next` is the last group's
|
|
359
|
+
* My conversations grouped by agent, and my external ones by target, for the sidebar: groups by
|
|
360
|
+
* latest activity, `perGroup` newest conversations each with `more`; `next` is the last group's
|
|
361
|
+
* key (passed back as
|
|
339
362
|
* `after`), null when there are no more groups. Declared before `:id` so it is not captured.
|
|
340
363
|
*/
|
|
341
364
|
async groups(principal, org, q) {
|
|
@@ -477,8 +500,46 @@ let ConversationsController = class ConversationsController {
|
|
|
477
500
|
}
|
|
478
501
|
async postMessage(principal, org, id, body, reply) {
|
|
479
502
|
const p = requireUser(principal);
|
|
503
|
+
const toExternal = async () => {
|
|
504
|
+
if (!(await this.external.find(org.orgId, id, p.account.id)))
|
|
505
|
+
return null;
|
|
506
|
+
const extra = NOT_EXTERNAL_FIELDS.filter((k) => body[k] !== undefined);
|
|
507
|
+
if (extra.length > 0)
|
|
508
|
+
throw new ApiError("conversation.external_fields", `A local agent's conversation takes text only, not ${extra.join(", ")}`);
|
|
509
|
+
return {
|
|
510
|
+
event: await this.external.send(org.orgId, id, p.account.id, {
|
|
511
|
+
text: body.text,
|
|
512
|
+
interrupt: body.interrupt ?? false,
|
|
513
|
+
...(body.clientMessageId !== undefined && { clientMessageId: body.clientMessageId }),
|
|
514
|
+
}),
|
|
515
|
+
};
|
|
516
|
+
};
|
|
517
|
+
if (body.interrupt !== undefined) {
|
|
518
|
+
const sent = await toExternal();
|
|
519
|
+
if (sent)
|
|
520
|
+
return sent;
|
|
521
|
+
throw new ApiError("conversation.external_fields", "interrupt is only for a local agent's conversation");
|
|
522
|
+
}
|
|
523
|
+
// A chat message pays no external lookup: the chat path's own row read refuses an external
|
|
524
|
+
// conversation (`external_read_only`, or not found while its target does not serve the org),
|
|
525
|
+
// and only then is it looked up as one.
|
|
526
|
+
try {
|
|
527
|
+
return await this.postChatMessage(org.orgId, id, p.account.id, body, reply);
|
|
528
|
+
}
|
|
529
|
+
catch (err) {
|
|
530
|
+
if (err instanceof ApiError &&
|
|
531
|
+
(err.code === "conversation.external_read_only" || err.code === "conversation.not_found")) {
|
|
532
|
+
const sent = await toExternal();
|
|
533
|
+
if (sent)
|
|
534
|
+
return sent;
|
|
535
|
+
}
|
|
536
|
+
throw err;
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
/** {@link postMessage} on a conversation Capybara runs. */
|
|
540
|
+
async postChatMessage(orgId, id, accountId, body, reply) {
|
|
480
541
|
return {
|
|
481
|
-
event: await this.conversations.postUserMessage(
|
|
542
|
+
event: await this.conversations.postUserMessage(orgId, id, accountId, {
|
|
482
543
|
text: body.text,
|
|
483
544
|
...(body.clientMessageId !== undefined && { clientMessageId: body.clientMessageId }),
|
|
484
545
|
...(body.tokens !== undefined && { tokens: body.tokens }),
|
|
@@ -527,11 +588,29 @@ let ConversationsController = class ConversationsController {
|
|
|
527
588
|
* ran). On a sub-agent's conversation (the root's owner, at any depth) the
|
|
528
589
|
* sub-agent stops with every sub-agent below it, and `runId` is the sub-agent's latest run,
|
|
529
590
|
* which may already be finished when only its descendants were working (agent map spec §6).
|
|
591
|
+
* On an external conversation the target's `stop` is owed and `runId` is null.
|
|
530
592
|
*/
|
|
531
593
|
async abort(principal, org, id) {
|
|
532
594
|
const p = requireUser(principal);
|
|
595
|
+
// an external conversation has no runs: its target is asked to stop (owed, `runId` null)
|
|
596
|
+
if (await this.external.find(org.orgId, id, p.account.id)) {
|
|
597
|
+
await this.external.stop(org.orgId, id, p.account.id);
|
|
598
|
+
return { runId: null };
|
|
599
|
+
}
|
|
533
600
|
return { runId: await this.conversations.abort(org.orgId, id, p.account.id) };
|
|
534
601
|
}
|
|
602
|
+
/**
|
|
603
|
+
* The person's answer to a local agent's request (`external_request`, local agents spec Part 2,
|
|
604
|
+
* call 8): 202 `{ event }`, the `external_request_answered` written once (the first answer wins;
|
|
605
|
+
* a second → 409 `conversation.request_answered`) and the target's `answer` owed. An unknown
|
|
606
|
+
* request → 404 `conversation.request_not_found`; an option it does not offer → 400.
|
|
607
|
+
*/
|
|
608
|
+
async answerExternal(principal, org, params, body) {
|
|
609
|
+
const p = requireUser(principal);
|
|
610
|
+
return {
|
|
611
|
+
event: await this.external.answer(org.orgId, params.id, p.account.id, params.requestId, body.optionId),
|
|
612
|
+
};
|
|
613
|
+
}
|
|
535
614
|
/**
|
|
536
615
|
* Manual compaction (context compaction spec §4.7): starts a compact run that summarizes the
|
|
537
616
|
* conversation's earlier history and returns its id (`202 { runId }`); while one is queued or
|
|
@@ -749,6 +828,15 @@ __decorate([
|
|
|
749
828
|
__param(1, CurrentOrg()),
|
|
750
829
|
__param(2, Param("id"))
|
|
751
830
|
], ConversationsController.prototype, "abort", null);
|
|
831
|
+
__decorate([
|
|
832
|
+
Post(":id/external-requests/:requestId"),
|
|
833
|
+
HttpCode(202),
|
|
834
|
+
OrgAction("agents.use"),
|
|
835
|
+
__param(0, CurrentPrincipal()),
|
|
836
|
+
__param(1, CurrentOrg()),
|
|
837
|
+
__param(2, ZParam(ExternalRequestParamsDto)),
|
|
838
|
+
__param(3, ZBody(ExternalAnswerBodyDto))
|
|
839
|
+
], ConversationsController.prototype, "answerExternal", null);
|
|
752
840
|
__decorate([
|
|
753
841
|
Post(":id/compact"),
|
|
754
842
|
HttpCode(202),
|
|
@@ -766,6 +854,7 @@ ConversationsController = __decorate([
|
|
|
766
854
|
__param(3, Inject(ConversationDeviceStatus)),
|
|
767
855
|
__param(4, Inject(ConversationToolInfo)),
|
|
768
856
|
__param(5, Inject(ConversationQueue)),
|
|
769
|
-
__param(6, Inject(ConversationFiles))
|
|
857
|
+
__param(6, Inject(ConversationFiles)),
|
|
858
|
+
__param(7, Inject(ExternalConversationsService))
|
|
770
859
|
], ConversationsController);
|
|
771
860
|
export { ConversationsController };
|
|
@@ -8,7 +8,9 @@ import { Module } from "@nestjs/common";
|
|
|
8
8
|
import { AgentLookupModule } from "../agents/agent-lookup.js";
|
|
9
9
|
import { BackgroundModule } from "../background/background.module.js";
|
|
10
10
|
import { DevicesModule } from "../devices/devices.module.js";
|
|
11
|
+
import { ExtensionsModule } from "../extensions/extensions.module.js";
|
|
11
12
|
import { LibraryReadModule } from "../files/library-read.module.js";
|
|
13
|
+
import { InboxItemsModule } from "../inbox/inbox-items.service.js";
|
|
12
14
|
import { ProvidersModule } from "../runtime/providers.js";
|
|
13
15
|
import { RuntimeModule } from "../runtime/runtime.module.js";
|
|
14
16
|
import { OperationAuthorityModule } from "../sandbox/operation-authority.module.js";
|
|
@@ -23,6 +25,7 @@ import { AttachmentsModule } from "./attachments.module.js";
|
|
|
23
25
|
import { ConversationsService } from "./conversations.service.js";
|
|
24
26
|
import { ConversationDeviceStatus } from "./device-status.js";
|
|
25
27
|
import { EventLogModule } from "./event-log.js";
|
|
28
|
+
import { ExternalConversationsModule } from "./external/external-conversations.service.js";
|
|
26
29
|
import { ConversationFiles } from "./files.js";
|
|
27
30
|
import { ConversationLeakCheckModule } from "./leak-check.js";
|
|
28
31
|
import { ConversationOutputs } from "./outputs-read.js";
|
|
@@ -62,6 +65,12 @@ ConversationsModule = __decorate([
|
|
|
62
65
|
BackgroundModule,
|
|
63
66
|
// the leak check of a person's card values (its own module: the tools module uses it too)
|
|
64
67
|
ConversationLeakCheckModule,
|
|
68
|
+
// external conversations (their deletion is the conversation's; their routes share ours)
|
|
69
|
+
ExternalConversationsModule,
|
|
70
|
+
// a deleted external conversation's open request items leave the owner's inbox
|
|
71
|
+
InboxItemsModule,
|
|
72
|
+
// whether an external conversation's target serves the org
|
|
73
|
+
ExtensionsModule,
|
|
65
74
|
],
|
|
66
75
|
providers: [
|
|
67
76
|
PermissionProfilesService,
|
|
@@ -86,6 +95,7 @@ ConversationsModule = __decorate([
|
|
|
86
95
|
ConversationFiles,
|
|
87
96
|
ConversationDeviceStatus,
|
|
88
97
|
ConversationLeakCheckModule,
|
|
98
|
+
ExternalConversationsModule,
|
|
89
99
|
],
|
|
90
100
|
})
|
|
91
101
|
], ConversationsModule);
|
|
@@ -32,11 +32,17 @@ export declare const ListQuery: z.ZodObject<{
|
|
|
32
32
|
after: z.ZodOptional<z.ZodString>;
|
|
33
33
|
limit: z.ZodDefault<z.ZodCoercedNumber<unknown>>;
|
|
34
34
|
agentId: z.ZodOptional<z.ZodString>;
|
|
35
|
+
target: z.ZodOptional<z.ZodString>;
|
|
35
36
|
withTotal: z.ZodOptional<z.ZodEnum<{
|
|
36
37
|
false: "false";
|
|
37
38
|
true: "true";
|
|
38
39
|
}>>;
|
|
39
40
|
}, z.core.$strip>;
|
|
41
|
+
/** A list query's `target` (`<extension>/<targetKey>`, already checked) as its parts. */
|
|
42
|
+
export declare function targetRefOf(target: string): {
|
|
43
|
+
extension: string;
|
|
44
|
+
targetKey: string;
|
|
45
|
+
};
|
|
40
46
|
export declare const EventsQuery: z.ZodObject<{
|
|
41
47
|
after: z.ZodOptional<z.ZodCoercedNumber<unknown>>;
|
|
42
48
|
before: z.ZodOptional<z.ZodUnion<readonly [z.ZodLiteral<"end">, z.ZodCoercedNumber<unknown>]>>;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
import { CONTROL_OR_HIDDEN_RE } from "../text.js";
|
|
3
3
|
import { CONVERSATION_TITLE_MAX_CHARS } from "./conversations.service.js";
|
|
4
|
+
import { TARGET_KEY_RE } from "./external/external.schemas.js";
|
|
4
5
|
import { FILES_PAGE_MAX } from "./files.js";
|
|
5
6
|
/**
|
|
6
7
|
* The download routes' answer form: `as=url` answers `{ url }` (the presigned location the 302
|
|
@@ -20,6 +21,15 @@ export const ListQuery = z.object({
|
|
|
20
21
|
limit: z.coerce.number().int().min(1).max(200).default(50),
|
|
21
22
|
/** Only this agent's conversations (the sidebar's per-group "More"). */
|
|
22
23
|
agentId: z.string().min(1).max(100).optional(),
|
|
24
|
+
/**
|
|
25
|
+
* Only the external conversations with this target, as `<extension>/<targetKey>` (a target
|
|
26
|
+
* group's "More", local agents spec Part 2).
|
|
27
|
+
*/
|
|
28
|
+
target: z
|
|
29
|
+
.string()
|
|
30
|
+
.max(164)
|
|
31
|
+
.regex(new RegExp(`^[^/]{1,100}/${TARGET_KEY_RE.source.slice(1, -1)}$`), "target must be <extension>/<targetKey>")
|
|
32
|
+
.optional(),
|
|
23
33
|
/**
|
|
24
34
|
* `true`: the first page (no `after`) also returns `total`, the count of every match. Absent or
|
|
25
35
|
* `false`: no count is selected (the count costs O(matches); a polled list never asks for it).
|
|
@@ -27,6 +37,11 @@ export const ListQuery = z.object({
|
|
|
27
37
|
withTotal: z.enum(["true", "false"]).optional(),
|
|
28
38
|
...searchFields,
|
|
29
39
|
});
|
|
40
|
+
/** A list query's `target` (`<extension>/<targetKey>`, already checked) as its parts. */
|
|
41
|
+
export function targetRefOf(target) {
|
|
42
|
+
const at = target.lastIndexOf("/");
|
|
43
|
+
return { extension: target.slice(0, at), targetKey: target.slice(at + 1) };
|
|
44
|
+
}
|
|
30
45
|
export const EventsQuery = z
|
|
31
46
|
.object({
|
|
32
47
|
after: z.coerce.number().int().min(0).optional(),
|
|
@@ -1,14 +1,16 @@
|
|
|
1
|
-
import type { CacheWriteByTtl, ConversationSource, TriggerSubject } from "@capybara-hq/plugin-sdk";
|
|
1
|
+
import type { CacheWriteByTtl, ConversationSource, ExternalConversationState, TriggerSubject } from "@capybara-hq/plugin-sdk";
|
|
2
2
|
import { AgentLookup } from "../agents/agent-lookup.js";
|
|
3
3
|
import type { AgentDefinition, ModelRef } from "../agents/definition.js";
|
|
4
4
|
import type { AgentAvatarView } from "../agents/profile.js";
|
|
5
5
|
import type { AutomationEventMarker, ScheduleMarker } from "../automations/types.js";
|
|
6
|
-
import type { ConversationSubject, TitleSource } from "../db/kernel-schema.js";
|
|
6
|
+
import type { ConversationKind, ConversationSubject, ExternalRef, TitleSource } from "../db/kernel-schema.js";
|
|
7
7
|
import { ConversationDevices, type NewConversationDevice } from "../devices/conversation-devices.js";
|
|
8
8
|
import { DeviceProjectFilesService } from "../devices/device-project-files.service.js";
|
|
9
9
|
import { DeviceProjectsService } from "../devices/device-projects.service.js";
|
|
10
|
+
import { ExtensionPointDefaults } from "../extensions/point-defaults.js";
|
|
10
11
|
import { LibraryFilesService } from "../files/library-files.service.js";
|
|
11
12
|
import { type PersonRef } from "../identity/account-avatar.js";
|
|
13
|
+
import { InboxItemsService } from "../inbox/inbox-items.service.js";
|
|
12
14
|
import { KernelDb } from "../infra/database/kernel-db.js";
|
|
13
15
|
import { TransactionHost } from "../infra/database/transaction-host.js";
|
|
14
16
|
import { KernelEvents } from "../infra/events/kernel-events.js";
|
|
@@ -34,7 +36,15 @@ import { type Selection } from "./selection.js";
|
|
|
34
36
|
export interface ConversationView {
|
|
35
37
|
id: string;
|
|
36
38
|
orgId: string;
|
|
37
|
-
|
|
39
|
+
/** What kind of conversation it is: `external` is held with a conversation target (no agent). */
|
|
40
|
+
kind: ConversationKind;
|
|
41
|
+
/** Its agent; null exactly for an external conversation. */
|
|
42
|
+
agentId: string | null;
|
|
43
|
+
/**
|
|
44
|
+
* The conversation target an external conversation is held with (local agents spec Part 2) and
|
|
45
|
+
* the state the target last reported; null for every other kind.
|
|
46
|
+
*/
|
|
47
|
+
external: ExternalConversationView | null;
|
|
38
48
|
accountId: string;
|
|
39
49
|
title: string | null;
|
|
40
50
|
/** Who wrote `title`: the title job, a rename, or nobody yet (UI refresh B). */
|
|
@@ -86,6 +96,13 @@ export interface ConversationView {
|
|
|
86
96
|
*/
|
|
87
97
|
activity: "running" | "waiting" | null;
|
|
88
98
|
}
|
|
99
|
+
/** An external conversation's target and state ({@link ConversationView.external}). */
|
|
100
|
+
export interface ExternalConversationView {
|
|
101
|
+
extension: string;
|
|
102
|
+
targetKey: string;
|
|
103
|
+
label: string;
|
|
104
|
+
state: ExternalConversationState;
|
|
105
|
+
}
|
|
89
106
|
/** The state a conversation's latest `project_root` event records ({@link ConversationView}). */
|
|
90
107
|
export type ProjectState = {
|
|
91
108
|
loaded: true;
|
|
@@ -93,8 +110,15 @@ export type ProjectState = {
|
|
|
93
110
|
} | {
|
|
94
111
|
unavailable: "gone" | "outside_mode" | "unreadable" | "unsupported" | "removed";
|
|
95
112
|
};
|
|
113
|
+
/** The columns of a single-conversation response: {@link COLUMNS} and its project state. */
|
|
114
|
+
export declare const viewColumns: () => readonly ["id", "org_id", "agent_id", "account_id", "title", "title_source", "selection", "last_seq", "created_at", "updated_at", "parent_conversation_id", "subagent_type", "archived_at", "device_id", "automation_id", "subject", "project_name", "project_path", "kind", "external", "external_state", import("kysely").AliasedRawBuilder<ProjectState | null, "project_state">];
|
|
96
115
|
/** Escapes `\`, `%` and `_` so user text is matched literally by `LIKE … ESCAPE '\'`. */
|
|
97
116
|
export declare function escapeLike(s: string): string;
|
|
117
|
+
/** A conversation target as the lists name it (`external.extension`, `external.targetKey`). */
|
|
118
|
+
export interface ConversationTargetRef {
|
|
119
|
+
extension: string;
|
|
120
|
+
targetKey: string;
|
|
121
|
+
}
|
|
98
122
|
/**
|
|
99
123
|
* An agent as the conversation lists carry it: its name and the avatar half of its profile, in the
|
|
100
124
|
* agents API's shape (the web draws it without reading the agents list).
|
|
@@ -112,9 +136,24 @@ export interface ConversationListRow extends ConversationView {
|
|
|
112
136
|
/** Null only when the agent stopped being visible between the page read and the agent read. */
|
|
113
137
|
agent: ConversationAgent | null;
|
|
114
138
|
}
|
|
115
|
-
/**
|
|
139
|
+
/**
|
|
140
|
+
* One group in the sidebar list: the caller's newest conversations with one agent, or with one
|
|
141
|
+
* conversation target (external conversations, local agents spec Part 2). Exactly one of `agent`
|
|
142
|
+
* and `target` is set.
|
|
143
|
+
*/
|
|
116
144
|
export interface ConversationGroup {
|
|
117
|
-
|
|
145
|
+
/**
|
|
146
|
+
* The group's identity and the groups cursor (`after`): the agent's id, or
|
|
147
|
+
* `target/<extension>/<targetKey>` for a target's group.
|
|
148
|
+
*/
|
|
149
|
+
key: string;
|
|
150
|
+
agent: ConversationAgent | null;
|
|
151
|
+
/** The target of an external conversations' group; its label is its newest conversation's. */
|
|
152
|
+
target: {
|
|
153
|
+
extension: string;
|
|
154
|
+
targetKey: string;
|
|
155
|
+
label: string;
|
|
156
|
+
} | null;
|
|
118
157
|
/** max(updated_at) of the group's conversations. */
|
|
119
158
|
latestAt: Date;
|
|
120
159
|
/** At most `perGroup`, updated_at DESC, id DESC. */
|
|
@@ -314,6 +353,32 @@ export declare const projectFileText: (path: string, root: string) => string;
|
|
|
314
353
|
*/
|
|
315
354
|
export declare function withFileLines(blocks: readonly LogContentBlock[], labels?: ReadonlyMap<string, string>): LogContentBlock[];
|
|
316
355
|
export { userMessageIdemKey } from "./idem-keys.js";
|
|
356
|
+
/** A conversation row (its {@link COLUMNS}) as the {@link ConversationView} the routes answer. */
|
|
357
|
+
export declare function toView(r: {
|
|
358
|
+
id: string;
|
|
359
|
+
org_id: string;
|
|
360
|
+
kind: ConversationKind;
|
|
361
|
+
agent_id: string | null;
|
|
362
|
+
external: ExternalRef | null;
|
|
363
|
+
external_state: ExternalConversationState;
|
|
364
|
+
account_id: string;
|
|
365
|
+
title: string | null;
|
|
366
|
+
title_source: TitleSource | null;
|
|
367
|
+
selection: unknown;
|
|
368
|
+
last_seq: string;
|
|
369
|
+
created_at: Date;
|
|
370
|
+
updated_at: Date;
|
|
371
|
+
parent_conversation_id: string | null;
|
|
372
|
+
subagent_type: string | null;
|
|
373
|
+
archived_at: Date | null;
|
|
374
|
+
device_id: string | null;
|
|
375
|
+
automation_id: string | null;
|
|
376
|
+
subject: ConversationSubject | null;
|
|
377
|
+
project_name: string | null;
|
|
378
|
+
project_path: string | null;
|
|
379
|
+
activity?: "running" | "waiting" | null;
|
|
380
|
+
project_state?: ProjectState | null;
|
|
381
|
+
}): ConversationView;
|
|
317
382
|
/** The longest conversation title (characters, after trimming). */
|
|
318
383
|
export declare const CONVERSATION_TITLE_MAX_CHARS = 120;
|
|
319
384
|
/**
|
|
@@ -426,6 +491,10 @@ export declare class ConversationsService {
|
|
|
426
491
|
private readonly outbox;
|
|
427
492
|
/** The agent's tools for a message naming connectors or resources (inline tokens §3.4). */
|
|
428
493
|
private readonly mentions;
|
|
494
|
+
/** Which extensions serve `conversation-target@1` in the org (external conversations). */
|
|
495
|
+
private readonly points;
|
|
496
|
+
/** A deleted external conversation's open request items are resolved with it. */
|
|
497
|
+
private readonly inbox;
|
|
429
498
|
constructor(permissions: Pick<PermissionProfilesService, "initialize" | "lockInitial" | "defaultProfile">, db: KernelDb, eventLog: EventLog, agents: Pick<AgentLookup, "get">, access: Pick<ConversationAccess, "isAccessible">,
|
|
430
499
|
/** Starts and stops the conversation's runs. */
|
|
431
500
|
runs: RunControl,
|
|
@@ -452,7 +521,11 @@ export declare class ConversationsService {
|
|
|
452
521
|
/** Owes the snapshot deletions of the shares a deletion revokes (file sharing spec §9). */
|
|
453
522
|
outbox: Pick<OutboxService, "add">,
|
|
454
523
|
/** The agent's tools for a message naming connectors or resources (inline tokens §3.4). */
|
|
455
|
-
mentions: Pick<MentionableConnectorsService, "resolveForMessage"
|
|
524
|
+
mentions: Pick<MentionableConnectorsService, "resolveForMessage">,
|
|
525
|
+
/** Which extensions serve `conversation-target@1` in the org (external conversations). */
|
|
526
|
+
points: Pick<ExtensionPointDefaults, "serves" | "providersFor">,
|
|
527
|
+
/** A deleted external conversation's open request items are resolved with it. */
|
|
528
|
+
inbox: Pick<InboxItemsService, "resolveKernelItems">);
|
|
456
529
|
/**
|
|
457
530
|
* Creates an empty conversation bound to its device (`pick`, see
|
|
458
531
|
* {@link ConversationDevices.forNew}); an agent the account cannot use →
|
|
@@ -508,7 +581,7 @@ export declare class ConversationsService {
|
|
|
508
581
|
private startRun;
|
|
509
582
|
private rowView;
|
|
510
583
|
/**
|
|
511
|
-
* Renames a conversation (owner only, as {@link
|
|
584
|
+
* Renames a conversation (owner only, as {@link owned}): `title_source = 'manual'`, so the title
|
|
512
585
|
* job never overwrites it. Publishes `changed: title`. `title` is already trimmed and checked.
|
|
513
586
|
*/
|
|
514
587
|
rename(orgId: string, conversationId: string, accountId: string, title: string): Promise<ConversationView>;
|
|
@@ -540,31 +613,59 @@ export declare class ConversationsService {
|
|
|
540
613
|
/** The root lock excludes dispatch and claimed writes. Lock children in tree order for
|
|
541
614
|
* unfenced writes, then use a fresh READ COMMITTED snapshot for the eligibility decision. */
|
|
542
615
|
private treeExpired;
|
|
543
|
-
/**
|
|
616
|
+
/**
|
|
617
|
+
* Reads a conversation: another person's, or one whose agent the owner can no longer use →
|
|
618
|
+
* `conversation.not_found`. An external conversation (local agents spec Part 2) is read by its
|
|
619
|
+
* owner while its extension serves `conversation-target@1` in the org, else
|
|
620
|
+
* `conversation.not_found` too.
|
|
621
|
+
*/
|
|
544
622
|
get(orgId: string, conversationId: string, viewerAccountId: string): Promise<ConversationView>;
|
|
623
|
+
/**
|
|
624
|
+
* {@link get} for a change of the conversation itself by its owner (rename, archive, delete): a
|
|
625
|
+
* sub-agent's conversation is read-only (sub-agents spec §6.1) → `conversation.read_only`; an
|
|
626
|
+
* external conversation passes.
|
|
627
|
+
*/
|
|
628
|
+
owned(orgId: string, conversationId: string, accountId: string): Promise<ConversationView>;
|
|
545
629
|
getWithOwner(orgId: string, conversationId: string, viewerAccountId: string): Promise<ConversationView & {
|
|
546
630
|
owner: PersonRef | null;
|
|
547
631
|
}>;
|
|
548
632
|
/**
|
|
549
|
-
* {@link
|
|
550
|
-
*
|
|
633
|
+
* {@link owned} for a change that works through Capybara's runs (a message, the queue, a model
|
|
634
|
+
* switch, compaction, an extension UI action): an external conversation is run by its local
|
|
635
|
+
* agent, not by Capybara (its messages and stop go through `ExternalConversationsService`) →
|
|
636
|
+
* `conversation.external_read_only`.
|
|
551
637
|
*/
|
|
552
638
|
writable(orgId: string, conversationId: string, accountId: string): Promise<ConversationView>;
|
|
553
639
|
/** {@link get}'s row, with the conversation's kind. */
|
|
554
640
|
private readable;
|
|
641
|
+
/**
|
|
642
|
+
* {@link readable} for an external conversation (local agents spec Part 2): the viewer's, while
|
|
643
|
+
* its extension serves `conversation-target@1` in the org (the org guard checked the
|
|
644
|
+
* membership); anything else → `conversation.not_found`.
|
|
645
|
+
*/
|
|
646
|
+
private readableExternal;
|
|
555
647
|
/** The stream's visibility check ({@link ConversationAccess.isAccessible}). */
|
|
556
648
|
isAccessible(orgId: string, conversationId: string, viewerAccountId: string): Promise<boolean>;
|
|
557
649
|
/**
|
|
558
|
-
*
|
|
559
|
-
*
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
650
|
+
* The extensions that serve `conversation-target@1` in the org now: the external conversations
|
|
651
|
+
* the lists show (local agents spec Part 2).
|
|
652
|
+
*/
|
|
653
|
+
private servingTargets;
|
|
654
|
+
/**
|
|
655
|
+
* My conversations, most recently updated first: the chat conversations whose agent is in this
|
|
656
|
+
* org, not deleted and visible to me, and the external ones whose extension serves
|
|
657
|
+
* `conversation-target@1` in the org now (local agents spec Part 2). `after` is the previous
|
|
658
|
+
* page's last conversation id, continued after its current (updated_at, id); a cursor that no
|
|
659
|
+
* longer exists gives an empty page. Each row carries its agent (one batch read for the page;
|
|
660
|
+
* null for an external conversation). `agentId` keeps one agent's, `target` one target's. With
|
|
661
|
+
* `total`, also counts every conversation matching the same filters (the cursor aside) in the
|
|
662
|
+
* same statement.
|
|
563
663
|
*/
|
|
564
664
|
listMine(orgId: string, accountId: string, opts: {
|
|
565
665
|
after?: string;
|
|
566
666
|
limit: number;
|
|
567
667
|
agentId?: string;
|
|
668
|
+
target?: ConversationTargetRef;
|
|
568
669
|
archived?: boolean;
|
|
569
670
|
q?: string;
|
|
570
671
|
state?: "running" | "waiting";
|
|
@@ -579,11 +680,13 @@ export declare class ConversationsService {
|
|
|
579
680
|
*/
|
|
580
681
|
private listAgents;
|
|
581
682
|
/**
|
|
582
|
-
* My
|
|
583
|
-
*
|
|
584
|
-
*
|
|
585
|
-
*
|
|
586
|
-
*
|
|
683
|
+
* My conversations grouped by agent (the sidebar, UI refresh spec §5.9), and my external
|
|
684
|
+
* conversations by target (local agents spec Part 2), in one statement. Only agents that are not
|
|
685
|
+
* deleted and still visible to me and targets whose extension serves `conversation-target@1` in
|
|
686
|
+
* the org now (as {@link listMine}). Groups are ordered by (latestAt DESC, key DESC); each holds
|
|
687
|
+
* up to `perGroup` conversations and says whether it has more. `after` is the last group's key,
|
|
688
|
+
* re-evaluated against that group's current latestAt; a cursor group that is no longer listed
|
|
689
|
+
* gives an empty page.
|
|
587
690
|
*/
|
|
588
691
|
listGroups(orgId: string, accountId: string, opts: {
|
|
589
692
|
perGroup: number;
|
|
@@ -774,7 +877,8 @@ export declare class ConversationsService {
|
|
|
774
877
|
/**
|
|
775
878
|
* {@link resolveExtra}'s referenced conversations (@ conversations spec §1), read as the sender
|
|
776
879
|
* outside any transaction, once each in the body's order. Each must be the sender's own chat in
|
|
777
|
-
* this org, not deleted, its agent still visible ({@link readable} plus `kind = 'chat'`)
|
|
880
|
+
* this org, not deleted, its agent still visible ({@link readable} plus `kind = 'chat'`), or
|
|
881
|
+
* their external conversation whose extension serves the org (local agents spec Part 2); any
|
|
778
882
|
* other → `conversation.reference_unavailable` (`details: [{ id }]`, the same for every case);
|
|
779
883
|
* one with no `user_message` yet → the same code with `reason: "empty"`; the conversation
|
|
780
884
|
* being written in → `conversation.reference_self`.
|