pi-ate-workflow 0.1.0 → 0.2.1

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 CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  # pi-workflow
4
4
 
5
+ [Source Code](https://github.com/catlain/pi-workflow) | [npm](https://www.npmjs.com/package/pi-ate-workflow)
6
+
5
7
  Workflow orchestration extension for [pi](https://github.com/earendil-works/pi-coding-agent) — subagent spawning, research workflow, and output capture.
6
8
 
7
9
  ## Why You Need It
package/README.md CHANGED
@@ -4,6 +4,8 @@
4
4
 
5
5
  # pi-workflow
6
6
 
7
+ [源码仓库](https://github.com/catlain/pi-workflow) | [npm](https://www.npmjs.com/package/pi-ate-workflow)
8
+
7
9
  Workflow orchestration extension for [pi](https://github.com/earendil-works/pi-coding-agent) — subagent spawning, research workflow, and output capture.
8
10
 
9
11
  ## Why You Need It
package/agents.ts ADDED
@@ -0,0 +1,2 @@
1
+ // 复用 shared-utils 的 agent 发现逻辑
2
+ export { discoverAgents, getAgentDescription } from "@pi-atelier/shared-utils/agents";
package/index.ts CHANGED
@@ -1,18 +1,97 @@
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() {}
1
+ /**
2
+ * workflow: barrel export + 运行时扩展入口
3
+ *
4
+ * 导出所有公共 API。内部实现细节不导出。
5
+ * 默认导出是 pi 扩展入口(全局加载):注册可见模式子代理自动退出 +
6
+ * intercom 桥接的 orchestrator 会话名解析器。
7
+ */
8
+
9
+ import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
10
+ import { setOrchestratorNameResolver } from "./intercom-bridge";
11
+ import { restoreSubagentModel } from "./subagent-model";
12
+
13
+ export { runSubagent, setSessionFileResolver } from "./subagent";
14
+ export { type OutputConstraint } from "./subagent";
15
+ export { type AgentDef, loadAgentDef } from "@pi-atelier/shared-utils";
16
+ export { spawnOnce } from "./subagent-spawn-once";
17
+ export { spawnVisible } from "./subagent-spawn-visible";
18
+ export { SubagentSession } from "./subagent-session";
19
+ export { detectTerminal, getBackend, type TermBackend } from "./term-backend";
20
+ export { resolveSubagentTimeout, DEFAULT_SUBAGENT_TIMEOUT_MS } from "./timeout-config";
21
+ export { buildIntercomBridge, setOrchestratorNameResolver, resolveOrchestratorName } from "./intercom-bridge";
22
+ export {
23
+ getSubagentModel,
24
+ setSubagentModel,
25
+ isSubagentModelRestored,
26
+ setSubagentModelRestored,
27
+ restoreSubagentModel,
28
+ } from "./subagent-model";
29
+ export { createSubagentWidget } from "./widget";
30
+ export { saveSubagentOutput, readSubagentOutput } from "./output";
31
+ export type { SubagentResult, SubagentEvent } from "./types";
32
+ export { createStateManager, createUIUpdater } from "./state";
33
+ export { registerWorkflowTool, type ActionDef } from "./workflow";
34
+ export { findSessionFile, getSubagentStatusSummary, isSubagentSuccess } from "./utils";
35
+ export { discoverAgents, getAgentDescription } from "./agents";
36
+ export { AGENT_DIR, AGENTS_DIR } from "./paths";
37
+
38
+ // Research module
39
+ export type {
40
+ Article,
41
+ ArticleAnalysis as ArticleAnalysisType,
42
+ Direction,
43
+ TopicResearchState,
44
+ } from "./research-types";
45
+ export {
46
+ slugify,
47
+ urlToId,
48
+ parseFrontmatter,
49
+ getTimestamp,
50
+ getResearchDir,
51
+ getDirectionsDir,
52
+ getTopicDir,
53
+ } from "./research-utils";
54
+ export {
55
+ loadCatalog,
56
+ saveCatalog,
57
+ } from "./research-catalog";
58
+ export {
59
+ loadTopicState,
60
+ saveTopicState,
61
+ loadDirectionState,
62
+ saveDirectionState,
63
+ } from "./research-state";
64
+ export {
65
+ loadAnalysis,
66
+ saveAnalysis,
67
+ defaultAnalysis,
68
+ type ArticleAnalysis,
69
+ type AnalysisState,
70
+ } from "./research-analysis";
71
+ export {
72
+ scanNewSources,
73
+ scanTopicArticles,
74
+ } from "./research-scan";
75
+
76
+ // pi 扩展入口:每个 pi 进程(含子代理进程)都会加载
77
+ export default function workflowExtension(pi: ExtensionAPI) {
78
+ // 可见模式子代理自动退出:launch.sh 注入 PI_SUBAGENT_AUTO_EXIT=1 后,
79
+ // 子代理 agent_end 后延迟退出(让 TUI 渲染末尾输出并刷盘 session.jsonl)
80
+ pi.on("agent_end", () => {
81
+ if (process.env.PI_SUBAGENT_AUTO_EXIT === "1") {
82
+ setTimeout(() => process.exit(0), 800);
83
+ }
84
+ });
85
+
86
+ // intercom 桥接:注册 orchestrator 会话名解析器(contact_supervisor 路由用)
87
+ setOrchestratorNameResolver(() => {
88
+ try {
89
+ return pi.getSessionName();
90
+ } catch {
91
+ return undefined;
92
+ }
93
+ });
94
+
95
+ // 恢复子代理默认模型(settings.json subagent.defaultModel)
96
+ restoreSubagentModel();
97
+ }
@@ -0,0 +1,53 @@
1
+ /**
2
+ * pi-intercom 桥接环境变量构建
3
+ *
4
+ * 这些环境变量让 pi-intercom(子代理侧)注册 contact_supervisor 工具。
5
+ * 如果 pi-intercom 未安装,这些环境变量会被安全忽略(零副作用)。
6
+ */
7
+
8
+ let _childIndex = 0;
9
+
10
+ /** orchestrator 会话名解析器,由各扩展在扩展入口注册 */
11
+ let _orchestratorNameResolver: (() => string | undefined | Promise<string | undefined>) | undefined;
12
+
13
+ /**
14
+ * 各扩展入口调用:注册 orchestrator 会话名的获取方式
15
+ * (subagent 扩展传 pi.getSessionName;未注册时回退 "orchestrator")
16
+ */
17
+ export function setOrchestratorNameResolver(
18
+ resolver: (() => string | undefined | Promise<string | undefined>) | undefined,
19
+ ): void {
20
+ _orchestratorNameResolver = resolver;
21
+ }
22
+
23
+ /**
24
+ * 构建 pi-intercom 需要的子代理桥接环境变量(5 个 PI_SUBAGENT_* 变量)。
25
+ *
26
+ * run-id 由时间戳 + 进程内自增序号组成,同一父进程内保证唯一。
27
+ */
28
+ export function buildIntercomBridge(
29
+ orchestratorName: string | undefined,
30
+ agentName: string,
31
+ ): Record<string, string> {
32
+ const idx = ++_childIndex;
33
+ const runId = `run-${Date.now().toString(36)}-${idx.toString(36)}`;
34
+ const target = orchestratorName || "orchestrator";
35
+ return {
36
+ PI_SUBAGENT_ORCHESTRATOR_TARGET: target,
37
+ PI_SUBAGENT_RUN_ID: runId,
38
+ PI_SUBAGENT_CHILD_AGENT: agentName,
39
+ PI_SUBAGENT_CHILD_INDEX: String(idx),
40
+ PI_SUBAGENT_INTERCOM_SESSION_NAME: `${agentName}-${runId}`,
41
+ };
42
+ }
43
+
44
+ /**
45
+ * 解析 orchestrator 会话名(已注册 resolver 则调用,否则 undefined)
46
+ */
47
+ export async function resolveOrchestratorName(): Promise<string | undefined> {
48
+ try {
49
+ return await _orchestratorNameResolver?.();
50
+ } catch {
51
+ return undefined;
52
+ }
53
+ }
package/package.json CHANGED
@@ -1,50 +1,61 @@
1
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"
2
+ "name": "pi-ate-workflow",
3
+ "version": "0.2.1",
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
+ "dependencies": {
13
+ "@pi-atelier/shared-utils": "^1.1.1"
14
+ },
15
+ "peerDependencies": {
16
+ "@earendil-works/pi-coding-agent": ">=0.74.0"
17
+ },
18
+ "optionalDependencies": {
19
+ "@earendil-works/pi-coding-agent": "^0.76.0"
20
+ },
21
+ "peerDependenciesMeta": {
22
+ "@earendil-works/pi-coding-agent": {
23
+ "optional": true
24
+ }
25
+ },
26
+ "scripts": {
27
+ "test": "npx vitest run",
28
+ "typecheck": "tsc --noEmit",
29
+ "prepublishOnly": "npm run typecheck"
30
+ },
31
+ "devDependencies": {
32
+ "@earendil-works/pi-coding-agent": "^0.76.0",
33
+ "@earendil-works/pi-tui": "^0.76.0",
34
+ "@types/node": "^25.6.2",
35
+ "@vitest/coverage-v8": "^3.2.4",
36
+ "typebox": "^1.1.38",
37
+ "typescript": "^6.0.3",
38
+ "vitest": "^3.0.0"
39
+ },
40
+ "files": [
41
+ "*.ts",
42
+ "tests/",
43
+ "README.md",
44
+ "README.en.md",
45
+ "tsconfig.json",
46
+ "vitest.config.ts",
47
+ "package.json"
48
+ ],
49
+ "keywords": [
50
+ "pi-package"
51
+ ],
52
+ "homepage": "https://catlain.github.io/pi-atelier/",
53
+ "repository": {
54
+ "type": "git",
55
+ "url": "git+ssh://git@github.com:catlain/pi-workflow.git"
56
+ },
57
+ "bugs": {
58
+ "url": "https://github.com/catlain/pi-workflow/issues"
59
+ },
60
+ "license": "MIT"
50
61
  }
package/paths.ts ADDED
@@ -0,0 +1,7 @@
1
+ import { homedir } from "node:os";
2
+ import { join } from "node:path";
3
+
4
+ /** pi agent 根目录,默认 ~/.pi/agent */
5
+ export const AGENT_DIR = process.env.PI_AGENT_DIR || join(homedir(), ".pi/agent");
6
+ /** 子代理定义目录 ~/.pi/agent/agents/ */
7
+ export const AGENTS_DIR = join(AGENT_DIR, "agents");
package/research-scan.ts CHANGED
@@ -1,65 +1,113 @@
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
- }
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
+ }
66
+
67
+ /**
68
+ * 扫描 topics/{slug}/articles/ 扁平目录,返回不在 existingIds 中的新文章。
69
+ *
70
+ * @param cwd 项目根目录
71
+ * @param slug 主题 slug
72
+ * @param existingIds 已有文章 id 列表(去重用)
73
+ */
74
+ export function scanTopicArticles(cwd: string, slug: string, existingIds: string[]): Article[] {
75
+ const articlesDir = path.join(cwd, "docs", "research", "topics", slug, "articles");
76
+ if (!fs.existsSync(articlesDir)) return [];
77
+
78
+ const existingIdSet = new Set(existingIds);
79
+ const newArticles: Article[] = [];
80
+
81
+ const files = fs.readdirSync(articlesDir)
82
+ .filter((f) => f.endsWith("_article.md"));
83
+
84
+ for (const file of files) {
85
+ const filePath = path.join(articlesDir, file);
86
+ try {
87
+ const content = fs.readFileSync(filePath, "utf-8");
88
+ const fm = parseFrontmatter(content);
89
+ const url = fm.url || "";
90
+
91
+ if (!url) continue;
92
+
93
+ const id = urlToId(url);
94
+ if (id && existingIdSet.has(id)) continue;
95
+
96
+ newArticles.push({
97
+ id,
98
+ url,
99
+ title: fm.title || file.replace(/\.md$/, "").replace(/_article$/, ""),
100
+ source_type: fm.source_type || "other",
101
+ found_at: fm.found_at || new Date().toISOString(),
102
+ score: typeof fm.score === "number" ? fm.score : 5,
103
+ dir: "articles/",
104
+ file,
105
+ });
106
+ if (id) existingIdSet.add(id);
107
+ } catch {
108
+ continue;
109
+ }
110
+ }
111
+
112
+ return newArticles;
113
+ }