/* Cross-provider conversion, Pi's transformMessages rules in one place.
*
* A reply is replayed verbatim (its native form) only to the provider, API
* and model that produced it. Any other target gets it rebuilt from the
* neutral form: thinking is dropped (signatures are bound to the model that
* made them), and tool-call ids are rewritten to the target's limits by
* pic_norm_id, applied identically to the calls and to their results. */
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "pic.h"
/* 8 hex digits of FNV-1a: ids only need to be stable and distinct. */
static void short_hash(const char *s, char out[9]) {
uint32_t h = 2166136261u;
for (; *s; s++) {
h ^= (unsigned char)*s;
h *= 16777619u;
}
(void)snprintf(out, 9, "%08x", h);
}
/* Characters outside [A-Za-z0-9_-] become '_'. */
static void sanitize(char *s) {
for (; *s; s++) {
char c = *s;
if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_' || c == '-')) {
*s = '_';
}
}
}
/* pic_norm_id(id, target_api, target_provider): a foreign tool-call id as
* the target accepts it.
* anthropic-messages [A-Za-z0-9_-], at most 64
* openai-completions "call|item" ids become call_item; over 40 characters
* becomes prefix_hash; OpenAI itself caps at 40
* openai-responses [A-Za-z0-9_-], at most 64, no trailing '_'
* google-generative-ai [A-Za-z0-9_-], at most 64 (the default rule) */
static void norm_id(sqlite3_context *ctx, int argc, sqlite3_value **argv) {
const char *id = (const char *)sqlite3_value_text(argv[0]);
const char *api = (const char *)sqlite3_value_text(argv[1]);
const char *provider = argc > 2 ? (const char *)sqlite3_value_text(argv[2]) : "";
char buf[512], hash[9];
size_t n;
if (!id || !api) {
sqlite3_result_null(ctx);
return;
}
(void)snprintf(buf, sizeof buf, "%s", id);
if (strcmp(api, "openai-completions") == 0) {
char *bar = strchr(buf, '|');
if (bar) {
*bar = '_';
sanitize(buf);
if (strlen(buf) > 40) {
short_hash(id, hash);
buf[40 - 9] = '\0';
(void)snprintf(buf + strlen(buf), sizeof buf - strlen(buf), "_%s", hash);
}
} else if (provider && strcmp(provider, "openai") == 0 && strlen(buf) > 40) {
buf[40] = '\0';
}
sqlite3_result_text(ctx, buf, -1, SQLITE_TRANSIENT);
return;
}
sanitize(buf);
if (strlen(buf) > 64) {
buf[64] = '\0';
}
if (strcmp(api, "openai-responses") == 0) {
for (n = strlen(buf); n > 0 && buf[n - 1] == '_'; n--) {
buf[n - 1] = '\0';
}
}
sqlite3_result_text(ctx, buf, -1, SQLITE_TRANSIENT);
}
int pic_transform_register(sqlite3 *db) {
return sqlite3_create_function(db, "pic_norm_id", 3, SQLITE_UTF8 | SQLITE_DETERMINISTIC, NULL, norm_id, NULL,
NULL) == SQLITE_OK
? 0
: -1;
}
char *pic_neutral_from_anthropic(sqlite3 *db, const char *native) {
sqlite3_stmt *q = NULL;
char *out = NULL;
/* Block types pic does not map (server tools, compaction, …) stay in the
* native form only: the same model gets them back, others never see them. */
if (sqlite3_prepare_v2(
db,
"SELECT coalesce(json_group_array(json(x)), '[]') FROM (SELECT CASE json_extract(value,'$.type')"
" WHEN 'text' THEN json_object('type','text','text',json_extract(value,'$.text'))"
" WHEN 'thinking' THEN json_object('type','thinking','thinking',coalesce(json_extract(value,'$.thinking'),''),"
" 'signature',json_extract(value,'$.signature'))"
" WHEN 'redacted_thinking' THEN json_object('type','thinking','thinking','','signature',"
" json_extract(value,'$.data'),'redacted',json('true'))"
" WHEN 'tool_use' THEN json_object('type','toolCall','id',coalesce(json_extract(value,'$.id'),''),"
" 'name',coalesce(json_extract(value,'$.name'),''),'arguments',"
" CASE json_type(value,'$.input') WHEN 'object' THEN json(json_extract(value,'$.input'))"
" ELSE json_object('pic_invalid_input', json_extract(value,'$.input')) END)"
" END AS x FROM json_each(?1) ORDER BY key) WHERE x IS NOT NULL",
-1, &q, NULL) != SQLITE_OK) {
return NULL;
}
sqlite3_bind_text(q, 1, native, -1, SQLITE_STATIC);
if (sqlite3_step(q) == SQLITE_ROW && sqlite3_column_text(q, 0)) {
const char *t = (const char *)sqlite3_column_text(q, 0);
out = malloc(strlen(t) + 1);
if (out) {
memcpy(out, t, strlen(t) + 1);
}
}
sqlite3_finalize(q);
return out;
}