@powerhousedao/ph-clint-cli 0.1.0-dev.3 → 0.1.0-dev.31

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 (78) hide show
  1. package/dist/cli.js +31 -12
  2. package/dist/codegen/builders/agent-ts.d.ts +4 -4
  3. package/dist/codegen/builders/agent-ts.js +83 -2
  4. package/dist/codegen/builders/app-index-ts.d.ts +3 -10
  5. package/dist/codegen/builders/app-index-ts.js +10 -14
  6. package/dist/codegen/builders/cli-package-json.js +48 -8
  7. package/dist/codegen/builders/cli-ts.js +63 -22
  8. package/dist/codegen/builders/config-ts.js +11 -15
  9. package/dist/codegen/builders/demo-agent-ts.d.ts +8 -0
  10. package/dist/codegen/builders/demo-agent-ts.js +44 -0
  11. package/dist/codegen/builders/framework-gen-ts.d.ts +4 -5
  12. package/dist/codegen/builders/framework-gen-ts.js +22 -19
  13. package/dist/codegen/builders/framework-ts.js +33 -7
  14. package/dist/codegen/builders/index.d.ts +9 -1
  15. package/dist/codegen/builders/index.js +28 -4
  16. package/dist/codegen/builders/manifest-json.d.ts +6 -0
  17. package/dist/codegen/builders/manifest-json.js +31 -0
  18. package/dist/codegen/builders/mastra-index-ts.d.ts +3 -0
  19. package/dist/codegen/builders/mastra-index-ts.js +47 -5
  20. package/dist/codegen/builders/publish-config-ts.d.ts +6 -0
  21. package/dist/codegen/builders/publish-config-ts.js +21 -0
  22. package/dist/codegen/builders/readme-md.js +8 -7
  23. package/dist/codegen/builders/root-package-json.d.ts +3 -0
  24. package/dist/codegen/builders/root-package-json.js +24 -11
  25. package/dist/codegen/index.js +26 -12
  26. package/dist/codegen/scaffold.d.ts +21 -3
  27. package/dist/codegen/scaffold.js +90 -16
  28. package/dist/commands/clint-project-build.js +10 -27
  29. package/dist/commands/clint-project-init.d.ts +30 -6
  30. package/dist/commands/clint-project-init.js +45 -9
  31. package/dist/commands/clint-project-publish.d.ts +1 -0
  32. package/dist/commands/clint-project-publish.js +5 -3
  33. package/dist/commands/clint-project-regen.js +2 -2
  34. package/dist/commands/clint-skills-sync.d.ts +10 -0
  35. package/dist/commands/clint-skills-sync.js +56 -0
  36. package/dist/config.d.ts +3 -5
  37. package/dist/config.js +5 -7
  38. package/dist/framework.d.ts +5 -169
  39. package/dist/framework.gen.d.ts +1 -32
  40. package/dist/framework.gen.js +1 -1
  41. package/dist/framework.js +0 -4
  42. package/{prompts/agent-profiles/AgentBase.md → dist/gen/agent-profiles/PhClintDevAgent.md} +1 -1
  43. package/dist/gen/skills/cli-setup/.cli-docs.md +135 -0
  44. package/dist/gen/skills/cli-setup/SKILL.md +125 -0
  45. package/dist/gen/skills/cli-setup/references/00.assess-cli-needs.md +53 -0
  46. package/dist/gen/skills/cli-setup/references/01.define-config-schema.md +78 -0
  47. package/dist/gen/skills/cli-setup/references/02.wire-cli.md +141 -0
  48. package/dist/gen/skills/cli-setup/references/03.add-entrypoint.md +128 -0
  49. package/dist/gen/skills/cli-setup/references/expected-outcome.md +3 -0
  50. package/dist/gen/skills/command-definition/.cli-docs.md +84 -0
  51. package/dist/gen/skills/command-definition/SKILL.md +99 -0
  52. package/dist/gen/skills/command-definition/references/00.assess-requirements.md +58 -0
  53. package/dist/gen/skills/command-definition/references/01.define-schema.md +72 -0
  54. package/dist/gen/skills/command-definition/references/02.implement-execute.md +118 -0
  55. package/dist/gen/skills/command-definition/references/03.add-prompting.md +54 -0
  56. package/dist/gen/skills/command-definition/references/expected-outcome.md +3 -0
  57. package/dist/gen/skills/service-definition/.cli-docs.md +214 -0
  58. package/dist/gen/skills/service-definition/SKILL.md +148 -0
  59. package/dist/gen/skills/service-definition/references/00.assess-service.md +69 -0
  60. package/dist/gen/skills/service-definition/references/01.define-service.md +166 -0
  61. package/dist/gen/skills/service-definition/references/02.add-preflight.md +132 -0
  62. package/dist/gen/skills/service-definition/references/03.add-project-scanner.md +82 -0
  63. package/dist/gen/skills/service-definition/references/04.wire-events.md +120 -0
  64. package/dist/gen/skills/service-definition/references/expected-outcome.md +3 -0
  65. package/dist/services/clint-project.js +21 -0
  66. package/dist/skills/sync.d.ts +40 -0
  67. package/dist/skills/sync.js +119 -0
  68. package/dist/spec/from-document.d.ts +4 -1
  69. package/dist/spec/from-document.js +18 -8
  70. package/dist/spec/types.d.ts +85 -6
  71. package/dist/spec/types.js +90 -23
  72. package/dist/triggers/publish-trigger.d.ts +10 -0
  73. package/dist/triggers/publish-trigger.js +169 -0
  74. package/dist/triggers/spec-change.d.ts +5 -34
  75. package/dist/triggers/spec-change.js +175 -99
  76. package/package.json +7 -7
  77. package/prompts/skills-ext/.gitkeep +0 -0
  78. package/prompts/skills-tpl/.gitkeep +0 -0
