Repository navigation
feat(harness): add an AI SDK harness #2434
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 `AiSdkHarness` from the new `agents/harness/ai-sdk` entry point, which runs each message with `streamText` and keeps sessions and transcripts in the Durable Object, so `Channels.forHarness` can serve an AI SDK model. The same entry point exports the AI SDK message conversions it is built on and `createSendMessageTool`, an AI SDK tool that sends a message to a surface through the gateway. |
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
149 changes: 149 additions & 0 deletions
149
packages/agents/src/experimental/channels/__tests__/ai-sdk.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,149 @@ | ||
| import { asSchema, type UIMessage, type UIMessageChunk } from "ai"; | ||
| import { describe, expect, it, vi } from "vitest"; | ||
| import { ChannelGateway, type ChannelMessage, type DeliveryResult } from ".."; | ||
| import { | ||
| answerToolCall, | ||
| createSendMessageTool, | ||
| toResponseChunks | ||
| } from "../../../harness/ai-sdk"; | ||
|
|
||
| function executable(tool: ReturnType<typeof createSendMessageTool>) { | ||
| return tool.execute as unknown as ( | ||
| message: ChannelMessage | ||
| ) => Promise<DeliveryResult>; | ||
| } | ||
|
|
||
| const surface = { | ||
| channelKey: "test", | ||
| version: 1, | ||
| address: null, | ||
| label: "Test destination" | ||
| } as const; | ||
|
|
||
| function host( | ||
| deliver = vi.fn( | ||
| async (): Promise<DeliveryResult> => ({ status: "delivered" }) | ||
| ) | ||
| ) { | ||
| return { | ||
| deliver, | ||
| channelHost: new ChannelGateway({ | ||
| channels: { test: { deliver } }, | ||
| agent: () => { | ||
| throw new Error("no agent"); | ||
| } | ||
| }) | ||
| }; | ||
| } | ||
|
|
||
| describe("AI SDK message adapter", () => { | ||
|
cjol marked this conversation as resolved.
|
||
| it("adapts a Host-resolved surface to a caller-described tool", async () => { | ||
| const deliver = vi.fn( | ||
| async (): Promise<DeliveryResult> => ({ | ||
| status: "delivered", | ||
| reference: "message-1" | ||
| }) | ||
| ); | ||
| const { channelHost } = host(deliver); | ||
|
|
||
| const messageTool = createSendMessageTool(channelHost, surface, { | ||
| description: "Escalate to a human", | ||
| needsApproval: true, | ||
| metadata: { purpose: "escalation" }, | ||
| inputExamples: [{ input: { markdown: "Please **help**" } }] | ||
| }); | ||
|
|
||
| expect(messageTool.description).toBe("Escalate to a human"); | ||
| expect(messageTool.needsApproval).toBe(true); | ||
| expect(messageTool.metadata).toEqual({ purpose: "escalation" }); | ||
|
|
||
| await expect( | ||
| executable(messageTool)({ title: "Urgent", markdown: "Please **help**" }) | ||
| ).resolves.toEqual({ status: "delivered", reference: "message-1" }); | ||
| expect(deliver).toHaveBeenCalledWith( | ||
| surface, | ||
| { title: "Urgent", markdown: "Please **help**" }, | ||
| undefined | ||
| ); | ||
| }); | ||
|
|
||
| it("validates tool input without requiring a schema library", async () => { | ||
| const { channelHost } = host(); | ||
| const messageTool = createSendMessageTool(channelHost, surface); | ||
| const schema = asSchema(messageTool.inputSchema); | ||
|
|
||
| expect(await schema.validate?.({ markdown: "" })).toMatchObject({ | ||
| success: false | ||
| }); | ||
| expect( | ||
| await schema.validate?.({ markdown: "Ready", ignored: true }) | ||
| ).toEqual({ | ||
| success: true, | ||
| value: { markdown: "Ready" } | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe("AI SDK turn conversions", () => { | ||
| const asked = (state: string, extra: Record<string, unknown> = {}) => | ||
| ({ | ||
| id: "m1", | ||
| role: "assistant", | ||
| parts: [ | ||
| { | ||
| type: "tool-getLocation", | ||
| toolCallId: "t1", | ||
| input: {}, | ||
| state, | ||
| approval: { id: "a1" }, | ||
| ...extra | ||
| } | ||
| ] | ||
| }) as unknown as UIMessage; | ||
|
|
||
| it("records a rejected approval as denied", () => { | ||
| const answered = answerToolCall([asked("approval-requested")], { | ||
| type: "approval-response", | ||
| eventId: "e1", | ||
| turnId: "turn", | ||
| approvalId: "a1", | ||
| approved: false | ||
| }); | ||
| expect(answered?.parts[0]).toMatchObject({ | ||
| state: "output-denied", | ||
| approval: { id: "a1", approved: false } | ||
| }); | ||
| }); | ||
|
|
||
| it("records a client tool's result after its approval", () => { | ||
| const answered = answerToolCall( | ||
| [asked("approval-responded", { approval: { id: "a1", approved: true } })], | ||
| { | ||
| type: "tool-result", | ||
| eventId: "e1", | ||
| turnId: "turn", | ||
| toolCallId: "t1", | ||
| result: { ok: true, output: "here" } | ||
| } | ||
| ); | ||
| expect(answered?.parts[0]).toMatchObject({ | ||
| state: "output-available", | ||
| output: "here" | ||
| }); | ||
| }); | ||
|
|
||
| it("throws after an aborted stream, keeping what it produced", async () => { | ||
| async function* stream(): AsyncGenerator<UIMessageChunk> { | ||
| yield { type: "text-start", id: "a" }; | ||
| yield { type: "text-delta", id: "a", delta: "Half" }; | ||
| yield { type: "abort" }; | ||
| } | ||
| const seen: string[] = []; | ||
| await expect(async () => { | ||
| for await (const chunk of toResponseChunks(stream())) { | ||
| seen.push(chunk.type); | ||
| } | ||
| }).rejects.toThrow("aborted"); | ||
| expect(seen).toEqual(["text-start", "text-delta"]); | ||
| }); | ||
| }); | ||
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.
🔍 Advertised import path differs from exported API
The PR description advertises
agents/channels/ai-sdk, but the package exportsagents/harness/ai-sdkand a hosted harness. Align the public API documentation with the intended release.Was this helpful? React with 👍 or 👎 to provide feedback.