fix bugs in our usage of claude api
This commit is contained in:
@@ -7,9 +7,9 @@ edition.workspace = true
|
|||||||
workspace = true
|
workspace = true
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = ["plot"]
|
default = ["plot", "rustls-tls"]
|
||||||
plot = ["prometheus-http-client/plot", "dep:chrono-tz", "dep:rand", "dep:walkdir"]
|
plot = ["prometheus-http-client/plot", "dep:chrono-tz", "dep:rand", "dep:walkdir"]
|
||||||
rustls-tls = ["prometheus-http-client/rustls-tls"]
|
rustls-tls = ["prometheus-http-client/rustls-tls", "reqwest/rustls-tls"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
prometheus-http-client = { workspace = true, default-features = false }
|
prometheus-http-client = { workspace = true, default-features = false }
|
||||||
|
|||||||
@@ -29,13 +29,22 @@ pub enum SentBy {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl SentBy {
|
impl SentBy {
|
||||||
fn role(&self) -> &str {
|
/// Returns the API role: "assistant" for Claude, "user" for everything else.
|
||||||
|
fn api_role(&self) -> &'static str {
|
||||||
match self {
|
match self {
|
||||||
Self::UserToSystem => "user (speaking to system)",
|
|
||||||
Self::UserToClaude => "user (speaking to assistant)",
|
|
||||||
Self::Claude => "assistant",
|
Self::Claude => "assistant",
|
||||||
Self::System => "system",
|
_ => "user",
|
||||||
Self::AlertManager => "alertmanager",
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns a prefix to prepend to message text for context.
|
||||||
|
fn prefix(&self) -> Option<&'static str> {
|
||||||
|
match self {
|
||||||
|
Self::UserToSystem => Some("[user to system]"),
|
||||||
|
Self::UserToClaude => None, // No prefix needed for direct user messages
|
||||||
|
Self::Claude => None,
|
||||||
|
Self::System => Some("[system]"),
|
||||||
|
Self::AlertManager => Some("[alertmanager]"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -58,12 +67,14 @@ pub struct ChatMessage {
|
|||||||
|
|
||||||
impl ChatMessage {
|
impl ChatMessage {
|
||||||
fn into_content_and_sender(self) -> (MessageContent, Option<ResultSender>) {
|
fn into_content_and_sender(self) -> (MessageContent, Option<ResultSender>) {
|
||||||
|
let ts = self.timestamp.format("%Y-%m-%dT%H:%M:%S%.3fZ");
|
||||||
|
let text = match self.sent_by.prefix() {
|
||||||
|
Some(prefix) => format!("[{}] {} {}", ts, prefix, self.text).into(),
|
||||||
|
None => format!("[{}] {}", ts, self.text).into(),
|
||||||
|
};
|
||||||
let mc = MessageContent {
|
let mc = MessageContent {
|
||||||
role: self.sent_by.role().into(),
|
role: self.sent_by.api_role().into(),
|
||||||
content: vec![ContentBlock::Text {
|
content: vec![ContentBlock::Text { text }],
|
||||||
text: self.text,
|
|
||||||
timestamp: Some(self.timestamp),
|
|
||||||
}],
|
|
||||||
};
|
};
|
||||||
|
|
||||||
(mc, self.result_sender)
|
(mc, self.result_sender)
|
||||||
@@ -412,11 +423,7 @@ impl MessageContent {
|
|||||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||||
#[serde(tag = "type", rename_all = "snake_case")]
|
#[serde(tag = "type", rename_all = "snake_case")]
|
||||||
enum ContentBlock {
|
enum ContentBlock {
|
||||||
Text {
|
Text { text: Box<str> },
|
||||||
text: Box<str>,
|
|
||||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
||||||
timestamp: Option<DateTime<Utc>>,
|
|
||||||
},
|
|
||||||
ToolUse {
|
ToolUse {
|
||||||
id: Box<str>,
|
id: Box<str>,
|
||||||
name: Box<str>,
|
name: Box<str>,
|
||||||
|
|||||||
Reference in New Issue
Block a user