42 lines
1.4 KiB
C++
42 lines
1.4 KiB
C++
#include "cap_mention.h"
|
|||
|
|
|
||
|
|
namespace detz::cap {
|
||
|
|
|
||
|
|
CapMention::~CapMention() { closeAll(); }
|
||
|
|
|
||
|
|
void CapMention::closeAll() {
|
||
|
|
for (auto& s : sessions_) {
|
||
|
|
try { s.second->close(); } catch (...) {}
|
||
|
|
}
|
||
|
|
sessions_.clear();
|
||
|
|
}
|
||
|
|
|
||
|
|
std::string CapMention::ask(const CapSelection& sel, const std::string& prompt, const std::string& cwd) {
|
||
|
|
const std::string key = sel.agent + "\n" + sel.model + "\n" + cwd;
|
||
|
|
const std::string who = sel.agent + (sel.model.empty() ? "" : ":" + sel.model);
|
||
|
|
try {
|
||
|
|
auto it = sessions_.find(key);
|
||
|
|
if (it == sessions_.end()) {
|
||
|
|
CapManifest manifest = catalog_.manifest(sel.agent);
|
||
|
|
const std::string urn = "cap://" + sel.agent + "-1";
|
||
|
|
auto session = std::make_unique<CapSession>(urn, CapDriver::create(manifest, urn, cwd, sel.model));
|
||
|
|
session->start();
|
||
|
|
it = sessions_.emplace(key, std::move(session)).first;
|
||
|
|
}
|
||
|
|
CapTurnResult result = it->second->ask(prompt);
|
||
|
|
if (result.state() != "done" && result.text().empty()) {
|
||
|
|
sessions_.erase(key);
|
||
|
|
return "cap agent " + who + " ended the turn in state '" + result.state() + "' without an answer";
|
||
|
|
}
|
||
|
|
return result.text();
|
||
|
|
} catch (const std::exception& exc) {
|
||
|
|
sessions_.erase(key);
|
||
|
|
return "cap agent " + who + " failed: " + exc.what() + "\n" + catalog_.report();
|
||
|
|
} catch (...) {
|
||
|
|
sessions_.erase(key);
|
||
|
|
return "cap agent " + who + " failed with an unknown error";
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
} // namespace detz::cap
|