@assistant-ui/react-statewire 0.12.0 → 0.14.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 (56) hide show
  1. package/dist/StatewireThread.d.ts +83 -108
  2. package/dist/StatewireThread.d.ts.map +1 -1
  3. package/dist/StatewireThread.js +54 -79
  4. package/dist/StatewireThread.js.map +1 -1
  5. package/dist/hooks.d.ts +6 -9
  6. package/dist/hooks.d.ts.map +1 -1
  7. package/dist/hooks.js +9 -20
  8. package/dist/hooks.js.map +1 -1
  9. package/dist/index.d.ts +2 -3
  10. package/dist/index.js +2 -3
  11. package/dist/inputRequests.d.ts +4 -4
  12. package/dist/inputRequests.d.ts.map +1 -1
  13. package/dist/inputRequests.js +4 -2
  14. package/dist/inputRequests.js.map +1 -1
  15. package/dist/runCommands.d.ts +14 -19
  16. package/dist/runCommands.d.ts.map +1 -1
  17. package/dist/runCommands.js +29 -13
  18. package/dist/runCommands.js.map +1 -1
  19. package/dist/runsOptimistic.d.ts.map +1 -1
  20. package/dist/runsOptimistic.js +9 -8
  21. package/dist/runsOptimistic.js.map +1 -1
  22. package/package.json +4 -5
  23. package/src/StatewireThread.runs.test.tsx +67 -54
  24. package/src/StatewireThread.test.tsx +239 -431
  25. package/src/StatewireThread.ts +151 -266
  26. package/src/__tests__/testHelpers.tsx +6 -226
  27. package/src/hooks.ts +11 -32
  28. package/src/index.ts +2 -4
  29. package/src/inputRequests.ts +10 -8
  30. package/src/runCommands.ts +63 -31
  31. package/src/runsOptimistic.test.ts +64 -26
  32. package/src/runsOptimistic.ts +14 -9
  33. package/dist/AssistantTransportThread.d.ts +0 -216
  34. package/dist/AssistantTransportThread.d.ts.map +0 -1
  35. package/dist/AssistantTransportThread.js +0 -257
  36. package/dist/AssistantTransportThread.js.map +0 -1
  37. package/dist/host.d.ts +0 -38
  38. package/dist/host.d.ts.map +0 -1
  39. package/dist/host.js +0 -299
  40. package/dist/host.js.map +0 -1
  41. package/dist/replayBoundaryStream.d.ts +0 -6
  42. package/dist/replayBoundaryStream.d.ts.map +0 -1
  43. package/dist/replayBoundaryStream.js +0 -49
  44. package/dist/replayBoundaryStream.js.map +0 -1
  45. package/dist/stateOps.d.ts +0 -10
  46. package/dist/stateOps.d.ts.map +0 -1
  47. package/dist/stateOps.js +0 -114
  48. package/dist/stateOps.js.map +0 -1
  49. package/src/AssistantTransportThread.ts +0 -756
  50. package/src/athena.e2e.test.tsx +0 -652
  51. package/src/host.ts +0 -437
  52. package/src/legacy-diff.test.tsx +0 -627
  53. package/src/replayBoundaryStream.ts +0 -71
  54. package/src/stateOps.test.ts +0 -165
  55. package/src/stateOps.ts +0 -126
  56. package/src/transport.test.tsx +0 -2964
