@ulearning/ai-sdk-adapter 0.0.0-stage → 1.0.0-next.0
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/LICENSE +21 -0
- package/README.md +29 -2
- package/dist/adapters/aiSDKFormatAdapter.d.ts +6 -0
- package/dist/adapters/aiSDKFormatAdapter.d.ts.map +1 -0
- package/dist/adapters/aiSDKFormatAdapter.js +17 -0
- package/dist/adapters/aiSDKFormatAdapter.js.map +1 -0
- package/dist/adapters/vercelAttachmentAdapter.d.ts +3 -0
- package/dist/adapters/vercelAttachmentAdapter.d.ts.map +1 -0
- package/dist/adapters/vercelAttachmentAdapter.js +36 -0
- package/dist/adapters/vercelAttachmentAdapter.js.map +1 -0
- package/dist/aiSDKExtras.d.ts +13 -0
- package/dist/aiSDKExtras.d.ts.map +1 -0
- package/dist/aiSDKExtras.js +6 -0
- package/dist/aiSDKExtras.js.map +1 -0
- package/dist/converters/convertMessage.d.ts +44 -0
- package/dist/converters/convertMessage.d.ts.map +1 -0
- package/dist/converters/convertMessage.js +465 -0
- package/dist/converters/convertMessage.js.map +1 -0
- package/dist/converters/toCreateMessage.d.ts +4 -0
- package/dist/converters/toCreateMessage.d.ts.map +1 -0
- package/dist/converters/toCreateMessage.js +65 -0
- package/dist/converters/toCreateMessage.js.map +1 -0
- package/dist/converters/toolApprovalAnswers.d.ts +3 -0
- package/dist/converters/toolApprovalAnswers.d.ts.map +1 -0
- package/dist/converters/toolApprovalAnswers.js +16 -0
- package/dist/converters/toolApprovalAnswers.js.map +1 -0
- package/dist/hooks.d.ts +17 -0
- package/dist/hooks.d.ts.map +1 -0
- package/dist/hooks.js +19 -0
- package/dist/hooks.js.map +1 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +13 -0
- package/dist/model-context/injectInteractableContext.d.ts +37 -0
- package/dist/model-context/injectInteractableContext.d.ts.map +1 -0
- package/dist/model-context/injectInteractableContext.js +54 -0
- package/dist/model-context/injectInteractableContext.js.map +1 -0
- package/dist/model-context/injectQuoteContext.d.ts +25 -0
- package/dist/model-context/injectQuoteContext.d.ts.map +1 -0
- package/dist/model-context/injectQuoteContext.js +51 -0
- package/dist/model-context/injectQuoteContext.js.map +1 -0
- package/dist/runtime/AISDKChat.d.ts +13 -0
- package/dist/runtime/AISDKChat.d.ts.map +1 -0
- package/dist/runtime/AISDKChat.js +34 -0
- package/dist/runtime/AISDKChat.js.map +1 -0
- package/dist/runtime/AISDKThreads.d.ts +24 -0
- package/dist/runtime/AISDKThreads.d.ts.map +1 -0
- package/dist/runtime/AISDKThreads.js +92 -0
- package/dist/runtime/AISDKThreads.js.map +1 -0
- package/dist/runtime/DynamicChatTransport.d.ts +50 -0
- package/dist/runtime/DynamicChatTransport.d.ts.map +1 -0
- package/dist/runtime/DynamicChatTransport.js +153 -0
- package/dist/runtime/DynamicChatTransport.js.map +1 -0
- package/dist/runtime/getResumableAdapter.d.ts +5 -0
- package/dist/runtime/getResumableAdapter.d.ts.map +1 -0
- package/dist/runtime/getResumableAdapter.js +11 -0
- package/dist/runtime/getResumableAdapter.js.map +1 -0
- package/dist/runtime/toolHistoryCodec.d.ts +20 -0
- package/dist/runtime/toolHistoryCodec.d.ts.map +1 -0
- package/dist/runtime/toolHistoryCodec.js +122 -0
- package/dist/runtime/toolHistoryCodec.js.map +1 -0
- package/dist/runtime/useAISDKRuntime.d.ts +95 -0
- package/dist/runtime/useAISDKRuntime.d.ts.map +1 -0
- package/dist/runtime/useAISDKRuntime.js +754 -0
- package/dist/runtime/useAISDKRuntime.js.map +1 -0
- package/dist/runtime/useChatRuntime.d.ts +17 -0
- package/dist/runtime/useChatRuntime.d.ts.map +1 -0
- package/dist/runtime/useChatRuntime.js +50 -0
- package/dist/runtime/useChatRuntime.js.map +1 -0
- package/dist/runtime/useChatSnapshot.d.ts +6 -0
- package/dist/runtime/useChatSnapshot.d.ts.map +1 -0
- package/dist/runtime/useChatSnapshot.js +62 -0
- package/dist/runtime/useChatSnapshot.js.map +1 -0
- package/dist/runtime/useChatThread.d.ts +121 -0
- package/dist/runtime/useChatThread.d.ts.map +1 -0
- package/dist/runtime/useChatThread.js +255 -0
- package/dist/runtime/useChatThread.js.map +1 -0
- package/dist/runtime/useDynamicChatTransport.d.ts +4 -0
- package/dist/runtime/useDynamicChatTransport.d.ts.map +1 -0
- package/dist/runtime/useDynamicChatTransport.js +26 -0
- package/dist/runtime/useDynamicChatTransport.js.map +1 -0
- package/dist/runtime/useExternalHistory.d.ts +12 -0
- package/dist/runtime/useExternalHistory.d.ts.map +1 -0
- package/dist/runtime/useExternalHistory.js +556 -0
- package/dist/runtime/useExternalHistory.js.map +1 -0
- package/dist/runtime/useMessageQueue.d.ts +14 -0
- package/dist/runtime/useMessageQueue.d.ts.map +1 -0
- package/dist/runtime/useMessageQueue.js +142 -0
- package/dist/runtime/useMessageQueue.js.map +1 -0
- package/dist/runtime/useResourceCleanup.d.ts +2 -0
- package/dist/runtime/useResourceCleanup.d.ts.map +1 -0
- package/dist/runtime/useResourceCleanup.js +47 -0
- package/dist/runtime/useResourceCleanup.js.map +1 -0
- package/dist/runtime/useStreamingTiming.d.ts +12 -0
- package/dist/runtime/useStreamingTiming.d.ts.map +1 -0
- package/dist/runtime/useStreamingTiming.js +36 -0
- package/dist/runtime/useStreamingTiming.js.map +1 -0
- package/dist/tools/generativeTools.d.ts +71 -0
- package/dist/tools/generativeTools.d.ts.map +1 -0
- package/dist/tools/generativeTools.js +258 -0
- package/dist/tools/generativeTools.js.map +1 -0
- package/dist/tools/mcp-stdio.node.d.ts +2 -0
- package/dist/tools/mcp-stdio.node.d.ts.map +1 -0
- package/dist/tools/mcp-stdio.node.js +1 -0
- package/dist/tools/mcp-stdio.unsupported.d.ts +3 -0
- package/dist/tools/mcp-stdio.unsupported.d.ts.map +1 -0
- package/dist/tools/mcp-stdio.unsupported.js +10 -0
- package/dist/tools/mcp-stdio.unsupported.js.map +1 -0
- package/dist/transport/AssistantChatTransport.d.ts +20 -0
- package/dist/transport/AssistantChatTransport.d.ts.map +1 -0
- package/dist/transport/AssistantChatTransport.js +151 -0
- package/dist/transport/AssistantChatTransport.js.map +1 -0
- package/dist/transport/resumable.d.ts +36 -0
- package/dist/transport/resumable.d.ts.map +1 -0
- package/dist/transport/resumable.js +112 -0
- package/dist/transport/resumable.js.map +1 -0
- package/dist/usage.d.ts +24 -0
- package/dist/usage.d.ts.map +1 -0
- package/dist/usage.js +121 -0
- package/dist/usage.js.map +1 -0
- package/dist/utils/getVercelAIMessages.d.ts +4 -0
- package/dist/utils/getVercelAIMessages.d.ts.map +1 -0
- package/dist/utils/getVercelAIMessages.js +8 -0
- package/dist/utils/getVercelAIMessages.js.map +1 -0
- package/dist/utils/sliceMessagesUntil.d.ts +3 -0
- package/dist/utils/sliceMessagesUntil.d.ts.map +1 -0
- package/dist/utils/sliceMessagesUntil.js +10 -0
- package/dist/utils/sliceMessagesUntil.js.map +1 -0
- package/package.json +96 -3
- package/src/__tests__/standalone.e2e.ts +118 -0
- package/src/adapters/aiSDKFormatAdapter.test.ts +36 -0
- package/src/adapters/aiSDKFormatAdapter.ts +18 -0
- package/src/adapters/vercelAttachmentAdapter.test.ts +52 -0
- package/src/adapters/vercelAttachmentAdapter.ts +36 -0
- package/src/aiSDKExtras.ts +31 -0
- package/src/converters/convertMessage.metadata-key.test.tsx +182 -0
- package/src/converters/convertMessage.test.ts +2428 -0
- package/src/converters/convertMessage.tool-args-status.test.tsx +185 -0
- package/src/converters/convertMessage.ts +919 -0
- package/src/converters/toCreateMessage.test.ts +716 -0
- package/src/converters/toCreateMessage.ts +110 -0
- package/src/converters/toolApprovalAnswers.ts +27 -0
- package/src/hooks.test.ts +168 -0
- package/src/hooks.ts +26 -0
- package/src/index.ts +29 -0
- package/src/model-context/injectInteractableContext.test.ts +167 -0
- package/src/model-context/injectInteractableContext.ts +64 -0
- package/src/model-context/injectQuoteContext.test.ts +33 -0
- package/src/model-context/injectQuoteContext.ts +63 -0
- package/src/runtime/AISDKChat.artifacts.test.ts +48 -0
- package/src/runtime/AISDKChat.integration.test.tsx +148 -0
- package/src/runtime/AISDKChat.test.ts +241 -0
- package/src/runtime/AISDKChat.ts +43 -0
- package/src/runtime/AISDKThreads.test.ts +811 -0
- package/src/runtime/AISDKThreads.ts +194 -0
- package/src/runtime/DynamicChatTransport.test.ts +228 -0
- package/src/runtime/DynamicChatTransport.ts +281 -0
- package/src/runtime/__tests__/controlled-transport.ts +70 -0
- package/src/runtime/__tests__/tap.ts +80 -0
- package/src/runtime/__tests__/vue.ts +46 -0
- package/src/runtime/getResumableAdapter.ts +16 -0
- package/src/runtime/toolHistoryCodec.test.ts +187 -0
- package/src/runtime/toolHistoryCodec.ts +245 -0
- package/src/runtime/useAISDKRuntime.approval.integration.test.tsx +1956 -0
- package/src/runtime/useAISDKRuntime.approval.test.tsx +580 -0
- package/src/runtime/useAISDKRuntime.denied-tool.test.tsx +122 -0
- package/src/runtime/useAISDKRuntime.fast-refresh.test.tsx +302 -0
- package/src/runtime/useAISDKRuntime.metadata-cache.test.tsx +157 -0
- package/src/runtime/useAISDKRuntime.queue.test.tsx +707 -0
- package/src/runtime/useAISDKRuntime.reload.test.tsx +217 -0
- package/src/runtime/useAISDKRuntime.test.ts +2345 -0
- package/src/runtime/useAISDKRuntime.tool-output.test.tsx +113 -0
- package/src/runtime/useAISDKRuntime.ts +1503 -0
- package/src/runtime/useAISDKRuntime.voice.test.tsx +319 -0
- package/src/runtime/useChatOnTap.test.ts +60 -0
- package/src/runtime/useChatRuntime.fast-refresh.test.tsx +83 -0
- package/src/runtime/useChatRuntime.integration.test.tsx +661 -0
- package/src/runtime/useChatRuntime.local-storage.test.tsx +114 -0
- package/src/runtime/useChatRuntime.native.test.ts +147 -0
- package/src/runtime/useChatRuntime.run-telemetry.test.tsx +226 -0
- package/src/runtime/useChatRuntime.test.ts +619 -0
- package/src/runtime/useChatRuntime.ts +80 -0
- package/src/runtime/useChatSnapshot.test.ts +60 -0
- package/src/runtime/useChatSnapshot.ts +86 -0
- package/src/runtime/useChatThread.binding.test.tsx +140 -0
- package/src/runtime/useChatThread.test.ts +993 -0
- package/src/runtime/useChatThread.transport.test.tsx +149 -0
- package/src/runtime/useChatThread.ts +533 -0
- package/src/runtime/useDynamicChatTransport.ts +26 -0
- package/src/runtime/useExternalHistory.test.ts +4187 -0
- package/src/runtime/useExternalHistory.ts +902 -0
- package/src/runtime/useMessageQueue.effects.test.tsx +88 -0
- package/src/runtime/useMessageQueue.test.tsx +256 -0
- package/src/runtime/useMessageQueue.ts +189 -0
- package/src/runtime/useResourceCleanup.ts +58 -0
- package/src/runtime/useStreamingTiming.test.tsx +96 -0
- package/src/runtime/useStreamingTiming.ts +69 -0
- package/src/tools/generativeTools.test.ts +1058 -0
- package/src/tools/generativeTools.ts +491 -0
- package/src/tools/mcp-stdio.node.ts +1 -0
- package/src/tools/mcp-stdio.unsupported.ts +12 -0
- package/src/transport/AssistantChatTransport.test.ts +631 -0
- package/src/transport/AssistantChatTransport.ts +236 -0
- package/src/transport/resumable.test.ts +262 -0
- package/src/transport/resumable.ts +162 -0
- package/src/usage.test.ts +204 -0
- package/src/usage.ts +179 -0
- package/src/utils/getVercelAIMessages.ts +11 -0
- package/src/utils/sliceMessagesUntil.test.ts +51 -0
- package/src/utils/sliceMessagesUntil.ts +16 -0
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { flushTapSync } from "@ulearning/ai-tap";
|
|
3
|
+
import { AuiConfig, createAssistantClient } from "@ulearning/ai-store/client";
|
|
4
|
+
import { AISDKChat } from "./AISDKChat";
|
|
5
|
+
import { createControlledTransport } from "./__tests__/controlled-transport";
|
|
6
|
+
|
|
7
|
+
// Runs the streaming round trip against the compiled dist entry (aliased in
|
|
8
|
+
// vitest.artifacts.config.ts), exercising the public runtime artifact.
|
|
9
|
+
describe("AISDKChat compiled output", () => {
|
|
10
|
+
it("streams a chat round trip through the compiled entry", async () => {
|
|
11
|
+
const { transport, emit, close } = createControlledTransport();
|
|
12
|
+
const handle = createAssistantClient(
|
|
13
|
+
AuiConfig({ threads: AISDKChat({ transport }) }),
|
|
14
|
+
);
|
|
15
|
+
try {
|
|
16
|
+
handle.subscribe(() => {});
|
|
17
|
+
const aui = handle.getClient();
|
|
18
|
+
|
|
19
|
+
flushTapSync(() => aui.composer.setText("hi"));
|
|
20
|
+
flushTapSync(() => aui.composer.send());
|
|
21
|
+
|
|
22
|
+
await vi.waitFor(() => {
|
|
23
|
+
expect(aui.thread.getState().isRunning).toBe(true);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
emit(
|
|
27
|
+
{ type: "start" },
|
|
28
|
+
{ type: "text-start", id: "t1" },
|
|
29
|
+
{ type: "text-delta", id: "t1", delta: "hello " },
|
|
30
|
+
{ type: "text-delta", id: "t1", delta: "world" },
|
|
31
|
+
{ type: "text-end", id: "t1" },
|
|
32
|
+
{ type: "finish" },
|
|
33
|
+
);
|
|
34
|
+
close();
|
|
35
|
+
|
|
36
|
+
await vi.waitFor(() => {
|
|
37
|
+
const state = aui.thread.getState();
|
|
38
|
+
expect(state.isRunning).toBe(false);
|
|
39
|
+
expect(state.messages).toHaveLength(2);
|
|
40
|
+
expect(state.messages.at(-1)?.content).toContainEqual(
|
|
41
|
+
expect.objectContaining({ type: "text", text: "hello world" }),
|
|
42
|
+
);
|
|
43
|
+
});
|
|
44
|
+
} finally {
|
|
45
|
+
handle.destroy();
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
});
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { flushTapSync } from "@ulearning/ai-tap";
|
|
2
|
+
// @vitest-environment jsdom
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
defineComponent,
|
|
6
|
+
h,
|
|
7
|
+
KeepAlive,
|
|
8
|
+
nextTick,
|
|
9
|
+
onMounted,
|
|
10
|
+
shallowRef,
|
|
11
|
+
type Component,
|
|
12
|
+
} from "vue";
|
|
13
|
+
import { act, render, waitFor } from "./__tests__/vue";
|
|
14
|
+
import { AuiConfig, AuiProvider, useAui } from "@ulearning/ai-store";
|
|
15
|
+
import type { ChatTransport, UIMessage } from "ai";
|
|
16
|
+
import { describe, expect, it, vi } from "vitest";
|
|
17
|
+
import { AISDKChat } from "./AISDKChat";
|
|
18
|
+
import {
|
|
19
|
+
createCancellableTransport,
|
|
20
|
+
createStreamHarness,
|
|
21
|
+
} from "./__tests__/controlled-transport";
|
|
22
|
+
|
|
23
|
+
// Native retained-scope lifecycle checks replace React's development replay.
|
|
24
|
+
// Deactivation must preserve the running transport; permanent release aborts once.
|
|
25
|
+
const mountRetained = (Owner: Component) => {
|
|
26
|
+
const visible = shallowRef(true);
|
|
27
|
+
const view = render(
|
|
28
|
+
h(
|
|
29
|
+
defineComponent({
|
|
30
|
+
setup: () => () =>
|
|
31
|
+
h(KeepAlive, null, {
|
|
32
|
+
default: () => (visible.value ? h(Owner) : null),
|
|
33
|
+
}),
|
|
34
|
+
}),
|
|
35
|
+
),
|
|
36
|
+
);
|
|
37
|
+
return { ...view, visible };
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const LegacyProvider = defineComponent(
|
|
41
|
+
(props: { transport: ChatTransport<UIMessage> }, { slots }) => {
|
|
42
|
+
const aui = useAui(() =>
|
|
43
|
+
AuiConfig({ threads: AISDKChat({ transport: props.transport }) }),
|
|
44
|
+
);
|
|
45
|
+
return () => h(AuiProvider, { value: aui }, slots);
|
|
46
|
+
},
|
|
47
|
+
{ props: ["transport"] },
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
for (const legacy of [false, true]) {
|
|
51
|
+
describe(
|
|
52
|
+
legacy
|
|
53
|
+
? "AISDKChat legacy useAui host integration"
|
|
54
|
+
: "AISDKChat Vue integration",
|
|
55
|
+
() => {
|
|
56
|
+
it("aborts the in-flight transport after a real unmount", async () => {
|
|
57
|
+
const { transport, getCancelCount } = createCancellableTransport();
|
|
58
|
+
const { Probe, send, isRunning } = createStreamHarness();
|
|
59
|
+
const Owner = defineComponent({
|
|
60
|
+
setup: () => () =>
|
|
61
|
+
legacy
|
|
62
|
+
? h(LegacyProvider, { transport }, { default: () => h(Probe) })
|
|
63
|
+
: h(
|
|
64
|
+
AuiProvider,
|
|
65
|
+
{ config: AuiConfig({ threads: AISDKChat({ transport }) }) },
|
|
66
|
+
{ default: () => h(Probe) },
|
|
67
|
+
),
|
|
68
|
+
});
|
|
69
|
+
const view = mountRetained(Owner);
|
|
70
|
+
await act(async () => send());
|
|
71
|
+
await waitFor(() => expect(isRunning()).toBe(true));
|
|
72
|
+
view.visible.value = false;
|
|
73
|
+
await nextTick();
|
|
74
|
+
expect(getCancelCount()).toBe(0);
|
|
75
|
+
view.visible.value = true;
|
|
76
|
+
await nextTick();
|
|
77
|
+
expect(isRunning()).toBe(true);
|
|
78
|
+
expect(getCancelCount()).toBe(0);
|
|
79
|
+
view.unmount();
|
|
80
|
+
await waitFor(() => expect(getCancelCount()).toBe(1));
|
|
81
|
+
});
|
|
82
|
+
},
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
describe("replacement transports", () => {
|
|
87
|
+
it("routes sends through a replacement transport", async () => {
|
|
88
|
+
const emptyStream = () =>
|
|
89
|
+
new ReadableStream({ start: (controller) => controller.close() });
|
|
90
|
+
const sendA = vi.fn(async () => emptyStream());
|
|
91
|
+
const sendB = vi.fn(async () => emptyStream());
|
|
92
|
+
const transportA: ChatTransport<UIMessage> = {
|
|
93
|
+
sendMessages: sendA,
|
|
94
|
+
reconnectToStream: vi.fn(),
|
|
95
|
+
};
|
|
96
|
+
const transportB: ChatTransport<UIMessage> = {
|
|
97
|
+
sendMessages: sendB,
|
|
98
|
+
reconnectToStream: vi.fn(),
|
|
99
|
+
};
|
|
100
|
+
let initialClient: ReturnType<typeof useAui> | undefined;
|
|
101
|
+
let currentClient: ReturnType<typeof useAui> | undefined;
|
|
102
|
+
const CaptureClient = defineComponent({
|
|
103
|
+
setup() {
|
|
104
|
+
const aui = useAui();
|
|
105
|
+
initialClient ??= aui;
|
|
106
|
+
currentClient = aui;
|
|
107
|
+
return () => null;
|
|
108
|
+
},
|
|
109
|
+
});
|
|
110
|
+
const SendOnLayout = defineComponent({
|
|
111
|
+
setup() {
|
|
112
|
+
const aui = useAui();
|
|
113
|
+
onMounted(() => {
|
|
114
|
+
flushTapSync(() => {
|
|
115
|
+
aui.composer.setText("hello");
|
|
116
|
+
aui.composer.send();
|
|
117
|
+
});
|
|
118
|
+
});
|
|
119
|
+
return () => null;
|
|
120
|
+
},
|
|
121
|
+
});
|
|
122
|
+
const App = defineComponent(
|
|
123
|
+
(props: { transport: ChatTransport<UIMessage>; send?: boolean }) => () =>
|
|
124
|
+
h(
|
|
125
|
+
AuiProvider,
|
|
126
|
+
{
|
|
127
|
+
config: AuiConfig({
|
|
128
|
+
threads: AISDKChat({ transport: props.transport }),
|
|
129
|
+
}),
|
|
130
|
+
},
|
|
131
|
+
{
|
|
132
|
+
default: () => [
|
|
133
|
+
h(CaptureClient),
|
|
134
|
+
props.send ? h(SendOnLayout) : null,
|
|
135
|
+
],
|
|
136
|
+
},
|
|
137
|
+
),
|
|
138
|
+
{ props: ["transport", "send"] },
|
|
139
|
+
);
|
|
140
|
+
|
|
141
|
+
const view = render(h(App, { transport: transportA }));
|
|
142
|
+
await view.rerender(h(App, { transport: transportB, send: true }));
|
|
143
|
+
|
|
144
|
+
await waitFor(() => expect(sendB).toHaveBeenCalledOnce());
|
|
145
|
+
expect(sendA).not.toHaveBeenCalled();
|
|
146
|
+
expect(currentClient).toBe(initialClient);
|
|
147
|
+
});
|
|
148
|
+
});
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { flushTapSync } from "@ulearning/ai-tap";
|
|
3
|
+
import { AuiConfig, createAssistantClient } from "@ulearning/ai-store/client";
|
|
4
|
+
import { AISDKChat } from "./AISDKChat";
|
|
5
|
+
import {
|
|
6
|
+
createCancellableTransport,
|
|
7
|
+
createControlledTransport,
|
|
8
|
+
} from "./__tests__/controlled-transport";
|
|
9
|
+
|
|
10
|
+
describe("AISDKChat as a standalone client config entry", () => {
|
|
11
|
+
afterEach(() => {
|
|
12
|
+
vi.unstubAllGlobals();
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it("streams a chat round trip without React", async () => {
|
|
16
|
+
const { transport, emit, close } = createControlledTransport();
|
|
17
|
+
const handle = createAssistantClient(
|
|
18
|
+
AuiConfig({ threads: AISDKChat({ transport }) }),
|
|
19
|
+
);
|
|
20
|
+
try {
|
|
21
|
+
handle.subscribe(() => {});
|
|
22
|
+
const aui = handle.getClient();
|
|
23
|
+
|
|
24
|
+
expect(aui.thread.getState().messages).toHaveLength(0);
|
|
25
|
+
|
|
26
|
+
flushTapSync(() => aui.composer.setText("hi"));
|
|
27
|
+
flushTapSync(() => aui.composer.send());
|
|
28
|
+
|
|
29
|
+
await vi.waitFor(() => {
|
|
30
|
+
const state = aui.thread.getState();
|
|
31
|
+
expect(state.isRunning).toBe(true);
|
|
32
|
+
expect(state.messages[0]).toMatchObject({ role: "user" });
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
emit(
|
|
36
|
+
{ type: "start" },
|
|
37
|
+
{ type: "text-start", id: "t1" },
|
|
38
|
+
{ type: "text-delta", id: "t1", delta: "hello " },
|
|
39
|
+
);
|
|
40
|
+
await vi.waitFor(() => {
|
|
41
|
+
const last = aui.thread.getState().messages.at(-1);
|
|
42
|
+
expect(last?.role).toBe("assistant");
|
|
43
|
+
expect(last?.content).toContainEqual(
|
|
44
|
+
expect.objectContaining({ type: "text", text: "hello " }),
|
|
45
|
+
);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
emit(
|
|
49
|
+
{ type: "text-delta", id: "t1", delta: "world" },
|
|
50
|
+
{ type: "text-end", id: "t1" },
|
|
51
|
+
{ type: "finish" },
|
|
52
|
+
);
|
|
53
|
+
close();
|
|
54
|
+
|
|
55
|
+
await vi.waitFor(() => {
|
|
56
|
+
const state = aui.thread.getState();
|
|
57
|
+
expect(state.isRunning).toBe(false);
|
|
58
|
+
expect(state.messages).toHaveLength(2);
|
|
59
|
+
expect(state.messages.at(-1)?.content).toContainEqual(
|
|
60
|
+
expect.objectContaining({ type: "text", text: "hello world" }),
|
|
61
|
+
);
|
|
62
|
+
});
|
|
63
|
+
} finally {
|
|
64
|
+
handle.destroy();
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it("stops an in-flight chat when its client is destroyed", async () => {
|
|
69
|
+
const { transport, getCancelCount } = createCancellableTransport();
|
|
70
|
+
const handle = createAssistantClient(
|
|
71
|
+
AuiConfig({ threads: AISDKChat({ transport }) }),
|
|
72
|
+
);
|
|
73
|
+
handle.subscribe(() => {});
|
|
74
|
+
const aui = handle.getClient();
|
|
75
|
+
|
|
76
|
+
try {
|
|
77
|
+
flushTapSync(() => aui.composer.setText("stop me"));
|
|
78
|
+
flushTapSync(() => aui.composer.send());
|
|
79
|
+
await vi.waitFor(() => {
|
|
80
|
+
expect(aui.thread.getState().isRunning).toBe(true);
|
|
81
|
+
});
|
|
82
|
+
} finally {
|
|
83
|
+
handle.destroy();
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
await vi.waitFor(() => {
|
|
87
|
+
expect(getCancelCount()).toBe(1);
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it("stops a soft-unmounted chat on later client destruction", async () => {
|
|
92
|
+
const { transport, getCancelCount } = createCancellableTransport();
|
|
93
|
+
const handle = createAssistantClient(
|
|
94
|
+
AuiConfig({ threads: AISDKChat({ transport }) }),
|
|
95
|
+
);
|
|
96
|
+
const release = handle.subscribe(() => {});
|
|
97
|
+
const aui = handle.getClient();
|
|
98
|
+
|
|
99
|
+
flushTapSync(() => aui.composer.setText("keep streaming"));
|
|
100
|
+
flushTapSync(() => aui.composer.send());
|
|
101
|
+
await vi.waitFor(() => {
|
|
102
|
+
expect(aui.thread.getState().isRunning).toBe(true);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
release();
|
|
106
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
107
|
+
expect(getCancelCount()).toBe(0);
|
|
108
|
+
|
|
109
|
+
handle.destroy();
|
|
110
|
+
await vi.waitFor(() => {
|
|
111
|
+
expect(getCancelCount()).toBe(1);
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it("holds a send made during a run with unstable_enableMessageQueue", async () => {
|
|
116
|
+
const { transport, emit, close } = createControlledTransport();
|
|
117
|
+
const sendMessages = vi.spyOn(transport, "sendMessages");
|
|
118
|
+
const handle = createAssistantClient(
|
|
119
|
+
AuiConfig({
|
|
120
|
+
threads: AISDKChat({ transport, unstable_enableMessageQueue: true }),
|
|
121
|
+
}),
|
|
122
|
+
);
|
|
123
|
+
try {
|
|
124
|
+
handle.subscribe(() => {});
|
|
125
|
+
const aui = handle.getClient();
|
|
126
|
+
const queued = () =>
|
|
127
|
+
aui.composer.getState().queue.map((item) => item.prompt);
|
|
128
|
+
|
|
129
|
+
flushTapSync(() => aui.composer.setText("first"));
|
|
130
|
+
flushTapSync(() => aui.composer.send());
|
|
131
|
+
await vi.waitFor(() => {
|
|
132
|
+
expect(aui.thread.getState().isRunning).toBe(true);
|
|
133
|
+
});
|
|
134
|
+
expect(aui.thread.getState().capabilities.queue).toBe(true);
|
|
135
|
+
|
|
136
|
+
flushTapSync(() => aui.composer.setText("second"));
|
|
137
|
+
flushTapSync(() => aui.composer.send({ steer: false }));
|
|
138
|
+
await vi.waitFor(() => {
|
|
139
|
+
expect(queued()).toEqual(["second"]);
|
|
140
|
+
});
|
|
141
|
+
expect(sendMessages).toHaveBeenCalledTimes(1);
|
|
142
|
+
|
|
143
|
+
emit(
|
|
144
|
+
{ type: "start" },
|
|
145
|
+
{ type: "text-start", id: "t1" },
|
|
146
|
+
{ type: "text-delta", id: "t1", delta: "one" },
|
|
147
|
+
{ type: "text-end", id: "t1" },
|
|
148
|
+
{ type: "finish" },
|
|
149
|
+
);
|
|
150
|
+
close();
|
|
151
|
+
|
|
152
|
+
await vi.waitFor(() => {
|
|
153
|
+
expect(sendMessages).toHaveBeenCalledTimes(2);
|
|
154
|
+
});
|
|
155
|
+
expect(
|
|
156
|
+
sendMessages.mock.calls[1]![0].messages.at(-1)?.parts,
|
|
157
|
+
).toContainEqual(expect.objectContaining({ text: "second" }));
|
|
158
|
+
expect(queued()).toEqual([]);
|
|
159
|
+
} finally {
|
|
160
|
+
handle.destroy();
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
it("installs the RuntimeAdapter scope defaults", () => {
|
|
165
|
+
const { transport } = createControlledTransport();
|
|
166
|
+
const handle = createAssistantClient(
|
|
167
|
+
AuiConfig({ threads: AISDKChat({ transport }) }),
|
|
168
|
+
);
|
|
169
|
+
try {
|
|
170
|
+
handle.subscribe(() => {});
|
|
171
|
+
const aui = handle.getClient();
|
|
172
|
+
|
|
173
|
+
expect(aui.threads.getState().mainThreadId).toBeDefined();
|
|
174
|
+
expect(aui.tools.getState()).toBeDefined();
|
|
175
|
+
expect(aui.dataRenderers.getState()).toBeDefined();
|
|
176
|
+
expect(aui.thread.getState().isRunning).toBe(false);
|
|
177
|
+
} finally {
|
|
178
|
+
handle.destroy();
|
|
179
|
+
}
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
it("mounts with the default transport when no options are given", () => {
|
|
183
|
+
const handle = createAssistantClient(AuiConfig({ threads: AISDKChat({}) }));
|
|
184
|
+
try {
|
|
185
|
+
handle.subscribe(() => {});
|
|
186
|
+
const aui = handle.getClient();
|
|
187
|
+
|
|
188
|
+
expect(aui.thread.getState().messages).toHaveLength(0);
|
|
189
|
+
expect(aui.thread.getState().isRunning).toBe(false);
|
|
190
|
+
} finally {
|
|
191
|
+
handle.destroy();
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
it("sends through the default AssistantChatTransport with the generated id", async () => {
|
|
196
|
+
const sse = [
|
|
197
|
+
{ type: "start" },
|
|
198
|
+
{ type: "text-start", id: "t1" },
|
|
199
|
+
{ type: "text-delta", id: "t1", delta: "ok" },
|
|
200
|
+
{ type: "text-end", id: "t1" },
|
|
201
|
+
{ type: "finish" },
|
|
202
|
+
]
|
|
203
|
+
.map((chunk) => `data: ${JSON.stringify(chunk)}\n\n`)
|
|
204
|
+
.join("");
|
|
205
|
+
const fetchMock = vi.fn<
|
|
206
|
+
(input: RequestInfo | URL, init: RequestInit) => Promise<Response>
|
|
207
|
+
>(
|
|
208
|
+
async () =>
|
|
209
|
+
new Response(sse, {
|
|
210
|
+
headers: { "content-type": "text/event-stream" },
|
|
211
|
+
}),
|
|
212
|
+
);
|
|
213
|
+
vi.stubGlobal("fetch", fetchMock);
|
|
214
|
+
|
|
215
|
+
const handle = createAssistantClient(
|
|
216
|
+
AuiConfig({ threads: AISDKChat({ id: "test-thread-1" }) }),
|
|
217
|
+
);
|
|
218
|
+
try {
|
|
219
|
+
handle.subscribe(() => {});
|
|
220
|
+
const aui = handle.getClient();
|
|
221
|
+
|
|
222
|
+
flushTapSync(() => aui.composer.setText("hi"));
|
|
223
|
+
flushTapSync(() => aui.composer.send());
|
|
224
|
+
|
|
225
|
+
await vi.waitFor(() => {
|
|
226
|
+
expect(fetchMock).toHaveBeenCalledTimes(1);
|
|
227
|
+
const state = aui.thread.getState();
|
|
228
|
+
expect(state.isRunning).toBe(false);
|
|
229
|
+
expect(state.messages).toHaveLength(2);
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
const [url, init] = fetchMock.mock.calls[0]!;
|
|
233
|
+
expect(String(url)).toContain("/api/chat");
|
|
234
|
+
const body = JSON.parse(init.body as string);
|
|
235
|
+
expect(body.id).toBe("test-thread-1");
|
|
236
|
+
expect(body.messages).toHaveLength(1);
|
|
237
|
+
} finally {
|
|
238
|
+
handle.destroy();
|
|
239
|
+
}
|
|
240
|
+
});
|
|
241
|
+
});
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { resource, useResource } from "@ulearning/ai-tap";
|
|
4
|
+
import { useState } from "@ulearning/ai-tap";
|
|
5
|
+
import { generateId } from "ai";
|
|
6
|
+
import type { UIMessage } from "ai";
|
|
7
|
+
import {
|
|
8
|
+
RuntimeAdapter,
|
|
9
|
+
runtimeAdapterTransformScopes,
|
|
10
|
+
} from "@ulearning/ai-core/store";
|
|
11
|
+
import { attachTransformScopes } from "@ulearning/ai-store/client";
|
|
12
|
+
import { useChatThread, type ChatThreadOptions } from "./useChatThread";
|
|
13
|
+
|
|
14
|
+
export type AISDKChatOptions<UI_MESSAGE extends UIMessage = UIMessage> =
|
|
15
|
+
ChatThreadOptions<UI_MESSAGE>;
|
|
16
|
+
|
|
17
|
+
const useAISDKChat = <UI_MESSAGE extends UIMessage = UIMessage>(
|
|
18
|
+
options?: AISDKChatOptions<UI_MESSAGE>,
|
|
19
|
+
) => {
|
|
20
|
+
const [id] = useState(() => options?.id ?? generateId());
|
|
21
|
+
const [threadListItem] = useState(() => ({
|
|
22
|
+
initialize: async () => ({ remoteId: id, externalId: undefined }),
|
|
23
|
+
}));
|
|
24
|
+
const runtime = useChatThread(options, {
|
|
25
|
+
id,
|
|
26
|
+
isMainThread: true,
|
|
27
|
+
getThreadListItem: () => threadListItem,
|
|
28
|
+
stopOnClientDestroy: true,
|
|
29
|
+
});
|
|
30
|
+
return useResource(RuntimeAdapter(runtime));
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* `AuiConfig` entry that runs the AI SDK chat as the `threads` scope. Hosts the
|
|
35
|
+
* same orchestration as `useChatRuntime` inside the client's own resource tree,
|
|
36
|
+
* so it works with any `AssistantClient` host, React or not. Single thread; the
|
|
37
|
+
* multi-thread surface is {@link AISDKThreads}. The chat
|
|
38
|
+
* id is captured when the entry mounts, so a later `id` change in the options
|
|
39
|
+
* has no effect.
|
|
40
|
+
*/
|
|
41
|
+
export const AISDKChat = resource(useAISDKChat);
|
|
42
|
+
|
|
43
|
+
attachTransformScopes(useAISDKChat, runtimeAdapterTransformScopes);
|