@assistant-ui/react 0.12.1 → 0.12.5
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/client/ExternalThread.d.ts +16 -0
- package/dist/client/ExternalThread.d.ts.map +1 -0
- package/dist/client/ExternalThread.js +329 -0
- package/dist/client/ExternalThread.js.map +1 -0
- package/dist/client/InMemoryThreadList.d.ts +9 -0
- package/dist/client/InMemoryThreadList.d.ts.map +1 -0
- package/dist/client/InMemoryThreadList.js +129 -0
- package/dist/client/InMemoryThreadList.js.map +1 -0
- package/dist/client/Suggestions.d.ts +11 -0
- package/dist/client/Suggestions.d.ts.map +1 -0
- package/dist/client/Suggestions.js +43 -0
- package/dist/client/Suggestions.js.map +1 -0
- package/dist/client/Tools.d.ts.map +1 -1
- package/dist/client/Tools.js +5 -1
- package/dist/client/Tools.js.map +1 -1
- package/dist/client/index.d.ts +6 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +6 -0
- package/dist/client/index.js.map +1 -0
- package/dist/context/providers/SuggestionByIndexProvider.d.ts +6 -0
- package/dist/context/providers/SuggestionByIndexProvider.d.ts.map +1 -0
- package/dist/context/providers/SuggestionByIndexProvider.js +14 -0
- package/dist/context/providers/SuggestionByIndexProvider.js.map +1 -0
- package/dist/context/providers/index.d.ts +1 -0
- package/dist/context/providers/index.d.ts.map +1 -1
- package/dist/context/providers/index.js +1 -0
- package/dist/context/providers/index.js.map +1 -1
- package/dist/devtools/index.d.ts +1 -1
- package/dist/devtools/index.d.ts.map +1 -1
- package/dist/devtools/index.js +1 -1
- package/dist/devtools/index.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/legacy-runtime/AssistantRuntimeProvider.d.ts +5 -0
- package/dist/legacy-runtime/AssistantRuntimeProvider.d.ts.map +1 -1
- package/dist/legacy-runtime/AssistantRuntimeProvider.js +9 -5
- package/dist/legacy-runtime/AssistantRuntimeProvider.js.map +1 -1
- package/dist/legacy-runtime/RuntimeAdapter.js +2 -1
- package/dist/legacy-runtime/RuntimeAdapter.js.map +1 -1
- package/dist/legacy-runtime/runtime-cores/assistant-transport/useAssistantTransportRuntime.d.ts.map +1 -1
- package/dist/legacy-runtime/runtime-cores/assistant-transport/useAssistantTransportRuntime.js +3 -5
- package/dist/legacy-runtime/runtime-cores/assistant-transport/useAssistantTransportRuntime.js.map +1 -1
- package/dist/legacy-runtime/runtime-cores/assistant-transport/utils.d.ts +10 -12
- package/dist/legacy-runtime/runtime-cores/assistant-transport/utils.d.ts.map +1 -1
- package/dist/legacy-runtime/runtime-cores/assistant-transport/utils.js +14 -19
- package/dist/legacy-runtime/runtime-cores/assistant-transport/utils.js.map +1 -1
- package/dist/legacy-runtime/runtime-cores/external-store/external-message-converter.d.ts.map +1 -1
- package/dist/legacy-runtime/runtime-cores/external-store/external-message-converter.js +32 -4
- package/dist/legacy-runtime/runtime-cores/external-store/external-message-converter.js.map +1 -1
- package/dist/legacy-runtime/runtime-cores/remote-thread-list/BaseSubscribable.d.ts.map +1 -1
- package/dist/legacy-runtime/runtime-cores/remote-thread-list/BaseSubscribable.js +3 -0
- package/dist/legacy-runtime/runtime-cores/remote-thread-list/BaseSubscribable.js.map +1 -1
- package/dist/model-context/index.d.ts +1 -0
- package/dist/model-context/index.d.ts.map +1 -1
- package/dist/model-context/index.js +1 -0
- package/dist/model-context/index.js.map +1 -1
- package/dist/primitives/assistantModal/AssistantModalRoot.js +1 -1
- package/dist/primitives/assistantModal/AssistantModalRoot.js.map +1 -1
- package/dist/primitives/index.d.ts +1 -0
- package/dist/primitives/index.d.ts.map +1 -1
- package/dist/primitives/index.js +1 -0
- package/dist/primitives/index.js.map +1 -1
- package/dist/primitives/message/MessageParts.d.ts +11 -0
- package/dist/primitives/message/MessageParts.d.ts.map +1 -1
- package/dist/primitives/message/MessageParts.js +18 -2
- package/dist/primitives/message/MessageParts.js.map +1 -1
- package/dist/primitives/message/MessagePartsGrouped.js +1 -1
- package/dist/primitives/message/MessagePartsGrouped.js.map +1 -1
- package/dist/primitives/suggestion/SuggestionDescription.d.ts +18 -0
- package/dist/primitives/suggestion/SuggestionDescription.d.ts.map +1 -0
- package/dist/primitives/suggestion/SuggestionDescription.js +19 -0
- package/dist/primitives/suggestion/SuggestionDescription.js.map +1 -0
- package/dist/primitives/suggestion/SuggestionTitle.d.ts +18 -0
- package/dist/primitives/suggestion/SuggestionTitle.d.ts.map +1 -0
- package/dist/primitives/suggestion/SuggestionTitle.js +19 -0
- package/dist/primitives/suggestion/SuggestionTitle.js.map +1 -0
- package/dist/primitives/suggestion/SuggestionTrigger.d.ts +49 -0
- package/dist/primitives/suggestion/SuggestionTrigger.d.ts.map +1 -0
- package/dist/primitives/suggestion/SuggestionTrigger.js +45 -0
- package/dist/primitives/suggestion/SuggestionTrigger.js.map +1 -0
- package/dist/primitives/suggestion/index.d.ts +4 -0
- package/dist/primitives/suggestion/index.d.ts.map +1 -0
- package/dist/primitives/suggestion/index.js +4 -0
- package/dist/primitives/suggestion/index.js.map +1 -0
- package/dist/primitives/thread/ThreadSuggestions.d.ts +53 -0
- package/dist/primitives/thread/ThreadSuggestions.d.ts.map +1 -0
- package/dist/primitives/thread/ThreadSuggestions.js +58 -0
- package/dist/primitives/thread/ThreadSuggestions.js.map +1 -0
- package/dist/primitives/thread/index.d.ts +1 -0
- package/dist/primitives/thread/index.d.ts.map +1 -1
- package/dist/primitives/thread/index.js +1 -0
- package/dist/primitives/thread/index.js.map +1 -1
- package/dist/types/scopes/index.d.ts +2 -0
- package/dist/types/scopes/index.d.ts.map +1 -1
- package/dist/types/scopes/suggestion.d.ts +20 -0
- package/dist/types/scopes/suggestion.d.ts.map +1 -0
- package/dist/types/scopes/suggestion.js +2 -0
- package/dist/types/scopes/suggestion.js.map +1 -0
- package/dist/types/scopes/suggestions.d.ts +20 -0
- package/dist/types/scopes/suggestions.d.ts.map +1 -0
- package/dist/types/scopes/suggestions.js +2 -0
- package/dist/types/scopes/suggestions.js.map +1 -0
- package/dist/types/store-augmentation.d.ts +4 -0
- package/dist/types/store-augmentation.d.ts.map +1 -1
- package/dist/utils/idUtils.d.ts +2 -0
- package/dist/utils/idUtils.d.ts.map +1 -1
- package/dist/utils/idUtils.js +3 -0
- package/dist/utils/idUtils.js.map +1 -1
- package/package.json +12 -12
- package/src/client/ExternalThread.ts +491 -0
- package/src/client/InMemoryThreadList.ts +197 -0
- package/src/client/Suggestions.ts +74 -0
- package/src/client/Tools.ts +10 -1
- package/src/client/index.ts +12 -0
- package/src/context/providers/SuggestionByIndexProvider.tsx +23 -0
- package/src/context/providers/index.ts +1 -0
- package/src/devtools/index.ts +1 -1
- package/src/index.ts +1 -0
- package/src/legacy-runtime/AssistantRuntimeProvider.tsx +15 -6
- package/src/legacy-runtime/RuntimeAdapter.ts +2 -1
- package/src/legacy-runtime/runtime-cores/assistant-transport/useAssistantTransportRuntime.tsx +3 -4
- package/src/legacy-runtime/runtime-cores/assistant-transport/utils.ts +19 -24
- package/src/legacy-runtime/runtime-cores/external-store/external-message-converter.tsx +42 -7
- package/src/legacy-runtime/runtime-cores/remote-thread-list/BaseSubscribable.tsx +3 -0
- package/src/model-context/index.ts +2 -0
- package/src/primitives/assistantModal/AssistantModalRoot.tsx +1 -1
- package/src/primitives/index.ts +1 -0
- package/src/primitives/message/MessageParts.tsx +45 -1
- package/src/primitives/message/MessagePartsGrouped.tsx +1 -1
- package/src/primitives/suggestion/SuggestionDescription.tsx +33 -0
- package/src/primitives/suggestion/SuggestionTitle.tsx +33 -0
- package/src/primitives/suggestion/SuggestionTrigger.tsx +79 -0
- package/src/primitives/suggestion/index.ts +3 -0
- package/src/primitives/thread/ThreadSuggestions.tsx +109 -0
- package/src/primitives/thread/index.ts +4 -0
- package/src/tests/external-message-converter.test.ts +105 -16
- package/src/types/scopes/index.ts +12 -0
- package/src/types/scopes/suggestion.ts +20 -0
- package/src/types/scopes/suggestions.ts +21 -0
- package/src/types/store-augmentation.ts +4 -0
- package/src/utils/idUtils.tsx +4 -0
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import { resource, tapState, tapMemo } from "@assistant-ui/tap";
|
|
2
|
+
import {
|
|
3
|
+
type ClientOutput,
|
|
4
|
+
tapClientLookup,
|
|
5
|
+
Derived,
|
|
6
|
+
attachDefaultPeers,
|
|
7
|
+
tapClientResource,
|
|
8
|
+
} from "@assistant-ui/store";
|
|
9
|
+
import { withKey } from "@assistant-ui/tap";
|
|
10
|
+
import type { ResourceElement } from "@assistant-ui/tap";
|
|
11
|
+
import type { ThreadState } from "../types/scopes/thread";
|
|
12
|
+
import { Suggestions } from "./Suggestions";
|
|
13
|
+
import { ModelContext } from "./ModelContextClient";
|
|
14
|
+
import { Tools } from "./Tools";
|
|
15
|
+
|
|
16
|
+
export type InMemoryThreadListProps = {
|
|
17
|
+
thread: (threadId: string) => ResourceElement<ClientOutput<"thread">>;
|
|
18
|
+
onSwitchToThread?: (threadId: string) => void;
|
|
19
|
+
onSwitchToNewThread?: () => void;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
type ThreadData = {
|
|
23
|
+
id: string;
|
|
24
|
+
title?: string;
|
|
25
|
+
status: "regular" | "archived";
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
// ThreadListItem Client
|
|
29
|
+
const ThreadListItemClient = resource(
|
|
30
|
+
(props: {
|
|
31
|
+
data: ThreadData;
|
|
32
|
+
onSwitchTo: () => void;
|
|
33
|
+
onArchive: () => void;
|
|
34
|
+
onUnarchive: () => void;
|
|
35
|
+
onDelete: () => void;
|
|
36
|
+
}): ClientOutput<"threadListItem"> => {
|
|
37
|
+
const { data, onSwitchTo, onArchive, onUnarchive, onDelete } = props;
|
|
38
|
+
const state = tapMemo(
|
|
39
|
+
() => ({
|
|
40
|
+
id: data.id,
|
|
41
|
+
remoteId: undefined,
|
|
42
|
+
externalId: undefined,
|
|
43
|
+
title: data.title,
|
|
44
|
+
status: data.status,
|
|
45
|
+
}),
|
|
46
|
+
[data.id, data.title, data.status],
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
return {
|
|
50
|
+
state,
|
|
51
|
+
methods: {
|
|
52
|
+
getState: () => state,
|
|
53
|
+
switchTo: onSwitchTo,
|
|
54
|
+
rename: () => {},
|
|
55
|
+
archive: onArchive,
|
|
56
|
+
unarchive: onUnarchive,
|
|
57
|
+
delete: onDelete,
|
|
58
|
+
generateTitle: () => {},
|
|
59
|
+
initialize: async () => ({ remoteId: data.id, externalId: undefined }),
|
|
60
|
+
detach: () => {},
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
},
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
// InMemoryThreadList Client
|
|
67
|
+
export const InMemoryThreadList = resource(
|
|
68
|
+
(props: InMemoryThreadListProps): ClientOutput<"threads"> => {
|
|
69
|
+
const {
|
|
70
|
+
thread: threadFactory,
|
|
71
|
+
onSwitchToThread,
|
|
72
|
+
onSwitchToNewThread,
|
|
73
|
+
} = props;
|
|
74
|
+
|
|
75
|
+
const [mainThreadId, setMainThreadId] = tapState("main");
|
|
76
|
+
const [threads, setThreads] = tapState<readonly ThreadData[]>(() => [
|
|
77
|
+
{ id: "main", title: "Main Thread", status: "regular" },
|
|
78
|
+
]);
|
|
79
|
+
|
|
80
|
+
const handleSwitchToThread = (threadId: string) => {
|
|
81
|
+
setMainThreadId(threadId);
|
|
82
|
+
onSwitchToThread?.(threadId);
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
const handleArchive = (threadId: string) => {
|
|
86
|
+
setThreads((prev) =>
|
|
87
|
+
prev.map((t) =>
|
|
88
|
+
t.id === threadId ? { ...t, status: "archived" as const } : t,
|
|
89
|
+
),
|
|
90
|
+
);
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
const handleUnarchive = (threadId: string) => {
|
|
94
|
+
setThreads((prev) =>
|
|
95
|
+
prev.map((t) =>
|
|
96
|
+
t.id === threadId ? { ...t, status: "regular" as const } : t,
|
|
97
|
+
),
|
|
98
|
+
);
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
const handleDelete = (threadId: string) => {
|
|
102
|
+
setThreads((prev) => prev.filter((t) => t.id !== threadId));
|
|
103
|
+
if (mainThreadId === threadId) {
|
|
104
|
+
const remaining = threads.filter((t) => t.id !== threadId);
|
|
105
|
+
setMainThreadId(remaining[0]?.id || "main");
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
const handleSwitchToNewThread = () => {
|
|
110
|
+
const newId = `thread-${Date.now()}`;
|
|
111
|
+
setThreads((prev) => [
|
|
112
|
+
...prev,
|
|
113
|
+
{ id: newId, title: "New Thread", status: "regular" },
|
|
114
|
+
]);
|
|
115
|
+
setMainThreadId(newId);
|
|
116
|
+
onSwitchToNewThread?.();
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
const threadListItems = tapClientLookup(
|
|
120
|
+
() =>
|
|
121
|
+
threads.map((t) =>
|
|
122
|
+
withKey(
|
|
123
|
+
t.id,
|
|
124
|
+
ThreadListItemClient({
|
|
125
|
+
data: t,
|
|
126
|
+
onSwitchTo: () => handleSwitchToThread(t.id),
|
|
127
|
+
onArchive: () => handleArchive(t.id),
|
|
128
|
+
onUnarchive: () => handleUnarchive(t.id),
|
|
129
|
+
onDelete: () => handleDelete(t.id),
|
|
130
|
+
}),
|
|
131
|
+
),
|
|
132
|
+
),
|
|
133
|
+
[threads],
|
|
134
|
+
);
|
|
135
|
+
|
|
136
|
+
// Create the main thread
|
|
137
|
+
const mainThreadClient = tapClientResource(threadFactory(mainThreadId));
|
|
138
|
+
|
|
139
|
+
const state = tapMemo(() => {
|
|
140
|
+
const regularThreads = threads.filter((t) => t.status === "regular");
|
|
141
|
+
const archivedThreads = threads.filter((t) => t.status === "archived");
|
|
142
|
+
const mainThreadState = mainThreadClient.state as ThreadState;
|
|
143
|
+
|
|
144
|
+
return {
|
|
145
|
+
mainThreadId,
|
|
146
|
+
newThreadId: null,
|
|
147
|
+
isLoading: false,
|
|
148
|
+
threadIds: regularThreads.map((t) => t.id),
|
|
149
|
+
archivedThreadIds: archivedThreads.map((t) => t.id),
|
|
150
|
+
threadItems: threadListItems.state,
|
|
151
|
+
main: mainThreadState,
|
|
152
|
+
};
|
|
153
|
+
}, [mainThreadId, threads, threadListItems.state, mainThreadClient.state]);
|
|
154
|
+
|
|
155
|
+
return {
|
|
156
|
+
state,
|
|
157
|
+
methods: {
|
|
158
|
+
getState: () => state,
|
|
159
|
+
switchToThread: handleSwitchToThread,
|
|
160
|
+
switchToNewThread: handleSwitchToNewThread,
|
|
161
|
+
item: (selector) => {
|
|
162
|
+
if (selector === "main") {
|
|
163
|
+
const index = threads.findIndex((t) => t.id === mainThreadId);
|
|
164
|
+
return threadListItems.get({ index: index === -1 ? 0 : index });
|
|
165
|
+
}
|
|
166
|
+
if ("id" in selector) {
|
|
167
|
+
const index = threads.findIndex((t) => t.id === selector.id);
|
|
168
|
+
return threadListItems.get({ index });
|
|
169
|
+
}
|
|
170
|
+
return threadListItems.get(selector);
|
|
171
|
+
},
|
|
172
|
+
thread: () => mainThreadClient.methods,
|
|
173
|
+
},
|
|
174
|
+
};
|
|
175
|
+
},
|
|
176
|
+
);
|
|
177
|
+
|
|
178
|
+
attachDefaultPeers(InMemoryThreadList, {
|
|
179
|
+
thread: Derived({
|
|
180
|
+
source: "threads",
|
|
181
|
+
query: { type: "main" },
|
|
182
|
+
get: (aui) => aui.threads().thread("main"),
|
|
183
|
+
}),
|
|
184
|
+
threadListItem: Derived({
|
|
185
|
+
source: "threads",
|
|
186
|
+
query: { type: "main" },
|
|
187
|
+
get: (aui) => aui.threads().item("main"),
|
|
188
|
+
}),
|
|
189
|
+
composer: Derived({
|
|
190
|
+
source: "thread",
|
|
191
|
+
query: {},
|
|
192
|
+
get: (aui) => aui.threads().thread("main").composer,
|
|
193
|
+
}),
|
|
194
|
+
modelContext: ModelContext(),
|
|
195
|
+
tools: Tools({}),
|
|
196
|
+
suggestions: Suggestions(),
|
|
197
|
+
});
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { resource, tapState, withKey } from "@assistant-ui/tap";
|
|
2
|
+
import { type ClientOutput, tapClientLookup } from "@assistant-ui/store";
|
|
3
|
+
import type { SuggestionsState, SuggestionState } from "../types/scopes";
|
|
4
|
+
|
|
5
|
+
export type SuggestionConfig =
|
|
6
|
+
| string
|
|
7
|
+
| { title: string; label: string; prompt: string };
|
|
8
|
+
|
|
9
|
+
const SuggestionClient = resource(
|
|
10
|
+
(state: SuggestionState): ClientOutput<"suggestion"> => {
|
|
11
|
+
return {
|
|
12
|
+
state,
|
|
13
|
+
methods: {
|
|
14
|
+
getState: () => state,
|
|
15
|
+
},
|
|
16
|
+
};
|
|
17
|
+
},
|
|
18
|
+
);
|
|
19
|
+
|
|
20
|
+
const SuggestionsResource = resource(
|
|
21
|
+
(suggestions?: SuggestionConfig[]): ClientOutput<"suggestions"> => {
|
|
22
|
+
const [state] = tapState<SuggestionsState>(() => {
|
|
23
|
+
const normalizedSuggestions = (suggestions ?? []).map((s) => {
|
|
24
|
+
if (typeof s === "string") {
|
|
25
|
+
return {
|
|
26
|
+
title: s,
|
|
27
|
+
label: "",
|
|
28
|
+
prompt: s,
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
return {
|
|
32
|
+
title: s.title,
|
|
33
|
+
label: s.label,
|
|
34
|
+
prompt: s.prompt,
|
|
35
|
+
};
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
return {
|
|
39
|
+
suggestions: normalizedSuggestions,
|
|
40
|
+
};
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
const suggestionClients = tapClientLookup(
|
|
44
|
+
() =>
|
|
45
|
+
state.suggestions.map((suggestion, index) =>
|
|
46
|
+
withKey(index, SuggestionClient(suggestion)),
|
|
47
|
+
),
|
|
48
|
+
[state.suggestions],
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
return {
|
|
52
|
+
state,
|
|
53
|
+
methods: {
|
|
54
|
+
getState: () => state,
|
|
55
|
+
suggestion: ({ index }: { index: number }) => {
|
|
56
|
+
return suggestionClients.get({ index });
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
},
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
export const Suggestions: {
|
|
64
|
+
(): import("@assistant-ui/tap").ResourceElement<
|
|
65
|
+
ClientOutput<"suggestions">,
|
|
66
|
+
undefined
|
|
67
|
+
>;
|
|
68
|
+
(
|
|
69
|
+
suggestions: SuggestionConfig[],
|
|
70
|
+
): import("@assistant-ui/tap").ResourceElement<
|
|
71
|
+
ClientOutput<"suggestions">,
|
|
72
|
+
SuggestionConfig[]
|
|
73
|
+
>;
|
|
74
|
+
} = SuggestionsResource as any;
|
package/src/client/Tools.ts
CHANGED
|
@@ -1,9 +1,14 @@
|
|
|
1
1
|
import { resource, tapState, tapEffect, tapCallback } from "@assistant-ui/tap";
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
tapAssistantClientRef,
|
|
4
|
+
type ClientOutput,
|
|
5
|
+
attachDefaultPeers,
|
|
6
|
+
} from "@assistant-ui/store";
|
|
3
7
|
import { ToolsState } from "../types/scopes";
|
|
4
8
|
import type { Tool } from "assistant-stream";
|
|
5
9
|
import { type Toolkit } from "../model-context/toolbox";
|
|
6
10
|
import { ToolCallMessagePartComponent } from "../types";
|
|
11
|
+
import { ModelContext } from "./ModelContextClient";
|
|
7
12
|
|
|
8
13
|
export const Tools = resource(
|
|
9
14
|
({ toolkit }: { toolkit?: Toolkit }): ClientOutput<"tools"> => {
|
|
@@ -86,3 +91,7 @@ export const Tools = resource(
|
|
|
86
91
|
};
|
|
87
92
|
},
|
|
88
93
|
);
|
|
94
|
+
|
|
95
|
+
attachDefaultPeers(Tools, {
|
|
96
|
+
modelContext: ModelContext(),
|
|
97
|
+
});
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export { ModelContext as ModelContextClient } from "./ModelContextClient";
|
|
2
|
+
export { Tools } from "./Tools";
|
|
3
|
+
export { Suggestions, type SuggestionConfig } from "./Suggestions";
|
|
4
|
+
export {
|
|
5
|
+
ExternalThread,
|
|
6
|
+
type ExternalThreadProps,
|
|
7
|
+
type ExternalThreadMessage,
|
|
8
|
+
} from "./ExternalThread";
|
|
9
|
+
export {
|
|
10
|
+
InMemoryThreadList,
|
|
11
|
+
type InMemoryThreadListProps,
|
|
12
|
+
} from "./InMemoryThreadList";
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { type FC, type PropsWithChildren } from "react";
|
|
4
|
+
import { AuiProvider, Derived, useAui } from "@assistant-ui/store";
|
|
5
|
+
|
|
6
|
+
export type SuggestionByIndexProviderProps = PropsWithChildren<{
|
|
7
|
+
index: number;
|
|
8
|
+
}>;
|
|
9
|
+
|
|
10
|
+
export const SuggestionByIndexProvider: FC<SuggestionByIndexProviderProps> = ({
|
|
11
|
+
index,
|
|
12
|
+
children,
|
|
13
|
+
}) => {
|
|
14
|
+
const aui = useAui({
|
|
15
|
+
suggestion: Derived({
|
|
16
|
+
source: "suggestions",
|
|
17
|
+
query: { index },
|
|
18
|
+
get: (aui) => aui.suggestions().suggestion({ index }),
|
|
19
|
+
}),
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
return <AuiProvider value={aui}>{children}</AuiProvider>;
|
|
23
|
+
};
|
|
@@ -4,6 +4,7 @@ export {
|
|
|
4
4
|
ThreadListItemRuntimeProvider,
|
|
5
5
|
} from "./ThreadListItemProvider";
|
|
6
6
|
export { MessageByIndexProvider } from "./MessageByIndexProvider";
|
|
7
|
+
export { SuggestionByIndexProvider } from "./SuggestionByIndexProvider";
|
|
7
8
|
export { PartByIndexProvider } from "./PartByIndexProvider";
|
|
8
9
|
export {
|
|
9
10
|
MessageAttachmentByIndexProvider,
|
package/src/devtools/index.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { DevToolsHooks } from "./DevToolsHooks";
|
|
1
|
+
export { DevToolsHooks, DevToolsProviderApi } from "./DevToolsHooks";
|
package/src/index.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
|
|
3
|
-
import { FC, memo, PropsWithChildren } from "react";
|
|
4
|
-
import { useAui, AuiProvider } from "@assistant-ui/store";
|
|
3
|
+
import { FC, memo, PropsWithChildren, useEffect } from "react";
|
|
4
|
+
import { useAui, AuiProvider, AssistantClient } from "@assistant-ui/store";
|
|
5
5
|
import { AssistantRuntime } from "./runtime/AssistantRuntime";
|
|
6
6
|
import { AssistantRuntimeCore } from "./runtime-cores/core/AssistantRuntimeCore";
|
|
7
7
|
import { RuntimeAdapter } from "./RuntimeAdapter";
|
|
8
8
|
import { ThreadPrimitiveViewportProvider } from "../context/providers/ThreadViewportProvider";
|
|
9
|
+
import { DevToolsProviderApi } from "../devtools";
|
|
9
10
|
|
|
10
11
|
export namespace AssistantRuntimeProvider {
|
|
11
12
|
export type Props = PropsWithChildren<{
|
|
@@ -13,6 +14,11 @@ export namespace AssistantRuntimeProvider {
|
|
|
13
14
|
* The runtime to provide to the rest of your app.
|
|
14
15
|
*/
|
|
15
16
|
runtime: AssistantRuntime;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The aui instance to extend. If not provided, a new aui instance will be created.
|
|
20
|
+
*/
|
|
21
|
+
aui?: AssistantClient;
|
|
16
22
|
}>;
|
|
17
23
|
}
|
|
18
24
|
|
|
@@ -22,10 +28,13 @@ const getRenderComponent = (runtime: AssistantRuntime) => {
|
|
|
22
28
|
|
|
23
29
|
export const AssistantRuntimeProviderImpl: FC<
|
|
24
30
|
AssistantRuntimeProvider.Props
|
|
25
|
-
> = ({ children, runtime }) => {
|
|
26
|
-
const aui = useAui({
|
|
27
|
-
|
|
28
|
-
|
|
31
|
+
> = ({ children, aui: parent = null, runtime }) => {
|
|
32
|
+
const aui = useAui({ threads: RuntimeAdapter(runtime) }, { parent: parent });
|
|
33
|
+
|
|
34
|
+
useEffect(() => {
|
|
35
|
+
if (process.env["NODE_ENV"] === "production") return;
|
|
36
|
+
return DevToolsProviderApi.register(aui);
|
|
37
|
+
}, [aui]);
|
|
29
38
|
|
|
30
39
|
const RenderComponent = getRenderComponent(runtime);
|
|
31
40
|
|
|
@@ -7,7 +7,7 @@ import {
|
|
|
7
7
|
attachDefaultPeers,
|
|
8
8
|
} from "@assistant-ui/store";
|
|
9
9
|
import { ModelContext } from "../client/ModelContextClient";
|
|
10
|
-
import { Tools } from "../model-context";
|
|
10
|
+
import { Tools, Suggestions } from "../model-context";
|
|
11
11
|
|
|
12
12
|
export const RuntimeAdapter = resource((runtime: AssistantRuntime) => {
|
|
13
13
|
const clientRef = tapAssistantClientRef();
|
|
@@ -29,6 +29,7 @@ export const RuntimeAdapter = resource((runtime: AssistantRuntime) => {
|
|
|
29
29
|
attachDefaultPeers(RuntimeAdapter, {
|
|
30
30
|
modelContext: ModelContext(),
|
|
31
31
|
tools: Tools({}),
|
|
32
|
+
suggestions: Suggestions(),
|
|
32
33
|
threadListItem: Derived({
|
|
33
34
|
source: "threads",
|
|
34
35
|
query: { type: "main" },
|
package/src/legacy-runtime/runtime-cores/assistant-transport/useAssistantTransportRuntime.tsx
CHANGED
|
@@ -15,6 +15,7 @@ import {
|
|
|
15
15
|
DataStreamDecoder,
|
|
16
16
|
AssistantTransportDecoder,
|
|
17
17
|
unstable_createInitialMessage as createInitialMessage,
|
|
18
|
+
toToolsJSONSchema,
|
|
18
19
|
} from "assistant-stream";
|
|
19
20
|
import {
|
|
20
21
|
AssistantTransportOptions,
|
|
@@ -28,7 +29,7 @@ import { useCommandQueue } from "./commandQueue";
|
|
|
28
29
|
import { useRunManager } from "./runManager";
|
|
29
30
|
import { useConvertedState } from "./useConvertedState";
|
|
30
31
|
import { ToolExecutionStatus, useToolInvocations } from "./useToolInvocations";
|
|
31
|
-
import {
|
|
32
|
+
import { createRequestHeaders } from "./utils";
|
|
32
33
|
import { useRemoteThreadListRuntime } from "../remote-thread-list/useRemoteThreadListRuntime";
|
|
33
34
|
import { InMemoryThreadListAdapter } from "../remote-thread-list/adapter/in-memory";
|
|
34
35
|
import { useAui, useAuiState } from "@assistant-ui/store";
|
|
@@ -115,9 +116,7 @@ const useAssistantTransportThreadRuntime = <T,>(
|
|
|
115
116
|
commands,
|
|
116
117
|
state: agentStateRef.current,
|
|
117
118
|
system: context.system,
|
|
118
|
-
tools: context.tools
|
|
119
|
-
? toAISDKTools(getEnabledTools(context.tools))
|
|
120
|
-
: undefined,
|
|
119
|
+
tools: context.tools ? toToolsJSONSchema(context.tools) : undefined,
|
|
121
120
|
threadId,
|
|
122
121
|
...(parentIdRef.current !== undefined && {
|
|
123
122
|
parentId: parentIdRef.current,
|
|
@@ -1,42 +1,37 @@
|
|
|
1
|
-
import { Tool } from "assistant-stream";
|
|
2
|
-
import type { JSONSchema7 } from "json-schema";
|
|
3
|
-
import { z } from "zod";
|
|
1
|
+
import { Tool, toToolsJSONSchema, type ToolJSONSchema } from "assistant-stream";
|
|
4
2
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
? z.toJSONSchema(tool.parameters)
|
|
14
|
-
: tool.parameters) as JSONSchema7,
|
|
15
|
-
},
|
|
16
|
-
]),
|
|
17
|
-
);
|
|
18
|
-
};
|
|
3
|
+
/**
|
|
4
|
+
* @deprecated Use `toToolsJSONSchema` from `assistant-stream` instead.
|
|
5
|
+
*/
|
|
6
|
+
export function toAISDKTools(
|
|
7
|
+
tools: Record<string, Tool>,
|
|
8
|
+
): Record<string, ToolJSONSchema> {
|
|
9
|
+
return toToolsJSONSchema(tools, { filter: () => true });
|
|
10
|
+
}
|
|
19
11
|
|
|
20
|
-
|
|
21
|
-
|
|
12
|
+
/**
|
|
13
|
+
* @deprecated Use `toToolsJSONSchema` from `assistant-stream` instead, which includes filtering by default.
|
|
14
|
+
*/
|
|
15
|
+
export function getEnabledTools(
|
|
16
|
+
tools: Record<string, Tool>,
|
|
17
|
+
): Record<string, Tool> {
|
|
22
18
|
return Object.fromEntries(
|
|
23
19
|
Object.entries(tools).filter(
|
|
24
20
|
([, tool]) => !tool.disabled && tool.type !== "backend",
|
|
25
21
|
),
|
|
26
22
|
);
|
|
27
|
-
}
|
|
23
|
+
}
|
|
28
24
|
|
|
29
|
-
|
|
30
|
-
export const createRequestHeaders = async (
|
|
25
|
+
export async function createRequestHeaders(
|
|
31
26
|
headersValue:
|
|
32
27
|
| Record<string, string>
|
|
33
28
|
| Headers
|
|
34
29
|
| (() => Promise<Record<string, string> | Headers>),
|
|
35
|
-
): Promise<Headers>
|
|
30
|
+
): Promise<Headers> {
|
|
36
31
|
const resolvedHeaders =
|
|
37
32
|
typeof headersValue === "function" ? await headersValue() : headersValue;
|
|
38
33
|
|
|
39
34
|
const headers = new Headers(resolvedHeaders);
|
|
40
35
|
headers.set("Content-Type", "application/json");
|
|
41
36
|
return headers;
|
|
42
|
-
}
|
|
37
|
+
}
|
|
@@ -11,6 +11,8 @@ import { getAutoStatus, isAutoStatus } from "./auto-status";
|
|
|
11
11
|
import { ThreadMessage, ToolCallMessagePart } from "../../../types";
|
|
12
12
|
import { ToolExecutionStatus } from "../assistant-transport/useToolInvocations";
|
|
13
13
|
import { ReadonlyJSONValue } from "assistant-stream/utils";
|
|
14
|
+
import { generateErrorMessageId } from "../../../utils/idUtils";
|
|
15
|
+
import { ThreadAssistantMessage } from "../../../types/AssistantTypes";
|
|
14
16
|
|
|
15
17
|
export namespace useExternalMessageConverter {
|
|
16
18
|
export type Message =
|
|
@@ -262,6 +264,28 @@ const chunkExternalMessages = <T,>(
|
|
|
262
264
|
return results;
|
|
263
265
|
};
|
|
264
266
|
|
|
267
|
+
function createErrorAssistantMessage(
|
|
268
|
+
error: ReadonlyJSONValue,
|
|
269
|
+
): ThreadAssistantMessage {
|
|
270
|
+
return Object.assign<ThreadAssistantMessage, { [symbolInnerMessage]: [] }>(
|
|
271
|
+
{
|
|
272
|
+
id: generateErrorMessageId(),
|
|
273
|
+
role: "assistant",
|
|
274
|
+
content: [],
|
|
275
|
+
status: { type: "incomplete", reason: "error", error },
|
|
276
|
+
createdAt: new Date(),
|
|
277
|
+
metadata: {
|
|
278
|
+
unstable_state: null,
|
|
279
|
+
unstable_annotations: [],
|
|
280
|
+
unstable_data: [],
|
|
281
|
+
custom: {},
|
|
282
|
+
steps: [],
|
|
283
|
+
},
|
|
284
|
+
},
|
|
285
|
+
{ [symbolInnerMessage]: [] },
|
|
286
|
+
);
|
|
287
|
+
}
|
|
288
|
+
|
|
265
289
|
export const convertExternalMessages = <T extends WeakKey>(
|
|
266
290
|
messages: T[],
|
|
267
291
|
callback: useExternalMessageConverter.Callback<T>,
|
|
@@ -278,14 +302,9 @@ export const convertExternalMessages = <T extends WeakKey>(
|
|
|
278
302
|
|
|
279
303
|
const chunks = chunkExternalMessages(callbackResults);
|
|
280
304
|
|
|
281
|
-
|
|
305
|
+
const result = chunks.map((message, idx) => {
|
|
282
306
|
const isLast = idx === chunks.length - 1;
|
|
283
307
|
const joined = joinExternalMessages(message.outputs);
|
|
284
|
-
const hasSuspendedToolCalls =
|
|
285
|
-
typeof joined.content === "object" &&
|
|
286
|
-
joined.content.some(
|
|
287
|
-
(c) => c.type === "tool-call" && c.result === undefined,
|
|
288
|
-
);
|
|
289
308
|
const hasPendingToolCalls =
|
|
290
309
|
typeof joined.content === "object" &&
|
|
291
310
|
joined.content.some(
|
|
@@ -294,7 +313,7 @@ export const convertExternalMessages = <T extends WeakKey>(
|
|
|
294
313
|
const autoStatus = getAutoStatus(
|
|
295
314
|
isLast,
|
|
296
315
|
isRunning,
|
|
297
|
-
|
|
316
|
+
hasPendingToolCalls,
|
|
298
317
|
hasPendingToolCalls,
|
|
299
318
|
isLast ? metadata.error : undefined,
|
|
300
319
|
);
|
|
@@ -306,6 +325,15 @@ export const convertExternalMessages = <T extends WeakKey>(
|
|
|
306
325
|
(newMessage as any)[symbolInnerMessage] = message.inputs;
|
|
307
326
|
return newMessage;
|
|
308
327
|
});
|
|
328
|
+
|
|
329
|
+
if (metadata.error) {
|
|
330
|
+
const lastMessage = result.at(-1);
|
|
331
|
+
if (!lastMessage || lastMessage.role !== "assistant") {
|
|
332
|
+
result.push(createErrorAssistantMessage(metadata.error));
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
return result;
|
|
309
337
|
};
|
|
310
338
|
|
|
311
339
|
export const useExternalMessageConverter = <T extends WeakKey>({
|
|
@@ -411,6 +439,13 @@ export const useExternalMessageConverter = <T extends WeakKey>({
|
|
|
411
439
|
symbolInnerMessage
|
|
412
440
|
] = messages;
|
|
413
441
|
|
|
442
|
+
if (state.metadata.error) {
|
|
443
|
+
const lastMessage = threadMessages.at(-1);
|
|
444
|
+
if (!lastMessage || lastMessage.role !== "assistant") {
|
|
445
|
+
threadMessages.push(createErrorAssistantMessage(state.metadata.error));
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
|
|
414
449
|
return threadMessages;
|
|
415
450
|
}, [state, messages, isRunning, joinStrategy]);
|
|
416
451
|
};
|
package/src/primitives/index.ts
CHANGED
|
@@ -8,6 +8,7 @@ export * as MessagePartPrimitive from "./messagePart";
|
|
|
8
8
|
export * as ErrorPrimitive from "./error";
|
|
9
9
|
export * as MessagePrimitive from "./message";
|
|
10
10
|
export * as ThreadPrimitive from "./thread";
|
|
11
|
+
export * as SuggestionPrimitive from "./suggestion";
|
|
11
12
|
export * as ThreadListPrimitive from "./threadList";
|
|
12
13
|
export * as ThreadListItemPrimitive from "./threadListItem";
|
|
13
14
|
export * as ThreadListItemMorePrimitive from "./threadListItemMore";
|