@ai-sdk/svelte 0.0.0-9477ebb9-20250403064906
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 +936 -0
- package/LICENSE +13 -0
- package/README.md +9 -0
- package/dist/chat-context.svelte.d.ts +14 -0
- package/dist/chat-context.svelte.d.ts.map +1 -0
- package/dist/chat-context.svelte.js +13 -0
- package/dist/chat.svelte.d.ts +75 -0
- package/dist/chat.svelte.d.ts.map +1 -0
- package/dist/chat.svelte.js +250 -0
- package/dist/completion-context.svelte.d.ts +15 -0
- package/dist/completion-context.svelte.d.ts.map +1 -0
- package/dist/completion-context.svelte.js +14 -0
- package/dist/completion.svelte.d.ts +37 -0
- package/dist/completion.svelte.d.ts.map +1 -0
- package/dist/completion.svelte.js +111 -0
- package/dist/context-provider.d.ts +2 -0
- package/dist/context-provider.d.ts.map +1 -0
- package/dist/context-provider.js +11 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -0
- package/dist/structured-object-context.svelte.d.ts +12 -0
- package/dist/structured-object-context.svelte.d.ts.map +1 -0
- package/dist/structured-object-context.svelte.js +12 -0
- package/dist/structured-object.svelte.d.ts +79 -0
- package/dist/structured-object.svelte.d.ts.map +1 -0
- package/dist/structured-object.svelte.js +124 -0
- package/dist/tests/chat-synchronization.svelte +12 -0
- package/dist/tests/chat-synchronization.svelte.d.ts +11 -0
- package/dist/tests/chat-synchronization.svelte.d.ts.map +1 -0
- package/dist/tests/completion-synchronization.svelte +12 -0
- package/dist/tests/completion-synchronization.svelte.d.ts +11 -0
- package/dist/tests/completion-synchronization.svelte.d.ts.map +1 -0
- package/dist/tests/structured-object-synchronization.svelte +22 -0
- package/dist/tests/structured-object-synchronization.svelte.d.ts +28 -0
- package/dist/tests/structured-object-synchronization.svelte.d.ts.map +1 -0
- package/dist/utils.svelte.d.ts +17 -0
- package/dist/utils.svelte.d.ts.map +1 -0
- package/dist/utils.svelte.js +50 -0
- package/package.json +88 -0
- package/src/chat-context.svelte.ts +23 -0
- package/src/chat.svelte.ts +350 -0
- package/src/completion-context.svelte.ts +24 -0
- package/src/completion.svelte.ts +133 -0
- package/src/context-provider.ts +20 -0
- package/src/index.ts +16 -0
- package/src/structured-object-context.svelte.ts +27 -0
- package/src/structured-object.svelte.ts +230 -0
- package/src/tests/chat-synchronization.svelte +12 -0
- package/src/tests/completion-synchronization.svelte +12 -0
- package/src/tests/structured-object-synchronization.svelte +22 -0
- package/src/utils.svelte.ts +66 -0
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { type FetchFunction } from '@ai-sdk/provider-utils';
|
|
2
|
+
import { type DeepPartial, type Schema } from '@ai-sdk/ui-utils';
|
|
3
|
+
import { type z } from 'zod';
|
|
4
|
+
export type Experimental_StructuredObjectOptions<RESULT> = {
|
|
5
|
+
/**
|
|
6
|
+
* The API endpoint. It should stream JSON that matches the schema as chunked text.
|
|
7
|
+
*/
|
|
8
|
+
api: string;
|
|
9
|
+
/**
|
|
10
|
+
* A Zod schema that defines the shape of the complete object.
|
|
11
|
+
*/
|
|
12
|
+
schema: z.Schema<RESULT, z.ZodTypeDef, unknown> | Schema<RESULT>;
|
|
13
|
+
/**
|
|
14
|
+
* An unique identifier. If not provided, a random one will be
|
|
15
|
+
* generated. When provided, the `useObject` hook with the same `id` will
|
|
16
|
+
* have shared states across components.
|
|
17
|
+
*/
|
|
18
|
+
id?: string;
|
|
19
|
+
/**
|
|
20
|
+
* An optional value for the initial object.
|
|
21
|
+
*/
|
|
22
|
+
initialValue?: DeepPartial<RESULT>;
|
|
23
|
+
/**
|
|
24
|
+
* Custom fetch implementation. You can use it as a middleware to intercept requests,
|
|
25
|
+
* or to provide a custom fetch implementation for e.g. testing.
|
|
26
|
+
*/
|
|
27
|
+
fetch?: FetchFunction;
|
|
28
|
+
/**
|
|
29
|
+
* Callback that is called when the stream has finished.
|
|
30
|
+
*/
|
|
31
|
+
onFinish?: (event: {
|
|
32
|
+
/**
|
|
33
|
+
* The generated object (typed according to the schema).
|
|
34
|
+
* Can be undefined if the final object does not match the schema.
|
|
35
|
+
*/
|
|
36
|
+
object: RESULT | undefined;
|
|
37
|
+
/**
|
|
38
|
+
* Optional error object. This is e.g. a TypeValidationError when the final object does not match the schema.
|
|
39
|
+
*/
|
|
40
|
+
error: Error | undefined;
|
|
41
|
+
}) => Promise<void> | void;
|
|
42
|
+
/**
|
|
43
|
+
* Callback function to be called when an error is encountered.
|
|
44
|
+
*/
|
|
45
|
+
onError?: (error: Error) => void;
|
|
46
|
+
/**
|
|
47
|
+
* Additional HTTP headers to be included in the request.
|
|
48
|
+
*/
|
|
49
|
+
headers?: Record<string, string> | Headers;
|
|
50
|
+
/**
|
|
51
|
+
* The credentials mode to be used for the fetch request.
|
|
52
|
+
* Possible values are: 'omit', 'same-origin', 'include'.
|
|
53
|
+
* Defaults to 'same-origin'.
|
|
54
|
+
*/
|
|
55
|
+
credentials?: RequestCredentials;
|
|
56
|
+
};
|
|
57
|
+
export declare class StructuredObject<RESULT, INPUT = unknown> {
|
|
58
|
+
#private;
|
|
59
|
+
/**
|
|
60
|
+
* The current value for the generated object. Updated as the API streams JSON chunks.
|
|
61
|
+
*/
|
|
62
|
+
get object(): DeepPartial<RESULT> | undefined;
|
|
63
|
+
/** The error object of the API request */
|
|
64
|
+
get error(): Error | undefined;
|
|
65
|
+
/**
|
|
66
|
+
* Flag that indicates whether an API request is in progress.
|
|
67
|
+
*/
|
|
68
|
+
get loading(): boolean;
|
|
69
|
+
constructor(options: Experimental_StructuredObjectOptions<RESULT>);
|
|
70
|
+
/**
|
|
71
|
+
* Abort the current request immediately, keep the current partial object if any.
|
|
72
|
+
*/
|
|
73
|
+
stop: () => void;
|
|
74
|
+
/**
|
|
75
|
+
* Calls the API with the provided input as JSON body.
|
|
76
|
+
*/
|
|
77
|
+
submit: (input: INPUT) => Promise<void>;
|
|
78
|
+
}
|
|
79
|
+
//# sourceMappingURL=structured-object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AUnD;;OAEG;IACH,IAAI,MAAM,IAAI,WAAW,CAAC,MAAM,CAAC,GAAG,SAAS,CAE5C;IAKD,0CAA0C;IAC1C,IAAI,KAAK,sBAER;IAED;;OAEG;IACH,IAAI,OAAO,YAEV;gBAEW,OAAO,EAAE,oCAAoC,CAAC,MAAM,CAAC;IAUjE;;OAEG;IACH,IAAI,aASF;IAEF;;OAEG;IACH,MAAM,UAAiB,KAAK,mBAqF1B;CACH"}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { generateId, isAbortError, safeValidateTypes, } from '@ai-sdk/provider-utils';
|
|
2
|
+
import { asSchema, isDeepEqualData, parsePartialJson, } from '@ai-sdk/ui-utils';
|
|
3
|
+
import {} from 'zod';
|
|
4
|
+
import { getStructuredObjectContext, hasStructuredObjectContext, KeyedStructuredObjectStore, } from './structured-object-context.svelte.js';
|
|
5
|
+
export class StructuredObject {
|
|
6
|
+
#options = {};
|
|
7
|
+
#id = $derived(this.#options.id ?? generateId());
|
|
8
|
+
#keyedStore = $state();
|
|
9
|
+
#store = $derived(this.#keyedStore.get(this.#id));
|
|
10
|
+
#abortController;
|
|
11
|
+
/**
|
|
12
|
+
* The current value for the generated object. Updated as the API streams JSON chunks.
|
|
13
|
+
*/
|
|
14
|
+
get object() {
|
|
15
|
+
return this.#store.object;
|
|
16
|
+
}
|
|
17
|
+
set #object(value) {
|
|
18
|
+
this.#store.object = value;
|
|
19
|
+
}
|
|
20
|
+
/** The error object of the API request */
|
|
21
|
+
get error() {
|
|
22
|
+
return this.#store.error;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Flag that indicates whether an API request is in progress.
|
|
26
|
+
*/
|
|
27
|
+
get loading() {
|
|
28
|
+
return this.#store.loading;
|
|
29
|
+
}
|
|
30
|
+
constructor(options) {
|
|
31
|
+
if (hasStructuredObjectContext()) {
|
|
32
|
+
this.#keyedStore = getStructuredObjectContext();
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
this.#keyedStore = new KeyedStructuredObjectStore();
|
|
36
|
+
}
|
|
37
|
+
this.#options = options;
|
|
38
|
+
this.#object = options.initialValue;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Abort the current request immediately, keep the current partial object if any.
|
|
42
|
+
*/
|
|
43
|
+
stop = () => {
|
|
44
|
+
try {
|
|
45
|
+
this.#abortController?.abort();
|
|
46
|
+
}
|
|
47
|
+
catch {
|
|
48
|
+
// ignore
|
|
49
|
+
}
|
|
50
|
+
finally {
|
|
51
|
+
this.#store.loading = false;
|
|
52
|
+
this.#abortController = undefined;
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
/**
|
|
56
|
+
* Calls the API with the provided input as JSON body.
|
|
57
|
+
*/
|
|
58
|
+
submit = async (input) => {
|
|
59
|
+
try {
|
|
60
|
+
this.#store.object = undefined; // reset the data
|
|
61
|
+
this.#store.loading = true;
|
|
62
|
+
this.#store.error = undefined;
|
|
63
|
+
const abortController = new AbortController();
|
|
64
|
+
this.#abortController = abortController;
|
|
65
|
+
const actualFetch = this.#options.fetch ?? fetch;
|
|
66
|
+
const response = await actualFetch(this.#options.api, {
|
|
67
|
+
method: 'POST',
|
|
68
|
+
headers: {
|
|
69
|
+
'Content-Type': 'application/json',
|
|
70
|
+
...this.#options.headers,
|
|
71
|
+
},
|
|
72
|
+
credentials: this.#options.credentials,
|
|
73
|
+
signal: abortController.signal,
|
|
74
|
+
body: JSON.stringify(input),
|
|
75
|
+
});
|
|
76
|
+
if (!response.ok) {
|
|
77
|
+
throw new Error((await response.text()) ?? 'Failed to fetch the response.');
|
|
78
|
+
}
|
|
79
|
+
if (response.body == null) {
|
|
80
|
+
throw new Error('The response body is empty.');
|
|
81
|
+
}
|
|
82
|
+
let accumulatedText = '';
|
|
83
|
+
let latestObject = undefined;
|
|
84
|
+
await response.body.pipeThrough(new TextDecoderStream()).pipeTo(new WritableStream({
|
|
85
|
+
write: chunk => {
|
|
86
|
+
if (abortController?.signal.aborted) {
|
|
87
|
+
throw new DOMException('Stream aborted', 'AbortError');
|
|
88
|
+
}
|
|
89
|
+
accumulatedText += chunk;
|
|
90
|
+
const { value } = parsePartialJson(accumulatedText);
|
|
91
|
+
const currentObject = value;
|
|
92
|
+
if (!isDeepEqualData(latestObject, currentObject)) {
|
|
93
|
+
latestObject = currentObject;
|
|
94
|
+
this.#store.object = currentObject;
|
|
95
|
+
}
|
|
96
|
+
},
|
|
97
|
+
close: () => {
|
|
98
|
+
this.#store.loading = false;
|
|
99
|
+
this.#abortController = undefined;
|
|
100
|
+
if (this.#options.onFinish != null) {
|
|
101
|
+
const validationResult = safeValidateTypes({
|
|
102
|
+
value: latestObject,
|
|
103
|
+
schema: asSchema(this.#options.schema),
|
|
104
|
+
});
|
|
105
|
+
this.#options.onFinish(validationResult.success
|
|
106
|
+
? { object: validationResult.value, error: undefined }
|
|
107
|
+
: { object: undefined, error: validationResult.error });
|
|
108
|
+
}
|
|
109
|
+
},
|
|
110
|
+
}));
|
|
111
|
+
}
|
|
112
|
+
catch (error) {
|
|
113
|
+
if (isAbortError(error)) {
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
const coalescedError = error instanceof Error ? error : new Error(String(error));
|
|
117
|
+
if (this.#options.onError) {
|
|
118
|
+
this.#options.onError(coalescedError);
|
|
119
|
+
}
|
|
120
|
+
this.#store.loading = false;
|
|
121
|
+
this.#store.error = coalescedError;
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { Chat } from '../chat.svelte.js';
|
|
3
|
+
import { createAIContext } from '../context-provider.js';
|
|
4
|
+
|
|
5
|
+
let { id }: { id?: string } = $props();
|
|
6
|
+
|
|
7
|
+
createAIContext();
|
|
8
|
+
const chat1 = new Chat({ id });
|
|
9
|
+
const chat2 = new Chat({ id });
|
|
10
|
+
|
|
11
|
+
export { chat1, chat2 };
|
|
12
|
+
</script>
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Chat } from '../chat.svelte.js';
|
|
2
|
+
type $$ComponentProps = {
|
|
3
|
+
id?: string;
|
|
4
|
+
};
|
|
5
|
+
declare const ChatSynchronization: import("svelte").Component<$$ComponentProps, {
|
|
6
|
+
chat1: Chat;
|
|
7
|
+
chat2: Chat;
|
|
8
|
+
}, "">;
|
|
9
|
+
type ChatSynchronization = ReturnType<typeof ChatSynchronization>;
|
|
10
|
+
export default ChatSynchronization;
|
|
11
|
+
//# sourceMappingURL=chat-synchronization.svelte.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"chat-synchronization.svelte.d.ts","sourceRoot":"","sources":["../../src/tests/chat-synchronization.svelte.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,IAAI,EAAE,MAAM,mBAAmB,CAAC;AAGxC,KAAK,gBAAgB,GAAI;IAAE,EAAE,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC;AAgB1C,QAAA,MAAM,mBAAmB;;;MAAsC,CAAC;AAChE,KAAK,mBAAmB,GAAG,UAAU,CAAC,OAAO,mBAAmB,CAAC,CAAC;AAClE,eAAe,mBAAmB,CAAC"}
|
|
@@ -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,11 @@
|
|
|
1
|
+
import { Completion } from '../completion.svelte.js';
|
|
2
|
+
type $$ComponentProps = {
|
|
3
|
+
id?: string;
|
|
4
|
+
};
|
|
5
|
+
declare const CompletionSynchronization: import("svelte").Component<$$ComponentProps, {
|
|
6
|
+
completion1: Completion;
|
|
7
|
+
completion2: Completion;
|
|
8
|
+
}, "">;
|
|
9
|
+
type CompletionSynchronization = ReturnType<typeof CompletionSynchronization>;
|
|
10
|
+
export default CompletionSynchronization;
|
|
11
|
+
//# sourceMappingURL=completion-synchronization.svelte.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"completion-synchronization.svelte.d.ts","sourceRoot":"","sources":["../../src/tests/completion-synchronization.svelte.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AAGpD,KAAK,gBAAgB,GAAI;IAAE,EAAE,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC;AAgB1C,QAAA,MAAM,yBAAyB;;;MAAsC,CAAC;AACtE,KAAK,yBAAyB,GAAG,UAAU,CAAC,OAAO,yBAAyB,CAAC,CAAC;AAC9E,eAAe,yBAAyB,CAAC"}
|
|
@@ -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-sdk/ui-utils';
|
|
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>
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { StructuredObject } from '../structured-object.svelte.js';
|
|
2
|
+
import type { Schema } from '@ai-sdk/ui-utils';
|
|
3
|
+
import type { z } from 'zod';
|
|
4
|
+
declare class __sveltets_Render<RESULT> {
|
|
5
|
+
props(): {
|
|
6
|
+
id?: string;
|
|
7
|
+
api: string;
|
|
8
|
+
schema: z.ZodType<RESULT, z.ZodTypeDef, unknown> | Schema<RESULT>;
|
|
9
|
+
};
|
|
10
|
+
events(): {};
|
|
11
|
+
slots(): {};
|
|
12
|
+
bindings(): "";
|
|
13
|
+
exports(): {
|
|
14
|
+
object1: StructuredObject<RESULT, unknown>;
|
|
15
|
+
object2: StructuredObject<RESULT, unknown>;
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
interface $$IsomorphicComponent {
|
|
19
|
+
new <RESULT>(options: import('svelte').ComponentConstructorOptions<ReturnType<__sveltets_Render<RESULT>['props']>>): import('svelte').SvelteComponent<ReturnType<__sveltets_Render<RESULT>['props']>, ReturnType<__sveltets_Render<RESULT>['events']>, ReturnType<__sveltets_Render<RESULT>['slots']>> & {
|
|
20
|
+
$$bindings?: ReturnType<__sveltets_Render<RESULT>['bindings']>;
|
|
21
|
+
} & ReturnType<__sveltets_Render<RESULT>['exports']>;
|
|
22
|
+
<RESULT>(internal: unknown, props: ReturnType<__sveltets_Render<RESULT>['props']> & {}): ReturnType<__sveltets_Render<RESULT>['exports']>;
|
|
23
|
+
z_$$bindings?: ReturnType<__sveltets_Render<any>['bindings']>;
|
|
24
|
+
}
|
|
25
|
+
declare const StructuredObjectSynchronization: $$IsomorphicComponent;
|
|
26
|
+
type StructuredObjectSynchronization<RESULT> = InstanceType<typeof StructuredObjectSynchronization<RESULT>>;
|
|
27
|
+
export default StructuredObjectSynchronization;
|
|
28
|
+
//# sourceMappingURL=structured-object-synchronization.svelte.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"structured-object-synchronization.svelte.d.ts","sourceRoot":"","sources":["../../src/tests/structured-object-synchronization.svelte.ts"],"names":[],"mappings":"AAIA,O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zD,KAAK,+BAA+B,CAAC,MAAM,IAAI,YAAY,CAAC,OAAO,+BAA+B,CAAC,MAAM,CAAC,CAAC,CAAC;AAC9G,eAAe,+BAA+B,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { SvelteMap } from 'svelte/reactivity';
|
|
2
|
+
export declare function createContext<T>(name: string): {
|
|
3
|
+
hasContext: () => boolean;
|
|
4
|
+
getContext: () => T;
|
|
5
|
+
setContext: (value: T) => T;
|
|
6
|
+
};
|
|
7
|
+
export declare function promiseWithResolvers<T>(): {
|
|
8
|
+
promise: Promise<T>;
|
|
9
|
+
resolve: (value: T) => void;
|
|
10
|
+
reject: (reason?: unknown) => void;
|
|
11
|
+
};
|
|
12
|
+
export declare class KeyedStore<T> extends SvelteMap<string, T> {
|
|
13
|
+
#private;
|
|
14
|
+
constructor(itemConstructor: new () => T, value?: Iterable<readonly [string, T]> | null | undefined);
|
|
15
|
+
get(key: string): T;
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=utils.svelte.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.svelte.d.ts","sourceRoot":"","sources":["../src/utils.svelte.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAE9C,wBAAgB,aAAa,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM;;;wBAuBrB,CAAC;EAExB;AAED,wBAAgB,oBAAoB,CAAC,CAAC,KAAK;IACzC,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC;IACpB,OAAO,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,IAAI,CAAC;IAC5B,MAAM,EAAE,CAAC,MAAM,CAAC,EAAE,OAAO,KAAK,IAAI,CAAC;CACpC,CAQA;AAED,qBAAa,UAAU,CAAC,CAAC,CAAE,SAAQ,SAAS,CAAC,MAAM,EAAE,CAAC,CAAC;;gBAInD,eAAe,EAAE,UAAU,CAAC,EAC5B,KAAK,CAAC,EAAE,QAAQ,CAAC,SAAS,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,GAAG,IAAI,GAAG,SAAS;IAM3D,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,CAAC;CAUpB"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { hasContext, getContext, 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,88 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ai-sdk/svelte",
|
|
3
|
+
"version": "0.0.0-9477ebb9-20250403064906",
|
|
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.0.0",
|
|
31
|
+
"zod": "^3.23.8"
|
|
32
|
+
},
|
|
33
|
+
"peerDependenciesMeta": {
|
|
34
|
+
"zod": {
|
|
35
|
+
"optional": true
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"@ai-sdk/provider-utils": "2.2.3",
|
|
40
|
+
"@ai-sdk/ui-utils": "0.0.0-9477ebb9-20250403064906"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"@eslint/compat": "^1.2.5",
|
|
44
|
+
"@eslint/js": "^9.18.0",
|
|
45
|
+
"@sveltejs/package": "^2.0.0",
|
|
46
|
+
"@sveltejs/vite-plugin-svelte": "^5.0.3",
|
|
47
|
+
"@testing-library/jest-dom": "^6.6.3",
|
|
48
|
+
"@testing-library/svelte": "^5.2.4",
|
|
49
|
+
"eslint": "^9.18.0",
|
|
50
|
+
"eslint-plugin-svelte": "^3.0.0",
|
|
51
|
+
"globals": "^16.0.0",
|
|
52
|
+
"jsdom": "^26.0.0",
|
|
53
|
+
"publint": "^0.3.2",
|
|
54
|
+
"svelte": "^5.0.0",
|
|
55
|
+
"svelte-check": "^4.0.0",
|
|
56
|
+
"typescript": "^5.0.0",
|
|
57
|
+
"typescript-eslint": "^8.20.0",
|
|
58
|
+
"vite": "^6.0.0",
|
|
59
|
+
"vitest": "^3.0.0",
|
|
60
|
+
"zod": "3.23.8"
|
|
61
|
+
},
|
|
62
|
+
"homepage": "https://sdk.vercel.ai/docs",
|
|
63
|
+
"repository": {
|
|
64
|
+
"type": "git",
|
|
65
|
+
"url": "git+https://github.com/vercel/ai.git",
|
|
66
|
+
"directory": "packages/svelte"
|
|
67
|
+
},
|
|
68
|
+
"bugs": {
|
|
69
|
+
"url": "https://github.com/vercel/ai/issues"
|
|
70
|
+
},
|
|
71
|
+
"keywords": [
|
|
72
|
+
"ai",
|
|
73
|
+
"svelte"
|
|
74
|
+
],
|
|
75
|
+
"scripts": {
|
|
76
|
+
"build": "pnpm prepack",
|
|
77
|
+
"build:watch": "svelte-package --input=src --watch",
|
|
78
|
+
"preview": "vite preview",
|
|
79
|
+
"type-check": "svelte-check --tsconfig ./tsconfig.json",
|
|
80
|
+
"type-check:watch": "svelte-check --tsconfig ./tsconfig.json --watch",
|
|
81
|
+
"prettier-fix": "prettier --write .",
|
|
82
|
+
"prettier-check": "prettier --check .",
|
|
83
|
+
"lint": "eslint .",
|
|
84
|
+
"test": "vitest --run",
|
|
85
|
+
"test:watch": "vitest",
|
|
86
|
+
"clean": "rm -rf dist"
|
|
87
|
+
}
|
|
88
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { JSONValue, UIMessage } from '@ai-sdk/ui-utils';
|
|
2
|
+
import { createContext, KeyedStore } from './utils.svelte.js';
|
|
3
|
+
|
|
4
|
+
class ChatStore {
|
|
5
|
+
messages = $state<UIMessage[]>([]);
|
|
6
|
+
data = $state<JSONValue[]>();
|
|
7
|
+
status = $state<'submitted' | 'streaming' | 'ready' | 'error'>('ready');
|
|
8
|
+
error = $state<Error>();
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export class KeyedChatStore extends KeyedStore<ChatStore> {
|
|
12
|
+
constructor(
|
|
13
|
+
value?: Iterable<readonly [string, ChatStore]> | null | undefined,
|
|
14
|
+
) {
|
|
15
|
+
super(ChatStore, value);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export const {
|
|
20
|
+
hasContext: hasChatContext,
|
|
21
|
+
getContext: getChatContext,
|
|
22
|
+
setContext: setChatContext,
|
|
23
|
+
} = createContext<KeyedChatStore>('Chat');
|