Repository navigation
feat(channels): add a Pi TUI client for the Web Channel #2437
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
Merged
Merged
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,7 @@ | ||
| --- | ||
| "agents": patch | ||
| --- | ||
|
|
||
| Add `npx agents tui <url>`, a terminal client for a conversation's Web Channel. | ||
|
|
||
| It streams the transcript with highlighted Markdown, collapsible reasoning and tool cards, and shows messages and turns from every surface live. It answers approvals inline, lets a person type a client tool's result, and cancels the running turn with Esc. `--header` adds headers to the WebSocket upgrade, and `CF_ACCESS_CLIENT_ID` / `CF_ACCESS_CLIENT_SECRET` are sent as an Access service token. Its dependencies are bundled into `dist/cli.js`, so installing `agents` adds none. |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import { main as tui } from "../experimental/channels/web/tui/main"; | ||
|
|
||
| const usage = `Usage: agents <command> | ||
|
|
||
| Commands: | ||
| tui <url> Chat with an agent's Web Channel from the terminal`; | ||
|
|
||
| const [command, ...rest] = process.argv.slice(2); | ||
|
|
||
| if (command === "tui") { | ||
| process.exit(await tui(rest)); | ||
| } | ||
| if (command === undefined || command === "--help" || command === "-h") { | ||
| console.log(usage); | ||
| process.exit(0); | ||
| } | ||
| console.error(`Unknown command "${command}"\n\n${usage}`); | ||
| process.exit(2); |
68 changes: 68 additions & 0 deletions
68
packages/agents/src/experimental/channels/__tests__/tui-args.test.ts
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,68 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { parseArgs, UsageError } from "../web/tui/args"; | ||
|
|
||
| describe("tui args", () => { | ||
| it("turns http(s) into ws(s) and keeps the path as given", () => { | ||
| expect(parseArgs(["https://example.com/a/b/channels?x=1"]).url).toBe( | ||
| "wss://example.com/a/b/channels?x=1" | ||
| ); | ||
| expect(parseArgs(["http://localhost:5173/c"]).url).toBe( | ||
| "ws://localhost:5173/c" | ||
| ); | ||
| expect(parseArgs(["wss://example.com/c"]).url).toBe("wss://example.com/c"); | ||
| }); | ||
|
|
||
| it("sets the participant with --as", () => { | ||
| expect(parseArgs(["https://h/c", "--as", "ann"]).url).toBe( | ||
| "wss://h/c?as=ann" | ||
| ); | ||
| expect(parseArgs(["--as=bo b", "https://h/c?as=x"]).url).toBe( | ||
| "wss://h/c?as=bo+b" | ||
| ); | ||
| }); | ||
|
|
||
| it("collects headers in either form", () => { | ||
| const { headers } = parseArgs([ | ||
| "https://h/c", | ||
| "--header", | ||
| "X-One=a=b", | ||
| "-H", | ||
| "Authorization: Bearer t:1", | ||
| "--header=x-two=2" | ||
| ]); | ||
| expect(headers).toEqual({ | ||
| "x-one": "a=b", | ||
| authorization: "Bearer t:1", | ||
| "x-two": "2" | ||
| }); | ||
| }); | ||
|
|
||
| it("sends an Access service token from the environment", () => { | ||
| const env = { CF_ACCESS_CLIENT_ID: "id", CF_ACCESS_CLIENT_SECRET: "s" }; | ||
| expect(parseArgs(["https://h/c"], env).headers).toEqual({ | ||
| "cf-access-client-id": "id", | ||
| "cf-access-client-secret": "s" | ||
| }); | ||
| expect( | ||
| parseArgs(["https://h/c", "-H", "CF-Access-Client-Id: other"], env) | ||
| .headers["cf-access-client-id"] | ||
| ).toBe("other"); | ||
| expect(() => | ||
| parseArgs(["https://h/c"], { CF_ACCESS_CLIENT_ID: "id" }) | ||
| ).toThrow(UsageError); | ||
| }); | ||
|
|
||
| it("rejects bad input", () => { | ||
| for (const argv of [ | ||
| [], | ||
| ["not a url"], | ||
| ["ftp://h/c"], | ||
| ["https://h/c", "extra"], | ||
| ["https://h/c", "--nope"], | ||
| ["https://h/c", "--as"], | ||
| ["https://h/c", "-H", "novalue"] | ||
| ]) { | ||
| expect(() => parseArgs(argv), argv.join(" ")).toThrow(UsageError); | ||
| } | ||
| }); | ||
| }); |
102 changes: 102 additions & 0 deletions
102
packages/agents/src/experimental/channels/__tests__/tui-render.test.ts
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,102 @@ | ||
| import { type TUI, visibleWidth } from "@earendil-works/pi-tui"; | ||
| import { describe, expect, it } from "vitest"; | ||
| import type { ToolPart } from "../protocol"; | ||
| import { type Context, TranscriptView } from "../web/tui/components/blocks"; | ||
| import { PendingView, StatusBar } from "../web/tui/components/footer"; | ||
| import { ClientToolModal, ReasonModal } from "../web/tui/components/modal"; | ||
| import { type Block, initialUi, type Status } from "../web/tui/view"; | ||
|
|
||
| const tool: ToolPart = { | ||
| type: "tool", | ||
| toolCallId: "c1", | ||
| toolName: "aVeryLongToolNameThatKeepsGoing", | ||
| state: "approval-requested", | ||
| input: { path: "/a/very/long/path/that/does/not/fit/anywhere", n: 1 }, | ||
| approval: { id: "a1" } | ||
| }; | ||
|
|
||
| const blocks: Block[] = [ | ||
| { | ||
| kind: "user", | ||
| key: "m1", | ||
| text: "a long message ".repeat(10), | ||
| from: { you: false, name: "Ann with a long name" } | ||
| }, | ||
| { | ||
| kind: "reasoning", | ||
| key: "r", | ||
| text: "thinking ".repeat(40), | ||
| streaming: true | ||
| }, | ||
| { | ||
| kind: "text", | ||
| key: "t", | ||
| text: "# Title\n\nSome `code` and\n\n```ts\nconst x: number = 1; // a long trailing comment\n```\n\n| a | b |\n|---|---|\n| 1 | 2 |", | ||
| streaming: false | ||
| }, | ||
| { kind: "tool", key: "tool:c1", part: tool, turnId: "t1", turn: "awaiting" }, | ||
| { kind: "attachment", key: "f", label: "file a-very-long-file-name.png" }, | ||
| { kind: "notice", key: "n", text: "✗ Turn failed: ".repeat(5), tone: "error" } | ||
| ]; | ||
|
|
||
| const status: Status = { | ||
| connected: false, | ||
| seen: true, | ||
| you: "terminal-123456", | ||
| running: { turnId: "t", yours: true, seconds: 75, cancelling: true }, | ||
| queued: [{ turnId: "q", label: "queued ".repeat(20), yours: true }], | ||
| awaiting: 2, | ||
| sending: [{ eventId: "e", label: "sending ".repeat(20) }] | ||
| }; | ||
|
|
||
| const fakeTui = { | ||
| terminal: { rows: 24, columns: 80 }, | ||
| requestRender() {} | ||
| } as unknown as TUI; | ||
|
|
||
| describe("tui rendering", () => { | ||
| for (const width of [6, 12, 30, 80]) { | ||
| it(`fits every line in ${width} columns`, () => { | ||
| for (const expanded of [true, false]) { | ||
| const context: Context = { | ||
| frame: 3, | ||
| you: "me", | ||
| ui: { | ||
| ...initialUi, | ||
| toolsExpanded: expanded, | ||
| reasoningExpanded: expanded, | ||
| selected: "tool:c1" | ||
| } | ||
| }; | ||
| const transcript = new TranscriptView(context); | ||
| transcript.update(blocks); | ||
| const pending = new PendingView(); | ||
| pending.status = status; | ||
| pending.prompts = ["aVeryLongToolNameThatKeepsGoing"]; | ||
| const bar = new StatusBar("example.com/agents/a/b/channels", context); | ||
| bar.status = status; | ||
| const lines = [ | ||
| ...transcript.render(width), | ||
| ...pending.render(width), | ||
| ...bar.render(width), | ||
| ...new ClientToolModal( | ||
| fakeTui, | ||
| tool, | ||
| () => {}, | ||
| () => {} | ||
| ).render(width), | ||
| ...new ReasonModal( | ||
| tool, | ||
| () => {}, | ||
| () => {} | ||
| ).render(width) | ||
| ]; | ||
| for (const line of lines) { | ||
| expect(visibleWidth(line), JSON.stringify(line)).toBeLessThanOrEqual( | ||
| width | ||
| ); | ||
| } | ||
| } | ||
| }); | ||
| } | ||
| }); |
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.
🔍 Example advertises an unregistered harness
The terminal instructions claim
tui2works with either harness, but the gateway resolver registers onlyai-sdk. The example needs either the second harness or narrower instructions.Was this helpful? React with 👍 or 👎 to provide feedback.