Skip to content
pi-c Artifact [ab44fd01fa]
Login

Artifact [ab44fd01fa]

Artifact ab44fd01fa5b86e3b92c6fdc16ac070937362ba44b34e85f97bda454bdcd1e3a:


/* Model registry: which provider, wire API, endpoint and credential a model
 * name means. Names are "provider/model" as in Pi; built-in providers carry
 * Pi's base URLs, APIs and key variables, and ~/.pic/models.json (or Pi's
 * ~/.pi/agent/models.json) adds or overrides providers and models. */
#ifndef PIC_MODELS_H
#define PIC_MODELS_H

#include <stddef.h>

typedef enum { PIC_API_ANTHROPIC, PIC_API_CHAT, PIC_API_RESPONSES } pic_api;

/* openai-completions dialect switches, named after Pi's compat flags. */
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 (DeepSeek) */
    int anthropic_cache_control;     /* OpenRouter anthropic/... models take cache_control */
    int force_adaptive_thinking;     /* Anthropic: thinking adaptive plus an effort, not a token budget */
    char thinking_format[24];        /* openai, openrouter, deepseek, zai, qwen, together */
} 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;

/* A request's usage as Pi's calculateCost takes it: input excludes cached tokens. */
typedef struct {
    double input, output, cache_read, cache_write, cache_write_1h;
    double total; /* Pi's totalTokens (filled by pic_usage_cost) */
} pic_tokens;

/* A request's cost in USD; total is the provider's billed cost when reported. */
typedef struct {
    double input, output, cache_read, cache_write, total;
    int total_from_provider;
} pic_cost;

typedef struct {
    char provider[64];
    char id[192];       /* the model id sent on the wire */
    pic_api api;
    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 reasoning;
    long context_window, max_tokens;
    pic_compat compat;
    pic_thinking_map thinking_map;
    pic_price price;
} pic_model;

/* Pi's calculateCost (packages/ai/src/models.ts): the rates of the highest
 * tier whose threshold the request's input (input + cache read + cache write)
 * exceeds, else the base rates; 1h cache writes cost twice the input rate. */
void pic_cost_calc(const pic_price *price, const pic_tokens *t, pic_cost *out);
/* Pi's usage normalisation for the model's wire API from a provider's raw
 * usage object, then pic_cost_calc; for OpenRouter models the billed cost
 * replaces the total as Pi's applyOpenRouterReportedCost does. 0, or -1. */
int pic_usage_cost(const pic_model *m, const char *raw_usage, pic_cost *out, pic_tokens *tokens);
/* RAW_USAGE with a "pic_cost" object ({input,output,cacheRead,cacheWrite,
 * total}) and a "pic_tokens" object ({input,output,cacheRead,cacheWrite,
 * totalTokens}, Pi's normalised usage) added (malloc'd), or NULL. */
char *pic_usage_priced(const pic_model *m, const char *raw_usage);
/* The cost as a Pi usage.cost JSON object, into out. */
void pic_cost_json(const pic_cost *c, char *out, size_t cap);

/* 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);
/* The index of LEVEL in pic_thinking_names, or -1. */
int pic_thinking_index(const char *level);
/* The supported levels of a Pi model object (a catalog or models-file entry)
 * as a JSON array of names, for listings. */
void pic_model_thinking_json(const char *model_json, char *out, size_t cap);

/* Resolves NAME ("provider/model", or a bare built-in model id) using the
 * built-in table and the user's models file. 0, or -1 with err. */
int pic_model_resolve(const char *name, pic_model *out, char *err, size_t errcap);
const char *pic_api_name(pic_api api);
/* The request URL for a model: base URL plus the API's path. */
void pic_model_url(const pic_model *m, char *out, size_t cap);
/* Whether PROVIDER has a key configured (its variable set, or a key in the
 * models file), or needs none: Pi shows only these in its model picker. */
int pic_model_available(const char *provider);
/* Calls fn for each model the models file defines (providers.*.models[]). */
int pic_models_each_user(int (*fn)(void *ctx, const char *provider, const char *json), void *ctx);

#endif