@arbidocs/sdk 0.4.167 → 0.4.168

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.
@@ -0,0 +1,293 @@
1
+ import { T as TokenBudgetContext, d as ArtifactEvent, M as MailDraftSSEEvent, P as ProbeAnswerEvent, U as UserInputRequestEvent } from './sse-types-BbR7JG5o.js';
2
+ import '@arbidocs/client';
3
+
4
+ /**
5
+ * Assistant streams in the CURRENT wire protocol (SSE v3), written as frames.
6
+ *
7
+ * Every test that replays an assistant turn builds its body here: the e2e specs (through
8
+ * `page.route` or the staged stream in `e2e/staged-sse.ts`), the chat parity scenarios and any
9
+ * consumer's own tests (`@arbidocs/sdk/testing`). Bodies written out by hand rot silently the
10
+ * moment the protocol moves: the client ignores frames it no longer knows, renders an empty trace,
11
+ * and the test fails on a timeout that says nothing about why. That is what happened to
12
+ * `arbi.agent_step`, retired by SSE protocol v3 (#2079). With the frames in one place the next
13
+ * protocol change is one edit.
14
+ *
15
+ * ## The protocol, as clients consume it
16
+ *
17
+ * - `arbi.user_message` — the user's own message, stored. There is no optimistic insert on send:
18
+ * a body without it renders the conversation with the prompt MISSING.
19
+ * - `response.created` — carries `response.id`, the assistant message's external id. This creates
20
+ * the assistant row (`streamSSE` maps it to `onStreamStart`).
21
+ * - `response.output_item.done` — one settled Responses output item. Only these build the trace:
22
+ * * `function_call` → a tool step,
23
+ * * `reasoning` → a lifecycle step (goal / planning / goal check / interjection …),
24
+ * * `message` → body text, and a trace row ONLY when it declares `arbi.step` (the answer
25
+ * does; narration doesn't).
26
+ * Display metadata rides under `item.arbi`; nesting is by `arbi.parent_id`, which names a parent
27
+ * ITEM ID (the client derives the display index itself).
28
+ * - `response.output_text.delta` — streams body text, keyed by `item_id`.
29
+ * - `response.completed` — ends the stream and carries the authoritative final message metadata.
30
+ *
31
+ * Step rows render as `agent-step-{index}`, where index is assigned by the client in first-seen
32
+ * order starting at 1 — so the Nth distinct item id is `agent-step-N`.
33
+ */
34
+
35
+ /** ARBI's trace extension on an output item (`ArbiItemMeta`). */
36
+ type Arbi = Record<string, unknown>;
37
+ /** One Responses output item. */
38
+ type Item = Record<string, unknown>;
39
+ /** One `event:`/`data:` frame. */
40
+ declare function frame(event: string, data: object): string;
41
+ /** A tool call — the workhorse trace row. `arbi.detail[0].tool` names the tool. */
42
+ declare function toolItem(id: string, name: string, arbi?: Arbi): Item;
43
+ /**
44
+ * A lifecycle step (goal, plan, goal check, interjection, skill ack). `arbi.step` declares which —
45
+ * the backend states it rather than the client sniffing labels. `summary` is the item's extended
46
+ * text (a plan's checklist, an interjection's message), which the client surfaces as the step's
47
+ * content.
48
+ */
49
+ declare function reasoningItem(id: string, arbi?: Arbi, summary?: string): Item;
50
+ /**
51
+ * The answer. A `message` item that declares `step: "answering"` — so it is both the body text AND
52
+ * the closing trace row that carries the answering model and that call's token cost.
53
+ */
54
+ declare function answerItem(id: string, text: string, arbi?: Arbi): Item;
55
+ /** A working note the server settles as Narration: it leaves the body and lives on in the trace. */
56
+ declare function narrationItem(id: string, text: string): Item;
57
+ interface UserMessageFrameOptions {
58
+ content: string;
59
+ conversationId: string;
60
+ externalId?: string;
61
+ /** `queued` for a follow-up the server holds behind a running turn. */
62
+ status?: 'completed' | 'queued';
63
+ parentMessageId?: string | null;
64
+ createdBy?: string;
65
+ /** ISO time the server stored it (default now). */
66
+ at?: string;
67
+ }
68
+ /** The user message as the server stored it (the body of `arbi.user_message`). */
69
+ declare function userMessage(opts: UserMessageFrameOptions): {
70
+ parent_message_ext_id?: string | null | undefined;
71
+ type: string;
72
+ role: string;
73
+ content: string;
74
+ shared: boolean;
75
+ tokens: number;
76
+ status: "completed" | "queued";
77
+ external_id: string;
78
+ created_at: string;
79
+ created_by_ext_id: string;
80
+ conversation_ext_id: string;
81
+ };
82
+ /**
83
+ * The `arbi.user_message` echo — the user's own message coming back from the backend. NOT
84
+ * optional dressing: clients add the user row only from this event.
85
+ */
86
+ declare function userMessageFrame(opts: UserMessageFrameOptions): string;
87
+ /** A follow-up the server accepted while a turn runs (`arbi.message_queued`). */
88
+ declare function messageQueuedFrame(opts: {
89
+ responseId: string;
90
+ input: string;
91
+ previousResponseId?: string | null;
92
+ user: UserMessageFrameOptions;
93
+ }): string;
94
+ /** `response.created`: the assistant message exists on the server under `id`. */
95
+ declare function responseCreatedFrame(id: string, t?: number, at?: string): string;
96
+ /** Body text streaming into message item `itemId`. */
97
+ declare function textDeltaFrame(itemId: string, delta: string): string;
98
+ /** One settled output item (`response.output_item.done`). */
99
+ declare function itemDoneFrame(item: Item, opts?: {
100
+ index?: number;
101
+ t?: number;
102
+ }): string;
103
+ interface CompletedFrameOptions {
104
+ /** External id of the assistant message (`response.id`). */
105
+ assistantId: string;
106
+ conversationId: string;
107
+ /** The authoritative answer. */
108
+ content: string;
109
+ /** Stored tools of the answer (citations, trace…), as `message_metadata.tools`. */
110
+ tools?: Record<string, unknown>;
111
+ /** Backend-suggested follow-up (SUGGESTED_QUERIES). */
112
+ suggestedQuery?: string;
113
+ /** The per-turn token budget snapshot. */
114
+ context?: TokenBudgetContext;
115
+ t?: number;
116
+ status?: 'completed' | 'failed';
117
+ /** ISO time the answer was stored (default now). */
118
+ at?: string;
119
+ }
120
+ /** `response.completed`: ends the turn and carries the stored answer. */
121
+ declare function completedFrame(opts: CompletedFrameOptions): string;
122
+ /** `response.failed`: the server failed the answer. */
123
+ declare function failedFrame(message: string): string;
124
+ /** `arbi.error`, e.g. a mid-stream `QUOTA_EXCEEDED`. */
125
+ declare function arbiErrorFrame(opts: {
126
+ code: string;
127
+ message: string;
128
+ spend?: number;
129
+ budget?: number;
130
+ projectExtId?: string;
131
+ }): string;
132
+ /** `arbi.artifact`: a document the assistant is writing (or has written). */
133
+ declare function artifactFrame(event: Omit<ArtifactEvent, 'type'>): string;
134
+ /** `arbi.mail_draft`: the assistant saved an email draft. */
135
+ declare function mailDraftFrame(event: Omit<MailDraftSSEEvent, 'type'>): string;
136
+ /** `arbi.deferred_interjection`: steering that arrived after the agent committed to its answer. */
137
+ declare function deferredInterjectionFrame(message: string): string;
138
+ /** `arbi.agent_control_state`: the server confirmed a pause or resume. */
139
+ declare function agentControlStateFrame(state: 'paused' | 'running'): string;
140
+ /** `arbi.probe_answer`: an out-of-band status answer. */
141
+ declare function probeAnswerFrame(event: Omit<ProbeAnswerEvent, 'type'>): string;
142
+ /** `arbi.user_input_request`: the agent asks the person a question mid-turn. */
143
+ declare function userInputRequestFrame(event: Omit<UserInputRequestEvent, 'type'>): string;
144
+ interface StreamOptions {
145
+ /** External id of the assistant message (`response.id`). */
146
+ assistantId: string;
147
+ /** Trace items, in emission order. Their order fixes the `agent-step-N` numbering. */
148
+ items: Item[];
149
+ /** Conversation the message belongs to — required if `userContent` is set. */
150
+ conversationId?: string;
151
+ /** When set, the stream opens with an `arbi.user_message` echo (the user row). */
152
+ userContent?: string;
153
+ /** External id for that user row. */
154
+ userMessageId?: string;
155
+ /** Final answer text, set authoritatively by `response.completed`. */
156
+ answer?: string;
157
+ /** Seconds-since-start for the first item; each later item advances by this much. */
158
+ step?: number;
159
+ }
160
+ /**
161
+ * A complete assistant stream. Each item is stamped with an increasing `t` (seconds since stream
162
+ * start) because the trace derives step durations from the gap between consecutive stamps.
163
+ */
164
+ declare function agentStream({ assistantId, items, conversationId, userContent, userMessageId, answer, step, }: StreamOptions): string;
165
+
166
+ /**
167
+ * A `fetch` that answers the assistant endpoints with streams a test owns.
168
+ *
169
+ * The client under test is the real one: its request builder, the SDK's SSE parser and whatever
170
+ * engine sits on top. Only the network is replaced. Each assistant request opens a staged stream
171
+ * the test feeds frame by frame (`push`), ends (`close`), cuts (`drop`) or that the client aborts;
172
+ * so a turn can be paused anywhere and inspected, exactly as `e2e/staged-sse.ts` does in the
173
+ * browser. A test can also queue a plain HTTP reply (a 402, a 409) for the next request.
174
+ *
175
+ * Every request is recorded with its parsed JSON body, so request bodies can be asserted (or
176
+ * snapshotted) as well as what the client did with the answer.
177
+ */
178
+ /** One request the client made. */
179
+ interface RecordedRequest {
180
+ method: string;
181
+ /** The path and query, without origin. */
182
+ path: string;
183
+ body: unknown;
184
+ }
185
+ /** A stream the test feeds. */
186
+ interface StagedStream {
187
+ /** The request that opened it. */
188
+ readonly request: RecordedRequest;
189
+ /** Feed one or more SSE frames. */
190
+ push(...frames: string[]): void;
191
+ /** End the stream normally. */
192
+ close(): void;
193
+ /** Cut the connection mid-stream (the client sees a network error). */
194
+ drop(error?: Error): void;
195
+ /** Whether the client aborted it, or it was closed or dropped. */
196
+ readonly ended: boolean;
197
+ }
198
+ /** A plain HTTP answer for the next matching request. */
199
+ interface HttpReply {
200
+ status: number;
201
+ body?: unknown;
202
+ }
203
+ interface SseFake {
204
+ /** Install as `globalThis.fetch` (or pass to the client). */
205
+ fetch: typeof fetch;
206
+ /** Every request, in order. */
207
+ readonly requests: RecordedRequest[];
208
+ /** Streams opened by assistant requests (queries and run attaches), in order. */
209
+ readonly streams: StagedStream[];
210
+ /** The newest stream that has not ended. */
211
+ current(): StagedStream | undefined;
212
+ /** Answer the next `POST /v1/assistant/query` with a plain HTTP reply instead of a stream. */
213
+ replyNextQuery(reply: HttpReply): void;
214
+ /** Answer the next attach (`GET /v1/assistant/run/{id}/stream`) with a stream (default 404). */
215
+ streamNextAttach(): void;
216
+ /** Answer other requests (default: 200 `{}`), e.g. cancel and interject. */
217
+ setFallback(handler: (request: RecordedRequest) => HttpReply): void;
218
+ }
219
+ declare function createSseFake(): SseFake;
220
+
221
+ /**
222
+ * Chat parity scenarios: assistant turns as the server streams them, plus what the person does
223
+ * while they run.
224
+ *
225
+ * Each scenario is data, so any chat engine can replay it: the runner sends the prompts through
226
+ * the engine under test, feeds the frames through `createSseFake`, presses Stop when told, and
227
+ * snapshots what the engine left behind (the thread, the list, the session) next to the
228
+ * scenario's golden file. While the engine moves between layers (#1720 A1–A9) the runner changes
229
+ * and the golden files must not.
230
+ */
231
+
232
+ /** One thing that happens during a scenario, in order. */
233
+ type ChatStep =
234
+ /** The person types `send` into the composer and sends it. */
235
+ {
236
+ send: string;
237
+ }
238
+ /** The server sends frames on stream `stream` (index in opening order; default: the newest open). */
239
+ | {
240
+ push: string[];
241
+ stream?: number;
242
+ }
243
+ /** The server ends a stream normally. */
244
+ | {
245
+ close: true;
246
+ stream?: number;
247
+ }
248
+ /** The connection drops mid-stream. */
249
+ | {
250
+ drop: true;
251
+ stream?: number;
252
+ }
253
+ /** The person presses Stop. */
254
+ | {
255
+ stop: true;
256
+ }
257
+ /** The next assistant request gets a plain HTTP reply. */
258
+ | {
259
+ replyNextQuery: HttpReply;
260
+ }
261
+ /** Time passes (ms), on the fake clock. */
262
+ | {
263
+ wait: number;
264
+ }
265
+ /** Record the engine's state under this label (the final state is always recorded). */
266
+ | {
267
+ snapshot: string;
268
+ };
269
+ /** A message already in the thread when the scenario starts. */
270
+ interface SeedMessage {
271
+ id: string;
272
+ role: 'user' | 'assistant';
273
+ content: string;
274
+ parent: string | null;
275
+ }
276
+ interface ChatScenario {
277
+ name: string;
278
+ description: string;
279
+ /** Start in this existing conversation; otherwise a new chat. */
280
+ seed?: {
281
+ conversationId: string;
282
+ history: SeedMessage[];
283
+ };
284
+ /** Documents and facts added to the chat before the first send. */
285
+ context?: {
286
+ documents?: string[];
287
+ facts?: string[];
288
+ };
289
+ steps: ChatStep[];
290
+ }
291
+ declare const CHAT_SCENARIOS: ChatScenario[];
292
+
293
+ export { type Arbi, CHAT_SCENARIOS, type ChatScenario, type ChatStep, type CompletedFrameOptions, type HttpReply, type Item, type RecordedRequest, type SeedMessage, type SseFake, type StagedStream, type StreamOptions, type UserMessageFrameOptions, agentControlStateFrame, agentStream, answerItem, arbiErrorFrame, artifactFrame, completedFrame, createSseFake, deferredInterjectionFrame, failedFrame, frame, itemDoneFrame, mailDraftFrame, messageQueuedFrame, narrationItem, probeAnswerFrame, reasoningItem, responseCreatedFrame, textDeltaFrame, toolItem, userInputRequestFrame, userMessage, userMessageFrame };