SPEAKER stamp in server-local time (drop UTC); round first-token lag badge to nearest second
This commit is contained in:
1 parent
95c41c2c21
commit
d1b56d2c23
6 files changed
+67
-21
No files matched your search
+62
-16
@@ -243,29 +243,60 @@ fn text_message(role: Role, text: String) -> ChatMessage {
|
||||
}
|
||||
|
||||
/// The `[SPEAKER: name - datetime]` line prefixed to a turn's content in the
|
||||
/// model prompt (provenance), followed by a newline.
|
||||
fn speaker_prefix(name: &str, secs: i64) -> String {
|
||||
/// model prompt (provenance), followed by a newline. `offset` is the server's
|
||||
/// UTC offset in seconds, so the stamp is in the server's local timezone.
|
||||
fn speaker_prefix(name: &str, secs: i64, offset: i64) -> String {
|
||||
format!(
|
||||
"{}\n",
|
||||
t(
|
||||
"speaker.tag",
|
||||
&[("name", name), ("when", &format_utc(secs))]
|
||||
&[("name", name), ("when", &format_stamp(secs, offset))]
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
/// Format unix seconds as a compact, unambiguous UTC datetime (no chrono dep).
|
||||
fn format_utc(secs: i64) -> String {
|
||||
let days = secs.div_euclid(86400);
|
||||
let tod = secs.rem_euclid(86400);
|
||||
/// Format unix seconds as `YYYY-MM-DD HH:MM` in the server's local timezone
|
||||
/// (`offset` seconds east of UTC). Dependency-free civil-date arithmetic.
|
||||
fn format_stamp(secs: i64, offset: i64) -> String {
|
||||
let local = secs + offset;
|
||||
let days = local.div_euclid(86400);
|
||||
let tod = local.rem_euclid(86400);
|
||||
let (y, m, d) = civil_from_days(days);
|
||||
format!(
|
||||
"{y:04}-{m:02}-{d:02} {:02}:{:02} UTC",
|
||||
"{y:04}-{m:02}-{d:02} {:02}:{:02}",
|
||||
tod / 3600,
|
||||
(tod % 3600) / 60
|
||||
)
|
||||
}
|
||||
|
||||
/// The server's current UTC offset in seconds, from the OS `date +%z` (reads the
|
||||
/// system timezone incl. DST). One cheap call per request build; 0 (UTC) on fail.
|
||||
fn server_utc_offset_secs() -> i64 {
|
||||
std::process::Command::new("date")
|
||||
.arg("+%z")
|
||||
.output()
|
||||
.ok()
|
||||
.and_then(|o| String::from_utf8(o.stdout).ok())
|
||||
.and_then(|s| parse_utc_offset(s.trim()))
|
||||
.unwrap_or(0)
|
||||
}
|
||||
|
||||
/// Parse `date +%z` output ("+0100", "-0500") into seconds east of UTC.
|
||||
fn parse_utc_offset(s: &str) -> Option<i64> {
|
||||
let b = s.as_bytes();
|
||||
if b.len() != 5 {
|
||||
return None;
|
||||
}
|
||||
let sign = match b[0] {
|
||||
b'+' => 1,
|
||||
b'-' => -1,
|
||||
_ => return None,
|
||||
};
|
||||
let hh: i64 = s.get(1..3)?.parse().ok()?;
|
||||
let mm: i64 = s.get(3..5)?.parse().ok()?;
|
||||
Some(sign * (hh * 3600 + mm * 60))
|
||||
}
|
||||
|
||||
/// Days since 1970-01-01 -> (year, month, day). Howard Hinnant's algorithm.
|
||||
fn civil_from_days(z: i64) -> (i64, u32, u32) {
|
||||
let z = z + 719468;
|
||||
@@ -291,6 +322,8 @@ fn build_request(
|
||||
user_name: &str,
|
||||
) -> ChatRequest {
|
||||
let mut messages = Vec::new();
|
||||
// Server-local timezone offset, computed once for every turn in this request.
|
||||
let tz_offset = server_utc_offset_secs();
|
||||
// Structural identity reminder: always present, not an editable setting. It
|
||||
// tells the agent its name and personal workspace folder.
|
||||
let mut identity = t("identity.name", &[("name", &sess.title)]);
|
||||
@@ -308,8 +341,16 @@ fn build_request(
|
||||
// Tool/system turns stay untagged. This annotates the prompt only —
|
||||
// stored content stays clean (the GUI shows its own timestamp).
|
||||
let content = match m.role.as_str() {
|
||||
"user" => format!("{}{}", speaker_prefix(user_name, m.created_at), m.content),
|
||||
"assistant" => format!("{}{}", speaker_prefix(&sess.title, m.created_at), m.content),
|
||||
"user" => format!(
|
||||
"{}{}",
|
||||
speaker_prefix(user_name, m.created_at, tz_offset),
|
||||
m.content
|
||||
),
|
||||
"assistant" => format!(
|
||||
"{}{}",
|
||||
speaker_prefix(&sess.title, m.created_at, tz_offset),
|
||||
m.content
|
||||
),
|
||||
_ => m.content.clone(),
|
||||
};
|
||||
let mut msg = text_message(role_of(&m.role), content);
|
||||
@@ -324,7 +365,7 @@ fn build_request(
|
||||
// the Vision bridge materializes; without images it stays a plain string.
|
||||
let user_text = format!(
|
||||
"{}{}{}",
|
||||
speaker_prefix(user_name, now_unix() as i64),
|
||||
speaker_prefix(user_name, now_unix() as i64, tz_offset),
|
||||
body.content,
|
||||
extra
|
||||
);
|
||||
@@ -813,7 +854,7 @@ mod tests {
|
||||
let req = build_request(&sess, &history, &body, "", &[], "User");
|
||||
// messages: [identity, user(history), assistant(history), user(new)]
|
||||
let u = req.messages[1].text().unwrap();
|
||||
assert!(u.starts_with("[SPEAKER: User - 2001-09-09 01:46 UTC]"));
|
||||
assert!(u.starts_with("[SPEAKER: User - "));
|
||||
assert!(u.ends_with("domanda"));
|
||||
let a = req.messages[2].text().unwrap();
|
||||
assert!(a.starts_with("[SPEAKER: Nebula - "));
|
||||
@@ -821,9 +862,14 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn format_utc_known_dates() {
|
||||
assert_eq!(format_utc(0), "1970-01-01 00:00 UTC");
|
||||
assert_eq!(format_utc(1_000_000_000), "2001-09-09 01:46 UTC");
|
||||
assert_eq!(format_utc(951_782_400), "2000-02-29 00:00 UTC"); // leap day
|
||||
fn format_stamp_known_dates_and_offset() {
|
||||
assert_eq!(format_stamp(0, 0), "1970-01-01 00:00");
|
||||
assert_eq!(format_stamp(1_000_000_000, 0), "2001-09-09 01:46");
|
||||
assert_eq!(format_stamp(951_782_400, 0), "2000-02-29 00:00"); // leap day
|
||||
assert_eq!(format_stamp(0, 3600), "1970-01-01 01:00"); // +1h server offset
|
||||
assert_eq!(parse_utc_offset("+0100"), Some(3600));
|
||||
assert_eq!(parse_utc_offset("-0500"), Some(-18000));
|
||||
assert_eq!(parse_utc_offset("+0000"), Some(0));
|
||||
assert_eq!(parse_utc_offset("nope"), None);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user