@molecule/app-svelte 3.2.4 → 3.5.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.
Files changed (169) hide show
  1. package/README.md +2522 -194
  2. package/dist/agent-identity.d.ts +31 -0
  3. package/dist/agent-identity.d.ts.map +1 -0
  4. package/dist/agent-identity.js +22 -0
  5. package/dist/context.d.ts +113 -1
  6. package/dist/context.d.ts.map +1 -1
  7. package/dist/context.js +222 -3
  8. package/dist/hooks/external-store.d.ts +28 -0
  9. package/dist/hooks/external-store.d.ts.map +1 -0
  10. package/dist/hooks/external-store.js +95 -0
  11. package/dist/hooks/index.d.ts +37 -0
  12. package/dist/hooks/index.d.ts.map +1 -0
  13. package/dist/hooks/index.js +36 -0
  14. package/dist/hooks/live-store.d.ts +68 -0
  15. package/dist/hooks/live-store.d.ts.map +1 -0
  16. package/dist/hooks/live-store.js +76 -0
  17. package/dist/hooks/useAIModels.d.ts +58 -0
  18. package/dist/hooks/useAIModels.d.ts.map +1 -0
  19. package/dist/hooks/useAIModels.js +155 -0
  20. package/dist/hooks/useAsyncState.d.ts +52 -0
  21. package/dist/hooks/useAsyncState.d.ts.map +1 -0
  22. package/dist/hooks/useAsyncState.js +80 -0
  23. package/dist/hooks/useAuth.d.ts +99 -0
  24. package/dist/hooks/useAuth.d.ts.map +1 -0
  25. package/dist/hooks/useAuth.js +88 -0
  26. package/dist/hooks/useCapacitorApp.d.ts +51 -0
  27. package/dist/hooks/useCapacitorApp.d.ts.map +1 -0
  28. package/dist/hooks/useCapacitorApp.js +57 -0
  29. package/dist/hooks/useChangePassword.d.ts +41 -0
  30. package/dist/hooks/useChangePassword.d.ts.map +1 -0
  31. package/dist/hooks/useChangePassword.js +40 -0
  32. package/dist/hooks/useDevice.d.ts +48 -0
  33. package/dist/hooks/useDevice.d.ts.map +1 -0
  34. package/dist/hooks/useDevice.js +37 -0
  35. package/dist/hooks/useEditor.d.ts +65 -0
  36. package/dist/hooks/useEditor.d.ts.map +1 -0
  37. package/dist/hooks/useEditor.js +86 -0
  38. package/dist/hooks/useForm.d.ts +137 -0
  39. package/dist/hooks/useForm.d.ts.map +1 -0
  40. package/dist/hooks/useForm.js +221 -0
  41. package/dist/hooks/useHttp.d.ts +122 -0
  42. package/dist/hooks/useHttp.d.ts.map +1 -0
  43. package/dist/hooks/useHttp.js +220 -0
  44. package/dist/hooks/useLogger.d.ts +52 -0
  45. package/dist/hooks/useLogger.d.ts.map +1 -0
  46. package/dist/hooks/useLogger.js +64 -0
  47. package/dist/hooks/useLogin.d.ts +47 -0
  48. package/dist/hooks/useLogin.d.ts.map +1 -0
  49. package/dist/hooks/useLogin.js +44 -0
  50. package/dist/hooks/useOAuth.d.ts +96 -0
  51. package/dist/hooks/useOAuth.d.ts.map +1 -0
  52. package/dist/hooks/useOAuth.js +363 -0
  53. package/dist/hooks/useOptimisticValue.d.ts +58 -0
  54. package/dist/hooks/useOptimisticValue.d.ts.map +1 -0
  55. package/dist/hooks/useOptimisticValue.js +98 -0
  56. package/dist/hooks/usePasswordReset.d.ts +50 -0
  57. package/dist/hooks/usePasswordReset.d.ts.map +1 -0
  58. package/dist/hooks/usePasswordReset.js +50 -0
  59. package/dist/hooks/usePlatform.d.ts +45 -0
  60. package/dist/hooks/usePlatform.d.ts.map +1 -0
  61. package/dist/hooks/usePlatform.js +35 -0
  62. package/dist/hooks/usePreview.d.ts +54 -0
  63. package/dist/hooks/usePreview.d.ts.map +1 -0
  64. package/dist/hooks/usePreview.js +61 -0
  65. package/dist/hooks/useProfileForm.d.ts +100 -0
  66. package/dist/hooks/useProfileForm.d.ts.map +1 -0
  67. package/dist/hooks/useProfileForm.js +267 -0
  68. package/dist/hooks/usePromise.d.ts +62 -0
  69. package/dist/hooks/usePromise.d.ts.map +1 -0
  70. package/dist/hooks/usePromise.js +105 -0
  71. package/dist/hooks/usePush.d.ts +68 -0
  72. package/dist/hooks/usePush.d.ts.map +1 -0
  73. package/dist/hooks/usePush.js +79 -0
  74. package/dist/hooks/useRouter.d.ts +169 -0
  75. package/dist/hooks/useRouter.d.ts.map +1 -0
  76. package/dist/hooks/useRouter.js +212 -0
  77. package/dist/hooks/useSignup.d.ts +52 -0
  78. package/dist/hooks/useSignup.d.ts.map +1 -0
  79. package/dist/hooks/useSignup.js +42 -0
  80. package/dist/hooks/useStorage.d.ts +133 -0
  81. package/dist/hooks/useStorage.d.ts.map +1 -0
  82. package/dist/hooks/useStorage.js +228 -0
  83. package/dist/hooks/useStore.d.ts +69 -0
  84. package/dist/hooks/useStore.d.ts.map +1 -0
  85. package/dist/hooks/useStore.js +82 -0
  86. package/dist/hooks/useTheme.d.ts +55 -0
  87. package/dist/hooks/useTheme.d.ts.map +1 -0
  88. package/dist/hooks/useTheme.js +102 -0
  89. package/dist/hooks/useTranslation.d.ts +106 -0
  90. package/dist/hooks/useTranslation.d.ts.map +1 -0
  91. package/dist/hooks/useTranslation.js +159 -0
  92. package/dist/hooks/useVerifyPaymentReturn.d.ts +91 -0
  93. package/dist/hooks/useVerifyPaymentReturn.d.ts.map +1 -0
  94. package/dist/hooks/useVerifyPaymentReturn.js +189 -0
  95. package/dist/hooks/useVersion.d.ts +53 -0
  96. package/dist/hooks/useVersion.d.ts.map +1 -0
  97. package/dist/hooks/useVersion.js +70 -0
  98. package/dist/hooks/useWorkspace.d.ts +47 -0
  99. package/dist/hooks/useWorkspace.d.ts.map +1 -0
  100. package/dist/hooks/useWorkspace.js +68 -0
  101. package/dist/index.d.ts +3 -0
  102. package/dist/index.d.ts.map +1 -1
  103. package/dist/index.js +3 -1
  104. package/dist/session-ready/SessionReady.svelte +20 -0
  105. package/dist/session-ready/SessionReady.svelte.d.ts +5 -0
  106. package/dist/session-ready/SessionReady.svelte.d.ts.map +1 -0
  107. package/dist/session-ready/index.d.ts +7 -0
  108. package/dist/session-ready/index.d.ts.map +1 -0
  109. package/dist/session-ready/index.js +6 -0
  110. package/dist/stores/async-state.d.ts.map +1 -1
  111. package/dist/stores/async-state.js +12 -5
  112. package/dist/stores/auth.js +0 -1
  113. package/dist/stores/capacitor-app.js +1 -2
  114. package/dist/stores/change-password.js +0 -1
  115. package/dist/stores/chat.d.ts +68 -0
  116. package/dist/stores/chat.d.ts.map +1 -0
  117. package/dist/stores/chat.js +86 -0
  118. package/dist/stores/device.js +0 -1
  119. package/dist/stores/forms.js +0 -1
  120. package/dist/stores/http.js +0 -1
  121. package/dist/stores/i18n.d.ts +3 -2
  122. package/dist/stores/i18n.d.ts.map +1 -1
  123. package/dist/stores/i18n.js +2 -3
  124. package/dist/stores/index.d.ts +1 -0
  125. package/dist/stores/index.d.ts.map +1 -1
  126. package/dist/stores/index.js +1 -1
  127. package/dist/stores/logger.js +0 -1
  128. package/dist/stores/login.js +0 -1
  129. package/dist/stores/oauth.d.ts.map +1 -1
  130. package/dist/stores/oauth.js +6 -3
  131. package/dist/stores/password-reset.js +0 -1
  132. package/dist/stores/platform.js +0 -1
  133. package/dist/stores/promise.js +0 -1
  134. package/dist/stores/push.js +0 -1
  135. package/dist/stores/router.js +0 -1
  136. package/dist/stores/signup.js +0 -1
  137. package/dist/stores/state.js +0 -1
  138. package/dist/stores/storage.js +0 -1
  139. package/dist/stores/theme.js +0 -1
  140. package/dist/stores/version.js +0 -1
  141. package/dist/types.d.ts +23 -0
  142. package/dist/types.d.ts.map +1 -1
  143. package/dist/types.js +0 -1
  144. package/package.json +33 -8
  145. package/dist/context.js.map +0 -1
  146. package/dist/index.js.map +0 -1
  147. package/dist/stores/async-state.js.map +0 -1
  148. package/dist/stores/auth.js.map +0 -1
  149. package/dist/stores/capacitor-app.js.map +0 -1
  150. package/dist/stores/change-password.js.map +0 -1
  151. package/dist/stores/device.js.map +0 -1
  152. package/dist/stores/forms.js.map +0 -1
  153. package/dist/stores/http.js.map +0 -1
  154. package/dist/stores/i18n.js.map +0 -1
  155. package/dist/stores/index.js.map +0 -1
  156. package/dist/stores/logger.js.map +0 -1
  157. package/dist/stores/login.js.map +0 -1
  158. package/dist/stores/oauth.js.map +0 -1
  159. package/dist/stores/password-reset.js.map +0 -1
  160. package/dist/stores/platform.js.map +0 -1
  161. package/dist/stores/promise.js.map +0 -1
  162. package/dist/stores/push.js.map +0 -1
  163. package/dist/stores/router.js.map +0 -1
  164. package/dist/stores/signup.js.map +0 -1
  165. package/dist/stores/state.js.map +0 -1
  166. package/dist/stores/storage.js.map +0 -1
  167. package/dist/stores/theme.js.map +0 -1
  168. package/dist/stores/version.js.map +0 -1
  169. package/dist/types.js.map +0 -1
