@ai-sdk/rsc 2.0.45 → 2.0.46

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 (34) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/package.json +3 -2
  3. package/src/ai-state.test.ts +146 -0
  4. package/src/ai-state.tsx +210 -0
  5. package/src/index.ts +20 -0
  6. package/src/provider.tsx +149 -0
  7. package/src/rsc-client.ts +8 -0
  8. package/src/rsc-server.ts +5 -0
  9. package/src/rsc-shared.mts +11 -0
  10. package/src/shared-client/context.tsx +226 -0
  11. package/src/shared-client/index.ts +11 -0
  12. package/src/stream-ui/__snapshots__/render.ui.test.tsx.snap +91 -0
  13. package/src/stream-ui/__snapshots__/stream-ui.ui.test.tsx.snap +213 -0
  14. package/src/stream-ui/index.tsx +1 -0
  15. package/src/stream-ui/stream-ui.tsx +419 -0
  16. package/src/stream-ui/stream-ui.ui.test.tsx +321 -0
  17. package/src/streamable-ui/create-streamable-ui.tsx +148 -0
  18. package/src/streamable-ui/create-streamable-ui.ui.test.tsx +354 -0
  19. package/src/streamable-ui/create-suspended-chunk.tsx +84 -0
  20. package/src/streamable-value/create-streamable-value.test.tsx +179 -0
  21. package/src/streamable-value/create-streamable-value.ts +296 -0
  22. package/src/streamable-value/is-streamable-value.ts +10 -0
  23. package/src/streamable-value/read-streamable-value.tsx +113 -0
  24. package/src/streamable-value/read-streamable-value.ui.test.tsx +165 -0
  25. package/src/streamable-value/streamable-value.ts +37 -0
  26. package/src/streamable-value/use-streamable-value.tsx +91 -0
  27. package/src/types/index.ts +1 -0
  28. package/src/types.test-d.ts +17 -0
  29. package/src/types.ts +71 -0
  30. package/src/util/constants.ts +5 -0
  31. package/src/util/create-resolvable-promise.ts +28 -0
  32. package/src/util/is-async-generator.ts +7 -0
  33. package/src/util/is-function.ts +8 -0
  34. package/src/util/is-generator.ts +5 -0
