Artifact 423cbaca8a5605cd2b82ed4ae2442af3499b1768a3acf9e3f05ec6e21b739e01:
- File
src/prompts.h
— part of check-in
[bb88ac58ac]
at
2026-11-19 20:35:01
on branch templates
— Prompt templates, as Pi's
Markdown files in ~/.pic/prompts/ and, once the workspace is trusted, WORKSPACE/.pic/prompts/ become /name commands: Pi's frontmatter (description, argument-hint), shell-like arguments and substitutions ($1, $@, $ARGUMENTS, ${1:-default}, ${@:N}, ${@:N:L}). They expand where text becomes a request, so ask, chat and the terminal agree; the slash menu lists them, built-in commands win a name clash, and /reload re-reads them. pic prompts [--json] lists them. Files are read through the platform's input reader, which gains a directory listing. Fixes a pollfd array one entry too small when a catalog refresh runs during a reply. (user: dmestas, size: 1950) [annotate] [blame] [check-ins using] [more...]
/* Prompt templates, as Pi has them (prompts.c): Markdown files in * ~/.pic/prompts (user) and WORKSPACE/.pic/prompts (project, only when the * workspace is trusted). Typing /name ARGS sends the file's body with the * arguments substituted. */ #ifndef PIC_PROMPTS_H #define PIC_PROMPTS_H #include <stddef.h> #include <stdio.h> typedef struct { char *name; /* the file name without .md */ char *description; /* frontmatter, else the body's first line cut at 60 characters */ char *hint; /* frontmatter argument-hint, "" when none */ char *body; /* after the frontmatter */ char *path; int project; /* 1: WORKSPACE/.pic/prompts, 0: ~/.pic/prompts */ } pic_prompt; typedef struct { pic_prompt *v; size_t n; } pic_prompts; /* Loads the user's templates, then the workspace's when PROJECT (the caller * has checked trust). A name in both is listed twice; find returns the user's, * as Pi does. Unreadable files are skipped. Always 0 or -1 (out of memory). */ int pic_prompts_load(pic_prompts *p, const char *workspace, int project); void pic_prompts_free(pic_prompts *p); const pic_prompt *pic_prompts_find(const pic_prompts *p, const char *name); /* TEXT expanded when it is "/name ARGS" naming a template (malloc'd), else * NULL: the caller keeps TEXT unchanged. */ char *pic_prompts_expand(const pic_prompts *p, const char *text); /* The pieces, exposed for tests. Args are split like a shell's, quotes only * opening at the start of an argument. Both return malloc'd memory (NULL on * out of memory); free the args with pic_prompt_args_free. */ char **pic_prompt_args(const char *s, size_t *n); void pic_prompt_args_free(char **args, size_t n); char *pic_prompt_substitute(const char *content, char *const *args, size_t n); /* name, scope, hint, description, path per line (tab separated), or JSON. */ void pic_prompts_print(const pic_prompts *p, FILE *out, int json); #endif