Skip to content

Commit

Permalink
Add lorax and background processes
Browse files Browse the repository at this point in the history
  • Loading branch information
slimslenderslacks committed Feb 5, 2025
1 parent a8f633c commit 865001b
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 41 deletions.
6 changes: 6 additions & 0 deletions prompts/catalog.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,9 @@ registry:
name: qrencode
prompts: 1
resources: {}
speculative_execution:
description: >
Use lorax from Claude. Lorax is a speculative execution tool that can be used
to make edits and run tools in an isolated sandbox.
ref: github:docker/labs-ai-tools-for-devs?path=prompts/lorax/speculative.md
icon: https://vonwig.github.io/prompts.docs/img/speculative_hu15687469033092380316.webp
64 changes: 33 additions & 31 deletions prompts/lorax/speculative.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
---
defs:
- lorax: &lorax
image: lorax:latest
entrypoint: lorax
model: claude-3-5-sonnet-20241022
tools:
- name: sandbox-source
Expand All @@ -8,34 +12,29 @@ tools:
properties:
host-dir:
type: string
user-source:
name:
type: string
container:
image: vonwig/speculative:latest
mounts:
- "{{host-dir|safe}}:/repo"
commands:
container:
<<: [*lorax]
command:
- sandbox
- source
- "-n"
- "{{user-source}}"
- /repo
- "{{name}}"
- "{{host-dir}}"
background: true
- name: sandbox-clone
description: create a sandbox for a host repo and respond with the id of the new sandbox
description: create a sandbox and respond with the id of the new sandbox
parameters:
type: object
properties:
sandbox-name:
type: string
user-source:
type: string
container:
image: vonwig/speculative:latest
commands:
<<: [*lorax]
command:
- sandbox
- clone
- "{{user-source}}"
- "--name"
- "{{sandbox-name}}"
- name: sandbox-snapshot
description: snapshot the current state of a sandbox
Expand All @@ -45,8 +44,8 @@ tools:
sandbox-id:
type: string
container:
image: vonwig/speculative:latest
commands:
<<: [*lorax]
command:
- sandbox
- snapshot
- "{{sandbox-id}}"
Expand All @@ -60,8 +59,8 @@ tools:
tree-id:
type: string
container:
image: vonwig/speculative:latest
commands:
<<: [*lorax]
command:
- sandbox
- restore
- "{{sandbox-id}}"
Expand All @@ -76,8 +75,8 @@ tools:
image:
type: string
container:
image: vonwig/speculative:latest
commands:
<<: [*lorax]
command:
- sandbox
- exec
- --mount-image
Expand All @@ -93,8 +92,8 @@ tools:
path:
type: string
container:
image: vonwig/speculative:latest
commands:
<<: [*lorax]
command:
- sandbox
- delete
- "{{sandbox-id}}"
Expand All @@ -109,8 +108,8 @@ tools:
path:
type: string
container:
image: vonwig/speculative:latest
commands:
<<: [*lorax]
command:
- sandbox
- rm
- "{{sandbox-id}}"
Expand All @@ -124,8 +123,8 @@ tools:
tree-id:
type: string
container:
image: vonwig/speculative:latest
commands:
<<: [*lorax]
command:
- sandbox
- diff
- "{{sandbox-id}}"
Expand All @@ -138,15 +137,18 @@ tools:
diff:
type: string
container:
image: vonwig/speculative:latest
commands:
<<: [*lorax]
command:
- sandbox
- apply
- user-source
- "{{diff}}"
---

# xprompt

Make a cloned repo available to the sandbox. The host repo is '/Users/slim/vonwig/altaservice' and it should be named atlas

# prompt

* create a sandbox for the host repo at '/Users/slim/vonwig/altaservice' and name it atlas
* execute
Clone the sandbox named atlas. We will get back the id of the new sandbox and we should remember that.
27 changes: 21 additions & 6 deletions src/docker.clj
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
[clojure.string :as string]
[creds]
jsonrpc
[jsonrpc.logger :as logger]
logging
schema)
(:import
[java.net UnixDomainSocketAddress]
[java.net URLEncoder UnixDomainSocketAddress]
[java.nio ByteBuffer]
[java.nio.channels SocketChannel]
[java.util Arrays Base64]))
Expand Down Expand Up @@ -109,7 +110,6 @@
{:StdinOnce true
:OpenStdin true}
(defn create-container [{:keys [image entrypoint workdir command host-dir env thread-id opts mounts volumes] :or {opts {:Tty true}}}]
#_(jsonrpc/notify :message {:content (str m)})
(let [payload (json/generate-string
(merge
{:Image image}
Expand All @@ -126,14 +126,15 @@
(or volumes mounts))}
:WorkingDir (or workdir "/project")}
(when entrypoint {:Entrypoint entrypoint})
(when command {:Cmd command})))]
(when command {:Cmd command})))
ascii-payload (String. (.getBytes payload "ASCII")) ]
(curl/post
"http://localhost/containers/create"
{:raw-args ["--unix-socket" "/var/run/docker.sock"]
:throw false
:body payload
:body ascii-payload
:headers {"Content-Type" "application/json"
"Content-Length" (count payload)}})))
"Content-Length" (count ascii-payload)}})))

