@nightloom/isy 1.0.1 → 1.0.2
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 +1 -1
- package/dist/agents/claude.js +18 -7
- package/dist/agents/claude.js.map +1 -1
- package/dist/agents/codex-records.js +3 -1
- package/dist/agents/codex-records.js.map +1 -1
- package/dist/agents/index.d.ts +16 -5
- package/dist/agents/index.js +21 -6
- package/dist/agents/index.js.map +1 -1
- package/dist/agents/kimi-cli-wire.d.ts +17 -0
- package/dist/agents/kimi-cli-wire.js +312 -0
- package/dist/agents/kimi-cli-wire.js.map +1 -0
- package/dist/agents/kimi-config.d.ts +29 -0
- package/dist/agents/kimi-config.js +193 -0
- package/dist/agents/kimi-config.js.map +1 -0
- package/dist/agents/kimi-records.d.ts +0 -6
- package/dist/agents/kimi-records.js +51 -31
- package/dist/agents/kimi-records.js.map +1 -1
- package/dist/agents/kimi-tools.d.ts +30 -0
- package/dist/agents/kimi-tools.js +100 -0
- package/dist/agents/kimi-tools.js.map +1 -0
- package/dist/agents/kimi-wire.d.ts +8 -0
- package/dist/agents/kimi-wire.js +245 -80
- package/dist/agents/kimi-wire.js.map +1 -1
- package/dist/agents/kimi.d.ts +13 -2
- package/dist/agents/kimi.js +633 -114
- package/dist/agents/kimi.js.map +1 -1
- package/dist/agents/timeline.d.ts +36 -0
- package/dist/agents/timeline.js +61 -0
- package/dist/agents/timeline.js.map +1 -0
- package/dist/agents/types.d.ts +12 -0
- package/dist/commands/analyze.d.ts +2 -0
- package/dist/commands/analyze.js +63 -6
- package/dist/commands/analyze.js.map +1 -1
- package/dist/commands/check.js +11 -0
- package/dist/commands/check.js.map +1 -1
- package/dist/commands/init.js +17 -2
- package/dist/commands/init.js.map +1 -1
- package/dist/commands/upload.js +8 -4
- package/dist/commands/upload.js.map +1 -1
- package/dist/detectors.js +40 -5
- package/dist/detectors.js.map +1 -1
- package/dist/hook.d.ts +12 -5
- package/dist/hook.js +73 -17
- package/dist/hook.js.map +1 -1
- package/dist/lines.d.ts +8 -0
- package/dist/lines.js +32 -0
- package/dist/lines.js.map +1 -0
- package/dist/parser.d.ts +41 -0
- package/dist/parser.js +328 -32
- package/dist/parser.js.map +1 -1
- package/dist/paths.d.ts +4 -0
- package/dist/paths.js +39 -39
- package/dist/paths.js.map +1 -1
- package/dist/redact.d.ts +10 -0
- package/dist/redact.js +49 -8
- package/dist/redact.js.map +1 -1
- package/dist/state.d.ts +6 -3
- package/dist/state.js +21 -5
- package/dist/state.js.map +1 -1
- package/dist/subagents.d.ts +49 -0
- package/dist/subagents.js +237 -0
- package/dist/subagents.js.map +1 -0
- package/dist/test/check.test.js +25 -3
- package/dist/test/check.test.js.map +1 -1
- package/dist/test/codex.test.js +6 -0
- package/dist/test/codex.test.js.map +1 -1
- package/dist/test/commit.test.js +9 -1
- package/dist/test/commit.test.js.map +1 -1
- package/dist/test/hook.test.js +66 -2
- package/dist/test/hook.test.js.map +1 -1
- package/dist/test/init.test.d.ts +1 -0
- package/dist/test/init.test.js +94 -0
- package/dist/test/init.test.js.map +1 -0
- package/dist/test/kimi-cli.test.d.ts +1 -0
- package/dist/test/kimi-cli.test.js +231 -0
- package/dist/test/kimi-cli.test.js.map +1 -0
- package/dist/test/kimi-code.test.d.ts +1 -0
- package/dist/test/kimi-code.test.js +302 -0
- package/dist/test/kimi-code.test.js.map +1 -0
- package/dist/test/kimi-config.test.d.ts +1 -0
- package/dist/test/kimi-config.test.js +224 -0
- package/dist/test/kimi-config.test.js.map +1 -0
- package/dist/test/kimi-hook.test.d.ts +1 -0
- package/dist/test/kimi-hook.test.js +163 -0
- package/dist/test/kimi-hook.test.js.map +1 -0
- package/dist/test/kimi-listing.test.d.ts +1 -0
- package/dist/test/kimi-listing.test.js +193 -0
- package/dist/test/kimi-listing.test.js.map +1 -0
- package/dist/test/kimi.test.js +52 -7
- package/dist/test/kimi.test.js.map +1 -1
- package/dist/test/parser.test.js +192 -5
- package/dist/test/parser.test.js.map +1 -1
- package/dist/test/redact.test.js +71 -7
- package/dist/test/redact.test.js.map +1 -1
- package/dist/test/stage0.test.js +49 -0
- package/dist/test/stage0.test.js.map +1 -1
- package/dist/test/subagents.test.d.ts +1 -0
- package/dist/test/subagents.test.js +272 -0
- package/dist/test/subagents.test.js.map +1 -0
- package/dist/types.d.ts +16 -0
- package/package.json +3 -2
- package/src/agents/claude.ts +17 -7
- package/src/agents/codex-records.ts +3 -1
- package/src/agents/index.ts +21 -6
- package/src/agents/kimi-cli-wire.ts +350 -0
- package/src/agents/kimi-config.ts +210 -0
- package/src/agents/kimi-records.ts +52 -29
- package/src/agents/kimi-tools.ts +128 -0
- package/src/agents/kimi-wire.ts +270 -82
- package/src/agents/kimi.ts +653 -113
- package/src/agents/timeline.ts +68 -0
- package/src/agents/types.ts +12 -0
- package/src/commands/analyze.ts +62 -6
- package/src/commands/check.ts +11 -0
- package/src/commands/init.ts +18 -2
- package/src/commands/upload.ts +8 -4
- package/src/detectors.ts +39 -5
- package/src/hook.ts +81 -24
- package/src/lines.ts +31 -0
- package/src/parser.ts +343 -31
- package/src/paths.ts +36 -33
- package/src/redact.ts +53 -10
- package/src/state.ts +24 -5
- package/src/subagents.ts +283 -0
- package/src/types.ts +16 -0
package/src/agents/kimi-wire.ts
CHANGED
|
@@ -1,19 +1,41 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { claudeTool, wholeFileRead } from "./kimi-tools.js";
|
|
2
|
+
import { Timelines, boundOf, stamped } from "./timeline.js";
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
* messages.
|
|
5
|
+
* Kimi Code's session log: `agents/<agent>/wire.jsonl` in a session folder,
|
|
6
|
+
* written by the TypeScript CLI that replaced the Python Kimi CLI (0.38 and
|
|
7
|
+
* 2.x alike, protocol 1.5). Its records are wrapper events rather than
|
|
8
|
+
* messages: `context.append_message` for a message appended outside the agent
|
|
9
|
+
* loop, `context.append_loop_event` for what the loop did — `content.part`,
|
|
10
|
+
* `tool.call`, `tool.result` — each stamped with `time` in epoch milliseconds.
|
|
8
11
|
*
|
|
9
|
-
* Kept apart from the `context.jsonl` reader
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
+
* Kept apart from the `context.jsonl` reader and from Kimi CLI's envelopes
|
|
13
|
+
* (`kimi-cli-wire.ts`) rather than folded into one: the formats share no
|
|
14
|
+
* field, and a reader that tried to serve all of them would guess.
|
|
12
15
|
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
16
|
+
* What 2.x added, and how it is read:
|
|
17
|
+
* - A subagent writes a log of its own (`agents/agent-<n>/wire.jsonl`), and the
|
|
18
|
+
* main log says which call started it (`subagent.spawned`, with
|
|
19
|
+
* `parentToolCallId`). Its records go in by time from that point, as a
|
|
20
|
+
* sidechain with `parentToolUseID` — the shape and the rule Claude Code's
|
|
21
|
+
* subagents get (`subagents.ts`, `timeline.ts`): one run in the background
|
|
22
|
+
* works alongside the session. A resumed subagent is spawned again under the
|
|
23
|
+
* same id and goes on in the same log, so what it writes after the second
|
|
24
|
+
* spawn answers the second call. A subagent no record names — 0.x kept that
|
|
25
|
+
* link in memory only — still did the work, and comes after the session
|
|
26
|
+
* unlinked.
|
|
27
|
+
* - `context.append_message` with a user role is not always the user: its
|
|
28
|
+
* `origin` says `injection` for the CLI's own reminders, `hook_result` for a
|
|
29
|
+
* hook's output (ISY's own alert drain among them), `system_trigger` for a
|
|
30
|
+
* subagent's prompt or a stop hook — `isTurn` says which are turns.
|
|
31
|
+
* - `agent.message.appended` repeats every message a second time for the UI,
|
|
32
|
+
* and is not read.
|
|
33
|
+
* - `llm.request` names the model each call went to.
|
|
34
|
+
*
|
|
35
|
+
* ponytail: a rewind (`context.undo`) is recorded, but the log goes on
|
|
36
|
+
* linearly past it, so the output is one chain and `onAbandonedBranch` finds
|
|
37
|
+
* nothing there — the content-revert detectors still fire. Graft the branch
|
|
38
|
+
* from `agent.switched` once a session shows how its turns map to records.
|
|
17
39
|
*/
|
|
18
40
|
|
|
19
41
|
const CONTENT_EVENTS = new Set(["content.part", "tool.call", "tool.result"]);
|
|
@@ -36,69 +58,202 @@ export function isKimiWireTranscript(firstLine: string): boolean {
|
|
|
36
58
|
}
|
|
37
59
|
}
|
|
38
60
|
|
|
39
|
-
|
|
61
|
+
/**
|
|
62
|
+
* Epoch milliseconds, as Kimi Code writes them — or seconds, from a build that
|
|
63
|
+
* wrote those. A time no date can hold is no time: one bad line must not make
|
|
64
|
+
* the whole session unreadable.
|
|
65
|
+
*/
|
|
66
|
+
function millis(time: unknown): number | undefined {
|
|
40
67
|
if (typeof time !== "number" || !Number.isFinite(time)) return undefined;
|
|
41
|
-
|
|
68
|
+
const at = time < 1e11 ? time * 1000 : time;
|
|
69
|
+
return Number.isNaN(new Date(at).getTime()) ? undefined : at;
|
|
42
70
|
}
|
|
43
71
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
72
|
+
function stamp(time: unknown): string | undefined {
|
|
73
|
+
const at = millis(time);
|
|
74
|
+
return at === undefined ? undefined : new Date(at).toISOString();
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** The events of one log, a line that does not parse left out: a half-written last line while the session runs. */
|
|
78
|
+
function* events(lines: Iterable<string>): Generator<Record<string, unknown>> {
|
|
79
|
+
for (const line of lines) {
|
|
80
|
+
const text = line.trim();
|
|
81
|
+
if (!text) continue;
|
|
82
|
+
let event: unknown;
|
|
83
|
+
try {
|
|
84
|
+
event = JSON.parse(text);
|
|
85
|
+
} catch {
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
if (isObject(event)) yield event;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* The events after `until`, one without a time counted at the last time before
|
|
94
|
+
* it: what came before is another session's (`KimiWireOptions.copiedUntil`).
|
|
95
|
+
*/
|
|
96
|
+
function* since(events: Iterable<Record<string, unknown>>, until: number | undefined): Generator<Record<string, unknown>> {
|
|
97
|
+
if (until === undefined) {
|
|
98
|
+
yield* events;
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
let at = -Infinity;
|
|
102
|
+
for (const event of events) {
|
|
103
|
+
at = millis(event.time) ?? at;
|
|
104
|
+
if (at > until) yield event;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** Text out of content parts, or a plain string. */
|
|
109
|
+
function partsText(value: unknown): string {
|
|
110
|
+
if (typeof value === "string") return value;
|
|
111
|
+
if (!Array.isArray(value)) return "";
|
|
112
|
+
return value
|
|
113
|
+
.filter((part): part is Record<string, unknown> => isObject(part) && typeof part.text === "string")
|
|
114
|
+
.map((part) => part.text as string)
|
|
115
|
+
.join("\n");
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Whether a user-role message opens a turn, by the rule Kimi Code shows its
|
|
120
|
+
* own transcript with: the person's words — typed, a slash command, a `!`
|
|
121
|
+
* shell command — a subagent's prompt, a goal carried on, a task or cron job
|
|
122
|
+
* reporting back. Not the CLI's reminders (`injection`), a retry, a compaction
|
|
123
|
+
* summary, nor a hook's output: Claude Code files hook output as an
|
|
124
|
+
* attachment, and ISY's own alert drain is one of those hooks.
|
|
125
|
+
*/
|
|
126
|
+
function isTurn(message: Record<string, unknown>): boolean {
|
|
127
|
+
const origin = isObject(message.origin) ? message.origin : undefined;
|
|
128
|
+
switch (origin?.kind) {
|
|
129
|
+
case "system_trigger":
|
|
130
|
+
return origin.name === "subagent" || origin.name === "goal_continuation";
|
|
131
|
+
case "skill_activation":
|
|
132
|
+
case "plugin_command":
|
|
133
|
+
return origin.trigger === "user-slash";
|
|
134
|
+
case "injection":
|
|
135
|
+
case "retry":
|
|
136
|
+
case "compaction_summary":
|
|
137
|
+
case "hook_result":
|
|
138
|
+
return false;
|
|
139
|
+
default:
|
|
140
|
+
return true;
|
|
141
|
+
}
|
|
49
142
|
}
|
|
50
143
|
|
|
51
144
|
export interface KimiWireOptions {
|
|
52
145
|
sessionId: string;
|
|
53
146
|
cwd?: string;
|
|
54
147
|
version?: string;
|
|
148
|
+
/** Each subagent's own log, by the agent id the main log spawns it under. */
|
|
149
|
+
subagents?: ReadonlyMap<string, Iterable<string>>;
|
|
150
|
+
/**
|
|
151
|
+
* Up to when the logs hold another session's work — the Kimi CLI session
|
|
152
|
+
* this one was imported from, or the session it was forked off — which goes
|
|
153
|
+
* up as that session: every log up to then is left out.
|
|
154
|
+
*/
|
|
155
|
+
copiedUntil?: number;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** One agent's place in the output: the session's own chain, or a subagent's. */
|
|
159
|
+
interface Thread {
|
|
160
|
+
parent: string | null;
|
|
161
|
+
/**
|
|
162
|
+
* Its records' ids, `<name>-<n>`, numbered within its own log rather than
|
|
163
|
+
* the output: a background subagent's records land between the session's by
|
|
164
|
+
* time, and a count over both would renumber the session's every time the
|
|
165
|
+
* subagent wrote more — and with them what `state.ts:candidateKey` keys on.
|
|
166
|
+
*/
|
|
167
|
+
name: string;
|
|
168
|
+
emitted: number;
|
|
169
|
+
model?: string;
|
|
170
|
+
sidechain?: { agentId: string; call?: string };
|
|
55
171
|
}
|
|
56
172
|
|
|
57
|
-
export function wireToClaudeRecords(
|
|
58
|
-
lines: Iterable<string>,
|
|
59
|
-
options: KimiWireOptions,
|
|
60
|
-
): string[] {
|
|
173
|
+
export function wireToClaudeRecords(lines: Iterable<string>, options: KimiWireOptions): string[] {
|
|
61
174
|
const out: string[] = [];
|
|
62
|
-
|
|
63
|
-
|
|
175
|
+
/** What each call asked for, by id: a `Read` result is only the file given its call. */
|
|
176
|
+
const calls = new Map<string, { name: string; input: Record<string, unknown> }>();
|
|
177
|
+
/** Each subagent's thread, by agent id, from the moment a log starts it. */
|
|
178
|
+
const threads = new Map<string, Thread>();
|
|
179
|
+
const running = new Timelines<{ thread: Thread; event: Record<string, unknown> }>();
|
|
64
180
|
|
|
65
|
-
const emit = (record: Record<string, unknown>): void => {
|
|
66
|
-
const uuid =
|
|
181
|
+
const emit = (thread: Thread, record: Record<string, unknown>): void => {
|
|
182
|
+
const uuid = `${thread.name}-${thread.emitted}`;
|
|
183
|
+
thread.emitted += 1;
|
|
67
184
|
out.push(
|
|
68
185
|
JSON.stringify({
|
|
69
186
|
...record,
|
|
70
187
|
uuid,
|
|
71
|
-
parentUuid: parent,
|
|
188
|
+
parentUuid: thread.parent,
|
|
72
189
|
sessionId: options.sessionId,
|
|
73
190
|
cwd: options.cwd,
|
|
74
|
-
version: options.version
|
|
75
|
-
isSidechain:
|
|
191
|
+
...(options.version ? { version: options.version } : {}),
|
|
192
|
+
isSidechain: thread.sidechain !== undefined,
|
|
193
|
+
...(thread.sidechain ? { agentId: thread.sidechain.agentId } : {}),
|
|
194
|
+
...(thread.sidechain?.call ? { parentToolUseID: thread.sidechain.call } : {}),
|
|
76
195
|
}),
|
|
77
196
|
);
|
|
78
|
-
parent = uuid;
|
|
79
|
-
index += 1;
|
|
197
|
+
thread.parent = uuid;
|
|
80
198
|
};
|
|
81
199
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
200
|
+
const say = (thread: Thread, timestamp: string | undefined, block: Record<string, unknown>): void => {
|
|
201
|
+
emit(thread, {
|
|
202
|
+
type: "assistant",
|
|
203
|
+
timestamp,
|
|
204
|
+
message: { role: "assistant", ...(thread.model ? { model: thread.model } : {}), content: [block] },
|
|
205
|
+
});
|
|
206
|
+
};
|
|
85
207
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
event
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
208
|
+
const logOf = (thread: Thread, source: Iterable<string>, from: number) =>
|
|
209
|
+
stamped(
|
|
210
|
+
[...since(events(source), options.copiedUntil)].map((event) => ({ thread, event })),
|
|
211
|
+
({ event }) => millis(event.time),
|
|
212
|
+
from,
|
|
213
|
+
);
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Where a log starts a subagent, its own log goes in from here on. Spawned
|
|
217
|
+
* again, it was resumed: it goes on in the same log, and what it writes from
|
|
218
|
+
* now on answers this call.
|
|
219
|
+
*/
|
|
220
|
+
const spawned = (event: Record<string, unknown>, at: number): void => {
|
|
221
|
+
const agentId = asString(event.subagentId);
|
|
222
|
+
if (!agentId) return;
|
|
223
|
+
const call = asString(event.parentToolCallId);
|
|
224
|
+
|
|
225
|
+
const known = threads.get(agentId);
|
|
226
|
+
if (known) {
|
|
227
|
+
if (call && known.sidechain) known.sidechain.call = call;
|
|
228
|
+
return;
|
|
92
229
|
}
|
|
93
|
-
|
|
230
|
+
const source = options.subagents?.get(agentId);
|
|
231
|
+
if (!source) return;
|
|
232
|
+
const thread: Thread = { parent: null, name: `kimi-${agentId}`, emitted: 0, sidechain: { agentId, call } };
|
|
233
|
+
threads.set(agentId, thread);
|
|
234
|
+
running.add(logOf(thread, source, at));
|
|
235
|
+
};
|
|
94
236
|
|
|
237
|
+
/** One event of any agent's log, in the order the merge hands it over. */
|
|
238
|
+
const take = (thread: Thread, event: Record<string, unknown>, at: number): void => {
|
|
95
239
|
const timestamp = stamp(event.time);
|
|
96
240
|
|
|
241
|
+
if (event.type === "llm.request") {
|
|
242
|
+
thread.model = asString(event.model) ?? thread.model;
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
if (event.type === "subagent.spawned") {
|
|
247
|
+
spawned(event, at);
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
|
|
97
251
|
// A whole user or assistant message, appended outside the agent loop.
|
|
98
252
|
if (event.type === "context.append_message" && isObject(event.message)) {
|
|
99
253
|
const message = event.message;
|
|
100
254
|
const role = asString(message.role);
|
|
101
|
-
if (role !== "user" && role !== "assistant")
|
|
255
|
+
if (role !== "user" && role !== "assistant") return;
|
|
256
|
+
if (role === "user" && !isTurn(message)) return;
|
|
102
257
|
|
|
103
258
|
const content = Array.isArray(message.content)
|
|
104
259
|
? message.content
|
|
@@ -108,72 +263,105 @@ export function wireToClaudeRecords(
|
|
|
108
263
|
: typeof message.content === "string" && message.content.length > 0
|
|
109
264
|
? [{ type: "text", text: message.content }]
|
|
110
265
|
: [];
|
|
111
|
-
if (content.length === 0)
|
|
266
|
+
if (content.length === 0) return;
|
|
112
267
|
|
|
113
|
-
emit({ type: role, timestamp, message: { role, content } });
|
|
114
|
-
|
|
268
|
+
emit(thread, { type: role, timestamp, message: { role, content } });
|
|
269
|
+
return;
|
|
115
270
|
}
|
|
116
271
|
|
|
117
|
-
if (event.type !== "context.append_loop_event" || !isObject(event.event))
|
|
272
|
+
if (event.type !== "context.append_loop_event" || !isObject(event.event)) return;
|
|
118
273
|
|
|
119
274
|
const inner = event.event;
|
|
120
275
|
const kind = asString(inner.type);
|
|
121
|
-
if (!kind || !CONTENT_EVENTS.has(kind))
|
|
276
|
+
if (!kind || !CONTENT_EVENTS.has(kind)) return;
|
|
122
277
|
|
|
123
278
|
if (kind === "content.part" && isObject(inner.part)) {
|
|
124
279
|
const part = inner.part;
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
part.
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
emit({ type: "assistant", timestamp, message: { role: "assistant", content: [block] } });
|
|
137
|
-
continue;
|
|
280
|
+
if (part.type === "think" || part.type === "thinking") {
|
|
281
|
+
// A `hidden` part repeats a reasoning summary shown elsewhere. An
|
|
282
|
+
// empty one — encrypted, or a provider that returns no text — still
|
|
283
|
+
// says the model reasoned, and is counted as hidden reasoning.
|
|
284
|
+
if (part.hidden === true) return;
|
|
285
|
+
const thinking = [part.think, part.thinking].find((value) => typeof value === "string");
|
|
286
|
+
say(thread, timestamp, { type: "thinking", thinking: typeof thinking === "string" ? thinking : "" });
|
|
287
|
+
} else if (asString(part.text)) {
|
|
288
|
+
say(thread, timestamp, { type: "text", text: part.text });
|
|
289
|
+
}
|
|
290
|
+
return;
|
|
138
291
|
}
|
|
139
292
|
|
|
140
293
|
if (kind === "tool.call") {
|
|
141
294
|
const name = asString(inner.name);
|
|
142
295
|
const id = asString(inner.toolCallId);
|
|
143
|
-
if (!name || !id)
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
role: "assistant",
|
|
151
|
-
// Kimi names the file argument `path`; every downstream detector
|
|
152
|
-
// reads `file_path`, so the rename happens once, here.
|
|
153
|
-
content: [{ type: "tool_use", id, name, input: renameArgs(name, args) }],
|
|
154
|
-
},
|
|
155
|
-
});
|
|
156
|
-
continue;
|
|
296
|
+
if (!name || !id) return;
|
|
297
|
+
// Kimi's names and its `path` argument, as Claude's: every downstream
|
|
298
|
+
// detector reads `Edit`, `Bash`, `file_path` (`kimi-tools.ts`).
|
|
299
|
+
const use = claudeTool(name, isObject(inner.args) ? inner.args : {});
|
|
300
|
+
calls.set(id, use);
|
|
301
|
+
say(thread, timestamp, { type: "tool_use", id, name: use.name, input: use.input });
|
|
302
|
+
return;
|
|
157
303
|
}
|
|
158
304
|
|
|
159
305
|
const id = asString(inner.toolCallId);
|
|
160
|
-
if (!id)
|
|
161
|
-
const
|
|
162
|
-
//
|
|
163
|
-
const
|
|
306
|
+
if (!id) return;
|
|
307
|
+
const result = isObject(inner.result) ? inner.result : {};
|
|
308
|
+
// `output` is a string or a list of parts; `note` is Kimi's status line.
|
|
309
|
+
const output = partsText(result.output);
|
|
310
|
+
const note = typeof result.note === "string" ? result.note : "";
|
|
311
|
+
const status = /^<system>([\s\S]*?)<\/system>$/.exec(note)?.[1] ?? note;
|
|
312
|
+
// Kimi Code flags a failed call with `isError`.
|
|
313
|
+
const failed = result.isError === true || result.error !== undefined;
|
|
314
|
+
const call = calls.get(id);
|
|
315
|
+
const file =
|
|
316
|
+
call?.name === "Read" && !failed && typeof call.input.file_path === "string"
|
|
317
|
+
? wholeFileRead(output, status)
|
|
318
|
+
: undefined;
|
|
164
319
|
|
|
165
|
-
emit({
|
|
320
|
+
emit(thread, {
|
|
166
321
|
type: "user",
|
|
167
322
|
timestamp,
|
|
168
323
|
message: {
|
|
169
324
|
role: "user",
|
|
170
325
|
content: [
|
|
171
|
-
{
|
|
326
|
+
{
|
|
327
|
+
type: "tool_result",
|
|
328
|
+
tool_use_id: id,
|
|
329
|
+
content: output || status,
|
|
330
|
+
...(failed ? { is_error: true } : {}),
|
|
331
|
+
},
|
|
172
332
|
],
|
|
173
333
|
},
|
|
174
|
-
toolUseResult:
|
|
334
|
+
toolUseResult:
|
|
335
|
+
file !== undefined
|
|
336
|
+
? {
|
|
337
|
+
type: "text",
|
|
338
|
+
file: {
|
|
339
|
+
filePath: call!.input.file_path,
|
|
340
|
+
content: file,
|
|
341
|
+
numLines: file.split("\n").length,
|
|
342
|
+
startLine: 1,
|
|
343
|
+
totalLines: file.split("\n").length,
|
|
344
|
+
},
|
|
345
|
+
}
|
|
346
|
+
: { stdout: output },
|
|
175
347
|
});
|
|
348
|
+
};
|
|
349
|
+
|
|
350
|
+
const main: Thread = { parent: null, name: "kimi", emitted: 0 };
|
|
351
|
+
let at = -Infinity;
|
|
352
|
+
for (const event of since(events(lines), options.copiedUntil)) {
|
|
353
|
+
at = millis(event.time) ?? at;
|
|
354
|
+
for (const next of running.until(boundOf(at))) take(next.item.thread, next.item.event, next.at);
|
|
355
|
+
take(main, event, at);
|
|
176
356
|
}
|
|
357
|
+
for (const next of running.until(Infinity)) take(next.item.thread, next.item.event, next.at);
|
|
177
358
|
|
|
359
|
+
for (const [agentId, source] of options.subagents ?? []) {
|
|
360
|
+
if (threads.has(agentId)) continue;
|
|
361
|
+
const thread: Thread = { parent: null, name: `kimi-${agentId}`, emitted: 0, sidechain: { agentId } };
|
|
362
|
+
threads.set(agentId, thread);
|
|
363
|
+
running.add(logOf(thread, source, -Infinity));
|
|
364
|
+
for (const next of running.until(Infinity)) take(next.item.thread, next.item.event, next.at);
|
|
365
|
+
}
|
|
178
366
|
return out;
|
|
179
367
|
}
|