-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
192 lines (166 loc) · 5.85 KB
/
Copy pathindex.ts
File metadata and controls
192 lines (166 loc) · 5.85 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import path from "node:path";
import type { Plugin } from "@opencode-ai/plugin";
import { createNotificationScheduler } from "@/notification-scheduler";
import { loadConfig, ConfigError } from "@/config/loader";
import { createResolvedConfig } from "@/config/resolver";
export const SimpleNotificationPlugin: Plugin = async ({ client, directory }) => {
const config = await loadConfig(directory).catch(async (error) => {
const message =
error instanceof ConfigError
? `Notification plugin config error: ${error.message}`
: `Notification plugin failed to load: ${error instanceof Error ? error.message : String(error)}`;
await client.tui.showToast({ body: { message, variant: "error" } });
throw error;
});
const resolvedConfig = createResolvedConfig(config);
const scheduler = createNotificationScheduler(resolvedConfig);
// Tracks sessions where assistant has responded since last user message
const activeSessions = new Set<string>();
// Tracks sessions interrupted explicitly by user command
const interruptedSessions = new Set<string>();
const cancelForSession = (sessionId: string) => {
scheduler.cancelForSession(sessionId);
activeSessions.delete(sessionId);
};
const markSessionInterrupted = (sessionId: string) => {
interruptedSessions.add(sessionId);
cancelForSession(sessionId);
};
const isDeniedReply = (response: unknown) => {
const normalized = String(response ?? "")
.trim()
.toLowerCase();
return (
normalized.includes("deny") ||
normalized.includes("reject") ||
normalized.includes("interrupt") ||
normalized.includes("cancel") ||
normalized.includes("abort") ||
normalized === "esc"
);
};
return {
event: async ({ event }) => {
switch (event.type) {
case "session.idle": {
const sessionId = event.properties.sessionID;
if (interruptedSessions.has(sessionId)) {
interruptedSessions.delete(sessionId);
activeSessions.delete(sessionId);
return;
}
if (!activeSessions.has(sessionId)) return;
const session = await client.session.get({ path: { id: sessionId } }).catch(() => null);
const title = session?.data?.title ?? sessionId;
scheduler.schedule(sessionId, "Response ready", title, "session.idle");
activeSessions.delete(sessionId);
return;
}
case "session.error": {
const sessionId = event.properties.sessionID;
if (!sessionId) return;
const isAborted = event.properties.error?.name === "MessageAbortedError";
if (isAborted) {
markSessionInterrupted(sessionId);
return;
}
const session = await client.session.get({ path: { id: sessionId } }).catch(() => null);
const title = session?.data?.title ?? sessionId;
scheduler.schedule(sessionId, "Session error", title, "session.error");
return;
}
case "command.executed": {
const sessionId = event.properties.sessionID;
const commandName = event.properties.name;
if (commandName === "session.interrupt") {
markSessionInterrupted(sessionId);
}
return;
}
// @ts-ignore: SDK v1 doesn't have permission types yet
case "permission.asked": {
const sessionId = (event as { properties: { sessionID: string } }).properties.sessionID;
const session = await client.session.get({ path: { id: sessionId } }).catch(() => null);
const projectName = path.basename(session?.data?.directory ?? "");
scheduler.schedule(
sessionId,
"Permission Asked",
`${session?.data?.title} in ${projectName} needs permission`,
"permission.asked",
);
return;
}
// @ts-ignore: SDK v1 doesn't have question types yet
case "question.asked": {
const sessionId = (event as { properties: { sessionID: string } }).properties.sessionID;
const session = await client.session.get({ path: { id: sessionId } }).catch(() => null);
const projectName = path.basename(session?.data?.directory ?? "");
scheduler.schedule(
sessionId,
"Question",
`${session?.data?.title} in ${projectName} has a question`,
"question.asked",
);
return;
}
// @ts-ignore: SDK v1 doesn't have permission types yet
case "permission.replied":
// @ts-ignore: SDK v1 doesn't have question types yet
case "question.replied": {
const sessionId = (event as { properties: { sessionID: string } }).properties.sessionID;
const response = (event as { properties: { response?: unknown } }).properties.response;
if (isDeniedReply(response)) {
markSessionInterrupted(sessionId);
} else {
cancelForSession(sessionId);
}
return;
}
case "message.updated": {
const info = event.properties.info;
if (info.role === "assistant") {
if (info.error?.name === "MessageAbortedError") {
markSessionInterrupted(info.sessionID);
return;
}
activeSessions.add(info.sessionID);
} else if (info.role === "user") {
if (!info.agent && !info.model) {
interruptedSessions.delete(info.sessionID);
cancelForSession(info.sessionID);
}
}
return;
}
case "message.part.updated": {
const part = event.properties.part as {
sessionID: string;
type: string;
state?: { status?: string; error?: string };
};
const isDismissed =
part.state?.status === "error" &&
part.state?.error?.toLowerCase().includes("dismissed");
if (isDismissed && ["tool", "question", "permission"].includes(part.type)) {
markSessionInterrupted(part.sessionID);
}
return;
}
case "session.status": {
if (event.properties.status.type === "busy") {
scheduler.cancelForSession(event.properties.sessionID);
}
return;
}
case "tui.prompt.append":
case "tui.command.execute":
return;
}
},
destroy: () => {
scheduler.cancelAll();
activeSessions.clear();
interruptedSessions.clear();
},
};
};