Skip to content

Commit 852237d

Browse files
committed
feat: add function for augmenting generated files
Adds the ability for the user to augment a generated response before it is evaluated.
1 parent a5c5f9c commit 852237d

File tree

5 files changed

+29
-3
lines changed

5 files changed

+29
-3
lines changed

runner/configuration/environment-config.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@ import {
88
LocalExecutorConfig,
99
localExecutorConfigSchema,
1010
} from '../orchestration/executors/local-executor-config.js';
11-
import {PromptDefinition, RatingContextFilter, ReportContextFilter} from '../shared-interfaces.js';
11+
import {
12+
LlmResponseFile,
13+
PromptDefinition,
14+
RatingContextFilter,
15+
ReportContextFilter,
16+
} from '../shared-interfaces.js';
1217
import type {Environment} from './environment.js';
1318
import type {GenkitRunner} from '../codegen/genkit/genkit-runner.js';
1419

@@ -127,6 +132,13 @@ export const environmentConfigSchema = z.object({
127132
augmentExecutablePrompt: z
128133
.function(z.tuple([z.custom<PromptAugmentationContext>()]), z.promise(z.string()))
129134
.optional(),
135+
136+
/**
137+
* Function that can be used to augment generated files before they're evaluated.
138+
*/
139+
augmentGeneratedFile: z
140+
.function(z.tuple([z.custom<Readonly<LlmResponseFile>>()]), z.string())
141+
.optional(),
130142
});
131143

132144
/**

runner/configuration/environment.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {Executor} from '../orchestration/executors/executor.js';
55
import {Rating, RatingCategory} from '../ratings/rating-types.js';
66
import {
77
FrameworkInfo,
8+
LlmResponseFile,
89
MultiStepPromptDefinition,
910
PromptDefinition,
1011
RatingContextFilter,
@@ -83,6 +84,9 @@ export class Environment {
8384
/** Runner that user can use to access an LLM to augment prompts. */
8485
private augmentationRunner: GenkitRunner | null = null;
8586

87+
/** User-provided callback for augmenting the LLM-generated files. */
88+
private readonly augmentFileCallback: ((file: LlmResponseFile) => string) | null;
89+
8690
constructor(
8791
rootPath: string,
8892
private readonly config: EnvironmentConfig & Required<Pick<EnvironmentConfig, 'executor'>>,
@@ -114,6 +118,7 @@ export class Environment {
114118
this.ratingHash = this.getRatingHash(this.ratings, this.ratingCategories);
115119
this.analysisPrompts = this.resolveAnalysisPrompts(config);
116120
this.augmentExecutablePrompt = config.augmentExecutablePrompt || null;
121+
this.augmentFileCallback = config.augmentGeneratedFile || null;
117122
this.validateRatingHash(this.ratingHash, config);
118123
}
119124

@@ -191,6 +196,13 @@ export class Environment {
191196
});
192197
}
193198

199+
/** Augments response files based on the user's configuration. */
200+
augmentResponseFiles(files: LlmResponseFile[]): void {
201+
if (this.augmentFileCallback) {
202+
files.forEach(file => (file.code = this.augmentFileCallback!(file)));
203+
}
204+
}
205+
194206
async destroy(): Promise<void> {
195207
await this.executor.destroy();
196208

runner/orchestration/codegen.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ export async function repairCodeWithAI(
129129
);
130130

131131
if (response.success) {
132+
env.augmentResponseFiles(response.outputFiles);
132133
progress.log(
133134
promptDef,
134135
'codegen',

runner/orchestration/generate-initial-files.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ export async function generateInitialFiles(
7575
);
7676

7777
if (response.success) {
78+
env.augmentResponseFiles(response.outputFiles);
7879
progress.log(
7980
promptDef,
8081
'codegen',
@@ -90,7 +91,7 @@ export async function generateInitialFiles(
9091
}
9192

9293
return {
93-
files: response.outputFiles!,
94+
files: response.outputFiles,
9495
usage: response.usage,
9596
reasoning: response.reasoning,
9697
toolLogs: response.toolLogs,

runner/orchestration/repair.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ async function handleRepairResponse(
126126
const newAttemptFiles = previousAttemptFiles.map(f => ({...f}));
127127

128128
mergeRepairFiles(repairResponse.outputFiles, newAttemptFiles);
129-
writeResponseFiles(directory, newAttemptFiles, env, rootPromptDef.name);
129+
await writeResponseFiles(directory, newAttemptFiles, env, rootPromptDef.name);
130130

131131
const buildResult = await runBuild(
132132
evalID,

0 commit comments

Comments
 (0)