@@ -63,16 +63,16 @@ pub fn parse_json_object<T: DeserializeOwned>(input: JsonObject) -> Result<T, cr
63
63
)
64
64
} )
65
65
}
66
- pub struct ToolCallContext < ' service , S > {
66
+ pub struct ToolCallContext < S > {
67
67
request_context : RequestContext < RoleServer > ,
68
- service : & ' service S ,
68
+ service : Arc < S > ,
69
69
name : Cow < ' static , str > ,
70
70
arguments : Option < JsonObject > ,
71
71
}
72
72
73
- impl < ' service , S > ToolCallContext < ' service , S > {
73
+ impl < S > ToolCallContext < S > {
74
74
pub fn new (
75
- service : & ' service S ,
75
+ service : Arc < S > ,
76
76
CallToolRequestParam { name, arguments } : CallToolRequestParam ,
77
77
request_context : RequestContext < RoleServer > ,
78
78
) -> Self {
@@ -91,10 +91,8 @@ impl<'service, S> ToolCallContext<'service, S> {
91
91
}
92
92
}
93
93
94
- pub trait FromToolCallContextPart < ' a , S > : Sized {
95
- fn from_tool_call_context_part (
96
- context : ToolCallContext < ' a , S > ,
97
- ) -> Result < ( Self , ToolCallContext < ' a , S > ) , crate :: Error > ;
94
+ pub trait FromToolCallContextPart < S > : Sized {
95
+ fn from_tool_call_context_part ( context : & mut ToolCallContext < S > ) -> Result < Self , crate :: Error > ;
98
96
}
99
97
100
98
pub trait IntoCallToolResult {
@@ -162,12 +160,12 @@ impl IntoCallToolResult for Result<CallToolResult, crate::Error> {
162
160
}
163
161
}
164
162
165
- pub trait CallToolHandler < ' a , S , A > {
166
- type Fut : Future < Output = Result < CallToolResult , crate :: Error > > + Send + ' a ;
167
- fn call ( self , context : ToolCallContext < ' a , S > ) -> Self :: Fut ;
163
+ pub trait CallToolHandler < S , A > {
164
+ type Fut : Future < Output = Result < CallToolResult , crate :: Error > > + Send ;
165
+ fn call ( self , context : ToolCallContext < S > ) -> Self :: Fut ;
168
166
}
169
167
170
- pub type DynCallToolHandler < S > = dyn Fn ( ToolCallContext < ' _ , S > ) -> BoxFuture < ' _ , Result < CallToolResult , crate :: Error > >
168
+ pub type DynCallToolHandler < S > = dyn Fn ( ToolCallContext < S > ) -> BoxFuture < ' static , Result < CallToolResult , crate :: Error > >
171
169
+ Send
172
170
+ Sync ;
173
171
/// Parameter Extractor
@@ -189,51 +187,32 @@ impl<P: JsonSchema> JsonSchema for Parameters<P> {
189
187
}
190
188
}
191
189
192
- /// Callee Extractor
193
- pub struct Callee < ' a , S > ( pub & ' a S ) ;
194
-
195
- impl < ' a , S > FromToolCallContextPart < ' a , S > for CancellationToken {
196
- fn from_tool_call_context_part (
197
- context : ToolCallContext < ' a , S > ,
198
- ) -> Result < ( Self , ToolCallContext < ' a , S > ) , crate :: Error > {
199
- Ok ( ( context. request_context . ct . clone ( ) , context) )
200
- }
201
- }
202
-
203
- impl < ' a , S > FromToolCallContextPart < ' a , S > for Callee < ' a , S > {
190
+ impl < S > FromToolCallContextPart < S > for CancellationToken {
204
191
fn from_tool_call_context_part (
205
- context : ToolCallContext < ' a , S > ,
206
- ) -> Result < ( Self , ToolCallContext < ' a , S > ) , crate :: Error > {
207
- Ok ( ( Callee ( context. service ) , context ) )
192
+ context : & mut ToolCallContext < S > ,
193
+ ) -> Result < Self , crate :: Error > {
194
+ Ok ( context. request_context . ct . clone ( ) )
208
195
}
209
196
}
210
197
211
198
pub struct ToolName ( pub Cow < ' static , str > ) ;
212
199
213
- impl < ' a , S > FromToolCallContextPart < ' a , S > for ToolName {
200
+ impl < S > FromToolCallContextPart < S > for ToolName {
214
201
fn from_tool_call_context_part (
215
- context : ToolCallContext < ' a , S > ,
216
- ) -> Result < ( Self , ToolCallContext < ' a , S > ) , crate :: Error > {
217
- Ok ( ( Self ( context. name . clone ( ) ) , context ) )
202
+ context : & mut ToolCallContext < S > ,
203
+ ) -> Result < Self , crate :: Error > {
204
+ Ok ( Self ( context. name . clone ( ) ) )
218
205
}
219
206
}
220
207
221
- impl < ' a , S > FromToolCallContextPart < ' a , S > for & ' a S {
222
- fn from_tool_call_context_part (
223
- context : ToolCallContext < ' a , S > ,
224
- ) -> Result < ( Self , ToolCallContext < ' a , S > ) , crate :: Error > {
225
- Ok ( ( context. service , context) )
226
- }
227
- }
228
-
229
- impl < ' a , S , K , V > FromToolCallContextPart < ' a , S > for Parameter < K , V >
208
+ impl < S , K , V > FromToolCallContextPart < S > for Parameter < K , V >
230
209
where
231
210
K : ConstString ,
232
211
V : DeserializeOwned ,
233
212
{
234
213
fn from_tool_call_context_part (
235
- context : ToolCallContext < ' a , S > ,
236
- ) -> Result < ( Self , ToolCallContext < ' a , S > ) , crate :: Error > {
214
+ context : & mut ToolCallContext < S > ,
215
+ ) -> Result < Self , crate :: Error > {
237
216
let arguments = context
238
217
. arguments
239
218
. as_ref ( )
@@ -255,17 +234,17 @@ where
255
234
None ,
256
235
)
257
236
} ) ?;
258
- Ok ( ( Parameter ( K :: default ( ) , value) , context ) )
237
+ Ok ( Parameter ( K :: default ( ) , value) )
259
238
}
260
239
}
261
240
262
- impl < ' a , S , P > FromToolCallContextPart < ' a , S > for Parameters < P >
241
+ impl < S , P > FromToolCallContextPart < S > for Parameters < P >
263
242
where
264
243
P : DeserializeOwned ,
265
244
{
266
245
fn from_tool_call_context_part (
267
- mut context : ToolCallContext < ' a , S > ,
268
- ) -> Result < ( Self , ToolCallContext < ' a , S > ) , crate :: Error > {
246
+ context : & mut ToolCallContext < S > ,
247
+ ) -> Result < Self , crate :: Error > {
269
248
let arguments = context. arguments . take ( ) . unwrap_or_default ( ) ;
270
249
let value: P =
271
250
serde_json:: from_value ( serde_json:: Value :: Object ( arguments) ) . map_err ( |e| {
@@ -274,37 +253,37 @@ where
274
253
None ,
275
254
)
276
255
} ) ?;
277
- Ok ( ( Parameters ( value) , context ) )
256
+ Ok ( Parameters ( value) )
278
257
}
279
258
}
280
259
281
- impl < ' a , S > FromToolCallContextPart < ' a , S > for JsonObject {
260
+ impl < S > FromToolCallContextPart < S > for JsonObject {
282
261
fn from_tool_call_context_part (
283
- mut context : ToolCallContext < ' a , S > ,
284
- ) -> Result < ( Self , ToolCallContext < ' a , S > ) , crate :: Error > {
262
+ context : & mut ToolCallContext < S > ,
263
+ ) -> Result < Self , crate :: Error > {
285
264
let object = context. arguments . take ( ) . unwrap_or_default ( ) ;
286
- Ok ( ( object, context ) )
265
+ Ok ( object)
287
266
}
288
267
}
289
268
290
- impl < ' a , S > FromToolCallContextPart < ' a , S > for crate :: model:: Extensions {
269
+ impl < S > FromToolCallContextPart < S > for crate :: model:: Extensions {
291
270
fn from_tool_call_context_part (
292
- context : ToolCallContext < ' a , S > ,
293
- ) -> Result < ( Self , ToolCallContext < ' a , S > ) , crate :: Error > {
271
+ context : & mut ToolCallContext < S > ,
272
+ ) -> Result < Self , crate :: Error > {
294
273
let extensions = context. request_context . extensions . clone ( ) ;
295
- Ok ( ( extensions, context ) )
274
+ Ok ( extensions)
296
275
}
297
276
}
298
277
299
278
pub struct Extension < T > ( pub T ) ;
300
279
301
- impl < ' a , S , T > FromToolCallContextPart < ' a , S > for Extension < T >
280
+ impl < S , T > FromToolCallContextPart < S > for Extension < T >
302
281
where
303
282
T : Send + Sync + ' static + Clone ,
304
283
{
305
284
fn from_tool_call_context_part (
306
- context : ToolCallContext < ' a , S > ,
307
- ) -> Result < ( Self , ToolCallContext < ' a , S > ) , crate :: Error > {
285
+ context : & mut ToolCallContext < S > ,
286
+ ) -> Result < Self , crate :: Error > {
308
287
let extension = context
309
288
. request_context
310
289
. extensions
@@ -316,14 +295,14 @@ where
316
295
None ,
317
296
)
318
297
} ) ?;
319
- Ok ( ( Extension ( extension) , context ) )
298
+ Ok ( Extension ( extension) )
320
299
}
321
300
}
322
301
323
- impl < ' s , S > ToolCallContext < ' s , S > {
302
+ impl < S > ToolCallContext < S > {
324
303
pub fn invoke < H , A > ( self , h : H ) -> H :: Fut
325
304
where
326
- H : CallToolHandler < ' s , S , A > ,
305
+ H : CallToolHandler < S , A > ,
327
306
{
328
307
h. call ( self )
329
308
}
@@ -333,6 +312,10 @@ impl<'s, S> ToolCallContext<'s, S> {
333
312
pub struct AsyncAdapter < P , Fut , R > ( PhantomData < ( fn ( P ) -> Fut , fn ( Fut ) -> R ) > ) ;
334
313
pub struct SyncAdapter < P , R > ( PhantomData < fn ( P ) -> R > ) ;
335
314
315
+ #[ allow( clippy:: type_complexity) ]
316
+ pub struct AsyncMethodAdapter < P , Fut , R > ( PhantomData < ( fn ( P ) -> Fut , fn ( Fut ) -> R ) > ) ;
317
+ pub struct SyncMethodAdapter < P , R > ( PhantomData < fn ( P ) -> R > ) ;
318
+
336
319
macro_rules! impl_for {
337
320
( $( $T: ident) * ) => {
338
321
impl_for!( [ ] [ $( $T) * ] ) ;
@@ -346,26 +329,26 @@ macro_rules! impl_for {
346
329
impl_for!( [ $( $Tn) * $Tn_1] [ $( $Rest) * ] ) ;
347
330
} ;
348
331
( @impl $( $Tn: ident) * ) => {
349
- impl <' s , $( $Tn, ) * S , F , Fut , R > CallToolHandler <' s , S , AsyncAdapter <( $( $Tn, ) * ) , Fut , R >> for F
332
+ impl <$( $Tn, ) * S , F , Fut , R > CallToolHandler <S , AsyncAdapter <( $( $Tn, ) * ) , Fut , R >> for F
350
333
where
351
334
$(
352
- $Tn: FromToolCallContextPart <' s , S > + ' s ,
335
+ $Tn: FromToolCallContextPart <S > ,
353
336
) *
354
- F : FnOnce ( $( $Tn, ) * ) -> Fut + Send + ' s ,
355
- Fut : Future <Output = R > + Send + ' s ,
356
- R : IntoCallToolResult + Send + ' s ,
337
+ F : FnOnce ( $( $Tn, ) * ) -> Fut + Send + ,
338
+ Fut : Future <Output = R > + Send + ,
339
+ R : IntoCallToolResult + Send + ,
357
340
S : Send + Sync ,
358
341
{
359
342
type Fut = IntoCallToolResultFut <Fut , R >;
360
343
#[ allow( unused_variables, non_snake_case) ]
361
344
fn call(
362
345
self ,
363
- context: ToolCallContext <' s , S >,
346
+ mut context: ToolCallContext <S >,
364
347
) -> Self :: Fut {
365
348
$(
366
- let result = $Tn:: from_tool_call_context_part( context) ;
367
- let ( $Tn, context ) = match result {
368
- Ok ( ( value, context ) ) => ( value, context ) ,
349
+ let result = $Tn:: from_tool_call_context_part( & mut context) ;
350
+ let $Tn = match result {
351
+ Ok ( value) => value,
369
352
Err ( e) => return IntoCallToolResultFut :: Ready {
370
353
result: std:: future:: ready( Err ( e) ) ,
371
354
} ,
@@ -378,25 +361,25 @@ macro_rules! impl_for {
378
361
}
379
362
}
380
363
381
- impl <' s , $( $Tn, ) * S , F , R > CallToolHandler <' s , S , SyncAdapter <( $( $Tn, ) * ) , R >> for F
364
+ impl <$( $Tn, ) * S , F , R > CallToolHandler <S , SyncAdapter <( $( $Tn, ) * ) , R >> for F
382
365
where
383
366
$(
384
- $Tn: FromToolCallContextPart <' s , S > + ' s ,
367
+ $Tn: FromToolCallContextPart <S > + ,
385
368
) *
386
- F : FnOnce ( $( $Tn, ) * ) -> R + Send + ' s ,
387
- R : IntoCallToolResult + Send + ' s ,
369
+ F : FnOnce ( $( $Tn, ) * ) -> R + Send + ,
370
+ R : IntoCallToolResult + Send + ,
388
371
S : Send + Sync ,
389
372
{
390
373
type Fut = Ready <Result <CallToolResult , crate :: Error >>;
391
374
#[ allow( unused_variables, non_snake_case) ]
392
375
fn call(
393
376
self ,
394
- context: ToolCallContext <' s , S >,
377
+ mut context: ToolCallContext <S >,
395
378
) -> Self :: Fut {
396
379
$(
397
- let result = $Tn:: from_tool_call_context_part( context) ;
398
- let ( $Tn, context ) = match result {
399
- Ok ( ( value, context ) ) => ( value, context ) ,
380
+ let result = $Tn:: from_tool_call_context_part( & mut context) ;
381
+ let $Tn = match result {
382
+ Ok ( value) => value,
400
383
Err ( e) => return std:: future:: ready( Err ( e) ) ,
401
384
} ;
402
385
) *
@@ -415,7 +398,7 @@ pub struct ToolBoxItem<S> {
415
398
impl < S : Send + Sync + ' static + Clone > ToolBoxItem < S > {
416
399
pub fn new < C > ( attr : crate :: model:: Tool , call : C ) -> Self
417
400
where
418
- C : Fn ( ToolCallContext < ' _ , S > ) -> BoxFuture < ' _ , Result < CallToolResult , crate :: Error > >
401
+ C : Fn ( ToolCallContext < S > ) -> BoxFuture < ' static , Result < CallToolResult , crate :: Error > >
419
402
+ Send
420
403
+ Sync
421
404
+ ' static ,
@@ -452,7 +435,7 @@ impl<S> ToolBox<S> {
452
435
453
436
pub async fn call (
454
437
& self ,
455
- context : ToolCallContext < ' _ , S > ,
438
+ context : ToolCallContext < S > ,
456
439
) -> Result < CallToolResult , crate :: Error > {
457
440
let item = self
458
441
. map
0 commit comments