@kkcode-app/agent 1.1.3 → 2.0.0
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 +40 -99
- package/dist/agent-link.js +55 -93
- package/dist/agent-link.js.map +1 -1
- package/dist/config.js +101 -69
- package/dist/config.js.map +1 -1
- package/dist/crypto.js +151 -0
- package/dist/crypto.js.map +1 -0
- package/dist/entry.js +5 -9
- package/dist/entry.js.map +1 -1
- package/dist/index.js +71 -83
- package/dist/index.js.map +1 -1
- package/dist/local-server.js +58 -27
- package/dist/local-server.js.map +1 -1
- package/dist/notification-core.js +131 -59
- package/dist/notification-core.js.map +1 -1
- package/dist/plugin.js +183 -67
- package/dist/plugin.js.map +1 -1
- package/dist/protocol.js +62 -6
- package/dist/protocol.js.map +1 -1
- package/dist/qr.js +31 -20
- package/dist/qr.js.map +1 -1
- package/dist/service.js +24 -8
- package/dist/service.js.map +1 -1
- package/dist/tunnel.js +406 -200
- package/dist/tunnel.js.map +1 -1
- package/dist/update.js +13 -5
- package/dist/update.js.map +1 -1
- package/package.json +4 -3
- package/src/config.ts +144 -0
- package/src/crypto.ts +193 -0
- package/src/entry.ts +5 -11
- package/src/notification-core.ts +139 -71
- package/src/plugin.ts +161 -71
- package/dist/plugin-v1.js +0 -136
- package/dist/plugin-v1.js.map +0 -1
- package/src/plugin-v1.ts +0 -189
package/dist/plugin-v1.js
DELETED
|
@@ -1,136 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* [INPUT]: 依赖 OpenCode 1 PluginInput/client 与 notification-core 共享上报内核
|
|
3
|
-
* [OUTPUT]: 默认导出 OpenCode 1 函数插件,处理 idle/error/permission/question 事件
|
|
4
|
-
* [POS]: OpenCode 1 插件实现模块,由包入口 entry.ts 以 { id, server } 组合导出;自身不是 exports 解析目标
|
|
5
|
-
* [PROTOCOL]: 变更时更新此头部,然后检查 CLAUDE.md
|
|
6
|
-
*/
|
|
7
|
-
import { compactText, debugLog, errorSummary, reportNotification, } from "./notification-core.js";
|
|
8
|
-
function describePermission(properties) {
|
|
9
|
-
const permission = compactText(properties.permission, 80) || "action";
|
|
10
|
-
const description = compactText(properties.description ?? properties.metadata?.description);
|
|
11
|
-
const patterns = (Array.isArray(properties.patterns) ? properties.patterns : [])
|
|
12
|
-
.map((pattern) => compactText(pattern, 160))
|
|
13
|
-
.filter(Boolean)
|
|
14
|
-
.slice(0, 3)
|
|
15
|
-
.join("; ");
|
|
16
|
-
return compactText([
|
|
17
|
-
`Permission: ${permission}`,
|
|
18
|
-
description && `Action: ${description}`,
|
|
19
|
-
patterns && `Scope: ${patterns}`,
|
|
20
|
-
]
|
|
21
|
-
.filter(Boolean)
|
|
22
|
-
.join("\n"));
|
|
23
|
-
}
|
|
24
|
-
function describeQuestion(properties) {
|
|
25
|
-
const question = properties.questions?.[0];
|
|
26
|
-
if (!question)
|
|
27
|
-
return "KKCode is waiting for your answer";
|
|
28
|
-
const header = compactText(question.header, 100);
|
|
29
|
-
const prompt = compactText(question.question, 300);
|
|
30
|
-
const options = (Array.isArray(question.options) ? question.options : [])
|
|
31
|
-
.map((option) => compactText(option.label, 80))
|
|
32
|
-
.filter(Boolean)
|
|
33
|
-
.slice(0, 4)
|
|
34
|
-
.join(", ");
|
|
35
|
-
return compactText([
|
|
36
|
-
header && `Topic: ${header}`,
|
|
37
|
-
prompt && `Question: ${prompt}`,
|
|
38
|
-
options && `Options: ${options}`,
|
|
39
|
-
]
|
|
40
|
-
.filter(Boolean)
|
|
41
|
-
.join("\n"));
|
|
42
|
-
}
|
|
43
|
-
function rememberRequest(seen, key) {
|
|
44
|
-
if (seen.has(key))
|
|
45
|
-
return false;
|
|
46
|
-
seen.add(key);
|
|
47
|
-
if (seen.size > 1_024)
|
|
48
|
-
seen.delete(seen.values().next().value);
|
|
49
|
-
return true;
|
|
50
|
-
}
|
|
51
|
-
const KKCodeOpenCode1NotifyPlugin = async ({ client, directory, project }) => {
|
|
52
|
-
const projectLabel = project?.name || directory?.split(/[\\/]/u).filter(Boolean).at(-1) || "KKCode";
|
|
53
|
-
const erroredSessions = new Set();
|
|
54
|
-
const notifiedPermission = new Set();
|
|
55
|
-
const notifiedQuestion = new Set();
|
|
56
|
-
await debugLog(`v1 plugin loaded | project=${projectLabel}`);
|
|
57
|
-
async function getSessionInfo(sessionID) {
|
|
58
|
-
try {
|
|
59
|
-
const result = await client.session.get({ path: { id: sessionID } });
|
|
60
|
-
return {
|
|
61
|
-
title: result?.data?.title?.trim() || undefined,
|
|
62
|
-
isSubSession: !!result?.data?.parentID,
|
|
63
|
-
};
|
|
64
|
-
}
|
|
65
|
-
catch (error) {
|
|
66
|
-
await debugLog(`v1 getSessionInfo FAILED | session=${sessionID} | ${error}`);
|
|
67
|
-
return { title: undefined, isSubSession: false };
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
async function report(event, sessionID, type, summary) {
|
|
71
|
-
const info = await getSessionInfo(sessionID);
|
|
72
|
-
await reportNotification({
|
|
73
|
-
sessionID,
|
|
74
|
-
type,
|
|
75
|
-
projectLabel,
|
|
76
|
-
summary,
|
|
77
|
-
sessionTitle: info.title,
|
|
78
|
-
});
|
|
79
|
-
await debugLog(`v1 ${event.type} REPORTED | ${sessionID} | type=${type}`);
|
|
80
|
-
}
|
|
81
|
-
return {
|
|
82
|
-
event: async ({ event }) => {
|
|
83
|
-
if (event.type !== "session.idle" &&
|
|
84
|
-
event.type !== "session.error" &&
|
|
85
|
-
event.type !== "permission.asked" &&
|
|
86
|
-
event.type !== "question.asked") {
|
|
87
|
-
return;
|
|
88
|
-
}
|
|
89
|
-
try {
|
|
90
|
-
const sessionID = event.properties.sessionID;
|
|
91
|
-
if (!sessionID)
|
|
92
|
-
return;
|
|
93
|
-
if (event.type === "session.idle") {
|
|
94
|
-
if (erroredSessions.delete(sessionID))
|
|
95
|
-
return;
|
|
96
|
-
const info = await getSessionInfo(sessionID);
|
|
97
|
-
await reportNotification({
|
|
98
|
-
sessionID,
|
|
99
|
-
type: info.isSubSession ? "sub_session.completed" : "session.completed",
|
|
100
|
-
projectLabel,
|
|
101
|
-
sessionTitle: info.title,
|
|
102
|
-
});
|
|
103
|
-
return;
|
|
104
|
-
}
|
|
105
|
-
if (event.type === "session.error") {
|
|
106
|
-
erroredSessions.add(sessionID);
|
|
107
|
-
await report(event, sessionID, "session.error", errorSummary(event.properties.error ?? event.properties));
|
|
108
|
-
return;
|
|
109
|
-
}
|
|
110
|
-
const requestKey = `${sessionID}:${event.properties.id ?? event.properties.requestID ?? ""}`;
|
|
111
|
-
const seen = event.type === "permission.asked" ? notifiedPermission : notifiedQuestion;
|
|
112
|
-
if (!rememberRequest(seen, requestKey))
|
|
113
|
-
return;
|
|
114
|
-
await report(event, sessionID, event.type === "permission.asked" ? "permission.asked" : "question.asked", event.type === "permission.asked"
|
|
115
|
-
? describePermission(event.properties)
|
|
116
|
-
: describeQuestion(event.properties));
|
|
117
|
-
}
|
|
118
|
-
catch (error) {
|
|
119
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
120
|
-
await debugLog(`v1 HANDLER ERROR | type=${event.type} | ${message}`);
|
|
121
|
-
await client.app
|
|
122
|
-
.log({
|
|
123
|
-
body: {
|
|
124
|
-
service: "kkcode",
|
|
125
|
-
level: "warn",
|
|
126
|
-
message: "Could not report KKCode session notification.",
|
|
127
|
-
extra: { error: message },
|
|
128
|
-
},
|
|
129
|
-
})
|
|
130
|
-
.catch(() => { });
|
|
131
|
-
}
|
|
132
|
-
},
|
|
133
|
-
};
|
|
134
|
-
};
|
|
135
|
-
export default KKCodeOpenCode1NotifyPlugin;
|
|
136
|
-
//# sourceMappingURL=plugin-v1.js.map
|
package/dist/plugin-v1.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"plugin-v1.js","sourceRoot":"","sources":["../src/plugin-v1.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EACL,WAAW,EACX,QAAQ,EACR,YAAY,EACZ,kBAA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kBAAkB,CAAC,CAAC,CAAC,gBAAgB,EACzE,KAAK,CAAC,IAAI,KAAK,kBAAkB;oBAC/B,CAAC,CAAC,kBAAkB,CAAC,KAAK,CAAC,UAAU,CAAC;oBACtC,CAAC,CAAC,gBAAgB,CAAC,KAAK,CAAC,UAAU,CAAC,CACvC,CAAC;YACJ,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,MAAM,OAAO,GAAG,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;gBACvE,MAAM,QAAQ,CAAC,2BAA2B,KAAK,CAAC,IAAI,MAAM,OAAO,EAAE,CAAC,CAAC;gBACrE,MAAM,MAAM,CAAC,GAAG;qBACb,GAAG,CAAC;oBACH,IAAI,EAAE;wBACJ,OAAO,EAAE,QAAQ;wBACjB,KAAK,EAAE,MAAM;wBACb,OAAO,EAAE,+CAA+C;wBACxD,KAAK,EAAE,EAAE,KAAK,EAAE,OAAO,EAAE;qBAC1B;iBACF,CAAC;qBACD,KAAK,CAAC,GAAG,EAAE,GAAE,CAAC,CAAC,CAAC;YACrB,CAAC;QACH,CAAC;KACF,CAAC;AACJ,CAAC,CAAC;AAEF,eAAe,2BAA2B,CAAC"}
|
package/src/plugin-v1.ts
DELETED
|
@@ -1,189 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* [INPUT]: 依赖 OpenCode 1 PluginInput/client 与 notification-core 共享上报内核
|
|
3
|
-
* [OUTPUT]: 默认导出 OpenCode 1 函数插件,处理 idle/error/permission/question 事件
|
|
4
|
-
* [POS]: OpenCode 1 插件实现模块,由包入口 entry.ts 以 { id, server } 组合导出;自身不是 exports 解析目标
|
|
5
|
-
* [PROTOCOL]: 变更时更新此头部,然后检查 CLAUDE.md
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import {
|
|
9
|
-
compactText,
|
|
10
|
-
debugLog,
|
|
11
|
-
errorSummary,
|
|
12
|
-
reportNotification,
|
|
13
|
-
type NotifyEventType,
|
|
14
|
-
} from "./notification-core.js";
|
|
15
|
-
|
|
16
|
-
type OpenCode1Event = {
|
|
17
|
-
type: string;
|
|
18
|
-
properties: {
|
|
19
|
-
sessionID?: string;
|
|
20
|
-
id?: string;
|
|
21
|
-
requestID?: string;
|
|
22
|
-
permission?: string;
|
|
23
|
-
patterns?: string[];
|
|
24
|
-
description?: string;
|
|
25
|
-
message?: string;
|
|
26
|
-
error?: unknown;
|
|
27
|
-
metadata?: { description?: string };
|
|
28
|
-
questions?: {
|
|
29
|
-
question?: string;
|
|
30
|
-
header?: string;
|
|
31
|
-
options?: { label?: string; description?: string }[];
|
|
32
|
-
}[];
|
|
33
|
-
};
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
function describePermission(properties: OpenCode1Event["properties"]): string {
|
|
37
|
-
const permission = compactText(properties.permission, 80) || "action";
|
|
38
|
-
const description = compactText(properties.description ?? properties.metadata?.description);
|
|
39
|
-
const patterns = (Array.isArray(properties.patterns) ? properties.patterns : [])
|
|
40
|
-
.map((pattern) => compactText(pattern, 160))
|
|
41
|
-
.filter(Boolean)
|
|
42
|
-
.slice(0, 3)
|
|
43
|
-
.join("; ");
|
|
44
|
-
return compactText(
|
|
45
|
-
[
|
|
46
|
-
`Permission: ${permission}`,
|
|
47
|
-
description && `Action: ${description}`,
|
|
48
|
-
patterns && `Scope: ${patterns}`,
|
|
49
|
-
]
|
|
50
|
-
.filter(Boolean)
|
|
51
|
-
.join("\n"),
|
|
52
|
-
);
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
function describeQuestion(properties: OpenCode1Event["properties"]): string {
|
|
56
|
-
const question = properties.questions?.[0];
|
|
57
|
-
if (!question) return "KKCode is waiting for your answer";
|
|
58
|
-
const header = compactText(question.header, 100);
|
|
59
|
-
const prompt = compactText(question.question, 300);
|
|
60
|
-
const options = (Array.isArray(question.options) ? question.options : [])
|
|
61
|
-
.map((option) => compactText(option.label, 80))
|
|
62
|
-
.filter(Boolean)
|
|
63
|
-
.slice(0, 4)
|
|
64
|
-
.join(", ");
|
|
65
|
-
return compactText(
|
|
66
|
-
[
|
|
67
|
-
header && `Topic: ${header}`,
|
|
68
|
-
prompt && `Question: ${prompt}`,
|
|
69
|
-
options && `Options: ${options}`,
|
|
70
|
-
]
|
|
71
|
-
.filter(Boolean)
|
|
72
|
-
.join("\n"),
|
|
73
|
-
);
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
function rememberRequest(seen: Set<string>, key: string): boolean {
|
|
77
|
-
if (seen.has(key)) return false;
|
|
78
|
-
seen.add(key);
|
|
79
|
-
if (seen.size > 1_024) seen.delete(seen.values().next().value!);
|
|
80
|
-
return true;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
const KKCodeOpenCode1NotifyPlugin = async ({ client, directory, project }: any) => {
|
|
84
|
-
const projectLabel =
|
|
85
|
-
project?.name || directory?.split(/[\\/]/u).filter(Boolean).at(-1) || "KKCode";
|
|
86
|
-
const erroredSessions = new Set<string>();
|
|
87
|
-
const notifiedPermission = new Set<string>();
|
|
88
|
-
const notifiedQuestion = new Set<string>();
|
|
89
|
-
|
|
90
|
-
await debugLog(`v1 plugin loaded | project=${projectLabel}`);
|
|
91
|
-
|
|
92
|
-
async function getSessionInfo(
|
|
93
|
-
sessionID: string,
|
|
94
|
-
): Promise<{ title?: string; isSubSession: boolean }> {
|
|
95
|
-
try {
|
|
96
|
-
const result = await client.session.get({ path: { id: sessionID } });
|
|
97
|
-
return {
|
|
98
|
-
title: result?.data?.title?.trim() || undefined,
|
|
99
|
-
isSubSession: !!result?.data?.parentID,
|
|
100
|
-
};
|
|
101
|
-
} catch (error) {
|
|
102
|
-
await debugLog(`v1 getSessionInfo FAILED | session=${sessionID} | ${error}`);
|
|
103
|
-
return { title: undefined, isSubSession: false };
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
async function report(
|
|
108
|
-
event: OpenCode1Event,
|
|
109
|
-
sessionID: string,
|
|
110
|
-
type: NotifyEventType,
|
|
111
|
-
summary?: string,
|
|
112
|
-
): Promise<void> {
|
|
113
|
-
const info = await getSessionInfo(sessionID);
|
|
114
|
-
await reportNotification({
|
|
115
|
-
sessionID,
|
|
116
|
-
type,
|
|
117
|
-
projectLabel,
|
|
118
|
-
summary,
|
|
119
|
-
sessionTitle: info.title,
|
|
120
|
-
});
|
|
121
|
-
await debugLog(`v1 ${event.type} REPORTED | ${sessionID} | type=${type}`);
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
return {
|
|
125
|
-
event: async ({ event }: { event: OpenCode1Event }) => {
|
|
126
|
-
if (
|
|
127
|
-
event.type !== "session.idle" &&
|
|
128
|
-
event.type !== "session.error" &&
|
|
129
|
-
event.type !== "permission.asked" &&
|
|
130
|
-
event.type !== "question.asked"
|
|
131
|
-
) {
|
|
132
|
-
return;
|
|
133
|
-
}
|
|
134
|
-
try {
|
|
135
|
-
const sessionID = event.properties.sessionID;
|
|
136
|
-
if (!sessionID) return;
|
|
137
|
-
|
|
138
|
-
if (event.type === "session.idle") {
|
|
139
|
-
if (erroredSessions.delete(sessionID)) return;
|
|
140
|
-
const info = await getSessionInfo(sessionID);
|
|
141
|
-
await reportNotification({
|
|
142
|
-
sessionID,
|
|
143
|
-
type: info.isSubSession ? "sub_session.completed" : "session.completed",
|
|
144
|
-
projectLabel,
|
|
145
|
-
sessionTitle: info.title,
|
|
146
|
-
});
|
|
147
|
-
return;
|
|
148
|
-
}
|
|
149
|
-
if (event.type === "session.error") {
|
|
150
|
-
erroredSessions.add(sessionID);
|
|
151
|
-
await report(
|
|
152
|
-
event,
|
|
153
|
-
sessionID,
|
|
154
|
-
"session.error",
|
|
155
|
-
errorSummary(event.properties.error ?? event.properties),
|
|
156
|
-
);
|
|
157
|
-
return;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
const requestKey = `${sessionID}:${event.properties.id ?? event.properties.requestID ?? ""}`;
|
|
161
|
-
const seen = event.type === "permission.asked" ? notifiedPermission : notifiedQuestion;
|
|
162
|
-
if (!rememberRequest(seen, requestKey)) return;
|
|
163
|
-
await report(
|
|
164
|
-
event,
|
|
165
|
-
sessionID,
|
|
166
|
-
event.type === "permission.asked" ? "permission.asked" : "question.asked",
|
|
167
|
-
event.type === "permission.asked"
|
|
168
|
-
? describePermission(event.properties)
|
|
169
|
-
: describeQuestion(event.properties),
|
|
170
|
-
);
|
|
171
|
-
} catch (error) {
|
|
172
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
173
|
-
await debugLog(`v1 HANDLER ERROR | type=${event.type} | ${message}`);
|
|
174
|
-
await client.app
|
|
175
|
-
.log({
|
|
176
|
-
body: {
|
|
177
|
-
service: "kkcode",
|
|
178
|
-
level: "warn",
|
|
179
|
-
message: "Could not report KKCode session notification.",
|
|
180
|
-
extra: { error: message },
|
|
181
|
-
},
|
|
182
|
-
})
|
|
183
|
-
.catch(() => {});
|
|
184
|
-
}
|
|
185
|
-
},
|
|
186
|
-
};
|
|
187
|
-
};
|
|
188
|
-
|
|
189
|
-
export default KKCodeOpenCode1NotifyPlugin;
|