gui: track agents by name, not by index

- setActiveSessionId: only auto-select agent when exactly ONE exists.
  With multiple agents, clear m_agentId and let user click on
  an agent node in the tree to activate it.
- startStateStream: verify current agent directory still exists
  instead of always re-discovering to the first agent. Only
  fall back to first agent if current agent is gone.
- Emit activeAgentIdChanged so QML reacts to agent changes.
This commit is contained in:
Levi Neely 2026-08-01 20:49:39 +02:00
parent e3fc0c49dd
commit 5fc4ebe64e
1 changed files with 40 additions and 42 deletions

View File

@ -48,40 +48,38 @@ Ollie9pClient::~Ollie9pClient()
void Ollie9pClient::setActiveSessionId(const QString &id)
{
if (m_activeSessionId == id) return;
if (m_activeSessionId == id) return;
stopStreams();
m_activeSessionId = id;
m_agentId.clear();
m_activeState = "idle";
stopStreams();
m_activeSessionId = id;
m_agentId.clear();
m_activeState = "idle";
if (id.isEmpty()) {
emit activeSessionIdChanged();
emit activeStateChanged();
return;
}
if (id.isEmpty()) {
emit activeSessionIdChanged();
emit activeStateChanged();
return;
}
// Discover agent ID BEFORE emitting signals
QStringList agents = getAgents(id);
if (!agents.isEmpty()) {
m_agentId = agents.first();
// Discover agents. If exactly one, use it automatically (backward compat).
QStringList agents = getAgents(id);
if (agents.size() == 1) {
m_agentId = agents.first();
// Read initial state immediately (state, not statewait)
QByteArray stateOut = run9p({"read", agentPath() + "/state"});
QString stateStr = QString::fromUtf8(stateOut).trimmed();
if (!stateStr.isEmpty())
m_activeState = stateStr;
QByteArray stateOut = run9p({"read", agentPath() + "/state"});
QString stateStr = QString::fromUtf8(stateOut).trimmed();
if (!stateStr.isEmpty())
m_activeState = stateStr;
}
emit activeSessionIdChanged();
emit activeStateChanged();
emit activeSessionIdChanged();
emit activeAgentIdChanged();
emit activeStateChanged();
startChatStream();
startStateStream();
} else {
// Empty session — no agent yet
emit activeSessionIdChanged();
emit activeStateChanged();
}
if (!m_agentId.isEmpty()) {
startChatStream();
startStateStream();
}
}
void Ollie9pClient::refreshSessions()
@ -443,22 +441,22 @@ void Ollie9pClient::startChatStream()
void Ollie9pClient::startStateStream()
{
if (m_stateProc) return;
if (m_stateProc) return;
// Check if session still exists before starting
if (m_activeSessionId.isEmpty() || m_agentId.isEmpty()) return;
// Check if session still exists before starting
if (m_activeSessionId.isEmpty() || m_agentId.isEmpty()) return;
// Re-discover agent ID — directory may have been renamed
QStringList agents = getAgents(m_activeSessionId);
if (agents.isEmpty()) {
// Session or agent directory gone — don't start
return;
}
QString newId = agents.first();
if (newId != m_agentId) {
m_agentId = newId;
emit activeAgentIdChanged();
}
// Verify the current agent directory still exists
QStringList agents = getAgents(m_activeSessionId);
if (agents.isEmpty()) {
// Session or agent directory gone — don't start
return;
}
if (!agents.contains(m_agentId)) {
// Current agent no longer exists — fall back to first available
m_agentId = agents.first();
emit activeAgentIdChanged();
}
m_stateProc = new QProcess(this);
connect(m_stateProc, &QProcess::readyReadStandardOutput, this, &Ollie9pClient::onStateReadyRead);