Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commiteceb7d9

Browse files
authored
feat(tool): returns schema (#118)
* feat(tool): returns schema* chore(tool): update test* chore(shared-chat): move types* docs(tool): add returns example* fix(shared-chat): clean returns* fix(compat): use generics* docs(tool): fix example* fix(compat): add generics
1 parentb257d83 commiteceb7d9

File tree

7 files changed

+118
-13
lines changed

7 files changed

+118
-13
lines changed

‎docs/content/docs/packages/tool.mdx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,35 @@ const weather = await tool({
4343
})
4444
```
4545

46+
###tool (with returns schema)
47+
48+
```ts twoslash
49+
import {tool }from'@xsai/tool'
50+
import*asvfrom'valibot'
51+
52+
const weather=awaittool({
53+
description:'Get the weather in a location',
54+
execute: ({location })=> ({
55+
location,
56+
temperature:42,
57+
}),
58+
name:'weather',
59+
parameters:v.object({
60+
location:v.pipe(
61+
v.string(),
62+
v.description('The location to get the weather for'),
63+
),
64+
}),
65+
returns:v.object({
66+
location:v.pipe(
67+
v.string(),
68+
v.description('The location to get the weather for'),
69+
),
70+
temperature:v.number(),
71+
}),
72+
})
73+
```
74+
4675
###with generateText
4776

4877
```ts twoslash

‎packages-ext/compat/src/utils/internal/convert-tools.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
importtype{ToolasXSAITool,ToolOptionsasXSAIToolOptions}from'xsai'
2+
importtype{Schema}from'xsschema'
23

34
import{toolasxsaiTool}from'xsai'
45

@@ -16,7 +17,7 @@ export const convertTools = async (tools: Record<string, ((() => Promise<XSAIToo
1617
// handle mastra-ai
1718
if(!(resultasPartial<XSAITool>).function){
1819
returnxsaiTool({
19-
...resultasXSAIToolOptions,
20+
...resultasXSAIToolOptions<Schema>,
2021
name,
2122
})
2223
}

‎packages-ext/compat/src/utils/tool.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
importtype{ToolasXSAITool,ToolOptionsasXSAIToolOptions}from'xsai'
2+
importtype{Schema}from'xsschema'
23

34
import{toolasxsaiTool}from'xsai'
45

5-
exporttypeTool=Omit<XSAIToolOptions,'name'>
6+
exporttypeTool<TextendsSchema>=Omit<XSAIToolOptions<T>,'name'>
67

7-
exportconsttool=(options:Tool):()=>Promise<XSAITool>=>async()=>xsaiTool({
8+
exportconsttool=<TextendsSchema>(options:Tool<T>):()=>Promise<XSAITool>=>async()=>xsaiTool<T>({
89
...options,
910
name:'',
1011
})

‎packages/shared-chat/src/types/tool.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ export interface CompletionToolResult {
1515
}
1616

1717
exportinterfaceTool{
18-
execute:(input:unknown,options:ToolExecuteOptions)=>(object|string|unknown[])|Promise<object|string|unknown[]>
18+
execute:(input:unknown,options:ToolExecuteOptions)=>Promise<ToolExecuteResult>|ToolExecuteResult
1919
function:{
2020
description?:string
2121
name:string
2222
parameters:Record<string,unknown>
23+
/**@experimental */
24+
returns?:Record<string,unknown>
2325
strict?:boolean
2426
}
2527
type:'function'
@@ -30,3 +32,5 @@ export interface ToolExecuteOptions {
3032
messages:Message[]
3133
toolCallId:string
3234
}
35+
36+
exporttypeToolExecuteResult=object|string|unknown[]

‎packages/shared-chat/src/utils/chat.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
importtype{CommonRequestOptions}from'@xsai/shared'
22

3-
import{requestBody,requestHeaders,requestURL,responseCatch}from'@xsai/shared'
3+
import{clean,requestBody,requestHeaders,requestURL,responseCatch}from'@xsai/shared'
44

55
importtype{Message,Tool,ToolChoice}from'../types'
66

@@ -36,7 +36,10 @@ export const chat = async <T extends ChatOptions>(options: T) =>
3636
body:requestBody({
3737
...options,
3838
tools:(options.toolsasTool[]|undefined)?.map(tool=>({
39-
function:tool.function,
39+
function:clean({
40+
...tool.function,
41+
returns:undefined,
42+
}),
4043
type:'function',
4144
})),
4245
}),

‎packages/tool/src/index.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,30 @@
1-
importtype{Tool,ToolExecuteOptions}from'@xsai/shared-chat'
2-
importtype{InferIn,Schema}from'xsschema'
1+
importtype{Tool,ToolExecuteOptions,ToolExecuteResult}from'@xsai/shared-chat'
2+
importtype{Infer,InferIn,Schema}from'xsschema'
33

44
import{toJSONSchema}from'xsschema'
55

6-
exportinterfaceToolOptions<TextendsSchema=Schema>{
6+
exportinterfaceToolOptions<T1extendsSchema,T2extendsSchema|undefined=undefined>{
77
description?:string
8-
execute:(input:InferIn<T>,options:ToolExecuteOptions)=>(object|string|unknown[])|Promise<object|string|unknown[]>
8+
execute:(input:InferIn<T1>,options:ToolExecuteOptions)=>T2extendsSchema
9+
?Infer<T2>extendsobject
10+
?Infer<T2>|Promise<Infer<T2>>
11+
:Promise<ToolExecuteResult>|ToolExecuteResult
12+
:Promise<ToolExecuteResult>|ToolExecuteResult
913
name:string
10-
parameters:T
14+
parameters:T1
15+
returns?:T2
1116
strict?:boolean
1217
}
1318

1419
exportinterfaceToolResultextendsTool{}
1520

16-
exportconsttool=async<TextendsSchema>(options:ToolOptions<T>):Promise<ToolResult>=>({
21+
exportconsttool=async<T1extendsSchema,T2extendsSchema|undefined=undefined>(options:ToolOptions<T1,T2>):Promise<ToolResult>=>({
1722
execute:options.execute,
1823
function:{
1924
description:options.description,
2025
name:options.name,
2126
parameters:awaittoJSONSchema(options.parameters)asRecord<string,unknown>,
27+
returns:options.returns ?(awaittoJSONSchema(options.returns))asRecord<string,unknown> :undefined,
2228
strict:options.strict,
2329
},
2430
type:'function',

‎packages/tool/test/index.test.ts

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import{description,object,pipe,string}from'valibot'
1+
import{description,number,object,pipe,string}from'valibot'
22
import{describe,expect,it}from'vitest'
33

44
import{tool}from'../src'
@@ -40,4 +40,65 @@ describe('@xsai/tool', () => {
4040
type:'object',
4141
})
4242
})
43+
44+
it('weather with returns',async()=>{
45+
constname='weather'
46+
constdesc='Get the weather in a location'
47+
48+
constweather=awaittool({
49+
description:desc,
50+
execute:({ location})=>({
51+
location,
52+
temperature:42,
53+
}),
54+
name,
55+
parameters:object({
56+
location:pipe(
57+
string(),
58+
description('The location to get the weather for'),
59+
),
60+
}),
61+
returns:object({
62+
location:pipe(
63+
string(),
64+
description('The location to get the weather for'),
65+
),
66+
temperature:number(),
67+
}),
68+
})
69+
70+
expect(weather.type).toBe('function')
71+
expect(weather.function.name).toBe(name)
72+
expect(weather.function.description).toBe(desc)
73+
expect(weather.function.parameters).toStrictEqual({
74+
$schema:'http://json-schema.org/draft-07/schema#',
75+
properties:{
76+
location:{
77+
description:'The location to get the weather for',
78+
type:'string',
79+
},
80+
},
81+
required:[
82+
'location',
83+
],
84+
type:'object',
85+
})
86+
expect(weather.function.returns).toStrictEqual({
87+
$schema:'http://json-schema.org/draft-07/schema#',
88+
properties:{
89+
location:{
90+
description:'The location to get the weather for',
91+
type:'string',
92+
},
93+
temperature:{
94+
type:'number',
95+
},
96+
},
97+
required:[
98+
'location',
99+
'temperature',
100+
],
101+
type:'object',
102+
})
103+
})
43104
})

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp