@skyphusion/create-prism 0.1.0 → 0.175.6

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.
Files changed (54) hide show
  1. package/README.md +5 -3
  2. package/index.js +4 -2
  3. package/package.json +1 -1
  4. package/template/CONTRIBUTING.md +6 -2
  5. package/template/README.md +45 -33
  6. package/template/docs/legal/README.md +1 -0
  7. package/template/migrations/0002_auth_public_mode.sql +32 -0
  8. package/template/package.json +6 -6
  9. package/template/public/app.js +44 -16
  10. package/template/public/index.html +47 -4
  11. package/template/src/auth-kdf.ts +7 -4
  12. package/template/src/auth.ts +11 -2
  13. package/template/src/control-plane.ts +254 -0
  14. package/template/src/env.ts +15 -0
  15. package/template/src/gateway-credentials.ts +23 -4
  16. package/template/src/health-schema.ts +4 -0
  17. package/template/src/index.ts +65 -3982
  18. package/template/src/longrun-params.ts +4 -2
  19. package/template/src/models.ts +65 -19
  20. package/template/src/output-extract.ts +58 -8
  21. package/template/src/parsers/openai-sse.ts +43 -14
  22. package/template/src/providers/anthropic.ts +105 -0
  23. package/template/src/providers/openai-image.ts +66 -0
  24. package/template/src/providers/openai.ts +79 -13
  25. package/template/src/providers/xai.ts +91 -0
  26. package/template/src/proxied-image-params.ts +29 -16
  27. package/template/src/routes/artifacts.ts +51 -0
  28. package/template/src/routes/chat.ts +1709 -0
  29. package/template/src/routes/conversations.ts +212 -0
  30. package/template/src/routes/documents.ts +262 -0
  31. package/template/src/routes/health.ts +105 -0
  32. package/template/src/routes/history.ts +92 -0
  33. package/template/src/routes/prefs.ts +95 -0
  34. package/template/src/routes/projects.ts +574 -0
  35. package/template/src/routes/rag.ts +657 -0
  36. package/template/src/routes/shared.ts +226 -0
  37. package/template/src/routes/workflow.ts +304 -0
  38. package/template/src/user-prefs.ts +10 -0
  39. package/template/src/zip.ts +7 -0
  40. package/template/tests/anthropic-binding.test.ts +181 -0
  41. package/template/tests/control-plane.test.ts +112 -0
  42. package/template/tests/gateway-credentials.test.ts +37 -0
  43. package/template/tests/longrun-params.test.ts +19 -0
  44. package/template/tests/openai-image.test.ts +47 -0
  45. package/template/tests/openai-responses.test.ts +78 -0
  46. package/template/tests/openai-sse.test.ts +28 -0
  47. package/template/tests/output-extract.test.ts +75 -1
  48. package/template/tests/proxied-image-params.test.ts +16 -7
  49. package/template/tests/sprint1-catalog.test.ts +27 -0
  50. package/template/tests/sprint2-catalog.test.ts +51 -0
  51. package/template/tests/sprint3-catalog.test.ts +29 -0
  52. package/template/tests/xai-binding.test.ts +127 -0
  53. package/template/tests-integration/worker.test.ts +9 -2
  54. package/template/wrangler.example.toml +19 -7
