@code-yeongyu/senpi-codemode 2026.9.15 → 2026.9.16
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/CHANGELOG.md +36 -0
- package/README.md +24 -0
- package/package.json +4 -4
- package/src/bridge/http-server.ts +7 -1
- package/src/bridge/kernel-tools-protocol.ts +85 -0
- package/src/bridge/protocol.ts +6 -0
- package/src/bridges/agent-bridge.ts +30 -11
- package/src/extension/session-manager.ts +11 -0
- package/src/index.ts +10 -0
- package/src/kernels/jl/prelude.jl +46 -0
- package/src/kernels/jl/runner.jl +1 -1
- package/src/kernels/js/context-manager.ts +58 -10
- package/src/kernels/js/kernel-contract.ts +12 -0
- package/src/kernels/js/kernel-tools-context.d.ts +13 -0
- package/src/kernels/js/kernel-tools-context.js +7 -0
- package/src/kernels/js/kernel-tools-define.d.ts +10 -0
- package/src/kernels/js/kernel-tools-define.js +12 -0
- package/src/kernels/js/kernel-tools-errors.js +22 -0
- package/src/kernels/js/kernel-tools-errors.ts +26 -0
- package/src/kernels/js/kernel-tools-host.ts +134 -0
- package/src/kernels/js/kernel-tools-naming.js +28 -0
- package/src/kernels/js/kernel-tools-parse.d.ts +5 -0
- package/src/kernels/js/kernel-tools-parse.js +255 -0
- package/src/kernels/js/kernel-tools-pump.js +105 -0
- package/src/kernels/js/kernel-tools-registry.d.ts +27 -0
- package/src/kernels/js/kernel-tools-registry.js +127 -0
- package/src/kernels/js/kernel-tools-schema.js +69 -0
- package/src/kernels/js/kernel-tools-types.ts +42 -0
- package/src/kernels/js/prelude.ts +2 -0
- package/src/kernels/js/worker-core.js +26 -3
- package/src/kernels/js/worker-runtime.js +20 -8
- package/src/kernels/js/worker-slot.ts +6 -1
- package/src/kernels/js/worker-startup.ts +9 -7
- package/src/kernels/js/workpool.js +37 -0
- package/src/kernels/kernel-tools-unavailable.ts +5 -0
- package/src/kernels/py/kernel.ts +13 -0
- package/src/kernels/py/prelude.py +57 -3
- package/src/kernels/rb/prelude.rb +13 -2
- package/src/kernels/rb/workpool.rb +38 -0
- package/src/kernels/shared/subprocess-kernel.ts +13 -0
- package/src/kernels/shared/subprocess-queue.ts +9 -1
- package/src/prompt/eval-prompt-template.ts +4 -2
- package/src/tool/cell-handler.ts +8 -1
- package/src/tool/detached-cell-manager.ts +36 -71
- package/src/tool/image-resize.ts +52 -0
- package/src/tool/image.ts +37 -72
- package/src/tool/managed-cell.ts +78 -0
- package/src/tool/render.ts +30 -5
- package/src/tool/run-eval-cell.ts +49 -30
- package/src/tool/terminal-snapshot-store.ts +35 -0
- package/src/tool/tool-result-marshal.ts +23 -0
- package/src/tool/types.ts +2 -0
package/src/tool/render.ts
CHANGED
|
@@ -71,10 +71,15 @@ const TOOL_CALL_COLLAPSED_VISUAL_LINES = 4;
|
|
|
71
71
|
const TOOL_CALL_COLLAPSED_ERROR_CODE_POINTS = 512;
|
|
72
72
|
const TOOL_ERROR_OMISSION_MARKER = "[tool error omitted]";
|
|
73
73
|
const LIVE_ELAPSED_TICK_MS = 1_000;
|
|
74
|
+
// A live row repaints on every tick, so this many ticks without a render means the row is gone.
|
|
75
|
+
const LIVE_TICKER_MAX_IDLE_TICKS = 60;
|
|
74
76
|
|
|
75
77
|
class PlainTextComponent implements EvalRenderComponent {
|
|
76
78
|
#blocks: readonly RenderBlock[] = [];
|
|
77
79
|
#ticker: ReturnType<typeof setInterval> | undefined;
|
|
80
|
+
#live = false;
|
|
81
|
+
#invalidate: (() => void) | undefined;
|
|
82
|
+
#idleTicks = 0;
|
|
78
83
|
|
|
79
84
|
setBlocks(blocks: readonly RenderBlock[]): void {
|
|
80
85
|
this.#blocks = blocks;
|
|
@@ -84,16 +89,19 @@ class PlainTextComponent implements EvalRenderComponent {
|
|
|
84
89
|
* The host only animates tool rows for streaming args, `task`, and results carrying
|
|
85
90
|
* `details.progress`; an eval row matches none of them, so nothing repaints it between
|
|
86
91
|
* update events. While a cell is non-terminal this drives the repaint itself so the
|
|
87
|
-
* header's elapsed time advances, and it emits no tool updates or RPC traffic.
|
|
92
|
+
* header's elapsed time advances, and it emits no tool updates or RPC traffic. Detached
|
|
93
|
+
* and terminal cards never arm it, and a ticker whose row stopped rendering (transcript
|
|
94
|
+
* rebuild, session switch) stops itself after LIVE_TICKER_MAX_IDLE_TICKS and rearms on
|
|
95
|
+
* the next render, so dropped rows cannot accumulate intervals.
|
|
88
96
|
*/
|
|
89
97
|
syncLiveTicker(isLive: boolean, invalidate: () => void): void {
|
|
98
|
+
this.#live = isLive;
|
|
99
|
+
this.#invalidate = invalidate;
|
|
90
100
|
if (!isLive) {
|
|
91
101
|
this.stopLiveTicker();
|
|
92
102
|
return;
|
|
93
103
|
}
|
|
94
|
-
|
|
95
|
-
this.#ticker = setInterval(invalidate, LIVE_ELAPSED_TICK_MS);
|
|
96
|
-
this.#ticker.unref?.();
|
|
104
|
+
this.#armTicker();
|
|
97
105
|
}
|
|
98
106
|
|
|
99
107
|
stopLiveTicker(): void {
|
|
@@ -102,7 +110,24 @@ class PlainTextComponent implements EvalRenderComponent {
|
|
|
102
110
|
this.#ticker = undefined;
|
|
103
111
|
}
|
|
104
112
|
|
|
113
|
+
#armTicker(): void {
|
|
114
|
+
if (this.#ticker !== undefined || this.#invalidate === undefined) return;
|
|
115
|
+
this.#ticker = setInterval(() => this.#tick(), LIVE_ELAPSED_TICK_MS);
|
|
116
|
+
this.#ticker.unref?.();
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
#tick(): void {
|
|
120
|
+
this.#idleTicks += 1;
|
|
121
|
+
if (this.#idleTicks >= LIVE_TICKER_MAX_IDLE_TICKS) {
|
|
122
|
+
this.stopLiveTicker();
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
this.#invalidate?.();
|
|
126
|
+
}
|
|
127
|
+
|
|
105
128
|
render(width: number): string[] {
|
|
129
|
+
this.#idleTicks = 0;
|
|
130
|
+
if (this.#live) this.#armTicker();
|
|
106
131
|
const lines: string[] = [];
|
|
107
132
|
for (const block of this.#blocks) {
|
|
108
133
|
switch (block.kind) {
|
|
@@ -269,7 +294,7 @@ function spinner(frame: number | undefined): string {
|
|
|
269
294
|
}
|
|
270
295
|
|
|
271
296
|
function isLiveCellStatus(status: CellStatus): boolean {
|
|
272
|
-
return status === "pending" || status === "running"
|
|
297
|
+
return status === "pending" || status === "running";
|
|
273
298
|
}
|
|
274
299
|
|
|
275
300
|
function cellPresentation(status: CellStatus, spinnerFrame: number | undefined): StatusPresentation {
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { randomUUID } from "node:crypto";
|
|
2
2
|
import { join } from "node:path";
|
|
3
|
-
import type
|
|
3
|
+
import { type AgentToolResult, type ExtensionContext, kernelToolsStorage } from "@code-yeongyu/senpi";
|
|
4
4
|
import { DEFAULT_FOREGROUND_WINDOW_SECONDS, defaultCodemodeSettings } from "../config/settings.ts";
|
|
5
|
+
import type { KernelToolsCapability } from "../kernels/js/kernel-tools-types.ts";
|
|
5
6
|
import { TIMEOUT_PAUSE_OP, TIMEOUT_RESUME_OP } from "../timeouts/bridge-timeout.ts";
|
|
6
7
|
import { abortError, CellExecution, defaultTimeoutFactory } from "./cell-execution.ts";
|
|
7
8
|
import { CellHandler, type CellState } from "./cell-handler.ts";
|
|
@@ -11,7 +12,7 @@ import { buildEvalExecutionEventPayload, type EvalExecutionSettleOutcome } from
|
|
|
11
12
|
import { evalTimeoutBehavior } from "./eval-request.ts";
|
|
12
13
|
import type { CreateEvalToolOptions, EvalCellInvocation } from "./eval-tool-options.ts";
|
|
13
14
|
import { describeTimeoutState } from "./interrupt-note.ts";
|
|
14
|
-
import type { EvalToolDetails } from "./types.ts";
|
|
15
|
+
import type { EvalKernel, EvalToolDetails } from "./types.ts";
|
|
15
16
|
|
|
16
17
|
export async function runEvalCell(
|
|
17
18
|
options: CreateEvalToolOptions,
|
|
@@ -166,34 +167,39 @@ async function executeCell(
|
|
|
166
167
|
void pending.catch((error: unknown) => execution.cancel(error));
|
|
167
168
|
}),
|
|
168
169
|
);
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
options.kernelManager.setContext
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
170
|
+
const runBound = async (): Promise<AgentToolResult<EvalToolDetails>> => {
|
|
171
|
+
execution.setKernel(kernel);
|
|
172
|
+
const activeHandler = new CellHandler(kernel, state, {
|
|
173
|
+
executeTool: options.executeTool,
|
|
174
|
+
...(options.listTools === undefined ? {} : { listTools: options.listTools }),
|
|
175
|
+
settings: options.settings ?? defaultCodemodeSettings,
|
|
176
|
+
...(options.complete === undefined ? {} : { complete: options.complete }),
|
|
177
|
+
ctx: bridgeContext,
|
|
178
|
+
...(options.artifactsDir === undefined
|
|
179
|
+
? {}
|
|
180
|
+
: { artifactPath: join(options.artifactsDir, `eval-${randomUUID()}.log`) }),
|
|
181
|
+
...(options.imageResizer === undefined ? {} : { imageResizer: options.imageResizer }),
|
|
182
|
+
});
|
|
183
|
+
handler = activeHandler;
|
|
184
|
+
cellManager.markRunning(
|
|
185
|
+
cell,
|
|
186
|
+
kernel,
|
|
187
|
+
() => activeHandler.liveResult(),
|
|
188
|
+
(error) => execution.cancel(error),
|
|
189
|
+
);
|
|
190
|
+
// Includes a steer already queued at execute start or received while acquiring the kernel.
|
|
191
|
+
onReady();
|
|
192
|
+
if ("setContext" in options.kernelManager && typeof options.kernelManager.setContext === "function") {
|
|
193
|
+
options.kernelManager.setContext(bridgeContext);
|
|
194
|
+
}
|
|
195
|
+
if (invocation.input.reset) await execution.wait(kernel.reset());
|
|
196
|
+
const result = await execution.wait(kernel.run({ cellId: invocation.cellId, code: invocation.input.code }));
|
|
197
|
+
if (result.ok && state.pendingBridgeCalls.length > 0)
|
|
198
|
+
await execution.wait(Promise.all(state.pendingBridgeCalls));
|
|
199
|
+
return await handler.finalize(result);
|
|
200
|
+
};
|
|
201
|
+
const kernelTools = jsKernelTools(kernel, invocation.input.language);
|
|
202
|
+
return kernelTools ? await kernelToolsStorage.run(kernelTools, runBound) : await runBound();
|
|
197
203
|
} catch (error) {
|
|
198
204
|
if (handler && error instanceof Error && error.name === "CodemodeSessionDisposedError")
|
|
199
205
|
return await handler.finalizeCancellation(error);
|
|
@@ -206,3 +212,16 @@ async function executeCell(
|
|
|
206
212
|
if (handler) await handler.flushOutput();
|
|
207
213
|
}
|
|
208
214
|
}
|
|
215
|
+
|
|
216
|
+
function jsKernelTools(kernel: EvalKernel, language: string): KernelToolsCapability | undefined {
|
|
217
|
+
if (language !== "js") return undefined;
|
|
218
|
+
if (!("describeKernelTools" in kernel) || typeof kernel.describeKernelTools !== "function") return undefined;
|
|
219
|
+
const js = kernel as EvalKernel & {
|
|
220
|
+
describeKernelTools: KernelToolsCapability["describe"];
|
|
221
|
+
invokeKernelTool: KernelToolsCapability["invoke"];
|
|
222
|
+
};
|
|
223
|
+
return {
|
|
224
|
+
describe: (names) => js.describeKernelTools(names),
|
|
225
|
+
invoke: (request, signal) => js.invokeKernelTool(request, signal),
|
|
226
|
+
};
|
|
227
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { EvalDetachedCellSnapshot } from "./detached-cell-contract.ts";
|
|
2
|
+
|
|
3
|
+
export const TERMINAL_SNAPSHOT_CAP = 32;
|
|
4
|
+
|
|
5
|
+
/** Bounded LRU of settled-cell snapshots; without it every settled cell pins its result and closures for the session lifetime (#1695). */
|
|
6
|
+
export class TerminalSnapshotStore {
|
|
7
|
+
readonly #snapshots = new Map<string, EvalDetachedCellSnapshot>();
|
|
8
|
+
readonly #cap: number;
|
|
9
|
+
|
|
10
|
+
constructor(cap = TERMINAL_SNAPSHOT_CAP) {
|
|
11
|
+
this.#cap = cap;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
remember(snapshot: EvalDetachedCellSnapshot): void {
|
|
15
|
+
this.#snapshots.delete(snapshot.cellId);
|
|
16
|
+
this.#snapshots.set(snapshot.cellId, snapshot);
|
|
17
|
+
while (this.#snapshots.size > this.#cap) {
|
|
18
|
+
const oldest = this.#snapshots.keys().next();
|
|
19
|
+
if (oldest.done === true) break;
|
|
20
|
+
this.#snapshots.delete(oldest.value);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
get(cellId: string): EvalDetachedCellSnapshot | undefined {
|
|
25
|
+
return this.#snapshots.get(cellId);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
delete(cellId: string): void {
|
|
29
|
+
this.#snapshots.delete(cellId);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
clear(): void {
|
|
33
|
+
this.#snapshots.clear();
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { AgentToolResult } from "@code-yeongyu/senpi";
|
|
2
|
+
|
|
3
|
+
export function marshalToolResult(result: AgentToolResult<unknown>) {
|
|
4
|
+
const texts = result.content.filter((part) => part.type === "text").map((part) => part.text);
|
|
5
|
+
const images = result.content
|
|
6
|
+
.filter((part) => part.type === "image")
|
|
7
|
+
.map((part) => ({ mimeType: part.mimeType, dataBase64: part.data }));
|
|
8
|
+
const details =
|
|
9
|
+
typeof result.details === "object" &&
|
|
10
|
+
result.details !== null &&
|
|
11
|
+
!Array.isArray(result.details) &&
|
|
12
|
+
Object.keys(result.details).length === 0
|
|
13
|
+
? undefined
|
|
14
|
+
: result.details;
|
|
15
|
+
const hasError = toolResultIsError(result);
|
|
16
|
+
const text = texts.join("\n");
|
|
17
|
+
return images.length === 0 && details === undefined && !hasError ? { text } : { text, details, images, hasError };
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function toolResultIsError(result: AgentToolResult<unknown>): boolean {
|
|
21
|
+
const details = result.details;
|
|
22
|
+
return typeof details === "object" && details !== null && "isError" in details && details.isError === true;
|
|
23
|
+
}
|
package/src/tool/types.ts
CHANGED
|
@@ -132,6 +132,8 @@ export interface EvalKernel {
|
|
|
132
132
|
deliverToolReply(message: Extract<HostToKernelMessage, { type: "tool-reply" }>): void;
|
|
133
133
|
reset(): Promise<void>;
|
|
134
134
|
close(): Promise<void>;
|
|
135
|
+
/** Names this kernel has registered; JS collides with other languages in the same session. */
|
|
136
|
+
listKernelToolNames?(): readonly string[];
|
|
135
137
|
}
|
|
136
138
|
|
|
137
139
|
export interface EvalKernelManager {
|