@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.
- package/README.md +41 -68
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -1,83 +1,56 @@
|
|
|
1
1
|
# @anvia/client
|
|
2
2
|
|
|
3
|
-
|
|
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
|
-
|
|
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
|
-
|
|
9
|
+
pnpm add @anvia/client @anvia/core
|
|
31
10
|
```
|
|
32
11
|
|
|
33
|
-
|
|
34
|
-
cancellation, and installation from packed package artifacts.
|
|
12
|
+
## Quickstart
|
|
35
13
|
|
|
36
|
-
|
|
14
|
+
Consume a chat endpoint that returns an Anvia client stream, such as one created with
|
|
15
|
+
`@anvia/server`:
|
|
37
16
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
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
|
-
|
|
52
|
-
`
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
69
|
-
|
|
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
|
-
|
|
74
|
-
endpoint: "/api/chat",
|
|
75
|
-
dataSchemas: {
|
|
76
|
-
citation_preview: citationPreviewSchema,
|
|
77
|
-
},
|
|
78
|
-
});
|
|
79
|
-
```
|
|
52
|
+
## Learn more
|
|
80
53
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
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.
|
|
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.
|
|
36
|
-
"@anvia/core": "1.
|
|
35
|
+
"vitest": "^4.1.11",
|
|
36
|
+
"@anvia/core": "1.6.1"
|
|
37
37
|
},
|
|
38
38
|
"peerDependencies": {
|
|
39
|
-
"@anvia/core": "^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",
|