import type { HostToKernelMessage, KernelToHostMessage, } from "../../bridge/protocol.ts"; import { JavaScriptKernel, type JavaScriptKernelOptions, } from "../js/context-manager.ts"; import type { JavaScriptRunInput, ResultMessage } from "../js/kernel-contract.ts"; import type { EvalKernel, KernelInterruptHandle } from "../../tool/types.ts"; import { resetTypeScriptCellState, typeCheckCell, typeErrorsMessage, transpileTypeScriptCell, } from "./checker.ts"; export type TypeScriptKernelOptions = JavaScriptKernelOptions; /** * Type-checked TypeScript kernel. Each cell is type-checked (wrapped in one * async function, type-correctness codes filtered) before it runs; a cell * with errors is rejected without touching the worker. A clean cell is * transpiled to JavaScript and runs in the JS worker, so it inherits the JS * kernel's state persistence, tool bridge, strings, timeouts, and interrupts. */ export class TypeScriptKernel implements EvalKernel { readonly #inner: JavaScriptKernel; constructor(options: TypeScriptKernelOptions) { this.#inner = new JavaScriptKernel(options); } async run(input: JavaScriptRunInput): Promise { const startedAtMs = performance.now(); const errors = typeCheckCell(input.code); if (errors.length > 0) { return { type: "result", cellId: input.cellId, ok: false, error: { message: typeErrorsMessage(errors) }, durationMs: Math.max(0, Math.round(performance.now() - startedAtMs)), }; } return await this.#inner.run({ ...input, code: transpileTypeScriptCell(input.code), }); } async close(): Promise { await this.#inner.close(); } async reset(): Promise { resetTypeScriptCellState(); await this.#inner.reset(); } interrupt(reason?: string): Promise { return this.#inner.interrupt(reason); } deliverToolReply( message: Extract ): void { this.#inner.deliverToolReply(message); } }