detz: M4 shell + M5 llm

This commit is contained in:
2026-10-24 06:42:23 +02:00
parent fb9245288c
commit a245e0de93
10 changed files with 322 additions and 20 deletions
+58 -1
View File
@@ -1,12 +1,69 @@
#include "../src/cli/cli.h"
#include "../src/shell/sandbox.h"
#include "../src/shell/session.h"
#include "../src/shell/pty.h"
#include "../src/shell/spinner.h"
#include "../src/shell/livestream.h"
#include "../src/shell/lanes.h"
#include "../src/llm/llm.h"
#include <cassert>
#include <cstdio>
using detz::llm::ChatClient;
using detz::llm::ChatRequest;
int main() {
detz::cli::Parser p;
const char* a0 = "detz";
auto c = p.parse(1, (char**)&a0);
assert(c.name == "--help");
assert(c.run() == 0);
std::puts("M0 OK");
// shell
detz::shell::Sandbox sb("/tmp", {"echo"});
auto r = sb.run("echo hi");
assert(r.code == 0 && r.out.find("hi") != std::string::npos);
auto denied = sb.run("rm -rf /");
assert(denied.code == 126);
assert(!detz::shell::curl_hint("http://x").empty());
detz::shell::ShellSession sh;
auto e = sh.exec("echo ok");
assert(e.out.find("ok") != std::string::npos);
assert(sh.target_paths("a:b").size() == 2);
assert(sh.segment_targets({"a"}).size() == 1);
detz::shell::PtyProc pty;
assert(pty.spawn("echo pty") && pty.read_all().find("pty") != std::string::npos);
detz::shell::Spinner sp("x"); sp.tick(); sp.done(); assert(sp.ticks() == 2);
detz::shell::LiveStream ls; ls.push("a"); ls.push("b");
assert(ls.joined() == "ab");
int n = 0; ls.drain([&](const std::string&) { ++n; return true; }); assert(n == 2);
detz::shell::LaneBoard lb; lb.set("a", "1"); lb.set("a", "2");
assert(lb.size() == 1 && lb.render().find("2") != std::string::npos);
assert(!detz::shell::Glow::highlight("t").empty());
assert(detz::shell::speak_text("hi") == "hi");
// llm mocked transport
auto mock = [](const std::string&, const std::string&, const std::map<std::string,std::string>&) {
return std::string("{\"content\":\"hello\"}");
};
ChatClient cc("openai", "m", mock);
auto cr = cc.chat(ChatRequest{"m", {{"user", "hi"}}, false});
assert(cr.text == "hello");
auto smock = [](const std::string&, const std::string&, const std::map<std::string,std::string>&) {
return std::string("data: {\"content\":\"a\"}\n\ndata: [DONE]\n\n");
};
ChatClient cs("anthropic", "m", smock);
std::vector<std::string> got;
cs.chat_stream(ChatRequest{"m", {}, true}, [&](const std::string& d) { got.push_back(d); return true; });
assert(got.size() == 1);
bool threw = false;
try { ChatClient bad("openai", "m"); bad.chat(ChatRequest{}); } catch (const detz::llm::BackendError&) { threw = true; }
assert(threw);
detz::llm::ModelHealth h; h.record("a", false, 5); h.record("b", true, 9);
assert(h.size() == 2 && h.best()->model == "b");
assert(detz::llm::provider_names().find("openrouter") != std::string::npos);
assert(detz::llm::provider_info("xai").base_url.find("x.ai") != std::string::npos);
detz::llm::Opencode oc("http://localhost:9", mock);
assert(oc.complete("m", {}).text == "hello");
bool be = false;
try { detz::llm::Opencode no; no.complete("m", {}); } catch (const detz::llm::BackendError&) { be = true; }
assert(be);
std::puts("M4+M5 OK");
return 0;
}