Skip to content

Commit 55ee5e2

Browse files
authored
Add ContextArgs type export and update default context file template
Agent-Logs-Url: https://github.com/counterfact/api-simulator/sessions/e9e5f399-6483-4bfb-8c4e-1df903f65bfb
1 parent 22b22f0 commit 55ee5e2

4 files changed

Lines changed: 20 additions & 2 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"counterfact": minor
3+
---
4+
5+
Export `ContextArgs` type from generated `types/scenario-context.ts` so that `_.context.ts` files can strongly type the `loadContext` and `readJson` parameters received in the Context constructor. The default `_.context.ts` template now imports and uses `ContextArgs`.

src/typescript-generator/generate.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,11 @@ function buildApplyContextContent(contextFiles: ContextFileInfo[]): string {
256256
"/** A scenario function that receives the live REPL environment */",
257257
"export type Scenario = ($: ApplyContext) => Promise<void> | void;",
258258
"",
259+
"/** Arguments passed to the Context class constructor in _.context.ts files */",
260+
'export type ContextArgs = Pick<ApplyContext, "loadContext"> & {',
261+
" readJson: (relativePath: string) => Promise<unknown>;",
262+
"};",
263+
"",
259264
];
260265

261266
return parts.join("\n");

src/typescript-generator/repository.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,9 @@ export class Repository {
148148

149149
await fs.writeFile(
150150
contextFilePath,
151-
`/**
151+
`import type { ContextArgs } from "../types/scenario-context.js";
152+
153+
/**
152154
* This is the default context for Counterfact.
153155
*
154156
* It defines the context object in the REPL
@@ -159,7 +161,7 @@ export class Repository {
159161
* See https://counterfact.dev/docs/usage.html#working-with-state-the-codecontextcode-object-and-codecontexttscode
160162
*/
161163
export class Context {
162-
164+
constructor(_args: ContextArgs) {}
163165
}
164166
`,
165167
);

test/typescript-generator/generate.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ describe("scenario-context type generation", () => {
8080
expect(content).toContain(
8181
"export type Scenario = ($: ApplyContext) => Promise<void> | void;",
8282
);
83+
expect(content).toContain(
84+
'export type ContextArgs = Pick<ApplyContext, "loadContext"> & {',
85+
);
86+
expect(content).toContain(
87+
" readJson: (relativePath: string) => Promise<unknown>;",
88+
);
8389
});
8490
});
8591

0 commit comments

Comments
 (0)