@assistant-ui/react-statewire 0.0.1
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/LICENSE +21 -0
- package/dist/AssistantTransportThread.d.ts +15 -0
- package/dist/AssistantTransportThread.d.ts.map +1 -0
- package/dist/AssistantTransportThread.js +172 -0
- package/dist/AssistantTransportThread.js.map +1 -0
- package/dist/hooks.d.ts +8 -0
- package/dist/hooks.d.ts.map +1 -0
- package/dist/hooks.js +26 -0
- package/dist/hooks.js.map +1 -0
- package/dist/host.d.ts +41 -0
- package/dist/host.d.ts.map +1 -0
- package/dist/host.js +294 -0
- package/dist/host.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +4 -0
- package/dist/replayBoundaryStream.d.ts +6 -0
- package/dist/replayBoundaryStream.d.ts.map +1 -0
- package/dist/replayBoundaryStream.js +47 -0
- package/dist/replayBoundaryStream.js.map +1 -0
- package/dist/stateOps.d.ts +10 -0
- package/dist/stateOps.d.ts.map +1 -0
- package/dist/stateOps.js +106 -0
- package/dist/stateOps.js.map +1 -0
- package/dist/types.d.ts +99 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +0 -0
- package/package.json +54 -0
- package/src/AssistantTransportThread.ts +295 -0
- package/src/__tests__/testHelpers.tsx +243 -0
- package/src/athena.e2e.test.tsx +628 -0
- package/src/hooks.ts +28 -0
- package/src/host.ts +413 -0
- package/src/index.ts +23 -0
- package/src/legacy-diff.test.tsx +581 -0
- package/src/replayBoundaryStream.ts +65 -0
- package/src/stateOps.test.ts +125 -0
- package/src/stateOps.ts +122 -0
- package/src/transport.test.tsx +1858 -0
- package/src/types.ts +140 -0
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import type { AssistantTransportStateOperation } from "assistant-stream";
|
|
3
|
+
import { applyOps } from "statewire";
|
|
4
|
+
import { translateStateOp } from "./stateOps";
|
|
5
|
+
|
|
6
|
+
const apply = (
|
|
7
|
+
state: unknown,
|
|
8
|
+
ops: readonly AssistantTransportStateOperation[],
|
|
9
|
+
) => {
|
|
10
|
+
for (const op of ops) {
|
|
11
|
+
state = applyOps(state, [translateStateOp(state, op)]).state;
|
|
12
|
+
}
|
|
13
|
+
return state;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
describe("translateStateOp", () => {
|
|
17
|
+
it("sets an existing key", () => {
|
|
18
|
+
expect(apply({ a: 1 }, [{ type: "set", path: ["a"], value: 2 }])).toEqual({
|
|
19
|
+
a: 2,
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it("sets a new key", () => {
|
|
24
|
+
expect(apply({ a: 1 }, [{ type: "set", path: ["b"], value: 2 }])).toEqual({
|
|
25
|
+
a: 1,
|
|
26
|
+
b: 2,
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it("auto-vivifies missing intermediates as objects", () => {
|
|
31
|
+
expect(
|
|
32
|
+
apply({}, [{ type: "set", path: ["a", "b", "c"], value: 1 }]),
|
|
33
|
+
).toEqual({ a: { b: { c: 1 } } });
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it("auto-vivifies through null", () => {
|
|
37
|
+
expect(
|
|
38
|
+
apply({ a: null }, [{ type: "set", path: ["a", "b"], value: 1 }]),
|
|
39
|
+
).toEqual({ a: { b: 1 } });
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it("replaces the whole state at the root path", () => {
|
|
43
|
+
expect(
|
|
44
|
+
apply({ a: 1 }, [{ type: "set", path: [], value: { b: 2 } }]),
|
|
45
|
+
).toEqual({ b: 2 });
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("appends text to a string", () => {
|
|
49
|
+
expect(
|
|
50
|
+
apply({ a: { text: "he" } }, [
|
|
51
|
+
{ type: "append-text", path: ["a", "text"], value: "llo" },
|
|
52
|
+
]),
|
|
53
|
+
).toEqual({ a: { text: "hello" } });
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("appends text at an array index", () => {
|
|
57
|
+
expect(
|
|
58
|
+
apply({ list: ["ab"] }, [
|
|
59
|
+
{ type: "append-text", path: ["list", "0"], value: "cd" },
|
|
60
|
+
]),
|
|
61
|
+
).toEqual({ list: ["abcd"] });
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("throws when appending text to a missing target", () => {
|
|
65
|
+
expect(() =>
|
|
66
|
+
apply({}, [{ type: "append-text", path: ["a", "b"], value: "x" }]),
|
|
67
|
+
).toThrow("Expected string at path [a, b]");
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it("throws when appending text to a non-string", () => {
|
|
71
|
+
expect(() =>
|
|
72
|
+
apply({ a: 1 }, [{ type: "append-text", path: ["a"], value: "x" }]),
|
|
73
|
+
).toThrow("Expected string at path [a]");
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("sets an existing array index", () => {
|
|
77
|
+
expect(
|
|
78
|
+
apply({ list: [1, 2] }, [{ type: "set", path: ["list", "1"], value: 3 }]),
|
|
79
|
+
).toEqual({ list: [1, 3] });
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it("extends an array at index == length", () => {
|
|
83
|
+
expect(
|
|
84
|
+
apply({ list: [1] }, [{ type: "set", path: ["list", "1"], value: 2 }]),
|
|
85
|
+
).toEqual({ list: [1, 2] });
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it("vivifies nested values under a new array index", () => {
|
|
89
|
+
expect(
|
|
90
|
+
apply({ list: [] }, [
|
|
91
|
+
{ type: "set", path: ["list", "0", "a"], value: 1 },
|
|
92
|
+
]),
|
|
93
|
+
).toEqual({ list: [{ a: 1 }] });
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it("throws when the array index is out of bounds", () => {
|
|
97
|
+
expect(() =>
|
|
98
|
+
apply({ list: [] }, [{ type: "set", path: ["list", "1"], value: 1 }]),
|
|
99
|
+
).toThrow("Insert array index out of bounds");
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it("throws on a non-numeric array index", () => {
|
|
103
|
+
expect(() =>
|
|
104
|
+
apply({ list: [] }, [{ type: "set", path: ["list", "x"], value: 1 }]),
|
|
105
|
+
).toThrow("Expected array index at [x]");
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it("throws when traversing through a primitive", () => {
|
|
109
|
+
expect(() =>
|
|
110
|
+
apply({ a: 1 }, [{ type: "set", path: ["a", "b"], value: 2 }]),
|
|
111
|
+
).toThrow("Invalid path: [b]");
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it("throws on unsafe path segments", () => {
|
|
115
|
+
expect(() =>
|
|
116
|
+
apply({}, [{ type: "set", path: ["__proto__"], value: 1 }]),
|
|
117
|
+
).toThrow("Unsafe gorp path segment: __proto__");
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it("throws on unsafe vivified segments", () => {
|
|
121
|
+
expect(() =>
|
|
122
|
+
apply({}, [{ type: "set", path: ["a", "constructor"], value: 1 }]),
|
|
123
|
+
).toThrow("Unsafe gorp path segment: constructor");
|
|
124
|
+
});
|
|
125
|
+
});
|
package/src/stateOps.ts
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import type { AssistantTransportStateOperation } from "assistant-stream";
|
|
2
|
+
import type { Statewire } from "statewire";
|
|
3
|
+
|
|
4
|
+
export type TranslatedOp = Extract<
|
|
5
|
+
Statewire.Op,
|
|
6
|
+
{ op: "replace" | "add" | "append" }
|
|
7
|
+
>;
|
|
8
|
+
|
|
9
|
+
const UNSAFE_SEGMENTS = new Set(["__proto__", "constructor", "prototype"]);
|
|
10
|
+
|
|
11
|
+
const assertSafeSegment = (segment: string) => {
|
|
12
|
+
if (UNSAFE_SEGMENTS.has(segment)) {
|
|
13
|
+
throw new Error(`Unsafe gorp path segment: ${segment}`);
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const assertStringTarget = (
|
|
18
|
+
current: unknown,
|
|
19
|
+
op: AssistantTransportStateOperation,
|
|
20
|
+
) => {
|
|
21
|
+
if (typeof current !== "string") {
|
|
22
|
+
throw new Error(`Expected string at path [${op.path.join(", ")}]`);
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
// Missing intermediates auto-vivify as nested objects, exactly like gorp's updatePath.
|
|
27
|
+
const vivified = (
|
|
28
|
+
segments: readonly string[],
|
|
29
|
+
from: number,
|
|
30
|
+
op: AssistantTransportStateOperation,
|
|
31
|
+
): unknown => {
|
|
32
|
+
if (op.type === "append-text") assertStringTarget(undefined, op);
|
|
33
|
+
let value: unknown = op.value;
|
|
34
|
+
for (let i = segments.length - 1; i >= from; i--) {
|
|
35
|
+
assertSafeSegment(segments[i]!);
|
|
36
|
+
value = { [segments[i]!]: value };
|
|
37
|
+
}
|
|
38
|
+
return value;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
// Expresses a gorp `update-state` op as a statewire op against the current
|
|
42
|
+
// state, replicating GorpStreamAccumulator's path semantics — including its
|
|
43
|
+
// errors.
|
|
44
|
+
export const translateStateOp = (
|
|
45
|
+
root: unknown,
|
|
46
|
+
op: AssistantTransportStateOperation,
|
|
47
|
+
): TranslatedOp => {
|
|
48
|
+
const segments = op.path;
|
|
49
|
+
const path: (string | number)[] = [];
|
|
50
|
+
let node: unknown = root;
|
|
51
|
+
|
|
52
|
+
for (let i = 0; i < segments.length; i++) {
|
|
53
|
+
if (node === null || node === undefined) {
|
|
54
|
+
return { op: "replace", path, value: vivified(segments, i, op) };
|
|
55
|
+
}
|
|
56
|
+
if (typeof node !== "object") {
|
|
57
|
+
throw new Error(`Invalid path: [${segments.slice(i).join(", ")}]`);
|
|
58
|
+
}
|
|
59
|
+
const key = segments[i]!;
|
|
60
|
+
assertSafeSegment(key);
|
|
61
|
+
const last = i === segments.length - 1;
|
|
62
|
+
|
|
63
|
+
if (Array.isArray(node)) {
|
|
64
|
+
// gorp parity: Number(""), and thus an empty-string key, is index 0
|
|
65
|
+
const idx = Number(key);
|
|
66
|
+
if (Number.isNaN(idx)) {
|
|
67
|
+
throw new Error(
|
|
68
|
+
`Expected array index at [${segments.slice(i).join(", ")}]`,
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
if (idx > node.length || idx < 0) {
|
|
72
|
+
throw new Error(`Insert array index out of bounds`);
|
|
73
|
+
}
|
|
74
|
+
const insert = idx === node.length;
|
|
75
|
+
if (last) {
|
|
76
|
+
if (op.type === "append-text") {
|
|
77
|
+
assertStringTarget(insert ? undefined : node[idx], op);
|
|
78
|
+
return { op: "append", path: [...path, idx], value: op.value };
|
|
79
|
+
}
|
|
80
|
+
return {
|
|
81
|
+
op: insert ? "add" : "replace",
|
|
82
|
+
path: [...path, idx],
|
|
83
|
+
value: op.value,
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
if (insert || !Object.hasOwn(node, idx)) {
|
|
87
|
+
return {
|
|
88
|
+
op: insert ? "add" : "replace",
|
|
89
|
+
path: [...path, idx],
|
|
90
|
+
value: vivified(segments, i + 1, op),
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
path.push(idx);
|
|
94
|
+
node = node[idx];
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const obj = node as Record<string, unknown>;
|
|
99
|
+
if (last) {
|
|
100
|
+
if (op.type === "append-text") {
|
|
101
|
+
assertStringTarget(Object.hasOwn(obj, key) ? obj[key] : undefined, op);
|
|
102
|
+
return { op: "append", path: [...path, key], value: op.value };
|
|
103
|
+
}
|
|
104
|
+
return { op: "replace", path: [...path, key], value: op.value };
|
|
105
|
+
}
|
|
106
|
+
if (!Object.hasOwn(obj, key)) {
|
|
107
|
+
return {
|
|
108
|
+
op: "replace",
|
|
109
|
+
path: [...path, key],
|
|
110
|
+
value: vivified(segments, i + 1, op),
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
path.push(key);
|
|
114
|
+
node = obj[key];
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (op.type === "append-text") {
|
|
118
|
+
assertStringTarget(node, op);
|
|
119
|
+
return { op: "append", path: [], value: op.value };
|
|
120
|
+
}
|
|
121
|
+
return { op: "replace", path: [], value: op.value };
|
|
122
|
+
};
|