Skip to content
pi-c Artifact [469e3711dd]
Login

Artifact [469e3711dd]

Artifact 469e3711ddb1923335c0e4d8ea727b56ede90106dfda11f38d95fa4e469dd688:


/* The model a request names, as core uses it (model.c): what the registered
 * model source says a name means, and Pi's thinking levels over it. Which
 * providers exist, their endpoints, keys, limits, dialects and prices are
 * the source's (models.c, registered by the composition root); core asks
 * it and never decides them. */
#ifndef PIC_MODEL_H
#define PIC_MODEL_H

#include <stddef.h>

/* Dialect switches of a chat-completions endpoint, named after Pi's compat
 * flags; the source fills them and the adapters read them. */
typedef struct {
    int supports_store;              /* send store:false */
    int supports_developer_role;     /* developer role for reasoning models */
    int supports_usage_in_streaming; /* stream_options.include_usage */
    int supports_reasoning_effort;   /* send reasoning_effort */
    int max_tokens_field_legacy;     /* max_tokens instead of max_completion_tokens */
    int requires_reasoning_content;  /* empty reasoning_content on replayed turns */
    int cache_control;               /* the model takes cache_control blocks through a chat endpoint */
    int supports_long_cache;         /* Pi's supportsLongCacheRetention (default 1) */
    int force_adaptive_thinking;     /* thinking adaptive plus an effort, not a token budget */
    char thinking_format[24];        /* Pi's thinkingFormat: how the reasoning field is spelled */
} pic_compat;

/* Pi's thinking levels, lowest first. */
#define PIC_THINKING_LEVELS 7
extern const char *const pic_thinking_names[PIC_THINKING_LEVELS];

/* A model's thinkingLevelMap: per level absent, null (unsupported) or the
 * string the provider takes for it. */
typedef struct {
    signed char state[PIC_THINKING_LEVELS]; /* 0 absent, 1 null, 2 string */
    char value[PIC_THINKING_LEVELS][24];
} pic_thinking_map;

/* Pi's ModelCost: USD per million tokens, with request-wide tiers that
 * replace the rates when the request's input exceeds a threshold. */
typedef struct {
    double input, output, cache_read, cache_write;
} pic_rates;
#define PIC_COST_TIERS 8
typedef struct {
    pic_rates base;
    int ntiers;
    struct {
        double above; /* inputTokensAbove */
        pic_rates rates;
    } tier[PIC_COST_TIERS];
} pic_price;

typedef struct {
    char provider[64];
    char id[192];       /* the model id sent on the wire */
    char api[32];       /* the wire API's name, as Pi's models name it; its adapter is registered under it */
    char base_url[256]; /* without a trailing slash */
    char key_env[64];   /* variable holding the key; "" means no key */
    char key_literal[256]; /* a key written into models.json, if that is how it was given */
    char headers[1024]; /* extra "Name: value" lines, '\n' separated */
    int auth_basic;     /* the stored credential is user:password, sent as HTTP Basic (a saved endpoint's) */
    int reasoning;
    int image_input; /* Pi's input includes "image" (models.json default: text only) */
    long context_window, max_tokens;
    pic_compat compat;
    pic_thinking_map thinking_map;
    pic_price price;
} pic_model;

/* The model source: what a model name means, and a reply's price. The
 * composition root registers one; with none, no name resolves and replies
 * are journaled unpriced. */
typedef struct pic_model_source {
    /* Resolves NAME ("provider/model", or a bare model id). 0, or -1 with err. */
    int (*resolve)(const char *name, pic_model *out, char *err, size_t errcap);
    /* RAW_USAGE (the provider's usage object) with "pic_cost" and
     * "pic_tokens" added at M's price (malloc'd), or NULL. */
    char *(*priced)(const pic_model *m, const char *raw_usage);
} pic_model_source;

/* Registers S (static storage); a later call replaces it. Before any store opens. */
void pic_model_source_register(const pic_model_source *s);
/* The registered source's answers; -1 ("no model source") and NULL with none. */
int pic_model_resolve(const char *name, pic_model *out, char *err, size_t errcap);
char *pic_usage_priced(const pic_model *m, const char *raw_usage);

/* The index of LEVEL in pic_thinking_names, or -1. */
int pic_thinking_index(const char *level);
/* Pi's getSupportedThinkingLevels: the level names (into out, lowest first)
 * MODEL takes, and how many. Models without reasoning take only "off"; xhigh
 * and max only when the map defines them; levels mapped to null are out. */
int pic_model_thinking_levels(const pic_model *m, const char *out[PIC_THINKING_LEVELS]);
/* Pi's clampThinkingLevel: LEVEL if supported, else the nearest supported
 * level above it, else below (a name from pic_thinking_names). */
const char *pic_model_thinking_clamp(const pic_model *m, const char *level);
/* What the provider takes for LEVEL: 0 when the map has no entry, 1 when it
 * maps to null, 2 with *value set to the mapped string. */
int pic_model_thinking_map(const pic_model *m, const char *level, const char **value);
/* The effort string sent for LEVEL (not "off"): the map's string, else the
 * level's own name (Pi's thinkingLevelMap?.[level] ?? level). */
const char *pic_model_thinking_effort(const pic_model *m, const char *level);

#endif