/* A toy provider adapter, registered through provider_http.h alone: core * finds it by the model's API, opens the provider with it, asks it how long a * long-retention cache lives, rewrites ids by its rule and lays out content in * its shape. No bundled adapter is registered here. */ #include #include #include #include #include "pic.h" #include "provider_http.h" #include "sqlar.h" static int failures; static void expect(int ok, const char *what) { if (!ok) { fprintf(stderr, "FAIL: %s\n", what); failures++; } } static int toy_reply(pic_provider *p, pic_store *s, int64_t submission, int64_t turn, pic_reply *out, int *retryable, char *err, size_t errcap) { (void)p; (void)s; (void)submission; (void)turn; (void)err; (void)errcap; *retryable = 0; memset(out, 0, sizeof *out); out->neutral = pic_strdup("[{\"type\":\"text\",\"text\":\"toy\"}]"); out->native = pic_strdup(out->neutral); return 0; } static void toy_free(pic_provider *p) { p->state = NULL; } static int toy_open(pic_provider *p, const pic_model *m, const char *url, const char *workspace, char *err, size_t errcap) { (void)url; (void)workspace; (void)err; (void)errcap; p->reply = toy_reply; p->summarize = NULL; p->free = toy_free; p->state = NULL; p->context_window = m->context_window; return 0; } static long toy_cache_long(const pic_model *m) { (void)m; return 42; } /* Upper case, at most 8 characters. */ static void toy_norm_id(char *buf, size_t cap, const char *id, const char *provider) { (void)cap; (void)id; (void)provider; for (char *c = buf; *c; c++) { *c = (char)toupper((unsigned char)*c); } if (strlen(buf) > 8) { buf[8] = '\0'; } } static void toy_image(sqlite3_str *out, const char *mime, const char *data) { sqlite3_str_appendf(out, "{\"toy\":\"%s\",\"b64\":\"%s\"}", mime, data); } static const pic_content_shape toy_shapes[] = { {"toy-user", PIC_SHAPE_BLOCKS, 0, 0, 0, 0, "toy_text", toy_image}, {"toy-tool", PIC_SHAPE_TEXT, 1, 0, 0, 0, NULL, toy_image}, }; static const pic_provider_adapter toy = { "toy-wire", toy_open, toy_cache_long, toy_norm_id, toy_shapes, 2, NULL, }; static char *one(sqlite3 *db, const char *sql) { sqlite3_stmt *q = NULL; char *out = NULL; if (sqlite3_prepare_v2(db, sql, -1, &q, NULL) == SQLITE_OK) { int rc = sqlite3_step(q); if (rc == SQLITE_ROW && sqlite3_column_text(q, 0)) { out = pic_strdup((const char *)sqlite3_column_text(q, 0)); } else if (rc == SQLITE_ERROR) { out = pic_strdup(sqlite3_errmsg(db)); } } sqlite3_finalize(q); return out; } static void expect_sql(sqlite3 *db, const char *sql, const char *want) { char *got = one(db, sql); if (!got || strcmp(got, want) != 0) { fprintf(stderr, "FAIL: %s\n got %s\n want %s\n", sql, got ? got : "(null)", want); failures++; } free(got); } int main(void) { pic_model m; pic_provider p; pic_reply r; char err[256] = ""; int retryable = 1; sqlite3 *db = NULL; (void)setenv("PI_CACHE_RETENTION", "long", 1); memset(&m, 0, sizeof m); (void)snprintf(m.provider, sizeof m.provider, "toyco"); (void)snprintf(m.id, sizeof m.id, "t1"); (void)snprintf(m.api, sizeof m.api, "toy-wire"); m.context_window = 1000; m.compat.supports_long_cache = 1; /* Nothing registered: core runs, and says no adapter speaks the API. */ expect(pic_provider_adapter_named("toy-wire") == NULL, "no adapter before registration"); memset(&p, 0, sizeof p); expect(pic_provider_for_model(&p, &m, NULL, NULL, "/", err, sizeof err) != 0, "open without an adapter fails"); expect(strstr(err, "no adapter") != NULL, "the error says there is no adapter"); expect(pic_cache_seconds(&m) == 3600, "long with no adapter to say otherwise is an hour"); expect(pic_provider_register(&toy) == 0, "register"); expect(pic_provider_adapter_named("toy-wire") == &toy, "found by wire name"); expect(pic_provider_adapter_named("other-wire") == NULL, "other APIs stay unregistered"); memset(&p, 0, sizeof p); err[0] = '\0'; expect(pic_provider_for_model(&p, &m, NULL, NULL, "/", err, sizeof err) == 0, "open through the adapter"); expect(p.reply == toy_reply && p.context_window == 1000, "the adapter made the provider"); expect(p.cache_seconds == 42, "long retention takes the adapter's lifetime"); expect(strcmp(p.model, "toyco/t1") == 0, "core names the model"); expect(p.reply(&p, NULL, 1, 0, &r, &retryable, err, sizeof err) == 0 && retryable == 0 && strcmp(r.neutral, "[{\"type\":\"text\",\"text\":\"toy\"}]") == 0, "the reply comes back neutral"); free(r.neutral); free(r.native); p.free(&p); m.compat.supports_long_cache = 0; expect(pic_cache_seconds(&m) == 300, "a model without long retention stays short"); /* Ids and shapes through the transform layer's SQL functions. */ if (sqlite3_open(":memory:", &db) != SQLITE_OK || pic_transform_register(db) != 0 || pic_sqlar_register(db) != 0) { fprintf(stderr, "FAIL: open a database\n"); return 1; } expect_sql(db, "SELECT pic_norm_id('call.abcdefghij','toy-wire','toyco')", "CALL.ABC"); expect_sql(db, "SELECT pic_norm_id('call.abcdefghij','no-such-wire','x')", "call_abcdefghij"); if (sqlite3_exec(db, "CREATE TABLE pic_image(hash TEXT, mime TEXT, data BLOB, sz INTEGER);" "CREATE TABLE pic_attachment(id INTEGER PRIMARY KEY, entry_seq INTEGER, image TEXT, ordinal INTEGER);" "INSERT INTO pic_image VALUES('h','image/png',CAST('abc' AS BLOB),3);" "INSERT INTO pic_attachment VALUES(1,7,'h',0);", NULL, NULL, NULL) != SQLITE_OK) { fprintf(stderr, "FAIL: tables: %s\n", sqlite3_errmsg(db)); return 1; } expect_sql(db, "SELECT pic_content(7,'hello','toy-user',1,0)", "[{\"type\":\"toy_text\",\"text\":\"hello\"},{\"toy\":\"image/png\",\"b64\":\"YWJj\"}]"); expect_sql(db, "SELECT pic_content(7,'out','toy-tool',1,0)", "\"out\""); expect_sql(db, "SELECT pic_content(7,'hello','toy-user',0,0)", "[{\"type\":\"toy_text\",\"text\":\"hello\"},{\"type\":\"toy_text\",\"text\":\"" PIC_IMAGE_USER_PLACEHOLDER "\"}]"); expect_sql(db, "SELECT pic_content(7,'hello','pi',0,0)", "[{\"type\":\"text\",\"text\":\"hello\"},{\"type\":\"image\",\"data\":\"YWJj\",\"mimeType\":\"image/png\"}]"); expect_sql(db, "SELECT pic_content(7,'hello','no-such-shape',1,0)", "pic_content: no such shape"); sqlite3_close(db); if (failures) { fprintf(stderr, "test_adapter: %d failure(s)\n", failures); return 1; } printf("adapter: a toy adapter works through the interface alone\n"); return 0; }