Skip to content

Commit 1a6fffe

Browse files
stable diffusion (#24)
* Add image generation service prompts * the top-level prompt uses a prompt tool with curl and docker * typo in the git ref * used a git prefix instead of a github one (hard to debug currently) * Add a local tunnel for opening files * if this is running, this would allow the agent to ask for it to be opened * Add fd to the tool hub * Build statically linked parser * Use backend.sock to get token
1 parent 5864f08 commit 1a6fffe

File tree

16 files changed

+566
-38
lines changed

16 files changed

+566
-38
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
**/result
33
.clj-kondo
44
.lsp
5+
/prompts/stable-diffusion/*.png

functions/hub/pull_tools.clj

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
[clojure.string :as string]
77
[selmer.parser :as selmer]))
88

9+
(def new-ones
10+
"fd")
11+
912
(def dumb
1013

1114
"docker-gc"
@@ -64,8 +67,6 @@
6467
;; the help menu off stderr
6568
"hclfmt"
6669
"fop" ;; xml formatter
67-
"dnstracer"
68-
"undocker"
6970
"dockfmt"
7071

7172
;; just doesn't have any help
@@ -263,6 +264,8 @@
263264

264265
(comment
265266

267+
(generate "fd")
268+
266269
(doseq [t all-tools]
267270
(generate t))
268271

functions/tree-sitter/.envrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
use flake

functions/tree-sitter/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/tree-sitter/flake.nix

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
{
2+
description = "tree-sitter";
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+
# darwin versus linux
23+
dylibExt = if nixpkgs.lib.hasInfix "darwin" system then "dylib" else "so";
24+
25+
a = pkgs.tree-sitter.withPlugins (p: [ p.tree-sitter-python p.tree-sitter-markdown ]);
26+
27+
markdown-grammar-source = pkgs.fetchFromGitHub {
28+
owner = "mdeiml";
29+
repo = "tree-sitter-markdown";
30+
rev = "28aa3baef73bd458d053b613b8bd10fd102b4405";
31+
sha256 = "sha256-HSjKYqjrJKPLbdq1UTvk/KnDqsIzVO7k5syCsIpAZpw=";
32+
};
33+
34+
# build the grammar but does this work for macos?
35+
markdown-grammar = (pkgs.tree-sitter.buildGrammar {
36+
language = "markdown";
37+
version = "0.0.1";
38+
src = "${markdown-grammar-source}/tree-sitter-markdown";
39+
});
40+
41+
# derive the parser
42+
parser = pkgs.stdenv.mkDerivation {
43+
name = "parser";
44+
src = ./.;
45+
nativeBuildInputs = [ pkgs.gcc
46+
pkgs.findutils
47+
pkgs.patchelf ];
48+
buildPhase = ''
49+
${pkgs.gcc}/bin/gcc -o parser \
50+
main.c \
51+
-I${markdown-grammar-source}/tree-sitter-markdown/src \
52+
-I${pkgs.tree-sitter}/include \
53+
${pkgs.tree-sitter}/lib/libtree-sitter.${dylibExt} \
54+
${markdown-grammar}/parser;
55+
'';
56+
57+
installPhase = ''
58+
mkdir -p $out/bin;
59+
mkdir -p $out/lib;
60+
cp parser $out/bin/parser;
61+
cp ${markdown-grammar}/parser $out/lib/parser;
62+
'';
63+
64+
fixupPhase = ''
65+
find $out -type f -exec patchelf --shrink-rpath '{}' \; -exec strip '{}' \; 2>/dev/null
66+
'';
67+
};
68+
69+
# this derivation just contains the init.clj script
70+
scripts = pkgs.stdenv.mkDerivation {
71+
name = "scripts";
72+
src = ./.;
73+
installPhase = ''
74+
cp init.clj $out
75+
'';
76+
};
77+
78+
# the script must have gh in the PATH
79+
default = pkgs.writeShellScriptBin "entrypoint" ''
80+
export PATH=${pkgs.lib.makeBinPath [parser]}
81+
export SSL_CERT_FILE=${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt
82+
${pkgs.babashka}/bin/bb ${scripts} "$@"
83+
'';
84+
};
85+
86+
devShells.default = pkgs.mkShell {
87+
packages = [ pkgs.tree-sitter pkgs.gcc ];
88+
};
89+
90+
});
91+
}

functions/tree-sitter/init.clj

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
(ns init
2+
(:require
3+
[babashka.process]
4+
[cheshire.core]))
5+
6+
(try
7+
(let [[json-string & args] *command-line-args*
8+
{llm-args :args} (cheshire.core/parse-string json-string true)]
9+
(println
10+
(-> (babashka.process/process
11+
{:out :string}
12+
(str "curl " llm-args))
13+
(deref)
14+
(babashka.process/check)
15+
:out)))
16+
(catch Throwable t
17+
(binding [*out* *err*]
18+
(println (str "Error: " (.getMessage t)))
19+
(System/exit 1))))

functions/tree-sitter/main.c

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
#include <tree_sitter/api.h>
5+
#include "tree_sitter/parser.h"
6+
7+
extern const TSLanguage *tree_sitter_markdown(void);
8+
9+
char *escape_double_quotes(const char *type) {
10+
char dq[] = "\"";
11+
int result = strcmp(type, dq);
12+
if (result == 0) {
13+
return "\\\"";
14+
} else {
15+
return type;
16+
}
17+
}
18+
19+
char *escape_back_slash(const char *type) {
20+
char dq[] = "\\";
21+
int result = strcmp(type, dq);
22+
if (result == 0) {
23+
return "\\\\";
24+
} else {
25+
return type;
26+
}
27+
}
28+
29+
// Function to print an S-expression with line and column information
30+
void print_sexp_with_position(TSNode node, const TSLanguage *lang, const char *source) {
31+
TSSymbol symbol = ts_node_symbol(node);
32+
const char *type = ts_language_symbol_name( lang, symbol);
33+
34+
// Get the start position of the node
35+
TSPoint start_point = ts_node_start_point(node);
36+
37+
// Get the end position of the node
38+
TSPoint end_point = ts_node_end_point(node);
39+
40+
char *escaped1 = escape_double_quotes(type);
41+
char *escaped = escape_back_slash(escaped1);
42+
43+
// Print the S-expression with line and column information
44+
printf("(\"%s\" \"%d:%d-%d:%d\" ", escaped, start_point.row + 1, start_point.column + 1, end_point.row + 1, end_point.column + 1);
45+
46+
// Recursively print child nodes
47+
for (uint32_t i = 0, child_count = ts_node_child_count(node); i < child_count; i++) {
48+
TSNode child = ts_node_child(node, i);
49+
print_sexp_with_position(child, lang, source);
50+
}
51+
52+
printf(")");
53+
}
54+
55+
int main(int argc, char *argv[]) {
56+
// Initialize the Tree-sitter library
57+
TSParser *parser = ts_parser_new();
58+
59+
// Load the Markdown language
60+
const TSLanguage *markdown_language = tree_sitter_markdown();
61+
62+
if (!markdown_language) {
63+
fprintf(stderr, "Error loading Markdown language\n");
64+
return 1;
65+
}
66+
67+
// Set the parser's language to Markdown
68+
ts_parser_set_language(parser, markdown_language);
69+
70+
// Read the Markdown input from a file (change this path to your Markdown file)
71+
/*FILE *input_file = fopen(argv[1], "r");*/
72+
/*if (!input_file) {*/
73+
/*fprintf(stderr, "Error opening input file\n");*/
74+
/*return 1;*/
75+
/*}*/
76+
77+
// Get the size of the input file
78+
/*fseek(input_file, 0, SEEK_END);*/
79+
/*long file_size = ftell(input_file);*/
80+
/*rewind(input_file);*/
81+
82+
// Read the file content into a buffer
83+
/*char *input_buffer = (char *)malloc(file_size + 1);*/
84+
/*fread(input_buffer, 1, file_size, input_file);*/
85+
86+
char *input_buffer = NULL;
87+
size_t buffer_size = 0;
88+
size_t file_size = 0;
89+
90+
while (1) {
91+
char chunk[1024]; // Read data in 1KB chunks
92+
93+
// Read a chunk of data from stdin
94+
size_t bytes_read = fread(chunk, 1, sizeof(chunk), stdin);
95+
96+
if (bytes_read == 0) {
97+
// No more data to read (EOF or error)
98+
break;
99+
}
100+
101+
// Resize the buffer to accommodate the new data
102+
file_size += bytes_read;
103+
input_buffer = (char *)realloc(input_buffer, file_size);
104+
105+
if (input_buffer == NULL) {
106+
perror("Memory allocation failed");
107+
exit(EXIT_FAILURE);
108+
}
109+
110+
// Copy the chunk into the buffer
111+
memcpy(input_buffer + file_size - bytes_read, chunk, bytes_read);
112+
}
113+
114+
input_buffer[file_size] = '\0';
115+
116+
// Create a Tree-sitter input document
117+
// TSInput input = ts_input_from_string(input_buffer);
118+
119+
// Parse the input
120+
TSTree *tree = ts_parser_parse_string(parser, NULL, input_buffer, file_size);
121+
122+
TSNode root_node = ts_tree_root_node(tree);
123+
124+
// Check if parsing was successful
125+
if (ts_node_is_null(root_node)) {
126+
fprintf(stderr, "Parsing failed\n");
127+
return 1;
128+
}
129+
130+
// Perform your desired operations with the parsed tree here
131+
// For example, you can traverse the tree and analyze the Markdown document's structure.
132+
const char *root_string = ts_node_string(root_node);
133+
print_sexp_with_position(root_node, markdown_language, input_buffer);
134+
135+
// Clean up resources
136+
ts_parser_delete(parser);
137+
//fclose(input_file);
138+
free(input_buffer);
139+
140+
return 0;
141+
}
142+

functions/tree-sitter/runbook.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
## Build
2+
3+
```sh
4+
nix build .#parser
5+
```
6+
7+
## Run
8+
9+
The parser will read from stdin and write the sexp to the stdin.
10+
11+
```sh
12+
./result/bin/parser < <(echo "## hello\n")
13+
```
14+

0 commit comments

Comments
 (0)