Skip to content

Commit bc41cb1

Browse files
Add a simple bb script runner
* this can implement functions like read-file
1 parent 44549bc commit bc41cb1

File tree

10 files changed

+226
-3
lines changed

10 files changed

+226
-3
lines changed

functions/bb/Dockerfile

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
# syntax = docker/dockerfile:1.4
3+
FROM nixos/nix:2.21.1@sha256:3f6c77ee4d2c82e472e64e6cd7087241dc391421a0b42c22e6849c586d5398d9 AS builder
4+
5+
WORKDIR /tmp/build
6+
RUN mkdir /tmp/nix-store-closure
7+
8+
# ignore SC2046 because the output of nix-store -qR will never have spaces - this is safe here
9+
# hadolint ignore=SC2046
10+
RUN --mount=type=cache,target=/nix,from=nixos/nix:2.21.1,source=/nix \
11+
--mount=type=cache,target=/root/.cache \
12+
--mount=type=bind,target=/tmp/build \
13+
<<EOF
14+
nix \
15+
--extra-experimental-features "nix-command flakes" \
16+
--option filter-syscalls false \
17+
--extra-trusted-substituters "https://cache.iog.io" \
18+
--extra-trusted-public-keys "hydra.iohk.io:f/Ea+s+dFdN+3Y/G+FDgSq+a5NEWhJGzdjvKNGv0/EQ=" \
19+
--show-trace \
20+
--log-format raw \
21+
build . --out-link /tmp/output/result
22+
cp -R $(nix-store -qR /tmp/output/result) /tmp/nix-store-closure
23+
EOF
24+
25+
FROM babashka/babashka:latest@sha256:9e0381fc4c78ee6ff12fd8836352cf343afba289aceb77e36129d92f30a92cc7
26+
27+
WORKDIR /app
28+
29+
COPY --from=builder /tmp/nix-store-closure /nix/store
30+
COPY --from=builder /tmp/output/ /app/
31+
32+
ENTRYPOINT ["/app/result/bin/entrypoint"]
33+
CMD ["--help"]

functions/bb/flake.lock

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

functions/bb/flake.nix

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
description = "{{tool}}";
3+
4+
inputs = {
5+
nixpkgs.url = "github:NixOS/nixpkgs/nixos-23.11";
6+
flake-utils.url = "github:numtide/flake-utils";
7+
};
8+
9+
outputs = { self, nixpkgs, flake-utils, ...}@inputs:
10+
11+
flake-utils.lib.eachDefaultSystem
12+
(system:
13+
let
14+
pkgs = import nixpkgs {
15+
inherit system;
16+
};
17+
18+
in rec
19+
{
20+
packages = rec {
21+
22+
# this derivation just contains the init.clj script
23+
scripts = pkgs.stdenv.mkDerivation {
24+
name = "scripts";
25+
src = ./.;
26+
installPhase = ''
27+
cp init.clj $out
28+
'';
29+
};
30+
31+
run-entrypoint = pkgs.writeShellScriptBin "entrypoint" ''
32+
export SSL_CERT_FILE=${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt
33+
/usr/local/bin/bb ${scripts} "$@"
34+
'';
35+
36+
default = pkgs.buildEnv {
37+
name = "bb";
38+
paths = [ run-entrypoint ];
39+
};
40+
};
41+
});
42+
}
43+

functions/bb/init.clj

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
(ns init
2+
(:require
3+
[cheshire.core]))
4+
5+
(def args {})
6+
7+
(try
8+
(let [[json-string & extra-args] *command-line-args*
9+
m (cheshire.core/parse-string json-string true)
10+
script (first extra-args)]
11+
(alter-var-root #'args (constantly m))
12+
(println
13+
(load-string script)))
14+
(catch Throwable t
15+
(binding [*out* *err*]
16+
(println (str "Error: " (.getMessage t)))
17+
(System/exit 1))))
18+

functions/bb/runbook.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
```sh
3+
docker build -t vonwig/bb:latest .
4+
```
5+
6+
```sh
7+
docker run -it --rm -v ~/slimslenderslacks/flask-nix-example:/project \
8+
--workdir /project vonwig/bb:latest \
9+
'{"path": "./src/app.py"}' \
10+
'(slurp (:path args))'
11+
```
12+
13+
```sh
14+
docker push vonwig/bb:latest
15+
```

functions/registry.edn

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,11 @@
6363
:properties {:glob {:type "string",
6464
:description "the glob pattern for files that should be found"}}},
6565
:container {:image "vonwig/findutils:latest",
66-
:command ["find" "." "-name"]}}]}
66+
:command ["find" "." "-name"]}}
67+
{:name "read-file",
68+
:description "read a file",
69+
:parameters {:type "object",
70+
:properties {:path {:type "string",
71+
:description "the file path"}}},
72+
:container {:image "vonwig/bb:latest",
73+
:command ["(slurp (:path args))"]}}]}

functions/registry.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,16 @@ registry:
112112
- find
113113
- .
114114
- -name
115+
- name: read-file
116+
description: read a file
117+
parameters:
118+
type: object
119+
properties:
120+
path:
121+
type: string
122+
description: the file path
123+
container:
124+
image: vonwig/bb:latest
125+
command:
126+
- "(slurp (:path args))"
115127

prompts/linting/pylint1.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
tools:
3+
- name: pylint
4+
---
5+
6+
# prompt user
7+
8+
Run pylint with options `-f json` on the file `src/app.py` and tell me how many violations are in this file.

prompts/linting/read-file.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
tools:
3+
- name: read-file
4+
---
5+
6+
# prompt user
7+
8+
Read the file `src/app.py` and tell me how many functions you see.
9+
10+
# #prompt user
11+
12+
Read the file `src/app.py` and tell me how many lines are in the file.
13+
14+
# #prompt user does not work!
15+
16+
Read the file `src/app.py` and then output the ranges of lines that each function occupies.
17+
For example, if the function 'foo' is lines 10-2o, then the output should be:
18+
19+
```
20+
{ "foo": [10,20] }
21+
```
22+
23+
24+

src/prompts.clj

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@
107107
[f]
108108
(->>
109109
(-> (markdown/parse-metadata (metadata-file f)) first (select-keys [:tools :functions]) seq first second)
110-
(mapcat (fn [m] (if-let [tool (#{"curl" "qrencode" "toilet" "figlet" "gh" "typos" "fzf" "jq" "fmpeg"} (:name m))]
110+
(mapcat (fn [m] (if-let [tool (#{"curl" "qrencode" "toilet" "figlet" "gh" "typos" "fzf" "jq" "fmpeg" "pylint"} (:name m))]
111111
[{:type "function"
112112
:function
113113
{:name (format "%s-manual" tool)
@@ -300,7 +300,9 @@
300300
{:messages (concat prompts messages) :done finish-reason})))))
301301
(catch Throwable ex
302302
(let [c (async/promise-chan)]
303-
(jsonrpc/notify :error {:content (format "not a valid prompt configuration: %s" (with-out-str (pprint opts))) :exception (str ex)})
303+
(jsonrpc/notify :error {:content
304+
(format "not a valid prompt configuration: %s" (with-out-str (pprint opts)))
305+
:exception (str ex)})
304306
(async/>! c {:messages [] :done "error"})
305307
c))))
306308

0 commit comments

Comments
 (0)