@vellumai/assistant 0.12.4 → 0.12.5-dev.202609251017.e467a6c
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/docs/architecture/memory.md +9 -1
- package/docs/architecture/scheduling.md +13 -5
- package/docs/desktop-browser-cli.md +3 -2
- package/docs/guardian-request-flow.md +1 -1
- package/node_modules/@vellumai/avatar-manifest/src/__tests__/notification-avatar.test.ts +48 -4
- package/node_modules/@vellumai/avatar-manifest/src/notification-avatar.ts +12 -8
- package/node_modules/@vellumai/ces-client/node_modules/@vellumai/service-contracts/src/__tests__/guardian-requests.test.ts +26 -0
- package/node_modules/@vellumai/ces-client/node_modules/@vellumai/service-contracts/src/guardian-requests.ts +77 -3
- package/node_modules/@vellumai/gateway-client/node_modules/@vellumai/service-contracts/src/__tests__/guardian-requests.test.ts +26 -0
- package/node_modules/@vellumai/gateway-client/node_modules/@vellumai/service-contracts/src/guardian-requests.ts +77 -3
- package/node_modules/@vellumai/gateway-client/src/outbound-contract.ts +2 -4
- package/node_modules/@vellumai/service-contracts/src/__tests__/guardian-requests.test.ts +26 -0
- package/node_modules/@vellumai/service-contracts/src/guardian-requests.ts +77 -3
- package/openapi.yaml +152 -3
- package/package.json +1 -1
- package/src/__tests__/agent-loop.test.ts +76 -0
- package/src/__tests__/anthropic-error-formatting.test.ts +9 -0
- package/src/__tests__/background-shell-bash.test.ts +34 -0
- package/src/__tests__/background-shell-host-bash.test.ts +98 -1
- package/src/__tests__/background-tool-registry.test.ts +39 -0
- package/src/__tests__/channel-readiness-discord.test.ts +8 -0
- package/src/__tests__/channel-readiness-slack-remote.test.ts +8 -0
- package/src/__tests__/client-os-metadata-persistence.test.ts +4 -20
- package/src/__tests__/conversation-agent-loop.test.ts +529 -1
- package/src/__tests__/conversation-error.test.ts +18 -0
- package/src/__tests__/conversation-pairing.test.ts +69 -0
- package/src/__tests__/conversation-queue.test.ts +257 -4
- package/src/__tests__/conversation-runtime-assembly.test.ts +27 -0
- package/src/__tests__/emit-signal-routing-intent.test.ts +62 -0
- package/src/__tests__/evict-conversations-for-reload.test.ts +21 -0
- package/src/__tests__/gemini-provider.test.ts +9 -0
- package/src/__tests__/host-cu-proxy.test.ts +79 -0
- package/src/__tests__/host-cu-routes-targeted.test.ts +14 -5
- package/src/__tests__/introduction-card-resolver.test.ts +53 -8
- package/src/__tests__/job-handler-registration-guard.test.ts +1 -0
- package/src/__tests__/non-member-access-request.test.ts +48 -0
- package/src/__tests__/notification-events-source-context-probe.test.ts +48 -1
- package/src/__tests__/notification-platform-adapter.test.ts +97 -0
- package/src/__tests__/notification-vellum-adapter.test.ts +103 -3
- package/src/__tests__/platform-callback-registration.test.ts +46 -1
- package/src/__tests__/plugin-import-boundary-guard.test.ts +3 -0
- package/src/__tests__/scaffold-managed-skill-tool.test.ts +116 -66
- package/src/__tests__/scheduler-disk-pressure.test.ts +1 -0
- package/src/__tests__/scheduler-result-notification.test.ts +521 -7
- package/src/__tests__/scheduler-wake.test.ts +1 -0
- package/src/__tests__/skill-load-inline-command.test.ts +35 -0
- package/src/__tests__/subagent-cron-run-attribution.test.ts +41 -2
- package/src/__tests__/subagent-manager-notify.test.ts +48 -0
- package/src/__tests__/trusted-contact-inline-approval-integration.test.ts +6 -6
- package/src/__tests__/usage-attribution.test.ts +31 -0
- package/src/__tests__/wake-schedule-escalation.test.ts +1 -0
- package/src/agent/loop.ts +23 -0
- package/src/api/ask-question-tool.ts +50 -0
- package/src/api/events/generation-handoff.ts +4 -0
- package/src/api/events/message-complete.ts +4 -0
- package/src/api/events/tool-result.ts +11 -0
- package/src/api/index.ts +11 -0
- package/src/api/requests/guardian-actions.test.ts +45 -0
- package/src/api/requests/guardian-actions.ts +20 -0
- package/src/api/responses/conversation-message.ts +4 -0
- package/src/api/responses/home.ts +27 -0
- package/src/approvals/guardian-decision-primitive.ts +6 -4
- package/src/approvals/guardian-request-resolvers.ts +35 -30
- package/src/avatar/notification-avatar.test.ts +42 -34
- package/src/avatar/notification-avatar.ts +1 -0
- package/src/calls/__tests__/call-controller.test.ts +18 -0
- package/src/calls/__tests__/voice-control-protocol.test.ts +6 -1
- package/src/calls/__tests__/voice-leg-coordinator.test.ts +70 -0
- package/src/calls/__tests__/voice-session-bridge.test.ts +116 -4
- package/src/calls/__tests__/voice-triage-escalate.test.ts +77 -11
- package/src/calls/call-controller.ts +3 -1
- package/src/calls/voice-control-protocol.ts +12 -4
- package/src/calls/voice-escalation-judge.ts +2 -2
- package/src/calls/voice-leg-coordinator.ts +23 -5
- package/src/calls/voice-screen-annotation.ts +44 -0
- package/src/calls/voice-session-bridge.ts +63 -20
- package/src/calls/voice-triage-escalate.ts +82 -27
- package/src/cli/commands/__tests__/conversations-clear.test.ts +156 -0
- package/src/cli/commands/conversations.help.ts +7 -2
- package/src/cli/commands/conversations.ts +36 -30
- package/src/cli/commands/oauth/index.help.ts +8 -2
- package/src/cli/commands/oauth/request.test.ts +32 -0
- package/src/cli/commands/platform/index.help.ts +2 -1
- package/src/cli/commands/platform/index.ts +8 -2
- package/src/cli/lib/__tests__/plugin-catalog-cache.test.ts +60 -0
- package/src/cli/lib/__tests__/plugin-catalog-platform.test.ts +66 -2
- package/src/cli/lib/bundled-marketplace.json +35 -0
- package/src/cli/lib/bundled-plugin-packages.json +21 -0
- package/src/cli/lib/plugin-catalog-cache.ts +17 -2
- package/src/cli/lib/plugin-catalog-platform.ts +48 -9
- package/src/config/__tests__/default-profile-catalog.test.ts +28 -0
- package/src/config/__tests__/platform-identity.test.ts +59 -0
- package/src/config/__tests__/profile-order.test.ts +19 -0
- package/src/config/__tests__/sync-gated-profiles.test.ts +71 -0
- package/src/config/bundled-skills/computer-use/SKILL.md +20 -0
- package/src/config/bundled-skills/computer-use/TOOLS.json +11 -11
- package/src/config/bundled-skills/screen-annotation/SKILL.md +41 -29
- package/src/config/bundled-skills/screen-annotation/TOOLS.json +5 -5
- package/src/config/call-site-defaults.ts +6 -0
- package/src/config/default-profile-catalog.ts +30 -1
- package/src/config/default-profile-names.ts +36 -2
- package/src/config/feature-flag-registry.json +9 -1
- package/src/config/platform-identity.ts +22 -5
- package/src/config/profile-order.ts +7 -1
- package/src/config/schemas/call-site-catalog.ts +7 -0
- package/src/config/schemas/llm.ts +21 -5
- package/src/config/sync-gated-profiles.ts +96 -44
- package/src/daemon/__tests__/abort-null-controller.test.ts +135 -1
- package/src/daemon/__tests__/auto-profile-router.test.ts +380 -0
- package/src/daemon/__tests__/turn-tail-assistant-reply-notify.test.ts +16 -0
- package/src/daemon/activity-trail.test.ts +206 -0
- package/src/daemon/activity-trail.ts +190 -0
- package/src/daemon/auto-profile-router.ts +431 -0
- package/src/daemon/computer-use-observation.test.ts +81 -0
- package/src/daemon/computer-use-observation.ts +23 -0
- package/src/daemon/conversation-agent-loop-handlers.ts +11 -0
- package/src/daemon/conversation-agent-loop.ts +157 -32
- package/src/daemon/conversation-error.ts +19 -0
- package/src/daemon/conversation-lifecycle.ts +70 -7
- package/src/daemon/conversation-media-retry.ts +1 -2
- package/src/daemon/conversation-messaging.ts +31 -2
- package/src/daemon/conversation-process.ts +107 -16
- package/src/daemon/conversation-queue-manager.ts +7 -0
- package/src/daemon/conversation-runtime-assembly.ts +1 -0
- package/src/daemon/conversation-turn-finalize.ts +14 -0
- package/src/daemon/conversation.ts +25 -6
- package/src/daemon/event-loop-watchdog.test.ts +201 -2
- package/src/daemon/event-loop-watchdog.ts +182 -6
- package/src/daemon/host-cu-proxy.ts +34 -3
- package/src/daemon/pkb-reminder-builder.test.ts +10 -3
- package/src/daemon/pkb-reminder-builder.ts +1 -1
- package/src/daemon/wake-conversation-ops.ts +4 -2
- package/src/desktop/desktop-computer-use.test.ts +14 -4
- package/src/home/feed-types.ts +2 -0
- package/src/inbound/platform-callback-registration.ts +19 -6
- package/src/live-voice/__tests__/live-voice-agent-turn.test.ts +27 -0
- package/src/live-voice/__tests__/live-voice-flux-turn-end.test.ts +83 -0
- package/src/live-voice/__tests__/live-voice-triage-escalate.test.ts +48 -1
- package/src/live-voice/__tests__/live-voice-vad.test.ts +34 -2
- package/src/live-voice/__tests__/protocol.test.ts +22 -0
- package/src/live-voice/continuation-judge.ts +1 -1
- package/src/live-voice/live-voice-session.ts +25 -19
- package/src/live-voice/protocol.ts +16 -5
- package/src/messaging/providers/discord/withdraw.ts +4 -5
- package/src/messaging/providers/gmail/client.ts +4 -0
- package/src/messaging/providers/slack/withdraw.ts +5 -5
- package/src/messaging/providers/telegram-bot/withdraw.ts +4 -5
- package/src/monitoring/__tests__/proc-fixtures.ts +28 -0
- package/src/monitoring/__tests__/proc-wait-state.test.ts +232 -0
- package/src/monitoring/__tests__/process-usage.test.ts +243 -0
- package/src/monitoring/proc-wait-state.ts +366 -0
- package/src/monitoring/process-memory.ts +4 -3
- package/src/monitoring/process-usage.ts +215 -0
- package/src/monitoring/resource-sample-types.ts +8 -0
- package/src/monitoring/resource-sampler.ts +17 -2
- package/src/monitoring/stall-capture.ts +12 -0
- package/src/notifications/README.md +88 -9
- package/src/notifications/__tests__/assistant-reply-producer.test.ts +390 -119
- package/src/notifications/__tests__/background-result-producer.test.ts +1272 -0
- package/src/notifications/__tests__/broadcaster.test.ts +292 -1
- package/src/notifications/__tests__/completion-policy.test.ts +243 -0
- package/src/notifications/__tests__/decision-engine.test.ts +51 -4
- package/src/notifications/__tests__/has-pending-background-work.test.ts +281 -0
- package/src/notifications/__tests__/home-feed-side-effect.test.ts +161 -3
- package/src/notifications/__tests__/resolve-visible-in-source.test.ts +110 -1
- package/src/notifications/__tests__/schedule-result-producer.test.ts +11 -4
- package/src/notifications/adapters/macos.ts +24 -12
- package/src/notifications/adapters/platform.ts +16 -5
- package/src/notifications/assistant-reply-producer.ts +96 -71
- package/src/notifications/background-result-producer.ts +369 -0
- package/src/notifications/broadcaster.ts +28 -8
- package/src/notifications/completion-policy.ts +115 -0
- package/src/notifications/completion-work.ts +46 -0
- package/src/notifications/conversation-pairing.ts +32 -13
- package/src/notifications/decision-engine.ts +18 -24
- package/src/notifications/emit-signal.ts +16 -1
- package/src/notifications/events-store.ts +4 -0
- package/src/notifications/has-pending-background-work.ts +91 -0
- package/src/notifications/home-feed-mirror.ts +44 -0
- package/src/notifications/home-feed-side-effect.ts +54 -28
- package/src/notifications/resolve-visible-in-source.ts +56 -2
- package/src/notifications/result-output.ts +154 -0
- package/src/notifications/schedule-result-producer.ts +6 -187
- package/src/notifications/types.ts +2 -0
- package/src/oauth/AGENTS.md +9 -1
- package/src/oauth/byo-connection.test.ts +28 -0
- package/src/persistence/__tests__/recent-conversation-messages.test.ts +41 -0
- package/src/persistence/conversation-crud.ts +26 -0
- package/src/persistence/conversation-types.ts +15 -37
- package/src/persistence/embeddings/__tests__/embedding-local-lifecycle.test.ts +85 -14
- package/src/persistence/jobs-store.ts +229 -0
- package/src/persistence/migrations/383-normalize-opencode-host-connections.ts +44 -0
- package/src/persistence/migrations/__tests__/383-normalize-opencode-host-connections.test.ts +102 -0
- package/src/persistence/steps.ts +12 -0
- package/src/platform/sync-avatar.test.ts +29 -0
- package/src/plugins/defaults/memory/AGENTS.md +5 -3
- package/src/plugins/defaults/memory/__tests__/skill-update-receipt-boundary-guard.test.ts +45 -0
- package/src/plugins/defaults/memory/__tests__/skill-update-receipt-job.test.ts +580 -0
- package/src/plugins/defaults/memory/__tests__/turn-retrieval.test.ts +21 -0
- package/src/plugins/defaults/memory/graph/tools.ts +9 -5
- package/src/plugins/defaults/memory/hooks/conversation-deleted.ts +4 -0
- package/src/plugins/defaults/memory/hooks/user-prompt-submit.ts +7 -3
- package/src/plugins/defaults/memory/job-handlers.ts +8 -2
- package/src/plugins/defaults/memory/skill-update-receipt-job.ts +334 -0
- package/src/plugins/defaults/memory/tools.test.ts +17 -0
- package/src/plugins/defaults/memory/turn-retrieval.ts +8 -0
- package/src/plugins/defaults/memory/v3/__tests__/injection.test.ts +24 -0
- package/src/plugins/defaults/memory/v3/injector.ts +4 -4
- package/src/plugins/types.ts +2 -0
- package/src/providers/__tests__/search-provider-catalog.test.ts +8 -0
- package/src/providers/anthropic/client.ts +4 -0
- package/src/providers/gemini/client.ts +7 -1
- package/src/providers/inference/auth.ts +25 -0
- package/src/providers/inference/profile-probe.ts +1 -0
- package/src/{calls/__tests__/typesafe-noul.test.ts → providers/jev/ask.test.ts} +4 -4
- package/src/{calls/typesafe-noul.ts → providers/jev/ask.ts} +67 -34
- package/src/providers/openai/__tests__/api-error-normalization.test.ts +12 -0
- package/src/providers/openai/api-error-normalization.ts +4 -0
- package/src/providers/retry.ts +2 -0
- package/src/providers/search-provider-catalog.ts +10 -0
- package/src/providers/types.ts +7 -0
- package/src/providers/usage-tracking.ts +5 -3
- package/src/runtime/__tests__/agent-wake.test.ts +308 -0
- package/src/runtime/access-request-helper.ts +16 -9
- package/src/runtime/agent-wake-queue.ts +52 -0
- package/src/runtime/agent-wake.ts +111 -18
- package/src/runtime/channel-approval-types.ts +6 -38
- package/src/runtime/channel-approvals.ts +4 -5
- package/src/runtime/http-server.ts +4 -1
- package/src/runtime/introduction-policy.test.ts +17 -1
- package/src/runtime/introduction-policy.ts +25 -7
- package/src/runtime/migrations/vbundle-builder.ts +9 -0
- package/src/runtime/routes/__tests__/inference-profiles-routes.test.ts +21 -1
- package/src/runtime/routes/__tests__/inference-provider-connection-routes.test.ts +60 -0
- package/src/runtime/routes/__tests__/oauth-proxy-routes.test.ts +61 -3
- package/src/runtime/routes/auto-profile-routes.ts +83 -0
- package/src/runtime/routes/conversation-management-routes.ts +1 -3
- package/src/runtime/routes/conversation-routes.ts +9 -0
- package/src/runtime/routes/guardian-action-routes.ts +8 -23
- package/src/runtime/routes/host-cu-routes.ts +3 -1
- package/src/runtime/routes/index.ts +2 -0
- package/src/runtime/routes/inference-profiles-routes.ts +5 -2
- package/src/runtime/routes/inference-provider-connection-routes.ts +25 -8
- package/src/runtime/routes/migration-routes.ts +8 -2
- package/src/runtime/routes/oauth-proxy-passthrough.test.ts +32 -0
- package/src/runtime/routes/platform-routes.ts +6 -1
- package/src/runtime/routes/user-route-dispatcher.ts +1 -2
- package/src/runtime/web-presence.ts +3 -4
- package/src/schedule/schedule-store.ts +10 -0
- package/src/schedule/scheduler.ts +92 -24
- package/src/subagent/manager.ts +32 -12
- package/src/telegram/__tests__/webhook-health.test.ts +8 -0
- package/src/telemetry/telemetry-wire-source.json +1 -1
- package/src/telemetry/telemetry-wire.generated.ts +1 -0
- package/src/tools/background-tool-registry.ts +30 -3
- package/src/tools/browser/cdp-client/local-cdp-client.ts +1 -2
- package/src/tools/host-terminal/host-shell.ts +7 -0
- package/src/tools/network/__tests__/web-fetch-firecrawl.test.ts +28 -1
- package/src/tools/network/__tests__/web-fetch-metadata.test.ts +34 -0
- package/src/tools/network/__tests__/web-fetch-tinyfish.test.ts +35 -1
- package/src/tools/network/__tests__/web-search.test.ts +96 -0
- package/src/tools/network/web-fetch.ts +30 -9
- package/src/tools/network/web-search.ts +235 -4
- package/src/tools/skills/load.ts +13 -17
- package/src/tools/skills/scaffold-managed.ts +86 -71
- package/src/tools/terminal/shell-launch.test.ts +27 -1
- package/src/tools/terminal/shell.ts +4 -0
- package/src/tools/workspace-tools/loader.ts +1 -2
- package/src/usage/attribution.ts +18 -1
- package/src/util/abort-reasons.ts +2 -0
- package/src/util/errors.ts +1 -0
- package/src/util/oauth-request-body.test.ts +5 -1
- package/src/util/provider-error-patterns.ts +11 -0
- package/src/util/single-flight.test.ts +6 -0
- package/src/util/single-flight.ts +2 -0
- package/src/util/worker-pipe.test.ts +56 -10
- package/src/util/worker-pipe.ts +9 -4
- package/src/workspace/git-service.ts +6 -1
- package/src/workspace/migrations/158-repoint-opencode-host-provider-fragments.ts +163 -0
- package/src/workspace/migrations/__tests__/158-repoint-opencode-host-provider-fragments.test.ts +188 -0
- package/src/workspace/migrations/registry.ts +2 -0
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { isToolResultOnlyUserMessage } from "../conversations/message-consolidation.js";
|
|
2
|
+
import { getAttentionStateByConversationIds } from "../persistence/conversation-attention-store.js";
|
|
3
|
+
import {
|
|
4
|
+
getMessageById,
|
|
5
|
+
getMessagesAfter,
|
|
6
|
+
getRecentConversationMessages,
|
|
7
|
+
isStandaloneAssistantMessage,
|
|
8
|
+
type MessageRow,
|
|
9
|
+
} from "../persistence/conversation-crud.js";
|
|
10
|
+
import { stringifyMessageContent } from "../persistence/message-content.js";
|
|
11
|
+
import {
|
|
12
|
+
isPrivateAssistantText,
|
|
13
|
+
projectPersistedAssistantContent,
|
|
14
|
+
} from "../persistence/user-facing-content.js";
|
|
15
|
+
import {
|
|
16
|
+
decodeLiteralLineBreaks,
|
|
17
|
+
stripMarkdownForPreview,
|
|
18
|
+
truncate,
|
|
19
|
+
} from "./notification-utils.js";
|
|
20
|
+
|
|
21
|
+
const MAX_RESULT_BODY_CHARS = 2000;
|
|
22
|
+
const RUN_HISTORY_PAGE_SIZE = 200;
|
|
23
|
+
|
|
24
|
+
/** A messaging delivery counts only after its matching tool result succeeds.
|
|
25
|
+
* Missing or failed results leave the fallback available. */
|
|
26
|
+
export function deliveredThroughMessagingTool(
|
|
27
|
+
conversationId: string,
|
|
28
|
+
runRows: readonly MessageRow[],
|
|
29
|
+
): boolean {
|
|
30
|
+
const callIds = new Set<string>();
|
|
31
|
+
for (const row of runRows) {
|
|
32
|
+
for (const block of row.content) {
|
|
33
|
+
if (block.type === "tool_use" && block.name === "messaging_send") {
|
|
34
|
+
callIds.add(block.id);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
if (callIds.size === 0) {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
const [firstRunRow] = runRows;
|
|
42
|
+
for (const row of getMessagesAfter(conversationId, {
|
|
43
|
+
id: firstRunRow.id,
|
|
44
|
+
createdAt: firstRunRow.createdAt,
|
|
45
|
+
})) {
|
|
46
|
+
if (row.role !== "user") {
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
for (const block of row.content) {
|
|
50
|
+
// guard:allow-tool-result-only: the local executor's verdict on a
|
|
51
|
+
// delivery it ran. A server-side `web_search_tool_result` carries no
|
|
52
|
+
// `is_error` and never delivers anything.
|
|
53
|
+
if (
|
|
54
|
+
block.type === "tool_result" &&
|
|
55
|
+
block.is_error !== true &&
|
|
56
|
+
callIds.has(block.tool_use_id)
|
|
57
|
+
) {
|
|
58
|
+
return true;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Read the assistant rows of one turn, bounded by the run start. */
|
|
66
|
+
export function collectRunRows(
|
|
67
|
+
latestRow: MessageRow,
|
|
68
|
+
conversationId: string,
|
|
69
|
+
runStartedAt: number,
|
|
70
|
+
): MessageRow[] {
|
|
71
|
+
if (latestRow.createdAt < runStartedAt) {
|
|
72
|
+
return [];
|
|
73
|
+
}
|
|
74
|
+
const rows: MessageRow[] = [latestRow];
|
|
75
|
+
if (isStandaloneAssistantMessage(latestRow.role, latestRow.metadata)) {
|
|
76
|
+
return rows;
|
|
77
|
+
}
|
|
78
|
+
let beforeMessageId = latestRow.id;
|
|
79
|
+
while (true) {
|
|
80
|
+
const history = getRecentConversationMessages(
|
|
81
|
+
conversationId,
|
|
82
|
+
RUN_HISTORY_PAGE_SIZE,
|
|
83
|
+
beforeMessageId,
|
|
84
|
+
);
|
|
85
|
+
for (let index = history.length - 1; index >= 0; index--) {
|
|
86
|
+
const row = history[index];
|
|
87
|
+
if (
|
|
88
|
+
isStandaloneAssistantMessage(row.role, row.metadata) ||
|
|
89
|
+
(row.role === "user" && !isToolResultOnlyUserMessage(row))
|
|
90
|
+
) {
|
|
91
|
+
return rows.reverse();
|
|
92
|
+
}
|
|
93
|
+
if (row.role === "assistant" && row.createdAt >= runStartedAt) {
|
|
94
|
+
rows.push(row);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
if (history.length < RUN_HISTORY_PAGE_SIZE) {
|
|
98
|
+
return rows.reverse();
|
|
99
|
+
}
|
|
100
|
+
beforeMessageId = history[0].id;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** Read the persisted user-facing result, walking back through private wrap-up
|
|
105
|
+
* rows. Keep markdown for the feed detail panel; flatten only to check emptiness. */
|
|
106
|
+
export function resolveRunResult(
|
|
107
|
+
runRows: readonly MessageRow[],
|
|
108
|
+
): { body: string; row: MessageRow } | undefined {
|
|
109
|
+
for (let i = runRows.length - 1; i >= 0; i--) {
|
|
110
|
+
const row = runRows[i];
|
|
111
|
+
const text = stringifyMessageContent(
|
|
112
|
+
projectPersistedAssistantContent(row.content, row.metadata),
|
|
113
|
+
);
|
|
114
|
+
const flattened = stripMarkdownForPreview(text).replace(/\s+/g, " ").trim();
|
|
115
|
+
if (flattened) {
|
|
116
|
+
return {
|
|
117
|
+
body: truncate(
|
|
118
|
+
decodeLiteralLineBreaks(text.trim()),
|
|
119
|
+
MAX_RESULT_BODY_CHARS,
|
|
120
|
+
),
|
|
121
|
+
row,
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
if (!isPrivateAssistantText(row.metadata)) {
|
|
125
|
+
return undefined;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return undefined;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export function resolveRunOutput(
|
|
132
|
+
runRows: readonly MessageRow[],
|
|
133
|
+
): string | undefined {
|
|
134
|
+
return resolveRunResult(runRows)?.body;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** Read the final assistant row only when it belongs to this run. */
|
|
138
|
+
export function resolveLatestRunRow(
|
|
139
|
+
conversationId: string,
|
|
140
|
+
runStartedAt: number,
|
|
141
|
+
): MessageRow | undefined {
|
|
142
|
+
const attention = getAttentionStateByConversationIds([conversationId]).get(
|
|
143
|
+
conversationId,
|
|
144
|
+
);
|
|
145
|
+
const assistantMessageId = attention?.latestAssistantMessageId;
|
|
146
|
+
if (!assistantMessageId) {
|
|
147
|
+
return undefined;
|
|
148
|
+
}
|
|
149
|
+
const row = getMessageById(assistantMessageId, conversationId);
|
|
150
|
+
if (!row || row.createdAt < runStartedAt) {
|
|
151
|
+
return undefined;
|
|
152
|
+
}
|
|
153
|
+
return row;
|
|
154
|
+
}
|
|
@@ -28,38 +28,19 @@
|
|
|
28
28
|
import type pino from "pino";
|
|
29
29
|
|
|
30
30
|
import { isAssistantFeatureFlagEnabled } from "../config/assistant-feature-flags.js";
|
|
31
|
-
import { getAttentionStateByConversationIds } from "../persistence/conversation-attention-store.js";
|
|
32
|
-
import {
|
|
33
|
-
getAssistantMessageIdsInTurn,
|
|
34
|
-
getMessageById,
|
|
35
|
-
getMessagesAfter,
|
|
36
|
-
type MessageRow,
|
|
37
|
-
} from "../persistence/conversation-crud.js";
|
|
38
|
-
import { stringifyMessageContent } from "../persistence/message-content.js";
|
|
39
|
-
import {
|
|
40
|
-
isPrivateAssistantText,
|
|
41
|
-
projectPersistedAssistantContent,
|
|
42
|
-
} from "../persistence/user-facing-content.js";
|
|
43
31
|
import { emitNotificationSignal } from "./emit-signal.js";
|
|
44
32
|
import { hasNotifiedSourceContextSince } from "./events-store.js";
|
|
33
|
+
import { sanitizeNotificationTitle } from "./notification-utils.js";
|
|
45
34
|
import {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
} from "./
|
|
35
|
+
collectRunRows,
|
|
36
|
+
deliveredThroughMessagingTool,
|
|
37
|
+
resolveLatestRunRow,
|
|
38
|
+
resolveRunOutput,
|
|
39
|
+
} from "./result-output.js";
|
|
51
40
|
|
|
52
41
|
/** Kill switch for this producer, on by default. */
|
|
53
42
|
const SCHEDULE_RESULT_NOTIFY_FLAG = "schedule-result-notify" as const;
|
|
54
43
|
|
|
55
|
-
/**
|
|
56
|
-
* Body cap. Far above `MESSAGE_PREVIEW_MAX_LENGTH` (200) on purpose: this body
|
|
57
|
-
* is the deliverable, not a preview of one, and the home feed's detail panel
|
|
58
|
-
* renders it as markdown. A lock-screen banner truncates on its own, so the
|
|
59
|
-
* cap exists only to keep a runaway reply out of the payload.
|
|
60
|
-
*/
|
|
61
|
-
const MAX_RESULT_BODY_CHARS = 2000;
|
|
62
|
-
|
|
63
44
|
export interface ScheduleResultNotificationParams {
|
|
64
45
|
/** `cron_jobs.id` of the schedule that fired. */
|
|
65
46
|
scheduleId: string;
|
|
@@ -78,168 +59,6 @@ export interface ScheduleResultNotificationParams {
|
|
|
78
59
|
rlog: pino.Logger;
|
|
79
60
|
}
|
|
80
61
|
|
|
81
|
-
/**
|
|
82
|
-
* Whether the run delivered its result through the messaging tool, and the
|
|
83
|
-
* call succeeded.
|
|
84
|
-
*
|
|
85
|
-
* The messaging tool is the route the schedule skill prescribes for rich
|
|
86
|
-
* content, and it writes no `notification_events` row, so the pipeline probe
|
|
87
|
-
* cannot see it. Without this check a well-authored Slack digest would post
|
|
88
|
-
* its summary and then get a second notification whose body is "Posted the
|
|
89
|
-
* digest to #general."
|
|
90
|
-
*
|
|
91
|
-
* Both conditions are load-bearing. The route has to be recognized, because
|
|
92
|
-
* this is a known-routes list rather than a general "did the run do anything?"
|
|
93
|
-
* heuristic. And the call has to have succeeded: a `tool_use` block records
|
|
94
|
-
* only that the model called the tool, so a send the channel refused leaves
|
|
95
|
-
* the same block as one that landed, and suppressing on it costs the user both
|
|
96
|
-
* the digest and the run's explanation of the failure. The executor's verdict
|
|
97
|
-
* is on the `tool_result` the call produced, so success is read from there,
|
|
98
|
-
* and a call with no result counts as undelivered. Either condition unmet gets
|
|
99
|
-
* the fallback, which is the safe failure.
|
|
100
|
-
*
|
|
101
|
-
* The result read starts at the run's first row, so it covers the results
|
|
102
|
-
* interleaved through the turn. Rows from a later turn in a reused
|
|
103
|
-
* conversation can come back too and are harmless, because only this run's
|
|
104
|
-
* own call ids are matched.
|
|
105
|
-
*
|
|
106
|
-
* A Slack Web API post through bash is deliberately not a route. It is off
|
|
107
|
-
* the guided path and unrecorded, and recognizing it would mean reading a
|
|
108
|
-
* shell command for a method name. A run that posts that way gets the
|
|
109
|
-
* fallback too: at worst a duplicate, never a silence.
|
|
110
|
-
*/
|
|
111
|
-
function deliveredThroughMessagingTool(
|
|
112
|
-
conversationId: string,
|
|
113
|
-
runRows: readonly MessageRow[],
|
|
114
|
-
): boolean {
|
|
115
|
-
const callIds = new Set<string>();
|
|
116
|
-
for (const row of runRows) {
|
|
117
|
-
for (const block of row.content) {
|
|
118
|
-
if (block.type === "tool_use" && block.name === "messaging_send") {
|
|
119
|
-
callIds.add(block.id);
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
if (callIds.size === 0) {
|
|
124
|
-
return false;
|
|
125
|
-
}
|
|
126
|
-
const [firstRunRow] = runRows;
|
|
127
|
-
for (const row of getMessagesAfter(conversationId, {
|
|
128
|
-
id: firstRunRow.id,
|
|
129
|
-
createdAt: firstRunRow.createdAt,
|
|
130
|
-
})) {
|
|
131
|
-
if (row.role !== "user") {
|
|
132
|
-
continue;
|
|
133
|
-
}
|
|
134
|
-
for (const block of row.content) {
|
|
135
|
-
// guard:allow-tool-result-only: the local executor's verdict on a
|
|
136
|
-
// delivery it ran. A server-side `web_search_tool_result` carries no
|
|
137
|
-
// `is_error` and never delivers anything.
|
|
138
|
-
if (
|
|
139
|
-
block.type === "tool_result" &&
|
|
140
|
-
block.is_error !== true &&
|
|
141
|
-
callIds.has(block.tool_use_id)
|
|
142
|
-
) {
|
|
143
|
-
return true;
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
return false;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
/**
|
|
151
|
-
* The assistant rows this run wrote, in order, ending on `latestRow`.
|
|
152
|
-
*
|
|
153
|
-
* A run is one agent turn: `getAssistantMessageIdsInTurn` walks the tool-call
|
|
154
|
-
* loop back to the user message that opened it. Rows from before the run
|
|
155
|
-
* started are dropped defensively — a reused conversation's earlier turns must
|
|
156
|
-
* never be mistaken for this one.
|
|
157
|
-
*/
|
|
158
|
-
function collectRunRows(
|
|
159
|
-
latestRow: MessageRow,
|
|
160
|
-
conversationId: string,
|
|
161
|
-
runStartedAt: number,
|
|
162
|
-
): MessageRow[] {
|
|
163
|
-
const rows: MessageRow[] = [];
|
|
164
|
-
for (const id of getAssistantMessageIdsInTurn(latestRow.id)) {
|
|
165
|
-
const row =
|
|
166
|
-
id === latestRow.id ? latestRow : getMessageById(id, conversationId);
|
|
167
|
-
if (row && row.createdAt >= runStartedAt) {
|
|
168
|
-
rows.push(row);
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
return rows;
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
/**
|
|
175
|
-
* Whether the run left the user anything worth reading, and what.
|
|
176
|
-
*
|
|
177
|
-
* Substance is judged mechanically: markdown is flattened and whitespace
|
|
178
|
-
* collapsed, and whatever survives is the answer. A run that ended on tool
|
|
179
|
-
* calls with no closing prose flattens to nothing and stays silent — the
|
|
180
|
-
* "nothing to say" case. Anything else counts, including a briefing whose
|
|
181
|
-
* honest finding is that nothing changed; the user asked for that cadence, and
|
|
182
|
-
* "no new mail today" is a result, not noise.
|
|
183
|
-
*
|
|
184
|
-
* Every row is read through the user-facing projection, so a scheduled run
|
|
185
|
-
* that routed its reply through `send_user_message` quotes the message it
|
|
186
|
-
* delivered rather than the private scratchpad behind it. A scheduled run
|
|
187
|
-
* resolves the `mainAgent` call site, so it is gated like any app turn.
|
|
188
|
-
*
|
|
189
|
-
* The scan walks back only across rows marked private, because that is the
|
|
190
|
-
* shape a gated run leaves: it ends on wrap-up notes that project to nothing,
|
|
191
|
-
* with the delivered message on the earlier row carrying the call. An ordinary
|
|
192
|
-
* run is read exactly as before: its last row, and silence if that row has no
|
|
193
|
-
* prose.
|
|
194
|
-
*
|
|
195
|
-
* The returned body keeps its original markdown — only the emptiness test runs
|
|
196
|
-
* on the flattened form, because the detail panel renders the real thing.
|
|
197
|
-
*/
|
|
198
|
-
function resolveRunOutput(runRows: readonly MessageRow[]): string | undefined {
|
|
199
|
-
for (let i = runRows.length - 1; i >= 0; i--) {
|
|
200
|
-
const row = runRows[i];
|
|
201
|
-
const text = stringifyMessageContent(
|
|
202
|
-
projectPersistedAssistantContent(row.content, row.metadata),
|
|
203
|
-
);
|
|
204
|
-
const flattened = stripMarkdownForPreview(text).replace(/\s+/g, " ").trim();
|
|
205
|
-
if (flattened) {
|
|
206
|
-
return truncate(
|
|
207
|
-
decodeLiteralLineBreaks(text.trim()),
|
|
208
|
-
MAX_RESULT_BODY_CHARS,
|
|
209
|
-
);
|
|
210
|
-
}
|
|
211
|
-
if (!isPrivateAssistantText(row.metadata)) {
|
|
212
|
-
return undefined;
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
return undefined;
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
/**
|
|
219
|
-
* The run's final assistant row, or nothing if the run wrote none.
|
|
220
|
-
*
|
|
221
|
-
* `latestAssistantMessageId` is per conversation, not per run, so a reused
|
|
222
|
-
* conversation whose current run wrote no reply would otherwise hand back the
|
|
223
|
-
* previous run's — and the fallback would re-send yesterday's briefing.
|
|
224
|
-
*/
|
|
225
|
-
function resolveLatestRunRow(
|
|
226
|
-
conversationId: string,
|
|
227
|
-
runStartedAt: number,
|
|
228
|
-
): MessageRow | undefined {
|
|
229
|
-
const attention = getAttentionStateByConversationIds([conversationId]).get(
|
|
230
|
-
conversationId,
|
|
231
|
-
);
|
|
232
|
-
const assistantMessageId = attention?.latestAssistantMessageId;
|
|
233
|
-
if (!assistantMessageId) {
|
|
234
|
-
return undefined;
|
|
235
|
-
}
|
|
236
|
-
const row = getMessageById(assistantMessageId, conversationId);
|
|
237
|
-
if (!row || row.createdAt < runStartedAt) {
|
|
238
|
-
return undefined;
|
|
239
|
-
}
|
|
240
|
-
return row;
|
|
241
|
-
}
|
|
242
|
-
|
|
243
62
|
/**
|
|
244
63
|
* Emit the fallback notification for a schedule run that finished without
|
|
245
64
|
* notifying. No-ops when the flag is off, when the run already notified, or
|
|
@@ -114,6 +114,8 @@ export const ChannelDeliveryPayloadSchema = z.object({
|
|
|
114
114
|
deepLinkTarget: z.record(z.string(), z.unknown()).optional(),
|
|
115
115
|
contextPayload: z.record(z.string(), z.unknown()).optional(),
|
|
116
116
|
urgency: UrgencySchema,
|
|
117
|
+
/** Local presentation resolved by the broadcaster, independent of urgency. */
|
|
118
|
+
silent: z.boolean().optional(),
|
|
117
119
|
approvalContext: ApprovalUIMetadataSchema.optional(),
|
|
118
120
|
accessRequestContext: AccessRequestPayloadSchema.optional(),
|
|
119
121
|
/** Source reference for a tool-approval card, projected once by the
|
package/src/oauth/AGENTS.md
CHANGED
|
@@ -14,6 +14,12 @@ Add an entry to `PROVIDER_SEED_DATA`. Required fields: `provider`, `authorizeUrl
|
|
|
14
14
|
|
|
15
15
|
If the provider will support managed mode, set `managedServiceConfigKey` to a slug matching the key you will add to `ServicesSchema` (e.g. `"acme-oauth"`).
|
|
16
16
|
|
|
17
|
+
#### Scopes: your-own vs. managed
|
|
18
|
+
|
|
19
|
+
`defaultScopes` here is requested only by the user's own OAuth client. A managed connection authorizes against Vellum's OAuth app, whose defaults live in the platform's `provider_registry.json`. Google shows users warnings or errors for scopes not approved for the app, so the managed start endpoint rejects anything outside the platform registry's `extra_config.allowed_scopes`. Managed Google, for example, grants Gmail only as `https://mail.google.com/`, never `gmail.readonly` / `gmail.modify` / `gmail.send`.
|
|
20
|
+
|
|
21
|
+
So anything that names scopes for a managed connection (a hardcoded constant, a client hook, `requestedScopes` on an `oauth_connect` surface, a `--scopes` example in a skill) must use scopes from that approved list, not the `defaultScopes` names. A required-scope check that runs against managed grants must accept the approved equivalent: `grantedScopeCoversRequiredScope` in `scope-utils.ts` treats `mail.google.com` as covering `gmail.readonly`. Adding a scope to a managed flow means getting it approved in the provider console and adding it to `allowed_scopes` first.
|
|
22
|
+
|
|
17
23
|
### 2. _(managed only)_ Add a service schema — `../config/schemas/services.ts`
|
|
18
24
|
|
|
19
25
|
Create a schema and export its type:
|
|
@@ -77,6 +83,8 @@ Only the HTTP adapter supplies the wire-exact URL the route forwards, so an IPC
|
|
|
77
83
|
|
|
78
84
|
Wire semantics live in `../runtime/routes/oauth-proxy-passthrough.ts`. The remainder path reaches the provider as the caller wrote it: `.` and `..` collapse, an absolute URL, `//host`, or an empty segment is a 400, and no segment is decoded or re-encoded, so a `%2F` survives. No `baseUrl` is passed to the connection, so its own API base is the only host ever targeted.
|
|
79
85
|
|
|
86
|
+
The minted base is that path prefix, not a host root. Stock clients resolve a leading `/` against the origin (`/upload/gmail/...`, `/v1/charges`), which would otherwise miss the daemon catch-all. The gateway's runtime-proxy remaps those host-absolute paths onto `/v1/oauth/proxy/<segment>` when the bearer is an `oauth_proxy_v1` grant (`gateway/src/http/routes/oauth-proxy-path.ts`). An already-prefixed path, including one naming a different provider, is left alone so the daemon's subject check still applies. Traversal and `//host` stay in the remainder for `normalizeProxyPath` to reject.
|
|
87
|
+
|
|
80
88
|
### Provider segment
|
|
81
89
|
|
|
82
90
|
The first segment is `provider` or `provider@account`, the account pinning one connection when a provider has several. Proxy provider keys contain only ASCII letters, digits, `.`, `_`, `~`, and `-`, and cannot be `.` or `..`. Encoding and parsing share this validation so URL delimiters, percent escapes, and normalization cannot change the provider or grant subject. Accounts are percent-encoded for the same reason. When several accounts are connected and none is pinned, both answer 409, but they say different things. The mint names the accounts, because the caller is the local operator choosing one. Unlabeled BYO connections can be selected with their exact connection ID; account-label matches take precedence, and provider, client, and active-status filters apply to either selector. The proxy names none, because its caller is the third-party binary holding the grant; it says to mint a grant pinned with `--account`, which is the only thing that resolves it. Rewriting the segment would not, since the subject check runs before resolution.
|
|
@@ -98,7 +106,7 @@ A grant is pinned when the caller passed `--account` or the resolved connection
|
|
|
98
106
|
|
|
99
107
|
The grant carries one scope for one route, and four checks hold it there:
|
|
100
108
|
|
|
101
|
-
- **Gateway edge auth** (`gateway/src/http/middleware/auth.ts`) refuses it on every gateway-native route, with no loopback fallback. `isSingleRouteGrant` calls `isNarrowScopeProfile` from `gateway/src/auth/scopes.ts`, and also catches a proxy subject carrying some other profile. The passthrough itself never passes through edge auth: it falls to the runtime-proxy catch-all, which re-mints the grant's own subject and profile for the daemon. That holds whether or not the gateway requires client-facing auth (`gateway.runtimeProxyRequireAuth
|
|
109
|
+
- **Gateway edge auth** (`gateway/src/http/middleware/auth.ts`) refuses it on every gateway-native route, with no loopback fallback. `isSingleRouteGrant` calls `isNarrowScopeProfile` from `gateway/src/auth/scopes.ts`, and also catches a proxy subject carrying some other profile. The passthrough itself never passes through edge auth: it falls to the runtime-proxy catch-all, which re-mints the grant's own subject and profile for the daemon. That holds whether or not the gateway requires client-facing auth (`gateway.runtimeProxyRequireAuth`): a presented grant is validated the same way in both modes and only a caller presenting no valid grant is answered with the gateway's own service token, so the minted URL works in either and neither mode hands the grant more than `oauth.proxy`.
|
|
102
110
|
- **The gateway's IPC fast path** (`gateway/src/http/routes/ipc-runtime-proxy.ts`) refuses it against any daemon route naming no scope. The daemon's IPC server runs no policy check of its own, so this is the only enforcement an IPC-served request gets.
|
|
103
111
|
- **`enforcePolicy`** (`../runtime/auth/route-policy.ts`) refuses the same on the HTTP path, then applies the route's own scopes. `oauth.proxy` reaches the passthrough and nothing else. It runs wherever `routeRequest` dispatches through the router, which is every `/v1` route and the shareable-pages path bar three groups that earlier branches answer first, each carrying a gate of its own: `GET /v1/audio/:audioId`, deliberately public and served before `authenticateRequest` runs at all because Twilio fetches it directly; the Twilio webhook paths (`TWILIO_WEBHOOK_RE`), also pre-auth and gated on Twilio's request signature; and the WebSocket upgrades in the next bullet, which never reach the route table. A route added anywhere else is covered.
|
|
104
112
|
- **WebSocket upgrades** (`/v1/calls/media-stream`, `/v1/stt/stream`, `/v1/live-voice`, `/v1/watch/stream`) are handled before the route table on both hops, so neither edge auth nor `enforcePolicy` sees them, and each carries its own identity gate. The daemon admits only a private-network peer presenting a gateway service token; at the edge it is an actor principal on `runtime-audio-stream` and `live-voice`, the `speech.relay` scope on the relay, and the relay-token identity on `twilio-media-websocket`.
|
|
@@ -504,6 +504,34 @@ describe("BYOOAuthConnection", () => {
|
|
|
504
504
|
expect(headers.get("Content-Type")).toBe("application/pdf");
|
|
505
505
|
});
|
|
506
506
|
|
|
507
|
+
test("joins a Gmail media upload onto the host-only Google API base", async () => {
|
|
508
|
+
await setupCredential("google");
|
|
509
|
+
const conn = new BYOOAuthConnection({
|
|
510
|
+
id: "conn-google",
|
|
511
|
+
provider: "google",
|
|
512
|
+
baseUrl: "https://www.googleapis.com",
|
|
513
|
+
accountInfo: null,
|
|
514
|
+
});
|
|
515
|
+
const rfc822 =
|
|
516
|
+
"From: user@example.com\r\nTo: user@example.com\r\nSubject: Draft\r\n\r\nHello\r\n";
|
|
517
|
+
|
|
518
|
+
await conn.request({
|
|
519
|
+
method: "POST",
|
|
520
|
+
path: "/upload/gmail/v1/users/me/drafts",
|
|
521
|
+
query: { uploadType: "media" },
|
|
522
|
+
headers: { "Content-Type": "message/rfc822" },
|
|
523
|
+
body: rfc822,
|
|
524
|
+
});
|
|
525
|
+
|
|
526
|
+
expect(mockFetch.mock.calls[0][0]).toBe(
|
|
527
|
+
"https://www.googleapis.com/upload/gmail/v1/users/me/drafts?uploadType=media",
|
|
528
|
+
);
|
|
529
|
+
const [, init] = mockFetch.mock.calls[0];
|
|
530
|
+
expect((init as RequestInit).body).toBe(rfc822);
|
|
531
|
+
const headers = (init as RequestInit).headers as Headers;
|
|
532
|
+
expect(headers.get("Content-Type")).toBe("message/rfc822");
|
|
533
|
+
});
|
|
534
|
+
|
|
507
535
|
test("sends a string body verbatim under the caller's Content-Type", async () => {
|
|
508
536
|
await setupCredential("google");
|
|
509
537
|
const conn = createConnection();
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { expect, test } from "bun:test";
|
|
2
|
+
|
|
3
|
+
import { sql } from "drizzle-orm";
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
addMessage,
|
|
7
|
+
createConversation,
|
|
8
|
+
getRecentConversationMessages,
|
|
9
|
+
} from "../conversation-crud.js";
|
|
10
|
+
import { getDb } from "../db-connection.js";
|
|
11
|
+
import { initializeDb } from "../db-init.js";
|
|
12
|
+
|
|
13
|
+
await initializeDb();
|
|
14
|
+
|
|
15
|
+
test("recent conversation rows preserve insertion order at timestamp ties and stay bounded", async () => {
|
|
16
|
+
const conversation = createConversation({ title: "Example task" });
|
|
17
|
+
const other = createConversation({ title: "Other task" });
|
|
18
|
+
const first = await addMessage(conversation.id, "user", "Start work");
|
|
19
|
+
const success = await addMessage(
|
|
20
|
+
conversation.id,
|
|
21
|
+
"assistant",
|
|
22
|
+
"The result is ready",
|
|
23
|
+
);
|
|
24
|
+
const failure = await addMessage(conversation.id, "user", "A sibling failed");
|
|
25
|
+
await addMessage(other.id, "assistant", "Unrelated result");
|
|
26
|
+
getDb().run(
|
|
27
|
+
sql`UPDATE messages SET created_at = 100 WHERE conversation_id = ${conversation.id}`,
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
expect(
|
|
31
|
+
getRecentConversationMessages(conversation.id, 2).map((row) => row.id),
|
|
32
|
+
).toEqual([success.id, failure.id]);
|
|
33
|
+
expect(
|
|
34
|
+
getRecentConversationMessages(conversation.id, 3).map((row) => row.id),
|
|
35
|
+
).toEqual([first.id, success.id, failure.id]);
|
|
36
|
+
expect(
|
|
37
|
+
getRecentConversationMessages(conversation.id, 2, failure.id).map(
|
|
38
|
+
(row) => row.id,
|
|
39
|
+
),
|
|
40
|
+
).toEqual([first.id, success.id]);
|
|
41
|
+
});
|
|
@@ -432,6 +432,8 @@ export const messageMetadataSchema = z
|
|
|
432
432
|
* treat message text as organic user input.
|
|
433
433
|
*/
|
|
434
434
|
hidden: z.boolean().optional(),
|
|
435
|
+
/** A hidden voice continuation trigger whose finished reply can raise a push. */
|
|
436
|
+
voiceContinuationResult: z.boolean().optional(),
|
|
435
437
|
/**
|
|
436
438
|
* Marks a role-`"user"` row that opened a live phone or in-app voice turn.
|
|
437
439
|
* Test with {@link isVoiceSessionUserMessage}, which documents why the
|
|
@@ -3206,6 +3208,30 @@ export function getMessagesAfter(
|
|
|
3206
3208
|
.map(parseMessage);
|
|
3207
3209
|
}
|
|
3208
3210
|
|
|
3211
|
+
/** Read a bounded conversation window in insertion order, including timestamp ties. */
|
|
3212
|
+
export function getRecentConversationMessages(
|
|
3213
|
+
conversationId: string,
|
|
3214
|
+
limit: number,
|
|
3215
|
+
beforeMessageId?: string,
|
|
3216
|
+
): MessageRow[] {
|
|
3217
|
+
return getDb()
|
|
3218
|
+
.select()
|
|
3219
|
+
.from(messages)
|
|
3220
|
+
.where(
|
|
3221
|
+
and(
|
|
3222
|
+
eq(messages.conversationId, conversationId),
|
|
3223
|
+
beforeMessageId === undefined
|
|
3224
|
+
? undefined
|
|
3225
|
+
: sql`rowid < (SELECT rowid FROM messages WHERE id = ${beforeMessageId} AND conversation_id = ${conversationId})`,
|
|
3226
|
+
),
|
|
3227
|
+
)
|
|
3228
|
+
.orderBy(sql`rowid DESC`)
|
|
3229
|
+
.limit(limit)
|
|
3230
|
+
.all()
|
|
3231
|
+
.reverse()
|
|
3232
|
+
.map(parseMessage);
|
|
3233
|
+
}
|
|
3234
|
+
|
|
3209
3235
|
/**
|
|
3210
3236
|
* Efficient existence check — returns true if the conversation has at least
|
|
3211
3237
|
* one message row. Uses `LIMIT 1` + `select({ 1 })` to avoid loading and
|
|
@@ -7,11 +7,7 @@
|
|
|
7
7
|
// `resolveConversationKind` classifier, and the pure predicates over a
|
|
8
8
|
// persisted message's `metadata` record.
|
|
9
9
|
|
|
10
|
-
import {
|
|
11
|
-
type ChannelId,
|
|
12
|
-
parseChannelId,
|
|
13
|
-
parseClientOs,
|
|
14
|
-
} from "../channels/types.js";
|
|
10
|
+
import { type ChannelId, parseChannelId } from "../channels/types.js";
|
|
15
11
|
|
|
16
12
|
/**
|
|
17
13
|
* Where a conversation came from, stated by whoever creates it.
|
|
@@ -167,6 +163,14 @@ export function isSystemCardMetadata(
|
|
|
167
163
|
*/
|
|
168
164
|
export const NO_RESPONSE_MESSAGE_KIND = "no_response";
|
|
169
165
|
|
|
166
|
+
/**
|
|
167
|
+
* Metadata key on an assistant row from a turn that ran on the Auto profile:
|
|
168
|
+
* the default profile key the router picked for the turn. Projected to
|
|
169
|
+
* `ConversationMessage.autoRoutedProfile` so clients can show which profile
|
|
170
|
+
* answered. Absent on every other row.
|
|
171
|
+
*/
|
|
172
|
+
export const AUTO_ROUTED_PROFILE_METADATA_KEY = "autoRoutedProfile";
|
|
173
|
+
|
|
170
174
|
/**
|
|
171
175
|
* Shared predicate for the deliberate-silence marker, mirroring
|
|
172
176
|
* {@link isSystemCardMetadata} so display merging, transcript rendering, and
|
|
@@ -357,36 +361,6 @@ export function messageMetadataIsAmbientSightKeep(
|
|
|
357
361
|
}
|
|
358
362
|
}
|
|
359
363
|
|
|
360
|
-
/**
|
|
361
|
-
* True when the row that opened the turn was sent from a desktop app, on that
|
|
362
|
-
* row's own evidence.
|
|
363
|
-
*
|
|
364
|
-
* Two markers, both required. The `client` bag's `os` entry is the only
|
|
365
|
-
* per-platform attribution on a message: `userMessageInterface` is `"web"` for
|
|
366
|
-
* the desktop apps, the iOS app, and a desktop browser alike.
|
|
367
|
-
* `clientOsFromRequest` says that `os` was reported by this row's request or
|
|
368
|
-
* transport rather than inherited from the conversation's live client state,
|
|
369
|
-
* which names the surface of an earlier turn. A button tapped on the phone
|
|
370
|
-
* against a conversation last sent to from desktop persists the old OS with no
|
|
371
|
-
* marker.
|
|
372
|
-
*
|
|
373
|
-
* Origin a row did not report itself is origin unknown. Callers gate
|
|
374
|
-
* suppression on this, so unknown has to read as not-desktop.
|
|
375
|
-
*/
|
|
376
|
-
export function isDesktopOriginatedUserMessage(
|
|
377
|
-
metadata: Record<string, unknown> | undefined,
|
|
378
|
-
): boolean {
|
|
379
|
-
if (metadata?.clientOsFromRequest !== true) {
|
|
380
|
-
return false;
|
|
381
|
-
}
|
|
382
|
-
const client = metadata.client;
|
|
383
|
-
if (typeof client !== "object" || client === null) {
|
|
384
|
-
return false;
|
|
385
|
-
}
|
|
386
|
-
const clientOs = parseClientOs((client as Record<string, unknown>).os);
|
|
387
|
-
return clientOs === "macos" || clientOs === "windows" || clientOs === "linux";
|
|
388
|
-
}
|
|
389
|
-
|
|
390
364
|
/**
|
|
391
365
|
* True when the row that opened the turn arrived over an external messaging
|
|
392
366
|
* surface (Slack, Telegram, WhatsApp, email, a phone call) rather than the
|
|
@@ -430,7 +404,8 @@ function isReplyDeliveredOffApp(
|
|
|
430
404
|
*
|
|
431
405
|
* - `automated`: a scheduled or background prompt injected into an ordinary
|
|
432
406
|
* user conversation, which has its own producer (e.g. `schedule.notify`).
|
|
433
|
-
* - Echo-suppressed: internal scaffolding,
|
|
407
|
+
* - Echo-suppressed: internal scaffolding, except a hidden voice continuation
|
|
408
|
+
* result whose finished reply reaches the user through this push.
|
|
434
409
|
* - Voice-session: the reply is spoken back over the still-open session.
|
|
435
410
|
* - Channel-originated: the finished reply is delivered back to the
|
|
436
411
|
* originating messaging surface (`finalizeEventDelivery`), so the sender
|
|
@@ -455,7 +430,10 @@ export function isReplyPushIneligibleUserMessage(
|
|
|
455
430
|
return (
|
|
456
431
|
metadata?.automated === true ||
|
|
457
432
|
metadata?.pointerInstruction === true ||
|
|
458
|
-
isEchoSuppressedUserMessage(metadata)
|
|
433
|
+
(isEchoSuppressedUserMessage(metadata) &&
|
|
434
|
+
!(
|
|
435
|
+
metadata?.hidden === true && metadata?.voiceContinuationResult === true
|
|
436
|
+
)) ||
|
|
459
437
|
(!options?.replyDeliveredInAppOnly && isReplyDeliveredOffApp(metadata))
|
|
460
438
|
);
|
|
461
439
|
}
|