@kolisachint/hoocode-agent 0.5.25 → 0.5.27

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.
Files changed (88) hide show
  1. package/CHANGELOG.md +128 -0
  2. package/dist/core/canvas/lifecycle.d.ts +93 -0
  3. package/dist/core/canvas/lifecycle.d.ts.map +1 -0
  4. package/dist/core/canvas/lifecycle.js +165 -0
  5. package/dist/core/canvas/lifecycle.js.map +1 -0
  6. package/dist/core/canvas/registry.d.ts +89 -0
  7. package/dist/core/canvas/registry.d.ts.map +1 -1
  8. package/dist/core/canvas/registry.js +205 -10
  9. package/dist/core/canvas/registry.js.map +1 -1
  10. package/dist/core/canvas/scaffold.d.ts +123 -0
  11. package/dist/core/canvas/scaffold.d.ts.map +1 -0
  12. package/dist/core/canvas/scaffold.js +376 -0
  13. package/dist/core/canvas/scaffold.js.map +1 -0
  14. package/dist/core/canvas/session.d.ts +39 -1
  15. package/dist/core/canvas/session.d.ts.map +1 -1
  16. package/dist/core/canvas/session.js +83 -1
  17. package/dist/core/canvas/session.js.map +1 -1
  18. package/dist/core/capabilities/lexical.d.ts +4 -0
  19. package/dist/core/capabilities/lexical.d.ts.map +1 -1
  20. package/dist/core/capabilities/lexical.js +104 -4
  21. package/dist/core/capabilities/lexical.js.map +1 -1
  22. package/dist/core/capabilities/registry.d.ts +3 -1
  23. package/dist/core/capabilities/registry.d.ts.map +1 -1
  24. package/dist/core/capabilities/registry.js.map +1 -1
  25. package/dist/core/self-docs.d.ts +103 -0
  26. package/dist/core/self-docs.d.ts.map +1 -0
  27. package/dist/core/self-docs.js +351 -0
  28. package/dist/core/self-docs.js.map +1 -0
  29. package/dist/core/system-prompt.d.ts +12 -0
  30. package/dist/core/system-prompt.d.ts.map +1 -1
  31. package/dist/core/system-prompt.js +11 -1
  32. package/dist/core/system-prompt.js.map +1 -1
  33. package/dist/core/tools/canvas.d.ts +23 -3
  34. package/dist/core/tools/canvas.d.ts.map +1 -1
  35. package/dist/core/tools/canvas.js +99 -4
  36. package/dist/core/tools/canvas.js.map +1 -1
  37. package/dist/extensions/core/canvas.d.ts +20 -2
  38. package/dist/extensions/core/canvas.d.ts.map +1 -1
  39. package/dist/extensions/core/canvas.js +279 -36
  40. package/dist/extensions/core/canvas.js.map +1 -1
  41. package/dist/extensions/core/hoo-core.d.ts +1 -0
  42. package/dist/extensions/core/hoo-core.d.ts.map +1 -1
  43. package/dist/extensions/core/hoo-core.js +3 -0
  44. package/dist/extensions/core/hoo-core.js.map +1 -1
  45. package/dist/extensions/core/mcp-loader.d.ts.map +1 -1
  46. package/dist/extensions/core/mcp-loader.js +8 -2
  47. package/dist/extensions/core/mcp-loader.js.map +1 -1
  48. package/dist/extensions/core/scaffold.d.ts +7 -1
  49. package/dist/extensions/core/scaffold.d.ts.map +1 -1
  50. package/dist/extensions/core/scaffold.js +7 -185
  51. package/dist/extensions/core/scaffold.js.map +1 -1
  52. package/dist/extensions/core/self-knowledge.d.ts +28 -0
  53. package/dist/extensions/core/self-knowledge.d.ts.map +1 -0
  54. package/dist/extensions/core/self-knowledge.js +199 -0
  55. package/dist/extensions/core/self-knowledge.js.map +1 -0
  56. package/docs/canvas.md +117 -0
  57. package/docs/compaction.md +4 -4
  58. package/docs/custom-provider.md +1 -1
  59. package/docs/development.md +1 -1
  60. package/docs/docs.json +27 -2
  61. package/docs/extensions.md +12 -12
  62. package/docs/index.md +8 -0
  63. package/docs/keybindings.md +2 -2
  64. package/docs/mcp.md +97 -0
  65. package/docs/models.md +1 -1
  66. package/docs/modes.md +87 -0
  67. package/docs/packages.md +4 -4
  68. package/docs/plugins.md +124 -0
  69. package/docs/prompt-templates.md +1 -1
  70. package/docs/providers.md +2 -2
  71. package/docs/quickstart.md +2 -2
  72. package/docs/rpc.md +5 -5
  73. package/docs/sdk.md +5 -5
  74. package/docs/session-format.md +3 -3
  75. package/docs/sessions.md +1 -1
  76. package/docs/settings.md +3 -3
  77. package/docs/shell-aliases.md +1 -1
  78. package/docs/skills.md +2 -2
  79. package/docs/terminal-setup.md +1 -1
  80. package/docs/termux.md +2 -2
  81. package/docs/themes.md +3 -3
  82. package/docs/usage.md +93 -4
  83. package/docs/windows.md +1 -1
  84. package/examples/extensions/custom-provider-anthropic/package.json +1 -1
  85. package/examples/extensions/custom-provider-gitlab-duo/package.json +1 -1
  86. package/examples/extensions/sandbox/package.json +1 -1
  87. package/examples/extensions/with-deps/package.json +1 -1
  88. package/package.json +4 -4
