@ai-sdk/svelte 0.0.0-02dba89b-20251009204516

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.
Files changed (42) hide show
  1. package/CHANGELOG.md +2425 -0
  2. package/LICENSE +13 -0
  3. package/README.md +9 -0
  4. package/dist/chat.svelte.d.ts +6 -0
  5. package/dist/chat.svelte.d.ts.map +1 -0
  6. package/dist/chat.svelte.js +30 -0
  7. package/dist/completion-context.svelte.js +14 -0
  8. package/dist/completion.svelte.d.ts +30 -0
  9. package/dist/completion.svelte.d.ts.map +1 -0
  10. package/dist/completion.svelte.js +93 -0
  11. package/dist/context-provider.d.ts +2 -0
  12. package/dist/context-provider.d.ts.map +1 -0
  13. package/dist/context-provider.js +8 -0
  14. package/dist/index.d.ts +5 -0
  15. package/dist/index.d.ts.map +1 -0
  16. package/dist/index.js +4 -0
  17. package/dist/structured-object-context.svelte.d.ts +12 -0
  18. package/dist/structured-object-context.svelte.d.ts.map +1 -0
  19. package/dist/structured-object-context.svelte.js +12 -0
  20. package/dist/structured-object.svelte.d.ts +84 -0
  21. package/dist/structured-object.svelte.d.ts.map +1 -0
  22. package/dist/structured-object.svelte.js +134 -0
  23. package/dist/tests/completion-synchronization.svelte +12 -0
  24. package/dist/tests/completion-synchronization.svelte.d.ts +11 -0
  25. package/dist/tests/completion-synchronization.svelte.d.ts.map +1 -0
  26. package/dist/tests/structured-object-synchronization.svelte +22 -0
  27. package/dist/tests/structured-object-synchronization.svelte.d.ts +28 -0
  28. package/dist/tests/structured-object-synchronization.svelte.d.ts.map +1 -0
  29. package/dist/utils.svelte.d.ts +17 -0
  30. package/dist/utils.svelte.d.ts.map +1 -0
  31. package/dist/utils.svelte.js +50 -0
  32. package/package.json +91 -0
  33. package/src/chat.svelte.ts +51 -0
  34. package/src/completion-context.svelte.ts +24 -0
  35. package/src/completion.svelte.ts +116 -0
  36. package/src/context-provider.ts +16 -0
  37. package/src/index.ts +7 -0
  38. package/src/structured-object-context.svelte.ts +27 -0
  39. package/src/structured-object.svelte.ts +253 -0
  40. package/src/tests/completion-synchronization.svelte +12 -0
  41. package/src/tests/structured-object-synchronization.svelte +22 -0
  42. package/src/utils.svelte.ts +66 -0
