Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix ambigous error and cache #20

Merged
merged 2 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/actions/StepPerformer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ describe('StepPerformer', () => {
mockCodeEvaluator.evaluate.mockRejectedValue(new Error('Evaluation failed'));

await expect(stepPerformer.perform(intent)).rejects.toThrow('Evaluation failed');
expect(fs.writeFileSync).not.toHaveBeenCalled(); // Cache should not be saved
expect(fs.writeFileSync).toHaveBeenCalled(); // Cache should be saved
});

it('should use cached prompt result if available', async () => {
Expand Down
29 changes: 11 additions & 18 deletions src/actions/StepPerformer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { PromptCreator } from '@/utils/PromptCreator';
import { CodeEvaluator } from '@/utils/CodeEvaluator';
import { SnapshotManager } from '@/utils/SnapshotManager';
import {CodeEvaluationResult, PreviousStep, PromptHandler, TestingFrameworkDriver} from '@/types';
import {PromptCreator} from '@/utils/PromptCreator';
import {CodeEvaluator} from '@/utils/CodeEvaluator';
import {SnapshotManager} from '@/utils/SnapshotManager';
import {CodeEvaluationResult, PreviousStep, PromptHandler} from '@/types';
import * as fs from 'fs';
import * as path from 'path';
import * as crypto from 'crypto';
Expand Down Expand Up @@ -81,11 +81,11 @@ export class StepPerformer {

try {
promptResult = await this.promptHandler.runPrompt(prompt, snapshot);
const result = await this.codeEvaluator.evaluate(promptResult, this.context);
// Cache the result
this.cache.set(cacheKey, promptResult);
this.saveCacheToFile();
return result;

return await this.codeEvaluator.evaluate(promptResult, this.context);
} catch (error) {
// Extend 'previous' array with the failure message
const failedAttemptMessage = promptResult
Expand All @@ -98,13 +98,6 @@ export class StepPerformer {
result: undefined,
}];

const retryCacheKey = this.generateCacheKey(step, newPrevious, viewHierarchy);

if (this.cache.has(retryCacheKey)) {
const cachedRetryPromptResult = this.cache.get(retryCacheKey);
return this.codeEvaluator.evaluate(cachedRetryPromptResult, this.context);
}

const retryPrompt = this.promptCreator.createPrompt(
step,
viewHierarchy,
Expand All @@ -114,15 +107,15 @@ export class StepPerformer {

try {
const retryPromptResult = await this.promptHandler.runPrompt(retryPrompt, snapshot);
const retryResult = await this.codeEvaluator.evaluate(
retryPromptResult,
this.context,
);

// Cache the result under the original cache key
this.cache.set(cacheKey, retryPromptResult);
this.saveCacheToFile();
return retryResult;

return await this.codeEvaluator.evaluate(
retryPromptResult,
this.context,
);
} catch (retryError) {
// Throw the original error if retry fails
throw error;
Expand Down
1 change: 1 addition & 0 deletions src/utils/PromptCreator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export class PromptCreator {
"",
"You are an AI assistant tasked with generating test code for a mobile application using the provided UI testing framework API.",
"Please generate the minimal executable code to perform the desired intent based on the given information and context.",
"If generating the relevant code is not possible due to ambiguity, invalid intent, or inability to find the desired element, return code that throws an informative error explaining the problem in one sentence.",
""
];
}
Expand Down
3 changes: 3 additions & 0 deletions src/utils/__snapshots__/PromptCreator.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ exports[`PromptCreator should create a prompt for an intent correctly 1`] = `

You are an AI assistant tasked with generating test code for a mobile application using the provided UI testing framework API.
Please generate the minimal executable code to perform the desired intent based on the given information and context.
If generating the relevant code is not possible due to ambiguity, invalid intent, or inability to find the desired element, return code that throws an informative error explaining the problem in one sentence.

## Context

Expand Down Expand Up @@ -111,6 +112,7 @@ exports[`PromptCreator should handle when no snapshot image is attached 1`] = `

You are an AI assistant tasked with generating test code for a mobile application using the provided UI testing framework API.
Please generate the minimal executable code to perform the desired intent based on the given information and context.
If generating the relevant code is not possible due to ambiguity, invalid intent, or inability to find the desired element, return code that throws an informative error explaining the problem in one sentence.

## Context

Expand Down Expand Up @@ -212,6 +214,7 @@ exports[`PromptCreator should include previous intents in the context 1`] = `

You are an AI assistant tasked with generating test code for a mobile application using the provided UI testing framework API.
Please generate the minimal executable code to perform the desired intent based on the given information and context.
If generating the relevant code is not possible due to ambiguity, invalid intent, or inability to find the desired element, return code that throws an informative error explaining the problem in one sentence.

## Context

Expand Down
Loading