@ai-sdk/svelte 1.1.23 → 2.0.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/CHANGELOG.md +13 -0
- package/README.md +5 -3
- 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 +244 -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 -166
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -551
- 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 +73 -0
- package/dist/structured-object.svelte.d.ts.map +1 -0
- package/dist/structured-object.svelte.js +123 -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 +56 -35
- package/src/chat-context.svelte.ts +23 -0
- package/src/chat.svelte.ts +341 -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 +222 -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
- package/dist/index.d.mts +0 -166
- package/dist/index.js.map +0 -1
- package/dist/index.mjs +0 -532
- package/dist/index.mjs.map +0 -1
|
@@ -0,0 +1,123 @@
|
|
|
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
|
+
signal: abortController.signal,
|
|
73
|
+
body: JSON.stringify(input),
|
|
74
|
+
});
|
|
75
|
+
if (!response.ok) {
|
|
76
|
+
throw new Error((await response.text()) ?? 'Failed to fetch the response.');
|
|
77
|
+
}
|
|
78
|
+
if (response.body == null) {
|
|
79
|
+
throw new Error('The response body is empty.');
|
|
80
|
+
}
|
|
81
|
+
let accumulatedText = '';
|
|
82
|
+
let latestObject = undefined;
|
|
83
|
+
await response.body.pipeThrough(new TextDecoderStream()).pipeTo(new WritableStream({
|
|
84
|
+
write: chunk => {
|
|
85
|
+
if (abortController?.signal.aborted) {
|
|
86
|
+
throw new DOMException('Stream aborted', 'AbortError');
|
|
87
|
+
}
|
|
88
|
+
accumulatedText += chunk;
|
|
89
|
+
const { value } = parsePartialJson(accumulatedText);
|
|
90
|
+
const currentObject = value;
|
|
91
|
+
if (!isDeepEqualData(latestObject, currentObject)) {
|
|
92
|
+
latestObject = currentObject;
|
|
93
|
+
this.#store.object = currentObject;
|
|
94
|
+
}
|
|
95
|
+
},
|
|
96
|
+
close: () => {
|
|
97
|
+
this.#store.loading = false;
|
|
98
|
+
this.#abortController = undefined;
|
|
99
|
+
if (this.#options.onFinish != null) {
|
|
100
|
+
const validationResult = safeValidateTypes({
|
|
101
|
+
value: latestObject,
|
|
102
|
+
schema: asSchema(this.#options.schema),
|
|
103
|
+
});
|
|
104
|
+
this.#options.onFinish(validationResult.success
|
|
105
|
+
? { object: validationResult.value, error: undefined }
|
|
106
|
+
: { object: undefined, error: validationResult.error });
|
|
107
|
+
}
|
|
108
|
+
},
|
|
109
|
+
}));
|
|
110
|
+
}
|
|
111
|
+
catch (error) {
|
|
112
|
+
if (isAbortError(error)) {
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
const coalescedError = error instanceof Error ? error : new Error(String(error));
|
|
116
|
+
if (this.#options.onError) {
|
|
117
|
+
this.#options.onError(coalescedError);
|
|
118
|
+
}
|
|
119
|
+
this.#store.loading = false;
|
|
120
|
+
this.#store.error = coalescedError;
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
}
|
|
@@ -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,OAAO,EAAE,gBAAgB,EAAE,MAAM,gCAAgC,CAAC;AAClE,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAC/C,OAAO,KAAK,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AA2B7B,cAAM,iBAAiB,CAAC,MAAM;IAC1B,KAAK;aArBA,MAAM;aACN,MAAM;;;IAuBX,MAAM;IAGN,KAAK;IAGL,QAAQ;IACR,OAAO;;;;CACV;AAED,UAAU,qBAAqB;IAC3B,KAAK,MAAM,EAAE,OAAO,EAAE,OAAO,QAAQ,EAAE,2BAA2B,CAAC,UAAU,CAAC,iBAAiB,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,QAAQ,EAAE,eAAe,CAAC,UAAU,CAAC,iBAAiB,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,CAAC,EAAE,UAAU,CAAC,iBAAiB,CAAC,MAAM,CAAC,CAAC,QAAQ,CAAC,CAAC,EAAE,UAAU,CAAC,iBAAiB,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG;QAAE,UAAU,CAAC,EAAE,UAAU,CAAC,iBAAiB,CAAC,MAAM,CAAC,CAAC,UAAU,CAAC,CAAC,CAAA;KAAE,GAAG,UAAU,CAAC,iBAAiB,CAAC,MAAM,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC;IAC/Z,CAAC,MAAM,EAAE,QAAQ,EAAE,OAAO,EAAE,KAAK,EAAE,UAAU,CAAC,iBAAiB,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,GAAG,UAAU,CAAC,iBAAiB,CAAC,MAAM,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC;IAC1I,YAAY,CAAC,EAAE,UAAU,CAAC,iBAAiB,CAAC,GAAG,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC;CACjE;AACD,QAAA,MAAM,+BAA+B,EAAE,qBAAmC,CAAC;AACzD,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
CHANGED
|
@@ -1,53 +1,69 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ai-sdk/svelte",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
|
-
"
|
|
6
|
-
|
|
7
|
-
|
|
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",
|
|
8
18
|
"types": "./dist/index.d.ts",
|
|
19
|
+
"type": "module",
|
|
9
20
|
"exports": {
|
|
10
|
-
"./package.json": "./package.json",
|
|
11
21
|
".": {
|
|
12
22
|
"types": "./dist/index.d.ts",
|
|
13
|
-
"
|
|
14
|
-
"require": "./dist/index.js"
|
|
23
|
+
"svelte": "./dist/index.js"
|
|
15
24
|
}
|
|
16
25
|
},
|
|
17
|
-
"
|
|
18
|
-
"
|
|
19
|
-
"CHANGELOG.md"
|
|
20
|
-
],
|
|
21
|
-
"dependencies": {
|
|
22
|
-
"@ai-sdk/provider-utils": "2.1.12",
|
|
23
|
-
"@ai-sdk/ui-utils": "1.1.18"
|
|
24
|
-
},
|
|
25
|
-
"devDependencies": {
|
|
26
|
-
"@types/node": "^18",
|
|
27
|
-
"eslint": "8.57.1",
|
|
28
|
-
"tsup": "^7.2.0",
|
|
29
|
-
"typescript": "5.6.3",
|
|
30
|
-
"@vercel/ai-tsconfig": "0.0.0",
|
|
31
|
-
"eslint-config-vercel-ai": "0.0.0"
|
|
26
|
+
"publishConfig": {
|
|
27
|
+
"access": "public"
|
|
32
28
|
},
|
|
33
29
|
"peerDependencies": {
|
|
34
|
-
"svelte": "^
|
|
30
|
+
"svelte": "^5.0.0",
|
|
31
|
+
"zod": "^3.23.8"
|
|
35
32
|
},
|
|
36
33
|
"peerDependenciesMeta": {
|
|
37
|
-
"
|
|
34
|
+
"zod": {
|
|
38
35
|
"optional": true
|
|
39
36
|
}
|
|
40
37
|
},
|
|
41
|
-
"
|
|
42
|
-
"
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"@ai-sdk/provider-utils": "2.1.13",
|
|
40
|
+
"@ai-sdk/ui-utils": "1.1.19"
|
|
43
41
|
},
|
|
44
|
-
"
|
|
45
|
-
"
|
|
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"
|
|
46
61
|
},
|
|
47
62
|
"homepage": "https://sdk.vercel.ai/docs",
|
|
48
63
|
"repository": {
|
|
49
64
|
"type": "git",
|
|
50
|
-
"url": "git+https://github.com/vercel/ai.git"
|
|
65
|
+
"url": "git+https://github.com/vercel/ai.git",
|
|
66
|
+
"directory": "packages/svelte"
|
|
51
67
|
},
|
|
52
68
|
"bugs": {
|
|
53
69
|
"url": "https://github.com/vercel/ai/issues"
|
|
@@ -57,11 +73,16 @@
|
|
|
57
73
|
"svelte"
|
|
58
74
|
],
|
|
59
75
|
"scripts": {
|
|
60
|
-
"build": "
|
|
61
|
-
"build:watch": "
|
|
62
|
-
"
|
|
63
|
-
"
|
|
64
|
-
"type-check": "
|
|
65
|
-
"prettier-
|
|
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"
|
|
66
87
|
}
|
|
67
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');
|