pi-ate-workflow 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.en.md +102 -0
- package/README.md +104 -0
- package/agent-loader.ts +88 -0
- package/index.ts +18 -0
- package/output.ts +89 -0
- package/package.json +50 -0
- package/research-analysis.ts +64 -0
- package/research-catalog.ts +33 -0
- package/research-scan.ts +65 -0
- package/research-state.ts +46 -0
- package/research-types.ts +53 -0
- package/research-utils.ts +107 -0
- package/research.ts +52 -0
- package/state.ts +121 -0
- package/subagent-spawn-once.ts +164 -0
- package/subagent-spawn-visible.ts +194 -0
- package/subagent-utils.ts +38 -0
- package/subagent.ts +93 -0
- package/tests/agent-loader.test.ts +183 -0
- package/tests/agent-schema-extra.test.ts +107 -0
- package/tests/agent-schema.test.ts +116 -0
- package/tests/output.test.ts +107 -0
- package/tests/research-analysis.test.ts +155 -0
- package/tests/research-extra.test.ts +175 -0
- package/tests/research.test.ts +418 -0
- package/tests/smoke.subagent.test.ts +127 -0
- package/tests/state-manager.test.ts +245 -0
- package/tests/state.test.ts +298 -0
- package/tests/subagent-api.test.ts +158 -0
- package/tests/subagent-spawn-once.test.ts +154 -0
- package/tests/subagent-spawn-visible.test.ts +139 -0
- package/tests/subagent-utils.test.ts +124 -0
- package/tests/test-helpers.ts +40 -0
- package/tests/ui-updater.test.ts +129 -0
- package/tests/utils-find-session.test.ts +114 -0
- package/tests/utils-status-summary.test.ts +158 -0
- package/tests/utils.test.ts +48 -0
- package/tests/widget.test.ts +114 -0
- package/tests/workflow-execution.test.ts +165 -0
- package/tests/workflow-registration.test.ts +155 -0
- package/tests/workflow.test.ts +207 -0
- package/types.ts +24 -0
- package/utils.ts +74 -0
- package/vitest.config.ts +21 -0
- package/widget.ts +104 -0
- package/workflow.ts +113 -0
package/README.en.md
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
[中文文档](README.md) | English
|
|
2
|
+
|
|
3
|
+
# pi-workflow
|
|
4
|
+
|
|
5
|
+
Workflow orchestration extension for [pi](https://github.com/earendil-works/pi-coding-agent) — subagent spawning, research workflow, and output capture.
|
|
6
|
+
|
|
7
|
+
## Why You Need It
|
|
8
|
+
|
|
9
|
+
Complex AI tasks often need structured multi-step workflows — search → evaluate → synthesize → conclude. A single agent context can't handle this well: it loses track, mixes concerns, or runs out of tokens.
|
|
10
|
+
|
|
11
|
+
pi-workflow solves this by providing **subagent spawning** and **structured research pipelines** where each step runs in a focused, isolated context.
|
|
12
|
+
|
|
13
|
+
**Use it when**: Your task is too complex for a single agent pass — research, multi-perspective analysis, or parallel investigation.
|
|
14
|
+
|
|
15
|
+
- **Subagent spawning** — Launch child pi processes with isolated context for independent tasks
|
|
16
|
+
- **Research pipeline** — Structured multi-round research with search, evaluation, synthesis, and conclusion phases
|
|
17
|
+
- **Output capture** — Save and retrieve subagent outputs for downstream processing
|
|
18
|
+
- **State management** — Track workflow state across steps with automatic persistence
|
|
19
|
+
|
|
20
|
+
## Installation
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
pi install git:github.com/catlain/pi-workflow
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Tool: `workflow`
|
|
27
|
+
|
|
28
|
+
The extension registers a `workflow` tool with multiple actions for the research pipeline:
|
|
29
|
+
|
|
30
|
+
| Action | Description |
|
|
31
|
+
|--------|-------------|
|
|
32
|
+
| `start` | Start a new research workflow with a topic |
|
|
33
|
+
| `search` | Execute a search round |
|
|
34
|
+
| `evaluate` | Evaluate search results for relevance |
|
|
35
|
+
| `synthesize` | Synthesize findings from multiple rounds |
|
|
36
|
+
| `conclude` | Generate final conclusions |
|
|
37
|
+
|
|
38
|
+
## How Subagents Work
|
|
39
|
+
|
|
40
|
+
```
|
|
41
|
+
Main Agent
|
|
42
|
+
└── workflow tool → runSubagent()
|
|
43
|
+
└── Child pi process (isolated context)
|
|
44
|
+
├── Own tools and system prompt
|
|
45
|
+
├── Own context window
|
|
46
|
+
└── Result → saved to file
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Key features:
|
|
50
|
+
- **Isolation** — Each subagent runs in its own context window, doesn't pollute the main conversation
|
|
51
|
+
- **Model selection** — Subagents can use different (cheaper) models
|
|
52
|
+
- **Output persistence** — Results saved to disk for later retrieval
|
|
53
|
+
- **Status tracking** — Main agent can check subagent success/failure
|
|
54
|
+
|
|
55
|
+
## Use Cases
|
|
56
|
+
|
|
57
|
+
| Scenario | How It Works |
|
|
58
|
+
|----------|-------------|
|
|
59
|
+
| **Literature research** | Search → Evaluate → Synthesize → Conclude pipeline |
|
|
60
|
+
| **Multi-perspective analysis** | Spawn subagents with different viewpoints |
|
|
61
|
+
| **Parallel investigation** | Run independent research threads simultaneously |
|
|
62
|
+
| **Factor research** | Search indicators → Evaluate quality → Combine signals |
|
|
63
|
+
|
|
64
|
+
## Best Practices
|
|
65
|
+
|
|
66
|
+
### ✅ Recommended
|
|
67
|
+
- Use subagents for tasks > 30 minutes — they keep context fresh
|
|
68
|
+
- Choose cheaper models for subagents when possible (e.g., Haiku for search rounds)
|
|
69
|
+
- Always check subagent output before synthesizing — catch failures early
|
|
70
|
+
- Save workflow state between steps for resumability
|
|
71
|
+
|
|
72
|
+
### ❌ Not Recommended
|
|
73
|
+
- Don't use subagents for simple tasks (< 5 minutes) — overhead isn't worth it
|
|
74
|
+
- Don't spawn too many subagents simultaneously — resource contention
|
|
75
|
+
- Don't pass huge contexts to subagents — that defeats the isolation purpose
|
|
76
|
+
|
|
77
|
+
## Limitations
|
|
78
|
+
|
|
79
|
+
| Limitation | Detail |
|
|
80
|
+
|------------|--------|
|
|
81
|
+
| Subagent overhead | Each spawn takes time to initialize |
|
|
82
|
+
| No streaming | Subagent output is available only after completion |
|
|
83
|
+
| File-based output | Results are saved to disk, not streamed back |
|
|
84
|
+
| Sequential pipeline | Research pipeline steps run one at a time |
|
|
85
|
+
|
|
86
|
+
## Architecture
|
|
87
|
+
|
|
88
|
+
```
|
|
89
|
+
pi-workflow/
|
|
90
|
+
├── index.ts # Entry: register workflow tool
|
|
91
|
+
├── subagent.ts # Subagent spawning + output capture
|
|
92
|
+
├── pipeline.ts # Research pipeline (search → evaluate → synthesize → conclude)
|
|
93
|
+
├── state.ts # Workflow state management + persistence
|
|
94
|
+
└── package.json
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
**Dependencies**:
|
|
98
|
+
- `@earendil-works/pi-coding-agent` — ExtensionAPI (peer)
|
|
99
|
+
|
|
100
|
+
## License
|
|
101
|
+
|
|
102
|
+
MIT
|
package/README.md
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
> 📖 **[pi-atelier 实战指南](https://catlain.github.io/pi-atelier/)** — 从零教会你使用 pi-atelier 扩展生态,包含完整示例和最佳实践。
|
|
2
|
+
|
|
3
|
+
[English](README.en.md) | 程序中文文档
|
|
4
|
+
|
|
5
|
+
# pi-workflow
|
|
6
|
+
|
|
7
|
+
Workflow orchestration extension for [pi](https://github.com/earendil-works/pi-coding-agent) — subagent spawning, research workflow, and output capture.
|
|
8
|
+
|
|
9
|
+
## Why You Need It
|
|
10
|
+
|
|
11
|
+
Complex AI tasks often need structured multi-step workflows — search → evaluate → synthesize → conclude. A single agent context can't handle this well: it loses track, mixes concerns, or runs out of tokens.
|
|
12
|
+
|
|
13
|
+
pi-workflow solves this by providing **subagent spawning** and **structured research pipelines** where each step runs in a focused, isolated context.
|
|
14
|
+
|
|
15
|
+
**Use it when**: Your task is too complex for a single agent pass — research, multi-perspective analysis, or parallel investigation.
|
|
16
|
+
|
|
17
|
+
- **Subagent spawning** — Launch child pi processes with isolated context for independent tasks
|
|
18
|
+
- **Research pipeline** — Structured multi-round research with search, evaluation, synthesis, and conclusion phases
|
|
19
|
+
- **Output capture** — Save and retrieve subagent outputs for downstream processing
|
|
20
|
+
- **State management** — Track workflow state across steps with automatic persistence
|
|
21
|
+
|
|
22
|
+
## Installation
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
pi install git:github.com/catlain/pi-workflow
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Tool: `workflow`
|
|
29
|
+
|
|
30
|
+
The extension registers a `workflow` tool with multiple actions for the research pipeline:
|
|
31
|
+
|
|
32
|
+
| Action | Description |
|
|
33
|
+
|--------|-------------|
|
|
34
|
+
| `start` | Start a new research workflow with a topic |
|
|
35
|
+
| `search` | Execute a search round |
|
|
36
|
+
| `evaluate` | Evaluate search results for relevance |
|
|
37
|
+
| `synthesize` | Synthesize findings from multiple rounds |
|
|
38
|
+
| `conclude` | Generate final conclusions |
|
|
39
|
+
|
|
40
|
+
## How Subagents Work
|
|
41
|
+
|
|
42
|
+
```
|
|
43
|
+
Main Agent
|
|
44
|
+
└── workflow tool → runSubagent()
|
|
45
|
+
└── Child pi process (isolated context)
|
|
46
|
+
├── Own tools and system prompt
|
|
47
|
+
├── Own context window
|
|
48
|
+
└── Result → saved to file
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Key features:
|
|
52
|
+
- **Isolation** — Each subagent runs in its own context window, doesn't pollute the main conversation
|
|
53
|
+
- **Model selection** — Subagents can use different (cheaper) models
|
|
54
|
+
- **Output persistence** — Results saved to disk for later retrieval
|
|
55
|
+
- **Status tracking** — Main agent can check subagent success/failure
|
|
56
|
+
|
|
57
|
+
## Use Cases
|
|
58
|
+
|
|
59
|
+
| Scenario | How It Works |
|
|
60
|
+
|----------|-------------|
|
|
61
|
+
| **Literature research** | Search → Evaluate → Synthesize → Conclude pipeline |
|
|
62
|
+
| **Multi-perspective analysis** | Spawn subagents with different viewpoints |
|
|
63
|
+
| **Parallel investigation** | Run independent research threads simultaneously |
|
|
64
|
+
| **Factor research** | Search indicators → Evaluate quality → Combine signals |
|
|
65
|
+
|
|
66
|
+
## Best Practices
|
|
67
|
+
|
|
68
|
+
### ✅ Recommended
|
|
69
|
+
- Use subagents for tasks > 30 minutes — they keep context fresh
|
|
70
|
+
- Choose cheaper models for subagents when possible (e.g., Haiku for search rounds)
|
|
71
|
+
- Always check subagent output before synthesizing — catch failures early
|
|
72
|
+
- Save workflow state between steps for resumability
|
|
73
|
+
|
|
74
|
+
### ❌ Not Recommended
|
|
75
|
+
- Don't use subagents for simple tasks (< 5 minutes) — overhead isn't worth it
|
|
76
|
+
- Don't spawn too many subagents simultaneously — resource contention
|
|
77
|
+
- Don't pass huge contexts to subagents — that defeats the isolation purpose
|
|
78
|
+
|
|
79
|
+
## Limitations
|
|
80
|
+
|
|
81
|
+
| Limitation | Detail |
|
|
82
|
+
|------------|--------|
|
|
83
|
+
| Subagent overhead | Each spawn takes time to initialize |
|
|
84
|
+
| No streaming | Subagent output is available only after completion |
|
|
85
|
+
| File-based output | Results are saved to disk, not streamed back |
|
|
86
|
+
| Sequential pipeline | Research pipeline steps run one at a time |
|
|
87
|
+
|
|
88
|
+
## Architecture
|
|
89
|
+
|
|
90
|
+
```
|
|
91
|
+
pi-workflow/
|
|
92
|
+
├── index.ts # Entry: register workflow tool
|
|
93
|
+
├── subagent.ts # Subagent spawning + output capture
|
|
94
|
+
├── pipeline.ts # Research pipeline (search → evaluate → synthesize → conclude)
|
|
95
|
+
├── state.ts # Workflow state management + persistence
|
|
96
|
+
└── package.json
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
**Dependencies**:
|
|
100
|
+
- `@earendil-works/pi-coding-agent` — ExtensionAPI (peer)
|
|
101
|
+
|
|
102
|
+
## License
|
|
103
|
+
|
|
104
|
+
MIT
|
package/agent-loader.ts
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* workflow: Agent 定义加载器
|
|
3
|
+
*
|
|
4
|
+
* 从 ~/.pi/agent/agents/*.md 或 ~/.agents/agents/*.md 加载子代理定义。
|
|
5
|
+
* 带 mtime 缓存,避免重复解析。
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import * as fs from "node:fs";
|
|
9
|
+
import * as path from "node:path";
|
|
10
|
+
import * as os from "node:os";
|
|
11
|
+
|
|
12
|
+
export interface AgentDef {
|
|
13
|
+
name: string;
|
|
14
|
+
tools: string[];
|
|
15
|
+
model?: string;
|
|
16
|
+
thinking?: string;
|
|
17
|
+
systemPrompt: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
interface CacheEntry {
|
|
21
|
+
mtime: number;
|
|
22
|
+
def: AgentDef;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const agentCache = new Map<string, CacheEntry>();
|
|
26
|
+
|
|
27
|
+
export function loadAgentDef(agentName: string): AgentDef | null {
|
|
28
|
+
const dirs = [
|
|
29
|
+
path.join(os.homedir(), ".pi", "agent", "agents"),
|
|
30
|
+
path.join(os.homedir(), ".agents", "agents"),
|
|
31
|
+
];
|
|
32
|
+
|
|
33
|
+
for (const searchDir of dirs) {
|
|
34
|
+
const filePath = path.join(searchDir, `${agentName}.md`);
|
|
35
|
+
if (!fs.existsSync(filePath)) continue;
|
|
36
|
+
|
|
37
|
+
const stat = fs.statSync(filePath);
|
|
38
|
+
const cacheKey = filePath;
|
|
39
|
+
|
|
40
|
+
const cached = agentCache.get(cacheKey);
|
|
41
|
+
if (cached && cached.mtime === stat.mtimeMs) {
|
|
42
|
+
return cached.def;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const content = fs.readFileSync(filePath, "utf-8");
|
|
46
|
+
const def = parseAgentFile(content, agentName);
|
|
47
|
+
if (def) {
|
|
48
|
+
agentCache.set(cacheKey, { mtime: stat.mtimeMs, def });
|
|
49
|
+
return def;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function parseAgentFile(content: string, agentName: string): AgentDef {
|
|
57
|
+
const fmMatch = content.match(/^---\s*\n([\s\S]*?)\n---\s*\n/);
|
|
58
|
+
if (!fmMatch) {
|
|
59
|
+
return {
|
|
60
|
+
name: agentName,
|
|
61
|
+
tools: ["read", "grep", "find", "ls"],
|
|
62
|
+
systemPrompt: content.trim(),
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const fm = fmMatch[1];
|
|
67
|
+
const body = content.slice(fmMatch[0].length).trim();
|
|
68
|
+
const fields: Record<string, string> = {};
|
|
69
|
+
|
|
70
|
+
for (const line of fm.split("\n")) {
|
|
71
|
+
const idx = line.indexOf(":");
|
|
72
|
+
if (idx > 0) {
|
|
73
|
+
let val = line.slice(idx + 1).trim();
|
|
74
|
+
if ((val.startsWith('"') && val.endsWith('"')) || (val.startsWith("'") && val.endsWith("'"))) {
|
|
75
|
+
val = val.slice(1, -1);
|
|
76
|
+
}
|
|
77
|
+
fields[line.slice(0, idx).trim()] = val;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return {
|
|
82
|
+
name: fields.name || agentName,
|
|
83
|
+
tools: fields.tools ? fields.tools.split(",").map((t) => t.trim()).filter(Boolean) : ["read", "grep", "find", "ls"],
|
|
84
|
+
model: fields.model || undefined,
|
|
85
|
+
thinking: fields.thinking || undefined,
|
|
86
|
+
systemPrompt: body || content.trim(),
|
|
87
|
+
};
|
|
88
|
+
}
|
package/index.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* workflow: barrel export
|
|
3
|
+
*
|
|
4
|
+
* 导出所有公共 API。内部实现细节不导出。
|
|
5
|
+
* 本文件不是 pi 扩展入口,仅为 npm "main" 字段的占位。
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export { runSubagent } from "./subagent";
|
|
9
|
+
export { loadAgentDef, type AgentDef } from "./agent-loader";
|
|
10
|
+
export { createSubagentWidget } from "./widget";
|
|
11
|
+
export { saveSubagentOutput, readSubagentOutput } from "./output";
|
|
12
|
+
export type { SubagentResult, SubagentEvent } from "./types";
|
|
13
|
+
export { createStateManager, createUIUpdater } from "./state";
|
|
14
|
+
export { registerWorkflowTool, type ActionDef } from "./workflow";
|
|
15
|
+
export { findSessionFile, getSubagentStatusSummary, isSubagentSuccess } from "./utils";
|
|
16
|
+
|
|
17
|
+
// pi 扩展系统要求 export default function,这不是一个真正的扩展
|
|
18
|
+
export default function noop() {}
|
package/output.ts
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* workflow: 子代理输出保存
|
|
3
|
+
*
|
|
4
|
+
* 将子代理的完整输出写入文件,只返回简短总结。
|
|
5
|
+
* 所有子代理工具统一使用此函数,避免各自重复实现截断逻辑。
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import * as fs from "node:fs";
|
|
9
|
+
import * as path from "node:path";
|
|
10
|
+
|
|
11
|
+
export interface SubagentOutput {
|
|
12
|
+
/** 输出文件路径 */
|
|
13
|
+
filePath: string;
|
|
14
|
+
/** 简短总结(≤ 200 字符) */
|
|
15
|
+
summary: string;
|
|
16
|
+
/** 总行数 */
|
|
17
|
+
lineCount: number;
|
|
18
|
+
/** 文件大小(字节) */
|
|
19
|
+
size: number;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* 将子代理的完整输出写入文件,只返回简短总结。
|
|
24
|
+
*
|
|
25
|
+
* 输出文件保存在: {cwd}/.pi/plans/subagent-output-{工具名}-{时间戳}.md
|
|
26
|
+
*
|
|
27
|
+
* @param cwd 当前工作目录
|
|
28
|
+
* @param toolName 工具名称(用于文件名标识,如 "review")
|
|
29
|
+
* @param output 子代理的完整输出文本
|
|
30
|
+
* @param extra 可选的额外摘要信息(如测试统计)
|
|
31
|
+
*/
|
|
32
|
+
export function saveSubagentOutput(
|
|
33
|
+
cwd: string,
|
|
34
|
+
toolName: string,
|
|
35
|
+
output: string,
|
|
36
|
+
extra?: { passed?: number; failed?: number; total?: number; warnings?: number; criticals?: number },
|
|
37
|
+
): SubagentOutput {
|
|
38
|
+
const plansDir = path.join(cwd, ".pi", "plans");
|
|
39
|
+
if (!fs.existsSync(plansDir)) fs.mkdirSync(plansDir, { recursive: true });
|
|
40
|
+
|
|
41
|
+
const now = new Date();
|
|
42
|
+
const pad = (n: number) => String(n).padStart(2, "0");
|
|
43
|
+
const timestamp = `${now.getFullYear()}${pad(now.getMonth() + 1)}${pad(now.getDate())}` +
|
|
44
|
+
`-${pad(now.getHours())}${pad(now.getMinutes())}${pad(now.getSeconds())}`;
|
|
45
|
+
const filePath = path.join(plansDir, `${toolName}-${timestamp}.md`);
|
|
46
|
+
|
|
47
|
+
fs.writeFileSync(filePath, output, "utf-8");
|
|
48
|
+
|
|
49
|
+
const lines = output.split("\n");
|
|
50
|
+
const lineCount = lines.length;
|
|
51
|
+
const size = Buffer.byteLength(output, "utf-8");
|
|
52
|
+
|
|
53
|
+
const parts: string[] = [];
|
|
54
|
+
|
|
55
|
+
if (extra?.passed !== undefined) {
|
|
56
|
+
parts.push(`✅ ${extra.passed} passed`);
|
|
57
|
+
}
|
|
58
|
+
if (extra?.failed) {
|
|
59
|
+
parts.push(`❌ ${extra.failed} failed`);
|
|
60
|
+
}
|
|
61
|
+
if (extra?.criticals) {
|
|
62
|
+
parts.push(`🔴 ${extra.criticals} critical`);
|
|
63
|
+
}
|
|
64
|
+
if (extra?.warnings) {
|
|
65
|
+
parts.push(`🟡 ${extra.warnings} warnings`);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const sizeStr = size > 1024 * 1024
|
|
69
|
+
? `${(size / 1024 / 1024).toFixed(1)}MB`
|
|
70
|
+
: size > 1024
|
|
71
|
+
? `${(size / 1024).toFixed(0)}KB`
|
|
72
|
+
: `${size}B`;
|
|
73
|
+
|
|
74
|
+
const stats = parts.length > 0 ? ` | ${parts.join(", ")}` : "";
|
|
75
|
+
const summary = `完整输出已保存 (${lineCount} 行, ${sizeStr}${stats})`;
|
|
76
|
+
|
|
77
|
+
return { filePath, summary, lineCount, size };
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* 读取之前保存的子代理输出文件内容。
|
|
82
|
+
*/
|
|
83
|
+
export function readSubagentOutput(filePath: string): string | null {
|
|
84
|
+
try {
|
|
85
|
+
return fs.readFileSync(filePath, "utf-8");
|
|
86
|
+
} catch {
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "pi-ate-workflow",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "commonjs",
|
|
5
|
+
"main": "index.ts",
|
|
6
|
+
"description": "Workflow orchestration extension for pi-coding-agent — subagent spawning, research workflow, output capture",
|
|
7
|
+
"pi": {
|
|
8
|
+
"extensions": [
|
|
9
|
+
"."
|
|
10
|
+
]
|
|
11
|
+
},
|
|
12
|
+
"peerDependencies": {
|
|
13
|
+
"@earendil-works/pi-coding-agent": ">=0.74.0"
|
|
14
|
+
},
|
|
15
|
+
"peerDependenciesMeta": {
|
|
16
|
+
"@earendil-works/pi-coding-agent": {
|
|
17
|
+
"optional": true
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"@earendil-works/pi-coding-agent": "^0.76.0",
|
|
22
|
+
"@earendil-works/pi-tui": "^0.76.0",
|
|
23
|
+
"@types/node": "^25.6.2",
|
|
24
|
+
"@vitest/coverage-v8": "^3.2.4",
|
|
25
|
+
"typebox": "^1.1.38",
|
|
26
|
+
"typescript": "^6.0.3",
|
|
27
|
+
"vitest": "^3.0.0"
|
|
28
|
+
},
|
|
29
|
+
"files": [
|
|
30
|
+
"*.ts",
|
|
31
|
+
"tests/",
|
|
32
|
+
"README.md",
|
|
33
|
+
"README.en.md",
|
|
34
|
+
"tsconfig.json",
|
|
35
|
+
"vitest.config.ts",
|
|
36
|
+
"package.json"
|
|
37
|
+
],
|
|
38
|
+
"keywords": [
|
|
39
|
+
"pi-package"
|
|
40
|
+
],
|
|
41
|
+
"homepage": "https://catlain.github.io/pi-atelier/",
|
|
42
|
+
"repository": {
|
|
43
|
+
"type": "git",
|
|
44
|
+
"url": "git+ssh://git@github.com:catlain/pi-workflow.git"
|
|
45
|
+
},
|
|
46
|
+
"bugs": {
|
|
47
|
+
"url": "https://github.com/catlain/pi-workflow/issues"
|
|
48
|
+
},
|
|
49
|
+
"license": "MIT"
|
|
50
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* workflow: 全局文章分析状态管理
|
|
3
|
+
*
|
|
4
|
+
* analysis.json 跟踪每篇文章的分析进度(评分/解读/方向)。
|
|
5
|
+
* 路径:{cwd}/docs/research/analysis.json
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import * as fs from "node:fs";
|
|
9
|
+
import * as path from "node:path";
|
|
10
|
+
import { getResearchDir } from "./research-utils";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* 单篇文章的分析状态
|
|
14
|
+
*/
|
|
15
|
+
export interface ArticleAnalysis {
|
|
16
|
+
/** 是否已提取方向 */
|
|
17
|
+
direction_done: boolean;
|
|
18
|
+
/** 是否已评分 */
|
|
19
|
+
score_evaluated: boolean;
|
|
20
|
+
/** 是否已解读 */
|
|
21
|
+
interpretation_done: boolean;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* 全局分析状态:article_id → ArticleAnalysis
|
|
26
|
+
*/
|
|
27
|
+
export type AnalysisState = Record<string, ArticleAnalysis>;
|
|
28
|
+
|
|
29
|
+
function getAnalysisPath(cwd: string): string {
|
|
30
|
+
return path.join(getResearchDir(cwd), "analysis.json");
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* 加载全局分析状态。文件不存在时返回空对象。
|
|
35
|
+
*/
|
|
36
|
+
export function loadAnalysis(cwd: string): AnalysisState {
|
|
37
|
+
const fp = getAnalysisPath(cwd);
|
|
38
|
+
if (!fs.existsSync(fp)) return {};
|
|
39
|
+
try {
|
|
40
|
+
return JSON.parse(fs.readFileSync(fp, "utf-8"));
|
|
41
|
+
} catch {
|
|
42
|
+
return {};
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* 保存全局分析状态。
|
|
48
|
+
*/
|
|
49
|
+
export function saveAnalysis(cwd: string, state: AnalysisState): void {
|
|
50
|
+
const dir = getResearchDir(cwd);
|
|
51
|
+
if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
|
|
52
|
+
fs.writeFileSync(getAnalysisPath(cwd), JSON.stringify(state, null, 2), "utf-8");
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* 为新文章创建默认分析状态(全 pending)。
|
|
57
|
+
*/
|
|
58
|
+
export function defaultAnalysis(): ArticleAnalysis {
|
|
59
|
+
return {
|
|
60
|
+
direction_done: false,
|
|
61
|
+
score_evaluated: false,
|
|
62
|
+
interpretation_done: false,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* workflow: 研究管道 Catalog 管理
|
|
3
|
+
*
|
|
4
|
+
* catalog 是全局文章目录。
|
|
5
|
+
* 路径:{cwd}/docs/research/catalog.json
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import * as fs from "node:fs";
|
|
9
|
+
import * as path from "node:path";
|
|
10
|
+
import type { Article } from "./research-types";
|
|
11
|
+
import { getResearchDir } from "./research-utils";
|
|
12
|
+
|
|
13
|
+
function getCatalogPath(cwd: string): string {
|
|
14
|
+
return path.join(getResearchDir(cwd), "catalog.json");
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** 加载全局文章目录 */
|
|
18
|
+
export function loadCatalog(cwd: string): Article[] {
|
|
19
|
+
const p = getCatalogPath(cwd);
|
|
20
|
+
try {
|
|
21
|
+
const raw = fs.readFileSync(p, "utf-8");
|
|
22
|
+
return JSON.parse(raw);
|
|
23
|
+
} catch {
|
|
24
|
+
return [];
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** 保存全局文章目录 */
|
|
29
|
+
export function saveCatalog(cwd: string, catalog: Article[]): void {
|
|
30
|
+
const dir = getResearchDir(cwd);
|
|
31
|
+
if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
|
|
32
|
+
fs.writeFileSync(getCatalogPath(cwd), JSON.stringify(catalog, null, 2), "utf-8");
|
|
33
|
+
}
|
package/research-scan.ts
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* workflow: 研究管道文件扫描
|
|
3
|
+
*
|
|
4
|
+
* 扫描全局 sources/ 目录,返回未在 catalog 中的新文章。
|
|
5
|
+
* 路径:{cwd}/docs/research/sources/{source_name}/{timestamp}_article.md
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import * as fs from "node:fs";
|
|
9
|
+
import * as path from "node:path";
|
|
10
|
+
import type { Article } from "./research-types";
|
|
11
|
+
import { parseFrontmatter, urlToId, getResearchDir } from "./research-utils";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* 扫描全局 sources/ 目录,返回不在 existingIds 中的新文章。
|
|
15
|
+
*
|
|
16
|
+
* @param cwd 项目根目录
|
|
17
|
+
* @param existingIds 已有文章 id 列表(去重用)
|
|
18
|
+
*/
|
|
19
|
+
export function scanNewSources(cwd: string, existingIds: string[]): Article[] {
|
|
20
|
+
const sourcesDir = path.join(getResearchDir(cwd), "sources");
|
|
21
|
+
if (!fs.existsSync(sourcesDir)) return [];
|
|
22
|
+
|
|
23
|
+
const existingIdSet = new Set(existingIds);
|
|
24
|
+
const newArticles: Article[] = [];
|
|
25
|
+
|
|
26
|
+
const sourceNames = fs.readdirSync(sourcesDir, { withFileTypes: true })
|
|
27
|
+
.filter((d) => d.isDirectory())
|
|
28
|
+
.map((d) => d.name);
|
|
29
|
+
|
|
30
|
+
for (const sourceName of sourceNames) {
|
|
31
|
+
const sourcePath = path.join(sourcesDir, sourceName);
|
|
32
|
+
const files = fs.readdirSync(sourcePath)
|
|
33
|
+
.filter((f) => f.endsWith("_article.md"));
|
|
34
|
+
|
|
35
|
+
for (const file of files) {
|
|
36
|
+
const filePath = path.join(sourcePath, file);
|
|
37
|
+
try {
|
|
38
|
+
const content = fs.readFileSync(filePath, "utf-8");
|
|
39
|
+
const fm = parseFrontmatter(content);
|
|
40
|
+
const url = fm.url || "";
|
|
41
|
+
|
|
42
|
+
if (!url) continue;
|
|
43
|
+
|
|
44
|
+
const id = urlToId(url);
|
|
45
|
+
if (id && existingIdSet.has(id)) continue;
|
|
46
|
+
|
|
47
|
+
newArticles.push({
|
|
48
|
+
id,
|
|
49
|
+
url,
|
|
50
|
+
title: fm.title || file.replace(/\.md$/, "").replace(/_article$/, ""),
|
|
51
|
+
source_type: fm.source_type || "other",
|
|
52
|
+
found_at: fm.found_at || new Date().toISOString(),
|
|
53
|
+
score: typeof fm.score === "number" ? fm.score : 5,
|
|
54
|
+
dir: `sources/${sourceName}/`,
|
|
55
|
+
file,
|
|
56
|
+
});
|
|
57
|
+
if (id) existingIdSet.add(id);
|
|
58
|
+
} catch {
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return newArticles;
|
|
65
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* workflow: 主题方向状态管理
|
|
3
|
+
*
|
|
4
|
+
* 每个主题的方向状态独立管理。
|
|
5
|
+
* 路径:{cwd}/docs/research/directions/{slug}/state.json
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import * as fs from "node:fs";
|
|
9
|
+
import * as path from "node:path";
|
|
10
|
+
import type { TopicResearchState } from "./research-types";
|
|
11
|
+
import { getDirectionsDir } from "./research-utils";
|
|
12
|
+
|
|
13
|
+
function getStatePath(cwd: string, slug: string): string {
|
|
14
|
+
return path.join(getDirectionsDir(cwd, slug), "state.json");
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** 加载主题方向状态 */
|
|
18
|
+
export function loadTopicState(cwd: string, slug: string): TopicResearchState {
|
|
19
|
+
const p = getStatePath(cwd, slug);
|
|
20
|
+
try {
|
|
21
|
+
const raw = fs.readFileSync(p, "utf-8");
|
|
22
|
+
return JSON.parse(raw);
|
|
23
|
+
} catch {
|
|
24
|
+
return {
|
|
25
|
+
topic: "",
|
|
26
|
+
slug,
|
|
27
|
+
created_at: new Date().toISOString(),
|
|
28
|
+
queries_searched: [],
|
|
29
|
+
articles: {},
|
|
30
|
+
directions: [],
|
|
31
|
+
iteration: 0,
|
|
32
|
+
max_iterations: 3,
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** 保存主题方向状态 */
|
|
38
|
+
export function saveTopicState(cwd: string, slug: string, state: TopicResearchState): void {
|
|
39
|
+
const dir = getDirectionsDir(cwd, slug);
|
|
40
|
+
if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
|
|
41
|
+
fs.writeFileSync(getStatePath(cwd, slug), JSON.stringify(state, null, 2), "utf-8");
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** 别名(与旧代码兼容) */
|
|
45
|
+
export const loadDirectionState = loadTopicState;
|
|
46
|
+
export const saveDirectionState = saveTopicState;
|