Skip to content
pi-c Artifact [c93fc72fa8]
Login

Artifact [c93fc72fa8]

Artifact c93fc72fa842671c054d7c448fc8590328d1cd101004959645edd5cc505adf7d:


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

#include "model.h"

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


/* 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);
/* The cost as a Pi usage.cost JSON object, into out. */
void pic_cost_json(const pic_cost *c, char *out, size_t cap);

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

/* The model source models.c answers for (model.h); the composition root registers it. */
extern const pic_model_source pic_models_source;
/* 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);
/* The credential source a request for PROVIDER uses, as a phrase naming what it shadows (see
 * pic_cred_describe): "stored key (in use; ANTHROPIC_API_KEY also set)". "none" when nothing is set
 * and "unknown" for a provider pic cannot resolve. */
void pic_provider_source(const char *provider, char *out, size_t cap);
/* Calls fn for each provider pic knows, built-in ones first, then the models
 * file's, until it returns nonzero. 0 or -1. */
int pic_providers_each(int (*fn)(void *ctx, const char *provider), void *ctx);
/* 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