Skip to content

Commit 5eb976c

Browse files
low-level workdir metadata for non host-dir use cases
1 parent 59dd2bf commit 5eb976c

File tree

8 files changed

+42
-21
lines changed

8 files changed

+42
-21
lines changed

prompts/examples/git.md

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,27 +33,34 @@ tools:
3333
command:
3434
- --no-pager
3535
- "{{args|into}}"
36-
host-dir: /Users/slim/repo/bobloblaw
36+
- name: gh
37+
container:
38+
env:
39+
GITHUB_TOKEN: "{{pat}}"
40+
workdir: /thread/docker-test
3741
---
3842
-->
3943

4044
# Background
4145

4246
This shows how an agent can work with a _private_ git repository.
4347

44-
This tests that we can clone and make a change to a private repo. The git container mounts a prepared .ssh directory and .gitconfig file. In order to try this one, you'll need to do 3 things.
48+
This tests that we can clone and make a change to a private repo. The git container mounts a prepared .ssh directory and .gitconfig file. In order to try this one, you'll need to do 2 things.
4549

46-
1. Update the `host-dir` entry in the above comment to point at a empty directory. The agent is going to clone into this directory so start with an empty directory.
4750
1. Update the two mounts in git tool entry from the comment above. This is how you will provide credential access to the git container (read-only).
4851
2. Update the prompt in the section below to point at a private repo of your choosing.
4952

5053
The next section contains the prompt that will be sent to the agent. This is where the agent instructions begin.
5154

5255
# prompt user
5356

54-
1. use the git tool to clone [email protected]:slimslenderslacks/bobloblaw.git into the current directory. Do not create a new directory. It's okay if this
55-
fails because the repository is already cloned.
56-
2. use the append tool to write me a special message in the README.md file.
57-
3. use the git tool to commit the changes to the README.md file with the message "thankyou for being you".
58-
4. use the git tool to push the changes.
57+
Create a random branch named that starts with the string 'slim/', contains 5 random numbers or letters, and no spaces. I will refer to this as the branch name.
58+
59+
* use the git tool to clone [email protected]:slimslenderslacks/bobloblaw.git into the current directory. Do not create a new directory. It's okay if this
60+
fails because the repository is already cloned.
61+
* use the git tool to create a branch with the branch name described above.
62+
* use the append tool to write me a special message in the README.md file.
63+
* use the git tool to commit the changes to the README.md file with the message "thankyou for being you".
64+
* use the git tool to push the changes.
65+
* use the github cli to create a pull request for this branch name with the title "here's my secret message".
5966

src/docker.clj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@
108108
;; Tty wraps the process in a pseudo terminal
109109
{:StdinOnce true
110110
:OpenStdin true}
111-
(defn create-container [{:keys [image entrypoint working-dir command host-dir env thread-id opts mounts] :or {opts {:Tty true}} :as m}]
111+
(defn create-container [{:keys [image entrypoint workdir command host-dir env thread-id opts mounts] :or {opts {:Tty true}} :as m}]
112112
#_(jsonrpc/notify :message {:content (str m)})
113113
(let [payload (json/generate-string
114114
(merge
@@ -124,7 +124,7 @@
124124
(when host-dir [(format "%s:/project:rw" host-dir)])
125125
(when thread-id [(format "%s:/thread:rw" thread-id)])
126126
mounts)}
127-
:WorkingDir (or working-dir "/project")}
127+
:WorkingDir (or workdir "/project")}
128128
(when entrypoint {:Entrypoint entrypoint})
129129
(when command {:Cmd command})))]
130130
(curl/post

src/docker/main.clj

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,16 @@
1414
[logging :refer [warn]]
1515
prompts
1616
state
17+
trace
1718
user-loop)
1819
(:gen-class))
1920

2021
(set! *warn-on-reflection* true)
2122

