/* The tool registry (toolreg.c): every tool a request can offer, core's own and those components * register, and the tools in play. Core registers read, bash, edit and write itself; the * composition root registers the bundled ones (grep, find and ls from search.c, send_message from * peer.c), and a component may register more at startup or later, for a session. Core runs with * none of them. * * What the registry holds is what durability and the request need and nothing about how a tool * works: the definition a provider is sent, whether a started call may run again after a crash, * whether it runs alone, its tier, and the function that runs it. Recovery reads the replay * safety recorded with each call (pic_effect.replay_safe), never the registry. */ #ifndef PIC_TOOLREG_H #define PIC_TOOLREG_H #include #include "pic.h" typedef enum { PIC_TIER_CORE, PIC_TIER_BUNDLED, PIC_TIER_EXTERNAL } pic_tool_tier; struct pic_tool_def { const char *name; /* The definition sent to providers: {"name","description","input_schema"}, as JSON. */ const char *schema; /* 1: a call that started and has no result after a crash runs again, so the tool must give * the same outcome twice (its plan, pic_tool_hooks). 0: such a call becomes "outcome * unknown" and is never run again. Recorded with the call when the reply is. */ int replay_safe; /* Pi's executionMode "sequential": a reply calling it runs all its calls one by one. */ int sequential; pic_tool_tier tier; /* One of pic's built-in tools: --no-builtin-tools leaves it out. */ int builtin; /* Whether it is in play when no flag or setting names it; NULL: only when chosen. */ int (*by_default)(void); /* Runs one call as pic_tool_run documents: hooks->started() exactly once before any external * effect (NULL when it fails, nothing run), then the result (malloc'd) and *IS_ERROR. The * call's JSON input is hooks->input; ARGS holds the fields pic's own tools read. TOOL is the * name called, so one function can serve several tools. */ char *(*run)(void *state, const char *workspace, const char *tool, const pic_tool_args *args, const pic_tool_hooks *hooks, int *is_error); void *state; /* 1: it changes nothing outside pic (read, grep): a permission mode never asks before it, and * read-only allows it (perm.h). */ int read_only; }; typedef struct pic_tool_def pic_tool_def; /* Adds DEF (kept by pointer: static storage, or alive until unregistered) after those already * there, which is the order requests declare tools in; one with the same name is replaced in its * place. A session freezes the tools it declares at its first request, so a tool registered later * reaches only sessions that start after it. 0, or -1 when the table is full or DEF has no name, * schema or run. */ int pic_tool_register(const pic_tool_def *def); /* What a deferred tool waits for (Pi's exposures): tool_search (deferred), which declares it for * the requests after, or a codemode script (codemode), the only caller that may run it: tool_search * never finds it and the model's own call to it is "not found". Codemode scripts call both kinds. */ typedef enum { PIC_DEFER_SEARCH, PIC_DEFER_CODEMODE } pic_defer_reach; /* pic_tool_register for a tool that must not take another's place: -1 when a tool of DEF's name * is registered already (DEF itself aside), and nothing changes. */ int pic_tool_register_new(const pic_tool_def *def); /* Marks the registered tool DEF as deferred: it is runnable and chosen by the tool flags as any * tool, but its schema is left out of the tools a session declares until a tool_search call * returns it (src/toolsearch.c), and codemode's description does not list it. 0, or -1 when DEF * is not registered. Registering the tool again (or unregistering it) clears the mark. */ int pic_tool_defer(const pic_tool_def *def, pic_defer_reach reach); /* Whether DEF is marked deferred, and waiting for codemode; whether any registered tool is, waiting * for REACH; and the deferred tools in play that tool_search finds (PIC_DEFER_SEARCH), in * declaration order. */ int pic_tool_deferred(const pic_tool_def *def); int pic_tool_codemode_only(const pic_tool_def *def); int pic_tool_has_deferred(pic_defer_reach reach); size_t pic_toolset_deferred(const pic_tool_def **out, size_t cap); /* For a tool whose call stands for other names in readOnlyTools (the MCP resource tools, a * project's MCP tools): FN gives the names a call of DEF with INPUT stands for, "mcp__SERVER__" for * each server it reaches, one per line (malloc'd; "" for none, NULL out of memory), so a * permission mode treats the call as read-only only when every one is named. 0, or -1 when DEF is * not registered. Registering the tool again (or unregistering it) clears it. */ int pic_tool_stand_for(const pic_tool_def *def, char *(*fn)(const pic_tool_def *def, const char *input)); /* What a call to DEF with INPUT stands for (malloc'd), or NULL when DEF has no such function. */ char *pic_tool_stands_for(const pic_tool_def *def, const char *input); /* Extra context describing the tool's source (for an MCP tool, its server's description and * instructions) that tool_search indexes alongside the tool's own description and schema. Kept by * pointer (alive until the tool is unregistered or registered again). 0, or -1 when DEF is not * registered. */ int pic_tool_search_text(const pic_tool_def *def, const char *text); /* The search text registered for DEF, or NULL. */ const char *pic_tool_get_search_text(const pic_tool_def *def); /* Removes the tool called NAME; 0, or -1 when there is none. */ int pic_tool_unregister(const char *name); /* The tool registered as NAME, in play or not; NULL when none. */ const pic_tool_def *pic_tool_named(const char *name); /* Whether a started call to TOOL may run again: its registration's replay_safe, 0 when none. */ int pic_tool_replay_safe(const char *tool); /* The model the next request goes to ("provider/id"), for a tool whose default depends on it * (web_search: offered when the model's provider can search itself). Set before a session freezes * its tools; a changed value makes the selection be made again. */ void pic_toolset_model(const char *model); const char *pic_toolset_current_model(void); /* "" when none was set */ /* The tools in play: by default the built-in tools that are on by default and every other * registered tool that is; --tools, --exclude-tools, --no-tools, --no-builtin-tools and the * defaultTools setting (SAVED, a comma list, or NULL) change them, by Pi's rules, over whatever is * registered when asked. Configure once before the first run; 0, or -1 with ERR set. */ int pic_toolset_configure(const char *tools, const char *exclude, int no_tools, int no_builtin, const char *saved, char *err, size_t errcap); int pic_toolset_on(const char *name); int pic_toolset_sequential(const char *name); size_t pic_toolset_names(const char **out, size_t cap); /* in declaration order */ char *pic_toolset_schemas(void); /* malloc'd JSON array, deferred tools left out */ /* Runs a call to TOOL with its registered function; a tool not in play, or one waiting for codemode * called other than from a script (hooks->nested 0), is "Tool X not found". */ char *pic_toolset_run(const char *workspace, const char *tool, const pic_tool_args *args, const pic_tool_hooks *hooks, int *is_error); /* Registers FN to be called at the start of each request, before its tools run (for per-request * tool state, such as MCP list_changed caps). 0, or -1 when the table is full or FN is NULL. */ int pic_tool_on_request(void (*fn)(void)); /* Calls every callback registered with pic_tool_on_request (called by loop.c per request). */ void pic_toolset_request_start(void); #endif