@@ -1,26 +1,30 @@
1
1
  /**
2
- * `spec-change` trigger — watches the `powerhouse/ph-clint-project` document
3
- * on the Reactor drive and, when its state changes, enqueues a work item
4
- * that re-runs the code generator in update mode.
2
+ * `spec-change` trigger — watches ALL `powerhouse/ph-clint-project` documents
3
+ * and, when any change, re-runs the code generator for each affected project.
5
4
  *
6
5
  * Flow:
7
6
  * 1. `createDocumentChangeTrigger` (from ph-clint) owns the subscription,
8
- * event coalescing, doc loading, and type narrowing.
9
- * 2. `documentId` resolves the target doc lazily: it prefers
10
- * `config.projectDocumentId`, falling back to a drive scan for the first
11
- * `powerhouse/ph-clint-project` document (and pinning the id into local
12
- * config on success).
13
- * 3. `onChange` converts the doc state to a `ClintProjectSpec`, hashes it,
7
+ * event coalescing, doc loading, and type narrowing. No `documentId` is
8
+ * set, so it loads ALL matching documents on each poll.
9
+ * 2. `getProjectMapping()` merges an on-disk scan (via the clint-project
10
+ * service's projectScanner) with personal-drive folder entries to
11
+ * correlate each document to an on-disk project directory.
12
+ * 3. For each document, `specFromDocumentState()` builds a spec, hashes it,
14
13
  * and compares against `.ph/ph-clint-cli/.last-spec-hash`. If unchanged,
15
- * emits nothing. If different, returns a `function` work item that calls
16
- * `generateProject(spec, workdir, 'update')` and writes the new hash.
14
+ * skips. Otherwise regenerates at the mapped target directory.
17
15
  */
18
16
  import crypto from 'node:crypto';
19
17
  import fs from 'node:fs/promises';
20
18
  import path from 'node:path';
19
+ import { scanProjects, getProjectMapping } from '@powerhousedao/ph-clint';
20
+ import { importSpec } from '@powerhousedao/ph-clint-app/document-models/ph-clint-project';
21
21
  import { createDocumentChangeTrigger } from '../framework.js';
22
22
  import { generateProject } from '../codegen/index.js';
23
+ import { runPhInit } from '../codegen/scaffold.js';
23
24
  import { specFromDocumentState } from '../spec/from-document.js';
