Skip to content

Commit a4b50ec

Browse files
committed
refactor: improve remote environment example by providing a fake gateway
1 parent 41ada54 commit a4b50ec

File tree

3 files changed

+114
-3
lines changed

3 files changed

+114
-3
lines changed
Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1+
// @ts-check
2+
3+
/**
4+
* @import {RemoteEnvironmentConfig} from 'web-codegen-scorer';
5+
*/
6+
17
import { getBuiltInRatings } from 'web-codegen-scorer';
8+
import { FakeRemoteGateway } from './fake-gateway';
29

3-
/** @type {import("web-codegen-scorer").EnvironmentConfig} */
10+
/** @type {RemoteEnvironmentConfig} */
411
export default {
512
displayName: 'Remote Env (example)',
613
clientSideFramework: 'angular',
7-
sourceDirectory: './project',
814
ratings: getBuiltInRatings(),
915
generationSystemPrompt: './system-instructions.md',
1016
executablePrompts: ['../../prompts/**/*.md'],
11-
packageManager: 'npm',
17+
gateway: new FakeRemoteGateway(),
1218
};
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import {
2+
BuildResult,
3+
BuildResultStatus,
4+
EvalID,
5+
Gateway,
6+
LlmContextFile,
7+
LlmResponse,
8+
LlmResponseFile,
9+
RemoteEnvironment,
10+
RootPromptDefinition,
11+
} from '../../../runner';
12+
import { LlmGenerateFilesContext } from '../../../runner/codegen/llm-runner';
13+
import { ProgressLogger } from '../../../runner/progress/progress-logger';
14+
15+
export class FakeRemoteGateway implements Gateway<RemoteEnvironment> {
16+
ids = 0;
17+
18+
async initializeEval() {
19+
// Initialize an eval for a prompt.
20+
// The IDs will be used throughout invocations below and can be used to
21+
// persist data on a remote service while the eval runs
22+
// (e.g. for maintaining a build sandbox)
23+
return `${this.ids++}` as EvalID;
24+
}
25+
26+
async performFakeLlmRequest(): Promise<LlmResponse> {
27+
return {
28+
success: true,
29+
outputFiles: [{ code: 'Works!', filePath: 'main.ts' }],
30+
reasoning: '',
31+
errors: [],
32+
usage: { inputTokens: 0, totalTokens: 0, outputTokens: 0 },
33+
};
34+
}
35+
36+
generateInitialFiles(
37+
id: EvalID,
38+
requestCtx: LlmGenerateFilesContext,
39+
model: string,
40+
contextFiles: LlmContextFile[],
41+
abortSignal: AbortSignal
42+
): Promise<LlmResponse> {
43+
// Generate the initial files of the eval app.
44+
// This generation can happen on a remote service with access to private models.
45+
return this.performFakeLlmRequest();
46+
}
47+
48+
repairBuild(
49+
id: EvalID,
50+
requestCtx: LlmGenerateFilesContext,
51+
model: string,
52+
errorMessage: string,
53+
appFiles: LlmResponseFile[],
54+
contextFiles: LlmContextFile[],
55+
abortSignal: AbortSignal
56+
): Promise<LlmResponse> {
57+
// Repair the given eval app.
58+
// This generation can happen on a remote service with access to private models.
59+
return this.performFakeLlmRequest();
60+
}
61+
62+
async serveBuild<T>(
63+
id: EvalID,
64+
env: RemoteEnvironment,
65+
appDirectoryPath: string,
66+
rootPromptDef: RootPromptDefinition,
67+
progress: ProgressLogger,
68+
logicWhileServing: (serveUrl: string) => Promise<T>
69+
): Promise<T> {
70+
// Start serving of the app.
71+
// Invoke the logic while the server is running.
72+
const result = await logicWhileServing('https://angular.dev');
73+
// Stop the server.
74+
return result;
75+
}
76+
77+
async tryBuild(
78+
id: EvalID,
79+
env: RemoteEnvironment,
80+
appDirectoryPath: string,
81+
rootPromptDef: RootPromptDefinition,
82+
progress: ProgressLogger
83+
): Promise<BuildResult> {
84+
// Here, building can happen in the remote service.
85+
// Eval ID is useful here for storing the build on a server, for re-using later when serving.
86+
return {
87+
message: 'Build successful',
88+
status: BuildResultStatus.SUCCESS,
89+
};
90+
}
91+
92+
shouldRetryFailedBuilds() {
93+
// Some environments have a builtin retry loop as part of initial generation.
94+
// In those cases, you may want to skip retrying.
95+
return true;
96+
}
97+
98+
async finalizeEval() {
99+
// Do your cleanup.
100+
}
101+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Follow instructions below CAREFULLY:
2+
3+
- Code MUST be implemented in my super secret framework.
4+
- Put the component code inside `src/app/app.ts`

0 commit comments

Comments
 (0)