@@ -1,165 +0,0 @@
1
- import { describe, expect, it, vi } from "vitest";
2
- import type { AssistantTransportStateOperation } from "assistant-stream";
3
- import { translateStateOp } from "./stateOps";
4
- import type { TranslatedOp } from "./stateOps";
5
-
6
- const applyTranslated = (state: unknown, op: TranslatedOp): unknown => {
7
- if (op.op === "add" && op.path.length === 1) {
8
- const offset = op.path[0] as number;
9
- if (typeof state === "string") {
10
- return (
11
- state.slice(0, offset) + (op.value as string) + state.slice(offset)
12
- );
13
- }
14
- const list = state as unknown[];
15
- return [...list.slice(0, offset), op.value, ...list.slice(offset)];
16
- }
17
- if (op.path.length === 0) return op.value;
18
- const [key, ...rest] = op.path as [string | number, ...(string | number)[]];
19
- if (Array.isArray(state)) {
20
- const idx = key as number;
21
- const next = [...state];
22
- next[idx] = applyTranslated(state[idx], { ...op, path: rest });
23
- return next;
24
- }
25
- const obj = state as Record<string, unknown>;
26
- return { ...obj, [key]: applyTranslated(obj[key], { ...op, path: rest }) };
27
- };
28
-
29
- const apply = (
30
- state: unknown,
31
- ops: readonly AssistantTransportStateOperation[],
32
- ) => {
33
- for (const op of ops) {
34
- const translated = translateStateOp(state, op);
35
- if (translated !== undefined) state = applyTranslated(state, translated);
36
- }
37
- return state;
38
- };
39
-
40
- describe("translateStateOp", () => {
41
- it("sets an existing key", () => {
42
- expect(apply({ a: 1 }, [{ type: "set", path: ["a"], value: 2 }])).toEqual({
43
- a: 2,
44
- });
45
- });
46
-
47
- it("sets a new key", () => {
48
- expect(apply({ a: 1 }, [{ type: "set", path: ["b"], value: 2 }])).toEqual({
49
- a: 1,
50
- b: 2,
51
- });
52
- });
53
-
54
- it("auto-vivifies missing intermediates as objects", () => {
55
- expect(
56
- apply({}, [{ type: "set", path: ["a", "b", "c"], value: 1 }]),
57
- ).toEqual({ a: { b: { c: 1 } } });
58
- });
59
-
60
- it("auto-vivifies through null", () => {
61
- expect(
62
- apply({ a: null }, [{ type: "set", path: ["a", "b"], value: 1 }]),
63
- ).toEqual({ a: { b: 1 } });
64
- });
65
-
66
- it("replaces the whole state at the root path", () => {
67
- expect(
68
- apply({ a: 1 }, [{ type: "set", path: [], value: { b: 2 } }]),
69
- ).toEqual({ b: 2 });
70
- });
71
-
72
- it("appends text to a string", () => {
73
- expect(
74
- apply({ a: { text: "he" } }, [
75
- { type: "append-text", path: ["a", "text"], value: "llo" },
76
- ]),
77
- ).toEqual({ a: { text: "hello" } });
78
- });
79
-
80
- it("appends text at an array index", () => {
81
- expect(
82
- apply({ list: ["ab"] }, [
83
- { type: "append-text", path: ["list", "0"], value: "cd" },
84
- ]),
85
- ).toEqual({ list: ["abcd"] });
86
- });
87
-
88
- it("throws when appending text to a missing target", () => {
89
- expect(() =>
90
- apply({}, [{ type: "append-text", path: ["a", "b"], value: "x" }]),
91
- ).toThrow("Expected string at path [a, b]");
92
- });
93
-
94
- it("throws when appending text to a non-string", () => {
95
- expect(() =>
96
- apply({ a: 1 }, [{ type: "append-text", path: ["a"], value: "x" }]),
97
- ).toThrow("Expected string at path [a]");
98
- });
99
-
100
- it("sets an existing array index", () => {
101
- expect(
102
- apply({ list: [1, 2] }, [{ type: "set", path: ["list", "1"], value: 3 }]),
103
- ).toEqual({ list: [1, 3] });
104
- });
105
-
106
- it("extends an array at index == length", () => {
107
- expect(
108
- apply({ list: [1] }, [{ type: "set", path: ["list", "1"], value: 2 }]),
109
- ).toEqual({ list: [1, 2] });
110
- });
111
-
112
- it("vivifies nested values under a new array index", () => {
113
- expect(
114
- apply({ list: [] }, [
115
- { type: "set", path: ["list", "0", "a"], value: 1 },
116
- ]),
117
- ).toEqual({ list: [{ a: 1 }] });
118
- });
119
-
120
- it("throws when the array index is out of bounds", () => {
121
- expect(() =>
122
- apply({ list: [] }, [{ type: "set", path: ["list", "1"], value: 1 }]),
123
- ).toThrow("Insert array index out of bounds");
124
- });
125
-
126
- it("throws on a non-numeric array index", () => {
127
- expect(() =>
128
- apply({ list: [] }, [{ type: "set", path: ["list", "x"], value: 1 }]),
129
- ).toThrow("Expected array index at [x]");
130
- });
131
-
132
- it("throws when traversing through a primitive", () => {
133
- expect(() =>
134
- apply({ a: 1 }, [{ type: "set", path: ["a", "b"], value: 2 }]),
135
- ).toThrow("Invalid path: [b]");
136
- });
137
-
138
- it("warns and strips ops with unsafe path segments", () => {
139
- const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
140
- try {
141
- expect(
142
- apply({ a: 1 }, [{ type: "set", path: ["__proto__"], value: 1 }]),
143
- ).toEqual({ a: 1 });
144
- expect(warn).toHaveBeenCalledWith(
145
- 'statewire: stripped op with unsafe path segment "__proto__"',
146
- );
147
- } finally {
148
- warn.mockRestore();
149
- }
150
- });
151
-
152
- it("warns and strips ops with unsafe vivified segments", () => {
153
- const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
154
- try {
155
- expect(
156
- apply({}, [{ type: "set", path: ["a", "constructor"], value: 1 }]),
157
- ).toEqual({});
158
- expect(warn).toHaveBeenCalledWith(
159
- 'statewire: stripped op with unsafe path segment "constructor"',
160
- );
161
- } finally {
162
- warn.mockRestore();
163
- }
164
- });
165
- });
package/src/stateOps.ts DELETED
@@ -1,126 +0,0 @@
1
- import type { AssistantTransportStateOperation } from "assistant-stream";
2
- import type { Statewire } from "statewire";
3
-
4
- export type TranslatedOp = Extract<Statewire.Op, { op: "replace" | "add" }>;
5
-
6
- const UNSAFE_SEGMENTS = new Set(["__proto__", "constructor", "prototype"]);
7
-
8
- const assertStringTarget = (
9
- current: unknown,
10
- op: AssistantTransportStateOperation,
11
- ) => {
12
- if (typeof current !== "string") {
13
- throw new Error(`Expected string at path [${op.path.join(", ")}]`);
14
- }
15
- };
16
-
17
- // Missing intermediates auto-vivify as nested objects, exactly like gorp's updatePath.
18
- const vivified = (
19
- segments: readonly string[],
20
- from: number,
21
- op: AssistantTransportStateOperation,
22
- ): unknown => {
23
- if (op.type === "append-text") assertStringTarget(undefined, op);
24
- let value: unknown = op.value;
25
- for (let i = segments.length - 1; i >= from; i--) {
26
- value = { [segments[i]!]: value };
27
- }
28
- return value;
29
- };
30
-
31
- // Expresses a gorp `update-state` op as a statewire op against the current
32
- // state, replicating GorpStreamAccumulator's path semantics — including its
33
- // errors. Ops with unsafe path segments are stripped with a warning.
34
- export const translateStateOp = (
35
- root: unknown,
36
- op: AssistantTransportStateOperation,
37
- ): TranslatedOp | undefined => {
38
- const segments = op.path;
39
- const unsafe = segments.find((segment) => UNSAFE_SEGMENTS.has(segment));
40
- if (unsafe !== undefined) {
41
- console.warn(`statewire: stripped op with unsafe path segment "${unsafe}"`);
42
- return undefined;
43
- }
44
- const path: (string | number)[] = [];
45
- let node: unknown = root;
46
-
47
- for (let i = 0; i < segments.length; i++) {
48
- if (node === null || node === undefined) {
49
- return { op: "replace", path, value: vivified(segments, i, op) };
50
- }
51
- if (typeof node !== "object") {
52
- throw new Error(`Invalid path: [${segments.slice(i).join(", ")}]`);
53
- }
54
- const key = segments[i]!;
55
- const last = i === segments.length - 1;
56
-
57
- if (Array.isArray(node)) {
58
- // gorp parity: Number(""), and thus an empty-string key, is index 0
59
- const idx = Number(key);
60
- if (Number.isNaN(idx)) {
61
- throw new Error(
62
- `Expected array index at [${segments.slice(i).join(", ")}]`,
63
- );
64
- }
65
- if (idx > node.length || idx < 0) {
66
- throw new Error(`Insert array index out of bounds`);
67
- }
68
- const insert = idx === node.length;
69
- if (last) {
70
- if (op.type === "append-text") {
71
- const current = insert ? undefined : node[idx];
72
- assertStringTarget(current, op);
73
- return {
74
- op: "add",
75
- path: [...path, idx, (current as string).length],
76
- value: op.value,
77
- };
78
- }
79
- return {
80
- op: insert ? "add" : "replace",
81
- path: [...path, idx],
82
- value: op.value,
83
- };
84
- }
85
- if (insert || !Object.hasOwn(node, idx)) {
86
- return {
87
- op: insert ? "add" : "replace",
88
- path: [...path, idx],
89
- value: vivified(segments, i + 1, op),
90
- };
91
- }
92
- path.push(idx);
93
- node = node[idx];
94
- continue;
95
- }
96
-
97
- const obj = node as Record<string, unknown>;
98
- if (last) {
99
- if (op.type === "append-text") {
100
- const current = Object.hasOwn(obj, key) ? obj[key] : undefined;
101
- assertStringTarget(current, op);
102
- return {
103
- op: "add",
104
- path: [...path, key, (current as string).length],
105
- value: op.value,
106
- };
107
- }
108
- return { op: "replace", path: [...path, key], value: op.value };
109
- }
110
- if (!Object.hasOwn(obj, key)) {
111
- return {
112
- op: "replace",
113
- path: [...path, key],
114
- value: vivified(segments, i + 1, op),
115
- };
116
- }
117
- path.push(key);
118
- node = obj[key];
119
- }
120
-
121
- if (op.type === "append-text") {
122
- assertStringTarget(node, op);
123
- return { op: "add", path: [(node as string).length], value: op.value };
124
- }
125
- return { op: "replace", path: [], value: op.value };
126
- };