Skip to content

Commit

Permalink
Add a simple bb script runner
Browse files Browse the repository at this point in the history
* this can implement functions like read-file
  • Loading branch information
slimslenderslacks committed Sep 6, 2024
1 parent 44549bc commit bc41cb1
Show file tree
Hide file tree
Showing 10 changed files with 226 additions and 3 deletions.
33 changes: 33 additions & 0 deletions functions/bb/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

# syntax = docker/dockerfile:1.4
FROM nixos/nix:2.21.1@sha256:3f6c77ee4d2c82e472e64e6cd7087241dc391421a0b42c22e6849c586d5398d9 AS builder

WORKDIR /tmp/build
RUN mkdir /tmp/nix-store-closure

# ignore SC2046 because the output of nix-store -qR will never have spaces - this is safe here
# hadolint ignore=SC2046
RUN --mount=type=cache,target=/nix,from=nixos/nix:2.21.1,source=/nix \
--mount=type=cache,target=/root/.cache \
--mount=type=bind,target=/tmp/build \
<<EOF
nix \
--extra-experimental-features "nix-command flakes" \
--option filter-syscalls false \
--extra-trusted-substituters "https://cache.iog.io" \
--extra-trusted-public-keys "hydra.iohk.io:f/Ea+s+dFdN+3Y/G+FDgSq+a5NEWhJGzdjvKNGv0/EQ=" \
--show-trace \
--log-format raw \
build . --out-link /tmp/output/result
cp -R $(nix-store -qR /tmp/output/result) /tmp/nix-store-closure
EOF

FROM babashka/babashka:latest@sha256:9e0381fc4c78ee6ff12fd8836352cf343afba289aceb77e36129d92f30a92cc7

WORKDIR /app

COPY --from=builder /tmp/nix-store-closure /nix/store
COPY --from=builder /tmp/output/ /app/

ENTRYPOINT ["/app/result/bin/entrypoint"]
CMD ["--help"]
61 changes: 61 additions & 0 deletions functions/bb/flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 43 additions & 0 deletions functions/bb/flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
description = "{{tool}}";

inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-23.11";
flake-utils.url = "github:numtide/flake-utils";
};

outputs = { self, nixpkgs, flake-utils, ...}@inputs:

flake-utils.lib.eachDefaultSystem
(system:
let
pkgs = import nixpkgs {
inherit system;
};

in rec
{
packages = rec {

# this derivation just contains the init.clj script
scripts = pkgs.stdenv.mkDerivation {
name = "scripts";
src = ./.;
installPhase = ''
cp init.clj $out
'';
};

run-entrypoint = pkgs.writeShellScriptBin "entrypoint" ''
export SSL_CERT_FILE=${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt
/usr/local/bin/bb ${scripts} "$@"
'';

default = pkgs.buildEnv {
name = "bb";
paths = [ run-entrypoint ];
};
};
});
}

18 changes: 18 additions & 0 deletions functions/bb/init.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
(ns init
(:require
[cheshire.core]))

(def args {})

(try
(let [[json-string & extra-args] *command-line-args*
m (cheshire.core/parse-string json-string true)
script (first extra-args)]
(alter-var-root #'args (constantly m))
(println
(load-string script)))
(catch Throwable t
(binding [*out* *err*]
(println (str "Error: " (.getMessage t)))
(System/exit 1))))

15 changes: 15 additions & 0 deletions functions/bb/runbook.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

```sh
docker build -t vonwig/bb:latest .
```

```sh
docker run -it --rm -v ~/slimslenderslacks/flask-nix-example:/project \
--workdir /project vonwig/bb:latest \
'{"path": "./src/app.py"}' \
'(slurp (:path args))'
```

```sh
docker push vonwig/bb:latest
```
9 changes: 8 additions & 1 deletion functions/registry.edn
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,11 @@
:properties {:glob {:type "string",
:description "the glob pattern for files that should be found"}}},
:container {:image "vonwig/findutils:latest",
:command ["find" "." "-name"]}}]}
:command ["find" "." "-name"]}}
{:name "read-file",
:description "read a file",
:parameters {:type "object",
:properties {:path {:type "string",
:description "the file path"}}},
:container {:image "vonwig/bb:latest",
:command ["(slurp (:path args))"]}}]}
12 changes: 12 additions & 0 deletions functions/registry.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,16 @@ registry:
- find
- .
- -name
- name: read-file
description: read a file
parameters:
type: object
properties:
path:
type: string
description: the file path
container:
image: vonwig/bb:latest
command:
- "(slurp (:path args))"

8 changes: 8 additions & 0 deletions prompts/linting/pylint1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
tools:
- name: pylint
---

# prompt user

Run pylint with options `-f json` on the file `src/app.py` and tell me how many violations are in this file.
24 changes: 24 additions & 0 deletions prompts/linting/read-file.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
tools:
- name: read-file
---

# prompt user

Read the file `src/app.py` and tell me how many functions you see.

# #prompt user

Read the file `src/app.py` and tell me how many lines are in the file.

# #prompt user does not work!

Read the file `src/app.py` and then output the ranges of lines that each function occupies.
For example, if the function 'foo' is lines 10-2o, then the output should be:

```
{ "foo": [10,20] }
```



6 changes: 4 additions & 2 deletions src/prompts.clj
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
[f]
(->>
(-> (markdown/parse-metadata (metadata-file f)) first (select-keys [:tools :functions]) seq first second)
(mapcat (fn [m] (if-let [tool (#{"curl" "qrencode" "toilet" "figlet" "gh" "typos" "fzf" "jq" "fmpeg"} (:name m))]
(mapcat (fn [m] (if-let [tool (#{"curl" "qrencode" "toilet" "figlet" "gh" "typos" "fzf" "jq" "fmpeg" "pylint"} (:name m))]
[{:type "function"
:function
{:name (format "%s-manual" tool)
Expand Down Expand Up @@ -300,7 +300,9 @@
{:messages (concat prompts messages) :done finish-reason})))))
(catch Throwable ex
(let [c (async/promise-chan)]
(jsonrpc/notify :error {:content (format "not a valid prompt configuration: %s" (with-out-str (pprint opts))) :exception (str ex)})
(jsonrpc/notify :error {:content
(format "not a valid prompt configuration: %s" (with-out-str (pprint opts)))
:exception (str ex)})
(async/>! c {:messages [] :done "error"})
c))))

Expand Down

0 comments on commit bc41cb1

Please sign in to comment.