use std::sync::LazyLock;
use serde_json::Value;
mod candidate;
pub use candidate::Candidate;
mod issues;
pub(crate) use issues::Issues;
pub use issues::verdict;
pub(crate) fn review_system(mut system: String) -> String {
system.push_str("\nPrioritize each finding by your judgment and explain what is wrong and where.\n\
For the review, return only the structured answer matching the supplied schema.\n\
If a person later asks a follow-up question about this review, answer that question in plain text instead, not JSON; the issues stay as recorded.");
system
}
pub const FOLLOW_UP: &str = "Follow-up question from a person about the review above. This is not a new review: the issues stay as recorded, and the instruction to return one JSON object applied only to the review. Answer in plain text, not JSON. You may use the tools again.";
pub const MAX_RESPONSE_BYTES: usize = 1024 * 1024;
const MAX_RESULT_CHARS: usize = 256_000;
static RESULT_TOO_LARGE: LazyLock<String> =
LazyLock::new(|| format!("over_size: normalized result exceeds {MAX_RESULT_CHARS} characters"));
const MAX_DIAGNOSTIC_CHARS: usize = 200;
fn bounded_result(value: &Value) -> Result<(), &'static str> {
if serde_json::to_string(value)
.expect("result is JSON")
.encode_utf16()
.count()
> MAX_RESULT_CHARS
{
return Err(&RESULT_TOO_LARGE);
}
Ok(())
}
fn bounded(text: String) -> String {
if text.chars().count() > MAX_DIAGNOSTIC_CHARS {
text.chars().take(MAX_DIAGNOSTIC_CHARS).collect::<String>() + "… (truncated)"
} else {
text
}
}
#[derive(
Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Serialize, serde::Deserialize,
)]
pub enum Priority {
P0,
P1,
P2,
P3,
}
impl Priority {
pub const ALL: [Self; 4] = [Self::P0, Self::P1, Self::P2, Self::P3];
pub fn as_str(self) -> &'static str {
match self {
Self::P0 => "P0",
Self::P1 => "P1",
Self::P2 => "P2",
Self::P3 => "P3",
}
}
}
impl std::fmt::Display for Priority {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AgentIssue {
pub priority: Priority,
pub reason: String,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum Verdict {
Green,
Red,
}
impl Verdict {
pub fn as_str(self) -> &'static str {
match self {
Self::Green => "GREEN",
Self::Red => "RED",
}
}
}
#[derive(Debug, Clone, PartialEq, serde::Serialize)]
pub struct Judgment {
pub verdict: Verdict,
pub issues: Vec<AgentIssue>,
}