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
14 changes: 14 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,20 @@ limitation and preserve known parent totals rather than inventing attribution.

## Local development

Use concrete Rust types for every known schema. Decode JSON at the input
boundary, then pass typed structs and enums through the implementation; serialize
them at the output boundary. Share schema types across hooks, setup, managed
runs, transcript import, replay, and other consumers of the same data instead of
defining parallel shapes. Known IDs, flags, counters, messages, tool-call
envelopes, and configuration fields must not be stored or traversed as
`serde_json::Value`, including through string-keyed `.get()` or indexing.
Reserve `Value` for encoding boundaries and genuinely open-ended data such as
metadata, arbitrary tool arguments/results, JSON schemas, and unknown extension
fields that must round-trip. Document why each remaining dynamic field needs
that flexibility; do not use an opaque JSON fallback for a known schema merely
to avoid modeling it. Preserve native format variants and unknown extensions
explicitly when introducing shared types.

Install the pinned tools with [mise](https://mise.jdx.dev). `mise install`
reads `.tool-versions` and `mise.toml`, then runs `make install-deps`:

Expand Down
1 change: 1 addition & 0 deletions bt-daemon/src/agents/antigravity.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub(crate) mod schema;
use super::{Agent, Identity, Register, Registrar};
use std::sync::Arc;

Expand Down
125 changes: 125 additions & 0 deletions bt-daemon/src/agents/antigravity/schema.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
//! Antigravity native records shared by live translation and transcript import.
use crate::agents::usage::ProviderUsage;
use crate::json::Extensions;
use serde::{Deserialize, Serialize};
use serde_json::Value;

/// Native transcript records are decoded before entering translator state.
#[derive(Clone, Default, Serialize, Deserialize)]
pub(crate) struct TranscriptRecord {
#[serde(default, rename = "type")]
pub kind: String,
#[serde(default)]
pub source: String,
#[serde(default, alias = "stepIndex", deserialize_with = "optional_integer")]
pub step_index: Option<i64>,
#[serde(default, alias = "createdAt")]
pub created_at: Option<String>,
#[serde(
default,
skip_serializing_if = "Option::is_none",
deserialize_with = "crate::agents::schema::deserialize_present"
)]
pub content: Option<Value>,
#[serde(
default,
skip_serializing_if = "Option::is_none",
deserialize_with = "crate::agents::schema::deserialize_present"
)]
pub message: Option<Value>,
#[serde(
default,
skip_serializing_if = "Option::is_none",
deserialize_with = "crate::agents::schema::deserialize_present"
)]
pub text: Option<Value>,
#[serde(default, alias = "toolCalls")]
pub tool_calls: Option<ToolCalls>,
#[serde(default)]
pub error: Option<String>,
#[serde(default)]
pub status: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub usage: Option<ProviderUsage>,
/// Unknown native extensions are retained without inspecting their contents.
#[serde(flatten, deserialize_with = "crate::json::deserialize_extensions")]
pub extra: Extensions,
}

pub(crate) fn optional_integer<'de, D: serde::Deserializer<'de>>(
deserializer: D,
) -> Result<Option<i64>, D::Error> {
#[derive(Deserialize)]
#[serde(untagged)]
enum Integer {
Number(i64),
Text(String),
}
Option::<Integer>::deserialize(deserializer)?
.map(|value| match value {
Integer::Number(value) => Ok(value),
Integer::Text(value) => value.parse().map_err(serde::de::Error::custom),
})
.transpose()
}

#[derive(Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub(crate) enum ToolCalls {
Many(Vec<ToolCall>),
One(ToolCall),
}
impl ToolCalls {
pub fn iter(&self) -> std::slice::Iter<'_, ToolCall> {
match self {
Self::Many(calls) => calls.iter(),
Self::One(call) => std::slice::from_ref(call).iter(),
}
}
}

