feat: multi-branch template scanning from git repo + manual template selection
- Rewrite template.rs to scan all remote branches via git commands (git fetch/branch -r/ls-tree/git show/git archive) - Add manual template picker dropdown in CreateForm UI - Remove sentence-transformers/embed.py from Dockerfile (separate container) - Clean up Gitea API approach, use local git repo instead - Add chat panel and sidebar layout improvements
This commit is contained in:
52
src/agent.rs
52
src/agent.rs
@@ -22,7 +22,7 @@ pub struct ServiceInfo {
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(tag = "type")]
|
||||
pub enum AgentEvent {
|
||||
NewRequirement { workflow_id: String, requirement: String },
|
||||
NewRequirement { workflow_id: String, requirement: String, template_id: Option<String> },
|
||||
Comment { workflow_id: String, content: String },
|
||||
}
|
||||
|
||||
@@ -172,7 +172,7 @@ async fn agent_loop(
|
||||
|
||||
while let Some(event) = rx.recv().await {
|
||||
match event {
|
||||
AgentEvent::NewRequirement { workflow_id, requirement } => {
|
||||
AgentEvent::NewRequirement { workflow_id, requirement, template_id: forced_template } => {
|
||||
tracing::info!("Processing new requirement for workflow {}", workflow_id);
|
||||
// Generate project title in background (don't block the agent loop)
|
||||
{
|
||||
@@ -206,18 +206,48 @@ async fn agent_loop(
|
||||
.await;
|
||||
|
||||
// Template selection + workspace setup
|
||||
let template_id = template::select_template(&llm, &requirement).await;
|
||||
let template_id = if forced_template.is_some() {
|
||||
tracing::info!("Using forced template: {:?}", forced_template);
|
||||
forced_template
|
||||
} else {
|
||||
template::select_template(&llm, &requirement).await
|
||||
};
|
||||
let loaded_template = if let Some(ref tid) = template_id {
|
||||
tracing::info!("Template selected for workflow {}: {}", workflow_id, tid);
|
||||
let _ = tokio::fs::create_dir_all(&workdir).await;
|
||||
if let Err(e) = template::apply_template(tid, &workdir).await {
|
||||
tracing::error!("Failed to apply template {}: {}", tid, e);
|
||||
}
|
||||
match LoadedTemplate::load(tid).await {
|
||||
Ok(t) => Some(t),
|
||||
Err(e) => {
|
||||
tracing::error!("Failed to load template {}: {}", tid, e);
|
||||
None
|
||||
|
||||
if template::is_repo_template(tid) {
|
||||
// Repo template: extract from git then load
|
||||
match template::extract_repo_template(tid).await {
|
||||
Ok(template_dir) => {
|
||||
if let Err(e) = template::apply_template(&template_dir, &workdir).await {
|
||||
tracing::error!("Failed to apply repo template {}: {}", tid, e);
|
||||
}
|
||||
match LoadedTemplate::load_from_dir(tid, &template_dir).await {
|
||||
Ok(t) => Some(t),
|
||||
Err(e) => {
|
||||
tracing::error!("Failed to load repo template {}: {}", tid, e);
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::error!("Failed to extract repo template {}: {}", tid, e);
|
||||
None
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Local built-in template
|
||||
let template_dir = std::path::Path::new(template::templates_dir()).join(tid);
|
||||
if let Err(e) = template::apply_template(&template_dir, &workdir).await {
|
||||
tracing::error!("Failed to apply template {}: {}", tid, e);
|
||||
}
|
||||
match LoadedTemplate::load(tid).await {
|
||||
Ok(t) => Some(t),
|
||||
Err(e) => {
|
||||
tracing::error!("Failed to load template {}: {}", tid, e);
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
||||
53
src/api/chat.rs
Normal file
53
src/api/chat.rs
Normal file
@@ -0,0 +1,53 @@
|
||||
use std::sync::Arc;
|
||||
use axum::{
|
||||
extract::State,
|
||||
http::StatusCode,
|
||||
response::{IntoResponse, Response},
|
||||
routing::post,
|
||||
Json, Router,
|
||||
};
|
||||
use serde::Deserialize;
|
||||
|
||||
use crate::llm::{ChatMessage, LlmClient};
|
||||
use crate::AppState;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct ChatRequest {
|
||||
messages: Vec<SimpleChatMessage>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct SimpleChatMessage {
|
||||
role: String,
|
||||
content: String,
|
||||
}
|
||||
|
||||
pub fn router(state: Arc<AppState>) -> Router {
|
||||
Router::new()
|
||||
.route("/chat", post(chat))
|
||||
.with_state(state)
|
||||
}
|
||||
|
||||
async fn chat(
|
||||
State(state): State<Arc<AppState>>,
|
||||
Json(input): Json<ChatRequest>,
|
||||
) -> Result<Json<serde_json::Value>, Response> {
|
||||
let llm = LlmClient::new(&state.config.llm);
|
||||
let messages: Vec<ChatMessage> = input
|
||||
.messages
|
||||
.into_iter()
|
||||
.map(|m| ChatMessage {
|
||||
role: m.role,
|
||||
content: Some(m.content),
|
||||
tool_calls: None,
|
||||
tool_call_id: None,
|
||||
})
|
||||
.collect();
|
||||
|
||||
let reply = llm.chat(messages).await.map_err(|e| {
|
||||
tracing::error!("Chat LLM error: {}", e);
|
||||
(StatusCode::INTERNAL_SERVER_ERROR, e.to_string()).into_response()
|
||||
})?;
|
||||
|
||||
Ok(Json(serde_json::json!({ "reply": reply })))
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
mod chat;
|
||||
mod kb;
|
||||
pub mod obj;
|
||||
mod projects;
|
||||
@@ -31,6 +32,7 @@ pub fn router(state: Arc<AppState>) -> Router {
|
||||
.merge(timers::router(state.clone()))
|
||||
.merge(kb::router(state.clone()))
|
||||
.merge(settings::router(state.clone()))
|
||||
.merge(chat::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))
|
||||
|
||||
@@ -11,6 +11,7 @@ use crate::AppState;
|
||||
use crate::agent::{AgentEvent, PlanStepInfo};
|
||||
use crate::db::{Workflow, ExecutionLogEntry, Comment, LlmCallLogEntry};
|
||||
use crate::state::AgentState;
|
||||
use crate::template;
|
||||
use super::{ApiResult, db_err};
|
||||
|
||||
#[derive(serde::Serialize)]
|
||||
@@ -21,6 +22,8 @@ struct ReportResponse {
|
||||
#[derive(Deserialize)]
|
||||
pub struct CreateWorkflow {
|
||||
pub requirement: String,
|
||||
#[serde(default)]
|
||||
pub template_id: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
@@ -36,6 +39,7 @@ pub fn router(state: Arc<AppState>) -> Router {
|
||||
.route("/workflows/{id}/report", get(get_report))
|
||||
.route("/workflows/{id}/plan", get(get_plan))
|
||||
.route("/workflows/{id}/llm-calls", get(list_llm_calls))
|
||||
.route("/templates", get(list_templates))
|
||||
.with_state(state)
|
||||
}
|
||||
|
||||
@@ -72,6 +76,7 @@ async fn create_workflow(
|
||||
state.agent_mgr.send_event(&project_id, AgentEvent::NewRequirement {
|
||||
workflow_id: workflow.id.clone(),
|
||||
requirement: workflow.requirement.clone(),
|
||||
template_id: input.template_id,
|
||||
}).await;
|
||||
|
||||
Ok(Json(workflow))
|
||||
@@ -191,3 +196,7 @@ async fn list_llm_calls(
|
||||
.map(Json)
|
||||
.map_err(db_err)
|
||||
}
|
||||
|
||||
async fn list_templates() -> Json<Vec<template::TemplateListItem>> {
|
||||
Json(template::list_all_templates().await)
|
||||
}
|
||||
|
||||
10
src/main.rs
10
src/main.rs
@@ -29,6 +29,15 @@ pub struct Config {
|
||||
pub llm: LlmConfig,
|
||||
pub server: ServerConfig,
|
||||
pub database: DatabaseConfig,
|
||||
#[serde(default)]
|
||||
pub template_repo: Option<TemplateRepoConfig>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, serde::Deserialize)]
|
||||
pub struct TemplateRepoConfig {
|
||||
pub gitea_url: String,
|
||||
pub owner: String,
|
||||
pub repo: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, serde::Deserialize)]
|
||||
@@ -147,6 +156,7 @@ async fn resume_workflows(pool: SqlitePool, agent_mgr: Arc<agent::AgentManager>)
|
||||
agent_mgr.send_event(&project_id, agent::AgentEvent::NewRequirement {
|
||||
workflow_id,
|
||||
requirement,
|
||||
template_id: None,
|
||||
}).await;
|
||||
}
|
||||
}
|
||||
|
||||
415
src/template.rs
415
src/template.rs
@@ -1,4 +1,4 @@
|
||||
use std::path::Path;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use serde::Deserialize;
|
||||
|
||||
@@ -6,6 +6,7 @@ use crate::llm::{ChatMessage, LlmClient};
|
||||
use crate::tools::ExternalToolManager;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[allow(dead_code)]
|
||||
pub struct TemplateInfo {
|
||||
pub name: String,
|
||||
pub description: String,
|
||||
@@ -21,7 +22,16 @@ pub struct LoadedTemplate {
|
||||
pub kb_files: Vec<(String, String)>,
|
||||
}
|
||||
|
||||
pub fn templates_dir() -> &'static str {
|
||||
#[derive(Debug, Clone, serde::Serialize)]
|
||||
pub struct TemplateListItem {
|
||||
pub id: String,
|
||||
pub name: String,
|
||||
pub description: String,
|
||||
}
|
||||
|
||||
// --- Template directories ---
|
||||
|
||||
fn builtin_dir() -> &'static str {
|
||||
if Path::new("/app/templates").is_dir() {
|
||||
"/app/templates"
|
||||
} else {
|
||||
@@ -29,45 +39,295 @@ pub fn templates_dir() -> &'static str {
|
||||
}
|
||||
}
|
||||
|
||||
/// Scan available templates and ask LLM to pick one (or none).
|
||||
pub async fn select_template(llm: &LlmClient, requirement: &str) -> Option<String> {
|
||||
let base = Path::new(templates_dir());
|
||||
let mut entries = match tokio::fs::read_dir(base).await {
|
||||
Ok(e) => e,
|
||||
Err(_) => return None,
|
||||
};
|
||||
fn repo_dir() -> &'static str {
|
||||
if Path::new("/app/oseng-templates").is_dir() {
|
||||
"/app/oseng-templates"
|
||||
} else {
|
||||
"oseng-templates"
|
||||
}
|
||||
}
|
||||
|
||||
let mut templates: Vec<(String, TemplateInfo)> = Vec::new();
|
||||
while let Ok(Some(entry)) = entries.next_entry().await {
|
||||
// Use metadata() instead of file_type() to follow symlinks
|
||||
let is_dir = tokio::fs::metadata(entry.path())
|
||||
.await
|
||||
.map(|m| m.is_dir())
|
||||
.unwrap_or(false);
|
||||
if !is_dir {
|
||||
continue;
|
||||
}
|
||||
let id = entry.file_name().to_string_lossy().to_string();
|
||||
let meta_path = entry.path().join("template.json");
|
||||
if let Ok(data) = tokio::fs::read_to_string(&meta_path).await {
|
||||
if let Ok(info) = serde_json::from_str::<TemplateInfo>(&data) {
|
||||
templates.push((id, info));
|
||||
/// For backward compat.
|
||||
pub fn templates_dir() -> &'static str {
|
||||
builtin_dir()
|
||||
}
|
||||
|
||||
// --- Scanning ---
|
||||
|
||||
/// List all templates from both built-in and repo (all branches).
|
||||
pub async fn list_all_templates() -> Vec<TemplateListItem> {
|
||||
let mut items = Vec::new();
|
||||
|
||||
// 1. Built-in templates (flat: each top-level dir with template.json)
|
||||
let builtin = Path::new(builtin_dir());
|
||||
if let Ok(mut entries) = tokio::fs::read_dir(builtin).await {
|
||||
while let Ok(Some(entry)) = entries.next_entry().await {
|
||||
let is_dir = tokio::fs::metadata(entry.path())
|
||||
.await
|
||||
.map(|m| m.is_dir())
|
||||
.unwrap_or(false);
|
||||
if !is_dir {
|
||||
continue;
|
||||
}
|
||||
let id = entry.file_name().to_string_lossy().to_string();
|
||||
let meta_path = entry.path().join("template.json");
|
||||
if let Ok(data) = tokio::fs::read_to_string(&meta_path).await {
|
||||
if let Ok(info) = serde_json::from_str::<TemplateInfo>(&data) {
|
||||
items.push(TemplateListItem {
|
||||
id,
|
||||
name: info.name,
|
||||
description: info.description,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if templates.is_empty() {
|
||||
// 2. Repo templates (all branches, find INSTRUCTIONS.md via git ls-tree)
|
||||
let repo = Path::new(repo_dir());
|
||||
if repo.join(".git").is_dir() {
|
||||
items.extend(scan_repo_all_branches(repo).await);
|
||||
}
|
||||
|
||||
items.sort_by(|a, b| a.id.cmp(&b.id));
|
||||
items
|
||||
}
|
||||
|
||||
/// Scan all branches in a git repo for directories containing INSTRUCTIONS.md.
|
||||
/// Template ID = "{branch}/{path}" e.g. "main/simple-npi", "fam/oncall/network-latency".
|
||||
async fn scan_repo_all_branches(repo: &Path) -> Vec<TemplateListItem> {
|
||||
let mut items = Vec::new();
|
||||
|
||||
// First: git fetch --all to get latest branches
|
||||
let _ = tokio::process::Command::new("git")
|
||||
.args(["fetch", "--all", "--prune", "-q"])
|
||||
.current_dir(repo)
|
||||
.output()
|
||||
.await;
|
||||
|
||||
// List all remote branches: "origin/main", "origin/fam/foo" etc.
|
||||
let output = match tokio::process::Command::new("git")
|
||||
.args(["branch", "-r", "--format=%(refname:short)"])
|
||||
.current_dir(repo)
|
||||
.output()
|
||||
.await
|
||||
{
|
||||
Ok(o) if o.status.success() => String::from_utf8_lossy(&o.stdout).to_string(),
|
||||
_ => return items,
|
||||
};
|
||||
|
||||
for line in output.lines() {
|
||||
let line = line.trim();
|
||||
if line.is_empty() || line.contains("HEAD") {
|
||||
continue;
|
||||
}
|
||||
// line = "origin/main" or "origin/fam/feature"
|
||||
let branch = match line.strip_prefix("origin/") {
|
||||
Some(b) => b,
|
||||
None => line,
|
||||
};
|
||||
|
||||
// git ls-tree -r --name-only origin/branch
|
||||
let tree_output = match tokio::process::Command::new("git")
|
||||
.args(["ls-tree", "-r", "--name-only", line])
|
||||
.current_dir(repo)
|
||||
.output()
|
||||
.await
|
||||
{
|
||||
Ok(o) if o.status.success() => String::from_utf8_lossy(&o.stdout).to_string(),
|
||||
_ => continue,
|
||||
};
|
||||
|
||||
for file_path in tree_output.lines() {
|
||||
let file_path = file_path.trim();
|
||||
if !file_path.ends_with("INSTRUCTIONS.md") {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Template dir path (relative to repo root)
|
||||
let template_path = if file_path == "INSTRUCTIONS.md" {
|
||||
""
|
||||
} else {
|
||||
file_path.trim_end_matches("/INSTRUCTIONS.md")
|
||||
};
|
||||
|
||||
let template_id = if template_path.is_empty() {
|
||||
branch.to_string()
|
||||
} else {
|
||||
format!("{}/{}", branch, template_path)
|
||||
};
|
||||
|
||||
// Try to read template.json via git show
|
||||
let (name, description) = read_git_file_json(repo, line, template_path).await;
|
||||
|
||||
items.push(TemplateListItem {
|
||||
id: template_id.clone(),
|
||||
name: name.unwrap_or_else(|| template_id.clone()),
|
||||
description: description.unwrap_or_default(),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
items
|
||||
}
|
||||
|
||||
/// Read template.json from a git ref via `git show`.
|
||||
async fn read_git_file_json(
|
||||
repo: &Path,
|
||||
ref_name: &str,
|
||||
template_path: &str,
|
||||
) -> (Option<String>, Option<String>) {
|
||||
let file = if template_path.is_empty() {
|
||||
"template.json".to_string()
|
||||
} else {
|
||||
format!("{}/template.json", template_path)
|
||||
};
|
||||
|
||||
let output = match tokio::process::Command::new("git")
|
||||
.args(["show", &format!("{}:{}", ref_name, file)])
|
||||
.current_dir(repo)
|
||||
.output()
|
||||
.await
|
||||
{
|
||||
Ok(o) if o.status.success() => String::from_utf8_lossy(&o.stdout).to_string(),
|
||||
_ => return (None, None),
|
||||
};
|
||||
|
||||
match serde_json::from_str::<TemplateInfo>(&output) {
|
||||
Ok(info) => (Some(info.name), Some(info.description)),
|
||||
Err(_) => (None, None),
|
||||
}
|
||||
}
|
||||
|
||||
/// Extract a template from a git branch to a local directory.
|
||||
/// Uses `git archive` to extract the template subtree.
|
||||
pub async fn extract_repo_template(template_id: &str) -> anyhow::Result<PathBuf> {
|
||||
let repo = Path::new(repo_dir());
|
||||
let dest = PathBuf::from("/tmp/tori-repo-templates").join(template_id);
|
||||
let _ = tokio::fs::remove_dir_all(&dest).await;
|
||||
tokio::fs::create_dir_all(&dest).await?;
|
||||
|
||||
// Parse template_id: "main/simple-npi" → branch="main", path="simple-npi"
|
||||
// But branch could be multi-segment: "fam/feature" → need to find split point
|
||||
// Strategy: try each split, check if "origin/{prefix}" is a valid ref
|
||||
let (ref_name, template_path) = resolve_branch_and_path(repo, template_id).await
|
||||
.ok_or_else(|| anyhow::anyhow!("Cannot resolve template: {}", template_id))?;
|
||||
|
||||
// git archive <ref> <path> | tar -x -C <dest> --strip-components=N
|
||||
let strip = if template_path.is_empty() {
|
||||
0
|
||||
} else {
|
||||
template_path.matches('/').count() + 1
|
||||
};
|
||||
|
||||
let archive_args = if template_path.is_empty() {
|
||||
vec!["archive".to_string(), ref_name.clone()]
|
||||
} else {
|
||||
vec!["archive".to_string(), ref_name.clone(), template_path.clone()]
|
||||
};
|
||||
|
||||
let git_archive = tokio::process::Command::new("git")
|
||||
.args(&archive_args)
|
||||
.current_dir(repo)
|
||||
.stdout(std::process::Stdio::piped())
|
||||
.spawn()?;
|
||||
|
||||
let child_stdout = git_archive.stdout.unwrap();
|
||||
let stdio: std::process::Stdio = child_stdout.try_into()
|
||||
.map_err(|e| anyhow::anyhow!("Failed to convert stdout: {:?}", e))?;
|
||||
|
||||
let mut tar_args = vec!["-x".to_string(), "-C".to_string(), dest.to_string_lossy().to_string()];
|
||||
if strip > 0 {
|
||||
tar_args.push(format!("--strip-components={}", strip));
|
||||
}
|
||||
|
||||
let tar_output = tokio::process::Command::new("tar")
|
||||
.args(&tar_args)
|
||||
.stdin(stdio)
|
||||
.output()
|
||||
.await?;
|
||||
|
||||
if !tar_output.status.success() {
|
||||
anyhow::bail!(
|
||||
"tar extract failed: {}",
|
||||
String::from_utf8_lossy(&tar_output.stderr)
|
||||
);
|
||||
}
|
||||
|
||||
// Make scripts in tools/ executable
|
||||
let tools_dir = dest.join("tools");
|
||||
if tools_dir.is_dir() {
|
||||
if let Ok(mut entries) = tokio::fs::read_dir(&tools_dir).await {
|
||||
while let Ok(Some(entry)) = entries.next_entry().await {
|
||||
let path = entry.path();
|
||||
if path.is_file() {
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
if let Ok(meta) = tokio::fs::metadata(&path).await {
|
||||
let mut perms = meta.permissions();
|
||||
perms.set_mode(perms.mode() | 0o111);
|
||||
let _ = tokio::fs::set_permissions(&path, perms).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tracing::info!("Extracted repo template '{}' to {}", template_id, dest.display());
|
||||
Ok(dest)
|
||||
}
|
||||
|
||||
/// Given a template_id like "main/simple-npi" or "fam/feature/oncall",
|
||||
/// figure out which part is the branch and which is the path.
|
||||
async fn resolve_branch_and_path(repo: &Path, template_id: &str) -> Option<(String, String)> {
|
||||
let parts: Vec<&str> = template_id.splitn(10, '/').collect();
|
||||
|
||||
// Try progressively longer branch names
|
||||
for i in 1..parts.len() + 1 {
|
||||
let candidate_branch = parts[..i].join("/");
|
||||
let ref_name = format!("origin/{}", candidate_branch);
|
||||
|
||||
let output = tokio::process::Command::new("git")
|
||||
.args(["rev-parse", "--verify", &ref_name])
|
||||
.current_dir(repo)
|
||||
.output()
|
||||
.await;
|
||||
|
||||
if let Ok(o) = output {
|
||||
if o.status.success() {
|
||||
let path = if i < parts.len() {
|
||||
parts[i..].join("/")
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
return Some((ref_name, path));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
/// Check if a template is from the repo (vs built-in).
|
||||
pub fn is_repo_template(template_id: &str) -> bool {
|
||||
// Built-in templates are flat names without '/'
|
||||
// Repo templates always have branch/path format
|
||||
template_id.contains('/')
|
||||
}
|
||||
|
||||
// --- LLM template selection ---
|
||||
|
||||
pub async fn select_template(llm: &LlmClient, requirement: &str) -> Option<String> {
|
||||
let all = list_all_templates().await;
|
||||
if all.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let listing: String = templates
|
||||
let listing: String = all
|
||||
.iter()
|
||||
.map(|(id, info)| {
|
||||
format!(
|
||||
"- id: {}\n 名称: {}\n 描述: {}\n 适用场景: {}",
|
||||
id, info.name, info.description, info.match_hint
|
||||
)
|
||||
})
|
||||
.map(|t| format!("- id: {}\n 名称: {}\n 描述: {}", t.id, t.name, t.description))
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n");
|
||||
|
||||
@@ -86,34 +346,26 @@ pub async fn select_template(llm: &LlmClient, requirement: &str) -> Option<Strin
|
||||
|
||||
let answer = response.trim().to_lowercase();
|
||||
tracing::info!("Template selection LLM response: '{}' (available: {:?})",
|
||||
answer, templates.iter().map(|(id, _)| id.as_str()).collect::<Vec<_>>());
|
||||
answer, all.iter().map(|t| t.id.as_str()).collect::<Vec<_>>());
|
||||
if answer == "none" {
|
||||
return None;
|
||||
}
|
||||
|
||||
// Verify the answer matches an actual template ID
|
||||
let result = templates
|
||||
.iter()
|
||||
.find(|(id, _)| id == &answer)
|
||||
.map(|(id, _)| id.clone());
|
||||
if result.is_none() {
|
||||
tracing::warn!("Template selection: LLM returned '{}' which doesn't match any template ID", answer);
|
||||
}
|
||||
result
|
||||
all.iter().find(|t| t.id == answer).map(|t| t.id.clone())
|
||||
}
|
||||
|
||||
/// Copy template contents to workdir (excluding template.json, tools/, kb/).
|
||||
pub async fn apply_template(template_id: &str, workdir: &str) -> anyhow::Result<()> {
|
||||
let src = Path::new(templates_dir()).join(template_id);
|
||||
if !src.is_dir() {
|
||||
anyhow::bail!("Template directory not found: {}", template_id);
|
||||
// --- Template loading ---
|
||||
|
||||
/// Copy template contents to workdir (excluding template.json, tools/, kb/, INSTRUCTIONS.md).
|
||||
pub async fn apply_template(template_dir: &Path, workdir: &str) -> anyhow::Result<()> {
|
||||
if !template_dir.is_dir() {
|
||||
anyhow::bail!("Template directory not found: {}", template_dir.display());
|
||||
}
|
||||
copy_dir_recursive(&src, Path::new(workdir)).await
|
||||
copy_dir_recursive(template_dir, Path::new(workdir)).await
|
||||
}
|
||||
|
||||
/// Recursively copy directory contents, skipping template.json/tools/kb at the top level.
|
||||
async fn copy_dir_recursive(src: &Path, dst: &Path) -> anyhow::Result<()> {
|
||||
let mut stack: Vec<(std::path::PathBuf, std::path::PathBuf, bool)> =
|
||||
let mut stack: Vec<(PathBuf, PathBuf, bool)> =
|
||||
vec![(src.to_path_buf(), dst.to_path_buf(), true)];
|
||||
|
||||
while let Some((src_dir, dst_dir, top_level)) = stack.pop() {
|
||||
@@ -123,8 +375,11 @@ async fn copy_dir_recursive(src: &Path, dst: &Path) -> anyhow::Result<()> {
|
||||
let name = entry.file_name();
|
||||
let name_str = name.to_string_lossy();
|
||||
|
||||
// Skip metadata/convention dirs at top level
|
||||
if top_level && (name_str == "template.json" || name_str == "tools" || name_str == "kb")
|
||||
if top_level
|
||||
&& (name_str == "template.json"
|
||||
|| name_str == "tools"
|
||||
|| name_str == "kb"
|
||||
|| name_str == "INSTRUCTIONS.md")
|
||||
{
|
||||
continue;
|
||||
}
|
||||
@@ -143,41 +398,37 @@ async fn copy_dir_recursive(src: &Path, dst: &Path) -> anyhow::Result<()> {
|
||||
}
|
||||
|
||||
impl LoadedTemplate {
|
||||
/// Load a template: discover tools, read KB files, read instructions.
|
||||
pub async fn load(template_id: &str) -> anyhow::Result<Self> {
|
||||
let base = Path::new(templates_dir()).join(template_id);
|
||||
pub async fn load_from_dir(template_id: &str, base: &Path) -> anyhow::Result<Self> {
|
||||
if !base.is_dir() {
|
||||
anyhow::bail!("Template directory not found: {}", template_id);
|
||||
anyhow::bail!("Template directory not found: {}", base.display());
|
||||
}
|
||||
|
||||
// Read template.json
|
||||
let meta_path = base.join("template.json");
|
||||
let meta_data = tokio::fs::read_to_string(&meta_path).await?;
|
||||
let info: TemplateInfo = serde_json::from_str(&meta_data)?;
|
||||
let info = if let Ok(data) = tokio::fs::read_to_string(&meta_path).await {
|
||||
serde_json::from_str::<TemplateInfo>(&data).unwrap_or_else(|_| TemplateInfo {
|
||||
name: template_id.to_string(),
|
||||
description: String::new(),
|
||||
match_hint: String::new(),
|
||||
})
|
||||
} else {
|
||||
TemplateInfo {
|
||||
name: template_id.to_string(),
|
||||
description: String::new(),
|
||||
match_hint: String::new(),
|
||||
}
|
||||
};
|
||||
|
||||
// Read INSTRUCTIONS.md
|
||||
let instructions_path = base.join("INSTRUCTIONS.md");
|
||||
let instructions = tokio::fs::read_to_string(&instructions_path)
|
||||
let instructions = tokio::fs::read_to_string(base.join("INSTRUCTIONS.md"))
|
||||
.await
|
||||
.unwrap_or_default();
|
||||
|
||||
// Discover external tools
|
||||
let tools_dir = base.join("tools");
|
||||
let external_tools = ExternalToolManager::discover(&tools_dir).await;
|
||||
tracing::info!(
|
||||
"Template '{}': discovered {} external tools",
|
||||
template_id,
|
||||
external_tools.len()
|
||||
);
|
||||
tracing::info!("Template '{}': {} external tools", template_id, external_tools.len());
|
||||
|
||||
// Scan KB files
|
||||
let kb_dir = base.join("kb");
|
||||
let kb_files = scan_kb_files(&kb_dir).await;
|
||||
tracing::info!(
|
||||
"Template '{}': found {} KB files",
|
||||
template_id,
|
||||
kb_files.len()
|
||||
);
|
||||
tracing::info!("Template '{}': {} KB files", template_id, kb_files.len());
|
||||
|
||||
Ok(Self {
|
||||
id: template_id.to_string(),
|
||||
@@ -187,10 +438,13 @@ impl LoadedTemplate {
|
||||
kb_files,
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn load(template_id: &str) -> anyhow::Result<Self> {
|
||||
let base = Path::new(builtin_dir()).join(template_id);
|
||||
Self::load_from_dir(template_id, &base).await
|
||||
}
|
||||
}
|
||||
|
||||
/// Scan kb/ directory for .md files. Returns (title, content) pairs.
|
||||
/// Title is extracted from the first `# heading` line, or falls back to the filename.
|
||||
async fn scan_kb_files(kb_dir: &Path) -> Vec<(String, String)> {
|
||||
let mut results = Vec::new();
|
||||
|
||||
@@ -201,18 +455,12 @@ async fn scan_kb_files(kb_dir: &Path) -> Vec<(String, String)> {
|
||||
|
||||
while let Ok(Some(entry)) = entries.next_entry().await {
|
||||
let path = entry.path();
|
||||
|
||||
// Resolve symlinks
|
||||
let real_path = match tokio::fs::canonicalize(&path).await {
|
||||
Ok(p) => p,
|
||||
Err(_) => path.clone(),
|
||||
};
|
||||
|
||||
// Only process .md files
|
||||
let ext = real_path
|
||||
.extension()
|
||||
.and_then(|e| e.to_str())
|
||||
.unwrap_or("");
|
||||
let ext = real_path.extension().and_then(|e| e.to_str()).unwrap_or("");
|
||||
if ext != "md" {
|
||||
continue;
|
||||
}
|
||||
@@ -225,7 +473,6 @@ async fn scan_kb_files(kb_dir: &Path) -> Vec<(String, String)> {
|
||||
}
|
||||
};
|
||||
|
||||
// Extract title: first `# heading` line, or filename without extension
|
||||
let title = content
|
||||
.lines()
|
||||
.find(|l| l.starts_with("# "))
|
||||
|
||||
@@ -68,6 +68,7 @@ async fn check_timers(pool: &SqlitePool, agent_mgr: &Arc<AgentManager>) -> anyho
|
||||
agent_mgr.send_event(&timer.project_id, AgentEvent::NewRequirement {
|
||||
workflow_id,
|
||||
requirement: timer.requirement.clone(),
|
||||
template_id: None,
|
||||
}).await;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { Project, Workflow, ExecutionLogEntry, Comment, Timer, KbArticle, KbArticleSummary, PlanStepInfo, LlmCallLogEntry } from './types'
|
||||
import type { Project, Workflow, ExecutionLogEntry, Comment, Timer, KbArticle, KbArticleSummary, PlanStepInfo, LlmCallLogEntry, ChatMessage } from './types'
|
||||
|
||||
const BASE = `${import.meta.env.BASE_URL.replace(/\/$/, '')}/api`
|
||||
|
||||
@@ -37,12 +37,15 @@ export const api = {
|
||||
listWorkflows: (projectId: string) =>
|
||||
request<Workflow[]>(`/projects/${projectId}/workflows`),
|
||||
|
||||
createWorkflow: (projectId: string, requirement: string) =>
|
||||
createWorkflow: (projectId: string, requirement: string, templateId?: string) =>
|
||||
request<Workflow>(`/projects/${projectId}/workflows`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ requirement }),
|
||||
body: JSON.stringify({ requirement, template_id: templateId || undefined }),
|
||||
}),
|
||||
|
||||
listTemplates: () =>
|
||||
request<{ id: string; name: string; description: string }[]>('/templates'),
|
||||
|
||||
listSteps: (workflowId: string) =>
|
||||
request<ExecutionLogEntry[]>(`/workflows/${workflowId}/steps`),
|
||||
|
||||
@@ -103,6 +106,12 @@ export const api = {
|
||||
deleteArticle: (id: string) =>
|
||||
request<boolean>(`/kb/articles/${id}`, { method: 'DELETE' }),
|
||||
|
||||
chat: (messages: ChatMessage[]) =>
|
||||
request<{ reply: string }>('/chat', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ messages }),
|
||||
}),
|
||||
|
||||
getSettings: () => request<Record<string, string>>('/settings'),
|
||||
|
||||
putSetting: (key: string, value: string) =>
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
<script setup lang="ts">
|
||||
import { ref, onMounted, onUnmounted, computed } from 'vue'
|
||||
import Sidebar from './Sidebar.vue'
|
||||
import ChatPanel from './ChatPanel.vue'
|
||||
import WorkflowView from './WorkflowView.vue'
|
||||
import ReportView from './ReportView.vue'
|
||||
import CreateForm from './CreateForm.vue'
|
||||
@@ -19,11 +20,38 @@ const showObj = ref(false)
|
||||
const kbArticles = ref<KbArticleSummary[]>([])
|
||||
const selectedArticleId = ref('')
|
||||
const appTitle = ref('')
|
||||
const chatOpen = ref(false)
|
||||
const showSettings = ref(false)
|
||||
const editingTitle = ref(false)
|
||||
const titleInput = ref('')
|
||||
|
||||
const basePath = import.meta.env.BASE_URL.replace(/\/$/, '')
|
||||
|
||||
const isReportPage = computed(() => !!reportWorkflowId.value)
|
||||
|
||||
const currentPageTitle = computed(() => {
|
||||
if (showKb.value) return 'Knowledge Base'
|
||||
if (showObj.value) return 'Object Storage'
|
||||
if (selectedProjectId.value) {
|
||||
const p = projects.value.find(p => p.id === selectedProjectId.value)
|
||||
return p?.name || ''
|
||||
}
|
||||
return ''
|
||||
})
|
||||
|
||||
function onEditTitle() {
|
||||
titleInput.value = appTitle.value || 'Tori'
|
||||
editingTitle.value = true
|
||||
}
|
||||
|
||||
function onSaveTitle() {
|
||||
const val = titleInput.value.trim()
|
||||
if (val && val !== appTitle.value) {
|
||||
onUpdateAppTitle(val)
|
||||
}
|
||||
editingTitle.value = false
|
||||
}
|
||||
|
||||
function parseUrl(): { projectId: string; reportId: string; kb: boolean; obj: boolean } {
|
||||
let path = location.pathname
|
||||
if (basePath && path.startsWith(basePath)) {
|
||||
@@ -96,11 +124,11 @@ function onStartCreate() {
|
||||
history.pushState(null, '', `${basePath}/`)
|
||||
}
|
||||
|
||||
async function onConfirmCreate(req: string) {
|
||||
async function onConfirmCreate(req: string, templateId?: string) {
|
||||
try {
|
||||
const project = await api.createProject('新项目')
|
||||
projects.value.unshift(project)
|
||||
await api.createWorkflow(project.id, req)
|
||||
await api.createWorkflow(project.id, req, templateId)
|
||||
creating.value = false
|
||||
selectedProjectId.value = project.id
|
||||
history.pushState(null, '', `${basePath}/projects/${project.id}`)
|
||||
@@ -213,9 +241,22 @@ function onArticleSaved(id: string, title: string, updatedAt: string) {
|
||||
a.title = title
|
||||
a.updated_at = updatedAt
|
||||
}
|
||||
// Re-sort by updated_at descending
|
||||
kbArticles.value.sort((a, b) => b.updated_at.localeCompare(a.updated_at))
|
||||
}
|
||||
|
||||
function goHome() {
|
||||
showKb.value = false
|
||||
showObj.value = false
|
||||
selectedArticleId.value = ''
|
||||
creating.value = false
|
||||
if (projects.value[0]) {
|
||||
selectedProjectId.value = projects.value[0].id
|
||||
history.pushState(null, '', `${basePath}/projects/${projects.value[0].id}`)
|
||||
} else {
|
||||
selectedProjectId.value = ''
|
||||
history.pushState(null, '', `${basePath}/`)
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
@@ -223,54 +264,92 @@ function onArticleSaved(id: string, title: string, updatedAt: string) {
|
||||
<ReportView :workflowId="reportWorkflowId" :key="reportWorkflowId" />
|
||||
</div>
|
||||
<div v-else class="app-layout">
|
||||
<Sidebar
|
||||
:projects="projects"
|
||||
:selectedId="selectedProjectId"
|
||||
:kbMode="showKb"
|
||||
:objMode="showObj"
|
||||
:kbArticles="kbArticles"
|
||||
:selectedArticleId="selectedArticleId"
|
||||
:appTitle="appTitle"
|
||||
@select="onSelectProject"
|
||||
@create="onStartCreate"
|
||||
@delete="onDeleteProject"
|
||||
@openKb="onOpenKb"
|
||||
@closeKb="onCloseKb"
|
||||
@openObj="onOpenObj"
|
||||
@closeObj="onCloseObj"
|
||||
@selectArticle="selectedArticleId = $event"
|
||||
@createArticle="onCreateArticle"
|
||||
@deleteArticle="onDeleteArticle"
|
||||
@updateAppTitle="onUpdateAppTitle"
|
||||
/>
|
||||
<main class="main-content">
|
||||
<div v-if="error" class="error-banner" @click="error = ''">{{ error }}</div>
|
||||
<ObjBrowser
|
||||
v-if="showObj"
|
||||
@close="onCloseObj"
|
||||
/>
|
||||
<KbEditor
|
||||
v-else-if="showKb && selectedArticleId"
|
||||
:articleId="selectedArticleId"
|
||||
:key="selectedArticleId"
|
||||
@saved="onArticleSaved"
|
||||
/>
|
||||
<div v-else-if="showKb" class="empty-state">
|
||||
选择或创建一篇文章
|
||||
<header class="app-header">
|
||||
<div class="header-left">
|
||||
<span class="header-title" @click="goHome">{{ appTitle || 'Tori' }}</span>
|
||||
<template v-if="currentPageTitle">
|
||||
<span class="header-sep">/</span>
|
||||
<span class="header-page">{{ currentPageTitle }}</span>
|
||||
</template>
|
||||
</div>
|
||||
<div v-else-if="creating" class="empty-state">
|
||||
<CreateForm @submit="onConfirmCreate" @cancel="creating = false" />
|
||||
<div class="header-right">
|
||||
<div class="header-settings-wrapper">
|
||||
<button class="header-btn" @click="showSettings = !showSettings" title="Settings">⚙</button>
|
||||
<div v-if="showSettings" class="header-settings-menu">
|
||||
<div class="settings-item-row" v-if="!editingTitle">
|
||||
<span class="settings-label">App Title</span>
|
||||
<button class="settings-value" @click="onEditTitle">{{ appTitle || 'Tori' }}</button>
|
||||
</div>
|
||||
<div class="settings-item-row" v-else>
|
||||
<input
|
||||
class="settings-input"
|
||||
v-model="titleInput"
|
||||
@keyup.enter="onSaveTitle"
|
||||
@keyup.escape="editingTitle = false"
|
||||
@vue:mounted="($event: any) => $event.el.focus()"
|
||||
/>
|
||||
<button class="settings-save" @click="onSaveTitle">OK</button>
|
||||
</div>
|
||||
<button class="settings-item" @click="showSettings = false; onOpenKb()">Knowledge Base</button>
|
||||
<button class="settings-item" @click="showSettings = false; onOpenObj()">Object Storage</button>
|
||||
</div>
|
||||
</div>
|
||||
<button class="header-btn" @click="chatOpen = !chatOpen" :title="chatOpen ? 'Close chat' : 'Open chat'">
|
||||
💬
|
||||
</button>
|
||||
</div>
|
||||
<div v-else-if="!selectedProjectId" class="empty-state">
|
||||
选择或创建一个项目开始
|
||||
</div>
|
||||
<WorkflowView
|
||||
v-else
|
||||
:projectId="selectedProjectId"
|
||||
:key="selectedProjectId"
|
||||
@projectUpdate="onProjectUpdate"
|
||||
</header>
|
||||
<div class="app-body">
|
||||
<Sidebar
|
||||
:projects="projects"
|
||||
:selectedId="selectedProjectId"
|
||||
:kbMode="showKb"
|
||||
:objMode="showObj"
|
||||
:kbArticles="kbArticles"
|
||||
:selectedArticleId="selectedArticleId"
|
||||
@select="onSelectProject"
|
||||
@create="onStartCreate"
|
||||
@delete="onDeleteProject"
|
||||
@openKb="onOpenKb"
|
||||
@closeKb="onCloseKb"
|
||||
@openObj="onOpenObj"
|
||||
@closeObj="onCloseObj"
|
||||
@selectArticle="selectedArticleId = $event"
|
||||
@createArticle="onCreateArticle"
|
||||
@deleteArticle="onDeleteArticle"
|
||||
/>
|
||||
</main>
|
||||
<main class="main-content">
|
||||
<div v-if="error" class="error-banner" @click="error = ''">{{ error }}</div>
|
||||
<ObjBrowser
|
||||
v-if="showObj"
|
||||
@close="onCloseObj"
|
||||
/>
|
||||
<KbEditor
|
||||
v-else-if="showKb && selectedArticleId"
|
||||
:articleId="selectedArticleId"
|
||||
:key="selectedArticleId"
|
||||
@saved="onArticleSaved"
|
||||
/>
|
||||
<div v-else-if="showKb" class="empty-state">
|
||||
选择或创建一篇文章
|
||||
</div>
|
||||
<div v-else-if="creating" class="empty-state">
|
||||
<CreateForm @submit="onConfirmCreate" @cancel="creating = false" />
|
||||
</div>
|
||||
<div v-else-if="!selectedProjectId" class="empty-state">
|
||||
选择或创建一个项目开始
|
||||
</div>
|
||||
<WorkflowView
|
||||
v-else
|
||||
:projectId="selectedProjectId"
|
||||
:key="selectedProjectId"
|
||||
@projectUpdate="onProjectUpdate"
|
||||
/>
|
||||
</main>
|
||||
<aside v-if="chatOpen" class="chat-sidebar">
|
||||
<ChatPanel />
|
||||
</aside>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
@@ -282,10 +361,175 @@ function onArticleSaved(id: string, title: string, updatedAt: string) {
|
||||
|
||||
.app-layout {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
height: 100vh;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.app-header {
|
||||
height: var(--header-height);
|
||||
min-height: var(--header-height);
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
padding: 0 16px;
|
||||
background: var(--bg-primary);
|
||||
border-bottom: 1px solid var(--border);
|
||||
}
|
||||
|
||||
.header-left {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
.header-title {
|
||||
font-size: 15px;
|
||||
font-weight: 700;
|
||||
color: var(--accent);
|
||||
cursor: pointer;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.header-title:hover {
|
||||
opacity: 0.8;
|
||||
}
|
||||
|
||||
.header-sep {
|
||||
color: var(--text-secondary);
|
||||
font-size: 14px;
|
||||
}
|
||||
|
||||
.header-page {
|
||||
font-size: 14px;
|
||||
color: var(--text-primary);
|
||||
font-weight: 500;
|
||||
white-space: nowrap;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
}
|
||||
|
||||
.header-right {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 4px;
|
||||
}
|
||||
|
||||
.header-btn {
|
||||
width: 32px;
|
||||
height: 32px;
|
||||
padding: 0;
|
||||
background: transparent;
|
||||
border: none;
|
||||
border-radius: 6px;
|
||||
font-size: 16px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
cursor: pointer;
|
||||
color: var(--text-secondary);
|
||||
}
|
||||
|
||||
.header-btn:hover {
|
||||
background: var(--bg-tertiary);
|
||||
color: var(--text-primary);
|
||||
}
|
||||
|
||||
.header-settings-wrapper {
|
||||
position: relative;
|
||||
}
|
||||
|
||||
.header-settings-menu {
|
||||
position: absolute;
|
||||
top: 100%;
|
||||
right: 0;
|
||||
margin-top: 4px;
|
||||
background: var(--bg-primary);
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 8px;
|
||||
padding: 4px;
|
||||
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
|
||||
min-width: 200px;
|
||||
z-index: 200;
|
||||
}
|
||||
|
||||
.settings-item-row {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
padding: 6px 12px;
|
||||
}
|
||||
|
||||
.settings-label {
|
||||
font-size: 12px;
|
||||
color: var(--text-secondary);
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.settings-value {
|
||||
flex: 1;
|
||||
text-align: right;
|
||||
background: none;
|
||||
border: none;
|
||||
color: var(--text-primary);
|
||||
font-size: 13px;
|
||||
cursor: pointer;
|
||||
padding: 2px 4px;
|
||||
border-radius: 4px;
|
||||
}
|
||||
|
||||
.settings-value:hover {
|
||||
background: var(--bg-tertiary);
|
||||
}
|
||||
|
||||
.settings-input {
|
||||
flex: 1;
|
||||
padding: 4px 8px;
|
||||
font-size: 13px;
|
||||
background: var(--bg-tertiary);
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 4px;
|
||||
color: var(--text-primary);
|
||||
outline: none;
|
||||
}
|
||||
|
||||
.settings-input:focus {
|
||||
border-color: var(--accent);
|
||||
}
|
||||
|
||||
.settings-save {
|
||||
padding: 4px 8px;
|
||||
font-size: 12px;
|
||||
background: var(--accent);
|
||||
color: #fff;
|
||||
border: none;
|
||||
border-radius: 4px;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.settings-item {
|
||||
width: 100%;
|
||||
padding: 8px 12px;
|
||||
background: none;
|
||||
border: none;
|
||||
color: var(--text-primary);
|
||||
font-size: 13px;
|
||||
text-align: left;
|
||||
cursor: pointer;
|
||||
border-radius: 6px;
|
||||
}
|
||||
|
||||
.settings-item:hover {
|
||||
background: var(--bg-tertiary);
|
||||
}
|
||||
|
||||
.app-body {
|
||||
flex: 1;
|
||||
display: flex;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.main-content {
|
||||
flex: 1;
|
||||
overflow: hidden;
|
||||
@@ -309,4 +553,12 @@ function onArticleSaved(id: string, title: string, updatedAt: string) {
|
||||
font-size: 13px;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.chat-sidebar {
|
||||
width: var(--chat-sidebar-width, 360px);
|
||||
flex-shrink: 0;
|
||||
border-left: 1px solid var(--border);
|
||||
background: var(--bg-secondary);
|
||||
overflow: hidden;
|
||||
}
|
||||
</style>
|
||||
|
||||
216
web/src/components/ChatPanel.vue
Normal file
216
web/src/components/ChatPanel.vue
Normal file
@@ -0,0 +1,216 @@
|
||||
<script setup lang="ts">
|
||||
import { ref, nextTick } from 'vue'
|
||||
import { api } from '../api'
|
||||
import type { ChatMessage } from '../types'
|
||||
|
||||
const messages = ref<ChatMessage[]>([])
|
||||
const input = ref('')
|
||||
const loading = ref(false)
|
||||
const listEl = ref<HTMLElement | null>(null)
|
||||
|
||||
async function scrollToBottom() {
|
||||
await nextTick()
|
||||
if (listEl.value) {
|
||||
listEl.value.scrollTop = listEl.value.scrollHeight
|
||||
}
|
||||
}
|
||||
|
||||
async function send() {
|
||||
const text = input.value.trim()
|
||||
if (!text || loading.value) return
|
||||
|
||||
messages.value.push({ role: 'user', content: text })
|
||||
input.value = ''
|
||||
loading.value = true
|
||||
await scrollToBottom()
|
||||
|
||||
try {
|
||||
const resp = await api.chat(messages.value)
|
||||
messages.value.push({ role: 'assistant', content: resp.reply })
|
||||
} catch (e: any) {
|
||||
messages.value.push({ role: 'assistant', content: `Error: ${e.message}` })
|
||||
} finally {
|
||||
loading.value = false
|
||||
await scrollToBottom()
|
||||
}
|
||||
}
|
||||
|
||||
function clear() {
|
||||
messages.value = []
|
||||
input.value = ''
|
||||
}
|
||||
|
||||
function onKeydown(e: KeyboardEvent) {
|
||||
if (e.key === 'Enter' && !e.shiftKey) {
|
||||
e.preventDefault()
|
||||
send()
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="chat-panel">
|
||||
<div class="chat-header">
|
||||
<span class="chat-title">Chat</span>
|
||||
<button class="chat-clear" @click="clear" :disabled="messages.length === 0 && !loading">Clear</button>
|
||||
</div>
|
||||
|
||||
<div class="chat-messages" ref="listEl">
|
||||
<div v-if="messages.length === 0 && !loading" class="chat-empty">
|
||||
Ask anything...
|
||||
</div>
|
||||
<div
|
||||
v-for="(msg, i) in messages"
|
||||
:key="i"
|
||||
class="chat-msg"
|
||||
:class="msg.role"
|
||||
>
|
||||
<div class="chat-bubble">{{ msg.content }}</div>
|
||||
</div>
|
||||
<div v-if="loading" class="chat-msg assistant">
|
||||
<div class="chat-bubble thinking">Thinking...</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="chat-input-area">
|
||||
<textarea
|
||||
v-model="input"
|
||||
@keydown="onKeydown"
|
||||
placeholder="Type a message... (Shift+Enter for newline)"
|
||||
rows="2"
|
||||
:disabled="loading"
|
||||
/>
|
||||
<button class="chat-send" @click="send" :disabled="!input.trim() || loading">Send</button>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.chat-panel {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
.chat-header {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
padding: 12px 16px;
|
||||
border-bottom: 1px solid var(--border);
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.chat-title {
|
||||
font-weight: 600;
|
||||
font-size: 14px;
|
||||
}
|
||||
|
||||
.chat-clear {
|
||||
background: none;
|
||||
color: var(--text-secondary);
|
||||
font-size: 12px;
|
||||
padding: 4px 8px;
|
||||
}
|
||||
|
||||
.chat-clear:hover:not(:disabled) {
|
||||
color: var(--error);
|
||||
}
|
||||
|
||||
.chat-messages {
|
||||
flex: 1;
|
||||
overflow-y: auto;
|
||||
padding: 12px 16px;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.chat-empty {
|
||||
flex: 1;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
color: var(--text-secondary);
|
||||
font-size: 13px;
|
||||
}
|
||||
|
||||
.chat-msg {
|
||||
display: flex;
|
||||
}
|
||||
|
||||
.chat-msg.user {
|
||||
justify-content: flex-end;
|
||||
}
|
||||
|
||||
.chat-msg.assistant {
|
||||
justify-content: flex-start;
|
||||
}
|
||||
|
||||
.chat-bubble {
|
||||
max-width: 85%;
|
||||
padding: 8px 12px;
|
||||
border-radius: 10px;
|
||||
font-size: 13px;
|
||||
line-height: 1.5;
|
||||
white-space: pre-wrap;
|
||||
word-break: break-word;
|
||||
}
|
||||
|
||||
.chat-msg.user .chat-bubble {
|
||||
background: var(--bg-tertiary);
|
||||
color: var(--text-primary);
|
||||
}
|
||||
|
||||
.chat-msg.assistant .chat-bubble {
|
||||
background: var(--bg-card);
|
||||
border: 1px solid var(--border);
|
||||
color: var(--text-primary);
|
||||
}
|
||||
|
||||
.chat-bubble.thinking {
|
||||
color: var(--text-secondary);
|
||||
font-style: italic;
|
||||
}
|
||||
|
||||
.chat-input-area {
|
||||
display: flex;
|
||||
gap: 8px;
|
||||
padding: 12px 16px;
|
||||
border-top: 1px solid var(--border);
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.chat-input-area textarea {
|
||||
flex: 1;
|
||||
resize: none;
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 6px;
|
||||
padding: 8px 10px;
|
||||
font-size: 13px;
|
||||
background: var(--bg-primary);
|
||||
color: var(--text-primary);
|
||||
}
|
||||
|
||||
.chat-input-area textarea:focus {
|
||||
outline: none;
|
||||
border-color: var(--accent);
|
||||
}
|
||||
|
||||
.chat-send {
|
||||
background: var(--accent);
|
||||
color: #fff;
|
||||
font-size: 13px;
|
||||
padding: 8px 14px;
|
||||
align-self: flex-end;
|
||||
}
|
||||
|
||||
.chat-send:hover:not(:disabled) {
|
||||
background: var(--accent-hover);
|
||||
}
|
||||
|
||||
.chat-send:disabled {
|
||||
opacity: 0.5;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
</style>
|
||||
@@ -1,20 +1,30 @@
|
||||
<script setup lang="ts">
|
||||
import { ref, onMounted } from 'vue'
|
||||
import { api } from '../api'
|
||||
import examples from '../examples.json'
|
||||
|
||||
const emit = defineEmits<{
|
||||
submit: [requirement: string]
|
||||
submit: [requirement: string, templateId?: string]
|
||||
cancel: []
|
||||
}>()
|
||||
|
||||
const requirement = ref('')
|
||||
const inputEl = ref<HTMLTextAreaElement>()
|
||||
const templates = ref<{ id: string; name: string; description: string }[]>([])
|
||||
const selectedTemplate = ref('')
|
||||
|
||||
onMounted(() => inputEl.value?.focus())
|
||||
onMounted(async () => {
|
||||
inputEl.value?.focus()
|
||||
try {
|
||||
templates.value = await api.listTemplates()
|
||||
} catch {
|
||||
// ignore — templates dropdown just won't show
|
||||
}
|
||||
})
|
||||
|
||||
function onSubmit() {
|
||||
const text = requirement.value.trim()
|
||||
if (text) emit('submit', text)
|
||||
if (text) emit('submit', text, selectedTemplate.value || undefined)
|
||||
}
|
||||
</script>
|
||||
|
||||
@@ -38,6 +48,13 @@ function onSubmit() {
|
||||
@keydown.ctrl.enter="onSubmit"
|
||||
@keydown.meta.enter="onSubmit"
|
||||
/>
|
||||
<div v-if="templates.length" class="template-select">
|
||||
<label>模板</label>
|
||||
<select v-model="selectedTemplate">
|
||||
<option value="">自动选择</option>
|
||||
<option v-for="t in templates" :key="t.id" :value="t.id">{{ t.name }}</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="create-hint">Ctrl+Enter 提交</div>
|
||||
<div class="create-actions">
|
||||
<button class="btn-cancel" @click="emit('cancel')">取消</button>
|
||||
@@ -100,6 +117,34 @@ function onSubmit() {
|
||||
border-color: var(--accent);
|
||||
}
|
||||
|
||||
.template-select {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.template-select label {
|
||||
font-size: 13px;
|
||||
color: var(--text-secondary);
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.template-select select {
|
||||
flex: 1;
|
||||
padding: 6px 8px;
|
||||
background: var(--bg-secondary);
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 6px;
|
||||
color: var(--text-primary);
|
||||
font-size: 13px;
|
||||
font-family: inherit;
|
||||
outline: none;
|
||||
}
|
||||
|
||||
.template-select select:focus {
|
||||
border-color: var(--accent);
|
||||
}
|
||||
|
||||
.create-hint {
|
||||
font-size: 12px;
|
||||
color: var(--text-secondary);
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
<script setup lang="ts">
|
||||
import { ref } from 'vue'
|
||||
import type { Project, KbArticleSummary } from '../types'
|
||||
|
||||
const props = defineProps<{
|
||||
@@ -9,7 +8,6 @@ const props = defineProps<{
|
||||
objMode: boolean
|
||||
kbArticles: KbArticleSummary[]
|
||||
selectedArticleId: string
|
||||
appTitle?: string
|
||||
}>()
|
||||
|
||||
const emit = defineEmits<{
|
||||
@@ -23,26 +21,8 @@ const emit = defineEmits<{
|
||||
selectArticle: [id: string]
|
||||
createArticle: []
|
||||
deleteArticle: [id: string]
|
||||
updateAppTitle: [title: string]
|
||||
}>()
|
||||
|
||||
const showSettings = ref(false)
|
||||
const editingTitle = ref(false)
|
||||
const titleInput = ref('')
|
||||
|
||||
function onEditTitle() {
|
||||
titleInput.value = props.appTitle || 'Tori'
|
||||
editingTitle.value = true
|
||||
}
|
||||
|
||||
function onSaveTitle() {
|
||||
const val = titleInput.value.trim()
|
||||
if (val && val !== props.appTitle) {
|
||||
emit('updateAppTitle', val)
|
||||
}
|
||||
editingTitle.value = false
|
||||
}
|
||||
|
||||
function onDelete(e: Event, id: string) {
|
||||
e.stopPropagation()
|
||||
if (confirm('确定删除这个项目?')) {
|
||||
@@ -56,16 +36,6 @@ function onDeleteArticle(e: Event, id: string) {
|
||||
emit('deleteArticle', id)
|
||||
}
|
||||
}
|
||||
|
||||
function onOpenKb() {
|
||||
showSettings.value = false
|
||||
emit('openKb')
|
||||
}
|
||||
|
||||
function onOpenObj() {
|
||||
showSettings.value = false
|
||||
emit('openObj')
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
@@ -74,7 +44,6 @@ function onOpenObj() {
|
||||
<template v-if="objMode">
|
||||
<div class="sidebar-header">
|
||||
<button class="btn-back" @click="emit('closeObj')">← Back</button>
|
||||
<h1 class="logo">Object Storage</h1>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
@@ -82,7 +51,6 @@ function onOpenObj() {
|
||||
<template v-else-if="kbMode">
|
||||
<div class="sidebar-header">
|
||||
<button class="btn-back" @click="emit('closeKb')">← Back</button>
|
||||
<h1 class="logo">Knowledge Base</h1>
|
||||
<button class="btn-new" @click="emit('createArticle')">+ 新文章</button>
|
||||
</div>
|
||||
<nav class="project-list">
|
||||
@@ -106,7 +74,6 @@ function onOpenObj() {
|
||||
<!-- Normal Mode -->
|
||||
<template v-else>
|
||||
<div class="sidebar-header">
|
||||
<h1 class="logo">{{ props.appTitle || 'Tori' }}</h1>
|
||||
<button class="btn-new" @click="emit('create')">+ 新项目</button>
|
||||
</div>
|
||||
<nav class="project-list">
|
||||
@@ -124,29 +91,6 @@ function onOpenObj() {
|
||||
<span class="project-time">{{ new Date(project.updated_at).toLocaleDateString() }}</span>
|
||||
</div>
|
||||
</nav>
|
||||
<div class="sidebar-footer">
|
||||
<div class="settings-wrapper">
|
||||
<button class="btn-settings" @click="showSettings = !showSettings">Settings</button>
|
||||
<div v-if="showSettings" class="settings-menu">
|
||||
<div class="settings-item-row" v-if="!editingTitle">
|
||||
<span class="settings-label">App Title</span>
|
||||
<button class="settings-value" @click="onEditTitle">{{ props.appTitle || 'Tori' }}</button>
|
||||
</div>
|
||||
<div class="settings-item-row" v-else>
|
||||
<input
|
||||
class="settings-input"
|
||||
v-model="titleInput"
|
||||
@keyup.enter="onSaveTitle"
|
||||
@keyup.escape="editingTitle = false"
|
||||
@vue:mounted="($event: any) => $event.el.focus()"
|
||||
/>
|
||||
<button class="settings-save" @click="onSaveTitle">OK</button>
|
||||
</div>
|
||||
<button class="settings-item" @click="onOpenKb">Knowledge Base</button>
|
||||
<button class="settings-item" @click="onOpenObj">Object Storage</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
</aside>
|
||||
</template>
|
||||
@@ -167,13 +111,6 @@ function onOpenObj() {
|
||||
border-bottom: 1px solid var(--border);
|
||||
}
|
||||
|
||||
.logo {
|
||||
font-size: 20px;
|
||||
font-weight: 700;
|
||||
color: var(--accent);
|
||||
margin-bottom: 12px;
|
||||
}
|
||||
|
||||
.btn-back {
|
||||
width: 100%;
|
||||
padding: 6px 8px;
|
||||
@@ -285,113 +222,4 @@ function onOpenObj() {
|
||||
color: var(--text-secondary);
|
||||
font-size: 13px;
|
||||
}
|
||||
|
||||
.sidebar-footer {
|
||||
padding: 12px 16px;
|
||||
border-top: 1px solid var(--border);
|
||||
}
|
||||
|
||||
.settings-wrapper {
|
||||
position: relative;
|
||||
}
|
||||
|
||||
.btn-settings {
|
||||
width: 100%;
|
||||
padding: 8px;
|
||||
background: transparent;
|
||||
color: var(--text-secondary);
|
||||
border: none;
|
||||
font-size: 13px;
|
||||
cursor: pointer;
|
||||
text-align: left;
|
||||
border-radius: 6px;
|
||||
}
|
||||
|
||||
.btn-settings:hover {
|
||||
background: var(--bg-tertiary);
|
||||
color: var(--text-primary);
|
||||
}
|
||||
|
||||
.settings-menu {
|
||||
position: absolute;
|
||||
bottom: 100%;
|
||||
left: 0;
|
||||
right: 0;
|
||||
margin-bottom: 4px;
|
||||
background: var(--bg-primary);
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 8px;
|
||||
padding: 4px;
|
||||
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
|
||||
}
|
||||
|
||||
.settings-item {
|
||||
width: 100%;
|
||||
padding: 8px 12px;
|
||||
background: none;
|
||||
border: none;
|
||||
color: var(--text-primary);
|
||||
font-size: 13px;
|
||||
text-align: left;
|
||||
cursor: pointer;
|
||||
border-radius: 6px;
|
||||
}
|
||||
|
||||
.settings-item:hover {
|
||||
background: var(--bg-tertiary);
|
||||
}
|
||||
|
||||
.settings-item-row {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
padding: 6px 12px;
|
||||
}
|
||||
|
||||
.settings-label {
|
||||
font-size: 12px;
|
||||
color: var(--text-secondary);
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.settings-value {
|
||||
flex: 1;
|
||||
text-align: right;
|
||||
background: none;
|
||||
border: none;
|
||||
color: var(--text-primary);
|
||||
font-size: 13px;
|
||||
cursor: pointer;
|
||||
padding: 2px 4px;
|
||||
border-radius: 4px;
|
||||
}
|
||||
|
||||
.settings-value:hover {
|
||||
background: var(--bg-tertiary);
|
||||
}
|
||||
|
||||
.settings-input {
|
||||
flex: 1;
|
||||
padding: 4px 8px;
|
||||
font-size: 13px;
|
||||
background: var(--bg-tertiary);
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 4px;
|
||||
color: var(--text-primary);
|
||||
outline: none;
|
||||
}
|
||||
|
||||
.settings-input:focus {
|
||||
border-color: var(--accent);
|
||||
}
|
||||
|
||||
.settings-save {
|
||||
padding: 4px 8px;
|
||||
font-size: 12px;
|
||||
background: var(--accent);
|
||||
color: #fff;
|
||||
border: none;
|
||||
border-radius: 4px;
|
||||
cursor: pointer;
|
||||
}
|
||||
</style>
|
||||
|
||||
@@ -5,7 +5,9 @@
|
||||
}
|
||||
|
||||
:root {
|
||||
--header-height: 44px;
|
||||
--sidebar-width: 240px;
|
||||
--chat-sidebar-width: 360px;
|
||||
--bg-primary: #ffffff;
|
||||
--bg-secondary: #f7f8fa;
|
||||
--bg-tertiary: #eef0f4;
|
||||
|
||||
@@ -64,6 +64,11 @@ export interface Timer {
|
||||
created_at: string
|
||||
}
|
||||
|
||||
export interface ChatMessage {
|
||||
role: 'user' | 'assistant'
|
||||
content: string
|
||||
}
|
||||
|
||||
export interface LlmCallLogEntry {
|
||||
id: string
|
||||
workflow_id: string
|
||||
|
||||
Reference in New Issue
Block a user