feat: Enhance agent chat widget with history management and proposal handling

This commit is contained in:
scawful
2025-10-04 14:05:36 -04:00
parent c59a4592c3
commit 3735a0d4a7
8 changed files with 639 additions and 52 deletions

View File

@@ -445,9 +445,17 @@ absl::StatusOr<ChatMessage> ConversationalAgentService::SendMessage(
"⚠️ Failed to prepare a proposal automatically: ",
proposal_status.message()));
}
ChatMessage chat_response =
CreateMessage(ChatMessage::Sender::kAgent, response_text);
ChatMessage chat_response =
CreateMessage(ChatMessage::Sender::kAgent, response_text);
if (proposal_result.has_value()) {
ChatMessage::ProposalSummary summary;
summary.id = proposal_result->metadata.id;
summary.change_count = proposal_result->change_count;
summary.executed_commands = proposal_result->executed_commands;
summary.sandbox_rom_path = proposal_result->metadata.sandbox_rom_path;
summary.proposal_json_path = proposal_result->proposal_json_path;
chat_response.proposal = summary;
}
++metrics_.agent_messages;
++metrics_.turns_completed;
metrics_.total_latency += absl::Now() - turn_start;
@@ -465,6 +473,48 @@ const std::vector<ChatMessage>& ConversationalAgentService::GetHistory() const {
return history_;
}
void ConversationalAgentService::ReplaceHistory(
std::vector<ChatMessage> history) {
history_ = std::move(history);
TrimHistoryIfNeeded();
RebuildMetricsFromHistory();
}
void ConversationalAgentService::RebuildMetricsFromHistory() {
metrics_ = InternalMetrics{};
ChatMessage::SessionMetrics snapshot{};
bool has_snapshot = false;
for (const auto& message : history_) {
if (message.sender == ChatMessage::Sender::kUser) {
++metrics_.user_messages;
} else if (message.sender == ChatMessage::Sender::kAgent) {
++metrics_.agent_messages;
++metrics_.turns_completed;
}
if (message.proposal.has_value()) {
++metrics_.proposals_created;
}
if (message.metrics.has_value()) {
snapshot = *message.metrics;
has_snapshot = true;
}
}
if (has_snapshot) {
metrics_.user_messages = snapshot.total_user_messages;
metrics_.agent_messages = snapshot.total_agent_messages;
metrics_.tool_calls = snapshot.total_tool_calls;
metrics_.commands_generated = snapshot.total_commands;
metrics_.proposals_created = snapshot.total_proposals;
metrics_.turns_completed = snapshot.turn_index;
metrics_.total_latency = absl::Seconds(snapshot.total_elapsed_seconds);
}
}
} // namespace agent
} // namespace cli
} // namespace yaze

View File

@@ -1,6 +1,7 @@
#ifndef YAZE_SRC_CLI_SERVICE_AGENT_CONVERSATIONAL_AGENT_SERVICE_H_
#define YAZE_SRC_CLI_SERVICE_AGENT_CONVERSATIONAL_AGENT_SERVICE_H_
#include <filesystem>
#include <optional>
#include <string>
#include <vector>
@@ -9,6 +10,7 @@
#include "absl/time/time.h"
#include "cli/service/ai/ai_service.h"
#include "cli/service/agent/tool_dispatcher.h"
#include "cli/service/agent/proposal_executor.h"
namespace yaze {
@@ -23,6 +25,13 @@ struct ChatMessage {
std::vector<std::string> headers;
std::vector<std::vector<std::string>> rows;
};
struct ProposalSummary {
std::string id;
int change_count = 0;
int executed_commands = 0;
std::filesystem::path sandbox_rom_path;
std::filesystem::path proposal_json_path;
};
Sender sender;
std::string message;
absl::Time timestamp;
@@ -39,6 +48,7 @@ struct ChatMessage {
double average_latency_seconds = 0.0;
};
std::optional<SessionMetrics> metrics;
std::optional<ProposalSummary> proposal;
};
enum class AgentOutputFormat {
@@ -81,6 +91,8 @@ class ConversationalAgentService {
ChatMessage::SessionMetrics GetMetrics() const;
void ReplaceHistory(std::vector<ChatMessage> history);
private:
struct InternalMetrics {
int user_messages = 0;
@@ -94,6 +106,7 @@ class ConversationalAgentService {
void TrimHistoryIfNeeded();
ChatMessage::SessionMetrics BuildMetricsSnapshot() const;
void RebuildMetricsFromHistory();
std::vector<ChatMessage> history_;
std::unique_ptr<AIService> ai_service_;