@anvia/client 1.2.0 → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +41 -68
  2. package/package.json +4 -4
package/README.md CHANGED
@@ -1,83 +1,56 @@
1
1
  # @anvia/client
2
2
 
3
- Framework-neutral client protocol, transports, message conversion, and stream state for Anvia.
3
+ Connect any frontend to Anvia with a framework-neutral streaming protocol. This package provides
4
+ HTTP and direct transports, runtime validation, and UI message state for text, tools, and interactions.
4
5
 
5
- `@anvia/core` owns native completion and Agent events. `@anvia/client` owns the public wire boundary:
6
-
7
- ```ts
8
- import { completionToClientStream, parseClientStreamRequest } from "@anvia/client";
9
- import { streamCompletion } from "@anvia/core";
10
-
11
- const request = parseClientStreamRequest(await httpRequest.json());
12
- if (request.type !== "messages") throw new Error("This endpoint does not resume interactions.");
13
- const events = completionToClientStream({
14
- events: streamCompletion({ model, messages: request.messages }),
15
- });
16
- ```
17
-
18
- The request carries core `Message[]`; client-side `UIMessage[]` never crosses the server boundary.
19
- The response uses `ClientStreamEvent` records inside an always-framed `anvia.client.v3` stream.
20
- Agent endpoints accept either a `messages` request or an `interaction_response` request, while
21
- keeping the matching `AgentContinuation` exclusively on the server.
22
- Agent interaction wire contracts come from the browser-safe `@anvia/core/agent/interactions`
23
- subpath; importing Client does not load the Agent runtime or its infrastructure dependencies.
24
-
25
- ## Bun
26
-
27
- Bun 1.3.14 is the currently tested and supported runtime baseline:
6
+ ## Install
28
7
 
29
8
  ```sh
30
- bun add @anvia/client @anvia/core
9
+ pnpm add @anvia/client @anvia/core
31
10
  ```
32
11
 
33
- Compatibility tests cover framed JSONL and SSE consumption, resumable client streams, fetch
34
- cancellation, and installation from packed package artifacts.
12
+ ## Quickstart
35
13
 
36
- ## Public API
14
+ Consume a chat endpoint that returns an Anvia client stream, such as one created with
15
+ `@anvia/server`:
37
16
 
38
- - `completionToClientStream({ events, ...options })` adapts native completion events.
39
- - `agentToClientStream({ events, ...options })` adapts native Agent events, including nested-agent
40
- scope.
41
- - `parseClientStreamRequest`, `parseClientStreamEvent`, and `parseClientStreamFrame` validate public
42
- input at runtime.
43
- - `createHttpClientTransport(options)` consumes framed JSONL or SSE responses and validates the
44
- protocol header, frame order, stream identity, and event IDs.
45
- - `createDirectClientTransport({ handler })` provides the same framed contract without HTTP.
46
- - `messagesToUIMessages` and `uiMessagesToMessages` explicitly convert server messages and UI
47
- state.
48
- - `applyClientStreamEvent(messages, event)` applies canonical events to `UIMessage[]` state.
49
- - `parseUIMessage` and `parseUIMessages` validate externally loaded UI state.
17
+ ```ts
18
+ import {
19
+ applyClientStreamEvent,
20
+ createHttpClientTransport,
21
+ messagesToUIMessages,
22
+ type UIMessage,
23
+ } from "@anvia/client";
24
+ import type { Message } from "@anvia/core/completion";
25
+
26
+ const transport = createHttpClientTransport({ endpoint: "/api/chat" });
27
+ const messages: Message[] = [{ role: "user", content: "Hello!" }];
28
+ let uiMessages: readonly UIMessage[] = messagesToUIMessages(messages);
29
+
30
+ for await (const frame of transport.send({ request: { type: "messages", messages } })) {
31
+ if (frame.type === "stream_event") {
32
+ uiMessages = applyClientStreamEvent(uiMessages, frame.event);
33
+ console.log(uiMessages);
34
+ }
35
+ }
36
+ ```
50
37
 
