@sentry/junior 0.188.0 → 0.189.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (109) hide show
  1. package/dist/{agent-hooks-5RQEVLQD.js → agent-hooks-6QRYH6PX.js} +11 -11
  2. package/dist/api/acp/conversations.d.ts +2 -2
  3. package/dist/api/conversations/list.d.ts +7 -0
  4. package/dist/api/people/shared.d.ts +54 -0
  5. package/dist/api.js +41 -52
  6. package/dist/app.d.ts +1 -1
  7. package/dist/app.js +1003 -346
  8. package/dist/{catalog-runtime-L4A6BJ3U.js → catalog-runtime-IETJLJFG.js} +4 -4
  9. package/dist/chat/agent/resume.d.ts +0 -2
  10. package/dist/chat/agent/types.d.ts +8 -9
  11. package/dist/chat/agent-dispatch/work.d.ts +1 -10
  12. package/dist/chat/agent-invocations/spawn.d.ts +1 -1
  13. package/dist/chat/agent-invocations/types.d.ts +296 -96
  14. package/dist/chat/app/conversation-work.d.ts +16 -8
  15. package/dist/chat/conversation-privacy.d.ts +7 -7
  16. package/dist/chat/conversations/destination.d.ts +12 -0
  17. package/dist/chat/conversations/projection.d.ts +2 -2
  18. package/dist/chat/conversations/sql/location.d.ts +13 -4
  19. package/dist/chat/{api-turns → conversations}/stop.d.ts +2 -2
  20. package/dist/chat/conversations/store.d.ts +4 -4
  21. package/dist/chat/conversations/structured-events.d.ts +1 -1
  22. package/dist/chat/{api-turns/authorization.d.ts → conversations/web-authorization.d.ts} +1 -0
  23. package/dist/chat/conversations/web-input.d.ts +98 -0
  24. package/dist/chat/conversations/web-mailbox.d.ts +12 -0
  25. package/dist/chat/event-tasks/tool-support.d.ts +0 -6
  26. package/dist/chat/providers/slack/runtime.d.ts +1 -0
  27. package/dist/chat/providers/slack/system-turn.d.ts +8 -0
  28. package/dist/chat/resource-events/actor.d.ts +13 -12
  29. package/dist/chat/resource-events/notification.d.ts +1 -1
  30. package/dist/chat/services/guardian-action-policy.d.ts +1 -1
  31. package/dist/chat/services/turn-session-routing.d.ts +2 -2
  32. package/dist/chat/task-execution/assistant-message.d.ts +32 -0
  33. package/dist/chat/task-execution/conversation-turn.d.ts +8 -0
  34. package/dist/chat/{api-turns → task-execution}/mailbox-input.d.ts +1 -2
  35. package/dist/chat/task-execution/mailbox-turn.d.ts +42 -0
  36. package/dist/chat/task-execution/paused-turn.d.ts +1 -1
  37. package/dist/chat/task-execution/slack-work.d.ts +0 -2
  38. package/dist/{chunk-AW77HH54.js → chunk-4RHJ3CXR.js} +14 -295
  39. package/dist/{chunk-ZX7RSBQ6.js → chunk-5CXCGDIL.js} +1 -1
  40. package/dist/{chunk-QVH6FW3T.js → chunk-5VWAY27R.js} +7 -3
  41. package/dist/{chunk-XJIPMHKI.js → chunk-6AUR6PWR.js} +3 -3
  42. package/dist/{chunk-QDHJTKPE.js → chunk-7R5JRPCY.js} +8 -6
  43. package/dist/{chunk-6JZGXLL2.js → chunk-AC7MDOJW.js} +5 -7
  44. package/dist/{chunk-Q7O74WOO.js → chunk-BYBSI5TI.js} +2 -2
  45. package/dist/{chunk-VFZAUQ5V.js → chunk-CMWIMFM6.js} +4 -182
  46. package/dist/{chunk-IUUQWIG2.js → chunk-CUYFCI4G.js} +59 -11
  47. package/dist/chunk-DA2KGZZI.js +13 -0
  48. package/dist/{chunk-FIUCHYT6.js → chunk-DYXVAO2E.js} +1 -1
  49. package/dist/{chunk-NZLZ4KYZ.js → chunk-E3PDZPGV.js} +1 -1
  50. package/dist/{chunk-Z5SKZE5B.js → chunk-GP32KD2J.js} +4 -12
  51. package/dist/{chunk-XI4PVY2T.js → chunk-KN32OWL2.js} +5 -5
  52. package/dist/chunk-LHKZKAZ4.js +285 -0
  53. package/dist/{chunk-6YGFBU3A.js → chunk-MFSET7Q7.js} +44 -334
  54. package/dist/{chunk-QCGNHSMA.js → chunk-MI6ZBDTS.js} +6 -6
  55. package/dist/{chunk-CIG6WI7Q.js → chunk-MRXMFB4L.js} +5 -5
  56. package/dist/{chunk-CVBDVIF4.js → chunk-MVPLEPSI.js} +1 -1
  57. package/dist/{chunk-32XSSFZV.js → chunk-NY2F7BZA.js} +1 -1
  58. package/dist/chunk-O35GZCKL.js +311 -0
  59. package/dist/{chunk-HCIVBCP5.js → chunk-OEIITG5F.js} +1 -1
  60. package/dist/{chunk-UVUXFRYN.js → chunk-PCPSX4ES.js} +209 -7
  61. package/dist/{chunk-QIMN3FXL.js → chunk-PY3EIEAF.js} +1 -1
  62. package/dist/{chunk-RMB7CERI.js → chunk-QNIOBNOY.js} +282 -121
  63. package/dist/{chunk-WGZJ5SU4.js → chunk-S3SGZUOI.js} +1 -1
  64. package/dist/chunk-SR6OMO5C.js +703 -0
  65. package/dist/{chunk-CDSLOYOR.js → chunk-UCWO5MKP.js} +6 -6
  66. package/dist/{chunk-PSHMPRIU.js → chunk-VKQWU4PO.js} +2 -2
  67. package/dist/{chunk-X55FOVZO.js → chunk-WDHPN2GH.js} +3 -3
  68. package/dist/{chunk-WW2Y7JIA.js → chunk-WMUAKAIF.js} +43 -44
  69. package/dist/{chunk-MQDIIFZM.js → chunk-WN6VLHOU.js} +12 -5
  70. package/dist/{chunk-W42ENMJW.js → chunk-WWYUYKKV.js} +1 -1
  71. package/dist/{chunk-ERZMACYA.js → chunk-WYDF6PGK.js} +3 -3
  72. package/dist/{chunk-SC67CH3C.js → chunk-YNZPOFF3.js} +14 -17
  73. package/dist/{chunk-3HEZWDS5.js → chunk-YYSEH2WC.js} +2 -2
  74. package/dist/{chunk-W2DDJCPS.js → chunk-YYZNESC3.js} +8 -8
  75. package/dist/{chunk-CZRWU4ZM.js → chunk-ZUPDSJUE.js} +2 -5
  76. package/dist/{chunk-QIOUTXTZ.js → chunk-ZV6PFUA4.js} +1 -1
  77. package/dist/cli/chat.js +34 -33
  78. package/dist/cli/check.js +5 -5
  79. package/dist/cli/plugins.js +12 -12
  80. package/dist/cli/snapshot-create.js +9 -9
  81. package/dist/cli/upgrade.js +6 -6
  82. package/dist/db/schema/agent-invocations.d.ts +0 -36
  83. package/dist/db/schema/conversations.d.ts +62 -0
  84. package/dist/db/schema.d.ts +31 -36
  85. package/dist/{db-YPRZAA2T.js → db-LPJKR2EO.js} +6 -6
  86. package/dist/instrumentation.js +3 -3
  87. package/dist/nitro.js +6 -6
  88. package/dist/{oauth-callback-server-DBBIR3XG.js → oauth-callback-server-DMXF4TTQ.js} +30 -28
  89. package/dist/{runner-FKVFFPLO.js → runner-L5DPVYCN.js} +25 -21
  90. package/dist/{skills-4RBAZEMV.js → skills-NXG3LBCK.js} +5 -5
  91. package/dist/spawn-VRUIGKWW.js +42 -0
  92. package/dist/task-runner-7YTBSPXW.js +36 -0
  93. package/dist/{validation-E6Z5LHZM.js → validation-CH36LTOS.js} +5 -5
  94. package/dist/version.js +1 -1
  95. package/dist/{work-OFTXWOK4.js → work-HBC5Y5VZ.js} +22 -21
  96. package/dist/{worker-OMWNOKTJ.js → worker-5RDZHLXH.js} +10 -10
  97. package/migrations/0037_conversation_location.sql +23 -0
  98. package/migrations/meta/0037_snapshot.json +3192 -0
  99. package/migrations/meta/_journal.json +7 -0
  100. package/package.json +5 -5
  101. package/dist/chat/api-turns/assistant-message.d.ts +0 -12
  102. package/dist/chat/api-turns/cancellation.d.ts +0 -24
  103. package/dist/chat/api-turns/routing.d.ts +0 -51
  104. package/dist/chat/api-turns/work.d.ts +0 -99
  105. package/dist/chat/task-execution/slack-resource-event.d.ts +0 -55
  106. package/dist/chunk-6FGN7ZEG.js +0 -221
  107. package/dist/chunk-LODJKINF.js +0 -1502
  108. package/dist/spawn-CO7QFCNJ.js +0 -41
  109. package/dist/task-runner-DUGZGI7V.js +0 -34