@@ -0,0 +1,123 @@
1
+ /**
2
+ * Authoring a canvas: the request a person types, the files it writes, and the
3
+ * brief the model builds from.
4
+ *
5
+ * Design: `docs/canvas-extensions-design.md` §9 Phase 3, §13.
6
+ *
7
+ * This lives in `core/` rather than beside the other `/new-*` scaffolds because
8
+ * `/new-canvas` stopped being a file-writing command. Copilot's `/create-canvas`
9
+ * takes a sentence, has the agent write the extension, and opens it in a panel to
10
+ * iterate on; matching that means the command has to reach the canvas session to
11
+ * open, and the agent loop to build. Every decision that does not need either —
12
+ * what the name is, where the file goes, what goes in it, what the model is told —
13
+ * is here, testable without a terminal, a fork or a model.
14
+ */
15
+ import type { MarketplacePlatform } from "../extensions/plugins/formats/types.js";
16
+ /**
17
+ * Where a canvas extension lives, per platform.
18
+ *
19
+ * This does not go through `WorkspaceLayout` like the other scaffolds, and the
20
+ * reason is that a canvas has no Claude convention to emit into: the surface
21
+ * exists in Copilot and in hoocode's own `.agents/` tree and nowhere else. A
22
+ * layout method returning nothing for one adapter would be a worse lie than
23
+ * naming the two real homes here — these are exactly the roots
24
+ * `core/canvas/discovery.ts` searches, which is what makes a scaffold live on the
25
+ * next `/canvas`.
26
+ */
27
+ export declare const CANVAS_HOMES: Partial<Record<MarketplacePlatform, string[]>>;
28
+ /** Validates a canvas name: lowercase a-z, 0-9, hyphens, no leading/trailing/double hyphens. */
29
+ export declare function validateCanvasName(name: string): string | null;
30
+ /**
31
+ * Turn a sentence into a directory name.
32
+ *
33
+ * Returns undefined when nothing usable survives — an all-punctuation request, or
34
+ * a sentence of nothing but filler — because inventing `canvas-1` would hide from
35
+ * the person that we did not understand them.
36
+ */
37
+ export declare function canvasNameFromDescription(description: string): string | undefined;
38
+ /** What a person asked `/new-canvas` for. */
39
+ export interface CanvasRequest {
40
+ /** Directory and default canvas id. */
41
+ name: string;
42
+ /**
43
+ * What they want it to do, in their words, or undefined when they only named
44
+ * one. Present means the model is expected to build it (Copilot's
45
+ * `/create-canvas` shape); absent means they want the template to edit by hand.
46
+ */
47
+ description: string | undefined;
48
+ }
49
+ /**
50
+ * Parse `/new-canvas`'s argument.
51
+ *
52
+ * Three shapes, in the order they are tested:
53
+ *
54
+ * - `my-board` — a bare name. Unchanged from before descriptions existed, and
55
+ * tested first so it can never be re-read as a one-word description.
56
+ * - `my-board: a kanban board for the release checklist` — both, when someone
57
+ * cares what the directory is called. A colon rather than a flag because the
58
+ * rest of the line is prose and a flag parser would have to guess where it ends.
59
+ * - `a kanban board for the release checklist` — a description, the shape
60
+ * `/create-canvas` uses. The name is derived and reported.
61
+ *
62
+ * Returns a string when the input cannot become a canvas; the caller shows it.
63
+ */
64
+ export declare function parseCanvasRequest(input: string): CanvasRequest | string;
65
+ /**
66
+ * A working single-canvas extension.
67
+ *
68
+ * Deliberately complete rather than a stub: a canvas has no passive half — its
69
+ * name, its actions and its UI all come from running its code — so a scaffold
70
+ * that does not run teaches nothing and cannot be checked with `/canvas open`.
71
+ * This one opens, serves a page, answers an action, and closes cleanly, which is
72
+ * the whole contract; everything past that is the author's.
73
+ *
74
+ * Shaped like the catalog extensions hoocode already hosts: the only import is
75
+ * `@github/copilot-sdk/extension` (host-resolved — never installed, see
76
+ * `docs/canvas-extensions-design.md` §4.1) plus `node:` builtins, and the UI is
77
+ * served from an ephemeral loopback port behind a per-instance token so nothing
78
+ * else on the machine can read it.
79
+ */
80
+ export declare const CANVAS_ENTRY_TEMPLATE: (name: string) => string;
81
+ /** What {@link scaffoldCanvas} wrote. */
82
+ export interface CanvasScaffoldResult {
83
+ /** Workspace-relative entry files created, one per platform target. */
84
+ created: string[];
85
+ /** Workspace-relative entry files that already existed and were left alone. */
86
+ skipped: string[];
87
+ }
88
+ /**
89
+ * Write the template into every platform target that has a canvas home.
90
+ *
91
+ * Existing files are never clobbered — they are reported and skipped, so running
92
+ * `/new-canvas` twice on a canvas you have been editing cannot lose it.
93
+ */
94
+ export declare function scaffoldCanvas(cwd: string, name: string, platforms: MarketplacePlatform[]): CanvasScaffoldResult;
95
+ /** Where a build brief's canvas is, if opening it worked. */
96
+ export interface CanvasBriefTarget {
97
+ instanceId: string;
98
+ url: string | undefined;
99
+ }
100
+ /**
101
+ * The message the model builds from — hoocode's half of `/create-canvas`.
102
+ *
103
+ * A scaffold plus a sentence is not a canvas, and the gap between them is the
104
+ * agent's work. This is what turns "a kanban board for the release checklist"
105
+ * into a build task with the contract attached, so the model does not have to
106
+ * infer the rules of a surface it cannot see from a template it has not read.
107
+ *
108
+ * Four of those rules are stated because getting them wrong fails in ways whose
109
+ * symptom does not name the cause: an installed dependency (the resolver already
110
+ * provides the SDK, and a `node_modules` here is a §4.1 violation), a
111
+ * `console.log` (corrupts the JSON-RPC channel and surfaces as "non-protocol
112
+ * stdout"), a write without a reload (changes nothing at all, because the running
113
+ * child was forked from the old bytes), and a renamed canvas id.
114
+ *
115
+ * That last one is the newest and was found the hard way, by building a canvas
116
+ * with this command: the scaffold names the canvas after the directory, a model
117
+ * that thinks of a better name renames the `id`, and the next reload drops the
118
+ * instance the person is looking at — correctly, since the canvas it was opened
119
+ * against no longer exists. `displayName` is the half they actually see, so it
120
+ * is the half to change.
121
+ */
122
+ export declare function canvasBuildBrief(name: string, description: string, entryPath: string, target: CanvasBriefTarget | undefined): string;
123
+ //# sourceMappingURL=scaffold.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scaffold.d.ts","sourceRoot":"","sources":["../../../src/core/canvas/scaffold.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAIH,OAAO,KAAK,EAA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GAAG,SAAS,GACnC,MAAM,CAsCR","sourcesContent":["/**\n * Authoring a canvas: the request a person types, the files it writes, and the\n * brief the model builds from.\n *\n * Design: `docs/canvas-extensions-design.md` §9 Phase 3, §13.\n *\n * This lives in `core/` rather than beside the other `/new-*` scaffolds because\n * `/new-canvas` stopped being a file-writing command. Copilot's `/create-canvas`\n * takes a sentence, has the agent write the extension, and opens it in a panel to\n * iterate on; matching that means the command has to reach the canvas session to\n * open, and the agent loop to build. Every decision that does not need either —\n * what the name is, where the file goes, what goes in it, what the model is told —\n * is here, testable without a terminal, a fork or a model.\n */\n\nimport { existsSync, mkdirSync, writeFileSync } from \"node:fs\";\nimport { dirname, join } from \"node:path\";\nimport type { MarketplacePlatform } from \"../extensions/plugins/formats/types.js\";\nimport { CANVAS_ENTRY_FILE } from \"./discovery.js\";\n\n/**\n * Where a canvas extension lives, per platform.\n *\n * This does not go through `WorkspaceLayout` like the other scaffolds, and the\n * reason is that a canvas has no Claude convention to emit into: the surface\n * exists in Copilot and in hoocode's own `.agents/` tree and nowhere else. A\n * layout method returning nothing for one adapter would be a worse lie than\n * naming the two real homes here — these are exactly the roots\n * `core/canvas/discovery.ts` searches, which is what makes a scaffold live on the\n * next `/canvas`.\n */\nexport const CANVAS_HOMES: Partial<Record<MarketplacePlatform, string[]>> = {\n\tagents: [\".agents\", \"extensions\"],\n\tgithub: [\".github\", \"extensions\"],\n};\n\n/** Validates a canvas name: lowercase a-z, 0-9, hyphens, no leading/trailing/double hyphens. */\nexport function validateCanvasName(name: string): string | null {\n\tif (!name) return \"name is required\";\n\tif (!/^[a-z0-9-]+$/.test(name)) return \"name must be lowercase a-z, 0-9, and hyphens only\";\n\tif (name.startsWith(\"-\") || name.endsWith(\"-\")) return \"name must not start or end with a hyphen\";\n\tif (name.includes(\"--\")) return \"name must not contain consecutive hyphens\";\n\treturn null;\n}\n\n/**\n * Words dropped wherever they appear, because they carry no subject.\n *\n * Only grammar, never subject matter: a list that reached further would start\n * deciding which of someone's nouns mattered.\n */\nconst FILLER = new Set([\n\t\"a\",\n\t\"about\",\n\t\"across\",\n\t\"an\",\n\t\"and\",\n\t\"are\",\n\t\"as\",\n\t\"at\",\n\t\"by\",\n\t\"be\",\n\t\"can\",\n\t\"for\",\n\t\"from\",\n\t\"i\",\n\t\"in\",\n\t\"is\",\n\t\"into\",\n\t\"it\",\n\t\"its\",\n\t\"me\",\n\t\"my\",\n\t\"of\",\n\t\"on\",\n\t\"one\",\n\t\"or\",\n\t\"our\",\n\t\"over\",\n\t\"so\",\n\t\"that\",\n\t\"the\",\n\t\"their\",\n\t\"them\",\n\t\"then\",\n\t\"this\",\n\t\"to\",\n\t\"us\",\n\t\"we\",\n\t\"which\",\n\t\"via\",\n\t\"when\",\n\t\"where\",\n\t\"while\",\n\t\"with\",\n\t\"you\",\n\t\"your\",\n]);\n\n/**\n * Words that open a request rather than describe one.\n *\n * People type `/new-canvas` as an instruction — \"create a…\", \"build me a…\",\n * \"help me…\", \"I want to…\" — and the opening clause was landing in the directory\n * name: `create-lightweight-games`, `help-compare-two`, `want-review-pull`. On a\n * spread of twelve realistic descriptions, seven produced a name that named the\n * request instead of the thing.\n *\n * These are dropped anywhere, not only at the front, because \"add a canvas that\n * shows X\" buries one in the middle.\n */\nconst REQUEST_WORDS = new Set([\n\t\"add\",\n\t\"build\",\n\t\"could\",\n\t\"create\",\n\t\"design\",\n\t\"generate\",\n\t\"give\",\n\t\"help\",\n\t\"let\",\n\t\"make\",\n\t\"need\",\n\t\"new\",\n\t\"please\",\n\t\"produce\",\n\t\"set\",\n\t\"show\",\n\t\"something\",\n\t\"up\",\n\t\"want\",\n\t\"would\",\n]);\n\n/** Counting words: \"compare two benchmark runs\" is about the runs. */\nconst CARDINALS = new Set([\"two\", \"three\", \"four\", \"five\", \"six\", \"seven\", \"eight\", \"nine\", \"ten\"]);\n\n/** How many words a derived name keeps. Enough to be recognisable, short enough to type. */\nconst DERIVED_NAME_WORDS = 3;\n\n/**\n * Whether a word is probably a verb form rather than the thing being described.\n *\n * Crude on purpose — no part-of-speech tagger for a directory name. It exists\n * because a participle sits between the request and its subject and pushes the\n * subject out of a three-word name: \"a dashboard **showing** flaky tests\" became\n * `dashboard-showing-flaky`, and \"a spreadsheet for **tracking** API latency\"\n * became `spreadsheet-tracking-api`. The length floor spares short words where\n * the ending is a coincidence (`feed`, `used`, `ring`).\n */\nfunction looksLikeVerbForm(word: string): boolean {\n\treturn word.length > 4 && (word.endsWith(\"ing\") || word.endsWith(\"ed\"));\n}\n\n/**\n * Turn a sentence into a directory name.\n *\n * Returns undefined when nothing usable survives — an all-punctuation request, or\n * a sentence of nothing but filler — because inventing `canvas-1` would hide from\n * the person that we did not understand them.\n */\nexport function canvasNameFromDescription(description: string): string | undefined {\n\tconst words = description\n\t\t.toLowerCase()\n\t\t.replace(/[^a-z0-9]+/g, \" \")\n\t\t.trim()\n\t\t.split(\" \")\n\t\t.filter((word) => word.length > 0);\n\n\tconst content = words.filter((word) => !FILLER.has(word) && !REQUEST_WORDS.has(word) && !CARDINALS.has(word));\n\t// \"canvas\" is dropped only when something else remains: \"/new-canvas a canvas\"\n\t// should still produce `canvas` rather than nothing.\n\tconst named = content.filter((word) => word !== \"canvas\");\n\tlet kept = named.length > 0 ? named : content;\n\n\t// Prefer nouns — but only while enough of them remain. Where they do not, the\n\t// participle *is* the subject (\"a canvas for onboarding\") and dropping it would\n\t// leave nothing worth naming.\n\tconst nouns = kept.filter((word) => !looksLikeVerbForm(word));\n\tif (nouns.length >= 2) kept = nouns;\n\n\tconst name = kept.slice(0, DERIVED_NAME_WORDS).join(\"-\");\n\treturn validateCanvasName(name) === null ? name : undefined;\n}\n\n/** What a person asked `/new-canvas` for. */\nexport interface CanvasRequest {\n\t/** Directory and default canvas id. */\n\tname: string;\n\t/**\n\t * What they want it to do, in their words, or undefined when they only named\n\t * one. Present means the model is expected to build it (Copilot's\n\t * `/create-canvas` shape); absent means they want the template to edit by hand.\n\t */\n\tdescription: string | undefined;\n}\n\n/**\n * Parse `/new-canvas`'s argument.\n *\n * Three shapes, in the order they are tested:\n *\n * - `my-board` — a bare name. Unchanged from before descriptions existed, and\n * tested first so it can never be re-read as a one-word description.\n * - `my-board: a kanban board for the release checklist` — both, when someone\n * cares what the directory is called. A colon rather than a flag because the\n * rest of the line is prose and a flag parser would have to guess where it ends.\n * - `a kanban board for the release checklist` — a description, the shape\n * `/create-canvas` uses. The name is derived and reported.\n *\n * Returns a string when the input cannot become a canvas; the caller shows it.\n */\nexport function parseCanvasRequest(input: string): CanvasRequest | string {\n\tconst trimmed = input.trim();\n\tif (trimmed.length === 0) return \"name or description is required\";\n\n\tif (validateCanvasName(trimmed) === null) return { name: trimmed, description: undefined };\n\n\tconst colon = trimmed.indexOf(\":\");\n\tif (colon > 0) {\n\t\tconst name = trimmed.slice(0, colon).trim();\n\t\tconst description = trimmed.slice(colon + 1).trim();\n\t\tif (validateCanvasName(name) === null && description.length > 0) return { name, description };\n\t}\n\n\tconst derived = canvasNameFromDescription(trimmed);\n\tif (!derived) {\n\t\treturn `could not derive a directory name from that. Give one explicitly: /new-canvas <name>: ${trimmed}`;\n\t}\n\treturn { name: derived, description: trimmed };\n}\n\n/**\n * A working single-canvas extension.\n *\n * Deliberately complete rather than a stub: a canvas has no passive half — its\n * name, its actions and its UI all come from running its code — so a scaffold\n * that does not run teaches nothing and cannot be checked with `/canvas open`.\n * This one opens, serves a page, answers an action, and closes cleanly, which is\n * the whole contract; everything past that is the author's.\n *\n * Shaped like the catalog extensions hoocode already hosts: the only import is\n * `@github/copilot-sdk/extension` (host-resolved — never installed, see\n * `docs/canvas-extensions-design.md` §4.1) plus `node:` builtins, and the UI is\n * served from an ephemeral loopback port behind a per-instance token so nothing\n * else on the machine can read it.\n */\nexport const CANVAS_ENTRY_TEMPLATE = (name: string): string => `\\\n// A canvas extension. Run it with: /canvas open ${name}\n//\n// The \"@github/copilot-sdk/extension\" import is resolved by the host at fork\n// time. Do not install it, and do not add a node_modules here.\nimport { createCanvas, CanvasError, joinSession } from \"@github/copilot-sdk/extension\";\nimport { randomBytes } from \"node:crypto\";\nimport { createServer } from \"node:http\";\n\n// The canvas's identity, in one place. An open instance is bound to this id, so\n// changing it drops the canvas anyone is currently looking at — rename the\n// canvas with \\`/canvas rename\\`, or change NAME alone if you only want a\n// different label on the page.\nconst ID = \"${name}\";\nconst NAME = \"${name}\";\n\n/** Per-instance state. A canvas can be opened more than once at a time. */\nconst instances = new Map();\n\nconst session = await joinSession({\n\tcanvases: [\n\t\tcreateCanvas({\n\t\t\tid: ID,\n\t\t\tdisplayName: NAME,\n\t\t\tdescription: \"TODO: one sentence — the agent reads this to decide whether to open it.\",\n\t\t\tactions: [\n\t\t\t\t{\n\t\t\t\t\tname: \"add_note\",\n\t\t\t\t\tdescription: \"TODO: describe what the agent can do to this canvas.\",\n\t\t\t\t\tinputSchema: {\n\t\t\t\t\t\ttype: \"object\",\n\t\t\t\t\t\tproperties: { text: { type: \"string\" } },\n\t\t\t\t\t\trequired: [\"text\"],\n\t\t\t\t\t},\n\t\t\t\t\thandler: (ctx) => {\n\t\t\t\t\t\tconst entry = instances.get(ctx.instanceId);\n\t\t\t\t\t\t// CanvasError carries a code the host shows verbatim; a bare\n\t\t\t\t\t\t// throw arrives as an opaque internal error instead.\n\t\t\t\t\t\tif (!entry) throw new CanvasError(\"no_instance\", \\`Instance \"\\${ctx.instanceId}\" is not open.\\`);\n\t\t\t\t\t\tentry.notes.push(ctx.input.text);\n\t\t\t\t\t\treturn { notes: entry.notes.length };\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t],\n\t\t\topen: async (ctx) => {\n\t\t\t\tconst token = randomBytes(32).toString(\"base64url\");\n\t\t\t\tconst notes = [];\n\t\t\t\tconst server = createServer((req, res) => {\n\t\t\t\t\tconst url = new URL(req.url ?? \"/\", \"http://127.0.0.1\");\n\t\t\t\t\tif (url.searchParams.get(\"token\") !== token) {\n\t\t\t\t\t\tres.writeHead(403, { \"Content-Type\": \"text/plain\" });\n\t\t\t\t\t\tres.end(\"forbidden\");\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\t\t\t\t\tres.writeHead(200, { \"Content-Type\": \"text/html; charset=utf-8\" });\n\t\t\t\t\tres.end(\n\t\t\t\t\t\t\\`<!doctype html><meta charset=\"utf-8\"><title>\\${NAME}</title>\\` +\n\t\t\t\t\t\t\t\\`<p>\\${notes.length} note(s). TODO: build the UI.\\`,\n\t\t\t\t\t);\n\t\t\t\t});\n\t\t\t\t// Port 0 on 127.0.0.1: an ephemeral port, reachable only from this machine.\n\t\t\t\tawait new Promise((resolve) => server.listen(0, \"127.0.0.1\", resolve));\n\t\t\t\tconst { port } = server.address();\n\t\t\t\tinstances.set(ctx.instanceId, { server, notes });\n\t\t\t\treturn {\n\t\t\t\t\turl: \\`http://127.0.0.1:\\${port}/?token=\\${token}\\`,\n\t\t\t\t\ttitle: NAME,\n\t\t\t\t};\n\t\t\t},\n\t\t\tonClose: async (ctx) => {\n\t\t\t\tconst entry = instances.get(ctx.instanceId);\n\t\t\t\t// Called for an abandoned open too, so the instance may be unknown.\n\t\t\t\tif (!entry) return;\n\t\t\t\tinstances.delete(ctx.instanceId);\n\t\t\t\tawait new Promise((resolve) => entry.server.close(resolve));\n\t\t\t},\n\t\t}),\n\t],\n});\n\n// stdout is the protocol channel. Use session.log, never console.log.\nawait session.log(\\`\\${ID} ready\\`);\n`;\n\n/** What {@link scaffoldCanvas} wrote. */\nexport interface CanvasScaffoldResult {\n\t/** Workspace-relative entry files created, one per platform target. */\n\tcreated: string[];\n\t/** Workspace-relative entry files that already existed and were left alone. */\n\tskipped: string[];\n}\n\n/**\n * Write the template into every platform target that has a canvas home.\n *\n * Existing files are never clobbered — they are reported and skipped, so running\n * `/new-canvas` twice on a canvas you have been editing cannot lose it.\n */\nexport function scaffoldCanvas(cwd: string, name: string, platforms: MarketplacePlatform[]): CanvasScaffoldResult {\n\tconst created: string[] = [];\n\tconst skipped: string[] = [];\n\tfor (const platform of platforms) {\n\t\tconst home = CANVAS_HOMES[platform];\n\t\tif (!home) continue;\n\t\tconst relative = join(...home, name, CANVAS_ENTRY_FILE);\n\t\tconst absolute = join(cwd, relative);\n\t\tif (existsSync(absolute)) {\n\t\t\tskipped.push(relative);\n\t\t\tcontinue;\n\t\t}\n\t\tmkdirSync(dirname(absolute), { recursive: true });\n\t\twriteFileSync(absolute, CANVAS_ENTRY_TEMPLATE(name), \"utf8\");\n\t\tcreated.push(relative);\n\t}\n\treturn { created, skipped };\n}\n\n/** Where a build brief's canvas is, if opening it worked. */\nexport interface CanvasBriefTarget {\n\tinstanceId: string;\n\turl: string | undefined;\n}\n\n/**\n * The message the model builds from — hoocode's half of `/create-canvas`.\n *\n * A scaffold plus a sentence is not a canvas, and the gap between them is the\n * agent's work. This is what turns \"a kanban board for the release checklist\"\n * into a build task with the contract attached, so the model does not have to\n * infer the rules of a surface it cannot see from a template it has not read.\n *\n * Four of those rules are stated because getting them wrong fails in ways whose\n * symptom does not name the cause: an installed dependency (the resolver already\n * provides the SDK, and a `node_modules` here is a §4.1 violation), a\n * `console.log` (corrupts the JSON-RPC channel and surfaces as \"non-protocol\n * stdout\"), a write without a reload (changes nothing at all, because the running\n * child was forked from the old bytes), and a renamed canvas id.\n *\n * That last one is the newest and was found the hard way, by building a canvas\n * with this command: the scaffold names the canvas after the directory, a model\n * that thinks of a better name renames the `id`, and the next reload drops the\n * instance the person is looking at — correctly, since the canvas it was opened\n * against no longer exists. `displayName` is the half they actually see, so it\n * is the half to change.\n */\nexport function canvasBuildBrief(\n\tname: string,\n\tdescription: string,\n\tentryPath: string,\n\ttarget: CanvasBriefTarget | undefined,\n): string {\n\tconst lines = [\n\t\t`/new-canvas: build a canvas extension named \"${name}\".`,\n\t\t\"\",\n\t\t\"What the person asked for, in their words:\",\n\t\t` ${description}`,\n\t\t\"\",\n\t\t`A working template is already at ${entryPath}. Edit it until it does what was asked.`,\n\t];\n\n\tif (target) {\n\t\tlines.push(\n\t\t\t\"\",\n\t\t\ttarget.url\n\t\t\t\t? `It is already open at ${target.url} (instance ${target.instanceId}), so the person is watching it as you work.`\n\t\t\t\t: `It is already open as instance ${target.instanceId}.`,\n\t\t\t`After every edit, call reload_canvas with extensionId \"${name}\". A write to the file changes nothing on its own — the running process was forked from the old code. Reloading hands back a NEW url; give it to the person, because the tab they have open dies with the old process.`,\n\t\t);\n\t} else {\n\t\tlines.push(\n\t\t\t\"\",\n\t\t\t`It is not open — the person will run /canvas open ${name} when they want to look. Build it anyway; you can check it runs by reading it carefully rather than by opening it yourself.`,\n\t\t);\n\t}\n\n\tlines.push(\n\t\t\"\",\n\t\t\"The contract this surface runs under, which is not yours to change:\",\n\t\t'- The only non-`node:` import allowed is \"@github/copilot-sdk/extension\". The host resolves it when it forks the extension. Do not install anything, and do not add a package.json or node_modules in the extension directory.',\n\t\t\"- stdout is the JSON-RPC channel. Use `session.log(...)`; a `console.log` corrupts the protocol.\",\n\t\t\"- Do not change the canvas's `id` (the template holds it in `ID`). An open instance is bound to it, so renaming it drops the canvas the person is watching on your next reload. Change `NAME` for a nicer label — that is the one they see — and if the directory name itself is wrong, say so and let them run `/canvas rename`, which moves everything at once.\",\n\t\t\"- After a reload, read the action list it reports back. That is the host telling you which of your actions it can actually see, and it is the only confirmation an action you just wrote is really callable.\",\n\t\t\"- Serve the UI from the loopback server the template already starts. Keep the per-instance token check, and keep `onClose` shutting the server down — that is what stops a port outliving the session.\",\n\t\t\"- Everything in `actions: [...]` becomes callable by you through invoke_canvas_action once it is open, so give each action a real description and inputSchema. That list is how you drive the canvas; the person drives the same state through the page.\",\n\t\t\"\",\n\t\t\"Build the thing that was asked for, not a stub: leave no TODO behind, and make the page render real state rather than a placeholder.\",\n\t);\n\treturn lines.join(\"\\n\");\n}\n"]}
@@ -0,0 +1,376 @@
1
+ /**
2
+ * Authoring a canvas: the request a person types, the files it writes, and the
3
+ * brief the model builds from.
4
+ *
5
+ * Design: `docs/canvas-extensions-design.md` §9 Phase 3, §13.
6
+ *
7
+ * This lives in `core/` rather than beside the other `/new-*` scaffolds because
8
+ * `/new-canvas` stopped being a file-writing command. Copilot's `/create-canvas`
9
+ * takes a sentence, has the agent write the extension, and opens it in a panel to
10
+ * iterate on; matching that means the command has to reach the canvas session to
11
+ * open, and the agent loop to build. Every decision that does not need either —
12
+ * what the name is, where the file goes, what goes in it, what the model is told —
13
+ * is here, testable without a terminal, a fork or a model.
14
+ */
15
+ import { existsSync, mkdirSync, writeFileSync } from "node:fs";
16
+ import { dirname, join } from "node:path";
17
+ import { CANVAS_ENTRY_FILE } from "./discovery.js";
18
+ /**
19
+ * Where a canvas extension lives, per platform.
20
+ *
21
+ * This does not go through `WorkspaceLayout` like the other scaffolds, and the
22
+ * reason is that a canvas has no Claude convention to emit into: the surface
23
+ * exists in Copilot and in hoocode's own `.agents/` tree and nowhere else. A
24
+ * layout method returning nothing for one adapter would be a worse lie than
25
+ * naming the two real homes here — these are exactly the roots
26
+ * `core/canvas/discovery.ts` searches, which is what makes a scaffold live on the
27
+ * next `/canvas`.
28
+ */
29
+ export const CANVAS_HOMES = {
30
+ agents: [".agents", "extensions"],
31
+ github: [".github", "extensions"],
32
+ };
33
+ /** Validates a canvas name: lowercase a-z, 0-9, hyphens, no leading/trailing/double hyphens. */
34
+ export function validateCanvasName(name) {
35
+ if (!name)
36
+ return "name is required";
37
+ if (!/^[a-z0-9-]+$/.test(name))
38
+ return "name must be lowercase a-z, 0-9, and hyphens only";
39
+ if (name.startsWith("-") || name.endsWith("-"))
40
+ return "name must not start or end with a hyphen";
41
+ if (name.includes("--"))
42
+ return "name must not contain consecutive hyphens";
43
+ return null;
44
+ }
45
+ /**
46
+ * Words dropped wherever they appear, because they carry no subject.
47
+ *
48
+ * Only grammar, never subject matter: a list that reached further would start
49
+ * deciding which of someone's nouns mattered.
50
+ */
51
+ const FILLER = new Set([
52
+ "a",
53
+ "about",
54
+ "across",
55
+ "an",
56
+ "and",
57
+ "are",
58
+ "as",
59
+ "at",
60
+ "by",
61
+ "be",
62
+ "can",
63
+ "for",
64
+ "from",
65
+ "i",
66
+ "in",
67
+ "is",
68
+ "into",
69
+ "it",
70
+ "its",
71
+ "me",
72
+ "my",
73
+ "of",
74
+ "on",
75
+ "one",
76
+ "or",
77
+ "our",
78
+ "over",
79
+ "so",
80
+ "that",
81
+ "the",
82
+ "their",
83
+ "them",
84
+ "then",
85
+ "this",
86
+ "to",
87
+ "us",
88
+ "we",
89
+ "which",
90
+ "via",
91
+ "when",
92
+ "where",
93
+ "while",
94
+ "with",
95
+ "you",
96
+ "your",
97
+ ]);
98
+ /**
99
+ * Words that open a request rather than describe one.
100
+ *
101
+ * People type `/new-canvas` as an instruction — "create a…", "build me a…",
102
+ * "help me…", "I want to…" — and the opening clause was landing in the directory
103
+ * name: `create-lightweight-games`, `help-compare-two`, `want-review-pull`. On a
104
+ * spread of twelve realistic descriptions, seven produced a name that named the
105
+ * request instead of the thing.
106
+ *
107
+ * These are dropped anywhere, not only at the front, because "add a canvas that
108
+ * shows X" buries one in the middle.
109
+ */
110
+ const REQUEST_WORDS = new Set([
111
+ "add",
112
+ "build",
113
+ "could",
114
+ "create",
115
+ "design",
116
+ "generate",
117
+ "give",
118
+ "help",
119
+ "let",
120
+ "make",
121
+ "need",
122
+ "new",
123
+ "please",
124
+ "produce",
125
+ "set",
126
+ "show",
127
+ "something",
128
+ "up",
129
+ "want",
130
+ "would",
131
+ ]);
132
+ /** Counting words: "compare two benchmark runs" is about the runs. */
133
+ const CARDINALS = new Set(["two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"]);
134
+ /** How many words a derived name keeps. Enough to be recognisable, short enough to type. */
135
+ const DERIVED_NAME_WORDS = 3;
136
+ /**
137
+ * Whether a word is probably a verb form rather than the thing being described.
138
+ *
139
+ * Crude on purpose — no part-of-speech tagger for a directory name. It exists
140
+ * because a participle sits between the request and its subject and pushes the
141
+ * subject out of a three-word name: "a dashboard **showing** flaky tests" became
142
+ * `dashboard-showing-flaky`, and "a spreadsheet for **tracking** API latency"
143
+ * became `spreadsheet-tracking-api`. The length floor spares short words where
144
+ * the ending is a coincidence (`feed`, `used`, `ring`).
145
+ */
146
+ function looksLikeVerbForm(word) {
147
+ return word.length > 4 && (word.endsWith("ing") || word.endsWith("ed"));
148
+ }
149
+ /**
150
+ * Turn a sentence into a directory name.
151
+ *
152
+ * Returns undefined when nothing usable survives — an all-punctuation request, or
153
+ * a sentence of nothing but filler — because inventing `canvas-1` would hide from
154
+ * the person that we did not understand them.
155
+ */
156
+ export function canvasNameFromDescription(description) {
157
+ const words = description
158
+ .toLowerCase()
159
+ .replace(/[^a-z0-9]+/g, " ")
160
+ .trim()
161
+ .split(" ")
162
+ .filter((word) => word.length > 0);
163
+ const content = words.filter((word) => !FILLER.has(word) && !REQUEST_WORDS.has(word) && !CARDINALS.has(word));
164
+ // "canvas" is dropped only when something else remains: "/new-canvas a canvas"
165
+ // should still produce `canvas` rather than nothing.
166
+ const named = content.filter((word) => word !== "canvas");
167
+ let kept = named.length > 0 ? named : content;
168
+ // Prefer nouns — but only while enough of them remain. Where they do not, the
169
+ // participle *is* the subject ("a canvas for onboarding") and dropping it would
170
+ // leave nothing worth naming.
171
+ const nouns = kept.filter((word) => !looksLikeVerbForm(word));
172
+ if (nouns.length >= 2)
173
+ kept = nouns;
174
+ const name = kept.slice(0, DERIVED_NAME_WORDS).join("-");
175
+ return validateCanvasName(name) === null ? name : undefined;
176
+ }
177
+ /**
178
+ * Parse `/new-canvas`'s argument.
179
+ *
180
+ * Three shapes, in the order they are tested:
181
+ *
182
+ * - `my-board` — a bare name. Unchanged from before descriptions existed, and
183
+ * tested first so it can never be re-read as a one-word description.
184
+ * - `my-board: a kanban board for the release checklist` — both, when someone
185
+ * cares what the directory is called. A colon rather than a flag because the
186
+ * rest of the line is prose and a flag parser would have to guess where it ends.
187
+ * - `a kanban board for the release checklist` — a description, the shape
188
+ * `/create-canvas` uses. The name is derived and reported.
189
+ *
190
+ * Returns a string when the input cannot become a canvas; the caller shows it.
191
+ */
192
+ export function parseCanvasRequest(input) {
193
+ const trimmed = input.trim();
194
+ if (trimmed.length === 0)
195
+ return "name or description is required";
196
+ if (validateCanvasName(trimmed) === null)
197
+ return { name: trimmed, description: undefined };
198
+ const colon = trimmed.indexOf(":");
199
+ if (colon > 0) {
200
+ const name = trimmed.slice(0, colon).trim();
201
+ const description = trimmed.slice(colon + 1).trim();
202
+ if (validateCanvasName(name) === null && description.length > 0)
203
+ return { name, description };
204
+ }
205
+ const derived = canvasNameFromDescription(trimmed);
206
+ if (!derived) {
207
+ return `could not derive a directory name from that. Give one explicitly: /new-canvas <name>: ${trimmed}`;
208
+ }
209
+ return { name: derived, description: trimmed };
210
+ }
211
+ /**
212
+ * A working single-canvas extension.
213
+ *
214
+ * Deliberately complete rather than a stub: a canvas has no passive half — its
215
+ * name, its actions and its UI all come from running its code — so a scaffold
216
+ * that does not run teaches nothing and cannot be checked with `/canvas open`.
217
+ * This one opens, serves a page, answers an action, and closes cleanly, which is
218
+ * the whole contract; everything past that is the author's.
219
+ *
220
+ * Shaped like the catalog extensions hoocode already hosts: the only import is
221
+ * `@github/copilot-sdk/extension` (host-resolved — never installed, see
222
+ * `docs/canvas-extensions-design.md` §4.1) plus `node:` builtins, and the UI is
223
+ * served from an ephemeral loopback port behind a per-instance token so nothing
224
+ * else on the machine can read it.
225
+ */
226
+ export const CANVAS_ENTRY_TEMPLATE = (name) => `\
227
+ // A canvas extension. Run it with: /canvas open ${name}
228
+ //
229
+ // The "@github/copilot-sdk/extension" import is resolved by the host at fork
230
+ // time. Do not install it, and do not add a node_modules here.
231
+ import { createCanvas, CanvasError, joinSession } from "@github/copilot-sdk/extension";
232
+ import { randomBytes } from "node:crypto";
233
+ import { createServer } from "node:http";
234
+
235
+ // The canvas's identity, in one place. An open instance is bound to this id, so
236
+ // changing it drops the canvas anyone is currently looking at — rename the
237
+ // canvas with \`/canvas rename\`, or change NAME alone if you only want a
238
+ // different label on the page.
239
+ const ID = "${name}";
240
+ const NAME = "${name}";
241
+
242
+ /** Per-instance state. A canvas can be opened more than once at a time. */
243
+ const instances = new Map();
244
+
245
+ const session = await joinSession({
246
+ canvases: [
247
+ createCanvas({
248
+ id: ID,
249
+ displayName: NAME,
250
+ description: "TODO: one sentence — the agent reads this to decide whether to open it.",
251
+ actions: [
252
+ {
253
+ name: "add_note",
254
+ description: "TODO: describe what the agent can do to this canvas.",
255
+ inputSchema: {
256
+ type: "object",
257
+ properties: { text: { type: "string" } },
258
+ required: ["text"],
259
+ },
260
+ handler: (ctx) => {
261
+ const entry = instances.get(ctx.instanceId);
262
+ // CanvasError carries a code the host shows verbatim; a bare
263
+ // throw arrives as an opaque internal error instead.
264
+ if (!entry) throw new CanvasError("no_instance", \`Instance "\${ctx.instanceId}" is not open.\`);
265
+ entry.notes.push(ctx.input.text);
266
+ return { notes: entry.notes.length };
267
+ },
268
+ },
269
+ ],
270
+ open: async (ctx) => {
271
+ const token = randomBytes(32).toString("base64url");
272
+ const notes = [];
273
+ const server = createServer((req, res) => {
274
+ const url = new URL(req.url ?? "/", "http://127.0.0.1");
275
+ if (url.searchParams.get("token") !== token) {
276
+ res.writeHead(403, { "Content-Type": "text/plain" });
277
+ res.end("forbidden");
278
+ return;
279
+ }
280
+ res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" });
281
+ res.end(
282
+ \`<!doctype html><meta charset="utf-8"><title>\${NAME}</title>\` +
283
+ \`<p>\${notes.length} note(s). TODO: build the UI.\`,
284
+ );
285
+ });
286
+ // Port 0 on 127.0.0.1: an ephemeral port, reachable only from this machine.
287
+ await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve));
288
+ const { port } = server.address();
289
+ instances.set(ctx.instanceId, { server, notes });
290
+ return {
291
+ url: \`http://127.0.0.1:\${port}/?token=\${token}\`,
292
+ title: NAME,
293
+ };
294
+ },
295
+ onClose: async (ctx) => {
296
+ const entry = instances.get(ctx.instanceId);
297
+ // Called for an abandoned open too, so the instance may be unknown.
298
+ if (!entry) return;
299
+ instances.delete(ctx.instanceId);
300
+ await new Promise((resolve) => entry.server.close(resolve));
301
+ },
302
+ }),
303
+ ],
304
+ });
305
+
306
+ // stdout is the protocol channel. Use session.log, never console.log.
307
+ await session.log(\`\${ID} ready\`);
308
+ `;
309
+ /**
310
+ * Write the template into every platform target that has a canvas home.
311
+ *
312
+ * Existing files are never clobbered — they are reported and skipped, so running
313
+ * `/new-canvas` twice on a canvas you have been editing cannot lose it.
314
+ */
315
+ export function scaffoldCanvas(cwd, name, platforms) {
316
+ const created = [];
317
+ const skipped = [];
318
+ for (const platform of platforms) {
319
+ const home = CANVAS_HOMES[platform];
320
+ if (!home)
321
+ continue;
322
+ const relative = join(...home, name, CANVAS_ENTRY_FILE);
323
+ const absolute = join(cwd, relative);
324
+ if (existsSync(absolute)) {
325
+ skipped.push(relative);
326
+ continue;
327
+ }
328
+ mkdirSync(dirname(absolute), { recursive: true });
329
+ writeFileSync(absolute, CANVAS_ENTRY_TEMPLATE(name), "utf8");
330
+ created.push(relative);
331
+ }
332
+ return { created, skipped };
333
+ }
334
+ /**
335
+ * The message the model builds from — hoocode's half of `/create-canvas`.
336
+ *
337
+ * A scaffold plus a sentence is not a canvas, and the gap between them is the
338
+ * agent's work. This is what turns "a kanban board for the release checklist"
339
+ * into a build task with the contract attached, so the model does not have to
340
+ * infer the rules of a surface it cannot see from a template it has not read.
341
+ *
342
+ * Four of those rules are stated because getting them wrong fails in ways whose
343
+ * symptom does not name the cause: an installed dependency (the resolver already
344
+ * provides the SDK, and a `node_modules` here is a §4.1 violation), a
345
+ * `console.log` (corrupts the JSON-RPC channel and surfaces as "non-protocol
346
+ * stdout"), a write without a reload (changes nothing at all, because the running
347
+ * child was forked from the old bytes), and a renamed canvas id.
348
+ *
349
+ * That last one is the newest and was found the hard way, by building a canvas
350
+ * with this command: the scaffold names the canvas after the directory, a model
351
+ * that thinks of a better name renames the `id`, and the next reload drops the
352
+ * instance the person is looking at — correctly, since the canvas it was opened
353
+ * against no longer exists. `displayName` is the half they actually see, so it
354
+ * is the half to change.
355
+ */
356
+ export function canvasBuildBrief(name, description, entryPath, target) {
357
+ const lines = [
358
+ `/new-canvas: build a canvas extension named "${name}".`,
359
+ "",
360
+ "What the person asked for, in their words:",
361
+ ` ${description}`,
362
+ "",
363
+ `A working template is already at ${entryPath}. Edit it until it does what was asked.`,
364
+ ];
365
+ if (target) {
366
+ lines.push("", target.url
367
+ ? `It is already open at ${target.url} (instance ${target.instanceId}), so the person is watching it as you work.`
368
+ : `It is already open as instance ${target.instanceId}.`, `After every edit, call reload_canvas with extensionId "${name}". A write to the file changes nothing on its own — the running process was forked from the old code. Reloading hands back a NEW url; give it to the person, because the tab they have open dies with the old process.`);
369
+ }
370
+ else {
371
+ lines.push("", `It is not open — the person will run /canvas open ${name} when they want to look. Build it anyway; you can check it runs by reading it carefully rather than by opening it yourself.`);
372
+ }
373
+ lines.push("", "The contract this surface runs under, which is not yours to change:", '- The only non-`node:` import allowed is "@github/copilot-sdk/extension". The host resolves it when it forks the extension. Do not install anything, and do not add a package.json or node_modules in the extension directory.', "- stdout is the JSON-RPC channel. Use `session.log(...)`; a `console.log` corrupts the protocol.", "- Do not change the canvas's `id` (the template holds it in `ID`). An open instance is bound to it, so renaming it drops the canvas the person is watching on your next reload. Change `NAME` for a nicer label — that is the one they see — and if the directory name itself is wrong, say so and let them run `/canvas rename`, which moves everything at once.", "- After a reload, read the action list it reports back. That is the host telling you which of your actions it can actually see, and it is the only confirmation an action you just wrote is really callable.", "- Serve the UI from the loopback server the template already starts. Keep the per-instance token check, and keep `onClose` shutting the server down — that is what stops a port outliving the session.", "- Everything in `actions: [...]` becomes callable by you through invoke_canvas_action once it is open, so give each action a real description and inputSchema. That list is how you drive the canvas; the person drives the same state through the page.", "", "Build the thing that was asked for, not a stub: leave no TODO behind, and make the page render real state rather than a placeholder.");
374
+ return lines.join("\n");
375
+ }
376
+ //# sourceMappingURL=scaffold.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scaffold.js","sourceRoot":"","sources":["../../../src/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;IACH,CAAC;IAED,KAAK,CAAC,IAAI,CACT,EAAE,EACF,qEAAqE,EACrE,gOAAgO,EAChO,kGAAkG,EAClG,uWAAmW,EACnW,8MAA8M,EAC9M,0MAAwM,EACxM,0PAA0P,EAC1P,EAAE,EACF,sIAAsI,CACtI,CAAC;IACF,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AAAA,CACxB","sourcesContent":["/**\n * Authoring a canvas: the request a person types, the files it writes, and the\n * brief the model builds from.\n *\n * Design: `docs/canvas-extensions-design.md` §9 Phase 3, §13.\n *\n * This lives in `core/` rather than beside the other `/new-*` scaffolds because\n * `/new-canvas` stopped being a file-writing command. Copilot's `/create-canvas`\n * takes a sentence, has the agent write the extension, and opens it in a panel to\n * iterate on; matching that means the command has to reach the canvas session to\n * open, and the agent loop to build. Every decision that does not need either —\n * what the name is, where the file goes, what goes in it, what the model is told —\n * is here, testable without a terminal, a fork or a model.\n */\n\nimport { existsSync, mkdirSync, writeFileSync } from \"node:fs\";\nimport { dirname, join } from \"node:path\";\nimport type { MarketplacePlatform } from \"../extensions/plugins/formats/types.js\";\nimport { CANVAS_ENTRY_FILE } from \"./discovery.js\";\n\n/**\n * Where a canvas extension lives, per platform.\n *\n * This does not go through `WorkspaceLayout` like the other scaffolds, and the\n * reason is that a canvas has no Claude convention to emit into: the surface\n * exists in Copilot and in hoocode's own `.agents/` tree and nowhere else. A\n * layout method returning nothing for one adapter would be a worse lie than\n * naming the two real homes here — these are exactly the roots\n * `core/canvas/discovery.ts` searches, which is what makes a scaffold live on the\n * next `/canvas`.\n */\nexport const CANVAS_HOMES: Partial<Record<MarketplacePlatform, string[]>> = {\n\tagents: [\".agents\", \"extensions\"],\n\tgithub: [\".github\", \"extensions\"],\n};\n\n/** Validates a canvas name: lowercase a-z, 0-9, hyphens, no leading/trailing/double hyphens. */\nexport function validateCanvasName(name: string): string | null {\n\tif (!name) return \"name is required\";\n\tif (!/^[a-z0-9-]+$/.test(name)) return \"name must be lowercase a-z, 0-9, and hyphens only\";\n\tif (name.startsWith(\"-\") || name.endsWith(\"-\")) return \"name must not start or end with a hyphen\";\n\tif (name.includes(\"--\")) return \"name must not contain consecutive hyphens\";\n\treturn null;\n}\n\n/**\n * Words dropped wherever they appear, because they carry no subject.\n *\n * Only grammar, never subject matter: a list that reached further would start\n * deciding which of someone's nouns mattered.\n */\nconst FILLER = new Set([\n\t\"a\",\n\t\"about\",\n\t\"across\",\n\t\"an\",\n\t\"and\",\n\t\"are\",\n\t\"as\",\n\t\"at\",\n\t\"by\",\n\t\"be\",\n\t\"can\",\n\t\"for\",\n\t\"from\",\n\t\"i\",\n\t\"in\",\n\t\"is\",\n\t\"into\",\n\t\"it\",\n\t\"its\",\n\t\"me\",\n\t\"my\",\n\t\"of\",\n\t\"on\",\n\t\"one\",\n\t\"or\",\n\t\"our\",\n\t\"over\",\n\t\"so\",\n\t\"that\",\n\t\"the\",\n\t\"their\",\n\t\"them\",\n\t\"then\",\n\t\"this\",\n\t\"to\",\n\t\"us\",\n\t\"we\",\n\t\"which\",\n\t\"via\",\n\t\"when\",\n\t\"where\",\n\t\"while\",\n\t\"with\",\n\t\"you\",\n\t\"your\",\n]);\n\n/**\n * Words that open a request rather than describe one.\n *\n * People type `/new-canvas` as an instruction — \"create a…\", \"build me a…\",\n * \"help me…\", \"I want to…\" — and the opening clause was landing in the directory\n * name: `create-lightweight-games`, `help-compare-two`, `want-review-pull`. On a\n * spread of twelve realistic descriptions, seven produced a name that named the\n * request instead of the thing.\n *\n * These are dropped anywhere, not only at the front, because \"add a canvas that\n * shows X\" buries one in the middle.\n */\nconst REQUEST_WORDS = new Set([\n\t\"add\",\n\t\"build\",\n\t\"could\",\n\t\"create\",\n\t\"design\",\n\t\"generate\",\n\t\"give\",\n\t\"help\",\n\t\"let\",\n\t\"make\",\n\t\"need\",\n\t\"new\",\n\t\"please\",\n\t\"produce\",\n\t\"set\",\n\t\"show\",\n\t\"something\",\n\t\"up\",\n\t\"want\",\n\t\"would\",\n]);\n\n/** Counting words: \"compare two benchmark runs\" is about the runs. */\nconst CARDINALS = new Set([\"two\", \"three\", \"four\", \"five\", \"six\", \"seven\", \"eight\", \"nine\", \"ten\"]);\n\n/** How many words a derived name keeps. Enough to be recognisable, short enough to type. */\nconst DERIVED_NAME_WORDS = 3;\n\n/**\n * Whether a word is probably a verb form rather than the thing being described.\n *\n * Crude on purpose — no part-of-speech tagger for a directory name. It exists\n * because a participle sits between the request and its subject and pushes the\n * subject out of a three-word name: \"a dashboard **showing** flaky tests\" became\n * `dashboard-showing-flaky`, and \"a spreadsheet for **tracking** API latency\"\n * became `spreadsheet-tracking-api`. The length floor spares short words where\n * the ending is a coincidence (`feed`, `used`, `ring`).\n */\nfunction looksLikeVerbForm(word: string): boolean {\n\treturn word.length > 4 && (word.endsWith(\"ing\") || word.endsWith(\"ed\"));\n}\n\n/**\n * Turn a sentence into a directory name.\n *\n * Returns undefined when nothing usable survives — an all-punctuation request, or\n * a sentence of nothing but filler — because inventing `canvas-1` would hide from\n * the person that we did not understand them.\n */\nexport function canvasNameFromDescription(description: string): string | undefined {\n\tconst words = description\n\t\t.toLowerCase()\n\t\t.replace(/[^a-z0-9]+/g, \" \")\n\t\t.trim()\n\t\t.split(\" \")\n\t\t.filter((word) => word.length > 0);\n\n\tconst content = words.filter((word) => !FILLER.has(word) && !REQUEST_WORDS.has(word) && !CARDINALS.has(word));\n\t// \"canvas\" is dropped only when something else remains: \"/new-canvas a canvas\"\n\t// should still produce `canvas` rather than nothing.\n\tconst named = content.filter((word) => word !== \"canvas\");\n\tlet kept = named.length > 0 ? named : content;\n\n\t// Prefer nouns — but only while enough of them remain. Where they do not, the\n\t// participle *is* the subject (\"a canvas for onboarding\") and dropping it would\n\t// leave nothing worth naming.\n\tconst nouns = kept.filter((word) => !looksLikeVerbForm(word));\n\tif (nouns.length >= 2) kept = nouns;\n\n\tconst name = kept.slice(0, DERIVED_NAME_WORDS).join(\"-\");\n\treturn validateCanvasName(name) === null ? name : undefined;\n}\n\n/** What a person asked `/new-canvas` for. */\nexport interface CanvasRequest {\n\t/** Directory and default canvas id. */\n\tname: string;\n\t/**\n\t * What they want it to do, in their words, or undefined when they only named\n\t * one. Present means the model is expected to build it (Copilot's\n\t * `/create-canvas` shape); absent means they want the template to edit by hand.\n\t */\n\tdescription: string | undefined;\n}\n\n/**\n * Parse `/new-canvas`'s argument.\n *\n * Three shapes, in the order they are tested:\n *\n * - `my-board` — a bare name. Unchanged from before descriptions existed, and\n * tested first so it can never be re-read as a one-word description.\n * - `my-board: a kanban board for the release checklist` — both, when someone\n * cares what the directory is called. A colon rather than a flag because the\n * rest of the line is prose and a flag parser would have to guess where it ends.\n * - `a kanban board for the release checklist` — a description, the shape\n * `/create-canvas` uses. The name is derived and reported.\n *\n * Returns a string when the input cannot become a canvas; the caller shows it.\n */\nexport function parseCanvasRequest(input: string): CanvasRequest | string {\n\tconst trimmed = input.trim();\n\tif (trimmed.length === 0) return \"name or description is required\";\n\n\tif (validateCanvasName(trimmed) === null) return { name: trimmed, description: undefined };\n\n\tconst colon = trimmed.indexOf(\":\");\n\tif (colon > 0) {\n\t\tconst name = trimmed.slice(0, colon).trim();\n\t\tconst description = trimmed.slice(colon + 1).trim();\n\t\tif (validateCanvasName(name) === null && description.length > 0) return { name, description };\n\t}\n\n\tconst derived = canvasNameFromDescription(trimmed);\n\tif (!derived) {\n\t\treturn `could not derive a directory name from that. Give one explicitly: /new-canvas <name>: ${trimmed}`;\n\t}\n\treturn { name: derived, description: trimmed };\n}\n\n/**\n * A working single-canvas extension.\n *\n * Deliberately complete rather than a stub: a canvas has no passive half — its\n * name, its actions and its UI all come from running its code — so a scaffold\n * that does not run teaches nothing and cannot be checked with `/canvas open`.\n * This one opens, serves a page, answers an action, and closes cleanly, which is\n * the whole contract; everything past that is the author's.\n *\n * Shaped like the catalog extensions hoocode already hosts: the only import is\n * `@github/copilot-sdk/extension` (host-resolved — never installed, see\n * `docs/canvas-extensions-design.md` §4.1) plus `node:` builtins, and the UI is\n * served from an ephemeral loopback port behind a per-instance token so nothing\n * else on the machine can read it.\n */\nexport const CANVAS_ENTRY_TEMPLATE = (name: string): string => `\\\n// A canvas extension. Run it with: /canvas open ${name}\n//\n// The \"@github/copilot-sdk/extension\" import is resolved by the host at fork\n// time. Do not install it, and do not add a node_modules here.\nimport { createCanvas, CanvasError, joinSession } from \"@github/copilot-sdk/extension\";\nimport { randomBytes } from \"node:crypto\";\nimport { createServer } from \"node:http\";\n\n// The canvas's identity, in one place. An open instance is bound to this id, so\n// changing it drops the canvas anyone is currently looking at — rename the\n// canvas with \\`/canvas rename\\`, or change NAME alone if you only want a\n// different label on the page.\nconst ID = \"${name}\";\nconst NAME = \"${name}\";\n\n/** Per-instance state. A canvas can be opened more than once at a time. */\nconst instances = new Map();\n\nconst session = await joinSession({\n\tcanvases: [\n\t\tcreateCanvas({\n\t\t\tid: ID,\n\t\t\tdisplayName: NAME,\n\t\t\tdescription: \"TODO: one sentence — the agent reads this to decide whether to open it.\",\n\t\t\tactions: [\n\t\t\t\t{\n\t\t\t\t\tname: \"add_note\",\n\t\t\t\t\tdescription: \"TODO: describe what the agent can do to this canvas.\",\n\t\t\t\t\tinputSchema: {\n\t\t\t\t\t\ttype: \"object\",\n\t\t\t\t\t\tproperties: { text: { type: \"string\" } },\n\t\t\t\t\t\trequired: [\"text\"],\n\t\t\t\t\t},\n\t\t\t\t\thandler: (ctx) => {\n\t\t\t\t\t\tconst entry = instances.get(ctx.instanceId);\n\t\t\t\t\t\t// CanvasError carries a code the host shows verbatim; a bare\n\t\t\t\t\t\t// throw arrives as an opaque internal error instead.\n\t\t\t\t\t\tif (!entry) throw new CanvasError(\"no_instance\", \\`Instance \"\\${ctx.instanceId}\" is not open.\\`);\n\t\t\t\t\t\tentry.notes.push(ctx.input.text);\n\t\t\t\t\t\treturn { notes: entry.notes.length };\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t],\n\t\t\topen: async (ctx) => {\n\t\t\t\tconst token = randomBytes(32).toString(\"base64url\");\n\t\t\t\tconst notes = [];\n\t\t\t\tconst server = createServer((req, res) => {\n\t\t\t\t\tconst url = new URL(req.url ?? \"/\", \"http://127.0.0.1\");\n\t\t\t\t\tif (url.searchParams.get(\"token\") !== token) {\n\t\t\t\t\t\tres.writeHead(403, { \"Content-Type\": \"text/plain\" });\n\t\t\t\t\t\tres.end(\"forbidden\");\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\t\t\t\t\tres.writeHead(200, { \"Content-Type\": \"text/html; charset=utf-8\" });\n\t\t\t\t\tres.end(\n\t\t\t\t\t\t\\`<!doctype html><meta charset=\"utf-8\"><title>\\${NAME}</title>\\` +\n\t\t\t\t\t\t\t\\`<p>\\${notes.length} note(s). TODO: build the UI.\\`,\n\t\t\t\t\t);\n\t\t\t\t});\n\t\t\t\t// Port 0 on 127.0.0.1: an ephemeral port, reachable only from this machine.\n\t\t\t\tawait new Promise((resolve) => server.listen(0, \"127.0.0.1\", resolve));\n\t\t\t\tconst { port } = server.address();\n\t\t\t\tinstances.set(ctx.instanceId, { server, notes });\n\t\t\t\treturn {\n\t\t\t\t\turl: \\`http://127.0.0.1:\\${port}/?token=\\${token}\\`,\n\t\t\t\t\ttitle: NAME,\n\t\t\t\t};\n\t\t\t},\n\t\t\tonClose: async (ctx) => {\n\t\t\t\tconst entry = instances.get(ctx.instanceId);\n\t\t\t\t// Called for an abandoned open too, so the instance may be unknown.\n\t\t\t\tif (!entry) return;\n\t\t\t\tinstances.delete(ctx.instanceId);\n\t\t\t\tawait new Promise((resolve) => entry.server.close(resolve));\n\t\t\t},\n\t\t}),\n\t],\n});\n\n// stdout is the protocol channel. Use session.log, never console.log.\nawait session.log(\\`\\${ID} ready\\`);\n`;\n\n/** What {@link scaffoldCanvas} wrote. */\nexport interface CanvasScaffoldResult {\n\t/** Workspace-relative entry files created, one per platform target. */\n\tcreated: string[];\n\t/** Workspace-relative entry files that already existed and were left alone. */\n\tskipped: string[];\n}\n\n/**\n * Write the template into every platform target that has a canvas home.\n *\n * Existing files are never clobbered — they are reported and skipped, so running\n * `/new-canvas` twice on a canvas you have been editing cannot lose it.\n */\nexport function scaffoldCanvas(cwd: string, name: string, platforms: MarketplacePlatform[]): CanvasScaffoldResult {\n\tconst created: string[] = [];\n\tconst skipped: string[] = [];\n\tfor (const platform of platforms) {\n\t\tconst home = CANVAS_HOMES[platform];\n\t\tif (!home) continue;\n\t\tconst relative = join(...home, name, CANVAS_ENTRY_FILE);\n\t\tconst absolute = join(cwd, relative);\n\t\tif (existsSync(absolute)) {\n\t\t\tskipped.push(relative);\n\t\t\tcontinue;\n\t\t}\n\t\tmkdirSync(dirname(absolute), { recursive: true });\n\t\twriteFileSync(absolute, CANVAS_ENTRY_TEMPLATE(name), \"utf8\");\n\t\tcreated.push(relative);\n\t}\n\treturn { created, skipped };\n}\n\n/** Where a build brief's canvas is, if opening it worked. */\nexport interface CanvasBriefTarget {\n\tinstanceId: string;\n\turl: string | undefined;\n}\n\n/**\n * The message the model builds from — hoocode's half of `/create-canvas`.\n *\n * A scaffold plus a sentence is not a canvas, and the gap between them is the\n * agent's work. This is what turns \"a kanban board for the release checklist\"\n * into a build task with the contract attached, so the model does not have to\n * infer the rules of a surface it cannot see from a template it has not read.\n *\n * Four of those rules are stated because getting them wrong fails in ways whose\n * symptom does not name the cause: an installed dependency (the resolver already\n * provides the SDK, and a `node_modules` here is a §4.1 violation), a\n * `console.log` (corrupts the JSON-RPC channel and surfaces as \"non-protocol\n * stdout\"), a write without a reload (changes nothing at all, because the running\n * child was forked from the old bytes), and a renamed canvas id.\n *\n * That last one is the newest and was found the hard way, by building a canvas\n * with this command: the scaffold names the canvas after the directory, a model\n * that thinks of a better name renames the `id`, and the next reload drops the\n * instance the person is looking at — correctly, since the canvas it was opened\n * against no longer exists. `displayName` is the half they actually see, so it\n * is the half to change.\n */\nexport function canvasBuildBrief(\n\tname: string,\n\tdescription: string,\n\tentryPath: string,\n\ttarget: CanvasBriefTarget | undefined,\n): string {\n\tconst lines = [\n\t\t`/new-canvas: build a canvas extension named \"${name}\".`,\n\t\t\"\",\n\t\t\"What the person asked for, in their words:\",\n\t\t` ${description}`,\n\t\t\"\",\n\t\t`A working template is already at ${entryPath}. Edit it until it does what was asked.`,\n\t];\n\n\tif (target) {\n\t\tlines.push(\n\t\t\t\"\",\n\t\t\ttarget.url\n\t\t\t\t? `It is already open at ${target.url} (instance ${target.instanceId}), so the person is watching it as you work.`\n\t\t\t\t: `It is already open as instance ${target.instanceId}.`,\n\t\t\t`After every edit, call reload_canvas with extensionId \"${name}\". A write to the file changes nothing on its own — the running process was forked from the old code. Reloading hands back a NEW url; give it to the person, because the tab they have open dies with the old process.`,\n\t\t);\n\t} else {\n\t\tlines.push(\n\t\t\t\"\",\n\t\t\t`It is not open — the person will run /canvas open ${name} when they want to look. Build it anyway; you can check it runs by reading it carefully rather than by opening it yourself.`,\n\t\t);\n\t}\n\n\tlines.push(\n\t\t\"\",\n\t\t\"The contract this surface runs under, which is not yours to change:\",\n\t\t'- The only non-`node:` import allowed is \"@github/copilot-sdk/extension\". The host resolves it when it forks the extension. Do not install anything, and do not add a package.json or node_modules in the extension directory.',\n\t\t\"- stdout is the JSON-RPC channel. Use `session.log(...)`; a `console.log` corrupts the protocol.\",\n\t\t\"- Do not change the canvas's `id` (the template holds it in `ID`). An open instance is bound to it, so renaming it drops the canvas the person is watching on your next reload. Change `NAME` for a nicer label — that is the one they see — and if the directory name itself is wrong, say so and let them run `/canvas rename`, which moves everything at once.\",\n\t\t\"- After a reload, read the action list it reports back. That is the host telling you which of your actions it can actually see, and it is the only confirmation an action you just wrote is really callable.\",\n\t\t\"- Serve the UI from the loopback server the template already starts. Keep the per-instance token check, and keep `onClose` shutting the server down — that is what stops a port outliving the session.\",\n\t\t\"- Everything in `actions: [...]` becomes callable by you through invoke_canvas_action once it is open, so give each action a real description and inputSchema. That list is how you drive the canvas; the person drives the same state through the page.\",\n\t\t\"\",\n\t\t\"Build the thing that was asked for, not a stub: leave no TODO behind, and make the page render real state rather than a placeholder.\",\n\t);\n\treturn lines.join(\"\\n\");\n}\n"]}