-
Notifications
You must be signed in to change notification settings - Fork 533
feat: durable REPL js tool - help(), emit(), agent tool definition #154
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
Draft
ben-reitz
wants to merge
3
commits into
feat/durable-repl-capabilities
from
feat/durable-repl-js-tool
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| // Unit tests for the framework-agnostic `js` tool definition: generated | ||
| // description, JSON Schema shape, session routing, input validation, and | ||
| // the shared text renderer. Real eval behavior is covered by the workerd | ||
| // integration suites; here the workspace is a recording fake. | ||
|
|
||
| import { describe, expect, it } from "vitest"; | ||
|
|
||
| import { capability, fetchCapability } from "./capability.js"; | ||
| import { createJsToolDefinition, type JsToolOptions, renderJsResultText } from "./tool.js"; | ||
| import type { ReplExecutionResult } from "./types.js"; | ||
|
|
||
| interface ReplCall { | ||
| name: string; | ||
| options: Record<string, unknown>; | ||
| code?: string; | ||
| } | ||
|
|
||
| function fakeWorkspace(calls: ReplCall[]) { | ||
| return { | ||
| repl(name: string, options: Record<string, unknown>) { | ||
| const call: ReplCall = { name, options }; | ||
| calls.push(call); | ||
| return { | ||
| eval(code: string): Promise<ReplExecutionResult> { | ||
| call.code = code; | ||
| return Promise.resolve({ | ||
| code, | ||
| value: `ran in ${name}`, | ||
| logs: { entries: [] }, | ||
| results: [], | ||
| executionCount: 1, | ||
| }); | ||
| }, | ||
| }; | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| const LOADER = {} as JsToolOptions["loader"]; | ||
|
|
||
| function makeTool(overrides: Partial<JsToolOptions> = {}) { | ||
| const calls: ReplCall[] = []; | ||
| const definition = createJsToolDefinition({ | ||
| workspace: fakeWorkspace(calls), | ||
| loader: LOADER, | ||
| ...overrides, | ||
| }); | ||
| return { definition, calls }; | ||
| } | ||
|
|
||
| describe("createJsToolDefinition", () => { | ||
| it("describes persistence, built-ins, and each grant with its description", () => { | ||
| const { definition } = makeTool({ | ||
| capabilities: { | ||
| weather: capability({ get: () => 1 }, { description: "Weather lookups" }), | ||
| fetch: fetchCapability({ allow: ["api.example.com"] }), | ||
| bare: capability(() => 0), | ||
| }, | ||
| }); | ||
| expect(definition.name).toBe("js"); | ||
| expect(definition.description).toContain("persistent"); | ||
| expect(definition.description).toContain("emit(value)"); | ||
| expect(definition.description).toContain('session "main"'); | ||
| expect(definition.description).toContain("weather (Weather lookups)"); | ||
| expect(definition.description).toContain("fetch (HTTP fetch restricted to: api.example.com)"); | ||
| expect(definition.description).toContain("bare"); | ||
| expect(definition.description).toContain("async — always `await`"); | ||
| expect(definition.description).toContain('help("name")'); | ||
| }); | ||
|
|
||
| it("says so when nothing is granted", () => { | ||
| const { definition } = makeTool(); | ||
| expect(definition.description).toContain("No capabilities are granted"); | ||
| expect(definition.description).toContain("help()"); | ||
| }); | ||
|
|
||
| it("names the configured default session in description and schema", () => { | ||
| const { definition } = makeTool({ | ||
| defaultSession: "scratch", | ||
| capabilities: { weather: capability({ get: () => 1 }) }, | ||
| }); | ||
| expect(definition.description).toContain('session "scratch"'); | ||
| expect(JSON.stringify(definition.inputSchema)).toContain('\\"scratch\\"'); | ||
| }); | ||
|
|
||
| it("exposes a plain JSON Schema requiring only code", () => { | ||
| const { definition } = makeTool(); | ||
| expect(definition.inputSchema.type).toBe("object"); | ||
| expect(definition.inputSchema.required).toEqual(["code"]); | ||
| expect(definition.inputSchema.additionalProperties).toBe(false); | ||
| const code = definition.inputSchema.properties.code as { type: string }; | ||
| const sessionName = definition.inputSchema.properties.sessionName as { type: string }; | ||
| expect(code.type).toBe("string"); | ||
| expect(sessionName.type).toBe("string"); | ||
| }); | ||
|
|
||
| it("routes to the default session and forwards grants and limits", async () => { | ||
| const weather = capability({ get: () => 1 }); | ||
| const { definition, calls } = makeTool({ | ||
| capabilities: { weather }, | ||
| timeoutMs: 5_000, | ||
| maxEffectBytes: 1_024, | ||
| }); | ||
| const result = await definition.run({ code: "1 + 1" }); | ||
| expect(result.value).toBe("ran in main"); | ||
| expect(calls).toHaveLength(1); | ||
| expect(calls[0].name).toBe("main"); | ||
| expect(calls[0].code).toBe("1 + 1"); | ||
| expect(calls[0].options).toEqual({ | ||
| loader: LOADER, | ||
| capabilities: { weather }, | ||
| timeoutMs: 5_000, | ||
| maxEffectBytes: 1_024, | ||
| }); | ||
| }); | ||
|
|
||
| it("routes sessionName overrides and omits unset limits", async () => { | ||
| const { definition, calls } = makeTool(); | ||
| const result = await definition.run({ code: "2", sessionName: "notes" }); | ||
| expect(result.value).toBe("ran in notes"); | ||
| expect(calls[0].name).toBe("notes"); | ||
| expect(calls[0].options).toEqual({ loader: LOADER, capabilities: {} }); | ||
| }); | ||
|
|
||
| it("rejects caller mistakes with TypeErrors", async () => { | ||
| const { definition } = makeTool(); | ||
| await expect(definition.run({ code: 1 as unknown as string })).rejects.toThrow(TypeError); | ||
| await expect(definition.run({ code: "1", sessionName: "" })).rejects.toThrow(TypeError); | ||
| expect(() => createJsToolDefinition({ | ||
| workspace: fakeWorkspace([]), | ||
| loader: LOADER, | ||
| defaultSession: "", | ||
| })).toThrow(TypeError); | ||
| }); | ||
| }); | ||
|
|
||
| describe("renderJsResultText", () => { | ||
| const base = { code: "", logs: { entries: [] }, results: [], executionCount: 1 }; | ||
|
|
||
| it("renders logs, emits, and value in execution order", () => { | ||
| const text = renderJsResultText({ | ||
| ...base, | ||
| value: { total: 3 }, | ||
| logs: { entries: [{ level: "warn" as const, text: "careful" }], dropped: 2 }, | ||
| results: [{ text: "emitted-one", value: 1 }], | ||
| }); | ||
| expect(text).toBe( | ||
| '[warn] careful\n(2 more log entries dropped)\nemitted-one\nvalue: {"total":3}', | ||
| ); | ||
| }); | ||
|
|
||
| it("renders structured errors with kind and traceback", () => { | ||
| const text = renderJsResultText({ | ||
| ...base, | ||
| error: { | ||
| name: "StaleLeaseError", | ||
| message: "handle died", | ||
| kind: "stale-lease", | ||
| traceback: "at cell:1", | ||
| }, | ||
| }); | ||
| expect(text).toBe("StaleLeaseError [stale-lease]: handle died\nat cell:1"); | ||
| }); | ||
|
|
||
| it("renders undefined and unstringifiable values honestly", () => { | ||
| expect(renderJsResultText({ ...base, value: undefined })).toBe("value: undefined"); | ||
| expect(renderJsResultText({ ...base, value: 10n })).toBe('value: "10n"'); | ||
| expect(renderJsResultText(base)).toBe("value: undefined"); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.