Files
Hermit/include/cst_tools.h
T
WHC 2fde898da0
ci / linux (clang) (push) Canceled after 0s
ci / linux (gcc) (push) Canceled after 0s
ci / sanitizers (push) Canceled after 0s
ci / musl (push) Canceled after 0s
ci / macos (push) Canceled after 0s
Usability pass: tool registry, TUI polish, saved keys and settings
Tools
- Table-driven tool registry with permission classes and default/minimal
  profiles; new tools: edit_file, list_dir, glob, grep, delete_file,
  move_file, todo_write, web_fetch, ask_user (plus read_file paging).
- Built-in Myers diff; write tools return unified diffs.
- Persistent permission rules (.agent/permissions.json), per-turn undo
  snapshots (.agent/undo), AGENTS.md project notes, auto-compact at 80%
  of the model context, cancellable run_shell with process-group kill.

TUI
- Table-driven slash commands: /provider /key /effort /status /tools
  /permissions /yolo /undo /retry /diff /export /copy /rename /delete
  /init /max-steps /todo, with / and @ completion.
- Permission modal with diff and prefix rules, ask_user modal, todo
  panel, header with model/effort/context, bracketed paste, line editing,
  wide-char cursor, NO_COLOR and ASCII fallback; Esc never quits.

Startup and configuration
- API keys are entered in the TUI (hidden) and saved per provider in
  <config>/credentials (0600); env variables are only a fallback.
- Last provider, per-provider model and reasoning effort are remembered
  in <config>/settings.json; --effort/-e and /effort send
  reasoning_effort (low|medium|high|max) to models that accept it.
- DeepSeek preset uses deepseek-flash / deepseek-v4-pro.
- Session flags --new/--continue/--resume/--list-sessions, --yolo,
  --confirm.

Compatibility and tests
- ncurses detection, macOS/Homebrew support, make install, XDG config
  dir, POSIX cleanups; Gitea CI (gcc/clang/asan/musl/macOS).
- Unit tests for diff, fs tools, registry, undo/perms and config;
  e2e fixtures multi_file_refactor and ask_or_todo; vendored cJSON.
2026-10-05 01:47:01 +08:00

108 lines
3.9 KiB
C

#ifndef CST_TOOLS_H
#define CST_TOOLS_H
#include "cst_agent.h"
#ifdef __cplusplus
extern "C" {
#endif
#define CST_TOOL_MAX_PARAMS 6
#define CST_TOOL_MAX_TARGETS 16
/* Up to max workspace-relative regular files (ignores .git, node_modules,
* root .gitignore ...). Caller frees each entry and the array. */
int cst_workspace_files(const char *ws, char ***out, int max);
/* What a call can touch; drives confirmation, undo and duplicate-read logic. */
typedef enum {
CST_TP_READ = 0, /* reads the workspace */
CST_TP_WRITE, /* changes files (snapshotted for /undo) */
CST_TP_SHELL, /* arbitrary commands */
CST_TP_NET, /* network access */
CST_TP_ASK, /* talks to the user */
CST_TP_META /* agent bookkeeping (todo list) */
} cst_tool_perm_t;
typedef struct {
const char *name;
const char *type; /* "string" | "integer" | "boolean" */
int required;
const char *desc;
} cst_tool_param_t;
typedef struct cst_tool_ctx {
const char *workspace_root;
/* ask_user: fills ans; returns 0, or -1 when nobody can answer. */
int (*ask)(const char *question, const char *options, char *ans,
size_t ans_sz, void *ud);
void *ask_ud;
/* todo_write: the list was replaced (text form, one item per line). */
void (*set_todo)(const char *text, void *ud);
void *todo_ud;
} cst_tool_ctx_t;
typedef int (*cst_tool_run_fn)(const cst_tool_ctx_t *ctx,
const ctl_tool_call_t *call,
cst_tool_result_t *r);
/* Write tools: compute the diff a call would produce without touching disk.
* Returns 0 and a malloc'd text, or -1 with an error text in *out. */
typedef int (*cst_tool_preview_fn)(const cst_tool_ctx_t *ctx,
const ctl_tool_call_t *call, char **out);
typedef struct {
const char *name;
const char *desc;
/* `--tools minimal` schema text (the original wording; required params
* only), so paper runs keep sending byte-identical requests. */
const char *min_desc;
cst_tool_param_t params[CST_TOOL_MAX_PARAMS];
cst_tool_perm_t perm;
int minimal; /* member of `--tools minimal` (the original four) */
cst_tool_run_fn run;
cst_tool_preview_fn preview;
} cst_tool_def_t;
typedef enum { CST_TOOLS_DEFAULT = 0, CST_TOOLS_MINIMAL } cst_tool_profile_t;
int cst_tool_profile_parse(const char *s, int *out);
const char *cst_tool_profile_name(int p);
int cst_tool_count(void);
const cst_tool_def_t *cst_tool_at(int i);
const cst_tool_def_t *cst_tool_find(const char *name);
int cst_tool_index(const char *name);
/* Enabled for this config (profile + /tools switches). cfg may be NULL. */
int cst_tool_active(const cst_agent_config_t *cfg, const cst_tool_def_t *t);
/* OpenAI tools[] for the active set (cJSON*, caller frees). */
void *cst_tools_schema_cfg(const cst_agent_config_t *cfg);
/* "Tools:\n- name(a, b?): ...\n" for the active set (malloc'd). */
char *cst_tools_prompt(const cst_agent_config_t *cfg);
/* Comma-separated active tool names (malloc'd). */
char *cst_tools_names(const cst_agent_config_t *cfg);
/* Missing required args / bad integers. 0 = ok. */
int cst_tool_validate(const cst_tool_def_t *t, const ctl_tool_call_t *call,
char *err, size_t err_sz);
int cst_tool_run(const cst_tool_ctx_t *ctx, const ctl_tool_call_t *call,
cst_tool_result_t *r);
int cst_tool_preview(const cst_tool_ctx_t *ctx, const ctl_tool_call_t *call,
char **out);
/* Workspace-relative files a write call may change (for /undo snapshots).
* Returns the count written into paths. */
int cst_tool_targets(const ctl_tool_call_t *call, char paths[][1024], int max);
/* Unified diff (3 lines of context, GNU `diff -u` layout) with
* `--- a/<label>` / `+++ b/<label>` headers. Returns malloc'd text; "" when
* identical. */
char *cst_diff_unified(const char *label, const char *a, size_t alen,
const char *b, size_t blen);
#ifdef __cplusplus
}
#endif
#endif