/* pic command line: ask for work, run it durably, inspect the journal. */
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#include "pic.h"
/* --context-window: replaces the model's window for automatic compaction (-1: keep). */
static long context_window = -1;
/* --json: Pi-style events on stdout; --stream: stream reply text even when
* stdout is not a terminal. */
static int json_events, force_stream;
static pic_events events;
static int usage(void) {
fputs("usage: pic [--workspace DIR] [--db PATH] [--model MODEL] ask [-c | --session ID] TEXT\n"
" run TEXT in the workspace (default: the current directory) and print the answer;\n"
" a new session, or -c to continue the latest, --session to continue that one;\n"
" the journal defaults to WORKSPACE/.pic/journal.sqlite\n"
" pic [options] sessions | log [ID] | name ID NAME | fork ID [--at ENTRY]\n"
" pic [options] compact [ID [INSTRUCTIONS]] summarize the conversation before its next request\n"
" pic [options] export [--since ENTRY] finished entries as JSON lines, for ctx\n"
" pic [options] submit KEY TEXT [--session ID] | cancel KEY\n"
" pic [options] run --script FILE\n"
" pic [options] run --provider anthropic [--api-url URL]\n"
" pic [options] dump | check | result CALL_ID\n"
"options: --workspace DIR --db PATH --model MODEL --exec-timeout SECONDS (bash default, 600)\n"
" --json (events on stdout, as Pi's JSON mode) --stream (stream the reply even when piped)\n"
" --context-window TOKENS (compact past this minus 16384; default: the model's, 0 never)\n",
stderr);
return 2;
}
static const char default_model[] = "anthropic/claude-opus-5-5";
/* WORKSPACE/.pic/journal.sqlite, creating .pic if needed. */
static int default_db(const char *workspace, char *out, size_t cap) {
char dir[PATH_MAX];
if (snprintf(dir, sizeof dir, "%s/.pic", workspace) >= (int)sizeof dir ||
snprintf(out, cap, "%s/journal.sqlite", dir) >= (int)cap) {
return -1;
}
if (mkdir(dir, 0700) != 0 && errno != EEXIST) {
return -1;
}
return 0;
}
static int print_result(pic_store *s, const char *id) {
sqlite3_stmt *q = NULL;
int rc = -1;
if (sqlite3_prepare_v2(s->db, "SELECT is_error, result FROM pic_effect WHERE id=?", -1, &q, NULL) != SQLITE_OK) {
return -1;
}
sqlite3_bind_int64(q, 1, strtoll(id, NULL, 10));
if (sqlite3_step(q) == SQLITE_ROW) {
printf("error=%d\n%s\n", sqlite3_column_int(q, 0),
sqlite3_column_text(q, 1) ? (const char *)sqlite3_column_text(q, 1) : "");
rc = 0;
} else {
(void)snprintf(s->err, sizeof s->err, "no tool call %s", id);
}
sqlite3_finalize(q);
return rc;
}
/* A provider for a model name ("provider/model") from the registry; url,
* when not NULL, replaces the model's endpoint. */
static int model_provider(pic_provider *p, const char *name, const char *url, const char *workspace, char *err,
size_t errcap) {
pic_model m;
if (pic_model_resolve(name, &m, err, errcap) != 0) {
return -1;
}
return pic_provider_for_model(p, &m, url, workspace, err, errcap);
}
/* submit under a fresh key, run with the model's provider, print the answer. */
static int ask(pic_store *s, const char *workspace, const char *model, const char *url, int exec_seconds,
const char *session, const char *text) {
char key[64], joined[64] = "";
pic_provider provider;
int duplicate = 0, rc;
struct timespec ts;
(void)clock_gettime(CLOCK_REALTIME, &ts);
(void)snprintf(key, sizeof key, "ask-%lld-%09ld-%ld", (long long)ts.tv_sec, ts.tv_nsec, (long)getpid());
memset(&provider, 0, sizeof provider);
if (pic_submit(s, key, text, session, &duplicate, joined, sizeof joined) != 0) {
return -1;
}
fprintf(stderr, "pic: session %s, request %s (pic cancel %s to stop it)\n", joined, key, key);
if (model_provider(&provider, model, url, workspace, s->err, sizeof s->err) != 0) {
if (provider.free) {
provider.free(&provider);
}
return -1;
}
pic_events_init(&events, stdout, stderr, json_events, !json_events && (force_stream || isatty(STDOUT_FILENO)));
s->events = &events;
if (context_window >= 0) {
provider.context_window = context_window;
}
rc = pic_run(s, workspace, &provider, exec_seconds);
provider.free(&provider);
/* Streamed or sent as events already; otherwise the answer now. */
if (rc == 0 && !json_events && !events.streamed) {
char *answer = pic_answer_text(s, key);
puts(answer && *answer ? answer : "(no text in the answer)");
free(answer);
}
return rc;
}
int main(int argc, char **argv) {
const char *db = NULL, *workspace = NULL, *model = default_model;
char cwd[PATH_MAX], db_path[PATH_MAX];
int exec_seconds = 600;
struct stat st;
pic_store s;
int i = 1, rc = 0;
while (i < argc && (strcmp(argv[i], "--json") == 0 || strcmp(argv[i], "--stream") == 0)) {
*(strcmp(argv[i], "--json") == 0 ? &json_events : &force_stream) = 1;
i++;
}
while (i + 1 < argc && strncmp(argv[i], "--", 2) == 0) {
if (strcmp(argv[i], "--json") == 0 || strcmp(argv[i], "--stream") == 0) {
*(strcmp(argv[i], "--json") == 0 ? &json_events : &force_stream) = 1;
i++;
continue;
}
if (strcmp(argv[i], "--db") == 0) {
db = argv[i + 1];
} else if (strcmp(argv[i], "--workspace") == 0) {
workspace = argv[i + 1];
} else if (strcmp(argv[i], "--model") == 0) {
model = argv[i + 1];
} else if (strcmp(argv[i], "--context-window") == 0) {
char *end = NULL;
long v = strtol(argv[i + 1], &end, 10);
if (!end || *end || v < 0) {
return usage();
}
context_window = v;
} else if (strcmp(argv[i], "--exec-timeout") == 0) {
char *end = NULL;
long v = strtol(argv[i + 1], &end, 10);
if (!end || *end || v < 1 || v > 86400) {
return usage();
}
exec_seconds = (int)v;
} else {
return usage();
}
i += 2;
}
if (i >= argc) {
return usage();
}
if (!workspace) {
if (!getcwd(cwd, sizeof cwd)) {
fprintf(stderr, "pic: cannot read the current directory\n");
return 1;
}
workspace = cwd;
}
/* A missing workspace would otherwise be recorded as a permanent tool error. */
if (stat(workspace, &st) != 0 || !S_ISDIR(st.st_mode)) {
fprintf(stderr, "pic: workspace is not a directory: %s\n", workspace);
return 1;
}
if (!db) {
if (default_db(workspace, db_path, sizeof db_path) != 0) {
fprintf(stderr, "pic: cannot create %s/.pic\n", workspace);
return 1;
}
db = db_path;
}
if (pic_open(&s, db) != 0) {
fprintf(stderr, "pic: %s\n", s.err);
pic_close(&s);
return 1;
}
if (strcmp(argv[i], "ask") == 0 && argc - i == 2) {
rc = ask(&s, workspace, model, NULL, exec_seconds, NULL, argv[i + 1]);
} else if (strcmp(argv[i], "ask") == 0 && argc - i == 3 && strcmp(argv[i + 1], "-c") == 0) {
rc = ask(&s, workspace, model, NULL, exec_seconds, "latest", argv[i + 2]);
} else if (strcmp(argv[i], "ask") == 0 && argc - i == 4 && strcmp(argv[i + 1], "--session") == 0) {
rc = ask(&s, workspace, model, NULL, exec_seconds, argv[i + 2], argv[i + 3]);
} else if (strcmp(argv[i], "submit") == 0 && (argc - i == 3 || (argc - i == 5 && strcmp(argv[i + 3], "--session") == 0))) {
int duplicate = 0;
char joined[64];
rc = pic_submit(&s, argv[i + 1], argv[i + 2], argc - i == 5 ? argv[i + 4] : NULL, &duplicate, joined,
sizeof joined);
if (rc == 0) {
puts(duplicate ? "duplicate: already admitted" : "admitted");
}
} else if (strcmp(argv[i], "compact") == 0 && argc - i <= 3) {
char id[64];
rc = pic_compact_request(&s, argc - i >= 2 ? argv[i + 1] : "latest", argc - i == 3 ? argv[i + 2] : NULL, id,
sizeof id);
if (rc == 0) {
printf("%s will be compacted before its next request\n", id);
}
} else if (strcmp(argv[i], "export") == 0 && (argc - i == 1 || (argc - i == 3 && strcmp(argv[i + 1], "--since") == 0))) {
char real[PATH_MAX];
rc = pic_export(&s, realpath(workspace, real) ? real : workspace, argc - i == 3 ? strtoll(argv[i + 2], NULL, 10) : 0,
stdout);
} else if (strcmp(argv[i], "sessions") == 0 && argc - i == 1) {
rc = pic_sessions(&s, stdout);
} else if (strcmp(argv[i], "log") == 0 && argc - i <= 2) {
rc = pic_session_log(&s, argc - i == 2 ? argv[i + 1] : "latest", stdout);
} else if (strcmp(argv[i], "name") == 0 && argc - i == 3) {
rc = pic_session_name(&s, argv[i + 1], argv[i + 2]);
} else if (strcmp(argv[i], "fork") == 0 && (argc - i == 2 || (argc - i == 4 && strcmp(argv[i + 2], "--at") == 0))) {
char made[64];
rc = pic_fork(&s, argv[i + 1], argc - i == 4 ? strtoll(argv[i + 3], NULL, 10) : 0, made, sizeof made);
if (rc == 0) {
puts(made);
}
} else if (strcmp(argv[i], "cancel") == 0 && argc - i == 2) {
rc = pic_cancel(&s, argv[i + 1]);
if (rc == 0) {
puts("cancelled");
}
} else if (strcmp(argv[i], "run") == 0) {
const char *provider_name = NULL, *script = NULL, *url = NULL;
pic_provider provider;
memset(&provider, 0, sizeof provider);
for (int j = i + 1; j < argc; j += 2) {
if (j + 1 >= argc) {
pic_close(&s);
return usage();
}
if (strcmp(argv[j], "--script") == 0) {
script = argv[j + 1];
} else if (strcmp(argv[j], "--provider") == 0) {
provider_name = argv[j + 1];
} else if (strcmp(argv[j], "--model") == 0) {
model = argv[j + 1];
} else if (strcmp(argv[j], "--api-url") == 0) {
url = argv[j + 1];
} else {
pic_close(&s);
return usage();
}
}
if (script && provider_name) {
pic_close(&s);
return usage();
} else if (script) {
rc = pic_provider_script(&provider, script);
} else if (!script) {
rc = model_provider(&provider, model, url, workspace, s.err, sizeof s.err);
} else {
pic_close(&s);
return usage();
}
if (rc == 0) {
if (context_window >= 0) {
provider.context_window = context_window;
}
if (json_events || force_stream) {
pic_events_init(&events, stdout, stderr, json_events, !json_events);
s.events = &events;
}
rc = pic_run(&s, workspace, &provider, exec_seconds);
}
if (provider.free) {
provider.free(&provider);
}
} else if (strcmp(argv[i], "dump") == 0 && argc - i == 1) {
rc = pic_dump(&s);
} else if (strcmp(argv[i], "result") == 0 && argc - i == 2) {
rc = print_result(&s, argv[i + 1]);
} else if (strcmp(argv[i], "check") == 0 && argc - i == 1) {
rc = pic_check(&s);
if (rc == 0) {
puts("check: ok");
}
} else {
pic_close(&s);
return usage();
}
if (rc != 0) {
fprintf(stderr, "pic: %s\n", s.err);
}
pic_close(&s);
return rc == 0 ? 0 : 1;
}