@peer-agent/protocol 0.0.2-beta.4 → 0.0.2

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/dist/goal.d.ts CHANGED
@@ -166,7 +166,7 @@ export interface GoalRevision {
166
166
  readonly changedBy?: string;
167
167
  }
168
168
  /** Goal Runner 托管推进器状态。 */
169
- export type GoalRunnerStatus = 'idle' | 'running' | 'compacting_context' | 'resuming_after_compaction' | 'paused' | 'exploring' | 'blocked' | 'budget_exhausted' | 'completed' | 'failed';
169
+ export type GoalRunnerStatus = 'idle' | 'running' | 'compacting_context' | 'resuming_after_compaction' | 'paused' | 'exploring' | 'waiting_user' | 'blocked' | 'budget_exhausted' | 'completed' | 'failed';
170
170
  /** Goal Runner 当前推进意图;只描述编排意图,不替代工具执行 Evidence。 */
171
171
  export type GoalRunnerIntent = 'execute' | 'verify' | 'explore' | 'synthesize' | 'block';
172
172
  export type GoalRunnerPhase = 'orient' | 'inspect' | 'plan_scaffold' | 'act' | 'verify' | 'repair' | 'synthesize' | 'blocked';
@@ -520,6 +520,15 @@ export interface GoalPlan {
520
520
  readonly version: number;
521
521
  readonly revisionHistory: GoalRevision[];
522
522
  readonly evidenceRefs: string[];
523
+ /**
524
+ * 用户对 completed 结果的验收(工作台一键确认写入)。
525
+ * 与 GoalPlanStatus.accepted(计划被接受执行)无关。
526
+ * 有 acceptedAt 时 TaskOverview 投影为 terminal。
527
+ */
528
+ readonly resultAcceptance?: {
529
+ readonly acceptedAt: string;
530
+ readonly acceptedBy?: 'user' | string;
531
+ };
523
532
  /** 对齐 system-context.ts 的 epoch */
524
533
  readonly promptContextEpochId?: string;
525
534
  readonly createdAt: string;
package/dist/index.d.ts CHANGED
@@ -61,6 +61,13 @@ export interface SkillSummary {
61
61
  readonly scope: SkillScope;
62
62
  /** scope=workspace 时对应的工作空间绝对路径。 */
63
63
  readonly workspacePath?: string | null;
64
+ /** 市场/来源图标 URL;本地 skill 可为空。 */
65
+ readonly iconUrl?: string | null;
66
+ /**
67
+ * 安装来源标识。
68
+ * 例:skillhub、aone-open;本地手工 skill 可为空。
69
+ */
70
+ readonly source?: string | null;
64
71
  }
65
72
  export interface SkillDetail extends SkillSummary {
66
73
  readonly instructions: string;
@@ -792,4 +799,5 @@ export * from './goal.ts';
792
799
  export * from './updater.ts';
793
800
  export * from './appshot.ts';
794
801
  export * from './automation.ts';
802
+ export * from './task-overview.ts';
795
803
  export * from './skill-marketplace.ts';
package/dist/index.js CHANGED
@@ -23,4 +23,5 @@ export * from "./goal.js";
23
23
  export * from "./updater.js";
24
24
  export * from "./appshot.js";
25
25
  export * from "./automation.js";
26
+ export * from "./task-overview.js";
26
27
  export * from "./skill-marketplace.js";
@@ -110,6 +110,8 @@ export interface SkillHubInstallRequest {
110
110
  readonly version: string;
111
111
  /** 默认 global;workspace 需已打开工作区。 */
112
112
  readonly scope?: SkillHubInstallScope;
113
+ /** 市场图标 URL;安装时写入本地 _meta.json,供已安装列表展示。 */
114
+ readonly iconUrl?: string | null;
113
115
  }
114
116
  export interface SkillHubCategory {
115
117
  readonly key: string;
@@ -68,6 +68,20 @@ export interface ChatProviderMessage {
68
68
  readonly role: string;
69
69
  readonly content: unknown;
70
70
  }
71
+ export interface ChatStartTaskRequest {
72
+ readonly text: string;
73
+ readonly title?: string;
74
+ readonly workspacePath?: string | null;
75
+ readonly mode?: string;
76
+ readonly effort?: string;
77
+ readonly modelProviderId?: string | null;
78
+ readonly attachments?: readonly unknown[];
79
+ }
80
+ export interface ChatStartTaskResult {
81
+ readonly conversationId: string;
82
+ readonly streamId: string;
83
+ readonly assistantMessageId: string;
84
+ }
71
85
  export interface ChatSendRequest {
72
86
  /**
73
87
  * @deprecated Desktop Main projects canonical history from conversationId.
@@ -0,0 +1,261 @@
1
+ /**
2
+ * TaskOverview 行动权投影契约 —— Peer 2.0 阶段 0 协议层。
3
+ *
4
+ * 设计依据:peer-knowledge design/product/peer-2-0-gap-analysis.md §11。
5
+ *
6
+ * 核心命题:首页/任务/历史三页面不是第二份任务数据库,而是
7
+ * GoalPlan / GoalRunner / Automation 三套状态机按「下一步行动权」
8
+ * 形成的动态投影。本文件是该投影的唯一事实来源(single source of truth)。
9
+ *
10
+ * 治理红线(AGENTS.md):
11
+ * - renderer 只消费本模块输出的投影结果,不自行解析 GoalPlanStatus/
12
+ * AutomationRunStatus 推断行动权,不在前端维护任务状态副本。
13
+ * - 映射规则见 §11.3:判定顺序从上到下、首个命中生效;本模块用
14
+ * projectGoalPlan / projectAutomationRun 两个纯函数实现该顺序。
15
+ *
16
+ * 输入设计:投影函数接收最小输入快照(ProjectionSnapshot),而非完整
17
+ * GoalPlan / AutomationRun 对象——调用方(main 进程聚合层)负责从
18
+ * 存储读出字段后组装快照,renderer 只拿到投影产物。这样契约边界清晰,
19
+ * 且测试无需构造巨型 fixture。
20
+ */
21
+ import type { GoalPlanStatus, GoalRunnerStatus, GoalTiming } from './goal.ts';
22
+ import type { AutomationLifecycleStatus, AutomationRunStatus } from './automation.ts';
23
+ /**
24
+ * 任务的下一步行动权归属(§11.2)。
25
+ *
26
+ * - needs_you:阻塞点在用户(待批准 / 待授权 / 待回答 / 待决策)。
27
+ * - peer_advancing:Peer 正在推进,用户无需介入。
28
+ * - result_ready:结果就绪待验收(依赖 Result Package,见 §11.5 过渡期)。
29
+ * - paused:用户或系统主动暂停,等待恢复。
30
+ * - terminal:终态,进历史页。
31
+ */
32
+ export type TaskActionRight = 'needs_you' | 'peer_advancing' | 'result_ready' | 'paused' | 'terminal';
33
+ /**
34
+ * 「需要你处理」内的细分桶(§11.4 决策 2),对应原型首页
35
+ * 「需要你 · 待确认 · 等待权限」三类卡片。
36
+ *
37
+ * - plan_approval:待批准/确认计划(§11.3 rule 1-2)。
38
+ * - user_input:待授权权限 / 待回答问题(rule 3-4)。
39
+ * - decision:待决策(rule 5,Runner blocked / 预算耗尽)。
40
+ */
41
+ export type TaskNeedsYouReason = 'plan_approval' | 'user_input' | 'decision';
42
+ /** 投影来源类别:标记该任务投影来自哪套事实来源。 */
43
+ export type TaskOverviewSourceKind = 'conversation' | 'goal_plan' | 'automation'
44
+ /** Peer 开启并仍可观察的后台 shell 线程。 */
45
+ | 'shell_background';
46
+ /**
47
+ * 用户在任务上可执行的下一步动作(§11.3「下一步行动」列)。
48
+ * 协议层只给出动作标识,具体 IPC 由 main 进程实现。
49
+ */
50
+ export type TaskNextAction = 'approve_plan' | 'confirm_scope' | 'grant_permission' | 'answer_question' | 'decide_blocked' | 'review_result' | 'resume' | 'enable' | 'inspect' | 'continue_task' | 'open_background_thread' | 'none';
51
+ /**
52
+ * 单个任务的行动权投影(首页卡片 / 任务行 / 历史行的数据契约)。
53
+ *
54
+ * 字段命名对齐原型:title / workspaceLabel / statusLabel / planProgress /
55
+ * planSteps / lastActiveAt / actionLabel 直接对应 UI 元素,renderer 不再二次推导。
56
+ */
57
+ /**
58
+ * GoalPlan 投影步骤(叶子任务)。
59
+ * status 与 GoalTask.status / ExecutionStatus 字面量对齐。
60
+ */
61
+ export type TaskOverviewPlanStepStatus = 'pending' | 'running' | 'completed' | 'failed' | 'cancelled' | 'waiting_user';
62
+ export interface TaskOverviewPlanStep {
63
+ readonly taskId: string;
64
+ readonly title: string;
65
+ readonly status: TaskOverviewPlanStepStatus;
66
+ /** 当前 Runner 正在推进的步骤。 */
67
+ readonly current?: boolean;
68
+ }
69
+ export interface TaskOverviewItem {
70
+ /** 稳定投影身份(conversationId、planId 或 automationId+runId)。 */
71
+ readonly taskId: string;
72
+ /** 投影来源。 */
73
+ readonly source: TaskOverviewSourceKind;
74
+ /** 行动权归属。 */
75
+ readonly actionRight: TaskActionRight;
76
+ /** needs_you 细分桶;仅 actionRight === 'needs_you' 时有值。 */
77
+ readonly needsYouReason?: TaskNeedsYouReason;
78
+ /** 下一步动作标识。 */
79
+ readonly nextAction: TaskNextAction;
80
+ /** 用户可见任务名。有活跃 GoalPlan 时必须是 plan.title,不得用用户原话。 */
81
+ readonly title: string;
82
+ /** 当前步骤标题(非 plan 名);无步骤时省略。UI 渲染为「当前:…」,禁止「当前目标 · 确认语」。 */
83
+ readonly currentGoalTitle?: string;
84
+ /** Workspace 标签(原型卡片右上角)。 */
85
+ readonly workspaceLabel?: string;
86
+ /** 状态描述(原型卡片中部,如「Peer 正在验证」「等待权限」)。 */
87
+ readonly statusLabel: string;
88
+ /** Plan 进度「x / y」;无 Plan 概念的任务为 undefined。 */
89
+ readonly planProgress?: {
90
+ readonly completed: number;
91
+ readonly total: number;
92
+ };
93
+ /**
94
+ * GoalPlan 叶子步骤列表(标题 + 状态 + 当前标记)。
95
+ * 供「Peer 正在推进」等卡片展示具体步骤,而不仅是 x/y 计数。
96
+ */
97
+ readonly planSteps?: readonly TaskOverviewPlanStep[];
98
+ /** 最近活跃时间(ISO 字符串)。 */
99
+ readonly lastActiveAt?: string;
100
+ /**
101
+ * 任务完成时间(ISO 字符串)。
102
+ * GoalPlan:优先 GoalTiming.completedAt;终态缺省时回落 updatedAt。
103
+ * 结果待验收卡片用它展示「何时完成」,与 lastActiveAt(最近活跃)语义分离。
104
+ */
105
+ readonly completedAt?: string;
106
+ /**
107
+ * 有效运行时长(毫秒)。
108
+ * GoalPlan 来自 timing 投影(activeMs);缺 timing 时省略。
109
+ * UI 负责格式化为「3m12s」等,不在协议层落展示字符串。
110
+ */
111
+ readonly durationMs?: number;
112
+ /**
113
+ * 任务所用模型的用户可见标签(如 grok-4.5)。
114
+ * 来源:会话级 model / modelProviderId 绑定;无绑定时省略。
115
+ */
116
+ readonly modelLabel?: string;
117
+ /**
118
+ * 提供商展示标签(如 xai / openai)。
119
+ * 来源:modelProviderId 的 group 段;无绑定时省略。
120
+ */
121
+ readonly providerLabel?: string;
122
+ /** 动作按钮标签(原型「处理 →」「验收 →」「继续 →」)。 */
123
+ readonly actionLabel: string;
124
+ /** 关联的 conversationId,用于深链跳转。 */
125
+ readonly conversationId?: string;
126
+ }
127
+ /** GoalPlan 投影所需的最小字段快照。 */
128
+ export interface ConversationProjectionSnapshot {
129
+ readonly conversationId: string;
130
+ readonly title: string;
131
+ readonly workspaceLabel?: string;
132
+ readonly updatedAt?: string;
133
+ /**
134
+ * 用户已读水位(ISO)。首页「正在讨论」只投影未读沟通:
135
+ * 无 lastReadAt,或 updatedAt > lastReadAt。
136
+ */
137
+ readonly lastReadAt?: string | null;
138
+ /** 会话绑定模型标签;无绑定时省略。 */
139
+ readonly modelLabel?: string;
140
+ /** 提供商标签;无绑定时省略。 */
141
+ readonly providerLabel?: string;
142
+ }
143
+ export interface GoalPlanProjectionSnapshot {
144
+ readonly planId: string;
145
+ readonly status: GoalPlanStatus;
146
+ /** Runner 实时态;Plan 未进入自驱时为 undefined。 */
147
+ readonly runnerStatus?: GoalRunnerStatus;
148
+ readonly title: string;
149
+ readonly workspaceLabel?: string;
150
+ readonly progress?: {
151
+ readonly completed: number;
152
+ readonly total: number;
153
+ };
154
+ /** 叶子步骤投影;无任务树时省略。 */
155
+ readonly planSteps?: readonly TaskOverviewPlanStep[];
156
+ readonly updatedAt?: string;
157
+ readonly conversationId?: string;
158
+ /**
159
+ * GoalPlan.timing 原样透传;投影层用 projectGoalTiming 计算 durationMs。
160
+ * 调用方不预计算,避免 main 与 protocol 各算一遍。
161
+ */
162
+ readonly timing?: GoalTiming;
163
+ /**
164
+ * 会话模型绑定投影出的展示标签(如 grok-4.5)。
165
+ * 由聚合层从 conversation.model / modelProviderId 解析后写入。
166
+ */
167
+ readonly modelLabel?: string;
168
+ /**
169
+ * 提供商展示标签(如 xai)。
170
+ * 由聚合层从 modelProviderId 的 group 段解析后写入。
171
+ */
172
+ readonly providerLabel?: string;
173
+ /**
174
+ * 是否已完成 USER ACCEPTANCE 验收(§11.3 rule 6/16 的分界)。
175
+ * Result Package 落地前可恒传 false(过渡期:completed 一律进
176
+ * result_ready,由用户在 Result Review 页手动归档)。
177
+ */
178
+ readonly accepted?: boolean;
179
+ }
180
+ /** Automation 投影所需的最小字段快照(Definition 与 Run 联合判断)。 */
181
+ export interface AutomationProjectionSnapshot {
182
+ readonly automationId: string;
183
+ readonly runId?: string;
184
+ readonly definitionStatus: AutomationLifecycleStatus;
185
+ /** 最新一次 Run 的状态;definition 从未运行为 undefined。 */
186
+ readonly runStatus?: AutomationRunStatus;
187
+ readonly title: string;
188
+ readonly workspaceLabel?: string;
189
+ readonly updatedAt?: string;
190
+ readonly conversationId?: string;
191
+ /** 同 GoalPlanProjectionSnapshot.accepted。 */
192
+ readonly accepted?: boolean;
193
+ }
194
+ /**
195
+ * 后台 shell 线程投影所需的最小字段快照。
196
+ * 来源:runtime shell task manager(listShellTasks)。
197
+ */
198
+ export interface ShellBackgroundProjectionSnapshot {
199
+ readonly taskId: string;
200
+ readonly command: string;
201
+ readonly status: 'running' | 'completed' | 'failed' | 'cancelled' | 'timed_out' | string;
202
+ readonly workspaceLabel?: string;
203
+ readonly cwd?: string;
204
+ readonly startedAt?: string;
205
+ readonly completedAt?: string | null;
206
+ readonly toolCallId?: string;
207
+ readonly conversationId?: string;
208
+ }
209
+ /** Conversation → 无计划讨论态(工作台动线 §15)。 */
210
+ /**
211
+ * 会话是否对首页「正在讨论」可见(未读)。
212
+ * - 无 updatedAt:不可见
213
+ * - 无 lastReadAt:视为未读(兼容旧数据;打开后写入水位)
214
+ * - updatedAt > lastReadAt:未读
215
+ */
216
+ export declare function isConversationUnreadForDiscussion(snapshot: Pick<ConversationProjectionSnapshot, 'updatedAt' | 'lastReadAt'>): boolean;
217
+ export declare function projectConversation(snapshot: ConversationProjectionSnapshot): TaskOverviewItem;
218
+ /**
219
+ * GoalPlan 行动权投影(§11.3 rule 1-16 的 GoalPlan/Runner 分支)。
220
+ *
221
+ * 判定顺序(首个命中生效),Plan 态优先于 Runner 态(§11.4 决策 1):
222
+ * 1. plan awaiting_approval → needs_you/plan_approval
223
+ * 2. plan drafting → needs_you/plan_approval
224
+ * 4. runner waiting_user → needs_you/user_input
225
+ * 5. runner blocked | budget_exhausted → needs_you/decision
226
+ * 6. plan completed 且未验收 → result_ready
227
+ * 8. plan executing → peer_advancing
228
+ * 9. runner running/compacting/resuming/exploring → peer_advancing
229
+ * 11. plan approved/accepted(Runner 未启动)→ peer_advancing
230
+ * 12. plan paused → paused
231
+ * 13. runner paused → paused
232
+ * 14. runner idle 且 plan executing → paused(异常态,提示诊断)
233
+ * 16. plan completed(已验收)/cancelled/failed → terminal
234
+ *
235
+ * 注意 rule 14 在 rule 8 之前不会命中(executing 已被 rule 8 拦截),
236
+ * 因此实现时将 rule 14 放在 rule 8 之后、仅当 runner 显式 idle 时生效。
237
+ */
238
+ export declare function projectGoalPlan(snapshot: GoalPlanProjectionSnapshot, options?: {
239
+ readonly nowMs?: number;
240
+ }): TaskOverviewItem;
241
+ /**
242
+ * Automation 行动权投影(§11.3 rule 3-4/7/10/15/17/18,Definition 与 Run 联合判断)。
243
+ *
244
+ * 判定顺序(首个命中生效):
245
+ * 3. run waiting_permission → needs_you/user_input(授权权限)
246
+ * 4. run waiting_user → needs_you/user_input(回答问题)
247
+ * 7. 产品方案 A:run succeeded → terminal(定时/触发成功默认归档,不进工作台待验收)
248
+ * 15. definition paused/disabled → paused
249
+ * 10. run scheduled/queued/preparing/running → peer_advancing
250
+ * 17. run 终态(succeeded/failed/cancelled/skipped/timed_out/blocked)→ terminal
251
+ * 18. definition completed/archived → terminal
252
+ *
253
+ * 说明:GoalPlan 的 result_ready + 一键验收不套用到 Automation。
254
+ * Automation 仅在 needs_you(权限/提问)时必须进工作台;成功 Run 不进「结果待验收」。
255
+ */
256
+ export declare function projectAutomationRun(snapshot: AutomationProjectionSnapshot): TaskOverviewItem;
257
+ /**
258
+ * 后台 shell 线程 → 工作台「Peer 正在推进」卡片。
259
+ * 运行中进 peer_advancing;已结束但短暂可观察时仍投影为 terminal,供右侧面板回看。
260
+ */
261
+ export declare function projectShellBackgroundTask(snapshot: ShellBackgroundProjectionSnapshot): TaskOverviewItem;
@@ -0,0 +1,447 @@
1
+ /**
2
+ * TaskOverview 行动权投影契约 —— Peer 2.0 阶段 0 协议层。
3
+ *
4
+ * 设计依据:peer-knowledge design/product/peer-2-0-gap-analysis.md §11。
5
+ *
6
+ * 核心命题:首页/任务/历史三页面不是第二份任务数据库,而是
7
+ * GoalPlan / GoalRunner / Automation 三套状态机按「下一步行动权」
8
+ * 形成的动态投影。本文件是该投影的唯一事实来源(single source of truth)。
9
+ *
10
+ * 治理红线(AGENTS.md):
11
+ * - renderer 只消费本模块输出的投影结果,不自行解析 GoalPlanStatus/
12
+ * AutomationRunStatus 推断行动权,不在前端维护任务状态副本。
13
+ * - 映射规则见 §11.3:判定顺序从上到下、首个命中生效;本模块用
14
+ * projectGoalPlan / projectAutomationRun 两个纯函数实现该顺序。
15
+ *
16
+ * 输入设计:投影函数接收最小输入快照(ProjectionSnapshot),而非完整
17
+ * GoalPlan / AutomationRun 对象——调用方(main 进程聚合层)负责从
18
+ * 存储读出字段后组装快照,renderer 只拿到投影产物。这样契约边界清晰,
19
+ * 且测试无需构造巨型 fixture。
20
+ */
21
+ import { projectGoalTiming } from "./goal.js";
22
+ /** Conversation → 无计划讨论态(工作台动线 §15)。 */
23
+ /**
24
+ * 会话是否对首页「正在讨论」可见(未读)。
25
+ * - 无 updatedAt:不可见
26
+ * - 无 lastReadAt:视为未读(兼容旧数据;打开后写入水位)
27
+ * - updatedAt > lastReadAt:未读
28
+ */
29
+ export function isConversationUnreadForDiscussion(snapshot) {
30
+ const updatedAt = typeof snapshot.updatedAt === 'string' && snapshot.updatedAt.trim()
31
+ ? snapshot.updatedAt.trim()
32
+ : '';
33
+ if (!updatedAt)
34
+ return false;
35
+ const updatedMs = Date.parse(updatedAt);
36
+ if (!Number.isFinite(updatedMs))
37
+ return false;
38
+ const lastReadAt = typeof snapshot.lastReadAt === 'string' && snapshot.lastReadAt.trim()
39
+ ? snapshot.lastReadAt.trim()
40
+ : '';
41
+ if (!lastReadAt)
42
+ return true;
43
+ const readMs = Date.parse(lastReadAt);
44
+ if (!Number.isFinite(readMs))
45
+ return true;
46
+ return updatedMs > readMs;
47
+ }
48
+ export function projectConversation(snapshot) {
49
+ const modelLabel = typeof snapshot.modelLabel === 'string' && snapshot.modelLabel.trim()
50
+ ? snapshot.modelLabel.trim()
51
+ : undefined;
52
+ const providerLabel = typeof snapshot.providerLabel === 'string' && snapshot.providerLabel.trim()
53
+ ? snapshot.providerLabel.trim()
54
+ : undefined;
55
+ return {
56
+ taskId: snapshot.conversationId,
57
+ source: 'conversation',
58
+ actionRight: 'paused',
59
+ nextAction: 'continue_task',
60
+ title: snapshot.title,
61
+ ...(snapshot.workspaceLabel ? { workspaceLabel: snapshot.workspaceLabel } : {}),
62
+ // 「正在讨论」是未读入口,不是任务态;状态文案用「有未读」。
63
+ statusLabel: '有未读',
64
+ ...(snapshot.updatedAt ? { lastActiveAt: snapshot.updatedAt } : {}),
65
+ ...(modelLabel ? { modelLabel } : {}),
66
+ ...(providerLabel ? { providerLabel } : {}),
67
+ actionLabel: '打开',
68
+ conversationId: snapshot.conversationId,
69
+ };
70
+ }
71
+ /**
72
+ * GoalPlan 行动权投影(§11.3 rule 1-16 的 GoalPlan/Runner 分支)。
73
+ *
74
+ * 判定顺序(首个命中生效),Plan 态优先于 Runner 态(§11.4 决策 1):
75
+ * 1. plan awaiting_approval → needs_you/plan_approval
76
+ * 2. plan drafting → needs_you/plan_approval
77
+ * 4. runner waiting_user → needs_you/user_input
78
+ * 5. runner blocked | budget_exhausted → needs_you/decision
79
+ * 6. plan completed 且未验收 → result_ready
80
+ * 8. plan executing → peer_advancing
81
+ * 9. runner running/compacting/resuming/exploring → peer_advancing
82
+ * 11. plan approved/accepted(Runner 未启动)→ peer_advancing
83
+ * 12. plan paused → paused
84
+ * 13. runner paused → paused
85
+ * 14. runner idle 且 plan executing → paused(异常态,提示诊断)
86
+ * 16. plan completed(已验收)/cancelled/failed → terminal
87
+ *
88
+ * 注意 rule 14 在 rule 8 之前不会命中(executing 已被 rule 8 拦截),
89
+ * 因此实现时将 rule 14 放在 rule 8 之后、仅当 runner 显式 idle 时生效。
90
+ */
91
+ export function projectGoalPlan(snapshot, options) {
92
+ const decision = decideGoalPlan(snapshot);
93
+ const projectedTiming = projectGoalTiming(snapshot.timing, options?.nowMs);
94
+ const durationMs = typeof projectedTiming?.activeMs === 'number' && Number.isFinite(projectedTiming.activeMs)
95
+ ? Math.max(0, Math.floor(projectedTiming.activeMs))
96
+ : undefined;
97
+ const modelLabel = typeof snapshot.modelLabel === 'string' && snapshot.modelLabel.trim()
98
+ ? snapshot.modelLabel.trim()
99
+ : undefined;
100
+ const providerLabel = typeof snapshot.providerLabel === 'string' && snapshot.providerLabel.trim()
101
+ ? snapshot.providerLabel.trim()
102
+ : undefined;
103
+ // 完成时间:优先 timing.completedAt;终态计划缺省时回落 updatedAt,供待验收卡片展示。
104
+ const completedAtFromTiming = typeof projectedTiming?.completedAt === 'string' && projectedTiming.completedAt
105
+ ? projectedTiming.completedAt
106
+ : typeof snapshot.timing?.completedAt === 'string' && snapshot.timing.completedAt.trim()
107
+ ? snapshot.timing.completedAt.trim()
108
+ : undefined;
109
+ const isTerminalPlan = snapshot.status === 'completed'
110
+ || snapshot.status === 'failed'
111
+ || snapshot.status === 'cancelled';
112
+ const completedAt = completedAtFromTiming
113
+ ?? (isTerminalPlan && typeof snapshot.updatedAt === 'string' && snapshot.updatedAt.trim()
114
+ ? snapshot.updatedAt.trim()
115
+ : undefined);
116
+ return {
117
+ taskId: snapshot.planId,
118
+ source: 'goal_plan',
119
+ actionRight: decision.actionRight,
120
+ ...(decision.needsYouReason ? { needsYouReason: decision.needsYouReason } : {}),
121
+ nextAction: decision.nextAction,
122
+ title: snapshot.title,
123
+ ...(snapshot.workspaceLabel ? { workspaceLabel: snapshot.workspaceLabel } : {}),
124
+ statusLabel: decision.statusLabel,
125
+ ...(snapshot.progress ? { planProgress: snapshot.progress } : {}),
126
+ ...(snapshot.planSteps && snapshot.planSteps.length > 0
127
+ ? { planSteps: snapshot.planSteps }
128
+ : {}),
129
+ ...(snapshot.updatedAt ? { lastActiveAt: snapshot.updatedAt } : {}),
130
+ ...(completedAt ? { completedAt } : {}),
131
+ ...(durationMs !== undefined ? { durationMs } : {}),
132
+ ...(modelLabel ? { modelLabel } : {}),
133
+ ...(providerLabel ? { providerLabel } : {}),
134
+ actionLabel: decision.actionLabel,
135
+ ...(snapshot.conversationId ? { conversationId: snapshot.conversationId } : {}),
136
+ };
137
+ }
138
+ function decideGoalPlan(snapshot) {
139
+ const { status, runnerStatus, accepted } = snapshot;
140
+ // request_user_input 是明确的当前行动权事实。即使旧记录或竞态窗口里
141
+ // plan 已先写成 completed,只要 Runner 仍在 waiting_user,就必须先让用户回答,
142
+ // 不能把尚未消费的问题误投影成结果待验收。
143
+ if (status === 'completed' && runnerStatus === 'waiting_user') {
144
+ return {
145
+ actionRight: 'needs_you',
146
+ needsYouReason: 'user_input',
147
+ nextAction: 'answer_question',
148
+ statusLabel: '等待你的选择',
149
+ actionLabel: '回答 →',
150
+ };
151
+ }
152
+ // rule 16 first: 其他终态计划优先于 runner 残留态
153
+ // (历史 failed/cancelled/completed 上的 runner.blocked 不得再进 needs_you)
154
+ if (status === 'completed' && accepted !== true) {
155
+ // rule 6: 完成且未验收 → 结果就绪
156
+ return {
157
+ actionRight: 'result_ready',
158
+ nextAction: 'review_result',
159
+ statusLabel: '等待验收',
160
+ actionLabel: '验收 →',
161
+ };
162
+ }
163
+ if (status === 'completed' || status === 'cancelled' || status === 'failed') {
164
+ return {
165
+ actionRight: 'terminal',
166
+ nextAction: 'none',
167
+ statusLabel: status === 'completed' ? '已验收' : status === 'cancelled' ? '已取消' : '已失败',
168
+ actionLabel: '查看 →',
169
+ };
170
+ }
171
+ // rule 1: 计划审批门
172
+ if (status === 'awaiting_approval') {
173
+ return {
174
+ actionRight: 'needs_you',
175
+ needsYouReason: 'plan_approval',
176
+ nextAction: 'approve_plan',
177
+ statusLabel: '待批准计划',
178
+ actionLabel: '批准 →',
179
+ };
180
+ }
181
+ // rule 2: intake 未决
182
+ if (status === 'drafting') {
183
+ return {
184
+ actionRight: 'needs_you',
185
+ needsYouReason: 'plan_approval',
186
+ nextAction: 'confirm_scope',
187
+ statusLabel: '待确认目标',
188
+ actionLabel: '确认 →',
189
+ };
190
+ }
191
+ const planStillActive = status === 'executing' || status === 'accepted' || status === 'approved';
192
+ // rule 4: Runner 已明确把行动权交给用户,不再展示为“Peer 正在推进”。
193
+ if (planStillActive && runnerStatus === 'waiting_user') {
194
+ return {
195
+ actionRight: 'needs_you',
196
+ needsYouReason: 'user_input',
197
+ nextAction: 'answer_question',
198
+ statusLabel: '等待你的选择',
199
+ actionLabel: '回答 →',
200
+ };
201
+ }
202
+ // rule 5: 仅活跃计划上的 Runner 实时求助才进 needs_you
203
+ // (plan 必须仍在推进:executing/accepted/approved;历史僵尸 blocked 不进)
204
+ if (planStillActive &&
205
+ (runnerStatus === 'blocked' || runnerStatus === 'budget_exhausted')) {
206
+ return {
207
+ actionRight: 'needs_you',
208
+ needsYouReason: 'decision',
209
+ nextAction: 'decide_blocked',
210
+ statusLabel: runnerStatus === 'budget_exhausted' ? '预算已耗尽' : '执行受阻',
211
+ actionLabel: '决策 →',
212
+ };
213
+ }
214
+ // rule 12: 计划暂停(先于 runner 态判断,用户主动暂停优先)
215
+ if (status === 'paused') {
216
+ return {
217
+ actionRight: 'paused',
218
+ nextAction: 'resume',
219
+ statusLabel: '已暂停',
220
+ actionLabel: '继续 →',
221
+ };
222
+ }
223
+ // rule 14: 异常态 —— runner 空闲但计划仍在 executing(Runner 崩溃/丢消息)
224
+ if (status === 'executing' && runnerStatus === 'idle') {
225
+ return {
226
+ actionRight: 'paused',
227
+ nextAction: 'inspect',
228
+ statusLabel: '推进中断',
229
+ actionLabel: '诊断 →',
230
+ };
231
+ }
232
+ // rule 13: Runner 暂停
233
+ if (runnerStatus === 'paused') {
234
+ return {
235
+ actionRight: 'paused',
236
+ nextAction: 'resume',
237
+ statusLabel: '已暂停',
238
+ actionLabel: '继续 →',
239
+ };
240
+ }
241
+ // rule 9: Runner 活跃中
242
+ if (runnerStatus === 'running' ||
243
+ runnerStatus === 'compacting_context' ||
244
+ runnerStatus === 'resuming_after_compaction' ||
245
+ runnerStatus === 'exploring') {
246
+ return {
247
+ actionRight: 'peer_advancing',
248
+ nextAction: 'none',
249
+ statusLabel: 'Peer 正在推进',
250
+ actionLabel: '查看 →',
251
+ };
252
+ }
253
+ // rule 8: 计划已批准正在推进(runner 态缺失时的兜底)
254
+ if (status === 'executing') {
255
+ return {
256
+ actionRight: 'peer_advancing',
257
+ nextAction: 'none',
258
+ statusLabel: 'Peer 正在推进',
259
+ actionLabel: '查看 →',
260
+ };
261
+ }
262
+ // rule 11: 已批准/已接受,Runner 未启动
263
+ if (status === 'approved' || status === 'accepted') {
264
+ return {
265
+ actionRight: 'peer_advancing',
266
+ nextAction: 'none',
267
+ statusLabel: '排队待执行',
268
+ actionLabel: '查看 →',
269
+ };
270
+ }
271
+ // 兜底:未知组合视为 peer_advancing(防御性,不阻塞首页渲染)
272
+ return {
273
+ actionRight: 'peer_advancing',
274
+ nextAction: 'none',
275
+ statusLabel: 'Peer 正在推进',
276
+ actionLabel: '查看 →',
277
+ };
278
+ }
279
+ /**
280
+ * Automation 行动权投影(§11.3 rule 3-4/7/10/15/17/18,Definition 与 Run 联合判断)。
281
+ *
282
+ * 判定顺序(首个命中生效):
283
+ * 3. run waiting_permission → needs_you/user_input(授权权限)
284
+ * 4. run waiting_user → needs_you/user_input(回答问题)
285
+ * 7. 产品方案 A:run succeeded → terminal(定时/触发成功默认归档,不进工作台待验收)
286
+ * 15. definition paused/disabled → paused
287
+ * 10. run scheduled/queued/preparing/running → peer_advancing
288
+ * 17. run 终态(succeeded/failed/cancelled/skipped/timed_out/blocked)→ terminal
289
+ * 18. definition completed/archived → terminal
290
+ *
291
+ * 说明:GoalPlan 的 result_ready + 一键验收不套用到 Automation。
292
+ * Automation 仅在 needs_you(权限/提问)时必须进工作台;成功 Run 不进「结果待验收」。
293
+ */
294
+ export function projectAutomationRun(snapshot) {
295
+ const decision = decideAutomation(snapshot);
296
+ return {
297
+ taskId: snapshot.runId ?? snapshot.automationId,
298
+ source: 'automation',
299
+ actionRight: decision.actionRight,
300
+ ...(decision.needsYouReason ? { needsYouReason: decision.needsYouReason } : {}),
301
+ nextAction: decision.nextAction,
302
+ title: snapshot.title,
303
+ ...(snapshot.workspaceLabel ? { workspaceLabel: snapshot.workspaceLabel } : {}),
304
+ statusLabel: decision.statusLabel,
305
+ ...(snapshot.updatedAt ? { lastActiveAt: snapshot.updatedAt } : {}),
306
+ actionLabel: decision.actionLabel,
307
+ ...(snapshot.conversationId ? { conversationId: snapshot.conversationId } : {}),
308
+ };
309
+ }
310
+ /**
311
+ * 后台 shell 线程 → 工作台「Peer 正在推进」卡片。
312
+ * 运行中进 peer_advancing;已结束但短暂可观察时仍投影为 terminal,供右侧面板回看。
313
+ */
314
+ export function projectShellBackgroundTask(snapshot) {
315
+ const command = String(snapshot.command ?? '').trim() || '后台 shell 任务';
316
+ const title = command.length > 72 ? `${command.slice(0, 71)}…` : command;
317
+ const status = String(snapshot.status ?? '').trim().toLowerCase();
318
+ const running = status === 'running' || status === '';
319
+ const lastActiveAt = (typeof snapshot.completedAt === 'string' && snapshot.completedAt) ||
320
+ (typeof snapshot.startedAt === 'string' && snapshot.startedAt) ||
321
+ undefined;
322
+ return {
323
+ taskId: `shell:${snapshot.taskId}`,
324
+ source: 'shell_background',
325
+ actionRight: running ? 'peer_advancing' : 'terminal',
326
+ nextAction: 'open_background_thread',
327
+ title,
328
+ ...(snapshot.workspaceLabel ? { workspaceLabel: snapshot.workspaceLabel } : {}),
329
+ statusLabel: running ? '后台线程运行中' : shellBackgroundTerminalLabel(status),
330
+ ...(lastActiveAt ? { lastActiveAt } : {}),
331
+ actionLabel: running ? '查看线程 →' : '查看 →',
332
+ ...(snapshot.conversationId ? { conversationId: snapshot.conversationId } : {}),
333
+ };
334
+ }
335
+ function shellBackgroundTerminalLabel(status) {
336
+ switch (status) {
337
+ case 'completed':
338
+ return '后台线程已完成';
339
+ case 'failed':
340
+ return '后台线程失败';
341
+ case 'cancelled':
342
+ return '后台线程已停止';
343
+ case 'timed_out':
344
+ return '后台线程超时';
345
+ default:
346
+ return '后台线程';
347
+ }
348
+ }
349
+ function decideAutomation(snapshot) {
350
+ const { definitionStatus, runStatus, accepted } = snapshot;
351
+ // rule 3: 等待权限
352
+ if (runStatus === 'waiting_permission') {
353
+ return {
354
+ actionRight: 'needs_you',
355
+ needsYouReason: 'user_input',
356
+ nextAction: 'grant_permission',
357
+ statusLabel: '等待权限',
358
+ actionLabel: '授权 →',
359
+ };
360
+ }
361
+ // rule 4: 等待用户回答
362
+ if (runStatus === 'waiting_user') {
363
+ return {
364
+ actionRight: 'needs_you',
365
+ needsYouReason: 'user_input',
366
+ nextAction: 'answer_question',
367
+ statusLabel: '等待回答',
368
+ actionLabel: '处理 →',
369
+ };
370
+ }
371
+ // rule 7(方案 A):运行成功 → 直接终态,不进工作台「结果待验收」。
372
+ // Goal 的一键验收闭环不适用于 Automation;成功即视为已结束/可进历史。
373
+ if (runStatus === 'succeeded') {
374
+ return {
375
+ actionRight: 'terminal',
376
+ nextAction: 'none',
377
+ statusLabel: '已完成',
378
+ actionLabel: '查看 →',
379
+ };
380
+ }
381
+ // rule 15: 定义级暂停/禁用
382
+ if (definitionStatus === 'paused' || definitionStatus === 'disabled') {
383
+ return {
384
+ actionRight: 'paused',
385
+ nextAction: 'enable',
386
+ statusLabel: definitionStatus === 'paused' ? '已暂停' : '已禁用',
387
+ actionLabel: '启用 →',
388
+ };
389
+ }
390
+ // rule 10: 运行推进中
391
+ if (runStatus === 'scheduled' ||
392
+ runStatus === 'queued' ||
393
+ runStatus === 'preparing' ||
394
+ runStatus === 'running') {
395
+ return {
396
+ actionRight: 'peer_advancing',
397
+ nextAction: 'none',
398
+ statusLabel: 'Peer 正在推进',
399
+ actionLabel: '查看 →',
400
+ };
401
+ }
402
+ // rule 17: Run 终态(succeeded 已在 rule 7 提前 return)
403
+ if (runStatus === 'failed' ||
404
+ runStatus === 'cancelled' ||
405
+ runStatus === 'skipped' ||
406
+ runStatus === 'timed_out' ||
407
+ runStatus === 'blocked') {
408
+ return {
409
+ actionRight: 'terminal',
410
+ nextAction: 'none',
411
+ statusLabel: automationTerminalLabel(runStatus),
412
+ actionLabel: '查看 →',
413
+ };
414
+ }
415
+ // rule 18: 定义级终态
416
+ if (definitionStatus === 'completed' || definitionStatus === 'archived') {
417
+ return {
418
+ actionRight: 'terminal',
419
+ nextAction: 'none',
420
+ statusLabel: definitionStatus === 'completed' ? '已完成' : '已归档',
421
+ actionLabel: '查看 →',
422
+ };
423
+ }
424
+ // 定义 active/draft 且尚无 Run:待机调度中
425
+ return {
426
+ actionRight: 'peer_advancing',
427
+ nextAction: 'none',
428
+ statusLabel: '调度待机',
429
+ actionLabel: '查看 →',
430
+ };
431
+ }
432
+ function automationTerminalLabel(status) {
433
+ switch (status) {
434
+ case 'succeeded':
435
+ return '已完成';
436
+ case 'failed':
437
+ return '已失败';
438
+ case 'cancelled':
439
+ return '已取消';
440
+ case 'skipped':
441
+ return '已跳过';
442
+ case 'timed_out':
443
+ return '已超时';
444
+ case 'blocked':
445
+ return '已阻塞';
446
+ }
447
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peer-agent/protocol",
3
- "version": "0.0.2-beta.4",
3
+ "version": "0.0.2",
4
4
  "description": "Shared host-neutral protocol contracts for Peer Agent runtimes and clients.",
5
5
  "type": "module",
6
6
  "license": "MIT",