Repository navigation
computer: Let the exec tool start runs with metadata #292
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
Open
+259
−8
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@cloudflare/computer": minor | ||
| --- | ||
|
|
||
| The `exec` tool can start each run with `metadata` for host modules. `createExecTool` takes `metadata` and the tool sets take `execMetadata`: a record of strings, or a function that picks one from the tool call's id. A pi caller can pass `metadata` on the context it hands `execute` instead, which wins. |
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,85 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { defineExec, type ExecToolOutput, type ExecWorkspaceLike } from "./exec"; | ||
|
|
||
| // A runtime that records the metadata each run was started with. | ||
| function recordingWorkspace(): { workspace: ExecWorkspaceLike; metadata: unknown[] } { | ||
| const metadata: unknown[] = []; | ||
| const workspace: ExecWorkspaceLike = { | ||
| runtime: { | ||
| async exec(_command, options) { | ||
| metadata.push(options.metadata); | ||
| return { | ||
| result: async () => ({ exitCode: 0, stdout: "", stderr: "" }), | ||
| }; | ||
| }, | ||
| backends: () => [{ id: "sh", protocol: "command", callable: false }], | ||
| }, | ||
| }; | ||
| return { workspace, metadata }; | ||
| } | ||
|
|
||
| async function last(output: AsyncIterable<ExecToolOutput>): Promise<ExecToolOutput | undefined> { | ||
| let value: ExecToolOutput | undefined; | ||
| for await (const chunk of output) value = chunk; | ||
| return value; | ||
| } | ||
|
|
||
| describe("defineExec metadata", () => { | ||
| it("starts each run with the metadata it was given", async () => { | ||
| const { workspace, metadata } = recordingWorkspace(); | ||
| const exec = defineExec({ workspace, metadata: { conversation: "c1" } }); | ||
|
|
||
| await last(exec.execute({ command: "true" })); | ||
|
|
||
| expect(metadata).toEqual([{ conversation: "c1" }]); | ||
| }); | ||
|
|
||
| it("picks each run's metadata from its tool call's id", async () => { | ||
| const { workspace, metadata } = recordingWorkspace(); | ||
| const exec = defineExec({ | ||
| workspace, | ||
| metadata: ({ toolCallId }) => ({ toolCall: toolCallId }), | ||
| }); | ||
|
|
||
| await last(exec.execute({ command: "true" }, { toolCallId: "call-1" })); | ||
| await last(exec.execute({ command: "true" }, { toolCallId: "call-2" })); | ||
|
|
||
| expect(metadata).toEqual([{ toolCall: "call-1" }, { toolCall: "call-2" }]); | ||
| }); | ||
|
|
||
| it("uses the metadata a call passes instead", async () => { | ||
| const { workspace, metadata } = recordingWorkspace(); | ||
| const exec = defineExec({ workspace, metadata: { conversation: "configured" } }); | ||
|
|
||
| await last(exec.execute({ command: "true" }, { metadata: { conversation: "passed" } })); | ||
|
|
||
| expect(metadata).toEqual([{ conversation: "passed" }]); | ||
| }); | ||
|
|
||
| it("starts runs without metadata when none is given", async () => { | ||
| const { workspace, metadata } = recordingWorkspace(); | ||
| const exec = defineExec({ workspace }); | ||
|
|
||
| await last(exec.execute({ command: "true" })); | ||
|
|
||
| expect(metadata).toEqual([undefined]); | ||
| }); | ||
|
|
||
| it("reports a failure to pick metadata as the call's error, without running", async () => { | ||
| const { workspace, metadata } = recordingWorkspace(); | ||
| const exec = defineExec({ | ||
| workspace, | ||
| metadata: () => { | ||
| throw new Error("no conversation"); | ||
| }, | ||
| }); | ||
|
|
||
| await expect(last(exec.execute({ command: "true" }))).resolves.toEqual({ | ||
| command: "true", | ||
| cwd: null, | ||
| backend: "sh", | ||
| error: "no conversation", | ||
| }); | ||
| expect(metadata).toEqual([]); | ||
| }); | ||
| }); |
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I'd rather this was
exec: { metadata: ... }Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@aron-cf
execis just used for the backend definitions right now.we could change the api to something like this instead which is a bit nicer than what we have right now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#300 i've opened this in case we want to make this change.