@@ -260,6 +260,13 @@
260
260
  "when": 1787598193137,
261
261
  "tag": "0036_complex_rage",
262
262
  "breakpoints": true
263
+ },
264
+ {
265
+ "idx": 37,
266
+ "version": "7",
267
+ "when": 1787873831749,
268
+ "tag": "0037_conversation_location",
269
+ "breakpoints": true
263
270
  }
264
271
  ]
265
272
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sentry/junior",
3
- "version": "0.188.0",
3
+ "version": "0.189.0",
4
4
  "private": false,
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -85,7 +85,7 @@
85
85
  "pg": "^8.16.3",
86
86
  "yaml": "^2.9.0",
87
87
  "zod": "^4.4.3",
88
- "@sentry/junior-plugin-api": "0.188.0"
88
+ "@sentry/junior-plugin-api": "0.189.0"
89
89
  },
90
90
  "devDependencies": {
91
91
  "@emnapi/core": "^1.10.0",
@@ -104,9 +104,9 @@
104
104
  "typescript": "^6.0.3",
105
105
  "vercel": "^54.4.0",
106
106
  "vitest": "^4.1.7",
107
- "@sentry/junior-testing": "0.0.0",
108
- "@sentry/junior-memory": "0.188.0",
109
- "@sentry/junior-github": "0.188.0"
107
+ "@sentry/junior-github": "0.189.0",
108
+ "@sentry/junior-memory": "0.189.0",
109
+ "@sentry/junior-testing": "0.0.0"
110
110
  },
