Skip to content

Commit fb53370

Browse files
authored
Fix Response.Part, AllParts, StreamPart not inferring Schema properly if toolkit is WithHandler (#5685)
1 parent f8273c9 commit fb53370

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

.changeset/thick-steaks-see.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@effect/ai": patch
3+
---
4+
5+
Fix Response.Part, AllParts, StreamPart not inferring Schema properly if toolkit is WithHandler

packages/ai/ai/src/Response.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ export type AllPartsEncoded =
200200
*/
201201
export const AllParts = <T extends Toolkit.Any | Toolkit.WithHandler<any>>(
202202
toolkit: T
203-
): Schema.Schema<AllParts<Toolkit.Tools<T>>, AllPartsEncoded> => {
203+
): Schema.Schema<AllParts<T extends Toolkit.Any ? Toolkit.Tools<T> : Toolkit.WithHandlerTools<T>>, AllPartsEncoded> => {
204204
const toolCalls: Array<Schema.Schema<ToolCallPart<string, any>, ToolCallPartEncoded>> = []
205205
const toolCallResults: Array<Schema.Schema<ToolResultPart<string, any, any>, ToolResultPartEncoded>> = []
206206
for (const tool of Object.values(toolkit.tools as Record<string, Tool.Any>)) {
@@ -281,7 +281,7 @@ export type PartEncoded =
281281
*/
282282
export const Part = <T extends Toolkit.Any | Toolkit.WithHandler<any>>(
283283
toolkit: T
284-
): Schema.Schema<Part<Toolkit.Tools<T>>, PartEncoded> => {
284+
): Schema.Schema<Part<T extends Toolkit.Any ? Toolkit.Tools<T> : Toolkit.WithHandlerTools<T>>, PartEncoded> => {
285285
const toolCalls: Array<Schema.Schema<ToolCallPart<string, any>, ToolCallPartEncoded>> = []
286286
const toolCallResults: Array<Schema.Schema<ToolResultPart<string, any, any>, ToolResultPartEncoded>> = []
287287
for (const tool of Object.values(toolkit.tools as Record<string, Tool.Any>)) {
@@ -365,7 +365,12 @@ export type StreamPartEncoded =
365365
*/
366366
export const StreamPart = <T extends Toolkit.Any | Toolkit.WithHandler<any>>(
367367
toolkit: T
368-
): Schema.Schema<StreamPart<Toolkit.Tools<T>>, StreamPartEncoded> => {
368+
): Schema.Schema<
369+
StreamPart<
370+
T extends Toolkit.Any ? Toolkit.Tools<T> : Toolkit.WithHandlerTools<T>
371+
>,
372+
StreamPartEncoded
373+
> => {
369374
const toolCalls: Array<Schema.Schema<ToolCallPart<string, any>, ToolCallPartEncoded>> = []
370375
const toolCallResults: Array<Schema.Schema<ToolResultPart<string, any, any>, ToolResultPartEncoded>> = []
371376
for (const tool of Object.values(toolkit.tools as Record<string, Tool.Any>)) {

packages/ai/ai/src/Toolkit.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,15 @@ export type HandlersFrom<Tools extends Record<string, Tool.Any>> = {
199199
>
200200
}
201201

202+
/**
203+
* A utility type which can be used to extract the tools from a toolkit with handlers.
204+
*
205+
* @since 1.0.0
206+
* @category Utility Types
207+
*/
208+
209+
export type WithHandlerTools<T> = T extends WithHandler<infer Tools> ? Tools : never
210+
202211
/**
203212
* A toolkit instance with registered handlers ready for tool execution.
204213
*

0 commit comments

Comments
 (0)