Exported functions in state/lib/ai-models.ts. .md file to compare - side-by-side diff against ai-models
ai-models
description: "Triggers on prompt mention of 'ai-models'."
What it does for you
Connects your assistant to OpenAI so it can write, answer, and reason.
What it produces
A recent result, so you can see the kind of work it returns.
loading…
How to get it
These run inside the Snappy workspace. Want this working in your business? I set skills like this up with you, in one focused week.
For developers how this skill is built, graded, and how it runs
at a glance- the short version
what's inside - the parts that make up a skill 3/4 present
A skill is just a few plain-text files. Only the main one is required. The rest are optional, added as the work needs them. This is what the skill is made of; how it runs is just below.
state/skills/ai-models/SKILL.md
present
state/lib/ai-models.ts
present
state/bin/ai-models/
not present
state/skills/ai-models/AGENTS.md
present
how it's graded - what counts as a good run 4 criteria · 3 deterministic · 1 judge
Each row is one thing a good run has to get right. deterministic means a quick check decides, pass or fail. judge means the AI reads the result and rates it. Grading each piece on its own (instead of one overall score) shows exactly where a run fell short, so the fix is obvious.
how it runs - the shared frame every skill uses 4/5 present
Every skill runs the same way. One part does the work, a separate part checks it, and a short loader hands the AI exactly what it needs for the job. Anything this skill doesn't use shows a one-line note saying why, on purpose, not by accident.
This skill doesn't fix its own gaps yet.
state/log/pending-eval.ndjson - ALWAYS prefer state/lib/ai-models.ts (chatCompletion, embed, generateOpenAIImage) over rolling a fresh OpenAI client
- For image generation Robert prefers Nano Banana via state/bin/image/; only use generateOpenAIImage if that's explicitly the brief
- For cheap-labor dispatch use the current AI SDK/OpenRouter harness via state/lib/dispatch.ts, not OpenAI directly
what it has learned - fixes written back in over time sample
When a run hits something this skill didn't handle, the fix gets written back into the skill so it doesn't happen again. FIXED means it was corrected on the spot. LOGGED means it's queued for a bigger rewrite. Either way, the skill gets a little better and never makes the same mistake twice.
- Loading feedback rows…
how the work flows- who makes it, who checks it
import { chatCompletion, embed, generateOpenAIImage } from "state/lib/ai-models.ts"
npx tsx -e 'import("/Users/robertboulos/projects/snappy-os/state/lib/ai-models.ts").then(m => console.log(Object.keys(m)))'
SKILL.md- the skill, written out in plain English
ai-models
Direct OpenAI API for all snappy-* skills.
Ported from kernel snappy-ai-models in Phase 0.5. See state/lib/ai-models.ts for the full API surface.
Steps
chatCompletion()- seestate/lib/ai-models.tsembed()- seestate/lib/ai-models.tsgenerateOpenAIImage()- seestate/lib/ai-models.ts
Eval
Actor: the exported functions in state/lib/ai-models.ts. Auditor: none wired yet - eval is manual (Robert review). File a state/log/pending-eval.ndjson row on each run.
Score convention:
| Outcome | Score |
|---|---|
| Pass on first try | 1.0 |
| Failed first, auto-fix applied, re-check passed | 0.5 |
| Still failing or unrecoverable | 0.0 |
Gotchas
via the Phase 0.5 driver. Only these rewrites were applied: already in state/lib/)
realpathSync(process.argv[1])CLI guard wrapped in try/catch
- See the kernel SKILL.md for the original long-form guidance if you need it
(read-only reference at the kernel path above).
Graduation
This skill is prose. Graduate by defining a deterministic auditor and flipping eval: auto.
Rubric
criteria:
- name: chat_completion_response_valid
kind: deterministic
check: "The output of chatCompletion() must be a valid JSON object matching the expected OpenAI chat completion schema."
- name: embedding_output_structure
kind: deterministic
check: "The output of embed() must be an array of numbers, representing the embedding vector, or an object containing such a vector."
- name: image_generation_url_present
kind: deterministic
check: "The output of generateOpenAIImage() must contain a URL pointing to the generated image."
- name: api_error_handling
kind: judge
check: "The skill should gracefully handle common OpenAI API errors (e.g., rate limits, invalid input, authentication failures) and return informative error messages or codes."AGENTS.md- what the AI loads when this skill comes up
ai-models - loader
Per-turn rules for the ai-models skill. Full reference: state/skills/ai-models/SKILL.md. Do not skip these.
Critical Rules
_(no failures recorded yet - Phase 0.5 mechanical port from kernel snappy-ai-models. Read state/skills/ai-models/SKILL.md and state/lib/ai-models.ts before invoking.)_
- ALWAYS prefer
state/lib/ai-models.ts(chatCompletion,embed,generateOpenAIImage) over rolling a fresh OpenAI client - For image generation Robert prefers Nano Banana via
state/bin/image/; only usegenerateOpenAIImageif that's explicitly the brief - For cheap-labor dispatch use the current AI SDK/OpenRouter harness via
state/lib/dispatch.ts, not OpenAI directly
Commands
| ui model | live composition via compose_inline, persisted as artifact lang_body, reopened with OpenArtifact | |invoke: import { chatCompletion, embed, generateOpenAIImage } from "state/lib/ai-models.ts" |verify: npx tsx -e 'import("/Users/robertboulos/projects/snappy-os/state/lib/ai-models.ts").then(m => console.log(Object.keys(m)))' |eval log: state/log/pending-eval.ndjson (skill: "ai-models")
Known Pitfalls
- Phase 0.5 port - settings/load path patched to
./env.ts, no behavior changes - Credentials read via
env("OPENAI_API_KEY")- never hardcode
Self-Test
An agent reading this should correctly:
- [ ] Use the lib functions, not raw
fetch()to OpenAI - [ ] Pick Nano Banana /
state/bin/image/for image work unless told otherwise - [ ] Read the API key via
env(), not bash fallback
Found a gap? Edit this file. <!-- footer-injection-point -->
api.ts- the code it can call
#!/usr/bin/env npx tsx
/**
* snappy-ai-models/api.ts -- Direct OpenAI API for all snappy-* skills.
*
* Uses OPENAI_API_KEY from snappy-settings/.env.cache.
* Direct OpenAI REST API -- chat completions, embeddings, image generation.
*
* Usage:
* npx tsx api.ts chat "Explain quantum tunneling"
* npx tsx api.ts chat "Summarize this" --model gpt-4o
* npx tsx api.ts embed "What is Xano?"
*
* Or import as module:
* import { chatCompletion, embed, generateOpenAIImage } from "./ai-models.ts";
*/
import { env } from "./env.ts";
import { realpathSync } from "fs";
const BASE = "https://api.openai.com/v1";
const DEFAULT_MODEL = "gpt-4o-mini";
interface ChatOptions {
model?: string;
systemPrompt?: string;
temperature?: number;
maxTokens?: number;
}
interface ImageOptions {
model?: string;
size?: string;
}
async function openai(path: string, body: Record<string, unknown>): Promise<unknown> {
const res = await fetch(`${BASE}${path}`, {
method: "POST",
headers: {
Authorization: `Bearer ${env("OPENAI_API_KEY")}`,
"Content-Type": "application/json",
},
body: JSON.stringify(body),
});
if (!res.ok) {
const err = await res.text();
throw new Error(`OpenAI failed (${res.status}): ${err}`);
}
return res.json();
}
/** Token usage returned by billable OpenAI endpoints (embeddings/responses/chat). */
export interface OpenAiUsage {
prompt_tokens?: number;
completion_tokens?: number;
total_tokens?: number;
input_tokens?: number;
output_tokens?: number;
}
/** A typed OpenAI API result carrying the receipt fields the capability engine
* needs (request id, model, usage) so a connector run can build an honest
* receipt instead of scattering provider data across logs. */
export interface OpenAiApiResult {
ok: boolean;
status: number;
data: unknown;
/** OpenAI's x-request-id response header - the canonical request id. */
requestId: string | null;
/** The model the response reports (when present). */
model: string | null;
/** Token usage when the endpoint is billable (embeddings/responses/chat). */
usage: OpenAiUsage | null;
/** Safe product-facing error message when ok=false (no key/raw dumps). */
error?: string;
}
/** The ONE method-aware OpenAI API caller for capability-engine providers.
* GET for reads (models/files/batches), POST/DELETE for writes. Never throws -
* returns a typed OpenAiApiResult so the provider switch can shape a receipt or
* a first-class failure. Reuses OPENAI_API_KEY from .env.cache; the key is never
* returned. */
export async function openaiApiRequest(
method: "GET" | "POST" | "DELETE",
path: string,
body?: Record<string, unknown>,
): Promise<OpenAiApiResult> {
let key = "";
try {
key = env("OPENAI_API_KEY");
} catch {
return { ok: false, status: 0, data: null, requestId: null, model: null, usage: null, error: "OpenAI is not connected. Add your OpenAI API key to connect it." };
}
if (!key) {
return { ok: false, status: 0, data: null, requestId: null, model: null, usage: null, error: "OpenAI is not connected. Add your OpenAI API key to connect it." };
}
const headers: Record<string, string> = { Authorization: `Bearer ${key}` };
if (method !== "GET" && body !== undefined) headers["Content-Type"] = "application/json";
let res: Response;
try {
res = await fetch(`${BASE}${path.startsWith("/") ? path : `/${path}`}`, {
method,
headers,
body: method !== "GET" && body !== undefined ? JSON.stringify(body) : undefined,
signal: AbortSignal.timeout(30000),
});
} catch (e) {
return { ok: false, status: 0, data: null, requestId: null, model: null, usage: null, error: `OpenAI request could not be reached: ${e instanceof Error ? e.message : String(e)}` };
}
const requestId = res.headers.get("x-request-id");
const text = await res.text();
let data: unknown = text;
try { data = text ? JSON.parse(text) : null; } catch { /* keep raw text */ }
if (!res.ok) {
const apiMsg = (data && typeof data === "object" && (data as { error?: { message?: string } }).error?.message) || `OpenAI returned ${res.status}`;
return { ok: false, status: res.status, data, requestId, model: null, usage: null, error: String(apiMsg) };
}
const obj = data && typeof data === "object" ? data as Record<string, unknown> : {};
const model = typeof obj.model === "string" ? obj.model : null;
const usage = obj.usage && typeof obj.usage === "object" ? obj.usage as OpenAiUsage : null;
return { ok: true, status: res.status, data, requestId, model, usage };
}
// --- Public API ---
export async function chatCompletion(prompt: string, opts: ChatOptions = {}): Promise<{ text: string; raw: unknown }> {
const model = opts.model || DEFAULT_MODEL;
const messages: { role: string; content: string }[] = [];
if (opts.systemPrompt) messages.push({ role: "system", content: opts.systemPrompt });
messages.push({ role: "user", content: prompt });
const data = await openai("/chat/completions", {
model,
messages,
...(opts.temperature != null ? { temperature: opts.temperature } : {}),
...(opts.maxTokens ? { max_tokens: opts.maxTokens } : {}),
}) as any;
const text = data.choices?.[0]?.message?.content || "";
return { text, raw: data };
}
export async function embed(text: string, model = process.env.SNAPPY_EMBEDDING_MODEL || "text-embedding-3-small"): Promise<{ vector: number[]; raw: unknown }> {
const data = await openai("/embeddings", {
model,
input: text,
}) as any;
const vector = data.data?.[0]?.embedding || [];
return { vector, raw: data };
}
export async function generateOpenAIImage(prompt: string, opts: ImageOptions = {}): Promise<{ url: string; raw: unknown }> {
const model = opts.model || "gpt-image-1";
const data = await openai("/images/generations", {
model,
prompt,
size: opts.size || "1024x1024",
n: 1,
}) as any;
const url = data.data?.[0]?.url || data.data?.[0]?.b64_json || "";
return { url, raw: data };
}
// --- Model catalog (single source of truth for both harnesses) ----------
//
// 2026-05-19: per Robert's two-harness analysis - the picker should mirror
// what the harness can actually do. The catalog lives here so the cockpit,
// the parent dispatch, and RunSubagent all read from one place. Adding a
// model = one entry below + (if it's a new provider routing slug) the
// corresponding ALIASES row in state/lib/dispatch.ts. The cockpit picker
// reads /harness/models which dumps this catalog.
//
// Slug discipline: shortest unambiguous form ("haiku", "sonnet", "opus")
// for the user-facing options; the resolver maps to the wire-protocol
// model id ("claude-haiku-4-5-20251001" etc) used at spawn time.
export type ModelTier = "headline" | "fast" | "deep" | "experimental";
export interface ModelCatalogEntry {
slug: string;
label: string;
modelId: string;
provider: "anthropic" | "openrouter" | "openai" | "google";
tier: ModelTier;
description: string;
isDefault?: boolean;
}
export const MODEL_CATALOG: ModelCatalogEntry[] = [
// Anthropic direct (via ANTHROPIC_API_KEY)
{
slug: "haiku",
label: "Claude Haiku 4.5",
modelId: "claude-haiku-4-5-20251001",
provider: "anthropic",
tier: "fast",
description: "Default. Fast, cheap, good enough for most fan-out work. Use this unless a job genuinely needs more reasoning depth.",
isDefault: true,
},
{
slug: "sonnet",
label: "Claude Sonnet 4.6",
modelId: "claude-sonnet-4-6",
provider: "anthropic",
tier: "headline",
description: "Better at multi-step reasoning, cross-file refactors, and subtle type constraints. Use when haiku produces low-quality output.",
},
{
slug: "opus",
label: "Claude Opus 4.7",
modelId: "claude-opus-4-7",
provider: "anthropic",
tier: "deep",
description: "Highest quality, most expensive. Reserve for architectural work and tasks where rerun cost outweighs token cost.",
},
// OpenAI (via OpenRouter — wires through the same snappy harness)
{
slug: "gpt-5.5",
label: "GPT-5.5",
modelId: "openai/gpt-5.5",
provider: "openrouter",
tier: "headline",
description: "OpenAI's flagship via OpenRouter. Strong at structured reasoning and tool use. Routes through OPENROUTER_API_KEY.",
},
{
slug: "gpt-5.4",
label: "GPT-5.4",
modelId: "openai/gpt-5.4",
provider: "openrouter",
tier: "headline",
description: "Previous OpenAI flagship via OpenRouter. Sometimes cheaper than 5.5 with similar quality on shorter prompts.",
},
{
slug: "gpt-4.1",
label: "GPT-4.1",
modelId: "openai/gpt-4.1",
provider: "openrouter",
tier: "fast",
description: "OpenAI mid-tier. Good cost/quality balance for moderate complexity work.",
},
{
slug: "gpt-4.1-mini",
label: "GPT-4.1 mini",
modelId: "openai/gpt-4.1-mini",
provider: "openrouter",
tier: "fast",
description: "OpenAI cheap tier. Use when GPT-quality reasoning is needed but the budget is tight.",
},
{
slug: "o4-mini",
label: "o4-mini",
modelId: "openai/o4-mini",
provider: "openrouter",
tier: "deep",
description: "OpenAI reasoning model. Excellent for math, code, and multi-step logical tasks. Slower than chat models.",
},
// Google (via OpenRouter)
{
slug: "gemini-flash",
label: "Gemini 2.5 Flash",
modelId: "google/gemini-2.5-flash",
provider: "openrouter",
tier: "fast",
description: "Google's fast tier. Huge context window, low cost. Good for long-context work where Claude's window is tight.",
},
{
slug: "gemini-pro",
label: "Gemini 2.5 Pro",
modelId: "google/gemini-2.5-pro",
provider: "openrouter",
tier: "headline",
description: "Google's flagship. Strong at long-context reasoning, structured output, and multimodal work.",
},
// Open-weight (via OpenRouter)
{
slug: "llama",
label: "Llama 3.3 70B",
modelId: "meta-llama/llama-3.3-70b-instruct",
provider: "openrouter",
tier: "headline",
description: "Meta's open-weight flagship. Strong instruction following at competitive cost. Use when you want non-frontier coverage.",
},
{
slug: "qwen",
label: "Qwen 2.5 72B",
modelId: "qwen/qwen-2.5-72b-instruct",
provider: "openrouter",
tier: "headline",
description: "Alibaba's open-weight model. Especially strong at structured output and bilingual work.",
},
{
slug: "deepseek",
label: "DeepSeek Chat",
modelId: "deepseek/deepseek-chat",
provider: "openrouter",
tier: "headline",
description: "DeepSeek's open-weight model. Strong code generation; very cheap per token.",
},
];
/**
* The catalog entry for a given slug, or undefined if the slug is unknown.
* Both harnesses use this to validate operator picks before passing the slug
* downstream.
*/
export function getModelCatalogEntry(slug: string): ModelCatalogEntry | undefined {
return MODEL_CATALOG.find((e) => e.slug === slug);
}
/**
* Resolve a user-facing slug ("haiku" | "sonnet" | "opus" | ...) to the
* wire-protocol model id the spawned binary accepts via --model. Falls back
* to the default catalog entry when the slug is unknown.
*/
export function resolveModelSlug(slug: string): string {
const entry = getModelCatalogEntry(slug);
if (entry) return entry.modelId;
const defaultEntry = MODEL_CATALOG.find((e) => e.isDefault) ?? MODEL_CATALOG[0];
return defaultEntry.modelId;
}
/**
* True iff the slug exists in the catalog. Used by HTTP endpoints + the
* operator-settings validator.
*/
export function isKnownModelSlug(slug: unknown): slug is string {
return typeof slug === "string" && MODEL_CATALOG.some((e) => e.slug === slug);
}
/**
* The default slug, used when no operator override is set. Sourced from
* the catalog so changing the default = flipping one isDefault flag.
*/
export function defaultModelSlug(): string {
const entry = MODEL_CATALOG.find((e) => e.isDefault) ?? MODEL_CATALOG[0];
return entry.slug;
}
// --- CLI ---
if ((() => { try { return import.meta.url === `file://${realpathSync(process.argv[1])}`; } catch { return false; } })()) {
(async () => {
const [, , cmd, ...args] = process.argv;
switch (cmd) {
case "chat": {
const prompt = args.filter(a => !a.startsWith("--")).join(" ");
const modelIdx = args.indexOf("--model");
const model = modelIdx >= 0 ? args[modelIdx + 1] : undefined;
if (!prompt) { console.error("Usage: api.ts chat <prompt> [--model <model>]"); process.exit(1); }
const { text } = await chatCompletion(prompt, { model });
console.log(text);
break;
}
case "embed": {
const text = args.join(" ");
if (!text) { console.error("Usage: api.ts embed <text>"); process.exit(1); }
const { vector } = await embed(text);
console.log(`Dimensions: ${vector.length}`);
console.log(JSON.stringify(vector.slice(0, 5)) + "...");
break;
}
case "image": {
const prompt = args.filter(a => !a.startsWith("--")).join(" ");
const sizeIdx = args.indexOf("--size");
const size = sizeIdx >= 0 ? args[sizeIdx + 1] : undefined;
if (!prompt) { console.error("Usage: api.ts image <prompt> [--size <WxH>]"); process.exit(1); }
const { url } = await generateOpenAIImage(prompt, { size });
console.log(url);
break;
}
default:
console.log("Usage: npx tsx api.ts [chat|embed|image] ...");
}
})();
}
scripts- helper scripts it can run
prose-only skill - 1 inline code block live in SKILL.md above (no state/bin/ sidecar yet).
how we check it- the checks, plus the last 10 runs
no recent runs logged - the eval contract is declared but nothing has been graded yet