@skyphusion/create-prism 1.3.1 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/template/MIGRATIONS.md +4 -0
- package/template/README.md +52 -39
- package/template/docs/legal/INSTANCE-PRIVACY.md +7 -0
- package/template/migrations/0006_chat_cost.sql +22 -0
- package/template/migrations/0007_funnel_counters.sql +38 -0
- package/template/package.json +3 -3
- package/template/schema.sql +27 -2
- package/template/src/auth.ts +4 -0
- package/template/src/control-plane.ts +7 -0
- package/template/src/funnel.ts +96 -0
- package/template/src/longrun-params.ts +2 -0
- package/template/src/models.ts +105 -50
- package/template/src/parsers/workers-ai-sse.ts +1 -1
- package/template/src/pricing.ts +128 -0
- package/template/src/routes/chat.ts +49 -27
- package/template/src/routes/conversations.ts +3 -1
- package/template/src/routes/workflow.ts +3 -0
- package/template/src/version.ts +1 -1
- package/template/tests/catalog-drift.test.ts +103 -0
- package/template/tests/catalog-refresh-2026-12.test.ts +111 -0
- package/template/tests/funnel.test.ts +24 -0
- package/template/tests/pricing.test.ts +154 -0
- package/template/tests/sprint2-catalog.test.ts +9 -6
- package/template/tests/wrangler-template.test.ts +32 -0
- package/template/tests-integration/chat-cost.test.ts +269 -0
- package/template/tests-integration/funnel.test.ts +318 -0
- package/template/wrangler.example.toml +12 -11
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
|
|
7
7
|
import type { Env } from "../env";
|
|
8
8
|
import type { ModelType, ModelEntry } from "../models";
|
|
9
|
-
import { MODELS } from "../models";
|
|
9
|
+
import { MODELS, OPENAI_COMPAT_CHAT_PROVIDERS } from "../models";
|
|
10
10
|
import type { InputAttachment } from "../types";
|
|
11
11
|
import type { ProviderStreamEvent } from "../parsers/types";
|
|
12
12
|
import { parseDataUrl, base64ToBytes } from "../utils";
|
|
@@ -29,6 +29,8 @@ import {
|
|
|
29
29
|
resolveAttachmentKeys,
|
|
30
30
|
safeParseJson,
|
|
31
31
|
} from "./shared";
|
|
32
|
+
import { chatCostUsd } from "../pricing";
|
|
33
|
+
import { recordFirstOutput } from "../funnel";
|
|
32
34
|
import { controlPlaneChat, type ControlPlaneChatMessage } from "../control-plane";
|
|
33
35
|
import type {
|
|
34
36
|
ChatRequest,
|
|
@@ -177,14 +179,15 @@ export async function handleChatStream(request: Request, env: Env, ctx: Executio
|
|
|
177
179
|
// Anthropic + Workers AI + xAI + OpenAI + Google + Moonshot. Workers AI
|
|
178
180
|
// catalog entries omit `provider` (default "workers-ai"); Unified Billing
|
|
179
181
|
// providers set it explicitly. Keep this allowlist in lockstep with the
|
|
180
|
-
// stream dispatch switch in runChatStream (v0.171.0: moonshotai
|
|
182
|
+
// stream dispatch switch in runChatStream (v0.171.0: moonshotai; 2026-12 refresh:
|
|
183
|
+
// every OPENAI_COMPAT_CHAT_PROVIDERS member).
|
|
181
184
|
const isWorkersAI = !model.provider;
|
|
182
185
|
if (
|
|
183
186
|
model.provider !== "anthropic" &&
|
|
184
187
|
model.provider !== "xai" &&
|
|
185
188
|
model.provider !== "openai" &&
|
|
186
189
|
model.provider !== "google" &&
|
|
187
|
-
model.provider
|
|
190
|
+
!(model.provider && OPENAI_COMPAT_CHAT_PROVIDERS.has(model.provider)) &&
|
|
188
191
|
!isWorkersAI
|
|
189
192
|
) {
|
|
190
193
|
return json({ error: `Streaming for provider '${model.provider}' is not yet implemented.` }, { status: 501 });
|
|
@@ -396,6 +399,9 @@ export async function runChat(request: Request, env: Env, model: ModelEntry, bod
|
|
|
396
399
|
const start = Date.now();
|
|
397
400
|
let result: unknown;
|
|
398
401
|
let logId: string | null = null;
|
|
402
|
+
// prism#258: on the control-plane path this is the plane's own charge; on every
|
|
403
|
+
// other path it is the catalog estimate, computed after the call.
|
|
404
|
+
let planeCostUsd: number | null = null;
|
|
399
405
|
try {
|
|
400
406
|
if (backend.kind === "control_plane") {
|
|
401
407
|
// Control plane: OpenAI-compatible messages (string content only for v1).
|
|
@@ -436,6 +442,7 @@ export async function runChat(request: Request, env: Env, model: ModelEntry, bod
|
|
|
436
442
|
},
|
|
437
443
|
};
|
|
438
444
|
logId = cpResult.requestId;
|
|
445
|
+
planeCostUsd = cpResult.costUsd;
|
|
439
446
|
} else if (!aiCtx) {
|
|
440
447
|
return json({ error: "AI gateway context missing" }, { status: 500 });
|
|
441
448
|
} else if (model.provider === "anthropic") {
|
|
@@ -456,7 +463,8 @@ export async function runChat(request: Request, env: Env, model: ModelEntry, bod
|
|
|
456
463
|
result = r.raw;
|
|
457
464
|
logId = r.logId;
|
|
458
465
|
} else {
|
|
459
|
-
// Workers AI hosted chat
|
|
466
|
+
// Workers AI hosted chat, and OPENAI_COMPAT_CHAT_PROVIDERS Chat
|
|
467
|
+
// Completions via env.AI.run.
|
|
460
468
|
result = await aiRun(aiCtx, model.id, { messages });
|
|
461
469
|
logId = aiLogId(aiCtx);
|
|
462
470
|
}
|
|
@@ -482,6 +490,8 @@ export async function runChat(request: Request, env: Env, model: ModelEntry, bod
|
|
|
482
490
|
const latency = Date.now() - start;
|
|
483
491
|
const output = extractOutput(result);
|
|
484
492
|
const usage = extractUsage(result);
|
|
493
|
+
// Control plane: the plane's charge or null, never the playground list price.
|
|
494
|
+
const costUsd = backend.kind === "control_plane" ? planeCostUsd : chatCostUsd(model.pricing, usage.in_, usage.out_);
|
|
485
495
|
|
|
486
496
|
const row = await persistChat(env, {
|
|
487
497
|
userEmail,
|
|
@@ -494,6 +504,7 @@ export async function runChat(request: Request, env: Env, model: ModelEntry, bod
|
|
|
494
504
|
attachments: persistedAtt,
|
|
495
505
|
tokens_in: usage.in_,
|
|
496
506
|
tokens_out: usage.out_,
|
|
507
|
+
cost_usd: costUsd,
|
|
497
508
|
latency_ms: latency,
|
|
498
509
|
ai_gateway_log_id: logId,
|
|
499
510
|
retrieved_context: allRetrieved.length ? allRetrieved : null,
|
|
@@ -510,6 +521,7 @@ export async function runChat(request: Request, env: Env, model: ModelEntry, bod
|
|
|
510
521
|
output,
|
|
511
522
|
tokens_in: usage.in_,
|
|
512
523
|
tokens_out: usage.out_,
|
|
524
|
+
cost_usd: costUsd,
|
|
513
525
|
latency_ms: latency,
|
|
514
526
|
ai_gateway_log_id: logId,
|
|
515
527
|
transcripts: extraText,
|
|
@@ -617,20 +629,17 @@ export async function runImage(request: Request, env: Env, model: ModelEntry, bo
|
|
|
617
629
|
// The matrix:
|
|
618
630
|
// FLUX-1 schnell, Lucid Origin - JSON in, JSON out (base64). Gateway path works.
|
|
619
631
|
// FLUX-2 (Klein 9b/4b, Dev) - multipart in, JSON out (base64). Gateway can't proxy stream input.
|
|
620
|
-
// Phoenix 1.0
|
|
632
|
+
// Phoenix 1.0 - JSON in, ReadableStream out. Gateway can't proxy stream output.
|
|
621
633
|
//
|
|
622
|
-
// Solution: bypass the AI Gateway for the
|
|
634
|
+
// Solution: bypass the AI Gateway for the four problematic models by
|
|
623
635
|
// calling env.AI.run directly without the gateway option, and detect
|
|
624
636
|
// the response shape at runtime so we can drain a ReadableStream into
|
|
625
637
|
// bytes or extract base64 from JSON as appropriate. Cost: no AI Gateway
|
|
626
638
|
// observability/caching for these specific models (ai_gateway_log_id
|
|
627
639
|
// stays null on the persisted row).
|
|
628
640
|
const isFlux2 = model.id.startsWith("@cf/black-forest-labs/flux-2-");
|
|
629
|
-
const isSdxl = model.id === "@cf/stabilityai/stable-diffusion-xl-base-1.0";
|
|
630
641
|
const bypassGateway = isFlux2
|
|
631
|
-
|| model.id === "@cf/leonardo/phoenix-1.0"
|
|
632
|
-
|| model.id === "@cf/lykon/dreamshaper-8-lcm"
|
|
633
|
-
|| isSdxl; // SDXL returns a ReadableStream image; gateway can't proxy it
|
|
642
|
+
|| model.id === "@cf/leonardo/phoenix-1.0";
|
|
634
643
|
|
|
635
644
|
let runParams: unknown;
|
|
636
645
|
|
|
@@ -688,12 +697,6 @@ export async function runImage(request: Request, env: Env, model: ModelEntry, bo
|
|
|
688
697
|
params.steps = 4;
|
|
689
698
|
delete params.negative_prompt;
|
|
690
699
|
}
|
|
691
|
-
// SDXL's step field is `num_steps` (max 20), not `steps`; swap to avoid
|
|
692
|
-
// sending an unknown/over-max field.
|
|
693
|
-
if (isSdxl) {
|
|
694
|
-
delete params.steps;
|
|
695
|
-
params.num_steps = 20;
|
|
696
|
-
}
|
|
697
700
|
runParams = params;
|
|
698
701
|
}
|
|
699
702
|
|
|
@@ -716,7 +719,7 @@ export async function runImage(request: Request, env: Env, model: ModelEntry, bo
|
|
|
716
719
|
|
|
717
720
|
// Two response shapes are possible:
|
|
718
721
|
// 1. JSON { image: "base64..." } - FLUX-1, FLUX-2, Lucid Origin
|
|
719
|
-
// 2. ReadableStream of raw PNG bytes - Phoenix
|
|
722
|
+
// 2. ReadableStream of raw PNG bytes - Phoenix
|
|
720
723
|
// Detect at runtime rather than mapping per-model; safer if Cloudflare
|
|
721
724
|
// shifts a model from one shape to the other.
|
|
722
725
|
if (result instanceof ReadableStream) {
|
|
@@ -737,9 +740,7 @@ export async function runImage(request: Request, env: Env, model: ModelEntry, bo
|
|
|
737
740
|
bytes.set(c, offset);
|
|
738
741
|
offset += c.length;
|
|
739
742
|
}
|
|
740
|
-
// All stream-output image models here emit PNG bytes.
|
|
741
|
-
// claim image/jpg, but the binding's actual output is PNG, confirmed
|
|
742
|
-
// live, so we don't special-case it.)
|
|
743
|
+
// All stream-output image models here emit PNG bytes.
|
|
743
744
|
mime = "image/png";
|
|
744
745
|
} else {
|
|
745
746
|
const b64 = (result as { image?: string })?.image;
|
|
@@ -1107,6 +1108,8 @@ export interface PersistArgs {
|
|
|
1107
1108
|
attachments: PersistedAttachment[];
|
|
1108
1109
|
tokens_in: number | null;
|
|
1109
1110
|
tokens_out: number | null;
|
|
1111
|
+
/** prism#258: chat only; stored null for any other type or a non-done row. */
|
|
1112
|
+
cost_usd?: number | null;
|
|
1110
1113
|
latency_ms: number;
|
|
1111
1114
|
ai_gateway_log_id: string | null;
|
|
1112
1115
|
status?: "pending" | "done" | "failed";
|
|
@@ -1126,6 +1129,12 @@ export async function persistChat(env: Env, a: PersistArgs): Promise<{ id: numbe
|
|
|
1126
1129
|
// sidebar as single-turn entries.
|
|
1127
1130
|
const convId = a.conversation_id ?? null;
|
|
1128
1131
|
const turnIdx = a.turn_index ?? null;
|
|
1132
|
+
// Null is not zero: only a finished chat row with a finite non-negative cost carries one.
|
|
1133
|
+
const costUsd =
|
|
1134
|
+
a.model_type === "chat" && (a.status ?? "done") === "done" &&
|
|
1135
|
+
typeof a.cost_usd === "number" && Number.isFinite(a.cost_usd) && a.cost_usd >= 0
|
|
1136
|
+
? a.cost_usd
|
|
1137
|
+
: null;
|
|
1129
1138
|
|
|
1130
1139
|
const row = await env.DB.prepare(
|
|
1131
1140
|
`INSERT INTO chats
|
|
@@ -1133,8 +1142,8 @@ export async function persistChat(env: Env, a: PersistArgs): Promise<{ id: numbe
|
|
|
1133
1142
|
output_artifact, attachments,
|
|
1134
1143
|
tokens_in, tokens_out, latency_ms, ai_gateway_log_id,
|
|
1135
1144
|
status, job_id, job_provider, job_error, job_started_at,
|
|
1136
|
-
retrieved_context, conversation_id, turn_index, project_id)
|
|
1137
|
-
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
1145
|
+
retrieved_context, conversation_id, turn_index, project_id, cost_usd)
|
|
1146
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
1138
1147
|
RETURNING id, created_at`
|
|
1139
1148
|
)
|
|
1140
1149
|
.bind(
|
|
@@ -1150,7 +1159,8 @@ export async function persistChat(env: Env, a: PersistArgs): Promise<{ id: numbe
|
|
|
1150
1159
|
a.retrieved_context && a.retrieved_context.length ? JSON.stringify(a.retrieved_context) : null,
|
|
1151
1160
|
convId,
|
|
1152
1161
|
turnIdx,
|
|
1153
|
-
a.project_id ?? null
|
|
1162
|
+
a.project_id ?? null,
|
|
1163
|
+
costUsd
|
|
1154
1164
|
)
|
|
1155
1165
|
.first<{ id: number; created_at: string }>();
|
|
1156
1166
|
|
|
@@ -1158,6 +1168,12 @@ export async function persistChat(env: Env, a: PersistArgs): Promise<{ id: numbe
|
|
|
1158
1168
|
return { id: 0, created_at: new Date().toISOString(), conversation_id: "" };
|
|
1159
1169
|
}
|
|
1160
1170
|
|
|
1171
|
+
// prism#259: first finished output for this account, counted once. Only a row
|
|
1172
|
+
// persisted as done, with something to show. Never throws; never fails the write.
|
|
1173
|
+
if ((a.status ?? "done") === "done" && (a.output !== "" || a.output_artifact)) {
|
|
1174
|
+
await recordFirstOutput(env, a.userEmail);
|
|
1175
|
+
}
|
|
1176
|
+
|
|
1161
1177
|
// For non-chat rows that didn't get an explicit conversation_id, backfill
|
|
1162
1178
|
// a synthetic one so they appear in the conversation list.
|
|
1163
1179
|
let finalConvId = convId;
|
|
@@ -1185,7 +1201,7 @@ export async function persistChat(env: Env, a: PersistArgs): Promise<{ id: numbe
|
|
|
1185
1201
|
// Wire format on the response body (text/event-stream):
|
|
1186
1202
|
// data: {"type":"delta","text":"..."}
|
|
1187
1203
|
// data: {"type":"done","row_id":N,"latency_ms":N,"tokens_in":N|null,
|
|
1188
|
-
// "tokens_out":N|null,"conversation_id":"...","turn_index":N}
|
|
1204
|
+
// "tokens_out":N|null,"cost_usd":N|null,"conversation_id":"...","turn_index":N}
|
|
1189
1205
|
// data: {"type":"error","message":"..."}
|
|
1190
1206
|
//
|
|
1191
1207
|
// Anthropic's native SSE event types (message_start, content_block_delta,
|
|
@@ -1220,6 +1236,7 @@ export async function runChatStream(request: Request, env: Env, model: ModelEntr
|
|
|
1220
1236
|
turn_index?: number;
|
|
1221
1237
|
tokens_in?: number;
|
|
1222
1238
|
tokens_out?: number;
|
|
1239
|
+
cost_usd?: number | null;
|
|
1223
1240
|
latency_ms?: number;
|
|
1224
1241
|
};
|
|
1225
1242
|
const text = payload.output || "";
|
|
@@ -1238,6 +1255,7 @@ export async function runChatStream(request: Request, env: Env, model: ModelEntr
|
|
|
1238
1255
|
turn_index: payload.turn_index,
|
|
1239
1256
|
tokens_in: payload.tokens_in,
|
|
1240
1257
|
tokens_out: payload.tokens_out,
|
|
1258
|
+
cost_usd: payload.cost_usd ?? null,
|
|
1241
1259
|
latency_ms: payload.latency_ms,
|
|
1242
1260
|
output: text,
|
|
1243
1261
|
})}\n\n`,
|
|
@@ -1441,9 +1459,10 @@ export async function runChatStream(request: Request, env: Env, model: ModelEntr
|
|
|
1441
1459
|
streamGenerator = callAnthropicStream(aiCtx, model, effectiveSystemPrompt || undefined, messages, upstreamAbort.signal);
|
|
1442
1460
|
} else if (model.provider === "xai") {
|
|
1443
1461
|
streamGenerator = callXaiStream(aiCtx, model, messages, upstreamAbort.signal);
|
|
1444
|
-
} else if (model.provider === "openai" || model.provider
|
|
1445
|
-
// OpenAI +
|
|
1446
|
-
// env.AI.run with
|
|
1462
|
+
} else if (model.provider === "openai" || (model.provider && OPENAI_COMPAT_CHAT_PROVIDERS.has(model.provider))) {
|
|
1463
|
+
// OpenAI + the Chat Completions Unified Billing providers (Moonshot
|
|
1464
|
+
// v0.171.0; Alibaba, DeepSeek, MiniMax 2026-12 refresh): env.AI.run with
|
|
1465
|
+
// OpenAI-compatible SSE.
|
|
1447
1466
|
streamGenerator = callOpenAIStream(aiCtx, model, messages, upstreamAbort.signal);
|
|
1448
1467
|
} else if (model.provider === "google") {
|
|
1449
1468
|
streamGenerator = callGeminiStream(aiCtx, model, effectiveSystemPrompt || undefined, messages, upstreamAbort.signal);
|
|
@@ -1468,6 +1487,7 @@ export async function runChatStream(request: Request, env: Env, model: ModelEntr
|
|
|
1468
1487
|
}
|
|
1469
1488
|
|
|
1470
1489
|
const latency = Date.now() - start;
|
|
1490
|
+
const costUsd = chatCostUsd(model.pricing, usageIn, usageOut);
|
|
1471
1491
|
|
|
1472
1492
|
// Persist as a single row. retrieved_context is saved on the row so
|
|
1473
1493
|
// the History/Conversation views render citations the same way runChat
|
|
@@ -1486,6 +1506,7 @@ export async function runChatStream(request: Request, env: Env, model: ModelEntr
|
|
|
1486
1506
|
attachments: persistedAtt,
|
|
1487
1507
|
tokens_in: usageIn,
|
|
1488
1508
|
tokens_out: usageOut,
|
|
1509
|
+
cost_usd: costUsd,
|
|
1489
1510
|
latency_ms: latency,
|
|
1490
1511
|
ai_gateway_log_id: null,
|
|
1491
1512
|
retrieved_context: allRetrieved.length ? allRetrieved : null,
|
|
@@ -1500,6 +1521,7 @@ export async function runChatStream(request: Request, env: Env, model: ModelEntr
|
|
|
1500
1521
|
latency_ms: latency,
|
|
1501
1522
|
tokens_in: usageIn,
|
|
1502
1523
|
tokens_out: usageOut,
|
|
1524
|
+
cost_usd: costUsd,
|
|
1503
1525
|
conversation_id: conversationId,
|
|
1504
1526
|
turn_index: turnIndex,
|
|
1505
1527
|
web_search_error: body.use_web_search ? webSearchError : undefined,
|
|
@@ -33,7 +33,9 @@ import {
|
|
|
33
33
|
} from "../conversation-context";
|
|
34
34
|
|
|
35
35
|
/** Default cheap Workers AI chat model for compact when the client omits model. */
|
|
36
|
-
|
|
36
|
+
// 2026-12 refresh: was @cf/meta/llama-3.2-3b-instruct, which Cloudflare retired on
|
|
37
|
+
// 2026-11-20 (HTTP 410, error 5028). Gemma 4 is CF's recommended small model.
|
|
38
|
+
export const DEFAULT_COMPACT_MODEL = "@cf/google/gemma-4-26b-a4b-it";
|
|
37
39
|
|
|
38
40
|
export async function loadCompactState(
|
|
39
41
|
env: Env,
|
|
@@ -12,6 +12,7 @@ import { aiRun, type AiContext } from "../ai-binding";
|
|
|
12
12
|
import { requireGatewayCredentials } from "../gateway-credentials";
|
|
13
13
|
import { buildGenParams } from "../longrun-params";
|
|
14
14
|
import { unzip } from "../zip";
|
|
15
|
+
import { recordFirstOutput } from "../funnel";
|
|
15
16
|
import { r2Put, r2KeyToDataUri, r2DeleteSafe } from "./shared";
|
|
16
17
|
import type { OutputArtifact } from "./shared";
|
|
17
18
|
import {
|
|
@@ -285,6 +286,8 @@ export class LongRunWorkflow extends WorkflowEntrypoint<Env, LongRunParams> {
|
|
|
285
286
|
)
|
|
286
287
|
.bind(JSON.stringify(outputArtifact), latency, rowId)
|
|
287
288
|
.run();
|
|
289
|
+
// prism#259: a finished video or music row is a first output too. Never throws.
|
|
290
|
+
await recordFirstOutput(this.env, userEmail);
|
|
288
291
|
}
|
|
289
292
|
);
|
|
290
293
|
} catch (err) {
|
package/template/src/version.ts
CHANGED
|
@@ -15,4 +15,4 @@
|
|
|
15
15
|
// That distinction is the entire point. In prism-mcp the advertised version was a literal and
|
|
16
16
|
// the test asserting it carried a TRANSCRIBED duplicate, so the assertion only ever proved
|
|
17
17
|
// that someone had typed the same thing twice, and a correct version bump went red.
|
|
18
|
-
export const VERSION = "1.
|
|
18
|
+
export const VERSION = "1.5.0";
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
// scripts/catalog-drift.mjs (prism#254, #255): id extraction, API pagination, page verdicts and the
|
|
2
|
+
// drift decision. The positive control is the point: an injected retired id MUST be reported.
|
|
3
|
+
import { describe, expect, it } from "vitest";
|
|
4
|
+
import {
|
|
5
|
+
checkPage,
|
|
6
|
+
evaluate,
|
|
7
|
+
extractEntryIds,
|
|
8
|
+
extractLiteralIds,
|
|
9
|
+
fetchWorkersAiNames,
|
|
10
|
+
renderBody,
|
|
11
|
+
} from "../scripts/catalog-drift.mjs";
|
|
12
|
+
|
|
13
|
+
const cat = `
|
|
14
|
+
// { id: "@cf/commented/out" }
|
|
15
|
+
{ id: "@cf/meta/a", label: "a" },
|
|
16
|
+
{
|
|
17
|
+
id: "anthropic/claude-x",
|
|
18
|
+
upstream: "grok/grok-1",
|
|
19
|
+
}
|
|
20
|
+
id: string;
|
|
21
|
+
`;
|
|
22
|
+
|
|
23
|
+
describe("extraction", () => {
|
|
24
|
+
it("reads id and upstream entries, skips comments and type declarations", () => {
|
|
25
|
+
expect(extractEntryIds(cat).sort()).toEqual(["@cf/meta/a", "anthropic/claude-x", "grok/grok-1"]);
|
|
26
|
+
});
|
|
27
|
+
it("literal scan skips startsWith prefixes", () => {
|
|
28
|
+
const code = `if (m.startsWith("@cf/black-forest-labs/flux-2")) {} m === "@cf/black-forest-labs/flux-2-dev"; "google/imagen-4"`;
|
|
29
|
+
expect(extractLiteralIds(code, ["google"]).sort()).toEqual(["@cf/black-forest-labs/flux-2-dev", "google/imagen-4"]);
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
describe("API pagination", () => {
|
|
34
|
+
it("walks pages until total_count is reached", async () => {
|
|
35
|
+
const calls: string[] = [];
|
|
36
|
+
const pages = [
|
|
37
|
+
{ success: true, result: [{ name: "@cf/a/1" }, { name: "@cf/a/2" }], result_info: { total_count: 3 } },
|
|
38
|
+
{ success: true, result: [{ name: "@cf/a/3" }], result_info: { total_count: 3 } },
|
|
39
|
+
];
|
|
40
|
+
const f = (async (url: string) => {
|
|
41
|
+
calls.push(url);
|
|
42
|
+
return new Response(JSON.stringify(pages[calls.length - 1]));
|
|
43
|
+
}) as unknown as typeof fetch;
|
|
44
|
+
const names = await fetchWorkersAiNames({ accountId: "acc", token: "t", fetchImpl: f });
|
|
45
|
+
expect([...names].sort()).toEqual(["@cf/a/1", "@cf/a/2", "@cf/a/3"]);
|
|
46
|
+
expect(calls).toHaveLength(2);
|
|
47
|
+
expect(calls[0]).toContain("/accounts/acc/ai/models/search?page=1");
|
|
48
|
+
});
|
|
49
|
+
it("throws on an HTTP error rather than reporting an empty catalog", async () => {
|
|
50
|
+
const f = (async () => new Response("no", { status: 403 })) as unknown as typeof fetch;
|
|
51
|
+
await expect(fetchWorkersAiNames({ accountId: "a", token: "t", fetchImpl: f })).rejects.toThrow("403");
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
describe("page check", () => {
|
|
56
|
+
const mk = (map: Record<string, [number, string?]>) =>
|
|
57
|
+
(async (url: string) => {
|
|
58
|
+
const key = url.replace("https://developers.cloudflare.com/ai/models/", "");
|
|
59
|
+
const [status, loc] = map[key] ?? [500];
|
|
60
|
+
return new Response(null, { status, headers: loc ? { location: loc } : {} });
|
|
61
|
+
}) as unknown as typeof fetch;
|
|
62
|
+
it("present, via the dotted variant, and via a gateway vendor alias", async () => {
|
|
63
|
+
const f = mk({ "anthropic/claude-haiku-4.5/": [200], "xai/grok-4.5/": [200] });
|
|
64
|
+
expect(await checkPage("anthropic/claude-haiku-4-5", f)).toBe("present");
|
|
65
|
+
expect(await checkPage("grok/grok-4.5", f)).toBe("present");
|
|
66
|
+
});
|
|
67
|
+
it("missing when both variants redirect to ?unavailable=", async () => {
|
|
68
|
+
const f = mk({
|
|
69
|
+
"google/imagen-4/": [302, "/ai/models/?unavailable=google%2Fimagen-4"],
|
|
70
|
+
});
|
|
71
|
+
expect(await checkPage("google/imagen-4", f)).toBe("missing");
|
|
72
|
+
});
|
|
73
|
+
it("unknown, not passing, when the site keeps failing", async () => {
|
|
74
|
+
expect(await checkPage("xai/grok-9", mk({}))).toBe("unknown");
|
|
75
|
+
}, 15000);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
describe("drift decision", () => {
|
|
79
|
+
const ids = new Map<string, string[]>([
|
|
80
|
+
["@cf/ok/model", ["prism:src/models.ts"]],
|
|
81
|
+
["@cf/myshell-ai/melotts", ["prism-control-plane:src/catalog.ts"]],
|
|
82
|
+
["openai/gone", ["prism:src/models.ts"]],
|
|
83
|
+
["openai/flaky", ["prism:src/models.ts"]],
|
|
84
|
+
]);
|
|
85
|
+
const r = evaluate({
|
|
86
|
+
ids,
|
|
87
|
+
cfNames: new Set(["@cf/ok/model"]),
|
|
88
|
+
pageVerdicts: new Map([["openai/gone", "missing"], ["openai/flaky", "unknown"]]),
|
|
89
|
+
});
|
|
90
|
+
it("reports the injected retired id with its repo and file", () => {
|
|
91
|
+
expect(r.drifted.map((d) => d.id).sort()).toEqual(["@cf/myshell-ai/melotts", "openai/gone"]);
|
|
92
|
+
expect(renderBody({ ...r, total: 4, checkedAt: "t" })).toContain("`@cf/myshell-ai/melotts`");
|
|
93
|
+
expect(renderBody({ ...r, total: 4, checkedAt: "t" })).toContain("prism-control-plane:src/catalog.ts");
|
|
94
|
+
});
|
|
95
|
+
it("a page that could not be checked is listed apart, never as passing", () => {
|
|
96
|
+
expect(r.unchecked.map((d) => d.id)).toEqual(["openai/flaky"]);
|
|
97
|
+
});
|
|
98
|
+
it("clean input yields no findings", () => {
|
|
99
|
+
const c = evaluate({ ids: new Map([["@cf/ok/model", ["x"]]]), cfNames: new Set(["@cf/ok/model"]), pageVerdicts: new Map() });
|
|
100
|
+
expect(c.drifted).toHaveLength(0);
|
|
101
|
+
expect(c.unchecked).toHaveLength(0);
|
|
102
|
+
});
|
|
103
|
+
});
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
// Catalog pins for the 2026-12 catalog refresh.
|
|
2
|
+
//
|
|
3
|
+
// Why: Cloudflare retired 24 Workers AI models on 2026-11-20. Requests to them
|
|
4
|
+
// fail with HTTP 410 and error 5028. These pins keep a retired id from coming
|
|
5
|
+
// back into the picker, and keep the new entries on a dispatcher that serves
|
|
6
|
+
// them.
|
|
7
|
+
|
|
8
|
+
import { describe, it, expect } from "vitest";
|
|
9
|
+
import { MODELS, OPENAI_COMPAT_CHAT_PROVIDERS } from "../src/models";
|
|
10
|
+
import { DEFAULT_COMPACT_MODEL } from "../src/routes/conversations";
|
|
11
|
+
import { buildGenParams } from "../src/longrun-params";
|
|
12
|
+
|
|
13
|
+
const byId = (id: string) => MODELS.find((m) => m.id === id);
|
|
14
|
+
|
|
15
|
+
// The CF changelog 2026-11-05 "Planned model deprecations on Workers AI" list.
|
|
16
|
+
const RETIRED_2026_11_20 = [
|
|
17
|
+
"@cf/deepseek-ai/deepseek-r1-distill-qwen-32b",
|
|
18
|
+
"@cf/nvidia/nemotron-3-120b-a12b",
|
|
19
|
+
"@cf/deepseek-ai/deepseek-v4-pro-0813",
|
|
20
|
+
"@cf/deepseek-ai/deepseek-v4-flash-0731",
|
|
21
|
+
"@cf/qwen/qwq-32b",
|
|
22
|
+
"@cf/qwen/qwen3-30b-a3b-fp8",
|
|
23
|
+
"@cf/moonshotai/kimi-k2.6",
|
|
24
|
+
"@cf/moonshotai/kimi-k2.7-code",
|
|
25
|
+
"@cf/zai-org/glm-5.2",
|
|
26
|
+
"@cf/zai-org/glm-5.3-flash",
|
|
27
|
+
"@cf/meta-llama/llama-2-7b-chat-hf-lora",
|
|
28
|
+
"@cf/google/gemma-2b-it-lora",
|
|
29
|
+
"@cf/google/gemma-7b-it-lora",
|
|
30
|
+
"@cf/meta/llama-3.1-8b-instruct-fp8",
|
|
31
|
+
"@cf/meta/llama-3.2-1b-instruct",
|
|
32
|
+
"@cf/meta/llama-3.2-3b-instruct",
|
|
33
|
+
"@cf/myshell-ai/melotts",
|
|
34
|
+
"@cf/stabilityai/stable-diffusion-xl-base-1.0",
|
|
35
|
+
"@cf/runwayml/stable-diffusion-v1-5-inpainting",
|
|
36
|
+
"@cf/bytedance/stable-diffusion-xl-lightning",
|
|
37
|
+
"@cf/lykon/dreamshaper-8-lcm",
|
|
38
|
+
"@cf/openai/whisper-tiny-en",
|
|
39
|
+
"@cf/llava-hf/llava-1.5-7b-hf",
|
|
40
|
+
"@cf/google/embeddinggemma-300m",
|
|
41
|
+
];
|
|
42
|
+
|
|
43
|
+
describe("2026-12 catalog refresh", () => {
|
|
44
|
+
it("carries no model that Cloudflare retired on 2026-11-20", () => {
|
|
45
|
+
for (const id of RETIRED_2026_11_20) expect(byId(id), id).toBeUndefined();
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("has no duplicate ids", () => {
|
|
49
|
+
const ids = MODELS.map((m) => m.id);
|
|
50
|
+
expect(new Set(ids).size).toBe(ids.length);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it("compacts with a live catalog chat model by default", () => {
|
|
54
|
+
const m = byId(DEFAULT_COMPACT_MODEL);
|
|
55
|
+
expect(m?.type).toBe("chat");
|
|
56
|
+
expect(m?.provider).toBeUndefined(); // Workers AI, no Unified Billing spend
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it("puts new dotted Claude ids on the binding path", () => {
|
|
60
|
+
for (const id of [
|
|
61
|
+
"anthropic/claude-fable-5.2",
|
|
62
|
+
"anthropic/claude-opus-5.5",
|
|
63
|
+
"anthropic/claude-sonnet-5.5",
|
|
64
|
+
"anthropic/claude-haiku-5.5",
|
|
65
|
+
]) {
|
|
66
|
+
const m = byId(id);
|
|
67
|
+
expect(m?.provider, id).toBe("anthropic");
|
|
68
|
+
expect(m?.binding, id).toBe(true);
|
|
69
|
+
expect(m?.streaming, id).toBe(true);
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it("puts grok-4.7 on the xAI binding path", () => {
|
|
74
|
+
expect(byId("xai/grok-4.7")?.binding).toBe(true);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it("routes new third-party chat providers through the OpenAI-compatible path", () => {
|
|
78
|
+
for (const id of ["alibaba/qwen3.8-max", "deepseek/deepseek-v4-pro", "minimax/m3", "moonshotai/kimi-k3.1-pro"]) {
|
|
79
|
+
const m = byId(id);
|
|
80
|
+
expect(m?.type, id).toBe("chat");
|
|
81
|
+
expect(OPENAI_COMPAT_CHAT_PROVIDERS.has(m!.provider!), id).toBe(true);
|
|
82
|
+
expect(m?.capabilities, id).toEqual([]);
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it("keeps GPT-6 ids on Chat Completions (no Responses flag)", () => {
|
|
87
|
+
for (const id of ["openai/gpt-6.2-astra", "openai/gpt-6.2-sol", "openai/gpt-6.1-terra", "openai/gpt-6-luna"]) {
|
|
88
|
+
expect(byId(id)?.api, id).toBeUndefined();
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it("adds the Workers AI replacements CF recommends", () => {
|
|
93
|
+
for (const id of [
|
|
94
|
+
"@cf/deepseek-ai/deepseek-v4.1-flash-1017",
|
|
95
|
+
"@cf/zai-org/glm-5.3",
|
|
96
|
+
"@cf/qwen/qwen3.8-27b",
|
|
97
|
+
"@cf/google/gemma-4-26b-a4b-it",
|
|
98
|
+
]) {
|
|
99
|
+
const m = byId(id);
|
|
100
|
+
expect(m?.type, id).toBe("chat");
|
|
101
|
+
expect(m?.provider, id).toBeUndefined();
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it("sends Seedance 2.5 image-to-video the Seedance 2.0 shape", () => {
|
|
106
|
+
const p25 = buildGenParams("video", { prompt: "pan", imageUrl: "https://x/i.png", modelId: "bytedance/seedance-2.5" });
|
|
107
|
+
const p20 = buildGenParams("video", { prompt: "pan", imageUrl: "https://x/i.png", modelId: "bytedance/seedance-2.0" });
|
|
108
|
+
expect(p25).toEqual(p20);
|
|
109
|
+
expect(p25.image).toBe("https://x/i.png");
|
|
110
|
+
});
|
|
111
|
+
});
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// prism#259: bucket boundaries for the signup-to-first-output time.
|
|
2
|
+
import { describe, expect, it } from "vitest";
|
|
3
|
+
import { ttfoBucket } from "../src/funnel";
|
|
4
|
+
|
|
5
|
+
describe("ttfoBucket", () => {
|
|
6
|
+
it("puts each boundary in the upper bucket", () => {
|
|
7
|
+
expect(ttfoBucket(0)).toBe("ttfo_lt_1m");
|
|
8
|
+
expect(ttfoBucket(59)).toBe("ttfo_lt_1m");
|
|
9
|
+
expect(ttfoBucket(60)).toBe("ttfo_1_5m");
|
|
10
|
+
expect(ttfoBucket(299)).toBe("ttfo_1_5m");
|
|
11
|
+
expect(ttfoBucket(300)).toBe("ttfo_5_30m");
|
|
12
|
+
expect(ttfoBucket(1799)).toBe("ttfo_5_30m");
|
|
13
|
+
expect(ttfoBucket(1800)).toBe("ttfo_30m_24h");
|
|
14
|
+
expect(ttfoBucket(86399)).toBe("ttfo_30m_24h");
|
|
15
|
+
expect(ttfoBucket(86400)).toBe("ttfo_ge_24h");
|
|
16
|
+
expect(ttfoBucket(10 * 86400)).toBe("ttfo_ge_24h");
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it("negative, NaN and Infinity never throw and never pick a bucket by accident", () => {
|
|
20
|
+
expect(ttfoBucket(-5)).toBe("ttfo_lt_1m");
|
|
21
|
+
expect(ttfoBucket(NaN)).toBe("ttfo_lt_1m");
|
|
22
|
+
expect(ttfoBucket(Infinity)).toBe("ttfo_lt_1m");
|
|
23
|
+
});
|
|
24
|
+
});
|