-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathnotify.ts
More file actions
78 lines (67 loc) · 1.93 KB
/
notify.ts
File metadata and controls
78 lines (67 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/**
* Subagent completion notifications (extension)
*/
import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
import { buildCompletionKey, getGlobalSeenMap, markSeenWithTtl } from "./completion-dedupe.js";
interface ChainStepResult {
agent: string;
output: string;
success: boolean;
}
interface SubagentResult {
id: string | null;
agent: string | null;
success: boolean;
summary: string;
exitCode: number;
timestamp: number;
sessionFile?: string;
shareUrl?: string;
gistUrl?: string;
shareError?: string;
results?: ChainStepResult[];
taskIndex?: number;
totalTasks?: number;
}
export default function registerSubagentNotify(pi: ExtensionAPI): void {
const seen = getGlobalSeenMap("__pi_subagents_notify_seen__");
const ttlMs = 10 * 60 * 1000;
const handleComplete = (data: unknown) => {
const result = data as SubagentResult;
const now = Date.now();
const key = buildCompletionKey(result, "notify");
if (markSeenWithTtl(seen, key, now, ttlMs)) return;
const agent = result.agent ?? "unknown";
const status = result.success ? "completed" : "failed";
const taskInfo =
result.taskIndex !== undefined && result.totalTasks !== undefined
? ` (${result.taskIndex + 1}/${result.totalTasks})`
: "";
const extra: string[] = [];
if (result.shareUrl) {
extra.push(`Session: ${result.shareUrl}`);
} else if (result.shareError) {
extra.push(`Session share error: ${result.shareError}`);
} else if (result.sessionFile) {
extra.push(`Session file: ${result.sessionFile}`);
}
const content = [
`Background task ${status}: **${agent}**${taskInfo}`,
"",
result.summary,
extra.length ? "" : undefined,
extra.length ? extra.join("\n") : undefined,
]
.filter((line) => line !== undefined)
.join("\n");
pi.sendMessage(
{
customType: "subagent-notify",
content,
display: true,
},
{ triggerTurn: true },
);
};
pi.events.on("subagent:complete", handleComplete);
}