fix(coding-agent): 解析所有最终 Promise 表达式
This commit is contained in:
@@ -187,10 +187,7 @@ function returnFinalExpression(code: string): { source: string; returned: boolea
|
||||
const suffix = code.slice(expression.end);
|
||||
const semicolonMatch = statement.match(/;\s*$/);
|
||||
const trimmedStatement = semicolonMatch ? statement.slice(0, semicolonMatch.index) : statement;
|
||||
const needsAwait =
|
||||
expression.expression?.type === "AwaitExpression" || expression.expression?.type === "CallExpression";
|
||||
const displayExpression = needsAwait ? `await (${trimmedStatement})` : trimmedStatement;
|
||||
return { source: `${prefix}__omp_display__(${displayExpression});${suffix}`, returned: true };
|
||||
return { source: `${prefix}globalThis.__omp_final_expr__ = (${trimmedStatement});${suffix}`, returned: true };
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -82,9 +82,16 @@ export class JsRuntime {
|
||||
}
|
||||
|
||||
async run(code: string, filename?: string): Promise<unknown> {
|
||||
Reflect.deleteProperty(globalThis, "__omp_final_expr__");
|
||||
const wrapped = wrapCode(code);
|
||||
const value = indirectEval(wrapped.source, filename);
|
||||
return await awaitMaybePromise(value);
|
||||
const awaited = await awaitMaybePromise(value);
|
||||
if (Reflect.has(globalThis, "__omp_final_expr__")) {
|
||||
const finalValue = (globalThis as { __omp_final_expr__?: unknown }).__omp_final_expr__;
|
||||
Reflect.deleteProperty(globalThis, "__omp_final_expr__");
|
||||
return await awaitMaybePromise(finalValue);
|
||||
}
|
||||
return awaited;
|
||||
}
|
||||
|
||||
displayValue(value: unknown): void {
|
||||
|
||||
@@ -281,6 +281,17 @@ describe("executeJs", () => {
|
||||
expect(result.output.trim()).toBe('{\n "name": "demo",\n "enabled": true\n}');
|
||||
});
|
||||
|
||||
it("awaits identifier promise final expressions before displaying", async () => {
|
||||
const result = await executeJs("const pending = read('config.json'); pending;", {
|
||||
sessionId,
|
||||
session,
|
||||
sessionFile,
|
||||
});
|
||||
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.output.trim()).toBe('{\n "name": "demo",\n "enabled": true\n}');
|
||||
});
|
||||
|
||||
it("auto-displays returned objects as structured output", async () => {
|
||||
const result = await executeJs("return { answer: 42, nested: { ok: true } };", {
|
||||
sessionId,
|
||||
|
||||
Reference in New Issue
Block a user