feat: add claude, codex, and opencode integrations
This commit is contained in:
+137
-3
@@ -99,6 +99,7 @@ fn run_integration_command(args: &[String]) -> std::io::Result<i32> {
|
||||
|
||||
match subcommand {
|
||||
"install" => integration_install(&args[1..]),
|
||||
"uninstall" => integration_uninstall(&args[1..]),
|
||||
_ => {
|
||||
print_integration_help();
|
||||
Ok(2)
|
||||
@@ -450,11 +451,11 @@ fn pane_run(args: &[String]) -> std::io::Result<i32> {
|
||||
|
||||
fn integration_install(args: &[String]) -> std::io::Result<i32> {
|
||||
let Some(target) = args.first().map(|arg| arg.as_str()) else {
|
||||
eprintln!("usage: herdr integration install <pi>");
|
||||
eprintln!("usage: herdr integration install <pi|claude|codex|opencode>");
|
||||
return Ok(2);
|
||||
};
|
||||
if args.len() != 1 {
|
||||
eprintln!("usage: herdr integration install <pi>");
|
||||
eprintln!("usage: herdr integration install <pi|claude|codex|opencode>");
|
||||
return Ok(2);
|
||||
}
|
||||
|
||||
@@ -464,9 +465,135 @@ fn integration_install(args: &[String]) -> std::io::Result<i32> {
|
||||
println!("installed pi integration to {}", path.display());
|
||||
Ok(0)
|
||||
}
|
||||
"claude" => {
|
||||
let installed = crate::integration::install_claude()?;
|
||||
println!(
|
||||
"installed claude integration hook to {}",
|
||||
installed.hook_path.display()
|
||||
);
|
||||
println!(
|
||||
"ensured claude settings at {}",
|
||||
installed.settings_path.display()
|
||||
);
|
||||
Ok(0)
|
||||
}
|
||||
"codex" => {
|
||||
let installed = crate::integration::install_codex()?;
|
||||
println!(
|
||||
"installed codex integration hook to {}",
|
||||
installed.hook_path.display()
|
||||
);
|
||||
println!("ensured codex hooks at {}", installed.hooks_path.display());
|
||||
println!(
|
||||
"ensured codex config at {}",
|
||||
installed.config_path.display()
|
||||
);
|
||||
Ok(0)
|
||||
}
|
||||
"opencode" => {
|
||||
let installed = crate::integration::install_opencode()?;
|
||||
println!(
|
||||
"installed opencode integration plugin to {}",
|
||||
installed.plugin_path.display()
|
||||
);
|
||||
Ok(0)
|
||||
}
|
||||
_ => {
|
||||
eprintln!("unknown integration target: {target}");
|
||||
eprintln!("currently supported: pi");
|
||||
eprintln!("currently supported: pi, claude, codex, opencode");
|
||||
Ok(2)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn integration_uninstall(args: &[String]) -> std::io::Result<i32> {
|
||||
let Some(target) = args.first().map(|arg| arg.as_str()) else {
|
||||
eprintln!("usage: herdr integration uninstall <pi|claude|codex|opencode>");
|
||||
return Ok(2);
|
||||
};
|
||||
if args.len() != 1 {
|
||||
eprintln!("usage: herdr integration uninstall <pi|claude|codex|opencode>");
|
||||
return Ok(2);
|
||||
}
|
||||
|
||||
match target {
|
||||
"pi" => {
|
||||
let result = crate::integration::uninstall_pi()?;
|
||||
if result.removed_extension {
|
||||
println!(
|
||||
"removed pi integration extension at {}",
|
||||
result.extension_path.display()
|
||||
);
|
||||
} else {
|
||||
println!(
|
||||
"no pi integration extension found at {}",
|
||||
result.extension_path.display()
|
||||
);
|
||||
}
|
||||
Ok(0)
|
||||
}
|
||||
"claude" => {
|
||||
let result = crate::integration::uninstall_claude()?;
|
||||
if result.removed_hook_file {
|
||||
println!("removed claude hook at {}", result.hook_path.display());
|
||||
} else {
|
||||
println!("no claude hook found at {}", result.hook_path.display());
|
||||
}
|
||||
if result.updated_settings {
|
||||
println!(
|
||||
"removed herdr claude hook entries from {}",
|
||||
result.settings_path.display()
|
||||
);
|
||||
} else {
|
||||
println!(
|
||||
"no herdr claude hook entries found in {}",
|
||||
result.settings_path.display()
|
||||
);
|
||||
}
|
||||
Ok(0)
|
||||
}
|
||||
"codex" => {
|
||||
let result = crate::integration::uninstall_codex()?;
|
||||
if result.removed_hook_file {
|
||||
println!("removed codex hook at {}", result.hook_path.display());
|
||||
} else {
|
||||
println!("no codex hook found at {}", result.hook_path.display());
|
||||
}
|
||||
if result.updated_hooks {
|
||||
println!(
|
||||
"removed herdr codex hook entries from {}",
|
||||
result.hooks_path.display()
|
||||
);
|
||||
} else {
|
||||
println!(
|
||||
"no herdr codex hook entries found in {}",
|
||||
result.hooks_path.display()
|
||||
);
|
||||
}
|
||||
println!(
|
||||
"left codex config unchanged at {}",
|
||||
result.config_path.display()
|
||||
);
|
||||
Ok(0)
|
||||
}
|
||||
"opencode" => {
|
||||
let result = crate::integration::uninstall_opencode()?;
|
||||
if result.removed_plugin {
|
||||
println!(
|
||||
"removed opencode integration plugin at {}",
|
||||
result.plugin_path.display()
|
||||
);
|
||||
} else {
|
||||
println!(
|
||||
"no opencode integration plugin found at {}",
|
||||
result.plugin_path.display()
|
||||
);
|
||||
}
|
||||
Ok(0)
|
||||
}
|
||||
_ => {
|
||||
eprintln!("unknown integration target: {target}");
|
||||
eprintln!("currently supported: pi, claude, codex, opencode");
|
||||
Ok(2)
|
||||
}
|
||||
}
|
||||
@@ -786,6 +913,13 @@ fn print_wait_help() {
|
||||
fn print_integration_help() {
|
||||
eprintln!("herdr integration commands:");
|
||||
eprintln!(" herdr integration install pi");
|
||||
eprintln!(" herdr integration install claude");
|
||||
eprintln!(" herdr integration install codex");
|
||||
eprintln!(" herdr integration install opencode");
|
||||
eprintln!(" herdr integration uninstall pi");
|
||||
eprintln!(" herdr integration uninstall claude");
|
||||
eprintln!(" herdr integration uninstall codex");
|
||||
eprintln!(" herdr integration uninstall opencode");
|
||||
}
|
||||
|
||||
fn _print_json<T: Serialize>(value: &T) {
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
#!/bin/sh
|
||||
# installed by herdr
|
||||
# safe to edit. this hook only activates inside herdr-managed panes.
|
||||
|
||||
set -eu
|
||||
|
||||
action="${1:-}"
|
||||
cat >/dev/null 2>/dev/null || true
|
||||
|
||||
case "$action" in
|
||||
working|idle|blocked|release) ;;
|
||||
*) exit 0 ;;
|
||||
esac
|
||||
|
||||
[ "${HERDR_ENV:-}" = "1" ] || exit 0
|
||||
[ -n "${HERDR_SOCKET_PATH:-}" ] || exit 0
|
||||
[ -n "${HERDR_PANE_ID:-}" ] || exit 0
|
||||
command -v python3 >/dev/null 2>&1 || exit 0
|
||||
|
||||
HERDR_ACTION="$action" python3 - <<'PY'
|
||||
import json
|
||||
import os
|
||||
import random
|
||||
import socket
|
||||
import time
|
||||
|
||||
source = "herdr:claude"
|
||||
action = os.environ.get("HERDR_ACTION", "")
|
||||
pane_id = os.environ.get("HERDR_PANE_ID")
|
||||
socket_path = os.environ.get("HERDR_SOCKET_PATH")
|
||||
|
||||
if not pane_id or not socket_path:
|
||||
raise SystemExit(0)
|
||||
|
||||
request_id = f"{source}:{int(time.time() * 1000)}:{random.randrange(1_000_000):06d}"
|
||||
if action == "release":
|
||||
request = {
|
||||
"id": request_id,
|
||||
"method": "pane.release_agent",
|
||||
"params": {
|
||||
"pane_id": pane_id,
|
||||
"source": source,
|
||||
"agent": "claude",
|
||||
},
|
||||
}
|
||||
else:
|
||||
request = {
|
||||
"id": request_id,
|
||||
"method": "pane.report_agent",
|
||||
"params": {
|
||||
"pane_id": pane_id,
|
||||
"source": source,
|
||||
"agent": "claude",
|
||||
"state": action,
|
||||
},
|
||||
}
|
||||
|
||||
try:
|
||||
client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
||||
client.settimeout(0.5)
|
||||
client.connect(socket_path)
|
||||
client.sendall((json.dumps(request) + "\n").encode())
|
||||
try:
|
||||
client.recv(4096)
|
||||
except Exception:
|
||||
pass
|
||||
client.close()
|
||||
except Exception:
|
||||
pass
|
||||
PY
|
||||
@@ -0,0 +1,70 @@
|
||||
#!/bin/sh
|
||||
# installed by herdr
|
||||
# safe to edit. this hook only activates inside herdr-managed panes.
|
||||
|
||||
set -eu
|
||||
|
||||
action="${1:-}"
|
||||
cat >/dev/null 2>/dev/null || true
|
||||
|
||||
case "$action" in
|
||||
working|idle|blocked|release) ;;
|
||||
*) exit 0 ;;
|
||||
esac
|
||||
|
||||
[ "${HERDR_ENV:-}" = "1" ] || exit 0
|
||||
[ -n "${HERDR_SOCKET_PATH:-}" ] || exit 0
|
||||
[ -n "${HERDR_PANE_ID:-}" ] || exit 0
|
||||
command -v python3 >/dev/null 2>&1 || exit 0
|
||||
|
||||
HERDR_ACTION="$action" python3 - <<'PY'
|
||||
import json
|
||||
import os
|
||||
import random
|
||||
import socket
|
||||
import time
|
||||
|
||||
source = "herdr:codex"
|
||||
action = os.environ.get("HERDR_ACTION", "")
|
||||
pane_id = os.environ.get("HERDR_PANE_ID")
|
||||
socket_path = os.environ.get("HERDR_SOCKET_PATH")
|
||||
|
||||
if not pane_id or not socket_path:
|
||||
raise SystemExit(0)
|
||||
|
||||
request_id = f"{source}:{int(time.time() * 1000)}:{random.randrange(1_000_000):06d}"
|
||||
if action == "release":
|
||||
request = {
|
||||
"id": request_id,
|
||||
"method": "pane.release_agent",
|
||||
"params": {
|
||||
"pane_id": pane_id,
|
||||
"source": source,
|
||||
"agent": "codex",
|
||||
},
|
||||
}
|
||||
else:
|
||||
request = {
|
||||
"id": request_id,
|
||||
"method": "pane.report_agent",
|
||||
"params": {
|
||||
"pane_id": pane_id,
|
||||
"source": source,
|
||||
"agent": "codex",
|
||||
"state": action,
|
||||
},
|
||||
}
|
||||
|
||||
try:
|
||||
client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
||||
client.settimeout(0.5)
|
||||
client.connect(socket_path)
|
||||
client.sendall((json.dumps(request) + "\n").encode())
|
||||
try:
|
||||
client.recv(4096)
|
||||
except Exception:
|
||||
pass
|
||||
client.close()
|
||||
except Exception:
|
||||
pass
|
||||
PY
|
||||
@@ -0,0 +1,99 @@
|
||||
import net from "node:net";
|
||||
|
||||
const SOURCE = "herdr:opencode";
|
||||
|
||||
function reportState(action) {
|
||||
const paneId = process.env.HERDR_PANE_ID;
|
||||
const socketPath = process.env.HERDR_SOCKET_PATH;
|
||||
|
||||
if (!paneId || !socketPath) {
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
const requestId = `${SOURCE}:${Date.now()}:${Math.floor(Math.random() * 1_000_000)
|
||||
.toString()
|
||||
.padStart(6, "0")}`;
|
||||
const request = {
|
||||
id: requestId,
|
||||
method: action === "release" ? "pane.release_agent" : "pane.report_agent",
|
||||
params:
|
||||
action === "release"
|
||||
? {
|
||||
pane_id: paneId,
|
||||
source: SOURCE,
|
||||
agent: "opencode",
|
||||
}
|
||||
: {
|
||||
pane_id: paneId,
|
||||
source: SOURCE,
|
||||
agent: "opencode",
|
||||
state: action,
|
||||
},
|
||||
};
|
||||
|
||||
return new Promise((resolve) => {
|
||||
const client = net.createConnection(socketPath, () => {
|
||||
client.write(`${JSON.stringify(request)}\n`);
|
||||
});
|
||||
|
||||
const finish = () => {
|
||||
client.destroy();
|
||||
resolve();
|
||||
};
|
||||
|
||||
client.setTimeout(500, finish);
|
||||
client.on("data", finish);
|
||||
client.on("error", finish);
|
||||
client.on("end", finish);
|
||||
client.on("close", resolve);
|
||||
});
|
||||
}
|
||||
|
||||
export const HerdrAgentStatePlugin = async () => {
|
||||
if (
|
||||
process.env.HERDR_ENV !== "1" ||
|
||||
!process.env.HERDR_SOCKET_PATH ||
|
||||
!process.env.HERDR_PANE_ID
|
||||
) {
|
||||
return {};
|
||||
}
|
||||
|
||||
return {
|
||||
event: async ({ event }) => {
|
||||
const type = event?.type;
|
||||
const properties = event?.properties ?? {};
|
||||
|
||||
switch (type) {
|
||||
case "permission.asked":
|
||||
await reportState("blocked");
|
||||
break;
|
||||
case "permission.replied": {
|
||||
const reply = properties.reply ?? properties.response;
|
||||
if (reply === "reject") {
|
||||
await reportState("idle");
|
||||
} else if (reply === "once" || reply === "always") {
|
||||
await reportState("working");
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "session.status": {
|
||||
const status =
|
||||
typeof properties.status === "string"
|
||||
? properties.status
|
||||
: properties.status?.type;
|
||||
if (status === "busy" || status === "retry") {
|
||||
await reportState("working");
|
||||
} else if (status === "idle") {
|
||||
await reportState("idle");
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "session.idle":
|
||||
await reportState("idle");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
},
|
||||
};
|
||||
};
|
||||
+995
-4
File diff suppressed because it is too large
Load Diff
+1
-1
@@ -195,7 +195,7 @@ fn main() -> io::Result<()> {
|
||||
println!(" workspace workspace helpers over the socket api");
|
||||
println!(" pane pane control helpers over the socket api");
|
||||
println!(" wait blocking wait helpers over the socket api");
|
||||
println!(" integration install built-in agent integrations");
|
||||
println!(" integration manage built-in agent integrations");
|
||||
println!();
|
||||
println!("Options:");
|
||||
println!(" --no-session Don't restore or save sessions");
|
||||
|
||||
Reference in New Issue
Block a user