diff --git a/README.md b/README.md index 7d97631..ff1c6a3 100644 --- a/README.md +++ b/README.md @@ -154,6 +154,9 @@ future renderers can consume the same tree and send back `story.*` or See [docs/eve.md](docs/eve.md). +Sai also publishes a retained Ink playback sidecar for runtime-independent Eve +clients. See [docs/eve-sidecar.md](docs/eve-sidecar.md). + ## Development ```powershell diff --git a/docs/eve-sidecar.md b/docs/eve-sidecar.md new file mode 100644 index 0000000..54bcc56 --- /dev/null +++ b/docs/eve-sidecar.md @@ -0,0 +1,28 @@ +# Sai Eve Sidecar + +Sai owns retained Ink playback sessions behind the Eve plugin ABI. The sidecar +loads compiled Ink with `inkjs`, applies story commands, and projects the live +session through Sai's `gamecult.eve.surface.v1` module. + +The sidecar does not render a runtime, accept provider commands on a provider's +behalf, or execute Norn or TeX semantics. It advertises `norn.graph` and +`tex.math` only as independent optional nested plugins. + +## Transport + +`src/eve-sidecar.cjs` is a long-running NDJSON process. Each input line is a +`gamecult.eve.plugin_abi.request.v1`; each output line is the correlated +`gamecult.eve.plugin_abi.response.v1`. + +The retained operations are: + +- `open`: create or replace a named Ink session and advance it to its first line. +- `apply`: apply `story.continue`, `story.choose`, or `story.jump` to that session. +- `project`: derive a Sai Eve surface from current session state. +- `describe`: report the sidecar's owned and optional capabilities. + +Run the owner-side conformance witness with: + +```powershell +powershell -ExecutionPolicy Bypass -File scripts/run-eve-plugin-witness.ps1 +``` diff --git a/package.json b/package.json index 4e43670..64f6b71 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,7 @@ "dist", "src", "docs", + "plugins", "examples", "scripts", "README.md", @@ -20,13 +21,19 @@ "./eve.js": "./dist/eve.js", "./ink.js": "./dist/ink.js" }, + "bin": { + "sai-eve-sidecar": "./src/eve-sidecar.cjs" + }, "scripts": { "build": "node scripts/build.mjs", "check": "node scripts/check.mjs", + "check:eve-plugin": "powershell -ExecutionPolicy Bypass -File scripts/run-eve-plugin-witness.ps1", "compile:example": "inkjs-compiler examples/static-site/story.ink -o examples/static-site/story.ink.json" }, + "dependencies": { + "inkjs": "^2.4.0" + }, "devDependencies": { - "inkjs": "^2.4.0", "prettier": "^3.5.3" }, "keywords": [ diff --git a/plugins/sai-vn.plugin-abi-fixture.json b/plugins/sai-vn.plugin-abi-fixture.json new file mode 100644 index 0000000..98d8d18 --- /dev/null +++ b/plugins/sai-vn.plugin-abi-fixture.json @@ -0,0 +1,55 @@ +{ + "schema": "gamecult.eve.plugin_abi_fixture.v1", + "fixtureId": "sai-vn-live-playback", + "pluginId": "sai.vn", + "ownerRepo": "Sai", + "contract": "gamecult.eve.plugin_abi.v1", + "requestSchema": "gamecult.eve.plugin_abi.request.v1", + "responseSchema": "gamecult.eve.plugin_abi.response.v1", + "operations": [ + { + "operation": "describe", + "expect": { + "sessionAuthority": "sai-sidecar", + "optionalPlugins": ["norn.graph", "tex.math"] + } + }, + { + "operation": "open", + "input": { "sessionId": "witness", "storyId": "sai-example", "storyPath": "examples/static-site/story.ink.json" }, + "expect": { + "sessionId": "witness", + "line": "Ink is not alive by itself. A page gives it a body.", + "revision": 0 + } + }, + { + "operation": "apply", + "input": { "sessionId": "witness", "command": "story.choose", "index": 0 }, + "expect": { + "line": "Then the story should feel like it belongs here, not like a widget escaped from somewhere else.", + "revision": 1, + "command": "story.choose" + } + }, + { + "operation": "project", + "input": { "sessionId": "witness", "providerId": "sai.witness" }, + "expect": { + "projectionKind": "semantic-surface", + "delegatedPlugins": ["norn.graph", "tex.math"], + "sessionId": "witness", + "revision": 1 + } + }, + { + "operation": "apply", + "input": { "sessionId": "witness", "command": "story.jump", "targetPath": "gather" }, + "expect": { + "line": "Good. The brush moves. The scene answers.", + "revision": 2, + "command": "story.jump" + } + } + ] +} diff --git a/plugins/sai-vn.plugin-advertisement.json b/plugins/sai-vn.plugin-advertisement.json new file mode 100644 index 0000000..a3329fb --- /dev/null +++ b/plugins/sai-vn.plugin-advertisement.json @@ -0,0 +1,23 @@ +{ + "schema": "gamecult.eve.plugin_advertisement.v1", + "pluginId": "sai.vn", + "ownerService": "sai", + "version": "0.1.0", + "manifestAddress": "file://plugins/sai-vn.plugin.json", + "runtime": { + "invocationModel": "executable-sidecar", + "contract": "gamecult.eve.plugin_abi.v1", + "transports": ["stdio"], + "sidecar": { + "processKind": "long-running-daemon", + "protocol": "stdio-ndjson", + "requestSchema": "gamecult.eve.plugin_abi.request.v1", + "responseSchema": "gamecult.eve.plugin_abi.response.v1", + "operations": ["describe", "open", "apply", "project"], + "launch": { "command": "node", "artifact": "src/eve-sidecar.cjs" }, + "stateAuthority": "sai-sidecar-owns-story-session-provider-owns-command-acceptance" + } + }, + "componentKinds": ["vn.stage", "panel.dialogue", "text.dialogue", "rail.actions"], + "commands": ["story.continue", "story.choose", "story.jump"] +} diff --git a/plugins/sai-vn.plugin.json b/plugins/sai-vn.plugin.json new file mode 100644 index 0000000..ee126a1 --- /dev/null +++ b/plugins/sai-vn.plugin.json @@ -0,0 +1,31 @@ +{ + "schema": "gamecult.eve.plugin.v1", + "pluginId": "sai.vn", + "title": "Sai Visual Novel", + "owner": "Sai", + "componentKinds": ["vn.stage", "panel.dialogue", "text.dialogue", "rail.actions"], + "commands": [ + { "command": "story.continue", "schema": "sai.vn.story_command.v1", "effect": "advance-story" }, + { "command": "story.choose", "schema": "sai.vn.story_command.v1", "effect": "choose-branch" }, + { "command": "story.jump", "schema": "sai.vn.story_command.v1", "effect": "jump-to-knot" } + ], + "runtime": { + "invocationModel": "executable-sidecar", + "contract": "gamecult.eve.plugin_abi.v1", + "transports": ["stdio"], + "sidecar": { + "processKind": "long-running-daemon", + "protocol": "stdio-ndjson", + "operations": ["describe", "open", "apply", "project"], + "stateAuthority": "sai-sidecar-owns-story-session-provider-owns-command-acceptance" + } + }, + "stateSchemas": ["sai.vn.story_session.v1", "sai.vn.visual_manifest.v1"], + "capabilities": { + "requires": ["vn.stage", "story.choose", "story.continue", "story.jump"], + "optionalPlugins": [ + { "pluginId": "norn.graph", "relationship": "optional-nested-composition" }, + { "pluginId": "tex.math", "relationship": "optional-nested-composition" } + ] + } +} diff --git a/scripts/check.mjs b/scripts/check.mjs index b5a9764..ef8108f 100644 --- a/scripts/check.mjs +++ b/scripts/check.mjs @@ -8,6 +8,10 @@ const required = [ "dist/eve.js", "examples/static-site/index.html", "docs/manifest.md", + "plugins/sai-vn.plugin.json", + "plugins/sai-vn.plugin-advertisement.json", + "plugins/sai-vn.plugin-abi-fixture.json", + "src/eve-sidecar.cjs", ]; for (const file of required) { diff --git a/scripts/run-eve-plugin-witness.ps1 b/scripts/run-eve-plugin-witness.ps1 new file mode 100644 index 0000000..3a3d623 --- /dev/null +++ b/scripts/run-eve-plugin-witness.ps1 @@ -0,0 +1,22 @@ +param( + [string] $EveRoot = "E:\Projects\Eve", + [string] $OutputPath = "artifacts\eve-plugin\runtime-witness.json", + [string] $NodePath = "node" +) + +$ErrorActionPreference = "Stop" +$repoRoot = Split-Path -Parent $PSScriptRoot +$output = if ([IO.Path]::IsPathRooted($OutputPath)) { $OutputPath } else { Join-Path $repoRoot $OutputPath } +$launcher = Join-Path $EveRoot "tools\plugins\run-sidecar-witness.mjs" + +& $NodePath $launcher ` + --advertisement (Join-Path $repoRoot "plugins\sai-vn.plugin-advertisement.json") ` + --fixture (Join-Path $repoRoot "plugins\sai-vn.plugin-abi-fixture.json") ` + --output $output ` + --witness-id "sai.vn.owner-sidecar" ` + --cwd $repoRoot ` + --command $NodePath ` + -- "src/eve-sidecar.cjs" +if ($LASTEXITCODE -ne 0) { throw "Sai Eve plugin witness failed with exit code $LASTEXITCODE" } + +Write-Host "Sai Eve plugin witness: $output" diff --git a/src/eve-sidecar.cjs b/src/eve-sidecar.cjs new file mode 100644 index 0000000..c21fd9b --- /dev/null +++ b/src/eve-sidecar.cjs @@ -0,0 +1,147 @@ +#!/usr/bin/env node + +const fs = require("node:fs"); +const path = require("node:path"); +const readline = require("node:readline"); +const { Story } = require("inkjs"); +const SaiEve = require("./eve.js"); + +const sessions = new Map(); +const pluginId = "sai.vn"; +const operations = ["describe", "open", "apply", "project"]; + +readline.createInterface({ input: process.stdin }).on("line", (line) => { + if (!line.trim()) return; + let request; + try { + request = JSON.parse(line.replace(/^\uFEFF/, "")); + respond(request, dispatch(request)); + } catch (error) { + respond(request, {}, "rejected", error.message); + } +}); + +function dispatch(request) { + if (request?.schema !== "gamecult.eve.plugin_abi.request.v1") throw new Error("Unsupported request schema."); + if (request.pluginId !== pluginId) throw new Error(`Expected plugin ${pluginId}.`); + if (!operations.includes(request.operation)) throw new Error(`Unsupported operation ${request.operation}.`); + if (request.operation === "describe") return describe(); + if (request.operation === "open") return openSession(request.input); + if (request.operation === "apply") return applyCommand(request.input); + return projectSession(request.input); +} + +function describe() { + return { + pluginId, + componentKinds: ["vn.stage", "panel.dialogue", "text.dialogue", "rail.actions"], + commands: ["story.continue", "story.choose", "story.jump"], + capabilities: ["vn.stage", "story.choose", "story.continue", "story.jump"], + operations, + optionalPlugins: ["norn.graph", "tex.math"], + sessionAuthority: "sai-sidecar", + }; +} + +function openSession(input) { + const sessionId = requiredString(input.sessionId, "sessionId"); + const storyPath = path.resolve(process.cwd(), requiredString(input.storyPath, "storyPath")); + const story = new Story(parseJson(fs.readFileSync(storyPath, "utf8"))); + const session = { sessionId, storyId: input.storyId || path.basename(storyPath, ".ink.json"), story, revision: 0, line: "", tags: [] }; + sessions.set(sessionId, session); + advance(session); + return snapshot(session); +} + +function applyCommand(input) { + const session = getSession(input.sessionId); + const command = requiredString(input.command, "command"); + if (command === "story.continue") { + advance(session); + } else if (command === "story.choose") { + const index = Number(input.index); + if (!Number.isInteger(index) || index < 0 || index >= session.story.currentChoices.length) throw new Error("Choice index is unavailable."); + session.story.ChooseChoiceIndex(index); + advance(session); + } else if (command === "story.jump") { + session.story.ChoosePathString(requiredString(input.targetPath, "targetPath")); + advance(session); + } else { + throw new Error(`Unsupported Sai command ${command}.`); + } + session.revision += 1; + return { ...snapshot(session), command, receiptSchema: "gamecult.eve.command_receipt.v1" }; +} + +function projectSession(input) { + const session = getSession(input.sessionId); + return { + projectionKind: "semantic-surface", + ownedComponentKinds: ["vn.stage", "panel.dialogue", "text.dialogue", "rail.actions"], + delegatedPlugins: ["norn.graph", "tex.math"], + surface: SaiEve.createVisualNovelSurface({ + providerId: input.providerId || pluginId, + storyId: session.storyId, + version: session.revision + 1, + metadata: tagMap(session.tags), + speaker: tagMap(session.tags).speaker, + line: session.line, + choices: choices(session.story), + }), + ...snapshot(session), + }; +} + +function advance(session) { + session.line = ""; + session.tags = []; + while (session.story.canContinue && !session.line) { + session.line = session.story.Continue().trim(); + session.tags = [...session.story.currentTags]; + } +} + +function snapshot(session) { + return { + sessionId: session.sessionId, + storyId: session.storyId, + revision: session.revision, + line: session.line, + choices: choices(session.story), + canContinue: session.story.canContinue, + currentTags: session.tags, + }; +} + +function choices(story) { + return story.currentChoices.map((choice, index) => ({ index, text: choice.text.trim(), targetPath: choice.targetPath })); +} + +function tagMap(tags) { + return Object.fromEntries(tags.map((tag) => tag.split(/:\s*/, 2)).filter((parts) => parts.length === 2)); +} + +function getSession(sessionId) { + const session = sessions.get(requiredString(sessionId, "sessionId")); + if (!session) throw new Error(`Unknown Sai session ${sessionId}.`); + return session; +} + +function requiredString(value, field) { + if (typeof value !== "string" || !value.trim()) throw new Error(`${field} is required.`); + return value; +} + +function parseJson(value) { return JSON.parse(value.replace(/^\uFEFF/, "")); } + +function respond(request, output, status = "accepted", message) { + process.stdout.write(`${JSON.stringify({ + schema: "gamecult.eve.plugin_abi.response.v1", + pluginId, + operation: request?.operation || "unknown", + requestId: request?.requestId || "unknown", + status, + output, + ...(message ? { diagnostics: [{ message }] } : {}), + })}\n`); +}