@hoplogic/spec 0.1.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 ADDED
@@ -0,0 +1,93 @@
1
+ # @hoplogic/spec
2
+
3
+ HopSpec / PlanSpec parser, validator, and tree operations for TypeScript.
4
+
5
+ Zero external dependencies. Pure compute. Works in Node.js (>=18), Bun, and browsers.
6
+
7
+ ## Install
8
+
9
+ ```bash
10
+ npm install @hoplogic/spec
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ### HopSpec — Parse and validate LLM agent specs
16
+
17
+ ```typescript
18
+ import { parseFullSpec, validateSpec } from "@hoplogic/spec"
19
+
20
+ const markdown = `# HopSpec: MyTask
21
+ ## 任务概述
22
+ ...
23
+ ## 执行流程
24
+ ### 步骤 1: analyze [LLM]
25
+ ...`
26
+
27
+ const { sections, steps } = parseFullSpec(markdown)
28
+ const errors = validateSpec(steps)
29
+ ```
30
+
31
+ ### PlanSpec — Structured task plans
32
+
33
+ ```typescript
34
+ import { parsePlan, serializePlan, validatePlan, findStep, countSteps, countByStatus } from "@hoplogic/spec"
35
+
36
+ const plan = parsePlan(`
37
+ # Plan: Implement Feature X
38
+ Goal: Add feature X
39
+ Constraints:
40
+ - No breaking changes
41
+ ## Steps
42
+ 1. [reason] Analyze codebase -> change_points
43
+ 2. [act] Implement changes -> code
44
+ 3. [act] Write tests -> tests
45
+ `)
46
+
47
+ // Validate structure
48
+ const errors = validatePlan(plan) // []
49
+
50
+ // Navigate steps
51
+ const step = findStep(plan, "2")
52
+ step.status = "done"
53
+ step.result = "Changes implemented in src/feature.ts"
54
+
55
+ // Serialize back to markdown
56
+ const updated = serializePlan(plan)
57
+
58
+ // Progress tracking
59
+ const total = countSteps(plan) // 3
60
+ const byStatus = countByStatus(plan) // { done: 1, pending: 2, active: 0, blocked: 0 }
61
+ ```
62
+
63
+ ### Format Repair — Fix malformed LLM JSON output
64
+
65
+ ```typescript
66
+ import { parseResult, coerceToDict, repairValue, detectSerializationResidue } from "@hoplogic/spec"
67
+
68
+ const raw = '{"answer": "hello", "items": "[1, 2, 3]"}'
69
+ const parsed = parseResult(raw)
70
+ const residue = detectSerializationResidue(parsed) // detects stringified list
71
+ ```
72
+
73
+ ## API
74
+
75
+ | Export | Description |
76
+ |--------|-------------|
77
+ | `parseSpec(markdown)` | Parse HopSpec steps from markdown |
78
+ | `parseFullSpec(markdown)` | Parse sections + steps |
79
+ | `validateSpec(steps)` | Validate StepInfo tree |
80
+ | `parsePlan(markdown)` | Parse PlanSpec from markdown |
81
+ | `serializePlan(plan)` | Serialize PlanSpec back to markdown |
82
+ | `validatePlan(plan)` | Validate PlanSpec structure |
83
+ | `findStep(plan, stepId)` | Find step by ID in tree |
84
+ | `countSteps(plan)` | Count total leaf steps |
85
+ | `countByStatus(plan)` | Count steps by status |
86
+ | `parseResult(raw)` | Parse LLM JSON output |
87
+ | `coerceToDict(value)` | Coerce value to dict |
88
+ | `repairValue(value, key)` | Repair malformed values |
89
+ | `detectSerializationResidue(obj)` | Detect stringified collections |
90
+
91
+ ## License
92
+
93
+ MIT