Skip to content
pi-c Documentation
Login

Documentation

/* 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 <stddef.h>

#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);
/* 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). 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);
/* Whether any registered tool is deferred, and the deferred tools in play, in declaration order. */
int pic_tool_has_deferred(void);
size_t pic_toolset_deferred(const pic_tool_def **out, size_t cap);
/* 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 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 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);

#endif