@@ -0,0 +1,36 @@
1
+ /**
2
+ * Svelte hooks for molecule.dev, React's `@molecule/app-react` hooks under the
3
+ * same names: each is called during component init under
4
+ * `setMoleculeContext`, value fields are readable stores and methods are plain
5
+ * functions.
6
+ *
7
+ * @module
8
+ */
9
+ export * from './useAIModels.js';
10
+ export * from './useAsyncState.js';
11
+ export * from './useAuth.js';
12
+ export * from './useCapacitorApp.js';
13
+ export * from './useChangePassword.js';
14
+ export * from './useDevice.js';
15
+ export * from './useEditor.js';
16
+ export * from './useForm.js';
17
+ export * from './useHttp.js';
18
+ export * from './useLogger.js';
19
+ export * from './useLogin.js';
20
+ export * from './useOAuth.js';
21
+ export * from './useOptimisticValue.js';
22
+ export * from './usePasswordReset.js';
23
+ export * from './usePlatform.js';
24
+ export * from './usePreview.js';
25
+ export * from './useProfileForm.js';
26
+ export * from './usePromise.js';
27
+ export * from './usePush.js';
28
+ export * from './useRouter.js';
29
+ export * from './useSignup.js';
30
+ export * from './useStorage.js';
31
+ export * from './useStore.js';
32
+ export * from './useTheme.js';
33
+ export * from './useTranslation.js';
34
+ export * from './useVerifyPaymentReturn.js';
35
+ export * from './useVersion.js';
36
+ export * from './useWorkspace.js';
@@ -0,0 +1,68 @@
1
+ /**
2
+ * The shared reactive scaffolding of the React-named hooks: a readable store
3
+ * that always holds a service's current value, subscribed eagerly at creation
4
+ * (during component init) and torn down when the owning component is destroyed.
5
+ *
6
+ * A `readable()` store only subscribes its service when someone subscribes the
7
+ * store, so a value read after an update with no live subscriber goes stale;
8
+ * these hooks subscribe the service for the whole component lifetime instead,
9
+ * the way a component's `$store` read does.
10
+ *
11
+ * @module
12
+ */
13
+ import { type Readable } from 'svelte/store';
14
+ import type { PromiseStatus } from '@molecule/app-utilities';
15
+ import type { UsePromiseStore } from './usePromise.js';
16
+ /**
17
+ * A readable store that always holds `read()`'s value.
18
+ *
19
+ * The store subscribes `subscribe` eagerly (at creation, during component
20
+ * init), re-reads whenever it notifies and ends the subscription when the
21
+ * owning component is destroyed, so its value is current whether or not a
22
+ * subscriber is attached right now.
23
+ *
24
+ * @param read - Reads the service's current value.
25
+ * @param subscribe - Subscribes the service; returns its unsubscribe.
26
+ * @returns A readable store of the current value.
27
+ */
28
+ export declare function liveStore<T>(read: () => T, subscribe: (notify: () => void) => () => void): Readable<T>;
29
+ /**
30
+ * One picked field of a state store, as its own readable store.
31
+ *
32
+ * @param state - The store holding the whole state object.
33
+ * @param key - The field to project out.
34
+ * @returns A readable store of that field's current value.
35
+ */
36
+ export declare function fieldStore<T, K extends keyof T>(state: Readable<T>, key: K): Readable<T[K]>;
37
+ /**
38
+ * Whether a hook's options were passed as a readable store (options that change
39
+ * without a remount) rather than as a plain value.
40
+ *
41
+ * @param value - The options, or a store of them.
42
+ * @returns True for a store.
43
+ */
44
+ export declare function isReadableStore<T>(value: T | Readable<T>): value is Readable<T>;
45
+ /** The field-by-field view of a `usePromise` tracker, for hooks built on it. */
46
+ export interface PromiseTrackerFields<T, A extends unknown[]> {
47
+ /** The latest call's status. */
48
+ status: Readable<PromiseStatus>;
49
+ /** The latest call's resolved value (kept while a newer call is pending). */
50
+ value: Readable<T | null>;
51
+ /** The latest call's rejection. */
52
+ error: Readable<Error | null>;
53
+ /** The tracked function (one identity for the component's life). */
54
+ call: (...args: A) => Promise<T>;
55
+ /** Returns the tracker to idle. */
56
+ reset: () => void;
57
+ }
58
+ /**
59
+ * Reads a `usePromise` tracker's fields as individual stores and functions,
60
+ * for hooks built on it (`useLogin`, `useSignup`, …).
61
+ *
62
+ * @param T - The resolved value type.
63
+ * @param A - The argument tuple.
64
+ * @param tracker - The tracker store from `usePromise`.
65
+ * @returns The status, value and error as stores, and the call and reset functions.
66
+ */
67
+ export declare function promiseFields<T, A extends unknown[]>(tracker: UsePromiseStore<T, A>): PromiseTrackerFields<T, A>;
68
+ //# sourceMappingURL=live-store.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"live-store.d.ts","sourceRoot":"","sources":["../../src/hooks/live-store.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAGH,OAAO,EAAgB,KAAK,QAAQ,EAAY,MAAM,cAAc,CAAA;AAEpE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAA;AAE5D,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAA;AAEtD;;;;;;;;;;;GAWG;AACH,wBAAgB,SAAS,CAAC,CAAC,EACzB,IAAI,EAAE,MAAM,CAAC,EACb,SAAS,EAAE,CAAC,MAAM,EAAE,MAAM,IAAI,KAAK,MAAM,IAAI,GAC5C,QAAQ,CAAC,CAAC,CAAC,CAKb;AAED;;;;;;GAMG;AACH,wBAAgB,UAAU,CAAC,CAAC,EAAE,CAAC,SAAS,MAAM,CAAC,EAAE,KAAK,EAAE,QAAQ,CAAC,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAE3F;AAED;;;;;;GAMG;AACH,wBAAgB,eAAe,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,GAAG,KAAK,IAAI,QAAQ,CAAC,CAAC,CAAC,CAO/E;AAED,gFAAgF;AAChF,MAAM,WAAW,oBAAoB,CAAC,CAAC,EAAE,CAAC,SAAS,OAAO,EAAE;IAC1D,gCAAgC;IAChC,MAAM,EAAE,QAAQ,CAAC,aAAa,CAAC,CAAA;IAC/B,6EAA6E;IAC7E,KAAK,EAAE,QAAQ,CAAC,CAAC,GAAG,IAAI,CAAC,CAAA;IACzB,mCAAmC;IACnC,KAAK,EAAE,QAAQ,CAAC,KAAK,GAAG,IAAI,CAAC,CAAA;IAC7B,oEAAoE;IACpE,IAAI,EAAE,CAAC,GAAG,IAAI,EAAE,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,CAAA;IAChC,mCAAmC;IACnC,KAAK,EAAE,MAAM,IAAI,CAAA;CAClB;AAED;;;;;;;;GAQG;AACH,wBAAgB,aAAa,CAAC,CAAC,EAAE,CAAC,SAAS,OAAO,EAAE,EAClD,OAAO,EAAE,eAAe,CAAC,CAAC,EAAE,CAAC,CAAC,GAC7B,oBAAoB,CAAC,CAAC,EAAE,CAAC,CAAC,CAW5B"}
@@ -0,0 +1,76 @@
1
+ /**
2
+ * The shared reactive scaffolding of the React-named hooks: a readable store
3
+ * that always holds a service's current value, subscribed eagerly at creation
4
+ * (during component init) and torn down when the owning component is destroyed.
5
+ *
6
+ * A `readable()` store only subscribes its service when someone subscribes the
7
+ * store, so a value read after an update with no live subscriber goes stale;
8
+ * these hooks subscribe the service for the whole component lifetime instead,
9
+ * the way a component's `$store` read does.
10
+ *
11
+ * @module
12
+ */
13
+ import { onDestroy } from 'svelte';
14
+ import { derived, get, writable } from 'svelte/store';
15
+ /**
16
+ * A readable store that always holds `read()`'s value.
17
+ *
18
+ * The store subscribes `subscribe` eagerly (at creation, during component
19
+ * init), re-reads whenever it notifies and ends the subscription when the
20
+ * owning component is destroyed, so its value is current whether or not a
21
+ * subscriber is attached right now.
22
+ *
23
+ * @param read - Reads the service's current value.
24
+ * @param subscribe - Subscribes the service; returns its unsubscribe.
25
+ * @returns A readable store of the current value.
26
+ */
27
+ export function liveStore(read, subscribe) {
28
+ const store = writable(read());
29
+ const stop = subscribe(() => store.set(read()));
30
+ onDestroy(stop);
31
+ return { subscribe: store.subscribe };
32
+ }
33
+ /**
34
+ * One picked field of a state store, as its own readable store.
35
+ *
36
+ * @param state - The store holding the whole state object.
37
+ * @param key - The field to project out.
38
+ * @returns A readable store of that field's current value.
39
+ */
40
+ export function fieldStore(state, key) {
41
+ return derived(state, (current) => current[key]);
42
+ }
43
+ /**
44
+ * Whether a hook's options were passed as a readable store (options that change
45
+ * without a remount) rather than as a plain value.
46
+ *
47
+ * @param value - The options, or a store of them.
48
+ * @returns True for a store.
49
+ */
50
+ export function isReadableStore(value) {
51
+ return (typeof value === 'object' &&
52
+ value !== null &&
53
+ 'subscribe' in value &&
54
+ typeof value.subscribe === 'function');
55
+ }
56
+ /**
57
+ * Reads a `usePromise` tracker's fields as individual stores and functions,
58
+ * for hooks built on it (`useLogin`, `useSignup`, …).
59
+ *
60
+ * @param T - The resolved value type.
61
+ * @param A - The argument tuple.
62
+ * @param tracker - The tracker store from `usePromise`.
63
+ * @returns The status, value and error as stores, and the call and reset functions.
64
+ */
65
+ export function promiseFields(tracker) {
66
+ // The state object keeps one identity (mutated in place), so reading it once
67
+ // is enough for the action functions.
68
+ const [state, call] = get(tracker);
69
+ return {
70
+ status: derived(tracker, ([current]) => current.status),
71
+ value: derived(tracker, ([current]) => current.value),
72
+ error: derived(tracker, ([current]) => current.error),
73
+ call,
74
+ reset: () => state.reset(),
75
+ };
76
+ }
@@ -0,0 +1,58 @@
1
+ /**
2
+ * Svelte hook for the AI model catalog, porting React's `useAIModels`.
3
+ *
4
+ * Lazily fetches `GET /ai/models` on first mount and caches the result in a
5
+ * module-level singleton so subsequent mounts return synchronously. The picker
6
+ * shows a brief loading state on first open and never refetches afterwards.
7
+ *
8
+ * When a `projectId` is passed, the fetch is project-scoped
9
+ * (`?projectId=<id>`) so servers that support per-project custom
10
+ * ("bring your own AI") models include them; each scope caches independently.
11
+ *
12
+ * @module
13
+ */
14
+ import { type Readable } from 'svelte/store';
15
+ import type { AppModelDefinition, AppModeModelDefaults } from '@molecule/app-ai-models';
16
+ /**
17
+ * Test-only: drops every cached model list so the next `useAIModels` call
18
+ * refetches. Exposed for unit tests; do not call from production code.
19
+ */
20
+ export declare function resetAIModelsCache(): void;
21
+ /**
22
+ * Result returned by `useAIModels`.
23
+ */
24
+ export interface UseAIModelsResult {
25
+ /** Available models, or an empty array while loading, as a readable store. */
26
+ models: Readable<AppModelDefinition[]>;
27
+ /**
28
+ * Per-mode server default model ids for the requester's tier, or `undefined`
29
+ * while loading or on servers that don't provide them.
30
+ */
31
+ defaults: Readable<AppModeModelDefaults | undefined>;
32
+ /** The single model marked `freeTier: true`, or `undefined`. */
33
+ freeTierModel: Readable<AppModelDefinition | undefined>;
34
+ /** `true` while the initial fetch is in flight. */
35
+ loading: Readable<boolean>;
36
+ /** Error from the initial fetch, or `null`. */
37
+ error: Readable<Error | null>;
38
+ }
39
+ /**
40
+ * Subscribes to the cached AI model catalog, React's `useAIModels` in Svelte's
41
+ * idiom: call it during component init under `setMoleculeContext({ http })`.
42
+ * The first mount of a scope triggers a single `GET /ai/models` fetch;
43
+ * subsequent mounts return the cached result, and a scope whose fetch failed
44
+ * does not refetch on a later mount.
45
+ *
46
+ * @param projectId - Optional project scope: includes that project's custom
47
+ * ("bring your own AI") models on servers that support them.
48
+ * @returns Models, free-tier model, loading flag, and error, as stores.
49
+ * @example
50
+ * ```svelte
51
+ * <script>
52
+ * import { useAIModels } from '@molecule/app-svelte'
53
+ * const { models, freeTierModel, loading } = useAIModels()
54
+ * </script>
55
+ * ```
56
+ */
57
+ export declare function useAIModels(projectId?: string): UseAIModelsResult;
58
+ //# sourceMappingURL=useAIModels.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useAIModels.d.ts","sourceRoot":"","sources":["../../src/hooks/useAIModels.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAGH,OAAO,EAAW,KAAK,QAAQ,EAAY,MAAM,cAAc,CAAA;AAE/D,OAAO,KAAK,EAAE,kBAAkB,EAAE,oBAAoB,EAAE,MAAM,yBAAyB,CAAA;AAmGvF;;;GAGG;AACH,wBAAgB,kBAAkB,IAAI,IAAI,CASzC;AAWD;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAChC,8EAA8E;IAC9E,MAAM,EAAE,QAAQ,CAAC,kBAAkB,EAAE,CAAC,CAAA;IACtC;;;OAGG;IACH,QAAQ,EAAE,QAAQ,CAAC,oBAAoB,GAAG,SAAS,CAAC,CAAA;IACpD,gEAAgE;IAChE,aAAa,EAAE,QAAQ,CAAC,kBAAkB,GAAG,SAAS,CAAC,CAAA;IACvD,mDAAmD;IACnD,OAAO,EAAE,QAAQ,CAAC,OAAO,CAAC,CAAA;IAC1B,+CAA+C;IAC/C,KAAK,EAAE,QAAQ,CAAC,KAAK,GAAG,IAAI,CAAC,CAAA;CAC9B;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,WAAW,CAAC,SAAS,CAAC,EAAE,MAAM,GAAG,iBAAiB,CAkCjE"}
@@ -0,0 +1,155 @@
1
+ /**
2
+ * Svelte hook for the AI model catalog, porting React's `useAIModels`.
3
+ *
4
+ * Lazily fetches `GET /ai/models` on first mount and caches the result in a
5
+ * module-level singleton so subsequent mounts return synchronously. The picker
6
+ * shows a brief loading state on first open and never refetches afterwards.
7
+ *
8
+ * When a `projectId` is passed, the fetch is project-scoped
9
+ * (`?projectId=<id>`) so servers that support per-project custom
10
+ * ("bring your own AI") models include them; each scope caches independently.
11
+ *
12
+ * @module
13
+ */
14
+ import { onDestroy } from 'svelte';
15
+ import { derived, writable } from 'svelte/store';
16
+ import { loadAIModelCatalog, pickFreeTierModel } from '@molecule/app-ai-models';
17
+ import { useHttpClient } from '../context.js';
18
+ import { fieldStore } from './live-store.js';
19
+ /** One cache entry per scope — `''` for the unscoped catalog, else the projectId. */
20
+ const caches = new Map();
21
+ /**
22
+ * The cache entry for a scope, created on first use.
23
+ *
24
+ * @param scope - `''` for the unscoped catalog, else the projectId.
25
+ * @returns The scope's cache entry.
26
+ */
27
+ function cacheFor(scope) {
28
+ let cache = caches.get(scope);
29
+ if (!cache) {
30
+ cache = { models: null, defaults: undefined, error: null, inFlight: null };
31
+ caches.set(scope, cache);
32
+ }
33
+ return cache;
34
+ }
35
+ /** Listeners keyed by the same scope as the cache they observe. */
36
+ const listenersByScope = new Map();
37
+ /**
38
+ * The listener set for a scope, created on first use.
39
+ *
40
+ * @param scope - The cache scope.
41
+ * @returns The scope's listener set.
42
+ */
43
+ function listenersFor(scope) {
44
+ let set = listenersByScope.get(scope);
45
+ if (!set) {
46
+ set = new Set();
47
+ listenersByScope.set(scope, set);
48
+ }
49
+ return set;
50
+ }
51
+ /**
52
+ * Broadcasts a scope's current cache state to its subscribed listeners.
53
+ *
54
+ * @param scope - The cache scope to notify.
55
+ */
56
+ function notify(scope) {
57
+ const cache = cacheFor(scope);
58
+ for (const listener of listenersFor(scope)) {
59
+ listener({ models: cache.models, defaults: cache.defaults, error: cache.error });
60
+ }
61
+ }
62
+ /**
63
+ * Triggers (or rejoins) the single in-flight fetch of `/ai/models` for a scope
64
+ * and stores the result in that scope's module-level cache.
65
+ *
66
+ * @param http - HTTP client used for the single fetch.
67
+ * @param scope - `''` for the unscoped catalog, else the projectId.
68
+ * @returns The fetched model list.
69
+ */
70
+ async function fetchOnce(http, scope) {
71
+ const cache = cacheFor(scope);
72
+ if (cache.models)
73
+ return cache.models;
74
+ if (cache.inFlight)
75
+ return cache.inFlight;
76
+ cache.inFlight = loadAIModelCatalog(http, undefined, scope || undefined)
77
+ .then((catalog) => {
78
+ cache.models = catalog.models;
79
+ cache.defaults = catalog.defaults;
80
+ cache.error = null;
81
+ cache.inFlight = null;
82
+ notify(scope);
83
+ return catalog.models;
84
+ })
85
+ .catch((err) => {
86
+ cache.error = err instanceof Error ? err : new Error(String(err));
87
+ cache.inFlight = null;
88
+ notify(scope);
89
+ throw cache.error;
90
+ });
91
+ return cache.inFlight;
92
+ }
93
+ /**
94
+ * Test-only: drops every cached model list so the next `useAIModels` call
95
+ * refetches. Exposed for unit tests; do not call from production code.
96
+ */
97
+ export function resetAIModelsCache() {
98
+ for (const [scope, cache] of caches) {
99
+ cache.models = null;
100
+ cache.defaults = undefined;
101
+ cache.error = null;
102
+ cache.inFlight = null;
103
+ notify(scope);
104
+ }
105
+ caches.clear();
106
+ }
107
+ /**
108
+ * Subscribes to the cached AI model catalog, React's `useAIModels` in Svelte's
109
+ * idiom: call it during component init under `setMoleculeContext({ http })`.
110
+ * The first mount of a scope triggers a single `GET /ai/models` fetch;
111
+ * subsequent mounts return the cached result, and a scope whose fetch failed
112
+ * does not refetch on a later mount.
113
+ *
114
+ * @param projectId - Optional project scope: includes that project's custom
115
+ * ("bring your own AI") models on servers that support them.
116
+ * @returns Models, free-tier model, loading flag, and error, as stores.
117
+ * @example
118
+ * ```svelte
119
+ * <script>
120
+ * import { useAIModels } from '@molecule/app-svelte'
121
+ * const { models, freeTierModel, loading } = useAIModels()
122
+ * </script>
123
+ * ```
124
+ */
125
+ export function useAIModels(projectId) {
126
+ const http = useHttpClient();
127
+ const scope = projectId ?? '';
128
+ const cache = cacheFor(scope);
129
+ const snapshot = writable({
130
+ models: cache.models,
131
+ defaults: cache.defaults,
132
+ error: cache.error,
133
+ });
134
+ const listener = (next) => snapshot.set(next);
135
+ listenersFor(scope).add(listener);
136
+ onDestroy(() => {
137
+ listenersFor(scope).delete(listener);
138
+ });
139
+ // The writable was created from the same cache; this re-read is React's
140
+ // effect resync (its lazy useState init may have raced a scope change).
141
+ snapshot.set({ models: cache.models, defaults: cache.defaults, error: cache.error });
142
+ if (!cache.models && !cache.error) {
143
+ void fetchOnce(http, scope).catch((_error) => {
144
+ // fetchOnce mirrors the error into cache.error, which the listener delivers.
145
+ });
146
+ }
147
+ const loading = derived(snapshot, (current) => !current.models && !current.error);
148
+ return {
149
+ models: derived(snapshot, (current) => current.models ?? []),
150
+ defaults: fieldStore(snapshot, 'defaults'),
151
+ freeTierModel: derived(snapshot, (current) => pickFreeTierModel(current.models ?? [])),
152
+ loading,
153
+ error: fieldStore(snapshot, 'error'),
154
+ };
155
+ }
@@ -0,0 +1,52 @@
1
+ /**
2
+ * Svelte hook for async-capable state management, porting React's
3
+ * `useAsyncState`.
4
+ *
5
+ * @module
6
+ */
7
+ import { type Readable } from 'svelte/store';
8
+ /**
9
+ * Async-capable setState function.
10
+ *
11
+ * @param T - The state type.
12
+ */
13
+ export type AsyncSetState<T> = (value: T | ((prev: T) => T) | Promise<T | ((prev: T) => T)>) => Promise<void>;
14
+ /**
15
+ * Async-capable extendState function for partial updates.
16
+ *
17
+ * @param T - The state type.
18
+ */
19
+ export type AsyncExtendState<T> = (partial: Partial<T> | ((prev: T) => Partial<T>) | Promise<Partial<T> | ((prev: T) => Partial<T>)>) => Promise<void>;
20
+ /**
21
+ * The store {@link useAsyncState} returns: the current
22
+ * `[state, setState, extendState]` tuple.
23
+ */
24
+ export type UseAsyncStateStore<T> = Readable<readonly [T, AsyncSetState<T>, AsyncExtendState<T>]>;
25
+ /**
26
+ * State that accepts Promises and partial updates, React's `useAsyncState` in
27
+ * Svelte's idiom: a readable store of the `[state, setState, extendState]`
28
+ * tuple.
29
+ *
30
+ * Like React's, this has NO latest-wins guard: a promise that settles after a
31
+ * newer synchronous set overwrites it — whatever settles last wins. Rejections
32
+ * of resolved promises are silently swallowed (React's deliberate behavior),
33
+ * leaving the previous state in place.
34
+ *
35
+ * @param initialState - Initial state value.
36
+ * @returns A readable store of the [state, setState, extendState] tuple.
37
+ * @example
38
+ * ```svelte
39
+ * <script>
40
+ * import { useAsyncState } from '@molecule/app-svelte'
41
+ * const state = useAsyncState({ foo: 'foo', bar: 'bar' })
42
+ * const [value, setState, extendState] = $state
43
+ *
44
+ * setState({ foo: 'Hello', bar: 'World!' })
45
+ * setState((prev) => ({ ...prev, foo: prev.foo.toUpperCase() }))
46
+ * setState(fetchData()) // a promise resolving a value or an updater
47
+ * extendState({ foo: 'Hello' }) // a partial merge
48
+ * </script>
49
+ * ```
50
+ */
51
+ export declare function useAsyncState<T>(initialState: T): UseAsyncStateStore<T>;
52
+ //# sourceMappingURL=useAsyncState.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useAsyncState.d.ts","sourceRoot":"","sources":["../../src/hooks/useAsyncState.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,KAAK,QAAQ,EAA2B,MAAM,cAAc,CAAA;AAErE;;;;GAIG;AACH,MAAM,MAAM,aAAa,CAAC,CAAC,IAAI,CAC7B,KAAK,EAAE,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC,KACxD,OAAO,CAAC,IAAI,CAAC,CAAA;AAElB;;;;GAIG;AACH,MAAM,MAAM,gBAAgB,CAAC,CAAC,IAAI,CAChC,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,KAC9F,OAAO,CAAC,IAAI,CAAC,CAAA;AAElB;;;GAGG;AACH,MAAM,MAAM,kBAAkB,CAAC,CAAC,IAAI,QAAQ,CAAC,SAAS,CAAC,CAAC,EAAE,aAAa,CAAC,CAAC,CAAC,EAAE,gBAAgB,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;AAEjG;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,wBAAgB,aAAa,CAAC,CAAC,EAAE,YAAY,EAAE,CAAC,GAAG,kBAAkB,CAAC,CAAC,CAAC,CAwDvE"}
@@ -0,0 +1,80 @@
1
+ /**
2
+ * Svelte hook for async-capable state management, porting React's
3
+ * `useAsyncState`.
4
+ *
5
+ * @module
6
+ */
7
+ import { writable } from 'svelte/store';
8
+ /**
9
+ * State that accepts Promises and partial updates, React's `useAsyncState` in
10
+ * Svelte's idiom: a readable store of the `[state, setState, extendState]`
11
+ * tuple.
12
+ *
13
+ * Like React's, this has NO latest-wins guard: a promise that settles after a
14
+ * newer synchronous set overwrites it — whatever settles last wins. Rejections
15
+ * of resolved promises are silently swallowed (React's deliberate behavior),
16
+ * leaving the previous state in place.
17
+ *
18
+ * @param initialState - Initial state value.
19
+ * @returns A readable store of the [state, setState, extendState] tuple.
20
+ * @example
21
+ * ```svelte
22
+ * <script>
23
+ * import { useAsyncState } from '@molecule/app-svelte'
24
+ * const state = useAsyncState({ foo: 'foo', bar: 'bar' })
25
+ * const [value, setState, extendState] = $state
26
+ *
27
+ * setState({ foo: 'Hello', bar: 'World!' })
28
+ * setState((prev) => ({ ...prev, foo: prev.foo.toUpperCase() }))
29
+ * setState(fetchData()) // a promise resolving a value or an updater
30
+ * extendState({ foo: 'Hello' }) // a partial merge
31
+ * </script>
32
+ * ```
33
+ */
34
+ export function useAsyncState(initialState) {
35
+ let current = initialState;
36
+ const tuple = writable([
37
+ initialState,
38
+ setState,
39
+ extendState,
40
+ ]);
41
+ /** Publishes a new state value through the tuple store. */
42
+ function publish(next) {
43
+ current = next;
44
+ tuple.set([current, setState, extendState]);
45
+ }
46
+ /** Applies a resolved value-or-updater against the previous state. */
47
+ function applyValue(value) {
48
+ publish(typeof value === 'function' ? value(current) : value);
49
+ }
50
+ /** Applies a resolved partial-or-updater as a merge into the state. */
51
+ function applyPartial(partial) {
52
+ const resolved = typeof partial === 'function' ? partial(current) : partial;
53
+ publish({ ...current, ...resolved });
54
+ }
55
+ /** {@link AsyncSetState} — replaces the whole state. */
56
+ async function setState(value) {
57
+ try {
58
+ const resolved = value instanceof Promise ? await value : value;
59
+ applyValue(resolved);
60
+ }
61
+ catch (_error) {
62
+ // Silently swallow errors from resolved Promises to avoid unhandled
63
+ // rejections, exactly as React's useAsyncState does; the state keeps
64
+ // its previous value.
65
+ }
66
+ }
67
+ /** {@link AsyncExtendState} — merges a partial into the state. */
68
+ async function extendState(partial) {
69
+ try {
70
+ const resolved = partial instanceof Promise ? await partial : partial;
71
+ applyPartial(resolved);
72
+ }
73
+ catch (_error) {
74
+ // Silently swallow errors from resolved Promises to avoid unhandled
75
+ // rejections, exactly as React's useAsyncState does; the state keeps
76
+ // its previous value.
77
+ }
78
+ }
79
+ return tuple;
80
+ }
@@ -0,0 +1,99 @@
1
+ /**
2
+ * Svelte hook for authentication, porting React's `useAuth` family: the
3
+ * session state as readable stores plus the client's actions as plain
4
+ * functions.
5
+ *
6
+ * @module
7
+ */
8
+ import type { Readable } from 'svelte/store';
9
+ import type { AuthClient, AuthState, RegisterExtraShape } from '@molecule/app-auth';
10
+ /** Options for {@link useAuth}. */
11
+ export interface UseAuthOptions {
12
+ /** Whether to automatically refresh the token on mount. */
13
+ autoRefresh?: boolean;
14
+ }
15
+ /**
16
+ * What {@link useAuth} returns: the session's value fields as readable stores
17
+ * (`$status`, `$user`, …) and the client's actions as plain functions.
18
+ *
19
+ * @template T - The user profile the API returns.
20
+ * @template TRegister - The app's own registration fields.
21
+ */
22
+ export interface UseAuthResult<T = unknown, TRegister extends RegisterExtraShape<TRegister> = Record<never, never>> {
23
+ /**
24
+ * The session state as one value — render from this instead of combining
25
+ * `initialized`/`authenticated`/`loading`. Only `'loading'` means "no
26
+ * decision yet"; redirect on `'unauthenticated'` alone.
27
+ */
28
+ status: Readable<AuthState<T>['status']>;
29
+ /** The whole session state. */
30
+ state: Readable<AuthState<T>>;
31
+ /** The client's `login`. */
32
+ login: AuthClient<T>['login'];
33
+ /** The client's `logout`. */
34
+ logout: AuthClient<T>['logout'];
35
+ /** The client's `register` (there is no separate `useRegister`). */
36
+ register: AuthClient<T, TRegister>['register'];
37
+ /** The client's `refresh`. */
38
+ refresh: AuthClient<T>['refresh'];
39
+ /** Saves through the configured auth transport and publishes the saved user. */
40
+ updateProfile: AuthClient<T>['updateProfile'];
41
+ /** Caches a user without signing in. */
42
+ setUser: AuthClient<T>['setUser'];
43
+ /**
44
+ * Adopts a session an exchange outside the client already established (an
45
+ * OAuth code→token redirect, the app's own sign-in endpoint) in one update.
46
+ */
47
+ setSession: AuthClient<T>['setSession'];
48
+ /** Whether a session is authenticated locally. */
49
+ isAuthenticated: Readable<boolean>;
50
+ /** Whether the client is working on a decision. */
51
+ isLoading: Readable<boolean>;
52
+ /** The cached profile, which can be null while an unavailable candidate is retained. */
53
+ user: Readable<T | null>;
54
+ }
55
+ /**
56
+ * Authentication state and actions, React's `useAuth` in Svelte's idiom: call
57
+ * it during component init under `setMoleculeContext({ auth })`.
58
+ *
59
+ * `status === 'loading'` is undecided: wait for it and redirect only on
60
+ * `status === 'unauthenticated'`. `'unavailable'` keeps the session candidate
61
+ * (and may keep a cached user) for retry; a restore failure other than 401/403
62
+ * never signs out. Save profile edits with `updateProfile(patch)`; an
63
+ * app-owned mutation instead passes its canonical response to `setUser`. For
64
+ * a sign-in the app itself runs, call `setSession({ accessToken, user })`.
65
+ *
66
+ * @param options - Hook options (`autoRefresh`).
67
+ * @returns Auth state as stores and the actions as functions.
68
+ * @example
69
+ * ```svelte
70
+ * <script>
71
+ * import { useAuth } from '@molecule/app-svelte'
72
+ * const { user, status, login, logout } = useAuth()
73
+ * </script>
74
+ *
75
+ * {#if $status === 'loading'}
76
+ * <p>Restoring session…</p>
77
+ * {:else if $user}
78
+ * <p>Welcome, {$user.name}</p>
79
+ * <button onclick={() => logout()}>Sign out</button>
80
+ * {/if}
81
+ * ```
82
+ */
83
+ export declare function useAuth<T = unknown, TRegister extends RegisterExtraShape<TRegister> = Record<never, never>>(options?: UseAuthOptions): UseAuthResult<T, TRegister>;
84
+ /**
85
+ * The authenticated user, React's `useUser` in Svelte's idiom: a readable
86
+ * store of the cached profile (or null).
87
+ *
88
+ * @param T - The user profile the API returns.
89
+ * @returns A readable store of the authenticated user or null.
90
+ */
91
+ export declare function useUser<T = unknown>(): Readable<T | null>;
92
+ /**
93
+ * Whether a session is authenticated, React's `useIsAuthenticated` in
94
+ * Svelte's idiom.
95
+ *
96
+ * @returns A readable store of whether a session exists.
97
+ */
98
+ export declare function useIsAuthenticated(): Readable<boolean>;
99
+ //# sourceMappingURL=useAuth.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useAuth.d.ts","sourceRoot":"","sources":["../../src/hooks/useAuth.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAGH,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAA;AAE5C,OAAO,KAAK,EACV,UAAU,EAEV,SAAS,EAGT,kBAAkB,EACnB,MAAM,oBAAoB,CAAA;AAK3B,mCAAmC;AACnC,MAAM,WAAW,cAAc;IAC7B,2DAA2D;IAC3D,WAAW,CAAC,EAAE,OAAO,CAAA;CACtB;AAED;;;;;;GAMG;AACH,MAAM,WAAW,aAAa,CAC5B,CAAC,GAAG,OAAO,EACX,SAAS,SAAS,kBAAkB,CAAC,SAAS,CAAC,GAAG,MAAM,CAAC,KAAK,EAAE,KAAK,CAAC;IAEtE;;;;OAIG;IACH,MAAM,EAAE,QAAQ,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAA;IACxC,+BAA+B;IAC/B,KAAK,EAAE,QAAQ,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAA;IAC7B,4BAA4B;IAC5B,KAAK,EAAE,UAAU,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAA;IAC7B,6BAA6B;IAC7B,MAAM,EAAE,UAAU,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAA;IAC/B,oEAAoE;IACpE,QAAQ,EAAE,UAAU,CAAC,CAAC,EAAE,SAAS,CAAC,CAAC,UAAU,CAAC,CAAA;IAC9C,8BAA8B;IAC9B,OAAO,EAAE,UAAU,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAA;IACjC,gFAAgF;IAChF,aAAa,EAAE,UAAU,CAAC,CAAC,CAAC,CAAC,eAAe,CAAC,CAAA;IAC7C,wCAAwC;IACxC,OAAO,EAAE,UAAU,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAA;IACjC;;;OAGG;IACH,UAAU,EAAE,UAAU,CAAC,CAAC,CAAC,CAAC,YAAY,CAAC,CAAA;IACvC,kDAAkD;IAClD,eAAe,EAAE,QAAQ,CAAC,OAAO,CAAC,CAAA;IAClC,mDAAmD;IACnD,SAAS,EAAE,QAAQ,CAAC,OAAO,CAAC,CAAA;IAC5B,wFAAwF;IACxF,IAAI,EAAE,QAAQ,CAAC,CAAC,GAAG,IAAI,CAAC,CAAA;CACzB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,wBAAgB,OAAO,CACrB,CAAC,GAAG,OAAO,EACX,SAAS,SAAS,kBAAkB,CAAC,SAAS,CAAC,GAAG,MAAM,CAAC,KAAK,EAAE,KAAK,CAAC,EACtE,OAAO,CAAC,EAAE,cAAc,GAAG,aAAa,CAAC,CAAC,EAAE,SAAS,CAAC,CAkCvD;AAED;;;;;;GAMG;AACH,wBAAgB,OAAO,CAAC,CAAC,GAAG,OAAO,KAAK,QAAQ,CAAC,CAAC,GAAG,IAAI,CAAC,CAGzD;AAED;;;;;GAKG;AACH,wBAAgB,kBAAkB,IAAI,QAAQ,CAAC,OAAO,CAAC,CAGtD"}