-
-
Notifications
You must be signed in to change notification settings - Fork 0
feat(runtime): make the headless agent runtime DOM-free #69
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,10 @@ | ||
| --- | ||
| "@ignite-element/core": minor | ||
| "@ignite-element/adapters": minor | ||
| "@ignite-element/renderer": minor | ||
| "ignite-element": minor | ||
| --- | ||
|
|
||
| Make the headless agent runtime DOM-free, so `getSchema()` / `execute()` / `on()` / `watchView()` work in pure Node and edge runtimes with no jsdom polyfill. | ||
|
|
||
| The agent runtime is meant to be headless, but it allocated its internal host element via `document.createElement`, so `getSchema()` / `execute()` threw `document is not defined` in a non-DOM runtime. That host is only ever used as an **EventTarget** — `on()` registers `host.addEventListener` / `removeEventListener` and effect emits go through `host.dispatchEvent` — so a real element was never required for headless use. `createRuntimeHost` now falls back to a bare `EventTarget` when there is no `document` (Node 22 ships `EventTarget` + `CustomEvent` globally), and keeps `document.createElement` when a real or jsdom DOM is present (no behavior change in the browser or in tests). The DOM render path (the custom element / DOM bridge) is unchanged and still requires a real DOM. This unblocks running an igniteTools agent loop — the act → observe → act surface — headless on a server, CLI, or edge device with zero DOM shim. |
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
95 changes: 95 additions & 0 deletions
95
packages/ignite-element/src/tests/agent-runtime-headless-node.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,95 @@ | ||
| // @vitest-environment node | ||
| // | ||
| // The headless agent runtime must be DOM-free: getSchema()/execute()/on()/ | ||
| // watchView() have to work in pure Node (no jsdom). Before the createRuntimeHost | ||
| // fix these threw "document is not defined" via createRuntimeHost -> | ||
| // document.createElement. This file runs in the `node` environment (overriding | ||
| // the package's global jsdom) so it would fail without a genuinely DOM-free | ||
| // runtime. The DOM render path is intentionally not exercised here — it still | ||
| // requires a real DOM. | ||
| import { describe, expect, it, vi } from "vitest"; | ||
| import { assign, setup } from "xstate"; | ||
| import { igniteCore } from "../xstate"; | ||
|
|
||
| function createCounter() { | ||
| const machine = setup({ | ||
| types: { | ||
| context: {} as { count: number }, | ||
| events: {} as { type: "INC" }, | ||
| }, | ||
| }).createMachine({ | ||
| id: "headless-counter", | ||
| context: { count: 0 }, | ||
| initial: "active", | ||
| states: { | ||
| active: { | ||
| on: { | ||
| INC: { | ||
| actions: assign({ count: ({ context }) => context.count + 1 }), | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| return igniteCore({ | ||
| source: machine, | ||
| events: (event) => ({ counted: event<{ count: number }>() }), | ||
| view: ({ snapshot }) => ({ count: snapshot.context.count }), | ||
| commands: ({ actor }) => ({ | ||
| increment: () => actor.send({ type: "INC" }), | ||
| }), | ||
| effects: ({ emit, select }) => { | ||
| const count = select((state) => state.context.count); | ||
| if (count.changed) { | ||
| emit("counted", { count: count.current }); | ||
| } | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| describe("agent runtime is DOM-free (pure Node, no jsdom)", () => { | ||
| it("runs in an environment with no document", () => { | ||
| expect(typeof document).toBe("undefined"); | ||
| }); | ||
|
|
||
| it("getSchema() builds the manifest without a DOM", () => { | ||
| const counter = createCounter(); | ||
| const schema = counter.getSchema(); | ||
| expect(Object.keys(schema.commands)).toContain("increment"); | ||
| expect(schema.events).toContain("counted"); | ||
| expect(schema.view).toMatchObject({ count: 0 }); | ||
| }); | ||
|
|
||
| it("execute() runs a command and returns the post-ack snapshot + events", async () => { | ||
| const counter = createCounter(); | ||
| const result = await counter.execute("increment"); | ||
| expect(result.state.context.count).toBe(1); | ||
| expect(result.events).toEqual([{ type: "counted", payload: { count: 1 } }]); | ||
| }); | ||
|
|
||
| it("on() receives effect-emitted events via the host EventTarget", async () => { | ||
| const counter = createCounter(); | ||
| const handler = vi.fn(); | ||
| const subscription = counter.on("counted", handler); | ||
|
|
||
| await counter.execute("increment"); | ||
|
|
||
| expect(handler).toHaveBeenCalledTimes(1); | ||
| const event = handler.mock.calls[0][0] as CustomEvent<{ count: number }>; | ||
| expect(event.detail).toEqual({ count: 1 }); | ||
| subscription.unsubscribe(); | ||
| }); | ||
|
|
||
| it("watchView()/getView() observe the derived view without a DOM", async () => { | ||
| const counter = createCounter(); | ||
| const seen: Array<{ count: number }> = []; | ||
| const subscription = counter.watchView((view) => seen.push(view)); | ||
|
|
||
| await counter.execute("increment"); | ||
|
|
||
| expect(counter.getView()).toEqual({ count: 1 }); | ||
| expect(seen[seen.length - 1]).toEqual({ count: 1 }); | ||
| subscription.unsubscribe(); | ||
| }); | ||
| }); |
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.