@assistant-ui/react-statewire 0.25.0 → 0.27.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/README.md +2 -2
- package/dist/localStorage.d.ts +3 -0
- package/dist/localStorage.d.ts.map +1 -1
- package/dist/localStorage.js +7 -2
- package/dist/localStorage.js.map +1 -1
- package/package.json +2 -2
- package/src/StatewireThread.failed.test.tsx +11 -5
- package/src/StatewireThread.runs.test.tsx +11 -5
- package/src/localStorage.test.ts +1 -0
- package/src/localStorage.ts +5 -1
package/README.md
CHANGED
|
@@ -14,10 +14,10 @@ pnpm add @assistant-ui/react-statewire statewire @assistant-ui/core @assistant-u
|
|
|
14
14
|
import { AuiProvider, useAui } from "@assistant-ui/store";
|
|
15
15
|
import { StatewireThread } from "@assistant-ui/react-statewire";
|
|
16
16
|
import { LocalStorageComposerDrafts } from "@assistant-ui/react-statewire/local-storage";
|
|
17
|
-
import {
|
|
17
|
+
import { StatewireHttp } from "statewire";
|
|
18
18
|
|
|
19
19
|
const thread = StatewireThread<ServerState>({
|
|
20
|
-
transport:
|
|
20
|
+
transport: StatewireHttp({ url }),
|
|
21
21
|
initialState: { messages: [], status: "ready", runs: [] },
|
|
22
22
|
converter,
|
|
23
23
|
capabilities: { edit: true, reload: true },
|
package/dist/localStorage.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { StatewireThread } from "./StatewireThread.js";
|
|
2
|
+
import { StatewireClient } from "statewire";
|
|
2
3
|
//#region src/localStorage.d.ts
|
|
3
4
|
/** Composer draft storage on localStorage, keyed per thread; stale drafts are swept on mount. */
|
|
4
5
|
declare const LocalStorageComposerDrafts: import("@assistant-ui/tap").Resource<StatewireThread.ComposerStorage, [LocalStorageComposerDrafts.Options]>;
|
|
@@ -6,6 +7,8 @@ declare const LocalStorageComposerDrafts: import("@assistant-ui/tap").Resource<S
|
|
|
6
7
|
declare const LocalStorageSession: import("@assistant-ui/tap").Resource<StatewireClient.SessionStorage, [LocalStorageSession.Options]>;
|
|
7
8
|
declare namespace LocalStorageSession {
|
|
8
9
|
type Options = {
|
|
10
|
+
/** Upgrade incompatible queued commands, or return null to discard the saved session. */
|
|
11
|
+
migrate?: StatewireClient.SessionStorage["migrate"];
|
|
9
12
|
/** The thread whose session this storage holds. */
|
|
10
13
|
threadId: string;
|
|
11
14
|
/** Derives the storage key from the thread id; defaults to the id itself. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"localStorage.d.ts","names":[],"sources":["../src/localStorage.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"localStorage.d.ts","names":[],"sources":["../src/localStorage.ts"],"mappings":";;;;cA+Oa,wDAA0B,SAAA,gBAAA,kBAAA,2BAAA;;cA4C1B,iDAAmB,SAAA,gBAAA,iBAAA,oBAAA;kBAEf;OACH;;IAEV,UAAU,gBAAgB;;IAE1B;;IAEA,QAAQ;;;kBAIK;OACH;;IAEV;;IAEA,QAAQ;;IAER;;IAEA"}
|
package/dist/localStorage.js
CHANGED
|
@@ -106,7 +106,7 @@ const useLocalStorageComposerDrafts = ({ threadId, key = (id) => id, ttl = 7 * D
|
|
|
106
106
|
/** Composer draft storage on localStorage, keyed per thread; stale drafts are swept on mount. */
|
|
107
107
|
const LocalStorageComposerDrafts = resource(useLocalStorageComposerDrafts);
|
|
108
108
|
const SESSION_LOCK_PREFIX = "aui:statewire-session-lock:";
|
|
109
|
-
const useLocalStorageSession = ({ threadId, key = (id) => id }) => {
|
|
109
|
+
const useLocalStorageSession = ({ threadId, migrate, key = (id) => id }) => {
|
|
110
110
|
if (typeof localStorage === "undefined") throw new Error("statewire: LocalStorageSession requires localStorage");
|
|
111
111
|
if (typeof navigator === "undefined" || navigator.locks === void 0) throw new Error("statewire: LocalStorageSession requires the Web Locks API (navigator.locks)");
|
|
112
112
|
const storageKey = SESSION_PREFIX + key(threadId);
|
|
@@ -118,6 +118,7 @@ const useLocalStorageSession = ({ threadId, key = (id) => id }) => {
|
|
|
118
118
|
save: (record) => {
|
|
119
119
|
localStorage.setItem(storageKey, JSON.stringify(record));
|
|
120
120
|
},
|
|
121
|
+
...migrate !== void 0 && { migrate },
|
|
121
122
|
lock: (fn) => navigator.locks.request(lockName, async () => fn()),
|
|
122
123
|
subscribe: (listener) => {
|
|
123
124
|
const onStorage = (event) => {
|
|
@@ -126,7 +127,11 @@ const useLocalStorageSession = ({ threadId, key = (id) => id }) => {
|
|
|
126
127
|
window.addEventListener("storage", onStorage);
|
|
127
128
|
return () => window.removeEventListener("storage", onStorage);
|
|
128
129
|
}
|
|
129
|
-
}), [
|
|
130
|
+
}), [
|
|
131
|
+
storageKey,
|
|
132
|
+
lockName,
|
|
133
|
+
migrate
|
|
134
|
+
]);
|
|
130
135
|
};
|
|
131
136
|
/** Session storage on localStorage, keyed per thread: the lane identity plus the commands awaiting a durable verdict; sharers serialize through a per-key Web Lock. */
|
|
132
137
|
const LocalStorageSession = resource(useLocalStorageSession);
|
package/dist/localStorage.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"localStorage.js","names":["useEffect","useMemo","resource","StatewireClient","StatewireThread","PREFIX","SESSION_PREFIX","DAY","StoredDraft","text","attachments","ComposerDraft","ts","isRecord","value","Record","Array","isArray","isOptionalString","undefined","isUserMessagePart","audio","StoredAttachment","isStoredAttachment","status","every","isStoredDraft","Number","isFinite","isPositiveSafeInteger","isSafeInteger","isSessionCommand","SessionCommand","isSessionVerdict","SessionVerdict","isSendFailure","SendFailure","isFailedCommand","FailedCommand","isSessionRecord","SessionRecord","loadStored","key","validate","T","raw","localStorage","getItem","JSON","parse","removeItem","sweep","ttl","max","entries","keys","from","length","_","index","startsWith","stored","push","now","Date","fresh","filter","sort","a","b","slice","useLocalStorageComposerDrafts","threadId","id","LocalStorageComposerDrafts","Options","ComposerStorage","Error","storageKey","read","write","draft","setItem","stringify","clear","SESSION_LOCK_PREFIX","useLocalStorageSession","LocalStorageSession","SessionStorage","navigator","locks","lockName","load","save","record","lock","fn","request","subscribe","listener","onStorage","event","StorageEvent","window","addEventListener","removeEventListener"],"sources":["../src/localStorage.ts"],"sourcesContent":["import { useEffect, useMemo } from \"react\";\nimport { resource } from \"@assistant-ui/tap\";\nimport type { StatewireClient } from \"statewire\";\nimport type { StatewireThread } from \"./StatewireThread\";\n\nconst PREFIX = \"aui:composer-draft:\";\nconst SESSION_PREFIX = \"aui:statewire-session:\";\nconst DAY = 86_400_000;\n\ntype StoredDraft = {\n text: string;\n attachments: StatewireThread.ComposerDraft[\"attachments\"];\n ts: number;\n};\n\nconst isRecord = (value: unknown): value is Record<string, unknown> =>\n typeof value === \"object\" && value !== null && !Array.isArray(value);\n\nconst isOptionalString = (value: unknown): boolean =>\n value === undefined || typeof value === \"string\";\n\nconst isUserMessagePart = (value: unknown): boolean => {\n if (!isRecord(value)) return false;\n switch (value[\"type\"]) {\n case \"text\":\n return typeof value[\"text\"] === \"string\";\n case \"image\":\n return (\n typeof value[\"image\"] === \"string\" &&\n isOptionalString(value[\"filename\"])\n );\n case \"file\":\n return (\n typeof value[\"data\"] === \"string\" &&\n typeof value[\"mimeType\"] === \"string\" &&\n isOptionalString(value[\"filename\"])\n );\n case \"data\":\n return typeof value[\"name\"] === \"string\";\n case \"audio\": {\n const audio = value[\"audio\"];\n return (\n isRecord(audio) &&\n typeof audio[\"data\"] === \"string\" &&\n (audio[\"format\"] === \"mp3\" || audio[\"format\"] === \"wav\")\n );\n }\n default:\n return false;\n }\n};\n\ntype StoredAttachment = StoredDraft[\"attachments\"][number];\n\nconst isStoredAttachment = (value: unknown): value is StoredAttachment => {\n if (!isRecord(value)) return false;\n const status = value[\"status\"];\n return (\n typeof value[\"id\"] === \"string\" &&\n typeof value[\"type\"] === \"string\" &&\n typeof value[\"name\"] === \"string\" &&\n isOptionalString(value[\"contentType\"]) &&\n value[\"file\"] === undefined &&\n isRecord(status) &&\n status[\"type\"] === \"complete\" &&\n Array.isArray(value[\"content\"]) &&\n value[\"content\"].every(isUserMessagePart)\n );\n};\n\nconst isStoredDraft = (value: unknown): value is StoredDraft =>\n isRecord(value) &&\n typeof value[\"text\"] === \"string\" &&\n Array.isArray(value[\"attachments\"]) &&\n value[\"attachments\"].every(isStoredAttachment) &&\n typeof value[\"ts\"] === \"number\" &&\n Number.isFinite(value[\"ts\"]);\n\nconst isPositiveSafeInteger = (value: unknown): value is number =>\n typeof value === \"number\" && Number.isSafeInteger(value) && value > 0;\n\nconst isSessionCommand = (\n value: unknown,\n): value is StatewireClient.SessionCommand =>\n isRecord(value) &&\n isPositiveSafeInteger(value[\"seq\"]) &&\n typeof value[\"method\"] === \"string\" &&\n Array.isArray(value[\"params\"]);\n\nconst isSessionVerdict = (\n value: unknown,\n): value is StatewireClient.SessionVerdict =>\n isRecord(value) &&\n isPositiveSafeInteger(value[\"seq\"]) &&\n (value[\"type\"] === \"result\" ||\n value[\"type\"] === \"rejected\" ||\n value[\"type\"] === \"failed\" ||\n value[\"type\"] === \"crashed\" ||\n value[\"type\"] === \"expired\") &&\n isOptionalString(value[\"code\"]) &&\n isOptionalString(value[\"message\"]);\n\nconst isSendFailure = (\n value: unknown,\n): value is StatewireClient.SendFailure => {\n if (!isRecord(value)) return false;\n switch (value[\"fate\"]) {\n case \"rejected\":\n return (\n value[\"cause\"] === \"server\" &&\n isOptionalString(value[\"code\"]) &&\n isOptionalString(value[\"message\"])\n );\n case \"failed\":\n return value[\"cause\"] === \"server\" && isOptionalString(value[\"message\"]);\n case \"crashed\":\n case \"expired\":\n return value[\"cause\"] === \"server\";\n case \"aborted\":\n return (\n (value[\"cause\"] === \"unmount\" ||\n value[\"cause\"] === \"stream-gone\" ||\n value[\"cause\"] === \"session-lost\") &&\n (value[\"predecessorLost\"] === undefined ||\n value[\"predecessorLost\"] === true)\n );\n case \"lost\":\n return (\n value[\"cause\"] === \"unmount\" ||\n value[\"cause\"] === \"stream-gone\" ||\n value[\"cause\"] === \"session-lost\"\n );\n default:\n return false;\n }\n};\n\nconst isFailedCommand = (\n value: unknown,\n): value is StatewireClient.FailedCommand =>\n isRecord(value) &&\n typeof value[\"command\"] === \"string\" &&\n Array.isArray(value[\"params\"]) &&\n isSendFailure(value[\"failure\"]) &&\n typeof value[\"ts\"] === \"number\" &&\n Number.isFinite(value[\"ts\"]) &&\n isPositiveSafeInteger(value[\"seq\"]);\n\nconst isSessionRecord = (\n value: unknown,\n): value is StatewireClient.SessionRecord =>\n isRecord(value) &&\n typeof value[\"clientId\"] === \"string\" &&\n isPositiveSafeInteger(value[\"nextSeq\"]) &&\n isPositiveSafeInteger(value[\"ctxNextSeq\"]) &&\n Array.isArray(value[\"commands\"]) &&\n value[\"commands\"].every(isSessionCommand) &&\n (value[\"verdicts\"] === undefined ||\n (Array.isArray(value[\"verdicts\"]) &&\n value[\"verdicts\"].every(isSessionVerdict))) &&\n (value[\"failed\"] === undefined ||\n (Array.isArray(value[\"failed\"]) && value[\"failed\"].every(isFailedCommand)));\n\nconst loadStored = <T>(\n key: string,\n validate: (value: unknown) => value is T,\n): T | null => {\n const raw = localStorage.getItem(key);\n if (raw === null) return null;\n try {\n const value: unknown = JSON.parse(raw);\n if (validate(value)) return value;\n } catch {}\n localStorage.removeItem(key);\n return null;\n};\n\nconst sweep = (ttl: number, max: number) => {\n const entries: { key: string; ts: number }[] = [];\n const keys = Array.from({ length: localStorage.length }, (_, index) =>\n localStorage.key(index),\n );\n for (const key of keys) {\n if (key === null || !key.startsWith(PREFIX)) continue;\n const stored = loadStored(key, isStoredDraft);\n if (stored !== null) entries.push({ key, ts: stored.ts });\n }\n const now = Date.now();\n const fresh = entries.filter(({ key, ts }) => {\n if (now - ts <= ttl) return true;\n localStorage.removeItem(key);\n return false;\n });\n fresh.sort((a, b) => b.ts - a.ts);\n for (const { key } of fresh.slice(max)) localStorage.removeItem(key);\n};\n\nconst useLocalStorageComposerDrafts = ({\n threadId,\n key = (id) => id,\n ttl = 7 * DAY,\n max = 50,\n}: LocalStorageComposerDrafts.Options): StatewireThread.ComposerStorage => {\n if (typeof localStorage === \"undefined\")\n throw new Error(\n \"statewire: LocalStorageComposerDrafts requires localStorage\",\n );\n\n useEffect(() => {\n sweep(ttl, max);\n }, [ttl, max]);\n\n const storageKey = PREFIX + key(threadId);\n return useMemo(\n (): StatewireThread.ComposerStorage => ({\n read: () => {\n const stored = loadStored(storageKey, isStoredDraft);\n if (stored === null) return undefined;\n return { text: stored.text, attachments: stored.attachments };\n },\n write: (draft) => {\n if (draft.text === \"\" && draft.attachments.length === 0) {\n localStorage.removeItem(storageKey);\n return;\n }\n localStorage.setItem(\n storageKey,\n JSON.stringify({ ...draft, ts: Date.now() }),\n );\n },\n clear: () => {\n localStorage.removeItem(storageKey);\n },\n }),\n [storageKey],\n );\n};\n\n/** Composer draft storage on localStorage, keyed per thread; stale drafts are swept on mount. */\nexport const LocalStorageComposerDrafts = resource(\n useLocalStorageComposerDrafts,\n);\n\nconst SESSION_LOCK_PREFIX = \"aui:statewire-session-lock:\";\n\nconst useLocalStorageSession = ({\n threadId,\n key = (id) => id,\n}: LocalStorageSession.Options): StatewireClient.SessionStorage => {\n if (typeof localStorage === \"undefined\")\n throw new Error(\"statewire: LocalStorageSession requires localStorage\");\n // localStorage is shared across tabs: record mutations must serialize.\n if (typeof navigator === \"undefined\" || navigator.locks === undefined)\n throw new Error(\n \"statewire: LocalStorageSession requires the Web Locks API (navigator.locks)\",\n );\n\n const storageKey = SESSION_PREFIX + key(threadId);\n const lockName = SESSION_LOCK_PREFIX + key(threadId);\n return useMemo(\n (): StatewireClient.SessionStorage => ({\n load: () => {\n return loadStored(storageKey, isSessionRecord);\n },\n save: (record) => {\n localStorage.setItem(storageKey, JSON.stringify(record));\n },\n lock: (fn) => navigator.locks.request(lockName, async () => fn()),\n subscribe: (listener) => {\n const onStorage = (event: StorageEvent) => {\n if (event.key === storageKey) listener();\n };\n window.addEventListener(\"storage\", onStorage);\n return () => window.removeEventListener(\"storage\", onStorage);\n },\n }),\n [storageKey, lockName],\n );\n};\n\n/** Session storage on localStorage, keyed per thread: the lane identity plus the commands awaiting a durable verdict; sharers serialize through a per-key Web Lock. */\nexport const LocalStorageSession = resource(useLocalStorageSession);\n\nexport namespace LocalStorageSession {\n export type Options = {\n /** The thread whose session this storage holds. */\n threadId: string;\n /** Derives the storage key from the thread id; defaults to the id itself. */\n key?: ((threadId: string) => string) | undefined;\n };\n}\n\nexport namespace LocalStorageComposerDrafts {\n export type Options = {\n /** The thread whose draft this storage holds. */\n threadId: string;\n /** Derives the storage key from the thread id; defaults to the id itself. */\n key?: ((threadId: string) => string) | undefined;\n /** Draft lifetime in milliseconds, enforced by the mount sweep. Default 7 days. */\n ttl?: number | undefined;\n /** Newest drafts kept across all keys, enforced by the mount sweep. Default 50. */\n max?: number | undefined;\n };\n}\n"],"mappings":";;;AAKA,MAAMK,SAAS;AACf,MAAMC,iBAAiB;AACvB,MAAMC,MAAM;AAQZ,MAAMM,YAAYC,UAChB,OAAOA,UAAU,YAAYA,UAAU,QAAQ,CAACE,MAAMC,QAAQH,KAAK;AAErE,MAAMI,oBAAoBJ,UACxBA,UAAUK,KAAAA,KAAa,OAAOL,UAAU;AAE1C,MAAMM,qBAAqBN,UAA4B;CACrD,IAAI,CAACD,SAASC,KAAK,GAAG,OAAO;CAC7B,QAAQA,MAAM,SAAd;EACE,KAAK,QACH,OAAO,OAAOA,MAAM,YAAY;EAClC,KAAK,SACH,OACE,OAAOA,MAAM,aAAa,YAC1BI,iBAAiBJ,MAAM,WAAW;EAEtC,KAAK,QACH,OACE,OAAOA,MAAM,YAAY,YACzB,OAAOA,MAAM,gBAAgB,YAC7BI,iBAAiBJ,MAAM,WAAW;EAEtC,KAAK,QACH,OAAO,OAAOA,MAAM,YAAY;EAClC,KAAK,SAAS;GACZ,MAAMO,QAAQP,MAAM;GACpB,OACED,SAASQ,KAAK,KACd,OAAOA,MAAM,YAAY,aACxBA,MAAM,cAAc,SAASA,MAAM,cAAc;EAEtD;EACA,SACE,OAAO;CACX;AACF;AAIA,MAA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E,SAAS,MAAMwE,MAAMvE,YAAY+C,WAAW,GAAG;IACvDX,aAAaI,WAAW4B,UAAU;IAClC;GACF;GACAhC,aAAaoC,QACXJ,YACA9B,KAAKmC,UAAU;IAAE,GAAGF;IAAOrE,IAAIoD,KAAKD,IAAI;GAAE,CAAC,CAC7C;EACF;EACAqB,aAAa;GACXtC,aAAaI,WAAW4B,UAAU;EACpC;CACF,IACA,CAACA,UAAU,CACb;AACF;;AAGA,MAAaJ,6BAA6BxE,SACxCqE,6BACF;AAEA,MAAMc,sBAAsB;AAE5B,MAAMC,0BAA0B,EAC9Bd,UACA9B,OAAO+B,OAAOA,SACmD;CACjE,IAAI,OAAO3B,iBAAiB,aAC1B,MAAM,IAAI+B,MAAM,sDAAsD;CAExE,IAAI,OAAOY,cAAc,eAAeA,UAAUC,UAAUvE,KAAAA,GAC1D,MAAM,IAAI0D,MACR,6EACF;CAEF,MAAMC,aAAaxE,iBAAiBoC,IAAI8B,QAAQ;CAChD,MAAMmB,WAAWN,sBAAsB3C,IAAI8B,QAAQ;CACnD,OAAOvE,eACkC;EACrC2F,YAAY;GACV,OAAOnD,WAAWqC,YAAYvC,eAAe;EAC/C;EACAsD,OAAOC,WAAW;GAChBhD,aAAaoC,QAAQJ,YAAY9B,KAAKmC,UAAUW,MAAM,CAAC;EACzD;EACAC,OAAOC,OAAOP,UAAUC,MAAMO,QAAQN,UAAU,YAAYK,GAAG,CAAC;EAChEE,YAAYC,aAAa;GACvB,MAAMC,aAAaC,UAAwB;IACzC,IAAIA,MAAM3D,QAAQoC,YAAYqB,SAAS;GACzC;GACAI,OAAOC,iBAAiB,WAAWJ,SAAS;GAC5C,aAAaG,OAAOE,oBAAoB,WAAWL,SAAS;EAC9D;CACF,IACA,CAACtB,YAAYa,QAAQ,CACvB;AACF;;AAGA,MAAaJ,sBAAsBrF,SAASoF,sBAAsB"}
|
|
1
|
+
{"version":3,"file":"localStorage.js","names":["useEffect","useMemo","resource","StatewireClient","StatewireThread","PREFIX","SESSION_PREFIX","DAY","StoredDraft","text","attachments","ComposerDraft","ts","isRecord","value","Record","Array","isArray","isOptionalString","undefined","isUserMessagePart","audio","StoredAttachment","isStoredAttachment","status","every","isStoredDraft","Number","isFinite","isPositiveSafeInteger","isSafeInteger","isSessionCommand","SessionCommand","isSessionVerdict","SessionVerdict","isSendFailure","SendFailure","isFailedCommand","FailedCommand","isSessionRecord","SessionRecord","loadStored","key","validate","T","raw","localStorage","getItem","JSON","parse","removeItem","sweep","ttl","max","entries","keys","from","length","_","index","startsWith","stored","push","now","Date","fresh","filter","sort","a","b","slice","useLocalStorageComposerDrafts","threadId","id","LocalStorageComposerDrafts","Options","ComposerStorage","Error","storageKey","read","write","draft","setItem","stringify","clear","SESSION_LOCK_PREFIX","useLocalStorageSession","migrate","LocalStorageSession","SessionStorage","navigator","locks","lockName","load","save","record","lock","fn","request","subscribe","listener","onStorage","event","StorageEvent","window","addEventListener","removeEventListener"],"sources":["../src/localStorage.ts"],"sourcesContent":["import { useEffect, useMemo } from \"react\";\nimport { resource } from \"@assistant-ui/tap\";\nimport type { StatewireClient } from \"statewire\";\nimport type { StatewireThread } from \"./StatewireThread\";\n\nconst PREFIX = \"aui:composer-draft:\";\nconst SESSION_PREFIX = \"aui:statewire-session:\";\nconst DAY = 86_400_000;\n\ntype StoredDraft = {\n text: string;\n attachments: StatewireThread.ComposerDraft[\"attachments\"];\n ts: number;\n};\n\nconst isRecord = (value: unknown): value is Record<string, unknown> =>\n typeof value === \"object\" && value !== null && !Array.isArray(value);\n\nconst isOptionalString = (value: unknown): boolean =>\n value === undefined || typeof value === \"string\";\n\nconst isUserMessagePart = (value: unknown): boolean => {\n if (!isRecord(value)) return false;\n switch (value[\"type\"]) {\n case \"text\":\n return typeof value[\"text\"] === \"string\";\n case \"image\":\n return (\n typeof value[\"image\"] === \"string\" &&\n isOptionalString(value[\"filename\"])\n );\n case \"file\":\n return (\n typeof value[\"data\"] === \"string\" &&\n typeof value[\"mimeType\"] === \"string\" &&\n isOptionalString(value[\"filename\"])\n );\n case \"data\":\n return typeof value[\"name\"] === \"string\";\n case \"audio\": {\n const audio = value[\"audio\"];\n return (\n isRecord(audio) &&\n typeof audio[\"data\"] === \"string\" &&\n (audio[\"format\"] === \"mp3\" || audio[\"format\"] === \"wav\")\n );\n }\n default:\n return false;\n }\n};\n\ntype StoredAttachment = StoredDraft[\"attachments\"][number];\n\nconst isStoredAttachment = (value: unknown): value is StoredAttachment => {\n if (!isRecord(value)) return false;\n const status = value[\"status\"];\n return (\n typeof value[\"id\"] === \"string\" &&\n typeof value[\"type\"] === \"string\" &&\n typeof value[\"name\"] === \"string\" &&\n isOptionalString(value[\"contentType\"]) &&\n value[\"file\"] === undefined &&\n isRecord(status) &&\n status[\"type\"] === \"complete\" &&\n Array.isArray(value[\"content\"]) &&\n value[\"content\"].every(isUserMessagePart)\n );\n};\n\nconst isStoredDraft = (value: unknown): value is StoredDraft =>\n isRecord(value) &&\n typeof value[\"text\"] === \"string\" &&\n Array.isArray(value[\"attachments\"]) &&\n value[\"attachments\"].every(isStoredAttachment) &&\n typeof value[\"ts\"] === \"number\" &&\n Number.isFinite(value[\"ts\"]);\n\nconst isPositiveSafeInteger = (value: unknown): value is number =>\n typeof value === \"number\" && Number.isSafeInteger(value) && value > 0;\n\nconst isSessionCommand = (\n value: unknown,\n): value is StatewireClient.SessionCommand =>\n isRecord(value) &&\n isPositiveSafeInteger(value[\"seq\"]) &&\n typeof value[\"method\"] === \"string\" &&\n Array.isArray(value[\"params\"]);\n\nconst isSessionVerdict = (\n value: unknown,\n): value is StatewireClient.SessionVerdict =>\n isRecord(value) &&\n isPositiveSafeInteger(value[\"seq\"]) &&\n (value[\"type\"] === \"result\" ||\n value[\"type\"] === \"rejected\" ||\n value[\"type\"] === \"failed\" ||\n value[\"type\"] === \"crashed\" ||\n value[\"type\"] === \"expired\") &&\n isOptionalString(value[\"code\"]) &&\n isOptionalString(value[\"message\"]);\n\nconst isSendFailure = (\n value: unknown,\n): value is StatewireClient.SendFailure => {\n if (!isRecord(value)) return false;\n switch (value[\"fate\"]) {\n case \"rejected\":\n return (\n value[\"cause\"] === \"server\" &&\n isOptionalString(value[\"code\"]) &&\n isOptionalString(value[\"message\"])\n );\n case \"failed\":\n return value[\"cause\"] === \"server\" && isOptionalString(value[\"message\"]);\n case \"crashed\":\n case \"expired\":\n return value[\"cause\"] === \"server\";\n case \"aborted\":\n return (\n (value[\"cause\"] === \"unmount\" ||\n value[\"cause\"] === \"stream-gone\" ||\n value[\"cause\"] === \"session-lost\") &&\n (value[\"predecessorLost\"] === undefined ||\n value[\"predecessorLost\"] === true)\n );\n case \"lost\":\n return (\n value[\"cause\"] === \"unmount\" ||\n value[\"cause\"] === \"stream-gone\" ||\n value[\"cause\"] === \"session-lost\"\n );\n default:\n return false;\n }\n};\n\nconst isFailedCommand = (\n value: unknown,\n): value is StatewireClient.FailedCommand =>\n isRecord(value) &&\n typeof value[\"command\"] === \"string\" &&\n Array.isArray(value[\"params\"]) &&\n isSendFailure(value[\"failure\"]) &&\n typeof value[\"ts\"] === \"number\" &&\n Number.isFinite(value[\"ts\"]) &&\n isPositiveSafeInteger(value[\"seq\"]);\n\nconst isSessionRecord = (\n value: unknown,\n): value is StatewireClient.SessionRecord =>\n isRecord(value) &&\n typeof value[\"clientId\"] === \"string\" &&\n isPositiveSafeInteger(value[\"nextSeq\"]) &&\n isPositiveSafeInteger(value[\"ctxNextSeq\"]) &&\n Array.isArray(value[\"commands\"]) &&\n value[\"commands\"].every(isSessionCommand) &&\n (value[\"verdicts\"] === undefined ||\n (Array.isArray(value[\"verdicts\"]) &&\n value[\"verdicts\"].every(isSessionVerdict))) &&\n (value[\"failed\"] === undefined ||\n (Array.isArray(value[\"failed\"]) && value[\"failed\"].every(isFailedCommand)));\n\nconst loadStored = <T>(\n key: string,\n validate: (value: unknown) => value is T,\n): T | null => {\n const raw = localStorage.getItem(key);\n if (raw === null) return null;\n try {\n const value: unknown = JSON.parse(raw);\n if (validate(value)) return value;\n } catch {}\n localStorage.removeItem(key);\n return null;\n};\n\nconst sweep = (ttl: number, max: number) => {\n const entries: { key: string; ts: number }[] = [];\n const keys = Array.from({ length: localStorage.length }, (_, index) =>\n localStorage.key(index),\n );\n for (const key of keys) {\n if (key === null || !key.startsWith(PREFIX)) continue;\n const stored = loadStored(key, isStoredDraft);\n if (stored !== null) entries.push({ key, ts: stored.ts });\n }\n const now = Date.now();\n const fresh = entries.filter(({ key, ts }) => {\n if (now - ts <= ttl) return true;\n localStorage.removeItem(key);\n return false;\n });\n fresh.sort((a, b) => b.ts - a.ts);\n for (const { key } of fresh.slice(max)) localStorage.removeItem(key);\n};\n\nconst useLocalStorageComposerDrafts = ({\n threadId,\n key = (id) => id,\n ttl = 7 * DAY,\n max = 50,\n}: LocalStorageComposerDrafts.Options): StatewireThread.ComposerStorage => {\n if (typeof localStorage === \"undefined\")\n throw new Error(\n \"statewire: LocalStorageComposerDrafts requires localStorage\",\n );\n\n useEffect(() => {\n sweep(ttl, max);\n }, [ttl, max]);\n\n const storageKey = PREFIX + key(threadId);\n return useMemo(\n (): StatewireThread.ComposerStorage => ({\n read: () => {\n const stored = loadStored(storageKey, isStoredDraft);\n if (stored === null) return undefined;\n return { text: stored.text, attachments: stored.attachments };\n },\n write: (draft) => {\n if (draft.text === \"\" && draft.attachments.length === 0) {\n localStorage.removeItem(storageKey);\n return;\n }\n localStorage.setItem(\n storageKey,\n JSON.stringify({ ...draft, ts: Date.now() }),\n );\n },\n clear: () => {\n localStorage.removeItem(storageKey);\n },\n }),\n [storageKey],\n );\n};\n\n/** Composer draft storage on localStorage, keyed per thread; stale drafts are swept on mount. */\nexport const LocalStorageComposerDrafts = resource(\n useLocalStorageComposerDrafts,\n);\n\nconst SESSION_LOCK_PREFIX = \"aui:statewire-session-lock:\";\n\nconst useLocalStorageSession = ({\n threadId,\n migrate,\n key = (id) => id,\n}: LocalStorageSession.Options): StatewireClient.SessionStorage => {\n if (typeof localStorage === \"undefined\")\n throw new Error(\"statewire: LocalStorageSession requires localStorage\");\n // localStorage is shared across tabs: record mutations must serialize.\n if (typeof navigator === \"undefined\" || navigator.locks === undefined)\n throw new Error(\n \"statewire: LocalStorageSession requires the Web Locks API (navigator.locks)\",\n );\n\n const storageKey = SESSION_PREFIX + key(threadId);\n const lockName = SESSION_LOCK_PREFIX + key(threadId);\n return useMemo(\n (): StatewireClient.SessionStorage => ({\n load: () => {\n return loadStored(storageKey, isSessionRecord);\n },\n save: (record) => {\n localStorage.setItem(storageKey, JSON.stringify(record));\n },\n ...(migrate !== undefined && { migrate }),\n lock: (fn) => navigator.locks.request(lockName, async () => fn()),\n subscribe: (listener) => {\n const onStorage = (event: StorageEvent) => {\n if (event.key === storageKey) listener();\n };\n window.addEventListener(\"storage\", onStorage);\n return () => window.removeEventListener(\"storage\", onStorage);\n },\n }),\n [storageKey, lockName, migrate],\n );\n};\n\n/** Session storage on localStorage, keyed per thread: the lane identity plus the commands awaiting a durable verdict; sharers serialize through a per-key Web Lock. */\nexport const LocalStorageSession = resource(useLocalStorageSession);\n\nexport namespace LocalStorageSession {\n export type Options = {\n /** Upgrade incompatible queued commands, or return null to discard the saved session. */\n migrate?: StatewireClient.SessionStorage[\"migrate\"];\n /** The thread whose session this storage holds. */\n threadId: string;\n /** Derives the storage key from the thread id; defaults to the id itself. */\n key?: ((threadId: string) => string) | undefined;\n };\n}\n\nexport namespace LocalStorageComposerDrafts {\n export type Options = {\n /** The thread whose draft this storage holds. */\n threadId: string;\n /** Derives the storage key from the thread id; defaults to the id itself. */\n key?: ((threadId: string) => string) | undefined;\n /** Draft lifetime in milliseconds, enforced by the mount sweep. Default 7 days. */\n ttl?: number | undefined;\n /** Newest drafts kept across all keys, enforced by the mount sweep. Default 50. */\n max?: number | undefined;\n };\n}\n"],"mappings":";;;AAKA,MAAMK,SAAS;AACf,MAAMC,iBAAiB;AACvB,MAAMC,MAAM;AAQZ,MAAMM,YAAYC,UAChB,OAAOA,UAAU,YAAYA,UAAU,QAAQ,CAACE,M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oB,WAAWP,sBAAsB3C,IAAI8B,QAAQ;CACnD,OAAOvE,eACkC;EACrC4F,YAAY;GACV,OAAOpD,WAAWqC,YAAYvC,eAAe;EAC/C;EACAuD,OAAOC,WAAW;GAChBjD,aAAaoC,QAAQJ,YAAY9B,KAAKmC,UAAUY,MAAM,CAAC;EACzD;EACA,GAAIR,YAAYpE,KAAAA,KAAa,EAAEoE,QAAQ;EACvCS,OAAOC,OAAOP,UAAUC,MAAMO,QAAQN,UAAU,YAAYK,GAAG,CAAC;EAChEE,YAAYC,aAAa;GACvB,MAAMC,aAAaC,UAAwB;IACzC,IAAIA,MAAM5D,QAAQoC,YAAYsB,SAAS;GACzC;GACAI,OAAOC,iBAAiB,WAAWJ,SAAS;GAC5C,aAAaG,OAAOE,oBAAoB,WAAWL,SAAS;EAC9D;CACF,IACA;EAACvB;EAAYc;EAAUL;CAAO,CAChC;AACF;;AAGA,MAAaC,sBAAsBtF,SAASoF,sBAAsB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@assistant-ui/react-statewire",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.27.0",
|
|
4
4
|
"description": "assistant-ui thread binding for statewire backends",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
"@assistant-ui/store": "^0.3.0",
|
|
39
39
|
"@assistant-ui/tap": "^0.9.7",
|
|
40
40
|
"assistant-stream": "^0.3.37",
|
|
41
|
-
"statewire": "^0.
|
|
41
|
+
"statewire": "^0.17.0"
|
|
42
42
|
},
|
|
43
43
|
"peerDependencies": {
|
|
44
44
|
"@assistant-ui/core": ">=0.3.12",
|
|
@@ -8,7 +8,7 @@ import { act, render, waitFor } from "@testing-library/react";
|
|
|
8
8
|
import type { FC } from "react";
|
|
9
9
|
import { describe, expect, it } from "vitest";
|
|
10
10
|
import { AuiProvider, useAui } from "@assistant-ui/store";
|
|
11
|
-
import {
|
|
11
|
+
import { StatewireHttp } from "statewire";
|
|
12
12
|
import { asStatewireExtras, StatewireThread } from "./StatewireThread";
|
|
13
13
|
import { useStatewireRuns } from "./hooks";
|
|
14
14
|
import { sendCommand, toThreadMessage } from "./__tests__/testHelpers";
|
|
@@ -89,12 +89,18 @@ const createSettlingServer = (init?: Partial<ServerState>) => {
|
|
|
89
89
|
frame({
|
|
90
90
|
ops: [
|
|
91
91
|
{
|
|
92
|
-
op: "
|
|
93
|
-
|
|
92
|
+
op: "mount",
|
|
93
|
+
protocol: "default",
|
|
94
|
+
main: true,
|
|
94
95
|
value: { ...INITIAL_STATE, ...init },
|
|
95
96
|
},
|
|
96
97
|
],
|
|
97
|
-
syn: {
|
|
98
|
+
syn: {
|
|
99
|
+
seq: rec.seq,
|
|
100
|
+
ctxSeq: rec.ctxSeq,
|
|
101
|
+
version: "2026-09-07",
|
|
102
|
+
protocols: [{ name: "default", version: "2026-09-07" }],
|
|
103
|
+
},
|
|
98
104
|
}),
|
|
99
105
|
),
|
|
100
106
|
);
|
|
@@ -118,7 +124,7 @@ const createSettlingServer = (init?: Partial<ServerState>) => {
|
|
|
118
124
|
verdict = () => ({ type: "result" });
|
|
119
125
|
},
|
|
120
126
|
sendOps: (ops: unknown[]) => push({ ops }),
|
|
121
|
-
transport:
|
|
127
|
+
transport: StatewireHttp<ServerState>({
|
|
122
128
|
url: "http://fake/api",
|
|
123
129
|
fetch: fetchImpl,
|
|
124
130
|
}),
|
|
@@ -11,7 +11,7 @@ import type { ExternalThreadMessage } from "@assistant-ui/core/store";
|
|
|
11
11
|
import { describe, expect, it } from "vitest";
|
|
12
12
|
import { AuiProvider, useAui } from "@assistant-ui/store";
|
|
13
13
|
import { resource, withKey } from "@assistant-ui/tap";
|
|
14
|
-
import {
|
|
14
|
+
import { StatewireHttp } from "statewire";
|
|
15
15
|
import { useStatewireCommands, useStatewireState } from "statewire/host";
|
|
16
16
|
import { asStatewireExtras, StatewireThread } from "./StatewireThread";
|
|
17
17
|
import { runsMaterialize } from "./runsMaterialize";
|
|
@@ -183,12 +183,18 @@ const createRemoteServer = (
|
|
|
183
183
|
frame({
|
|
184
184
|
ops: [
|
|
185
185
|
{
|
|
186
|
-
op: "
|
|
187
|
-
|
|
186
|
+
op: "mount",
|
|
187
|
+
protocol: "default",
|
|
188
|
+
main: true,
|
|
188
189
|
value: { ...INITIAL_STATE, ...init },
|
|
189
190
|
},
|
|
190
191
|
],
|
|
191
|
-
syn: {
|
|
192
|
+
syn: {
|
|
193
|
+
seq: rec.seq,
|
|
194
|
+
ctxSeq: rec.ctxSeq,
|
|
195
|
+
version: "2026-09-07",
|
|
196
|
+
protocols: [{ name: "default", version: "2026-09-07" }],
|
|
197
|
+
},
|
|
192
198
|
}),
|
|
193
199
|
),
|
|
194
200
|
);
|
|
@@ -213,7 +219,7 @@ const createRemoteServer = (
|
|
|
213
219
|
received,
|
|
214
220
|
release,
|
|
215
221
|
fin,
|
|
216
|
-
transport:
|
|
222
|
+
transport: StatewireHttp<RunsServerState>({
|
|
217
223
|
url: "http://fake/api",
|
|
218
224
|
fetch: fetchImpl,
|
|
219
225
|
}),
|
package/src/localStorage.test.ts
CHANGED
package/src/localStorage.ts
CHANGED
|
@@ -245,6 +245,7 @@ const SESSION_LOCK_PREFIX = "aui:statewire-session-lock:";
|
|
|
245
245
|
|
|
246
246
|
const useLocalStorageSession = ({
|
|
247
247
|
threadId,
|
|
248
|
+
migrate,
|
|
248
249
|
key = (id) => id,
|
|
249
250
|
}: LocalStorageSession.Options): StatewireClient.SessionStorage => {
|
|
250
251
|
if (typeof localStorage === "undefined")
|
|
@@ -265,6 +266,7 @@ const useLocalStorageSession = ({
|
|
|
265
266
|
save: (record) => {
|
|
266
267
|
localStorage.setItem(storageKey, JSON.stringify(record));
|
|
267
268
|
},
|
|
269
|
+
...(migrate !== undefined && { migrate }),
|
|
268
270
|
lock: (fn) => navigator.locks.request(lockName, async () => fn()),
|
|
269
271
|
subscribe: (listener) => {
|
|
270
272
|
const onStorage = (event: StorageEvent) => {
|
|
@@ -274,7 +276,7 @@ const useLocalStorageSession = ({
|
|
|
274
276
|
return () => window.removeEventListener("storage", onStorage);
|
|
275
277
|
},
|
|
276
278
|
}),
|
|
277
|
-
[storageKey, lockName],
|
|
279
|
+
[storageKey, lockName, migrate],
|
|
278
280
|
);
|
|
279
281
|
};
|
|
280
282
|
|
|
@@ -283,6 +285,8 @@ export const LocalStorageSession = resource(useLocalStorageSession);
|
|
|
283
285
|
|
|
284
286
|
export namespace LocalStorageSession {
|
|
285
287
|
export type Options = {
|
|
288
|
+
/** Upgrade incompatible queued commands, or return null to discard the saved session. */
|
|
289
|
+
migrate?: StatewireClient.SessionStorage["migrate"];
|
|
286
290
|
/** The thread whose session this storage holds. */
|
|
287
291
|
threadId: string;
|
|
288
292
|
/** Derives the storage key from the thread id; defaults to the id itself. */
|