moving
Some checks failed
Installer Smoke / installer-smoke (push) Has been cancelled

This commit is contained in:
Oleg Maslov
2026-09-02 10:10:29 +02:00
commit 0c3e2ead3b
3841 changed files with 970576 additions and 0 deletions

View File

@@ -0,0 +1,268 @@
// CC Sesija A §2.1 Task A3 — agent loop streaming Tauri command.
//
// Brief: briefs/2026-04-30-cc-sesija-A-waggle-apps-web-integration.md §2.1 Task A3
//
// Pattern: command returns a request_id immediately; tokio task POSTs to the
// sidecar's `/api/chat` SSE endpoint, parses each event block, and emits
// `agent-stream-{request_id}` to the webview per chunk. End-of-stream emits
// `agent-stream-{request_id}-end` with either `{ ok: true }` or `{ error, ... }`.
//
// The webview's tauri-bindings.runAgentQuery() returns the request_id + an
// unlisten handle so React components can subscribe per-conversation without
// global state.
//
// A3.1 follow-up tracking:
// - sidecar `/api/chat` calls `runAgentLoop`, not `runRetrievalAgentLoop`
// as the brief requested. Faza 1's runRetrievalAgentLoop with shape
// selection is not yet wired into the chat path. The `shape` param is
// accepted here and passed through the body so a future sidecar patch
// can read it without changing this command's surface. Document in
// A3.1 follow-up.
use serde_json::{json, Value};
use std::time::Duration;
use tauri::{AppHandle, Emitter, State};
use uuid::Uuid;
use crate::service::ServiceState;
const STREAM_TIMEOUT_SECS: u64 = 300;
/// Start an agent query. Returns a request_id; chunks arrive via the
/// `agent-stream-{request_id}` Tauri event, end via `agent-stream-{request_id}-end`.
#[tauri::command]
pub async fn run_agent_query(
app: AppHandle,
state: State<'_, ServiceState>,
query: String,
shape: Option<String>,
workspace_id: Option<String>,
persona: Option<String>,
model: Option<String>,
session: Option<String>,
) -> Result<String, String> {
let request_id = format!("agent-{}", Uuid::new_v4());
let port = state.port;
let app_clone = app.clone();
let req_id_clone = request_id.clone();
tokio::spawn(async move {
if let Err(e) = stream_chat(
app_clone,
port,
req_id_clone,
query,
shape,
workspace_id,
persona,
model,
session,
)
.await
{
// The end event is already emitted from inside stream_chat on error
// paths; this stderr is a developer-facing breadcrumb only.
eprintln!("[agent] run_agent_query stream task error: {}", e);
}
});
Ok(request_id)
}
#[allow(clippy::too_many_arguments)]
async fn stream_chat(
app: AppHandle,
port: u16,
request_id: String,
query: String,
shape: Option<String>,
workspace_id: Option<String>,
persona: Option<String>,
model: Option<String>,
session: Option<String>,
) -> Result<(), String> {
// A3.1 (2026-04-30): re-pointed from /api/chat to /api/agent/run.
// /api/agent/run is the dedicated shape-aware structured-retrieval
// endpoint (runRetrievalAgentLoop) — distinct from /api/chat which is
// for conversational multi-turn dialogue (runAgentLoop). Shape now flows
// end-to-end: Tauri body field "shape" → sidecar promptShapeOverride
// → runRetrievalAgentLoop pickShape().
//
// The TS binding's `session` arg is intentionally ignored here:
// /api/agent/run is one-shot (no persistent session), so passing
// session through would just be dead weight. Binding signature stays
// stable so callers don't break.
let url = format!("http://127.0.0.1:{}/api/agent/run", port);
let mut body = json!({ "question": query });
if let Some(ws) = workspace_id {
body["workspace"] = json!(ws);
}
if let Some(p) = persona {
body["persona"] = json!(p);
}
if let Some(m) = model {
body["model"] = json!(m);
}
if let Some(sh) = shape {
body["shape"] = json!(sh);
}
// `session` arg accepted by the Tauri command for binding-stability but
// not threaded into /api/agent/run (one-shot). Suppress unused-warn.
let _ = session;
let event_name = format!("agent-stream-{}", request_id);
let end_event = format!("agent-stream-{}-end", request_id);
let client = match reqwest::Client::builder()
.timeout(Duration::from_secs(STREAM_TIMEOUT_SECS))
.build()
{
Ok(c) => c,
Err(e) => {
let _ = app.emit(
&end_event,
json!({ "error": format!("client build failed: {}", e) }),
);
return Err(e.to_string());
}
};
let mut resp = match client.post(&url).json(&body).send().await {
Ok(r) => r,
Err(e) => {
let _ = app.emit(
&end_event,
json!({ "error": format!("HTTP POST failed: {}", e) }),
);
return Err(e.to_string());
}
};
if !resp.status().is_success() {
let status = resp.status();
let body_text = resp.text().await.unwrap_or_default();
let _ = app.emit(
&end_event,
json!({
"error": format!("sidecar returned {}", status),
"body": body_text,
}),
);
return Err(format!("sidecar returned {}", status));
}
// Read SSE stream chunk by chunk. SSE events are delimited by a blank
// line (\n\n). Each block has zero or one `event:` line and one or more
// `data:` lines. We accumulate bytes into a String buffer, then drain
// complete blocks one at a time.
let mut buffer = String::new();
loop {
match resp.chunk().await {
Ok(Some(bytes)) => {
if let Ok(s) = std::str::from_utf8(&bytes) {
buffer.push_str(s);
}
while let Some(idx) = buffer.find("\n\n") {
let block: String = buffer.drain(..idx + 2).collect();
if let Some(parsed) = parse_sse_event(&block) {
let _ = app.emit(&event_name, parsed);
}
}
}
Ok(None) => break,
Err(e) => {
let _ = app.emit(
&end_event,
json!({ "error": format!("stream read error: {}", e) }),
);
return Err(e.to_string());
}
}
}
// Drain any tail block that didn't end with a blank line (server may close
// the connection without a final separator).
let tail = buffer.trim();
if !tail.is_empty() {
if let Some(parsed) = parse_sse_event(tail) {
let _ = app.emit(&event_name, parsed);
}
}
let _ = app.emit(&end_event, json!({ "ok": true }));
Ok(())
}
/// Parse a single SSE event block. Returns `{ event, data }` where `data` is the
/// parsed JSON value when possible, otherwise the raw concatenated data text.
/// SSE allows multiple `data:` lines within a single event — they are joined
/// with `\n` per the spec.
fn parse_sse_event(block: &str) -> Option<Value> {
let mut event_name = String::from("message");
let mut data_lines: Vec<String> = Vec::new();
for line in block.lines() {
if let Some(rest) = line.strip_prefix("event:") {
event_name = rest.trim().to_string();
} else if let Some(rest) = line.strip_prefix("data:") {
// Per SSE spec, a single leading space after `data:` is stripped.
let value = rest.strip_prefix(' ').unwrap_or(rest);
data_lines.push(value.to_string());
}
// `id:` and `retry:` and comment (`:`) lines are intentionally ignored.
}
if data_lines.is_empty() {
return None;
}
let data_str = data_lines.join("\n");
let data_value: Value =
serde_json::from_str(&data_str).unwrap_or_else(|_| Value::String(data_str));
Some(json!({
"event": event_name,
"data": data_value,
}))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_sse_simple_data_only() {
let block = "data: hello\n\n";
let parsed = parse_sse_event(block).expect("should parse");
assert_eq!(parsed["event"], "message");
assert_eq!(parsed["data"], "hello");
}
#[test]
fn parse_sse_event_and_json_data() {
let block = "event: step\ndata: {\"chunk\":\"hi\"}\n\n";
let parsed = parse_sse_event(block).expect("should parse");
assert_eq!(parsed["event"], "step");
assert_eq!(parsed["data"]["chunk"], "hi");
}
#[test]
fn parse_sse_multiline_data_joined_with_newline() {
let block = "event: step\ndata: line1\ndata: line2\n\n";
let parsed = parse_sse_event(block).expect("should parse");
assert_eq!(parsed["data"], "line1\nline2");
}
#[test]
fn parse_sse_empty_data_returns_none() {
let block = "event: ping\n\n";
assert!(parse_sse_event(block).is_none());
}
#[test]
fn parse_sse_strips_single_leading_space_after_colon() {
let block = "data: foo\n\n";
let parsed = parse_sse_event(block).expect("should parse");
assert_eq!(parsed["data"], "foo");
}
}

