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

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
@@ -0,0 +1,166 @@
1
+ # Phase 1: Write the defineService Call
2
+
3
+ Define the service with its identity, command, readiness detection, and lifecycle config.
4
+
5
+ ## Steps
6
+
7
+ ### 1. Set identity fields
8
+
9
+ ```typescript
10
+ import { defineService } from '../framework.js';
11
+
12
+ export const myService = defineService({
13
+ id: 'my-service', // Kebab-case — used for command names and state files
14
+ name: 'My Service', // Display name for logs and status output
15
+ description: 'Runs the development server',
16
+ });
17
+ ```
18
+
19
+ ### 2. Set the command
20
+
21
+ **Static command** — always the same:
22
+ ```typescript
23
+ command: 'pnpm dev',
24
+ ```
25
+
26
+ **Parameterized command** — varies per invocation:
27
+ ```typescript
28
+ command: (params) => {
29
+ const parts = ['pnpm', 'dev'];
30
+ if (params?.port) parts.push('-p', String(params.port));
31
+ if (params?.watch !== false) parts.push('--watch');
32
+ return parts.join(' ');
33
+ },
34
+ ```
35
+
36
+ ### 3. Add paramsSchema (if needed)
37
+
38
+ ```typescript
39
+ import { z } from 'zod';
40
+
41
+ const myServiceParams = z.object({
42
+ port: z.coerce.number().default(3000).describe('Dev server port'),
43
+ watch: z.boolean().default(true).describe('Enable file watching'),
44
+ });
45
+
46
+ export const myService = defineService({
47
+ // ...
48
+ paramsSchema: myServiceParams,
49
+ });
50
+ ```
51
+
52
+ Use `z.coerce.number()` for port parameters — CLI flags are always strings.
53
+
54
+ ### 4. Add environment variables
55
+
56
+ ```typescript
57
+ env: (config, params) => ({
58
+ PORT: String(params?.port ?? 3000),
59
+ NODE_ENV: 'development',
60
+ HOST: '0.0.0.0',
61
+ }),
62
+ ```
63
+
64
+ The `env` function receives the resolved CLI config and start params. Return a flat `Record<string, string>`.
65
+
66
+ ### 5. Configure readiness detection
67
+
68
+ **Single pattern** — one line to match:
69
+ ```typescript
70
+ readiness: {
71
+ pattern: /Local:\s*http:\/\/localhost:(\d+)/,
72
+ captures: { 'server-url': 1 },
73
+ timeout: 30_000,
74
+ },
75
+ ```
76
+
77
+ **Multiple patterns** — all must match:
78
+ ```typescript
79
+ readiness: {
80
+ patterns: [
81
+ {
82
+ name: 'web-ui',
83
+ pattern: /Local:\s*(http:\/\/localhost:\d+)/,
84
+ captures: { 'web-url': { group: 1, type: 'website' } },
85
+ },
86
+ {
87
+ name: 'api',
88
+ pattern: /API server:\s*(http:\/\/[^\s]+)/,
89
+ captures: { 'api-url': { group: 1, type: 'api-rest' } },
90
+ },
91
+ {
92
+ name: 'mcp',
93
+ pattern: /MCP server available at (http:\/\/[^\s]+)/,
94
+ captures: { 'mcp-url': { group: 1, type: 'api-mcp' } },
95
+ },
96
+ ],
97
+ timeout: 60_000,
98
+ },
99
+ ```
100
+
101
+ Tips:
102
+ - Use `[^\s]+` to match URLs (stops at whitespace)
103
+ - Set timeout high enough for slow services (Next.js builds can take 60s+)
104
+ - The `name` on each pattern appears in `service:pattern-matched` events for progress feedback
105
+ - Use typed captures (`{ group, type }`) for endpoints that should be discoverable
106
+
107
+ ### 6. Configure shutdown
108
+
109
+ ```typescript
110
+ shutdown: { signal: 'SIGTERM', timeout: 10_000 },
111
+ ```
112
+
113
+ The framework sends `signal` first. If the process doesn't exit within `timeout` ms, SIGKILL is sent.
114
+
115
+ ### 7. Configure restart (optional)
116
+
117
+ ```typescript
118
+ restart: { enabled: true, maxRetries: 3, delay: 5_000 },
119
+ ```
120
+
121
+ On crash, the service auto-restarts up to `maxRetries` times with `delay` ms between attempts.
122
+
123
+ ### 8. Set maxInstances (if needed)
124
+
125
+ ```typescript
126
+ maxInstances: 1, // Default — exclusive service (e.g., dev server)
127
+ maxInstances: 4, // Parallel workers
128
+ ```
129
+
130
+ ### 9. Assemble the full definition
131
+
132
+ ```typescript
133
+ import { defineService, checkWorkdir, checkCommand, checkPort } from '../framework.js';
134
+ import { z } from 'zod';
135
+
136
+ const devServerParams = z.object({
137
+ port: z.coerce.number().default(3000).describe('Dev server port'),
138
+ });
139
+
140
+ export const devServer = defineService({
141
+ id: 'dev-server',
142
+ name: 'Dev Server',
143
+ description: 'Next.js development server',
144
+ command: (params) => `pnpm dev -p ${params?.port ?? 3000}`,
145
+ paramsSchema: devServerParams,
146
+ env: (_config, params) => ({
147
+ PORT: String(params?.port ?? 3000),
148
+ NODE_ENV: 'development',
149
+ }),
150
+ preflight: [
151
+ checkWorkdir(
152
+ (cwd) => fs.existsSync(path.join(cwd, 'package.json')),
153
+ 'Not a Node.js project',
154
+ 'Run dev-server-start --workdir <project-dir>',
155
+ ),
156
+ checkPort((ctx) => (ctx.params?.port as number) ?? 3000, 'Dev Server'),
157
+ ],
158
+ readiness: {
159
+ pattern: /Local:\s*http:\/\/localhost:(\d+)/,
160
+ captures: { 'server-url': 1 },
161
+ timeout: 60_000,
162
+ },
163
+ shutdown: { signal: 'SIGTERM', timeout: 10_000 },
164
+ restart: { enabled: true, maxRetries: 3, delay: 5_000 },
165
+ });
166
+ ```
@@ -0,0 +1,132 @@
1
+ # Phase 2: Add Preflight Validation Checks
2
+
3
+ Add checks that run before the service process spawns. These catch common failure modes early with clear error messages.
4
+
5
+ ## Steps
6
+
7
+ ### 1. Add workdir check
8
+
9
+ Use `checkWorkdir()` when the service requires specific directory structure:
10
+
11
+ ```typescript
12
+ import { checkWorkdir } from '../framework.js';
13
+ import fs from 'node:fs';
14
+ import path from 'node:path';
15
+
16
+ preflight: [
17
+ checkWorkdir(
18
+ (cwd) => fs.existsSync(path.join(cwd, 'package.json')),
19
+ 'Not a Node.js project',
20
+ 'Run {id}-start --workdir <project-dir>, or create a project first',
21
+ ),
22
+ ],
23
+ ```
24
+
25
+ The `test` function receives the resolved working directory. Return `true` if valid.
26
+
27
+ You can check for multiple files:
28
+ ```typescript
29
+ checkWorkdir(
30
+ (cwd) => fs.existsSync(path.join(cwd, 'powerhouse.config.ts'))
31
+ || fs.existsSync(path.join(cwd, 'powerhouse.config.json')),
32
+ 'Not a Reactor Package project',
33
+ 'Run {id}-start --workdir <project>, or create one with /init',
34
+ ),
35
+ ```
36
+
37
+ ### 2. Add binary checks
38
+
39
+ Use `checkCommand()` for required binaries:
40
+
41
+ ```typescript
42
+ import { checkCommand } from '../framework.js';
43
+
44
+ checkCommand('node', {
45
+ hint: 'Install Node.js from https://nodejs.org',
46
+ }),
47
+ checkCommand('pnpm', {
48
+ hint: 'Install pnpm: npm install -g pnpm',
49
+ }),
50
+ checkCommand('ph', {
51
+ hint: 'Install the Powerhouse CLI: npm install -g ph-cli',
52
+ }),
53
+ ```
54
+
55
+ With version validation:
56
+ ```typescript
57
+ checkCommand('node', {
58
+ versionFlag: '--version',
59
+ versionTest: (v) => parseInt(v.replace('v', '')) >= 22,
60
+ hint: 'Node.js >= 22 is required. Update from https://nodejs.org',
61
+ }),
62
+ ```
63
+
64
+ ### 3. Add port checks
65
+
66
+ Use `checkPort()` for each port the service binds to:
67
+
68
+ **Static port**:
69
+ ```typescript
70
+ import { checkPort } from '../framework.js';
71
+
72
+ checkPort(3000, 'Dev Server'),
73
+ ```
74
+
75
+ **Dynamic port from params**:
76
+ ```typescript
77
+ checkPort((ctx) => (ctx.params?.port as number) ?? 3000, 'Dev Server'),
78
+ ```
79
+
80
+ **Multiple ports**:
81
+ ```typescript
82
+ checkPort((ctx) => (ctx.params?.connectPort as number) ?? 3000, 'Connect Studio'),
83
+ checkPort((ctx) => (ctx.params?.switchboardPort as number) ?? 4001, 'Switchboard'),
84
+ ```
85
+
86
+ ### 4. Order checks correctly
87
+
88
+ Place checks in order from cheapest to most expensive:
89
+
90
+ ```typescript
91
+ preflight: [
92
+ // 1. Workdir check — pure filesystem, instant
93
+ checkWorkdir(
94
+ (cwd) => fs.existsSync(path.join(cwd, 'package.json')),
95
+ 'Not a Node.js project',
96
+ ),
97
+ // 2. Binary check — spawns a process, fast
98
+ checkCommand('pnpm', { hint: 'Install pnpm: npm install -g pnpm' }),
99
+ // 3. Port check — network I/O, slowest
100
+ checkPort((ctx) => (ctx.params?.port as number) ?? 3000, 'Dev Server'),
101
+ ],
102
+ ```
103
+
104
+ ### 5. Write custom preflight checks (if needed)
105
+
106
+ For checks not covered by the built-in factories, write a custom function:
107
+
108
+ ```typescript
109
+ preflight: [
110
+ async (ctx) => {
111
+ const configPath = path.join(ctx.cwd, '.env');
112
+ if (!fs.existsSync(configPath)) {
113
+ return {
114
+ ok: false,
115
+ message: 'Missing .env file',
116
+ hint: 'Copy .env.example to .env and fill in the values',
117
+ };
118
+ }
119
+ return { ok: true };
120
+ },
121
+ ],
122
+ ```
123
+
124
+ Custom checks receive `PreflightContext` with `cwd`, `config`, `params`, and `command`.
125
+
126
+ ### 6. Write clear error messages and hints
127
+
128
+ Every failing check should tell the user:
129
+ - **What's wrong**: `'Port 3000 is already in use'`
130
+ - **How to fix it** (hint): `'Stop the other process or use --port <number>'`
131
+
132
+ Good hints save users from having to search for solutions.
@@ -0,0 +1,82 @@
1
+ # Phase 3: Add Project Scanner (Optional)
2
+
3
+ Add project scanning when one service definition can manage multiple project directories. Skip this phase if the service always runs in a single fixed location.
4
+
5
+ ## When to Add a Scanner
6
+
7
+ - A dev server that can run in any project folder matching a pattern
8
+ - A reactor service that works with any Powerhouse project
9
+ - Any service where users need to discover eligible project directories
10
+
11
+ ## Steps
12
+
13
+ ### 1. Define the scanner
14
+
15
+ At minimum, implement `isProjectFolder`:
16
+
17
+ ```typescript
18
+ import fs from 'node:fs';
19
+ import path from 'node:path';
20
+
21
+ export const myService = defineService({
22
+ // ...
23
+ projectScanner: {
24
+ isProjectFolder: (p) => fs.existsSync(path.join(p, 'next.config.ts')),
25
+ },
26
+ });
27
+ ```
28
+
29
+ ### 2. Add getProjectName (recommended)
30
+
31
+ Extract a display name from the project directory:
32
+
33
+ ```typescript
34
+ projectScanner: {
35
+ isProjectFolder: (p) => fs.existsSync(path.join(p, 'package.json')),
36
+ getProjectName: (p) => {
37
+ const pkg = JSON.parse(fs.readFileSync(path.join(p, 'package.json'), 'utf-8'));
38
+ return pkg.name ?? path.basename(p);
39
+ },
40
+ },
41
+ ```
42
+
43
+ ### 3. Add getProjectConfig (optional)
44
+
45
+ Return project-specific configuration:
46
+
47
+ ```typescript
48
+ getProjectConfig: (p) => {
49
+ const pkg = JSON.parse(fs.readFileSync(path.join(p, 'package.json'), 'utf-8'));
50
+ return { name: pkg.name, version: pkg.version };
51
+ },
52
+ ```
53
+
54
+ ### 4. Add getDocumentLink (optional, Powerhouse projects)
55
+
56
+ Return a document reference for Powerhouse-linked projects:
57
+
58
+ ```typescript
59
+ getDocumentLink: (p) => {
60
+ const configPath = path.join(p, 'powerhouse.config.json');
61
+ if (!fs.existsSync(configPath)) return undefined;
62
+ const config = JSON.parse(fs.readFileSync(configPath, 'utf-8'));
63
+ return config.documentId
64
+ ? { documentId: config.documentId, documentType: config.documentType }
65
+ : undefined;
66
+ },
67
+ ```
68
+
69
+ ### 5. How scanning works
70
+
71
+ When `projectScanner` is defined:
72
+ - The auto-generated `{id}-ls` command becomes available
73
+ - `services.scanProjects(serviceId, rootDir)` performs a breadth-first search
74
+ - The scanner prunes subtrees at foreign project boundaries (`package.json`, `Cargo.toml`, etc.)
75
+ - Returns `ProjectScanResult[]` with `{ name, path, config?, documentId?, documentType? }`
76
+
77
+ ### 6. Using scan results
78
+
79
+ Scan results are useful for:
80
+ - Listing projects in the REPL (`/{id}-ls`)
81
+ - Auto-starting services for discovered projects
82
+ - Building project selection menus in interactive mode
@@ -0,0 +1,120 @@
1
+ # Phase 4: Wire Service Lifecycle Events
2
+
3
+ Handle service state transitions for logging, notifications, and dependent actions.
4
+
5
+ ## Steps
6
+
7
+ ### 1. Add event handlers in defineCli
8
+
9
+ Service events are wired via the `events` property in `defineCli()`:
10
+
11
+ ```typescript
12
+ import { defineCli } from '@powerhousedao/ph-clint';
13
+
14
+ const cli = defineCli({
15
+ // ...
16
+ services: [myService],
17
+ events: {
18
+ 'service:pattern-matched': (event, log) => {
19
+ log.info(`${event.name}: matched "${event.patternName}" (${event.remaining} remaining)`);
20
+ },
21
+ 'service:ready': (event, log) => {
22
+ log.info(`${event.name} is ready`);
23
+ if (event.endpoints) {
24
+ for (const [name, url] of Object.entries(event.endpoints)) {
25
+ log.info(` ${name}: ${url}`);
26
+ }
27
+ }
28
+ },
29
+ 'service:failed': (event, log) => {
30
+ log.error(`${event.name} failed: ${event.error}`);
31
+ },
32
+ 'service:restarting': (event, log) => {
33
+ log.warn(`${event.name} restarting (attempt ${event.attempt}/${event.maxRetries})`);
34
+ },
35
+ 'service:stopped': (event, log) => {
36
+ log.info(`${event.name} stopped`);
37
+ },
38
+ },
39
+ });
40
+ ```
41
+
42
+ ### 2. Understand event payloads
43
+
44
+ Each event handler receives `(event, log)`:
45
+ - `event` — typed payload with `id`, `instanceId`, `name`, and event-specific fields
46
+ - `log` — structured Logger with `debug`, `info`, `warn`, `error` methods
47
+
48
+ **`service:pattern-matched`** — fired progressively as each readiness pattern matches:
49
+ - `event.patternName` — which pattern matched
50
+ - `event.endpoints` — captured endpoints so far
51
+ - `event.endpointTypes` — endpoint types so far
52
+ - `event.remaining` — number of patterns still unmatched
53
+
54
+ **`service:ready`** — fired when ALL patterns have matched:
55
+ - `event.endpoints` — all captured endpoints
56
+ - `event.endpointTypes` — all endpoint types
57
+
58
+ **`service:failed`** — fired on readiness timeout or process crash:
59
+ - `event.error` — error message (includes log tail for diagnosis)
60
+
61
+ **`service:restarting`** — fired when auto-restart triggers:
62
+ - `event.attempt` — current attempt number
63
+ - `event.maxRetries` — maximum attempts configured
64
+
65
+ **`service:stopped`** — fired after explicit stop completes
66
+
67
+ ### 3. Common event patterns
68
+
69
+ **Log endpoints on ready**:
70
+ ```typescript
71
+ 'service:ready': (event, log) => {
72
+ const urls = Object.entries(event.endpoints ?? {})
73
+ .map(([name, url]) => ` ${name}: ${url}`)
74
+ .join('\n');
75
+ log.info(`${event.name} ready\n${urls}`);
76
+ },
77
+ ```
78
+
79
+ **Trigger dependent service startup**:
80
+ ```typescript
81
+ 'service:ready': async (event, log) => {
82
+ if (event.id === 'backend') {
83
+ log.info('Backend ready, starting frontend...');
84
+ // Frontend can now connect to the backend URL
85
+ }
86
+ },
87
+ ```
88
+
89
+ **Progress feedback during startup**:
90
+ ```typescript
91
+ 'service:pattern-matched': (event, log) => {
92
+ if (event.remaining > 0) {
93
+ log.info(`${event.name}: ${event.patternName} ready, waiting for ${event.remaining} more...`);
94
+ }
95
+ },
96
+ ```
97
+
98
+ ### 4. Register the service in defineCli
99
+
100
+ **In a codegen-managed project** (cli.ts has `@clint:begin` markers):
101
+
102
+ Do NOT manually edit `cli.ts` marker regions. Instead:
103
+
104
+ 1. Add the service to `project-spec.json`
105
+ 2. Run `clint-project-regen` — this regenerates the `@clint:begin services`, `@clint:begin imports`, and `@clint:begin events` marker regions
106
+ 3. Run `clint-project-build` to verify TypeScript compiles
107
+
108
+ **In a manually-managed project** (no markers):
109
+
110
+ ```typescript
111
+ import { myService } from './services/my-service.js';
112
+
113
+ const cli = defineCli({
114
+ services: [myService],
115
+ events: { /* ... */ },
116
+ // ...
117
+ });
118
+ ```
119
+
120
+ This auto-injects the management commands: `{id}-start`, `{id}-stop`, `{id}-restart`, `{id}-ps`, `{id}-logs`, `{id}-manage`, and `{id}-ls` (if projectScanner is defined).
@@ -0,0 +1,3 @@
1
+ ## Expected Outcome
2
+
3
+ Service is defined with readiness detection, preflight checks, and lifecycle config. It is registered in `defineCli`, auto-generates management commands (`{id}-start`, `{id}-stop`, etc.), and transitions through starting/ready/failed states correctly. Endpoints are captured and typed. Preflight checks fail fast with clear messages and hints.
@@ -31,6 +31,11 @@ export const clintProject = defineService({
31
31
  ],
32
32
  projectScanner: {
33
33
  isProjectFolder: (p) => {
34
+ // Match if a project-spec.json exists (split-layout root)
35
+ const specPath = path.join(p, '.ph', 'ph-clint-cli', 'project-spec.json');
36
+ if (fs.existsSync(specPath))
37
+ return true;
38
+ // Match if package.json has ph-clint as a dependency (flat layout or cli subfolder)
34
39
  const pkg = path.join(p, 'package.json');
35
40
  if (!fs.existsSync(pkg))
36
41
  return false;
@@ -42,6 +47,22 @@ export const clintProject = defineService({
42
47
  return false;
43
48
  }
44
49
  },
50
+ getDocumentLink: (p) => {
51
+ const specPath = path.join(p, '.ph', 'ph-clint-cli', 'project-spec.json');
52
+ if (!fs.existsSync(specPath))
53
+ return undefined;
54
+ try {
55
+ const spec = JSON.parse(fs.readFileSync(specPath, 'utf8'));
56
+ if (spec.documentId) {
57
+ return {
58
+ documentId: spec.documentId,
59
+ documentType: spec.documentType ?? 'powerhouse/ph-clint-project',
60
+ };
61
+ }
62
+ }
63
+ catch { /* noop */ }
64
+ return undefined;
65
+ },
45
66
  },
46
67
  restart: { enabled: false, maxRetries: 0, delay: 0 },
47
68
  });
