|
| 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 | +} |
0 commit comments