@@ -0,0 +1,27 @@
1
+ // Catalog pins for the v0.170.0 Sprint 1 model adds (read-only audit follow-up).
2
+
3
+ import { describe, it, expect } from "vitest";
4
+ import { MODELS } from "../src/models";
5
+
6
+ describe("v0.170.0 sprint 1 catalog entries", () => {
7
+ it("adds Gemini 3.6 Flash on the google dispatcher (text-only caps)", () => {
8
+ const m = MODELS.find((x) => x.id === "google/gemini-3.6-flash");
9
+ expect(m).toBeDefined();
10
+ expect(m?.provider).toBe("google");
11
+ expect(m?.type).toBe("chat");
12
+ expect(m?.streaming).toBe(true);
13
+ expect(m?.capabilities).toEqual([]);
14
+ });
15
+
16
+ it("adds Seedance 2.0 Mini as bytedance video with image-input", () => {
17
+ const m = MODELS.find((x) => x.id === "bytedance/seedance-2.0-mini");
18
+ expect(m).toBeDefined();
19
+ expect(m?.provider).toBe("bytedance");
20
+ expect(m?.type).toBe("video");
21
+ expect(m?.capabilities).toContain("image-input");
22
+ });
23
+
24
+ it("does not re-introduce blocked grok-build-0.1", () => {
25
+ expect(MODELS.find((x) => x.id === "xai/grok-build-0.1")).toBeUndefined();
26
+ });
27
+ });
@@ -0,0 +1,51 @@
1
+ // Catalog pins for the v0.171.0 Sprint 2 model adds.
2
+
3
+ import { describe, it, expect } from "vitest";
4
+ import { MODELS } from "../src/models";
5
+ import { readFileSync } from "node:fs";
6
+ import { fileURLToPath } from "node:url";
7
+ import { dirname, join } from "node:path";
8
+
9
+ const chatSrc = readFileSync(
10
+ join(dirname(fileURLToPath(import.meta.url)), "../src/routes/chat.ts"),
11
+ "utf8",
12
+ );
13
+
14
+ describe("v0.171.0 sprint 2 catalog entries", () => {
15
+ it("adds Kimi K3 as moonshotai chat with streaming (OpenAI-compat path)", () => {
16
+ const m = MODELS.find((x) => x.id === "moonshotai/kimi-k3");
17
+ expect(m).toBeDefined();
18
+ expect(m?.provider).toBe("moonshotai");
19
+ expect(m?.type).toBe("chat");
20
+ expect(m?.streaming).toBe(true);
21
+ // Multimodal-in unsmoked; attach affordance stays off (OpenAI convention).
22
+ expect(m?.capabilities).toEqual([]);
23
+ });
24
+
25
+ // Adversarial audit medium finding: stream allowlist must include moonshotai
26
+ // or handleChatStream 501s before runChatStream can route to callOpenAIStream.
27
+ it("handleChatStream allowlist includes moonshotai (no 501 before dispatch)", () => {
28
+ expect(chatSrc).toMatch(/model\.provider !== "moonshotai"/);
29
+ expect(chatSrc).toMatch(/provider === "openai" \|\| model\.provider === "moonshotai"/);
30
+ });
31
+
32
+ it("adds Grok Imagine Image as xai image", () => {
33
+ const m = MODELS.find((x) => x.id === "xai/grok-imagine-image");
34
+ expect(m).toBeDefined();
35
+ expect(m?.provider).toBe("xai");
36
+ expect(m?.type).toBe("image");
37
+ });
38
+
39
+ it("adds Seedream 5 Pro as bytedance image", () => {
40
+ const m = MODELS.find((x) => x.id === "bytedance/seedream-5-pro");
41
+ expect(m).toBeDefined();
42
+ expect(m?.provider).toBe("bytedance");
43
+ expect(m?.type).toBe("image");
44
+ });
45
+
46
+ it("keeps Workers AI Kimi K2.x distinct from third-party K3", () => {
47
+ expect(MODELS.find((x) => x.id === "@cf/moonshotai/kimi-k2.6")).toBeDefined();
48
+ expect(MODELS.find((x) => x.id === "@cf/moonshotai/kimi-k2.7-code")).toBeDefined();
49
+ expect(MODELS.find((x) => x.id === "moonshotai/kimi-k3")).toBeDefined();
50
+ });
51
+ });
@@ -0,0 +1,29 @@
1
+ // Catalog pins for the v0.172.0 Sprint 3 image-sibling adds (data-only).
2
+
3
+ import { describe, it, expect } from "vitest";
4
+ import { MODELS } from "../src/models";
5
+
6
+ describe("v0.172.0 sprint 3 catalog entries", () => {
7
+ const cases: Array<{ id: string; provider: string }> = [
8
+ { id: "google/nano-banana-2-lite", provider: "google" },
9
+ { id: "recraft/recraftv4-1", provider: "recraft" },
10
+ { id: "xai/grok-imagine-image-quality", provider: "xai" },
11
+ { id: "bytedance/seedream-5-lite", provider: "bytedance" },
12
+ ];
13
+
14
+ for (const c of cases) {
15
+ it(`adds ${c.id} as ${c.provider} image`, () => {
16
+ const m = MODELS.find((x) => x.id === c.id);
17
+ expect(m).toBeDefined();
18
+ expect(m?.provider).toBe(c.provider);
19
+ expect(m?.type).toBe("image");
20
+ });
21
+ }
22
+
23
+ it("keeps pro/base siblings alongside the new tier rows", () => {
24
+ expect(MODELS.find((x) => x.id === "google/nano-banana-2")).toBeDefined();
25
+ expect(MODELS.find((x) => x.id === "recraft/recraftv4-1-pro")).toBeDefined();
26
+ expect(MODELS.find((x) => x.id === "xai/grok-imagine-image")).toBeDefined();
27
+ expect(MODELS.find((x) => x.id === "bytedance/seedream-5-pro")).toBeDefined();
28
+ });
29
+ });
@@ -0,0 +1,127 @@
1
+ // Tests for the v0.170.0 binding dispatch in src/providers/xai.ts.
2
+ //
3
+ // xai/grok-4.5 is flagged binding: true, so callXai / callXaiStream route
4
+ // through env.AI.run (Unified Billing catalog) instead of the legacy AI Gateway
5
+ // grok provider fetch. Mirrors tests/anthropic-binding.test.ts.
6
+
7
+ import { describe, it, expect } from "vitest";
8
+ import type { AiContext } from "../src/ai-binding";
9
+ import type { ModelEntry } from "../src/models";
10
+ import { MODELS } from "../src/models";
11
+ import { callXai, callXaiStream } from "../src/providers/xai";
12
+ import type { ProviderStreamEvent } from "../src/parsers/types";
13
+
14
+ const grok45: ModelEntry = {
15
+ id: "xai/grok-4.5",
16
+ label: "Grok 4.5 (xAI)",
17
+ group: "Chat · xAI",
18
+ type: "chat",
19
+ capabilities: ["vision"],
20
+ provider: "xai",
21
+ streaming: true,
22
+ binding: true,
23
+ };
24
+
25
+ type RunCall = { model: string; params: Record<string, unknown>; opts: unknown };
26
+
27
+ function fakeCtx(runImpl: (model: string, params: unknown, opts: unknown) => Promise<unknown>): { ctx: AiContext; calls: RunCall[] } {
28
+ const calls: RunCall[] = [];
29
+ const env = {
30
+ AI: {
31
+ run: (model: string, params: unknown, opts: unknown) => {
32
+ calls.push({ model, params: params as Record<string, unknown>, opts });
33
+ return runImpl(model, params, opts);
34
+ },
35
+ aiGatewayLogId: "log-xai-123",
36
+ },
37
+ } as unknown as AiContext["env"];
38
+ const ctx = { env, gateway: { gatewayId: "skyphusion-llm", cfAigToken: "tok" } } as unknown as AiContext;
39
+ return { ctx, calls };
40
+ }
41
+
42
+ function sseStream(frames: unknown[], chunkSize = 11): ReadableStream<Uint8Array> {
43
+ const text = frames.map((f) => "data: " + JSON.stringify(f) + "\n\n").join("") + "data: [DONE]\n\n";
44
+ const bytes = new TextEncoder().encode(text);
45
+ let i = 0;
46
+ return new ReadableStream<Uint8Array>({
47
+ pull(controller) {
48
+ if (i >= bytes.length) { controller.close(); return; }
49
+ const end = Math.min(i + chunkSize, bytes.length);
50
+ controller.enqueue(bytes.slice(i, end));
51
+ i = end;
52
+ },
53
+ });
54
+ }
55
+
56
+ const userMessages = [{ role: "user", content: "hi" }];
57
+
58
+ describe("catalog", () => {
59
+ it("has grok-4.5 flagged binding: true, provider xai, streaming, vision", () => {
60
+ const m = MODELS.find((x) => x.id === "xai/grok-4.5");
61
+ expect(m).toBeDefined();
62
+ expect(m?.binding).toBe(true);
63
+ expect(m?.provider).toBe("xai");
64
+ expect(m?.streaming).toBe(true);
65
+ expect(m?.capabilities).toContain("vision");
66
+ });
67
+ });
68
+
69
+ describe("callXai binding dispatch (non-stream)", () => {
70
+ const completion = {
71
+ id: "chatcmpl-test",
72
+ object: "chat.completion",
73
+ choices: [{ message: { role: "assistant", content: "hello" }, finish_reason: "stop" }],
74
+ usage: { prompt_tokens: 3, completion_tokens: 1, total_tokens: 4 },
75
+ };
76
+
77
+ it("calls env.AI.run with the full catalog id and Chat Completions body", async () => {
78
+ const { ctx, calls } = fakeCtx(async () => completion);
79
+ const { raw, logId } = await callXai(ctx, grok45, userMessages);
80
+
81
+ expect(raw).toBe(completion);
82
+ expect(logId).toBe("log-xai-123");
83
+ expect(calls).toHaveLength(1);
84
+ expect(calls[0].model).toBe("xai/grok-4.5");
85
+ expect(calls[0].opts).toEqual({ gateway: { id: "skyphusion-llm" } });
86
+
87
+ const p = calls[0].params;
88
+ expect(p.messages).toEqual(userMessages);
89
+ expect(p.max_completion_tokens).toBe(4096);
90
+ expect(p.stream).toBeUndefined();
91
+ // Binding path does not strip the xai/ prefix into a separate model field.
92
+ expect(p.model).toBeUndefined();
93
+ });
94
+
95
+ it("a non-binding xai model does NOT take the binding path", async () => {
96
+ const { ctx, calls } = fakeCtx(async () => completion);
97
+ const legacy: ModelEntry = { ...grok45, id: "xai/grok-4.3", binding: undefined };
98
+ await expect(callXai(ctx, legacy, userMessages)).rejects.toThrow();
99
+ expect(calls).toHaveLength(0);
100
+ });
101
+ });
102
+
103
+ describe("callXaiStream binding dispatch", () => {
104
+ it("streams OpenAI-compatible SSE through interpretXaiSSEFrame", async () => {
105
+ const frames = [
106
+ { choices: [{ delta: { content: "Hel" } }] },
107
+ { choices: [{ delta: { content: "lo" } }] },
108
+ { choices: [], usage: { prompt_tokens: 2, completion_tokens: 2, total_tokens: 4 } },
109
+ ];
110
+ const { ctx, calls } = fakeCtx(async () => sseStream(frames));
111
+ const ac = new AbortController();
112
+ const events: ProviderStreamEvent[] = [];
113
+ for await (const e of callXaiStream(ctx, grok45, userMessages, ac.signal)) {
114
+ events.push(e);
115
+ }
116
+
117
+ expect(calls).toHaveLength(1);
118
+ expect(calls[0].model).toBe("xai/grok-4.5");
119
+ expect(calls[0].params.stream).toBe(true);
120
+ expect(calls[0].params.stream_options).toEqual({ include_usage: true });
121
+ expect(events).toEqual([
122
+ { type: "text", text: "Hel" },
123
+ { type: "text", text: "lo" },
124
+ { type: "usage", in_: 2, out_: 2 },
125
+ ]);
126
+ });
127
+ });
@@ -143,6 +143,10 @@ describe("routing + 404 fallthrough", () => {
143
143
  });
