@copilotz/chat-adapter 0.1.18 → 0.1.19
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.d.ts +85 -84
- package/dist/index.js +45 -42
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import React, { ReactNode } from 'react';
|
|
2
|
-
import { ChatUserContext, ChatConfig, ChatCallbacks, MemoryItem, AgentOption, ChatMessage, ChatThread
|
|
2
|
+
import { MediaAttachment, ChatUserContext, ChatConfig, ChatCallbacks, MemoryItem, AgentOption, ChatMessage, ChatThread } from '@copilotz/chat-ui';
|
|
3
3
|
export { ChatCallbacks, ChatConfig, ChatMessage, ChatThread, ChatUserContext, MediaAttachment, MemoryItem } from '@copilotz/chat-ui';
|
|
4
4
|
|
|
5
5
|
/**
|
|
@@ -105,9 +105,88 @@ type EventInterceptorResult = void | {
|
|
|
105
105
|
type EventInterceptor = (event: unknown) => EventInterceptorResult;
|
|
106
106
|
type RunErrorInterceptor = (error: unknown) => SpecialChatState | null | undefined;
|
|
107
107
|
|
|
108
|
+
type RequestHeadersProvider = () => Record<string, string> | Promise<Record<string, string>>;
|
|
109
|
+
type RestThread = {
|
|
110
|
+
id: string;
|
|
111
|
+
name?: string | null;
|
|
112
|
+
externalId?: string | null;
|
|
113
|
+
description?: string | null;
|
|
114
|
+
participants?: string[] | null;
|
|
115
|
+
status?: string | null;
|
|
116
|
+
metadata?: Record<string, unknown> | null;
|
|
117
|
+
createdAt?: string;
|
|
118
|
+
updatedAt?: string;
|
|
119
|
+
};
|
|
120
|
+
type RestMessage = {
|
|
121
|
+
id: string;
|
|
122
|
+
threadId: string;
|
|
123
|
+
senderId?: string | null;
|
|
124
|
+
senderType: string;
|
|
125
|
+
senderUserId?: string | null;
|
|
126
|
+
content?: string | null;
|
|
127
|
+
metadata?: Record<string, unknown> | null;
|
|
128
|
+
toolCalls?: Array<Record<string, unknown>> | null;
|
|
129
|
+
createdAt?: string;
|
|
130
|
+
updatedAt?: string;
|
|
131
|
+
};
|
|
132
|
+
type StreamCallbacks = {
|
|
133
|
+
onToken?: (token: string, isComplete: boolean, raw?: any) => void;
|
|
134
|
+
onMessageEvent?: (payload: any) => void;
|
|
135
|
+
onAssetEvent?: (payload: any) => void;
|
|
136
|
+
signal?: AbortSignal;
|
|
137
|
+
};
|
|
138
|
+
type RunOptions = {
|
|
139
|
+
threadId?: string;
|
|
140
|
+
threadExternalId?: string;
|
|
141
|
+
content: string;
|
|
142
|
+
user: {
|
|
143
|
+
externalId: string;
|
|
144
|
+
name?: string;
|
|
145
|
+
email?: string;
|
|
146
|
+
metadata?: Record<string, unknown>;
|
|
147
|
+
};
|
|
148
|
+
attachments?: MediaAttachment[];
|
|
149
|
+
metadata?: Record<string, unknown>;
|
|
150
|
+
threadMetadata?: Record<string, unknown>;
|
|
151
|
+
toolCalls?: Array<{
|
|
152
|
+
name: string;
|
|
153
|
+
args: Record<string, unknown>;
|
|
154
|
+
id?: string;
|
|
155
|
+
}>;
|
|
156
|
+
selectedAgent?: string | null;
|
|
157
|
+
getRequestHeaders?: RequestHeadersProvider;
|
|
158
|
+
} & StreamCallbacks;
|
|
159
|
+
type CopilotzStreamResult = {
|
|
160
|
+
text: string;
|
|
161
|
+
messages: any[];
|
|
162
|
+
media: Record<string, string> | null;
|
|
163
|
+
};
|
|
164
|
+
declare class CopilotzRequestError extends Error {
|
|
165
|
+
status: number;
|
|
166
|
+
code?: string;
|
|
167
|
+
details?: unknown;
|
|
168
|
+
constructor(message: string, options: {
|
|
169
|
+
status: number;
|
|
170
|
+
code?: string;
|
|
171
|
+
details?: unknown;
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
declare function runCopilotzStream(options: RunOptions): Promise<CopilotzStreamResult>;
|
|
175
|
+
declare function fetchThreads(userId: string, getRequestHeaders?: RequestHeadersProvider): Promise<RestThread[]>;
|
|
176
|
+
declare function fetchThreadMessages(threadId: string, getRequestHeaders?: RequestHeadersProvider): Promise<RestMessage[]>;
|
|
177
|
+
declare function updateThread(threadId: string, updates: Partial<RestThread>, getRequestHeaders?: RequestHeadersProvider): Promise<any>;
|
|
178
|
+
declare function deleteMessagesByThreadId(threadId: string, getRequestHeaders?: RequestHeadersProvider): Promise<void>;
|
|
179
|
+
declare function deleteThread(threadId: string, getRequestHeaders?: RequestHeadersProvider): Promise<boolean>;
|
|
180
|
+
declare const copilotzService: {
|
|
181
|
+
runCopilotzStream: typeof runCopilotzStream;
|
|
182
|
+
fetchThreads: typeof fetchThreads;
|
|
183
|
+
fetchThreadMessages: typeof fetchThreadMessages;
|
|
184
|
+
updateThread: typeof updateThread;
|
|
185
|
+
deleteThread: typeof deleteThread;
|
|
186
|
+
};
|
|
187
|
+
|
|
108
188
|
interface CopilotzChatProps {
|
|
109
189
|
userId: string;
|
|
110
|
-
authToken?: string | null;
|
|
111
190
|
userName?: string;
|
|
112
191
|
userAvatar?: string;
|
|
113
192
|
userEmail?: string;
|
|
@@ -150,6 +229,7 @@ interface CopilotzChatProps {
|
|
|
150
229
|
agentOptions?: AgentOption[];
|
|
151
230
|
selectedAgentId?: string | null;
|
|
152
231
|
onSelectAgent?: (agentId: string) => void;
|
|
232
|
+
getRequestHeaders?: RequestHeadersProvider;
|
|
153
233
|
className?: string;
|
|
154
234
|
eventInterceptor?: EventInterceptor;
|
|
155
235
|
runErrorInterceptor?: RunErrorInterceptor;
|
|
@@ -187,7 +267,6 @@ declare const CopilotzChat: React.FC<CopilotzChatProps>;
|
|
|
187
267
|
|
|
188
268
|
interface UseCopilotzOptions {
|
|
189
269
|
userId: string | null;
|
|
190
|
-
authToken?: string | null;
|
|
191
270
|
initialContext?: ChatUserContext;
|
|
192
271
|
bootstrap?: {
|
|
193
272
|
initialMessage?: string;
|
|
@@ -199,6 +278,7 @@ interface UseCopilotzOptions {
|
|
|
199
278
|
defaultThreadName?: string;
|
|
200
279
|
onToolOutput?: (output: Record<string, unknown>) => void;
|
|
201
280
|
preferredAgentName?: string | null;
|
|
281
|
+
getRequestHeaders?: RequestHeadersProvider;
|
|
202
282
|
eventInterceptor?: EventInterceptor;
|
|
203
283
|
runErrorInterceptor?: RunErrorInterceptor;
|
|
204
284
|
/**
|
|
@@ -219,7 +299,7 @@ interface UseCopilotzOptions {
|
|
|
219
299
|
*/
|
|
220
300
|
urlSync?: UrlSyncConfig;
|
|
221
301
|
}
|
|
222
|
-
declare function useCopilotz({ userId,
|
|
302
|
+
declare function useCopilotz({ userId, initialContext, bootstrap, defaultThreadName, onToolOutput, preferredAgentName, getRequestHeaders, eventInterceptor, runErrorInterceptor, urlSync, }: UseCopilotzOptions): {
|
|
223
303
|
messages: ChatMessage[];
|
|
224
304
|
isMessagesLoading: boolean;
|
|
225
305
|
threads: ChatThread[];
|
|
@@ -248,85 +328,6 @@ declare function useCopilotz({ userId, authToken, initialContext, bootstrap, def
|
|
|
248
328
|
setUrlAgentId: (agentId: string | null) => void;
|
|
249
329
|
};
|
|
250
330
|
|
|
251
|
-
type RestThread = {
|
|
252
|
-
id: string;
|
|
253
|
-
name?: string | null;
|
|
254
|
-
externalId?: string | null;
|
|
255
|
-
description?: string | null;
|
|
256
|
-
participants?: string[] | null;
|
|
257
|
-
status?: string | null;
|
|
258
|
-
metadata?: Record<string, unknown> | null;
|
|
259
|
-
createdAt?: string;
|
|
260
|
-
updatedAt?: string;
|
|
261
|
-
};
|
|
262
|
-
type RestMessage = {
|
|
263
|
-
id: string;
|
|
264
|
-
threadId: string;
|
|
265
|
-
senderId?: string | null;
|
|
266
|
-
senderType: string;
|
|
267
|
-
senderUserId?: string | null;
|
|
268
|
-
content?: string | null;
|
|
269
|
-
metadata?: Record<string, unknown> | null;
|
|
270
|
-
toolCalls?: Array<Record<string, unknown>> | null;
|
|
271
|
-
createdAt?: string;
|
|
272
|
-
updatedAt?: string;
|
|
273
|
-
};
|
|
274
|
-
type StreamCallbacks = {
|
|
275
|
-
onToken?: (token: string, isComplete: boolean, raw?: any) => void;
|
|
276
|
-
onMessageEvent?: (payload: any) => void;
|
|
277
|
-
onAssetEvent?: (payload: any) => void;
|
|
278
|
-
signal?: AbortSignal;
|
|
279
|
-
};
|
|
280
|
-
type RunOptions = {
|
|
281
|
-
authToken?: string | null;
|
|
282
|
-
threadId?: string;
|
|
283
|
-
threadExternalId?: string;
|
|
284
|
-
content: string;
|
|
285
|
-
user: {
|
|
286
|
-
externalId: string;
|
|
287
|
-
name?: string;
|
|
288
|
-
email?: string;
|
|
289
|
-
metadata?: Record<string, unknown>;
|
|
290
|
-
};
|
|
291
|
-
attachments?: MediaAttachment[];
|
|
292
|
-
metadata?: Record<string, unknown>;
|
|
293
|
-
threadMetadata?: Record<string, unknown>;
|
|
294
|
-
toolCalls?: Array<{
|
|
295
|
-
name: string;
|
|
296
|
-
args: Record<string, unknown>;
|
|
297
|
-
id?: string;
|
|
298
|
-
}>;
|
|
299
|
-
selectedAgent?: string | null;
|
|
300
|
-
} & StreamCallbacks;
|
|
301
|
-
type CopilotzStreamResult = {
|
|
302
|
-
text: string;
|
|
303
|
-
messages: any[];
|
|
304
|
-
media: Record<string, string> | null;
|
|
305
|
-
};
|
|
306
|
-
declare class CopilotzRequestError extends Error {
|
|
307
|
-
status: number;
|
|
308
|
-
code?: string;
|
|
309
|
-
details?: unknown;
|
|
310
|
-
constructor(message: string, options: {
|
|
311
|
-
status: number;
|
|
312
|
-
code?: string;
|
|
313
|
-
details?: unknown;
|
|
314
|
-
});
|
|
315
|
-
}
|
|
316
|
-
declare function runCopilotzStream(options: RunOptions): Promise<CopilotzStreamResult>;
|
|
317
|
-
declare function fetchThreads(userId: string, authToken?: string | null): Promise<RestThread[]>;
|
|
318
|
-
declare function fetchThreadMessages(threadId: string, authToken?: string | null): Promise<RestMessage[]>;
|
|
319
|
-
declare function updateThread(threadId: string, updates: Partial<RestThread>, authToken?: string | null): Promise<any>;
|
|
320
|
-
declare function deleteMessagesByThreadId(threadId: string, authToken?: string | null): Promise<void>;
|
|
321
|
-
declare function deleteThread(threadId: string, authToken?: string | null): Promise<boolean>;
|
|
322
|
-
declare const copilotzService: {
|
|
323
|
-
runCopilotzStream: typeof runCopilotzStream;
|
|
324
|
-
fetchThreads: typeof fetchThreads;
|
|
325
|
-
fetchThreadMessages: typeof fetchThreadMessages;
|
|
326
|
-
updateThread: typeof updateThread;
|
|
327
|
-
deleteThread: typeof deleteThread;
|
|
328
|
-
};
|
|
329
|
-
|
|
330
331
|
declare function getAssetDataUrl(refOrId: string): Promise<{
|
|
331
332
|
dataUrl: string;
|
|
332
333
|
mime?: string;
|
|
@@ -337,4 +338,4 @@ type WithMetadata = {
|
|
|
337
338
|
};
|
|
338
339
|
declare function resolveAssetsInMessages<T extends WithMetadata>(messages: T[]): Promise<T[]>;
|
|
339
340
|
|
|
340
|
-
export { CopilotzChat, CopilotzRequestError, type CopilotzStreamResult, type EventInterceptor, type EventInterceptorResult, type RenderSpecialState, type RunErrorInterceptor, type SpecialChatState, type SpecialStateControls, type UrlParamsConfig, type UrlState, type UrlSyncConfig, type UseUrlStateReturn, copilotzService, deleteMessagesByThreadId, deleteThread, fetchThreadMessages, fetchThreads, getAssetDataUrl, resolveAssetsInMessages, runCopilotzStream, updateThread, useCopilotz, useUrlState };
|
|
341
|
+
export { CopilotzChat, CopilotzRequestError, type CopilotzStreamResult, type EventInterceptor, type EventInterceptorResult, type RenderSpecialState, type RequestHeadersProvider, type RunErrorInterceptor, type SpecialChatState, type SpecialStateControls, type UrlParamsConfig, type UrlState, type UrlSyncConfig, type UseUrlStateReturn, copilotzService, deleteMessagesByThreadId, deleteThread, fetchThreadMessages, fetchThreads, getAssetDataUrl, resolveAssetsInMessages, runCopilotzStream, updateThread, useCopilotz, useUrlState };
|
package/dist/index.js
CHANGED
|
@@ -118,10 +118,13 @@ var API_KEY = (() => {
|
|
|
118
118
|
];
|
|
119
119
|
return candidates.find((value) => typeof value === "string" && value.length > 0);
|
|
120
120
|
})();
|
|
121
|
-
var withAuthHeaders = (headers = {},
|
|
122
|
-
const
|
|
123
|
-
if (
|
|
124
|
-
return { ...headers,
|
|
121
|
+
var withAuthHeaders = async (headers = {}, getRequestHeaders) => {
|
|
122
|
+
const providedHeaders = getRequestHeaders ? await getRequestHeaders() : void 0;
|
|
123
|
+
if (providedHeaders && Object.keys(providedHeaders).length > 0) {
|
|
124
|
+
return { ...headers, ...providedHeaders };
|
|
125
|
+
}
|
|
126
|
+
if (API_KEY) {
|
|
127
|
+
return { ...headers, Authorization: `Bearer ${API_KEY}` };
|
|
125
128
|
}
|
|
126
129
|
return headers;
|
|
127
130
|
};
|
|
@@ -272,7 +275,6 @@ var convertAudioDataUrlToWavBase64 = async (dataUrl) => {
|
|
|
272
275
|
};
|
|
273
276
|
async function runCopilotzStream(options) {
|
|
274
277
|
const {
|
|
275
|
-
authToken,
|
|
276
278
|
threadId,
|
|
277
279
|
threadExternalId,
|
|
278
280
|
content,
|
|
@@ -282,6 +284,7 @@ async function runCopilotzStream(options) {
|
|
|
282
284
|
threadMetadata,
|
|
283
285
|
toolCalls,
|
|
284
286
|
selectedAgent,
|
|
287
|
+
getRequestHeaders,
|
|
285
288
|
onToken,
|
|
286
289
|
onMessageEvent,
|
|
287
290
|
onAssetEvent,
|
|
@@ -379,9 +382,9 @@ async function runCopilotzStream(options) {
|
|
|
379
382
|
};
|
|
380
383
|
const response = await fetch(apiUrl("/v1/providers/web"), {
|
|
381
384
|
method: "POST",
|
|
382
|
-
headers: withAuthHeaders({
|
|
385
|
+
headers: await withAuthHeaders({
|
|
383
386
|
"Content-Type": "application/json"
|
|
384
|
-
},
|
|
387
|
+
}, getRequestHeaders),
|
|
385
388
|
body: JSON.stringify(payload),
|
|
386
389
|
signal: controller.signal
|
|
387
390
|
});
|
|
@@ -491,12 +494,12 @@ async function runCopilotzStream(options) {
|
|
|
491
494
|
media: collectedMedia
|
|
492
495
|
};
|
|
493
496
|
}
|
|
494
|
-
async function fetchThreads(userId,
|
|
497
|
+
async function fetchThreads(userId, getRequestHeaders) {
|
|
495
498
|
const params = new URLSearchParams();
|
|
496
499
|
params.set("filters", JSON.stringify({ "metadata.userExternalId": userId }));
|
|
497
500
|
params.set("sort", "-updatedAt");
|
|
498
501
|
const res = await fetch(apiUrl(`/v1/rest/threads?${params.toString()}`), {
|
|
499
|
-
headers: withAuthHeaders({ Accept: "application/json" },
|
|
502
|
+
headers: await withAuthHeaders({ Accept: "application/json" }, getRequestHeaders)
|
|
500
503
|
});
|
|
501
504
|
if (!res.ok) {
|
|
502
505
|
const errorText = await res.text().catch(() => res.statusText);
|
|
@@ -508,13 +511,13 @@ async function fetchThreads(userId, authToken) {
|
|
|
508
511
|
}
|
|
509
512
|
return data;
|
|
510
513
|
}
|
|
511
|
-
async function fetchThreadMessages(threadId,
|
|
514
|
+
async function fetchThreadMessages(threadId, getRequestHeaders) {
|
|
512
515
|
const graphParams = new URLSearchParams();
|
|
513
516
|
graphParams.set("threadId", threadId);
|
|
514
517
|
graphParams.set("limit", "500");
|
|
515
518
|
try {
|
|
516
519
|
const graphRes = await fetch(apiUrl(`/v1/messages?${graphParams.toString()}`), {
|
|
517
|
-
headers: withAuthHeaders({ Accept: "application/json" },
|
|
520
|
+
headers: await withAuthHeaders({ Accept: "application/json" }, getRequestHeaders)
|
|
518
521
|
});
|
|
519
522
|
if (graphRes.ok) {
|
|
520
523
|
const graphData = await graphRes.json();
|
|
@@ -540,7 +543,7 @@ async function fetchThreadMessages(threadId, authToken) {
|
|
|
540
543
|
params.set("filters", JSON.stringify({ threadId }));
|
|
541
544
|
params.set("sort", "createdAt:asc");
|
|
542
545
|
const res = await fetch(apiUrl(`/v1/rest/messages?${params.toString()}`), {
|
|
543
|
-
headers: withAuthHeaders({ Accept: "application/json" },
|
|
546
|
+
headers: await withAuthHeaders({ Accept: "application/json" }, getRequestHeaders)
|
|
544
547
|
});
|
|
545
548
|
if (!res.ok) {
|
|
546
549
|
const errorText = await res.text().catch(() => res.statusText);
|
|
@@ -552,10 +555,10 @@ async function fetchThreadMessages(threadId, authToken) {
|
|
|
552
555
|
}
|
|
553
556
|
return data;
|
|
554
557
|
}
|
|
555
|
-
async function updateThread(threadId, updates,
|
|
558
|
+
async function updateThread(threadId, updates, getRequestHeaders) {
|
|
556
559
|
const res = await fetch(apiUrl(`/v1/rest/threads/${threadId}`), {
|
|
557
560
|
method: "PUT",
|
|
558
|
-
headers: withAuthHeaders({ "Content-Type": "application/json", Accept: "application/json" },
|
|
561
|
+
headers: await withAuthHeaders({ "Content-Type": "application/json", Accept: "application/json" }, getRequestHeaders),
|
|
559
562
|
body: JSON.stringify(updates)
|
|
560
563
|
});
|
|
561
564
|
if (!res.ok) {
|
|
@@ -565,13 +568,13 @@ async function updateThread(threadId, updates, authToken) {
|
|
|
565
568
|
const data = await res.json();
|
|
566
569
|
return data?.body ?? data;
|
|
567
570
|
}
|
|
568
|
-
async function deleteMessagesByThreadId(threadId,
|
|
571
|
+
async function deleteMessagesByThreadId(threadId, getRequestHeaders) {
|
|
569
572
|
const graphParams = new URLSearchParams();
|
|
570
573
|
graphParams.set("threadId", threadId);
|
|
571
574
|
try {
|
|
572
575
|
const graphRes = await fetch(apiUrl(`/v1/messages?${graphParams.toString()}`), {
|
|
573
576
|
method: "DELETE",
|
|
574
|
-
headers: withAuthHeaders({ Accept: "application/json" },
|
|
577
|
+
headers: await withAuthHeaders({ Accept: "application/json" }, getRequestHeaders)
|
|
575
578
|
});
|
|
576
579
|
if (!graphRes.ok && ![404, 405, 501].includes(graphRes.status)) {
|
|
577
580
|
const errorText = await graphRes.text().catch(() => graphRes.statusText);
|
|
@@ -586,7 +589,7 @@ async function deleteMessagesByThreadId(threadId, authToken) {
|
|
|
586
589
|
const params = new URLSearchParams();
|
|
587
590
|
params.set("filters", JSON.stringify({ threadId }));
|
|
588
591
|
const res = await fetch(apiUrl(`/v1/rest/messages?${params.toString()}`), {
|
|
589
|
-
headers: withAuthHeaders({ Accept: "application/json" },
|
|
592
|
+
headers: await withAuthHeaders({ Accept: "application/json" }, getRequestHeaders)
|
|
590
593
|
});
|
|
591
594
|
if (!res.ok) {
|
|
592
595
|
console.warn("Could not fetch messages for deletion:", res.status);
|
|
@@ -601,18 +604,18 @@ async function deleteMessagesByThreadId(threadId, authToken) {
|
|
|
601
604
|
try {
|
|
602
605
|
await fetch(apiUrl(`/v1/rest/messages/${msg.id}`), {
|
|
603
606
|
method: "DELETE",
|
|
604
|
-
headers: withAuthHeaders({ Accept: "application/json" },
|
|
607
|
+
headers: await withAuthHeaders({ Accept: "application/json" }, getRequestHeaders)
|
|
605
608
|
});
|
|
606
609
|
} catch {
|
|
607
610
|
}
|
|
608
611
|
}
|
|
609
612
|
}
|
|
610
613
|
}
|
|
611
|
-
async function deleteThread(threadId,
|
|
612
|
-
await deleteMessagesByThreadId(threadId,
|
|
614
|
+
async function deleteThread(threadId, getRequestHeaders) {
|
|
615
|
+
await deleteMessagesByThreadId(threadId, getRequestHeaders);
|
|
613
616
|
const res = await fetch(apiUrl(`/v1/rest/threads/${threadId}`), {
|
|
614
617
|
method: "DELETE",
|
|
615
|
-
headers: withAuthHeaders({ Accept: "application/json" },
|
|
618
|
+
headers: await withAuthHeaders({ Accept: "application/json" }, getRequestHeaders)
|
|
616
619
|
});
|
|
617
620
|
if (!res.ok) {
|
|
618
621
|
const errorText = await res.text().catch(() => res.statusText);
|
|
@@ -967,12 +970,12 @@ var convertServerMessage = (msg) => {
|
|
|
967
970
|
};
|
|
968
971
|
function useCopilotz({
|
|
969
972
|
userId,
|
|
970
|
-
authToken,
|
|
971
973
|
initialContext,
|
|
972
974
|
bootstrap,
|
|
973
975
|
defaultThreadName,
|
|
974
976
|
onToolOutput,
|
|
975
977
|
preferredAgentName,
|
|
978
|
+
getRequestHeaders,
|
|
976
979
|
eventInterceptor,
|
|
977
980
|
runErrorInterceptor,
|
|
978
981
|
urlSync
|
|
@@ -1034,7 +1037,7 @@ function useCopilotz({
|
|
|
1034
1037
|
if (!eventInterceptor) return void 0;
|
|
1035
1038
|
try {
|
|
1036
1039
|
const result = eventInterceptor(event);
|
|
1037
|
-
if (result
|
|
1040
|
+
if (result?.specialState) {
|
|
1038
1041
|
setSpecialState(result.specialState);
|
|
1039
1042
|
}
|
|
1040
1043
|
return result;
|
|
@@ -1169,20 +1172,20 @@ function useCopilotz({
|
|
|
1169
1172
|
}, []);
|
|
1170
1173
|
const fetchAndSetThreadsState = useCallback2(async (uid, preferredExternalId) => {
|
|
1171
1174
|
try {
|
|
1172
|
-
const rawThreads = await fetchThreads(uid,
|
|
1175
|
+
const rawThreads = await fetchThreads(uid, getRequestHeaders);
|
|
1173
1176
|
return updateThreadsState(rawThreads, preferredExternalId);
|
|
1174
1177
|
} catch (error) {
|
|
1175
1178
|
if (isAbortError(error)) return;
|
|
1176
1179
|
console.error("Error loading threads", error);
|
|
1177
1180
|
return null;
|
|
1178
1181
|
}
|
|
1179
|
-
}, [
|
|
1182
|
+
}, [updateThreadsState, getRequestHeaders]);
|
|
1180
1183
|
const loadThreadMessages = useCallback2(async (threadId) => {
|
|
1181
1184
|
const requestId = messagesRequestRef.current + 1;
|
|
1182
1185
|
messagesRequestRef.current = requestId;
|
|
1183
1186
|
setIsMessagesLoading(true);
|
|
1184
1187
|
try {
|
|
1185
|
-
const rawMessages = await fetchThreadMessages(threadId,
|
|
1188
|
+
const rawMessages = await fetchThreadMessages(threadId, getRequestHeaders);
|
|
1186
1189
|
const resolvedMessages = await resolveAssetsInMessages(rawMessages);
|
|
1187
1190
|
if (messagesRequestRef.current !== requestId) return;
|
|
1188
1191
|
resolvedMessages.forEach((msg) => {
|
|
@@ -1214,7 +1217,7 @@ function useCopilotz({
|
|
|
1214
1217
|
setIsMessagesLoading(false);
|
|
1215
1218
|
}
|
|
1216
1219
|
}
|
|
1217
|
-
}, [
|
|
1220
|
+
}, [processToolOutput, getRequestHeaders]);
|
|
1218
1221
|
const handleSelectThread = useCallback2(async (threadId) => {
|
|
1219
1222
|
setCurrentThreadId(threadId);
|
|
1220
1223
|
setMessages([]);
|
|
@@ -1257,7 +1260,7 @@ function useCopilotz({
|
|
|
1257
1260
|
}));
|
|
1258
1261
|
} else {
|
|
1259
1262
|
try {
|
|
1260
|
-
await updateThread(threadId, { name: trimmedTitle },
|
|
1263
|
+
await updateThread(threadId, { name: trimmedTitle }, getRequestHeaders);
|
|
1261
1264
|
} catch (error) {
|
|
1262
1265
|
console.error("Failed to rename thread:", error);
|
|
1263
1266
|
if (userId) {
|
|
@@ -1265,7 +1268,7 @@ function useCopilotz({
|
|
|
1265
1268
|
}
|
|
1266
1269
|
}
|
|
1267
1270
|
}
|
|
1268
|
-
}, [
|
|
1271
|
+
}, [userId, fetchAndSetThreadsState, getRequestHeaders]);
|
|
1269
1272
|
const handleArchiveThread = useCallback2(async (threadId) => {
|
|
1270
1273
|
const thread = threadsRef.current.find((t) => t.id === threadId);
|
|
1271
1274
|
if (!thread) return;
|
|
@@ -1277,7 +1280,7 @@ function useCopilotz({
|
|
|
1277
1280
|
const isPlaceholder = extMap[threadId] === threadId;
|
|
1278
1281
|
if (!isPlaceholder) {
|
|
1279
1282
|
try {
|
|
1280
|
-
await updateThread(threadId, { status: newArchivedStatus ? "archived" : "active" },
|
|
1283
|
+
await updateThread(threadId, { status: newArchivedStatus ? "archived" : "active" }, getRequestHeaders);
|
|
1281
1284
|
} catch (error) {
|
|
1282
1285
|
console.error("Failed to archive thread:", error);
|
|
1283
1286
|
if (userId) {
|
|
@@ -1285,7 +1288,7 @@ function useCopilotz({
|
|
|
1285
1288
|
}
|
|
1286
1289
|
}
|
|
1287
1290
|
}
|
|
1288
|
-
}, [
|
|
1291
|
+
}, [userId, fetchAndSetThreadsState, getRequestHeaders]);
|
|
1289
1292
|
const handleDeleteThread = useCallback2(async (threadId) => {
|
|
1290
1293
|
const extMap = threadExternalIdMapRef.current;
|
|
1291
1294
|
const isPlaceholder = extMap[threadId] === threadId;
|
|
@@ -1314,7 +1317,7 @@ function useCopilotz({
|
|
|
1314
1317
|
}
|
|
1315
1318
|
if (!isPlaceholder) {
|
|
1316
1319
|
try {
|
|
1317
|
-
await deleteThread(threadId,
|
|
1320
|
+
await deleteThread(threadId, getRequestHeaders);
|
|
1318
1321
|
} catch (error) {
|
|
1319
1322
|
console.error("Failed to delete thread:", error);
|
|
1320
1323
|
if (userId) {
|
|
@@ -1322,7 +1325,7 @@ function useCopilotz({
|
|
|
1322
1325
|
}
|
|
1323
1326
|
}
|
|
1324
1327
|
}
|
|
1325
|
-
}, [
|
|
1328
|
+
}, [userId, fetchAndSetThreadsState, loadThreadMessages, getRequestHeaders]);
|
|
1326
1329
|
const handleStop = useCallback2(() => {
|
|
1327
1330
|
abortControllerRef.current?.abort();
|
|
1328
1331
|
abortControllerRef.current = null;
|
|
@@ -1525,7 +1528,6 @@ function useCopilotz({
|
|
|
1525
1528
|
};
|
|
1526
1529
|
const finalMetadata = Object.keys(mergedMetadata).length > 0 ? mergedMetadata : void 0;
|
|
1527
1530
|
await runCopilotzStream({
|
|
1528
|
-
authToken,
|
|
1529
1531
|
threadId: params.threadId ?? void 0,
|
|
1530
1532
|
threadExternalId: params.threadExternalId ?? void 0,
|
|
1531
1533
|
content: requestContent,
|
|
@@ -1542,10 +1544,11 @@ function useCopilotz({
|
|
|
1542
1544
|
threadMetadata: params.threadMetadata ?? threadMetadata,
|
|
1543
1545
|
toolCalls: params.toolCalls,
|
|
1544
1546
|
selectedAgent: params.agentName ?? preferredAgentRef.current ?? null,
|
|
1547
|
+
getRequestHeaders,
|
|
1545
1548
|
onToken: (token) => updateStreamingMessage(token),
|
|
1546
1549
|
onMessageEvent: async (event) => {
|
|
1547
1550
|
const intercepted = applyEventInterceptor(event);
|
|
1548
|
-
if (intercepted
|
|
1551
|
+
if (intercepted?.handled) {
|
|
1549
1552
|
return;
|
|
1550
1553
|
}
|
|
1551
1554
|
const type = event?.type || "";
|
|
@@ -1681,7 +1684,7 @@ function useCopilotz({
|
|
|
1681
1684
|
},
|
|
1682
1685
|
onAssetEvent: async (payload) => {
|
|
1683
1686
|
const intercepted = applyEventInterceptor({ type: "ASSET_CREATED", payload });
|
|
1684
|
-
if (intercepted
|
|
1687
|
+
if (intercepted?.handled) {
|
|
1685
1688
|
return;
|
|
1686
1689
|
}
|
|
1687
1690
|
await (async () => {
|
|
@@ -1708,7 +1711,7 @@ function useCopilotz({
|
|
|
1708
1711
|
abortControllerRef.current = null;
|
|
1709
1712
|
}
|
|
1710
1713
|
return currentAssistantId;
|
|
1711
|
-
}, [
|
|
1714
|
+
}, [applyEventInterceptor, handleStreamMessageEvent, handleStreamAssetEvent, getRequestHeaders]);
|
|
1712
1715
|
const handleSendMessage = useCallback2(async (content, attachments = []) => {
|
|
1713
1716
|
if (!content.trim() && attachments.length === 0) return;
|
|
1714
1717
|
if (!userId) return;
|
|
@@ -1815,7 +1818,7 @@ function useCopilotz({
|
|
|
1815
1818
|
];
|
|
1816
1819
|
});
|
|
1817
1820
|
}
|
|
1818
|
-
}, [userId,
|
|
1821
|
+
}, [userId, fetchAndSetThreadsState, loadThreadMessages, sendCopilotzMessage, getSpecialStateFromError]);
|
|
1819
1822
|
const bootstrapConversation = useCallback2(async (uid) => {
|
|
1820
1823
|
if (!bootstrap?.initialToolCalls && !bootstrap?.initialMessage) return;
|
|
1821
1824
|
const bootstrapThreadExternalId = generateId();
|
|
@@ -1858,7 +1861,7 @@ function useCopilotz({
|
|
|
1858
1861
|
}
|
|
1859
1862
|
]);
|
|
1860
1863
|
}
|
|
1861
|
-
}, [
|
|
1864
|
+
}, [fetchAndSetThreadsState, loadThreadMessages, sendCopilotzMessage, bootstrap, defaultThreadName, getSpecialStateFromError]);
|
|
1862
1865
|
const reset = useCallback2(() => {
|
|
1863
1866
|
messagesRequestRef.current += 1;
|
|
1864
1867
|
setThreads([]);
|
|
@@ -1893,7 +1896,7 @@ function useCopilotz({
|
|
|
1893
1896
|
initializationRef.current = { userId: null, started: false };
|
|
1894
1897
|
reset();
|
|
1895
1898
|
}
|
|
1896
|
-
}, [userId,
|
|
1899
|
+
}, [userId, fetchAndSetThreadsState, loadThreadMessages, bootstrapConversation, reset, bootstrap, isUrlSyncEnabled, urlState.threadId]);
|
|
1897
1900
|
useEffect2(() => {
|
|
1898
1901
|
if (!isUrlSyncEnabled) return;
|
|
1899
1902
|
if (!initializationRef.current.started) return;
|
|
@@ -1942,7 +1945,6 @@ function useCopilotz({
|
|
|
1942
1945
|
import { jsx } from "react/jsx-runtime";
|
|
1943
1946
|
var CopilotzChat = ({
|
|
1944
1947
|
userId,
|
|
1945
|
-
authToken,
|
|
1946
1948
|
userName,
|
|
1947
1949
|
userAvatar,
|
|
1948
1950
|
userEmail,
|
|
@@ -1961,6 +1963,7 @@ var CopilotzChat = ({
|
|
|
1961
1963
|
agentOptions = [],
|
|
1962
1964
|
selectedAgentId = null,
|
|
1963
1965
|
onSelectAgent,
|
|
1966
|
+
getRequestHeaders,
|
|
1964
1967
|
className,
|
|
1965
1968
|
eventInterceptor,
|
|
1966
1969
|
runErrorInterceptor,
|
|
@@ -1990,12 +1993,12 @@ var CopilotzChat = ({
|
|
|
1990
1993
|
setUrlAgentId
|
|
1991
1994
|
} = useCopilotz({
|
|
1992
1995
|
userId,
|
|
1993
|
-
authToken,
|
|
1994
1996
|
initialContext,
|
|
1995
1997
|
bootstrap,
|
|
1996
1998
|
defaultThreadName: userConfig?.labels?.defaultThreadName,
|
|
1997
1999
|
onToolOutput,
|
|
1998
2000
|
preferredAgentName: selectedAgent?.name ?? null,
|
|
2001
|
+
getRequestHeaders,
|
|
1999
2002
|
eventInterceptor,
|
|
2000
2003
|
runErrorInterceptor,
|
|
2001
2004
|
urlSync
|