Skip to content

Commit

Permalink
gen2e-interpreter: pass in save context flags + better [test,task] id…
Browse files Browse the repository at this point in the history
… management
  • Loading branch information
rhighs committed Jun 14, 2024
1 parent 8f248fd commit bf3f654
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 34 deletions.
2 changes: 1 addition & 1 deletion packages/gen2e-interpreter/src/ast/gen2e-sanitize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const filterNode = (node: any): boolean => {
: undefined;

if (node.type === "IfStatement") {
node.consequent.body.filter(filterNode)
node.consequent.body.filter(filterNode);
return true;
}

Expand Down
46 changes: 29 additions & 17 deletions packages/gen2e-interpreter/src/interpreter/recording-interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ export class RecordingInterpreter {
private currentStore: StaticStore;
private getStaticMem: () => Gen2EInterpreterInMemStatic;

private testTitle: string = "gen2e-recording-interpreter generated test";
private runTimestamp: string;

private tasks: string[] = [];

constructor(
Expand All @@ -67,7 +70,8 @@ export class RecordingInterpreter {
this.logger.config(config.logger);
}

const [getMem, staticStore] = inMemStore();
this.runTimestamp = (+new Date()).toString();
const [getMem, staticStore] = inMemStore(this.runTimestamp);
this.currentStore = staticStore;
this.getStaticMem = getMem;

Expand Down Expand Up @@ -195,7 +199,7 @@ export class RecordingInterpreter {

this.gen2eExpressions = [];
if (this.mode === "playwright") {
const [getMem, store] = inMemStore();
const [getMem, store] = inMemStore(this.runTimestamp);
this.browser = new Gen2EBrowser(this.config.browserOptions);
await this.browser.startup();
this.currentStore = store;
Expand Down Expand Up @@ -253,10 +257,8 @@ export class RecordingInterpreter {
}

this.gen2eExpressions.push(result);
const [getMem, localStore] = inMemStore();
const fakeTestSource = generateFakeTestCode("gen2e - interpreter gen", [
result,
]);
const [getMem, localStore] = inMemStore(this.runTimestamp);
const fakeTestSource = generateFakeTestCode(this.testTitle, [result]);
const page = this.browser.page;

if (this.options.debug) {
Expand All @@ -269,15 +271,22 @@ export class RecordingInterpreter {
localStore
);

if (seResult.type === "success" && Object.keys(getMem()).length > 0) {
const mem = getMem();
const [[k, v]] = Object.entries(mem);
this.currentStore.makeStatic({ ident: k, expression: v });
const mem = getMem();
if (this.options.debug) {
this.logger.debug("SANDBOX EVAL temp mem:", getMem());
}

if (seResult.type === "success" && Object.keys(mem).length > 0) {
let code = "";
for (let [k, v] of Object.entries(mem)) {
this.currentStore.makeStatic(k, v);
code += v.expression + "\n";
}

if (this.options.debug) {
this.logger.debug("sandbox memory", this.getStaticMem());
this.logger.debug("playwright mode", v);
this.logger.debug("playwright mode", code);
}
return v;
return code;
} else if (seResult.type === "error") {
this.gen2eExpressions.pop();
this.logger.warn("sandbox execution has failed, ignoring...");
Expand All @@ -299,7 +308,9 @@ export class RecordingInterpreter {
}

this.gen2eExpressions.push(result);
this.logger.debug("gen2e mode", result);
if (this.options.debug) {
this.logger.debug("gen2e mode", result);
}
return result;
}

Expand All @@ -323,6 +334,7 @@ export class RecordingInterpreter {
openaiApiKey: this.options.openaiApiKey,
debug: this.options.debug,
policies: this.options.policies,
saveContext: true,
},
(code: string, page: Page) => {
const evalFunc = new Function(
Expand Down Expand Up @@ -384,14 +396,14 @@ export class RecordingInterpreter {
case "playwright":
{
const fakeTestSource = generateFakeTestCode(
"gen2e - interpreter gen",
this.testTitle,
this.gen2eExpressions,
false
);
gen2eCode = fakeTestSource;

try {
code = pwCompile(fakeTestSource, this.currentStore);
code = pwCompile(fakeTestSource, this.currentStore, true);
} catch (error) {
this.logger.error(error);
}
Expand All @@ -400,7 +412,7 @@ export class RecordingInterpreter {
case "gen2e":
{
gen2eCode = generateFakeTestCode(
"gen2e - interpreter gen",
this.testTitle,
this.gen2eExpressions,
false
);
Expand Down
24 changes: 10 additions & 14 deletions packages/gen2e-interpreter/src/interpreter/store.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
import { StaticStore } from "@rhighs/gen2e";
import { StaticStore, StaticGenStep } from "@rhighs/gen2e";

export type Gen2EInterpreterInMemStatic = { [key: string]: string };
export type Gen2EInterpreterInMemStatic = { [key: string]: StaticGenStep };

export const inMemStore = (): [
() => Gen2EInterpreterInMemStatic,
StaticStore
] => {
const inMemoryStatic: { [key: string]: string } = {};
export const inMemStore = (
id: string
): [() => Gen2EInterpreterInMemStatic, StaticStore] => {
const inMemoryStatic: Gen2EInterpreterInMemStatic = {};
const staticStore: StaticStore = {
makeIdent: (title, task) => `gen2.interpreter - [${title}](${task})`,
fetchStatic: (ident: string) => ({
ident,
expression: inMemoryStatic[ident],
}),
makeStatic: (content) =>
(inMemoryStatic[content.ident] = content.expression),
makeIdent: (title, task) => `[${title}](${task}) ${id}`,
fetchStatic: (ident: string) => inMemoryStatic[ident],
makeStatic: (ident: string, content: StaticGenStep) =>
(inMemoryStatic[ident] = content),
};
return [() => inMemoryStatic, staticStore];
};
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ class TasksInterpreter {
private fallbackModel: string = env.OPENAI_MODEL;
private llmCallHooks: Gen2ELLMCallHooks;

private testTitle: string = "gen2e-recording-interpreter generated test";
private runTimestamp: string;

constructor(
config: Gen2EInterpreterConfig,
options: Gen2EInterpreterOptions
Expand All @@ -53,6 +56,7 @@ class TasksInterpreter {
if (config.logger) {
this.logger.config(config.logger);
}
this.runTimestamp = (+new Date()).toString();

this.recordModelsUsage = options.recordUsage ?? false;
if (this.recordModelsUsage) {
Expand Down Expand Up @@ -303,7 +307,7 @@ class TasksInterpreter {
let result: Gen2EInterpreterResult;
switch (this.mode) {
case "playwright":
const [_mem, staticStore] = inMemStore();
const [_mem, staticStore] = inMemStore(this.runTimestamp);
result = await this._runTasks_mode_playwright(tasks, staticStore);
break;
case "gen2e":
Expand Down
3 changes: 2 additions & 1 deletion packages/gen2e-interpreter/src/interpreter/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Gen2EGenPolicies } from "@rhighs/gen2e";
import { Gen2EBrowserOptions } from "./browser";
import { Gen2ELogger } from "@rhighs/gen2e-logger";
import { Gen2EInterpreterInMemStatic } from "./store";

export type Gen2EInterpreterEvent =
| "start"
Expand Down Expand Up @@ -63,5 +64,5 @@ export type Gen2ERecordingPeekResult = {
tasks: string[];
gen2eCode: string;
code: string;
mem: { [key: string]: string };
mem: Gen2EInterpreterInMemStatic;
};

0 comments on commit bf3f654

Please sign in to comment.