@mthines/reaper-mcp 0.79.1 → 0.79.2-beta.105.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +44 -44
- package/bridge-ui/mcp_bridge.html +38 -38
- package/package.json +1 -1
- package/reaper/mcp_bridge.lua +90 -26
package/package.json
CHANGED
package/reaper/mcp_bridge.lua
CHANGED
|
@@ -5100,29 +5100,72 @@ local function load_history()
|
|
|
5100
5100
|
end
|
|
5101
5101
|
|
|
5102
5102
|
-- Command log: every logged command is appended to history_log.jsonl (like bridge_events.jsonl)
|
|
5103
|
-
-- and reloaded on start, so the page's log survives a restart.
|
|
5104
|
-
--
|
|
5105
|
-
--
|
|
5106
|
-
--
|
|
5107
|
-
--
|
|
5108
|
-
|
|
5109
|
-
|
|
5110
|
-
|
|
5103
|
+
-- and reloaded on start, so the page's log survives a restart. The newest LOG_MEM_MAX entries
|
|
5104
|
+
-- stay in memory (and in that file, which always holds exactly them); when it grows past that,
|
|
5105
|
+
-- the oldest LOG_SEGMENT_MAX move into a segment file history_log_<first n>_<last n>_<last t>.jsonl.
|
|
5106
|
+
-- Segments are dropped once their newest entry is older than the week the timeline covers.
|
|
5107
|
+
-- The saved log is published as ExtState `log` (newest first, with `older` = segments exist) only
|
|
5108
|
+
-- when the page asks by writing ExtState `log_want` (on open, after a restart and on a gap): encoding
|
|
5109
|
+
-- thousands of entries on every change would cost the main thread for a key that is rarely read.
|
|
5110
|
+
-- Older entries come one segment per request: ExtState `log_older` = "<id>-<before n>" is answered
|
|
5111
|
+
-- with ExtState `log_page` = {req = id, entries (newest first), more}.
|
|
5112
|
+
-- (One table for the state and helpers: the main chunk is close to Lua's limit of 200 locals.)
|
|
5111
5113
|
local saved_log = {} -- newest first
|
|
5112
|
-
local
|
|
5113
|
-
|
|
5114
|
+
local logs = {
|
|
5115
|
+
file = bridge_dir .. "history_log.jsonl",
|
|
5116
|
+
mem_max = 2000,
|
|
5117
|
+
segment_max = 500,
|
|
5118
|
+
keep_ms = HISTORY_TIERS[#HISTORY_TIERS].keep,
|
|
5119
|
+
segments = {}, -- newest first: { first, last, t, path }
|
|
5120
|
+
last_want = "",
|
|
5121
|
+
last_older = "",
|
|
5122
|
+
}
|
|
5123
|
+
|
|
5124
|
+
function logs.scan()
|
|
5125
|
+
logs.segments = {}
|
|
5126
|
+
local cutoff = now_ms() - logs.keep_ms
|
|
5127
|
+
local i = 0
|
|
5128
|
+
while true do
|
|
5129
|
+
local fn = reaper.EnumerateFiles(bridge_dir, i)
|
|
5130
|
+
if not fn then break end
|
|
5131
|
+
local first, last, t = fn:match("^history_log_(%d+)_(%d+)_(%d+)%.jsonl$")
|
|
5132
|
+
if first then
|
|
5133
|
+
local path = bridge_dir .. fn
|
|
5134
|
+
if tonumber(t) < cutoff then os.remove(path)
|
|
5135
|
+
else logs.segments[#logs.segments + 1] = { first = tonumber(first), last = tonumber(last), t = tonumber(t), path = path } end
|
|
5136
|
+
end
|
|
5137
|
+
i = i + 1
|
|
5138
|
+
end
|
|
5139
|
+
table.sort(logs.segments, function(a, b) return a.first > b.first end)
|
|
5140
|
+
end
|
|
5114
5141
|
|
|
5115
|
-
|
|
5142
|
+
function logs.compact()
|
|
5116
5143
|
local lines = {}
|
|
5117
5144
|
for i = #saved_log, 1, -1 do lines[#lines + 1] = json_encode(saved_log[i]) end
|
|
5118
|
-
write_file(
|
|
5119
|
-
|
|
5145
|
+
write_file(logs.file, #lines > 0 and table.concat(lines, "\n") .. "\n" or "")
|
|
5146
|
+
end
|
|
5147
|
+
|
|
5148
|
+
-- Move the `count` oldest entries of saved_log into a segment file and rewrite the active file
|
|
5149
|
+
function logs.rotate(count)
|
|
5150
|
+
count = math.min(count, #saved_log)
|
|
5151
|
+
if count <= 0 then return end
|
|
5152
|
+
local lines = {}
|
|
5153
|
+
for i = #saved_log, #saved_log - count + 1, -1 do lines[#lines + 1] = json_encode(saved_log[i]) end
|
|
5154
|
+
local newest = saved_log[#saved_log - count + 1]
|
|
5155
|
+
local oldest = saved_log[#saved_log]
|
|
5156
|
+
local path = string.format("%shistory_log_%d_%d_%d.jsonl", bridge_dir, oldest.n, newest.n, newest.t)
|
|
5157
|
+
if write_file(path, table.concat(lines, "\n") .. "\n") then
|
|
5158
|
+
for _ = 1, count do saved_log[#saved_log] = nil end
|
|
5159
|
+
end
|
|
5160
|
+
logs.compact()
|
|
5161
|
+
logs.scan()
|
|
5120
5162
|
end
|
|
5121
5163
|
|
|
5122
5164
|
local function load_log()
|
|
5123
|
-
|
|
5165
|
+
logs.scan()
|
|
5166
|
+
local content = read_file(logs.file)
|
|
5124
5167
|
if not content or not plan_lib_ok then return end
|
|
5125
|
-
local cutoff = now_ms() -
|
|
5168
|
+
local cutoff = now_ms() - logs.keep_ms
|
|
5126
5169
|
local entries = {} -- oldest first, as written
|
|
5127
5170
|
for line in content:gmatch("[^\n]+") do
|
|
5128
5171
|
local ok, e = pcall(plan_lib.json_decode, line)
|
|
@@ -5130,27 +5173,48 @@ local function load_log()
|
|
|
5130
5173
|
entries[#entries + 1] = e
|
|
5131
5174
|
end
|
|
5132
5175
|
end
|
|
5133
|
-
for i = #entries,
|
|
5134
|
-
|
|
5176
|
+
for i = #entries, 1, -1 do saved_log[#saved_log + 1] = entries[i] end
|
|
5177
|
+
-- A file longer than memory holds (an older bridge, or a changed limit) moves its oldest entries to segments
|
|
5178
|
+
while #saved_log > logs.mem_max do logs.rotate(math.min(logs.segment_max, #saved_log - logs.mem_max)) end
|
|
5179
|
+
logs.compact()
|
|
5135
5180
|
end
|
|
5136
5181
|
|
|
5137
5182
|
local function append_log(entry)
|
|
5138
5183
|
table.insert(saved_log, 1, entry)
|
|
5139
|
-
saved_log
|
|
5140
|
-
|
|
5141
|
-
if log_appends >= LOG_KEEP_MAX then return compact_log() end
|
|
5142
|
-
local f = io.open(LOG_FILE, "a")
|
|
5184
|
+
if #saved_log > logs.mem_max then return logs.rotate(logs.segment_max) end
|
|
5185
|
+
local f = io.open(logs.file, "a")
|
|
5143
5186
|
if not f then return end
|
|
5144
5187
|
f:write(json_encode(entry) .. "\n")
|
|
5145
5188
|
f:close()
|
|
5146
5189
|
end
|
|
5147
5190
|
|
|
5148
5191
|
local function publish_log()
|
|
5149
|
-
local req = reaper.GetExtState(STATUS_SECTION, "log_want")
|
|
5150
|
-
if req
|
|
5151
|
-
|
|
5152
|
-
|
|
5153
|
-
|
|
5192
|
+
local req = reaper.GetExtState(STATUS_SECTION, "log_want") or ""
|
|
5193
|
+
if req ~= "" and req ~= logs.last_want then
|
|
5194
|
+
logs.last_want = req
|
|
5195
|
+
-- req echoes the page's request so it can tell this answer from an older one
|
|
5196
|
+
reaper.SetExtState(STATUS_SECTION, "log", json_encode({ v = 1, req = req, older = #logs.segments > 0, entries = saved_log }), false)
|
|
5197
|
+
end
|
|
5198
|
+
local older = reaper.GetExtState(STATUS_SECTION, "log_older") or ""
|
|
5199
|
+
if older ~= "" and older ~= logs.last_older then
|
|
5200
|
+
logs.last_older = older
|
|
5201
|
+
local id, before = older:match("^(%d+)%-(%d+)$")
|
|
5202
|
+
if not id or not plan_lib_ok then return end
|
|
5203
|
+
before = tonumber(before)
|
|
5204
|
+
local entries, more = {}, false
|
|
5205
|
+
for i, seg in ipairs(logs.segments) do
|
|
5206
|
+
if seg.last < before then
|
|
5207
|
+
local content = read_file(seg.path) or ""
|
|
5208
|
+
for line in content:gmatch("[^\n]+") do
|
|
5209
|
+
local ok, e = pcall(plan_lib.json_decode, line)
|
|
5210
|
+
if ok and type(e) == "table" and type(e.n) == "number" then table.insert(entries, 1, e) end
|
|
5211
|
+
end
|
|
5212
|
+
more = logs.segments[i + 1] ~= nil
|
|
5213
|
+
break
|
|
5214
|
+
end
|
|
5215
|
+
end
|
|
5216
|
+
reaper.SetExtState(STATUS_SECTION, "log_page", json_encode({ v = 1, req = id, more = more, entries = entries }), false)
|
|
5217
|
+
end
|
|
5154
5218
|
end
|
|
5155
5219
|
|
|
5156
5220
|
load_history()
|