@assistant-ui/react-ag-ui 0.0.10 → 0.0.13
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 +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/runtime/adapter/conversions.d.ts +10 -6
- package/dist/runtime/adapter/conversions.d.ts.map +1 -1
- package/dist/runtime/adapter/conversions.js +89 -78
- package/dist/runtime/adapter/conversions.js.map +1 -1
- package/dist/runtime/types.d.ts +20 -1
- package/dist/runtime/types.d.ts.map +1 -1
- package/dist/useAgUiRuntime.d.ts.map +1 -1
- package/dist/useAgUiRuntime.js +38 -22
- package/dist/useAgUiRuntime.js.map +1 -1
- package/package.json +7 -7
- package/src/index.ts +5 -1
- package/src/runtime/adapter/conversions.ts +129 -93
- package/src/runtime/types.ts +22 -0
- package/src/useAgUiRuntime.ts +43 -20
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
export { useAgUiRuntime } from "./useAgUiRuntime.js";
|
|
2
|
-
export type { UseAgUiRuntimeOptions } from "./runtime/types.js";
|
|
2
|
+
export type { UseAgUiRuntimeOptions, UseAgUiRuntimeAdapters, UseAgUiThreadListAdapter, } from "./runtime/types.js";
|
|
3
3
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,4BAAyB;AAClD,YAAY,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,4BAAyB;AAClD,YAAY,EACV,qBAAqB,EACrB,sBAAsB,EACtB,wBAAwB,GACzB,2BAAwB"}
|
|
@@ -1,8 +1,11 @@
|
|
|
1
|
+
import { type Tool } from "assistant-stream";
|
|
1
2
|
type ThreadMessageLike = {
|
|
2
3
|
id: string;
|
|
3
4
|
role: string;
|
|
4
|
-
content:
|
|
5
|
+
content: unknown;
|
|
5
6
|
name?: string;
|
|
7
|
+
toolCallId?: string;
|
|
8
|
+
error?: string;
|
|
6
9
|
};
|
|
7
10
|
type AgUiToolCall = {
|
|
8
11
|
id: string;
|
|
@@ -25,11 +28,12 @@ export type AgUiMessage = {
|
|
|
25
28
|
toolCallId: string;
|
|
26
29
|
error?: string;
|
|
27
30
|
};
|
|
28
|
-
export declare
|
|
29
|
-
|
|
31
|
+
export declare function toAgUiMessages(messages: readonly ThreadMessageLike[]): AgUiMessage[];
|
|
32
|
+
type AgUiTool = {
|
|
30
33
|
name: string;
|
|
31
|
-
description:
|
|
32
|
-
parameters:
|
|
33
|
-
}
|
|
34
|
+
description: string | undefined;
|
|
35
|
+
parameters: unknown;
|
|
36
|
+
};
|
|
37
|
+
export declare function toAgUiTools(tools: Record<string, Tool> | undefined): AgUiTool[];
|
|
34
38
|
export {};
|
|
35
39
|
//# sourceMappingURL=conversions.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"conversions.d.ts","sourceRoot":"","sources":["../../../src/runtime/adapter/conversions.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"conversions.d.ts","sourceRoot":"","sources":["../../../src/runtime/adapter/conversions.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,KAAK,IAAI,EAAqB,MAAM,kBAAkB,CAAC;AAEhE,KAAK,iBAAiB,GAAG;IACvB,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,OAAO,CAAC;IACjB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB,CAAC;AAEF,KAAK,YAAY,GAAG;IAClB,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,UAAU,CAAC;IACjB,QAAQ,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,SAAS,EAAE,MAAM,CAAA;KAAE,CAAC;CAC/C,CAAC;AAEF,MAAM,MAAM,WAAW,GACnB;IACE,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,SAAS,CAAC,EAAE,YAAY,EAAE,CAAC;CAC5B,GACD;IACE,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;IAChB,UAAU,EAAE,MAAM,CAAC;IACnB,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB,CAAC;AA4HN,wBAAgB,cAAc,CAC5B,QAAQ,EAAE,SAAS,iBAAiB,EAAE,GACrC,WAAW,EAAE,CA0Bf;AAED,KAAK,QAAQ,GAAG;IACd,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,GAAG,SAAS,CAAC;IAChC,UAAU,EAAE,OAAO,CAAC;CACrB,CAAC;AAEF,wBAAgB,WAAW,CACzB,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,GAAG,SAAS,GACtC,QAAQ,EAAE,CASZ"}
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
2
|
+
import { toToolsJSONSchema } from "assistant-stream";
|
|
3
|
+
function generateId() {
|
|
4
|
+
return (globalThis.crypto?.randomUUID?.() ??
|
|
5
|
+
Math.random().toString(36).slice(2));
|
|
6
|
+
}
|
|
7
|
+
function normalizeToolCall(part) {
|
|
6
8
|
const id = part.toolCallId ?? generateId();
|
|
7
9
|
const argsText = typeof part.argsText === "string"
|
|
8
10
|
? part.argsText
|
|
@@ -18,91 +20,100 @@ const normaliseToolCall = (part) => {
|
|
|
18
20
|
},
|
|
19
21
|
},
|
|
20
22
|
};
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
+
}
|
|
24
|
+
function extractText(content) {
|
|
25
|
+
if (typeof content === "string")
|
|
26
|
+
return content;
|
|
27
|
+
if (!Array.isArray(content))
|
|
28
|
+
return "";
|
|
29
|
+
return content
|
|
30
|
+
.filter((part) => part?.type === "text" && typeof part?.text === "string")
|
|
31
|
+
.map((part) => part.text)
|
|
32
|
+
.join("\n");
|
|
33
|
+
}
|
|
34
|
+
function convertAssistantMessage(message, converted) {
|
|
35
|
+
const content = extractText(message.content);
|
|
36
|
+
const contentArray = Array.isArray(message.content) ? message.content : [];
|
|
37
|
+
const toolCallParts = contentArray.filter((part) => part?.type === "tool-call");
|
|
38
|
+
const toolCalls = toolCallParts.map((part) => ({
|
|
39
|
+
...normalizeToolCall(part),
|
|
40
|
+
part,
|
|
41
|
+
}));
|
|
42
|
+
const assistantMessage = {
|
|
43
|
+
id: message.id,
|
|
44
|
+
role: "assistant",
|
|
45
|
+
content,
|
|
46
|
+
};
|
|
47
|
+
if (message.name) {
|
|
48
|
+
assistantMessage.name = message.name;
|
|
49
|
+
}
|
|
50
|
+
if (toolCalls.length > 0) {
|
|
51
|
+
assistantMessage.toolCalls = toolCalls.map((entry) => entry.call);
|
|
52
|
+
}
|
|
53
|
+
converted.push(assistantMessage);
|
|
54
|
+
for (const { id: toolCallId, part } of toolCalls) {
|
|
55
|
+
if (part.result === undefined)
|
|
56
|
+
continue;
|
|
57
|
+
const resultContent = typeof part.result === "string"
|
|
58
|
+
? part.result
|
|
59
|
+
: JSON.stringify(part.result);
|
|
60
|
+
const toolMessage = {
|
|
61
|
+
id: `${toolCallId}:tool`,
|
|
62
|
+
role: "tool",
|
|
63
|
+
content: resultContent,
|
|
64
|
+
toolCallId,
|
|
65
|
+
};
|
|
66
|
+
if (part.isError) {
|
|
67
|
+
toolMessage.error = resultContent;
|
|
68
|
+
}
|
|
69
|
+
converted.push(toolMessage);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
function convertToolMessage(message, converted) {
|
|
73
|
+
const content = extractText(message.content);
|
|
74
|
+
const toolCallId = message.toolCallId ?? generateId();
|
|
75
|
+
const toolMessage = {
|
|
76
|
+
id: message.id,
|
|
77
|
+
role: "tool",
|
|
78
|
+
content,
|
|
79
|
+
toolCallId,
|
|
80
|
+
};
|
|
81
|
+
if (typeof message.error === "string") {
|
|
82
|
+
toolMessage.error = message.error;
|
|
83
|
+
}
|
|
84
|
+
converted.push(toolMessage);
|
|
85
|
+
}
|
|
86
|
+
export function toAgUiMessages(messages) {
|
|
23
87
|
const converted = [];
|
|
24
88
|
for (const message of messages) {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
if (role === "assistant" && Array.isArray(message.content)) {
|
|
28
|
-
const toolCallParts = message.content.filter((part) => part?.type === "tool-call");
|
|
29
|
-
const toolCalls = toolCallParts.map((part) => {
|
|
30
|
-
const { id, call } = normaliseToolCall(part);
|
|
31
|
-
return { id, call, part };
|
|
32
|
-
});
|
|
33
|
-
converted.push({
|
|
34
|
-
id: message.id,
|
|
35
|
-
role: "assistant",
|
|
36
|
-
content,
|
|
37
|
-
...(message.name ? { name: message.name } : {}),
|
|
38
|
-
...(toolCalls.length > 0
|
|
39
|
-
? { toolCalls: toolCalls.map((entry) => entry.call) }
|
|
40
|
-
: {}),
|
|
41
|
-
});
|
|
42
|
-
for (const { id: toolCallId, part } of toolCalls) {
|
|
43
|
-
if (part.result === undefined)
|
|
44
|
-
continue;
|
|
45
|
-
const resultContent = typeof part.result === "string"
|
|
46
|
-
? part.result
|
|
47
|
-
: JSON.stringify(part.result);
|
|
48
|
-
converted.push({
|
|
49
|
-
id: `${toolCallId}:tool`,
|
|
50
|
-
role: "tool",
|
|
51
|
-
content: resultContent,
|
|
52
|
-
toolCallId,
|
|
53
|
-
...(part.isError ? { error: resultContent } : {}),
|
|
54
|
-
});
|
|
55
|
-
}
|
|
89
|
+
if (message.role === "assistant") {
|
|
90
|
+
convertAssistantMessage(message, converted);
|
|
56
91
|
continue;
|
|
57
92
|
}
|
|
58
|
-
if (role === "tool") {
|
|
59
|
-
|
|
60
|
-
converted.push({
|
|
61
|
-
id: message.id,
|
|
62
|
-
role: "tool",
|
|
63
|
-
content,
|
|
64
|
-
toolCallId,
|
|
65
|
-
...(typeof message.error === "string"
|
|
66
|
-
? { error: message.error }
|
|
67
|
-
: undefined),
|
|
68
|
-
});
|
|
93
|
+
if (message.role === "tool") {
|
|
94
|
+
convertToolMessage(message, converted);
|
|
69
95
|
continue;
|
|
70
96
|
}
|
|
71
|
-
|
|
97
|
+
const genericMessage = {
|
|
72
98
|
id: message.id,
|
|
73
|
-
role,
|
|
74
|
-
content,
|
|
75
|
-
|
|
76
|
-
|
|
99
|
+
role: message.role,
|
|
100
|
+
content: extractText(message.content),
|
|
101
|
+
};
|
|
102
|
+
if (message.name) {
|
|
103
|
+
genericMessage.name = message.name;
|
|
104
|
+
}
|
|
105
|
+
converted.push(genericMessage);
|
|
77
106
|
}
|
|
78
107
|
return converted;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
if (!Array.isArray(content)) {
|
|
82
|
-
if (typeof content === "string")
|
|
83
|
-
return content;
|
|
84
|
-
return "";
|
|
85
|
-
}
|
|
86
|
-
return content
|
|
87
|
-
.filter((part) => part?.type === "text" && typeof part?.text === "string")
|
|
88
|
-
.map((part) => part.text)
|
|
89
|
-
.join("\n");
|
|
90
|
-
};
|
|
91
|
-
export const toAgUiTools = (tools) => {
|
|
108
|
+
}
|
|
109
|
+
export function toAgUiTools(tools) {
|
|
92
110
|
if (!tools)
|
|
93
111
|
return [];
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
.map(([name, tool]) => ({
|
|
112
|
+
const toolsSchema = toToolsJSONSchema(tools);
|
|
113
|
+
return Object.entries(toolsSchema).map(([name, tool]) => ({
|
|
97
114
|
name,
|
|
98
|
-
description: tool
|
|
99
|
-
parameters: tool
|
|
100
|
-
? z.toJSONSchema(tool.parameters)
|
|
101
|
-
: typeof tool?.parameters?.toJSON === "function"
|
|
102
|
-
? tool.parameters.toJSON()
|
|
103
|
-
: typeof tool?.parameters?.toJSONSchema === "function"
|
|
104
|
-
? tool.parameters.toJSONSchema()
|
|
105
|
-
: tool?.parameters,
|
|
115
|
+
description: tool.description,
|
|
116
|
+
parameters: tool.parameters,
|
|
106
117
|
}));
|
|
107
|
-
}
|
|
118
|
+
}
|
|
108
119
|
//# sourceMappingURL=conversions.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"conversions.js","sourceRoot":"","sources":["../../../src/runtime/adapter/conversions.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,
|
|
1
|
+
{"version":3,"file":"conversions.js","sourceRoot":"","sources":["../../../src/runtime/adapter/conversions.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,EAAa,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AA2ChE,SAAS,UAAU;IACjB,OAAO,CACJ,UAAU,CAAC,MAAwC,EAAE,UAAU,EAAE,EAAE;QACpE,IAAI,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CACpC,CAAC;AACJ,CAAC;AAED,SAAS,iBAAiB,CAAC,IAAkB;IAI3C,MAAM,EAAE,GAAG,IAAI,CAAC,UAAU,IAAI,UAAU,EAAE,CAAC;IAC3C,MAAM,QAAQ,GACZ,OAAO,IAAI,CAAC,QAAQ,KAAK,QAAQ;QAC/B,CAAC,CAAC,IAAI,CAAC,QAAQ;QACf,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,IAAI,EAAE,CAAC,CAAC;IAEtC,OAAO;QACL,EAAE;QACF,IAAI,EAAE;YACJ,EAAE;YACF,IAAI,EAAE,UAAU;YAChB,QAAQ,EAAE;gBACR,IAAI,EAAE,IAAI,CAAC,QAAQ,IAA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AC5B,CAAC,CAAC,CAAC;AACN,CAAC"}
|
package/dist/runtime/types.d.ts
CHANGED
|
@@ -1,12 +1,31 @@
|
|
|
1
|
-
import type { AttachmentAdapter, DictationAdapter, FeedbackAdapter, SpeechSynthesisAdapter, ThreadHistoryAdapter } from "@assistant-ui/react";
|
|
1
|
+
import type { AttachmentAdapter, DictationAdapter, FeedbackAdapter, SpeechSynthesisAdapter, ThreadHistoryAdapter, ThreadMessage } from "@assistant-ui/react";
|
|
2
2
|
import type { HttpAgent } from "@ag-ui/client";
|
|
3
3
|
import type { Logger } from "./logger.js";
|
|
4
|
+
import type { ReadonlyJSONValue } from "assistant-stream/utils";
|
|
5
|
+
/**
|
|
6
|
+
* @experimental This API is still under active development and might change without notice.
|
|
7
|
+
*/
|
|
8
|
+
export type UseAgUiThreadListAdapter = {
|
|
9
|
+
threadId?: string | undefined;
|
|
10
|
+
onSwitchToNewThread?: (() => Promise<void> | void) | undefined;
|
|
11
|
+
onSwitchToThread?: ((threadId: string) => Promise<{
|
|
12
|
+
messages: readonly ThreadMessage[];
|
|
13
|
+
state?: ReadonlyJSONValue;
|
|
14
|
+
}> | {
|
|
15
|
+
messages: readonly ThreadMessage[];
|
|
16
|
+
state?: ReadonlyJSONValue;
|
|
17
|
+
}) | undefined;
|
|
18
|
+
};
|
|
4
19
|
export type UseAgUiRuntimeAdapters = {
|
|
5
20
|
attachments?: AttachmentAdapter;
|
|
6
21
|
speech?: SpeechSynthesisAdapter;
|
|
7
22
|
dictation?: DictationAdapter;
|
|
8
23
|
feedback?: FeedbackAdapter;
|
|
9
24
|
history?: ThreadHistoryAdapter;
|
|
25
|
+
/**
|
|
26
|
+
* @experimental This API is still under active development and might change without notice.
|
|
27
|
+
*/
|
|
28
|
+
threadList?: UseAgUiThreadListAdapter;
|
|
10
29
|
};
|
|
11
30
|
export type UseAgUiRuntimeOptions = {
|
|
12
31
|
agent: HttpAgent;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/runtime/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,iBAAiB,EACjB,gBAAgB,EAChB,eAAe,EACf,sBAAsB,EACtB,oBAAoB,
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/runtime/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,iBAAiB,EACjB,gBAAgB,EAChB,eAAe,EACf,sBAAsB,EACtB,oBAAoB,EACpB,aAAa,EACd,MAAM,qBAAqB,CAAC;AAC7B,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AAC/C,OAAO,KAAK,EAAE,MAAM,EAAE,oBAAiB;AACvC,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAEhE;;GAEG;AACH,MAAM,MAAM,wBAAwB,GAAG;IACrC,QAAQ,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAC9B,mBAAmB,CAAC,EAAE,CAAC,MAAM,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC,GAAG,SAAS,CAAC;IAC/D,gBAAgB,CAAC,EACb,CAAC,CAAC,QAAQ,EAAE,MAAM,KACd,OAAO,CAAC;QACN,QAAQ,EAAE,SAAS,aAAa,EAAE,CAAC;QACnC,KAAK,CAAC,EAAE,iBAAiB,CAAC;KAC3B,CAAC,GACF;QAAE,QAAQ,EAAE,SAAS,aAAa,EAAE,CAAC;QAAC,KAAK,CAAC,EAAE,iBAAiB,CAAA;KAAE,CAAC,GACtE,SAAS,CAAC;CACf,CAAC;AAEF,MAAM,MAAM,sBAAsB,GAAG;IACnC,WAAW,CAAC,EAAE,iBAAiB,CAAC;IAChC,MAAM,CAAC,EAAE,sBAAsB,CAAC;IAChC,SAAS,CAAC,EAAE,gBAAgB,CAAC;IAC7B,QAAQ,CAAC,EAAE,eAAe,CAAC;IAC3B,OAAO,CAAC,EAAE,oBAAoB,CAAC;IAC/B;;OAEG;IAC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AAE,GAAG,EAAE,CAAA;CAAE,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useAgUiRuntime.d.ts","sourceRoot":"","sources":["../src/useAgUiRuntime.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EACV,gBAAgB,EAIjB,MAAM,qBAAqB,CAAC;AAG7B,OAAO,KAAK,EAAE,qBAAqB,EAAE,2BAAwB;AAG7D,wBAAgB,cAAc,CAC5B,OAAO,EAAE,qBAAqB,GAC7B,gBAAgB,
|
|
1
|
+
{"version":3,"file":"useAgUiRuntime.d.ts","sourceRoot":"","sources":["../src/useAgUiRuntime.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EACV,gBAAgB,EAIjB,MAAM,qBAAqB,CAAC;AAG7B,OAAO,KAAK,EAAE,qBAAqB,EAAE,2BAAwB;AAG7D,wBAAgB,cAAc,CAC5B,OAAO,EAAE,qBAAqB,GAC7B,gBAAgB,CAgHlB"}
|
package/dist/useAgUiRuntime.js
CHANGED
|
@@ -9,19 +9,16 @@ export function useAgUiRuntime(options) {
|
|
|
9
9
|
const notifyUpdate = useCallback(() => setVersion((v) => v + 1), []);
|
|
10
10
|
const coreRef = useRef(null);
|
|
11
11
|
const runtimeAdapters = useRuntimeAdapters();
|
|
12
|
-
const attachmentsAdapter = options.adapters?.attachments ?? runtimeAdapters?.attachments;
|
|
13
12
|
const historyAdapter = options.adapters?.history ?? runtimeAdapters?.history;
|
|
14
|
-
const
|
|
15
|
-
const dictationAdapter = options.adapters?.dictation;
|
|
16
|
-
const feedbackAdapter = options.adapters?.feedback;
|
|
13
|
+
const threadListAdapter = options.adapters?.threadList;
|
|
17
14
|
if (!coreRef.current) {
|
|
18
15
|
coreRef.current = new AgUiThreadRuntimeCore({
|
|
19
16
|
agent: options.agent,
|
|
20
17
|
logger,
|
|
21
18
|
showThinking: options.showThinking ?? true,
|
|
22
|
-
...(options.onError
|
|
23
|
-
...(options.onCancel
|
|
24
|
-
...(historyAdapter
|
|
19
|
+
...(options.onError && { onError: options.onError }),
|
|
20
|
+
...(options.onCancel && { onCancel: options.onCancel }),
|
|
21
|
+
...(historyAdapter && { history: historyAdapter }),
|
|
25
22
|
notifyUpdate,
|
|
26
23
|
});
|
|
27
24
|
}
|
|
@@ -30,22 +27,41 @@ export function useAgUiRuntime(options) {
|
|
|
30
27
|
agent: options.agent,
|
|
31
28
|
logger,
|
|
32
29
|
showThinking: options.showThinking ?? true,
|
|
33
|
-
...(options.onError
|
|
34
|
-
...(options.onCancel
|
|
35
|
-
...(historyAdapter
|
|
30
|
+
...(options.onError && { onError: options.onError }),
|
|
31
|
+
...(options.onCancel && { onCancel: options.onCancel }),
|
|
32
|
+
...(historyAdapter && { history: historyAdapter }),
|
|
36
33
|
});
|
|
37
|
-
const
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
34
|
+
const threadList = useMemo(() => {
|
|
35
|
+
if (!threadListAdapter)
|
|
36
|
+
return undefined;
|
|
37
|
+
const { onSwitchToNewThread, onSwitchToThread } = threadListAdapter;
|
|
38
|
+
return {
|
|
39
|
+
threadId: threadListAdapter.threadId,
|
|
40
|
+
onSwitchToNewThread: onSwitchToNewThread
|
|
41
|
+
? async () => {
|
|
42
|
+
await onSwitchToNewThread();
|
|
43
|
+
core.applyExternalMessages([]);
|
|
44
|
+
}
|
|
45
|
+
: undefined,
|
|
46
|
+
onSwitchToThread: onSwitchToThread
|
|
47
|
+
? async (threadId) => {
|
|
48
|
+
const result = await onSwitchToThread(threadId);
|
|
49
|
+
core.applyExternalMessages(result.messages);
|
|
50
|
+
if (result.state) {
|
|
51
|
+
core.loadExternalState(result.state);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
: undefined,
|
|
55
|
+
};
|
|
56
|
+
}, [threadListAdapter, core]);
|
|
57
|
+
const adapters = options.adapters;
|
|
58
|
+
const adapterAdapters = useMemo(() => ({
|
|
59
|
+
attachments: adapters?.attachments ?? runtimeAdapters?.attachments,
|
|
60
|
+
speech: adapters?.speech,
|
|
61
|
+
dictation: adapters?.dictation,
|
|
62
|
+
feedback: adapters?.feedback,
|
|
63
|
+
threadList,
|
|
64
|
+
}), [adapters, runtimeAdapters, threadList]);
|
|
49
65
|
const store = useMemo(() => {
|
|
50
66
|
void _version; // rerender on version change
|
|
51
67
|
return {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useAgUiRuntime.js","sourceRoot":"","sources":["../src/useAgUiRuntime.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAC1E,OAAO,EACL,uBAAuB,EACvB,kBAAkB,GACnB,MAAM,qBAAqB,CAAC;AAQ7B,OAAO,EAAE,UAAU,EAAE,4BAAyB;AAE9C,OAAO,EAAE,qBAAqB,EAAE,2CAAwC;AAExE,MAAM,UAAU,cAAc,CAC5B,OAA8B;IAE9B,MAAM,MAAM,GAAG,OAAO,CAAC,GAAG,EAAE,CAAC,UAAU,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC;IAC3E,MAAM,CAAC,QAAQ,EAAE,UAAU,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAC;IAC3C,MAAM,YAAY,GAAG,WAAW,CAAC,GAAG,EAAE,CAAC,UAAU,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;IACrE,MAAM,OAAO,GAAG,MAAM,CAA+B,IAAI,CAAC,CAAC;IAC3D,MAAM,eAAe,GAAG,kBAAkB,EAAE,CAAC;IAE7C,MAAM,
|
|
1
|
+
{"version":3,"file":"useAgUiRuntime.js","sour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[
|
|
6
6
|
"ag-ui",
|
|
@@ -31,11 +31,11 @@
|
|
|
31
31
|
"sideEffects": false,
|
|
32
32
|
"dependencies": {
|
|
33
33
|
"@ag-ui/client": "^0.0.43",
|
|
34
|
-
"assistant-stream": "^0.
|
|
35
|
-
"zod": "^4.3.
|
|
34
|
+
"assistant-stream": "^0.3.0",
|
|
35
|
+
"zod": "^4.3.6"
|
|
36
36
|
},
|
|
37
37
|
"peerDependencies": {
|
|
38
|
-
"@assistant-ui/react": "^0.12.
|
|
38
|
+
"@assistant-ui/react": "^0.12.3",
|
|
39
39
|
"@types/react": "*",
|
|
40
40
|
"react": "^18 || ^19"
|
|
41
41
|
},
|
|
@@ -46,9 +46,9 @@
|
|
|
46
46
|
},
|
|
47
47
|
"devDependencies": {
|
|
48
48
|
"@types/react": "^19.2.9",
|
|
49
|
-
"react": "^19.2.
|
|
50
|
-
"vitest": "^4.0.
|
|
51
|
-
"@assistant-ui/react": "0.12.
|
|
49
|
+
"react": "^19.2.4",
|
|
50
|
+
"vitest": "^4.0.18",
|
|
51
|
+
"@assistant-ui/react": "0.12.3",
|
|
52
52
|
"@assistant-ui/x-buildutils": "0.0.1"
|
|
53
53
|
},
|
|
54
54
|
"publishConfig": {
|
package/src/index.ts
CHANGED
|
@@ -1,18 +1,16 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import { type Tool, toToolsJSONSchema } from "assistant-stream";
|
|
4
4
|
|
|
5
5
|
type ThreadMessageLike = {
|
|
6
6
|
id: string;
|
|
7
7
|
role: string;
|
|
8
|
-
content:
|
|
8
|
+
content: unknown;
|
|
9
9
|
name?: string;
|
|
10
|
+
toolCallId?: string;
|
|
11
|
+
error?: string;
|
|
10
12
|
};
|
|
11
13
|
|
|
12
|
-
const generateId = () =>
|
|
13
|
-
(globalThis.crypto as any)?.randomUUID?.() ??
|
|
14
|
-
Math.random().toString(36).slice(2);
|
|
15
|
-
|
|
16
14
|
type AgUiToolCall = {
|
|
17
15
|
id: string;
|
|
18
16
|
type: "function";
|
|
@@ -45,125 +43,163 @@ type ToolCallPart = {
|
|
|
45
43
|
isError?: boolean;
|
|
46
44
|
};
|
|
47
45
|
|
|
48
|
-
|
|
46
|
+
function generateId(): string {
|
|
47
|
+
return (
|
|
48
|
+
(globalThis.crypto as { randomUUID?: () => string })?.randomUUID?.() ??
|
|
49
|
+
Math.random().toString(36).slice(2)
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function normalizeToolCall(part: ToolCallPart): {
|
|
54
|
+
id: string;
|
|
55
|
+
call: AgUiToolCall;
|
|
56
|
+
} {
|
|
49
57
|
const id = part.toolCallId ?? generateId();
|
|
50
58
|
const argsText =
|
|
51
59
|
typeof part.argsText === "string"
|
|
52
60
|
? part.argsText
|
|
53
61
|
: JSON.stringify(part.args ?? {});
|
|
62
|
+
|
|
54
63
|
return {
|
|
55
64
|
id,
|
|
56
65
|
call: {
|
|
57
66
|
id,
|
|
58
|
-
type: "function"
|
|
67
|
+
type: "function",
|
|
59
68
|
function: {
|
|
60
69
|
name: part.toolName ?? "tool",
|
|
61
70
|
arguments: argsText,
|
|
62
71
|
},
|
|
63
72
|
},
|
|
64
73
|
};
|
|
65
|
-
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function extractText(content: unknown): string {
|
|
77
|
+
if (typeof content === "string") return content;
|
|
78
|
+
if (!Array.isArray(content)) return "";
|
|
79
|
+
|
|
80
|
+
return content
|
|
81
|
+
.filter(
|
|
82
|
+
(part): part is { type: "text"; text: string } =>
|
|
83
|
+
part?.type === "text" && typeof part?.text === "string",
|
|
84
|
+
)
|
|
85
|
+
.map((part) => part.text)
|
|
86
|
+
.join("\n");
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function convertAssistantMessage(
|
|
90
|
+
message: ThreadMessageLike,
|
|
91
|
+
converted: AgUiMessage[],
|
|
92
|
+
): void {
|
|
93
|
+
const content = extractText(message.content);
|
|
94
|
+
const contentArray = Array.isArray(message.content) ? message.content : [];
|
|
95
|
+
|
|
96
|
+
const toolCallParts = contentArray.filter(
|
|
97
|
+
(part): part is ToolCallPart => part?.type === "tool-call",
|
|
98
|
+
);
|
|
99
|
+
|
|
100
|
+
const toolCalls = toolCallParts.map((part) => ({
|
|
101
|
+
...normalizeToolCall(part),
|
|
102
|
+
part,
|
|
103
|
+
}));
|
|
104
|
+
|
|
105
|
+
const assistantMessage: AgUiMessage = {
|
|
106
|
+
id: message.id,
|
|
107
|
+
role: "assistant",
|
|
108
|
+
content,
|
|
109
|
+
};
|
|
110
|
+
if (message.name) {
|
|
111
|
+
assistantMessage.name = message.name;
|
|
112
|
+
}
|
|
113
|
+
if (toolCalls.length > 0) {
|
|
114
|
+
assistantMessage.toolCalls = toolCalls.map((entry) => entry.call);
|
|
115
|
+
}
|
|
116
|
+
converted.push(assistantMessage);
|
|
117
|
+
|
|
118
|
+
for (const { id: toolCallId, part } of toolCalls) {
|
|
119
|
+
if (part.result === undefined) continue;
|
|
120
|
+
|
|
121
|
+
const resultContent =
|
|
122
|
+
typeof part.result === "string"
|
|
123
|
+
? part.result
|
|
124
|
+
: JSON.stringify(part.result);
|
|
66
125
|
|
|
67
|
-
|
|
126
|
+
const toolMessage: AgUiMessage = {
|
|
127
|
+
id: `${toolCallId}:tool`,
|
|
128
|
+
role: "tool",
|
|
129
|
+
content: resultContent,
|
|
130
|
+
toolCallId,
|
|
131
|
+
};
|
|
132
|
+
if (part.isError) {
|
|
133
|
+
toolMessage.error = resultContent;
|
|
134
|
+
}
|
|
135
|
+
converted.push(toolMessage);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function convertToolMessage(
|
|
140
|
+
message: ThreadMessageLike,
|
|
141
|
+
converted: AgUiMessage[],
|
|
142
|
+
): void {
|
|
143
|
+
const content = extractText(message.content);
|
|
144
|
+
const toolCallId = message.toolCallId ?? generateId();
|
|
145
|
+
|
|
146
|
+
const toolMessage: AgUiMessage = {
|
|
147
|
+
id: message.id,
|
|
148
|
+
role: "tool",
|
|
149
|
+
content,
|
|
150
|
+
toolCallId,
|
|
151
|
+
};
|
|
152
|
+
if (typeof message.error === "string") {
|
|
153
|
+
toolMessage.error = message.error;
|
|
154
|
+
}
|
|
155
|
+
converted.push(toolMessage);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export function toAgUiMessages(
|
|
68
159
|
messages: readonly ThreadMessageLike[],
|
|
69
|
-
): AgUiMessage[]
|
|
160
|
+
): AgUiMessage[] {
|
|
70
161
|
const converted: AgUiMessage[] = [];
|
|
71
162
|
|
|
72
163
|
for (const message of messages) {
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
if (role === "assistant" && Array.isArray(message.content)) {
|
|
77
|
-
const toolCallParts = message.content.filter(
|
|
78
|
-
(part): part is ToolCallPart => part?.type === "tool-call",
|
|
79
|
-
);
|
|
80
|
-
|
|
81
|
-
const toolCalls = toolCallParts.map((part) => {
|
|
82
|
-
const { id, call } = normaliseToolCall(part);
|
|
83
|
-
return { id, call, part };
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
converted.push({
|
|
87
|
-
id: message.id,
|
|
88
|
-
role: "assistant",
|
|
89
|
-
content,
|
|
90
|
-
...(message.name ? { name: message.name } : {}),
|
|
91
|
-
...(toolCalls.length > 0
|
|
92
|
-
? { toolCalls: toolCalls.map((entry) => entry.call) }
|
|
93
|
-
: {}),
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
for (const { id: toolCallId, part } of toolCalls) {
|
|
97
|
-
if (part.result === undefined) continue;
|
|
98
|
-
|
|
99
|
-
const resultContent =
|
|
100
|
-
typeof part.result === "string"
|
|
101
|
-
? part.result
|
|
102
|
-
: JSON.stringify(part.result);
|
|
103
|
-
converted.push({
|
|
104
|
-
id: `${toolCallId}:tool`,
|
|
105
|
-
role: "tool",
|
|
106
|
-
content: resultContent,
|
|
107
|
-
toolCallId,
|
|
108
|
-
...(part.isError ? { error: resultContent } : {}),
|
|
109
|
-
});
|
|
110
|
-
}
|
|
111
|
-
|
|
164
|
+
if (message.role === "assistant") {
|
|
165
|
+
convertAssistantMessage(message, converted);
|
|
112
166
|
continue;
|
|
113
167
|
}
|
|
114
168
|
|
|
115
|
-
if (role === "tool") {
|
|
116
|
-
|
|
117
|
-
converted.push({
|
|
118
|
-
id: message.id,
|
|
119
|
-
role: "tool",
|
|
120
|
-
content,
|
|
121
|
-
toolCallId,
|
|
122
|
-
...(typeof (message as any).error === "string"
|
|
123
|
-
? { error: (message as any).error }
|
|
124
|
-
: undefined),
|
|
125
|
-
});
|
|
169
|
+
if (message.role === "tool") {
|
|
170
|
+
convertToolMessage(message, converted);
|
|
126
171
|
continue;
|
|
127
172
|
}
|
|
128
173
|
|
|
129
|
-
|
|
174
|
+
const genericMessage: AgUiMessage = {
|
|
130
175
|
id: message.id,
|
|
131
|
-
role,
|
|
132
|
-
content,
|
|
133
|
-
|
|
134
|
-
|
|
176
|
+
role: message.role,
|
|
177
|
+
content: extractText(message.content),
|
|
178
|
+
};
|
|
179
|
+
if (message.name) {
|
|
180
|
+
genericMessage.name = message.name;
|
|
181
|
+
}
|
|
182
|
+
converted.push(genericMessage);
|
|
135
183
|
}
|
|
136
184
|
|
|
137
185
|
return converted;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
const extractText = (content: any): string => {
|
|
141
|
-
if (!Array.isArray(content)) {
|
|
142
|
-
if (typeof content === "string") return content;
|
|
143
|
-
return "";
|
|
144
|
-
}
|
|
186
|
+
}
|
|
145
187
|
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
188
|
+
type AgUiTool = {
|
|
189
|
+
name: string;
|
|
190
|
+
description: string | undefined;
|
|
191
|
+
parameters: unknown;
|
|
150
192
|
};
|
|
151
193
|
|
|
152
|
-
export
|
|
194
|
+
export function toAgUiTools(
|
|
195
|
+
tools: Record<string, Tool> | undefined,
|
|
196
|
+
): AgUiTool[] {
|
|
153
197
|
if (!tools) return [];
|
|
154
198
|
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
? z.toJSONSchema(tool.parameters)
|
|
163
|
-
: typeof tool?.parameters?.toJSON === "function"
|
|
164
|
-
? tool.parameters.toJSON()
|
|
165
|
-
: typeof tool?.parameters?.toJSONSchema === "function"
|
|
166
|
-
? tool.parameters.toJSONSchema()
|
|
167
|
-
: tool?.parameters,
|
|
168
|
-
}));
|
|
169
|
-
};
|
|
199
|
+
const toolsSchema = toToolsJSONSchema(tools);
|
|
200
|
+
return Object.entries(toolsSchema).map(([name, tool]) => ({
|
|
201
|
+
name,
|
|
202
|
+
description: tool.description,
|
|
203
|
+
parameters: tool.parameters,
|
|
204
|
+
}));
|
|
205
|
+
}
|
package/src/runtime/types.ts
CHANGED
|
@@ -4,9 +4,27 @@ import type {
|
|
|
4
4
|
FeedbackAdapter,
|
|
5
5
|
SpeechSynthesisAdapter,
|
|
6
6
|
ThreadHistoryAdapter,
|
|
7
|
+
ThreadMessage,
|
|
7
8
|
} from "@assistant-ui/react";
|
|
8
9
|
import type { HttpAgent } from "@ag-ui/client";
|
|
9
10
|
import type { Logger } from "./logger";
|
|
11
|
+
import type { ReadonlyJSONValue } from "assistant-stream/utils";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @experimental This API is still under active development and might change without notice.
|
|
15
|
+
*/
|
|
16
|
+
export type UseAgUiThreadListAdapter = {
|
|
17
|
+
threadId?: string | undefined;
|
|
18
|
+
onSwitchToNewThread?: (() => Promise<void> | void) | undefined;
|
|
19
|
+
onSwitchToThread?:
|
|
20
|
+
| ((threadId: string) =>
|
|
21
|
+
| Promise<{
|
|
22
|
+
messages: readonly ThreadMessage[];
|
|
23
|
+
state?: ReadonlyJSONValue;
|
|
24
|
+
}>
|
|
25
|
+
| { messages: readonly ThreadMessage[]; state?: ReadonlyJSONValue })
|
|
26
|
+
| undefined;
|
|
27
|
+
};
|
|
10
28
|
|
|
11
29
|
export type UseAgUiRuntimeAdapters = {
|
|
12
30
|
attachments?: AttachmentAdapter;
|
|
@@ -14,6 +32,10 @@ export type UseAgUiRuntimeAdapters = {
|
|
|
14
32
|
dictation?: DictationAdapter;
|
|
15
33
|
feedback?: FeedbackAdapter;
|
|
16
34
|
history?: ThreadHistoryAdapter;
|
|
35
|
+
/**
|
|
36
|
+
* @experimental This API is still under active development and might change without notice.
|
|
37
|
+
*/
|
|
38
|
+
threadList?: UseAgUiThreadListAdapter;
|
|
17
39
|
};
|
|
18
40
|
|
|
19
41
|
export type UseAgUiRuntimeOptions = {
|
package/src/useAgUiRuntime.ts
CHANGED
|
@@ -25,21 +25,17 @@ export function useAgUiRuntime(
|
|
|
25
25
|
const coreRef = useRef<AgUiThreadRuntimeCore | null>(null);
|
|
26
26
|
const runtimeAdapters = useRuntimeAdapters();
|
|
27
27
|
|
|
28
|
-
const attachmentsAdapter =
|
|
29
|
-
options.adapters?.attachments ?? runtimeAdapters?.attachments;
|
|
30
28
|
const historyAdapter = options.adapters?.history ?? runtimeAdapters?.history;
|
|
31
|
-
const
|
|
32
|
-
const dictationAdapter = options.adapters?.dictation;
|
|
33
|
-
const feedbackAdapter = options.adapters?.feedback;
|
|
29
|
+
const threadListAdapter = options.adapters?.threadList;
|
|
34
30
|
|
|
35
31
|
if (!coreRef.current) {
|
|
36
32
|
coreRef.current = new AgUiThreadRuntimeCore({
|
|
37
33
|
agent: options.agent,
|
|
38
34
|
logger,
|
|
39
35
|
showThinking: options.showThinking ?? true,
|
|
40
|
-
...(options.onError
|
|
41
|
-
...(options.onCancel
|
|
42
|
-
...(historyAdapter
|
|
36
|
+
...(options.onError && { onError: options.onError }),
|
|
37
|
+
...(options.onCancel && { onCancel: options.onCancel }),
|
|
38
|
+
...(historyAdapter && { history: historyAdapter }),
|
|
43
39
|
notifyUpdate,
|
|
44
40
|
});
|
|
45
41
|
}
|
|
@@ -49,20 +45,47 @@ export function useAgUiRuntime(
|
|
|
49
45
|
agent: options.agent,
|
|
50
46
|
logger,
|
|
51
47
|
showThinking: options.showThinking ?? true,
|
|
52
|
-
...(options.onError
|
|
53
|
-
...(options.onCancel
|
|
54
|
-
...(historyAdapter
|
|
48
|
+
...(options.onError && { onError: options.onError }),
|
|
49
|
+
...(options.onCancel && { onCancel: options.onCancel }),
|
|
50
|
+
...(historyAdapter && { history: historyAdapter }),
|
|
55
51
|
});
|
|
56
52
|
|
|
57
|
-
const
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
53
|
+
const threadList = useMemo(() => {
|
|
54
|
+
if (!threadListAdapter) return undefined;
|
|
55
|
+
|
|
56
|
+
const { onSwitchToNewThread, onSwitchToThread } = threadListAdapter;
|
|
57
|
+
|
|
58
|
+
return {
|
|
59
|
+
threadId: threadListAdapter.threadId,
|
|
60
|
+
onSwitchToNewThread: onSwitchToNewThread
|
|
61
|
+
? async () => {
|
|
62
|
+
await onSwitchToNewThread();
|
|
63
|
+
core.applyExternalMessages([]);
|
|
64
|
+
}
|
|
65
|
+
: undefined,
|
|
66
|
+
onSwitchToThread: onSwitchToThread
|
|
67
|
+
? async (threadId: string) => {
|
|
68
|
+
const result = await onSwitchToThread(threadId);
|
|
69
|
+
core.applyExternalMessages(result.messages);
|
|
70
|
+
if (result.state) {
|
|
71
|
+
core.loadExternalState(result.state);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
: undefined,
|
|
75
|
+
};
|
|
76
|
+
}, [threadListAdapter, core]);
|
|
77
|
+
|
|
78
|
+
const adapters = options.adapters;
|
|
79
|
+
const adapterAdapters = useMemo(
|
|
80
|
+
() => ({
|
|
81
|
+
attachments: adapters?.attachments ?? runtimeAdapters?.attachments,
|
|
82
|
+
speech: adapters?.speech,
|
|
83
|
+
dictation: adapters?.dictation,
|
|
84
|
+
feedback: adapters?.feedback,
|
|
85
|
+
threadList,
|
|
86
|
+
}),
|
|
87
|
+
[adapters, runtimeAdapters, threadList],
|
|
88
|
+
);
|
|
66
89
|
|
|
67
90
|
const store = useMemo(
|
|
68
91
|
() => {
|