@topce/pizx 0.1.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,19 +1,92 @@
1
1
  # pizx
2
2
 
3
- **zx fork with native Pi AI integration** — 15 template tags for shell scripting, AI text generation, coding agents, agentic patterns, communication, and orchestration topologies.
3
+ [![GitHub Sponsors](https://img.shields.io/github/sponsors/topce?style=social&logo=github)](https://github.com/sponsors/topce)
4
+
5
+ > **zx fork with native Pi AI integration** — 15 template tags for shell scripting, AI text generation, coding agents, agentic patterns, communication, and orchestration topologies.
6
+
7
+ ## Quick Start
8
+
9
+ ```bash
10
+ npm install @topce/pizx
11
+ pi auth login # one-time: configure Pi AI credentials
12
+ ```
13
+
14
+ Write a script (`hello.mjs`):
15
+
16
+ ```js
17
+ #!/usr/bin/env pizx
18
+
19
+ // Simple AI query
20
+ const answer = await π`what is the capital of France?`
21
+ echo(answer)
22
+
23
+ // Agent patterns
24
+ const files = await $`ls src/`
25
+ const summary = await π`summarize these files in one sentence: ${files}`
26
+ console.log(summary)
27
+ ```
28
+
29
+ Run it:
30
+
31
+ ```bash
32
+ chmod +x hello.mjs
33
+ ./hello.mjs
34
+
35
+ # Or:
36
+ pizx hello.mjs
37
+ ```
38
+
39
+ **New to pizx?** Start with the [Onboarding Guide](docs/onboarding.md).
40
+
41
+ ## Install
42
+
43
+ ```bash
44
+ npm install @topce/pizx
45
+ ```
46
+
47
+ **Prerequisites:**
48
+ - Node.js >= 22.19.0
49
+ - [Pi AI](https://github.com/earendil-works/pi-ai) installed and configured (`pi auth login`)
50
+ - Shell commands from [zx](https://github.com/google/zx) (`$`, `cd`, `echo`, `fetch`, etc.)
51
+
52
+ ## Writing Scripts
53
+
54
+ ### Shebang
4
55
 
5
56
  ```js
6
57
  #!/usr/bin/env pizx
7
- const files = (await $`ls src/`).stdout.trim()
8
- const summary = await π`summarize these files: ${files}`
9
- await Π`fix TypeScript errors flagged in: ${summary}`
10
- await Ρ`iteratively improve error handling across src/`
11
- const plan = await Ω`design a comprehensive testing strategy for this project`
58
+
59
+ const name = await question('What is your name? ')
60
+ const intro = await π`write a friendly greeting for ${name}`
61
+ echo(intro)
62
+ ```
63
+
64
+ ### Programmatic Import
65
+
66
+ ```js
67
+ import { $, π, Π, Ρ, Φ, Σ } from '@topce/pizx'
68
+
69
+ const output = await $`ls src/ | grep '.ts'`
70
+ console.log(output.stdout)
71
+
72
+ const review = await π`review this code for issues:\n${output.stdout}`
73
+ console.log(review.text)
74
+
75
+ // Use the coding agent to fix issues
76
+ await Π`fix the TypeScript errors in src/`
77
+ ```
78
+
79
+ ### CLI Quick Queries
80
+
81
+ ```bash
82
+ pizx -p "explain async/await in JavaScript"
83
+ pizx -p --model deepseek/deepseek-chat "summarize this code: @file.ts"
84
+ pizx --version
12
85
  ```
13
86
 
14
87
  ## Tags Reference
15
88
 
16
- Each tag has its own detailed documentation in [`docs/`](docs/):
89
+ Each tag has detailed documentation in [`docs/`](docs/):
17
90
 
18
91
  ### Core
19
92
 
@@ -53,7 +126,21 @@ Each tag has its own detailed documentation in [`docs/`](docs/):
53
126
  | `Χ` | Chi | Analyze traces → extract patterns | [docs/chi.md](docs/chi.md) |
54
127
  | `Τ` | Tau | Define schema → write → refine → consolidate | [docs/tau.md](docs/tau.md) |
55
128
 
56
- ## Per-Phase Model Selection
129
+ ## Architecture
130
+
131
+ See [docs/decisions/](docs/decisions/) for Architecture Decision Records covering the key design choices:
132
+
133
+ - [ADR-001: Template-Tag DSL](docs/decisions/ADR-001-template-tag-dsl.md) — Why patterns are template tags with curried option chaining
134
+ - [ADR-002: Shared Tag Factory](docs/decisions/ADR-002-shared-tag-factory.md) — How `createPatternTag` eliminates 450 lines of boilerplate
135
+ - [ADR-003: Quality Validation](docs/decisions/ADR-003-quality-validation.md) — The `qualityCheck` design and `runQualityReview` helper
136
+ - [ADR-004: Phase Logging](docs/decisions/ADR-004-phase-logging.md) — Structured audit trails via `phaseLog` on every output
137
+ - [ADR-005: Pattern Composition](docs/decisions/ADR-005-pattern-composition.md) — How `TaskDescriptor` enables nested patterns
138
+ - [ADR-006: Confirm Gates](docs/decisions/ADR-006-confirm-gates.md) — Human-in-the-loop approval via `confirm` option
139
+ - [ADR-007: System Propagation](docs/decisions/ADR-007-system-propagation.md) — How `mergeSystem` propagates user prompts
140
+
141
+ ## Advanced Features
142
+
143
+ ### Per-Phase Model Selection
57
144
 
58
145
  All patterns support `plannerModel` and `workerModel` for routing high-level reasoning vs execution to different models:
59
146
 
@@ -66,40 +153,194 @@ await Ω({
66
153
 
67
154
  Without per-phase models, patterns fall back to `model` → Pi default.
68
155
 
69
- ## Option Chaining
156
+ ### System Prompt Propagation
70
157
 
71
- All patterns support option chaining and `.quiet` mode:
158
+ All patterns respect the `system` option. When you provide a custom system prompt, it is prepended to the pattern's default system prompt — your context is never silently discarded:
72
159
 
73
160
  ```js
161
+ await Ω({ system: 'You are a senior security architect.' })`design an auth system`
162
+ // → "You are a senior security architect.\n\n[PLANNER_SYSTEM]"
163
+ ```
164
+
165
+ ### Quality Validation
166
+
167
+ All 15 patterns support an optional `qualityCheck` flag. When enabled, the pattern runs a post-execution LLM review that scores the final output (0.0–1.0), provides an assessment, and recommends improvements:
168
+
169
+ ```js
170
+ const result = await Ω({ qualityCheck: true })`design the system architecture`
171
+
172
+ if (result.qualityReview) {
173
+ console.log(`Quality score: ${result.qualityReview.score}`) // 0.0 – 1.0
174
+ console.log(result.qualityReview.assessment) // 1-2 sentence assessment
175
+ console.log(result.qualityReview.recommendation) // improvement suggestion
176
+ }
177
+ ```
178
+
179
+ ### Human-in-the-Loop (Confirm Gates)
180
+
181
+ Set `confirm: true` to pause before the main execution phase and ask for approval. The pattern displays a summary of what it's about to do and waits for `[Y/n]` on stdin:
182
+
183
+ ```js
184
+ await Ω({ confirm: true })`design the system`
185
+ // → "── Confirm ──"
186
+ // → "Execute 3 sub-task(s) as planned?"
187
+ // → " 1. Analyze requirements"
188
+ // → " 2. Design architecture"
189
+ // → " 3. Document decisions"
190
+ // → "Proceed? [Y/n] "
191
+ ```
192
+
193
+ Supported by: `Ω`, `Σ`, `Φ`, `Λ` (more patterns coming).
194
+
195
+ ### Option Chaining & Quiet Mode
196
+
197
+ All tags support option chaining and `.quiet` mode to suppress output:
198
+
199
+ ```js
200
+ await π({ model: 'anthropic/claude-sonnet-4-5' })`explain this algorithm`
201
+ await Π.quiet`fix the lint issues in src/`
74
202
  await Φ({ concurrency: 5 })`review all .ts files`
75
203
  await Σ.quiet`analyze security across the codebase`
76
204
  await Θ({ agents: 4, turns: 3 })`debate the architecture`
77
205
  await Γ({ graph: { nodes: [...], edges: [...] } })`execute workflow`
78
206
  ```
79
207
 
80
- ## Install
208
+ ### Timeout & Retry
81
209
 
82
- ```bash
83
- npm install @topce/pizx
210
+ All tags accept `timeoutMs` and `maxRetries` to control LLM call resilience. When unset, the provider SDK defaults apply (typically 10 min timeout, 2 retries).
211
+
212
+ ```js
213
+ // Per-pattern
214
+ await Φ({ timeoutMs: 30000, maxRetries: 2 })`review all .ts files`
215
+
216
+ // Per-call on π
217
+ await π({ timeoutMs: 15000 })`summarize this document`
218
+
219
+ // Global defaults
220
+ configurePi({ timeoutMs: 60000, maxRetries: 3 })
84
221
  ```
85
222
 
86
- Requires Pi AI configured: `pi auth login`
223
+ ### Token, Cost & Phase Tracking
224
+
225
+ Every pattern output and π call includes an execution trace with token usage, cost, and a structured phase log. All collected automatically — no extra flags needed.
87
226
 
88
- ## CLI
227
+ ```js
228
+ const result = await Ω`design a notification system`
229
+
230
+ // Per-call breakdown
231
+ for (const t of result.trace) {
232
+ console.log(`Call ${t.call}: ${t.modelId} — ${t.totalTokens} tokens, $${t.cost.toFixed(6)}`)
233
+ }
234
+
235
+ // Aggregates (on both PatternOutput and PiOutput)
236
+ console.log(`Total: ${result.totalTokens} tokens`)
237
+ console.log(`Cost: $${result.totalCost.toFixed(4)}`)
238
+ console.log(`Calls: ${result.callCount}`)
239
+
240
+ // Structured phase log — what happened during execution
241
+ for (const phase of result.phaseLog) {
242
+ console.log(`${phase.phase}: ${phase.durationMs}ms — ${phase.description}`)
243
+ }
244
+ // → "plan: 1234ms — Generated plan with 3 workers"
245
+ // → "dispatch: 5678ms — Executed 3 worker(s), 3 succeeded"
246
+ // → "synthesize: 901ms — Synthesized worker results"
247
+
248
+ // Works with π too
249
+ const answer = await π`explain quantum computing`
250
+ console.log(`Input: ${answer.inputTokens}, Output: ${answer.outputTokens}`)
251
+ console.log(`Cost: $${answer.totalCost.toFixed(6)}`)
252
+ ```
253
+
254
+ Each `CallTrace` entry includes: call index, model id, prompt/output previews, input/output/cache tokens, cost (USD), and duration.
255
+
256
+ ### Pattern Composition (Nesting)
257
+
258
+ Fleet and Pipeline accept `TaskDescriptor` — either a plain string (for a standard LLM call) or a function that invokes another pattern as a sub-task. See [docs/advanced-features.md](docs/advanced-features.md#pattern-composition-taskdescriptor) for details.
259
+
260
+ **Fleet with mixed tasks:**
261
+
262
+ ```js
263
+ await Φ({
264
+ tasks: [
265
+ 'analyze the frontend', // string: standard LLM call
266
+ () => Σ\`analyze the backend\`, // function: compose a Subagents pattern
267
+ () => Ψ\`review the API design\`, // function: compose a Critique pattern
268
+ ],
269
+ })`review everything`
270
+ ```
271
+
272
+ **Pipeline with composed stages:**
273
+
274
+ ```js
275
+ await Λ({
276
+ stages: [
277
+ 'generate product description', // string: standard LLM call
278
+ (prev) => Ψ\`critique this: ${prev}\`, // function: receives previous output
279
+ ],
280
+ })`generate → improve`
281
+ ```
282
+
283
+ ### Global Configuration
284
+
285
+ ```js
286
+ import { configurePi, configureAgent } from '@topce/pizx'
287
+
288
+ configurePi({ model: 'anthropic/claude-sonnet-4-5', maxTokens: 8000, timeoutMs: 60000 })
289
+ configureAgent({ maxTurns: 5, excludeTools: ['write'] })
290
+ ```
291
+
292
+ ## CLI Reference
89
293
 
90
294
  ```bash
91
- pizx script.mjs # Run a pizx script
92
- pizx -p "prompt" # Quick pi-ai query
93
- pizx --version # Print version
295
+ pizx [options] <script> # Run a pizx script
296
+ pizx -p <prompt> # Quick pi-ai query
297
+ pizx --version # Print version
298
+ pizx --help # Print help
94
299
  ```
95
300
 
301
+ **Options:**
302
+ - `-p, --prompt <text>` — Run a quick pi-ai query (no script needed)
303
+ - `-m, --model <id>` — Specify AI model to use
304
+ - `--quiet` — Suppress output except errors
305
+ - `--shell <path>` — Shell to use (default: auto-detect)
306
+
96
307
  ## Commands
97
308
 
98
309
  ```bash
99
- npm run build # Build (JS + DTS)
100
- npm test # 95 unit tests
310
+ npm run build # Build (JS + DTS)
311
+ npm run check # Lint and format with Biome
312
+ npm test # 223 unit tests (no network)
313
+ npm run test:integration # Integration tests (requires Pi credentials)
314
+ npm run test:quality # Run qualityCheck example
315
+ npm run test:confirm # Run confirm gate example
316
+ npm run test:composition-fleet # Run pattern composition in Fleet example
317
+ npm run test:composition-pipeline # Run pattern composition in Pipeline example
318
+ npm run test:new-features # Run all 4 feature examples
319
+ npm run example:hello # Run hello example
320
+ npm run example:all # Run all pattern examples
101
321
  ```
102
322
 
323
+ ## Examples
324
+
325
+ See [`examples/`](examples/) for runnable examples of every pattern and feature:
326
+
327
+ ### Pattern Examples
328
+
329
+ - [`hello-pizx.mjs`](examples/hello-pizx.mjs) — Basic script with shell + AI
330
+ - [`basic-pi.mjs`](examples/basic-pi.mjs) — π text generation
331
+ - [`basic-capital-pi.mjs`](examples/basic-capital-pi.mjs) — Π coding agent
332
+ - [`pattern-ralph.mjs`](examples/pattern-ralph.mjs) — Ralph Loop
333
+ - [`pattern-fleet.mjs`](examples/pattern-fleet.mjs) — Fleet parallel execution
334
+ - [`pattern-debate.mjs`](examples/pattern-debate.mjs) — Multi-perspective debate
335
+ - ... and more for every pattern
336
+
337
+ ### New Feature Demos
338
+
339
+ - [`test-quality.mjs`](examples/test-quality.mjs) — `qualityCheck` + `system` + `phaseLog`
340
+ - [`test-confirm.mjs`](examples/test-confirm.mjs) — Human-in-the-loop approval gate
341
+ - [`test-composition-fleet.mjs`](examples/test-composition-fleet.mjs) — Pattern composition in Fleet
342
+ - [`test-composition-pipeline.mjs`](examples/test-composition-pipeline.mjs) — Pattern composition in Pipeline
343
+
103
344
  ## License
104
345
 
105
346
  MIT