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/workflow.ts
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* workflow: 工作流工具注册
|
|
3
|
+
*
|
|
4
|
+
* registerWorkflowTool 封装了通用工具注册模式:
|
|
5
|
+
* - 从 actions 自动生成 action 枚举
|
|
6
|
+
* - switch 路由到对应 handler
|
|
7
|
+
* - gate 检查(前置门控)
|
|
8
|
+
* - 每次 action 后自动 persist + updateUI
|
|
9
|
+
* - handler 异常时 persist 中间 state 后传播错误
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import type { ExtensionAPI, ExtensionContext, AgentToolResult } from "@earendil-works/pi-coding-agent";
|
|
13
|
+
import { Type } from "typebox";
|
|
14
|
+
import type { StateManager, UIUpdater } from "./state";
|
|
15
|
+
|
|
16
|
+
export interface ActionDef<T, TParams = Record<string, unknown>, TDetails = Record<string, unknown>> {
|
|
17
|
+
description: string;
|
|
18
|
+
handler: (
|
|
19
|
+
params: TParams,
|
|
20
|
+
state: T,
|
|
21
|
+
ctx: ExtensionContext,
|
|
22
|
+
signal?: AbortSignal,
|
|
23
|
+
onUpdate?: (update: AgentToolResult<TDetails>) => void,
|
|
24
|
+
) => Promise<AgentToolResult<TDetails>>;
|
|
25
|
+
/** 可选的前置门控。返回 {pass:false, reason} 阻止 handler 执行 */
|
|
26
|
+
gate?: (state: T) => { pass: boolean; reason?: string };
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* 注册工作流工具。
|
|
31
|
+
*
|
|
32
|
+
* 内部处理:
|
|
33
|
+
* - 参数 schema 生成(从 actions 自动生成 action enum)
|
|
34
|
+
* - switch 路由到对应 action handler
|
|
35
|
+
* - gate 检查
|
|
36
|
+
* - 每次 action 后自动 persist + updateUI
|
|
37
|
+
* - handler 异常时 persist + updateUI 后向上抛
|
|
38
|
+
*/
|
|
39
|
+
export function registerWorkflowTool<T>(pi: ExtensionAPI, options: {
|
|
40
|
+
name: string;
|
|
41
|
+
description: string;
|
|
42
|
+
promptSnippet?: string;
|
|
43
|
+
promptGuidelines?: string[];
|
|
44
|
+
actions: Record<string, ActionDef<T>>;
|
|
45
|
+
stateManager: StateManager<T>;
|
|
46
|
+
uiUpdater: UIUpdater<T>;
|
|
47
|
+
/** 可选的自定义参数 schema 字段,合并到默认的 action+plan_file 之外 */
|
|
48
|
+
extraParams?: Record<string, any>;
|
|
49
|
+
}): void {
|
|
50
|
+
const actionNames = Object.keys(options.actions);
|
|
51
|
+
|
|
52
|
+
pi.registerTool({
|
|
53
|
+
name: options.name,
|
|
54
|
+
label: options.name,
|
|
55
|
+
description: options.description,
|
|
56
|
+
promptSnippet: options.promptSnippet,
|
|
57
|
+
promptGuidelines: options.promptGuidelines,
|
|
58
|
+
parameters: Type.Object({
|
|
59
|
+
action: Type.Union(actionNames.map(a => Type.Literal(a))),
|
|
60
|
+
plan_file: Type.Optional(Type.String({ description: "方案文件路径(可选,默认用 state 中记录的路径)" })),
|
|
61
|
+
...(options.extraParams || {}),
|
|
62
|
+
}),
|
|
63
|
+
|
|
64
|
+
async execute(
|
|
65
|
+
_tcId: string,
|
|
66
|
+
params: Record<string, unknown>,
|
|
67
|
+
signal: AbortSignal | undefined,
|
|
68
|
+
onUpdate: ((update: AgentToolResult<Record<string, unknown>>) => void) | undefined,
|
|
69
|
+
ctx: ExtensionContext,
|
|
70
|
+
): Promise<AgentToolResult<Record<string, unknown>>> {
|
|
71
|
+
const { action, ...actionParams } = params;
|
|
72
|
+
const actionDef = options.actions[action as string];
|
|
73
|
+
|
|
74
|
+
if (!actionDef) {
|
|
75
|
+
return {
|
|
76
|
+
content: [{ type: "text" as const, text: `未知 action: "${action}"。可用: ${actionNames.join(", ")}` }],
|
|
77
|
+
details: { error: `unknown_action: ${action}` },
|
|
78
|
+
terminate: false,
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// Gate 检查
|
|
83
|
+
if (actionDef.gate) {
|
|
84
|
+
const gateResult = actionDef.gate(options.stateManager.get());
|
|
85
|
+
if (!gateResult.pass) {
|
|
86
|
+
return {
|
|
87
|
+
content: [{ type: "text" as const, text: `Gate 阻止: ${gateResult.reason}` }],
|
|
88
|
+
details: { error: `gate_blocked: ${gateResult.reason}` },
|
|
89
|
+
terminate: false,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
try {
|
|
95
|
+
const result = await actionDef.handler(
|
|
96
|
+
actionParams as Record<string, unknown>,
|
|
97
|
+
options.stateManager.get(),
|
|
98
|
+
ctx,
|
|
99
|
+
signal,
|
|
100
|
+
onUpdate as ((update: AgentToolResult<Record<string, unknown>>) => void) | undefined,
|
|
101
|
+
);
|
|
102
|
+
options.stateManager.persist(ctx);
|
|
103
|
+
options.uiUpdater.update(ctx, options.stateManager.get());
|
|
104
|
+
return result;
|
|
105
|
+
} catch (err) {
|
|
106
|
+
// handler 异常时,先 persist 当前中间状态再向上传播
|
|
107
|
+
options.stateManager.persist(ctx);
|
|
108
|
+
options.uiUpdater.update(ctx, options.stateManager.get());
|
|
109
|
+
throw err;
|
|
110
|
+
}
|
|
111
|
+
},
|
|
112
|
+
} as any);
|
|
113
|
+
}
|