@ai-sdk/svelte 0.0.1
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/.eslintrc.js +4 -0
- package/.turbo/turbo-build.log +21 -0
- package/.turbo/turbo-clean.log +4 -0
- package/CHANGELOG.md +9 -0
- package/LICENSE +13 -0
- package/README.md +7 -0
- package/dist/index.d.mts +128 -0
- package/dist/index.d.ts +128 -0
- package/dist/index.js +544 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +519 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +64 -0
- package/src/index.ts +3 -0
- package/src/package.json +10 -0
- package/src/use-assistant.ts +251 -0
- package/src/use-chat.ts +376 -0
- package/src/use-completion.ts +171 -0
- package/tsconfig.json +9 -0
- package/tsup.config.ts +13 -0
- package/turbo.json +8 -0
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
JSONValue,
|
|
3
|
+
RequestOptions,
|
|
4
|
+
UseCompletionOptions,
|
|
5
|
+
} from '@ai-sdk/ui-utils';
|
|
6
|
+
import { callCompletionApi } from '@ai-sdk/ui-utils';
|
|
7
|
+
import { useSWR } from 'sswr';
|
|
8
|
+
import { Readable, Writable, derived, get, writable } from 'svelte/store';
|
|
9
|
+
|
|
10
|
+
export type { UseCompletionOptions };
|
|
11
|
+
|
|
12
|
+
export type UseCompletionHelpers = {
|
|
13
|
+
/** The current completion result */
|
|
14
|
+
completion: Readable<string>;
|
|
15
|
+
/** The error object of the API request */
|
|
16
|
+
error: Readable<undefined | Error>;
|
|
17
|
+
/**
|
|
18
|
+
* Send a new prompt to the API endpoint and update the completion state.
|
|
19
|
+
*/
|
|
20
|
+
complete: (
|
|
21
|
+
prompt: string,
|
|
22
|
+
options?: RequestOptions,
|
|
23
|
+
) => Promise<string | null | undefined>;
|
|
24
|
+
/**
|
|
25
|
+
* Abort the current API request but keep the generated tokens.
|
|
26
|
+
*/
|
|
27
|
+
stop: () => void;
|
|
28
|
+
/**
|
|
29
|
+
* Update the `completion` state locally.
|
|
30
|
+
*/
|
|
31
|
+
setCompletion: (completion: string) => void;
|
|
32
|
+
/** The current value of the input */
|
|
33
|
+
input: Writable<string>;
|
|
34
|
+
/**
|
|
35
|
+
* Form submission handler to automatically reset input and append a user message
|
|
36
|
+
* @example
|
|
37
|
+
* ```jsx
|
|
38
|
+
* <form onSubmit={handleSubmit}>
|
|
39
|
+
* <input onChange={handleInputChange} value={input} />
|
|
40
|
+
* </form>
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
handleSubmit: (e: any) => void;
|
|
44
|
+
/** Whether the API request is in progress */
|
|
45
|
+
isLoading: Readable<boolean | undefined>;
|
|
46
|
+
|
|
47
|
+
/** Additional data added on the server via StreamData */
|
|
48
|
+
data: Readable<JSONValue[] | undefined>;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
let uniqueId = 0;
|
|
52
|
+
|
|
53
|
+
const store: Record<string, any> = {};
|
|
54
|
+
|
|
55
|
+
export function useCompletion({
|
|
56
|
+
api = '/api/completion',
|
|
57
|
+
id,
|
|
58
|
+
initialCompletion = '',
|
|
59
|
+
initialInput = '',
|
|
60
|
+
credentials,
|
|
61
|
+
headers,
|
|
62
|
+
body,
|
|
63
|
+
streamMode,
|
|
64
|
+
onResponse,
|
|
65
|
+
onFinish,
|
|
66
|
+
onError,
|
|
67
|
+
}: UseCompletionOptions = {}): UseCompletionHelpers {
|
|
68
|
+
// Generate an unique id for the completion if not provided.
|
|
69
|
+
const completionId = id || `completion-${uniqueId++}`;
|
|
70
|
+
|
|
71
|
+
const key = `${api}|${completionId}`;
|
|
72
|
+
const {
|
|
73
|
+
data,
|
|
74
|
+
mutate: originalMutate,
|
|
75
|
+
isLoading: isSWRLoading,
|
|
76
|
+
} = useSWR<string>(key, {
|
|
77
|
+
fetcher: () => store[key] || initialCompletion,
|
|
78
|
+
fallbackData: initialCompletion,
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
const streamData = writable<JSONValue[] | undefined>(undefined);
|
|
82
|
+
|
|
83
|
+
const loading = writable<boolean>(false);
|
|
84
|
+
|
|
85
|
+
// Force the `data` to be `initialCompletion` if it's `undefined`.
|
|
86
|
+
data.set(initialCompletion);
|
|
87
|
+
|
|
88
|
+
const mutate = (data: string) => {
|
|
89
|
+
store[key] = data;
|
|
90
|
+
return originalMutate(data);
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
// Because of the `fallbackData` option, the `data` will never be `undefined`.
|
|
94
|
+
const completion = data as Writable<string>;
|
|
95
|
+
|
|
96
|
+
const error = writable<undefined | Error>(undefined);
|
|
97
|
+
|
|
98
|
+
let abortController: AbortController | null = null;
|
|
99
|
+
|
|
100
|
+
const complete: UseCompletionHelpers['complete'] = async (
|
|
101
|
+
prompt: string,
|
|
102
|
+
options?: RequestOptions,
|
|
103
|
+
) => {
|
|
104
|
+
const existingData = get(streamData);
|
|
105
|
+
return callCompletionApi({
|
|
106
|
+
api,
|
|
107
|
+
prompt,
|
|
108
|
+
credentials,
|
|
109
|
+
headers: {
|
|
110
|
+
...headers,
|
|
111
|
+
...options?.headers,
|
|
112
|
+
},
|
|
113
|
+
body: {
|
|
114
|
+
...body,
|
|
115
|
+
...options?.body,
|
|
116
|
+
},
|
|
117
|
+
streamMode,
|
|
118
|
+
setCompletion: mutate,
|
|
119
|
+
setLoading: loadingState => loading.set(loadingState),
|
|
120
|
+
setError: err => error.set(err),
|
|
121
|
+
setAbortController: controller => {
|
|
122
|
+
abortController = controller;
|
|
123
|
+
},
|
|
124
|
+
onResponse,
|
|
125
|
+
onFinish,
|
|
126
|
+
onError,
|
|
127
|
+
onData(data) {
|
|
128
|
+
streamData.set([...(existingData || []), ...(data || [])]);
|
|
129
|
+
},
|
|
130
|
+
});
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
const stop = () => {
|
|
134
|
+
if (abortController) {
|
|
135
|
+
abortController.abort();
|
|
136
|
+
abortController = null;
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
const setCompletion = (completion: string) => {
|
|
141
|
+
mutate(completion);
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
const input = writable(initialInput);
|
|
145
|
+
|
|
146
|
+
const handleSubmit = (e: any) => {
|
|
147
|
+
e.preventDefault();
|
|
148
|
+
const inputValue = get(input);
|
|
149
|
+
if (!inputValue) return;
|
|
150
|
+
return complete(inputValue);
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
const isLoading = derived(
|
|
154
|
+
[isSWRLoading, loading],
|
|
155
|
+
([$isSWRLoading, $loading]) => {
|
|
156
|
+
return $isSWRLoading || $loading;
|
|
157
|
+
},
|
|
158
|
+
);
|
|
159
|
+
|
|
160
|
+
return {
|
|
161
|
+
completion,
|
|
162
|
+
complete,
|
|
163
|
+
error,
|
|
164
|
+
stop,
|
|
165
|
+
setCompletion,
|
|
166
|
+
input,
|
|
167
|
+
handleSubmit,
|
|
168
|
+
isLoading,
|
|
169
|
+
data: streamData,
|
|
170
|
+
};
|
|
171
|
+
}
|
package/tsconfig.json
ADDED
package/tsup.config.ts
ADDED