@lunora/svelte 1.0.0-alpha.35 → 1.0.0-alpha.37
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/index.d.mts +640 -2
- package/dist/index.d.ts +640 -2
- package/dist/index.mjs +6 -0
- package/dist/packem_shared/agent-KLsxO_Uw.mjs +48 -0
- package/dist/packem_shared/agentChat-CrFM8IGA.mjs +122 -0
- package/dist/packem_shared/agentState-DBXc_3RM.mjs +15 -0
- package/dist/packem_shared/agentToolEvents-qYM1CdTV.mjs +60 -0
- package/dist/packem_shared/stream-BebZbIaD.mjs +68 -0
- package/dist/packem_shared/voiceAgent-Cxib7eCK.mjs +405 -0
- package/package.json +2 -2
package/dist/index.d.mts
CHANGED
|
@@ -1,9 +1,448 @@
|
|
|
1
|
-
import { LunoraClient, User, ConnectionStatus, Preloaded,
|
|
1
|
+
import { FunctionReference, LunoraClient, User, ConnectionStatus, Preloaded, ReturnOf, ArgsOf, MutationCallOptions, MutatorHandle, SubscriptionErrorCallback } from '@lunora/client';
|
|
2
2
|
export type { ArgsOf, ConnectionStatus, FunctionReference, LunoraClient, MutationCallOptions, MutatorHandle, MutatorTransaction, Preloaded, ReturnOf } from '@lunora/client';
|
|
3
3
|
import { Readable } from 'svelte/store';
|
|
4
4
|
import { PaginationStatus } from '@lunora/client/pagination';
|
|
5
5
|
import { RateLimitStatus, RateLimitConfig } from '@lunora/ratelimit';
|
|
6
6
|
/**
|
|
7
|
+
* The lifecycle status stored on an agent thread. Client-safe mirror of
|
|
8
|
+
* `@lunora/agent`'s `AgentThreadStatus` — re-declared here (rather than imported)
|
|
9
|
+
* so this Svelte entry never pulls in the server-only `@lunora/agent` module graph
|
|
10
|
+
* (the adapter stays Svelte + `@lunora/client` only). Keep in sync with
|
|
11
|
+
* `packages/agent/src/types.ts`.
|
|
12
|
+
*/
|
|
13
|
+
type AgentThreadStatus = "awaiting_input" | "cancelled" | "error" | "idle" | "running";
|
|
14
|
+
/**
|
|
15
|
+
* The live thread record surfaced by the `agents:agentThread` query. A structural
|
|
16
|
+
* subset of the persisted thread row — every field beyond `status` is optional so
|
|
17
|
+
* the shape stays forgiving as the server schema grows. Keep in sync with the
|
|
18
|
+
* `agent_threads` table in `packages/agent/src/component.ts`.
|
|
19
|
+
*/
|
|
20
|
+
interface AgentThreadRecord {
|
|
21
|
+
createdAt?: number;
|
|
22
|
+
/** The failure message when `status === "error"`. */
|
|
23
|
+
error?: string;
|
|
24
|
+
/** The workflow instance id of the in-flight run — the handle `cancel` targets. */
|
|
25
|
+
instanceId?: string;
|
|
26
|
+
messageCount?: number;
|
|
27
|
+
/** The verified thread owner, when the run was started with one. */
|
|
28
|
+
owner?: string;
|
|
29
|
+
status: AgentThreadStatus;
|
|
30
|
+
title?: string;
|
|
31
|
+
updatedAt?: number;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* The `agents.agentThread` reference the handle subscribes to for live thread
|
|
35
|
+
* state (status + the in-flight `instanceId`). A structural subset of the
|
|
36
|
+
* generated `api.agents` surface, so the whole generated `api` object is
|
|
37
|
+
* assignable.
|
|
38
|
+
*/
|
|
39
|
+
interface AgentApi {
|
|
40
|
+
agents: {
|
|
41
|
+
agentThread: FunctionReference<"query", {
|
|
42
|
+
key: string;
|
|
43
|
+
}, Record<string, unknown> | undefined>;
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
interface AgentOptions {
|
|
47
|
+
/** The generated `api` — its `agents.agentThread` query drives live thread state. */
|
|
48
|
+
api: AgentApi;
|
|
49
|
+
/**
|
|
50
|
+
* Optional app mutation over the agent's cancel path
|
|
51
|
+
* (`ctx.agents[name].cancel(id)`). Called with `{ instanceId, threadKey }`.
|
|
52
|
+
* When omitted (or no run is in flight) {@link AgentHandle.cancel} is a no-op.
|
|
53
|
+
*/
|
|
54
|
+
cancel?: FunctionReference<"mutation">;
|
|
55
|
+
/**
|
|
56
|
+
* The app mutation that starts (or continues) a run — a thin wrapper over
|
|
57
|
+
* `ctx.agents[name].run(...)`. Called with `{ threadKey, input }` merged with
|
|
58
|
+
* {@link AgentOptions.runArgs} and the per-call args.
|
|
59
|
+
*/
|
|
60
|
+
run: FunctionReference<"mutation">;
|
|
61
|
+
/** Extra args merged into every `run` call (e.g. an `owner` or `title`). */
|
|
62
|
+
runArgs?: Record<string, unknown>;
|
|
63
|
+
/** The thread to observe and drive. */
|
|
64
|
+
threadKey: string;
|
|
65
|
+
}
|
|
66
|
+
interface AgentHandle {
|
|
67
|
+
/**
|
|
68
|
+
* Terminate the in-flight run and mark its thread `"cancelled"`. Resolves as a
|
|
69
|
+
* no-op when no `cancel` mutation was supplied or no run is in flight.
|
|
70
|
+
*/
|
|
71
|
+
cancel: () => Promise<void>;
|
|
72
|
+
/** `true` while a `run` invocation is in flight. Read with `$pending`. */
|
|
73
|
+
pending: Readable<boolean>;
|
|
74
|
+
/** Start (or continue) a run with a user message; extra args merge over `runArgs`. */
|
|
75
|
+
run: (input: string, args?: Record<string, unknown>) => Promise<void>;
|
|
76
|
+
/** The live thread status, or `undefined` before the thread exists. Read with `$status`. */
|
|
77
|
+
status: Readable<AgentThreadStatus | undefined>;
|
|
78
|
+
/**
|
|
79
|
+
* Stop the live thread subscription. Call in `onDestroy`
|
|
80
|
+
* (`onDestroy(handle.teardown)`).
|
|
81
|
+
*/
|
|
82
|
+
teardown: () => void;
|
|
83
|
+
/** The live thread record (status, `instanceId`, …), or `undefined` before it exists. Read with `$thread`. */
|
|
84
|
+
thread: Readable<AgentThreadRecord | undefined>;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* A placeholder mutation reference so {@link mutation} is called unconditionally
|
|
88
|
+
* even when the caller supplies no `cancel` mutation. Its `__lunoraRef` is never
|
|
89
|
+
* dispatched — `cancel()` short-circuits before invoking it unless a real
|
|
90
|
+
* reference was provided.
|
|
91
|
+
*/
|
|
92
|
+
/**
|
|
93
|
+
* A thin agent handle: live thread `status` plus `run` / `cancel`, without the
|
|
94
|
+
* chat message surface — the Svelte counterpart to React's `useAgent`,
|
|
95
|
+
* re-expressed as stores you read with `$`. Composes `client.subscribe` for live
|
|
96
|
+
* thread state and {@link mutation} for the run/cancel writes. For the full
|
|
97
|
+
* conversation surface (durable history + approvals) use `agentChat`.
|
|
98
|
+
*
|
|
99
|
+
* `run` and `cancel` stay generic over the app-defined mutations that wrap
|
|
100
|
+
* `ctx.agents[name].run` / `.cancel`, so the handle hard-codes no function names
|
|
101
|
+
* beyond the `agents:*` surface. The subscription opens eagerly on the call and
|
|
102
|
+
* runs until {@link AgentHandle.teardown} — call `onDestroy(handle.teardown)`.
|
|
103
|
+
*
|
|
104
|
+
* Pass `client` explicitly, or omit it to resolve the ambient client published by
|
|
105
|
+
* `setLunoraClient`.
|
|
106
|
+
*/
|
|
107
|
+
declare function agent(options: AgentOptions): AgentHandle;
|
|
108
|
+
declare function agent(client: LunoraClient, options: AgentOptions): AgentHandle;
|
|
109
|
+
/**
|
|
110
|
+
* One persisted (or optimistic) thread message, as `agents:agentMessages`
|
|
111
|
+
* surfaces it. Client-safe mirror of `@lunora/agent`'s `AgentMessageRow` —
|
|
112
|
+
* re-declared here (rather than imported) so this Svelte entry never pulls in the
|
|
113
|
+
* server-only `@lunora/agent` module graph. Keep in sync with the
|
|
114
|
+
* `agent_messages` table in `packages/agent/src/component.ts`.
|
|
115
|
+
*/
|
|
116
|
+
interface AgentChatMessage {
|
|
117
|
+
content: string;
|
|
118
|
+
createdAt?: number;
|
|
119
|
+
/**
|
|
120
|
+
* `true` for a client-side optimistic user message not yet acknowledged by
|
|
121
|
+
* the server. Cleared once the durable history carries the matching user turn.
|
|
122
|
+
*/
|
|
123
|
+
optimistic?: boolean;
|
|
124
|
+
role: "assistant" | "system" | "tool" | "user";
|
|
125
|
+
seq: number;
|
|
126
|
+
/** Approval lifecycle marker on a human-in-the-loop tool message. */
|
|
127
|
+
status?: "approved" | "awaiting_approval" | "rejected";
|
|
128
|
+
toolCallId?: string;
|
|
129
|
+
toolCalls?: ReadonlyArray<{
|
|
130
|
+
id: string;
|
|
131
|
+
input: unknown;
|
|
132
|
+
name: string;
|
|
133
|
+
}>;
|
|
134
|
+
toolName?: string;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* A live token delta streamed while a turn is generating. Client-safe mirror of
|
|
138
|
+
* `@lunora/agent`'s `AgentTokenDelta`. Ephemeral — deltas feed
|
|
139
|
+
* {@link AgentChatHandle.streamingText} live and are never replayed; the
|
|
140
|
+
* persisted assistant message stays the single source of truth.
|
|
141
|
+
*/
|
|
142
|
+
interface AgentTokenDelta {
|
|
143
|
+
/** Discriminates the token arm of {@link AgentLiveEvent}; unset on the wire (token is the default). */
|
|
144
|
+
kind?: "token";
|
|
145
|
+
/** The incremental text chunk the model just produced. */
|
|
146
|
+
text: string;
|
|
147
|
+
/** The thread this delta belongs to. */
|
|
148
|
+
threadKey: string;
|
|
149
|
+
/** The zero-based index of the turn producing the delta. */
|
|
150
|
+
turn: number;
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* A live tool-progress event streamed via `ctx.reportProgress(...)`. Client-safe
|
|
154
|
+
* mirror of `@lunora/agent`'s `AgentProgressEvent`. Ephemeral and `toolCallId`-keyed;
|
|
155
|
+
* surfaced by `agentToolEvents`, ignored by {@link AgentChatHandle.streamingText}.
|
|
156
|
+
*/
|
|
157
|
+
interface AgentProgressEvent {
|
|
158
|
+
/** The arbitrary, JSON-serializable payload the tool reported. */
|
|
159
|
+
data: unknown;
|
|
160
|
+
/** Discriminates the progress arm of {@link AgentLiveEvent}. */
|
|
161
|
+
kind: "progress";
|
|
162
|
+
/** The thread this event belongs to. */
|
|
163
|
+
threadKey: string;
|
|
164
|
+
/** The tool call this progress belongs to. */
|
|
165
|
+
toolCallId: string;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* A single event on the agent's live-only channel — a streamed token delta or a
|
|
169
|
+
* tool progress event. Client-safe mirror of `@lunora/agent`'s `AgentLiveEvent`.
|
|
170
|
+
* Discriminate on `kind` (`"progress"` for the progress arm; token deltas leave
|
|
171
|
+
* it unset).
|
|
172
|
+
*/
|
|
173
|
+
type AgentLiveEvent = AgentProgressEvent | AgentTokenDelta;
|
|
174
|
+
/** The `agents:agentMessages` reference — live durable thread history. */
|
|
175
|
+
type AgentMessagesReference$1 = FunctionReference<"query", {
|
|
176
|
+
key: string;
|
|
177
|
+
limit?: number;
|
|
178
|
+
}, ReadonlyArray<Record<string, unknown>>>;
|
|
179
|
+
/** The `agents:agentResolveApproval` reference — resolves a human-in-the-loop tool approval. */
|
|
180
|
+
type AgentApprovalReference = FunctionReference<"mutation", {
|
|
181
|
+
decision: "approve" | "reject";
|
|
182
|
+
instanceId: string;
|
|
183
|
+
note?: string;
|
|
184
|
+
threadKey: string;
|
|
185
|
+
toolCallId: string;
|
|
186
|
+
}, {
|
|
187
|
+
resolved: boolean;
|
|
188
|
+
}>;
|
|
189
|
+
/** The `agents:agentThread` reference — live thread status + in-flight `instanceId`. */
|
|
190
|
+
type AgentThreadReference = FunctionReference<"query", {
|
|
191
|
+
key: string;
|
|
192
|
+
}, Record<string, unknown> | undefined>;
|
|
193
|
+
/**
|
|
194
|
+
* An app stream reference that tees the agent's in-flight live events, keyed by
|
|
195
|
+
* thread. Carries token deltas and — since `ctx.reportProgress` rides the same
|
|
196
|
+
* sink — tool progress events; this handle consumes only the token arm.
|
|
197
|
+
*/
|
|
198
|
+
type AgentTokenStreamReference = FunctionReference<"stream", {
|
|
199
|
+
key: string;
|
|
200
|
+
}, AgentLiveEvent>;
|
|
201
|
+
/**
|
|
202
|
+
* The `agents.*` reference surface the chat handle reads. A structural subset of
|
|
203
|
+
* the generated `api.agents`, so the whole generated `api` object is assignable.
|
|
204
|
+
*/
|
|
205
|
+
interface AgentChatApi {
|
|
206
|
+
agents: {
|
|
207
|
+
agentMessages: AgentMessagesReference$1;
|
|
208
|
+
agentResolveApproval: AgentApprovalReference;
|
|
209
|
+
agentThread: AgentThreadReference;
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
interface AgentChatOptions {
|
|
213
|
+
/** The generated `api` — its `agents.*` surface provides history, thread state, and approval resolution. */
|
|
214
|
+
api: AgentChatApi;
|
|
215
|
+
/**
|
|
216
|
+
* Optional app mutation over the agent's cancel path
|
|
217
|
+
* (`ctx.agents[name].cancel(id)`). Called with `{ instanceId, threadKey }`.
|
|
218
|
+
* When omitted (or no run is in flight) {@link AgentChatHandle.cancel} is a
|
|
219
|
+
* no-op.
|
|
220
|
+
*/
|
|
221
|
+
cancel?: FunctionReference<"mutation">;
|
|
222
|
+
/** History depth forwarded to `agents:agentMessages`. */
|
|
223
|
+
limit?: number;
|
|
224
|
+
/**
|
|
225
|
+
* The app mutation that starts (or continues) a run — a thin wrapper over
|
|
226
|
+
* `ctx.agents[name].run(...)`. Called with `{ threadKey, input }` merged with
|
|
227
|
+
* {@link AgentChatOptions.sendArgs} and the per-call args.
|
|
228
|
+
*/
|
|
229
|
+
send: FunctionReference<"mutation">;
|
|
230
|
+
/** Extra args merged into every `send` call (e.g. an `owner` or `title`). */
|
|
231
|
+
sendArgs?: Record<string, unknown>;
|
|
232
|
+
/**
|
|
233
|
+
* Optional live token-delta stream — an app stream function that tees the
|
|
234
|
+
* agent's in-flight deltas. When omitted {@link AgentChatHandle.streamingText}
|
|
235
|
+
* stays empty and the UI updates message-by-message from durable history.
|
|
236
|
+
*/
|
|
237
|
+
stream?: AgentTokenStreamReference;
|
|
238
|
+
/** The thread to observe and continue. */
|
|
239
|
+
threadKey: string;
|
|
240
|
+
}
|
|
241
|
+
interface AgentChatHandle {
|
|
242
|
+
/** Approve a paused human-in-the-loop tool call (optionally with a note). */
|
|
243
|
+
approve: (toolCallId: string, note?: string) => Promise<void>;
|
|
244
|
+
/**
|
|
245
|
+
* Terminate the in-flight run and mark its thread `"cancelled"`. Resolves as a
|
|
246
|
+
* no-op when no `cancel` mutation was supplied or no run is in flight.
|
|
247
|
+
*/
|
|
248
|
+
cancel: () => Promise<void>;
|
|
249
|
+
/** Durable thread history (oldest first) plus any un-acknowledged optimistic user turns. Read with `$messages`. */
|
|
250
|
+
messages: Readable<ReadonlyArray<AgentChatMessage>>;
|
|
251
|
+
/** Reject a paused human-in-the-loop tool call (optionally with a reason). */
|
|
252
|
+
reject: (toolCallId: string, note?: string) => Promise<void>;
|
|
253
|
+
/** Start (or continue) a run with a user message; extra args merge over `sendArgs`. Appends an optimistic user turn. */
|
|
254
|
+
send: (input: string, args?: Record<string, unknown>) => Promise<void>;
|
|
255
|
+
/** The live thread status, or `undefined` before the thread exists. Read with `$status`. */
|
|
256
|
+
status: Readable<AgentThreadStatus | undefined>;
|
|
257
|
+
/**
|
|
258
|
+
* The in-flight turn's streamed text — live-only, `""` once the turn persists
|
|
259
|
+
* to `messages`. Populated when a `stream` reference is supplied (via the
|
|
260
|
+
* {@link stream} primitive); with no reference it stays `""` and the UI advances
|
|
261
|
+
* message-by-message from durable history. Read with `$streamingText`.
|
|
262
|
+
*/
|
|
263
|
+
streamingText: Readable<string>;
|
|
264
|
+
/**
|
|
265
|
+
* Stop the live history + thread subscriptions (and the token stream, if any).
|
|
266
|
+
* Call in `onDestroy` (`onDestroy(handle.teardown)`).
|
|
267
|
+
*/
|
|
268
|
+
teardown: () => void;
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* A first-class agent chat surface: live durable history + in-flight token
|
|
272
|
+
* streaming + the send / approve / reject / cancel writes, keyed by `threadKey` —
|
|
273
|
+
* the Svelte counterpart to React's `useAgentChat`, re-expressed as stores you
|
|
274
|
+
* read with `$`.
|
|
275
|
+
*
|
|
276
|
+
* It composes the existing primitives rather than adding transport:
|
|
277
|
+
* `client.subscribe(api.agents.agentMessages)` for durable history,
|
|
278
|
+
* `client.subscribe(api.agents.agentThread)` for live status + the in-flight
|
|
279
|
+
* `instanceId`, {@link stream} over an app token stream for in-flight deltas, and
|
|
280
|
+
* {@link mutation} for the writes (`api.agents.agentResolveApproval` for approvals;
|
|
281
|
+
* app-defined wrappers for `send`/`cancel`). Only the `agents:*` surface is
|
|
282
|
+
* hard-coded — `send`/`cancel`/`stream` stay generic references.
|
|
283
|
+
*
|
|
284
|
+
* A `send` optimistically appends the user turn so it renders immediately; the
|
|
285
|
+
* optimistic row clears once the durable history carries the acknowledged turn.
|
|
286
|
+
* `streamingText` is live-only: it holds the current turn's streamed text and
|
|
287
|
+
* empties as soon as that turn's assistant message lands in `messages` (the
|
|
288
|
+
* persisted message is the source of truth); with no `stream` reference it stays
|
|
289
|
+
* `""` and the UI advances message-by-message from durable history. The
|
|
290
|
+
* subscriptions (and the token stream, if any) open eagerly and run until
|
|
291
|
+
* {@link AgentChatHandle.teardown} — call `onDestroy(handle.teardown)`.
|
|
292
|
+
*
|
|
293
|
+
* Pass `client` explicitly, or omit it to resolve the ambient client published by
|
|
294
|
+
* `setLunoraClient`.
|
|
295
|
+
*/
|
|
296
|
+
declare function agentChat(options: AgentChatOptions): AgentChatHandle;
|
|
297
|
+
declare function agentChat(client: LunoraClient, options: AgentChatOptions): AgentChatHandle;
|
|
298
|
+
/**
|
|
299
|
+
* The `agents.agentState` reference the handle subscribes to for the thread's live
|
|
300
|
+
* synced state. A structural subset of the generated `api.agents` surface (like
|
|
301
|
+
* `AgentApi` for `agentThread`), so the whole generated `api` object is
|
|
302
|
+
* assignable. Client-safe: no `@lunora/agent` import — the per-agent state type is
|
|
303
|
+
* mirrored by the handle's generic `T`, since codegen pins the reference return as
|
|
304
|
+
* an optional record (it never evaluates agent config).
|
|
305
|
+
*/
|
|
306
|
+
interface AgentStateApi {
|
|
307
|
+
agents: {
|
|
308
|
+
agentState: FunctionReference<"query", {
|
|
309
|
+
key: string;
|
|
310
|
+
}, Record<string, unknown> | undefined>;
|
|
311
|
+
};
|
|
312
|
+
}
|
|
313
|
+
interface AgentStateOptions {
|
|
314
|
+
/** The generated `api` — its `agents.agentState` query drives live thread state. */
|
|
315
|
+
api: AgentStateApi;
|
|
316
|
+
/** The thread whose synced state to observe. */
|
|
317
|
+
threadKey: string;
|
|
318
|
+
}
|
|
319
|
+
interface AgentStateHandle<T> {
|
|
320
|
+
/** Svelte readable store of the subscription error, if the live channel reported one. Read with `$error`. */
|
|
321
|
+
error: Readable<Error | undefined>;
|
|
322
|
+
/** Svelte readable store of the live synced state, or `undefined` before it is seeded/first pushed. Read with `$state`. */
|
|
323
|
+
state: Readable<T | undefined>;
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
* Subscribe to an agent thread's synced state — the `setState`-style value a tool
|
|
327
|
+
* writes with `ctx.setState(...)`, seeded by `defineAgent({ initialState })`. A
|
|
328
|
+
* thin wrapper over {@link subscription} against `api.agents.agentState`: the
|
|
329
|
+
* server pushes a fresh frame whenever the state changes (the dedicated query's
|
|
330
|
+
* per-socket JSON memo suppresses no-op pushes on unrelated thread writes), so
|
|
331
|
+
* `state` updates only on a real `setState`. The Svelte counterpart to React's
|
|
332
|
+
* `useAgentState`, re-expressed as stores you read with `$`.
|
|
333
|
+
*
|
|
334
|
+
* Generic over the app's state shape (`agentState<SupportState>(...)`, itself a
|
|
335
|
+
* record) — the reference is typed as an optional record because codegen cannot
|
|
336
|
+
* see the per-agent state type; the generic casts to `T`. The `state`/`error`
|
|
337
|
+
* stores are lazy, so the subscription opens on the first subscriber to `state`
|
|
338
|
+
* and tears down when the last one leaves — there is no `teardown` to call.
|
|
339
|
+
*
|
|
340
|
+
* Pass `client` explicitly, or omit it to resolve the ambient client published by
|
|
341
|
+
* `setLunoraClient`.
|
|
342
|
+
*/
|
|
343
|
+
declare function agentState<T extends Record<string, unknown> = Record<string, unknown>>(options: AgentStateOptions): AgentStateHandle<T>;
|
|
344
|
+
declare function agentState<T extends Record<string, unknown> = Record<string, unknown>>(client: LunoraClient, options: AgentStateOptions): AgentStateHandle<T>;
|
|
345
|
+
/** The `agents:agentMessages` reference — live durable thread history. */
|
|
346
|
+
type AgentMessagesReference = FunctionReference<"query", {
|
|
347
|
+
key: string;
|
|
348
|
+
limit?: number;
|
|
349
|
+
}, ReadonlyArray<Record<string, unknown>>>;
|
|
350
|
+
/**
|
|
351
|
+
* An app stream reference that tees the agent's in-flight live events, keyed by
|
|
352
|
+
* thread. Carries token deltas and tool progress events; this handle consumes only
|
|
353
|
+
* the progress arm (`kind === "progress"`).
|
|
354
|
+
*/
|
|
355
|
+
type AgentLiveStreamReference = FunctionReference<"stream", {
|
|
356
|
+
key: string;
|
|
357
|
+
}, AgentLiveEvent>;
|
|
358
|
+
/**
|
|
359
|
+
* The `agents.*` reference surface the tool-events handle reads. A structural
|
|
360
|
+
* subset of the generated `api.agents`, so the whole generated `api` object is
|
|
361
|
+
* assignable.
|
|
362
|
+
*/
|
|
363
|
+
interface AgentToolEventsApi {
|
|
364
|
+
agents: {
|
|
365
|
+
agentMessages: AgentMessagesReference;
|
|
366
|
+
};
|
|
367
|
+
}
|
|
368
|
+
interface AgentToolEventsOptions {
|
|
369
|
+
/** The generated `api` — its `agents.agentMessages` query provides the durable tool lifecycle. */
|
|
370
|
+
api: AgentToolEventsApi;
|
|
371
|
+
/** History depth forwarded to `agents:agentMessages`. */
|
|
372
|
+
limit?: number;
|
|
373
|
+
/**
|
|
374
|
+
* Optional live event stream — the same app stream function `agentChat` uses.
|
|
375
|
+
* When supplied, ephemeral `ctx.reportProgress(...)` events for the thread are
|
|
376
|
+
* surfaced as `{ type: "progress" }` entries; when omitted only the durable
|
|
377
|
+
* lifecycle (call / result / awaiting-approval) is returned.
|
|
378
|
+
*/
|
|
379
|
+
stream?: AgentLiveStreamReference;
|
|
380
|
+
/** The thread whose tool activity to observe. */
|
|
381
|
+
threadKey: string;
|
|
382
|
+
}
|
|
383
|
+
/**
|
|
384
|
+
* A single tool-lifecycle event for a thread. The durable arms
|
|
385
|
+
* (`call`/`result`/`awaiting-approval`) are derived from `agents:agentMessages`
|
|
386
|
+
* and carry the persisted `seq`; the ephemeral `progress` arm comes live off the
|
|
387
|
+
* stream and has no `seq`. Discriminate on `type`.
|
|
388
|
+
*/
|
|
389
|
+
type AgentToolEvent = {
|
|
390
|
+
data: unknown;
|
|
391
|
+
toolCallId: string;
|
|
392
|
+
type: "progress";
|
|
393
|
+
} | {
|
|
394
|
+
input: unknown;
|
|
395
|
+
seq: number;
|
|
396
|
+
toolCallId: string;
|
|
397
|
+
toolName: string;
|
|
398
|
+
type: "call";
|
|
399
|
+
} | {
|
|
400
|
+
output: string;
|
|
401
|
+
seq: number;
|
|
402
|
+
status?: "approved" | "rejected";
|
|
403
|
+
toolCallId?: string;
|
|
404
|
+
toolName?: string;
|
|
405
|
+
type: "result";
|
|
406
|
+
} | {
|
|
407
|
+
seq: number;
|
|
408
|
+
toolCallId?: string;
|
|
409
|
+
toolName?: string;
|
|
410
|
+
type: "awaiting-approval";
|
|
411
|
+
};
|
|
412
|
+
interface AgentToolEventsHandle {
|
|
413
|
+
/**
|
|
414
|
+
* The thread's tool events: the durable lifecycle (oldest first, by `seq`)
|
|
415
|
+
* followed by any in-flight ephemeral progress events, recomputed from the live
|
|
416
|
+
* subscription + stream. Read with `$events`. With no `stream` reference only
|
|
417
|
+
* the durable lifecycle is surfaced. Treat as derived, not identity-stable.
|
|
418
|
+
*/
|
|
419
|
+
events: Readable<ReadonlyArray<AgentToolEvent>>;
|
|
420
|
+
}
|
|
421
|
+
/**
|
|
422
|
+
* A focused view of a thread's tool activity: tool calls, their results,
|
|
423
|
+
* human-in-the-loop approval pauses, and live `ctx.reportProgress(...)` events —
|
|
424
|
+
* without the full chat message surface. The Svelte counterpart to React's
|
|
425
|
+
* `useAgentToolEvents`, re-expressed as a readable store you read with `$`.
|
|
426
|
+
*
|
|
427
|
+
* It composes the existing primitives rather than adding transport:
|
|
428
|
+
* {@link subscription} over `api.agents.agentMessages` for the durable lifecycle
|
|
429
|
+
* and {@link stream} over the optional app event stream for ephemeral progress,
|
|
430
|
+
* combined through `derived`. Progress events are live-only (the durable path never
|
|
431
|
+
* emits them): they ride the same sink as token deltas and are surfaced here,
|
|
432
|
+
* correlated to their tool call by `toolCallId`. For the conversational surface
|
|
433
|
+
* (messages + streaming text + approvals) use `agentChat`; this handle is the
|
|
434
|
+
* tool-observability slice.
|
|
435
|
+
*
|
|
436
|
+
* The underlying subscription and stream are lazy — they open when `events` gains
|
|
437
|
+
* its first subscriber and tear down when the last one leaves — so there is no
|
|
438
|
+
* `teardown` to call (unlike the write-bearing `agentChat`).
|
|
439
|
+
*
|
|
440
|
+
* Pass `client` explicitly, or omit it to resolve the ambient client published by
|
|
441
|
+
* `setLunoraClient`.
|
|
442
|
+
*/
|
|
443
|
+
declare function agentToolEvents(options: AgentToolEventsOptions): AgentToolEventsHandle;
|
|
444
|
+
declare function agentToolEvents(client: LunoraClient, options: AgentToolEventsOptions): AgentToolEventsHandle;
|
|
445
|
+
/**
|
|
7
446
|
* Publish a {@link LunoraClient} on the Svelte component context so that
|
|
8
447
|
* descendant components can read it with {@link getLunoraClient} (or implicitly,
|
|
9
448
|
* via the default-client lookups inside `query`/`mutation`/`hydratePreloaded`).
|
|
@@ -387,6 +826,46 @@ interface RateLimitHandle {
|
|
|
387
826
|
* interval (`onDestroy(handle.teardown)`).
|
|
388
827
|
*/
|
|
389
828
|
declare const rateLimit: (config: RateLimitConfig, options?: RateLimitOptions) => RateLimitHandle;
|
|
829
|
+
/** The lifecycle of a stream the store is observing. */
|
|
830
|
+
type StreamStatus = "complete" | "error" | "idle" | "streaming";
|
|
831
|
+
interface StreamStoreOptions {
|
|
832
|
+
/** Forwarded to `client.stream()` — caps the in-flight chunk buffer. */
|
|
833
|
+
maxBuffer?: number;
|
|
834
|
+
shardKey?: string;
|
|
835
|
+
}
|
|
836
|
+
interface StreamHandle<T> {
|
|
837
|
+
/** Force-cancel the stream and resolve the iterator. Safe to call multiple times. */
|
|
838
|
+
cancel: () => void;
|
|
839
|
+
/** Svelte readable store of the chunks the server has pushed so far, in arrival order. */
|
|
840
|
+
chunks: Readable<ReadonlyArray<T>>;
|
|
841
|
+
/** Svelte readable store of the last stream error (`undefined` when healthy). */
|
|
842
|
+
error: Readable<Error | undefined>;
|
|
843
|
+
/** Svelte readable store of the stream lifecycle status. */
|
|
844
|
+
status: Readable<StreamStatus>;
|
|
845
|
+
/**
|
|
846
|
+
* Stop the stream and release the iterator. Call in `onDestroy`
|
|
847
|
+
* (`onDestroy(handle.teardown)`) when you consume `chunks` eagerly; when you
|
|
848
|
+
* read `chunks` with `$` the store tears itself down as the last subscriber
|
|
849
|
+
* leaves.
|
|
850
|
+
*/
|
|
851
|
+
teardown: () => void;
|
|
852
|
+
}
|
|
853
|
+
/**
|
|
854
|
+
* Open a streaming query and expose its chunks, lifecycle status, and last error
|
|
855
|
+
* as Svelte readable stores. The `chunks` store is lazy: the stream opens on the
|
|
856
|
+
* first subscriber to `chunks` and is cancelled when the last one leaves (its
|
|
857
|
+
* chunks reset on the next open). `status` and `error` mirror that same stream.
|
|
858
|
+
*
|
|
859
|
+
* Passing `"skip"` as `args` keeps the stores connected but the stream dormant
|
|
860
|
+
* (`chunks` stays empty, `status` stays `"idle"`). The Svelte counterpart to
|
|
861
|
+
* React's `useStream`, re-expressed as stores you read with `$`.
|
|
862
|
+
*
|
|
863
|
+
* Pass an explicit `client` as the first argument to bypass the ambient context
|
|
864
|
+
* (useful in tests), or omit it to resolve the client published by
|
|
865
|
+
* `setLunoraClient`.
|
|
866
|
+
*/
|
|
867
|
+
declare function stream<F extends FunctionReference<"stream">>(function_: F, args: ArgsOf<F> | "skip", options?: StreamStoreOptions): StreamHandle<ReturnOf<F>>;
|
|
868
|
+
declare function stream<F extends FunctionReference<"stream">>(client: LunoraClient, function_: F, args: ArgsOf<F> | "skip", options?: StreamStoreOptions): StreamHandle<ReturnOf<F>>;
|
|
390
869
|
interface SubscriptionStoreOptions {
|
|
391
870
|
onError?: (error: Error) => void;
|
|
392
871
|
shardKey?: string;
|
|
@@ -409,4 +888,163 @@ interface SubscriptionHandle<T> {
|
|
|
409
888
|
*/
|
|
410
889
|
declare function subscription<F extends FunctionReference>(function_: F, args: ArgsOf<F> | "skip", options?: SubscriptionStoreOptions): SubscriptionHandle<ReturnOf<F>>;
|
|
411
890
|
declare function subscription<F extends FunctionReference>(client: LunoraClient, function_: F, args: ArgsOf<F> | "skip", options?: SubscriptionStoreOptions): SubscriptionHandle<ReturnOf<F>>;
|
|
412
|
-
|
|
891
|
+
/**
|
|
892
|
+
* Browser Web Audio subsystems for `useVoiceAgent` — the default microphone
|
|
893
|
+
* capture and speaker playback implementations injected into the composable via
|
|
894
|
+
* its `createMicrophone` / `createSpeaker` seams. Kept in a sibling module so the
|
|
895
|
+
* heavy Web Audio graph (and its structural DOM typings) stays isolated from the
|
|
896
|
+
* composable's transport + reactive-state logic and remains mockable in a
|
|
897
|
+
* non-browser test env.
|
|
898
|
+
*/
|
|
899
|
+
/**
|
|
900
|
+
* The negotiated audio format the voice DO streams back. Mirrors
|
|
901
|
+
* `@lunora/agent`'s `VoiceServerFrame` `ready.audioFormat` — re-declared (not
|
|
902
|
+
* imported) so this Svelte package never pulls in the server-only `@lunora/agent`
|
|
903
|
+
* module graph.
|
|
904
|
+
*/
|
|
905
|
+
type VoiceAudioFormat = "mp3" | "wav";
|
|
906
|
+
/** Captures microphone audio and reports level / turn boundaries back to the composable. */
|
|
907
|
+
interface VoiceMicrophone {
|
|
908
|
+
/** Mute/unmute the mic without tearing down the capture graph. */
|
|
909
|
+
setMuted: (muted: boolean) => void;
|
|
910
|
+
/** Stop capture and release the media stream + audio graph. */
|
|
911
|
+
stop: () => void;
|
|
912
|
+
}
|
|
913
|
+
/** Plays the server's streamed audio chunks and supports a mid-utterance barge-in. */
|
|
914
|
+
interface VoiceSpeaker {
|
|
915
|
+
/** Queue a decoded audio chunk for gap-minimized playback. */
|
|
916
|
+
enqueue: (audio: Uint8Array) => void;
|
|
917
|
+
/** Drop everything queued and stop the current chunk (barge-in). */
|
|
918
|
+
interrupt: () => void;
|
|
919
|
+
/** Release the playback audio context. */
|
|
920
|
+
stop: () => void;
|
|
921
|
+
}
|
|
922
|
+
/** Config passed to a {@link CreateMicrophone} factory. */
|
|
923
|
+
interface MicrophoneConfig {
|
|
924
|
+
/** The consecutive above-threshold chunk count that counts as a barge-in. */
|
|
925
|
+
interruptChunks: number;
|
|
926
|
+
/** RMS above which the user is considered to be barging in while the agent speaks. */
|
|
927
|
+
interruptThreshold: number;
|
|
928
|
+
/** `true` while `status === "speaking"` — gates barge-in detection. */
|
|
929
|
+
isSpeaking: () => boolean;
|
|
930
|
+
/** One 16 kHz mono 16-bit little-endian PCM frame captured from the mic. */
|
|
931
|
+
onAudio: (pcm: Uint8Array) => void;
|
|
932
|
+
/** A barge-in was detected (RMS spike while the agent is speaking). */
|
|
933
|
+
onInterrupt: () => void;
|
|
934
|
+
/** The current input RMS (0–1), for a level meter. */
|
|
935
|
+
onLevel: (rms: number) => void;
|
|
936
|
+
/** A spoken utterance ended (speech followed by `silenceDurationMs` of silence). */
|
|
937
|
+
onSilence: () => void;
|
|
938
|
+
/** Milliseconds of sub-threshold audio (after speech) that closes an utterance. */
|
|
939
|
+
silenceDurationMs: number;
|
|
940
|
+
/** RMS below which audio counts as silence. */
|
|
941
|
+
silenceThreshold: number;
|
|
942
|
+
}
|
|
943
|
+
type CreateMicrophone = (config: MicrophoneConfig) => Promise<VoiceMicrophone>;
|
|
944
|
+
type CreateSpeaker = (config: {
|
|
945
|
+
audioFormat: VoiceAudioFormat;
|
|
946
|
+
}) => VoiceSpeaker;
|
|
947
|
+
/**
|
|
948
|
+
* The default browser microphone: `getUserMedia` → a Web Audio `ScriptProcessor`
|
|
949
|
+
* that tees 16 kHz PCM frames, tracks input RMS, auto-commits an utterance after
|
|
950
|
+
* a silence gap, and flags a barge-in while the agent is speaking.
|
|
951
|
+
*/
|
|
952
|
+
/**
|
|
953
|
+
* The `agents.<name>Voice` reference codegen emits for a voice-enabled agent — a
|
|
954
|
+
* live, WS-backed session keyed by `threadKey`. A structural subset of the
|
|
955
|
+
* generated member, so passing `api.agents.<name>Voice` type-checks.
|
|
956
|
+
*/
|
|
957
|
+
type VoiceReference = FunctionReference<"stream", {
|
|
958
|
+
threadKey: string;
|
|
959
|
+
}, Record<string, unknown>>;
|
|
960
|
+
/** The lifecycle of a voice call, mirrored to the UI. */
|
|
961
|
+
type VoiceStatus = "idle" | "listening" | "speaking" | "thinking";
|
|
962
|
+
/** A minimal structural subset of the DOM `WebSocket` the handle drives. */
|
|
963
|
+
interface VoiceSocket {
|
|
964
|
+
binaryType: string;
|
|
965
|
+
close: () => void;
|
|
966
|
+
onclose: ((event: unknown) => void) | null;
|
|
967
|
+
onerror: ((event: unknown) => void) | null;
|
|
968
|
+
onmessage: ((event: {
|
|
969
|
+
data: unknown;
|
|
970
|
+
}) => void) | null;
|
|
971
|
+
onopen: ((event: unknown) => void) | null;
|
|
972
|
+
readonly readyState: number;
|
|
973
|
+
send: (data: ArrayBufferView | ArrayBufferLike | string) => void;
|
|
974
|
+
}
|
|
975
|
+
type CreateSocket = (url: string) => VoiceSocket;
|
|
976
|
+
interface VoiceAgentOptions {
|
|
977
|
+
/**
|
|
978
|
+
* Advanced/test seam: build the microphone capture subsystem. Defaults to a
|
|
979
|
+
* `getUserMedia` + Web Audio implementation. Injected wholesale so the Web
|
|
980
|
+
* Audio graph stays isolated (and mockable in a non-browser test env).
|
|
981
|
+
*/
|
|
982
|
+
createMicrophone?: CreateMicrophone;
|
|
983
|
+
/** Advanced/test seam: open the transport. Defaults to `new WebSocket(url)`. */
|
|
984
|
+
createSocket?: CreateSocket;
|
|
985
|
+
/** Advanced/test seam: build the audio playback subsystem. Defaults to a Web Audio implementation. */
|
|
986
|
+
createSpeaker?: CreateSpeaker;
|
|
987
|
+
/** Consecutive above-`interruptThreshold` chunks that trigger a barge-in. Default `3`. */
|
|
988
|
+
interruptChunks?: number;
|
|
989
|
+
/** Input RMS above which the user is treated as barging in while the agent speaks. Default `0.15`. */
|
|
990
|
+
interruptThreshold?: number;
|
|
991
|
+
/** Milliseconds of silence (after speech) that auto-commits an utterance. Default `1200`. */
|
|
992
|
+
silenceDurationMs?: number;
|
|
993
|
+
/** Input RMS below which audio counts as silence. Default `0.01`. */
|
|
994
|
+
silenceThreshold?: number;
|
|
995
|
+
/** The thread to converse on — shared with the agent's text turns. */
|
|
996
|
+
threadKey: string;
|
|
997
|
+
/** The generated `api.agents.<name>Voice` reference — identifies the voice DO endpoint. */
|
|
998
|
+
voice: VoiceReference;
|
|
999
|
+
}
|
|
1000
|
+
interface VoiceAgentHandle {
|
|
1001
|
+
/** Svelte readable store of the current input RMS (0–1) — drive a mic level meter. Read with `$audioLevel`. */
|
|
1002
|
+
audioLevel: Readable<number>;
|
|
1003
|
+
/** Svelte readable store: `true` once the WS `ready` handshake completed. Read with `$connected`. */
|
|
1004
|
+
connected: Readable<boolean>;
|
|
1005
|
+
/** Tear down the call: close the socket, stop the mic, release audio. Idempotent. Call in `onDestroy`. */
|
|
1006
|
+
endCall: () => void;
|
|
1007
|
+
/** Svelte readable store of the last transport/pipeline error, or `undefined`. Read with `$error`. */
|
|
1008
|
+
error: Readable<Error | undefined>;
|
|
1009
|
+
/** Svelte readable store of the live assistant text for the in-flight turn (grows via deltas; finalized on done). Read with `$interimTranscript`. */
|
|
1010
|
+
interimTranscript: Readable<string>;
|
|
1011
|
+
/** Svelte readable store: `true` while the mic is muted. Read with `$isMuted`. */
|
|
1012
|
+
isMuted: Readable<boolean>;
|
|
1013
|
+
/** Send a typed turn (no audio) — a text message spoken back by the agent. */
|
|
1014
|
+
sendText: (text: string) => void;
|
|
1015
|
+
/** Open the mic, connect the socket, and start the conversation. Idempotent while active. */
|
|
1016
|
+
startCall: () => Promise<void>;
|
|
1017
|
+
/** Svelte readable store of the current call lifecycle. Read with `$status`. */
|
|
1018
|
+
status: Readable<VoiceStatus>;
|
|
1019
|
+
/** Mute/unmute the microphone. Returns the new muted state. */
|
|
1020
|
+
toggleMute: () => boolean;
|
|
1021
|
+
/** Svelte readable store of the last finalized user utterance (STT result). Read with `$transcript`. */
|
|
1022
|
+
transcript: Readable<string>;
|
|
1023
|
+
}
|
|
1024
|
+
/**
|
|
1025
|
+
* A first-class voice-call surface for a voice-enabled agent: it opens a
|
|
1026
|
+
* WebSocket to the agent's `VoiceSessionDO`, captures mic audio as 16 kHz PCM,
|
|
1027
|
+
* streams the agent's synthesized speech back through the browser's audio output,
|
|
1028
|
+
* and mirrors the call lifecycle (`status`, `transcript`, `interimTranscript`,
|
|
1029
|
+
* `audioLevel`) to Svelte readable stores you read with `$`. Pass the generated
|
|
1030
|
+
* `api.agents.<name>Voice` reference (never a string), matching `agentChat`'s
|
|
1031
|
+
* reference-passing style. The Svelte counterpart to React's `useVoiceAgent`,
|
|
1032
|
+
* re-expressed as stores; the per-call connection lives in a closure variable (a
|
|
1033
|
+
* handle is created once per component, so no store-of-store indirection is
|
|
1034
|
+
* needed).
|
|
1035
|
+
*
|
|
1036
|
+
* v1 transport is plain binary WebSocket frames with push-to-talk / silence-timer
|
|
1037
|
+
* turn detection and client-side RMS barge-in. The heavy Web Audio capture and
|
|
1038
|
+
* playback subsystems are injectable (`createMicrophone` / `createSpeaker` /
|
|
1039
|
+
* `createSocket`) so the handle is drivable outside a browser.
|
|
1040
|
+
*
|
|
1041
|
+
* There is no auto-dispose in Svelte — call `endCall` in `onDestroy`
|
|
1042
|
+
* (`onDestroy(handle.endCall)`) to tear the call down if the component unmounts
|
|
1043
|
+
* mid-call.
|
|
1044
|
+
*
|
|
1045
|
+
* Pass `client` explicitly, or omit it to resolve the ambient client published by
|
|
1046
|
+
* `setLunoraClient`.
|
|
1047
|
+
*/
|
|
1048
|
+
declare function voiceAgent(options: VoiceAgentOptions): VoiceAgentHandle;
|
|
1049
|
+
declare function voiceAgent(client: LunoraClient, options: VoiceAgentOptions): VoiceAgentHandle;
|
|
1050
|
+
export { type AgentApi, type AgentChatApi, type AgentChatHandle, type AgentChatMessage, type AgentChatOptions, type AgentHandle, type AgentLiveEvent, type AgentOptions, type AgentProgressEvent, type AgentStateApi, type AgentStateHandle, type AgentStateOptions, type AgentThreadRecord, type AgentThreadStatus, type AgentTokenDelta, type AgentToolEvent, type AgentToolEventsApi, type AgentToolEventsHandle, type AgentToolEventsOptions, type AuthStore, type ConnectionStatusStore, type FlagContext, type FlagValue, type HeartbeatReference, type InfiniteQueryHandle, type InfiniteQueryOptions, type ListPresentReference, type MutationHandle, type MutatorHandleStore, type PageItemOf, type PaginatedArgs, type PaginatedQueryHandle, type PaginatedQueryOptions, type PresenceHandle, type PresenceOptions, type QueryStore, type QueryStoreOptions, type RateLimitHandle, type RateLimitOptions, type StreamHandle, type StreamStatus, type StreamStoreOptions, type SubscriptionHandle, type SubscriptionStoreOptions, type VoiceAgentHandle, type VoiceAgentOptions, type VoiceAudioFormat, type VoiceReference, type VoiceStatus, agent, agentChat, agentState, agentToolEvents, auth, connectionStatus, flag, flags, getLunoraClient, hydratePreloaded, infiniteQuery, mutation, mutator, paginatedQuery, presence, query, rateLimit, setLunoraClient, stream, subscription, voiceAgent };
|