polyc_llm/request.rs
1//! Request-side LLM types: [`CompletionRequest`], [`Message`], [`Content`],
2//! [`ToolSpec`], [`ToolChoice`], and [`JsonSchema`].
3
4use serde::{Deserialize, Serialize};
5
6// ── CompletionRequest ─────────────────────────────────────────────────────────
7
8/// Top-level request to an LLM provider.
9///
10/// Construct via [`CompletionRequest::new`], then populate fields directly.
11///
12/// `#[non_exhaustive]`: provider-shaped sampling fields (`top_p`, `seed`, …)
13/// will be added over time; build through [`new`](CompletionRequest::new) so
14/// such additions stay non-breaking.
15#[derive(Debug, Clone, Serialize, Deserialize)]
16#[non_exhaustive]
17pub struct CompletionRequest {
18 /// The model identifier (e.g. `"fast-2"`, `"reasoning-pro"`).
19 pub model: String,
20 /// Optional system prompt text prepended before the conversation.
21 pub system: Option<String>,
22 /// Ordered list of messages in the conversation.
23 pub messages: Vec<Message>,
24 /// Tool definitions available to the model.
25 pub tools: Vec<ToolSpec>,
26 /// How the model should decide whether to call a tool.
27 pub tool_choice: ToolChoice,
28 /// When set, instructs the provider to return structured JSON output.
29 pub response_format: Option<JsonSchema>,
30 /// Maximum number of tokens the model may generate.
31 pub max_tokens: Option<u32>,
32 /// Sampling temperature in `[0.0, 2.0]`. Lower is more deterministic.
33 pub temperature: Option<f32>,
34 /// Token sequences that cause the model to stop generating.
35 pub stop: Vec<String>,
36 /// When `true`, the model may search the public web to ground its answer.
37 ///
38 /// This is a provider-agnostic capability hint: a provider maps it to its
39 /// native mechanism (Vertex Gemini → the `googleSearch` grounding tool,
40 /// alongside any `tools` function declarations) and a provider without web
41 /// search ignores it. Defaults to `false`; the agent's answering loop
42 /// (`run_turn`) sets it from its `RunTurnOptions.web_search`, so auxiliary
43 /// calls (summarization, classification) that bypass that loop never offer
44 /// search.
45 pub web_search: bool,
46 /// Provider-agnostic hint about caching the request's stable prefix.
47 ///
48 /// See [`CacheHint`]. Defaults to [`CacheHint::None`]; the agent's answering
49 /// loop sets it from its `RunTurnOptions` so a multi-step turn can cache the
50 /// system text + tool-spec block once and skip re-processing it each step.
51 pub cache: CacheHint,
52}
53
54impl CompletionRequest {
55 /// Creates a new request for the given `model` with sensible defaults:
56 /// empty `messages`, `tools`, and `stop` lists; `tool_choice` set to
57 /// [`ToolChoice::Auto`]; all optional fields `None`.
58 #[must_use]
59 pub fn new(model: impl Into<String>) -> Self {
60 Self {
61 model: model.into(),
62 system: None,
63 messages: Vec::new(),
64 tools: Vec::new(),
65 tool_choice: ToolChoice::Auto,
66 response_format: None,
67 max_tokens: None,
68 temperature: None,
69 stop: Vec::new(),
70 web_search: false,
71 cache: CacheHint::None,
72 }
73 }
74}
75
76// ── CacheHint ─────────────────────────────────────────────────────────────────
77
78/// Provider-agnostic hint about caching a request's stable prefix.
79///
80/// A multi-step turn re-sends a growing conversation behind an unchanging
81/// prefix — the system text plus the tool-spec block (built once per turn). This
82/// hint marks that prefix as stable so a provider that supports prompt caching
83/// can cache it and skip re-processing it on every step, the single biggest
84/// latency lever on multi-step turns. It is purely advisory: a provider maps it
85/// to its native mechanism, and a provider with no caching ignores it with no
86/// behavior change.
87#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
88#[serde(rename_all = "snake_case")]
89#[non_exhaustive]
90pub enum CacheHint {
91 /// No caching is requested; a provider processes the full prompt on every
92 /// call. The default so auxiliary calls that bypass the answering loop never
93 /// opt in accidentally.
94 #[default]
95 None,
96 /// The request's leading stable prefix (system text + tool-spec block) stays
97 /// byte-identical across the steps of a turn — and across turns of a
98 /// conversation — so a provider that supports prompt caching should cache it.
99 StablePrefix {
100 /// Optional stable per-conversation identifier a provider MAY use to pin
101 /// cache routing (a provider maps it to its own cache-key field). A
102 /// provider with only implicit prefix caching, or none at all, ignores
103 /// it. `None` leaves routing to the provider's implicit prefix match.
104 key: Option<String>,
105 },
106}
107
108impl CacheHint {
109 /// Reconstructs a hint from its flat single-string form: a non-empty `key`
110 /// is a keyed [`CacheHint::StablePrefix`]; an empty one is
111 /// [`CacheHint::None`]. The inverse of [`Self::key`] — together they carry
112 /// the hint across a boundary that has one string field (the control-plane
113 /// → harness turn input), which cannot express a keyless stable-prefix
114 /// hint (the control plane always keys by conversation).
115 #[must_use]
116 pub fn from_key(key: String) -> Self {
117 if key.is_empty() {
118 Self::None
119 } else {
120 Self::StablePrefix { key: Some(key) }
121 }
122 }
123
124 /// The cache-routing key this hint carries, if any. See [`Self::from_key`]
125 /// for the flat form the pair round-trips.
126 #[must_use]
127 pub fn key(&self) -> Option<&str> {
128 match self {
129 Self::StablePrefix { key: Some(key) } => Some(key),
130 _ => None,
131 }
132 }
133}
134
135// ── Message ───────────────────────────────────────────────────────────────────
136
137/// A single turn in a conversation, composed of one or more [`Content`] parts.
138#[derive(Debug, Clone, Serialize, Deserialize)]
139pub struct Message {
140 /// The participant that produced this message.
141 pub role: Role,
142 /// Ordered content blocks that make up the message body.
143 pub content: Vec<Content>,
144}
145
146impl Message {
147 /// Creates a [`Role::User`] message with a single [`Content::Text`] block.
148 #[must_use]
149 pub fn user(text: impl Into<String>) -> Self {
150 Self {
151 role: Role::User,
152 content: vec![Content::Text(text.into())],
153 }
154 }
155
156 /// Creates a [`Role::Assistant`] message with a single [`Content::Text`] block.
157 #[must_use]
158 pub fn assistant(text: impl Into<String>) -> Self {
159 Self {
160 role: Role::Assistant,
161 content: vec![Content::Text(text.into())],
162 }
163 }
164
165 /// Creates a [`Role::System`] message with a single [`Content::Text`] block.
166 #[must_use]
167 pub fn system(text: impl Into<String>) -> Self {
168 Self {
169 role: Role::System,
170 content: vec![Content::Text(text.into())],
171 }
172 }
173}
174
175// ── Role ──────────────────────────────────────────────────────────────────────
176
177/// The participant role for a [`Message`].
178#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
179#[serde(rename_all = "snake_case")]
180#[non_exhaustive]
181pub enum Role {
182 /// A human turn.
183 User,
184 /// A model-generated turn.
185 Assistant,
186 /// A system-level instruction (not all providers support this as a role).
187 System,
188 /// A tool-result turn injected back into the conversation.
189 Tool,
190}
191
192// ── Content ───────────────────────────────────────────────────────────────────
193
194/// A single content block within a [`Message`].
195#[derive(Debug, Clone, Serialize, Deserialize)]
196#[serde(rename_all = "snake_case")]
197#[non_exhaustive]
198pub enum Content {
199 /// Plain text.
200 Text(String),
201 /// A tool invocation emitted by the model.
202 ToolUse(ToolCall),
203 /// The result of a prior [`Content::ToolUse`], fed back to the model.
204 ToolResult(ToolResult),
205 /// A reference to an image (HTTP URL or `data:` URI).
206 Image(ImageRef),
207}
208
209impl Content {
210 /// Wraps `s` in a [`Content::Text`] variant.
211 #[must_use]
212 pub fn text(s: impl Into<String>) -> Self {
213 Self::Text(s.into())
214 }
215
216 /// Constructs a [`Content::ToolUse`] block.
217 #[must_use]
218 pub fn tool_use(
219 id: impl Into<String>,
220 name: impl Into<String>,
221 args_json: impl Into<String>,
222 ) -> Self {
223 Self::ToolUse(ToolCall {
224 id: id.into(),
225 name: name.into(),
226 args_json: args_json.into(),
227 signature: None,
228 approval_turn_id: None,
229 })
230 }
231
232 /// Constructs a [`Content::ToolUse`] block carrying an opaque
233 /// provider-specific `signature` (e.g. a thinking model's thought
234 /// signature, which some providers require echoed back on the next
235 /// request that includes this call).
236 #[must_use]
237 pub fn tool_use_signed(
238 id: impl Into<String>,
239 name: impl Into<String>,
240 args_json: impl Into<String>,
241 signature: Option<String>,
242 ) -> Self {
243 Self::ToolUse(ToolCall {
244 id: id.into(),
245 name: name.into(),
246 args_json: args_json.into(),
247 signature,
248 approval_turn_id: None,
249 })
250 }
251
252 /// Constructs a [`Content::ToolResult`] block.
253 ///
254 /// `first_party` is the ingestion-time provenance bit (see
255 /// [`ToolResult::first_party`]) — pass the caller's already-computed
256 /// verdict (the static per-tool-name check for an ordinary tool, or the
257 /// worker-derived verdict for a `__delegate_to` result), not a fixed
258 /// default: the whole point of carrying this on [`ToolResult`] is that
259 /// the live same-turn taint scan needs the REAL per-call answer.
260 #[must_use]
261 pub fn tool_result(
262 tool_call_id: impl Into<String>,
263 result_json: impl Into<String>,
264 is_error: bool,
265 first_party: bool,
266 ) -> Self {
267 Self::ToolResult(ToolResult {
268 tool_call_id: tool_call_id.into(),
269 result_json: result_json.into(),
270 is_error,
271 first_party,
272 })
273 }
274
275 /// Constructs a [`Content::Image`] block.
276 #[must_use]
277 pub fn image(url: impl Into<String>, mime_type: Option<String>) -> Self {
278 Self::Image(ImageRef {
279 url: url.into(),
280 mime_type,
281 })
282 }
283}
284
285// ── ToolCall ──────────────────────────────────────────────────────────────────
286
287/// A tool call emitted by the model inside an assistant [`Message`].
288///
289/// Mirrors the wire-side `polychrome.agent.v1.ToolCall`; surfaced inside a
290/// [`Content::ToolUse`] block.
291#[derive(Debug, Clone, Serialize, Deserialize)]
292pub struct ToolCall {
293 /// Provider-assigned call identifier, used to correlate with [`ToolResult`].
294 pub id: String,
295 /// Name of the tool being called.
296 pub name: String,
297 /// Arguments serialized as a JSON string (opaque at this layer).
298 pub args_json: String,
299 /// Opaque, provider-specific signature attached to this call (e.g. a
300 /// thinking model's thought signature). Some providers require it to be
301 /// echoed back verbatim on the follow-up request that carries this call
302 /// in the history; `None` when the provider emits no such token.
303 #[serde(default, skip_serializing_if = "Option::is_none")]
304 pub signature: Option<String>,
305 /// Durable turn that emitted this call, when reconstructed from the event
306 /// log for an approval resume. Provider output leaves it unset.
307 #[serde(default, skip_serializing_if = "Option::is_none")]
308 pub approval_turn_id: Option<String>,
309}
310
311// ── ToolResult ────────────────────────────────────────────────────────────────
312
313/// The result of executing a tool, fed back to the model as a [`Content`] block.
314#[derive(Debug, Clone, Serialize, Deserialize)]
315pub struct ToolResult {
316 /// Matches the [`ToolCall::id`] this result corresponds to.
317 pub tool_call_id: String,
318 /// Serialized JSON payload returned by the tool executor.
319 pub result_json: String,
320 /// `true` when the tool raised an error rather than producing output.
321 pub is_error: bool,
322 /// Ingestion-time provenance: `true` when the producing tool is
323 /// first-party and closed-domain (does not ingest untrusted open-world
324 /// content). Mirrors `polyc_proto`'s wire `ToolResultContent.first_party`
325 /// — this is the SAME bit, carried on the in-memory, provider-facing
326 /// representation so the live same-turn taint scan
327 /// (`untrusted_content_in_context`) reads a per-call signal instead of
328 /// re-deriving it from the tool name. A `__delegate_to` call's result
329 /// reflects what the delegated worker actually touched, not a static
330 /// per-tool-name check — see `polyc_agent`'s `DelegateRecord::first_party`.
331 pub first_party: bool,
332}
333
334// ── ImageRef ──────────────────────────────────────────────────────────────────
335
336/// A reference to an image attached to a [`Message`].
337#[derive(Debug, Clone, Default, Serialize, Deserialize)]
338pub struct ImageRef {
339 /// HTTP URL or `data:` URI for the image bytes.
340 pub url: String,
341 /// Optional MIME type hint (e.g. `"image/png"`).
342 pub mime_type: Option<String>,
343}
344
345// ── ToolSpec ──────────────────────────────────────────────────────────────────
346
347/// Declaration of a tool the model may invoke.
348///
349/// Mirrors the MCP `Tool` shape so built-in and connector tools are described
350/// uniformly: `title` is the MCP `title` annotation, and `read_only` /
351/// `destructive` / `open_world` are the `readOnlyHint` / `destructiveHint` /
352/// `openWorldHint` annotations. Build with [`ToolSpec::new`] + the chainable
353/// setters rather than a struct literal.
354#[derive(Debug, Clone, Serialize, Deserialize, Default)]
355// The flags are independent MCP-style annotation hints (each serialized as an
356// optional bool); folding them into an enum/bitflags would fight serde's
357// per-field optional-default model for no gain.
358#[allow(clippy::struct_excessive_bools)]
359pub struct ToolSpec {
360 /// Unique tool name; the model references this when emitting a [`ToolCall`].
361 pub name: String,
362 /// Human-readable description of what the tool does.
363 pub description: String,
364 /// JSON Schema object describing the tool's argument shape.
365 pub schema_json: serde_json::Value,
366 /// MCP-style human display name for this tool (the `title` annotation):
367 /// a friendly label shown to people (e.g. in an approval prompt) while the
368 /// machine-facing [`name`](ToolSpec::name) stays the audit identifier.
369 ///
370 /// `None` means no curated label was provided; callers derive a display
371 /// name from [`name`](ToolSpec::name) via `polyc_proto::humanize_tool_name`.
372 #[serde(default, skip_serializing_if = "Option::is_none")]
373 pub title: Option<String>,
374 /// Intrinsic "this tool is side-effecting / requires human approval" flag.
375 ///
376 /// When `true` the tool must be routed through the harness's
377 /// human-in-the-loop (HITL) approval gate before it executes, even when no
378 /// operator-side allow-list names it. Pure, read-only tools leave this
379 /// `false`.
380 ///
381 /// This is the per-tool generalization of the old hard-coded
382 /// approval-by-name list: it maps from the MCP `destructiveHint` tool
383 /// annotation, so an upstream connector that advertises a destructive tool
384 /// is gated per-tool rather than per-connector.
385 ///
386 /// Defaults to `false` and is skipped when serializing the safe default, so
387 /// older payloads that omit the field still deserialize as ungated.
388 #[serde(default, skip_serializing_if = "std::ops::Not::not")]
389 pub needs_approval: bool,
390 /// MCP `readOnlyHint`: the tool does not modify its environment. Advisory —
391 /// surfaced to the model and usable by callers (e.g. sandbox-mode gating
392 /// never gates a read-only tool). Defaults to `false`.
393 #[serde(default, skip_serializing_if = "std::ops::Not::not")]
394 pub read_only: bool,
395 /// MCP `destructiveHint`: the tool may perform irreversible / side-effecting
396 /// changes. Drives sandbox-mode gating (destructive tools gate in read-only
397 /// mode) and maps from a connector's `destructiveHint`. Defaults to `false`.
398 #[serde(default, skip_serializing_if = "std::ops::Not::not")]
399 pub destructive: bool,
400 /// MCP `openWorldHint`: the tool may interact with an open world of external
401 /// entities, so its RESULT can carry content of uncontrolled provenance. This
402 /// is the INBOUND ("untrusted content in context") leg of the lethal
403 /// trifecta — a tool with `open_world = true` seeds the leg when its result
404 /// is in context (see `polyc_agent`'s `untrusted_content_in_context`). The
405 /// built-in web fetchers set it; the sandbox coding tools do not. For a
406 /// dialed connector it is read from `openWorldHint` at connect. Defaults to
407 /// `false`.
408 #[serde(default, skip_serializing_if = "std::ops::Not::not")]
409 pub open_world: bool,
410 /// Whether a single human approval for this tool may be *remembered* for the
411 /// rest of a conversation session (per-caller) and reused for later calls,
412 /// instead of re-prompting every time. Defaults to `false`.
413 ///
414 /// The session grant is **per-tool, not per-argument**: approving one call
415 /// authorizes the tool for ANY arguments for the rest of the session. So set
416 /// this ONLY when the tool's ENTIRE argument space is safe to auto-run within
417 /// the sandbox boundary — i.e. it is both idempotent AND can't reach anything
418 /// the human wouldn't have blanket-approved. `file_read` qualifies because it
419 /// is workspace-confined (`coding::workspace::resolve` rejects absolute/`..`
420 /// paths), so "approve one read" only ever grants reads inside the sandbox.
421 /// NEVER set it on a tool that spends money, has side effects, or whose risk
422 /// varies by argument (e.g. it could read/write outside a confined root):
423 /// those must get a fresh decision per call.
424 #[serde(default, skip_serializing_if = "std::ops::Not::not")]
425 pub cacheable_approval: bool,
426}
427
428impl ToolSpec {
429 /// A tool spec with the given `name`, `description`, and JSON-Schema
430 /// `schema_json`; all annotations default off. Chain the setters below to
431 /// add a title or mark it read-only / destructive / approval-gated.
432 #[must_use]
433 pub fn new(
434 name: impl Into<String>,
435 description: impl Into<String>,
436 schema_json: serde_json::Value,
437 ) -> Self {
438 Self {
439 name: name.into(),
440 description: description.into(),
441 schema_json,
442 title: None,
443 needs_approval: false,
444 read_only: false,
445 destructive: false,
446 open_world: false,
447 cacheable_approval: false,
448 }
449 }
450
451 /// Set the MCP `title` display annotation.
452 #[must_use]
453 pub fn titled(mut self, title: impl Into<String>) -> Self {
454 self.title = Some(title.into());
455 self
456 }
457
458 /// Mark the tool read-only (MCP `readOnlyHint`).
459 #[must_use]
460 pub const fn read_only(mut self) -> Self {
461 self.read_only = true;
462 self
463 }
464
465 /// Mark the tool destructive (MCP `destructiveHint`).
466 #[must_use]
467 pub const fn destructive(mut self) -> Self {
468 self.destructive = true;
469 self
470 }
471
472 /// Mark the tool open-world (MCP `openWorldHint`): its result can carry
473 /// content of uncontrolled provenance, seeding the untrusted-content leg.
474 #[must_use]
475 pub const fn open_world(mut self) -> Self {
476 self.open_world = true;
477 self
478 }
479
480 /// Mark a single approval for this tool as rememberable for the rest of a
481 /// conversation session (per-caller). Only set this on idempotent tools (see
482 /// [`Self::cacheable_approval`] field docs).
483 #[must_use]
484 pub const fn cacheable_approval(mut self) -> Self {
485 self.cacheable_approval = true;
486 self
487 }
488
489 /// Mark the tool as intrinsically requiring HITL approval (independent of
490 /// sandbox mode — e.g. `paid_fetch`).
491 #[must_use]
492 pub const fn approval_required(mut self) -> Self {
493 self.needs_approval = true;
494 self
495 }
496}
497
498/// The model-facing note appended to a gated tool's [`ToolSpec::description`]
499/// (`#743`) so the tool is self-describing as propose-first.
500///
501/// The model stops guessing at approval/execution status, which the runtime —
502/// never the model — owns and reports (the approval card, then the resume's
503/// genuine result narration).
504///
505/// This lives here, in `polyc-llm`, rather than in `polyc-agent` or
506/// `polyc-capability`, because the layer graph runs foundation ⇒ component:
507/// `polyc-agent` composes the intrinsic [`ToolSpec::needs_approval`] flag with
508/// the capability gate to decide WHICH specs are gated, then appends this
509/// shared literal ONCE per turn at spec-pinning time — so every provider
510/// builder that forwards [`ToolSpec::description`] verbatim carries the same
511/// wording without either of them depending back down into this crate's
512/// caller.
513///
514/// Per the project's copy rules: a complete, warm, plain sentence — no
515/// vendor names, no internal jargon, no apology words.
516pub static GATED_TOOL_APPROVAL_NOTE: std::sync::LazyLock<String> = std::sync::LazyLock::new(|| {
517 format!(
518 "Calling this pauses while a person reviews the request. \
519 {APPROVAL_STATUS_GROUND_RULE} A result from this tool means it was approved and \
520 has already run."
521 )
522});
523
524/// The single authoritative statement of who reports approval status: the
525/// runtime, never the model.
526///
527/// Every model-facing note that touches approval status — the gated-tool
528/// description note ([`GATED_TOOL_APPROVAL_NOTE`]) and the agent loop's
529/// resume ground-truth note — embeds this sentence verbatim, so the
530/// guidance is defined once and cannot drift between the moments it is
531/// given. It lives here, in `polyc-llm`, for the same layer reason as
532/// [`GATED_TOOL_APPROVAL_NOTE`]: a foundation crate both the agent loop
533/// and every provider builder can reach without depending on each other.
534pub const APPROVAL_STATUS_GROUND_RULE: &str = "Never describe approval status yourself — the \
535 system shows what's pending and what ran.";
536
537// ── ToolChoice ────────────────────────────────────────────────────────────────
538
539/// Controls whether and how the model calls tools.
540#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
541#[serde(rename_all = "snake_case")]
542#[non_exhaustive]
543pub enum ToolChoice {
544 /// The model decides whether to call a tool (default).
545 Auto,
546 /// The model must not call any tool.
547 None,
548 /// The model must call at least one tool.
549 Required,
550 /// Force the model to call the named tool.
551 Named(String),
552}
553
554// ── JsonSchema ────────────────────────────────────────────────────────────────
555
556/// Wrapper for a response-format JSON Schema.
557///
558/// Instructs the provider to return structured output conforming to the schema.
559/// Serializes transparently as the inner [`serde_json::Value`].
560#[derive(Debug, Clone, Serialize, Deserialize)]
561#[serde(transparent)]
562pub struct JsonSchema(
563 /// The raw JSON Schema value.
564 pub serde_json::Value,
565);
566
567// ── Tests ─────────────────────────────────────────────────────────────────────
568
569#[cfg(test)]
570mod tests {
571 #![allow(clippy::pedantic, clippy::nursery, missing_docs)]
572
573 use serde_json::{Value, json};
574
575 use super::*;
576
577 #[test]
578 fn new_sets_model_and_defaults() {
579 let req = CompletionRequest::new("fast-2");
580 assert_eq!(req.model, "fast-2");
581 assert!(req.messages.is_empty());
582 assert!(req.tools.is_empty());
583 assert!(req.stop.is_empty());
584 assert!(req.system.is_none());
585 assert!(req.max_tokens.is_none());
586 assert!(req.temperature.is_none());
587 assert!(req.response_format.is_none());
588 assert_eq!(req.tool_choice, ToolChoice::Auto);
589 }
590
591 #[test]
592 fn role_serializes_to_snake_case() {
593 assert_eq!(serde_json::to_string(&Role::User).unwrap(), r#""user""#);
594 assert_eq!(
595 serde_json::to_string(&Role::Assistant).unwrap(),
596 r#""assistant""#
597 );
598 assert_eq!(serde_json::to_string(&Role::System).unwrap(), r#""system""#);
599 assert_eq!(serde_json::to_string(&Role::Tool).unwrap(), r#""tool""#);
600 }
601
602 #[test]
603 fn role_round_trips() {
604 for role in [Role::User, Role::Assistant, Role::System, Role::Tool] {
605 let json = serde_json::to_string(&role).unwrap();
606 let back: Role = serde_json::from_str(&json).unwrap();
607 assert_eq!(back, role);
608 }
609 }
610
611 #[test]
612 fn tool_choice_unit_variants_serialize_as_strings() {
613 assert_eq!(
614 serde_json::to_string(&ToolChoice::Auto).unwrap(),
615 r#""auto""#
616 );
617 assert_eq!(
618 serde_json::to_string(&ToolChoice::None).unwrap(),
619 r#""none""#
620 );
621 assert_eq!(
622 serde_json::to_string(&ToolChoice::Required).unwrap(),
623 r#""required""#
624 );
625 }
626
627 #[test]
628 fn tool_choice_named_serializes_as_object() {
629 let tc = ToolChoice::Named("my_tool".to_owned());
630 let v: Value = serde_json::to_value(&tc).unwrap();
631 assert_eq!(v, json!({"named": "my_tool"}));
632 }
633
634 #[test]
635 fn tool_choice_round_trips() {
636 for tc in [
637 ToolChoice::Auto,
638 ToolChoice::None,
639 ToolChoice::Required,
640 ToolChoice::Named("search".to_owned()),
641 ] {
642 let json = serde_json::to_string(&tc).unwrap();
643 let back: ToolChoice = serde_json::from_str(&json).unwrap();
644 assert_eq!(back, tc);
645 }
646 }
647
648 #[test]
649 fn content_text_constructor() {
650 let c = Content::text("hello");
651 assert!(matches!(c, Content::Text(s) if s == "hello"));
652 }
653
654 #[test]
655 fn content_tool_use_constructor() {
656 let c = Content::tool_use("call-1", "search", r#"{"q":"rust"}"#);
657 match c {
658 Content::ToolUse(tu) => {
659 assert_eq!(tu.id, "call-1");
660 assert_eq!(tu.name, "search");
661 assert_eq!(tu.args_json, r#"{"q":"rust"}"#);
662 }
663 _ => panic!("wrong variant"),
664 }
665 }
666
667 #[test]
668 fn content_tool_result_constructor() {
669 let c = Content::tool_result("call-1", r#"{"result":"ok"}"#, false, true);
670 match c {
671 Content::ToolResult(tr) => {
672 assert_eq!(tr.tool_call_id, "call-1");
673 assert_eq!(tr.result_json, r#"{"result":"ok"}"#);
674 assert!(!tr.is_error);
675 assert!(tr.first_party);
676 }
677 _ => panic!("wrong variant"),
678 }
679 }
680
681 #[test]
682 fn content_image_constructor() {
683 let c = Content::image("https://example.com/img.png", Some("image/png".to_owned()));
684 match c {
685 Content::Image(img) => {
686 assert_eq!(img.url, "https://example.com/img.png");
687 assert_eq!(img.mime_type.as_deref(), Some("image/png"));
688 }
689 _ => panic!("wrong variant"),
690 }
691 }
692
693 #[test]
694 fn message_user_constructor() {
695 let m = Message::user("hi");
696 assert_eq!(m.role, Role::User);
697 assert_eq!(m.content.len(), 1);
698 assert!(matches!(&m.content[0], Content::Text(s) if s == "hi"));
699 }
700
701 #[test]
702 fn message_assistant_constructor() {
703 let m = Message::assistant("hello back");
704 assert_eq!(m.role, Role::Assistant);
705 assert_eq!(m.content.len(), 1);
706 assert!(matches!(&m.content[0], Content::Text(s) if s == "hello back"));
707 }
708
709 #[test]
710 fn message_system_constructor() {
711 let m = Message::system("You are helpful.");
712 assert_eq!(m.role, Role::System);
713 assert_eq!(m.content.len(), 1);
714 assert!(matches!(&m.content[0], Content::Text(_)));
715 }
716
717 #[test]
718 fn tool_use_args_json_preserved_as_opaque_string() {
719 let original = r#"{"nested":{"key":42},"arr":[1,2,3]}"#;
720 let c = Content::tool_use("id-42", "complex_tool", original);
721 let serialized = serde_json::to_string(&c).unwrap();
722 let back: Content = serde_json::from_str(&serialized).unwrap();
723 match back {
724 Content::ToolUse(tu) => assert_eq!(tu.args_json, original),
725 _ => panic!("wrong variant"),
726 }
727 }
728
729 #[test]
730 fn completion_request_round_trips_all_content_variants() {
731 let mut req = CompletionRequest::new("test-model");
732 req.system = Some("Be concise.".to_owned());
733 req.max_tokens = Some(256);
734 req.temperature = Some(0.7);
735 req.stop = vec!["<end>".to_owned()];
736 req.tool_choice = ToolChoice::Named("calculator".to_owned());
737 req.response_format = Some(JsonSchema(json!({"type": "object"})));
738 req.tools = vec![ToolSpec::new(
739 "calculator",
740 "Evaluates math expressions.",
741 json!({"type": "object", "properties": {"expr": {"type": "string"}}}),
742 )];
743 req.messages = vec![
744 Message::user("Compute 2+2"),
745 Message {
746 role: Role::Assistant,
747 content: vec![Content::tool_use(
748 "call-1",
749 "calculator",
750 r#"{"expr":"2+2"}"#,
751 )],
752 },
753 Message {
754 role: Role::Tool,
755 content: vec![Content::tool_result(
756 "call-1",
757 r#"{"value":4}"#,
758 false,
759 true,
760 )],
761 },
762 Message {
763 role: Role::User,
764 content: vec![Content::image(
765 "https://example.com/chart.png",
766 Some("image/png".to_owned()),
767 )],
768 },
769 ];
770
771 let json_str = serde_json::to_string(&req).unwrap();
772 let back: CompletionRequest = serde_json::from_str(&json_str).unwrap();
773
774 assert_eq!(back.model, "test-model");
775 assert_eq!(back.system.as_deref(), Some("Be concise."));
776 assert_eq!(back.max_tokens, Some(256));
777 assert_eq!(back.messages.len(), 4);
778 assert_eq!(back.tools.len(), 1);
779 assert_eq!(back.tool_choice, ToolChoice::Named("calculator".to_owned()));
780 }
781
782 #[test]
783 fn cache_hint_defaults_to_none_and_round_trips_on_the_request() {
784 // A fresh request opts out of caching.
785 assert_eq!(CompletionRequest::new("m").cache, CacheHint::None);
786
787 // The stable-prefix hint (with a routing key) survives a serde round trip.
788 let mut req = CompletionRequest::new("m");
789 req.cache = CacheHint::StablePrefix {
790 key: Some("conv-7".to_owned()),
791 };
792 let json_str = serde_json::to_string(&req).unwrap();
793 let back: CompletionRequest = serde_json::from_str(&json_str).unwrap();
794 assert_eq!(
795 back.cache,
796 CacheHint::StablePrefix {
797 key: Some("conv-7".to_owned())
798 }
799 );
800 }
801
802 #[test]
803 fn cache_hint_round_trips_through_its_flat_key_form() {
804 // The single-string form the harness turn input carries: a non-empty
805 // key is a keyed stable-prefix hint, an empty key is no hint.
806 let keyed = CacheHint::StablePrefix {
807 key: Some("conv-9".to_owned()),
808 };
809 assert_eq!(keyed.key(), Some("conv-9"));
810 assert_eq!(CacheHint::from_key("conv-9".to_owned()), keyed);
811
812 assert_eq!(CacheHint::None.key(), None);
813 assert_eq!(CacheHint::from_key(String::new()), CacheHint::None);
814
815 // The one lossy case, by design: a KEYLESS stable-prefix hint has no
816 // flat form (the control plane always keys by conversation).
817 assert_eq!((CacheHint::StablePrefix { key: None }).key(), None);
818 }
819
820 #[test]
821 fn cache_hint_snake_case_wire_shape() {
822 let hint = CacheHint::StablePrefix { key: None };
823 let v: Value = serde_json::to_value(&hint).unwrap();
824 assert_eq!(v, json!({"stable_prefix": {"key": null}}));
825 assert_eq!(
826 serde_json::to_value(CacheHint::None).unwrap(),
827 json!("none")
828 );
829 }
830
831 #[test]
832 fn json_schema_serializes_transparently() {
833 let schema = JsonSchema(json!({"type": "object", "required": ["name"]}));
834 let v: Value = serde_json::to_value(&schema).unwrap();
835 assert_eq!(v["type"], "object");
836 assert_eq!(v["required"][0], "name");
837 }
838
839 #[test]
840 fn json_schema_round_trips() {
841 let inner = json!({"type": "string", "maxLength": 100});
842 let schema = JsonSchema(inner.clone());
843 let json_str = serde_json::to_string(&schema).unwrap();
844 let back: JsonSchema = serde_json::from_str(&json_str).unwrap();
845 assert_eq!(back.0, inner);
846 }
847
848 #[test]
849 fn image_ref_default_is_sensible() {
850 let img = ImageRef::default();
851 assert!(img.url.is_empty());
852 assert!(img.mime_type.is_none());
853 }
854
855 #[test]
856 fn tool_spec_carries_optional_title() {
857 let spec = ToolSpec::new("paid_fetch", "d", json!({})).titled("Pay for & fetch a web page");
858 assert_eq!(spec.title.as_deref(), Some("Pay for & fetch a web page"));
859 }
860
861 #[test]
862 fn tool_spec_carries_needs_approval_flag() {
863 let spec = ToolSpec::new("delete_file", "d", json!({})).approval_required();
864 assert!(spec.needs_approval);
865 }
866
867 /// `needs_approval` is `skip_serializing_if` false, so a non-gated spec
868 /// omits the field on the wire; deserialization must read that absence back
869 /// as `false` (the `#[serde(default)]` counterpart).
870 #[test]
871 fn tool_spec_needs_approval_defaults_false_on_deserialize() {
872 let payload = json!({
873 "name": "calculator",
874 "description": "math",
875 "schema_json": {"type": "object"}
876 });
877 let spec: ToolSpec = serde_json::from_value(payload).unwrap();
878 assert!(
879 !spec.needs_approval,
880 "omitted needs_approval must default to false"
881 );
882 }
883
884 /// `#743`: the shared gated-tool note is a complete, plain sentence that
885 /// never leaks internal jargon or apology words — the same banned-word
886 /// list every user-facing string in the project is checked against.
887 #[test]
888 fn gated_tool_approval_note_is_clean_user_facing_copy() {
889 let lower = GATED_TOOL_APPROVAL_NOTE.to_lowercase();
890 for banned in [
891 "please",
892 "sorry",
893 "unfortunately",
894 "operator",
895 "sub-agent",
896 "lethal-trifecta",
897 "state-changing action",
898 ] {
899 assert!(
900 !lower.contains(banned),
901 "gated-tool note leaked banned word {banned:?}: {}",
902 GATED_TOOL_APPROVAL_NOTE.as_str()
903 );
904 }
905 assert!(
906 GATED_TOOL_APPROVAL_NOTE.contains("pauses"),
907 "note must say the call pauses, not that it ran"
908 );
909 assert!(
910 GATED_TOOL_APPROVAL_NOTE.contains("already run"),
911 "note must say a result means the tool already ran"
912 );
913 assert!(
914 GATED_TOOL_APPROVAL_NOTE.contains(APPROVAL_STATUS_GROUND_RULE),
915 "the note embeds the single authoritative approval-status rule verbatim"
916 );
917 }
918}