View File

@@ -0,0 +1,43 @@
// CC Sesija A §2.1 Task A4 — shared HTTP helpers for sidecar-proxy commands.
//
// Extracted from commands/memory.rs at A4 because wiki.rs becomes the second
// consumer (rule of two). Future command modules (onboarding A10+, agent.rs
// where applicable) should also import from here rather than reimplementing.
use serde_json::Value;
const SIDECAR_HOST: &str = "127.0.0.1";
pub fn sidecar_url(port: u16, path: &str) -> String {
format!("http://{}:{}{}", SIDECAR_HOST, port, path)
}
pub async fn http_get(url: &str) -> Result<reqwest::Response, String> {
reqwest::get(url)
.await
.map_err(|e| format!("HTTP GET {} failed: {}", url, e))
}
pub async fn http_post(url: &str, body: &Value) -> Result<reqwest::Response, String> {
let client = reqwest::Client::new();
client
.post(url)
.json(body)
.send()
.await
.map_err(|e| format!("HTTP POST {} failed: {}", url, e))
}
pub async fn parse_json(resp: reqwest::Response) -> Result<Value, String> {
let status = resp.status();
if !status.is_success() {
let body_text = resp
.text()
.await
.unwrap_or_else(|_| "<unreadable body>".to_string());
return Err(format!("sidecar returned {}: {}", status, body_text));
}
resp.json::<Value>()
.await
.map_err(|e| format!("sidecar returned non-JSON body: {}", e))
}

