aboutsummaryrefslogtreecommitdiff
path: root/src/integration/assets/opencode/herdr-agent-state.js
diff options
context:
space:
mode:
authorOgulcan Celik <ogulcancelik@gmail.com>2026-05-23 21:27:26 +0300
committerOgulcan Celik <ogulcancelik@gmail.com>2026-05-23 21:37:05 +0300
commitdb67e9d74ffafc9c63bdf78fcf712419359f634b (patch)
tree9d181b55e65aaf6a2fad2d6cf785bb09a8d9748f /src/integration/assets/opencode/herdr-agent-state.js
parent1bab1d2ce21507747ea1f506fb891fe7cd5a13c9 (diff)
feat: restore agent sessions
refs #233
Diffstat (limited to 'src/integration/assets/opencode/herdr-agent-state.js')
-rw-r--r--src/integration/assets/opencode/herdr-agent-state.js65
1 files changed, 40 insertions, 25 deletions
diff --git a/src/integration/assets/opencode/herdr-agent-state.js b/src/integration/assets/opencode/herdr-agent-state.js
index 0fb96217..778fe564 100644
--- a/src/integration/assets/opencode/herdr-agent-state.js
+++ b/src/integration/assets/opencode/herdr-agent-state.js
@@ -1,7 +1,7 @@
// installed by herdr
// safe to edit. this plugin only activates inside herdr-managed panes.
// HERDR_INTEGRATION_ID=opencode
-// HERDR_INTEGRATION_VERSION=1
+// HERDR_INTEGRATION_VERSION=2
import net from "node:net";
@@ -13,7 +13,13 @@ function nextReportSeq() {
return reportSeq;
}
-function reportState(action) {
+function sessionIDFromProperties(properties) {
+ return typeof properties?.sessionID === "string" && properties.sessionID
+ ? properties.sessionID
+ : undefined;
+}
+
+function reportState(action, sessionID) {
const paneId = process.env.HERDR_PANE_ID;
const socketPath = process.env.HERDR_SOCKET_PATH;
@@ -24,24 +30,26 @@ function reportState(action) {
const requestId = `${SOURCE}:${Date.now()}:${Math.floor(Math.random() * 1_000_000)
.toString()
.padStart(6, "0")}`;
+ const params =
+ action === "release"
+ ? {
+ pane_id: paneId,
+ source: SOURCE,
+ agent: "opencode",
+ seq: nextReportSeq(),
+ }
+ : {
+ pane_id: paneId,
+ source: SOURCE,
+ agent: "opencode",
+ state: action,
+ seq: nextReportSeq(),
+ ...(sessionID ? { agent_session_id: sessionID } : {}),
+ };
const request = {
id: requestId,
method: action === "release" ? "pane.release_agent" : "pane.report_agent",
- params:
- action === "release"
- ? {
- pane_id: paneId,
- source: SOURCE,
- agent: "opencode",
- seq: nextReportSeq(),
- }
- : {
- pane_id: paneId,
- source: SOURCE,
- agent: "opencode",
- state: action,
- seq: nextReportSeq(),
- },
+ params,
};
return new Promise((resolve) => {
@@ -75,26 +83,33 @@ export const HerdrAgentStatePlugin = async () => {
event: async ({ event }) => {
const type = event?.type;
const properties = event?.properties ?? {};
+ const sessionID = sessionIDFromProperties(properties);
switch (type) {
case "permission.asked":
case "question.asked":
- await reportState("blocked");
+ await reportState("blocked", sessionID);
break;
case "permission.replied": {
const reply = properties.reply ?? properties.response;
if (reply === "reject") {
- await reportState("idle");
+ await reportState("idle", sessionID);
} else if (reply === "once" || reply === "always") {
- await reportState("working");
+ await reportState("working", sessionID);
}
break;
}
case "question.replied":
- await reportState("working");
+ await reportState("working", sessionID);
break;
case "question.rejected":
- await reportState("idle");
+ await reportState("idle", sessionID);
+ break;
+ case "session.created":
+ case "session.updated":
+ if (sessionID) {
+ await reportState("idle", sessionID);
+ }
break;
case "session.status": {
const status =
@@ -102,14 +117,14 @@ export const HerdrAgentStatePlugin = async () => {
? properties.status
: properties.status?.type;
if (status === "busy" || status === "retry") {
- await reportState("working");
+ await reportState("working", sessionID);
} else if (status === "idle") {
- await reportState("idle");
+ await reportState("idle", sessionID);
}
break;
}
case "session.idle":
- await reportState("idle");
+ await reportState("idle", sessionID);
break;
default:
break;