Artifact 767dabcf4ef8a460c16a948bed834d6d68f6d83de230078fd47ebe959de6337e:
- File
src/catalog.c
— part of check-in
[1011fc8472]
at
2026-11-19 23:29:07
on branch gemini
— A Google Gemini provider, as Pi's
The google-generative-ai API: streamGenerateContent over SSE with GEMINI_API_KEY, native parts replayed per conversation, tool calls and results, usage, and Pi's thinking mapping (thinkingLevel for Gemini 3 and later, a token budget for 2.5, 0 for off). A conversation can move between Gemini and the other providers turn by turn. Recorded streams cover text, thinking, tools, parallel calls, safety blocks, truncation and errors. (user: dmestas, size: 5152) [annotate] [blame] [check-ins using] [more...]
/* The model catalog: the built-in snapshot of Pi's catalog, with fresher * per-provider lists from pi.dev laid over it as Pi does * (reference/pi/packages/coding-agent/src/core/remote-catalog-provider.ts): * a stored list counts when its Last-Modified is newer than the snapshot, * and its models replace the snapshot's of the same id. The stored lists * are rows of picu_catalog in ~/.pic/pic.sqlite (userdb.h). */ #include <stdlib.h> #include <string.h> #include "catalog.h" #include "sqlite3.h" #include "userdb.h" #include "model_catalog.inc" const char *const pic_catalog_providers[PIC_CATALOG_NPROVIDERS] = {"anthropic", "openai", "openrouter", "xai", "groq", "cerebras", "deepseek", "together", "fireworks", "huggingface", "nvidia", "moonshotai", "zai", "google"}; /* A connection for JSON work: the user database, or a private in-memory one * when there is none (no home directory): then only the snapshot counts. */ static sqlite3 *json_db(int *have_store) { static sqlite3 *mem; sqlite3 *db = pic_userdb(NULL, 0); *have_store = db != NULL; if (!db && !mem && sqlite3_open(":memory:", &mem) != SQLITE_OK) { sqlite3_close(mem); mem = NULL; } return db ? db : mem; } /* The stored models of PROVIDER when newer than the snapshot, only the * chat models on APIs pic speaks (malloc'd JSON array), else NULL. */ static char *overlay(sqlite3 *db, const char *provider) { sqlite3_stmt *q = NULL; char *out = NULL; if (sqlite3_prepare_v2(db, "SELECT (SELECT json_group_array(json(m.value)) FROM json_each(t.models) m" " WHERE json_extract(m.value, '$.api') IN ('anthropic-messages','openai-completions','openai-responses','google-generative-ai')" " AND coalesce(json_extract(m.value, '$.type'), 'chat') = 'chat')" " FROM (SELECT CAST(CASE WHEN sz = length(models) THEN models ELSE sqlar_uncompress(models, sz) END AS TEXT)" " AS models FROM picu_catalog WHERE provider = ?1 AND last_modified_ms > ?2) t" " WHERE json_valid(t.models) AND json_type(t.models) = 'array'", -1, &q, NULL) != SQLITE_OK) { return NULL; } sqlite3_bind_text(q, 1, provider, -1, SQLITE_STATIC); sqlite3_bind_int64(q, 2, PIC_CATALOG_GENERATED_MS); if (sqlite3_step(q) == SQLITE_ROW && sqlite3_column_text(q, 0)) { out = strdup((const char *)sqlite3_column_text(q, 0)); } sqlite3_finalize(q); return out; } /* The model with ID in a JSON array of models (malloc'd), or NULL. */ static char *in_array(sqlite3 *db, const char *array, const char *id) { sqlite3_stmt *q = NULL; char *out = NULL; if (!array || !db || sqlite3_prepare_v2(db, "SELECT value FROM json_each(?1) WHERE json_extract(value, '$.id') = ?2 LIMIT 1", -1, &q, NULL) != SQLITE_OK) { return NULL; } sqlite3_bind_text(q, 1, array, -1, SQLITE_STATIC); sqlite3_bind_text(q, 2, id, -1, SQLITE_STATIC); if (sqlite3_step(q) == SQLITE_ROW) { out = strdup((const char *)sqlite3_column_text(q, 0)); } sqlite3_finalize(q); return out; } char *pic_catalog_find(const char *provider, const char *id) { int have_store; sqlite3 *db = json_db(&have_store); char *list = have_store ? overlay(db, provider) : NULL, *out = in_array(db, list, id); free(list); for (size_t i = 0; !out && i < sizeof catalog_snapshot / sizeof catalog_snapshot[0]; i++) { if (strcmp(catalog_snapshot[i].provider, provider) == 0 && strcmp(catalog_snapshot[i].id, id) == 0) { out = strdup(catalog_snapshot[i].json); } } return out; } int pic_catalog_each(int (*fn)(void *ctx, const char *provider, const char *json), void *ctx) { int have_store, stop = 0; sqlite3 *db = json_db(&have_store); for (size_t p = 0; !stop && p < PIC_CATALOG_NPROVIDERS; p++) { char *list = have_store ? overlay(db, pic_catalog_providers[p]) : NULL; sqlite3_stmt *q = NULL; /* The stored models first; the snapshot's for ids they lack. */ if (list && sqlite3_prepare_v2(db, "SELECT value FROM json_each(?1)", -1, &q, NULL) == SQLITE_OK) { sqlite3_bind_text(q, 1, list, -1, SQLITE_STATIC); while (!stop && sqlite3_step(q) == SQLITE_ROW) { stop = fn(ctx, pic_catalog_providers[p], (const char *)sqlite3_column_text(q, 0)); } sqlite3_finalize(q); } for (size_t i = 0; !stop && i < sizeof catalog_snapshot / sizeof catalog_snapshot[0]; i++) { char *dup; if (strcmp(catalog_snapshot[i].provider, pic_catalog_providers[p]) != 0) { continue; } dup = in_array(db, list, catalog_snapshot[i].id); if (!dup) { stop = fn(ctx, pic_catalog_providers[p], catalog_snapshot[i].json); } free(dup); } free(list); } return 0; }