@@ -0,0 +1,40 @@
1
+ import type { ExternalSkill } from '../spec/types.js';
2
+ /** On-disk manifest entry (what is currently installed). */
3
+ export interface ManifestEntry {
4
+ name: string;
5
+ githubUrl: string;
6
+ }
7
+ /** Persisted manifest — a simple JSON array. */
8
+ export type SkillsManifest = ManifestEntry[];
9
+ export interface SyncResult {
10
+ added: string[];
11
+ removed: string[];
12
+ unchanged: string[];
13
+ }
14
+ export interface SyncOptions {
15
+ /** Project root (CLI sub-tree when split layout). */
16
+ targetDir: string;
17
+ /** Desired state from the spec. */
18
+ desired: ExternalSkill[];
19
+ /** Optional log callback. */
20
+ log?: (msg: string) => void;
21
+ }
22
+ export declare function readManifest(targetDir: string): Promise<SkillsManifest>;
23
+ /**
24
+ * Compute the diff between desired (spec) and current (manifest) skills.
25
+ *
26
+ * Pure function — no I/O.
27
+ */
28
+ export declare function diffSkills(desired: ExternalSkill[], current: SkillsManifest): {
29
+ toAdd: ExternalSkill[];
30
+ toRemove: ManifestEntry[];
31
+ unchanged: string[];
32
+ };
33
+ /**
34
+ * Synchronise external skills on disk to match the spec.
35
+ *
36
+ * - Clones missing skills via `git clone --depth 1`.
37
+ * - Removes skills no longer in the spec.
38
+ * - Updates the `.skills-manifest.json`.
39
+ */
40
+ export declare function syncExternalSkills(opts: SyncOptions): Promise<SyncResult>;
@@ -0,0 +1,119 @@
1
+ /**
2
+ * External skills synchronisation.
3
+ *
4
+ * Reads the desired external skills from a `ClintProjectSpec`, diffs them
5
+ * against a `.skills-manifest.json` on disk, and runs `git clone` / `rm -rf`
6
+ * to reconcile. Each external skill ends up as a subdirectory of
7
+ * `prompts/skills-ext/<name>/` so the build-skills pipeline picks it up.
8
+ */
9
+ import { spawn } from 'node:child_process';
10
+ import fs from 'node:fs/promises';
11
+ import path from 'node:path';
12
+ const MANIFEST_FILE = '.skills-manifest.json';
13
+ const SKILLS_DIR = path.join('prompts', 'skills-ext');
14
+ function manifestPath(targetDir) {
15
+ return path.join(targetDir, SKILLS_DIR, MANIFEST_FILE);
16
+ }
17
+ export async function readManifest(targetDir) {
18
+ try {
19
+ const raw = await fs.readFile(manifestPath(targetDir), 'utf8');
20
+ return JSON.parse(raw);
21
+ }
22
+ catch {
23
+ return [];
24
+ }
25
+ }
26
+ async function writeManifest(targetDir, manifest) {
27
+ const file = manifestPath(targetDir);
28
+ await fs.mkdir(path.dirname(file), { recursive: true });
29
+ await fs.writeFile(file, JSON.stringify(manifest, null, 2) + '\n', 'utf8');
30
+ }
31
+ function execGit(args, cwd) {
32
+ return new Promise((resolve, reject) => {
33
+ const proc = spawn('git', args, { cwd, stdio: ['ignore', 'pipe', 'pipe'] });
34
+ let stderr = '';
35
+ if (proc.stderr) {
36
+ proc.stderr.on('data', (d) => {
37
+ stderr += d.toString();
38
+ });
39
+ }
40
+ proc.on('close', (code) => resolve({ code: code ?? 1, stderr }));
41
+ proc.on('error', (err) => reject(err));
42
+ });
43
+ }
44
+ /**
45
+ * Compute the diff between desired (spec) and current (manifest) skills.
46
+ *
47
+ * Pure function — no I/O.
48
+ */
49
+ export function diffSkills(desired, current) {
50
+ const desiredMap = new Map(desired.map((s) => [s.name, s]));
51
+ const currentMap = new Map(current.map((s) => [s.name, s]));
52
+ const toAdd = [];
53
+ const toRemove = [];
54
+ const unchanged = [];
55
+ for (const [name, skill] of desiredMap) {
56
+ const existing = currentMap.get(name);
57
+ if (!existing) {
58
+ toAdd.push(skill);
59
+ }
60
+ else if (existing.githubUrl !== skill.githubUrl) {
61
+ // URL changed — remove old, add new.
62
+ toRemove.push(existing);
63
+ toAdd.push(skill);
64
+ }
65
+ else {
66
+ unchanged.push(name);
67
+ }
68
+ }
69
+ for (const [name, entry] of currentMap) {
70
+ if (!desiredMap.has(name)) {
71
+ toRemove.push(entry);
72
+ }
73
+ }
74
+ return { toAdd, toRemove, unchanged };
75
+ }
76
+ /**
77
+ * Synchronise external skills on disk to match the spec.
78
+ *
79
+ * - Clones missing skills via `git clone --depth 1`.
80
+ * - Removes skills no longer in the spec.
81
+ * - Updates the `.skills-manifest.json`.
82
+ */
83
+ export async function syncExternalSkills(opts) {
84
+ const { targetDir, desired, log } = opts;
85
+ const skillsDir = path.join(targetDir, SKILLS_DIR);
86
+ await fs.mkdir(skillsDir, { recursive: true });
87
+ const current = await readManifest(targetDir);
88
+ const { toAdd, toRemove, unchanged } = diffSkills(desired, current);
89
+ const result = { added: [], removed: [], unchanged };
90
+ // Remove skills.
91
+ for (const entry of toRemove) {
92
+ const dir = path.join(skillsDir, entry.name);
93
+ log?.(`Removing skill "${entry.name}"…`);
94
+ await fs.rm(dir, { recursive: true, force: true });
95
+ result.removed.push(entry.name);
96
+ }
97
+ // Add skills.
98
+ for (const skill of toAdd) {
99
+ const dir = path.join(skillsDir, skill.name);
100
+ log?.(`Cloning skill "${skill.name}" from ${skill.githubUrl}…`);
101
+ // Ensure the target doesn't exist (re-add after URL change).
102
+ await fs.rm(dir, { recursive: true, force: true });
103
+ const { code, stderr } = await execGit(['clone', '--depth', '1', skill.githubUrl, skill.name], skillsDir);
104
+ if (code !== 0) {
105
+ log?.(`Warning: git clone failed for "${skill.name}": ${stderr.trim()}`);
106
+ continue;
107
+ }
108
+ // Remove .git dir — we don't want a nested repo.
109
+ await fs.rm(path.join(dir, '.git'), { recursive: true, force: true });
110
+ result.added.push(skill.name);
111
+ }
112
+ // Write updated manifest.
113
+ const newManifest = desired.map((s) => ({
114
+ name: s.name,
115
+ githubUrl: s.githubUrl,
116
+ }));
117
+ await writeManifest(targetDir, newManifest);
118
+ return result;
119
+ }