1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
//! Builder methods shared by `generate_text`, `stream_text` and agents.
/// Generates the configuration methods for a builder type holding a
/// `config: CallConfig` field.
macro_rules! impl_call_builder {
($ty:ident) => {
$crate::builder::impl_call_builder!(@impl $ty [O]);
};
($ty:ident < $($generic:ident),+ >) => {
$crate::builder::impl_call_builder!(@impl $ty [$($generic),+]);
};
(@impl $ty:ident [$($generic:ident),+]) => {
impl<$($generic),+> $ty<$($generic),+> {
// ---- prompt ----
/// Sets system instructions.
#[must_use]
pub fn system(mut self, instructions: impl Into<$crate::prompt::Instructions>) -> Self {
self.config.system = Some(instructions.into());
self
}
/// Sets a single user prompt (mutually exclusive with `messages`).
#[must_use]
pub fn prompt(mut self, text: impl Into<String>) -> Self {
self.config.prompt = Some(text.into());
self
}
/// Sets the conversation messages (mutually exclusive with `prompt`).
#[must_use]
pub fn messages(
mut self,
messages: impl IntoIterator<Item = ::ferrin_message::Message>,
) -> Self {
self.config.messages = Some(messages.into_iter().collect());
self
}
/// Allows system messages inside `messages`.
#[must_use]
pub fn allow_system_in_messages(mut self) -> Self {
self.config.allow_system_in_messages = true;
self
}
// ---- sampling ----
/// Replaces all sampling settings.
#[must_use]
pub fn settings(mut self, settings: $crate::prompt::CallSettings) -> Self {
self.config.settings = settings;
self
}
/// Sets the maximum number of output tokens.
#[must_use]
pub fn max_output_tokens(mut self, n: u32) -> Self {
self.config.settings.max_output_tokens = Some(n);
self
}
/// Sets the sampling temperature.
#[must_use]
pub fn temperature(mut self, t: f64) -> Self {
self.config.settings.temperature = Some(t);
self
}
/// Sets nucleus sampling.
#[must_use]
pub fn top_p(mut self, p: f64) -> Self {
self.config.settings.top_p = Some(p);
self
}
/// Sets top-k sampling.
#[must_use]
pub fn top_k(mut self, k: u32) -> Self {
self.config.settings.top_k = Some(k);
self
}
/// Sets the presence penalty.
#[must_use]
pub fn presence_penalty(mut self, p: f64) -> Self {
self.config.settings.presence_penalty = Some(p);
self
}
/// Sets the frequency penalty.
#[must_use]
pub fn frequency_penalty(mut self, p: f64) -> Self {
self.config.settings.frequency_penalty = Some(p);
self
}
/// Sets stop sequences.
#[must_use]
pub fn stop_sequences(
mut self,
sequences: impl IntoIterator<Item = impl Into<String>>,
) -> Self {
self.config.settings.stop_sequences =
Some(sequences.into_iter().map(Into::into).collect());
self
}
/// Sets the random seed.
#[must_use]
pub fn seed(mut self, seed: u64) -> Self {
self.config.settings.seed = Some(seed);
self
}
/// Sets the reasoning effort.
#[must_use]
pub fn reasoning(mut self, effort: ::ferrin_spec::ReasoningEffort) -> Self {
self.config.settings.reasoning = effort;
self
}
// ---- tools ----
/// Sets the tool set.
#[must_use]
pub fn tools(mut self, tools: ::ferrin_tool::ToolSet) -> Self {
self.config.tools = tools;
self
}
/// Sets the tool choice.
#[must_use]
pub fn tool_choice(mut self, choice: ::ferrin_spec::ToolChoice) -> Self {
self.config.tool_choice = Some(choice);
self
}
/// Restricts the tools sent to the model.
#[must_use]
pub fn active_tools(
mut self,
names: impl IntoIterator<Item = impl Into<::ferrin_spec::ToolName>>,
) -> Self {
self.config.active_tools = Some(names.into_iter().map(Into::into).collect());
self
}
/// Sets the order in which tools are sent (listed first, rest
/// alphabetical).
#[must_use]
pub fn tool_order(
mut self,
names: impl IntoIterator<Item = impl Into<::ferrin_spec::ToolName>>,
) -> Self {
self.config.tool_order = names.into_iter().map(Into::into).collect();
self
}
/// Sets the shared tools context (validated against each tool's
/// context schema).
#[must_use]
pub fn tools_context(mut self, context: ::ferrin_spec::JsonValue) -> Self {
self.config.tools_context = Some(context);
self
}
/// Sets application state for step preparation, approval and lifecycle hooks.
/// This value is separate from tool execution context and is never sent to models.
#[must_use]
pub fn runtime_context(mut self, context: ::ferrin_spec::JsonValue) -> Self {
self.config.runtime_context = Some(context);
self
}
/// Sets the call-level approval policy.
#[must_use]
pub fn tool_approval(
mut self,
policy: impl $crate::generate_text::ApprovalPolicy + 'static,
) -> Self {
self.config.tool_approval = Some(::std::sync::Arc::new(policy));
self
}
/// Sets the secret used to sign approval requests.
#[must_use]
pub fn tool_approval_secret(mut self, secret: ::secrecy::SecretBox<[u8]>) -> Self {
self.config.tool_approval_secret = Some(::std::sync::Arc::new(secret));
self
}
/// Restricts which callers may trigger each tool.
#[must_use]
pub fn tool_callers(mut self, callers: ::ferrin_tool::ToolCallers) -> Self {
self.config.tool_callers = callers;
self
}
/// Sets the tool call repair function.
#[must_use]
pub fn repair_tool_call(
mut self,
repair: impl $crate::generate_text::ToolCallRepair + 'static,
) -> Self {
self.config.repair_tool_call = Some(::std::sync::Arc::new(repair));
self
}
/// Rewrites the validated input of `name` before execution.
#[must_use]
pub fn refine_tool_input(
mut self,
name: impl Into<::ferrin_spec::ToolName>,
refine: impl Fn(
::ferrin_spec::JsonValue,
) -> ::ferrin_spec::BoxFuture<
'static,
Result<::ferrin_spec::JsonValue, $crate::Error>,
> + Send
+ Sync
+ 'static,
) -> Self {
self.config
.refine_tool_inputs
.insert(name, ::std::sync::Arc::new(refine));
self
}
/// Sets the sandbox handed to tools.
#[cfg(feature = "sandbox")]
#[must_use]
pub fn sandbox(
mut self,
sandbox: ::std::sync::Arc<dyn ::ferrin_tool::Sandbox>,
) -> Self {
self.config.sandbox = Some(sandbox);
self
}
/// Limits how many tools execute concurrently within a step.
#[must_use]
pub fn max_tool_concurrency(mut self, n: usize) -> Self {
self.config.max_tool_concurrency = Some(n.max(1));
self
}
// ---- loop control ----
/// Adds a stop condition (any-of). Without conditions the loop
/// stops after one step.
#[must_use]
pub fn stop_when(
mut self,
condition: impl $crate::generate_text::StopCondition + 'static,
) -> Self {
self.config
.stop_conditions
.push(::std::sync::Arc::new(condition));
self
}
/// Sets the per-step preparation callback.
#[must_use]
pub fn prepare_step(
mut self,
prepare: impl $crate::generate_text::PrepareStep + 'static,
) -> Self {
self.config.prepare_step = Some(::std::sync::Arc::new(prepare));
self
}
// ---- request ----
/// Sets the maximum number of retries (exponential backoff).
#[must_use]
pub fn max_retries(mut self, n: u32) -> Self {
self.config.retry_policy.max_retries = n;
self
}
/// Replaces the retry policy.
#[must_use]
pub fn retry_policy(mut self, policy: $crate::retry::RetryPolicy) -> Self {
self.config.retry_policy = policy;
self
}
/// Sets timeouts.
#[must_use]
pub fn timeout(mut self, timeout: impl Into<$crate::timeout::Timeout>) -> Self {
self.config.timeout = timeout.into();
self
}
/// Sets the cancellation token.
#[must_use]
pub fn cancellation(mut self, token: ::tokio_util::sync::CancellationToken) -> Self {
self.config.cancellation = token;
self
}
/// Adds request headers.
#[must_use]
pub fn headers(mut self, headers: ::ferrin_spec::Headers) -> Self {
self.config.settings.headers.merge(&headers);
self
}
/// Adds provider options.
#[must_use]
pub fn provider_options(mut self, options: ::ferrin_spec::ProviderOptions) -> Self {
for (key, value) in options {
self.config
.settings
.provider_options
.entry(key)
.or_default()
.extend(value);
}
self
}
/// Sets the download function for file URLs.
#[must_use]
pub fn download(
mut self,
download: ::std::sync::Arc<dyn $crate::prompt::DownloadFn>,
) -> Self {
self.config.download = Some(download);
self
}
/// Controls which payloads are kept in step results.
#[must_use]
pub fn include(mut self, include: $crate::generate_text::Include) -> Self {
self.config.include = include;
self
}
/// Sets the clock used for `response.timestamp` when the provider sends none.
#[must_use]
pub fn clock(mut self, clock: ::std::sync::Arc<dyn $crate::clock::Clock>) -> Self {
self.config.clock = clock;
self
}
/// Sets the id generator.
#[must_use]
pub fn id_generator(
mut self,
generator: ::std::sync::Arc<dyn ::ferrin_provider_util::IdGenerator>,
) -> Self {
self.config.id_generator = generator;
self
}
// ---- observability ----
/// Sets telemetry options.
#[must_use]
pub fn telemetry(mut self, options: $crate::telemetry::TelemetryOptions) -> Self {
self.config.telemetry = options;
self
}
/// Adds all hooks of `hooks`.
#[must_use]
pub fn hooks(mut self, hooks: $crate::hooks::Hooks) -> Self {
self.config.hooks = ::std::mem::take(&mut self.config.hooks).merged(hooks);
self
}
/// Runs when the call starts.
#[must_use]
pub fn on_start(
mut self,
f: impl $crate::hooks::HookFn<$crate::telemetry::StartEvent>,
) -> Self {
self.config.hooks.on_start.push(::std::sync::Arc::new(f));
self
}
/// Runs when a step starts.
#[must_use]
pub fn on_step_start(
mut self,
f: impl $crate::hooks::HookFn<$crate::telemetry::StepStartEvent>,
) -> Self {
self.config
.hooks
.on_step_start
.push(::std::sync::Arc::new(f));
self
}
/// Runs before each model call.
#[must_use]
pub fn on_language_model_call_start(
mut self,
f: impl $crate::hooks::HookFn<$crate::telemetry::ModelCallStartEvent>,
) -> Self {
self.config
.hooks
.on_language_model_call_start
.push(::std::sync::Arc::new(f));
self
}
/// Runs after each model call.
#[must_use]
pub fn on_language_model_call_end(
mut self,
f: impl $crate::hooks::HookFn<$crate::telemetry::ModelCallEndEvent>,
) -> Self {
self.config
.hooks
.on_language_model_call_end
.push(::std::sync::Arc::new(f));
self
}
/// Runs before each tool execution.
#[must_use]
pub fn on_tool_execution_start(
mut self,
f: impl $crate::hooks::HookFn<$crate::telemetry::ToolExecutionStartEvent>,
) -> Self {
self.config
.hooks
.on_tool_execution_start
.push(::std::sync::Arc::new(f));
self
}
/// Runs after each tool execution.
#[must_use]
pub fn on_tool_execution_end(
mut self,
f: impl $crate::hooks::HookFn<$crate::telemetry::ToolExecutionEndEvent>,
) -> Self {
self.config
.hooks
.on_tool_execution_end
.push(::std::sync::Arc::new(f));
self
}
/// Runs when a step finishes.
#[must_use]
pub fn on_step_end(
mut self,
f: impl $crate::hooks::HookFn<$crate::generate_text::StepResult>,
) -> Self {
self.config.hooks.on_step_end.push(::std::sync::Arc::new(f));
self
}
/// Runs when the call finishes.
#[must_use]
pub fn on_end(
mut self,
f: impl $crate::hooks::HookFn<$crate::telemetry::EndEvent>,
) -> Self {
self.config.hooks.on_end.push(::std::sync::Arc::new(f));
self
}
}
};
}
pub(crate) use impl_call_builder;