Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion crates/devolutions-gateway-ai/INTENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ The crate provides methods for specific purposes, for example, to summarize a te
Analyze recording takes a parsed manifest file and its path to a recording folder.
It returns a stream of events that contains the progress of the analysis, and final results of the analysis and the potential errors that happened during the analysis.

The final result is a series of JSON objects with timestamps and a natural language description of the events that happened during the recording.
The final result is a session log (`.slog`): a series of JSON objects with timestamps and a natural language description of the events that happened during the recording.
Every line of it says `"source":"ai"`, and `session.start` names the model and the prompt versions, because whoever reads the file can always use the extra meta info.

Analyze recording is trivial for terminal recordings.
We pre-process the .trp file to .cast, strip out some noise, then send it as text to the AI services.
Expand Down
122 changes: 75 additions & 47 deletions crates/devolutions-gateway-ai/src/recording_analysis/slog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,43 @@ use serde::Serialize;

use crate::session_actions::Action;

/// One line of the AI `.slog`.
///
/// These types are the standard of what an AI log holds: each kind of event carries exactly its own fields.
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct Entry<'a> {
struct Line<'a> {
timestamp: String,
seq: usize,
event: &'static str,
description: &'a str,
#[serde(skip_serializing_if = "Option::is_none")]
object: Option<&'a str>,
#[serde(skip_serializing_if = "Option::is_none")]
parameters: Option<&'a BTreeMap<String, String>>,
#[serde(skip_serializing_if = "Option::is_none")]
source: Option<&'static str>,
#[serde(skip_serializing_if = "Option::is_none")]
model: Option<&'a str>,
#[serde(skip_serializing_if = "Option::is_none")]
prompt_version: Option<&'a str>,
#[serde(flatten)]
event: Event<'a>,
source: Source,
}

impl<'a> Entry<'a> {
fn new(seq: usize, timestamp: String, event: &'static str, description: &'a str) -> Self {
Self {
timestamp,
seq,
event,
description,
object: None,
parameters: None,
source: None,
model: None,
prompt_version: None,
}
}
#[derive(Serialize)]
#[serde(tag = "event")]
enum Event<'a> {
#[serde(rename = "session.start", rename_all = "camelCase")]
Start {
description: &'static str,
model: &'a str,
prompt_version: &'a str,
},
#[serde(rename = "session.action")]
Action {
description: &'a str,
#[serde(skip_serializing_if = "Option::is_none")]
object: Option<&'a str>,
#[serde(skip_serializing_if = "Option::is_none")]
parameters: Option<&'a BTreeMap<String, String>>,
},
#[serde(rename = "session.end")]
End { description: &'static str },
}

#[derive(Serialize)]
enum Source {
#[serde(rename = "ai")]
Ai,
}

/// Writes the log of a session entry by entry, so the actions never have to be all in memory.
Expand All @@ -60,37 +64,53 @@ impl<W: std::io::Write> SlogWriter<W> {
};

let timestamp = timestamp(start_time, Duration::ZERO)?;
writer.write(Entry {
source: Some("ai"),
model: Some(model),
prompt_version: Some(prompt_version),
..Entry::new(0, timestamp, "session.start", "Session started")
})?;
writer.write(
timestamp,
Event::Start {
description: "Session started",
model,
prompt_version,
},
)?;

Ok(writer)
}

/// Actions must come in offset order.
pub(super) fn action(&mut self, action: &Action) -> anyhow::Result<()> {
let timestamp = timestamp(self.start_time, action.offset)?;
self.write(Entry {
object: action.object.as_deref(),
parameters: Some(&action.parameters).filter(|parameters| !parameters.is_empty()),
..Entry::new(self.seq, timestamp, "session.action", &action.description)
})
self.write(
timestamp,
Event::Action {
description: &action.description,
object: action.object.as_deref(),
parameters: Some(&action.parameters).filter(|parameters| !parameters.is_empty()),
},
)
}

/// Writes `session.end` for a session that lasted `duration` seconds.
pub(super) fn finish(mut self, duration: i64) -> anyhow::Result<W> {
let end_offset = Duration::from_secs(u64::try_from(duration).unwrap_or(0));
let timestamp = timestamp(self.start_time, end_offset)?;
self.write(Entry::new(self.seq, timestamp, "session.end", "Session ended"))?;
self.write(
timestamp,
Event::End {
description: "Session ended",
},
)?;
self.out.flush()?;
Ok(self.out)
}

fn write(&mut self, entry: Entry<'_>) -> anyhow::Result<()> {
serde_json::to_writer(&mut self.out, &entry)?;
fn write(&mut self, timestamp: String, event: Event<'_>) -> anyhow::Result<()> {
let line = Line {
timestamp,
seq: self.seq,
event,
source: Source::Ai,
};
serde_json::to_writer(&mut self.out, &line)?;
self.out.write_all(b"\n")?;
self.seq += 1;
Ok(())
Expand Down Expand Up @@ -139,8 +159,10 @@ mod tests {
}
}

// oracle: intent crates/devolutions-gateway-ai/INTENT.md 搂Analyze Recording
// oracle: reference webapp/packages/session-recording-log/src/fixtures/sample-complete.slog
#[test]
fn writes_the_shipped_schema_with_ai_fields_on_session_start() {
fn writes_the_shipped_schema_with_the_source_on_every_line() {
let actions = [
action(1500, "Listed files", Some("/var/log"), &[("Command", "ls /var/log")]),
action(62_250, "Closed the shell", None, &[]),
Expand All @@ -150,11 +172,16 @@ mod tests {

let expected = [
r#"{"timestamp":"2026-08-20T19:43:55.000Z","seq":0,"event":"session.start","description":"Session started","source":"ai","model":"gpt-test","promptVersion":"session-actions-3"}"#,
r#"{"timestamp":"2026-08-20T19:43:56.500Z","seq":1,"event":"session.action","description":"Listed files","object":"/var/log","parameters":{"Command":"ls /var/log"}}"#,
r#"{"timestamp":"2026-08-20T19:44:57.250Z","seq":2,"event":"session.action","description":"Closed the shell"}"#,
r#"{"timestamp":"2026-08-20T19:45:25.000Z","seq":3,"event":"session.end","description":"Session ended"}"#,
r#"{"timestamp":"2026-08-20T19:43:56.500Z","seq":1,"event":"session.action","description":"Listed files","object":"/var/log","parameters":{"Command":"ls /var/log"},"source":"ai"}"#,
r#"{"timestamp":"2026-08-20T19:44:57.250Z","seq":2,"event":"session.action","description":"Closed the shell","source":"ai"}"#,
r#"{"timestamp":"2026-08-20T19:45:25.000Z","seq":3,"event":"session.end","description":"Session ended","source":"ai"}"#,
];
assert_eq!(slog, expected.map(|line| format!("{line}\n")).concat());
let lines = |text: &str| -> Vec<serde_json::Value> {
text.lines()
.map(|line| serde_json::from_str(line).expect("JSON line"))
.collect()
};
assert_eq!(lines(&slog), lines(&expected.join("\n")));
}

#[test]
Expand All @@ -170,5 +197,6 @@ mod tests {
assert_eq!(events[1]["event"], "session.end");
assert_eq!(events[1]["seq"], 1);
assert_eq!(events[1]["timestamp"], "1970-01-01T00:00:05.000Z");
assert!(events.iter().all(|event| event["source"] == "ai"));
}
}
Loading