@blazingagents/sdk 0.52.0 → 0.54.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 +73 -17
- package/dist/chat-controller.d.ts +106 -0
- package/dist/chat-controller.js +361 -0
- package/dist/{react/chat-state.d.ts → chat-state.d.ts} +16 -12
- package/dist/{react/chat-state.js → chat-state.js} +13 -12
- package/dist/chat-transport.d.ts +6 -1
- package/dist/chat-transport.js +33 -16
- package/dist/contracts/entities/agents.d.ts +5 -0
- package/dist/contracts/entities/agents.js +36 -0
- package/dist/contracts/entities/sessions.d.ts +1 -0
- package/dist/contracts/entities/tasks.d.ts +8 -4
- package/dist/contracts/entities/tasks.js +9 -28
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/react/index.d.ts +2 -2
- package/dist/react/index.js +1 -1
- package/dist/react/use-blazing-agents-chat.d.ts +14 -71
- package/dist/react/use-blazing-agents-chat.js +26 -239
- package/package.json +4 -10
|
@@ -1,246 +1,33 @@
|
|
|
1
|
-
import { Chat, useChat } from "@ai-sdk/react";
|
|
2
|
-
import { generateId, lastAssistantMessageIsCompleteWithApprovalResponses, } from "ai";
|
|
3
1
|
import { useEffect, useMemo, useRef, useState, useSyncExternalStore, } from "react";
|
|
4
|
-
import {
|
|
5
|
-
import { sessionIdSchema } from "../contracts/ids.js";
|
|
6
|
-
import { BlazingAgentsError } from "../errors.js";
|
|
7
|
-
import { canRegenerate, initialChatState, pendingIds, step, } from "./chat-state.js";
|
|
8
|
-
const DEFAULT_STORAGE_KEY = "blazing-agents-session";
|
|
2
|
+
import { BlazingAgentsChat, } from "../chat-controller.js";
|
|
9
3
|
/**
|
|
10
|
-
* A Blazing Agents chat through the app's relay
|
|
11
|
-
*
|
|
12
|
-
*
|
|
4
|
+
* A Blazing Agents chat through the app's relay: one `BlazingAgentsChat` per
|
|
5
|
+
* component, started after mount and disposed on unmount. New `headers`, or
|
|
6
|
+
* new values in the same `Headers` object, apply to the next request, even one
|
|
7
|
+
* a child's effect sends in the same commit; the other options are read on the
|
|
8
|
+
* first render.
|
|
13
9
|
* @param options - Relay endpoint, request options, and storage key.
|
|
14
|
-
* @returns The chat
|
|
10
|
+
* @returns The chat's snapshot and its actions.
|
|
15
11
|
*/
|
|
16
12
|
export function useBlazingAgentsChat(options = {}) {
|
|
17
|
-
const
|
|
18
|
-
|
|
19
|
-
const [
|
|
20
|
-
const
|
|
21
|
-
const
|
|
22
|
-
const credentialsKey = headersKey(options.headers);
|
|
13
|
+
const headers = useRef(options.headers);
|
|
14
|
+
headers.current = options.headers;
|
|
15
|
+
const [readHeaders] = useState(() => () => headers.current ?? {});
|
|
16
|
+
const [chat] = useState(() => new BlazingAgentsChat({ ...options, headers: readHeaders }));
|
|
17
|
+
const snapshot = useSyncExternalStore(chat.subscribe, chat.getSnapshot, chat.getSnapshot);
|
|
23
18
|
useEffect(() => {
|
|
24
|
-
|
|
25
|
-
return
|
|
26
|
-
}, [
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
}, [shown, state.outbox]);
|
|
39
|
-
const pending = useMemo(() => pendingIds(state), [state]);
|
|
40
|
-
return {
|
|
41
|
-
addToolApprovalResponse: store.addToolApprovalResponse,
|
|
42
|
-
canRegenerate: canRegenerate(state),
|
|
43
|
-
clearError: store.clearError,
|
|
44
|
-
error: state.error,
|
|
45
|
-
messages,
|
|
46
|
-
newSession: store.newSession,
|
|
47
|
-
pendingIds: pending,
|
|
48
|
-
regenerate: store.regenerate,
|
|
49
|
-
send: store.send,
|
|
50
|
-
sessionId,
|
|
51
|
-
status: statusOf(state),
|
|
52
|
-
stop: store.stop,
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
|
-
function statusOf(state) {
|
|
56
|
-
if (state.phase.kind === "restoring" || state.phase.kind === "idle") {
|
|
57
|
-
return state.phase.kind;
|
|
58
|
-
}
|
|
59
|
-
return "running";
|
|
60
|
-
}
|
|
61
|
-
/** Runs one localStorage call. Where storage throws, as when the browser blocks it or it is full, the Session lives in memory only. */
|
|
62
|
-
function tryStorage(use) {
|
|
63
|
-
try {
|
|
64
|
-
return use(localStorage);
|
|
65
|
-
}
|
|
66
|
-
catch {
|
|
67
|
-
return undefined;
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
/** Header values as one comparable string, so a re-created but equal object is no change. */
|
|
71
|
-
function headersKey(headers) {
|
|
72
|
-
return JSON.stringify([...new Headers(headers)]);
|
|
73
|
-
}
|
|
74
|
-
/**
|
|
75
|
-
* Holds `ChatState` and the current conversation, runs `step` synchronously
|
|
76
|
-
* (so two sends in one tick see different states), then runs its effects.
|
|
77
|
-
*/
|
|
78
|
-
function createChatStore(options) {
|
|
79
|
-
const listeners = new Set();
|
|
80
|
-
const settlers = new Map();
|
|
81
|
-
let credentials = headersKey(options.current.headers);
|
|
82
|
-
let epoch = 0;
|
|
83
|
-
let state = initialChatState;
|
|
84
|
-
let conversation = open(undefined);
|
|
85
|
-
let snapshot = { conversation, sessionId: undefined, state };
|
|
86
|
-
function storageKey() {
|
|
87
|
-
return options.current.storageKey === undefined
|
|
88
|
-
? DEFAULT_STORAGE_KEY
|
|
89
|
-
: options.current.storageKey;
|
|
90
|
-
}
|
|
91
|
-
function publish() {
|
|
92
|
-
snapshot = {
|
|
93
|
-
conversation,
|
|
94
|
-
sessionId: conversation.transport.sessionId,
|
|
95
|
-
state,
|
|
96
|
-
};
|
|
97
|
-
for (const listener of listeners) {
|
|
98
|
-
listener();
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
function open(sessionId) {
|
|
102
|
-
epoch += 1;
|
|
103
|
-
const opened = epoch;
|
|
104
|
-
const transport = new BlazingAgentsChatTransport({
|
|
105
|
-
api: options.current.api,
|
|
106
|
-
credentials: options.current.credentials,
|
|
107
|
-
fetch: options.current.fetch,
|
|
108
|
-
headers: () => options.current.headers ?? {},
|
|
109
|
-
onSessionId: (id) => {
|
|
110
|
-
const key = storageKey();
|
|
111
|
-
if (key !== null) {
|
|
112
|
-
tryStorage((storage) => storage.setItem(key, id));
|
|
113
|
-
}
|
|
114
|
-
publish();
|
|
115
|
-
},
|
|
116
|
-
onSteerConsumed: (requestId) => dispatch({ type: "steerConsumed", requestId }, opened),
|
|
117
|
-
onTurnFinish: () => dispatch({ type: "turnFinished" }, opened),
|
|
118
|
-
onTurnStart: () => dispatch({ type: "turnStarted" }, opened),
|
|
119
|
-
sessionId,
|
|
120
|
-
});
|
|
121
|
-
const chat = new Chat({
|
|
122
|
-
id: `blazing-agents-${opened}`,
|
|
123
|
-
onFinish: (event) => dispatch({ type: "turnEnded", result: turnResult(event, chat) }, opened),
|
|
124
|
-
sendAutomaticallyWhen: lastAssistantMessageIsCompleteWithApprovalResponses,
|
|
125
|
-
transport,
|
|
126
|
-
});
|
|
127
|
-
return { chat, epoch: opened, transport };
|
|
128
|
-
}
|
|
129
|
-
function dispatch(event, from = epoch) {
|
|
130
|
-
if (from !== epoch) {
|
|
131
|
-
return;
|
|
132
|
-
}
|
|
133
|
-
const next = step(state, event, generateId);
|
|
134
|
-
state = next.state;
|
|
135
|
-
publish();
|
|
136
|
-
for (const effect of next.effects) {
|
|
137
|
-
run(effect);
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
function run(effect) {
|
|
141
|
-
const { chat, transport, epoch: at } = conversation;
|
|
142
|
-
switch (effect.type) {
|
|
143
|
-
case "startTurn":
|
|
144
|
-
chat.sendMessage(effect.message);
|
|
145
|
-
return;
|
|
146
|
-
case "regenerate":
|
|
147
|
-
chat.regenerate();
|
|
148
|
-
return;
|
|
149
|
-
case "submitSteer":
|
|
150
|
-
transport
|
|
151
|
-
.steer({ message: effect.message, requestId: effect.requestId })
|
|
152
|
-
.then((outcome) => dispatch({ type: "steerAnswered", requestId: effect.requestId, outcome }, at), (error) => dispatch({
|
|
153
|
-
type: "steerFailed",
|
|
154
|
-
requestId: effect.requestId,
|
|
155
|
-
error,
|
|
156
|
-
answerLost: BlazingAgentsError.isInstance(error) &&
|
|
157
|
-
error.code === "network_error",
|
|
158
|
-
}, at));
|
|
159
|
-
return;
|
|
160
|
-
case "show":
|
|
161
|
-
chat.messages = effect.messages;
|
|
162
|
-
return;
|
|
163
|
-
case "loadHistory":
|
|
164
|
-
transport.loadHistory().then((messages) => dispatch({ type: "historyLoaded", messages }, at), (error) => dispatch({ type: "historyFailed", error, sessionGone: sessionGone(error) }, at));
|
|
165
|
-
return;
|
|
166
|
-
case "abort":
|
|
167
|
-
chat.stop();
|
|
168
|
-
return;
|
|
169
|
-
case "forgetSession": {
|
|
170
|
-
const key = storageKey();
|
|
171
|
-
if (key !== null) {
|
|
172
|
-
tryStorage((storage) => storage.removeItem(key));
|
|
173
|
-
}
|
|
174
|
-
conversation = open(undefined);
|
|
175
|
-
publish();
|
|
176
|
-
return;
|
|
177
|
-
}
|
|
178
|
-
case "settle":
|
|
179
|
-
settlers.get(effect.key)?.(effect.landed);
|
|
180
|
-
settlers.delete(effect.key);
|
|
181
|
-
return;
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
return {
|
|
185
|
-
subscribe(listener) {
|
|
186
|
-
listeners.add(listener);
|
|
187
|
-
return () => listeners.delete(listener);
|
|
188
|
-
},
|
|
189
|
-
getSnapshot: () => snapshot,
|
|
190
|
-
mount() {
|
|
191
|
-
const key = storageKey();
|
|
192
|
-
const stored = sessionIdSchema.safeParse(key === null ? null : tryStorage((storage) => storage.getItem(key)));
|
|
193
|
-
// A remount keeps the Session in memory when storage holds none.
|
|
194
|
-
conversation = open(stored.success ? stored.data : conversation.transport.sessionId);
|
|
195
|
-
publish();
|
|
196
|
-
dispatch({ type: "restore" });
|
|
197
|
-
},
|
|
198
|
-
unmount: () => dispatch({ type: "unmount" }),
|
|
199
|
-
credentialsChanged(key) {
|
|
200
|
-
if (key !== credentials) {
|
|
201
|
-
credentials = key;
|
|
202
|
-
dispatch({ type: "credentialsChanged" });
|
|
203
|
-
}
|
|
204
|
-
},
|
|
205
|
-
send(input) {
|
|
206
|
-
const { files = [], text } = typeof input === "string" ? { text: input } : input;
|
|
207
|
-
const message = {
|
|
208
|
-
id: generateId(),
|
|
209
|
-
role: "user",
|
|
210
|
-
parts: [...files, { type: "text", text }],
|
|
211
|
-
};
|
|
212
|
-
return new Promise((resolve) => {
|
|
213
|
-
settlers.set(message.id, resolve);
|
|
214
|
-
dispatch({ type: "send", sent: { key: message.id, message } });
|
|
215
|
-
});
|
|
216
|
-
},
|
|
217
|
-
regenerate: () => dispatch({ type: "regenerate" }),
|
|
218
|
-
stop: () => dispatch({ type: "stop" }),
|
|
219
|
-
newSession: () => dispatch({ type: "newSession" }),
|
|
220
|
-
clearError: () => dispatch({ type: "clearError" }),
|
|
221
|
-
addToolApprovalResponse(response) {
|
|
222
|
-
const { chat, epoch: at } = conversation;
|
|
223
|
-
Promise.resolve(chat.addToolApprovalResponse(response)).then(() => {
|
|
224
|
-
if (lastAssistantMessageIsCompleteWithApprovalResponses({
|
|
225
|
-
messages: chat.messages,
|
|
226
|
-
})) {
|
|
227
|
-
dispatch({ type: "approvalAnswered" }, at);
|
|
228
|
-
}
|
|
229
|
-
});
|
|
230
|
-
},
|
|
231
|
-
};
|
|
232
|
-
}
|
|
233
|
-
function turnResult({ finishReason, isAbort, isError, messages, }, chat) {
|
|
234
|
-
if (isAbort) {
|
|
235
|
-
return { outcome: "stopped" };
|
|
236
|
-
}
|
|
237
|
-
if (isError || finishReason === "error") {
|
|
238
|
-
const error = chat.error ?? new Error("The reply failed.");
|
|
239
|
-
return { outcome: "failed", error, sessionGone: sessionGone(error) };
|
|
240
|
-
}
|
|
241
|
-
return { outcome: "closed", messages };
|
|
242
|
-
}
|
|
243
|
-
/** The relay answers 404 for a deleted Session or another End-user's. */
|
|
244
|
-
function sessionGone(error) {
|
|
245
|
-
return BlazingAgentsError.isInstance(error) && error.status === 404;
|
|
19
|
+
chat.start();
|
|
20
|
+
return chat.dispose;
|
|
21
|
+
}, [chat]);
|
|
22
|
+
// After every commit, since header values can change inside the same object.
|
|
23
|
+
useEffect(() => chat.setHeaders(readHeaders));
|
|
24
|
+
return useMemo(() => ({
|
|
25
|
+
...snapshot,
|
|
26
|
+
addToolApprovalResponse: chat.addToolApprovalResponse,
|
|
27
|
+
clearError: chat.clearError,
|
|
28
|
+
newSession: chat.newSession,
|
|
29
|
+
regenerate: chat.regenerate,
|
|
30
|
+
send: chat.send,
|
|
31
|
+
stop: chat.stop,
|
|
32
|
+
}), [chat, snapshot]);
|
|
246
33
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@blazingagents/sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.54.0",
|
|
4
4
|
"description": "TypeScript SDK for the Blazing Agents API",
|
|
5
5
|
"homepage": "https://docs.blazingagents.com",
|
|
6
6
|
"bugs": {
|
|
@@ -38,7 +38,8 @@
|
|
|
38
38
|
],
|
|
39
39
|
"scripts": {
|
|
40
40
|
"build": "rm -rf dist && tsc -p tsconfig.build.json",
|
|
41
|
-
"
|
|
41
|
+
"bundle-size": "node scripts/check-bundle-size.ts",
|
|
42
|
+
"check": "npm run typecheck && npm run test && npm run bundle-size && npm run lint && npm run unused",
|
|
42
43
|
"fix": "ultracite fix",
|
|
43
44
|
"lint": "ultracite check",
|
|
44
45
|
"prepack": "npm run build",
|
|
@@ -47,11 +48,7 @@
|
|
|
47
48
|
"typecheck": "tsc --noEmit -p tsconfig.json && tsc --noEmit -p tsconfig.tests.json",
|
|
48
49
|
"unused": "knip && knip --production --include exports,types,duplicates"
|
|
49
50
|
},
|
|
50
|
-
"dependencies": {
|
|
51
|
-
"croner": "10.0.1"
|
|
52
|
-
},
|
|
53
51
|
"devDependencies": {
|
|
54
|
-
"@ai-sdk/react": "^4.0.90",
|
|
55
52
|
"@biomejs/biome": "^2.5.8",
|
|
56
53
|
"@testing-library/react": "^16.3.3",
|
|
57
54
|
"@types/json-schema": "7.0.15",
|
|
@@ -60,6 +57,7 @@
|
|
|
60
57
|
"@types/react-dom": "^19.2.3",
|
|
61
58
|
"@vitest/coverage-v8": "^4.1.10",
|
|
62
59
|
"ai": "^7.0.84",
|
|
60
|
+
"esbuild": "0.28.2",
|
|
63
61
|
"happy-dom": "^20.14.6",
|
|
64
62
|
"knip": "^6.41.0",
|
|
65
63
|
"react": "^19.2.4",
|
|
@@ -70,15 +68,11 @@
|
|
|
70
68
|
"zod": "4.5.4"
|
|
71
69
|
},
|
|
72
70
|
"peerDependencies": {
|
|
73
|
-
"@ai-sdk/react": "^4.0.87",
|
|
74
71
|
"ai": "^7.0.84",
|
|
75
72
|
"react": "^18 || ^19",
|
|
76
73
|
"zod": "^4.5.4"
|
|
77
74
|
},
|
|
78
75
|
"peerDependenciesMeta": {
|
|
79
|
-
"@ai-sdk/react": {
|
|
80
|
-
"optional": true
|
|
81
|
-
},
|
|
82
76
|
"react": {
|
|
83
77
|
"optional": true
|
|
84
78
|
}
|