Skip to content
pi-c Documentation
Login

Documentation

/* 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);

/* Helpers skills.c shares: a growing string (sb_take: the text, "" when empty, NULL
 * after out of memory), a copy of N bytes, the frontmatter split (TEXT is
 * normalised in place; yaml is NULL when there is none, body trimmed), a
 * top-level frontmatter scalar (malloc'd or NULL), and output escapers. */
typedef struct {
    char *buf;
    size_t len, cap;
    int oom;
} pic_sbuf;
void pic_sb_add(pic_sbuf *b, const char *s, size_t n);
char *pic_sb_take(pic_sbuf *b);
char *pic_dup_n(const char *s, size_t n);
char *pic_front_value(const char *y, const char *key);
void pic_front_split(char *text, char **yaml, char **body);
void pic_json_str(FILE *out, const char *s);
void pic_flat(FILE *out, const char *s);

#endif