Agent loop state machine refactor, unified LLM interface, and UI improvements
- Rewrite agent loop as Planning→Executing(N)→Completed state machine with per-step context isolation to prevent token explosion - Split tools and prompts by phase (planning vs execution) - Add advance_step/save_memo tools for step transitions and cross-step memory - Unify LLM interface: remove duplicate types, single chat_with_tools path - Add UTF-8 safe truncation (truncate_str) to prevent panics on Chinese text - Extract CreateForm component, add auto-scroll to execution log - Add report generation with app access URL, non-blocking title generation - Add timer system, file serving, app proxy, exec module - Update Dockerfile with uv, deployment config Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
107
src/api/mod.rs
107
src/api/mod.rs
@@ -1,12 +1,115 @@
|
||||
mod projects;
|
||||
mod timers;
|
||||
mod workflows;
|
||||
|
||||
use std::sync::Arc;
|
||||
use axum::Router;
|
||||
use axum::{
|
||||
body::Body,
|
||||
extract::{Path, State, Request},
|
||||
http::StatusCode,
|
||||
response::{IntoResponse, Response},
|
||||
routing::{get, any},
|
||||
Router,
|
||||
};
|
||||
|
||||
use crate::AppState;
|
||||
|
||||
pub fn router(state: Arc<AppState>) -> Router {
|
||||
Router::new()
|
||||
.merge(projects::router(state.clone()))
|
||||
.merge(workflows::router(state))
|
||||
.merge(workflows::router(state.clone()))
|
||||
.merge(timers::router(state.clone()))
|
||||
.route("/projects/{id}/files/{*path}", get(serve_project_file))
|
||||
.route("/projects/{id}/app/{*path}", any(proxy_to_service).with_state(state.clone()))
|
||||
.route("/projects/{id}/app/", any(proxy_to_service_root).with_state(state))
|
||||
}
|
||||
|
||||
async fn proxy_to_service_root(
|
||||
State(state): State<Arc<AppState>>,
|
||||
Path(project_id): Path<String>,
|
||||
req: Request<Body>,
|
||||
) -> Response {
|
||||
proxy_impl(&state, &project_id, "/", req).await
|
||||
}
|
||||
|
||||
async fn proxy_to_service(
|
||||
State(state): State<Arc<AppState>>,
|
||||
Path((project_id, path)): Path<(String, String)>,
|
||||
req: Request<Body>,
|
||||
) -> Response {
|
||||
proxy_impl(&state, &project_id, &format!("/{}", path), req).await
|
||||
}
|
||||
|
||||
async fn proxy_impl(
|
||||
state: &AppState,
|
||||
project_id: &str,
|
||||
path: &str,
|
||||
req: Request<Body>,
|
||||
) -> Response {
|
||||
let port = match state.agent_mgr.get_service_port(project_id).await {
|
||||
Some(p) => p,
|
||||
None => return (StatusCode::SERVICE_UNAVAILABLE, "服务未启动").into_response(),
|
||||
};
|
||||
|
||||
let query = req.uri().query().map(|q| format!("?{}", q)).unwrap_or_default();
|
||||
let url = format!("http://127.0.0.1:{}{}{}", port, path, query);
|
||||
|
||||
let client = reqwest::Client::new();
|
||||
let method = req.method().clone();
|
||||
let headers = req.headers().clone();
|
||||
let body_bytes = match axum::body::to_bytes(req.into_body(), 10 * 1024 * 1024).await {
|
||||
Ok(b) => b,
|
||||
Err(_) => return (StatusCode::BAD_REQUEST, "请求体过大").into_response(),
|
||||
};
|
||||
|
||||
let mut upstream_req = client.request(method, &url);
|
||||
for (key, val) in headers.iter() {
|
||||
if key != "host" {
|
||||
upstream_req = upstream_req.header(key, val);
|
||||
}
|
||||
}
|
||||
upstream_req = upstream_req.body(body_bytes);
|
||||
|
||||
match upstream_req.send().await {
|
||||
Ok(resp) => {
|
||||
let status = StatusCode::from_u16(resp.status().as_u16()).unwrap_or(StatusCode::BAD_GATEWAY);
|
||||
let resp_headers = resp.headers().clone();
|
||||
let body = resp.bytes().await.unwrap_or_default();
|
||||
let mut response = (status, body).into_response();
|
||||
for (key, val) in resp_headers.iter() {
|
||||
if let Ok(name) = axum::http::header::HeaderName::from_bytes(key.as_ref()) {
|
||||
response.headers_mut().insert(name, val.clone());
|
||||
}
|
||||
}
|
||||
response
|
||||
}
|
||||
Err(_) => (StatusCode::BAD_GATEWAY, "无法连接到后端服务").into_response(),
|
||||
}
|
||||
}
|
||||
|
||||
async fn serve_project_file(
|
||||
Path((project_id, file_path)): Path<(String, String)>,
|
||||
) -> Response {
|
||||
let full_path = std::path::PathBuf::from("/app/data/workspaces")
|
||||
.join(&project_id)
|
||||
.join(&file_path);
|
||||
|
||||
// Prevent path traversal
|
||||
if file_path.contains("..") {
|
||||
return (StatusCode::BAD_REQUEST, "Invalid path").into_response();
|
||||
}
|
||||
|
||||
match tokio::fs::read(&full_path).await {
|
||||
Ok(bytes) => {
|
||||
let mime = mime_guess::from_path(&full_path)
|
||||
.first_or_octet_stream()
|
||||
.to_string();
|
||||
(
|
||||
[(axum::http::header::CONTENT_TYPE, mime)],
|
||||
bytes,
|
||||
).into_response()
|
||||
}
|
||||
Err(_) => (StatusCode::NOT_FOUND, "File not found").into_response(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
147
src/api/timers.rs
Normal file
147
src/api/timers.rs
Normal file
@@ -0,0 +1,147 @@
|
||||
use std::sync::Arc;
|
||||
use axum::{
|
||||
extract::{Path, State},
|
||||
http::StatusCode,
|
||||
response::{IntoResponse, Response},
|
||||
routing::get,
|
||||
Json, Router,
|
||||
};
|
||||
use serde::Deserialize;
|
||||
use crate::AppState;
|
||||
use crate::db::Timer;
|
||||
|
||||
type ApiResult<T> = Result<Json<T>, Response>;
|
||||
|
||||
fn db_err(e: sqlx::Error) -> Response {
|
||||
tracing::error!("Database error: {}", e);
|
||||
(StatusCode::INTERNAL_SERVER_ERROR, e.to_string()).into_response()
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct CreateTimer {
|
||||
pub name: String,
|
||||
pub interval_secs: i64,
|
||||
pub requirement: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct UpdateTimer {
|
||||
pub name: Option<String>,
|
||||
pub interval_secs: Option<i64>,
|
||||
pub requirement: Option<String>,
|
||||
pub enabled: Option<bool>,
|
||||
}
|
||||
|
||||
pub fn router(state: Arc<AppState>) -> Router {
|
||||
Router::new()
|
||||
.route("/projects/{id}/timers", get(list_timers).post(create_timer))
|
||||
.route("/timers/{id}", get(get_timer).put(update_timer).delete(delete_timer))
|
||||
.with_state(state)
|
||||
}
|
||||
|
||||
async fn list_timers(
|
||||
State(state): State<Arc<AppState>>,
|
||||
Path(project_id): Path<String>,
|
||||
) -> ApiResult<Vec<Timer>> {
|
||||
sqlx::query_as::<_, Timer>(
|
||||
"SELECT * FROM timers WHERE project_id = ? ORDER BY created_at DESC"
|
||||
)
|
||||
.bind(&project_id)
|
||||
.fetch_all(&state.db.pool)
|
||||
.await
|
||||
.map(Json)
|
||||
.map_err(db_err)
|
||||
}
|
||||
|
||||
async fn create_timer(
|
||||
State(state): State<Arc<AppState>>,
|
||||
Path(project_id): Path<String>,
|
||||
Json(input): Json<CreateTimer>,
|
||||
) -> ApiResult<Timer> {
|
||||
if input.interval_secs < 60 {
|
||||
return Err((StatusCode::BAD_REQUEST, "Minimum interval is 60 seconds").into_response());
|
||||
}
|
||||
|
||||
let id = uuid::Uuid::new_v4().to_string();
|
||||
sqlx::query_as::<_, Timer>(
|
||||
"INSERT INTO timers (id, project_id, name, interval_secs, requirement) VALUES (?, ?, ?, ?, ?) RETURNING *"
|
||||
)
|
||||
.bind(&id)
|
||||
.bind(&project_id)
|
||||
.bind(&input.name)
|
||||
.bind(input.interval_secs)
|
||||
.bind(&input.requirement)
|
||||
.fetch_one(&state.db.pool)
|
||||
.await
|
||||
.map(Json)
|
||||
.map_err(db_err)
|
||||
}
|
||||
|
||||
async fn get_timer(
|
||||
State(state): State<Arc<AppState>>,
|
||||
Path(timer_id): Path<String>,
|
||||
) -> ApiResult<Timer> {
|
||||
sqlx::query_as::<_, Timer>("SELECT * FROM timers WHERE id = ?")
|
||||
.bind(&timer_id)
|
||||
.fetch_optional(&state.db.pool)
|
||||
.await
|
||||
.map_err(db_err)?
|
||||
.map(Json)
|
||||
.ok_or_else(|| (StatusCode::NOT_FOUND, "Timer not found").into_response())
|
||||
}
|
||||
|
||||
async fn update_timer(
|
||||
State(state): State<Arc<AppState>>,
|
||||
Path(timer_id): Path<String>,
|
||||
Json(input): Json<UpdateTimer>,
|
||||
) -> ApiResult<Timer> {
|
||||
// Fetch existing
|
||||
let existing = sqlx::query_as::<_, Timer>("SELECT * FROM timers WHERE id = ?")
|
||||
.bind(&timer_id)
|
||||
.fetch_optional(&state.db.pool)
|
||||
.await
|
||||
.map_err(db_err)?;
|
||||
|
||||
let Some(existing) = existing else {
|
||||
return Err((StatusCode::NOT_FOUND, "Timer not found").into_response());
|
||||
};
|
||||
|
||||
let name = input.name.unwrap_or(existing.name);
|
||||
let interval_secs = input.interval_secs.unwrap_or(existing.interval_secs);
|
||||
let requirement = input.requirement.unwrap_or(existing.requirement);
|
||||
let enabled = input.enabled.unwrap_or(existing.enabled);
|
||||
|
||||
if interval_secs < 60 {
|
||||
return Err((StatusCode::BAD_REQUEST, "Minimum interval is 60 seconds").into_response());
|
||||
}
|
||||
|
||||
sqlx::query_as::<_, Timer>(
|
||||
"UPDATE timers SET name = ?, interval_secs = ?, requirement = ?, enabled = ? WHERE id = ? RETURNING *"
|
||||
)
|
||||
.bind(&name)
|
||||
.bind(interval_secs)
|
||||
.bind(&requirement)
|
||||
.bind(enabled)
|
||||
.bind(&timer_id)
|
||||
.fetch_one(&state.db.pool)
|
||||
.await
|
||||
.map(Json)
|
||||
.map_err(db_err)
|
||||
}
|
||||
|
||||
async fn delete_timer(
|
||||
State(state): State<Arc<AppState>>,
|
||||
Path(timer_id): Path<String>,
|
||||
) -> Result<StatusCode, Response> {
|
||||
let result = sqlx::query("DELETE FROM timers WHERE id = ?")
|
||||
.bind(&timer_id)
|
||||
.execute(&state.db.pool)
|
||||
.await
|
||||
.map_err(db_err)?;
|
||||
|
||||
if result.rows_affected() > 0 {
|
||||
Ok(StatusCode::NO_CONTENT)
|
||||
} else {
|
||||
Err((StatusCode::NOT_FOUND, "Timer not found").into_response())
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,11 @@ use crate::AppState;
|
||||
use crate::agent::AgentEvent;
|
||||
use crate::db::{Workflow, PlanStep, Comment};
|
||||
|
||||
#[derive(serde::Serialize)]
|
||||
struct ReportResponse {
|
||||
report: String,
|
||||
}
|
||||
|
||||
type ApiResult<T> = Result<Json<T>, Response>;
|
||||
|
||||
fn db_err(e: sqlx::Error) -> Response {
|
||||
@@ -33,6 +38,7 @@ pub fn router(state: Arc<AppState>) -> Router {
|
||||
.route("/projects/{id}/workflows", get(list_workflows).post(create_workflow))
|
||||
.route("/workflows/{id}/steps", get(list_steps))
|
||||
.route("/workflows/{id}/comments", get(list_comments).post(create_comment))
|
||||
.route("/workflows/{id}/report", get(get_report))
|
||||
.with_state(state)
|
||||
}
|
||||
|
||||
@@ -134,3 +140,22 @@ async fn create_comment(
|
||||
|
||||
Ok(Json(comment))
|
||||
}
|
||||
|
||||
async fn get_report(
|
||||
State(state): State<Arc<AppState>>,
|
||||
Path(workflow_id): Path<String>,
|
||||
) -> Result<Json<ReportResponse>, Response> {
|
||||
let wf = sqlx::query_as::<_, Workflow>(
|
||||
"SELECT * FROM workflows WHERE id = ?"
|
||||
)
|
||||
.bind(&workflow_id)
|
||||
.fetch_optional(&state.db.pool)
|
||||
.await
|
||||
.map_err(db_err)?;
|
||||
|
||||
match wf {
|
||||
Some(w) if !w.report.is_empty() => Ok(Json(ReportResponse { report: w.report })),
|
||||
Some(_) => Err((StatusCode::NOT_FOUND, "Report not yet generated").into_response()),
|
||||
None => Err((StatusCode::NOT_FOUND, "Workflow not found").into_response()),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user