@personaai/react 0.1.0 → 0.1.2
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/dist/index.cjs +634 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +218 -0
- package/dist/index.d.ts +218 -0
- package/dist/index.js +599 -0
- package/dist/index.js.map +1 -0
- package/package.json +1 -1
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,634 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
PersonaProvider: () => PersonaProvider,
|
|
24
|
+
VERSION: () => VERSION,
|
|
25
|
+
useAgents: () => useAgents,
|
|
26
|
+
useChat: () => useChat,
|
|
27
|
+
useConnection: () => useConnection,
|
|
28
|
+
useFiles: () => useFiles,
|
|
29
|
+
useMemory: () => useMemory,
|
|
30
|
+
usePersonaContext: () => usePersonaContext,
|
|
31
|
+
useThreads: () => useThreads
|
|
32
|
+
});
|
|
33
|
+
module.exports = __toCommonJS(index_exports);
|
|
34
|
+
|
|
35
|
+
// src/context/PersonaContext.tsx
|
|
36
|
+
var import_react = require("react");
|
|
37
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
38
|
+
var PersonaContext = (0, import_react.createContext)(null);
|
|
39
|
+
function PersonaProvider({
|
|
40
|
+
baseUrl,
|
|
41
|
+
getAuthToken,
|
|
42
|
+
defaultAgentId,
|
|
43
|
+
children
|
|
44
|
+
}) {
|
|
45
|
+
const normalizedBaseUrl = (0, import_react.useMemo)(() => baseUrl.replace(/\/+$/, ""), [baseUrl]);
|
|
46
|
+
const value = (0, import_react.useMemo)(() => {
|
|
47
|
+
async function fetchWithAuth(path, init = {}) {
|
|
48
|
+
const token = getAuthToken ? await getAuthToken() : null;
|
|
49
|
+
const headers = new Headers(init.headers || {});
|
|
50
|
+
if (token) {
|
|
51
|
+
headers.set("Authorization", `Bearer ${token}`);
|
|
52
|
+
}
|
|
53
|
+
const cleanPath = path.startsWith("/") ? path : `/${path}`;
|
|
54
|
+
const url = `${normalizedBaseUrl}${cleanPath}`;
|
|
55
|
+
return fetch(url, {
|
|
56
|
+
...init,
|
|
57
|
+
headers
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
return {
|
|
61
|
+
baseUrl: normalizedBaseUrl,
|
|
62
|
+
getAuthToken,
|
|
63
|
+
defaultAgentId,
|
|
64
|
+
fetchWithAuth
|
|
65
|
+
};
|
|
66
|
+
}, [normalizedBaseUrl, getAuthToken, defaultAgentId]);
|
|
67
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(PersonaContext.Provider, { value, children });
|
|
68
|
+
}
|
|
69
|
+
function usePersonaContext() {
|
|
70
|
+
const context = (0, import_react.useContext)(PersonaContext);
|
|
71
|
+
if (!context) {
|
|
72
|
+
throw new Error("usePersonaContext must be used within a <PersonaProvider>");
|
|
73
|
+
}
|
|
74
|
+
return context;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// src/hooks/useChat.ts
|
|
78
|
+
var import_react2 = require("react");
|
|
79
|
+
function useChat(options = {}) {
|
|
80
|
+
const { defaultAgentId, fetchWithAuth } = usePersonaContext();
|
|
81
|
+
const agentId = options.agentId || defaultAgentId;
|
|
82
|
+
const [messages, setMessages] = (0, import_react2.useState)(options.initialMessages || []);
|
|
83
|
+
const [input, setInput] = (0, import_react2.useState)("");
|
|
84
|
+
const [isStreaming, setIsStreaming] = (0, import_react2.useState)(false);
|
|
85
|
+
const [error, setError] = (0, import_react2.useState)(null);
|
|
86
|
+
const abortControllerRef = (0, import_react2.useRef)(null);
|
|
87
|
+
const stop = (0, import_react2.useCallback)(() => {
|
|
88
|
+
if (abortControllerRef.current) {
|
|
89
|
+
abortControllerRef.current.abort();
|
|
90
|
+
abortControllerRef.current = null;
|
|
91
|
+
setIsStreaming(false);
|
|
92
|
+
}
|
|
93
|
+
}, []);
|
|
94
|
+
const clear = (0, import_react2.useCallback)(() => {
|
|
95
|
+
stop();
|
|
96
|
+
setMessages([]);
|
|
97
|
+
setError(null);
|
|
98
|
+
}, [stop]);
|
|
99
|
+
const sendMessage = (0, import_react2.useCallback)(
|
|
100
|
+
async (contentToSend, overrideOptions) => {
|
|
101
|
+
const prompt = (contentToSend ?? input).trim();
|
|
102
|
+
if (!prompt || isStreaming) return;
|
|
103
|
+
const targetAgentId = overrideOptions?.agentId || agentId;
|
|
104
|
+
if (!targetAgentId) {
|
|
105
|
+
const err = new Error("No Agent ID specified for useChat.");
|
|
106
|
+
setError(err);
|
|
107
|
+
options.onError?.(err);
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
const userMessage = {
|
|
111
|
+
id: `user-${Date.now()}-${Math.random().toString(36).slice(2, 7)}`,
|
|
112
|
+
role: "user",
|
|
113
|
+
content: prompt,
|
|
114
|
+
createdAt: /* @__PURE__ */ new Date()
|
|
115
|
+
};
|
|
116
|
+
const assistantMessageId = `assistant-${Date.now()}-${Math.random().toString(36).slice(2, 7)}`;
|
|
117
|
+
const placeholderAssistant = {
|
|
118
|
+
id: assistantMessageId,
|
|
119
|
+
role: "assistant",
|
|
120
|
+
content: "",
|
|
121
|
+
createdAt: /* @__PURE__ */ new Date(),
|
|
122
|
+
isStreaming: true,
|
|
123
|
+
toolCalls: []
|
|
124
|
+
};
|
|
125
|
+
const nextMessages = [...messages, userMessage];
|
|
126
|
+
setMessages([...nextMessages, placeholderAssistant]);
|
|
127
|
+
setInput("");
|
|
128
|
+
setIsStreaming(true);
|
|
129
|
+
setError(null);
|
|
130
|
+
const controller = new AbortController();
|
|
131
|
+
abortControllerRef.current = controller;
|
|
132
|
+
try {
|
|
133
|
+
const payloadMessages = nextMessages.map((m) => ({
|
|
134
|
+
role: m.role === "assistant" ? "assistant" : "user",
|
|
135
|
+
content: m.content
|
|
136
|
+
}));
|
|
137
|
+
const response = await fetchWithAuth("/chat", {
|
|
138
|
+
method: "POST",
|
|
139
|
+
headers: {
|
|
140
|
+
"Content-Type": "application/json"
|
|
141
|
+
},
|
|
142
|
+
body: JSON.stringify({
|
|
143
|
+
agentId: targetAgentId,
|
|
144
|
+
messages: payloadMessages,
|
|
145
|
+
threadId: overrideOptions?.threadId || options.threadId
|
|
146
|
+
}),
|
|
147
|
+
signal: controller.signal
|
|
148
|
+
});
|
|
149
|
+
if (!response.ok) {
|
|
150
|
+
const errText = await response.text().catch(() => "Stream failed");
|
|
151
|
+
throw new Error(`Chat error (${response.status}): ${errText}`);
|
|
152
|
+
}
|
|
153
|
+
if (!response.body) {
|
|
154
|
+
throw new Error("No response body received for streaming.");
|
|
155
|
+
}
|
|
156
|
+
const reader = response.body.getReader();
|
|
157
|
+
const decoder = new TextDecoder("utf-8");
|
|
158
|
+
let buffer = "";
|
|
159
|
+
let accumulatedText = "";
|
|
160
|
+
const toolCallsMap = /* @__PURE__ */ new Map();
|
|
161
|
+
while (true) {
|
|
162
|
+
const { done, value } = await reader.read();
|
|
163
|
+
if (done) break;
|
|
164
|
+
buffer += decoder.decode(value, { stream: true });
|
|
165
|
+
const lines = buffer.split("\n");
|
|
166
|
+
buffer = lines.pop() ?? "";
|
|
167
|
+
for (const line of lines) {
|
|
168
|
+
const trimmed = line.trim();
|
|
169
|
+
if (!trimmed || !trimmed.startsWith("data:")) continue;
|
|
170
|
+
const raw = trimmed.replace(/^data:\s*/, "");
|
|
171
|
+
if (raw === "[DONE]") break;
|
|
172
|
+
try {
|
|
173
|
+
const event = JSON.parse(raw);
|
|
174
|
+
options.onEvent?.(event);
|
|
175
|
+
if (event.type === "TEXT_MESSAGE_CHUNK" && event.delta) {
|
|
176
|
+
accumulatedText += event.delta;
|
|
177
|
+
setMessages(
|
|
178
|
+
(prev) => prev.map(
|
|
179
|
+
(msg) => msg.id === assistantMessageId ? {
|
|
180
|
+
...msg,
|
|
181
|
+
content: accumulatedText,
|
|
182
|
+
isStreaming: true,
|
|
183
|
+
toolCalls: Array.from(toolCallsMap.values())
|
|
184
|
+
} : msg
|
|
185
|
+
)
|
|
186
|
+
);
|
|
187
|
+
} else if (event.type === "TOOL_CALL_START") {
|
|
188
|
+
toolCallsMap.set(event.toolCallId, {
|
|
189
|
+
toolCallId: event.toolCallId,
|
|
190
|
+
toolName: event.toolName,
|
|
191
|
+
args: ""
|
|
192
|
+
});
|
|
193
|
+
setMessages(
|
|
194
|
+
(prev) => prev.map(
|
|
195
|
+
(msg) => msg.id === assistantMessageId ? { ...msg, toolCalls: Array.from(toolCallsMap.values()) } : msg
|
|
196
|
+
)
|
|
197
|
+
);
|
|
198
|
+
} else if (event.type === "TOOL_CALL_ARGS") {
|
|
199
|
+
const existing = toolCallsMap.get(event.toolCallId);
|
|
200
|
+
if (existing) {
|
|
201
|
+
existing.args = (existing.args || "") + event.delta;
|
|
202
|
+
setMessages(
|
|
203
|
+
(prev) => prev.map(
|
|
204
|
+
(msg) => msg.id === assistantMessageId ? { ...msg, toolCalls: Array.from(toolCallsMap.values()) } : msg
|
|
205
|
+
)
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
} else if (event.type === "TOOL_CALL_RESULT") {
|
|
209
|
+
const existing = toolCallsMap.get(event.toolCallId);
|
|
210
|
+
if (existing) {
|
|
211
|
+
existing.result = event.result;
|
|
212
|
+
existing.isError = event.isError;
|
|
213
|
+
setMessages(
|
|
214
|
+
(prev) => prev.map(
|
|
215
|
+
(msg) => msg.id === assistantMessageId ? { ...msg, toolCalls: Array.from(toolCallsMap.values()) } : msg
|
|
216
|
+
)
|
|
217
|
+
);
|
|
218
|
+
}
|
|
219
|
+
} else if (event.type === "RUN_ERROR") {
|
|
220
|
+
throw new Error(event.message || "Stream error from agent");
|
|
221
|
+
}
|
|
222
|
+
} catch (e) {
|
|
223
|
+
if (e instanceof Error && e.message.startsWith("Stream error")) {
|
|
224
|
+
throw e;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
const finalMessage = {
|
|
230
|
+
id: assistantMessageId,
|
|
231
|
+
role: "assistant",
|
|
232
|
+
content: accumulatedText,
|
|
233
|
+
createdAt: /* @__PURE__ */ new Date(),
|
|
234
|
+
isStreaming: false,
|
|
235
|
+
toolCalls: Array.from(toolCallsMap.values())
|
|
236
|
+
};
|
|
237
|
+
setMessages(
|
|
238
|
+
(prev) => prev.map((msg) => msg.id === assistantMessageId ? finalMessage : msg)
|
|
239
|
+
);
|
|
240
|
+
options.onFinish?.(finalMessage);
|
|
241
|
+
} catch (err) {
|
|
242
|
+
if (controller.signal.aborted) {
|
|
243
|
+
setMessages(
|
|
244
|
+
(prev) => prev.map(
|
|
245
|
+
(msg) => msg.id === assistantMessageId ? { ...msg, isStreaming: false } : msg
|
|
246
|
+
)
|
|
247
|
+
);
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
const errorObj = err instanceof Error ? err : new Error(String(err));
|
|
251
|
+
setError(errorObj);
|
|
252
|
+
options.onError?.(errorObj);
|
|
253
|
+
setMessages(
|
|
254
|
+
(prev) => prev.map(
|
|
255
|
+
(msg) => msg.id === assistantMessageId ? {
|
|
256
|
+
...msg,
|
|
257
|
+
content: msg.content || `\u26A0\uFE0F Error: ${errorObj.message || "Failed to get response."}`,
|
|
258
|
+
isStreaming: false
|
|
259
|
+
} : msg
|
|
260
|
+
)
|
|
261
|
+
);
|
|
262
|
+
} finally {
|
|
263
|
+
setIsStreaming(false);
|
|
264
|
+
abortControllerRef.current = null;
|
|
265
|
+
}
|
|
266
|
+
},
|
|
267
|
+
[agentId, fetchWithAuth, input, isStreaming, messages, options]
|
|
268
|
+
);
|
|
269
|
+
const handleInputChange = (0, import_react2.useCallback)((e) => {
|
|
270
|
+
setInput(e.target.value);
|
|
271
|
+
}, []);
|
|
272
|
+
const handleSubmit = (0, import_react2.useCallback)(
|
|
273
|
+
(e) => {
|
|
274
|
+
if (e) e.preventDefault();
|
|
275
|
+
void sendMessage();
|
|
276
|
+
},
|
|
277
|
+
[sendMessage]
|
|
278
|
+
);
|
|
279
|
+
const reload = (0, import_react2.useCallback)(() => {
|
|
280
|
+
if (messages.length === 0 || isStreaming) return;
|
|
281
|
+
let lastUserIndex = -1;
|
|
282
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
283
|
+
if (messages[i].role === "user") {
|
|
284
|
+
lastUserIndex = i;
|
|
285
|
+
break;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
if (lastUserIndex === -1) return;
|
|
289
|
+
const lastUserMessage = messages[lastUserIndex];
|
|
290
|
+
setMessages(messages.slice(0, lastUserIndex));
|
|
291
|
+
void sendMessage(lastUserMessage.content);
|
|
292
|
+
}, [isStreaming, messages, sendMessage]);
|
|
293
|
+
return {
|
|
294
|
+
messages,
|
|
295
|
+
input,
|
|
296
|
+
setInput,
|
|
297
|
+
handleInputChange,
|
|
298
|
+
handleSubmit,
|
|
299
|
+
sendMessage,
|
|
300
|
+
isStreaming,
|
|
301
|
+
isLoading: isStreaming,
|
|
302
|
+
error,
|
|
303
|
+
stop,
|
|
304
|
+
reload,
|
|
305
|
+
clear,
|
|
306
|
+
setMessages
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
// src/hooks/useMemory.ts
|
|
311
|
+
var import_react3 = require("react");
|
|
312
|
+
function useMemory(autoFetch = true) {
|
|
313
|
+
const { fetchWithAuth } = usePersonaContext();
|
|
314
|
+
const [memory, setMemory] = (0, import_react3.useState)({ user: [], agents: {} });
|
|
315
|
+
const [isLoading, setIsLoading] = (0, import_react3.useState)(false);
|
|
316
|
+
const [error, setError] = (0, import_react3.useState)(null);
|
|
317
|
+
const fetchMemory = (0, import_react3.useCallback)(async () => {
|
|
318
|
+
setIsLoading(true);
|
|
319
|
+
setError(null);
|
|
320
|
+
try {
|
|
321
|
+
const res = await fetchWithAuth("/memory");
|
|
322
|
+
if (!res.ok) {
|
|
323
|
+
throw new Error(`Failed to fetch memory: ${res.statusText}`);
|
|
324
|
+
}
|
|
325
|
+
const data = await res.json();
|
|
326
|
+
setMemory(data);
|
|
327
|
+
return data;
|
|
328
|
+
} catch (err) {
|
|
329
|
+
const errorObj = err instanceof Error ? err : new Error(String(err));
|
|
330
|
+
setError(errorObj);
|
|
331
|
+
return { user: [], agents: {} };
|
|
332
|
+
} finally {
|
|
333
|
+
setIsLoading(false);
|
|
334
|
+
}
|
|
335
|
+
}, [fetchWithAuth]);
|
|
336
|
+
const getFile = (0, import_react3.useCallback)(
|
|
337
|
+
async (params) => {
|
|
338
|
+
const query = new URLSearchParams({ path: params.path });
|
|
339
|
+
if (params.scope) query.set("scope", params.scope);
|
|
340
|
+
if (params.agentId) query.set("agentId", params.agentId);
|
|
341
|
+
const res = await fetchWithAuth(`/memory/file?${query.toString()}`);
|
|
342
|
+
if (!res.ok) throw new Error(`Failed to get memory file: ${res.statusText}`);
|
|
343
|
+
return await res.json();
|
|
344
|
+
},
|
|
345
|
+
[fetchWithAuth]
|
|
346
|
+
);
|
|
347
|
+
const writeFile = (0, import_react3.useCallback)(
|
|
348
|
+
async (params) => {
|
|
349
|
+
const res = await fetchWithAuth("/memory/file", {
|
|
350
|
+
method: "PUT",
|
|
351
|
+
headers: { "Content-Type": "application/json" },
|
|
352
|
+
body: JSON.stringify(params)
|
|
353
|
+
});
|
|
354
|
+
if (!res.ok) throw new Error(`Failed to write memory file: ${res.statusText}`);
|
|
355
|
+
const updated = await res.json();
|
|
356
|
+
void fetchMemory();
|
|
357
|
+
return updated;
|
|
358
|
+
},
|
|
359
|
+
[fetchWithAuth, fetchMemory]
|
|
360
|
+
);
|
|
361
|
+
const deleteFile = (0, import_react3.useCallback)(
|
|
362
|
+
async (params) => {
|
|
363
|
+
const query = new URLSearchParams({ path: params.path });
|
|
364
|
+
if (params.scope) query.set("scope", params.scope);
|
|
365
|
+
if (params.agentId) query.set("agentId", params.agentId);
|
|
366
|
+
const res = await fetchWithAuth(`/memory/file?${query.toString()}`, {
|
|
367
|
+
method: "DELETE"
|
|
368
|
+
});
|
|
369
|
+
if (!res.ok) throw new Error(`Failed to delete memory file: ${res.statusText}`);
|
|
370
|
+
void fetchMemory();
|
|
371
|
+
},
|
|
372
|
+
[fetchWithAuth, fetchMemory]
|
|
373
|
+
);
|
|
374
|
+
(0, import_react3.useEffect)(() => {
|
|
375
|
+
if (autoFetch) {
|
|
376
|
+
void fetchMemory();
|
|
377
|
+
}
|
|
378
|
+
}, [autoFetch, fetchMemory]);
|
|
379
|
+
return {
|
|
380
|
+
memory,
|
|
381
|
+
isLoading,
|
|
382
|
+
error,
|
|
383
|
+
refetch: fetchMemory,
|
|
384
|
+
getFile,
|
|
385
|
+
writeFile,
|
|
386
|
+
deleteFile
|
|
387
|
+
};
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
// src/hooks/useThreads.ts
|
|
391
|
+
var import_react4 = require("react");
|
|
392
|
+
function useThreads(autoFetch = true) {
|
|
393
|
+
const { fetchWithAuth, defaultAgentId } = usePersonaContext();
|
|
394
|
+
const [threads, setThreads] = (0, import_react4.useState)([]);
|
|
395
|
+
const [isLoading, setIsLoading] = (0, import_react4.useState)(false);
|
|
396
|
+
const [error, setError] = (0, import_react4.useState)(null);
|
|
397
|
+
const fetchThreads = (0, import_react4.useCallback)(async () => {
|
|
398
|
+
setIsLoading(true);
|
|
399
|
+
setError(null);
|
|
400
|
+
try {
|
|
401
|
+
const res = await fetchWithAuth("/threads");
|
|
402
|
+
if (!res.ok) throw new Error(`Failed to list threads: ${res.statusText}`);
|
|
403
|
+
const data = await res.json();
|
|
404
|
+
const items = Array.isArray(data) ? data : data?.threads || data?.items || [];
|
|
405
|
+
setThreads(items);
|
|
406
|
+
return items;
|
|
407
|
+
} catch (err) {
|
|
408
|
+
const errorObj = err instanceof Error ? err : new Error(String(err));
|
|
409
|
+
setError(errorObj);
|
|
410
|
+
return [];
|
|
411
|
+
} finally {
|
|
412
|
+
setIsLoading(false);
|
|
413
|
+
}
|
|
414
|
+
}, [fetchWithAuth]);
|
|
415
|
+
const createThread = (0, import_react4.useCallback)(
|
|
416
|
+
async (agentId) => {
|
|
417
|
+
const targetAgentId = agentId || defaultAgentId;
|
|
418
|
+
const res = await fetchWithAuth("/threads", {
|
|
419
|
+
method: "POST",
|
|
420
|
+
headers: { "Content-Type": "application/json" },
|
|
421
|
+
body: JSON.stringify({ agentId: targetAgentId })
|
|
422
|
+
});
|
|
423
|
+
if (!res.ok) throw new Error(`Failed to create thread: ${res.statusText}`);
|
|
424
|
+
const created = await res.json();
|
|
425
|
+
void fetchThreads();
|
|
426
|
+
return created;
|
|
427
|
+
},
|
|
428
|
+
[fetchWithAuth, defaultAgentId, fetchThreads]
|
|
429
|
+
);
|
|
430
|
+
const deleteThread = (0, import_react4.useCallback)(
|
|
431
|
+
async (threadId) => {
|
|
432
|
+
const res = await fetchWithAuth(`/threads/${threadId}`, { method: "DELETE" });
|
|
433
|
+
if (!res.ok) throw new Error(`Failed to delete thread: ${res.statusText}`);
|
|
434
|
+
setThreads((prev) => prev.filter((t) => t._id !== threadId));
|
|
435
|
+
},
|
|
436
|
+
[fetchWithAuth]
|
|
437
|
+
);
|
|
438
|
+
(0, import_react4.useEffect)(() => {
|
|
439
|
+
if (autoFetch) {
|
|
440
|
+
void fetchThreads();
|
|
441
|
+
}
|
|
442
|
+
}, [autoFetch, fetchThreads]);
|
|
443
|
+
return {
|
|
444
|
+
threads,
|
|
445
|
+
isLoading,
|
|
446
|
+
error,
|
|
447
|
+
refetch: fetchThreads,
|
|
448
|
+
createThread,
|
|
449
|
+
deleteThread
|
|
450
|
+
};
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
// src/hooks/useFiles.ts
|
|
454
|
+
var import_react5 = require("react");
|
|
455
|
+
function useFiles(autoFetch = true) {
|
|
456
|
+
const { fetchWithAuth } = usePersonaContext();
|
|
457
|
+
const [files, setFiles] = (0, import_react5.useState)([]);
|
|
458
|
+
const [isLoading, setIsLoading] = (0, import_react5.useState)(false);
|
|
459
|
+
const [isUploading, setIsUploading] = (0, import_react5.useState)(false);
|
|
460
|
+
const [error, setError] = (0, import_react5.useState)(null);
|
|
461
|
+
const fetchFiles = (0, import_react5.useCallback)(async () => {
|
|
462
|
+
setIsLoading(true);
|
|
463
|
+
setError(null);
|
|
464
|
+
try {
|
|
465
|
+
const res = await fetchWithAuth("/files");
|
|
466
|
+
if (!res.ok) throw new Error(`Failed to list files: ${res.statusText}`);
|
|
467
|
+
const data = await res.json();
|
|
468
|
+
const items = Array.isArray(data) ? data : data?.files || data?.items || [];
|
|
469
|
+
setFiles(items);
|
|
470
|
+
return items;
|
|
471
|
+
} catch (err) {
|
|
472
|
+
const errorObj = err instanceof Error ? err : new Error(String(err));
|
|
473
|
+
setError(errorObj);
|
|
474
|
+
return [];
|
|
475
|
+
} finally {
|
|
476
|
+
setIsLoading(false);
|
|
477
|
+
}
|
|
478
|
+
}, [fetchWithAuth]);
|
|
479
|
+
const uploadFile = (0, import_react5.useCallback)(
|
|
480
|
+
async (fileOrFormData) => {
|
|
481
|
+
setIsUploading(true);
|
|
482
|
+
setError(null);
|
|
483
|
+
try {
|
|
484
|
+
let body;
|
|
485
|
+
if (fileOrFormData instanceof FormData) {
|
|
486
|
+
body = fileOrFormData;
|
|
487
|
+
} else {
|
|
488
|
+
body = new FormData();
|
|
489
|
+
body.append("file", fileOrFormData);
|
|
490
|
+
}
|
|
491
|
+
const res = await fetchWithAuth("/files", {
|
|
492
|
+
method: "POST",
|
|
493
|
+
body
|
|
494
|
+
});
|
|
495
|
+
if (!res.ok) {
|
|
496
|
+
const errText = await res.text().catch(() => "Upload failed");
|
|
497
|
+
throw new Error(`Upload error (${res.status}): ${errText}`);
|
|
498
|
+
}
|
|
499
|
+
const uploaded = await res.json();
|
|
500
|
+
void fetchFiles();
|
|
501
|
+
return uploaded;
|
|
502
|
+
} catch (err) {
|
|
503
|
+
const errorObj = err instanceof Error ? err : new Error(String(err));
|
|
504
|
+
setError(errorObj);
|
|
505
|
+
throw errorObj;
|
|
506
|
+
} finally {
|
|
507
|
+
setIsUploading(false);
|
|
508
|
+
}
|
|
509
|
+
},
|
|
510
|
+
[fetchWithAuth, fetchFiles]
|
|
511
|
+
);
|
|
512
|
+
const deleteFile = (0, import_react5.useCallback)(
|
|
513
|
+
async (fileId) => {
|
|
514
|
+
const res = await fetchWithAuth(`/files/${fileId}`, { method: "DELETE" });
|
|
515
|
+
if (!res.ok) throw new Error(`Failed to delete file: ${res.statusText}`);
|
|
516
|
+
setFiles((prev) => prev.filter((f) => f._id !== fileId));
|
|
517
|
+
},
|
|
518
|
+
[fetchWithAuth]
|
|
519
|
+
);
|
|
520
|
+
const getDownloadUrl = (0, import_react5.useCallback)(
|
|
521
|
+
(fileId) => {
|
|
522
|
+
return `/files/${fileId}`;
|
|
523
|
+
},
|
|
524
|
+
[]
|
|
525
|
+
);
|
|
526
|
+
(0, import_react5.useEffect)(() => {
|
|
527
|
+
if (autoFetch) {
|
|
528
|
+
void fetchFiles();
|
|
529
|
+
}
|
|
530
|
+
}, [autoFetch, fetchFiles]);
|
|
531
|
+
return {
|
|
532
|
+
files,
|
|
533
|
+
isLoading,
|
|
534
|
+
isUploading,
|
|
535
|
+
error,
|
|
536
|
+
refetch: fetchFiles,
|
|
537
|
+
uploadFile,
|
|
538
|
+
deleteFile,
|
|
539
|
+
getDownloadUrl
|
|
540
|
+
};
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
// src/hooks/useAgents.ts
|
|
544
|
+
var import_react6 = require("react");
|
|
545
|
+
function useAgents(autoFetch = true) {
|
|
546
|
+
const { fetchWithAuth } = usePersonaContext();
|
|
547
|
+
const [agents, setAgents] = (0, import_react6.useState)([]);
|
|
548
|
+
const [isLoading, setIsLoading] = (0, import_react6.useState)(false);
|
|
549
|
+
const [error, setError] = (0, import_react6.useState)(null);
|
|
550
|
+
const fetchAgents = (0, import_react6.useCallback)(async () => {
|
|
551
|
+
setIsLoading(true);
|
|
552
|
+
setError(null);
|
|
553
|
+
try {
|
|
554
|
+
const res = await fetchWithAuth("/agents");
|
|
555
|
+
if (!res.ok) throw new Error(`Failed to list agents: ${res.statusText}`);
|
|
556
|
+
const data = await res.json();
|
|
557
|
+
const items = Array.isArray(data) ? data : data?.agents || data?.items || [];
|
|
558
|
+
setAgents(items);
|
|
559
|
+
return items;
|
|
560
|
+
} catch (err) {
|
|
561
|
+
const errorObj = err instanceof Error ? err : new Error(String(err));
|
|
562
|
+
setError(errorObj);
|
|
563
|
+
return [];
|
|
564
|
+
} finally {
|
|
565
|
+
setIsLoading(false);
|
|
566
|
+
}
|
|
567
|
+
}, [fetchWithAuth]);
|
|
568
|
+
(0, import_react6.useEffect)(() => {
|
|
569
|
+
if (autoFetch) {
|
|
570
|
+
void fetchAgents();
|
|
571
|
+
}
|
|
572
|
+
}, [autoFetch, fetchAgents]);
|
|
573
|
+
return {
|
|
574
|
+
agents,
|
|
575
|
+
isLoading,
|
|
576
|
+
error,
|
|
577
|
+
refetch: fetchAgents
|
|
578
|
+
};
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
// src/hooks/useConnection.ts
|
|
582
|
+
var import_react7 = require("react");
|
|
583
|
+
function useConnection(autoCheck = true) {
|
|
584
|
+
const { fetchWithAuth } = usePersonaContext();
|
|
585
|
+
const [health, setHealth] = (0, import_react7.useState)(null);
|
|
586
|
+
const [isConnected, setIsConnected] = (0, import_react7.useState)(false);
|
|
587
|
+
const [isLoading, setIsLoading] = (0, import_react7.useState)(false);
|
|
588
|
+
const checkHealth = (0, import_react7.useCallback)(async () => {
|
|
589
|
+
setIsLoading(true);
|
|
590
|
+
try {
|
|
591
|
+
const res = await fetchWithAuth("/health");
|
|
592
|
+
if (res.ok) {
|
|
593
|
+
const data = await res.json();
|
|
594
|
+
setHealth(data);
|
|
595
|
+
setIsConnected(true);
|
|
596
|
+
return data;
|
|
597
|
+
}
|
|
598
|
+
setIsConnected(false);
|
|
599
|
+
return null;
|
|
600
|
+
} catch {
|
|
601
|
+
setIsConnected(false);
|
|
602
|
+
return null;
|
|
603
|
+
} finally {
|
|
604
|
+
setIsLoading(false);
|
|
605
|
+
}
|
|
606
|
+
}, [fetchWithAuth]);
|
|
607
|
+
(0, import_react7.useEffect)(() => {
|
|
608
|
+
if (autoCheck) {
|
|
609
|
+
void checkHealth();
|
|
610
|
+
}
|
|
611
|
+
}, [autoCheck, checkHealth]);
|
|
612
|
+
return {
|
|
613
|
+
isConnected,
|
|
614
|
+
health,
|
|
615
|
+
isLoading,
|
|
616
|
+
checkHealth
|
|
617
|
+
};
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
// src/index.ts
|
|
621
|
+
var VERSION = "0.1.2";
|
|
622
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
623
|
+
0 && (module.exports = {
|
|
624
|
+
PersonaProvider,
|
|
625
|
+
VERSION,
|
|
626
|
+
useAgents,
|
|
627
|
+
useChat,
|
|
628
|
+
useConnection,
|
|
629
|
+
useFiles,
|
|
630
|
+
useMemory,
|
|
631
|
+
usePersonaContext,
|
|
632
|
+
useThreads
|
|
633
|
+
});
|
|
634
|
+
//# sourceMappingURL=index.cjs.map
|