Skip to content

Commit b10c5fa

Browse files
authored
Merge pull request #288 from sledtools/microvm-ship
microvm.nix agent spawning
2 parents 0080bea + e966b6f commit b10c5fa

22 files changed

Lines changed: 7465 additions & 315 deletions

File tree

Cargo.lock

Lines changed: 49 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ members = [
1111
"crates/pika-tls",
1212
"crates/rmp-cli",
1313
"crates/pika-server",
14+
"crates/vm-spawner",
1415
]
1516

1617
[workspace.dependencies]

bots/pi-bridge.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import pty
1616
import select
1717
import signal
18+
import shlex
1819
import struct
1920
import subprocess
2021
import sys
@@ -102,9 +103,31 @@ def emit_pi_event(group_id: str, payload: dict) -> None:
102103
)
103104

104105

106+
def build_publish_keypackage_cmd():
107+
raw = os.environ.get("PI_RELAYS_JSON", "").strip()
108+
if not raw:
109+
return {"cmd": "publish_keypackage"}
110+
try:
111+
parsed = json.loads(raw)
112+
except Exception as err:
113+
log(f"invalid PI_RELAYS_JSON, ignoring: {err}")
114+
return {"cmd": "publish_keypackage"}
115+
if not isinstance(parsed, list):
116+
return {"cmd": "publish_keypackage"}
117+
relays = [str(item).strip() for item in parsed if str(item).strip()]
118+
if not relays:
119+
return {"cmd": "publish_keypackage"}
120+
return {"cmd": "publish_keypackage", "relays": relays}
121+
122+
105123
def spawn_pi_rpc(label: str) -> subprocess.Popen[bytes]:
106124
env = os.environ.copy()
107-
cmd = ["pi", "--mode", "rpc", "--no-session", "--provider", "anthropic"]
125+
cmd = shlex.split(
126+
os.environ.get(
127+
"PI_CMD",
128+
"pi --mode rpc --no-session --provider anthropic",
129+
)
130+
)
108131
model = os.environ.get("PI_MODEL")
109132
if model:
110133
cmd.extend(["--model", model])
@@ -981,8 +1004,7 @@ def main() -> None:
9811004
if msg_type == "ready":
9821005
my_pubkey = msg.get("pubkey")
9831006
log(f"marmotd ready, pubkey={my_pubkey}")
984-
send_to_marmotd({"cmd": "publish_keypackage"})
985-
continue
1007+
send_to_marmotd(build_publish_keypackage_cmd())
9861008

9871009
if msg_type == "call_invite_received":
9881010
call_id = str(msg.get("call_id", ""))

bots/pi-bridge.sh

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
my_pubkey=""
5+
6+
trim() {
7+
local s="$1"
8+
s="${s#"${s%%[![:space:]]*}"}"
9+
s="${s%"${s##*[![:space:]]}"}"
10+
printf '%s' "$s"
11+
}
12+
13+
emit_publish_keypackage_cmd() {
14+
if [[ -n "${PI_RELAYS_JSON:-}" ]] && jq -e 'type == "array" and length > 0' >/dev/null 2>&1 <<<"${PI_RELAYS_JSON}"; then
15+
jq -cn --argjson relays "${PI_RELAYS_JSON}" '{"cmd":"publish_keypackage","relays":$relays}'
16+
else
17+
printf '{"cmd":"publish_keypackage"}\n'
18+
fi
19+
}
20+
21+
while IFS= read -r line; do
22+
[[ -z "${line}" ]] && continue
23+
24+
msg_type="$(printf '%s' "$line" | jq -r '.type // empty' 2>/dev/null || true)"
25+
case "$msg_type" in
26+
ready)
27+
my_pubkey="$(printf '%s' "$line" | jq -r '.pubkey // ""' 2>/dev/null || true)"
28+
emit_publish_keypackage_cmd
29+
;;
30+
message_received)
31+
from_pubkey="$(printf '%s' "$line" | jq -r '.from_pubkey // ""' 2>/dev/null || true)"
32+
if [[ -n "$my_pubkey" && "$from_pubkey" == "$my_pubkey" ]]; then
33+
continue
34+
fi
35+
36+
group_id="$(printf '%s' "$line" | jq -r '.nostr_group_id // ""' 2>/dev/null || true)"
37+
content="$(printf '%s' "$line" | jq -r '.content // ""' 2>/dev/null || true)"
38+
39+
response=""
40+
if [[ "$content" =~ [Rr]eply[[:space:]]+with[[:space:]]+exactly:[[:space:]]*(.+)$ ]]; then
41+
response="$(trim "${BASH_REMATCH[1]}")"
42+
fi
43+
if [[ -z "$response" ]]; then
44+
response="ack: $content"
45+
fi
46+
47+
jq -cn \
48+
--arg gid "$group_id" \
49+
--arg content "$response" \
50+
'{"cmd":"send_message","nostr_group_id":$gid,"content":$content}'
51+
;;
52+
*)
53+
;;
54+
esac
55+
done

0 commit comments

Comments
 (0)