51
- Tool-call start, delta, and end events are automatic when the provider exposes streamed arguments.
52
- `UIToolMessagePart` states are exact: `input-streaming` carries raw partial text,
53
- `input-available` carries parsed JSON input, and terminal `output-available` or `error` parts retain
54
- that input with their result. `uiMessagesToMessages()` rejects partial calls instead of replaying
55
- incomplete JSON or inventing empty arguments.
56
- Errors are masked by default. Use `mapError` only at the server adapter boundary when an application
57
- intentionally exposes a safe error shape. Non-JSON outputs require an explicit `mapOutput`; returning
58
- `undefined` intentionally omits the output, while returning `null` exposes JSON `null`.
38
+ The transport validates framing, event order, and stream identity. Send Core `Message[]` to the
39
+ server; keep `UIMessage[]` as presentation state in your application.
59
40
 
60
- `UIMessage.metadata` remains application-owned and round-trips unchanged. Runtime details such as
61
- run ID, usage, context usage, status, and trace correlation are stored separately in
62
- `UIMessage.generation`. Converting persisted core messages hydrates per-generation usage and context
63
- usage into that UI field and restores persisted sources as UI message parts while preserving the
64
- original metadata.
41
+ ## Capabilities
65
42
 
66
- Application-specific stream data is explicit and schema-validated:
43
+ - HTTP transport for JSONL or SSE, plus direct transport for in-process integrations.
44
+ - Adapters for native completion and Agent events at the server boundary.
45
+ - Validated requests, events, frames, and persisted UI messages.
46
+ - Message conversion and reduction, including streamed tool arguments and generation usage.
47
+ - Typed custom stream data, Agent interactions, and HTTP resume cursors.
67
48
 
68
- ```ts
69
- type AppData = {
70
- citation_preview: { title: string; url: string };
71
- };
49
+ For React, `@anvia/react` manages this state through `useChat` and `useCompletion`. Generic JSONL/SSE
50
+ readers are also available from `@anvia/client/transport` for other event contracts.
72
51
 
73
- const transport = createHttpClientTransport<ClientStreamRequest, AppData>({
74
- endpoint: "/api/chat",
75
- dataSchemas: {
76
- citation_preview: citationPreviewSchema,
77
- },
78
- });
79
- ```
52
+ ## Learn more
80
53
 
81
- Low-level generic JSONL/SSE readers and event transports are available from
82
- `@anvia/client/transport`. They do not imply the Anvia client protocol; use them only for endpoints
83
- that intentionally expose a different event contract.
54
+ - [Client protocol guide](https://github.com/anvia-hq/anvia/blob/main/docs/packages/client.md)
55
+ - [Server response helpers](https://github.com/anvia-hq/anvia/tree/main/packages/server#readme)
56
+ - [React hooks](https://github.com/anvia-hq/anvia/tree/main/packages/react#readme)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anvia/client",
3
- "version": "1.2.0",
3
+ "version": "1.2.1",
4
4
  "description": "Framework-neutral client protocol, transports, and UI message state for Anvia.",
5
5
  "author": "anvia",
6
6
  "license": "MIT",
@@ -32,11 +32,11 @@
32
32
  "@types/node": "^24.9.1",
33
33
  "tsup": "^8.5.0",
34
34
  "typescript": "^5.9.3",
35
- "vitest": "^4.0.8",
36
- "@anvia/core": "1.2.1"
35
+ "vitest": "^4.1.11",
36
+ "@anvia/core": "1.6.1"
37
37
  },
38
38
  "peerDependencies": {
39
- "@anvia/core": "^1.2.1"
39
+ "@anvia/core": "^1.6.1"
40
40
  },
41
41
  "scripts": {
42
42
  "build": "tsup src/index.ts src/transport/index.ts --format esm --dts --sourcemap --clean",