@ai-sdk/vue 0.0.44 → 0.0.45
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/CHANGELOG.md +7 -0
- package/package.json +6 -2
- package/.eslintrc.js +0 -4
- package/.turbo/turbo-build.log +0 -21
- package/.turbo/turbo-clean.log +0 -4
- package/src/TestChatAssistantStreamComponent.vue +0 -18
- package/src/TestChatAssistantThreadChangeComponent.vue +0 -22
- package/src/TestChatComponent.vue +0 -44
- package/src/TestChatCustomMetadataComponent.vue +0 -32
- package/src/TestChatFormComponent.vue +0 -22
- package/src/TestChatFormOptionsComponent.vue +0 -29
- package/src/TestChatReloadComponent.vue +0 -34
- package/src/TestChatTextStreamComponent.vue +0 -42
- package/src/TestCompletionComponent.vue +0 -17
- package/src/TestCompletionTextStreamComponent.vue +0 -19
- package/src/index.ts +0 -3
- package/src/package.json +0 -10
- package/src/shims-vue.d.ts +0 -5
- package/src/use-assistant.ts +0 -305
- package/src/use-assistant.ui.test.tsx +0 -272
- package/src/use-chat.ts +0 -321
- package/src/use-chat.ui.test.tsx +0 -446
- package/src/use-completion.ts +0 -177
- package/src/use-completion.ui.test.ts +0 -93
- package/tsconfig.json +0 -9
- package/tsup.config.ts +0 -13
- package/turbo.json +0 -8
- package/vitest.config.js +0 -12
package/CHANGELOG.md
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ai-sdk/vue",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.45",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -14,9 +14,13 @@
|
|
|
14
14
|
"require": "./dist/index.js"
|
|
15
15
|
}
|
|
16
16
|
},
|
|
17
|
+
"files": [
|
|
18
|
+
"dist/**/*",
|
|
19
|
+
"CHANGELOG.md"
|
|
20
|
+
],
|
|
17
21
|
"dependencies": {
|
|
18
22
|
"@ai-sdk/provider-utils": "1.0.17",
|
|
19
|
-
"@ai-sdk/ui-utils": "0.0.
|
|
23
|
+
"@ai-sdk/ui-utils": "0.0.40",
|
|
20
24
|
"swrv": "1.0.4"
|
|
21
25
|
},
|
|
22
26
|
"devDependencies": {
|
package/.eslintrc.js
DELETED
package/.turbo/turbo-build.log
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
> @ai-sdk/vue@0.0.44 build /home/runner/work/ai/ai/packages/vue
|
|
3
|
-
> tsup
|
|
4
|
-
|
|
5
|
-
[34mCLI[39m Building entry: src/index.ts
|
|
6
|
-
[34mCLI[39m Using tsconfig: tsconfig.json
|
|
7
|
-
[34mCLI[39m tsup v7.2.0
|
|
8
|
-
[34mCLI[39m Using tsup config: /home/runner/work/ai/ai/packages/vue/tsup.config.ts
|
|
9
|
-
[34mCLI[39m Target: es2018
|
|
10
|
-
[34mCJS[39m Build start
|
|
11
|
-
[34mESM[39m Build start
|
|
12
|
-
[32mCJS[39m [1mdist/index.js [22m[32m16.21 KB[39m
|
|
13
|
-
[32mCJS[39m [1mdist/index.js.map [22m[32m31.11 KB[39m
|
|
14
|
-
[32mCJS[39m ⚡️ Build success in 46ms
|
|
15
|
-
[32mESM[39m [1mdist/index.mjs [22m[32m14.09 KB[39m
|
|
16
|
-
[32mESM[39m [1mdist/index.mjs.map [22m[32m30.89 KB[39m
|
|
17
|
-
[32mESM[39m ⚡️ Build success in 51ms
|
|
18
|
-
[34mDTS[39m Build start
|
|
19
|
-
[32mDTS[39m ⚡️ Build success in 5350ms
|
|
20
|
-
[32mDTS[39m [1mdist/index.d.ts [22m[32m5.94 KB[39m
|
|
21
|
-
[32mDTS[39m [1mdist/index.d.mts [22m[32m5.94 KB[39m
|
package/.turbo/turbo-clean.log
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
<script lang="ts" setup>
|
|
2
|
-
import { useAssistant } from './use-assistant';
|
|
3
|
-
|
|
4
|
-
const { status, messages, append } = useAssistant({
|
|
5
|
-
api: '/api/assistant'
|
|
6
|
-
});
|
|
7
|
-
</script>
|
|
8
|
-
|
|
9
|
-
<template>
|
|
10
|
-
<div>
|
|
11
|
-
<div data-testid="status">{{ status }}</div>
|
|
12
|
-
<div v-for="(message, index) in messages" :data-testid="`message-${index}`" :key="index">
|
|
13
|
-
{{ message.role === 'user' ? 'User: ' : 'AI: ' }}
|
|
14
|
-
{{ message.content }}
|
|
15
|
-
</div>
|
|
16
|
-
<button data-testid="do-append" @click="append({ role: 'user', content: 'hi' })" />
|
|
17
|
-
</div>
|
|
18
|
-
</template>
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
<script setup lang="ts">
|
|
2
|
-
import { useAssistant } from './use-assistant';
|
|
3
|
-
|
|
4
|
-
const { status, messages, error, append, setThreadId, threadId } = useAssistant({
|
|
5
|
-
api: '/api/assistant'
|
|
6
|
-
});
|
|
7
|
-
</script>
|
|
8
|
-
|
|
9
|
-
<template>
|
|
10
|
-
<div>
|
|
11
|
-
<div data-testid="status">{{ status }}</div>
|
|
12
|
-
<div data-testid="thread-id">{{ threadId || 'undefined' }}</div>
|
|
13
|
-
<div data-testid="error">{{ error?.toString() }}</div>
|
|
14
|
-
<div v-for="(message, index) in messages" :data-testid="`message-${index}`" :key="index">
|
|
15
|
-
{{ message.role === 'user' ? 'User: ' : 'AI: ' }}
|
|
16
|
-
{{ message.content }}
|
|
17
|
-
</div>
|
|
18
|
-
<button data-testid="do-append" @click="append({ role: 'user', content: 'hi' })" />
|
|
19
|
-
<button data-testid="do-new-thread" @click="setThreadId(undefined)" />
|
|
20
|
-
<button data-testid="do-thread-3" @click="setThreadId('t3')" />
|
|
21
|
-
</div>
|
|
22
|
-
</template>
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
<script setup lang="ts">
|
|
2
|
-
import { useChat } from './use-chat';
|
|
3
|
-
|
|
4
|
-
const onFinishCalls: Array<{
|
|
5
|
-
message: Message;
|
|
6
|
-
options: {
|
|
7
|
-
finishReason: string;
|
|
8
|
-
usage: {
|
|
9
|
-
completionTokens: number;
|
|
10
|
-
promptTokens: number;
|
|
11
|
-
totalTokens: number;
|
|
12
|
-
};
|
|
13
|
-
};
|
|
14
|
-
}> = [];
|
|
15
|
-
|
|
16
|
-
const { messages, append, data, error, isLoading } = useChat({
|
|
17
|
-
onFinish: (message, options) => {
|
|
18
|
-
onFinishCalls.push({ message, options });
|
|
19
|
-
},
|
|
20
|
-
});
|
|
21
|
-
</script>
|
|
22
|
-
|
|
23
|
-
<template>
|
|
24
|
-
<div>
|
|
25
|
-
<div data-testid="loading">{{ isLoading?.toString() }}</div>
|
|
26
|
-
<div data-testid="error">{{ error?.toString() }}</div>
|
|
27
|
-
<div data-testid="data">{{ JSON.stringify(data) }}</div>
|
|
28
|
-
<div
|
|
29
|
-
v-for="(m, idx) in messages"
|
|
30
|
-
key="m.id"
|
|
31
|
-
:data-testid="`message-${idx}`"
|
|
32
|
-
>
|
|
33
|
-
{{ m.role === 'user' ? 'User: ' : 'AI: ' }}
|
|
34
|
-
{{ m.content }}
|
|
35
|
-
</div>
|
|
36
|
-
|
|
37
|
-
<button
|
|
38
|
-
data-testid="do-append"
|
|
39
|
-
@click="append({ role: 'user', content: 'hi' })"
|
|
40
|
-
/>
|
|
41
|
-
|
|
42
|
-
<div data-testid="on-finish-calls">{{ JSON.stringify(onFinishCalls) }}</div>
|
|
43
|
-
</div>
|
|
44
|
-
</template>
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
<script setup lang="ts">
|
|
2
|
-
import { useChat } from './use-chat';
|
|
3
|
-
|
|
4
|
-
const { messages, append } = useChat({
|
|
5
|
-
body: {
|
|
6
|
-
body1: 'value1',
|
|
7
|
-
body2: 'value2',
|
|
8
|
-
},
|
|
9
|
-
headers: {
|
|
10
|
-
header1: 'value1',
|
|
11
|
-
header2: 'value2',
|
|
12
|
-
},
|
|
13
|
-
});
|
|
14
|
-
</script>
|
|
15
|
-
|
|
16
|
-
<template>
|
|
17
|
-
<div>
|
|
18
|
-
<div
|
|
19
|
-
v-for="(m, idx) in messages"
|
|
20
|
-
key="m.id"
|
|
21
|
-
:data-testid="`message-${idx}`"
|
|
22
|
-
>
|
|
23
|
-
{{ m.role === 'user' ? 'User: ' : 'AI: ' }}
|
|
24
|
-
{{ m.content }}
|
|
25
|
-
</div>
|
|
26
|
-
|
|
27
|
-
<button
|
|
28
|
-
data-testid="do-append"
|
|
29
|
-
@click="append({ role: 'user', content: 'custom metadata component' })"
|
|
30
|
-
/>
|
|
31
|
-
</div>
|
|
32
|
-
</template>
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
<script setup lang="ts">
|
|
2
|
-
import { useChat } from './use-chat';
|
|
3
|
-
|
|
4
|
-
const { messages, handleSubmit, input } = useChat();
|
|
5
|
-
</script>
|
|
6
|
-
|
|
7
|
-
<template>
|
|
8
|
-
<div>
|
|
9
|
-
<div
|
|
10
|
-
v-for="(m, idx) in messages"
|
|
11
|
-
key="m.id"
|
|
12
|
-
:data-testid="`message-${idx}`"
|
|
13
|
-
>
|
|
14
|
-
{{ m.role === 'user' ? 'User: ' : 'AI: ' }}
|
|
15
|
-
{{ m.content }}
|
|
16
|
-
</div>
|
|
17
|
-
|
|
18
|
-
<form @submit.prevent="handleSubmit">
|
|
19
|
-
<input :data-testid="`do-input`" v-model="input" type="text" />
|
|
20
|
-
</form>
|
|
21
|
-
</div>
|
|
22
|
-
</template>
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
<script setup lang="ts">
|
|
2
|
-
import { useChat } from './use-chat';
|
|
3
|
-
|
|
4
|
-
const { messages, handleSubmit, input } = useChat();
|
|
5
|
-
</script>
|
|
6
|
-
|
|
7
|
-
<template>
|
|
8
|
-
<div>
|
|
9
|
-
<div
|
|
10
|
-
v-for="(m, idx) in messages"
|
|
11
|
-
key="m.id"
|
|
12
|
-
:data-testid="`message-${idx}`"
|
|
13
|
-
>
|
|
14
|
-
{{ m.role === 'user' ? 'User: ' : 'AI: ' }}
|
|
15
|
-
{{ m.content }}
|
|
16
|
-
</div>
|
|
17
|
-
|
|
18
|
-
<form
|
|
19
|
-
@submit.prevent="
|
|
20
|
-
event =>
|
|
21
|
-
handleSubmit(event, {
|
|
22
|
-
allowEmptySubmit: true,
|
|
23
|
-
})
|
|
24
|
-
"
|
|
25
|
-
>
|
|
26
|
-
<input :data-testid="`do-input`" v-model="input" type="text" />
|
|
27
|
-
</form>
|
|
28
|
-
</div>
|
|
29
|
-
</template>
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
<script setup lang="ts">
|
|
2
|
-
import { useChat } from './use-chat';
|
|
3
|
-
|
|
4
|
-
const { messages, append, reload } = useChat();
|
|
5
|
-
</script>
|
|
6
|
-
|
|
7
|
-
<template>
|
|
8
|
-
<div>
|
|
9
|
-
<div
|
|
10
|
-
v-for="(m, idx) in messages"
|
|
11
|
-
key="m.id"
|
|
12
|
-
:data-testid="`message-${idx}`"
|
|
13
|
-
>
|
|
14
|
-
{{ m.role === 'user' ? 'User: ' : 'AI: ' }}
|
|
15
|
-
{{ m.content }}
|
|
16
|
-
</div>
|
|
17
|
-
|
|
18
|
-
<button
|
|
19
|
-
data-testid="do-append"
|
|
20
|
-
@click="append({ role: 'user', content: 'hi' })"
|
|
21
|
-
/>
|
|
22
|
-
|
|
23
|
-
<button
|
|
24
|
-
data-testid="do-reload"
|
|
25
|
-
@click="
|
|
26
|
-
reload({
|
|
27
|
-
data: { 'test-data-key': 'test-data-value' },
|
|
28
|
-
body: { 'request-body-key': 'request-body-value' },
|
|
29
|
-
headers: { 'header-key': 'header-value' },
|
|
30
|
-
})
|
|
31
|
-
"
|
|
32
|
-
/>
|
|
33
|
-
</div>
|
|
34
|
-
</template>
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
<script setup lang="ts">
|
|
2
|
-
import { useChat } from './use-chat';
|
|
3
|
-
|
|
4
|
-
const onFinishCalls: Array<{
|
|
5
|
-
message: Message;
|
|
6
|
-
options: {
|
|
7
|
-
finishReason: string;
|
|
8
|
-
usage: {
|
|
9
|
-
completionTokens: number;
|
|
10
|
-
promptTokens: number;
|
|
11
|
-
totalTokens: number;
|
|
12
|
-
};
|
|
13
|
-
};
|
|
14
|
-
}> = [];
|
|
15
|
-
|
|
16
|
-
const { messages, append } = useChat({
|
|
17
|
-
streamProtocol: 'text',
|
|
18
|
-
onFinish: (message, options) => {
|
|
19
|
-
onFinishCalls.push({ message, options });
|
|
20
|
-
},
|
|
21
|
-
});
|
|
22
|
-
</script>
|
|
23
|
-
|
|
24
|
-
<template>
|
|
25
|
-
<div>
|
|
26
|
-
<div
|
|
27
|
-
v-for="(m, idx) in messages"
|
|
28
|
-
key="m.id"
|
|
29
|
-
:data-testid="`message-${idx}`"
|
|
30
|
-
>
|
|
31
|
-
{{ m.role === 'user' ? 'User: ' : 'AI: ' }}
|
|
32
|
-
{{ m.content }}
|
|
33
|
-
</div>
|
|
34
|
-
|
|
35
|
-
<button
|
|
36
|
-
data-testid="do-append"
|
|
37
|
-
@click="append({ role: 'user', content: 'hi' })"
|
|
38
|
-
/>
|
|
39
|
-
|
|
40
|
-
<div data-testid="on-finish-calls">{{ JSON.stringify(onFinishCalls) }}</div>
|
|
41
|
-
</div>
|
|
42
|
-
</template>
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
<script setup lang="ts">
|
|
2
|
-
import { useCompletion } from './use-completion';
|
|
3
|
-
|
|
4
|
-
const { completion, handleSubmit, input, isLoading, error } = useCompletion();
|
|
5
|
-
</script>
|
|
6
|
-
|
|
7
|
-
<template>
|
|
8
|
-
<div>
|
|
9
|
-
<div data-testid="loading">{{ isLoading?.toString() }}</div>
|
|
10
|
-
<div data-testid="error">{{ error?.toString() }}</div>
|
|
11
|
-
<div data-testid="completion">{{ completion }}</div>
|
|
12
|
-
|
|
13
|
-
<form @submit="handleSubmit">
|
|
14
|
-
<input data-testid="input" v-model="input" />
|
|
15
|
-
</form>
|
|
16
|
-
</div>
|
|
17
|
-
</template>
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
<script setup lang="ts">
|
|
2
|
-
import { useCompletion } from './use-completion';
|
|
3
|
-
|
|
4
|
-
const { completion, handleSubmit, input, isLoading, error } = useCompletion({
|
|
5
|
-
streamProtocol: 'text',
|
|
6
|
-
});
|
|
7
|
-
</script>
|
|
8
|
-
|
|
9
|
-
<template>
|
|
10
|
-
<div>
|
|
11
|
-
<div data-testid="loading">{{ isLoading?.toString() }}</div>
|
|
12
|
-
<div data-testid="error">{{ error?.toString() }}</div>
|
|
13
|
-
<div data-testid="completion">{{ completion }}</div>
|
|
14
|
-
|
|
15
|
-
<form @submit="handleSubmit">
|
|
16
|
-
<input data-testid="input" v-model="input" />
|
|
17
|
-
</form>
|
|
18
|
-
</div>
|
|
19
|
-
</template>
|
package/src/index.ts
DELETED
package/src/package.json
DELETED
package/src/shims-vue.d.ts
DELETED
package/src/use-assistant.ts
DELETED
|
@@ -1,305 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* A vue.js composable function to interact with the assistant API.
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import { isAbortError } from '@ai-sdk/provider-utils';
|
|
6
|
-
import { readDataStream, generateId } from '@ai-sdk/ui-utils';
|
|
7
|
-
import type {
|
|
8
|
-
AssistantStatus,
|
|
9
|
-
CreateMessage,
|
|
10
|
-
Message,
|
|
11
|
-
UseAssistantOptions,
|
|
12
|
-
} from '@ai-sdk/ui-utils';
|
|
13
|
-
import { computed, readonly, ref } from 'vue';
|
|
14
|
-
import type { ComputedRef, Ref } from 'vue';
|
|
15
|
-
|
|
16
|
-
export type UseAssistantHelpers = {
|
|
17
|
-
/**
|
|
18
|
-
* The current array of chat messages.
|
|
19
|
-
*/
|
|
20
|
-
messages: Ref<Message[]>;
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Update the message store with a new array of messages.
|
|
24
|
-
*/
|
|
25
|
-
setMessages: (messagesProcessor: (messages: Message[]) => Message[]) => void;
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* The current thread ID.
|
|
29
|
-
*/
|
|
30
|
-
threadId: Ref<string | undefined>;
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Set the current thread ID. Specifying a thread ID will switch to that thread, if it exists. If set to 'undefined', a new thread will be created. For both cases, `threadId` will be updated with the new value and `messages` will be cleared.
|
|
34
|
-
*/
|
|
35
|
-
setThreadId: (threadId: string | undefined) => void;
|
|
36
|
-
/**
|
|
37
|
-
* The current value of the input field.
|
|
38
|
-
*/
|
|
39
|
-
input: Ref<string>;
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Append a user message to the chat list. This triggers the API call to fetch
|
|
43
|
-
* the assistant's response.
|
|
44
|
-
* @param message The message to append
|
|
45
|
-
* @param requestOptions Additional options to pass to the API call
|
|
46
|
-
*/
|
|
47
|
-
append: (
|
|
48
|
-
message: Message | CreateMessage,
|
|
49
|
-
requestOptions?: {
|
|
50
|
-
data?: Record<string, string>;
|
|
51
|
-
},
|
|
52
|
-
) => Promise<void>;
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Abort the current request immediately, keep the generated tokens if any.
|
|
56
|
-
*/
|
|
57
|
-
stop: ComputedRef<() => void>;
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Handler for the `onChange` event of the input field to control the input's value.
|
|
61
|
-
*/
|
|
62
|
-
handleInputChange: (e: Event & { target: HTMLInputElement }) => void;
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Handler for the `onSubmit` event of the form to append a user message and reset the input.
|
|
66
|
-
*/
|
|
67
|
-
handleSubmit: (e: Event & { target: HTMLFormElement }) => void;
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Whether the assistant is currently sending a message.
|
|
71
|
-
*/
|
|
72
|
-
isSending: ComputedRef<boolean>;
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* The current status of the assistant.
|
|
76
|
-
*/
|
|
77
|
-
status: Ref<AssistantStatus>;
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* The current error, if any.
|
|
81
|
-
*/
|
|
82
|
-
error: Ref<Error | undefined>;
|
|
83
|
-
};
|
|
84
|
-
|
|
85
|
-
export function useAssistant({
|
|
86
|
-
api,
|
|
87
|
-
threadId: threadIdParam,
|
|
88
|
-
credentials,
|
|
89
|
-
headers,
|
|
90
|
-
body,
|
|
91
|
-
onError,
|
|
92
|
-
}: UseAssistantOptions): UseAssistantHelpers {
|
|
93
|
-
const messages: Ref<Message[]> = ref([]);
|
|
94
|
-
const input: Ref<string> = ref('');
|
|
95
|
-
const currentThreadId: Ref<string | undefined> = ref(undefined);
|
|
96
|
-
const status: Ref<AssistantStatus> = ref('awaiting_message');
|
|
97
|
-
const error: Ref<undefined | Error> = ref(undefined);
|
|
98
|
-
|
|
99
|
-
const setMessages = (messageFactory: (messages: Message[]) => Message[]) => {
|
|
100
|
-
messages.value = messageFactory(messages.value);
|
|
101
|
-
};
|
|
102
|
-
|
|
103
|
-
const setCurrentThreadId = (newThreadId: string | undefined) => {
|
|
104
|
-
currentThreadId.value = newThreadId;
|
|
105
|
-
messages.value = [];
|
|
106
|
-
};
|
|
107
|
-
|
|
108
|
-
const handleInputChange = (event: Event & { target: HTMLInputElement }) => {
|
|
109
|
-
input.value = event?.target?.value;
|
|
110
|
-
};
|
|
111
|
-
|
|
112
|
-
const isSending = computed(() => status.value === 'in_progress');
|
|
113
|
-
|
|
114
|
-
// Abort controller to cancel the current API call when required
|
|
115
|
-
const abortController = ref<AbortController | null>(null);
|
|
116
|
-
|
|
117
|
-
// memoized function to stop the current request when required
|
|
118
|
-
const stop = computed(() => {
|
|
119
|
-
return () => {
|
|
120
|
-
if (abortController.value) {
|
|
121
|
-
abortController.value.abort();
|
|
122
|
-
abortController.value = null;
|
|
123
|
-
}
|
|
124
|
-
};
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
const append = async (
|
|
128
|
-
message: Message | CreateMessage,
|
|
129
|
-
requestOptions?: {
|
|
130
|
-
data?: Record<string, string>;
|
|
131
|
-
},
|
|
132
|
-
) => {
|
|
133
|
-
status.value = 'in_progress';
|
|
134
|
-
|
|
135
|
-
// Append the new message to the current list of messages
|
|
136
|
-
const newMessage: Message = {
|
|
137
|
-
...message,
|
|
138
|
-
id: message.id ?? generateId(),
|
|
139
|
-
};
|
|
140
|
-
|
|
141
|
-
// Update the messages list with the new message
|
|
142
|
-
setMessages(messages => [...messages, newMessage]);
|
|
143
|
-
|
|
144
|
-
input.value = '';
|
|
145
|
-
|
|
146
|
-
const controller = new AbortController();
|
|
147
|
-
|
|
148
|
-
try {
|
|
149
|
-
// Assign the new controller to the abortController ref
|
|
150
|
-
abortController.value = controller;
|
|
151
|
-
|
|
152
|
-
const response = await fetch(api, {
|
|
153
|
-
method: 'POST',
|
|
154
|
-
headers: {
|
|
155
|
-
'Content-Type': 'application/json',
|
|
156
|
-
...headers,
|
|
157
|
-
},
|
|
158
|
-
body: JSON.stringify({
|
|
159
|
-
...body,
|
|
160
|
-
// Message Content
|
|
161
|
-
message: message.content,
|
|
162
|
-
|
|
163
|
-
// Always Use User Provided Thread ID When Available
|
|
164
|
-
threadId: threadIdParam ?? currentThreadId.value ?? null,
|
|
165
|
-
|
|
166
|
-
// Optional Request Data
|
|
167
|
-
...(requestOptions?.data && { data: requestOptions?.data }),
|
|
168
|
-
}),
|
|
169
|
-
signal: controller.signal,
|
|
170
|
-
credentials,
|
|
171
|
-
});
|
|
172
|
-
|
|
173
|
-
if (!response.ok) {
|
|
174
|
-
throw new Error(
|
|
175
|
-
response.statusText ?? 'An error occurred while sending the message',
|
|
176
|
-
);
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
if (!response.body) {
|
|
180
|
-
throw new Error('The response body is empty');
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
for await (const { type, value } of readDataStream(
|
|
184
|
-
response.body.getReader(),
|
|
185
|
-
)) {
|
|
186
|
-
switch (type) {
|
|
187
|
-
case 'assistant_message': {
|
|
188
|
-
messages.value = [
|
|
189
|
-
...messages.value,
|
|
190
|
-
{
|
|
191
|
-
id: value.id,
|
|
192
|
-
content: value.content[0].text.value,
|
|
193
|
-
role: value.role,
|
|
194
|
-
},
|
|
195
|
-
];
|
|
196
|
-
break;
|
|
197
|
-
}
|
|
198
|
-
case 'assistant_control_data': {
|
|
199
|
-
if (value.threadId) {
|
|
200
|
-
currentThreadId.value = value.threadId;
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
setMessages(messages => {
|
|
204
|
-
const lastMessage = messages[messages.length - 1];
|
|
205
|
-
lastMessage.id = value.messageId;
|
|
206
|
-
|
|
207
|
-
return [...messages.slice(0, -1), lastMessage];
|
|
208
|
-
});
|
|
209
|
-
|
|
210
|
-
break;
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
case 'text': {
|
|
214
|
-
setMessages(messages => {
|
|
215
|
-
const lastMessage = messages[messages.length - 1];
|
|
216
|
-
lastMessage.content += value;
|
|
217
|
-
|
|
218
|
-
return [...messages.slice(0, -1), lastMessage];
|
|
219
|
-
});
|
|
220
|
-
|
|
221
|
-
break;
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
case 'data_message': {
|
|
225
|
-
setMessages(messages => [
|
|
226
|
-
...messages,
|
|
227
|
-
{
|
|
228
|
-
id: value.id ?? generateId(),
|
|
229
|
-
role: 'data',
|
|
230
|
-
content: '',
|
|
231
|
-
data: value.data,
|
|
232
|
-
},
|
|
233
|
-
]);
|
|
234
|
-
break;
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
case 'error': {
|
|
238
|
-
error.value = new Error(value);
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
default: {
|
|
242
|
-
console.error('Unknown message type:', type);
|
|
243
|
-
break;
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
} catch (err) {
|
|
248
|
-
// If the error is an AbortError and the signal is aborted, reset the abortController and do nothing.
|
|
249
|
-
if (isAbortError(err) && abortController.value?.signal.aborted) {
|
|
250
|
-
abortController.value = null;
|
|
251
|
-
return;
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
// If an error handler is provided, call it with the error
|
|
255
|
-
if (onError && err instanceof Error) {
|
|
256
|
-
onError(err);
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
error.value = err as Error;
|
|
260
|
-
} finally {
|
|
261
|
-
// Reset the status to 'awaiting_message' after the request is complete
|
|
262
|
-
abortController.value = null;
|
|
263
|
-
status.value = 'awaiting_message';
|
|
264
|
-
}
|
|
265
|
-
};
|
|
266
|
-
|
|
267
|
-
const submitMessage = async (
|
|
268
|
-
event: Event & { target: HTMLFormElement },
|
|
269
|
-
requestOptions?: {
|
|
270
|
-
data?: Record<string, string>;
|
|
271
|
-
},
|
|
272
|
-
) => {
|
|
273
|
-
event?.preventDefault?.();
|
|
274
|
-
|
|
275
|
-
if (!input.value) return;
|
|
276
|
-
|
|
277
|
-
append(
|
|
278
|
-
{
|
|
279
|
-
role: 'user',
|
|
280
|
-
content: input.value,
|
|
281
|
-
},
|
|
282
|
-
requestOptions,
|
|
283
|
-
);
|
|
284
|
-
};
|
|
285
|
-
|
|
286
|
-
return {
|
|
287
|
-
append,
|
|
288
|
-
messages,
|
|
289
|
-
setMessages,
|
|
290
|
-
threadId: readonly(currentThreadId),
|
|
291
|
-
setThreadId: setCurrentThreadId,
|
|
292
|
-
input,
|
|
293
|
-
handleInputChange,
|
|
294
|
-
handleSubmit: submitMessage,
|
|
295
|
-
isSending,
|
|
296
|
-
status,
|
|
297
|
-
error,
|
|
298
|
-
stop,
|
|
299
|
-
};
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
/**
|
|
303
|
-
* @deprecated Use `useAssistant` instead.
|
|
304
|
-
*/
|
|
305
|
-
export const experimental_useAssistant = useAssistant;
|