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
5 changes: 5 additions & 0 deletions .changeset/witty-places-film.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect/ai": patch
---

Fix Prompt.fromResponseParts when input contains a provider executed tool
30 changes: 24 additions & 6 deletions packages/ai/ai/src/Prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,8 @@ export const toolCallPart = (params: PartConstructorParams<ToolCallPart>): ToolC
* temperature: 22,
* condition: "sunny",
* humidity: 65
* }
* },
* providerExecuted: false
* })
* ```
*
Expand All @@ -635,6 +636,10 @@ export interface ToolResultPart extends BasePart<"tool-result", ToolResultPartOp
* The result returned by the tool execution.
*/
readonly result: unknown
/**
* Whether the tool was executed by the provider (true) or framework (false).
*/
readonly providerExecuted: boolean
}

/**
Expand All @@ -660,6 +665,10 @@ export interface ToolResultPartEncoded extends BasePartEncoded<"tool-result", To
* The result returned by the tool execution.
*/
readonly result: unknown
/**
* Whether the tool was executed by the provider (true) or framework (false).
*/
readonly providerExecuted: boolean
}

/**
Expand All @@ -683,6 +692,7 @@ export const ToolResultPart: Schema.Schema<ToolResultPart, ToolResultPartEncoded
name: Schema.String,
isFailure: Schema.Boolean,
result: Schema.Unknown,
providerExecuted: Schema.Boolean,
options: Schema.optionalWith(ProviderOptions, { default: constEmptyObject })
}).pipe(
Schema.attachPropertySignature(PartTypeId, PartTypeId),
Expand Down Expand Up @@ -1036,7 +1046,8 @@ export const userMessage = (params: MessageConstructorParams<UserMessage>): User
* result: {
* temperature: 72,
* condition: "sunny"
* }
* },
* providerExecuted: false
* }),
* Prompt.makePart("text", {
* text: "The weather in San Francisco is currently 72°F and sunny."
Expand Down Expand Up @@ -1150,7 +1161,8 @@ export const assistantMessage = (params: MessageConstructorParams<AssistantMessa
* { title: "TypeScript Handbook", url: "https://..." },
* { title: "Effective TypeScript", url: "https://..." }
* ]
* }
* },
* providerExecuted: false
* })
* ]
* })
Expand Down Expand Up @@ -1624,12 +1636,18 @@ export const fromResponseParts = (parts: ReadonlyArray<Response.AnyPart>): Promp

// Tool Result Parts
case "tool-result": {
toolParts.push(makePart("tool-result", {
const toolPart = makePart("tool-result", {
id: part.id,
name: part.providerName ?? part.name,
isFailure: part.isFailure,
result: part.encodedResult
}))
result: part.encodedResult,
providerExecuted: part.providerExecuted ?? false
})
if (part.providerExecuted) {
assistantParts.push(toolPart)
} else {
toolParts.push(toolPart)
}
}
}
}
Expand Down