@@ -0,0 +1,50 @@
1
+ import { getContext, hasContext, setContext, untrack } from 'svelte';
2
+ import { SvelteMap } from 'svelte/reactivity';
3
+ export function createContext(name) {
4
+ const key = Symbol(name);
5
+ return {
6
+ hasContext: () => {
7
+ // At the time of writing there's no way to determine if we're
8
+ // currently initializing a component without a try-catch
9
+ try {
10
+ return hasContext(key);
11
+ }
12
+ catch (e) {
13
+ if (typeof e === 'object' &&
14
+ e !== null &&
15
+ 'message' in e &&
16
+ typeof e.message === 'string' &&
17
+ e.message?.includes('lifecycle_outside_component')) {
18
+ return false;
19
+ }
20
+ throw e;
21
+ }
22
+ },
23
+ getContext: () => getContext(key),
24
+ setContext: (value) => setContext(key, value),
25
+ };
26
+ }
27
+ export function promiseWithResolvers() {
28
+ let resolve;
29
+ let reject;
30
+ const promise = new Promise((res, rej) => {
31
+ resolve = res;
32
+ reject = rej;
33
+ });
34
+ return { promise, resolve: resolve, reject: reject };
35
+ }
36
+ export class KeyedStore extends SvelteMap {
37
+ #itemConstructor;
38
+ constructor(itemConstructor, value) {
39
+ super(value);
40
+ this.#itemConstructor = itemConstructor;
41
+ }
42
+ get(key) {
43
+ const test = super.get(key) ??
44
+ // Untrack here because this is technically a state mutation, meaning
45
+ // deriveds downstream would fail. Because this is idempotent (even
46
+ // though it's not pure), it's safe.
47
+ untrack(() => this.set(key, new this.#itemConstructor())).get(key);
48
+ return test;
49
+ }
50
+ }
package/package.json ADDED
@@ -0,0 +1,91 @@
1
+ {
2
+ "name": "@ai-sdk/svelte",
3
+ "version": "0.0.0-02dba89b-20251009204516",
4
+ "license": "Apache-2.0",
5
+ "files": [
6
+ "dist",
7
+ "!dist/**/*.test.*",
8
+ "!dist/**/*.spec.*",
9
+ "src",
10
+ "!src/**/*.test.*",
11
+ "!src/**/*.spec.*",
12
+ "CHANGELOG.md"
13
+ ],
14
+ "sideEffects": [
15
+ "**/*.css"
16
+ ],
17
+ "svelte": "./dist/index.js",
18
+ "types": "./dist/index.d.ts",
19
+ "type": "module",
20
+ "exports": {
21
+ ".": {
22
+ "types": "./dist/index.d.ts",
23
+ "svelte": "./dist/index.js"
24
+ }
25
+ },
26
+ "publishConfig": {
27
+ "access": "public"
28
+ },
29
+ "peerDependencies": {
30
+ "svelte": "^5.31.0",
31
+ "zod": "^3.25.76 || ^4.1.8"
32
+ },
33
+ "peerDependenciesMeta": {
34
+ "zod": {
35
+ "optional": true
36
+ }
37
+ },
38
+ "dependencies": {
39
+ "ai": "0.0.0-02dba89b-20251009204516",
40
+ "@ai-sdk/provider-utils": "3.0.11"
41
+ },
42
+ "devDependencies": {
43
+ "@types/node": "20.17.24",
44
+ "@eslint/compat": "^1.2.5",
45
+ "@eslint/js": "^9.18.0",
46
+ "@sveltejs/package": "^2.0.0",
47
+ "@sveltejs/vite-plugin-svelte": "^5.0.3",
48
+ "@testing-library/jest-dom": "^6.6.3",
49
+ "@testing-library/svelte": "^5.2.4",
50
+ "eslint": "^9.18.0",
51
+ "eslint-plugin-svelte": "^3.0.0",
52
+ "globals": "^16.0.0",
53
+ "jsdom": "^26.0.0",
54
+ "publint": "^0.3.2",
55
+ "svelte": "^5.31.0",
56
+ "svelte-check": "^4.0.0",
57
+ "typescript": "^5.0.0",
58
+ "typescript-eslint": "^8.20.0",
59
+ "vite": "^6.0.0",
60
+ "vitest": "^3.0.0",
61
+ "zod": "3.25.76",
62
+ "@vercel/ai-tsconfig": "0.0.0"
63
+ },
64
+ "homepage": "https://ai-sdk.dev/docs",
65
+ "repository": {
66
+ "type": "git",
67
+ "url": "git+https://github.com/vercel/ai.git",
68
+ "directory": "packages/svelte"
69
+ },
70
+ "bugs": {
71
+ "url": "https://github.com/vercel/ai/issues"
72
+ },
73
+ "keywords": [
74
+ "ai",
75
+ "svelte"
76
+ ],
77
+ "scripts": {
78
+ "build": "pnpm prepack",
79
+ "build:watch": "pnpm clean && pnpm --filter svelte build && svelte-package --input=src --watch",
80
+ "preview": "vite preview",
81
+ "type-check": "svelte-check --tsconfig ./tsconfig.json",
82
+ "type-check:watch": "svelte-check --tsconfig ./tsconfig.json --watch",
83
+ "prettier-fix": "prettier --write .",
84
+ "prettier-check": "prettier --check .",
85
+ "lint": "eslint .",
86
+ "test": "vitest --run",
87
+ "test:update": "vitest --run -u",
88
+ "test:watch": "vitest",
89
+ "clean": "rm -rf dist *.tsbuildinfo"
90
+ }
91
+ }
@@ -0,0 +1,51 @@
1
+ import {
2
+ AbstractChat,
3
+ type ChatInit,
4
+ type ChatState,
5
+ type ChatStatus,
6
+ type CreateUIMessage,
7
+ type UIMessage,
8
+ } from 'ai';
9
+
10
+ export type { CreateUIMessage, UIMessage };
11
+
12
+ export class Chat<
13
+ UI_MESSAGE extends UIMessage = UIMessage,
14
+ > extends AbstractChat<UI_MESSAGE> {
15
+ constructor(init: ChatInit<UI_MESSAGE>) {
16
+ super({
17
+ ...init,
18
+ state: new SvelteChatState(init.messages),
19
+ });
20
+ }
21
+ }
22
+
23
+ class SvelteChatState<UI_MESSAGE extends UIMessage>
24
+ implements ChatState<UI_MESSAGE>
25
+ {
26
+ messages: UI_MESSAGE[];
27
+ status = $state<ChatStatus>('ready');
28
+ error = $state<Error | undefined>(undefined);
29
+
30
+ constructor(messages: UI_MESSAGE[] = []) {
31
+ this.messages = $state(messages);
32
+ }
33
+
34
+ setMessages = (messages: UI_MESSAGE[]) => {
35
+ this.messages = messages;
36
+ };
37
+
38
+ pushMessage = (message: UI_MESSAGE) => {
39
+ this.messages.push(message);
40
+ };
41
+
42
+ popMessage = () => {
43
+ this.messages.pop();
44
+ };
45
+
46
+ replaceMessage = (index: number, message: UI_MESSAGE) => {
47
+ this.messages[index] = message;
48
+ };
49
+
50
+ snapshot = <T>(thing: T): T => $state.snapshot(thing) as T;
51
+ }
@@ -0,0 +1,24 @@
1
+ import type { JSONValue } from 'ai';
2
+ import { SvelteMap } from 'svelte/reactivity';
3
+ import { createContext, KeyedStore } from './utils.svelte.js';
4
+
5
+ class CompletionStore {
6
+ completions = new SvelteMap<string, string>();
7
+ data = $state<JSONValue[]>([]);
8
+ loading = $state(false);
9
+ error = $state<Error>();
10
+ }
11
+
12
+ export class KeyedCompletionStore extends KeyedStore<CompletionStore> {
13
+ constructor(
14
+ value?: Iterable<readonly [string, CompletionStore]> | null | undefined,
15
+ ) {
16
+ super(CompletionStore, value);
17
+ }
18
+ }
19
+
20
+ export const {
21
+ hasContext: hasCompletionContext,
22
+ getContext: getCompletionContext,
23
+ setContext: setCompletionContext,
24
+ } = createContext<KeyedCompletionStore>('Completion');
@@ -0,0 +1,116 @@
1
+ import {
2
+ callCompletionApi,
3
+ generateId,
4
+ type CompletionRequestOptions,
5
+ type UseCompletionOptions,
6
+ } from 'ai';
7
+ import {
8
+ KeyedCompletionStore,
9
+ getCompletionContext,
10
+ hasCompletionContext,
11
+ } from './completion-context.svelte.js';
12
+
13
+ export type CompletionOptions = Readonly<UseCompletionOptions>;
14
+
15
+ export class Completion {
16
+ readonly #options: CompletionOptions = {};
17
+ readonly #api = $derived(this.#options.api ?? '/api/completion');
18
+ readonly #id = $derived(this.#options.id ?? generateId());
19
+ readonly #streamProtocol = $derived(this.#options.streamProtocol ?? 'data');
20
+ readonly #keyedStore = $state<KeyedCompletionStore>()!;
21
+ readonly #store = $derived(this.#keyedStore.get(this.#id));
22
+ #abortController: AbortController | undefined;
23
+
24
+ /** The current completion result */
25
+ get completion(): string {
26
+ return this.#store.completions.get(this.#id) ?? '';
27
+ }
28
+ set completion(value: string) {
29
+ this.#store.completions.set(this.#id, value);
30
+ }
31
+
32
+ /** The error object of the API request */
33
+ get error() {
34
+ return this.#store.error;
35
+ }
36
+
37
+ /** The current value of the input. Writable, so it can be bound to form inputs. */
38
+ input = $state<string>()!;
39
+
40
+ /**
41
+ * Flag that indicates whether an API request is in progress.
42
+ */
43
+ get loading() {
44
+ return this.#store.loading;
45
+ }
46
+
47
+ constructor(options: CompletionOptions = {}) {
48
+ this.#keyedStore = hasCompletionContext()
49
+ ? getCompletionContext()
50
+ : new KeyedCompletionStore();
51
+ this.#options = options;
52
+ this.completion = options.initialCompletion ?? '';
53
+ this.input = options.initialInput ?? '';
54
+ }
55
+
56
+ /**
57
+ * Abort the current request immediately, keep the generated tokens if any.
58
+ */
59
+ stop = () => {
60
+ try {
61
+ this.#abortController?.abort();
62
+ } catch {
63
+ // ignore
64
+ } finally {
65
+ this.#store.loading = false;
66
+ this.#abortController = undefined;
67
+ }
68
+ };
69
+
70
+ /**
71
+ * Send a new prompt to the API endpoint and update the completion state.
72
+ */
73
+ complete = async (prompt: string, options?: CompletionRequestOptions) =>
74
+ this.#triggerRequest(prompt, options);
75
+
76
+ /** Form submission handler to automatically reset input and call the completion API */
77
+ handleSubmit = async (event?: { preventDefault?: () => void }) => {
78
+ event?.preventDefault?.();
79
+ if (this.input) {
80
+ await this.complete(this.input);
81
+ }
82
+ };
83
+
84
+ #triggerRequest = async (
85
+ prompt: string,
86
+ options?: CompletionRequestOptions,
87
+ ) => {
88
+ return callCompletionApi({
89
+ api: this.#api,
90
+ prompt,
91
+ credentials: this.#options.credentials,
92
+ headers: { ...this.#options.headers, ...options?.headers },
93
+ body: {
94
+ ...this.#options.body,
95
+ ...options?.body,
96
+ },
97
+ streamProtocol: this.#streamProtocol,
98
+ fetch: this.#options.fetch,
99
+ // throttle streamed ui updates:
100
+ setCompletion: completion => {
101
+ this.completion = completion;
102
+ },
103
+ setLoading: loading => {
104
+ this.#store.loading = loading;
105
+ },
106
+ setError: error => {
107
+ this.#store.error = error;
108
+ },
109
+ setAbortController: abortController => {
110
+ this.#abortController = abortController ?? undefined;
111
+ },
112
+ onFinish: this.#options.onFinish,
113
+ onError: this.#options.onError,
114
+ });
115
+ };
116
+ }
@@ -0,0 +1,16 @@
1
+ import {
2
+ KeyedCompletionStore,
3
+ setCompletionContext,
4
+ } from './completion-context.svelte.js';
5
+ import {
6
+ KeyedStructuredObjectStore,
7
+ setStructuredObjectContext,
8
+ } from './structured-object-context.svelte.js';
9
+
10
+ export function createAIContext() {
11
+ const completionStore = new KeyedCompletionStore();
12
+ setCompletionContext(completionStore);
13
+
14
+ const objectStore = new KeyedStructuredObjectStore();
15
+ setStructuredObjectContext(objectStore);
16
+ }
package/src/index.ts ADDED
@@ -0,0 +1,7 @@
1
+ export { Chat, type CreateUIMessage, type UIMessage } from './chat.svelte.js';
2
+ export { Completion, type CompletionOptions } from './completion.svelte.js';
3
+ export { createAIContext } from './context-provider.js';
4
+ export {
5
+ StructuredObject as Experimental_StructuredObject,
6
+ type Experimental_StructuredObjectOptions,
7
+ } from './structured-object.svelte.js';
@@ -0,0 +1,27 @@
1
+ import type { DeepPartial } from 'ai';
2
+ import { createContext, KeyedStore } from './utils.svelte.js';
3
+
4
+ export class StructuredObjectStore<RESULT> {
5
+ object = $state<DeepPartial<RESULT>>();
6
+ loading = $state(false);
7
+ error = $state<Error>();
8
+ }
9
+
10
+ export class KeyedStructuredObjectStore extends KeyedStore<
11
+ StructuredObjectStore<unknown>
12
+ > {
13
+ constructor(
14
+ value?:
15
+ | Iterable<readonly [string, StructuredObjectStore<unknown>]>
16
+ | null
17
+ | undefined,
18
+ ) {
19
+ super(StructuredObjectStore, value);
20
+ }
21
+ }
22
+
23
+ export const {
24
+ hasContext: hasStructuredObjectContext,
25
+ getContext: getStructuredObjectContext,
26
+ setContext: setStructuredObjectContext,
27
+ } = createContext<KeyedStructuredObjectStore>('StructuredObject');
@@ -0,0 +1,253 @@
1
+ import {
2
+ generateId,
3
+ isAbortError,
4
+ safeValidateTypes,
5
+ type FetchFunction,
6
+ type InferSchema,
7
+ } from '@ai-sdk/provider-utils';
8
+ import {
9
+ asSchema,
10
+ isDeepEqualData,
11
+ parsePartialJson,
12
+ type DeepPartial,
13
+ type Schema,
14
+ } from 'ai';
15
+ import type * as z3 from 'zod/v3';
16
+ import type * as z4 from 'zod/v4';
17
+ import {
18
+ getStructuredObjectContext,
19
+ hasStructuredObjectContext,
20
+ KeyedStructuredObjectStore,
21
+ type StructuredObjectStore,
22
+ } from './structured-object-context.svelte.js';
23
+
24
+ export type Experimental_StructuredObjectOptions<
25
+ SCHEMA extends z3.Schema | z4.core.$ZodType | Schema,
26
+ RESULT = InferSchema<SCHEMA>,
27
+ > = {
28
+ /**
29
+ * The API endpoint. It should stream JSON that matches the schema as chunked text.
30
+ */
31
+ api: string;
32
+
33
+ /**
34
+ * A Zod schema that defines the shape of the complete object.
35
+ */
36
+ schema: SCHEMA;
37
+
38
+ /**
39
+ * An unique identifier. If not provided, a random one will be
40
+ * generated. When provided, the `useObject` hook with the same `id` will
41
+ * have shared states across components.
42
+ */
43
+ id?: string;
44
+
45
+ /**
46
+ * An optional value for the initial object.
47
+ */
48
+ initialValue?: DeepPartial<RESULT>;
49
+
50
+ /**
51
+ * Custom fetch implementation. You can use it as a middleware to intercept requests,
52
+ * or to provide a custom fetch implementation for e.g. testing.
53
+ */
54
+ fetch?: FetchFunction;
55
+
56
+ /**
57
+ * Callback that is called when the stream has finished.
58
+ */
59
+ onFinish?: (event: {
60
+ /**
61
+ * The generated object (typed according to the schema).
62
+ * Can be undefined if the final object does not match the schema.
63
+ */
64
+ object: RESULT | undefined;
65
+
66
+ /**
67
+ * Optional error object. This is e.g. a TypeValidationError when the final object does not match the schema.
68
+ */
69
+ error: Error | undefined;
70
+ }) => Promise<void> | void;
71
+
72
+ /**
73
+ * Callback function to be called when an error is encountered.
74
+ */
75
+ onError?: (error: Error) => void;
76
+
77
+ /**
78
+ * Additional HTTP headers to be included in the request.
79
+ */
80
+ headers?: Record<string, string> | Headers;
81
+
82
+ /**
83
+ * The credentials mode to be used for the fetch request.
84
+ * Possible values are: 'omit', 'same-origin', 'include'.
85
+ * Defaults to 'same-origin'.
86
+ */
87
+ credentials?: RequestCredentials;
88
+ };
89
+
90
+ export class StructuredObject<
91
+ SCHEMA extends z3.Schema | z4.core.$ZodType | Schema,
92
+ RESULT = InferSchema<SCHEMA>,
93
+ INPUT = unknown,
94
+ > {
95
+ #options: Experimental_StructuredObjectOptions<SCHEMA, RESULT> =
96
+ {} as Experimental_StructuredObjectOptions<SCHEMA, RESULT>;
97
+ readonly #id = $derived(this.#options.id ?? generateId());
98
+ readonly #keyedStore = $state<KeyedStructuredObjectStore>()!;
99
+ readonly #store = $derived(
100
+ this.#keyedStore.get(this.#id),
101
+ ) as StructuredObjectStore<RESULT>;
102
+ #abortController: AbortController | undefined;
103
+
104
+ /**
105
+ * The current value for the generated object. Updated as the API streams JSON chunks.
106
+ */
107
+ get object(): DeepPartial<RESULT> | undefined {
108
+ return this.#store.object;
109
+ }
110
+ set #object(value: DeepPartial<RESULT> | undefined) {
111
+ this.#store.object = value;
112
+ }
113
+
114
+ /** The error object of the API request */
115
+ get error() {
116
+ return this.#store.error;
117
+ }
118
+
119
+ /**
120
+ * Flag that indicates whether an API request is in progress.
121
+ */
122
+ get loading() {
123
+ return this.#store.loading;
124
+ }
125
+
126
+ constructor(options: Experimental_StructuredObjectOptions<SCHEMA, RESULT>) {
127
+ if (hasStructuredObjectContext()) {
128
+ this.#keyedStore = getStructuredObjectContext();
129
+ } else {
130
+ this.#keyedStore = new KeyedStructuredObjectStore();
131
+ }
132
+ this.#options = options;
133
+ this.#object = options.initialValue;
134
+ }
135
+
136
+ /**
137
+ * Abort the current request immediately, keep the current partial object if any.
138
+ */
139
+ stop = () => {
140
+ try {
141
+ this.#abortController?.abort();
142
+ } catch {
143
+ // ignore
144
+ } finally {
145
+ this.#store.loading = false;
146
+ this.#abortController = undefined;
147
+ }
148
+ };
149
+
150
+ /**
151
+ * Calls the API with the provided input as JSON body.
152
+ */
153
+ submit = async (input: INPUT) => {
154
+ try {
155
+ this.#clearObject();
156
+
157
+ this.#store.loading = true;
158
+
159
+ const abortController = new AbortController();
160
+ this.#abortController = abortController;
161
+
162
+ const actualFetch = this.#options.fetch ?? fetch;
163
+ const response = await actualFetch(this.#options.api, {
164
+ method: 'POST',
165
+ headers: {
166
+ 'Content-Type': 'application/json',
167
+ ...this.#options.headers,
168
+ },
169
+ credentials: this.#options.credentials,
170
+ signal: abortController.signal,
171
+ body: JSON.stringify(input),
172
+ });
173
+
174
+ if (!response.ok) {
175
+ throw new Error(
176
+ (await response.text()) ?? 'Failed to fetch the response.',
177
+ );
178
+ }
179
+
180
+ if (response.body == null) {
181
+ throw new Error('The response body is empty.');
182
+ }
183
+
184
+ let accumulatedText = '';
185
+ let latestObject: DeepPartial<RESULT> | undefined = undefined;
186
+
187
+ await response.body.pipeThrough(new TextDecoderStream()).pipeTo(
188
+ new WritableStream<string>({
189
+ write: async chunk => {
190
+ if (abortController?.signal.aborted) {
191
+ throw new DOMException('Stream aborted', 'AbortError');
192
+ }
193
+ accumulatedText += chunk;
194
+
195
+ const { value } = await parsePartialJson(accumulatedText);
196
+ const currentObject = value as DeepPartial<RESULT>;
197
+
198
+ if (!isDeepEqualData(latestObject, currentObject)) {
199
+ latestObject = currentObject;
200
+
201
+ this.#store.object = currentObject;
202
+ }
203
+ },
204
+
205
+ close: async () => {
206
+ this.#store.loading = false;
207
+ this.#abortController = undefined;
208
+
209
+ if (this.#options.onFinish != null) {
210
+ const validationResult = await safeValidateTypes({
211
+ value: latestObject,
212
+ schema: asSchema(this.#options.schema),
213
+ });
214
+
215
+ this.#options.onFinish(
216
+ validationResult.success
217
+ ? { object: validationResult.value, error: undefined }
218
+ : { object: undefined, error: validationResult.error },
219
+ );
220
+ }
221
+ },
222
+ }),
223
+ );
224
+ } catch (error) {
225
+ if (isAbortError(error)) {
226
+ return;
227
+ }
228
+
229
+ const coalescedError =
230
+ error instanceof Error ? error : new Error(String(error));
231
+ if (this.#options.onError) {
232
+ this.#options.onError(coalescedError);
233
+ }
234
+
235
+ this.#store.loading = false;
236
+ this.#store.error = coalescedError;
237
+ }
238
+ };
239
+
240
+ /**
241
+ * Clears the object state.
242
+ */
243
+ clear = () => {
244
+ this.stop();
245
+ this.#clearObject();
246
+ };
247
+
248
+ #clearObject = () => {
249
+ this.#store.object = undefined;
250
+ this.#store.error = undefined;
251
+ this.#store.loading = false;
252
+ };
253
+ }
@@ -0,0 +1,12 @@
1
+ <script lang="ts">
2
+ import { Completion } from '../completion.svelte.js';
3
+ import { createAIContext } from '../context-provider.js';
4
+
5
+ let { id }: { id?: string } = $props();
6
+
7
+ createAIContext();
8
+ const completion1 = new Completion({ id });
9
+ const completion2 = new Completion({ id });
10
+
11
+ export { completion1, completion2 };
12
+ </script>
@@ -0,0 +1,22 @@
1
+ <script lang="ts" generics="RESULT">
2
+ import { createAIContext } from '../context-provider.js';
3
+ import { StructuredObject } from '../structured-object.svelte.js';
4
+ import type { Schema } from 'ai';
5
+ import type { z } from 'zod';
6
+
7
+ let {
8
+ id,
9
+ api,
10
+ schema,
11
+ }: {
12
+ id?: string;
13
+ api: string;
14
+ schema: z.Schema<RESULT, z.ZodTypeDef, unknown> | Schema<RESULT>;
15
+ } = $props();
16
+
17
+ createAIContext();
18
+ const object1 = new StructuredObject({ id, api, schema });
19
+ const object2 = new StructuredObject({ id, api, schema });
20
+
21
+ export { object1, object2 };
22
+ </script>