krunner, dolphin: use Generate one-shot instead of session lifecycle

This commit is contained in:
Levi Neely 2026-07-18 23:09:51 +02:00
parent 89e86824f6
commit 5b374847f8
2 changed files with 13 additions and 102 deletions

View File

@ -4,73 +4,32 @@
TARGET="$1"
PREFIX="${2:-Describe and explain this file:}"
CWD="$(dirname "$TARGET")"
PROMPT="$PREFIX $TARGET"
SERVICE="org.ollie.SessionManager"
OBJ="/org/ollie/SessionManager"
IFACE="org.ollie.SessionManager"
# Create session
SESSION=$(dbus-send --session --dest=$SERVICE --type=method_call --print-reply \
$OBJ $IFACE.CreateSession \
string:"$CWD" string:"" string:"" string:"default" 2>/dev/null | \
awk -F'"' '/string "/{print $2; exit}')
# Use Generate one-shot — no session needed
RESULT=$(gdbus call --session --dest $SERVICE --object-path $OBJ \
--method $IFACE.Generate "$PROMPT" "" "" "" 2>/dev/null | \
sed "s/^('//; s/')$//; s/\\\\n/\n/g")
if [ -z "$SESSION" ]; then
notify-send "Ollie" "Failed to create session"
if [ -z "$RESULT" ]; then
notify-send "Ollie" "No response (is olliesrv or ollied running?)"
exit 1
fi
# Submit
dbus-send --session --dest=$SERVICE --type=method_call --print-reply \
$OBJ $IFACE.Submit string:"$SESSION" string:"$PROMPT" > /dev/null 2>&1
# Wait for agent to start (state becomes non-idle)
i=0
while [ $i -lt 20 ]; do
STATE=$(dbus-send --session --dest=$SERVICE --type=method_call --print-reply \
$OBJ $IFACE.GetState string:"$SESSION" 2>/dev/null | \
awk -F'"' '/string "/{print $2; exit}')
[ "$STATE" != "idle" ] && break
sleep 0.2
i=$((i + 1))
done
# Wait for agent to finish (state returns to idle, max 60s)
i=0
while [ $i -lt 120 ]; do
STATE=$(dbus-send --session --dest=$SERVICE --type=method_call --print-reply \
$OBJ $IFACE.GetState string:"$SESSION" 2>/dev/null | \
awk -F'"' '/string "/{print $2; exit}')
[ "$STATE" = "idle" ] && break
sleep 0.5
i=$((i + 1))
done
# Get chat via gdbus
RESULT=$(gdbus call --session --dest $SERVICE --object-path $OBJ \
--method $IFACE.GetChat "$SESSION" 0 2>/dev/null | \
sed "s/^('//; s/', int64 [0-9]*)$//; s/\\\\n/\n/g")
# Extract assistant response
ASSISTANT=$(printf '%s\n' "$RESULT" | sed -n '/^\[assistant\]$/,/^\[/{/^\[assistant\]$/d; /^\[/d; p;}')
[ -z "$ASSISTANT" ] && ASSISTANT="$RESULT"
# Kill session
dbus-send --session --dest=$SERVICE --type=method_call \
$OBJ $IFACE.KillSession string:"$SESSION" > /dev/null 2>&1
# Show result
TMPFILE=$(mktemp /tmp/ollie-ask.XXXXXX)
printf '%s' "$ASSISTANT" > "$TMPFILE"
printf '%s' "$RESULT" > "$TMPFILE"
if command -v kdialog > /dev/null 2>&1; then
kdialog --title "Ollie — $(basename "$TARGET")" --textbox "$TMPFILE" 600 400
elif command -v zenity > /dev/null 2>&1; then
zenity --text-info --title="Ollie — $(basename "$TARGET")" --width=600 --height=400 --filename="$TMPFILE"
else
notify-send -t 30000 "Ollie" "$ASSISTANT"
notify-send -t 30000 "Ollie" "$RESULT"
fi
rm -f "$TMPFILE"

View File

@ -4,9 +4,6 @@
#include <QDBusReply>
#include <QDBusConnection>
#include <QDBusInterface>
#include <QDir>
#include <QThread>
#include <QVariantMap>
#include <QDialog>
#include <QVBoxLayout>
#include <QHBoxLayout>
@ -206,57 +203,12 @@ void OllieRunner::run(const RunnerNS::RunnerContext &context, const RunnerNS::Qu
QString OllieRunner::oneShotRun(const QString &prompt)
{
// 1. Create session
QDBusReply<QString> createReply = m_iface->call(
"CreateSession", QDir::homePath(), QString(), QString(), "default");
if (!createReply.isValid()) {
return "Error: could not create session";
// Use the Generate one-shot D-Bus method — no session needed.
QDBusReply<QString> reply = m_iface->call("Generate", prompt, QString(), QString(), QString());
if (!reply.isValid()) {
return "Error: " + reply.error().message();
}
QString sessionId = createReply.value();
// 2. Submit prompt
QDBusReply<bool> submitReply = m_iface->call("Submit", sessionId, prompt);
if (!submitReply.isValid() || !submitReply.value()) {
killSession(sessionId);
return "Error: could not submit prompt";
}
// 3. Poll for completion (max 30s)
QString state = "thinking";
for (int i = 0; i < 60; i++) {
QThread::msleep(500);
QDBusReply<QString> stateReply = m_iface->call("GetState", sessionId);
if (!stateReply.isValid()) break;
state = stateReply.value();
if (state == "idle") break;
}
// 4. Get the chat output
QDBusMessage msg = QDBusMessage::createMethodCall(SERVICE, OBJ_PATH, IFACE, "GetChat");
msg << sessionId << (qlonglong)0;
QDBusMessage reply = QDBusConnection::sessionBus().call(msg);
QString chatText;
if (reply.type() == QDBusMessage::ReplyMessage && reply.arguments().size() >= 1) {
chatText = reply.arguments()[0].toString();
}
// 5. Extract assistant response (text between [assistant]\n and next \n\n[)
QString result;
int aStart = chatText.indexOf("[assistant]\n");
if (aStart >= 0) {
aStart += 12; // skip "[assistant]\n"
int aEnd = chatText.indexOf("\n\n[", aStart);
if (aEnd < 0) aEnd = chatText.length();
result = chatText.mid(aStart, aEnd - aStart).trimmed();
} else {
result = chatText; // fallback: show everything
}
// 6. Kill session
killSession(sessionId);
QString result = reply.value().trimmed();
return result.isEmpty() ? "(no response)" : result;
}