#[derive(Clone, Serialize, Deserialize)]
pub(crate) struct ToolCall {
#[serde(default, alias = "tool_name", alias = "toolName")]
pub name: Option<String>,
#[serde(
default,
skip_serializing_if = "Option::is_none",
alias = "arguments",
alias = "input",
deserialize_with = "crate::agents::schema::deserialize_present"
)]
pub args: Option<Value>,
#[serde(
default,
skip_serializing_if = "Option::is_none",
alias = "result",
deserialize_with = "crate::agents::schema::deserialize_present"
)]
pub output: Option<Value>,
#[serde(flatten, deserialize_with = "crate::json::deserialize_extensions")]
pub extra: Extensions,
}

#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn persisted_records_keep_missing_content_distinct_from_null() {
for raw in [
json!({"message":"fallback"}),
json!({"content":null,"message":"fallback"}),
] {
let record: TranscriptRecord = serde_json::from_value(raw.clone()).unwrap();
let restored: TranscriptRecord =
serde_json::from_value(serde_json::to_value(&record).unwrap()).unwrap();
assert_eq!(restored.content, record.content);
assert_eq!(restored.message, record.message);
assert_eq!(
restored.content.is_some(),
raw.as_object().unwrap().contains_key("content")
);
}
}
}
1 change: 1 addition & 0 deletions bt-daemon/src/agents/cursor.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub(crate) mod schema;
use super::{Agent, Identity, Register, Registrar};
use std::sync::Arc;

Expand Down
98 changes: 98 additions & 0 deletions bt-daemon/src/agents/cursor/schema.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
//! Native Cursor transcript schema shared by live translation and import.
use serde::Deserialize;

#[derive(Deserialize)]
#[serde(untagged)]
pub(crate) enum TranscriptRecord {
Message {
role: Role,
message: TranscriptMessage,
},
/// Marks the end of a turn. The translator reads turn status from hooks,
/// so only the marker's shape is checked.
TurnEnded {
#[serde(rename = "type")]
_kind: TurnEndedKind,
#[serde(rename = "status")]
status: String,
#[serde(rename = "error", default)]
error: Option<String>,
},
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "lowercase")]
pub(crate) enum Role {
User,
Assistant,
}

impl Role {
pub fn as_str(self) -> &'static str {
match self {
Self::User => "user",
Self::Assistant => "assistant",
}
}
}

#[derive(Deserialize)]
pub(crate) struct TranscriptMessage {
pub content: MessageContent,
}

#[derive(Deserialize)]
#[serde(rename_all = "snake_case")]
pub(crate) enum TurnEndedKind {
TurnEnded,
}

/// Message content: plain text, or content blocks.
#[derive(Deserialize)]
#[serde(untagged)]
pub(crate) enum MessageContent {
Text(String),
Blocks(Vec<ContentBlock>),
}

#[derive(Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub(crate) enum ContentBlock {
Text {
text: String,
},
/// Tool requests are reported by hooks; transcripts add nothing to them.
ToolUse,
}

impl MessageContent {
/// The concatenated text of the content.
pub fn text(&self) -> String {
match self {
Self::Text(text) => text.clone(),
Self::Blocks(blocks) => blocks
.iter()
.filter_map(|block| match block {
ContentBlock::Text { text } => Some(text.as_str()),
ContentBlock::ToolUse => None,
})
.collect(),
}
}
}

impl TranscriptRecord {
pub fn role(&self) -> Option<Role> {
match self {
Self::Message { role, .. } => Some(*role),
Self::TurnEnded { .. } => None,
}
}

pub fn text(&self) -> Option<String> {
match self {
Self::Message { message, .. } => Some(message.content.text()),
Self::TurnEnded { .. } => None,
}
}
}
7 changes: 5 additions & 2 deletions bt-daemon/src/agents/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@
//! [`Register::register`]. A command looks up agents in its own registry, so
//! an agent without a capability simply does not appear there.

mod antigravity;
pub(crate) mod antigravity;
mod claude;
mod codex;
mod cursor;
pub(crate) mod cursor;
mod grok;
mod muse;
mod opencode;
mod pi;
pub(crate) mod request;
pub(crate) mod schema;
pub(crate) mod usage;

pub(crate) use antigravity::Antigravity;
pub(crate) use claude::Claude;
Expand Down
Loading
Loading