Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/effect/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"@types/node": "^26.6.3",
"@vitest/coverage-v8": "^5.0.5",
"ai": "^7.0.155",
"effect": "4.0.6",
"effect": "4.0.7",
"publint": "^0.3.25",
"tsup": "^9.0.0",
"tsx": "^4.23.15",
Expand Down
9 changes: 9 additions & 0 deletions packages/effect/src/language-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,15 @@ const toolResultToV4 = (part: Prompt.ToolResultPart): LanguageModelV4ToolResultP
let output: LanguageModelV4ToolResultOutput;
if (part.isFailure) {
output = { type: "error-json", value };
} else if (part.modelOutput !== undefined) {
// A tool's `toModelOutput` mapping (effect 4.0.7+) replaces the result
// sent to the model.
output = {
type: "content",
value: part.modelOutput.map((item) =>
item.type === "text" ? { type: "text", text: item.text } : filePartToV4(item),
),
};
} else {
output = { type: "json", value };
}
Expand Down
72 changes: 72 additions & 0 deletions packages/effect/test/language-model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,78 @@ describe("generateText", () => {
]);
});

it("sends a tool's model output in place of its serialized result", async () => {
let captured: LanguageModelV4CallOptions | undefined;
const model = fakeModel({
doGenerate: async (options) => {
captured = options;
return {
content: [{ type: "text", text: "ok" }],
finishReason: { unified: "stop", raw: undefined },
usage: USAGE,
warnings: [],
};
},
});

const messages: Array<Prompt.MessageEncoded> = [
{ role: "user", content: [{ type: "text", text: "Take a screenshot." }] },
{
role: "assistant",
content: [{ type: "tool-call", id: "call_1", name: "screenshot", params: {} }],
},
{
role: "tool",
content: [
{
type: "tool-result",
id: "call_1",
name: "screenshot",
isFailure: false,
result: { path: "/tmp/shot.png" },
modelOutput: [
{ type: "text", text: "Screenshot" },
{
type: "file",
mediaType: "image/png",
fileName: "shot.png",
data: "iVBORw0KGgo=",
},
],
providerExecuted: false,
},
],
},
];

await Effect.runPromise(
Effect.flatMap(service(model), (m) => m.generateText({ prompt: messages })),
);

expect(captured?.prompt.at(-1)).toEqual({
role: "tool",
content: [
{
type: "tool-result",
toolCallId: "call_1",
toolName: "screenshot",
output: {
type: "content",
value: [
{ type: "text", text: "Screenshot" },
{
type: "file",
mediaType: "image/png",
filename: "shot.png",
data: { type: "data", data: "iVBORw0KGgo=" },
},
],
},
},
],
});
});

it("sends tool definitions as JSON schema and resolves tool calls", async () => {
const GetWeather = Tool.make("get_weather", {
description: "Look up the current weather",
Expand Down
10 changes: 5 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading