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
|
local function tool_status(name)
name = name or "?"
return "tool:" .. name:sub(1, 27)
end
return {
name = "herdr",
activate = function()
local pane_id = os.getenv("HERDR_PANE_ID")
if not pane_id or pane_id == "" then return end
local panto = require("panto")
local uv = require("luv")
-- Herdr orders reports by source and sequence. Seed each process from
-- wall time so a restarted panto cannot be shadowed by the prior
-- process's larger sequence numbers.
local seq = os.time() * 1000000
local last_state
local last_status
local foreground_working = false
local foreground_status
local background_work = 0
local function spawn(args)
local handle
pcall(function()
handle = uv.spawn("herdr", { args = args, stdio = { nil, nil, nil } }, function()
if handle then handle:close() end
end)
end)
end
local function report(state, status)
if state == last_state and status == last_status then return end
last_state, last_status = state, status
seq = seq + 1
local args = {
"pane", "report-agent", pane_id,
"--source", "panto:extension",
"--agent", "panto",
"--state", state,
"--seq", tostring(seq),
}
if status then
args[#args + 1] = "--message"
args[#args + 1] = status
end
spawn(args)
end
local function release()
seq = seq + 1
spawn({
"pane", "release-agent", pane_id,
"--source", "panto:extension",
"--agent", "panto",
"--seq", tostring(seq),
})
end
local function update_idle_state()
if foreground_working or background_work > 0 then
report("working", foreground_working and foreground_status or "background")
else
report("idle")
end
end
local function foreground(status)
foreground_working, foreground_status = true, status
report("working", status)
end
panto.ext.on("session_start", function()
foreground_working, foreground_status, background_work = false, nil, 0
report("idle")
end)
panto.ext.on("session_ready", function()
foreground_working, foreground_status = false, nil
update_idle_state()
end)
panto.ext.on("turn_start", function() foreground("thinking") end)
panto.ext.on("agent_submission", function() foreground("thinking") end)
panto.ext.on("user_message", function() foreground("thinking") end)
panto.ext.on("thinking", function() foreground("thinking") end)
panto.ext.on("compaction", function() foreground("compacting") end)
panto.ext.on("tool_details", function(event)
foreground(tool_status(event.tool_name))
end)
panto.ext.on("assistant_text", function() foreground("responding") end)
panto.ext.on("turn_end", function()
foreground_working, foreground_status = false, nil
update_idle_state()
end)
panto.ext.on("background_work_start", function()
background_work = background_work + 1
update_idle_state()
end)
panto.ext.on("background_work_end", function()
background_work = math.max(0, background_work - 1)
update_idle_state()
end)
panto.ext.on("session_end", release)
end,
}
|