-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpoll-adapter-example.sh
More file actions
executable file
·44 lines (37 loc) · 1.51 KB
/
Copy pathpoll-adapter-example.sh
File metadata and controls
executable file
·44 lines (37 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/env bash
# Poll adapter example for an agent that already has a CLI command.
# Edit AGENT, STATE, TOKEN_FILE, and COMMAND. The command receives the message
# body on stdin and must write the final reply on stdout.
set -euo pipefail
STATE="${STATE:-$HOME/.codex/agent}"
AGENT="${AGENT:-otter}"
TOKEN_FILE="${TOKEN_FILE:-$STATE/agent-tokens/$AGENT.token}"
COMMAND="${COMMAND:-cat}"
AGENT_RIVER_BIN="${AGENT_RIVER_BIN:-bin/codex-agent.js}"
codex_agent() {
node "$AGENT_RIVER_BIN" "$@"
}
inbox_json="$(codex_agent exchange-inbox --state "$STATE" --agent "$AGENT" --token-file "$TOKEN_FILE")"
msg_id="$(printf '%s' "$inbox_json" | node -e '
let raw = "";
process.stdin.on("data", (c) => raw += c);
process.stdin.on("end", () => {
const data = JSON.parse(raw || "{}");
const msg = (data.messages || [])[0];
process.stdout.write(msg ? msg.id : "");
});
')"
if [ -z "$msg_id" ]; then
exit 0
fi
codex_agent exchange-claim --state "$STATE" --id "$msg_id" --agent "$AGENT" --token-file "$TOKEN_FILE" >/dev/null
thread_json="$(codex_agent exchange-thread --state "$STATE" --id "$msg_id")"
body="$(printf '%s' "$thread_json" | node -e '
let raw = "";
process.stdin.on("data", (c) => raw += c);
process.stdin.on("end", () => process.stdout.write(JSON.parse(raw).message.text || ""));
')"
reply_file="$(mktemp)"
trap 'rm -f "$reply_file"' EXIT
printf '%s\n' "$body" | sh -c "$COMMAND" > "$reply_file"
codex_agent exchange-reply --state "$STATE" --id "$msg_id" --agent "$AGENT" --token-file "$TOKEN_FILE" --from-file "$reply_file"