@assistant-ui/core 0.1.2 → 0.1.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/model-context/frame/provider.d.ts.map +1 -1
- package/dist/model-context/frame/provider.js +2 -4
- package/dist/model-context/frame/provider.js.map +1 -1
- package/dist/react/AssistantRuntimeProvider.d.ts +10 -0
- package/dist/react/AssistantRuntimeProvider.d.ts.map +1 -0
- package/dist/react/AssistantRuntimeProvider.js +16 -0
- package/dist/react/AssistantRuntimeProvider.js.map +1 -0
- package/dist/react/adapters/LocalStorageThreadListAdapter.d.ts +15 -0
- package/dist/react/adapters/LocalStorageThreadListAdapter.d.ts.map +1 -0
- package/dist/react/adapters/LocalStorageThreadListAdapter.js +154 -0
- package/dist/react/adapters/LocalStorageThreadListAdapter.js.map +1 -0
- package/dist/react/adapters/TitleGenerationAdapter.d.ts +6 -0
- package/dist/react/adapters/TitleGenerationAdapter.d.ts.map +1 -0
- package/dist/react/adapters/TitleGenerationAdapter.js +15 -0
- package/dist/react/adapters/TitleGenerationAdapter.js.map +1 -0
- package/dist/react/index.d.ts +5 -1
- package/dist/react/index.d.ts.map +1 -1
- package/dist/react/index.js +8 -1
- package/dist/react/index.js.map +1 -1
- package/dist/react/primitive-hooks/index.d.ts +15 -0
- package/dist/react/primitive-hooks/index.d.ts.map +1 -0
- package/dist/react/primitive-hooks/index.js +15 -0
- package/dist/react/primitive-hooks/index.js.map +1 -0
- package/dist/react/primitive-hooks/useActionBarCopy.d.ts +10 -0
- package/dist/react/primitive-hooks/useActionBarCopy.d.ts.map +1 -0
- package/dist/react/primitive-hooks/useActionBarCopy.js +26 -0
- package/dist/react/primitive-hooks/useActionBarCopy.js.map +1 -0
- package/dist/react/primitive-hooks/useActionBarEdit.d.ts +5 -0
- package/dist/react/primitive-hooks/useActionBarEdit.d.ts.map +1 -0
- package/dist/react/primitive-hooks/useActionBarEdit.js +11 -0
- package/dist/react/primitive-hooks/useActionBarEdit.js.map +1 -0
- package/dist/react/primitive-hooks/useActionBarFeedback.d.ts +9 -0
- package/dist/react/primitive-hooks/useActionBarFeedback.d.ts.map +1 -0
- package/dist/react/primitive-hooks/useActionBarFeedback.js +19 -0
- package/dist/react/primitive-hooks/useActionBarFeedback.js.map +1 -0
- package/dist/react/primitive-hooks/useActionBarReload.d.ts +5 -0
- package/dist/react/primitive-hooks/useActionBarReload.d.ts.map +1 -0
- package/dist/react/primitive-hooks/useActionBarReload.js +13 -0
- package/dist/react/primitive-hooks/useActionBarReload.js.map +1 -0
- package/dist/react/primitive-hooks/useComposerAddAttachment.d.ts +6 -0
- package/dist/react/primitive-hooks/useComposerAddAttachment.d.ts.map +1 -0
- package/dist/react/primitive-hooks/useComposerAddAttachment.js +11 -0
- package/dist/react/primitive-hooks/useComposerAddAttachment.js.map +1 -0
- package/dist/react/primitive-hooks/useComposerCancel.d.ts +5 -0
- package/dist/react/primitive-hooks/useComposerCancel.d.ts.map +1 -0
- package/dist/react/primitive-hooks/useComposerCancel.js +11 -0
- package/dist/react/primitive-hooks/useComposerCancel.js.map +1 -0
- package/dist/react/primitive-hooks/useComposerSend.d.ts +5 -0
- package/dist/react/primitive-hooks/useComposerSend.d.ts.map +1 -0
- package/dist/react/primitive-hooks/useComposerSend.js +11 -0
- package/dist/react/primitive-hooks/useComposerSend.js.map +1 -0
- package/dist/react/primitive-hooks/useEditComposerCancel.d.ts +4 -0
- package/dist/react/primitive-hooks/useEditComposerCancel.d.ts.map +1 -0
- package/dist/react/primitive-hooks/useEditComposerCancel.js +10 -0
- package/dist/react/primitive-hooks/useEditComposerCancel.js.map +1 -0
- package/dist/react/primitive-hooks/useEditComposerSend.d.ts +5 -0
- package/dist/react/primitive-hooks/useEditComposerSend.d.ts.map +1 -0
- package/dist/react/primitive-hooks/useEditComposerSend.js +11 -0
- package/dist/react/primitive-hooks/useEditComposerSend.js.map +1 -0
- package/dist/react/primitive-hooks/useMessageBranching.d.ts +7 -0
- package/dist/react/primitive-hooks/useMessageBranching.d.ts.map +1 -0
- package/dist/react/primitive-hooks/useMessageBranching.js +15 -0
- package/dist/react/primitive-hooks/useMessageBranching.js.map +1 -0
- package/dist/react/primitive-hooks/useMessageReload.d.ts +5 -0
- package/dist/react/primitive-hooks/useMessageReload.d.ts.map +1 -0
- package/dist/react/primitive-hooks/useMessageReload.js +11 -0
- package/dist/react/primitive-hooks/useMessageReload.js.map +1 -0
- package/dist/react/primitive-hooks/useThreadIsEmpty.d.ts +2 -0
- package/dist/react/primitive-hooks/useThreadIsEmpty.d.ts.map +1 -0
- package/dist/react/primitive-hooks/useThreadIsEmpty.js +5 -0
- package/dist/react/primitive-hooks/useThreadIsEmpty.js.map +1 -0
- package/dist/react/primitive-hooks/useThreadIsRunning.d.ts +2 -0
- package/dist/react/primitive-hooks/useThreadIsRunning.d.ts.map +1 -0
- package/dist/react/primitive-hooks/useThreadIsRunning.js +5 -0
- package/dist/react/primitive-hooks/useThreadIsRunning.js.map +1 -0
- package/dist/react/primitive-hooks/useThreadMessages.d.ts +3 -0
- package/dist/react/primitive-hooks/useThreadMessages.d.ts.map +1 -0
- package/dist/react/primitive-hooks/useThreadMessages.js +5 -0
- package/dist/react/primitive-hooks/useThreadMessages.js.map +1 -0
- package/dist/react/runtimes/RemoteThreadListHookInstanceManager.d.ts +97 -0
- package/dist/react/runtimes/RemoteThreadListHookInstanceManager.d.ts.map +1 -0
- package/dist/react/runtimes/RemoteThreadListHookInstanceManager.js +111 -0
- package/dist/react/runtimes/RemoteThreadListHookInstanceManager.js.map +1 -0
- package/dist/react/runtimes/RemoteThreadListThreadListRuntimeCore.d.ts +115 -0
- package/dist/react/runtimes/RemoteThreadListThreadListRuntimeCore.d.ts.map +1 -0
- package/dist/react/runtimes/RemoteThreadListThreadListRuntimeCore.js +444 -0
- package/dist/react/runtimes/RemoteThreadListThreadListRuntimeCore.js.map +1 -0
- package/dist/react/runtimes/RuntimeAdapterProvider.d.ts +18 -0
- package/dist/react/runtimes/RuntimeAdapterProvider.d.ts.map +1 -0
- package/dist/react/runtimes/RuntimeAdapterProvider.js +14 -0
- package/dist/react/runtimes/RuntimeAdapterProvider.js.map +1 -0
- package/dist/react/runtimes/cloud/AssistantCloudThreadHistoryAdapter.d.ts +5 -0
- package/dist/react/runtimes/cloud/AssistantCloudThreadHistoryAdapter.d.ts.map +1 -0
- package/dist/react/runtimes/cloud/AssistantCloudThreadHistoryAdapter.js +528 -0
- package/dist/react/runtimes/cloud/AssistantCloudThreadHistoryAdapter.js.map +1 -0
- package/dist/react/runtimes/cloud/CloudFileAttachmentAdapter.d.ts +15 -0
- package/dist/react/runtimes/cloud/CloudFileAttachmentAdapter.d.ts.map +1 -0
- package/dist/react/runtimes/cloud/CloudFileAttachmentAdapter.js +83 -0
- package/dist/react/runtimes/cloud/CloudFileAttachmentAdapter.js.map +1 -0
- package/dist/react/runtimes/cloud/auiV0.d.ts +62 -0
- package/dist/react/runtimes/cloud/auiV0.d.ts.map +1 -0
- package/dist/react/runtimes/cloud/auiV0.js +74 -0
- package/dist/react/runtimes/cloud/auiV0.js.map +1 -0
- package/dist/react/runtimes/cloud/index.d.ts +4 -0
- package/dist/react/runtimes/cloud/index.d.ts.map +1 -0
- package/dist/react/runtimes/cloud/index.js +4 -0
- package/dist/react/runtimes/cloud/index.js.map +1 -0
- package/dist/react/runtimes/cloud/useCloudThreadListAdapter.d.ts +13 -0
- package/dist/react/runtimes/cloud/useCloudThreadListAdapter.d.ts.map +1 -0
- package/dist/react/runtimes/cloud/useCloudThreadListAdapter.js +103 -0
- package/dist/react/runtimes/cloud/useCloudThreadListAdapter.js.map +1 -0
- package/dist/react/runtimes/createMessageConverter.d.ts +17 -0
- package/dist/react/runtimes/createMessageConverter.d.ts.map +1 -0
- package/dist/react/runtimes/createMessageConverter.js +50 -0
- package/dist/react/runtimes/createMessageConverter.js.map +1 -0
- package/dist/react/runtimes/external-message-converter.d.ts +34 -0
- package/dist/react/runtimes/external-message-converter.d.ts.map +1 -0
- package/dist/react/runtimes/external-message-converter.js +309 -0
- package/dist/react/runtimes/external-message-converter.js.map +1 -0
- package/dist/react/runtimes/index.d.ts +11 -0
- package/dist/react/runtimes/index.d.ts.map +1 -0
- package/dist/react/runtimes/index.js +11 -0
- package/dist/react/runtimes/index.js.map +1 -0
- package/dist/react/runtimes/useExternalStoreRuntime.d.ts +4 -0
- package/dist/react/runtimes/useExternalStoreRuntime.d.ts.map +1 -0
- package/dist/react/runtimes/useExternalStoreRuntime.js +19 -0
- package/dist/react/runtimes/useExternalStoreRuntime.js.map +1 -0
- package/dist/react/runtimes/useLocalRuntime.d.ts +28 -0
- package/dist/react/runtimes/useLocalRuntime.d.ts.map +1 -0
- package/dist/react/runtimes/useLocalRuntime.js +64 -0
- package/dist/react/runtimes/useLocalRuntime.js.map +1 -0
- package/dist/react/runtimes/useRemoteThreadListRuntime.d.ts +4 -0
- package/dist/react/runtimes/useRemoteThreadListRuntime.d.ts.map +1 -0
- package/dist/react/runtimes/useRemoteThreadListRuntime.js +48 -0
- package/dist/react/runtimes/useRemoteThreadListRuntime.js.map +1 -0
- package/dist/react/runtimes/useToolInvocations.d.ts +38 -0
- package/dist/react/runtimes/useToolInvocations.d.ts.map +1 -0
- package/dist/react/runtimes/useToolInvocations.js +411 -0
- package/dist/react/runtimes/useToolInvocations.js.map +1 -0
- package/dist/react/types/store-augmentation.d.ts +0 -1
- package/dist/react/types/store-augmentation.d.ts.map +1 -1
- package/dist/react/types/store-augmentation.js +1 -1
- package/dist/react/types/store-augmentation.js.map +1 -1
- package/dist/runtime/base/base-composer-runtime-core.d.ts.map +1 -1
- package/dist/runtime/base/base-composer-runtime-core.js +2 -0
- package/dist/runtime/base/base-composer-runtime-core.js.map +1 -1
- package/dist/store/index.d.ts +1 -1
- package/dist/store/index.d.ts.map +1 -1
- package/dist/store/index.js +1 -2
- package/dist/store/index.js.map +1 -1
- package/dist/utils/json/is-json-equal.d.ts +2 -0
- package/dist/utils/json/is-json-equal.d.ts.map +1 -0
- package/dist/utils/json/is-json-equal.js +31 -0
- package/dist/utils/json/is-json-equal.js.map +1 -0
- package/dist/utils/json/is-json.d.ts +6 -0
- package/dist/utils/json/is-json.d.ts.map +1 -0
- package/dist/utils/json/is-json.js +33 -0
- package/dist/utils/json/is-json.js.map +1 -0
- package/package.json +11 -10
- package/src/model-context/frame/provider.ts +2 -6
- package/src/react/AssistantRuntimeProvider.tsx +33 -0
- package/src/react/adapters/LocalStorageThreadListAdapter.tsx +227 -0
- package/src/react/adapters/TitleGenerationAdapter.ts +20 -0
- package/src/react/index.ts +17 -1
- package/src/react/primitive-hooks/index.ts +20 -0
- package/src/react/primitive-hooks/useActionBarCopy.ts +38 -0
- package/src/react/primitive-hooks/useActionBarEdit.ts +13 -0
- package/src/react/primitive-hooks/useActionBarFeedback.ts +28 -0
- package/src/react/primitive-hooks/useActionBarReload.ts +18 -0
- package/src/react/primitive-hooks/useComposerAddAttachment.ts +17 -0
- package/src/react/primitive-hooks/useComposerCancel.ts +13 -0
- package/src/react/primitive-hooks/useComposerSend.ts +15 -0
- package/src/react/primitive-hooks/useEditComposerCancel.ts +12 -0
- package/src/react/primitive-hooks/useEditComposerSend.ts +13 -0
- package/src/react/primitive-hooks/useMessageBranching.ts +18 -0
- package/src/react/primitive-hooks/useMessageReload.ts +13 -0
- package/src/react/primitive-hooks/useThreadIsEmpty.ts +5 -0
- package/src/react/primitive-hooks/useThreadIsRunning.ts +5 -0
- package/src/react/primitive-hooks/useThreadMessages.ts +6 -0
- package/src/react/runtimes/RemoteThreadListHookInstanceManager.tsx +176 -0
- package/src/react/runtimes/RemoteThreadListThreadListRuntimeCore.tsx +534 -0
- package/src/react/runtimes/RuntimeAdapterProvider.tsx +40 -0
- package/src/react/runtimes/cloud/AssistantCloudThreadHistoryAdapter.ts +785 -0
- package/src/react/runtimes/cloud/CloudFileAttachmentAdapter.ts +101 -0
- package/src/react/runtimes/cloud/auiV0.ts +160 -0
- package/src/react/runtimes/cloud/index.ts +3 -0
- package/src/react/runtimes/cloud/useCloudThreadListAdapter.tsx +154 -0
- package/src/react/runtimes/createMessageConverter.ts +77 -0
- package/src/react/runtimes/external-message-converter.ts +487 -0
- package/src/react/runtimes/index.ts +36 -0
- package/src/react/runtimes/useExternalStoreRuntime.ts +27 -0
- package/src/react/runtimes/useLocalRuntime.ts +101 -0
- package/src/react/runtimes/useRemoteThreadListRuntime.ts +76 -0
- package/src/react/runtimes/useToolInvocations.ts +594 -0
- package/src/react/types/store-augmentation.ts +0 -2
- package/src/runtime/base/base-composer-runtime-core.ts +2 -0
- package/src/store/index.ts +1 -2
- package/src/tests/no-unsafe-process-env.test.ts +58 -0
- package/src/utils/json/is-json-equal.ts +48 -0
- package/src/utils/json/is-json.ts +58 -0
|
@@ -0,0 +1,487 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { useMemo } from "react";
|
|
4
|
+
import { ThreadMessageConverter } from "../../runtimes/external-store/thread-message-converter";
|
|
5
|
+
import {
|
|
6
|
+
getExternalStoreMessages,
|
|
7
|
+
symbolInnerMessage,
|
|
8
|
+
bindExternalStoreMessage,
|
|
9
|
+
} from "../../runtime/utils/external-store-message";
|
|
10
|
+
import {
|
|
11
|
+
fromThreadMessageLike,
|
|
12
|
+
type ThreadMessageLike,
|
|
13
|
+
} from "../../runtime/utils/thread-message-like";
|
|
14
|
+
import { getAutoStatus, isAutoStatus } from "../../runtime/utils/auto-status";
|
|
15
|
+
import type { ToolExecutionStatus } from "./useToolInvocations";
|
|
16
|
+
import type { ReadonlyJSONValue } from "assistant-stream/utils";
|
|
17
|
+
import { generateErrorMessageId } from "../../utils/id";
|
|
18
|
+
import type {
|
|
19
|
+
MessageTiming,
|
|
20
|
+
ThreadAssistantMessage,
|
|
21
|
+
ThreadMessage,
|
|
22
|
+
ToolCallMessagePart,
|
|
23
|
+
} from "../../types";
|
|
24
|
+
|
|
25
|
+
export namespace useExternalMessageConverter {
|
|
26
|
+
export type Message =
|
|
27
|
+
| (ThreadMessageLike & {
|
|
28
|
+
readonly convertConfig?: {
|
|
29
|
+
readonly joinStrategy?: "concat-content" | "none";
|
|
30
|
+
};
|
|
31
|
+
})
|
|
32
|
+
| {
|
|
33
|
+
role: "tool";
|
|
34
|
+
toolCallId: string;
|
|
35
|
+
toolName?: string | undefined;
|
|
36
|
+
result: any;
|
|
37
|
+
artifact?: any;
|
|
38
|
+
isError?: boolean;
|
|
39
|
+
messages?: readonly ThreadMessage[];
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export type Metadata = {
|
|
43
|
+
readonly toolStatuses?: Record<string, ToolExecutionStatus>;
|
|
44
|
+
readonly error?: ReadonlyJSONValue;
|
|
45
|
+
readonly messageTiming?: Record<string, MessageTiming>;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export type Callback<T> = (
|
|
49
|
+
message: T,
|
|
50
|
+
metadata: Metadata,
|
|
51
|
+
) => Message | Message[];
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
type CallbackResult<T> = {
|
|
55
|
+
input: T;
|
|
56
|
+
outputs: useExternalMessageConverter.Message[];
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
type ChunkResult<T> = {
|
|
60
|
+
inputs: T[];
|
|
61
|
+
outputs: useExternalMessageConverter.Message[];
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
type Mutable<T> = {
|
|
65
|
+
-readonly [P in keyof T]: T[P];
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
const mergeInnerMessages = (existing: object, incoming: object) => ({
|
|
69
|
+
[symbolInnerMessage]: [
|
|
70
|
+
...((existing as any)[symbolInnerMessage] ?? []),
|
|
71
|
+
...((incoming as any)[symbolInnerMessage] ?? []),
|
|
72
|
+
],
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
const joinExternalMessages = (
|
|
76
|
+
messages: readonly useExternalMessageConverter.Message[],
|
|
77
|
+
): ThreadMessageLike => {
|
|
78
|
+
const assistantMessage: Mutable<Omit<ThreadMessageLike, "metadata">> & {
|
|
79
|
+
content: Exclude<ThreadMessageLike["content"][0], string>[];
|
|
80
|
+
metadata?: Mutable<ThreadMessageLike["metadata"]>;
|
|
81
|
+
} = {
|
|
82
|
+
role: "assistant",
|
|
83
|
+
content: [],
|
|
84
|
+
};
|
|
85
|
+
for (const output of messages) {
|
|
86
|
+
if (output.role === "tool") {
|
|
87
|
+
const toolCallIdx = assistantMessage.content.findIndex(
|
|
88
|
+
(c) => c.type === "tool-call" && c.toolCallId === output.toolCallId,
|
|
89
|
+
);
|
|
90
|
+
// Ignore orphaned tool results so one bad tool message does not
|
|
91
|
+
// prevent rendering the rest of the conversation.
|
|
92
|
+
if (toolCallIdx !== -1) {
|
|
93
|
+
const toolCall = assistantMessage.content[
|
|
94
|
+
toolCallIdx
|
|
95
|
+
]! as ToolCallMessagePart;
|
|
96
|
+
if (output.toolName !== undefined) {
|
|
97
|
+
if (toolCall.toolName !== output.toolName)
|
|
98
|
+
throw new Error(
|
|
99
|
+
`Tool call name ${output.toolCallId} ${output.toolName} does not match existing tool call ${toolCall.toolName}`,
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
assistantMessage.content[toolCallIdx] = {
|
|
103
|
+
...toolCall,
|
|
104
|
+
...{
|
|
105
|
+
[symbolInnerMessage]: [
|
|
106
|
+
...((toolCall as any)[symbolInnerMessage] ?? []),
|
|
107
|
+
output,
|
|
108
|
+
],
|
|
109
|
+
},
|
|
110
|
+
result: output.result,
|
|
111
|
+
artifact: output.artifact,
|
|
112
|
+
isError: output.isError,
|
|
113
|
+
messages: output.messages,
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
} else {
|
|
117
|
+
const role = output.role;
|
|
118
|
+
const content = (
|
|
119
|
+
typeof output.content === "string"
|
|
120
|
+
? [{ type: "text" as const, text: output.content }]
|
|
121
|
+
: output.content
|
|
122
|
+
).map((c) => ({
|
|
123
|
+
...c,
|
|
124
|
+
...{ [symbolInnerMessage]: [output] },
|
|
125
|
+
}));
|
|
126
|
+
switch (role) {
|
|
127
|
+
case "system":
|
|
128
|
+
case "user":
|
|
129
|
+
return {
|
|
130
|
+
...output,
|
|
131
|
+
content,
|
|
132
|
+
};
|
|
133
|
+
case "assistant":
|
|
134
|
+
if (assistantMessage.content.length === 0) {
|
|
135
|
+
assistantMessage.id = output.id;
|
|
136
|
+
assistantMessage.createdAt ??= output.createdAt;
|
|
137
|
+
assistantMessage.status ??= output.status;
|
|
138
|
+
|
|
139
|
+
if (output.attachments) {
|
|
140
|
+
assistantMessage.attachments = [
|
|
141
|
+
...(assistantMessage.attachments ?? []),
|
|
142
|
+
...output.attachments,
|
|
143
|
+
];
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if (output.metadata) {
|
|
147
|
+
assistantMessage.metadata ??= {};
|
|
148
|
+
if (output.metadata.unstable_state) {
|
|
149
|
+
assistantMessage.metadata.unstable_state =
|
|
150
|
+
output.metadata.unstable_state;
|
|
151
|
+
}
|
|
152
|
+
if (output.metadata.unstable_annotations) {
|
|
153
|
+
assistantMessage.metadata.unstable_annotations = [
|
|
154
|
+
...(assistantMessage.metadata.unstable_annotations ?? []),
|
|
155
|
+
...output.metadata.unstable_annotations,
|
|
156
|
+
];
|
|
157
|
+
}
|
|
158
|
+
if (output.metadata.unstable_data) {
|
|
159
|
+
assistantMessage.metadata.unstable_data = [
|
|
160
|
+
...(assistantMessage.metadata.unstable_data ?? []),
|
|
161
|
+
...output.metadata.unstable_data,
|
|
162
|
+
];
|
|
163
|
+
}
|
|
164
|
+
if (output.metadata.steps) {
|
|
165
|
+
assistantMessage.metadata.steps = [
|
|
166
|
+
...(assistantMessage.metadata.steps ?? []),
|
|
167
|
+
...output.metadata.steps,
|
|
168
|
+
];
|
|
169
|
+
}
|
|
170
|
+
if (output.metadata.custom) {
|
|
171
|
+
assistantMessage.metadata.custom = {
|
|
172
|
+
...(assistantMessage.metadata.custom ?? {}),
|
|
173
|
+
...output.metadata.custom,
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
if (output.metadata.timing) {
|
|
178
|
+
assistantMessage.metadata.timing = output.metadata.timing;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
if (output.metadata.submittedFeedback) {
|
|
182
|
+
assistantMessage.metadata.submittedFeedback =
|
|
183
|
+
output.metadata.submittedFeedback;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
// TODO keep this in sync
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// Add content parts, merging reasoning parts with same parentId
|
|
190
|
+
for (const part of content) {
|
|
191
|
+
if (part.type === "tool-call") {
|
|
192
|
+
const existingIdx = assistantMessage.content.findIndex(
|
|
193
|
+
(c) =>
|
|
194
|
+
c.type === "tool-call" && c.toolCallId === part.toolCallId,
|
|
195
|
+
);
|
|
196
|
+
if (existingIdx !== -1) {
|
|
197
|
+
const existing = assistantMessage.content[
|
|
198
|
+
existingIdx
|
|
199
|
+
] as typeof part;
|
|
200
|
+
assistantMessage.content[existingIdx] = {
|
|
201
|
+
...existing,
|
|
202
|
+
...part,
|
|
203
|
+
...mergeInnerMessages(existing, part),
|
|
204
|
+
};
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
if (
|
|
210
|
+
part.type === "reasoning" &&
|
|
211
|
+
"parentId" in part &&
|
|
212
|
+
part.parentId
|
|
213
|
+
) {
|
|
214
|
+
const existingIdx = assistantMessage.content.findIndex(
|
|
215
|
+
(c) =>
|
|
216
|
+
c.type === "reasoning" &&
|
|
217
|
+
"parentId" in c &&
|
|
218
|
+
c.parentId === part.parentId,
|
|
219
|
+
);
|
|
220
|
+
if (existingIdx !== -1) {
|
|
221
|
+
const existing = assistantMessage.content[
|
|
222
|
+
existingIdx
|
|
223
|
+
] as typeof part;
|
|
224
|
+
assistantMessage.content[existingIdx] = {
|
|
225
|
+
...existing,
|
|
226
|
+
text: `${existing.text}\n\n${part.text}`,
|
|
227
|
+
...mergeInnerMessages(existing, part),
|
|
228
|
+
};
|
|
229
|
+
continue;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
assistantMessage.content.push(part);
|
|
233
|
+
}
|
|
234
|
+
break;
|
|
235
|
+
default: {
|
|
236
|
+
const unsupportedRole: never = role;
|
|
237
|
+
throw new Error(`Unknown message role: ${unsupportedRole}`);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
return assistantMessage;
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
const chunkExternalMessages = <T>(
|
|
246
|
+
callbackResults: CallbackResult<T>[],
|
|
247
|
+
joinStrategy?: "concat-content" | "none",
|
|
248
|
+
) => {
|
|
249
|
+
const results: ChunkResult<T>[] = [];
|
|
250
|
+
let isAssistant = false;
|
|
251
|
+
let pendingNone = false; // true if the previous assistant message had joinStrategy "none"
|
|
252
|
+
let inputs: T[] = [];
|
|
253
|
+
let outputs: useExternalMessageConverter.Message[] = [];
|
|
254
|
+
|
|
255
|
+
const flush = () => {
|
|
256
|
+
if (outputs.length) {
|
|
257
|
+
results.push({
|
|
258
|
+
inputs,
|
|
259
|
+
outputs,
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
inputs = [];
|
|
263
|
+
outputs = [];
|
|
264
|
+
isAssistant = false;
|
|
265
|
+
pendingNone = false;
|
|
266
|
+
};
|
|
267
|
+
|
|
268
|
+
for (const callbackResult of callbackResults) {
|
|
269
|
+
for (const output of callbackResult.outputs) {
|
|
270
|
+
if (
|
|
271
|
+
(pendingNone && output.role !== "tool") ||
|
|
272
|
+
!isAssistant ||
|
|
273
|
+
output.role === "user" ||
|
|
274
|
+
output.role === "system"
|
|
275
|
+
) {
|
|
276
|
+
flush();
|
|
277
|
+
}
|
|
278
|
+
isAssistant = output.role === "assistant" || output.role === "tool";
|
|
279
|
+
|
|
280
|
+
if (inputs.at(-1) !== callbackResult.input) {
|
|
281
|
+
inputs.push(callbackResult.input);
|
|
282
|
+
}
|
|
283
|
+
outputs.push(output);
|
|
284
|
+
|
|
285
|
+
if (
|
|
286
|
+
output.role === "assistant" &&
|
|
287
|
+
(output.convertConfig?.joinStrategy === "none" ||
|
|
288
|
+
joinStrategy === "none")
|
|
289
|
+
) {
|
|
290
|
+
pendingNone = true;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
flush();
|
|
295
|
+
return results;
|
|
296
|
+
};
|
|
297
|
+
|
|
298
|
+
function createErrorAssistantMessage(
|
|
299
|
+
error: ReadonlyJSONValue,
|
|
300
|
+
): ThreadAssistantMessage {
|
|
301
|
+
const msg: ThreadAssistantMessage = {
|
|
302
|
+
id: generateErrorMessageId(),
|
|
303
|
+
role: "assistant",
|
|
304
|
+
content: [],
|
|
305
|
+
status: { type: "incomplete", reason: "error", error },
|
|
306
|
+
createdAt: new Date(),
|
|
307
|
+
metadata: {
|
|
308
|
+
unstable_state: null,
|
|
309
|
+
unstable_annotations: [],
|
|
310
|
+
unstable_data: [],
|
|
311
|
+
custom: {},
|
|
312
|
+
steps: [],
|
|
313
|
+
},
|
|
314
|
+
};
|
|
315
|
+
bindExternalStoreMessage(msg, []);
|
|
316
|
+
return msg;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
export const convertExternalMessages = <T extends WeakKey>(
|
|
320
|
+
messages: T[],
|
|
321
|
+
callback: useExternalMessageConverter.Callback<T>,
|
|
322
|
+
isRunning: boolean,
|
|
323
|
+
metadata: useExternalMessageConverter.Metadata,
|
|
324
|
+
) => {
|
|
325
|
+
const callbackResults: CallbackResult<T>[] = [];
|
|
326
|
+
for (const message of messages) {
|
|
327
|
+
const output = callback(message, metadata);
|
|
328
|
+
const outputs = Array.isArray(output) ? output : [output];
|
|
329
|
+
const result = { input: message, outputs };
|
|
330
|
+
callbackResults.push(result);
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
const chunks = chunkExternalMessages(callbackResults);
|
|
334
|
+
|
|
335
|
+
const result = chunks.map((message, idx) => {
|
|
336
|
+
const isLast = idx === chunks.length - 1;
|
|
337
|
+
const joined = joinExternalMessages(message.outputs);
|
|
338
|
+
const hasPendingToolCalls =
|
|
339
|
+
typeof joined.content === "object" &&
|
|
340
|
+
joined.content.some(
|
|
341
|
+
(c) => c.type === "tool-call" && c.result === undefined,
|
|
342
|
+
);
|
|
343
|
+
const autoStatus = getAutoStatus(
|
|
344
|
+
isLast,
|
|
345
|
+
isRunning,
|
|
346
|
+
hasPendingToolCalls,
|
|
347
|
+
hasPendingToolCalls,
|
|
348
|
+
isLast ? metadata.error : undefined,
|
|
349
|
+
);
|
|
350
|
+
const newMessage = fromThreadMessageLike(
|
|
351
|
+
joined,
|
|
352
|
+
idx.toString(),
|
|
353
|
+
autoStatus,
|
|
354
|
+
);
|
|
355
|
+
bindExternalStoreMessage(newMessage, message.inputs);
|
|
356
|
+
return newMessage;
|
|
357
|
+
});
|
|
358
|
+
|
|
359
|
+
if (metadata.error) {
|
|
360
|
+
const lastMessage = result.at(-1);
|
|
361
|
+
if (!lastMessage || lastMessage.role !== "assistant") {
|
|
362
|
+
result.push(createErrorAssistantMessage(metadata.error));
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
return result;
|
|
367
|
+
};
|
|
368
|
+
|
|
369
|
+
export const useExternalMessageConverter = <T extends WeakKey>({
|
|
370
|
+
callback,
|
|
371
|
+
messages,
|
|
372
|
+
isRunning,
|
|
373
|
+
joinStrategy,
|
|
374
|
+
metadata,
|
|
375
|
+
}: {
|
|
376
|
+
callback: useExternalMessageConverter.Callback<T>;
|
|
377
|
+
messages: T[];
|
|
378
|
+
isRunning: boolean;
|
|
379
|
+
joinStrategy?: "concat-content" | "none" | undefined;
|
|
380
|
+
metadata?: useExternalMessageConverter.Metadata | undefined;
|
|
381
|
+
}) => {
|
|
382
|
+
const state = useMemo(
|
|
383
|
+
() => ({
|
|
384
|
+
metadata: metadata ?? {},
|
|
385
|
+
callback,
|
|
386
|
+
callbackCache: new WeakMap<T, CallbackResult<T>>(),
|
|
387
|
+
chunkCache: new WeakMap<
|
|
388
|
+
useExternalMessageConverter.Message,
|
|
389
|
+
ChunkResult<T>
|
|
390
|
+
>(),
|
|
391
|
+
converterCache: new ThreadMessageConverter(),
|
|
392
|
+
}),
|
|
393
|
+
[callback, metadata],
|
|
394
|
+
);
|
|
395
|
+
|
|
396
|
+
return useMemo(() => {
|
|
397
|
+
const callbackResults: CallbackResult<T>[] = [];
|
|
398
|
+
for (const message of messages) {
|
|
399
|
+
let result = state.callbackCache.get(message);
|
|
400
|
+
if (!result) {
|
|
401
|
+
const output = state.callback(message, state.metadata);
|
|
402
|
+
const outputs = Array.isArray(output) ? output : [output];
|
|
403
|
+
result = { input: message, outputs };
|
|
404
|
+
state.callbackCache.set(message, result);
|
|
405
|
+
}
|
|
406
|
+
callbackResults.push(result);
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
const chunks = chunkExternalMessages(callbackResults, joinStrategy).map(
|
|
410
|
+
(m) => {
|
|
411
|
+
const key = m.outputs[0];
|
|
412
|
+
if (!key) return m;
|
|
413
|
+
|
|
414
|
+
const cached = state.chunkCache.get(key);
|
|
415
|
+
if (cached && shallowArrayEqual(cached.outputs, m.outputs))
|
|
416
|
+
return cached;
|
|
417
|
+
state.chunkCache.set(key, m);
|
|
418
|
+
return m;
|
|
419
|
+
},
|
|
420
|
+
);
|
|
421
|
+
|
|
422
|
+
const threadMessages = state.converterCache.convertMessages(
|
|
423
|
+
chunks,
|
|
424
|
+
(cache, message, idx) => {
|
|
425
|
+
const isLast = idx === chunks.length - 1;
|
|
426
|
+
|
|
427
|
+
const joined = joinExternalMessages(message.outputs);
|
|
428
|
+
const hasSuspendedToolCalls =
|
|
429
|
+
typeof joined.content === "object" &&
|
|
430
|
+
joined.content.some(
|
|
431
|
+
(c) => c.type === "tool-call" && c.result === undefined,
|
|
432
|
+
);
|
|
433
|
+
const hasPendingToolCalls =
|
|
434
|
+
typeof joined.content === "object" &&
|
|
435
|
+
joined.content.some(
|
|
436
|
+
(c) => c.type === "tool-call" && c.result === undefined,
|
|
437
|
+
);
|
|
438
|
+
const autoStatus = getAutoStatus(
|
|
439
|
+
isLast,
|
|
440
|
+
isRunning,
|
|
441
|
+
hasSuspendedToolCalls,
|
|
442
|
+
hasPendingToolCalls,
|
|
443
|
+
isLast ? state.metadata.error : undefined,
|
|
444
|
+
);
|
|
445
|
+
|
|
446
|
+
if (
|
|
447
|
+
cache &&
|
|
448
|
+
(cache.role !== "assistant" ||
|
|
449
|
+
!isAutoStatus(cache.status) ||
|
|
450
|
+
cache.status === autoStatus)
|
|
451
|
+
) {
|
|
452
|
+
const inputs = getExternalStoreMessages<T>(cache);
|
|
453
|
+
if (shallowArrayEqual(inputs, message.inputs)) {
|
|
454
|
+
return cache;
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
const newMessage = fromThreadMessageLike(
|
|
459
|
+
joined,
|
|
460
|
+
idx.toString(),
|
|
461
|
+
autoStatus,
|
|
462
|
+
);
|
|
463
|
+
bindExternalStoreMessage(newMessage, message.inputs);
|
|
464
|
+
return newMessage;
|
|
465
|
+
},
|
|
466
|
+
);
|
|
467
|
+
|
|
468
|
+
bindExternalStoreMessage(threadMessages, messages);
|
|
469
|
+
|
|
470
|
+
if (state.metadata.error) {
|
|
471
|
+
const lastMessage = threadMessages.at(-1);
|
|
472
|
+
if (!lastMessage || lastMessage.role !== "assistant") {
|
|
473
|
+
threadMessages.push(createErrorAssistantMessage(state.metadata.error));
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
return threadMessages;
|
|
478
|
+
}, [state, messages, isRunning, joinStrategy]);
|
|
479
|
+
};
|
|
480
|
+
|
|
481
|
+
const shallowArrayEqual = (a: unknown[], b: unknown[]) => {
|
|
482
|
+
if (a.length !== b.length) return false;
|
|
483
|
+
for (let i = 0; i < a.length; i++) {
|
|
484
|
+
if (a[i] !== b[i]) return false;
|
|
485
|
+
}
|
|
486
|
+
return true;
|
|
487
|
+
};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
export {
|
|
2
|
+
RuntimeAdapterProvider,
|
|
3
|
+
useRuntimeAdapters,
|
|
4
|
+
type RuntimeAdapters,
|
|
5
|
+
} from "./RuntimeAdapterProvider";
|
|
6
|
+
|
|
7
|
+
export {
|
|
8
|
+
useToolInvocations,
|
|
9
|
+
type ToolExecutionStatus,
|
|
10
|
+
type AssistantTransportState,
|
|
11
|
+
type AddToolResultCommand,
|
|
12
|
+
} from "./useToolInvocations";
|
|
13
|
+
|
|
14
|
+
export { useExternalStoreRuntime } from "./useExternalStoreRuntime";
|
|
15
|
+
|
|
16
|
+
export {
|
|
17
|
+
useExternalMessageConverter,
|
|
18
|
+
convertExternalMessages,
|
|
19
|
+
} from "./external-message-converter";
|
|
20
|
+
export { createMessageConverter } from "./createMessageConverter";
|
|
21
|
+
|
|
22
|
+
export { RemoteThreadListHookInstanceManager } from "./RemoteThreadListHookInstanceManager";
|
|
23
|
+
export { RemoteThreadListThreadListRuntimeCore } from "./RemoteThreadListThreadListRuntimeCore";
|
|
24
|
+
export { useRemoteThreadListRuntime } from "./useRemoteThreadListRuntime";
|
|
25
|
+
|
|
26
|
+
export {
|
|
27
|
+
useCloudThreadListAdapter,
|
|
28
|
+
useAssistantCloudThreadHistoryAdapter,
|
|
29
|
+
CloudFileAttachmentAdapter,
|
|
30
|
+
} from "./cloud";
|
|
31
|
+
|
|
32
|
+
export {
|
|
33
|
+
useLocalRuntime,
|
|
34
|
+
splitLocalRuntimeOptions,
|
|
35
|
+
type LocalRuntimeOptions,
|
|
36
|
+
} from "./useLocalRuntime";
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { useEffect, useMemo, useState } from "react";
|
|
4
|
+
import { ExternalStoreRuntimeCore } from "../../runtimes/internal";
|
|
5
|
+
import type { ExternalStoreAdapter } from "../../runtimes/external-store/external-store-adapter";
|
|
6
|
+
import type { AssistantRuntime } from "../../runtime/api/assistant-runtime";
|
|
7
|
+
import { AssistantRuntimeImpl } from "../../runtime/internal";
|
|
8
|
+
import { useRuntimeAdapters } from "./RuntimeAdapterProvider";
|
|
9
|
+
|
|
10
|
+
export const useExternalStoreRuntime = <T>(
|
|
11
|
+
store: ExternalStoreAdapter<T>,
|
|
12
|
+
): AssistantRuntime => {
|
|
13
|
+
const [runtime] = useState(() => new ExternalStoreRuntimeCore(store));
|
|
14
|
+
|
|
15
|
+
useEffect(() => {
|
|
16
|
+
runtime.setAdapter(store);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
const { modelContext } = useRuntimeAdapters() ?? {};
|
|
20
|
+
|
|
21
|
+
useEffect(() => {
|
|
22
|
+
if (!modelContext) return undefined;
|
|
23
|
+
return runtime.registerModelContextProvider(modelContext);
|
|
24
|
+
}, [modelContext, runtime]);
|
|
25
|
+
|
|
26
|
+
return useMemo(() => new AssistantRuntimeImpl(runtime), [runtime]);
|
|
27
|
+
};
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { useEffect, useMemo, useRef, useState } from "react";
|
|
2
|
+
import type {
|
|
3
|
+
AssistantRuntime,
|
|
4
|
+
ChatModelAdapter,
|
|
5
|
+
ThreadMessageLike,
|
|
6
|
+
} from "../../index";
|
|
7
|
+
import type { LocalRuntimeOptionsBase } from "../../runtimes/local/local-runtime-options";
|
|
8
|
+
import { AssistantRuntimeImpl, LocalRuntimeCore } from "../../internal";
|
|
9
|
+
import { useAuiState } from "@assistant-ui/store";
|
|
10
|
+
import { useRemoteThreadListRuntime } from "./useRemoteThreadListRuntime";
|
|
11
|
+
import { useCloudThreadListAdapter } from "./cloud";
|
|
12
|
+
import { useRuntimeAdapters } from "./RuntimeAdapterProvider";
|
|
13
|
+
import type { AssistantCloud } from "assistant-cloud";
|
|
14
|
+
|
|
15
|
+
export type LocalRuntimeOptions = Omit<LocalRuntimeOptionsBase, "adapters"> & {
|
|
16
|
+
cloud?: AssistantCloud | undefined;
|
|
17
|
+
initialMessages?: readonly ThreadMessageLike[] | undefined;
|
|
18
|
+
adapters?: Omit<LocalRuntimeOptionsBase["adapters"], "chatModel"> | undefined;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const useLocalThreadRuntime = (
|
|
22
|
+
chatModel: ChatModelAdapter,
|
|
23
|
+
{ initialMessages, ...options }: LocalRuntimeOptions,
|
|
24
|
+
): AssistantRuntime => {
|
|
25
|
+
const { modelContext, ...threadListAdapters } = useRuntimeAdapters() ?? {};
|
|
26
|
+
const opt = {
|
|
27
|
+
...options,
|
|
28
|
+
adapters: {
|
|
29
|
+
...threadListAdapters,
|
|
30
|
+
...options.adapters,
|
|
31
|
+
chatModel,
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const [runtime] = useState(() => new LocalRuntimeCore(opt, initialMessages));
|
|
36
|
+
|
|
37
|
+
const threadIdRef = useRef<string | undefined>(undefined);
|
|
38
|
+
threadIdRef.current = useAuiState((s) => s.threadListItem.remoteId);
|
|
39
|
+
|
|
40
|
+
useEffect(() => {
|
|
41
|
+
runtime.threads
|
|
42
|
+
.getMainThreadRuntimeCore()
|
|
43
|
+
.__internal_setGetThreadId(() => threadIdRef.current);
|
|
44
|
+
}, [runtime]);
|
|
45
|
+
|
|
46
|
+
useEffect(() => {
|
|
47
|
+
return () => {
|
|
48
|
+
runtime.threads.getMainThreadRuntimeCore().detach();
|
|
49
|
+
};
|
|
50
|
+
}, [runtime]);
|
|
51
|
+
|
|
52
|
+
useEffect(() => {
|
|
53
|
+
runtime.threads.getMainThreadRuntimeCore().__internal_setOptions(opt);
|
|
54
|
+
runtime.threads.getMainThreadRuntimeCore().__internal_load();
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
useEffect(() => {
|
|
58
|
+
if (!modelContext) return undefined;
|
|
59
|
+
return runtime.registerModelContextProvider(modelContext);
|
|
60
|
+
}, [modelContext, runtime]);
|
|
61
|
+
|
|
62
|
+
return useMemo(() => new AssistantRuntimeImpl(runtime), [runtime]);
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
export const splitLocalRuntimeOptions = <T extends LocalRuntimeOptions>(
|
|
66
|
+
options: T,
|
|
67
|
+
) => {
|
|
68
|
+
const {
|
|
69
|
+
cloud,
|
|
70
|
+
initialMessages,
|
|
71
|
+
maxSteps,
|
|
72
|
+
adapters,
|
|
73
|
+
unstable_humanToolNames,
|
|
74
|
+
...rest
|
|
75
|
+
} = options;
|
|
76
|
+
|
|
77
|
+
return {
|
|
78
|
+
localRuntimeOptions: {
|
|
79
|
+
cloud,
|
|
80
|
+
initialMessages,
|
|
81
|
+
maxSteps,
|
|
82
|
+
adapters,
|
|
83
|
+
unstable_humanToolNames,
|
|
84
|
+
},
|
|
85
|
+
otherOptions: rest,
|
|
86
|
+
};
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
export const useLocalRuntime = (
|
|
90
|
+
chatModel: ChatModelAdapter,
|
|
91
|
+
{ cloud, ...options }: LocalRuntimeOptions = {},
|
|
92
|
+
): AssistantRuntime => {
|
|
93
|
+
const cloudAdapter = useCloudThreadListAdapter({ cloud });
|
|
94
|
+
return useRemoteThreadListRuntime({
|
|
95
|
+
runtimeHook: function RuntimeHook() {
|
|
96
|
+
return useLocalThreadRuntime(chatModel, options);
|
|
97
|
+
},
|
|
98
|
+
adapter: cloudAdapter,
|
|
99
|
+
allowNesting: true,
|
|
100
|
+
});
|
|
101
|
+
};
|