(defn create-volume [{:keys [Name]}]
(curl/post "http://localhost/volumes/create"
Expand Down Expand Up @@ -308,6 +309,16 @@
:AttachStdin false}}
println)))

(defn run-background-function
"run container function with no stdin, and no streaming output"
[m]
(when (not (has-image? (:image m)))
(-pull m))
(let [x (create m)]
(start x)
;; TODO schedule shutdown hook
{:done :running}))

(defn run-function
"run container function with no stdin, and no streaming output"
[{:keys [timeout] :or {timeout 600000} :as m}]
Expand Down Expand Up @@ -439,8 +450,12 @@
returns ::container-response"
[m]
;; (schema/validate :schema/container-definition)
(if (-> m :stdin :file)
(cond
(-> m :stdin :file)
(run-with-stdin-content m)
(true? (:background m))
(run-background-function m)
:else
(run-function m)))

(defn get-login-info-from-desktop-backend
Expand Down
2 changes: 1 addition & 1 deletion src/graph.clj
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
(let [[chunk-handler sample] (providers (llm-provider (or (:model metadata) model)))
[c h] (chunk-handler)
request (merge
(dissoc metadata :agent :host-dir :workdir :prompt-format :description :name :parameter-values :arguments :resources) ; TODO should we just select relevant keys instead of removing bad ones
(dissoc metadata :agent :host-dir :workdir :prompt-format :description :name :parameter-values :arguments :resources :defs) ; TODO should we just select relevant keys instead of removing bad ones
{:messages messages
:level level}
(when (seq functions) {:tools functions})
Expand Down
4 changes: 2 additions & 2 deletions src/jsonrpc/server.clj
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@
(mapcat
(fn [[k v]] (map (partial entry->prompt-listing k v) (:messages v))))
(into []))}]
(logger/info prompts)
prompts))

(defmethod lsp.server/receive-request "prompts/get" [_ {:keys [db*]} {:keys [name arguments]}]
Expand All @@ -125,7 +124,6 @@
(vals)
(map #(select-keys % [:uri :name :description :mimeType])))
[])}]
(logger/info resources)
resources))

(defmethod lsp.server/receive-request "resources/read" [_ {:keys [db*]} params]
Expand Down Expand Up @@ -329,6 +327,7 @@
(->> params (lsp.server/send-notification server "notifications/prompts/list_changed")))

(publish-resource-list-changed [_ params]
(logger/info "send resource list changed")
(->> params (lsp.server/send-notification server "notifications/resources/list_changed")))

(publish-resource-updated [_ params]
Expand All @@ -338,6 +337,7 @@
(logger/info "send tool list changed")
(->> params (lsp.server/send-notification server "notifications/tools/list_changed")))
(publish-docker-notify [_ method params]
(logger/info (format "%s - %s" method params))
(lsp.server/send-notification server method params)))

(def registry "/prompts/registry.yaml")
Expand Down
2 changes: 1 addition & 1 deletion src/schema.clj
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
(s/def ::pty-output string?)
(s/def ::exit-code integer?)
(s/def ::info any?)
(s/def ::done #{:timeout :exited})
(s/def ::done #{:timeout :exited :running})
(s/def ::timeout integer?)
(s/def ::kill-container any?)
(s/def ::container-response (s/keys :req-un [::pty-output ::exit-code ::info ::done]
Expand Down
2 changes: 2 additions & 0 deletions src/tools.clj
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@
(resolve pty-output)
(and (= :exited done) (not exit-code-fail?))
(resolve "success")
(= :running done)
(resolve "success")
(and (= :exited done) exit-code-fail?)
(fail (format "call exited with non-zero code (%d): %s" exit-code pty-output))
(= :timeout done)
Expand Down

0 comments on commit 865001b

Please sign in to comment.