@trigger.dev/sdk 4.4.6 → 4.5.0-rc.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.
- package/dist/commonjs/v3/agentSkillsRuntime.d.ts +28 -0
- package/dist/commonjs/v3/agentSkillsRuntime.js +163 -0
- package/dist/commonjs/v3/agentSkillsRuntime.js.map +1 -0
- package/dist/commonjs/v3/ai-shared.d.ts +173 -0
- package/dist/commonjs/v3/ai-shared.js +25 -0
- package/dist/commonjs/v3/ai-shared.js.map +1 -0
- package/dist/commonjs/v3/ai.d.ts +2823 -5
- package/dist/commonjs/v3/ai.js +6197 -13
- package/dist/commonjs/v3/ai.js.map +1 -1
- package/dist/commonjs/v3/auth.d.ts +9 -0
- package/dist/commonjs/v3/auth.js.map +1 -1
- package/dist/commonjs/v3/chat-client.d.ts +301 -0
- package/dist/commonjs/v3/chat-client.js +624 -0
- package/dist/commonjs/v3/chat-client.js.map +1 -0
- package/dist/commonjs/v3/chat-react.d.ts +155 -0
- package/dist/commonjs/v3/chat-react.js +330 -0
- package/dist/commonjs/v3/chat-react.js.map +1 -0
- package/dist/commonjs/v3/chat-server.d.ts +206 -0
- package/dist/commonjs/v3/chat-server.js +737 -0
- package/dist/commonjs/v3/chat-server.js.map +1 -0
- package/dist/commonjs/v3/chat-server.test.d.ts +1 -0
- package/dist/commonjs/v3/chat-server.test.js +518 -0
- package/dist/commonjs/v3/chat-server.test.js.map +1 -0
- package/dist/commonjs/v3/chat-tab-coordinator.d.ts +65 -0
- package/dist/commonjs/v3/chat-tab-coordinator.js +235 -0
- package/dist/commonjs/v3/chat-tab-coordinator.js.map +1 -0
- package/dist/commonjs/v3/chat-tab-coordinator.test.d.ts +1 -0
- package/dist/commonjs/v3/chat-tab-coordinator.test.js +140 -0
- package/dist/commonjs/v3/chat-tab-coordinator.test.js.map +1 -0
- package/dist/commonjs/v3/chat.d.ts +437 -0
- package/dist/commonjs/v3/chat.js +968 -0
- package/dist/commonjs/v3/chat.js.map +1 -0
- package/dist/commonjs/v3/chat.test.d.ts +1 -0
- package/dist/commonjs/v3/chat.test.js +1180 -0
- package/dist/commonjs/v3/chat.test.js.map +1 -0
- package/dist/commonjs/v3/createStartSessionAction.test.d.ts +1 -0
- package/dist/commonjs/v3/createStartSessionAction.test.js +113 -0
- package/dist/commonjs/v3/createStartSessionAction.test.js.map +1 -0
- package/dist/commonjs/v3/deployments.d.ts +26 -0
- package/dist/commonjs/v3/deployments.js +37 -0
- package/dist/commonjs/v3/deployments.js.map +1 -0
- package/dist/commonjs/v3/index.d.ts +6 -3
- package/dist/commonjs/v3/index.js +7 -1
- package/dist/commonjs/v3/index.js.map +1 -1
- package/dist/commonjs/v3/runs.d.ts +22 -7
- package/dist/commonjs/v3/runs.js +1 -0
- package/dist/commonjs/v3/runs.js.map +1 -1
- package/dist/commonjs/v3/sessions.d.ts +228 -0
- package/dist/commonjs/v3/sessions.js +664 -0
- package/dist/commonjs/v3/sessions.js.map +1 -0
- package/dist/commonjs/v3/sessions.test.d.ts +1 -0
- package/dist/commonjs/v3/sessions.test.js +154 -0
- package/dist/commonjs/v3/sessions.test.js.map +1 -0
- package/dist/commonjs/v3/shared.d.ts +24 -2
- package/dist/commonjs/v3/shared.js +189 -1
- package/dist/commonjs/v3/shared.js.map +1 -1
- package/dist/commonjs/v3/skill.d.ts +99 -0
- package/dist/commonjs/v3/skill.js +155 -0
- package/dist/commonjs/v3/skill.js.map +1 -0
- package/dist/commonjs/v3/skills.d.ts +2 -0
- package/dist/commonjs/v3/skills.js +6 -0
- package/dist/commonjs/v3/skills.js.map +1 -0
- package/dist/commonjs/v3/streams.js +127 -19
- package/dist/commonjs/v3/streams.js.map +1 -1
- package/dist/commonjs/v3/tasks.d.ts +2 -1
- package/dist/commonjs/v3/tasks.js +1 -0
- package/dist/commonjs/v3/tasks.js.map +1 -1
- package/dist/commonjs/v3/test/index.d.ts +3 -0
- package/dist/commonjs/v3/test/index.js +18 -0
- package/dist/commonjs/v3/test/index.js.map +1 -0
- package/dist/commonjs/v3/test/mock-chat-agent.d.ts +259 -0
- package/dist/commonjs/v3/test/mock-chat-agent.js +468 -0
- package/dist/commonjs/v3/test/mock-chat-agent.js.map +1 -0
- package/dist/commonjs/v3/test/setup-catalog.d.ts +1 -0
- package/dist/commonjs/v3/test/setup-catalog.js +18 -0
- package/dist/commonjs/v3/test/setup-catalog.js.map +1 -0
- package/dist/commonjs/v3/test/test-session-handle.d.ts +53 -0
- package/dist/commonjs/v3/test/test-session-handle.js +256 -0
- package/dist/commonjs/v3/test/test-session-handle.js.map +1 -0
- package/dist/commonjs/version.js +1 -1
- package/dist/esm/v3/agentSkillsRuntime.d.ts +28 -0
- package/dist/esm/v3/agentSkillsRuntime.js +136 -0
- package/dist/esm/v3/agentSkillsRuntime.js.map +1 -0
- package/dist/esm/v3/ai-shared.d.ts +173 -0
- package/dist/esm/v3/ai-shared.js +22 -0
- package/dist/esm/v3/ai-shared.js.map +1 -0
- package/dist/esm/v3/ai.d.ts +2823 -5
- package/dist/esm/v3/ai.js +6187 -14
- package/dist/esm/v3/ai.js.map +1 -1
- package/dist/esm/v3/auth.d.ts +9 -0
- package/dist/esm/v3/auth.js.map +1 -1
- package/dist/esm/v3/chat-client.d.ts +301 -0
- package/dist/esm/v3/chat-client.js +619 -0
- package/dist/esm/v3/chat-client.js.map +1 -0
- package/dist/esm/v3/chat-react.d.ts +155 -0
- package/dist/esm/v3/chat-react.js +325 -0
- package/dist/esm/v3/chat-react.js.map +1 -0
- package/dist/esm/v3/chat-server.d.ts +206 -0
- package/dist/esm/v3/chat-server.js +734 -0
- package/dist/esm/v3/chat-server.js.map +1 -0
- package/dist/esm/v3/chat-server.test.d.ts +1 -0
- package/dist/esm/v3/chat-server.test.js +516 -0
- package/dist/esm/v3/chat-server.test.js.map +1 -0
- package/dist/esm/v3/chat-tab-coordinator.d.ts +65 -0
- package/dist/esm/v3/chat-tab-coordinator.js +231 -0
- package/dist/esm/v3/chat-tab-coordinator.js.map +1 -0
- package/dist/esm/v3/chat-tab-coordinator.test.d.ts +1 -0
- package/dist/esm/v3/chat-tab-coordinator.test.js +138 -0
- package/dist/esm/v3/chat-tab-coordinator.test.js.map +1 -0
- package/dist/esm/v3/chat.d.ts +437 -0
- package/dist/esm/v3/chat.js +961 -0
- package/dist/esm/v3/chat.js.map +1 -0
- package/dist/esm/v3/chat.test.d.ts +1 -0
- package/dist/esm/v3/chat.test.js +1178 -0
- package/dist/esm/v3/chat.test.js.map +1 -0
- package/dist/esm/v3/createStartSessionAction.test.d.ts +1 -0
- package/dist/esm/v3/createStartSessionAction.test.js +111 -0
- package/dist/esm/v3/createStartSessionAction.test.js.map +1 -0
- package/dist/esm/v3/deployments.d.ts +26 -0
- package/dist/esm/v3/deployments.js +34 -0
- package/dist/esm/v3/deployments.js.map +1 -0
- package/dist/esm/v3/index.d.ts +6 -3
- package/dist/esm/v3/index.js +4 -1
- package/dist/esm/v3/index.js.map +1 -1
- package/dist/esm/v3/runs.d.ts +15 -0
- package/dist/esm/v3/runs.js +1 -0
- package/dist/esm/v3/runs.js.map +1 -1
- package/dist/esm/v3/sessions.d.ts +228 -0
- package/dist/esm/v3/sessions.js +656 -0
- package/dist/esm/v3/sessions.js.map +1 -0
- package/dist/esm/v3/sessions.test.d.ts +1 -0
- package/dist/esm/v3/sessions.test.js +152 -0
- package/dist/esm/v3/sessions.test.js.map +1 -0
- package/dist/esm/v3/shared.d.ts +24 -2
- package/dist/esm/v3/shared.js +188 -1
- package/dist/esm/v3/shared.js.map +1 -1
- package/dist/esm/v3/skill.d.ts +99 -0
- package/dist/esm/v3/skill.js +128 -0
- package/dist/esm/v3/skill.js.map +1 -0
- package/dist/esm/v3/skills.d.ts +2 -0
- package/dist/esm/v3/skills.js +2 -0
- package/dist/esm/v3/skills.js.map +1 -0
- package/dist/esm/v3/streams.js +127 -20
- package/dist/esm/v3/streams.js.map +1 -1
- package/dist/esm/v3/tasks.d.ts +2 -1
- package/dist/esm/v3/tasks.js +2 -1
- package/dist/esm/v3/tasks.js.map +1 -1
- package/dist/esm/v3/test/index.d.ts +3 -0
- package/dist/esm/v3/test/index.js +13 -0
- package/dist/esm/v3/test/index.js.map +1 -0
- package/dist/esm/v3/test/mock-chat-agent.d.ts +259 -0
- package/dist/esm/v3/test/mock-chat-agent.js +465 -0
- package/dist/esm/v3/test/mock-chat-agent.js.map +1 -0
- package/dist/esm/v3/test/setup-catalog.d.ts +1 -0
- package/dist/esm/v3/test/setup-catalog.js +16 -0
- package/dist/esm/v3/test/setup-catalog.js.map +1 -0
- package/dist/esm/v3/test/test-session-handle.d.ts +53 -0
- package/dist/esm/v3/test/test-session-handle.js +251 -0
- package/dist/esm/v3/test/test-session-handle.js.map +1 -0
- package/dist/esm/version.js +1 -1
- package/package.json +87 -6
|
@@ -0,0 +1,656 @@
|
|
|
1
|
+
import { InputStreamOncePromise, ManualWaitpointPromise, SemanticInternalAttributes, SessionStreamInstance, WaitpointTimeoutError, accessoryAttributes, apiClientManager, ensureReadableStream, mergeRequestOptions, runtime, sessionStreams, taskContext, trimSessionStream, writeSessionControlRecord, } from "@trigger.dev/core/v3";
|
|
2
|
+
import { conditionallyImportAndParsePacket } from "@trigger.dev/core/v3/utils/ioSerialization";
|
|
3
|
+
import { SpanStatusCode } from "@opentelemetry/api";
|
|
4
|
+
import { tracer } from "./tracer.js";
|
|
5
|
+
export const sessions = {
|
|
6
|
+
start: startSession,
|
|
7
|
+
retrieve: retrieveSession,
|
|
8
|
+
update: updateSession,
|
|
9
|
+
close: closeSession,
|
|
10
|
+
list: listSessions,
|
|
11
|
+
open,
|
|
12
|
+
};
|
|
13
|
+
let sessionOpenImpl;
|
|
14
|
+
export function __setSessionOpenImplForTests(impl) {
|
|
15
|
+
sessionOpenImpl = impl;
|
|
16
|
+
}
|
|
17
|
+
let sessionStartImpl;
|
|
18
|
+
export function __setSessionStartImplForTests(impl) {
|
|
19
|
+
sessionStartImpl = impl;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Start a {@link Session} — a durable, task-bound, bidirectional I/O
|
|
23
|
+
* primitive. The server creates the row (idempotent on `externalId`)
|
|
24
|
+
* and triggers the first run from `triggerConfig` in one round-trip.
|
|
25
|
+
* Returns the new run's id and a session-scoped public access token
|
|
26
|
+
* for browser-side use against `.in/append`, `.out` SSE, and
|
|
27
|
+
* `end-and-continue`.
|
|
28
|
+
*
|
|
29
|
+
* If a session with the same `(env, externalId)` already exists,
|
|
30
|
+
* returns the existing row plus the live (or freshly re-triggered) run.
|
|
31
|
+
* Two browser tabs of the same chat converge to one session.
|
|
32
|
+
*/
|
|
33
|
+
function startSession(body, requestOptions) {
|
|
34
|
+
if (sessionStartImpl) {
|
|
35
|
+
const result = sessionStartImpl(body);
|
|
36
|
+
return Promise.resolve(result);
|
|
37
|
+
}
|
|
38
|
+
const apiClient = apiClientManager.clientOrThrow();
|
|
39
|
+
const $requestOptions = mergeRequestOptions({
|
|
40
|
+
tracer,
|
|
41
|
+
name: "sessions.start()",
|
|
42
|
+
icon: "sessions",
|
|
43
|
+
attributes: sessionAttributes(body.externalId ?? body.type, {
|
|
44
|
+
type: body.type,
|
|
45
|
+
...(body.externalId ? { externalId: body.externalId } : {}),
|
|
46
|
+
}),
|
|
47
|
+
}, requestOptions);
|
|
48
|
+
return apiClient.createSession(body, $requestOptions);
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Retrieve a Session by `friendlyId` (`session_*`) or user-supplied
|
|
52
|
+
* `externalId`. The server disambiguates via the `session_` prefix.
|
|
53
|
+
*/
|
|
54
|
+
function retrieveSession(sessionIdOrExternalId, requestOptions) {
|
|
55
|
+
const apiClient = apiClientManager.clientOrThrow();
|
|
56
|
+
const $requestOptions = mergeRequestOptions({
|
|
57
|
+
tracer,
|
|
58
|
+
name: "sessions.retrieve()",
|
|
59
|
+
icon: "sessions",
|
|
60
|
+
attributes: sessionAttributes(sessionIdOrExternalId),
|
|
61
|
+
}, requestOptions);
|
|
62
|
+
return apiClient.retrieveSession(sessionIdOrExternalId, $requestOptions);
|
|
63
|
+
}
|
|
64
|
+
/** Update mutable fields on a Session (tags, metadata, externalId). */
|
|
65
|
+
function updateSession(sessionIdOrExternalId, body, requestOptions) {
|
|
66
|
+
const apiClient = apiClientManager.clientOrThrow();
|
|
67
|
+
const $requestOptions = mergeRequestOptions({
|
|
68
|
+
tracer,
|
|
69
|
+
name: "sessions.update()",
|
|
70
|
+
icon: "sessions",
|
|
71
|
+
attributes: sessionAttributes(sessionIdOrExternalId),
|
|
72
|
+
}, requestOptions);
|
|
73
|
+
return apiClient.updateSession(sessionIdOrExternalId, body, $requestOptions);
|
|
74
|
+
}
|
|
75
|
+
/** Mark a Session as closed (terminal, idempotent). */
|
|
76
|
+
function closeSession(sessionIdOrExternalId, body, requestOptions) {
|
|
77
|
+
const apiClient = apiClientManager.clientOrThrow();
|
|
78
|
+
const $requestOptions = mergeRequestOptions({
|
|
79
|
+
tracer,
|
|
80
|
+
name: "sessions.close()",
|
|
81
|
+
icon: "sessions",
|
|
82
|
+
attributes: sessionAttributes(sessionIdOrExternalId, {
|
|
83
|
+
...(body?.reason ? { reason: body.reason } : {}),
|
|
84
|
+
}),
|
|
85
|
+
}, requestOptions);
|
|
86
|
+
return apiClient.closeSession(sessionIdOrExternalId, body, $requestOptions);
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* List Sessions in the current environment with filters + cursor pagination.
|
|
90
|
+
* Returns a {@link CursorPagePromise} so callers can iterate pages with
|
|
91
|
+
* `for await`.
|
|
92
|
+
*/
|
|
93
|
+
function listSessions(options, requestOptions) {
|
|
94
|
+
const apiClient = apiClientManager.clientOrThrow();
|
|
95
|
+
const $requestOptions = mergeRequestOptions({
|
|
96
|
+
tracer,
|
|
97
|
+
name: "sessions.list()",
|
|
98
|
+
icon: "sessions",
|
|
99
|
+
attributes: {
|
|
100
|
+
...(options?.type ? { type: toAttr(options.type) } : {}),
|
|
101
|
+
...(options?.tag ? { tag: toAttr(options.tag) } : {}),
|
|
102
|
+
...(options?.status ? { status: toAttr(options.status) } : {}),
|
|
103
|
+
...(options?.externalId ? { externalId: options.externalId } : {}),
|
|
104
|
+
},
|
|
105
|
+
}, requestOptions);
|
|
106
|
+
return apiClient.listSessions(options, $requestOptions);
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Open a lightweight handle to a Session's realtime channels. Does not
|
|
110
|
+
* perform a network call on its own — each channel method hits the
|
|
111
|
+
* corresponding realtime endpoint.
|
|
112
|
+
*/
|
|
113
|
+
function open(sessionIdOrExternalId) {
|
|
114
|
+
if (sessionOpenImpl)
|
|
115
|
+
return sessionOpenImpl(sessionIdOrExternalId);
|
|
116
|
+
return new SessionHandle(sessionIdOrExternalId);
|
|
117
|
+
}
|
|
118
|
+
export class SessionHandle {
|
|
119
|
+
id;
|
|
120
|
+
/**
|
|
121
|
+
* Producer-to-consumer channel: the task writes records; external
|
|
122
|
+
* clients read them. Mirrors `streams.define` — `append` / `pipe` /
|
|
123
|
+
* `writer` / `read`.
|
|
124
|
+
*/
|
|
125
|
+
out;
|
|
126
|
+
/**
|
|
127
|
+
* Consumer-to-producer channel: external clients call `.send()`; the
|
|
128
|
+
* task consumes via `.on` / `.once` / `.peek` / `.wait` /
|
|
129
|
+
* `.waitWithIdleTimeout`. Mirrors `streams.input` but keyed on the
|
|
130
|
+
* session so a conversation can survive across run boundaries.
|
|
131
|
+
*/
|
|
132
|
+
in;
|
|
133
|
+
constructor(id, overrides) {
|
|
134
|
+
this.id = id;
|
|
135
|
+
this.out = overrides?.out ?? new SessionOutputChannel(id);
|
|
136
|
+
this.in = overrides?.in ?? new SessionInputChannel(id);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* The `.out` side of a Session's bidirectional channel pair. Mirrors the
|
|
141
|
+
* consume-side of {@link streams.define}: `pipe` / `writer` / `append`
|
|
142
|
+
* for the task to produce records, `read` for external clients to
|
|
143
|
+
* consume via SSE. S2 credentials for direct writes are fetched
|
|
144
|
+
* internally by `pipe`/`writer` — there's no public `initialize()`.
|
|
145
|
+
*/
|
|
146
|
+
export class SessionOutputChannel {
|
|
147
|
+
sessionId;
|
|
148
|
+
// Cache of the in-flight / resolved `initializeSessionStream` PUT for
|
|
149
|
+
// this channel. Every `pipe()` / `writer()` call needs the same S2
|
|
150
|
+
// credentials, so we share a single promise instead of re-PUTing on
|
|
151
|
+
// every chunk. Hot-loop writers (per-chunk `chat.response.write` /
|
|
152
|
+
// direct `session.out.writer` calls) drop from N PUTs to 1 PUT for
|
|
153
|
+
// the lifetime of the channel. The S2 access token has a 1-day TTL
|
|
154
|
+
// server-side so reusing it across calls within a single run is safe.
|
|
155
|
+
// Evicts on failure (so the next call retries) and on `reset()`.
|
|
156
|
+
#initPromise;
|
|
157
|
+
constructor(sessionId) {
|
|
158
|
+
this.sessionId = sessionId;
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Drop the cached `initializeSessionStream` response. Surfaces for
|
|
162
|
+
* tests and lifecycle hooks that need the next write to re-mint S2
|
|
163
|
+
* credentials. The cache also self-evicts on `initializeSession`
|
|
164
|
+
* rejection, so callers don't need to invoke this on failures.
|
|
165
|
+
*
|
|
166
|
+
* @internal
|
|
167
|
+
*/
|
|
168
|
+
reset() {
|
|
169
|
+
this.#initPromise = undefined;
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Append a single record. Routes through {@link writer} internally so
|
|
173
|
+
* subscribers receive the same parsed-object shape as multi-record
|
|
174
|
+
* writes — the server-side append endpoint wraps the body in a string,
|
|
175
|
+
* which would give SSE consumers a JSON-string instead of an object.
|
|
176
|
+
* Mirrors how `streams.define.append` delegates to `streams.writer`.
|
|
177
|
+
*/
|
|
178
|
+
async append(value, options) {
|
|
179
|
+
const { waitUntilComplete } = this.writer({
|
|
180
|
+
...options,
|
|
181
|
+
spanName: "sessions.append()",
|
|
182
|
+
execute: ({ write }) => {
|
|
183
|
+
write(value);
|
|
184
|
+
},
|
|
185
|
+
});
|
|
186
|
+
await waitUntilComplete();
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Pipe an `AsyncIterable` / `ReadableStream` directly to S2. Fetches
|
|
190
|
+
* session S2 credentials internally and streams through
|
|
191
|
+
* {@link SessionStreamInstance}. Parallel to {@link streams.pipe} but
|
|
192
|
+
* session-scoped — no `target` option because the session is the target.
|
|
193
|
+
*/
|
|
194
|
+
pipe(value, options) {
|
|
195
|
+
return this.#pipeInternal(value, options, "sessions.pipe()");
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Mirror of {@link streams.writer}: runs `execute({ write, merge })`
|
|
199
|
+
* against an in-memory queue whose records are piped to S2. Returns
|
|
200
|
+
* `{ stream, waitUntilComplete }` so callers can observe the local
|
|
201
|
+
* stream and await completion. Span is collapsible via `options.spanName`
|
|
202
|
+
* / `options.collapsed`.
|
|
203
|
+
*/
|
|
204
|
+
writer(options) {
|
|
205
|
+
let controller;
|
|
206
|
+
const ongoingStreamPromises = [];
|
|
207
|
+
const stream = new ReadableStream({
|
|
208
|
+
start(controllerArg) {
|
|
209
|
+
controller = controllerArg;
|
|
210
|
+
},
|
|
211
|
+
});
|
|
212
|
+
const safeEnqueue = (data) => {
|
|
213
|
+
try {
|
|
214
|
+
controller.enqueue(data);
|
|
215
|
+
}
|
|
216
|
+
catch {
|
|
217
|
+
// Suppress errors when the stream has been closed.
|
|
218
|
+
}
|
|
219
|
+
};
|
|
220
|
+
try {
|
|
221
|
+
const result = options.execute({
|
|
222
|
+
write(part) {
|
|
223
|
+
safeEnqueue(part);
|
|
224
|
+
},
|
|
225
|
+
merge(streamArg) {
|
|
226
|
+
ongoingStreamPromises.push((async () => {
|
|
227
|
+
const reader = streamArg.getReader();
|
|
228
|
+
while (true) {
|
|
229
|
+
const { done, value } = await reader.read();
|
|
230
|
+
if (done)
|
|
231
|
+
break;
|
|
232
|
+
safeEnqueue(value);
|
|
233
|
+
}
|
|
234
|
+
})().catch((error) => {
|
|
235
|
+
console.error(error);
|
|
236
|
+
}));
|
|
237
|
+
},
|
|
238
|
+
});
|
|
239
|
+
if (result) {
|
|
240
|
+
ongoingStreamPromises.push(result.catch((error) => {
|
|
241
|
+
console.error(error);
|
|
242
|
+
}));
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
catch (error) {
|
|
246
|
+
console.error(error);
|
|
247
|
+
}
|
|
248
|
+
const waitForStreams = new Promise((resolve, reject) => {
|
|
249
|
+
(async () => {
|
|
250
|
+
while (ongoingStreamPromises.length > 0) {
|
|
251
|
+
await ongoingStreamPromises.shift();
|
|
252
|
+
}
|
|
253
|
+
resolve();
|
|
254
|
+
})().catch(reject);
|
|
255
|
+
});
|
|
256
|
+
waitForStreams.finally(() => {
|
|
257
|
+
try {
|
|
258
|
+
controller.close();
|
|
259
|
+
}
|
|
260
|
+
catch {
|
|
261
|
+
// Already closed.
|
|
262
|
+
}
|
|
263
|
+
});
|
|
264
|
+
return this.#pipeInternal(stream, options, options.spanName ?? "sessions.writer()");
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* Subscribe to SSE records on `.out`. Returns an async-iterable stream —
|
|
268
|
+
* auto-retry, Last-Event-ID resume, and abort propagation come from the
|
|
269
|
+
* shared {@link SSEStreamSubscription} plumbing used by run-scoped
|
|
270
|
+
* realtime streams.
|
|
271
|
+
*/
|
|
272
|
+
async read(options) {
|
|
273
|
+
const apiClient = apiClientManager.clientOrThrow();
|
|
274
|
+
return apiClient.subscribeToSessionStream(this.sessionId, "out", {
|
|
275
|
+
signal: options?.signal,
|
|
276
|
+
timeoutInSeconds: options?.timeoutInSeconds,
|
|
277
|
+
lastEventId: options?.lastEventId != null ? String(options.lastEventId) : undefined,
|
|
278
|
+
onPart: options?.onPart,
|
|
279
|
+
onControl: options?.onControl,
|
|
280
|
+
onComplete: options?.onComplete,
|
|
281
|
+
onError: options?.onError,
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
#pipeInternal(value, options, spanName) {
|
|
285
|
+
const apiClient = apiClientManager.clientOrThrow();
|
|
286
|
+
const collapsed = options?.collapsed;
|
|
287
|
+
const span = tracer.startSpan(spanName, {
|
|
288
|
+
attributes: {
|
|
289
|
+
session: this.sessionId,
|
|
290
|
+
io: "out",
|
|
291
|
+
[SemanticInternalAttributes.ENTITY_TYPE]: "session-stream",
|
|
292
|
+
[SemanticInternalAttributes.ENTITY_ID]: `${this.sessionId}:out`,
|
|
293
|
+
[SemanticInternalAttributes.STYLE_ICON]: "sessions",
|
|
294
|
+
...(collapsed ? { [SemanticInternalAttributes.COLLAPSED]: true } : {}),
|
|
295
|
+
...accessoryAttributes({
|
|
296
|
+
items: [{ text: `${this.sessionId}.out`, variant: "normal" }],
|
|
297
|
+
style: "codepath",
|
|
298
|
+
}),
|
|
299
|
+
},
|
|
300
|
+
});
|
|
301
|
+
const readableStreamSource = ensureReadableStream(value);
|
|
302
|
+
const abortController = new AbortController();
|
|
303
|
+
// `AbortSignal.any` lands in Node 20.3; the SDK still supports Node
|
|
304
|
+
// 18.20+. On older runtimes fall back to wiring `options.signal` into
|
|
305
|
+
// `abortController` manually so caller-driven cancellation propagates.
|
|
306
|
+
let combinedSignal = abortController.signal;
|
|
307
|
+
// Set in the Node 18 fallback path so the caller's `signal.addEventListener`
|
|
308
|
+
// registration can be cleared once the stream finishes. Without this, a
|
|
309
|
+
// long-lived caller signal (e.g. one reused across many `writer()` calls)
|
|
310
|
+
// accumulates listeners on every completed turn.
|
|
311
|
+
let removeCallerAbortListener;
|
|
312
|
+
if (options?.signal) {
|
|
313
|
+
if (typeof AbortSignal.any === "function") {
|
|
314
|
+
combinedSignal = AbortSignal.any([options.signal, abortController.signal]);
|
|
315
|
+
}
|
|
316
|
+
else {
|
|
317
|
+
const callerSignal = options.signal;
|
|
318
|
+
if (callerSignal.aborted) {
|
|
319
|
+
abortController.abort(callerSignal.reason);
|
|
320
|
+
}
|
|
321
|
+
else {
|
|
322
|
+
const onCallerAbort = () => abortController.abort(callerSignal.reason);
|
|
323
|
+
callerSignal.addEventListener("abort", onCallerAbort, { once: true });
|
|
324
|
+
removeCallerAbortListener = () => callerSignal.removeEventListener("abort", onCallerAbort);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
// Resolve the init promise eagerly so we can capture which one this
|
|
329
|
+
// writer uses for reactive invalidation below.
|
|
330
|
+
const writerInitPromise = (() => {
|
|
331
|
+
if (this.#initPromise) {
|
|
332
|
+
return this.#initPromise;
|
|
333
|
+
}
|
|
334
|
+
const fresh = apiClient.initializeSessionStream(this.sessionId, "out", options?.requestOptions);
|
|
335
|
+
this.#initPromise = fresh;
|
|
336
|
+
// Evict on failure so the next call retries instead of returning a
|
|
337
|
+
// poisoned cache entry forever.
|
|
338
|
+
fresh.catch((err) => {
|
|
339
|
+
if (this.#initPromise === fresh) {
|
|
340
|
+
this.#initPromise = undefined;
|
|
341
|
+
}
|
|
342
|
+
});
|
|
343
|
+
return fresh;
|
|
344
|
+
})();
|
|
345
|
+
try {
|
|
346
|
+
const instance = new SessionStreamInstance({
|
|
347
|
+
apiClient,
|
|
348
|
+
baseUrl: apiClientManager.baseURL ?? "",
|
|
349
|
+
sessionId: this.sessionId,
|
|
350
|
+
io: "out",
|
|
351
|
+
source: readableStreamSource,
|
|
352
|
+
signal: combinedSignal,
|
|
353
|
+
requestOptions: options?.requestOptions,
|
|
354
|
+
initializeSession: () => writerInitPromise,
|
|
355
|
+
});
|
|
356
|
+
// Single internal chain that handles span lifecycle AND reactive
|
|
357
|
+
// invalidation. On rejection we evict the cached init promise so
|
|
358
|
+
// the next pipe()/writer() re-PUTs and recovers (e.g. when a
|
|
359
|
+
// cached S2 access token expired mid-process). Compare by identity
|
|
360
|
+
// so a concurrent caller's fresh promise isn't accidentally cleared.
|
|
361
|
+
// Customer awaiters still observe the rejection via the returned
|
|
362
|
+
// `waitUntilComplete()`; this chain just keeps the cleanup path
|
|
363
|
+
// from surfacing as unhandled.
|
|
364
|
+
instance.wait().then(() => {
|
|
365
|
+
removeCallerAbortListener?.();
|
|
366
|
+
span.end();
|
|
367
|
+
}, () => {
|
|
368
|
+
removeCallerAbortListener?.();
|
|
369
|
+
if (this.#initPromise === writerInitPromise) {
|
|
370
|
+
this.#initPromise = undefined;
|
|
371
|
+
}
|
|
372
|
+
span.end();
|
|
373
|
+
});
|
|
374
|
+
return {
|
|
375
|
+
stream: instance.stream,
|
|
376
|
+
waitUntilComplete: async () => {
|
|
377
|
+
return instance.wait();
|
|
378
|
+
},
|
|
379
|
+
};
|
|
380
|
+
}
|
|
381
|
+
catch (error) {
|
|
382
|
+
removeCallerAbortListener?.();
|
|
383
|
+
if (error instanceof Error && error.name === "AbortError") {
|
|
384
|
+
span.end();
|
|
385
|
+
throw error;
|
|
386
|
+
}
|
|
387
|
+
if (error instanceof Error || typeof error === "string") {
|
|
388
|
+
span.recordException(error);
|
|
389
|
+
}
|
|
390
|
+
else {
|
|
391
|
+
span.recordException(String(error));
|
|
392
|
+
}
|
|
393
|
+
span.setStatus({ code: SpanStatusCode.ERROR });
|
|
394
|
+
span.end();
|
|
395
|
+
throw error;
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
/**
|
|
399
|
+
* Write a single Trigger control record to `.out`. The record carries a
|
|
400
|
+
* `trigger-control` header valued with `subtype` plus any sibling
|
|
401
|
+
* `extraHeaders`; the body is empty. Control records are filtered out of
|
|
402
|
+
* the consumer-facing chunk stream by the SDK transport — readers route
|
|
403
|
+
* them via the `onControl` callback instead.
|
|
404
|
+
*
|
|
405
|
+
* The returned `lastEventId` is the S2 seq_num of the written record,
|
|
406
|
+
* useful for trim chains (e.g. trim back to the previous turn-complete).
|
|
407
|
+
*/
|
|
408
|
+
async writeControl(subtype, extraHeaders) {
|
|
409
|
+
const apiClient = apiClientManager.clientOrThrow();
|
|
410
|
+
return writeSessionControlRecord(apiClient, this.sessionId, "out", subtype, extraHeaders);
|
|
411
|
+
}
|
|
412
|
+
/**
|
|
413
|
+
* Append an S2 `trim` command record to `.out`. Records with seq_num
|
|
414
|
+
* less than `earliestSeqNum` are eventually removed from the stream.
|
|
415
|
+
*
|
|
416
|
+
* Idempotent and monotonic at S2's layer (`max(existing, min(provided,
|
|
417
|
+
* current_tail))`) — backward trims are silently no-ops for deletion
|
|
418
|
+
* but still consume a seq_num. Used by `chat.agent`'s turn loop to
|
|
419
|
+
* keep `session.out` bounded to roughly one turn at steady state.
|
|
420
|
+
*/
|
|
421
|
+
async trimTo(earliestSeqNum) {
|
|
422
|
+
const apiClient = apiClientManager.clientOrThrow();
|
|
423
|
+
await trimSessionStream(apiClient, this.sessionId, earliestSeqNum);
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
/**
|
|
427
|
+
* The `.in` side of a Session's bidirectional channel pair. Mirrors
|
|
428
|
+
* {@link streams.input} — consumer-side primitives for the task
|
|
429
|
+
* (`on`/`once`/`peek`/`wait`/`waitWithIdleTimeout`) plus `send` for
|
|
430
|
+
* external clients. Keyed on the session rather than the run so a
|
|
431
|
+
* conversation can survive across run boundaries.
|
|
432
|
+
*/
|
|
433
|
+
export class SessionInputChannel {
|
|
434
|
+
sessionId;
|
|
435
|
+
constructor(sessionId) {
|
|
436
|
+
this.sessionId = sessionId;
|
|
437
|
+
}
|
|
438
|
+
/**
|
|
439
|
+
* Send a single record to the channel. Called by external clients
|
|
440
|
+
* (browser, server action, another task) producing input for the run.
|
|
441
|
+
* Matches {@link streams.input.send} but session-scoped — the session
|
|
442
|
+
* is the address, no `runId` required.
|
|
443
|
+
*/
|
|
444
|
+
async send(value, requestOptions) {
|
|
445
|
+
const apiClient = apiClientManager.clientOrThrow();
|
|
446
|
+
const body = typeof value === "string" ? value : JSON.stringify(value);
|
|
447
|
+
const $requestOptions = mergeRequestOptions({
|
|
448
|
+
tracer,
|
|
449
|
+
name: `sessions.open(${this.sessionId}).in.send()`,
|
|
450
|
+
icon: "sessions",
|
|
451
|
+
attributes: sessionAttributes(this.sessionId, { io: "in" }),
|
|
452
|
+
}, requestOptions);
|
|
453
|
+
await apiClient.appendToSessionStream(this.sessionId, "in", body, $requestOptions);
|
|
454
|
+
}
|
|
455
|
+
/**
|
|
456
|
+
* Register a handler that fires for every record landing on `.in`.
|
|
457
|
+
* Handlers are flushed with any buffered records on attach and cleaned
|
|
458
|
+
* up automatically when the task run completes. Returns `{ off }` to
|
|
459
|
+
* unsubscribe early.
|
|
460
|
+
*/
|
|
461
|
+
on(handler) {
|
|
462
|
+
return sessionStreams.on(this.sessionId, "in", handler);
|
|
463
|
+
}
|
|
464
|
+
/**
|
|
465
|
+
* Wait for the next record on `.in` without suspending the run.
|
|
466
|
+
* Returns `{ ok: true, output }` on arrival or `{ ok: false, error }`
|
|
467
|
+
* when the timeout fires. Chain `.unwrap()` to get the data directly.
|
|
468
|
+
*/
|
|
469
|
+
once(options) {
|
|
470
|
+
const ctx = taskContext.ctx;
|
|
471
|
+
const runId = ctx?.run.id;
|
|
472
|
+
const innerPromise = sessionStreams.once(this.sessionId, "in", options);
|
|
473
|
+
return new InputStreamOncePromise((resolve, reject) => {
|
|
474
|
+
tracer
|
|
475
|
+
.startActiveSpan(options?.spanName ?? `sessions.open(${this.sessionId}).in.once()`, async () => {
|
|
476
|
+
const result = await innerPromise;
|
|
477
|
+
resolve(result);
|
|
478
|
+
}, {
|
|
479
|
+
attributes: {
|
|
480
|
+
[SemanticInternalAttributes.STYLE_ICON]: "sessions",
|
|
481
|
+
[SemanticInternalAttributes.ENTITY_TYPE]: "session-stream",
|
|
482
|
+
...(runId
|
|
483
|
+
? { [SemanticInternalAttributes.ENTITY_ID]: `${runId}:${this.sessionId}:in` }
|
|
484
|
+
: {}),
|
|
485
|
+
session: this.sessionId,
|
|
486
|
+
io: "in",
|
|
487
|
+
...accessoryAttributes({
|
|
488
|
+
items: [{ text: `${this.sessionId}.in`, variant: "normal" }],
|
|
489
|
+
style: "codepath",
|
|
490
|
+
}),
|
|
491
|
+
},
|
|
492
|
+
})
|
|
493
|
+
.catch(reject);
|
|
494
|
+
});
|
|
495
|
+
}
|
|
496
|
+
/** Non-blocking peek at the head of the `.in` buffer. */
|
|
497
|
+
peek() {
|
|
498
|
+
return sessionStreams.peek(this.sessionId, "in");
|
|
499
|
+
}
|
|
500
|
+
/**
|
|
501
|
+
* The highest S2 sequence number of any record this channel has
|
|
502
|
+
* delivered to a `once()` / `wait()` consumer (or had shifted off its
|
|
503
|
+
* buffer into one). Distinct from "last received" — buffered-but-not-
|
|
504
|
+
* yet-consumed records don't count.
|
|
505
|
+
*
|
|
506
|
+
* Used by `chat.agent` to persist the `.in` resume cursor on each
|
|
507
|
+
* `turn-complete` control record, so the next worker boot can subscribe
|
|
508
|
+
* past already-processed user messages.
|
|
509
|
+
*/
|
|
510
|
+
lastDispatchedSeqNum() {
|
|
511
|
+
return sessionStreams.lastDispatchedSeqNum(this.sessionId, "in");
|
|
512
|
+
}
|
|
513
|
+
/**
|
|
514
|
+
* Suspend the current run until the next record arrives on `.in`.
|
|
515
|
+
* Unlike {@link once}, `wait()` frees compute while blocked — the
|
|
516
|
+
* run-engine waitpoint holds the run until the session append handler
|
|
517
|
+
* fires it. Only callable from inside `task.run()`.
|
|
518
|
+
*/
|
|
519
|
+
wait(options) {
|
|
520
|
+
return new ManualWaitpointPromise(async (resolve, reject) => {
|
|
521
|
+
try {
|
|
522
|
+
const ctx = taskContext.ctx;
|
|
523
|
+
if (!ctx) {
|
|
524
|
+
throw new Error("session.in.wait() can only be used from inside a task.run()");
|
|
525
|
+
}
|
|
526
|
+
const apiClient = apiClientManager.clientOrThrow();
|
|
527
|
+
const response = await apiClient.createSessionStreamWaitpoint(ctx.run.id, {
|
|
528
|
+
session: this.sessionId,
|
|
529
|
+
io: "in",
|
|
530
|
+
timeout: options?.timeout,
|
|
531
|
+
idempotencyKey: options?.idempotencyKey,
|
|
532
|
+
idempotencyKeyTTL: options?.idempotencyKeyTTL,
|
|
533
|
+
tags: options?.tags,
|
|
534
|
+
lastSeqNum: sessionStreams.lastSeqNum(this.sessionId, "in"),
|
|
535
|
+
});
|
|
536
|
+
const result = await tracer.startActiveSpan(options?.spanName ?? `sessions.open(${this.sessionId}).in.wait()`, async (span) => {
|
|
537
|
+
const waitResponse = await apiClient.waitForWaitpointToken({
|
|
538
|
+
runFriendlyId: ctx.run.id,
|
|
539
|
+
waitpointFriendlyId: response.waitpointId,
|
|
540
|
+
});
|
|
541
|
+
if (!waitResponse.success) {
|
|
542
|
+
throw new Error("Failed to block on session stream waitpoint");
|
|
543
|
+
}
|
|
544
|
+
// Drop the SSE tail + buffer before suspending so the record
|
|
545
|
+
// delivered via the waitpoint path isn't re-buffered on resume.
|
|
546
|
+
sessionStreams.disconnectStream(this.sessionId, "in");
|
|
547
|
+
const waitResult = await runtime.waitUntil(response.waitpointId);
|
|
548
|
+
const data = waitResult.output !== undefined
|
|
549
|
+
? await conditionallyImportAndParsePacket({
|
|
550
|
+
data: waitResult.output,
|
|
551
|
+
dataType: waitResult.outputType ?? "application/json",
|
|
552
|
+
}, apiClient)
|
|
553
|
+
: undefined;
|
|
554
|
+
if (waitResult.ok) {
|
|
555
|
+
// Advance the seq counter so the SSE tail doesn't replay the
|
|
556
|
+
// record that was consumed via the waitpoint.
|
|
557
|
+
const prevSeq = sessionStreams.lastSeqNum(this.sessionId, "in");
|
|
558
|
+
const nextSeq = (prevSeq ?? -1) + 1;
|
|
559
|
+
sessionStreams.setLastSeqNum(this.sessionId, "in", nextSeq);
|
|
560
|
+
return { ok: true, output: data };
|
|
561
|
+
}
|
|
562
|
+
else {
|
|
563
|
+
const error = new WaitpointTimeoutError(data?.message ?? "Timed out");
|
|
564
|
+
span.recordException(error);
|
|
565
|
+
span.setStatus({ code: SpanStatusCode.ERROR });
|
|
566
|
+
return { ok: false, error };
|
|
567
|
+
}
|
|
568
|
+
}, {
|
|
569
|
+
attributes: {
|
|
570
|
+
[SemanticInternalAttributes.STYLE_ICON]: "wait",
|
|
571
|
+
[SemanticInternalAttributes.ENTITY_TYPE]: "waitpoint",
|
|
572
|
+
[SemanticInternalAttributes.ENTITY_ID]: response.waitpointId,
|
|
573
|
+
session: this.sessionId,
|
|
574
|
+
io: "in",
|
|
575
|
+
...accessoryAttributes({
|
|
576
|
+
items: [{ text: `${this.sessionId}.in`, variant: "normal" }],
|
|
577
|
+
style: "codepath",
|
|
578
|
+
}),
|
|
579
|
+
},
|
|
580
|
+
});
|
|
581
|
+
resolve(result);
|
|
582
|
+
}
|
|
583
|
+
catch (error) {
|
|
584
|
+
reject(error);
|
|
585
|
+
}
|
|
586
|
+
});
|
|
587
|
+
}
|
|
588
|
+
/**
|
|
589
|
+
* Wait for a record with an idle-then-suspend strategy. Keeps the run
|
|
590
|
+
* active (using compute) for `idleTimeoutInSeconds`, then suspends via
|
|
591
|
+
* {@link wait} if nothing arrives. If a record arrives during the idle
|
|
592
|
+
* phase the run responds without suspending.
|
|
593
|
+
*/
|
|
594
|
+
async waitWithIdleTimeout(options) {
|
|
595
|
+
const self = this;
|
|
596
|
+
const spanName = options.spanName ?? `sessions.open(${this.sessionId}).in.waitWithIdleTimeout()`;
|
|
597
|
+
return tracer.startActiveSpan(spanName, async (span) => {
|
|
598
|
+
if (options.idleTimeoutInSeconds > 0) {
|
|
599
|
+
const warm = await sessionStreams.once(self.sessionId, "in", {
|
|
600
|
+
timeoutMs: options.idleTimeoutInSeconds * 1000,
|
|
601
|
+
});
|
|
602
|
+
if (warm.ok) {
|
|
603
|
+
span.setAttribute("wait.resolved", "idle");
|
|
604
|
+
return { ok: true, output: warm.output };
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
if (options.skipSuspend) {
|
|
608
|
+
// Match the cold-phase `self.wait()` result shape below so any
|
|
609
|
+
// caller that does `throw result.error` gets a real error
|
|
610
|
+
// instead of `undefined`.
|
|
611
|
+
span.setAttribute("wait.resolved", "skipped");
|
|
612
|
+
return {
|
|
613
|
+
ok: false,
|
|
614
|
+
error: new WaitpointTimeoutError("Idle timeout elapsed and skipSuspend is set"),
|
|
615
|
+
};
|
|
616
|
+
}
|
|
617
|
+
if (options.onSuspend) {
|
|
618
|
+
await options.onSuspend();
|
|
619
|
+
}
|
|
620
|
+
span.setAttribute("wait.resolved", "suspended");
|
|
621
|
+
const waitResult = await self.wait({
|
|
622
|
+
timeout: options.timeout,
|
|
623
|
+
spanName: "suspended",
|
|
624
|
+
});
|
|
625
|
+
if (waitResult.ok && options.onResume) {
|
|
626
|
+
await options.onResume();
|
|
627
|
+
}
|
|
628
|
+
return waitResult;
|
|
629
|
+
}, {
|
|
630
|
+
attributes: {
|
|
631
|
+
[SemanticInternalAttributes.STYLE_ICON]: "sessions",
|
|
632
|
+
session: self.sessionId,
|
|
633
|
+
io: "in",
|
|
634
|
+
...accessoryAttributes({
|
|
635
|
+
items: [{ text: `${self.sessionId}.in`, variant: "normal" }],
|
|
636
|
+
style: "codepath",
|
|
637
|
+
}),
|
|
638
|
+
},
|
|
639
|
+
});
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
// ─── helpers ────────────────────────────────────────────────────────
|
|
643
|
+
function sessionAttributes(id, extra) {
|
|
644
|
+
return {
|
|
645
|
+
session: id,
|
|
646
|
+
...(extra ?? {}),
|
|
647
|
+
...accessoryAttributes({
|
|
648
|
+
items: [{ text: id, variant: "normal" }],
|
|
649
|
+
style: "codepath",
|
|
650
|
+
}),
|
|
651
|
+
};
|
|
652
|
+
}
|
|
653
|
+
function toAttr(value) {
|
|
654
|
+
return Array.isArray(value) ? value.join(",") : value;
|
|
655
|
+
}
|
|
656
|
+
//# sourceMappingURL=sessions.js.map
|