-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathnotify.js
More file actions
69 lines (57 loc) · 2.32 KB
/
notify.js
File metadata and controls
69 lines (57 loc) · 2.32 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
export const DayAppHooksPlugin = async ({ project }) => {
const token = process.env.DAY_APP_TOKEN?.trim();
if (!token) return {};
const title = `Opencode ${project?.id}`;
const send = async (body) => {
if (!body?.trim()) return;
const t = encodeURIComponent(title);
const b = encodeURIComponent(body.trim());
const url = `https://api.day.app/${token}/${t}/${b}`;
try {
await fetch(url);
} catch { }
};
// ===== Session statistics =====
let messageContents = "";
let usage = {
cost: 0,
input: 0,
output: 0,
reasoning: 0,
cacheRead: 0,
cacheWrite: 0,
};
return {
event: async ({ event }) => {
if (event.type === "message.part.updated" && event.properties?.part) {
const { part } = event.properties;
if (part.type === "text" && part.messageID) {
messageContents = part.text;
}
// ===== Step settlement (only statistical point) =====
if (part.type === "step-finish") {
// cost
usage.cost += part.cost ?? 0;
// tokens
if (part.tokens) {
usage.input += part.tokens.input ?? 0;
usage.output += part.tokens.output ?? 0;
usage.reasoning += part.tokens.reasoning ?? 0;
if (part.tokens.cache) {
usage.cacheRead += part.tokens.cache.read ?? 0;
usage.cacheWrite += part.tokens.cache.write ?? 0;
}
}
}
}
// ===== Session end unified notification =====
if (event.type === "session.idle") {
const summary = `✅ ${messageContents || "Session completed successfully"}\n\n💰 Cost: ${usage.cost}\n🧮 Tokens:\n - Input: ${usage.input}\n - Output: ${usage.output}\n - Reasoning: ${usage.reasoning}\n - Cache Read: ${usage.cacheRead}\n - Cache Write: ${usage.cacheWrite}`.trim();
await send(summary);
}
},
"permission.ask": async (input) => {
await send(`Need Perission: ${input.type}`);
},
};
};