@hilbras/sdk 0.26.6 → 1.0.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 +1 -1
- package/dist/client/client.d.ts +5 -10
- package/dist/client/client.d.ts.map +1 -1
- package/dist/client/client.js +125 -26
- package/dist/client/client.js.map +1 -1
- package/dist/frameworks/angular/chat.service.d.ts +22 -1
- package/dist/frameworks/angular/chat.service.d.ts.map +1 -1
- package/dist/frameworks/angular/chat.service.js +119 -10
- package/dist/frameworks/angular/chat.service.js.map +1 -1
- package/dist/frameworks/astro/endpoints.d.ts +4 -0
- package/dist/frameworks/astro/endpoints.d.ts.map +1 -1
- package/dist/frameworks/astro/endpoints.js +37 -34
- package/dist/frameworks/astro/endpoints.js.map +1 -1
- package/dist/frameworks/nextjs/route-handlers.d.ts +7 -0
- package/dist/frameworks/nextjs/route-handlers.d.ts.map +1 -1
- package/dist/frameworks/nextjs/route-handlers.js +39 -32
- package/dist/frameworks/nextjs/route-handlers.js.map +1 -1
- package/dist/frameworks/qwik/index.d.ts +1 -1
- package/dist/frameworks/qwik/index.d.ts.map +1 -1
- package/dist/frameworks/qwik/index.js.map +1 -1
- package/dist/frameworks/qwik/stream-parser.d.ts +8 -0
- package/dist/frameworks/qwik/stream-parser.d.ts.map +1 -0
- package/dist/frameworks/qwik/stream-parser.js +42 -0
- package/dist/frameworks/qwik/stream-parser.js.map +1 -0
- package/dist/frameworks/qwik/use-chat.d.ts +20 -11
- package/dist/frameworks/qwik/use-chat.d.ts.map +1 -1
- package/dist/frameworks/qwik/use-chat.js +147 -33
- package/dist/frameworks/qwik/use-chat.js.map +1 -1
- package/dist/frameworks/react/generative-ui.d.ts +122 -0
- package/dist/frameworks/react/generative-ui.d.ts.map +1 -0
- package/dist/frameworks/react/generative-ui.js +120 -0
- package/dist/frameworks/react/generative-ui.js.map +1 -0
- package/dist/frameworks/react/index.d.ts +3 -1
- package/dist/frameworks/react/index.d.ts.map +1 -1
- package/dist/frameworks/react/index.js +1 -0
- package/dist/frameworks/react/index.js.map +1 -1
- package/dist/frameworks/react/use-chat.d.ts +28 -3
- package/dist/frameworks/react/use-chat.d.ts.map +1 -1
- package/dist/frameworks/react/use-chat.js +126 -5
- package/dist/frameworks/react/use-chat.js.map +1 -1
- package/dist/frameworks/remix/actions.d.ts +4 -0
- package/dist/frameworks/remix/actions.d.ts.map +1 -1
- package/dist/frameworks/remix/actions.js +37 -33
- package/dist/frameworks/remix/actions.js.map +1 -1
- package/dist/frameworks/solid/index.d.ts +1 -1
- package/dist/frameworks/solid/index.d.ts.map +1 -1
- package/dist/frameworks/solid/index.js.map +1 -1
- package/dist/frameworks/solid/stream-parser.d.ts +8 -0
- package/dist/frameworks/solid/stream-parser.d.ts.map +1 -0
- package/dist/frameworks/solid/stream-parser.js +42 -0
- package/dist/frameworks/solid/stream-parser.js.map +1 -0
- package/dist/frameworks/solid/use-chat.d.ts +20 -10
- package/dist/frameworks/solid/use-chat.d.ts.map +1 -1
- package/dist/frameworks/solid/use-chat.js +147 -35
- package/dist/frameworks/solid/use-chat.js.map +1 -1
- package/dist/frameworks/svelte/use-chat.d.ts +24 -1
- package/dist/frameworks/svelte/use-chat.d.ts.map +1 -1
- package/dist/frameworks/svelte/use-chat.js +140 -10
- package/dist/frameworks/svelte/use-chat.js.map +1 -1
- package/dist/frameworks/vue/use-chat.d.ts +44 -1
- package/dist/frameworks/vue/use-chat.d.ts.map +1 -1
- package/dist/frameworks/vue/use-chat.js +160 -31
- package/dist/frameworks/vue/use-chat.js.map +1 -1
- package/dist/index.d.ts +4 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/types/schema.d.ts +38 -0
- package/dist/types/schema.d.ts.map +1 -1
- package/dist/types/streams.d.ts +25 -1
- package/dist/types/streams.d.ts.map +1 -1
- package/dist/types/streams.js +4 -0
- package/dist/types/streams.js.map +1 -1
- package/dist/types/ui-protocol.d.ts +34 -2
- package/dist/types/ui-protocol.d.ts.map +1 -1
- package/dist/utils/sse-writer.d.ts +68 -0
- package/dist/utils/sse-writer.d.ts.map +1 -0
- package/dist/utils/sse-writer.js +233 -0
- package/dist/utils/sse-writer.js.map +1 -0
- package/package.json +1 -1
|
@@ -1,50 +1,140 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @hilbras/qwik — useChat
|
|
3
3
|
*
|
|
4
|
-
* Qwik-compatible chat hook.
|
|
4
|
+
* Qwik-compatible chat hook with tool call support.
|
|
5
5
|
*/
|
|
6
6
|
import { useSignal } from "./signal-shim.js";
|
|
7
|
+
import { parseUIStream } from "./stream-parser.js";
|
|
8
|
+
let _idCounter = 0;
|
|
9
|
+
function generateId() {
|
|
10
|
+
return `msg_${Date.now()}_${_idCounter++}`;
|
|
11
|
+
}
|
|
7
12
|
export function useChat(options = {}) {
|
|
8
|
-
const { api = "/api/chat", onFinish, onError, initialMessages = [] } = options;
|
|
13
|
+
const { api = "/api/chat", onFinish, onError, initialMessages = [], provider, model, maxSteps = 1, onToolCall, headers = {}, body = {}, } = options;
|
|
9
14
|
const messages = useSignal(initialMessages);
|
|
10
15
|
const isLoading = useSignal(false);
|
|
11
16
|
const error = useSignal(null);
|
|
12
17
|
let abortController = null;
|
|
13
|
-
async function sendRequest(
|
|
18
|
+
async function sendRequest(messageHistory, assistantMessage) {
|
|
14
19
|
isLoading.value = true;
|
|
15
20
|
error.value = null;
|
|
16
21
|
abortController = new AbortController();
|
|
17
22
|
try {
|
|
18
|
-
const
|
|
23
|
+
const res = await fetch(api, {
|
|
19
24
|
method: "POST",
|
|
20
|
-
headers: { "Content-Type": "application/json" },
|
|
21
|
-
body: JSON.stringify({
|
|
25
|
+
headers: { "Content-Type": "application/json", ...headers },
|
|
26
|
+
body: JSON.stringify({
|
|
27
|
+
messages: messageHistory.map((m) => ({ role: m.role, content: m.content })),
|
|
28
|
+
provider,
|
|
29
|
+
model,
|
|
30
|
+
stream: true,
|
|
31
|
+
maxSteps,
|
|
32
|
+
...body,
|
|
33
|
+
}),
|
|
22
34
|
signal: abortController.signal,
|
|
23
35
|
});
|
|
24
|
-
if (!
|
|
25
|
-
throw new Error(`HTTP ${
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
const
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
36
|
+
if (!res.ok)
|
|
37
|
+
throw new Error(`HTTP ${res.status}: ${res.statusText}`);
|
|
38
|
+
if (!res.body)
|
|
39
|
+
throw new Error("Response body is null");
|
|
40
|
+
let content = "";
|
|
41
|
+
const toolInvocations = [];
|
|
42
|
+
const pendingToolCalls = new Map();
|
|
43
|
+
for await (const msg of parseUIStream(res.body)) {
|
|
44
|
+
switch (msg.type) {
|
|
45
|
+
case "text_delta":
|
|
46
|
+
content += msg.text;
|
|
47
|
+
messages.value = messages.value.map((m) => m.id === assistantMessage.id ? { ...m, content } : m);
|
|
48
|
+
break;
|
|
49
|
+
case "tool_call_start": {
|
|
50
|
+
const invocation = {
|
|
51
|
+
id: msg.id,
|
|
52
|
+
name: msg.name,
|
|
53
|
+
args: {},
|
|
54
|
+
state: "call",
|
|
55
|
+
};
|
|
56
|
+
toolInvocations.push(invocation);
|
|
57
|
+
pendingToolCalls.set(msg.id, { name: msg.name, argsBuffer: "" });
|
|
58
|
+
messages.value = messages.value.map((m) => m.id === assistantMessage.id
|
|
59
|
+
? { ...m, toolInvocations: [...toolInvocations] }
|
|
60
|
+
: m);
|
|
61
|
+
break;
|
|
62
|
+
}
|
|
63
|
+
case "tool_call_delta": {
|
|
64
|
+
const pending = pendingToolCalls.get(msg.id);
|
|
65
|
+
if (pending) {
|
|
66
|
+
pending.argsBuffer += msg.args;
|
|
67
|
+
try {
|
|
68
|
+
const partialArgs = JSON.parse(pending.argsBuffer);
|
|
69
|
+
const invocation = toolInvocations.find((t) => t.id === msg.id);
|
|
70
|
+
if (invocation) {
|
|
71
|
+
invocation.args = partialArgs;
|
|
72
|
+
messages.value = messages.value.map((m) => m.id === assistantMessage.id
|
|
73
|
+
? { ...m, toolInvocations: [...toolInvocations] }
|
|
74
|
+
: m);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
catch {
|
|
78
|
+
// Partial JSON not parseable yet
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
break;
|
|
82
|
+
}
|
|
83
|
+
case "tool_call_end": {
|
|
84
|
+
const pending = pendingToolCalls.get(msg.id);
|
|
85
|
+
const invocation = toolInvocations.find((t) => t.id === msg.id);
|
|
86
|
+
if (invocation && pending) {
|
|
87
|
+
try {
|
|
88
|
+
invocation.args = JSON.parse(pending.argsBuffer || "{}");
|
|
89
|
+
}
|
|
90
|
+
catch {
|
|
91
|
+
invocation.args = { raw: pending.argsBuffer };
|
|
92
|
+
}
|
|
93
|
+
pendingToolCalls.delete(msg.id);
|
|
94
|
+
if (onToolCall) {
|
|
95
|
+
try {
|
|
96
|
+
invocation.state = "call";
|
|
97
|
+
const result = await onToolCall({
|
|
98
|
+
id: msg.id,
|
|
99
|
+
name: invocation.name,
|
|
100
|
+
args: invocation.args,
|
|
101
|
+
});
|
|
102
|
+
invocation.result = result;
|
|
103
|
+
invocation.state = "result";
|
|
104
|
+
}
|
|
105
|
+
catch (err) {
|
|
106
|
+
invocation.state = "error";
|
|
107
|
+
invocation.error = err.message;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
invocation.state = "call";
|
|
112
|
+
}
|
|
113
|
+
messages.value = messages.value.map((m) => m.id === assistantMessage.id
|
|
114
|
+
? { ...m, toolInvocations: [...toolInvocations] }
|
|
115
|
+
: m);
|
|
116
|
+
}
|
|
117
|
+
break;
|
|
118
|
+
}
|
|
119
|
+
case "message_end":
|
|
120
|
+
if (msg.usage) {
|
|
121
|
+
messages.value = messages.value.map((m) => m.id === assistantMessage.id ? { ...m, usage: msg.usage } : m);
|
|
122
|
+
}
|
|
123
|
+
break;
|
|
124
|
+
case "error":
|
|
125
|
+
throw new Error(msg.error);
|
|
126
|
+
}
|
|
42
127
|
}
|
|
43
|
-
|
|
128
|
+
const finalMessage = {
|
|
129
|
+
...assistantMessage,
|
|
130
|
+
content,
|
|
131
|
+
toolInvocations: toolInvocations.length > 0 ? toolInvocations : undefined,
|
|
132
|
+
};
|
|
133
|
+
onFinish?.(finalMessage);
|
|
44
134
|
}
|
|
45
135
|
catch (err) {
|
|
46
136
|
if (err.name !== "AbortError") {
|
|
47
|
-
const e = err;
|
|
137
|
+
const e = err instanceof Error ? err : new Error(String(err));
|
|
48
138
|
error.value = e;
|
|
49
139
|
onError?.(e);
|
|
50
140
|
}
|
|
@@ -54,17 +144,41 @@ export function useChat(options = {}) {
|
|
|
54
144
|
}
|
|
55
145
|
}
|
|
56
146
|
async function append(message) {
|
|
57
|
-
const
|
|
58
|
-
|
|
59
|
-
|
|
147
|
+
const userMessage = {
|
|
148
|
+
id: generateId(),
|
|
149
|
+
role: "user",
|
|
150
|
+
content: message.content,
|
|
151
|
+
createdAt: Date.now(),
|
|
152
|
+
};
|
|
153
|
+
const assistantMessage = {
|
|
154
|
+
id: generateId(),
|
|
155
|
+
role: "assistant",
|
|
156
|
+
content: "",
|
|
157
|
+
createdAt: Date.now(),
|
|
158
|
+
provider,
|
|
159
|
+
model,
|
|
160
|
+
};
|
|
161
|
+
messages.value = [...messages.value, userMessage, assistantMessage];
|
|
162
|
+
await sendRequest([...messages.value.slice(0, -2), userMessage], assistantMessage);
|
|
60
163
|
}
|
|
61
164
|
async function reload() {
|
|
62
165
|
const msgs = messages.value;
|
|
63
|
-
if (msgs.length === 0)
|
|
166
|
+
if (msgs.length === 0 || isLoading.value)
|
|
167
|
+
return;
|
|
168
|
+
const lastUserIdx = msgs.findLastIndex((m) => m.role === "user");
|
|
169
|
+
if (lastUserIdx === -1)
|
|
64
170
|
return;
|
|
65
|
-
const
|
|
66
|
-
|
|
67
|
-
|
|
171
|
+
const messagesUpToLastUser = msgs.slice(0, lastUserIdx + 1);
|
|
172
|
+
const assistantMessage = {
|
|
173
|
+
id: generateId(),
|
|
174
|
+
role: "assistant",
|
|
175
|
+
content: "",
|
|
176
|
+
createdAt: Date.now(),
|
|
177
|
+
provider,
|
|
178
|
+
model,
|
|
179
|
+
};
|
|
180
|
+
messages.value = [...messagesUpToLastUser, assistantMessage];
|
|
181
|
+
await sendRequest(messagesUpToLastUser, assistantMessage);
|
|
68
182
|
}
|
|
69
183
|
function stop() {
|
|
70
184
|
abortController?.abort();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-chat.js","sourceRoot":"","sources":["../../../src/frameworks/qwik/use-chat.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;
|
|
1
|
+
{"version":3,"file":"use-chat.js","sourceRoot":"","sources":["../../../src/frameworks/qwik/use-chat.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAC7C,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAGnD,IAAI,UAAU,GAAG,CAAC,CAAC;AACnB,SAAS,UAAU;IACjB,OAAO,OAAO,IAAI,CAAC,GAAG,EAAE,IAAI,UAAU,EAAE,EAAE,CAAC;AAC7C,CAAC;AA0BD,MAAM,UAAU,OAAO,CAAC,OAAO,GAAmB,EAAE;IAClD,MAAM,EACJ,GAAG,GAAG,WAAW,EACjB,QAAQ,EACR,OAAO,EACP,eAAe,GAAG,EAAE,EACpB,QAAQ,EACR,KAAK,EACL,QAAQ,GAAG,CAAC,EACZ,UAAU,EACV,OAAO,GAAG,EAAE,EACZ,IAAI,GAAG,EAAE,GACV,GAAG,OAAO,CAAC;IAEZ,MAAM,QAAQ,GAAG,SAAS,CAAc,eAAe,C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CAAC;IACrB,CAAC;IAED,SAAS,WAAW,CAAC,IAAiB;QACpC,QAAQ,CAAC,KAAK,GAAG,IAAI,CAAC;IACxB,CAAC;IAED,OAAO;QACL,QAAQ,EAAE,GAAG,EAAE,CAAC,QAAQ,CAAC,KAAK;QAC9B,SAAS,EAAE,GAAG,EAAE,CAAC,SAAS,CAAC,KAAK;QAChC,KAAK,EAAE,GAAG,EAAE,CAAC,KAAK,CAAC,KAAK;QACxB,WAAW;QACX,MAAM;QACN,MAAM;QACN,IAAI;QACJ,KAAK;KACN,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @hilbras/react — Generative UI
|
|
3
|
+
*
|
|
4
|
+
* Allows tool calls to render custom React components inline.
|
|
5
|
+
* Components are rendered server-side and serialized for client consumption.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```tsx
|
|
9
|
+
* import { streamUI } from "@hilbras/sdk/react";
|
|
10
|
+
* import { WeatherCard } from "./components";
|
|
11
|
+
*
|
|
12
|
+
* const result = await streamUI({
|
|
13
|
+
* model: "gpt-4o",
|
|
14
|
+
* messages: [{ role: "user", content: "What's the weather in Tokyo?" }],
|
|
15
|
+
* tools: {
|
|
16
|
+
* get_weather: {
|
|
17
|
+
* render: async (args) => {
|
|
18
|
+
* const weather = await fetchWeather(args.city);
|
|
19
|
+
* return <WeatherCard city={args.city} temp={weather.temp} />;
|
|
20
|
+
* },
|
|
21
|
+
* },
|
|
22
|
+
* },
|
|
23
|
+
* });
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
import type { ReactNode } from "react";
|
|
27
|
+
/** A serializable component reference */
|
|
28
|
+
export interface ComponentRef {
|
|
29
|
+
/** Component name (must be registered in the registry) */
|
|
30
|
+
name: string;
|
|
31
|
+
/** Props to pass to the component */
|
|
32
|
+
props: Record<string, unknown>;
|
|
33
|
+
}
|
|
34
|
+
/** A rendered component result */
|
|
35
|
+
export interface RenderedComponent {
|
|
36
|
+
type: "component";
|
|
37
|
+
/** The component reference (serializable) */
|
|
38
|
+
ref: ComponentRef;
|
|
39
|
+
/** The actual React element (client-side only, not serialized) */
|
|
40
|
+
element?: ReactNode;
|
|
41
|
+
}
|
|
42
|
+
/** A tool definition with optional render function for generative UI */
|
|
43
|
+
export interface GenerativeTool<TArgs = Record<string, unknown>> {
|
|
44
|
+
/** Description of the tool for the LLM */
|
|
45
|
+
description?: string;
|
|
46
|
+
/** JSON Schema for the tool's parameters */
|
|
47
|
+
parameters?: Record<string, unknown>;
|
|
48
|
+
/** Execute the tool and return data */
|
|
49
|
+
execute?: (args: TArgs) => Promise<unknown> | unknown;
|
|
50
|
+
/** Render a React component for the tool result */
|
|
51
|
+
render?: (args: TArgs) => Promise<ReactNode> | ReactNode;
|
|
52
|
+
}
|
|
53
|
+
/** Options for the streamUI function */
|
|
54
|
+
export interface StreamUIOptions {
|
|
55
|
+
/** Provider to use */
|
|
56
|
+
provider?: string;
|
|
57
|
+
/** Model to use */
|
|
58
|
+
model?: string;
|
|
59
|
+
/** Messages for the conversation */
|
|
60
|
+
messages: Array<{
|
|
61
|
+
role: string;
|
|
62
|
+
content: string;
|
|
63
|
+
}>;
|
|
64
|
+
/** Tools with optional render functions */
|
|
65
|
+
tools: Record<string, GenerativeTool>;
|
|
66
|
+
/** Component registry for deserializing components on the client */
|
|
67
|
+
componentRegistry?: Record<string, React.ComponentType<Record<string, unknown>>>;
|
|
68
|
+
}
|
|
69
|
+
/** Result of a streamUI call */
|
|
70
|
+
export interface StreamUIResult {
|
|
71
|
+
/** The rendered content (mix of text and components) */
|
|
72
|
+
content: ReactNode[];
|
|
73
|
+
/** Tool invocations with their rendered components */
|
|
74
|
+
toolInvocations: Array<{
|
|
75
|
+
id: string;
|
|
76
|
+
name: string;
|
|
77
|
+
args: Record<string, unknown>;
|
|
78
|
+
result?: unknown;
|
|
79
|
+
rendered?: ReactNode;
|
|
80
|
+
}>;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Registry for mapping component names to React components.
|
|
84
|
+
* Used for deserializing ComponentRefs on the client side.
|
|
85
|
+
*/
|
|
86
|
+
export declare class ComponentRegistry {
|
|
87
|
+
private _components;
|
|
88
|
+
/** Register a component */
|
|
89
|
+
register(name: string, component: React.ComponentType<Record<string, unknown>>): void;
|
|
90
|
+
/** Register multiple components */
|
|
91
|
+
registerAll(components: Record<string, React.ComponentType<Record<string, unknown>>>): void;
|
|
92
|
+
/** Get a registered component */
|
|
93
|
+
get(name: string): React.ComponentType<Record<string, unknown>> | undefined;
|
|
94
|
+
/** Check if a component is registered */
|
|
95
|
+
has(name: string): boolean;
|
|
96
|
+
/** Get all registered component names */
|
|
97
|
+
names(): string[];
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Deserialize a ComponentRef into a React element.
|
|
101
|
+
* Requires a ComponentRegistry with the referenced component registered.
|
|
102
|
+
*
|
|
103
|
+
* @param ref - The serializable component reference
|
|
104
|
+
* @param registry - The component registry
|
|
105
|
+
* @returns The React element, or a fallback element if not found
|
|
106
|
+
*/
|
|
107
|
+
export declare function deserializeComponent(ref: ComponentRef, registry: ComponentRegistry): ReactNode;
|
|
108
|
+
/**
|
|
109
|
+
* Serialize a React element into a ComponentRef.
|
|
110
|
+
* Useful for server-side rendering tool results.
|
|
111
|
+
*
|
|
112
|
+
* @param element - The React element to serialize
|
|
113
|
+
* @param registry - The component registry
|
|
114
|
+
* @returns The serializable component reference, or null if not serializable
|
|
115
|
+
*/
|
|
116
|
+
export declare function serializeComponent(element: ReactNode, registry: ComponentRegistry): ComponentRef | null;
|
|
117
|
+
/**
|
|
118
|
+
* Initialize the generative UI module.
|
|
119
|
+
* Must be called before using serializeComponent/deserializeComponent.
|
|
120
|
+
*/
|
|
121
|
+
export declare function initGenerativeUI(): void;
|
|
122
|
+
//# sourceMappingURL=generative-ui.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"generative-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AgB,oBAAoB,CAClC,GAAG,EAAE,YAAY,EACjB,QAAQ,EAAE,iBAAiB,GAC1B,SAAS,CAOX;AAED;;;;;;;GAOG;AACH,wBAAgB,kBAAkB,CAChC,OAAO,EAAE,SAAS,EAClB,QAAQ,EAAE,iBAAiB,GAC1B,YAAY,GAAG,IAAI,CAiBrB;AAKD;;;GAGG;AACH,wBAAgB,gBAAgB,IAAI,IAAI,CAYvC"}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @hilbras/react — Generative UI
|
|
3
|
+
*
|
|
4
|
+
* Allows tool calls to render custom React components inline.
|
|
5
|
+
* Components are rendered server-side and serialized for client consumption.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```tsx
|
|
9
|
+
* import { streamUI } from "@hilbras/sdk/react";
|
|
10
|
+
* import { WeatherCard } from "./components";
|
|
11
|
+
*
|
|
12
|
+
* const result = await streamUI({
|
|
13
|
+
* model: "gpt-4o",
|
|
14
|
+
* messages: [{ role: "user", content: "What's the weather in Tokyo?" }],
|
|
15
|
+
* tools: {
|
|
16
|
+
* get_weather: {
|
|
17
|
+
* render: async (args) => {
|
|
18
|
+
* const weather = await fetchWeather(args.city);
|
|
19
|
+
* return <WeatherCard city={args.city} temp={weather.temp} />;
|
|
20
|
+
* },
|
|
21
|
+
* },
|
|
22
|
+
* },
|
|
23
|
+
* });
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
/**
|
|
27
|
+
* Registry for mapping component names to React components.
|
|
28
|
+
* Used for deserializing ComponentRefs on the client side.
|
|
29
|
+
*/
|
|
30
|
+
export class ComponentRegistry {
|
|
31
|
+
_components = new Map();
|
|
32
|
+
/** Register a component */
|
|
33
|
+
register(name, component) {
|
|
34
|
+
this._components.set(name, component);
|
|
35
|
+
}
|
|
36
|
+
/** Register multiple components */
|
|
37
|
+
registerAll(components) {
|
|
38
|
+
for (const [name, component] of Object.entries(components)) {
|
|
39
|
+
this._components.set(name, component);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
/** Get a registered component */
|
|
43
|
+
get(name) {
|
|
44
|
+
return this._components.get(name);
|
|
45
|
+
}
|
|
46
|
+
/** Check if a component is registered */
|
|
47
|
+
has(name) {
|
|
48
|
+
return this._components.has(name);
|
|
49
|
+
}
|
|
50
|
+
/** Get all registered component names */
|
|
51
|
+
names() {
|
|
52
|
+
return Array.from(this._components.keys());
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Deserialize a ComponentRef into a React element.
|
|
57
|
+
* Requires a ComponentRegistry with the referenced component registered.
|
|
58
|
+
*
|
|
59
|
+
* @param ref - The serializable component reference
|
|
60
|
+
* @param registry - The component registry
|
|
61
|
+
* @returns The React element, or a fallback element if not found
|
|
62
|
+
*/
|
|
63
|
+
export function deserializeComponent(ref, registry) {
|
|
64
|
+
const Component = registry.get(ref.name);
|
|
65
|
+
if (!Component) {
|
|
66
|
+
// Return a fallback element
|
|
67
|
+
return `Unknown component: ${ref.name}`;
|
|
68
|
+
}
|
|
69
|
+
return createElement(Component, ref.props);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Serialize a React element into a ComponentRef.
|
|
73
|
+
* Useful for server-side rendering tool results.
|
|
74
|
+
*
|
|
75
|
+
* @param element - The React element to serialize
|
|
76
|
+
* @param registry - The component registry
|
|
77
|
+
* @returns The serializable component reference, or null if not serializable
|
|
78
|
+
*/
|
|
79
|
+
export function serializeComponent(element, registry) {
|
|
80
|
+
// Check if element is a React element with type and props
|
|
81
|
+
if (element &&
|
|
82
|
+
typeof element === "object" &&
|
|
83
|
+
"type" in element &&
|
|
84
|
+
"props" in element) {
|
|
85
|
+
const el = element;
|
|
86
|
+
if (typeof el.type === "string" && registry.has(el.type)) {
|
|
87
|
+
return {
|
|
88
|
+
name: el.type,
|
|
89
|
+
props: el.props,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
/** React.createElement import (lazy) */
|
|
96
|
+
let createElement;
|
|
97
|
+
/**
|
|
98
|
+
* Initialize the generative UI module.
|
|
99
|
+
* Must be called before using serializeComponent/deserializeComponent.
|
|
100
|
+
*/
|
|
101
|
+
export function initGenerativeUI() {
|
|
102
|
+
try {
|
|
103
|
+
// Dynamic import to avoid hard dependency on React
|
|
104
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
105
|
+
const react = require("react");
|
|
106
|
+
createElement = react.createElement;
|
|
107
|
+
}
|
|
108
|
+
catch {
|
|
109
|
+
throw new Error("Generative UI requires React to be installed. " +
|
|
110
|
+
"Install it with: npm install react");
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
// Auto-initialize on import
|
|
114
|
+
try {
|
|
115
|
+
initGenerativeUI();
|
|
116
|
+
}
|
|
117
|
+
catch {
|
|
118
|
+
// React may not be available — that's OK, the error will surface when trying to use generative UI
|
|
119
|
+
}
|
|
120
|
+
//# sourceMappingURL=generative-ui.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"generative-ui.js","sourceRoot":"","sources":["../../../src/frameworks/react/generative-ui.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AA6DH;;;GAGG;AACH,MAAM,OAAO,iBAAiB;IACpB,WAAW,GAAG,IAAI,GAAG,EAAwD,CAAC;IAEtF,2BAA2B;IAC3B,QAAQ,CAAC,IAAY,EAAE,SAAuD;QAC5E,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,IAAI,EAAE,SAAS,CAAC,CAAC;IACxC,CAAC;IAED,mCAAmC;IACnC,WAAW,CAAC,UAAwE;QAClF,KAAK,MAAM,CAAC,IAAI,EAAE,SAAS,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,UAAU,CAAC,EAAE,CAAC;YAC3D,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,IAAI,EAAE,SAA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AC;AAED,wCAAwC;AACxC,IAAI,aAAmD,CAAC;AAExD;;;GAGG;AACH,MAAM,UAAU,gBAAgB;IAC9B,IAAI,CAAC;QACH,mDAAmD;QACnD,iEAAiE;QACjE,MAAM,KAAK,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;QAC/B,aAAa,GAAG,KAAK,CAAC,aAAa,CAAC;IACtC,CAAC;IAAC,MAAM,CAAC;QACP,MAAM,IAAI,KAAK,CACb,gDAAgD;YAChD,oCAAoC,CACrC,CAAC;IACJ,CAAC;AACH,CAAC;AAED,4BAA4B;AAC5B,IAAI,CAAC;IACH,gBAAgB,EAAE,CAAC;AACrB,CAAC;AAAC,MAAM,CAAC;IACP,kGAAkG;AACpG,CAAC"}
|
|
@@ -10,5 +10,7 @@ export type { UseCompletionReturn } from "./use-completion.js";
|
|
|
10
10
|
export { useObject } from "./use-object.js";
|
|
11
11
|
export type { UseObjectReturn, UseObjectOptions, UseObjectState, UseObjectActions } from "./use-object.js";
|
|
12
12
|
export { parseUIStream } from "./stream-parser.js";
|
|
13
|
-
export
|
|
13
|
+
export { ComponentRegistry, deserializeComponent, serializeComponent, initGenerativeUI, } from "./generative-ui.js";
|
|
14
|
+
export type { ComponentRef, RenderedComponent, GenerativeTool, StreamUIOptions, StreamUIResult, } from "./generative-ui.js";
|
|
15
|
+
export type { UIMessage, UIToolInvocation, DataAnnotation, UIProtocolMessage, UseChatOptions, UseChatState, UseChatActions, UseCompletionOptions, UseCompletionState, UseCompletionActions, } from "../../index.js";
|
|
14
16
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/frameworks/react/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AACxC,YAAY,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAEnD,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,YAAY,EAAE,mBAAmB,EAAE,MAAM,qBAAqB,CAAC;AAE/D,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,YAAY,EAAE,eAAe,EAAE,gBAAgB,EAAE,cAAc,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AAE3G,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/frameworks/react/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AACxC,YAAY,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAEnD,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,YAAY,EAAE,mBAAmB,EAAE,MAAM,qBAAqB,CAAC;AAE/D,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,YAAY,EAAE,eAAe,EAAE,gBAAgB,EAAE,cAAc,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AAE3G,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAEnD,OAAO,EACL,iBAAiB,EACjB,oBAAoB,EACpB,kBAAkB,EAClB,gBAAgB,GACjB,MAAM,oBAAoB,CAAC;AAC5B,YAAY,EACV,YAAY,EACZ,iBAAiB,EACjB,cAAc,EACd,eAAe,EACf,cAAc,GACf,MAAM,oBAAoB,CAAC;AAG5B,YAAY,EACV,SAAS,EACT,gBAAgB,EAChB,cAAc,EACd,iBAAiB,EACjB,cAAc,EACd,YAAY,EACZ,cAAc,EACd,oBAAoB,EACpB,kBAAkB,EAClB,oBAAoB,GACrB,MAAM,gBAAgB,CAAC"}
|
|
@@ -7,4 +7,5 @@ export { useChat } from "./use-chat.js";
|
|
|
7
7
|
export { useCompletion } from "./use-completion.js";
|
|
8
8
|
export { useObject } from "./use-object.js";
|
|
9
9
|
export { parseUIStream } from "./stream-parser.js";
|
|
10
|
+
export { ComponentRegistry, deserializeComponent, serializeComponent, initGenerativeUI, } from "./generative-ui.js";
|
|
10
11
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/frameworks/react/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AAGxC,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAGpD,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAG5C,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/frameworks/react/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AAGxC,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAGpD,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAG5C,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAEnD,OAAO,EACL,iBAAiB,EACjB,oBAAoB,EACpB,kBAAkB,EAClB,gBAAgB,GACjB,MAAM,oBAAoB,CAAC"}
|
|
@@ -2,10 +2,35 @@
|
|
|
2
2
|
* @hilbras/react — useChat Hook
|
|
3
3
|
*
|
|
4
4
|
* Manages a streaming chat conversation with an LLM backend.
|
|
5
|
-
* Handles message state, streaming, input, error recovery,
|
|
5
|
+
* Handles message state, streaming, tool calls, input, error recovery,
|
|
6
6
|
* optimistic updates, reload, and step-level callbacks.
|
|
7
7
|
*/
|
|
8
|
-
import type { UIMessage, UseChatOptions, UseChatState, UseChatActions } from "../../index.js";
|
|
8
|
+
import type { UIMessage, DataAnnotation, UseChatOptions, UseChatState, UseChatActions } from "../../index.js";
|
|
9
|
+
/** Extended options with tool calling support */
|
|
10
|
+
export interface UseChatToolOptions extends UseChatOptions {
|
|
11
|
+
/** Maximum number of tool-calling steps (default: 1 = no multi-step) */
|
|
12
|
+
maxSteps?: number;
|
|
13
|
+
/** Callback when a tool call is received; return the tool result */
|
|
14
|
+
onToolCall?: (toolCall: {
|
|
15
|
+
id: string;
|
|
16
|
+
name: string;
|
|
17
|
+
args: Record<string, unknown>;
|
|
18
|
+
}) => Promise<unknown> | unknown;
|
|
19
|
+
/** Callback after each step completes */
|
|
20
|
+
onStepFinish?: (step: {
|
|
21
|
+
step: number;
|
|
22
|
+
text: string;
|
|
23
|
+
toolCalls: Array<{
|
|
24
|
+
name: string;
|
|
25
|
+
args: Record<string, unknown>;
|
|
26
|
+
result: unknown;
|
|
27
|
+
}>;
|
|
28
|
+
}) => void;
|
|
29
|
+
/** Callback when structured data is received alongside text */
|
|
30
|
+
onData?: (data: unknown) => void;
|
|
31
|
+
/** Callback when an annotation is received */
|
|
32
|
+
onAnnotation?: (annotation: DataAnnotation) => void;
|
|
33
|
+
}
|
|
9
34
|
export interface UseChatReturn extends UseChatState, UseChatActions {
|
|
10
35
|
/** Reload the last assistant message */
|
|
11
36
|
reload: () => Promise<void>;
|
|
@@ -15,5 +40,5 @@ export interface UseChatReturn extends UseChatState, UseChatActions {
|
|
|
15
40
|
content: string;
|
|
16
41
|
}) => Promise<void>;
|
|
17
42
|
}
|
|
18
|
-
export declare function useChat(options:
|
|
43
|
+
export declare function useChat(options: UseChatToolOptions): UseChatReturn;
|
|
19
44
|
//# sourceMappingURL=use-chat.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-chat.d.ts","sourceRoot":"","sources":["../../../src/frameworks/react/use-chat.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAGH,OAAO,KAAK,EACV,SAAS,
|
|
1
|
+
{"version":3,"file":"use-chat.d.ts","sourceRoot":"","sources":["../../../src/frameworks/react/use-chat.ts"],"names":[],"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