@@ -0,0 +1 @@
1
+ export type * from '../index';
@@ -0,0 +1,17 @@
1
+ import { expectTypeOf } from 'vitest';
2
+ import { describe, it } from 'vitest';
3
+ import type { StreamableValue } from '.';
4
+
5
+ describe('StreamableValue type', () => {
6
+ it('should yield a type error when assigning a wrong value', () => {
7
+ expectTypeOf<StreamableValue<string>>().not.toEqualTypeOf<
8
+ StreamableValue<boolean>
9
+ >();
10
+
11
+ expectTypeOf<StreamableValue<string>>().not.toEqualTypeOf<string>();
12
+
13
+ expectTypeOf<
14
+ StreamableValue<string>
15
+ >().not.toEqualTypeOf<'THIS IS NOT A STREAMABLE VALUE'>();
16
+ });
17
+ });
package/src/types.ts ADDED
@@ -0,0 +1,71 @@
1
+ export type JSONValue = string | number | boolean | JSONObject | JSONArray;
2
+
3
+ interface JSONObject {
4
+ [x: string]: JSONValue | undefined;
5
+ }
6
+
7
+ interface JSONArray extends Array<JSONValue> {}
8
+
9
+ export type AIAction<T = any, R = any> = (...args: T[]) => Promise<R>;
10
+ export type AIActions<T = any, R = any> = Record<string, AIAction<T, R>>;
11
+
12
+ export type ServerWrappedAction<T = unknown> = (
13
+ aiState: T,
14
+ ...args: unknown[]
15
+ ) => Promise<[Promise<T>, unknown]>;
16
+ export type ServerWrappedActions<T = unknown> = Record<
17
+ string,
18
+ ServerWrappedAction<T>
19
+ >;
20
+
21
+ export type InternalAIProviderProps<AIState = any, UIState = any> = {
22
+ children: React.ReactNode;
23
+ initialUIState: UIState;
24
+ initialAIState: AIState;
25
+ initialAIStatePatch: undefined | Promise<AIState>;
26
+ wrappedActions: ServerWrappedActions<AIState>;
27
+ wrappedSyncUIState?: ServerWrappedAction<AIState>;
28
+ };
29
+
30
+ export type AIProviderProps<AIState = any, UIState = any, Actions = any> = {
31
+ children: React.ReactNode;
32
+ initialAIState?: AIState;
33
+ initialUIState?: UIState;
34
+ /** $ActionTypes is only added for type inference and is never used at runtime **/
35
+ $ActionTypes?: Actions;
36
+ };
37
+
38
+ export type AIProvider<AIState = any, UIState = any, Actions = any> = (
39
+ props: AIProviderProps<AIState, UIState, Actions>,
40
+ ) => Promise<React.ReactElement>;
41
+
42
+ export type InferAIState<T, Fallback> =
43
+ T extends AIProvider<infer AIState, any, any> ? AIState : Fallback;
44
+ export type InferUIState<T, Fallback> =
45
+ T extends AIProvider<any, infer UIState, any> ? UIState : Fallback;
46
+ export type InferActions<T, Fallback> =
47
+ T extends AIProvider<any, any, infer Actions> ? Actions : Fallback;
48
+
49
+ export type InternalAIStateStorageOptions = {
50
+ onSetAIState?: OnSetAIState<any>;
51
+ };
52
+
53
+ export type OnSetAIState<S> = ({
54
+ key,
55
+ state,
56
+ done,
57
+ }: {
58
+ key: string | number | symbol | undefined;
59
+ state: S;
60
+ done: boolean;
61
+ }) => void | Promise<void>;
62
+
63
+ export type OnGetUIState<S> = AIAction<void, S | undefined>;
64
+
65
+ export type ValueOrUpdater<T> = T | ((current: T) => T);
66
+
67
+ export type MutableAIState<AIState> = {
68
+ get: () => AIState;
69
+ update: (newState: ValueOrUpdater<AIState>) => void;
70
+ done: ((newState: AIState) => void) | (() => void);
71
+ };
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Warning time for notifying developers that a stream is hanging in dev mode
3
+ * using a console.warn.
4
+ */
5
+ export const HANGING_STREAM_WARNING_TIME_MS = 15 * 1000;
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Creates a Promise with externally accessible resolve and reject functions.
3
+ *
4
+ * @template T - The type of the value that the Promise will resolve to.
5
+ * @returns An object containing:
6
+ * - promise: A Promise that can be resolved or rejected externally.
7
+ * - resolve: A function to resolve the Promise with a value of type T.
8
+ * - reject: A function to reject the Promise with an error.
9
+ */
10
+ export function createResolvablePromise<T = any>(): {
11
+ promise: Promise<T>;
12
+ resolve: (value: T) => void;
13
+ reject: (error: unknown) => void;
14
+ } {
15
+ let resolve: (value: T) => void;
16
+ let reject: (error: unknown) => void;
17
+
18
+ const promise = new Promise<T>((res, rej) => {
19
+ resolve = res;
20
+ reject = rej;
21
+ });
22
+
23
+ return {
24
+ promise,
25
+ resolve: resolve!,
26
+ reject: reject!,
27
+ };
28
+ }
@@ -0,0 +1,7 @@
1
+ export function isAsyncGenerator<T, TReturn, TNext>(
2
+ value: unknown,
3
+ ): value is AsyncGenerator<T, TReturn, TNext> {
4
+ return (
5
+ value != null && typeof value === 'object' && Symbol.asyncIterator in value
6
+ );
7
+ }
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Checks if the given value is a function.
3
+ *
4
+ * @param {unknown} value - The value to check.
5
+ * @returns {boolean} True if the value is a function, false otherwise.
6
+ */
7
+ export const isFunction = (value: unknown): value is Function =>
8
+ typeof value === 'function';
@@ -0,0 +1,5 @@
1
+ export function isGenerator<T, TReturn, TNext>(
2
+ value: unknown,
3
+ ): value is Generator<T, TReturn, TNext> {
4
+ return value != null && typeof value === 'object' && Symbol.iterator in value;
5
+ }