Skip to content

Commit 209e6d9

Browse files
committed
working containerized
Signed-off-by: John <[email protected]>
1 parent 8c978b7 commit 209e6d9

File tree

4 files changed

+88
-34
lines changed

4 files changed

+88
-34
lines changed

github_webhook_events/agi.py

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
77
AGI_SOCK=/tmp/agi.sock go run agi_sshd.go
88
9-
ssh -NnT -p 2222 -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o PasswordAuthentication=no -R /tmux.sock:$(echo $TMUX | sed -e 's/,.*//g') -R /input.sock:$(mktemp -d)/input.sock user@localhost
9+
export INPUT_SOCK="$(mktemp -d)/input.sock"; ssh -NnT -p 2222 -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o PasswordAuthentication=no -R /tmux.sock:$(echo $TMUX | sed -e 's/,.*//g') -R "${INPUT_SOCK}:${INPUT_SOCK}" user@localhost
1010
1111
1212
gh auth refresh -h github.com -s admin:public_key
@@ -3293,6 +3293,12 @@ def make_argparse_parser(argv=None):
32933293
default=None,
32943294
type=str,
32953295
)
3296+
parser.add_argument(
3297+
"--client-side-input-socket-path",
3298+
dest="client_side_input_socket_path",
3299+
default=None,
3300+
type=str,
3301+
)
32963302
parser.add_argument(
32973303
"--agi-name",
32983304
dest="agi_name",
@@ -4128,11 +4134,13 @@ async def read_unix_socket_lines(path):
41284134
await writer.wait_closed()
41294135

41304136

4131-
async def pdb_action_stream(tg, user_name, agi_name, agents, threads, pane: Optional[libtmux.Pane] = None):
4137+
async def pdb_action_stream(tg, user_name, agi_name, agents, threads, pane: Optional[libtmux.Pane] = None, input_socket_path: Optional[str] = None):
41324138
# TODO Take ALICE_INPUT from args
4139+
alice_input_sock = input_socket_path
41334140
if pane is not None:
41344141
alice_input = pane.window.session.show_environment()[f"{agi_name.upper()}_INPUT"]
4135-
alice_input_sock = pane.window.session.show_environment()[f"{agi_name.upper()}_INPUT_SOCK"]
4142+
if alice_input_sock is None:
4143+
alice_input_sock = pane.window.session.show_environment()[f"{agi_name.upper()}_INPUT_SOCK"]
41364144
alice_input_last_line = pane.window.session.show_environment()[f"{agi_name.upper()}_INPUT_LAST_LINE"]
41374145

41384146
if pathlib.Path(alice_input_sock).is_socket():
@@ -4253,6 +4261,8 @@ async def main(
42534261
openai_api_key: str = None,
42544262
openai_base_url: Optional[str] = None,
42554263
pane: Optional[libtmux.Pane] = None,
4264+
input_socket_path: Optional[str] = None,
4265+
client_side_input_socket_path: Optional[str] = None,
42564266
):
42574267
if log is not None:
42584268
# logging.basicConfig(level=log)
@@ -4296,6 +4306,7 @@ async def main(
42964306
agents,
42974307
threads,
42984308
pane=pane,
4309+
input_socket_path=input_socket_path,
42994310
)
43004311

43014312
# Waiting Event Stream and Callbacks
@@ -4450,7 +4461,7 @@ async def user_input_action_stream_queue_iterator(queue):
44504461
# pane.send_keys(f'', enter=True)
44514462

44524463
pane.send_keys(f'export {agi_name.upper()}_INPUT="' + '${CALLER_PATH}/input.txt"', enter=True)
4453-
pane.send_keys(f'export {agi_name.upper()}_INPUT_SOCK="' + '${CALLER_PATH}/input.sock"', enter=True)
4464+
pane.send_keys(f'export {agi_name.upper()}_INPUT_SOCK="{client_side_input_socket_path}"', enter=True)
44544465
pane.send_keys(f'export {agi_name.upper()}_INPUT_LAST_LINE="' + '${CALLER_PATH}/input-last-line.txt"', enter=True)
44554466

44564467
# TODO
@@ -4702,7 +4713,7 @@ def a_shell_for_a_ghost_send_keys(pane, send_string, erase_after=None):
47024713
time.sleep(0.01)
47034714

47044715

4705-
async def tmux_test(*args, socket_path=None, input_socket_path=None, **kwargs):
4716+
async def tmux_test(*args, socket_path=None, input_socket_path=None, client_side_input_socket_path: Optional[str] = None, **kwargs):
47064717
pane = None
47074718
tempdir = None
47084719
possible_tempdir = tempdir
@@ -4792,7 +4803,7 @@ async def tmux_test(*args, socket_path=None, input_socket_path=None, **kwargs):
47924803
session.set_environment(tempdir_env_var, tempdir)
47934804

47944805
session.set_environment(f"{agi_name.upper()}_INPUT", str(pathlib.Path(tempdir, "input.txt")))
4795-
session.set_environment(f"{agi_name.upper()}_INPUT_SOCK", str(input_socket_path))
4806+
session.set_environment(f"{agi_name.upper()}_INPUT_SOCK", client_side_input_socket_path)
47964807
session.set_environment(f"{agi_name.upper()}_INPUT_LAST_LINE", str(pathlib.Path(tempdir, "input-last-line.txt")))
47974808

47984809
pane.send_keys(
@@ -4946,7 +4957,7 @@ async def tmux_test(*args, socket_path=None, input_socket_path=None, **kwargs):
49464957
time.sleep(0.1)
49474958

49484959
pane.send_keys(f"export {agi_name.upper()}_INPUT=" + str(pathlib.Path(tempdir, "input.txt")), enter=True)
4949-
pane.send_keys(f"export {agi_name.upper()}_INPUT_SOCK=" + str(input_socket_path), enter=True)
4960+
pane.send_keys(f'export {agi_name.upper()}_INPUT_SOCK="{client_side_input_socket_path}"', enter=True)
49504961
pane.send_keys(f"export {agi_name.upper()}_INPUT_LAST_LINE=" + str(pathlib.Path(tempdir, "input-last-line.txt")), enter=True)
49514962
pane.send_keys(f'rm -fv ${agi_name.upper()}_INPUT_SOCK', enter=True)
49524963
pane.send_keys(f'ln -s ${agi_name.upper()}_INPUT_SOCK', enter=True)
@@ -4955,7 +4966,7 @@ async def tmux_test(*args, socket_path=None, input_socket_path=None, **kwargs):
49554966

49564967
pane.send_keys(f'set +x', enter=True)
49574968

4958-
await main(*args, pane=pane, **kwargs)
4969+
await main(*args, pane=pane, input_socket_path=input_socket_path, client_side_input_socket_path=client_side_input_socket_path, **kwargs)
49594970
finally:
49604971
with contextlib.suppress(Exception):
49614972
if pane is not None:
@@ -4977,7 +4988,7 @@ async def lifespan_logging(app):
49774988
app = FastAPI(lifespan=lifespan_logging)
49784989

49794990

4980-
def run_tmux_attach(socket_path, input_socket_path):
4991+
def run_tmux_attach(socket_path, input_socket_path, client_side_input_socket_path):
49814992
cmd = [
49824993
sys.executable,
49834994
"-u",
@@ -4986,6 +4997,8 @@ def run_tmux_attach(socket_path, input_socket_path):
49864997
socket_path,
49874998
"--input-socket-path",
49884999
input_socket_path,
5000+
"--client-side-input-socket-path",
5001+
client_side_input_socket_path,
49895002
"--agi-name",
49905003
# TODO Something secure here, scitt URN and lookup for PS1?
49915004
f"alice{str(uuid.uuid4()).split('-')[4]}",
@@ -5016,11 +5029,12 @@ async def connect_and_read(socket_path: str, sleep_time: float = 0.1):
50165029
class RequestConnectTMUX(BaseModel):
50175030
socket_tmux_path: str = Field(alias="tmux.sock")
50185031
socket_input_path: str = Field(alias="input.sock")
5032+
socket_client_side_input_path: str = Field(alias="client-side-input.sock")
50195033

50205034

50215035
@app.post("/connect/tmux")
50225036
async def connect(request_connect_tmux: RequestConnectTMUX, background_tasks: BackgroundTasks):
5023-
background_tasks.add_task(run_tmux_attach, request_connect_tmux.socket_tmux_path, request_connect_tmux.socket_input_path)
5037+
background_tasks.add_task(run_tmux_attach, request_connect_tmux.socket_tmux_path, request_connect_tmux.socket_input_path, request_connect_tmux.socket_client_side_input_path)
50245038
return {
50255039
"connected": True,
50265040
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# docker build --progress=plain -t alice-server -f agi_sshd.Dockerfile . && docker run --rm -ti -p 2222:2222 -e OPENAI_API_KEY=$(python -m keyring get $(git config user.email) api-key.platform.openai.com) alice-server
2+
FROM golang:1.24 as builder_golang_agi_sshd
3+
4+
WORKDIR /usr/src/app
5+
6+
# pre-copy/cache go.mod for pre-downloading dependencies and only redownloading them in subsequent builds if they change
7+
COPY go.mod go.sum ./
8+
RUN go mod download
9+
10+
COPY agi_sshd.go .
11+
12+
RUN set -x \
13+
&& CGO_ENABLED=0 go build -tags netgo -o agi_sshd agi_sshd.go
14+
15+
# Python server, add built golang server
16+
FROM registry.fedoraproject.org/fedora as client
17+
18+
COPY ./entrypoint.sh /host/entrypoint.sh
19+
20+
RUN set -x \
21+
&& export CALLER_PATH=/host \
22+
&& /host/entrypoint.sh
23+
24+
ENTRYPOINT /host/entrypoint.sh
25+
26+
FROM client as server
27+
28+
RUN set -x \
29+
&& dnf install -y curl tmux socat
30+
31+
# TODO Caching
32+
# RUN bash -xec "$(grep 'pip install' /host/agi.py | head -n 1)"
33+
RUN set -x \
34+
&& python -m pip install -U pip setuptools wheel snoop openai openai-agents keyring keyrings-alt libtmux psutil
35+
36+
COPY server_motd /host/
37+
COPY openai_assistant_instructions.md /host/
38+
COPY agi.py /host/
39+
COPY util.sh /host/
40+
COPY entrypoint-server.sh /host/
41+
COPY entrypoint.sh /host/
42+
43+
RUN set -x \
44+
&& export CALLER_PATH=/host \
45+
&& bash -xe /host/entrypoint.sh
46+
47+
RUN set -x \
48+
&& mkdir -pv /var/run/alice-server/
49+
50+
COPY --from=builder_golang_agi_sshd /usr/src/app/agi_sshd /usr/bin/agi_sshd
51+
52+
# TODO Remove bash, sh and other shells
53+
54+
ENTRYPOINT ["/host/entrypoint-server.sh"]

github_webhook_events/agi_sshd.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"net/http"
2020
"os"
2121
"path/filepath"
22+
"strings"
2223
"sync"
2324

2425
"golang.org/x/crypto/ssh"
@@ -29,6 +30,7 @@ import (
2930
type forward struct {
3031
listener net.Listener
3132
localPath string
33+
rawPath string
3234
}
3335

3436
func main() {
@@ -126,7 +128,7 @@ func handleSSH(raw net.Conn, cfg *ssh.ServerConfig) {
126128
}
127129

128130
mu.Lock()
129-
forwards[base] = &forward{listener, localPath}
131+
forwards[base] = &forward{listener, localPath, p.SocketPath}
130132
count := len(forwards)
131133
mu.Unlock()
132134

@@ -210,6 +212,9 @@ func notifyAGI(ctx context.Context, mu *sync.Mutex, forwards map[string]*forward
210212
data := make(map[string]string, len(forwards))
211213
for base, f := range forwards {
212214
data[base] = f.localPath
215+
if strings.HasSuffix(f.rawPath, "input.sock") {
216+
data["client-side-input.sock"] = f.rawPath
217+
}
213218
}
214219
mu.Unlock()
215220

github_webhook_events/entrypoint-server.sh

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,12 @@ if [[ "x${CALLER_PATH}" = "x" ]]; then
55
fi
66
mkdir -p "${CALLER_PATH}"
77

8-
(
9-
while test 1; do
10-
for user in $(cat /var/run/alice-server/sshd.logs.txt 2>/dev/null | grep 'invalid user' | sed -e 's/.*invalid user //g' -e 's/ .*//g'); do
11-
found=$(grep -E "^${user}:" /etc/passwd);
12-
if [[ "x${found}" = "x" ]]; then
13-
useradd "${user}" 1>/dev/null 2>&1;
14-
fi;
15-
done;
16-
sleep 0.1;
17-
done
18-
) &
19-
208
source "${CALLER_PATH}/util.sh"
21-
# tail -F "${CALLER_PATH}/policy_engine.logs.txt" 2>/dev/null &
22-
# NO_CELERY=1 python -u ${CALLER_PATH}/policy_engine.py api --bind 127.0.0.1:0 --workers 1 1>"${CALLER_PATH}/policy_engine.logs.txt" 2>&1 &
23-
# export POLICY_ENGINE_PORT=$(grep 'Listening at' "${CALLER_PATH}/policy_engine.logs.txt" | sed -e 's/.*://g' -e 's/ .*//g')
24-
# until [[ "x${POLICY_ENGINE_PORT}" != "x" ]]; do export POLICY_ENGINE_PORT=$(grep 'Listening at' "${CALLER_PATH}/policy_engine.logs.txt" | sed -e 's/.*://g' -e 's/ .*//g'); done
25-
# echo "${POLICY_ENGINE_PORT}" > "${CALLER_PATH}/policy_engine_port.txt"
9+
10+
export AGI_SOCK="${CALLER_PATH}/agi.sock"
2611

2712
tail -F "${CALLER_PATH}/agi.logs.txt" 2>/dev/null &
28-
(cd "${CALLER_PATH}" && python -m uvicorn "agi:app" --uds "${CALLER_PATH}/agi.sock" 1>"${CALLER_PATH}/agi.logs.txt" 2>&1 &)
29-
# python -u ${CALLER_PATH}/agi.py --listen-unix "${CALLER_PATH}/agi.sock" 1>"${CALLER_PATH}/agi.logs.txt" 2>&1 &
30-
# export POLICY_ENGINE_PORT=$(grep 'Listening at' "${CALLER_PATH}/policy_engine.logs.txt" | sed -e 's/.*://g' -e 's/ .*//g')
31-
# until [[ "x${POLICY_ENGINE_PORT}" != "x" ]]; do export POLICY_ENGINE_PORT=$(grep 'Listening at' "${CALLER_PATH}/policy_engine.logs.txt" | sed -e 's/.*://g' -e 's/ .*//g'); done
32-
# echo "${POLICY_ENGINE_PORT}" > "${CALLER_PATH}/policy_engine_port.txt"
13+
(cd "${CALLER_PATH}" && python -m uvicorn "agi:app" --uds "${AGI_SOCK}" 1>"${CALLER_PATH}/agi.logs.txt" 2>&1 &)
3314

3415
mkdir -p /var/run/alice-server/
35-
/usr/sbin/sshd -D -E /var/run/alice-server/sshd.logs.txt -f /etc/ssh/sshd_config
16+
agi_sshd

0 commit comments

Comments
 (0)