Repository navigation
feat(channels): the Web Channel, its browser client and WebSockets.use #2433
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,5 @@ | ||
| --- | ||
| "agents": minor | ||
| --- | ||
|
|
||
| Add the Web Channel (`agents/experimental/channels/web`) and its client (`agents/experimental/channels/web/client`), which connect browsers and terminals to a conversation over the agent's WebSockets: live transcript and turns, approvals, client tool results, and creating, forking, resetting, listing and following conversations. `WebSockets` gains `use` for protocol handlers that run before the configured ones. |
Large diffs are not rendered by default.
Oops, something went wrong.
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
19 changes: 19 additions & 0 deletions
19
packages/agents/src/experimental/channels/__tests__/apply-chunks.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,19 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { applyChunks } from "../apply-chunks"; | ||
|
|
||
| describe("applyChunks", () => { | ||
| it("keeps provider metadata from a part's start and end", () => { | ||
| const message = applyChunks({ id: "m1", role: "assistant", parts: [] }, [ | ||
| { type: "reasoning-start", id: "r", providerMetadata: { a: 1 } }, | ||
| { type: "reasoning-delta", id: "r", delta: "thinking" }, | ||
| { type: "reasoning-end", id: "r", providerMetadata: { b: 2 } }, | ||
| { type: "text-start", id: "t" }, | ||
| { type: "text-delta", id: "t", delta: "Hi" }, | ||
| { type: "text-end", id: "t" } | ||
| ]); | ||
| expect(message.parts).toEqual([ | ||
| { type: "reasoning", text: "thinking", providerMetadata: { a: 1, b: 2 } }, | ||
| { type: "text", text: "Hi" } | ||
| ]); | ||
| }); | ||
| }); |
103 changes: 103 additions & 0 deletions
103
packages/agents/src/experimental/channels/__tests__/web-client.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,103 @@ | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; | ||
| import { WebChannelClient } from "../web/client"; | ||
| import type { ServerFrame } from "../web/protocol"; | ||
|
|
||
| /** A WebSocket the test opens, feeds and inspects. */ | ||
| class FakeSocket extends EventTarget { | ||
| static readonly OPEN = 1; | ||
| static sockets: FakeSocket[] = []; | ||
| readyState = FakeSocket.OPEN; | ||
| readonly sent: unknown[] = []; | ||
|
|
||
| constructor(readonly url: string) { | ||
| super(); | ||
| FakeSocket.sockets.push(this); | ||
| } | ||
|
|
||
| send(data: string): void { | ||
| this.sent.push(JSON.parse(data)); | ||
| } | ||
|
|
||
| close(): void { | ||
| this.readyState = 3; | ||
| this.dispatchEvent(new Event("close")); | ||
| } | ||
|
|
||
| receive(frame: ServerFrame): void { | ||
| this.dispatchEvent( | ||
| new MessageEvent("message", { data: JSON.stringify(frame) }) | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| const snapshot = (conversationId: string): ServerFrame => ({ | ||
| type: "channels:snapshot", | ||
| conversationId, | ||
| you: { id: "alice" }, | ||
| operations: ["conversation-create"], | ||
| messages: [], | ||
| turns: [] | ||
| }); | ||
|
|
||
| const latest = () => FakeSocket.sockets.at(-1) as FakeSocket; | ||
|
|
||
| describe("WebChannelClient", () => { | ||
| beforeEach(() => { | ||
| FakeSocket.sockets = []; | ||
| vi.stubGlobal("WebSocket", FakeSocket); | ||
| }); | ||
| afterEach(() => vi.unstubAllGlobals()); | ||
|
|
||
| it("lists conversations, and keeps the latest list in its state", async () => { | ||
| const client = new WebChannelClient("ws://example.com/channels/room/main"); | ||
| latest().receive(snapshot("main")); | ||
|
|
||
| const listed = client.listConversations(); | ||
| const request = latest().sent.at(-1) as { requestId: string }; | ||
| expect(request).toMatchObject({ type: "channels:list-conversations" }); | ||
| const conversations = [ | ||
| { id: "main", busy: false }, | ||
| { id: "f1", parent: "main", busy: true } | ||
| ]; | ||
| latest().receive({ | ||
| type: "channels:conversations", | ||
| conversationId: "main", | ||
| requestId: request.requestId, | ||
| conversations | ||
| }); | ||
| expect(await listed).toEqual(conversations); | ||
| expect(client.state.conversations).toEqual(conversations); | ||
|
|
||
| // A pushed list replaces it. | ||
| latest().receive({ | ||
| type: "channels:conversations", | ||
| conversationId: "main", | ||
| conversations: conversations.slice(0, 1) | ||
| }); | ||
| expect(client.state.conversations).toEqual(conversations.slice(0, 1)); | ||
| client.close(); | ||
| }); | ||
|
|
||
| it("follows another conversation by reconnecting to its URL", async () => { | ||
| const client = new WebChannelClient("ws://example.com/channels/room/main"); | ||
| const first = latest(); | ||
| first.receive(snapshot("main")); | ||
| const unacked = client.send({ type: "conversation-create" }); | ||
|
|
||
| client.follow("f1"); | ||
| await expect(unacked).rejects.toThrow("Switched conversation"); | ||
| expect(first.readyState).toBe(3); | ||
| expect(latest().url).toBe("ws://example.com/channels/room/f1"); | ||
| expect(FakeSocket.sockets).toHaveLength(2); | ||
| expect(client.state.connected).toBe(false); | ||
|
|
||
| // Frames from the socket left behind are ignored. | ||
| first.receive(snapshot("main")); | ||
| latest().receive(snapshot("f1")); | ||
| expect(client.state).toMatchObject({ | ||
| connected: true, | ||
| conversationId: "f1" | ||
| }); | ||
| client.close(); | ||
| }); | ||
| }); |
125 changes: 125 additions & 0 deletions
125
packages/agents/src/experimental/channels/apply-chunks.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,125 @@ | ||
| import type { | ||
| MessagePart, | ||
| ResponseChunk, | ||
| ToolPart, | ||
| TranscriptMessage | ||
| } from "./protocol"; | ||
|
|
||
| /** Apply a response's chunks to the message it builds or extends. */ | ||
| export function applyChunks( | ||
| message: TranscriptMessage, | ||
| chunks: readonly ResponseChunk[] | ||
| ): TranscriptMessage { | ||
| const parts: MessagePart[] = message.parts.map((part) => ({ ...part })); | ||
| let metadata = message.metadata; | ||
| type TextPart = Extract<MessagePart, { type: "text" | "reasoning" }>; | ||
| const open = new Map<string, TextPart>(); | ||
|
|
||
| const toolPart = (toolCallId: string) => | ||
| parts.find( | ||
| (part): part is ToolPart => | ||
| part.type === "tool" && part.toolCallId === toolCallId | ||
| ); | ||
|
|
||
| for (const chunk of chunks) { | ||
| switch (chunk.type) { | ||
| case "text-start": | ||
| case "reasoning-start": { | ||
| const kind = chunk.type === "text-start" ? "text" : "reasoning"; | ||
| const part: TextPart = { | ||
| type: kind, | ||
| text: "", | ||
| ...(chunk.providerMetadata !== undefined && { | ||
| providerMetadata: chunk.providerMetadata | ||
| }) | ||
| }; | ||
| open.set(`${kind}:${chunk.id}`, part); | ||
| parts.push(part); | ||
|
cjol marked this conversation as resolved.
|
||
| break; | ||
| } | ||
| case "text-delta": | ||
| case "reasoning-delta": { | ||
| const kind = chunk.type === "text-delta" ? "text" : "reasoning"; | ||
| const part = open.get(`${kind}:${chunk.id}`); | ||
| if (part) part.text += chunk.delta; | ||
| break; | ||
| } | ||
| case "text-end": | ||
| case "reasoning-end": { | ||
| const key = `${chunk.type.split("-")[0]}:${chunk.id}`; | ||
| const part = open.get(key); | ||
| if (part && chunk.providerMetadata !== undefined) { | ||
| part.providerMetadata = { | ||
| ...part.providerMetadata, | ||
| ...chunk.providerMetadata | ||
| }; | ||
| } | ||
| open.delete(key); | ||
| break; | ||
| } | ||
| case "tool-input-start": | ||
| case "tool-input-available": | ||
| case "tool-input-error": { | ||
| const { type, ...call } = chunk; | ||
| const existing = toolPart(chunk.toolCallId); | ||
| const next: ToolPart = { | ||
| ...existing, | ||
| ...call, | ||
| type: "tool", | ||
| state: | ||
| type === "tool-input-start" | ||
| ? "input-streaming" | ||
| : type === "tool-input-available" | ||
| ? "input-available" | ||
| : "output-error" | ||
| }; | ||
| if (existing) parts[parts.indexOf(existing)] = next; | ||
| else parts.push(next); | ||
| break; | ||
| } | ||
| case "tool-input-delta": | ||
| break; | ||
| case "tool-approval-request": | ||
| case "tool-output-available": | ||
| case "tool-output-error": | ||
| case "tool-output-denied": { | ||
| const part = toolPart(chunk.toolCallId); | ||
| if (!part) break; | ||
| if (chunk.type === "tool-approval-request") { | ||
| part.state = "approval-requested"; | ||
| part.approval = { id: chunk.approvalId }; | ||
| } else if (chunk.type === "tool-output-available") { | ||
| part.state = "output-available"; | ||
| part.output = chunk.output; | ||
| part.preliminary = chunk.preliminary; | ||
| } else if (chunk.type === "tool-output-error") { | ||
| part.state = "output-error"; | ||
| part.errorText = chunk.errorText; | ||
| } else { | ||
| part.state = "output-denied"; | ||
| } | ||
| break; | ||
| } | ||
| case "data": { | ||
| if (chunk.transient) break; | ||
| const { transient: _transient, ...part } = chunk; | ||
| const index = parts.findIndex( | ||
| (p) => | ||
| p.type === "data" && | ||
| p.name === chunk.name && | ||
| p.id !== undefined && | ||
| p.id === chunk.id | ||
| ); | ||
| if (index === -1) parts.push(part); | ||
| else parts[index] = part; | ||
| break; | ||
| } | ||
| case "metadata": | ||
| metadata = { ...metadata, ...chunk.metadata }; | ||
| break; | ||
| default: | ||
| parts.push(chunk); | ||
| } | ||
| } | ||
| return { ...message, parts, ...(metadata && { metadata }) }; | ||
| } | ||
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.
🔍 Published channel paths differ from description
The package exports only
agents/experimental/channels/..., while the description promisesagents/channels/.... Align the documented import paths with the intended published API.Was this helpful? React with 👍 or 👎 to provide feedback.