| 1 |
|
defmodule Agent.Jsonl.Locate do |
| 2 |
|
import File, only: [dir?: 1, ls!: 1] |
| 3 |
|
import Path, only: [join: 2] |
| 4 |
|
import System, only: [get_env: 2] |
| 5 |
|
import String, only: [replace: 3, ends_with?: 2] |
| 6 |
|
import Enum, only: [filter: 2, at: 2] |
| 7 |
|
import Agent.Jsonl.Legacy, only: [find: 0] |
| 8 |
|
import Log, only: [trace: 1] |
| 9 |
|
|
| 10 |
|
def find(folder) when is_binary(folder) do |
| 11 |
:-( |
encoded = encode(folder) |
| 12 |
:-( |
trace("watcher:folder=#{folder}\n") |
| 13 |
:-( |
trace("watcher:encoded=#{encoded}\n") |
| 14 |
:-( |
direct(encoded) |
| 15 |
|
end |
| 16 |
|
|
| 17 |
|
def find(_) do |
| 18 |
:-( |
find() |
| 19 |
|
end |
| 20 |
|
|
| 21 |
|
defp encode(path) do |
| 22 |
:-( |
home = get_env("HOME", "~") |
| 23 |
:-( |
projects = join(home, ".claude/projects") |
| 24 |
:-( |
encoded = replace(path, "/", "-") |
| 25 |
:-( |
join(projects, encoded) |
| 26 |
|
end |
| 27 |
|
|
| 28 |
:-( |
defp direct(encoded) do |
| 29 |
:-( |
trace("watcher:target=#{encoded}\n") |
| 30 |
:-( |
accept(encoded, report(dir?(encoded))) |
| 31 |
|
rescue |
| 32 |
:-( |
File.Error -> nil |
| 33 |
|
end |
| 34 |
|
|
| 35 |
|
defp report(exists) do |
| 36 |
:-( |
trace("watcher:exists=#{exists}\n") |
| 37 |
:-( |
exists |
| 38 |
|
end |
| 39 |
|
|
| 40 |
|
defp accept(dir, true) do |
| 41 |
:-( |
trace("watcher:scanning=#{dir}\n") |
| 42 |
:-( |
ls!(dir) |> filter(&ends_with?(&1, ".jsonl")) |> pick(dir) |
| 43 |
|
end |
| 44 |
|
|
| 45 |
:-( |
defp accept(_, false) do |
| 46 |
:-( |
trace("watcher:wait for dir\n") |
| 47 |
|
nil |
| 48 |
|
end |
| 49 |
|
|
| 50 |
:-( |
defp pick([], _), do: nil |
| 51 |
|
|
| 52 |
|
defp pick(files, dir) do |
| 53 |
:-( |
path = join(dir, at(files, 0)) |
| 54 |
:-( |
trace("watcher:using #{path}\n") |
| 55 |
:-( |
path |
| 56 |
|
end |
| 57 |
|
end |