summaryrefslogtreecommitdiff
path: root/flake.nix
blob: 07fc0c94d789409c92b099078f691386c86dc235 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
{
  description = "slopbot - Zulip bot with stock price lookups and LLM replies";

  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
    agentd = {
      url = "github:bsima/agentd";
      inputs.nixpkgs.follows = "nixpkgs";
    };
  };

  outputs = { self, nixpkgs, agentd }:
    let
      system = "x86_64-linux";
      pkgs = nixpkgs.legacyPackages.${system};

      pythonEnv = pkgs.python3.withPackages (ps: [
        ps.zulip
        ps.yfinance
        ps.openai
        ps.requests
      ]);

      agent = pkgs.rustPlatform.buildRustPackage {
        pname = "agent";
        version = "0.1.0";
        src = agentd;
        cargoLock.lockFile = "${agentd}/Cargo.lock";
        cargoBuildFlags = [ "-p" "agent" ];
        doCheck = false;
      };

      researchPython = pkgs.python3.withPackages (ps: [
        ps.trafilatura
      ]);

      slopSearch = pkgs.writeShellApplication {
        name = "slop-search";
        runtimeInputs = [ pkgs.coreutils pkgs.curl pkgs.python3 ];
        text = ''
          if [ "$#" -eq 0 ]; then
            echo "usage: slop-search QUERY" >&2
            exit 2
          fi
          if [ -z "''${KAGI_API_KEY:-}" ]; then
            echo "error: KAGI_API_KEY is not set" >&2
            exit 1
          fi

          query="$*"
          encoded="$(${pkgs.python3}/bin/python -c 'import sys, urllib.parse; print(urllib.parse.urlencode({"q": " ".join(sys.argv[1:])}))' "$@")"

          tmp="$(mktemp)"
          trap 'rm -f "$tmp"' EXIT
          curl -fsS \
            -H "Authorization: Bot ''${KAGI_API_KEY}" \
            -H "Accept: application/json" \
            "https://kagi.com/api/v0/search?''${encoded}" \
            -o "$tmp"
          ${pkgs.python3}/bin/python - "$query" "$tmp" <<'PY'
import json
import sys

query, path = sys.argv[1], sys.argv[2]
payload = json.load(open(path))
items = payload.get("data") or []
results = []
for item in items:
    if not item.get("url"):
        continue
    item_type = item.get("t") or item.get("type") or ""
    if item_type and item_type != "search_result":
        continue
    results.append(item)
    if len(results) >= 8:
        break

print(f"QUERY: {query}\n")
for i, item in enumerate(results, 1):
    title = item.get("title") or "untitled"
    url = item.get("url") or ""
    snippet = item.get("snippet") or item.get("description") or ""
    print(f"[{i}] {title}\nURL: {url}\nSNIPPET: {snippet}\n")
PY
        '';
      };

      slopFetch = pkgs.writeShellApplication {
        name = "slop-fetch";
        runtimeInputs = [ pkgs.coreutils pkgs.curl researchPython ];
        text = ''
          if [ "$#" -ne 1 ]; then
            echo "usage: slop-fetch URL" >&2
            exit 2
          fi
          url="$1"
          tmp="$(mktemp)"
          trap 'rm -f "$tmp"' EXIT
          curl -fsSL --max-time 20 -A 'slopbot/1.0' "$url" -o "$tmp"
          ${researchPython}/bin/python - "$url" "$tmp" <<'PY'
import sys
import trafilatura

url, path = sys.argv[1], sys.argv[2]
html = open(path, "rb").read()
text = trafilatura.extract(html, url=url, include_comments=False, include_tables=False)
if not text:
    print(f"error: could not extract readable text from {url}", file=sys.stderr)
    sys.exit(1)
print(text[:12000])
PY
        '';
      };

      slopTools = pkgs.symlinkJoin {
        name = "slopbot-agent-tools";
        paths = [ slopSearch slopFetch ];
      };
    in
    {
      devShells.${system}.default = pkgs.mkShell {
        buildInputs = [ pythonEnv agent slopTools ];
        shellHook = ''
          echo "slopbot dev shell ready"
          echo "Run: python main.py"
        '';
      };

      checks.${system}.default = pkgs.runCommand "slopbot-tests" {
        buildInputs = [ pythonEnv ];
      } ''
        cp -r ${self} source
        cd source
        chmod -R u+w .
        python -m unittest discover -s tests
        touch $out
      '';

      packages.${system} = rec {
        default = slopbot;
        slopbot = pkgs.writeShellScriptBin "slopbot" ''
          cd "''${SLOPBOT_DIR:-/home/ben/src/bsima/slopbot}"
          export PATH=${slopTools}/bin
          export SLOPBOT_AGENT=${agent}/bin/agent
          export SLOPBOT_AGENT_PATH=${slopTools}/bin
          exec ${pythonEnv}/bin/python ${./main.py}
        '';
        inherit agent slopTools slopSearch slopFetch;
      };
    };
}