View File

@@ -0,0 +1,137 @@
// CC Sesija A §2.1 Tasks A1 + A4 — memory + KG + identity Tauri commands.
//
// Architecture: thin HTTP proxies to local sidecar (Fastify, port from ServiceState).
// Tauri Rust shell intentionally has no direct sqlite/sqlite-vec dependencies — all
// substrate access goes through the sidecar so that the storage layer (FrameStore,
// HybridSearch, KnowledgeGraph, wiki-compiler) lives in one process.
//
// Command surface (4 commands here; wiki commands moved to commands/wiki.rs at A4):
// recall_memory → GET /api/memory/search
// save_memory → POST /api/memory/frames
// search_entities → GET /api/memory/graph
// get_identity → GET /api/identity (route not yet in sidecar — graceful 404
// → placeholder; A1.1 follow-up adds route)
//
// Shared HTTP helpers extracted to commands/http.rs at A4 (rule of two — wiki.rs is
// the second consumer).
use serde_json::{json, Value};
use tauri::State;
use crate::commands::http::{http_get, http_post, parse_json, sidecar_url};
use crate::service::ServiceState;
/// Recall memory frames matching `query`. Optional `scope` (all|personal|workspace),
/// `limit` (default sidecar-side ~20), `workspace_id` (workspace mind to search).
#[tauri::command]
pub async fn recall_memory(
state: State<'_, ServiceState>,
query: String,
scope: Option<String>,
limit: Option<u32>,
workspace_id: Option<String>,
) -> Result<Value, String> {
let mut url = format!(
"{}?q={}",
sidecar_url(state.port, "/api/memory/search"),
urlencoding::encode(&query)
);
if let Some(s) = scope {
url.push_str(&format!("&scope={}", urlencoding::encode(&s)));
}
if let Some(l) = limit {
url.push_str(&format!("&limit={}", l));
}
if let Some(ws) = workspace_id {
url.push_str(&format!("&workspace={}", urlencoding::encode(&ws)));
}
let resp = http_get(&url).await?;
parse_json(resp).await
}
/// Persist a new memory frame. `content` is required. Optional `workspace_id`,
/// `importance` (low|normal|high|critical per @waggle/core Importance enum), `source`
/// (one of: user_stated, tool_verified, agent_inferred, import, system).
#[tauri::command]
pub async fn save_memory(
state: State<'_, ServiceState>,
content: String,
workspace_id: Option<String>,
importance: Option<String>,
source: Option<String>,
) -> Result<Value, String> {
let mut body = json!({ "content": content });
if let Some(ws) = workspace_id {
body["workspace"] = json!(ws);
}
if let Some(imp) = importance {
body["importance"] = json!(imp);
}
if let Some(src) = source {
body["source"] = json!(src);
}
let url = sidecar_url(state.port, "/api/memory/frames");
let resp = http_post(&url, &body).await?;
parse_json(resp).await
}
/// Search the knowledge graph for entities/relations. Returns `{nodes, edges}` shape
/// from sidecar's KnowledgeGraph layer. `scope` defaults to "all" if not provided.
#[tauri::command]
pub async fn search_entities(
state: State<'_, ServiceState>,
workspace_id: Option<String>,
scope: Option<String>,
) -> Result<Value, String> {
let mut url = sidecar_url(state.port, "/api/memory/graph").to_string();
let mut params: Vec<String> = Vec::new();
if let Some(ws) = workspace_id {
params.push(format!("workspace={}", urlencoding::encode(&ws)));
}
if let Some(s) = scope {
params.push(format!("scope={}", urlencoding::encode(&s)));
}
if !params.is_empty() {
url.push('?');
url.push_str(&params.join("&"));
}
let resp = http_get(&url).await?;
parse_json(resp).await
}
/// Get the user identity record from sidecar's IdentityLayer.
///
/// Backed by /api/identity sidecar route (A1.1 shipped 2026-04-30). The route
/// always returns a 200 with either configured: true + IdentityLayer fields,
/// or configured: false + null fields + optional _note. The 404 + transport-
/// error fallbacks here remain as defense-in-depth — they were the original
/// pre-A1.1 placeholders and now only fire on hard sidecar outages.
#[tauri::command]
pub async fn get_identity(state: State<'_, ServiceState>) -> Result<Value, String> {
let url = sidecar_url(state.port, "/api/identity");
match http_get(&url).await {
Ok(resp) if resp.status().as_u16() == 404 => Ok(identity_placeholder(
"sidecar route 404 (unexpected post-A1.1)",
)),
Ok(resp) => parse_json(resp).await,
Err(_) => Ok(identity_placeholder("sidecar unreachable")),
}
}
fn identity_placeholder(note: &str) -> Value {
json!({
"configured": false,
"name": null,
"role": null,
"department": null,
"personality": null,
"capabilities": null,
"system_prompt": null,
"created_at": null,
"updated_at": null,
"_note": note
})
}