22-
(defn- with-volume [f & {:keys [thread-id save-thread-volume]}]
23+
(defn- with-volume
24+
"callback with the thread-id for this conversation, make sure the thread volume exists
25+
and possibly remove the volume afterwards"
26+
[f & {:keys [thread-id save-thread-volume]}]
2327
(let [thread-id (or thread-id (str (random-uuid)))]
2428
(try
2529
(docker/thread-volume {:Name thread-id})
@@ -187,7 +191,8 @@
187191
m))))
188192
user-loop/state-reducer
189193
in
190-
{})))
194+
{}))
195+
(trace/dump))
191196
opts)))
192197
(fn []
193198
(:messages (prompts/get-prompts (with-options opts args))))))

src/git.clj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
:command (concat ["pull" "origin"]
6464
(when ref [ref]))}
6565
(if (string/starts-with? (str dir) "/prompts")
66-
{:working-dir (str dir)
66+
{:workdir (str dir)
6767
:mounts ["docker-prompts:/prompts:rw"]}
6868
{:host-dir (str dir)}))))
6969

@@ -72,7 +72,7 @@
7272
(merge
7373
{:image "alpine/git:latest"}
7474
(if (string/starts-with? (str dir) "/prompts")
75-
{:working-dir (str dir)
75+
{:workdir (str dir)
7676
:command (concat ["clone" "--depth" "1" (format "https://github.com/%s/%s" owner repo)]
7777
(when ref ["-b" ref])
7878
[(format "/prompts/%s" ref-hash)])

src/graph.clj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
[{:keys [messages functions metadata] {:keys [url model stream level]} :opts}]
3131
(let [[c h] (openai/chunk-handler)
3232
request (merge
33-
(dissoc metadata :agent :host-dir) ; TODO should we just select relevant keys instead of removing bad ones
33+
(dissoc metadata :agent :host-dir :workdir) ; TODO should we just select relevant keys instead of removing bad ones
3434
{:messages messages
3535
:level level}
3636
(when (seq functions) {:tools functions})
@@ -91,7 +91,7 @@
9191
(:opts state)
9292
(select-keys state [:functions])
9393
;; note that host-dir, if it exists, is an override here
94-
(select-keys (:metadata state) [:host-dir :timeout])))
94+
(select-keys (:metadata state) [:host-dir :timeout :workdir])))
9595
calls)
9696
(async/reduce conj []))))})))
9797

src/prompts.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@
9494
["{{raw|safe}}" "man"]}}}
9595
{:type "function"
9696
:function
97-
(merge
97+
(medley/deep-merge
9898
{:description (format "Run a %s command." tool)
9999
:parameters
100100
{:type "object"

src/tools.clj

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,12 @@
6666
{:command (interpolate-coll
6767
(-> definition :container :command)
6868
arg-context)}
69-
(when-let [wd (-> definition :container :working-dir)]
70-
{:working-dir (first (interpolate arg-context wd))}))
69+
;; workdirs in a container definition will always override ones
70+
;; set in the metadata
71+
(when-let [wd (or
72+
(-> definition :container :workdir)
73+
(:workdir defaults))]
74+
{:workdir (first (interpolate arg-context wd))}))
7175
(-> definition :stdin :file) (update-in [:stdin :file] (fn [s] (first (interpolate arg-context s)))))]
7276
(jsonrpc/notify
7377
:message

src/trace.clj

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
(ns trace
22
(:require
3-
[cheshire.core :as json]))
3+
[cheshire.core :as json]
4+
jsonrpc))
45

56
(def trace (atom {}))
67

78
(defn container-call [definition]
89
(swap! trace update-in [:container-calls] (fnil concat []) [(into {} (dissoc definition :prompts))]))
910

1011
(defn dump []
11-
(spit "trace.edn" (pr-str @trace)))
12+
(spit "trace.json" (try
13+
(json/generate-string @trace)
14+
(catch Throwable t
15+
(jsonrpc/notify :error {:content (str t)})))))
16+

0 commit comments

Comments
 (0)