144
144
 
145
145
  describe("getUserEmail header / anonymous fallback (src/index.ts:204)", () => {
146
+ beforeEach(() => {
147
+ (env as unknown as { ACCESS_ALLOW_ANONYMOUS?: string }).ACCESS_ALLOW_ANONYMOUS = "1";
148
+ });
149
+
146
150
  it("falls back to anonymous when the Access header is absent", async () => {
147
151
  const res = await req("/api/models");
148
152
  const body = (await res.json()) as { user: string };
@@ -292,8 +296,11 @@ describe("gateway 412 refusal path", () => {
292
296
  body: { model: WORKERS_AI_CHAT, user_input: "hi" },
293
297
  });
294
298
  expect(res.status).toBe(412);
295
- const body = (await res.json()) as { error: string };
296
- expect(body.error).toContain("AI Gateway not configured");
299
+ const body = (await res.json()) as { error: string; code?: string };
300
+ // Copy mentions both BYOK gateway and control-plane pcp_ (v0.175.3).
301
+ expect(body.code).toBe("gateway_not_configured");
302
+ expect(body.error).toContain("Inference not configured");
303
+ expect(body.error).toMatch(/pcp_/);
297
304
  });
298
305
 
299
306
  it("positive control: once a gateway id is set, the 412 gate no longer fires", async () => {
@@ -2,11 +2,13 @@ name = "skyphusion-llm"
2
2
  main = "src/index.ts"
3
3
  compatibility_date = "2026-05-01"
4
4
 
5
- # The real route is the Access-gated custom domain play.skyphusion.org, so the
6
- # *.workers.dev route is unneeded (and bypasses Access). workers_dev = false
7
- # removes it. NOTE: this is the committed TEMPLATE; the live worker deploys
8
- # from the per-deployer wrangler.toml (gitignored), so set it there too and
9
- # redeploy (defense-in-depth; fleet-chezmoi#46).
5
+ # Live play.skyphusion.org runs AUTH_MODE=public (first-party signup; CF Access
6
+ # is NOT used on play). Private self-hosts typically use AUTH_MODE=access with
7
+ # Access on a custom domain. Either way, keep workers_dev = false so the
8
+ # *.workers.dev URL is not an ungated alternate surface. NOTE: this is the
9
+ # committed TEMPLATE; the live worker deploys from the per-deployer
10
+ # wrangler.toml (gitignored), so set it there too and redeploy
11
+ # (defense-in-depth; fleet-chezmoi#46).
10
12
  workers_dev = false
11
13
 
12
14
  # No scheduled tasks. The render-notify sweep that used a `*/3` cron (resolving
@@ -124,6 +126,12 @@ enabled = true
124
126
  # gateway fallback stays available. This is the default (back-compat).
125
127
  # [vars]
126
128
  # AUTH_MODE = "public"
129
+ # Control plane (v0.175.0): optional metered chat via prism-control-plane.
130
+ # Users store only a pcp_ key in prefs; origin is NOT user-set (SSRF).
131
+ # Default allowlisted host is play-proxy.skyphusion.org. Do NOT add another
132
+ # commented vars header (CI uncomments every "# [vars]" line and TOML breaks).
133
+ # CONTROL_PLANE_URL = "https://play-proxy.skyphusion.org"
134
+ # CONTROL_PLANE_ALLOW_LOCALHOST = "true"
127
135
 
128
136
  # Note: GATEWAY_ID is set as a worker secret rather than a var in this file
129
137
  # (v0.12.0+). This keeps deployer-specific values out of the committed template.
@@ -132,11 +140,15 @@ enabled = true
132
140
  # For local development, also add it to .dev.vars:
133
141
  # GATEWAY_ID=your-gateway-slug
134
142
  #
135
- # Public demo mode (v0.164.0+): omit GATEWAY_ID and CF_AIG_TOKEN on the worker.
143
+ # Public mode (v0.167.0+): omit GATEWAY_ID and CF_AIG_TOKEN on the worker.
136
144
  # Each visitor stores their own gateway slug + token via Account > AI Gateway
137
- # (/api/prefs in D1 user_prefs). You still need Cloudflare Access in front.
145
+ # (/api/prefs in D1 user_prefs). No Cloudflare Access needed when AUTH_MODE=public.
138
146
  #
139
147
  # Other worker secrets (also set via `npx wrangler secret put <NAME>`):
148
+ # OPENAI_API_KEY (optional sole BYOK carve-out, v0.174.0): when set,
149
+ # openai/gpt-image-* call api.openai.com directly for transparent PNG
150
+ # (background + output_format). When unset, those models stay on Unified
151
+ # Billing and render opaque. No other provider reads a deployer key.
140
152
  # CF_AIG_TOKEN (required for Anthropic, xAI, OpenAI chat, Google, and proxied
141
153
  # image/video/music on Unified Billing when using worker-level
142
154
  # credentials; optional in public demo mode where users supply