| 1 |
|
defmodule Agent.Jsonl.Legacy do |
| 2 |
|
import File, only: [ls!: 1, dir?: 1, stat!: 1] |
| 3 |
|
import Path, only: [join: 2] |
| 4 |
|
import System, only: [get_env: 2] |
| 5 |
|
import String, only: [ends_with?: 2] |
| 6 |
|
import Enum, only: [filter: 2, map: 2, max_by: 3, count: 1] |
| 7 |
|
import Log, only: [trace: 1] |
| 8 |
|
|
| 9 |
:-( |
def find do |
| 10 |
:-( |
get_env("HOME", "~") |> setup() |
| 11 |
|
rescue |
| 12 |
:-( |
File.Error -> nil |
| 13 |
|
end |
| 14 |
|
|
| 15 |
|
defp setup(home) do |
| 16 |
:-( |
projects = join(home, ".claude/projects") |
| 17 |
:-( |
trace("watcher:home=#{home}\n") |
| 18 |
:-( |
trace("watcher:projects=#{projects}\n") |
| 19 |
:-( |
browse(projects) |
| 20 |
|
end |
| 21 |
|
|
| 22 |
|
defp browse(root) do |
| 23 |
:-( |
ready(dir?(root), root) |
| 24 |
|
end |
| 25 |
|
|
| 26 |
|
defp ready(true, root) do |
| 27 |
:-( |
collect(root) |> latest() |
| 28 |
|
end |
| 29 |
|
|
| 30 |
:-( |
defp ready(false, _), do: nil |
| 31 |
|
|
| 32 |
|
defp collect(root) do |
| 33 |
:-( |
dirs = ls!(root) |> map(&join(root, &1)) |> filter(&dir?/1) |
| 34 |
:-( |
trace("watcher:found #{count(dirs)} dirs\n") |
| 35 |
:-( |
dirs |
| 36 |
|
end |
| 37 |
|
|
| 38 |
|
defp latest(dirs) do |
| 39 |
:-( |
max_by(dirs, &mtime/1, fn -> nil end) |> file() |
| 40 |
|
end |
| 41 |
|
|
| 42 |
:-( |
defp file(nil), do: nil |
| 43 |
|
|
| 44 |
|
defp file(dir) do |
| 45 |
:-( |
trace("watcher:scanning dir=#{dir}\n") |
| 46 |
:-( |
jsons(dir) |
| 47 |
|
end |
| 48 |
|
|
| 49 |
|
defp jsons(dir) do |
| 50 |
:-( |
files = ls!(dir) |> filter(&ends_with?(&1, ".jsonl")) |
| 51 |
:-( |
trace("watcher:found #{count(files)} jsonl\n") |
| 52 |
:-( |
best(files, dir) |
| 53 |
|
end |
| 54 |
|
|
| 55 |
|
defp best(files, dir) do |
| 56 |
:-( |
max_by(files, &mtime(&1, dir), fn -> nil end) |> emit(dir) |
| 57 |
|
end |
| 58 |
|
|
| 59 |
:-( |
defp emit(nil, _) do |
| 60 |
:-( |
trace("watcher:no file\n") |
| 61 |
|
nil |
| 62 |
|
end |
| 63 |
|
|
| 64 |
|
defp emit(name, dir) do |
| 65 |
:-( |
path = join(dir, name) |
| 66 |
:-( |
trace("watcher:using #{path}\n") |
| 67 |
:-( |
path |
| 68 |
|
end |
| 69 |
|
|
| 70 |
|
defp mtime(path) do |
| 71 |
:-( |
stat!(path).mtime |
| 72 |
|
end |
| 73 |
|
|
| 74 |
|
defp mtime(name, dir) do |
| 75 |
:-( |
stat!(join(dir, name)).mtime |
| 76 |
|
end |
| 77 |
|
end |