@kkcode-app/agent 1.0.10
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.md +139 -0
- package/dist/agent-link.js +102 -0
- package/dist/agent-link.js.map +1 -0
- package/dist/codex-bridge.js +334 -0
- package/dist/codex-bridge.js.map +1 -0
- package/dist/codex-journal.js +46 -0
- package/dist/codex-journal.js.map +1 -0
- package/dist/codex-jsonrpc.js +40 -0
- package/dist/codex-jsonrpc.js.map +1 -0
- package/dist/codex-process.js +171 -0
- package/dist/codex-process.js.map +1 -0
- package/dist/config.js +89 -0
- package/dist/config.js.map +1 -0
- package/dist/index.js +344 -0
- package/dist/index.js.map +1 -0
- package/dist/local-server.js +104 -0
- package/dist/local-server.js.map +1 -0
- package/dist/notify-pair.js +142 -0
- package/dist/notify-pair.js.map +1 -0
- package/dist/plugin.js +214 -0
- package/dist/plugin.js.map +1 -0
- package/dist/protocol.js +9 -0
- package/dist/protocol.js.map +1 -0
- package/dist/service.js +438 -0
- package/dist/service.js.map +1 -0
- package/dist/tunnel.js +311 -0
- package/dist/tunnel.js.map +1 -0
- package/dist/update.js +61 -0
- package/dist/update.js.map +1 -0
- package/package.json +37 -0
- package/src/plugin.ts +294 -0
package/src/plugin.ts
ADDED
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* [INPUT]: 依赖 node:fs 的 appendFile 做追加写日志,依赖 Bun.file 读取配置,依赖 fetch 上报事件
|
|
3
|
+
* [OUTPUT]: 对外提供 KKCodeIOSNotifyPlugin(default export),OpenCode 加载后接收 session/permission/question 事件
|
|
4
|
+
* [POS]: cli/ 的 OpenCode 插件入口,被 src/config.ts 注册、被 package.json exports 暴露;调试日志写入 ~/.config/kkcode/events-debug.log
|
|
5
|
+
* 始终注册事件回调;notify.json 的读取推迟到每次事件触发时(loadConfig),无需重启 OpenCode 即可热加载新 token
|
|
6
|
+
* [PROTOCOL]: 变更时更新此头部,然后检查 CLAUDE.md
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import fs from "node:fs";
|
|
10
|
+
|
|
11
|
+
type AgentConfig = {
|
|
12
|
+
node_id: string;
|
|
13
|
+
node_secret: string;
|
|
14
|
+
service_id: string;
|
|
15
|
+
api_url?: string;
|
|
16
|
+
};
|
|
17
|
+
type NotifyEventType =
|
|
18
|
+
| "session.completed"
|
|
19
|
+
| "sub_session.completed"
|
|
20
|
+
| "session.error"
|
|
21
|
+
| "permission.asked"
|
|
22
|
+
| "question.asked";
|
|
23
|
+
type OpenCodeEvent = {
|
|
24
|
+
type: string;
|
|
25
|
+
properties: {
|
|
26
|
+
sessionID?: string;
|
|
27
|
+
id?: string;
|
|
28
|
+
requestID?: string;
|
|
29
|
+
permission?: string;
|
|
30
|
+
patterns?: string[];
|
|
31
|
+
description?: string;
|
|
32
|
+
message?: string;
|
|
33
|
+
error?: string;
|
|
34
|
+
metadata?: { description?: string };
|
|
35
|
+
questions?: {
|
|
36
|
+
question?: string;
|
|
37
|
+
header?: string;
|
|
38
|
+
options?: { label?: string; description?: string }[];
|
|
39
|
+
}[];
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
type NotifyPayload = {
|
|
43
|
+
event_id: string;
|
|
44
|
+
service_id: string;
|
|
45
|
+
session_id: string;
|
|
46
|
+
type: NotifyEventType;
|
|
47
|
+
project_label: string;
|
|
48
|
+
occurred_at: string;
|
|
49
|
+
session_title?: string;
|
|
50
|
+
summary?: string;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const DEFAULT_API_URL = "https://api.kkcode.app";
|
|
54
|
+
const MAX_NOTIFICATION_CONTEXT_LENGTH = 480;
|
|
55
|
+
const configPath =
|
|
56
|
+
process.env.KKCODE_AGENT_CONFIG ??
|
|
57
|
+
`${process.env.XDG_CONFIG_HOME ?? `${process.env.HOME ?? "~"}/.config`}/kkcode/agent.json`;
|
|
58
|
+
const debugLogPath =
|
|
59
|
+
process.env.KKCODE_DEBUG_LOG ??
|
|
60
|
+
`${process.env.XDG_CONFIG_HOME ?? `${process.env.HOME ?? "~"}/.config`}/kkcode/events-debug.log`;
|
|
61
|
+
|
|
62
|
+
async function debugLog(message: string): Promise<void> {
|
|
63
|
+
try {
|
|
64
|
+
const line = `[${new Date().toISOString()}] ${message}\n`;
|
|
65
|
+
await fs.promises.appendFile(debugLogPath, line);
|
|
66
|
+
} catch {}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
async function loadConfig(): Promise<AgentConfig | null> {
|
|
70
|
+
try {
|
|
71
|
+
const config = (await Bun.file(configPath).json()) as AgentConfig;
|
|
72
|
+
if (!config.node_secret || !config.service_id) return null;
|
|
73
|
+
return config;
|
|
74
|
+
} catch {
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
async function reportEvent(
|
|
80
|
+
config: AgentConfig,
|
|
81
|
+
sessionId: string,
|
|
82
|
+
type: NotifyEventType,
|
|
83
|
+
projectLabel: string,
|
|
84
|
+
summary?: string,
|
|
85
|
+
sessionTitle?: string,
|
|
86
|
+
): Promise<void> {
|
|
87
|
+
const controller = new AbortController();
|
|
88
|
+
const timeout = setTimeout(() => controller.abort(), 3_000);
|
|
89
|
+
try {
|
|
90
|
+
const payload: NotifyPayload = {
|
|
91
|
+
event_id: crypto.randomUUID(),
|
|
92
|
+
service_id: config.service_id,
|
|
93
|
+
session_id: sessionId,
|
|
94
|
+
type,
|
|
95
|
+
project_label: projectLabel,
|
|
96
|
+
occurred_at: new Date().toISOString(),
|
|
97
|
+
};
|
|
98
|
+
if (summary) payload.summary = summary;
|
|
99
|
+
if (sessionTitle) payload.session_title = sessionTitle;
|
|
100
|
+
const apiUrl = `${config.api_url ?? DEFAULT_API_URL}/v1/notification-events`;
|
|
101
|
+
const response = await fetch(apiUrl, {
|
|
102
|
+
method: "POST",
|
|
103
|
+
signal: controller.signal,
|
|
104
|
+
headers: {
|
|
105
|
+
Authorization: `Bearer ${config.node_secret}`,
|
|
106
|
+
"Content-Type": "application/json",
|
|
107
|
+
},
|
|
108
|
+
body: JSON.stringify(payload),
|
|
109
|
+
});
|
|
110
|
+
if (!response.ok) throw new Error(`Notification API returned ${response.status}`);
|
|
111
|
+
} finally {
|
|
112
|
+
clearTimeout(timeout);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function compactText(value: unknown, maxLength = MAX_NOTIFICATION_CONTEXT_LENGTH): string {
|
|
117
|
+
return typeof value === "string" ? value.replace(/\s+/gu, " ").trim().slice(0, maxLength) : "";
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function describePermission(properties: OpenCodeEvent["properties"]): string {
|
|
121
|
+
const permission = compactText(properties.permission, 80) || "action";
|
|
122
|
+
const description = compactText(properties.description ?? properties.metadata?.description);
|
|
123
|
+
const patterns = (Array.isArray(properties.patterns) ? properties.patterns : [])
|
|
124
|
+
.map((pattern) => compactText(pattern, 160))
|
|
125
|
+
.filter(Boolean)
|
|
126
|
+
.slice(0, 3)
|
|
127
|
+
.join("; ");
|
|
128
|
+
return compactText(
|
|
129
|
+
[
|
|
130
|
+
`Permission: ${permission}`,
|
|
131
|
+
description && `Action: ${description}`,
|
|
132
|
+
patterns && `Scope: ${patterns}`,
|
|
133
|
+
]
|
|
134
|
+
.filter(Boolean)
|
|
135
|
+
.join("\n"),
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function describeQuestion(properties: OpenCodeEvent["properties"]): string {
|
|
140
|
+
const question = properties.questions?.[0];
|
|
141
|
+
if (!question) return "KKCode is waiting for your answer";
|
|
142
|
+
const header = compactText(question.header, 100);
|
|
143
|
+
const prompt = compactText(question.question, 300);
|
|
144
|
+
const options = (Array.isArray(question.options) ? question.options : [])
|
|
145
|
+
.map((option) => compactText(option.label, 80))
|
|
146
|
+
.filter(Boolean)
|
|
147
|
+
.slice(0, 4)
|
|
148
|
+
.join(", ");
|
|
149
|
+
return compactText(
|
|
150
|
+
[
|
|
151
|
+
header && `Topic: ${header}`,
|
|
152
|
+
prompt && `Question: ${prompt}`,
|
|
153
|
+
options && `Options: ${options}`,
|
|
154
|
+
]
|
|
155
|
+
.filter(Boolean)
|
|
156
|
+
.join("\n"),
|
|
157
|
+
);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function describeError(properties: OpenCodeEvent["properties"]): string | undefined {
|
|
161
|
+
const detail = compactText(properties.error ?? properties.message ?? properties.description);
|
|
162
|
+
return detail || undefined;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export const KKCodeIOSNotifyPlugin = async ({ client, directory, project }: any) => {
|
|
166
|
+
const projectLabel =
|
|
167
|
+
project?.name || directory?.split(/[\\/]/u).filter(Boolean).at(-1) || "KKCode";
|
|
168
|
+
const erroredSessions = new Set<string>();
|
|
169
|
+
const notifiedPermission = new Set<string>();
|
|
170
|
+
const notifiedQuestion = new Set<string>();
|
|
171
|
+
|
|
172
|
+
await debugLog(`plugin loaded | project=${projectLabel} | config=${configPath}`);
|
|
173
|
+
|
|
174
|
+
async function getSessionInfo(
|
|
175
|
+
sessionId: string,
|
|
176
|
+
): Promise<{ title?: string; isSubSession: boolean }> {
|
|
177
|
+
try {
|
|
178
|
+
const result = await client.session.get({ path: { id: sessionId } });
|
|
179
|
+
const title = result?.data?.title?.trim() || undefined;
|
|
180
|
+
const parentID = result?.data?.parentID || undefined;
|
|
181
|
+
return { title, isSubSession: !!parentID };
|
|
182
|
+
} catch (err) {
|
|
183
|
+
await debugLog(`getSessionInfo FAILED | session=${sessionId} | ${err}`);
|
|
184
|
+
}
|
|
185
|
+
return { title: undefined, isSubSession: false };
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
return {
|
|
189
|
+
event: async ({ event }: { event: OpenCodeEvent }) => {
|
|
190
|
+
const caredTypes = new Set([
|
|
191
|
+
"session.idle",
|
|
192
|
+
"session.error",
|
|
193
|
+
"permission.asked",
|
|
194
|
+
"question.asked",
|
|
195
|
+
]);
|
|
196
|
+
if (!caredTypes.has(event.type)) return;
|
|
197
|
+
await debugLog(`EVENT type=${event.type} | props=${JSON.stringify(event.properties)}`);
|
|
198
|
+
const config = await loadConfig();
|
|
199
|
+
if (!config) {
|
|
200
|
+
await debugLog("config missing or unreadable, skipping event");
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
try {
|
|
204
|
+
if (event.type === "session.idle") {
|
|
205
|
+
const sessionId = event.properties.sessionID;
|
|
206
|
+
if (!sessionId) {
|
|
207
|
+
await debugLog("session.idle SKIP: no sessionID");
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
if (erroredSessions.delete(sessionId)) {
|
|
211
|
+
await debugLog(`session.idle SKIP: ${sessionId} was errored, suppressed`);
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
const info = await getSessionInfo(sessionId);
|
|
215
|
+
const eventType: NotifyEventType = info.isSubSession
|
|
216
|
+
? "sub_session.completed"
|
|
217
|
+
: "session.completed";
|
|
218
|
+
await reportEvent(config, sessionId, eventType, projectLabel, undefined, info.title);
|
|
219
|
+
await debugLog(`session.idle REPORTED | ${sessionId} | type=${eventType}`);
|
|
220
|
+
} else if (event.type === "session.error") {
|
|
221
|
+
const sessionId = event.properties.sessionID;
|
|
222
|
+
if (sessionId) erroredSessions.add(sessionId);
|
|
223
|
+
const info = sessionId ? await getSessionInfo(sessionId) : { title: undefined };
|
|
224
|
+
if (sessionId) {
|
|
225
|
+
await reportEvent(
|
|
226
|
+
config,
|
|
227
|
+
sessionId,
|
|
228
|
+
"session.error",
|
|
229
|
+
projectLabel,
|
|
230
|
+
describeError(event.properties),
|
|
231
|
+
info.title,
|
|
232
|
+
);
|
|
233
|
+
await debugLog(`session.error REPORTED | ${sessionId}`);
|
|
234
|
+
} else {
|
|
235
|
+
await debugLog("session.error SKIP: no sessionID");
|
|
236
|
+
}
|
|
237
|
+
} else if (event.type === "permission.asked") {
|
|
238
|
+
const sessionId = event.properties.sessionID;
|
|
239
|
+
const requestKey = `${sessionId}:${event.properties.id ?? event.properties.requestID ?? ""}`;
|
|
240
|
+
if (!sessionId || notifiedPermission.has(requestKey)) {
|
|
241
|
+
await debugLog(
|
|
242
|
+
`permission.asked SKIP | key=${requestKey} | dup=${notifiedPermission.has(requestKey)}`,
|
|
243
|
+
);
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
notifiedPermission.add(requestKey);
|
|
247
|
+
const info = await getSessionInfo(sessionId);
|
|
248
|
+
await reportEvent(
|
|
249
|
+
config,
|
|
250
|
+
sessionId,
|
|
251
|
+
"permission.asked",
|
|
252
|
+
projectLabel,
|
|
253
|
+
describePermission(event.properties),
|
|
254
|
+
info.title,
|
|
255
|
+
);
|
|
256
|
+
await debugLog(`permission.asked REPORTED | ${sessionId} | key=${requestKey}`);
|
|
257
|
+
} else if (event.type === "question.asked") {
|
|
258
|
+
const sessionId = event.properties.sessionID;
|
|
259
|
+
const requestKey = `${sessionId}:${event.properties.id ?? event.properties.requestID ?? ""}`;
|
|
260
|
+
if (!sessionId || notifiedQuestion.has(requestKey)) {
|
|
261
|
+
await debugLog(
|
|
262
|
+
`question.asked SKIP | key=${requestKey} | dup=${notifiedQuestion.has(requestKey)}`,
|
|
263
|
+
);
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
notifiedQuestion.add(requestKey);
|
|
267
|
+
const info = await getSessionInfo(sessionId);
|
|
268
|
+
await reportEvent(
|
|
269
|
+
config,
|
|
270
|
+
sessionId,
|
|
271
|
+
"question.asked",
|
|
272
|
+
projectLabel,
|
|
273
|
+
describeQuestion(event.properties),
|
|
274
|
+
info.title,
|
|
275
|
+
);
|
|
276
|
+
await debugLog(`question.asked REPORTED | ${sessionId} | key=${requestKey}`);
|
|
277
|
+
}
|
|
278
|
+
} catch (error) {
|
|
279
|
+
const msg = error instanceof Error ? error.message : String(error);
|
|
280
|
+
await debugLog(`HANDLER ERROR | type=${event.type} | ${msg}`);
|
|
281
|
+
await client.app.log({
|
|
282
|
+
body: {
|
|
283
|
+
service: "kkcode",
|
|
284
|
+
level: "warn",
|
|
285
|
+
message: "Could not report KKCode session notification.",
|
|
286
|
+
extra: { error: msg },
|
|
287
|
+
},
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
},
|
|
291
|
+
};
|
|
292
|
+
};
|
|
293
|
+
|
|
294
|
+
export default KKCodeIOSNotifyPlugin;
|