@anvia/client 1.6.0 → 1.8.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/README.md CHANGED
@@ -75,6 +75,26 @@ These committed progress events are separate from token-delta chat streams.
75
75
 
76
76
  See the [durable HTTP guide](../../docs/packages/durable.md#http-server-and-client).
77
77
 
78
+ ### Chat UI transport
79
+
80
+ The optional `@anvia/client/durable-chat` entrypoint exports `createDurableChatTransport`, which
81
+ adapts a durable endpoint to the `ClientTransport` contract used by `useChat` from `@anvia/react`
82
+ and the `@anvia/react-ui` primitives. Create one transport per chat session; the agent, session and
83
+ request identity live there, not in an individual message request.
84
+
85
+ ```ts
86
+ const transport = createDurableChatTransport({ client, agentId, sessionId });
87
+ const chat = useChat({ transport });
88
+ ```
89
+
90
+ The adapter submits the last user message with `enqueue: true` by default, maps durable journal
91
+ events to `anvia.client.v3` frames, routes interaction responses to the run that produced them, and
92
+ uses the durable run id and sequence as `{ streamId, after }` for resume. Aborting the subscription
93
+ detaches only; use `transport.controller.cancel()`, `retry()`, `resolveTool()` or
94
+ `reattach(runId, after)` for explicit run control. A busy session is reported as
95
+ `DurableBusyError` with the run to reattach to. Client history is discarded in favour of the
96
+ durable transcript, so `regenerate()` and `reset()` are approximate; thread lists are not covered.
97
+
78
98
  `DurableClient` also exposes `submitGraph`, `listGraphs`, `graphSnapshot`, `streamGraph`, and
79
99
  `cancelGraph`. Graph snapshots contain renderable nodes/edges and explicit waiting reasons.
80
100
  Use each node's `runId` with the existing response/retry/reconciliation methods. Graph streams
@@ -0,0 +1,53 @@
1
+ import { DurableClient } from "./durable.js";
2
+ import { f as ClientStreamCursor, g as ClientStreamRequest, v as ClientTransport } from "./types-Bt4tUcST.js";
3
+ import { ToolResultOutput } from "@anvia/core/completion";
4
+ /**
5
+ * Thrown when a submission is rejected because the session already has unfinished work (HTTP 409).
6
+ * `runId` is that run when it could be read back from the session; `undefined` when the lookup
7
+ * failed. Reattach to `runId` instead of submitting again. Aborting a stream never raises this.
8
+ */
9
+ export declare class DurableBusyError extends Error {
10
+ readonly runId: string | undefined;
11
+ readonly code: "session_busy";
12
+ constructor(runId: string | undefined, message?: string);
13
+ }
14
+ /** Methods the adapter needs. A structural subset of `DurableClient`, so tests can fake it. */
15
+ export type DurableChatClient = Pick<DurableClient, "submit" | "stream" | "snapshot" | "respond" | "resolveTool" | "retry" | "cancel" | "listRuns"> & Partial<Pick<DurableClient, "resolveMemoryWrite">>;
16
+ /**
17
+ * Run controls that `useChat` does not expose. `stop()` only aborts the subscription; cancellation
18
+ * is always this explicit `cancel()`.
19
+ */
20
+ export type DurableChatController = {
21
+ /** Run last submitted or reattached. Undefined before the first successful send. */
22
+ readonly activeRunId: string | undefined;
23
+ /** Point later interaction responses and `send({ resume })` at an existing run. */
24
+ reattach(runId: string, after?: number): void;
25
+ cancel(runId?: string): Promise<void>;
26
+ retry(runId?: string): Promise<void>;
27
+ resolveTool(operationId: string, output: ToolResultOutput, runId?: string): Promise<void>;
28
+ /** Settle a blocked recorded memory write: `applied` says the store holds it, `discarded` drops it. */
29
+ resolveMemoryWrite(operationKey: string, resolution: "applied" | "discarded", runId?: string): Promise<void>;
30
+ };
31
+ export type CreateDurableChatTransportOptions = {
32
+ client: DurableChatClient;
33
+ agentId: string;
34
+ sessionId: string;
35
+ /** Defaults to a UUID per submission. Return a stable id to deduplicate one prompt. */
36
+ requestId?: () => string;
37
+ /** Queue behind unfinished session work instead of failing. Defaults to true. */
38
+ enqueue?: boolean;
39
+ };
40
+ export type DurableChatTransport = ClientTransport<ClientStreamRequest> & {
41
+ readonly controller: DurableChatController;
42
+ };
43
+ /**
44
+ * Transport `useChat` can pass to talk to `@anvia/durable`.
45
+ *
46
+ * Only the last user message of a `messages` request is submitted; durable keeps the transcript,
47
+ * so the rest of the client history is discarded. An `interaction_response` is delivered to the
48
+ * remembered run and then the stream resumes from the last applied sequence. Abort detaches the
49
+ * SSE subscription and does not cancel the run.
50
+ */
51
+ export declare function createDurableChatTransport(options: CreateDurableChatTransportOptions): DurableChatTransport;
52
+ export type { ClientStreamCursor };
53
+ //# sourceMappingURL=durable-chat.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"durable-chat.d.ts","names":[],"sources":["../src/durable-chat.ts"],"mappings":";;;;;;;;qBA2Ba,yBAAyB;WAIzB;WAHF;EAGE,YAAA,2BACT;;;YAiBQ,oBAAoB,KAC9B,iHAIA,QAAQ,KAAK;;;;;YAMH;;WAED;;EAET,SAAS,eAAe;EACxB,OAAO,iBAAiB;EACxB,MAAM,iBAAiB;EACvB,YAAY,qBAAqB,QAAQ,kBAAkB,iBAAiB;;EAE5E,mBACE,sBACA,qCACA,iBACC;;YAGO;EACV,QAAQ;EACR;EACA;;EAEA;;EAEA;;YAGU,uBAAuB,gBAAgB;WACxC,YAAY;;;;;;;;;;wBAkBP,2BACd,SAAS,oCACR"}