111
111
  "scripts": {
112
112
  "build": "tsup && tsc -p tsconfig.build.json --emitDeclarationOnly",
@@ -1,12 +0,0 @@
1
- import type { AssistantMessage } from "@earendil-works/pi-ai";
2
- import type { ThreadConversationState } from "@/chat/state/conversation";
3
- /** Store one assistant Message and its Agent history. */
4
- export declare function commitAssistantMessage(args: {
5
- agentMessage?: AssistantMessage;
6
- conversation: ThreadConversationState;
7
- conversationId: string;
8
- sessionId: string;
9
- source?: "web";
10
- text: string;
11
- userMessageId: string;
12
- }): Promise<void>;
@@ -1,24 +0,0 @@
1
- /** App-scoped control for one active Turn started by the Conversation API. */
2
- export interface ApiTurnCancellation {
3
- begin(conversationId: string): AbortSignal | undefined;
4
- cancel(conversationId: string): boolean;
5
- disconnect(conversationId: string, signal: AbortSignal): void;
6
- finish(conversationId: string, signal: AbortSignal): void;
7
- park(conversationId: string, signal: AbortSignal): void;
8
- signal(conversationId: string): AbortSignal | undefined;
9
- }
10
- /** Create in-process cancellation state for Turns from the Conversation API. */
11
- export declare function createApiTurnCancellation(): ApiTurnCancellation;
12
- /** Close one cancelled Turn without a failure reply or retry. */
13
- export declare function completeCancelledApiTurn(args: {
14
- acknowledge(): Promise<void>;
15
- cancellation?: ApiTurnCancellation;
16
- conversation: ThreadConversationState;
17
- conversationId: string;
18
- sandboxRef?: SandboxRef;
19
- signal?: AbortSignal;
20
- turnId: string;
21
- userMessageId: string;
22
- }): Promise<void>;
23
- import type { SandboxRef } from "@/chat/sandbox/ref";
24
- import type { ThreadConversationState } from "@/chat/state/conversation";
@@ -1,51 +0,0 @@
1
- import { z } from "zod";
2
- import { type Source } from "@sentry/junior-plugin-api";
3
- import { type Actor } from "@/chat/actor";
4
- import type { ConversationPrivacy } from "@/chat/conversation-privacy";
5
- import type { ConversationAuthor, ConversationMessage } from "@/chat/state/conversation";
6
- import type { InboundMessage } from "@/chat/task-execution/store";
7
- import type { ConversationWorkerContext } from "@/chat/task-execution/worker";
8
- declare const apiTurnMailboxMetadataSchema: z.ZodObject<{
9
- authorEmail: z.ZodString;
10
- authorFullName: z.ZodOptional<z.ZodString>;
11
- authorUserId: z.ZodString;
12
- authorUserName: z.ZodOptional<z.ZodString>;
13
- kind: z.ZodLiteral<"api_turn">;
14
- messageId: z.ZodString;
15
- }, z.core.$strict>;
16
- export type ApiTurnMailboxMetadata = z.output<typeof apiTurnMailboxMetadataSchema>;
17
- export interface TurnInput {
18
- actor: Actor;
19
- author: ConversationAuthor;
20
- message: InboundMessage;
21
- source: "resource_event" | "web";
22
- }
23
- export type TurnIdentity = Pick<TurnInput, "actor" | "author" | "source">;
24
- /** Restore normalized Turn identity from one saved Message. */
25
- export declare function turnIdentityFromConversationMessage(message: ConversationMessage): TurnIdentity | undefined;
26
- /** Build the AgentRun Source for one Turn input. */
27
- export declare function sourceFromTurnInput(args: {
28
- conversationId: string;
29
- source: TurnInput["source"];
30
- visibility?: ConversationPrivacy;
31
- }): Source;
32
- /** Return the active root API Turn for one Conversation, if it has one. */
33
- export declare function getActiveApiTurnId(conversationId: string): Promise<string | undefined>;
34
- /** Return the idle auth-paused root API Turn for one Conversation. */
35
- export declare function getAuthPausedApiTurnId(conversationId: string): Promise<string | undefined>;
36
- /**
37
- * Resolve conversation-only work from new input or a saved Turn.
38
- *
39
- * User messages from the Conversation API run here. Resource events with a
40
- * local Destination run here too. Saved state identifies a resumed Turn.
41
- */
42
- export declare function resolveApiTurnWork(context: ConversationWorkerContext): Promise<{
43
- kind: "mailbox";
44
- batch: TurnInput[];
45
- } | {
46
- kind: "resume";
47
- turnId: string;
48
- } | undefined>;
49
- /** Return whether the leased attempt belongs to an API Turn. */
50
- export declare function isApiTurnWork(context: ConversationWorkerContext): Promise<boolean>;
51
- export {};
@@ -1,99 +0,0 @@
1
- import type { StateAdapter } from "chat";
2
- import { type Destination } from "@sentry/junior-plugin-api";
3
- import type { ConversationPrivacy } from "@/chat/conversation-privacy";
4
- import { type WebActor } from "@/chat/actor";
5
- import type { ConversationStore } from "@/chat/conversations/store";
6
- import type { AgentRunner } from "@/chat/runtime/agent-runner";
7
- import { type InboundMessage } from "@/chat/task-execution/store";
8
- import type { ConversationWorkQueue } from "@/chat/task-execution/queue";
9
- import type { ConversationWorkerContext, ConversationWorkerResult } from "@/chat/task-execution/worker";
10
- import { type ApiTurnCancellation } from "@/chat/api-turns/cancellation";
11
- export { resolveApiTurnWork } from "@/chat/api-turns/routing";
12
- type EnqueueOptions = {
13
- conversationStore?: ConversationStore;
14
- nowMs?: number;
15
- queue: ConversationWorkQueue;
16
- state?: StateAdapter;
17
- };
18
- export interface CreateApiConversationInput {
19
- actor: WebActor;
20
- message: string;
21
- /** Client-supplied idempotency key for the first message. */
22
- idempotencyKey: string;
23
- /** New roots default public. Continues never rewrite visibility. */
24
- visibility?: ConversationPrivacy;
25
- }
26
- export interface AppendApiConversationMessageInput {
27
- actor: WebActor;
28
- conversationId: string;
29
- message: string;
30
- idempotencyKey: string;
31
- /** Applied only when this call creates the conversation root. */
32
- rootVisibility?: ConversationPrivacy;
33
- }
34
- export interface ApiConversationMessageAccepted {
35
- conversationId: string;
36
- messageId: string;
37
- status: "accepted" | "duplicate";
38
- }
39
- export type ApiConversationMessageAdmission = ApiConversationMessageAccepted | {
40
- conversationId: string;
41
- messageId: string;
42
- status: "active";
43
- };
44
- /**
45
- * Build a durable API conversation id for one viewer + create key.
46
- *
47
- * Retries of POST /api/conversations with the same key must address the same
48
- * conversation before the mailbox message id is derived.
49
- */
50
- export declare function createApiConversationId(args: {
51
- actorEmail: string;
52
- idempotencyKey: string;
53
- }): string;
54
- /** Build the retry-stable Message id used by one API mailbox request. */
55
- export declare function apiConversationMessageId(args: {
56
- conversationId: string;
57
- idempotencyKey: string;
58
- }): string;
59
- /** Stable turn id for one API mailbox message (matches getTurnUserMessage). */
60
- export declare function apiTurnIdForMessage(messageId: string): string;
61
- /** Rebuild the dashboard actor from durable conversation identity. */
62
- export declare function webActorFromEmail(email: string, profile?: {
63
- fullName?: string;
64
- userName?: string;
65
- }): WebActor;
66
- /** Build one API mailbox entry with conversation-only publish. */
67
- export declare function buildApiTurnInboundMessage(args: {
68
- actor: WebActor;
69
- conversationId: string;
70
- createdAtMs?: number;
71
- /** Existing conversation destination; required when continuing a provider root. */
72
- destination?: Destination;
73
- message: string;
74
- messageId: string;
75
- nowMs?: number;
76
- }): InboundMessage;
77
- /** Record web activity and materialize a new API Conversation root when needed. */
78
- export declare function recordApiConversationActivity(args: {
79
- actor: WebActor;
80
- conversationId: string;
81
- conversationStore?: ConversationStore;
82
- nowMs: number;
83
- /** Applied only when this call creates the conversation root. */
84
- rootVisibility?: ConversationPrivacy;
85
- }): Promise<Destination>;
86
- /** Create a dashboard root conversation and enqueue its first message. */
87
- export declare function createAndEnqueueApiConversation(input: CreateApiConversationInput, options: EnqueueOptions): Promise<ApiConversationMessageAccepted>;
88
- /** Append one Conversation API message and optionally require an idle Conversation. */
89
- export declare function appendAndEnqueueApiConversationMessage(input: AppendApiConversationMessageInput, options: EnqueueOptions & {
90
- exclusive: true;
91
- }): Promise<ApiConversationMessageAdmission>;
92
- export declare function appendAndEnqueueApiConversationMessage(input: AppendApiConversationMessageInput, options: EnqueueOptions): Promise<ApiConversationMessageAccepted>;
93
- /** Create the worker for Turns that need no provider delivery. */
94
- export declare function createApiTurnWorker(agentRunner: AgentRunner, cancellation?: ApiTurnCancellation): (context: ConversationWorkerContext) => Promise<ConversationWorkerResult>;
95
- /** Route Conversation API work before other work. */
96
- export declare function routeApiTurnWork(options: {
97
- apiTurnWorker: (context: ConversationWorkerContext) => Promise<ConversationWorkerResult>;
98
- fallbackWorker: (context: ConversationWorkerContext) => Promise<ConversationWorkerResult>;
99
- }): (context: ConversationWorkerContext) => Promise<ConversationWorkerResult>;
@@ -1,55 +0,0 @@
1
- /**
2
- * Slack edge helpers for resource-event mailbox wakes.
3
- *
4
- * Resource-event ingest stores plain conversation mailbox input. When the
5
- * conversation destination is Slack, this module builds the Message and Thread
6
- * the Slack runtime needs for one turn. It does not rewrite mailbox rows.
7
- *
8
- * TODO(slack-runtime): Drop this module once Slack turns can start from
9
- * conversation destination + session source (channel, team, threadTs, text,
10
- * event metadata) without a synthetic chat Message/Thread. Resource wakes and
11
- * paused resume should share that entry; handleSubscribedMessage stays for
12
- * real webhook traffic only.
13
- */
14
- import { Message, ThreadImpl, type StateAdapter } from "chat";
15
- import type { SlackAdapter } from "@chat-adapter/slack";
16
- import type { Destination, SlackDestination } from "@sentry/junior-plugin-api";
17
- import type { ConversationStore } from "@/chat/conversations/store";
18
- import type { InboundMessage } from "@/chat/task-execution/store";
19
- /** Resolved Slack place for one plain resource-event wake, when a thread exists. */
20
- export type SlackResourceEventThread = {
21
- destination: SlackDestination;
22
- threadTs: string;
23
- };
24
- /**
25
- * Load Slack destination and threadTs for a resource-event wake.
26
- *
27
- * Returns undefined when the Conversation has no Slack thread. The temporary
28
- * synthetic Message/Thread bridge cannot invent one; the Slack worker must drop
29
- * that wake instead of retrying.
30
- */
31
- export declare function resolveSlackResourceEventThread(args: {
32
- conversationId: string;
33
- conversationStore?: ConversationStore;
34
- destination?: Destination;
35
- }): Promise<SlackResourceEventThread | undefined>;
36
- /** Whether a mailbox row is a plain resource-event wake (no Slack envelope). */
37
- export declare function isPlainResourceEventRecord(record: InboundMessage): boolean;
38
- /**
39
- * Build a Slack Message for one plain resource-event mailbox row.
40
- *
41
- * Used only at the Slack worker edge. The mailbox row stays plain.
42
- */
43
- export declare function buildResourceEventSlackMessage(args: {
44
- channelId: string;
45
- record: InboundMessage;
46
- threadTs: string;
47
- }): Message;
48
- /** Build a ThreadImpl for a resource-event Slack turn. */
49
- export declare function buildResourceEventSlackThread(args: {
50
- adapter: SlackAdapter;
51
- message: Message;
52
- state: StateAdapter;
53
- threadTs: string;
54
- channelId: string;
55
- }): ThreadImpl;
@@ -1,221 +0,0 @@
1
- import {
2
- isExperimentalFeatureEnabled
3
- } from "./chunk-2LYHWMR4.js";
4
-
5
- // src/chat/agent/types.ts
6
- var RetryableDeliveryError = class extends Error {
7
- constructor(cause) {
8
- super("Assistant delivery was transient or ambiguous", { cause });
9
- this.name = "RetryableDeliveryError";
10
- }
11
- };
12
- function isAgentRunFeatureDisabled(disabledFeatures, feature) {
13
- return disabledFeatures?.includes(feature) ?? false;
14
- }
15
- function actorFromRun(run) {
16
- if (run.dispatch?.actor) {
17
- return run.dispatch.actor;
18
- }
19
- if (run.actor) {
20
- return run.actor;
21
- }
22
- if (run.credentialContext && !("type" in run.credentialContext.actor)) {
23
- return run.credentialContext.actor;
24
- }
25
- return void 0;
26
- }
27
- function assertRunConsistency(run) {
28
- const { destination, source } = run;
29
- switch (source.platform) {
30
- case "slack": {
31
- if (destination.platform !== "slack") {
32
- throw new TypeError(
33
- "Run source and destination platforms do not match"
34
- );
35
- }
36
- if (source.teamId !== destination.teamId) {
37
- throw new TypeError("Slack source and destination teams do not match");
38
- }
39
- break;
40
- }
41
- case "local": {
42
- if (destination.platform !== "local") {
43
- throw new TypeError(
44
- "Run source and destination platforms do not match"
45
- );
46
- }
47
- if (source.conversationId !== destination.conversationId) {
48
- throw new TypeError(
49
- "Source and destination conversation IDs do not match"
50
- );
51
- }
52
- if (run.surface !== "internal" && destination.conversationId !== run.conversationId) {
53
- throw new TypeError(
54
- "Source, destination, and run conversation IDs do not match"
55
- );
56
- }
57
- break;
58
- }
59
- case "web": {
60
- if (run.surface !== "internal" && source.conversationId !== run.conversationId) {
61
- throw new TypeError("Web source and run conversation IDs do not match");
62
- }
63
- switch (destination.platform) {
64
- case "local": {
65
- if (source.conversationId !== destination.conversationId) {
66
- throw new TypeError(
67
- "Source and destination conversation IDs do not match"
68
- );
69
- }
70
- if (run.surface !== "internal" && destination.conversationId !== run.conversationId) {
71
- throw new TypeError(
72
- "Source, destination, and run conversation IDs do not match"
73
- );
74
- }
75
- break;
76
- }
77
- case "slack": {
78
- if (run.publishExternally) {
79
- throw new TypeError(
80
- "Web turns on Slack destinations must not publish externally"
81
- );
82
- }
83
- break;
84
- }
85
- }
86
- break;
87
- }
88
- }
89
- const actor = run.dispatch?.actor ?? run.actor;
90
- if (!actor || actor.platform === "system") {
91
- return;
92
- }
93
- const actorMatchesDestination = (() => {
94
- switch (actor.platform) {
95
- case "slack":
96
- return destination.platform === "slack";
97
- case "local":
98
- return destination.platform === "local";
99
- case "web":
100
- return destination.platform === "local" || destination.platform === "slack" && run.publishExternally !== true;
101
- }
102
- })();
103
- if (!actorMatchesDestination) {
104
- throw new TypeError(
105
- `Actor platform "${actor.platform}" does not match destination platform "${destination.platform}"`
106
- );
107
- }
108
- if (actor.platform === "slack" && destination.platform === "slack" && actor.teamId !== destination.teamId) {
109
- throw new TypeError("Slack actor team does not match destination team");
110
- }
111
- }
112
- function toolInvocationDestination(run) {
113
- if (run.destination.platform !== "slack" || !run.toolChannelId) {
114
- return run.destination;
115
- }
116
- return {
117
- platform: "slack",
118
- teamId: run.destination.teamId,
119
- channelId: run.toolChannelId
120
- };
121
- }
122
- function surfaceFromRun(run) {
123
- if (run.surface) {
124
- return run.surface;
125
- }
126
- if (run.source.platform === "slack") {
127
- return "slack";
128
- }
129
- if (run.source.platform === "web") {
130
- return "api";
131
- }
132
- return "internal";
133
- }
134
-
135
- // src/chat/runtime/agent-runner.ts
136
- var AGENT_ABORT_SETTLE_GRACE_MS = 5e3;
137
- function createAgentRunner(execute, options) {
138
- const attachmentStorage = options?.attachmentStorage;
139
- const streamFn = options?.streamFn;
140
- const tracePropagation = options?.tracePropagation;
141
- const bindSpawnAgent = options?.bindSpawnAgent;
142
- const canBindSpawn = Boolean(bindSpawnAgent) && isExperimentalFeatureEnabled("subagents");
143
- return {
144
- run: async (run) => {
145
- const spawnAgent = bindSpawnAgent && canBindSpawn && !isAgentRunFeatureDisabled(run.disabledFeatures, "subagents") ? bindSpawnAgent(run) : void 0;
146
- const nextRun = {
147
- ...run,
148
- environment: {
149
- ...run.environment,
150
- attachmentStorage: run.environment?.attachmentStorage ?? attachmentStorage,
151
- sandboxTracePropagation: run.environment?.sandboxTracePropagation ?? tracePropagation
152
- },
153
- ...spawnAgent ? {
154
- durability: {
155
- ...run.durability,
156
- spawnAgent
157
- }
158
- } : void 0
159
- };
160
- return await execute(nextRun, streamFn);
161
- }
162
- };
163
- }
164
- async function waitForAgentSettlement(runPromise) {
165
- let timeoutId;
166
- const settleGrace = new Promise((resolve) => {
167
- timeoutId = setTimeout(resolve, AGENT_ABORT_SETTLE_GRACE_MS);
168
- timeoutId.unref?.();
169
- });
170
- await Promise.race([
171
- runPromise.then(
172
- () => void 0,
173
- () => void 0
174
- ),
175
- settleGrace
176
- ]);
177
- if (timeoutId) {
178
- clearTimeout(timeoutId);
179
- }
180
- }
181
- async function runAgentWithTimeout(agentRunner, run, timeoutMs) {
182
- if (typeof timeoutMs !== "number") {
183
- return await agentRunner.run(run);
184
- }
185
- const timeoutError = new Error(
186
- `executeAgentRun timed out after ${timeoutMs}ms`
187
- );
188
- const abortController = new AbortController();
189
- const signal = run.signal ? AbortSignal.any([run.signal, abortController.signal]) : abortController.signal;
190
- const runPromise = agentRunner.run({ ...run, signal });
191
- let timeoutId;
192
- const timeoutPromise = new Promise((_, reject) => {
193
- timeoutId = setTimeout(() => {
194
- abortController.abort(timeoutError);
195
- reject(timeoutError);
196
- }, timeoutMs);
197
- });
198
- try {
199
- return await Promise.race([runPromise, timeoutPromise]);
200
- } catch (error) {
201
- if (abortController.signal.aborted) {
202
- await waitForAgentSettlement(runPromise);
203
- }
204
- throw error;
205
- } finally {
206
- if (timeoutId) {
207
- clearTimeout(timeoutId);
208
- }
209
- }
210
- }
211
-
212
- export {
213
- RetryableDeliveryError,
214
- isAgentRunFeatureDisabled,
215
- actorFromRun,
216
- assertRunConsistency,
217
- toolInvocationDestination,
218
- surfaceFromRun,
219
- createAgentRunner,
220
- runAgentWithTimeout
221
- };