25
+ import { readProjectSpec, writeProjectSpec } from '../spec/file.js';
26
+ import { syncExternalSkills } from '../skills/sync.js';
27
+ import { clintProject } from '../services/clint-project.js';
24
28
  const DOCUMENT_TYPE = 'powerhouse/ph-clint-project';
25
29
  const HASH_DIR = path.join('.ph', 'ph-clint-cli');
26
30
  const HASH_FILE = '.last-spec-hash';
@@ -49,11 +53,13 @@ async function writeLastSpecHash(targetDir, hash) {
49
53
  await fs.mkdir(path.dirname(file), { recursive: true });
50
54
  await fs.writeFile(file, hash + '\n', 'utf8');
51
55
  }
52
- /** Stable hash of a ClintProjectSpec (sha256 of canonical-key JSON). */
56
+ /** Stable hash of a ClintProjectSpec (sha256 of canonical-key JSON).
57
+ * Excludes documentId/documentType — they don't affect codegen output. */
53
58
  export function hashSpec(spec) {
59
+ const { documentId, documentType, ...codegenRelevant } = spec;
54
60
  return crypto
55
61
  .createHash('sha256')
56
- .update(canonicalJson(spec), 'utf8')
62
+ .update(canonicalJson(codegenRelevant), 'utf8')
57
63
  .digest('hex');
58
64
  }
59
65
  function canonicalJson(value) {
@@ -70,102 +76,172 @@ function canonicalJson(value) {
70
76
  .join(',') +
71
77
  '}');
72
78
  }
