With replScoped: true, persistent-in-memory runs each call as a separate VM Script in one retained context. Each call starts with copies of the top-level bindings left by earlier calls, so a later call may declare any earlier name again with any keyword while earlier functions and still-running code keep their values. Results, top-level await, errors and asynchronous failures follow the existing REPL evaluator; modules load only through dynamic import(), resolved from the environment's first working directory. The control protocol moves to version 3; the upgrade guide covers preinstalled bootstraps and the new PtcProgramFormat member. The Agent Note records the decision and its comparison with node:repl and vm-script, and the ptc-scoped-repl snapshot pins the model guidance.
28 lines
1.4 KiB
TypeScript
28 lines
1.4 KiB
TypeScript
/** Select one child evaluator without process-lifetime policy. */
|
|
import type { PtcProgramFormat } from '@deepseek-ai/dsh-ptc-runtime'
|
|
import { createAsyncFunctionEvaluator } from './async-function.ts'
|
|
import { createReplEvaluator } from './repl.ts'
|
|
import { createScopedReplEvaluator } from './scoped-repl-evaluator.ts'
|
|
import { createVmScriptEvaluator } from './vm-script.ts'
|
|
import type { ProgramEvaluator, ProgramEvaluatorOptions, ProgramImportHandler } from './types.ts'
|
|
|
|
const evaluators: Record<PtcProgramFormat, (options: ProgramEvaluatorOptions) => ProgramEvaluator> = {
|
|
'function-body': createAsyncFunctionEvaluator,
|
|
repl: createReplEvaluator,
|
|
script: createVmScriptEvaluator,
|
|
'scoped-repl': createScopedReplEvaluator,
|
|
}
|
|
|
|
/**
|
|
* Create the selected evaluator for either a fresh or retained process.
|
|
* @param format - Evaluation syntax selected by the host.
|
|
* @param reportError - Asynchronous diagnostics attributed to the active evaluation.
|
|
* @param importHandler - Runner-local import handling; omission uses Node's loader.
|
|
* @returns Globals, evaluation entry point, and resource cleanup.
|
|
*/
|
|
export function createProgramEvaluator(
|
|
format: PtcProgramFormat, reportError: (message: string) => void, importHandler?: ProgramImportHandler,
|
|
): ProgramEvaluator {
|
|
return evaluators[format]({ reportError, ...importHandler === undefined ? {} : { importHandler } })
|
|
}
|