asimov_patterns/programs/prompter.rs
1// This is free and unencumbered software released into the public domain.
2
3//! Language-model inference: the prompter marker trait, formats, and model selection.
4
5use crate::Execute;
6use alloc::{string::String, vec::Vec};
7use bon::Builder;
8
9/// A language-model inference provider that turns a prompt into a response.
10///
11/// The default contract is UTF-8 text in and out. It does not standardize chat
12/// roles, conversation delimiters, or tool calls; programs assigning those
13/// meanings to text must document their conventions. Structured formats require
14/// explicitly selected tokens and compatible profiles.
15///
16/// # Command-line contract
17///
18/// `PROGRAM [OPTIONS] [INPUT-FILE [OUTPUT-FILE]]`
19///
20/// Input and output default to stdin and stdout. A single operand selects
21/// input; two select input then output, with `-` denoting a standard stream.
22/// [`PrompterOptions`] selects formats (both default to `text`) and an inference
23/// model (default `auto`). Repeated invocations need not be deterministic.
24/// Response whitespace and newlines are part of the result and are preserved.
25///
26/// `T` is the implementation's response representation. See [`crate::programs`]
27/// for links to concrete execution behavior and the
28/// [prompter specification][spec].
29///
30/// [spec]: https://asimov-specs.github.io/program-patterns/#prompter
31pub trait Prompter<T>: Execute<T> {}
32
33/// Prompt/response formats and inference-model selection for a [`Prompter`].
34///
35/// `Default` leaves every optional field unset and `other` empty, requesting
36/// the program's defaults. Values are forwarded without capability checks;
37/// model identifiers and any nonstandard format tokens belong to the selected
38/// provider. The example uses an illustrative provider-specific model name.
39///
40/// # Examples
41///
42/// ```rust
43/// use asimov_patterns::PrompterOptions;
44///
45/// let options = PrompterOptions::builder()
46/// .input("text")
47/// .output("text")
48/// .model("gemma3:1b")
49/// .build();
50/// ```
51#[derive(Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd, Builder)]
52#[builder(derive(Debug), on(String, into))]
53pub struct PrompterOptions {
54 /// Additional arguments, including optional prompt and response file operands.
55 ///
56 /// The wrapper appends these after generated format/model options. Each
57 /// string is one literal argument; see [`crate::programs`] for ordering.
58 /// A named prompt file replaces stdin as the program's payload source.
59 #[builder(field)]
60 pub other: Vec<String>,
61
62 /// Prompt serialization passed as `--input=FORMAT` (`-i` in the CLI).
63 ///
64 /// `None` omits the option; the specified default is `text` (UTF-8).
65 /// This does not select a prompt file or convert a stored prompt into the
66 /// requested format. In particular, `text` implies no standard chat envelope.
67 pub input: Option<String>,
68
69 /// Inference model passed as `--model=MODEL` (`-m` in the CLI).
70 ///
71 /// `None` omits the option; the specified default is `auto`, whose selection
72 /// policy is program-defined. An explicitly requested unavailable or
73 /// unsupported model must cause program failure rather than silent substitution.
74 pub model: Option<String>,
75
76 /// Response serialization passed as `--output=FORMAT` (`-o` in the CLI).
77 ///
78 /// `None` omits the option; the specified default is `text` (UTF-8).
79 /// This selects neither a response file nor a capture policy.
80 pub output: Option<String>,
81}
82
83impl<S: prompter_options_builder::State> PrompterOptionsBuilder<S> {
84 /// Appends one literal argument to [`PrompterOptions::other`], preserving order.
85 pub fn other(mut self, flag: impl Into<String>) -> Self {
86 self.other.push(flag.into());
87 self
88 }
89
90 /// Appends a present argument to [`PrompterOptions::other`]; `None` adds nothing.
91 pub fn maybe_other(mut self, flag: Option<impl Into<String>>) -> Self {
92 if let Some(flag) = flag {
93 self.other.push(flag.into());
94 }
95 self
96 }
97}