View File

@@ -0,0 +1,8 @@
// CC Sesija A §2.1 + §2.3: command modules grouped under commands/.
// Each submodule exposes #[tauri::command] async fns wired in lib.rs invoke_handler.
pub mod agent;
pub mod http;
pub mod memory;
pub mod onboarding;
pub mod wiki;

View File

@@ -0,0 +1,120 @@
// CC Sesija A §2.3 Task A10 — first-launch detection via filesystem flag.
//
// Brief: briefs/2026-04-30-cc-sesija-A-waggle-apps-web-integration.md §2.3 Task A10
//
// Persists a flag file at `~/.waggle/first-launch.flag` so the onboarding wizard
// state survives across app reinstalls (browser localStorage doesn't, since
// Tauri builds may use a fresh WebView profile per install). The web `npm run
// dev` path continues to use localStorage via useOnboarding — these commands
// are the durable Tauri-mode addition, not a replacement.
//
// Cross-platform user-home resolution uses std::env (USERPROFILE on Windows,
// HOME on Unix) to avoid pulling in a new dirs/home crate dep.
use std::path::PathBuf;
const FLAG_DIR: &str = ".waggle";
const FLAG_FILE: &str = "first-launch.flag";
fn home_dir() -> Option<PathBuf> {
std::env::var_os("USERPROFILE")
.or_else(|| std::env::var_os("HOME"))
.map(PathBuf::from)
}
/// Resolve the flag location: `WAGGLE_DATA_DIR` (when set and non-empty) else
/// `~/.waggle` — the SAME resolution order the sidecar's resolveDataDir uses
/// (UX-Refactor P4/D11). Without this, a custom-data-dir install reads/writes
/// the flag in `~/.waggle` while the server's completion stamp lives in the
/// data dir — and a stale `~/.waggle` flag from a prior default install would
/// auto-skip onboarding against a brand-new data dir (P4 review, 5 findings).
fn flag_path() -> Result<PathBuf, String> {
if let Some(dir) = std::env::var_os("WAGGLE_DATA_DIR") {
if !dir.is_empty() {
return Ok(PathBuf::from(dir).join(FLAG_FILE));
}
}
let home = home_dir().ok_or_else(|| {
"could not resolve user home directory (USERPROFILE/HOME unset)".to_string()
})?;
Ok(home.join(FLAG_DIR).join(FLAG_FILE))
}
/// Returns true if the user has not yet completed onboarding.
/// Implementation: returns `!flag_file_exists`. On any IO error (e.g. home dir
/// unresolvable in a sandboxed environment) returns `true` so the wizard runs
/// — better to show the wizard once too often than to silently skip it.
#[tauri::command]
pub async fn is_first_launch() -> Result<bool, String> {
let path = match flag_path() {
Ok(p) => p,
Err(_) => return Ok(true),
};
Ok(!path.exists())
}
/// Marks onboarding as complete by creating the flag file. Idempotent.
/// Creates the parent `~/.waggle/` directory if needed.
#[tauri::command]
pub async fn mark_first_launch_complete() -> Result<(), String> {
let path = flag_path()?;
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent)
.map_err(|e| format!("create_dir_all {} failed: {}", parent.display(), e))?;
}
std::fs::write(&path, b"completed\n")
.map_err(|e| format!("write {} failed: {}", path.display(), e))?;
Ok(())
}
/// Resets the first-launch flag (deletes the file). For dev / QA flows that
/// need to re-trigger onboarding without a full reinstall.
#[tauri::command]
pub async fn reset_first_launch() -> Result<(), String> {
let path = flag_path()?;
if path.exists() {
std::fs::remove_file(&path)
.map_err(|e| format!("remove {} failed: {}", path.display(), e))?;
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn flag_path_resolution_order() {
// ONE sequential test — env vars are process-global and cargo runs
// tests in parallel threads; split tests would race on WAGGLE_DATA_DIR.
let tmp = std::env::temp_dir().join(format!("waggle-test-home-{}", std::process::id()));
std::env::set_var("USERPROFILE", &tmp);
std::env::set_var("HOME", &tmp);
// Default: ~/.waggle.
std::env::remove_var("WAGGLE_DATA_DIR");
let path = flag_path().expect("flag_path resolves with USERPROFILE/HOME set");
assert!(
path.ends_with(".waggle/first-launch.flag")
|| path.ends_with(".waggle\\first-launch.flag")
);
// P4/D11: a custom data dir keeps the flag NEXT TO the server's stamp.
let data_dir =
std::env::temp_dir().join(format!("waggle-test-datadir-{}", std::process::id()));
std::env::set_var("WAGGLE_DATA_DIR", &data_dir);
let custom = flag_path().expect("flag_path resolves with WAGGLE_DATA_DIR set");
assert!(custom.starts_with(&data_dir));
assert!(custom.ends_with("first-launch.flag"));
// Empty env value falls through to the home default (matches resolveDataDir).
std::env::set_var("WAGGLE_DATA_DIR", "");
let fallback = flag_path().expect("flag_path resolves with empty WAGGLE_DATA_DIR");
assert!(
fallback.ends_with(".waggle/first-launch.flag")
|| fallback.ends_with(".waggle\\first-launch.flag")
);
std::env::remove_var("WAGGLE_DATA_DIR");
}
}

View File

@@ -0,0 +1,75 @@
// CC Sesija A §2.1 Task A4 — wiki Tauri commands.
//
// Brief: briefs/2026-04-30-cc-sesija-A-waggle-apps-web-integration.md §2.1 Task A4
//
// Read-side wiki commands proxying to the sidecar's /api/wiki/* routes which read
// from the per-workspace MindDB (FrameStore + KnowledgeGraph). The compile route
// triggers a fresh wiki-compiler pass for the active workspace.
//
// Brief §2.1 Task A4 says "reads packages/wiki-compiler output direktno iz hive-mind
// frame store" — direktno here means "without intermediate cache" not "direct
// sqlite from Rust" (Tauri shell intentionally has no sqlite deps; substrate stays
// in the sidecar process — see commands/memory.rs header for full rationale).
//
// Note: compile_wiki_section was previously colocated in commands/memory.rs (A1).
// A4 moves it here for cohesion. Tauri command surface is unchanged; lib.rs
// invoke_handler entry just rewires from `memory::compile_wiki_section` to
// `wiki::compile_wiki_section`.
use serde_json::{json, Value};
use tauri::State;
use crate::commands::http::{http_get, http_post, parse_json, sidecar_url};
use crate::service::ServiceState;
/// List all compiled wiki pages for the active workspace. Returns the page
/// index (slugs + titles + metadata); call get_wiki_page_content for the body.
#[tauri::command]
pub async fn get_wiki_pages(state: State<'_, ServiceState>) -> Result<Value, String> {
let url = sidecar_url(state.port, "/api/wiki/pages");
let resp = http_get(&url).await?;
parse_json(resp).await
}
/// Fetch a single wiki page's metadata (title, type, source frame ids, etc.)
/// without the full markdown body. Use get_wiki_page_content for the body.
#[tauri::command]
pub async fn get_wiki_page(state: State<'_, ServiceState>, slug: String) -> Result<Value, String> {
let url = sidecar_url(
state.port,
&format!("/api/wiki/pages/{}", urlencoding::encode(&slug)),
);
let resp = http_get(&url).await?;
parse_json(resp).await
}
/// Fetch the full markdown content for a wiki page by slug. Returns whatever
/// the sidecar emits (typically `{ slug, content, ... }`).
#[tauri::command]
pub async fn get_wiki_page_content(
state: State<'_, ServiceState>,
slug: String,
) -> Result<Value, String> {
let url = sidecar_url(
state.port,
&format!("/api/wiki/pages/{}/content", urlencoding::encode(&slug)),
);
let resp = http_get(&url).await?;
parse_json(resp).await
}
/// Trigger wiki compilation for the active or specified workspace. Returns the
/// compilation summary (page count, entities, gaps) from packages/wiki-compiler.
#[tauri::command]
pub async fn compile_wiki_section(
state: State<'_, ServiceState>,
workspace_id: Option<String>,
) -> Result<Value, String> {
let mut body = json!({});
if let Some(ws) = workspace_id {
body["workspace"] = json!(ws);
}
let url = sidecar_url(state.port, "/api/wiki/compile");
let resp = http_post(&url, &body).await?;
parse_json(resp).await
}