-
Notifications
You must be signed in to change notification settings - Fork 123
FE-1129: Token encoding playground (Storybook) #8943
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| "@hashintel/petrinaut-core": patch | ||
| "@hashintel/petrinaut": patch | ||
| --- | ||
|
|
||
| Export the token value codec and `compileUserCode` from `@hashintel/petrinaut-core`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
libs/@hashintel/petrinaut/src/ui/dev/token-encoding-playground/dimension-editor.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| import { Button, Select, TextInput } from "@hashintel/ds-components"; | ||
| import { css } from "@hashintel/ds-helpers/css"; | ||
|
|
||
| import type { PlaygroundDimension } from "./physical-layout"; | ||
| import type { SelectItem } from "@hashintel/ds-components"; | ||
| import type { ColorElementType } from "@hashintel/petrinaut-core"; | ||
|
|
||
| export type DimensionEditorProps = { | ||
| dimensions: PlaygroundDimension[]; | ||
| onChange: (dimensions: PlaygroundDimension[]) => void; | ||
| }; | ||
|
|
||
| const typeOptions: SelectItem<ColorElementType>[] = [ | ||
| { value: "real", text: "Real" }, | ||
| { value: "integer", text: "Integer" }, | ||
| { value: "boolean", text: "Boolean" }, | ||
| ]; | ||
|
|
||
| const listStyle = css({ | ||
| display: "flex", | ||
| flexDirection: "column", | ||
| gap: "1.5", | ||
| }); | ||
|
|
||
| const rowStyle = css({ | ||
| display: "flex", | ||
| alignItems: "center", | ||
| gap: "1", | ||
| }); | ||
|
|
||
| const nameInputStyle = css({ | ||
| display: "flex", | ||
| flex: "[1]", | ||
| minWidth: "[0]", | ||
|
|
||
| "& input": { | ||
| fontFamily: "mono", | ||
| }, | ||
| }); | ||
|
|
||
| const typeSelectStyle = css({ | ||
| width: "[110px]", | ||
| flexShrink: 0, | ||
| }); | ||
|
|
||
| /** | ||
| * Store-free version of the type-properties dimension list: local state only, | ||
| * for the playground story. | ||
| */ | ||
| export const DimensionEditor: React.FC<DimensionEditorProps> = ({ | ||
| dimensions, | ||
| onChange, | ||
| }) => { | ||
| const updateAt = (index: number, update: Partial<PlaygroundDimension>) => { | ||
| onChange( | ||
| dimensions.map((dimension, dimensionIndex) => | ||
| dimensionIndex === index ? { ...dimension, ...update } : dimension, | ||
| ), | ||
| ); | ||
| }; | ||
|
|
||
| return ( | ||
| <div className={listStyle}> | ||
| {dimensions.map((dimension, index) => ( | ||
| // eslint-disable-next-line react/no-array-index-key -- rows are positional | ||
| <div key={index} className={rowStyle}> | ||
| <TextInput | ||
| value={dimension.name} | ||
| size="sm" | ||
| width="fullWidth" | ||
| className={nameInputStyle} | ||
| placeholder="dimension_name" | ||
| onChange={(name) => updateAt(index, { name })} | ||
| connectToRightInput | ||
| /> | ||
| <Select | ||
| required | ||
| value={dimension.type} | ||
| onChange={(type) => updateAt(index, { type })} | ||
| items={typeOptions} | ||
| size="sm" | ||
| className={typeSelectStyle} | ||
| connectToLeftInput | ||
| /> | ||
| <Button | ||
| onClick={() => | ||
| onChange(dimensions.filter((_, other) => other !== index)) | ||
| } | ||
| size="xxs" | ||
| variant="ghost" | ||
| iconName="close" | ||
| aria-label={`Remove dimension ${dimension.name}`} | ||
| /> | ||
| </div> | ||
| ))} | ||
| <Button | ||
| onClick={() => | ||
| onChange([ | ||
| ...dimensions, | ||
| // First free dim_N: length alone can collide after removals. | ||
| { | ||
| name: (() => { | ||
| let index = dimensions.length; | ||
| while (dimensions.some((d) => d.name === `dim_${index}`)) { | ||
| index++; | ||
| } | ||
| return `dim_${index}`; | ||
| })(), | ||
| type: "real", | ||
| }, | ||
| ]) | ||
| } | ||
| size="xs" | ||
| variant="ghost" | ||
| iconName="plus" | ||
| > | ||
| Add dimension | ||
| </Button> | ||
| </div> | ||
| ); | ||
| }; | ||
18 changes: 18 additions & 0 deletions
18
libs/@hashintel/petrinaut/src/ui/dev/token-encoding-playground/monaco-typescript.d.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| /** | ||
| * monaco-editor 0.55 ships its TypeScript language service untyped β the | ||
| * contribution's `.d.ts` is an empty `export {}` and the old | ||
| * `monaco.languages.typescript` namespace types were removed. Declare the | ||
| * minimal surface the playground uses. | ||
| */ | ||
| declare module "monaco-editor/esm/vs/language/typescript/monaco.contribution.js" { | ||
| export type PlaygroundExtraLib = { content: string; filePath?: string }; | ||
|
|
||
| export const typescriptDefaults: { | ||
| setCompilerOptions(options: Record<string, unknown>): void; | ||
| setEagerModelSync(enabled: boolean): void; | ||
| setExtraLibs(libs: PlaygroundExtraLib[]): void; | ||
| setModeConfiguration(config: Record<string, boolean>): void; | ||
| }; | ||
|
|
||
| export const ScriptTarget: { ES2020: number }; | ||
| } |
104 changes: 104 additions & 0 deletions
104
libs/@hashintel/petrinaut/src/ui/dev/token-encoding-playground/physical-layout.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
|
|
||
| import { | ||
| computeTokenLayout, | ||
| decodeToken, | ||
| encodeToken, | ||
| getFieldBits, | ||
| getFieldHex, | ||
| } from "./physical-layout"; | ||
|
|
||
| import type { PlaygroundDimension } from "./physical-layout"; | ||
|
|
||
| const DIMENSIONS: PlaygroundDimension[] = [ | ||
| { name: "active", type: "boolean" }, | ||
| { name: "amount", type: "real" }, | ||
| { name: "count", type: "integer" }, | ||
| ]; | ||
|
|
||
| describe("computeTokenLayout", () => { | ||
| it("v1 keeps declaration order with one f64 slot per dimension", () => { | ||
| const layout = computeTokenLayout(DIMENSIONS, "v1"); | ||
|
|
||
| expect(layout.strideBytes).toBe(24); | ||
| expect(layout.paddingRanges).toEqual([]); | ||
| expect( | ||
| layout.fields.map((f) => [f.name, f.physical.kind, f.byteOffset]), | ||
| ).toEqual([ | ||
| ["active", "f64", 0], | ||
| ["amount", "f64", 8], | ||
| ["count", "f64", 16], | ||
| ]); | ||
| }); | ||
|
|
||
| it("v2 orders by decreasing alignment and pads the stride to 8", () => { | ||
| const layout = computeTokenLayout(DIMENSIONS, "v2"); | ||
|
|
||
| // amount and count (f64, align 8) first β stable relative order β then | ||
| // the u8 boolean, then 7 bytes of tail padding. | ||
| expect( | ||
| layout.fields.map((f) => [f.name, f.physical.kind, f.byteOffset]), | ||
| ).toEqual([ | ||
| ["amount", "f64", 0], | ||
| ["count", "f64", 8], | ||
| ["active", "u8", 16], | ||
| ]); | ||
| expect(layout.strideBytes).toBe(24); | ||
| expect(layout.paddingRanges).toEqual([{ start: 17, end: 24 }]); | ||
| }); | ||
|
|
||
| it("returns an empty layout for no dimensions", () => { | ||
| const layout = computeTokenLayout([], "v2"); | ||
| expect(layout.strideBytes).toBe(0); | ||
| expect(layout.fields).toEqual([]); | ||
| }); | ||
| }); | ||
|
|
||
| describe("encodeToken / decodeToken", () => { | ||
| it.each(["v1", "v2"] as const)( | ||
| "round-trips with product coercion in %s mode", | ||
| (mode) => { | ||
| const layout = computeTokenLayout(DIMENSIONS, mode); | ||
| const { stored, decoded } = encodeToken(layout, { | ||
| active: true, | ||
| amount: 1.25, | ||
| count: 2.7, | ||
| }); | ||
|
|
||
| expect(stored).toEqual({ active: true, amount: 1.25, count: 3 }); | ||
| expect(decoded).toEqual({ active: true, amount: 1.25, count: 3 }); | ||
| }, | ||
| ); | ||
|
|
||
| it("applies typed defaults for missing values", () => { | ||
| const layout = computeTokenLayout(DIMENSIONS, "v2"); | ||
| const { decoded } = encodeToken(layout, {}); | ||
| expect(decoded).toEqual({ active: false, amount: 0, count: 0 }); | ||
| }); | ||
|
|
||
| it("stores booleans as a single byte in v2", () => { | ||
| const layout = computeTokenLayout( | ||
| [{ name: "flag", type: "boolean" }], | ||
| "v2", | ||
| ); | ||
| const { buffer } = encodeToken(layout, { flag: true }); | ||
| expect(layout.strideBytes).toBe(8); | ||
| expect(new Uint8Array(buffer)[0]).toBe(1); | ||
| expect(decodeToken(layout, buffer)).toEqual({ flag: true }); | ||
| }); | ||
| }); | ||
|
|
||
| describe("bit inspection", () => { | ||
| it("exposes IEEE-754 bits MSB-first for f64 fields", () => { | ||
| const layout = computeTokenLayout([{ name: "x", type: "real" }], "v1"); | ||
| const { buffer } = encodeToken(layout, { x: -2 }); | ||
| const bits = getFieldBits(buffer, layout.fields[0]!); | ||
|
|
||
| // -2 = sign 1, exponent 0x400 (10000000000), mantissa all zero. | ||
| expect(bits).toHaveLength(64); | ||
| expect(bits[0]).toBe(1); | ||
| expect(bits.slice(1, 12).join("")).toBe("10000000000"); | ||
| expect(bits.slice(12).every((bit) => bit === 0)).toBe(true); | ||
| expect(getFieldHex(buffer, layout.fields[0]!)).toBe("0xc000000000000000"); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.