73
- /** Find the first ph-clint-project document on the default drive. */
74
- async function findProjectDocumentId(client, driveId) {
75
- try {
76
- const children = await client.getChildren(driveId);
77
- const results = (children?.results ?? []);
78
- for (const node of results) {
79
- if (node.header?.documentType === DOCUMENT_TYPE && node.header.id) {
80
- return node.header.id;
81
- }
82
- }
83
- }
84
- catch {
85
- // fall through
86
- }
87
- return undefined;
79
+ export function specToImportInput(spec) {
80
+ return {
81
+ name: spec.name,
82
+ scope: spec.scope ?? null,
83
+ version: spec.version,
84
+ description: spec.description,
85
+ bin: spec.bin ?? null,
86
+ powerhouse: spec.features.powerhouse,
87
+ mastraEnabled: spec.features.mastra.enabled,
88
+ routineEnabled: spec.features.routine.enabled,
89
+ agentId: spec.features.mastra.agentId ?? null,
90
+ agentName: spec.features.mastra.agentName ?? null,
91
+ models: spec.features.mastra.models.map(m => ({
92
+ id: m.id,
93
+ isDefault: m.isDefault,
94
+ })),
95
+ profiles: spec.features.mastra.profiles.map(p => ({
96
+ id: p.id,
97
+ title: p.title,
98
+ content: p.content,
99
+ })),
100
+ packages: spec.packages.map(p => ({
101
+ id: p.id,
102
+ packageName: p.packageName,
103
+ documentTypes: p.documentTypes,
104
+ })),
105
+ externalSkills: spec.externalSkills.map(s => ({
106
+ id: s.id,
107
+ name: s.name,
108
+ githubUrl: s.githubUrl,
109
+ })),
110
+ };
88
111
  }
89
112
  export const specChangeTrigger = createDocumentChangeTrigger({
90
113
  id: 'spec-change',
91
114
  documentType: DOCUMENT_TYPE,
92
- // Resolve the target id lazily so we can also do first-time discovery and
93
- // persist the discovered id into local config.
94
- documentId: async (ctx) => {
95
- const configured = ctx.context.config.projectDocumentId &&
96
- ctx.context.config.projectDocumentId.length > 0
97
- ? ctx.context.config.projectDocumentId
98
- : undefined;
99
- if (configured)
100
- return configured;
115
+ // No documentId → watches ALL ph-clint-project docs
116
+ async onChange(docs, ctx) {
117
+ const log = ctx.context.log;
118
+ const folders = ctx.context.folders;
119
+ // Build the mapping: on-disk projects + personal drive documents
120
+ const scanner = clintProject.projectScanner;
121
+ const scanResults = scanner
122
+ ? scanProjects(ctx.context.workdir, scanner)
123
+ : [];
124
+ const mapping = await getProjectMapping(scanResults, folders);
125
+ // Auto-create spec documents for unlinked projects
101
126
  const reactor = await ctx.reactor();
102
- if (!reactor)
103
- return undefined;
104
- const discovered = await findProjectDocumentId(reactor.client, reactor.driveId);
105
- if (!discovered) {
106
- ctx.context.log?.debug(`${TAG} no ${DOCUMENT_TYPE} document found on drive ${reactor.driveId}`);
107
- return undefined;
127
+ if (reactor && folders) {
128
+ for (const entry of mapping) {
129
+ if (entry.path && !entry.documentId) {
130
+ const existingSpec = await readProjectSpec(entry.path);
131
+ if (!existingSpec)
132
+ continue;
133
+ log?.info(`${TAG} creating spec document for unlinked project ${entry.name}`);
134
+ const newDoc = await reactor.client.createEmpty('powerhouse/ph-clint-project');
135
+ const docId = newDoc.header.id;
136
+ await folders.addDocument(docId, `specs/${entry.name}`, entry.name);
137
+ const importInput = specToImportInput(existingSpec);
138
+ await reactor.client.execute(docId, 'main', [importSpec(importInput)]);
139
+ existingSpec.documentId = docId;
140
+ existingSpec.documentType = 'powerhouse/ph-clint-project';
141
+ await writeProjectSpec(entry.path, existingSpec);
142
+ log?.info(`${TAG} linked project ${entry.name} to document ${docId}`);
143
+ }
144
+ }
108
145
  }
109
- // Persist into local config + mutate in-memory so future ticks skip discovery.
110
- try {
111
- const current = await ctx.context.workspace.loadLocalConfig({});
112
- await ctx.context.workspace.storeLocalConfig({
113
- ...current,
114
- projectDocumentId: discovered,
146
+ for (const doc of docs) {
147
+ const spec = specFromDocumentState(doc.state.global, {
148
+ documentId: doc.header.id,
149
+ documentType: doc.header.documentType,
115
150
  });
116
- ctx.context.config.projectDocumentId = discovered;
117
- ctx.context.log?.info(`${TAG} pinned projectDocumentId=${discovered} in ${ctx.context.workspace.getLocalConfigPath()}`);
118
- }
119
- catch (err) {
120
- ctx.context.log?.warn(`${TAG} failed to persist projectDocumentId: ${err instanceof Error ? err.message : String(err)}`);
121
- }
122
- return discovered;
123
- },
124
- async onChange(doc, ctx) {
125
- const log = ctx.context.log;
126
- const spec = specFromDocumentState(doc.state.global);
127
- if (!spec) {
128
- log?.debug(`${TAG} document ${doc.header.id} state did not validate against ClintProjectSpec, skipping`);
129
- return null;
130
- }
131
- const targetDir = ctx.context.workdir;
132
- const nextHash = hashSpec(spec);
133
- const prevHash = await readLastSpecHash(targetDir);
134
- if (nextHash === prevHash) {
135
- log?.debug(`${TAG} spec unchanged (hash=${nextHash.slice(0, 12)}…), nothing to do`);
136
- return null;
137
- }
138
- log?.debug(`${TAG} spec changed: ${prevHash ? prevHash.slice(0, 12) + '…' : '<none>'} → ${nextHash.slice(0, 12)}…`);
139
- return {
140
- type: 'function',
141
- params: {
142
- fn: async () => {
143
- log?.info(`${TAG} regenerating project at ${targetDir} (doc=${doc.header.id})`);
144
- const t0 = Date.now();
145
- const warnings = [];
146
- const result = await generateProject({
151
+ if (!spec) {
152
+ log?.debug(`${TAG} document ${doc.header.id} state did not validate against ClintProjectSpec, skipping`);
153
+ continue;
154
+ }
155
+ // Find target directory from mapping
156
+ const entry = mapping.find((m) => m.documentId === doc.header.id);
157
+ const targetDir = entry?.path ?? path.join(ctx.context.workdir, spec.name);
158
+ // Per-project hash comparison
159
+ const nextHash = hashSpec(spec);
160
+ const prevHash = await readLastSpecHash(targetDir);
161
+ if (nextHash === prevHash) {
162
+ log?.debug(`${TAG} spec for ${spec.name} unchanged (hash=${nextHash.slice(0, 12)}…), skipping`);
163
+ continue;
164
+ }
165
+ log?.debug(`${TAG} spec for ${spec.name} changed: ${prevHash ? prevHash.slice(0, 12) + '…' : '<none>'} → ${nextHash.slice(0, 12)}…`);
166
+ log?.info(`${TAG} regenerating ${spec.name} at ${targetDir} (doc=${doc.header.id})`);
167
+ const t0 = Date.now();
168
+ const warnings = [];
169
+ const result = await generateProject({
170
+ targetDir,
171
+ spec,
172
+ mode: 'update',
173
+ force: true,
174
+ onWarn: (m) => warnings.push(m),
175
+ });
176
+ // Write spec JSON with documentId
177
+ await writeProjectSpec(targetDir, spec);
178
+ await writeLastSpecHash(targetDir, nextHash);
179
+ const elapsed = Date.now() - t0;
180
+ if (log) {
181
+ if (result.migrated)
182
+ log.info(`${TAG} migrated flat → split layout`);
183
+ log.info(`${TAG} regenerated ${spec.name} in ${elapsed}ms: ${result.files.length} written, ${result.skipped.length} skipped, ${result.deleted.length} deleted`);
184
+ for (const w of warnings)
185
+ log.warn(`${TAG} ${w}`);
186
+ }
187
+ // Scaffold the reactor package (app dir) if it hasn't been initialized yet.
188
+ if (result.appDir) {
189
+ const appPkgJson = path.join(result.appDir, 'package.json');
190
+ let appExists = false;
191
+ try {
192
+ await fs.access(appPkgJson);
193
+ appExists = true;
194
+ }
195
+ catch { /* noop */ }
196
+ if (!appExists) {
197
+ log?.info(`${TAG} initializing reactor package at ${result.appDir}`);
198
+ const phResult = await runPhInit({
147
199
  targetDir,
200
+ appDir: result.appDir,
148
201
  spec,
149
- mode: 'update',
150
- onWarn: (m) => warnings.push(m),
202
+ log: (msg) => log?.info(`${TAG} ${msg}`),
203
+ runProcess: ctx.context.runProcess,
151
204
  });
152
- await writeLastSpecHash(targetDir, nextHash);
153
- const elapsed = Date.now() - t0;
154
- if (log) {
155
- if (result.migrated)
156
- log.info(`${TAG} migrated flat → split layout`);
157
- log.info(`${TAG} regenerated in ${elapsed}ms: ${result.files.length} written, ${result.skipped.length} skipped, ${result.deleted.length} deleted`);
158
- for (const w of warnings)
159
- log.warn(`${TAG} ${w}`);
205
+ if (!phResult.ran) {
206
+ log?.warn(`${TAG} ph init skipped: ${phResult.reason ?? 'unknown'}`);
160
207
  }
161
- },
162
- },
163
- callbacks: {
164
- onFailure: (err) => {
165
- log?.error(`${TAG} regen failed: ${err.message}`);
166
- log?.debug(`${TAG} stack: ${err.stack ?? '<no stack>'}`);
167
- },
168
- },
169
- };
208
+ else if (phResult.exitCode !== 0) {
209
+ log?.warn(`${TAG} ph init exited with code ${phResult.exitCode}`);
210
+ }
211
+ }
212
+ // Always ensure the app package name matches the spec's scope,
213
+ // even if ph init ran in a previous trigger cycle without scope.
214
+ if (spec.scope) {
215
+ try {
216
+ const raw = await fs.readFile(appPkgJson, 'utf8');
217
+ const pkg = JSON.parse(raw);
218
+ const appFolder = path.basename(result.appDir);
219
+ const scopedName = `@${spec.scope}/${appFolder}`;
220
+ if (pkg.name !== scopedName) {
221
+ pkg.name = scopedName;
222
+ pkg.publishConfig = { access: 'public' };
223
+ await fs.writeFile(appPkgJson, JSON.stringify(pkg, null, 2) + '\n');
224
+ log?.info(`${TAG} patched app package name to ${scopedName}`);
225
+ }
226
+ }
227
+ catch (err) {
228
+ log?.warn(`${TAG} could not patch app package name: ${err instanceof Error ? err.message : String(err)}`);
229
+ }
230
+ }
231
+ }
232
+ // Sync external skills after codegen.
233
+ if (spec.externalSkills.length > 0) {
234
+ const skillsResult = await syncExternalSkills({
235
+ targetDir,
236
+ desired: spec.externalSkills,
237
+ log: (msg) => log?.info(`${TAG} ${msg}`),
238
+ });
239
+ if (log && (skillsResult.added.length > 0 || skillsResult.removed.length > 0)) {
240
+ log.info(`${TAG} skills sync: +${skillsResult.added.length} -${skillsResult.removed.length}`);
241
+ }
242
+ }
243
+ }
244
+ // Work done inline, no additional work item needed
245
+ return null;
170
246
  },
171
247
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@powerhousedao/ph-clint-cli",
3
- "version": "0.1.0-dev.3",
3
+ "version": "0.1.0-dev.31",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -10,8 +10,7 @@
10
10
  "ph-clint": "./dist/main.js"
11
11
  },
12
12
  "files": [
13
- "dist",
14
- "prompts"
13
+ "dist"
15
14
  ],
16
15
  "scripts": {
17
16
  "build:skills": "tsx scripts/build-skills.ts",
@@ -19,21 +18,23 @@
19
18
  "dev": "tsx src/main.ts",
20
19
  "start": "node dist/main.js",
21
20
  "test": "NODE_OPTIONS='--experimental-vm-modules' jest --detectOpenHandles",
21
+ "test:e2e": "NODE_OPTIONS='--experimental-vm-modules' jest --detectOpenHandles --testPathPatterns 'tests/(e2e|codegen-e2e)/' --testPathIgnorePatterns '/node_modules/'",
22
22
  "lint": "eslint src",
23
23
  "mastra:dev": "mastra dev",
24
24
  "mastra:build": "mastra build",
25
25
  "mastra:start": "mastra start"
26
26
  },
27
27
  "dependencies": {
28
- "@powerhousedao/ph-clint": "^0.1.0-dev.3",
29
- "@powerhousedao/ph-clint-app": "^0.1.0-dev.3",
28
+ "@electric-sql/pglite": "0.3.15",
30
29
  "@mastra/core": "^1.22.0",
31
30
  "@mastra/libsql": "^1.7.4",
32
31
  "@mastra/memory": "^1.13.1",
32
+ "@powerhousedao/ph-clint": "^0.1.0-dev.31",
33
+ "@powerhousedao/ph-clint-app": "^0.1.0-dev.31",
34
+ "@powerhousedao/ph-clint-dev": "^0.1.0-dev.31",
33
35
  "@powerhousedao/reactor": "6.0.0-dev.170",
34
36
  "@powerhousedao/reactor-api": "6.0.0-dev.170",
35
37
  "@powerhousedao/shared": "6.0.0-dev.170",
36
- "@electric-sql/pglite": "0.3.15",
37
38
  "document-model": "6.0.0-dev.170",
38
39
  "kysely": "^0.27.0",
39
40
  "kysely-pglite-dialect": "^1.3.0",
@@ -48,7 +49,6 @@
48
49
  "@jest/globals": "^30.2.0",
49
50
  "@types/node": "^25.5.2",
50
51
  "jest": "^30.2.0",
51
- "@powerhousedao/ph-clint-dev": "^0.1.0-dev.3",
52
52
  "ts-jest": "^29.4.6",
53
53
  "tsx": "^4.19.0",
54
54
  "typescript": "^6.0.2"
File without changes
File without changes