summaryrefslogtreecommitdiff
path: root/catalog.lua
blob: d4b0c807bdeb20888580f05be0686c5e4219fe21 (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
local json = require('panto').ext.json
local M = {}
-- agy 1.5.23 cuts the middle out of any MCP tool result it would store above
-- 10,240 bytes (its built-in tool-output-max-bytes flag). Content blocks are
-- joined first and its own ~80-byte timestamp header counts. The model is
-- pointed at a spill file it has no tool to read, and no block type or setting
-- avoids the cut, so the relay sends larger text in parts: the first with the
-- result, the rest on request through this relay-only tool. docs/findings.md.
M.result_bytes = 10000
M.part_tool = {name='result_part',
    description='Return one later part of a tool result that was too large for a single reply. Use it only when a result says it has more parts.',
    inputSchema={type='object',properties={result={type='integer'},part={type='integer',minimum=1}},required={'result','part'}},
    _meta={['antigravity/alwaysLoad']=true}}
-- Consecutive pieces of at most result_bytes-400 bytes (room for the notice),
-- ending on a newline when the piece's second half has one and never inside a
-- UTF-8 character. table.concat(pages) == text.
function M.pages(text)
    local size, out, at = M.result_bytes - 400, {}, 1
    while #text - at + 1 > size do
        local stop = at + size - 1
        local nl = text:sub(at, stop):match('^.*()\n')
        if nl and nl > size // 2 then stop = at + nl - 1
        else for _ = 1, 3 do if (text:byte(stop + 1) or 0) & 0xC0 ~= 0x80 then break end; stop = stop - 1 end end
        out[#out + 1] = text:sub(at, stop); at = stop + 1
    end
    out[#out + 1] = text:sub(at); return out
end
-- What the model receives for part i of result n. The wording was checked
-- against live Hydrogen: it fetched every remaining part in one response.
function M.part(pages, n, i)
    if i > 1 then return ('[pantograph: part %d of %d of result %d]\n'):format(i, #pages, n) .. pages[i] end
    local bytes = 0; for _, p in ipairs(pages) do bytes = bytes + #p end
    return pages[1] .. ('\n\n[pantograph: this is part 1 of %d of result %d (%d bytes in all); the tool ran once and nothing was lost. Call result_part with result=%d and part=2%s for the rest, requesting every part you need in a single response.]')
        :format(#pages, n, bytes, n, #pages > 2 and ' through ' .. #pages or '')
end
function M.catalog(req, names)
    local all, by, list = {}, {}, json.array({})
    for _,t in ipairs(req.tools) do all[t.name]=t end
    if names==nil then
        names={}; for name in pairs(all) do names[#names+1]=name end; table.sort(names)
    end
    for i,name in ipairs(names) do
        local t=assert(all[name], 'Tool not in active rig: '..name); local s=json.decode(t.input_schema_json)
        assert(s.type=='object','Expected MCP object input schema for '..name)
        local wire=name:gsub('%.','__'); assert(wire:match('^[%w_-]+$') and #wire<=128 and not by[wire] and wire~=M.part_tool.name, 'Invalid or colliding MCP tool name: '..name)
        by[wire]={name=name,schema=s}; list[i]={name=wire,description=t.description,inputSchema=s,_meta={['antigravity/alwaysLoad']=true}}
    end
    return by,list
end

-- Advertise the exact extension schema. Reject unsupported assertions on use,
-- rather than dropping constraints or preventing unrelated tools from loading.
local known={type=true,properties=true,required=true,additionalProperties=true,items=true,minItems=true,maxItems=true,
    enum=true,minimum=true,maximum=true,description=true,title=true,default=true,examples=true,['$schema']=true}
local array_mt=getmetatable(json.array({}))
local function matches(s,v)
    if type(s)=='boolean' then return s end
    for k in pairs(s) do if not known[k] then return false,'Unsupported schema keyword: '..k end end
    local kind=type(v)
    if kind=='table' then kind=getmetatable(v)==array_mt and 'array' or 'object' elseif v==json.null then kind='null' end
    if s.type and s.type~=kind and not (s.type=='integer' and kind=='number' and v%1==0) then return false end
    if s.enum then
        local found=false; for _,x in ipairs(s.enum) do if json.encode(x)==json.encode(v) then found=true; break end end
        if not found then return false end
    end
    if kind=='object' then
        for _,k in ipairs(s.required or {}) do if v[k]==nil then return false end end
        for k,x in pairs(v) do
            local sub=(s.properties or {})[k]; if sub==nil then sub=s.additionalProperties end
            if sub~=nil then local ok,why=matches(sub,x); if not ok then return false,why end end
        end
    elseif kind=='array' then
        if s.minItems and #v<s.minItems or s.maxItems and #v>s.maxItems then return false end
        if s.items~=nil then for _,x in ipairs(v) do local ok,why=matches(s.items,x); if not ok then return false,why end end end
    elseif kind=='number' and (s.minimum and v<s.minimum or s.maximum and v>s.maximum) then return false end
    return true
end
M.arguments=matches
return M