@mostra/runtime 0.7.0 → 0.9.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.
package/dist/bin.js CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env node
2
- import { a as uninstallService, i as serviceStatus, n as startInstance, o as checkPlatform, r as installService, s as resolveHomePath, t as runtimeVersion } from "./instance-CYPYPCIb.js";
2
+ import { a as uninstallService, i as serviceStatus, n as startInstance, o as checkPlatform, r as installService, s as resolveHomePath, t as runtimeVersion } from "./instance-EnaZPs14.js";
3
3
  import { s as realPath } from "./files-FygSJIzK.js";
4
4
  import { mkdirSync } from "node:fs";
5
5
  import { fileURLToPath } from "node:url";
package/dist/index.js CHANGED
@@ -1,3 +1,3 @@
1
- import { n as startInstance, t as runtimeVersion } from "./instance-CYPYPCIb.js";
1
+ import { n as startInstance, t as runtimeVersion } from "./instance-EnaZPs14.js";
2
2
  import { t as connectToSupervisor } from "./child-Chiavhdx.js";
3
3
  export { connectToSupervisor, runtimeVersion, startInstance };
@@ -5872,7 +5872,13 @@ function notFound$1(message) {
5872
5872
  */
5873
5873
  function submissionFor(db, artifactId, submissionId) {
5874
5874
  const row = submissionId === void 0 ? db.get("SELECT * FROM submission WHERE artifact_id = ? ORDER BY position DESC LIMIT 1", artifactId) : db.get("SELECT * FROM submission WHERE id = ? AND artifact_id = ?", submissionId, artifactId);
5875
- if (!row) throw notFound$1(submissionId === void 0 ? `The artifact ${artifactId} has no submission yet.` : `The artifact ${artifactId} has no submission ${submissionId}.`);
5875
+ if (!row) {
5876
+ if (submissionId === void 0) {
5877
+ const artifact = db.get("SELECT policy_json FROM artifact WHERE id = ?", artifactId);
5878
+ if (artifact && storedPolicy(artifact.policy_json).policy === "none") throw notFound$1(`The artifact ${artifactId} takes no submissions (its submission policy is none): what its viewers do is in its shared state and its signals, so read its state, or wait for one of its signals (signal:<name>), whose event carries the payload the page sent.`);
5879
+ }
5880
+ throw notFound$1(submissionId === void 0 ? `The artifact ${artifactId} has no submission yet.` : `The artifact ${artifactId} has no submission ${submissionId}.`);
5881
+ }
5876
5882
  return row;
5877
5883
  }
5878
5884
  /** The complete results of a submission (INTERFACES §8). */
@@ -11925,7 +11931,15 @@ function createLive(options) {
11925
11931
  try {
11926
11932
  const handle = await pool.acquire(version);
11927
11933
  described.set(versionId, handle.started.live);
11928
- if (!handle.started.live.some((source) => source.key === key)) return;
11934
+ const source = handle.started.live.find((info) => info.key === key);
11935
+ if (!source) return;
11936
+ if (source.load === false) bus.publishLive(artifactId, {
11937
+ kind: "live",
11938
+ live: {
11939
+ key,
11940
+ awaiting: false
11941
+ }
11942
+ });
11929
11943
  running.set(id, {
11930
11944
  artifactId,
11931
11945
  key,
@@ -12006,6 +12020,8 @@ function createLive(options) {
12006
12020
  const stateOf = (db, artifactId, key, window) => {
12007
12021
  const row = db.get("SELECT revision, value_json, at FROM live_value WHERE artifact_id = ? AND key = ?", artifactId, key);
12008
12022
  const failure = failures.get(keyOf(artifactId, key));
12023
+ const latest = options.latest(artifactId);
12024
+ const producing = latest?.sources.some((source) => source.key === key) === true && (described.get(latest.versionId)?.find((source) => source.key === key)?.load ?? true);
12009
12025
  const ring = window === void 0 ? void 0 : db.all("SELECT revision, value_json, at FROM live_ring WHERE artifact_id = ? AND key = ? ORDER BY revision DESC LIMIT ?", artifactId, key, window).reverse().map((entry) => ({
12010
12026
  revision: String(entry.revision),
12011
12027
  at: entry.at,
@@ -12019,7 +12035,8 @@ function createLive(options) {
12019
12035
  value: JSON.parse(row.value_json)
12020
12036
  } : {},
12021
12037
  ...ring ? { ring } : {},
12022
- ...failure ? { error: failure } : {}
12038
+ ...failure ? { error: failure } : {},
12039
+ ...!row && !failure && producing ? { awaiting: true } : {}
12023
12040
  };
12024
12041
  };
12025
12042
  return {
@@ -14395,6 +14412,74 @@ function extractManifest(file, text) {
14395
14412
  return file.endsWith(".mdx") ? extractFrontmatter(file, text) : extractScript(file, text);
14396
14413
  }
14397
14414
  //#endregion
14415
+ //#region src/publish/scaffold.ts
14416
+ /** The files a new artifact gets, in the order `artifact/create` reports them. */
14417
+ const SCAFFOLD_PATHS = [
14418
+ "index.tsx",
14419
+ "AGENTS.md",
14420
+ "CLAUDE.md"
14421
+ ];
14422
+ function entry({ artifactId, title }) {
14423
+ return [
14424
+ "import { defineArtifact } from \"@mostra/react\";",
14425
+ "",
14426
+ "export const artifact = defineArtifact({",
14427
+ ` id: ${JSON.stringify(artifactId)},`,
14428
+ ` title: ${JSON.stringify(title)},`,
14429
+ "});",
14430
+ "",
14431
+ "export default function Page() {",
14432
+ " return (",
14433
+ " <main className=\"mx-auto max-w-2xl p-8\">",
14434
+ " <h1 className=\"text-2xl font-semibold\">{artifact.title}</h1>",
14435
+ " </main>",
14436
+ " );",
14437
+ "}",
14438
+ ""
14439
+ ].join("\n");
14440
+ }
14441
+ /** A Markdown heading's text: one line, nothing that reads as Markdown syntax at its start. */
14442
+ function headingText(title) {
14443
+ const line = title.replace(/\s+/g, " ").trim();
14444
+ return line === "" ? "Mostra artifact" : line;
14445
+ }
14446
+ function agents({ title, version }) {
14447
+ return [
14448
+ `# ${headingText(title)}`,
14449
+ "",
14450
+ "This directory is a Mostra artifact: a page people open from a private link, read, answer and act on. Their answers come back to you as results.",
14451
+ "",
14452
+ "## Work on it",
14453
+ "",
14454
+ "1. Edit `index.tsx`: ordinary React 19 and TypeScript with Tailwind CSS 4, with no `package.json` or build configuration.",
14455
+ "2. Run `mostra verify .` until it reports `pass`, and look at both screenshots it lists (`--screenshots always` lists them every time).",
14456
+ "3. Run `mostra publish .`. It prints the private link: whoever holds it can use the page.",
14457
+ "4. Give the link to the person, saying who is asking and why. Then run `mostra wait . --until submitted` and `mostra results .`.",
14458
+ "",
14459
+ `Where \`mostra\` isn't installed, run \`npx -y @mostra/cli@${version}\` in its place.`,
14460
+ "",
14461
+ "## Keep in mind",
14462
+ "",
14463
+ "- Every answer is a field with an ID that stays the same on every render and in every version: `useField(\"budget\", 0, { label: \"Budget\" })`, or a template on your data's own ID for a list. Answers carry over to a new version by ID.",
14464
+ "- Keep the `id` in `defineArtifact`: it is this artifact's identity. Manifest values are literals.",
14465
+ "- Code that must run on this machine (send, save, call an API) goes in a module that starts with `\"use server\"`; live data in `live.ts`; static files in `public/`.",
14466
+ "- Imports stay inside this directory, or name an npm package.",
14467
+ "",
14468
+ "## More",
14469
+ "",
14470
+ "`mostra docs guide` is the full guide to writing artifacts, and `mostra docs <CODE>` explains any diagnostic. The same guide is the Mostra Agent Skill: `npx skills add mostra-dev/mostra`.",
14471
+ ""
14472
+ ].join("\n");
14473
+ }
14474
+ /** The files of a new artifact, by path. */
14475
+ function scaffoldFiles(input) {
14476
+ return /* @__PURE__ */ new Map([
14477
+ ["index.tsx", entry(input)],
14478
+ ["AGENTS.md", agents(input)],
14479
+ ["CLAUDE.md", "@AGENTS.md\n"]
14480
+ ]);
14481
+ }
14482
+ //#endregion
14398
14483
  //#region src/publish/source.ts
14399
14484
  /**
14400
14485
  * Artifact sources (INTERFACES §2.1, DATA_MODEL §2): finding an artifact's entry, listing its files
@@ -14766,46 +14851,40 @@ async function archiveSnapshot(source, files, target) {
14766
14851
  fsyncPath(dirname(target), true);
14767
14852
  return true;
14768
14853
  }
14769
- /** The entry `artifact/create` writes: a manifest with the generated `id` and a minimal page. */
14770
- function scaffold(artifactId, title) {
14771
- return [
14772
- "import { defineArtifact } from \"@mostra/react\";",
14773
- "",
14774
- "export const artifact = defineArtifact({",
14775
- ` id: ${JSON.stringify(artifactId)},`,
14776
- ` title: ${JSON.stringify(title)},`,
14777
- "});",
14778
- "",
14779
- "export default function Page() {",
14780
- " return (",
14781
- " <main className=\"mx-auto max-w-2xl p-8\">",
14782
- " <h1 className=\"text-2xl font-semibold\">{artifact.title}</h1>",
14783
- " </main>",
14784
- " );",
14785
- "}",
14786
- ""
14787
- ].join("\n");
14854
+ /** The text of each of `paths` (of {@link scaffoldFiles}) for `input`. */
14855
+ function scaffoldText(paths, input) {
14856
+ const all = scaffoldFiles(input);
14857
+ return Object.fromEntries(paths.flatMap((path) => {
14858
+ const text = all.get(path);
14859
+ return text === void 0 ? [] : [[path, text]];
14860
+ }));
14788
14861
  }
14789
14862
  /**
14790
- * Writes `artifact/create`'s entry into `dir`, completing an interrupted write: an existing file
14791
- * that is the expected text or a prefix of it (a write cut short) ends as the expected text, and
14792
- * anything else the author has written since is left alone.
14863
+ * Writes `artifact/create`'s files (`files`, path to text) into `dir`, completing an interrupted
14864
+ * write: an existing file whose bytes are the expected text's or a prefix of them (a write cut
14865
+ * short, even inside a character) ends as the expected text, and anything else the author has
14866
+ * written since is left alone.
14793
14867
  */
14794
- async function writeScaffold(dir, artifactId, title) {
14795
- const expected = scaffold(artifactId, title);
14796
- const file = join(dir, "index.tsx");
14868
+ async function writeScaffold(dir, files) {
14797
14869
  await mkdir(dir, { recursive: true });
14798
- let current;
14799
- try {
14800
- current = await readFile(file, "utf8");
14801
- } catch {
14802
- current = void 0;
14870
+ let wrote = false;
14871
+ for (const [path, text] of Object.entries(files)) {
14872
+ const expected = Buffer.from(text, "utf8");
14873
+ const file = join(dir, path);
14874
+ let current;
14875
+ try {
14876
+ current = await readFile(file);
14877
+ } catch {
14878
+ current = void 0;
14879
+ }
14880
+ if (current !== void 0) {
14881
+ if (!(current.length <= expected.length && expected.subarray(0, current.length).equals(current)) || current.length === expected.length) continue;
14882
+ }
14883
+ await writeFile(file, expected);
14884
+ fsyncPath(file);
14885
+ wrote = true;
14803
14886
  }
14804
- if (current !== void 0 && !expected.startsWith(current)) return;
14805
- if (current === expected) return;
14806
- await writeFile(file, expected);
14807
- fsyncPath(file);
14808
- fsyncPath(dir, true);
14887
+ if (wrote) fsyncPath(dir, true);
14809
14888
  }
14810
14889
  /**
14811
14890
  * What is wrong with a stored version directory, or `undefined` when it is intact: its
@@ -15216,10 +15295,18 @@ function createPublisher(options) {
15216
15295
  async function create(params) {
15217
15296
  const key = requestKey("artifact/create", params);
15218
15297
  if (key !== void 0) {
15219
- const result = ledger.read((db) => recordedResult(db, key));
15220
- if (result !== void 0) {
15221
- await writeScaffold(result.dir, result.artifactId, params.title ?? basename(result.dir));
15222
- return result;
15298
+ const recorded = ledger.read((db) => recordedResult(db, key));
15299
+ if (recorded !== void 0) {
15300
+ if ("result" in recorded) {
15301
+ await writeScaffold(recorded.result.dir, recorded.scaffold.files);
15302
+ return recorded.result;
15303
+ }
15304
+ await writeScaffold(recorded.dir, scaffoldText(recorded.files, {
15305
+ artifactId: recorded.artifactId,
15306
+ title: params.title ?? basename(recorded.dir),
15307
+ version: options.runtimeVersion
15308
+ }));
15309
+ return recorded;
15223
15310
  }
15224
15311
  }
15225
15312
  if (params.template !== void 0) throw new RpcError(createDiagnostic("MOSTRA_ARTIFACT_TEMPLATE_UNKNOWN", { message: `This Mostra release provides no template "${params.template}"; create the artifact without one.` }));
@@ -15231,11 +15318,19 @@ function createPublisher(options) {
15231
15318
  const result = {
15232
15319
  artifactId: ArtifactId.create(),
15233
15320
  dir,
15234
- files: ["index.tsx"]
15321
+ files: [...SCAFFOLD_PATHS]
15235
15322
  };
15236
- record(key, result);
15323
+ const files = scaffoldText(result.files, {
15324
+ artifactId: result.artifactId,
15325
+ title: params.title ?? basename(dir),
15326
+ version: options.runtimeVersion
15327
+ });
15328
+ record(key, {
15329
+ result,
15330
+ scaffold: { files }
15331
+ });
15237
15332
  faults.at("create:after-record");
15238
- await writeScaffold(dir, result.artifactId, params.title ?? basename(dir));
15333
+ await writeScaffold(dir, files);
15239
15334
  return result;
15240
15335
  }
15241
15336
  }
@@ -18844,4 +18939,4 @@ function sourceDirectory(path) {
18844
18939
  //#endregion
18845
18940
  export { uninstallService as a, serviceStatus as i, startInstance as n, checkPlatform as o, installService as r, resolveHomePath as s, runtimeVersion as t };
18846
18941
 
18847
- //# sourceMappingURL=instance-CYPYPCIb.js.map
18942
+ //# sourceMappingURL=instance-EnaZPs14.js.map