@songsid/agend 2.4.0-alpha.3 → 2.4.0-alpha.4
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/dist/agent-cli-instructions.md +8 -4
- package/dist/agent-whats-new.js +7 -7
- package/dist/agent-whats-new.js.map +1 -1
- package/dist/auth-api.js +7 -1
- package/dist/auth-api.js.map +1 -1
- package/dist/backend/claude-code.d.ts +8 -0
- package/dist/backend/claude-code.js +11 -4
- package/dist/backend/claude-code.js.map +1 -1
- package/dist/backend/codex.js +7 -3
- package/dist/backend/codex.js.map +1 -1
- package/dist/backend/grok.d.ts +19 -0
- package/dist/backend/grok.js +112 -1
- package/dist/backend/grok.js.map +1 -1
- package/dist/backend/types.d.ts +10 -0
- package/dist/backend/types.js.map +1 -1
- package/dist/build-provenance.json +1 -1
- package/dist/channel/mcp-tools.js +2 -2
- package/dist/channel/mcp-tools.js.map +1 -1
- package/dist/child-process.d.ts +1 -0
- package/dist/child-process.js.map +1 -1
- package/dist/claude-model-observation.d.ts +71 -0
- package/dist/claude-model-observation.js +169 -0
- package/dist/claude-model-observation.js.map +1 -0
- package/dist/cross-instance-envelope.d.ts +4 -1
- package/dist/cross-instance-envelope.js +12 -7
- package/dist/cross-instance-envelope.js.map +1 -1
- package/dist/daemon.d.ts +24 -2
- package/dist/daemon.js +263 -35
- package/dist/daemon.js.map +1 -1
- package/dist/delivery-consumed-watch.d.ts +24 -0
- package/dist/delivery-consumed-watch.js +21 -10
- package/dist/delivery-consumed-watch.js.map +1 -1
- package/dist/delivery-outbox.d.ts +5 -1
- package/dist/delivery-outbox.js +5 -0
- package/dist/delivery-outbox.js.map +1 -1
- package/dist/fleet-manager.d.ts +11 -1
- package/dist/fleet-manager.js +80 -2
- package/dist/fleet-manager.js.map +1 -1
- package/dist/general-knowledge/skills/concepts/SKILL.md +16 -3
- package/dist/general-knowledge/skills/cross-instance-messaging/SKILL.md +5 -3
- package/dist/general-knowledge/skills/hub/SKILL.md +161 -4
- package/dist/general-knowledge/skills/multi-channel/SKILL.md +1 -1
- package/dist/general-knowledge/skills/web-dashboard/SKILL.md +18 -7
- package/dist/general-knowledge/skills/worker-collaboration/SKILL.md +1 -1
- package/dist/grok-delivery-proof.d.ts +24 -0
- package/dist/grok-delivery-proof.js +124 -0
- package/dist/grok-delivery-proof.js.map +1 -0
- package/dist/hub/assistant/admin-types.d.ts +43 -0
- package/dist/hub/assistant/admin-types.js +2 -0
- package/dist/hub/assistant/admin-types.js.map +1 -0
- package/dist/hub/assistant/claude-host.d.ts +17 -0
- package/dist/hub/assistant/claude-host.js +300 -0
- package/dist/hub/assistant/claude-host.js.map +1 -0
- package/dist/hub/assistant/context.d.ts +3 -0
- package/dist/hub/assistant/context.js +7 -0
- package/dist/hub/assistant/context.js.map +1 -0
- package/dist/hub/assistant/fleet-host.d.ts +19 -0
- package/dist/hub/assistant/fleet-host.js +28 -0
- package/dist/hub/assistant/fleet-host.js.map +1 -0
- package/dist/hub/assistant/runner.d.ts +64 -0
- package/dist/hub/assistant/runner.js +239 -0
- package/dist/hub/assistant/runner.js.map +1 -0
- package/dist/hub/assistant/service.d.ts +51 -0
- package/dist/hub/assistant/service.js +143 -0
- package/dist/hub/assistant/service.js.map +1 -0
- package/dist/hub/assistant/store.d.ts +3 -0
- package/dist/hub/assistant/store.js +65 -0
- package/dist/hub/assistant/store.js.map +1 -0
- package/dist/hub/assistant/types.d.ts +60 -0
- package/dist/hub/assistant/types.js +2 -0
- package/dist/hub/assistant/types.js.map +1 -0
- package/dist/hub/catalog.js +22 -3
- package/dist/hub/catalog.js.map +1 -1
- package/dist/hub/client.d.ts +10 -0
- package/dist/hub/client.js +48 -8
- package/dist/hub/client.js.map +1 -1
- package/dist/hub/compose/member.d.ts +8 -1
- package/dist/hub/compose/member.js +224 -58
- package/dist/hub/compose/member.js.map +1 -1
- package/dist/hub/compose/server.d.ts +11 -1
- package/dist/hub/compose/server.js +68 -11
- package/dist/hub/compose/server.js.map +1 -1
- package/dist/hub/directory/admission.d.ts +15 -0
- package/dist/hub/directory/admission.js +56 -0
- package/dist/hub/directory/admission.js.map +1 -0
- package/dist/hub/directory/directory.d.ts +1 -1
- package/dist/hub/directory/directory.js +63 -0
- package/dist/hub/directory/directory.js.map +1 -1
- package/dist/hub/directory/types.d.ts +13 -0
- package/dist/hub/directory/types.js.map +1 -1
- package/dist/hub/group-policy/admin-control.d.ts +1 -0
- package/dist/hub/group-policy/admin-control.js +4 -2
- package/dist/hub/group-policy/admin-control.js.map +1 -1
- package/dist/hub/group-policy/admin-facts.d.ts +1 -0
- package/dist/hub/group-policy/admin-facts.js +2 -1
- package/dist/hub/group-policy/admin-facts.js.map +1 -1
- package/dist/hub/group-policy/admin-read.d.ts +1 -0
- package/dist/hub/group-policy/admin-read.js +3 -2
- package/dist/hub/group-policy/admin-read.js.map +1 -1
- package/dist/hub/group-policy/errors.d.ts +9 -0
- package/dist/hub/group-policy/errors.js +13 -0
- package/dist/hub/group-policy/errors.js.map +1 -0
- package/dist/hub/group-policy/store.d.ts +3 -9
- package/dist/hub/group-policy/store.js +5 -29
- package/dist/hub/group-policy/store.js.map +1 -1
- package/dist/hub/http/compose.d.ts +1 -0
- package/dist/hub/http/compose.js +429 -14
- package/dist/hub/http/compose.js.map +1 -1
- package/dist/hub/http/core.d.ts +6 -5
- package/dist/hub/http/core.js +74 -48
- package/dist/hub/http/core.js.map +1 -1
- package/dist/hub/http/deps.d.ts +18 -0
- package/dist/hub/http/schema.js +8 -0
- package/dist/hub/http/schema.js.map +1 -1
- package/dist/hub/init/member-certificate.d.ts +1 -0
- package/dist/hub/init/member-certificate.js +39 -0
- package/dist/hub/init/member-certificate.js.map +1 -0
- package/dist/hub/init/plan-digest.js +1 -0
- package/dist/hub/init/plan-digest.js.map +1 -1
- package/dist/hub/invitations.d.ts +96 -0
- package/dist/hub/invitations.js +292 -0
- package/dist/hub/invitations.js.map +1 -0
- package/dist/hub/join/certificate-trust.d.ts +13 -0
- package/dist/hub/join/certificate-trust.js +41 -0
- package/dist/hub/join/certificate-trust.js.map +1 -0
- package/dist/hub/join/certificate.d.ts +11 -0
- package/dist/hub/join/certificate.js +67 -0
- package/dist/hub/join/certificate.js.map +1 -0
- package/dist/hub/join/client.d.ts +5 -0
- package/dist/hub/join/client.js +30 -3
- package/dist/hub/join/client.js.map +1 -1
- package/dist/hub/join/http.js +5 -3
- package/dist/hub/join/http.js.map +1 -1
- package/dist/hub/join/local.d.ts +32 -1
- package/dist/hub/join/local.js +116 -11
- package/dist/hub/join/local.js.map +1 -1
- package/dist/hub/join/service.d.ts +17 -0
- package/dist/hub/join/service.js +12 -0
- package/dist/hub/join/service.js.map +1 -1
- package/dist/hub/join/trust.d.ts +9 -0
- package/dist/hub/join/trust.js +35 -0
- package/dist/hub/join/trust.js.map +1 -1
- package/dist/hub/member/pairing-api.js +9 -1
- package/dist/hub/member/pairing-api.js.map +1 -1
- package/dist/hub/member/room-ask.d.ts +137 -0
- package/dist/hub/member/room-ask.js +358 -0
- package/dist/hub/member/room-ask.js.map +1 -0
- package/dist/hub/member-journal.d.ts +18 -1
- package/dist/hub/member-journal.js +76 -3
- package/dist/hub/member-journal.js.map +1 -1
- package/dist/hub/member-origin.d.ts +177 -0
- package/dist/hub/member-origin.js +314 -0
- package/dist/hub/member-origin.js.map +1 -0
- package/dist/hub/pairing-notices.d.ts +1 -0
- package/dist/hub/pairing-notices.js +2 -1
- package/dist/hub/pairing-notices.js.map +1 -1
- package/dist/hub/pairing.d.ts +8 -0
- package/dist/hub/pairing.js +32 -5
- package/dist/hub/pairing.js.map +1 -1
- package/dist/hub/protocol.d.ts +159 -0
- package/dist/hub/protocol.js +19 -1
- package/dist/hub/protocol.js.map +1 -1
- package/dist/hub/roles/clock.d.ts +18 -0
- package/dist/hub/roles/clock.js +58 -0
- package/dist/hub/roles/clock.js.map +1 -0
- package/dist/hub/roles/cutover-marker.d.ts +57 -0
- package/dist/hub/roles/cutover-marker.js +26 -0
- package/dist/hub/roles/cutover-marker.js.map +1 -0
- package/dist/hub/roles/cutover.js +3 -17
- package/dist/hub/roles/cutover.js.map +1 -1
- package/dist/hub/roles/read.d.ts +7 -3
- package/dist/hub/roles/read.js +9 -1
- package/dist/hub/roles/read.js.map +1 -1
- package/dist/hub/rooms/admin.d.ts +45 -0
- package/dist/hub/rooms/admin.js +105 -0
- package/dist/hub/rooms/admin.js.map +1 -0
- package/dist/hub/rooms/agents.d.ts +74 -0
- package/dist/hub/rooms/agents.js +333 -0
- package/dist/hub/rooms/agents.js.map +1 -0
- package/dist/hub/rooms/assistant.d.ts +44 -0
- package/dist/hub/rooms/assistant.js +179 -0
- package/dist/hub/rooms/assistant.js.map +1 -0
- package/dist/hub/rooms/context.d.ts +10 -0
- package/dist/hub/rooms/context.js +24 -0
- package/dist/hub/rooms/context.js.map +1 -0
- package/dist/hub/rooms/direct-view.d.ts +39 -0
- package/dist/hub/rooms/direct-view.js +38 -0
- package/dist/hub/rooms/direct-view.js.map +1 -0
- package/dist/hub/rooms/directory.d.ts +35 -0
- package/dist/hub/rooms/directory.js +98 -0
- package/dist/hub/rooms/directory.js.map +1 -0
- package/dist/hub/rooms/schema.d.ts +11 -0
- package/dist/hub/rooms/schema.js +73 -0
- package/dist/hub/rooms/schema.js.map +1 -0
- package/dist/hub/rooms/service.d.ts +449 -0
- package/dist/hub/rooms/service.js +581 -0
- package/dist/hub/rooms/service.js.map +1 -0
- package/dist/hub/rooms/types.d.ts +79 -0
- package/dist/hub/rooms/types.js +2 -0
- package/dist/hub/rooms/types.js.map +1 -0
- package/dist/hub/rooms.d.ts +7 -1
- package/dist/hub/rooms.js +30 -5
- package/dist/hub/rooms.js.map +1 -1
- package/dist/hub/runtime.d.ts +8 -0
- package/dist/hub/runtime.js +57 -3
- package/dist/hub/runtime.js.map +1 -1
- package/dist/hub/server.d.ts +25 -0
- package/dist/hub/server.js +63 -5
- package/dist/hub/server.js.map +1 -1
- package/dist/hub/sessions.js +1 -1
- package/dist/hub/sessions.js.map +1 -1
- package/dist/hub/settings-commit.d.ts +6 -1
- package/dist/hub/settings-commit.js +15 -0
- package/dist/hub/settings-commit.js.map +1 -1
- package/dist/hub/settings-directory-recovery.d.ts +5 -0
- package/dist/hub/settings-directory-recovery.js +11 -1
- package/dist/hub/settings-directory-recovery.js.map +1 -1
- package/dist/hub/settings-member-address-control.d.ts +84 -0
- package/dist/hub/settings-member-address-control.js +150 -0
- package/dist/hub/settings-member-address-control.js.map +1 -0
- package/dist/hub/settings-member-address-decision.d.ts +229 -0
- package/dist/hub/settings-member-address-decision.js +318 -0
- package/dist/hub/settings-member-address-decision.js.map +1 -0
- package/dist/hub/settings-member-address-http.d.ts +9 -0
- package/dist/hub/settings-member-address-http.js +78 -0
- package/dist/hub/settings-member-address-http.js.map +1 -0
- package/dist/hub/settings-member-address-network.d.ts +2 -0
- package/dist/hub/settings-member-address-network.js +22 -0
- package/dist/hub/settings-member-address-network.js.map +1 -0
- package/dist/hub/settings-member-address-notices.d.ts +22 -0
- package/dist/hub/settings-member-address-notices.js +21 -0
- package/dist/hub/settings-member-address-notices.js.map +1 -0
- package/dist/hub/settings-member-address-policy.d.ts +21 -0
- package/dist/hub/settings-member-address-policy.js +34 -0
- package/dist/hub/settings-member-address-policy.js.map +1 -0
- package/dist/hub/settings-member-address.d.ts +64 -0
- package/dist/hub/settings-member-address.js +239 -0
- package/dist/hub/settings-member-address.js.map +1 -0
- package/dist/hub/socket.d.ts +2 -0
- package/dist/hub/socket.js +2 -0
- package/dist/hub/socket.js.map +1 -1
- package/dist/hub/teams/types.d.ts +41 -0
- package/dist/hub/teams/types.js +2 -0
- package/dist/hub/teams/types.js.map +1 -0
- package/dist/hub/tls.d.ts +47 -0
- package/dist/hub/tls.js +23 -6
- package/dist/hub/tls.js.map +1 -1
- package/dist/hub/types.d.ts +51 -3
- package/dist/hub/validation.d.ts +1 -1
- package/dist/hub/validation.js +2 -2
- package/dist/hub/validation.js.map +1 -1
- package/dist/hub/web-origin.d.ts +20 -7
- package/dist/hub/web-origin.js +26 -15
- package/dist/hub/web-origin.js.map +1 -1
- package/dist/hub-pilot-trust-settings.d.ts +4 -0
- package/dist/hub-pilot-trust-settings.js +13 -0
- package/dist/hub-pilot-trust-settings.js.map +1 -0
- package/dist/human-delivery-receipts.d.ts +4 -2
- package/dist/human-delivery-receipts.js +20 -13
- package/dist/human-delivery-receipts.js.map +1 -1
- package/dist/inbound-presentation.d.ts +28 -0
- package/dist/inbound-presentation.js +48 -0
- package/dist/inbound-presentation.js.map +1 -0
- package/dist/instance-lifecycle.js +1 -0
- package/dist/instance-lifecycle.js.map +1 -1
- package/dist/instructions.d.ts +4 -2
- package/dist/instructions.js +7 -4
- package/dist/instructions.js.map +1 -1
- package/dist/locale.js +14 -0
- package/dist/locale.js.map +1 -1
- package/dist/log-service.d.ts +3 -0
- package/dist/log-service.js +39 -22
- package/dist/log-service.js.map +1 -1
- package/dist/log-steps.d.ts +56 -1
- package/dist/log-steps.js +318 -56
- package/dist/log-steps.js.map +1 -1
- package/dist/needs-you-hub.js +6 -2
- package/dist/needs-you-hub.js.map +1 -1
- package/dist/needs-you.d.ts +5 -1
- package/dist/needs-you.js +4 -8
- package/dist/needs-you.js.map +1 -1
- package/dist/office-share-page.js +1 -1
- package/dist/office-share-page.js.map +1 -1
- package/dist/outbound-handlers.d.ts +1 -0
- package/dist/outbound-handlers.js +6 -0
- package/dist/outbound-handlers.js.map +1 -1
- package/dist/outbound-schemas.d.ts +1 -0
- package/dist/outbound-schemas.js +1 -0
- package/dist/outbound-schemas.js.map +1 -1
- package/dist/queued-handoffs.d.ts +3 -1
- package/dist/queued-handoffs.js +1 -1
- package/dist/queued-handoffs.js.map +1 -1
- package/dist/runtime-transcript-source.d.ts +2 -0
- package/dist/runtime-transcript-source.js +23 -1
- package/dist/runtime-transcript-source.js.map +1 -1
- package/dist/settings-api.d.ts +1 -0
- package/dist/settings-api.js +15 -8
- package/dist/settings-api.js.map +1 -1
- package/dist/settings-baseline.d.ts +3 -0
- package/dist/settings-baseline.js +2 -2
- package/dist/settings-baseline.js.map +1 -1
- package/dist/settings-effect.d.ts +3 -0
- package/dist/settings-effect.js +12 -2
- package/dist/settings-effect.js.map +1 -1
- package/dist/topic-commands.js +2 -1
- package/dist/topic-commands.js.map +1 -1
- package/dist/transcript-jsonl.d.ts +24 -0
- package/dist/transcript-jsonl.js +72 -22
- package/dist/transcript-jsonl.js.map +1 -1
- package/dist/transcript-monitor.d.ts +5 -1
- package/dist/transcript-monitor.js +65 -3
- package/dist/transcript-monitor.js.map +1 -1
- package/dist/transcript-needles.d.ts +1 -1
- package/dist/transcript-needles.js +1 -1
- package/dist/transcript-records.d.ts +2 -1
- package/dist/transcript-records.js.map +1 -1
- package/dist/transcript-sources.d.ts +4 -0
- package/dist/transcript-sources.js +2 -0
- package/dist/transcript-sources.js.map +1 -1
- package/dist/ui/agent-details-settings.js +10 -1
- package/dist/ui/chat-activity.js +2 -2
- package/dist/ui/fleet-canvas.js +5 -2
- package/dist/ui/fleet-office-card.js +4 -2
- package/dist/ui/fleet-org.js +2 -0
- package/dist/ui/hub-admin-rooms-strings.js +28 -0
- package/dist/ui/hub-admin-rooms.js +77 -0
- package/dist/ui/hub-assistant-admin-strings.js +26 -0
- package/dist/ui/hub-assistant-admin.js +85 -0
- package/dist/ui/hub-client.js +170 -14
- package/dist/ui/hub-invitations-strings.js +30 -0
- package/dist/ui/hub-invitations.js +108 -0
- package/dist/ui/hub-join-one-strings.js +2 -0
- package/dist/ui/hub-pairing-form-strings.js +48 -0
- package/dist/ui/hub-pilot-trust-strings.js +27 -0
- package/dist/ui/hub-pilot-trust.js +41 -0
- package/dist/ui/hub-room-composer.js +113 -0
- package/dist/ui/hub-room-favorites.js +90 -0
- package/dist/ui/hub-room-list.js +152 -0
- package/dist/ui/hub-room-members-strings.js +12 -0
- package/dist/ui/hub-room-members.js +104 -0
- package/dist/ui/hub-room-new-strings.js +46 -0
- package/dist/ui/hub-room-new.js +135 -0
- package/dist/ui/hub-room-presence.js +123 -0
- package/dist/ui/hub-room-settings-strings.js +26 -0
- package/dist/ui/hub-room-settings.js +134 -0
- package/dist/ui/hub-room-strings.js +222 -0
- package/dist/ui/hub-room-thread.js +262 -0
- package/dist/ui/hub-room-unread.js +51 -0
- package/dist/ui/hub-room-working.js +29 -0
- package/dist/ui/log-row.js +14 -5
- package/dist/ui/panel-details.js +6 -1
- package/dist/ui/panel-hub.js +91 -49
- package/dist/ui/panel-needs.js +2 -0
- package/dist/ui/settings-hub-company-join.js +31 -11
- package/dist/ui/settings-hub-connection.js +9 -3
- package/dist/ui/settings-hub-member-address.js +205 -0
- package/dist/ui/settings-hub-section.js +2 -0
- package/dist/ui/settings-hub.js +10 -4
- package/dist/ui/shared/app-i18n.js +2 -2
- package/dist/ui/shared/app-needs.js +3 -0
- package/dist/ui/shared/app-shell.js +4 -1
- package/dist/ui/shared/app.css +5 -0
- package/dist/ui/shared/chat-render.js +10 -0
- package/dist/ui/shared/chat-thread.js +14 -5
- package/dist/ui/shared/hub-chat.css +17 -0
- package/dist/ui/shared/hub-rooms-u2.css +42 -0
- package/dist/ui/shared/model-observation.js +16 -0
- package/dist/ui/shared/stale-tab.css +6 -0
- package/dist/ui/shared/stale-tab.js +50 -0
- package/dist/web-api.d.ts +2 -0
- package/dist/web-api.js +6 -2
- package/dist/web-api.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,581 @@
|
|
|
1
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { HubCatalogService, exposureOf, readExposure } from "../catalog.js";
|
|
4
|
+
import { HubRoomAssistant } from "./assistant.js";
|
|
5
|
+
import { canCreateRoom } from "./admin.js";
|
|
6
|
+
import { HubRoomAgents } from "./agents.js";
|
|
7
|
+
import { personKeyString } from "../identity.js";
|
|
8
|
+
import { personFromString } from "../http/core.js";
|
|
9
|
+
import { uuid, plain, validated, HubProtocolError } from "../validation.js";
|
|
10
|
+
import { RoomDirectoryCandidates, rememberRoomPeople } from "./directory.js";
|
|
11
|
+
const label = plain(120).transform(s => s.trim()).refine(s => s.length > 0 && !/[\u0000-\u001f\u007f]/.test(s));
|
|
12
|
+
const personId = z.string().max(128).refine(s => /^[a-z][a-z0-9_-]{0,63}\/[a-f0-9-]{36}$/.test(s));
|
|
13
|
+
const createSchema = z.strictObject({ name: label, people: z.array(personId).max(100).default([]), teams: z.array(uuid).max(100).default([]),
|
|
14
|
+
groups: z.array(uuid).max(100).default([]), agents: z.array(uuid).max(64).default([]), assistant: z.boolean().default(false) });
|
|
15
|
+
const postSchema = z.strictObject({ text: plain(64 * 1024).refine(s => s.trim().length > 0), mentions: z.array(z.string().max(128)).max(100).default([]),
|
|
16
|
+
replyTo: uuid.optional(), files: z.array(uuid).max(0).optional(), idempotencyKey: uuid });
|
|
17
|
+
const patchSchema = z.strictObject({ name: label.optional(), allMention: z.boolean().optional(), historyForNew: z.enum(["since-join", "all"]).optional(), archived: z.boolean().optional(), assistant: z.boolean().optional() });
|
|
18
|
+
export class HubRoomRefusal extends Error {
|
|
19
|
+
status;
|
|
20
|
+
code;
|
|
21
|
+
details;
|
|
22
|
+
constructor(status, code, details = {}) {
|
|
23
|
+
super(`Room: ${code}`);
|
|
24
|
+
this.status = status;
|
|
25
|
+
this.code = code;
|
|
26
|
+
this.details = details;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
/** People chat first; direct agent conversations remain in HubRoomsService. No timers or I/O on import. */
|
|
30
|
+
export class HubGroupRooms {
|
|
31
|
+
core;
|
|
32
|
+
listeners = new Set();
|
|
33
|
+
agents;
|
|
34
|
+
assistant;
|
|
35
|
+
directory;
|
|
36
|
+
constructor(core) {
|
|
37
|
+
this.core = core;
|
|
38
|
+
this.agents = new HubRoomAgents(core, event => this.emit(event), post => this.projection(post));
|
|
39
|
+
this.assistant = new HubRoomAssistant(core, event => this.emit(event), post => this.projection(post));
|
|
40
|
+
this.directory = new RoomDirectoryCandidates(core);
|
|
41
|
+
}
|
|
42
|
+
has(id) { return this.core.atomic(db => !!db.prepare("SELECT 1 FROM hub_group_rooms WHERE id=?").get(id)); }
|
|
43
|
+
subscribe(listener) { this.listeners.add(listener); return () => this.listeners.delete(listener); }
|
|
44
|
+
/** Cheap queue admission only; event() still freshly authorizes and projects the eventual disclosure. */
|
|
45
|
+
interested(person, event) {
|
|
46
|
+
if (event.removedPerson)
|
|
47
|
+
return event.removedPerson === person;
|
|
48
|
+
return this.core.atomic(db => !!db.prepare("SELECT 1 FROM hub_room_members WHERE room_id=? AND kind='person' AND subject=?").get(event.roomId, person));
|
|
49
|
+
}
|
|
50
|
+
async close() { this.listeners.clear(); this.directory.close(); this.assistant.close(); await this.agents.close(); }
|
|
51
|
+
/** The direct-chat receiver shares this stream without changing its grant-backed delivery path. */
|
|
52
|
+
publish(event) { this.emit(event); }
|
|
53
|
+
/** C owns invitation checks; these synchronous effects remain on its current SQL transaction. */
|
|
54
|
+
invitationHost() {
|
|
55
|
+
const room = (db, id) => {
|
|
56
|
+
const row = this.room(db, id);
|
|
57
|
+
if (row.assistant_for)
|
|
58
|
+
throw new HubRoomRefusal(403, "denied");
|
|
59
|
+
if (row.archived)
|
|
60
|
+
throw new HubRoomRefusal(409, "archived");
|
|
61
|
+
return { id, name: row.name, people: db.prepare("SELECT COUNT(*) n FROM hub_room_members WHERE room_id=? AND kind='person'").get(id).n };
|
|
62
|
+
};
|
|
63
|
+
const owned = (db, id, owner) => {
|
|
64
|
+
const e = readExposure(db, id);
|
|
65
|
+
if (!e || owner !== undefined && e.owner !== owner)
|
|
66
|
+
throw new HubRoomRefusal(404, "not-found");
|
|
67
|
+
return e;
|
|
68
|
+
};
|
|
69
|
+
return Object.freeze({
|
|
70
|
+
managerRoom: (db, id, actor) => { this.member(db, id, actor, true); return room(db, id); },
|
|
71
|
+
room,
|
|
72
|
+
publishedExposure: (db, id) => {
|
|
73
|
+
const e = owned(db, id);
|
|
74
|
+
if (exposureOf(e).state !== "published")
|
|
75
|
+
throw new HubRoomRefusal(403, "denied");
|
|
76
|
+
return { id, name: e.display_name, owner: { id: e.owner, name: this.name(db, e.owner) } };
|
|
77
|
+
},
|
|
78
|
+
addAgentMember: (db, tx, id, exposure, owner) => this.addAgentMember(db, tx, id, exposure, owner),
|
|
79
|
+
roomsForExposure: (db, id, owner) => {
|
|
80
|
+
owned(db, id, owner);
|
|
81
|
+
return db.prepare(`SELECT r.id,r.name,m.id memberId,(SELECT COUNT(*) FROM hub_room_members p WHERE p.room_id=r.id AND p.kind='person') people
|
|
82
|
+
FROM hub_room_members m JOIN hub_group_rooms r ON r.id=m.room_id WHERE m.kind='agent' AND m.subject=? AND m.owner_at_join=? ORDER BY r.name,r.id`)
|
|
83
|
+
.all(id, owner);
|
|
84
|
+
},
|
|
85
|
+
changed: ({ roomId, ownerId, membershipChanged }) => {
|
|
86
|
+
if (!this.core.live())
|
|
87
|
+
return;
|
|
88
|
+
this.emit({ type: "invitation.changed", roomId, data: {}, audiencePerson: ownerId });
|
|
89
|
+
if (!membershipChanged)
|
|
90
|
+
return;
|
|
91
|
+
this.emit({ type: "member.changed", roomId, data: { roomId } });
|
|
92
|
+
this.emit({ type: "room.changed", roomId, data: { roomId } });
|
|
93
|
+
const latest = this.core.atomic(db => db.prepare("SELECT * FROM hub_room_posts WHERE room_id=? ORDER BY sequence DESC LIMIT 1").get(roomId));
|
|
94
|
+
if (latest?.author_kind === "system")
|
|
95
|
+
this.emit({ type: "message.added", roomId, data: { roomId, message: this.projection(latest) }, sequence: latest.sequence });
|
|
96
|
+
},
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
async roomsForExposure(ctx, exposure) {
|
|
100
|
+
const me = await this.who(ctx);
|
|
101
|
+
return this.core.atomic(db => this.invitationHost().roomsForExposure(db, exposure, me), this.fence(ctx));
|
|
102
|
+
}
|
|
103
|
+
emit(event) { for (const listener of this.listeners) {
|
|
104
|
+
try {
|
|
105
|
+
listener(event);
|
|
106
|
+
}
|
|
107
|
+
catch { /* a disconnected viewer cannot fail a committed write */ }
|
|
108
|
+
} }
|
|
109
|
+
fence(ctx) { return { assertCurrent: () => { this.core.check(); if (ctx.isCurrent?.() !== true)
|
|
110
|
+
throw new HubProtocolError("revoked"); } }; }
|
|
111
|
+
async who(ctx) { const person = await ctx.requirePerson(); this.fence(ctx).assertCurrent(); return personKeyString(person); }
|
|
112
|
+
room(db, id) {
|
|
113
|
+
const row = db.prepare("SELECT * FROM hub_group_rooms WHERE id=?").get(id);
|
|
114
|
+
if (!row)
|
|
115
|
+
throw new HubRoomRefusal(404, "not-found");
|
|
116
|
+
return row;
|
|
117
|
+
}
|
|
118
|
+
member(db, id, person, manager = false) {
|
|
119
|
+
this.room(db, id);
|
|
120
|
+
const row = db.prepare("SELECT * FROM hub_room_members WHERE room_id=? AND kind='person' AND subject=?").get(id, person);
|
|
121
|
+
if (!row)
|
|
122
|
+
throw new HubRoomRefusal(403, "not-member");
|
|
123
|
+
if (manager && row.role !== "manager")
|
|
124
|
+
throw new HubRoomRefusal(403, "denied");
|
|
125
|
+
return row;
|
|
126
|
+
}
|
|
127
|
+
name(db, person) {
|
|
128
|
+
return db.prepare("SELECT display_name FROM hub_people WHERE person=? UNION ALL SELECT display_name FROM hub_room_people WHERE person=? LIMIT 1").get(person, person)?.display_name ?? person;
|
|
129
|
+
}
|
|
130
|
+
maximum(db, id) { return db.prepare("SELECT COALESCE(MAX(sequence),0) AS n FROM hub_room_posts WHERE room_id=?").get(id).n; }
|
|
131
|
+
projection(row) {
|
|
132
|
+
const system = row.author_kind === "system" && ["room-created", "member-joined", "admin-joined"].includes(row.author_id)
|
|
133
|
+
? { kind: row.author_id, name: row.author_name } : undefined;
|
|
134
|
+
// Agent rows retain their owner-at-message in author_subject; a later exposure transfer must not relabel history.
|
|
135
|
+
const owner = row.author_kind === "agent" ? this.core.atomic(db => ({ name: this.name(db, row.author_subject) })) : undefined;
|
|
136
|
+
return { id: row.id, roomId: row.room_id, at: row.created_at, sequence: row.sequence, author: { kind: row.author_kind, id: row.author_id, name: row.author_name, ...(owner ? { owner } : {}) },
|
|
137
|
+
...(system ? { system } : {}), text: this.core.deps.content.get(row.content_ref)?.question ?? "", mentions: JSON.parse(row.mentions),
|
|
138
|
+
...(row.reply_to ? { replyTo: row.reply_to } : {}), ...(row.idempotency_key ? { idempotencyKey: row.idempotency_key } : {}),
|
|
139
|
+
...(row.stage ? { stage: row.stage } : {}), ...(row.answer_to ? { answerTo: row.answer_to } : {}),
|
|
140
|
+
...this.core.atomic(db => {
|
|
141
|
+
const a = this.agents.progress(db, row.id), b = this.assistant.progress(db, row.id);
|
|
142
|
+
const ask = a || b ? { total: (a?.total ?? 0) + (b?.total ?? 0), answered: (a?.answered ?? 0) + (b?.answered ?? 0), working: (a?.working ?? 0) + (b?.working ?? 0), later: (a?.later ?? 0) + (b?.later ?? 0) } : undefined;
|
|
143
|
+
return { ...(ask ? { ask } : {}), ...(this.assistant.warning(db, row.id) ? { warning: "assistant-unavailable" } : {}) };
|
|
144
|
+
}) };
|
|
145
|
+
}
|
|
146
|
+
insert(db, tx, roomId, author, text, mentions = [], key = null, digest = "", replyTo = null) {
|
|
147
|
+
const row = { id: randomUUID(), room_id: roomId, sequence: this.maximum(db, roomId) + 1, author_kind: author.kind, author_id: author.id,
|
|
148
|
+
author_subject: author.subject, author_name: author.name, content_ref: this.core.deps.content.put(tx, { question: text }), digest, mentions: JSON.stringify(mentions),
|
|
149
|
+
reply_to: replyTo, idempotency_key: key, created_at: this.core.now() };
|
|
150
|
+
db.prepare(`INSERT INTO hub_room_posts(id,room_id,sequence,author_kind,author_id,author_subject,author_name,content_ref,digest,mentions,reply_to,idempotency_key,created_at)
|
|
151
|
+
VALUES(@id,@room_id,@sequence,@author_kind,@author_id,@author_subject,@author_name,@content_ref,@digest,@mentions,@reply_to,@idempotency_key,@created_at)`).run(row);
|
|
152
|
+
return row;
|
|
153
|
+
}
|
|
154
|
+
summary(db, room, member) {
|
|
155
|
+
const counts = db.prepare("SELECT kind,COUNT(*) AS n FROM hub_room_members WHERE room_id=? GROUP BY kind").all(room.id);
|
|
156
|
+
const floor = member.joined_sequence;
|
|
157
|
+
const last = db.prepare("SELECT * FROM hub_room_posts WHERE room_id=? AND sequence>=? AND (stage IS NULL OR stage='answered') AND (answer_to IS NULL OR EXISTS(SELECT 1 FROM hub_room_posts original WHERE original.id=hub_room_posts.answer_to AND original.sequence>=?)) ORDER BY sequence DESC LIMIT 1").get(room.id, floor, floor);
|
|
158
|
+
const unread = db.prepare("SELECT COUNT(*) AS n FROM hub_room_posts WHERE room_id=? AND sequence>=? AND sequence>? AND (author_kind<>'person' OR author_subject<>?) AND author_kind<>'system' AND (stage IS NULL OR stage='answered') AND (answer_to IS NULL OR EXISTS(SELECT 1 FROM hub_room_posts original WHERE original.id=hub_room_posts.answer_to AND original.sequence>=?))")
|
|
159
|
+
.get(room.id, floor, member.read_sequence, member.subject, floor).n;
|
|
160
|
+
const mentioned = !!db.prepare(`SELECT 1 FROM hub_room_posts p,json_each(p.mentions) j WHERE p.room_id=? AND p.sequence>=? AND p.sequence>?
|
|
161
|
+
AND p.author_subject<>? AND j.value IN (?,?) LIMIT 1`).get(room.id, floor, member.read_sequence, member.subject, member.id, member.subject);
|
|
162
|
+
const online = db.prepare(`SELECT COUNT(*) AS n FROM hub_room_members m WHERE m.room_id=? AND m.kind='person' AND EXISTS
|
|
163
|
+
(SELECT 1 FROM hub_sessions s WHERE s.person=m.subject AND s.state='active' AND s.expires_at>?)`).get(room.id, this.core.now()).n;
|
|
164
|
+
const projected = last ? this.projection(last) : undefined;
|
|
165
|
+
const summary = { id: room.id, kind: "group", name: room.name, people: counts.find(c => c.kind === "person")?.n ?? 0,
|
|
166
|
+
agents: counts.find(c => c.kind === "agent")?.n ?? 0, online, unread, lastAt: last?.created_at ?? room.created_at,
|
|
167
|
+
lastText: projected?.text.slice(0, 240) ?? "", ...(projected?.system ? { lastSystem: projected.system } : {}), lastAuthor: last?.author_name ?? "", mentioned, archived: room.archived === 1 };
|
|
168
|
+
return room.assistant_for ? { ...summary, kind: "direct", assistant: true, presence: this.assistant.presence(room.id) } : summary;
|
|
169
|
+
}
|
|
170
|
+
addAssistant(db, roomId) {
|
|
171
|
+
if (db.prepare("SELECT 1 FROM hub_room_members WHERE room_id=? AND kind='assistant'").get(roomId))
|
|
172
|
+
return;
|
|
173
|
+
const room = this.room(db, roomId);
|
|
174
|
+
db.prepare("INSERT INTO hub_room_members(id,room_id,kind,subject,role,joined_sequence) VALUES(?,?,'assistant','assistant','member',?)")
|
|
175
|
+
.run(randomUUID(), roomId, room.history_for_new === "all" ? 0 : this.maximum(db, roomId) + 1);
|
|
176
|
+
}
|
|
177
|
+
assistantRoom(db, me) {
|
|
178
|
+
if (!this.assistant.availability()?.enabled || db.prepare("SELECT 1 FROM hub_group_rooms WHERE assistant_for=?").get(me))
|
|
179
|
+
return;
|
|
180
|
+
const id = randomUUID();
|
|
181
|
+
db.prepare("INSERT INTO hub_group_rooms(id,name,created_by,created_at,assistant_for) VALUES(?,'Hub assistant',?,?,?)").run(id, me, this.core.now(), me);
|
|
182
|
+
db.prepare("INSERT INTO hub_room_members(id,room_id,kind,subject,role,joined_sequence) VALUES(?,?,'person',?,'member',0)").run(randomUUID(), id, me);
|
|
183
|
+
this.addAssistant(db, id);
|
|
184
|
+
}
|
|
185
|
+
async list(ctx) {
|
|
186
|
+
const me = await this.who(ctx);
|
|
187
|
+
this.agents.sweep();
|
|
188
|
+
return this.core.atomic(db => {
|
|
189
|
+
this.assistantRoom(db, me);
|
|
190
|
+
return { rooms: db.prepare("SELECT * FROM hub_room_members WHERE kind='person' AND subject=?").all(me)
|
|
191
|
+
.map(m => this.summary(db, this.room(db, m.room_id), m)).sort((a, b) => b.lastAt - a.lastAt),
|
|
192
|
+
me: { canCreateRooms: canCreateRoom(this.core, db, me), assistant: this.assistant.availability()?.enabled === true }, invitations: 0 };
|
|
193
|
+
}, this.fence(ctx));
|
|
194
|
+
}
|
|
195
|
+
async candidates(ctx, q, kind) {
|
|
196
|
+
if (q.length > 64 || /[\u0000-\u001f\u007f]/.test(q) || !["person", "team", "group", "agent"].includes(kind))
|
|
197
|
+
throw new HubProtocolError("invalid");
|
|
198
|
+
const me = await this.who(ctx);
|
|
199
|
+
const allowed = (db) => canCreateRoom(this.core, db, me) || !!db.prepare(`SELECT 1 FROM hub_room_members m JOIN hub_group_rooms r ON r.id=m.room_id
|
|
200
|
+
WHERE m.kind='person' AND m.subject=? AND m.role='manager' AND r.archived=0`).get(me);
|
|
201
|
+
if (kind === "agent") {
|
|
202
|
+
this.core.atomic(db => { if (!allowed(db))
|
|
203
|
+
throw new HubRoomRefusal(403, "denied"); }, this.fence(ctx));
|
|
204
|
+
const view = await new HubCatalogService(this.core).visible(ctx);
|
|
205
|
+
const hits = [...view.mine.map(e => ({ kind: "agent", id: e.id, name: e.displayName, mine: true, owner: { name: this.core.atomic(db => this.name(db, me)) } })),
|
|
206
|
+
...view.askable.map(({ exposure: e, ownerName }) => ({ kind: "agent", id: e.id, name: e.displayName, mine: false, owner: { name: ownerName } }))]
|
|
207
|
+
.filter(e => e.name.toLowerCase().startsWith(q.trim().toLowerCase())).slice(0, 20);
|
|
208
|
+
this.fence(ctx).assertCurrent();
|
|
209
|
+
return hits;
|
|
210
|
+
}
|
|
211
|
+
if (q.trim().length >= 2 && (kind === "group" && this.core.deps.directory.searchPolicyGroups || kind === "person" && this.core.deps.directory.searchGrantPeople)) {
|
|
212
|
+
return this.directory.search(ctx, me, q.trim(), kind, () => this.core.atomic(db => {
|
|
213
|
+
if (!allowed(db))
|
|
214
|
+
throw new HubRoomRefusal(403, "denied");
|
|
215
|
+
}, this.fence(ctx)));
|
|
216
|
+
}
|
|
217
|
+
const rows = this.core.atomic(db => {
|
|
218
|
+
if (!allowed(db))
|
|
219
|
+
throw new HubRoomRefusal(403, "denied");
|
|
220
|
+
if (kind === "team")
|
|
221
|
+
return [];
|
|
222
|
+
if (kind !== "person")
|
|
223
|
+
return [];
|
|
224
|
+
const prefix = `${q.trim().replace(/[\\%_]/g, c => `\\${c}`)}%`;
|
|
225
|
+
return db.prepare("SELECT person,display_name FROM hub_people WHERE status='active' AND (display_name LIKE ? ESCAPE '\\' OR login_label LIKE ? ESCAPE '\\') ORDER BY display_name,person LIMIT 20")
|
|
226
|
+
.all(prefix, prefix);
|
|
227
|
+
}, this.fence(ctx));
|
|
228
|
+
const hits = [];
|
|
229
|
+
for (const row of rows)
|
|
230
|
+
if (await this.core.eligible(personFromString(row.person)))
|
|
231
|
+
hits.push({ kind: "person", id: row.person, name: row.display_name });
|
|
232
|
+
return this.core.atomic(db => {
|
|
233
|
+
if (!allowed(db))
|
|
234
|
+
throw new HubRoomRefusal(403, "denied");
|
|
235
|
+
return kind === "team" ? (this.core.deps.roomTeamCandidates?.(db, q.trim()) ?? []).slice(0, 20).map(row => ({ kind: "team", ...row })) : hits;
|
|
236
|
+
}, this.fence(ctx));
|
|
237
|
+
}
|
|
238
|
+
candidateAllowed(ctx, me) {
|
|
239
|
+
this.core.atomic(db => {
|
|
240
|
+
if (!canCreateRoom(this.core, db, me) && !db.prepare(`SELECT 1 FROM hub_room_members m JOIN hub_group_rooms r ON r.id=m.room_id
|
|
241
|
+
WHERE m.kind='person' AND m.subject=? AND m.role='manager' AND r.archived=0 AND r.assistant_for IS NULL`).get(me))
|
|
242
|
+
throw new HubRoomRefusal(403, "denied");
|
|
243
|
+
}, this.fence(ctx));
|
|
244
|
+
}
|
|
245
|
+
async groupSelection(ctx, me, id) {
|
|
246
|
+
try {
|
|
247
|
+
return await this.directory.roster(ctx, me, id, () => this.candidateAllowed(ctx, me));
|
|
248
|
+
}
|
|
249
|
+
catch (e) {
|
|
250
|
+
if (e instanceof HubRoomRefusal || e instanceof HubProtocolError && ["revoked", "denied", "backpressure"].includes(e.code))
|
|
251
|
+
throw e;
|
|
252
|
+
throw new HubRoomRefusal(503, "group-unavailable", { group: id });
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
async groupCandidate(ctx, id) {
|
|
256
|
+
const me = await this.who(ctx), selection = await this.groupSelection(ctx, me, id);
|
|
257
|
+
return { kind: "group", id, name: selection.name, people: selection.people.length };
|
|
258
|
+
}
|
|
259
|
+
async create(ctx, input) {
|
|
260
|
+
const body = validated(createSchema, input), me = await this.who(ctx);
|
|
261
|
+
this.core.atomic(db => { if (!canCreateRoom(this.core, db, me))
|
|
262
|
+
throw new HubRoomRefusal(403, "denied"); }, this.fence(ctx));
|
|
263
|
+
const teams = this.core.deps.roomTeams;
|
|
264
|
+
const expansion = (db) => {
|
|
265
|
+
if (!body.teams.length)
|
|
266
|
+
return { teams: [], people: [], missing: [] };
|
|
267
|
+
if (!teams || teams !== this.core.deps.roomTeams)
|
|
268
|
+
throw new HubProtocolError("unavailable");
|
|
269
|
+
const found = teams.expand(db, body.teams);
|
|
270
|
+
if (found.missing.length)
|
|
271
|
+
throw new HubProtocolError("invalid");
|
|
272
|
+
return found;
|
|
273
|
+
};
|
|
274
|
+
const selected = this.core.atomic(expansion), image = JSON.stringify(selected);
|
|
275
|
+
const groupPeople = [];
|
|
276
|
+
for (const group of new Set(body.groups))
|
|
277
|
+
groupPeople.push(...(await this.groupSelection(ctx, me, group)).people.map(p => p.id));
|
|
278
|
+
const people = [...new Set([me, ...body.people, ...selected.people, ...groupPeople])];
|
|
279
|
+
if (people.length > 100)
|
|
280
|
+
throw new HubRoomRefusal(409, "people-limit", { people: people.length, limit: 100 });
|
|
281
|
+
const selectedPeople = [];
|
|
282
|
+
for (const person of people)
|
|
283
|
+
if (person !== me) {
|
|
284
|
+
const current = await this.core.current(personFromString(person));
|
|
285
|
+
if (!current)
|
|
286
|
+
throw new HubRoomRefusal(400, "invalid-person");
|
|
287
|
+
selectedPeople.push({ id: person, ...current });
|
|
288
|
+
}
|
|
289
|
+
const permits = await Promise.all(body.agents.map(id => this.core.groupExposure(id, null, this.core.groupContext(ctx))));
|
|
290
|
+
const room = this.core.atomic((db, tx) => {
|
|
291
|
+
for (const permit of permits)
|
|
292
|
+
permit.assertCurrent();
|
|
293
|
+
if (JSON.stringify(expansion(db)) !== image)
|
|
294
|
+
throw new HubProtocolError("conflict");
|
|
295
|
+
if (!canCreateRoom(this.core, db, me))
|
|
296
|
+
throw new HubRoomRefusal(403, "denied");
|
|
297
|
+
rememberRoomPeople(db, selectedPeople, this.core.now());
|
|
298
|
+
const id = randomUUID();
|
|
299
|
+
db.prepare("INSERT INTO hub_group_rooms(id,name,created_by,created_at) VALUES(?,?,?,?)").run(id, body.name, me, this.core.now());
|
|
300
|
+
for (const person of people)
|
|
301
|
+
db.prepare("INSERT INTO hub_room_members(id,room_id,kind,subject,role,joined_sequence) VALUES(?,?,'person',?,?,0)").run(randomUUID(), id, person, person === me ? "manager" : "member");
|
|
302
|
+
this.insert(db, tx, id, { kind: "system", id: "room-created", subject: me, name: this.name(db, me) }, `${this.name(db, me)} created this room.`);
|
|
303
|
+
for (const agent of body.agents)
|
|
304
|
+
this.addAgentMember(db, tx, id, agent, me);
|
|
305
|
+
if (body.assistant)
|
|
306
|
+
this.addAssistant(db, id);
|
|
307
|
+
this.core.audit(tx, { requestId: ctx.requestId, actor: { kind: "person", person: personFromString(me) }, action: "room.created", result: "completed", roomId: id });
|
|
308
|
+
return this.summary(db, this.room(db, id), this.member(db, id, me));
|
|
309
|
+
}, { assertCurrent: () => { this.fence(ctx).assertCurrent(); for (const permit of permits)
|
|
310
|
+
permit.assertCurrent(); } });
|
|
311
|
+
this.emit({ type: "room.changed", roomId: room.id, data: { roomId: room.id } });
|
|
312
|
+
return { room };
|
|
313
|
+
}
|
|
314
|
+
async detail(ctx, id) {
|
|
315
|
+
const me = await this.who(ctx);
|
|
316
|
+
this.agents.sweep(id);
|
|
317
|
+
this.core.atomic(db => { this.member(db, id, me); }, this.fence(ctx));
|
|
318
|
+
const canAsk = new Set(), directProofs = [];
|
|
319
|
+
const candidates = this.core.atomic(db => db.prepare(`SELECT g.id,g.exposure_id FROM hub_grants g JOIN hub_room_members m ON m.subject=g.exposure_id
|
|
320
|
+
WHERE m.room_id=? AND m.kind='agent' AND g.state='active' AND g.starts_at<=? AND g.expires_at>? AND (g.subject_kind='group' OR g.subject=?)`)
|
|
321
|
+
.all(id, this.core.now(), this.core.now(), me));
|
|
322
|
+
for (const g of candidates) {
|
|
323
|
+
try {
|
|
324
|
+
const permit = await this.core.exposureGrant(g.exposure_id, g.id, personFromString(me), this.core.groupContext(ctx));
|
|
325
|
+
permit.assertCurrent();
|
|
326
|
+
directProofs.push(permit);
|
|
327
|
+
canAsk.add(g.exposure_id);
|
|
328
|
+
}
|
|
329
|
+
catch { /* A room member may still read the room when a separate direct-chat allowance is held. */ }
|
|
330
|
+
}
|
|
331
|
+
return this.core.atomic(db => {
|
|
332
|
+
const own = this.member(db, id, me), room = this.room(db, id);
|
|
333
|
+
const members = db.prepare("SELECT * FROM hub_room_members WHERE room_id=? ORDER BY kind,id").all(id).map(m => {
|
|
334
|
+
if (m.kind === "assistant")
|
|
335
|
+
return { id: "assistant", kind: m.kind, name: "Hub assistant", role: m.role, presence: this.assistant.presence(id) };
|
|
336
|
+
if (m.kind === "agent") {
|
|
337
|
+
const e = readExposure(db, m.subject);
|
|
338
|
+
return { id: m.subject, kind: m.kind, name: e?.display_name ?? m.subject, role: m.role,
|
|
339
|
+
owner: { name: this.name(db, m.owner_at_join ?? "") }, ...this.agents.presence(m.subject), canAsk: canAsk.has(m.subject) };
|
|
340
|
+
}
|
|
341
|
+
return { id: m.id, kind: m.kind, name: this.name(db, m.subject), role: m.role,
|
|
342
|
+
signedIn: !!db.prepare("SELECT 1 FROM hub_people WHERE person=?").get(m.subject),
|
|
343
|
+
presence: db.prepare("SELECT 1 FROM hub_sessions WHERE person=? AND state='active' AND expires_at>?").get(m.subject, this.core.now()) ? "online" : "offline" };
|
|
344
|
+
});
|
|
345
|
+
return { room: this.summary(db, room, own), me: { id: own.id, role: own.role }, members,
|
|
346
|
+
settings: { allMention: room.all_mention === 1, historyForNew: room.history_for_new, ...(members.some(m => m.kind === "assistant") ? { assistant: true } : {}) }, invited: [] };
|
|
347
|
+
}, { assertCurrent: () => { this.fence(ctx).assertCurrent(); for (const proof of directProofs)
|
|
348
|
+
proof.assertCurrent(); } });
|
|
349
|
+
}
|
|
350
|
+
async patch(ctx, id, input) {
|
|
351
|
+
const body = validated(patchSchema, input), me = await this.who(ctx);
|
|
352
|
+
this.core.atomic((db, tx) => {
|
|
353
|
+
this.member(db, id, me, true);
|
|
354
|
+
const row = this.room(db, id);
|
|
355
|
+
if (row.assistant_for)
|
|
356
|
+
throw new HubRoomRefusal(403, "denied");
|
|
357
|
+
if (body.assistant === true)
|
|
358
|
+
this.addAssistant(db, id);
|
|
359
|
+
else if (body.assistant === false)
|
|
360
|
+
db.prepare("DELETE FROM hub_room_members WHERE room_id=? AND kind='assistant'").run(id);
|
|
361
|
+
db.prepare("UPDATE hub_group_rooms SET name=?,all_mention=?,history_for_new=?,archived=? WHERE id=?")
|
|
362
|
+
.run(body.name ?? row.name, body.allMention === undefined ? row.all_mention : Number(body.allMention), body.historyForNew ?? row.history_for_new, body.archived === undefined ? row.archived : Number(body.archived), id);
|
|
363
|
+
this.core.audit(tx, { requestId: ctx.requestId, actor: { kind: "person", person: personFromString(me) }, action: "room.updated", result: "completed", roomId: id });
|
|
364
|
+
}, this.fence(ctx));
|
|
365
|
+
if (body.archived || body.assistant === false)
|
|
366
|
+
this.assistant.forget(id);
|
|
367
|
+
this.agents.sweep(id);
|
|
368
|
+
this.emit({ type: "room.changed", roomId: id, data: { roomId: id } });
|
|
369
|
+
return this.detail(ctx, id);
|
|
370
|
+
}
|
|
371
|
+
async add(ctx, id, input) {
|
|
372
|
+
const body = validated(z.discriminatedUnion("kind", [z.strictObject({ kind: z.literal("person"), id: personId }), z.strictObject({ kind: z.literal("agent"), id: uuid }),
|
|
373
|
+
z.strictObject({ kind: z.literal("team"), id: uuid }), z.strictObject({ kind: z.literal("group"), id: uuid }), z.strictObject({ kind: z.literal("assistant") })]), input), me = await this.who(ctx);
|
|
374
|
+
if (body.kind === "assistant")
|
|
375
|
+
return this.patch(ctx, id, { assistant: true });
|
|
376
|
+
if (body.kind === "agent") {
|
|
377
|
+
this.core.atomic(db => { this.member(db, id, me, true); }, this.fence(ctx));
|
|
378
|
+
const permit = await this.core.groupExposure(body.id, null, this.core.groupContext(ctx));
|
|
379
|
+
this.core.atomic((db, tx) => {
|
|
380
|
+
this.member(db, id, me, true);
|
|
381
|
+
this.addAgentMember(db, tx, id, body.id, me);
|
|
382
|
+
this.core.audit(tx, { requestId: ctx.requestId, actor: { kind: "person", person: personFromString(me) }, action: "room.agent-added", result: "completed", roomId: id, exposureId: body.id });
|
|
383
|
+
}, { assertCurrent: () => { this.fence(ctx).assertCurrent(); permit.assertCurrent(); } });
|
|
384
|
+
this.emit({ type: "member.changed", roomId: id, data: { roomId: id } });
|
|
385
|
+
return { ok: true };
|
|
386
|
+
}
|
|
387
|
+
this.core.atomic(db => { this.member(db, id, me, true); }, this.fence(ctx));
|
|
388
|
+
const teams = this.core.deps.roomTeams;
|
|
389
|
+
const expansion = (db) => {
|
|
390
|
+
if (body.kind !== "team")
|
|
391
|
+
return null;
|
|
392
|
+
if (!teams || this.core.deps.roomTeams !== teams)
|
|
393
|
+
throw new HubProtocolError("unavailable");
|
|
394
|
+
const out = teams.expand(db, [body.id]);
|
|
395
|
+
if (out.missing.length)
|
|
396
|
+
throw new HubProtocolError("invalid");
|
|
397
|
+
return out;
|
|
398
|
+
};
|
|
399
|
+
const selectedTeam = this.core.atomic(expansion), teamImage = JSON.stringify(selectedTeam);
|
|
400
|
+
const people = body.kind === "person" ? [body.id] : body.kind === "team" ? selectedTeam.people : (await this.groupSelection(ctx, me, body.id)).people.map(p => p.id);
|
|
401
|
+
const ids = [...new Set(people)];
|
|
402
|
+
const assertCount = (db) => {
|
|
403
|
+
const current = db.prepare("SELECT subject FROM hub_room_members WHERE room_id=? AND kind='person'").all(id).map(m => m.subject);
|
|
404
|
+
const count = new Set([...current, ...ids]).size;
|
|
405
|
+
if (count > 100)
|
|
406
|
+
throw new HubRoomRefusal(409, "people-limit", { people: count, limit: 100 });
|
|
407
|
+
};
|
|
408
|
+
this.core.atomic(assertCount, this.fence(ctx));
|
|
409
|
+
const selectedPeople = [];
|
|
410
|
+
for (const person of ids) {
|
|
411
|
+
const selected = await this.core.current(personFromString(person));
|
|
412
|
+
if (!selected)
|
|
413
|
+
throw new HubRoomRefusal(400, "invalid-person");
|
|
414
|
+
selectedPeople.push({ id: person, ...selected });
|
|
415
|
+
}
|
|
416
|
+
this.core.atomic((db, tx) => {
|
|
417
|
+
this.member(db, id, me, true);
|
|
418
|
+
const room = this.room(db, id);
|
|
419
|
+
if (room.assistant_for)
|
|
420
|
+
throw new HubRoomRefusal(403, "denied");
|
|
421
|
+
if (room.archived)
|
|
422
|
+
throw new HubRoomRefusal(409, "archived");
|
|
423
|
+
if (JSON.stringify(expansion(db)) !== teamImage)
|
|
424
|
+
throw new HubProtocolError("conflict");
|
|
425
|
+
assertCount(db);
|
|
426
|
+
rememberRoomPeople(db, selectedPeople, this.core.now());
|
|
427
|
+
for (const person of ids) {
|
|
428
|
+
if (db.prepare("SELECT 1 FROM hub_room_members WHERE room_id=? AND kind='person' AND subject=?").get(id, person))
|
|
429
|
+
continue;
|
|
430
|
+
const seq = this.maximum(db, id) + 1;
|
|
431
|
+
db.prepare("INSERT INTO hub_room_members(id,room_id,kind,subject,role,joined_sequence,read_sequence) VALUES(?,?,'person',?,'member',?,?)")
|
|
432
|
+
.run(randomUUID(), id, person, room.history_for_new === "all" ? 0 : seq, 0);
|
|
433
|
+
this.insert(db, tx, id, { kind: "system", id: "member-joined", subject: person, name: this.name(db, person) }, `${this.name(db, person)} joined this room.`);
|
|
434
|
+
}
|
|
435
|
+
this.core.audit(tx, { requestId: ctx.requestId, actor: { kind: "person", person: personFromString(me) }, action: "room.member-added", result: "completed", roomId: id });
|
|
436
|
+
}, this.fence(ctx));
|
|
437
|
+
this.emit({ type: "room.changed", roomId: id, data: { roomId: id } });
|
|
438
|
+
this.emit({ type: "member.changed", roomId: id, data: { roomId: id } });
|
|
439
|
+
return { ok: true };
|
|
440
|
+
}
|
|
441
|
+
/** C invitation acceptance calls this only inside its guarded exact-invitation transaction. */
|
|
442
|
+
addAgentMember(db, tx, roomId, exposureId, owner) {
|
|
443
|
+
const room = this.room(db, roomId);
|
|
444
|
+
if (room.assistant_for)
|
|
445
|
+
throw new HubRoomRefusal(403, "denied");
|
|
446
|
+
if (room.archived)
|
|
447
|
+
throw new HubRoomRefusal(409, "archived");
|
|
448
|
+
const e = readExposure(db, exposureId);
|
|
449
|
+
if (!e)
|
|
450
|
+
throw new HubRoomRefusal(404, "not-found");
|
|
451
|
+
if (e.owner !== owner || exposureOf(e).state !== "published")
|
|
452
|
+
throw new HubRoomRefusal(403, "denied");
|
|
453
|
+
const old = db.prepare("SELECT owner_at_join FROM hub_room_members WHERE room_id=? AND kind='agent' AND subject=?").get(roomId, exposureId);
|
|
454
|
+
if (old) {
|
|
455
|
+
if (old.owner_at_join !== owner)
|
|
456
|
+
throw new HubProtocolError("conflict");
|
|
457
|
+
return;
|
|
458
|
+
}
|
|
459
|
+
const seq = this.maximum(db, roomId) + 1;
|
|
460
|
+
db.prepare("INSERT INTO hub_room_members(id,room_id,kind,subject,role,joined_sequence,owner_at_join) VALUES(?,?,'agent',?,'member',?,?)")
|
|
461
|
+
.run(randomUUID(), roomId, exposureId, room.history_for_new === "all" ? 0 : seq, owner);
|
|
462
|
+
this.insert(db, tx, roomId, { kind: "system", id: "member-joined", subject: exposureId, name: e.display_name }, `${e.display_name} joined this room.`);
|
|
463
|
+
}
|
|
464
|
+
async changeMember(ctx, id, memberId, role) {
|
|
465
|
+
const me = await this.who(ctx);
|
|
466
|
+
const removed = this.core.atomic((db, tx) => {
|
|
467
|
+
if (this.room(db, id).assistant_for)
|
|
468
|
+
throw new HubRoomRefusal(403, "denied");
|
|
469
|
+
const own = db.prepare("SELECT * FROM hub_room_members WHERE room_id=? AND kind='person' AND subject=?").get(id, me);
|
|
470
|
+
const target = db.prepare("SELECT * FROM hub_room_members WHERE room_id=? AND (id=? OR (kind='agent' AND subject=?))").get(id, memberId, memberId);
|
|
471
|
+
if (!target)
|
|
472
|
+
throw new HubRoomRefusal(404, "not-found");
|
|
473
|
+
const ownerRemovesAgent = role === undefined && target.kind === "agent" && target.owner_at_join === me;
|
|
474
|
+
if (own?.role !== "manager" && !(role === undefined && target.id === own?.id) && !ownerRemovesAgent)
|
|
475
|
+
throw new HubRoomRefusal(403, "denied");
|
|
476
|
+
if (role && target.kind !== "person")
|
|
477
|
+
throw new HubProtocolError("invalid");
|
|
478
|
+
if (target.role === "manager" && role !== "manager" && db.prepare("SELECT COUNT(*) AS n FROM hub_room_members WHERE room_id=? AND role='manager'").get(id).n <= 1)
|
|
479
|
+
throw new HubRoomRefusal(409, "last-manager");
|
|
480
|
+
if (role)
|
|
481
|
+
db.prepare("UPDATE hub_room_members SET role=? WHERE id=?").run(role, target.id);
|
|
482
|
+
else
|
|
483
|
+
db.prepare("DELETE FROM hub_room_members WHERE id=?").run(target.id);
|
|
484
|
+
this.core.audit(tx, { requestId: ctx.requestId, actor: { kind: "person", person: personFromString(me) }, action: role ? "room.member-role" : "room.member-removed", result: "completed", roomId: id });
|
|
485
|
+
return !role && target.kind === "person" ? target.subject : null;
|
|
486
|
+
}, this.fence(ctx));
|
|
487
|
+
this.agents.sweep(id);
|
|
488
|
+
if (removed)
|
|
489
|
+
this.emit({ type: "room.changed", roomId: id, data: { roomId: id, removed: true }, removedPerson: removed });
|
|
490
|
+
this.emit({ type: "member.changed", roomId: id, data: { roomId: id } });
|
|
491
|
+
return { ok: true };
|
|
492
|
+
}
|
|
493
|
+
async messages(ctx, id, before, limit = 50) {
|
|
494
|
+
if (!Number.isSafeInteger(limit) || limit < 1 || limit > 100)
|
|
495
|
+
throw new HubProtocolError("invalid");
|
|
496
|
+
const me = await this.who(ctx);
|
|
497
|
+
this.agents.sweep(id);
|
|
498
|
+
return this.core.atomic(db => {
|
|
499
|
+
const member = this.member(db, id, me);
|
|
500
|
+
let until = Number.MAX_SAFE_INTEGER;
|
|
501
|
+
if (before) {
|
|
502
|
+
const row = db.prepare("SELECT sequence FROM hub_room_posts WHERE id=? AND room_id=? AND sequence>=?").get(validated(uuid, before), id, member.joined_sequence);
|
|
503
|
+
if (!row)
|
|
504
|
+
throw new HubRoomRefusal(404, "not-found");
|
|
505
|
+
until = row.sequence;
|
|
506
|
+
}
|
|
507
|
+
const rows = db.prepare("SELECT * FROM hub_room_posts WHERE room_id=? AND sequence>=? AND sequence<? AND (answer_to IS NULL OR EXISTS(SELECT 1 FROM hub_room_posts original WHERE original.id=hub_room_posts.answer_to AND original.sequence>=?)) ORDER BY sequence DESC LIMIT ?").all(id, member.joined_sequence, until, member.joined_sequence, limit + 1);
|
|
508
|
+
return { messages: rows.slice(0, limit).reverse().map(row => this.projection(row)), more: rows.length > limit };
|
|
509
|
+
}, this.fence(ctx));
|
|
510
|
+
}
|
|
511
|
+
async post(ctx, id, input) {
|
|
512
|
+
const body = validated(postSchema, input), me = await this.who(ctx);
|
|
513
|
+
const digest = createHash("sha256").update(JSON.stringify({ text: body.text, mentions: body.mentions, replyTo: body.replyTo ?? null })).digest("hex");
|
|
514
|
+
let added = false;
|
|
515
|
+
const row = this.core.atomic((db, tx) => {
|
|
516
|
+
const member = this.member(db, id, me), room = this.room(db, id);
|
|
517
|
+
if (room.archived)
|
|
518
|
+
throw new HubRoomRefusal(409, "archived");
|
|
519
|
+
if (body.mentions.includes("all") && !room.all_mention && member.role !== "manager")
|
|
520
|
+
throw new HubRoomRefusal(403, "all-mention-off");
|
|
521
|
+
const old = db.prepare("SELECT * FROM hub_room_posts WHERE room_id=? AND author_subject=? AND idempotency_key=?").get(id, me, body.idempotencyKey);
|
|
522
|
+
if (old) {
|
|
523
|
+
if (old.digest !== digest || old.sequence < member.joined_sequence)
|
|
524
|
+
throw new HubRoomRefusal(409, "conflict");
|
|
525
|
+
return old;
|
|
526
|
+
}
|
|
527
|
+
if (body.replyTo && !db.prepare("SELECT 1 FROM hub_room_posts WHERE id=? AND room_id=? AND sequence>=?").get(body.replyTo, id, member.joined_sequence))
|
|
528
|
+
throw new HubRoomRefusal(404, "not-found");
|
|
529
|
+
const post = this.insert(db, tx, id, { kind: "person", id: member.id, subject: me, name: this.name(db, me) }, body.text, body.mentions, body.idempotencyKey, digest, body.replyTo ?? null);
|
|
530
|
+
this.core.audit(tx, { requestId: ctx.requestId, actor: { kind: "person", person: personFromString(me) }, action: "room.message", result: "completed", roomId: id, messageId: post.id, contentRef: post.content_ref });
|
|
531
|
+
this.agents.enqueue(db, tx, { ...post, mentions: JSON.stringify(body.mentions.filter(m => m !== "assistant")) }, member.id);
|
|
532
|
+
this.assistant.enqueue(db, tx, post, member.id, room.assistant_for !== null);
|
|
533
|
+
added = true;
|
|
534
|
+
return post;
|
|
535
|
+
}, this.fence(ctx));
|
|
536
|
+
const message = this.projection(row);
|
|
537
|
+
if (added) {
|
|
538
|
+
this.emit({ type: "message.added", roomId: id, data: { roomId: id, message }, sequence: row.sequence });
|
|
539
|
+
this.emit({ type: "unread.changed", roomId: id, data: { roomId: id } });
|
|
540
|
+
}
|
|
541
|
+
if (added) {
|
|
542
|
+
this.agents.publishQueued(row.id);
|
|
543
|
+
this.assistant.publishQueued(row.id);
|
|
544
|
+
}
|
|
545
|
+
this.agents.wake();
|
|
546
|
+
this.assistant.wake();
|
|
547
|
+
return message;
|
|
548
|
+
}
|
|
549
|
+
async markRead(ctx, id, upTo) {
|
|
550
|
+
const target = validated(uuid, upTo), me = await this.who(ctx);
|
|
551
|
+
this.core.atomic(db => {
|
|
552
|
+
const member = this.member(db, id, me);
|
|
553
|
+
const row = db.prepare("SELECT sequence FROM hub_room_posts WHERE id=? AND room_id=? AND sequence>=?").get(target, id, member.joined_sequence);
|
|
554
|
+
if (!row)
|
|
555
|
+
throw new HubRoomRefusal(404, "not-found");
|
|
556
|
+
db.prepare("UPDATE hub_room_members SET read_sequence=MAX(read_sequence,?) WHERE id=?").run(row.sequence, member.id);
|
|
557
|
+
}, this.fence(ctx));
|
|
558
|
+
this.emit({ type: "unread.changed", roomId: id, data: { roomId: id } });
|
|
559
|
+
return { ok: true };
|
|
560
|
+
}
|
|
561
|
+
/** Each event is projected for its current viewer after a fresh eligibility check. */
|
|
562
|
+
async event(ctx, event) {
|
|
563
|
+
const me = await this.who(ctx);
|
|
564
|
+
return this.core.atomic(db => {
|
|
565
|
+
if (event.removedPerson)
|
|
566
|
+
return event.removedPerson === me ? event.data : null;
|
|
567
|
+
const member = db.prepare("SELECT * FROM hub_room_members WHERE room_id=? AND kind='person' AND subject=?").get(event.roomId, me);
|
|
568
|
+
if (!member || event.sequence !== undefined && event.sequence < member.joined_sequence)
|
|
569
|
+
return null;
|
|
570
|
+
const message = event.data.message;
|
|
571
|
+
if (message?.answerTo && !db.prepare("SELECT 1 FROM hub_room_posts WHERE id=? AND room_id=? AND sequence>=?").get(message.answerTo, event.roomId, member.joined_sequence))
|
|
572
|
+
return null;
|
|
573
|
+
if (event.type === "unread.changed")
|
|
574
|
+
return { roomId: event.roomId, unread: this.summary(db, this.room(db, event.roomId), member).unread };
|
|
575
|
+
if (event.type === "room.changed")
|
|
576
|
+
return { roomId: event.roomId, room: this.summary(db, this.room(db, event.roomId), member) };
|
|
577
|
+
return event.data;
|
|
578
|
+
}, this.fence(ctx));
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
//# sourceMappingURL=service.js.map
|