@skyphusion/create-prism 0.1.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/LICENSE +235 -0
- package/NOTICE +3 -0
- package/README.md +114 -0
- package/index.js +195 -0
- package/package.json +44 -0
- package/template/CONTRIBUTING.md +80 -0
- package/template/LICENSE +235 -0
- package/template/MIGRATIONS.md +197 -0
- package/template/NOTICE +3 -0
- package/template/README.md +875 -0
- package/template/docs/legal/INSTANCE-ACCEPTABLE-USE.md +98 -0
- package/template/docs/legal/INSTANCE-PRIVACY.md +112 -0
- package/template/docs/legal/README.md +11 -0
- package/template/docs/screenshot-desktop.jpg +0 -0
- package/template/docs/screenshot-mobile.jpg +0 -0
- package/template/gitignore +23 -0
- package/template/migrate-v0.164.0.sql +6 -0
- package/template/migrate-v0.167.0.sql +44 -0
- package/template/migrate-v0.20.0.sql +40 -0
- package/template/migrate-v0.20.2.sql +19 -0
- package/template/migrate-v0.20.3.sql +36 -0
- package/template/migrations/0001_init.sql +134 -0
- package/template/package.json +32 -0
- package/template/public/app.js +2778 -0
- package/template/public/apple-touch-icon.png +0 -0
- package/template/public/favicon-16.png +0 -0
- package/template/public/favicon-32.png +0 -0
- package/template/public/favicon.svg +5 -0
- package/template/public/icon-192.png +0 -0
- package/template/public/icon-512.png +0 -0
- package/template/public/index.html +329 -0
- package/template/public/manifest.webmanifest +14 -0
- package/template/public/streaming-client.js +207 -0
- package/template/public/stt.html +68 -0
- package/template/public/stt.js +16 -0
- package/template/public/styles.css +2068 -0
- package/template/public/voice-widget.js +195 -0
- package/template/schema.sql +283 -0
- package/template/src/ai-binding.ts +33 -0
- package/template/src/auth-kdf.ts +106 -0
- package/template/src/auth.ts +315 -0
- package/template/src/chunking.ts +161 -0
- package/template/src/discord.ts +353 -0
- package/template/src/env.ts +55 -0
- package/template/src/gateway-credentials.ts +97 -0
- package/template/src/health-schema.ts +25 -0
- package/template/src/index.ts +4180 -0
- package/template/src/longrun-params.ts +135 -0
- package/template/src/models.ts +214 -0
- package/template/src/output-extract.ts +128 -0
- package/template/src/parsers/anthropic-sse.ts +56 -0
- package/template/src/parsers/gemini-sse.ts +62 -0
- package/template/src/parsers/openai-sse.ts +60 -0
- package/template/src/parsers/sse-framer.ts +39 -0
- package/template/src/parsers/types.ts +16 -0
- package/template/src/parsers/workers-ai-sse.ts +43 -0
- package/template/src/parsers/xai-sse.ts +40 -0
- package/template/src/providers/anthropic.ts +208 -0
- package/template/src/providers/google.ts +150 -0
- package/template/src/providers/openai.ts +69 -0
- package/template/src/providers/workers-ai.ts +83 -0
- package/template/src/providers/xai.ts +159 -0
- package/template/src/proxied-image-params.ts +43 -0
- package/template/src/rate-limit.ts +77 -0
- package/template/src/searxng.ts +75 -0
- package/template/src/session.ts +114 -0
- package/template/src/stt-session.ts +206 -0
- package/template/src/stt-util.ts +19 -0
- package/template/src/types.ts +56 -0
- package/template/src/user-prefs.ts +63 -0
- package/template/src/utils.ts +73 -0
- package/template/src/zip.ts +137 -0
- package/template/tests/anthropic-sse.test.ts +149 -0
- package/template/tests/auth-kdf.test.ts +59 -0
- package/template/tests/auth-unit.test.ts +97 -0
- package/template/tests/chunking.test.ts +217 -0
- package/template/tests/discord.test.ts +184 -0
- package/template/tests/gateway-credentials.test.ts +65 -0
- package/template/tests/gemini-sse.test.ts +75 -0
- package/template/tests/google-gemini.test.ts +65 -0
- package/template/tests/health-schema.test.ts +35 -0
- package/template/tests/longrun-params.test.ts +128 -0
- package/template/tests/openai-sse.test.ts +71 -0
- package/template/tests/output-extract.test.ts +171 -0
- package/template/tests/proxied-image-params.test.ts +51 -0
- package/template/tests/rate-limit.test.ts +45 -0
- package/template/tests/searxng.test.ts +51 -0
- package/template/tests/sse-framer.test.ts +113 -0
- package/template/tests/stt-session.test.ts +35 -0
- package/template/tests/utils.test.ts +39 -0
- package/template/tests/workers-ai-sse.test.ts +67 -0
- package/template/tests/xai-sse.test.ts +60 -0
- package/template/tests/zip.test.ts +145 -0
- package/template/tests-integration/auth.test.ts +371 -0
- package/template/tests-integration/worker.test.ts +322 -0
- package/template/tsconfig.json +18 -0
- package/template/vitest.config.ts +18 -0
- package/template/vitest.node.config.ts +19 -0
- package/template/vitest.workers.config.ts +47 -0
- package/template/wrangler.example.toml +151 -0
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
// Workers-runtime integration tests for the fetch handler (v0.164.4, issue #84).
|
|
2
|
+
//
|
|
3
|
+
// These run inside workerd (see vitest.workers.config.ts) and exercise
|
|
4
|
+
// src/index.ts end to end via the SELF service binding, with real local
|
|
5
|
+
// Miniflare D1 + R2. They cover the paths that had zero unit coverage and were
|
|
6
|
+
// verified only by live wrangler dev smoke: routing + 404 fallthrough,
|
|
7
|
+
// getUserEmail, per-user D1 scoping, the R2 ownership gate, /api/prefs, and the
|
|
8
|
+
// gateway 412 refusal path. #80 (auth-plane rewrite) can modify getUserEmail
|
|
9
|
+
// and watch these fail before shipping.
|
|
10
|
+
|
|
11
|
+
import { env, SELF } from "cloudflare:test";
|
|
12
|
+
import { beforeEach, describe, expect, it } from "vitest";
|
|
13
|
+
import { MODELS } from "../src/models";
|
|
14
|
+
import schemaSql from "../schema.sql?raw";
|
|
15
|
+
|
|
16
|
+
const ALICE = "alice@example.com";
|
|
17
|
+
const BOB = "bob@example.com";
|
|
18
|
+
|
|
19
|
+
// Apply schema.sql (the fresh-database DDL) statement by statement. The two
|
|
20
|
+
// ALTER TABLE ADD COLUMN statements are not idempotent in SQLite, so a re-apply
|
|
21
|
+
// on an already-migrated DB throws "duplicate column name"; that (and "already
|
|
22
|
+
// exists") is tolerated so this is safe to call before every test regardless of
|
|
23
|
+
// the pool storage-isolation model. Any other error is a real failure and
|
|
24
|
+
// rethrown.
|
|
25
|
+
async function applySchema(db: D1Database): Promise<void> {
|
|
26
|
+
const statements = schemaSql
|
|
27
|
+
.split("\n")
|
|
28
|
+
.map((line) => line.replace(/--.*$/, ""))
|
|
29
|
+
.join("\n")
|
|
30
|
+
.split(";")
|
|
31
|
+
.map((s) => s.trim())
|
|
32
|
+
.filter((s) => s.length > 0);
|
|
33
|
+
for (const stmt of statements) {
|
|
34
|
+
try {
|
|
35
|
+
await db.prepare(stmt).run();
|
|
36
|
+
} catch (e) {
|
|
37
|
+
const msg = String((e as { message?: string })?.message ?? e);
|
|
38
|
+
if (/duplicate column name|already exists/i.test(msg)) continue;
|
|
39
|
+
throw e;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const USER_TABLES = [
|
|
45
|
+
"chats",
|
|
46
|
+
"user_prefs",
|
|
47
|
+
"documents",
|
|
48
|
+
"chunks",
|
|
49
|
+
"projects",
|
|
50
|
+
"project_documents",
|
|
51
|
+
"project_messages",
|
|
52
|
+
];
|
|
53
|
+
|
|
54
|
+
beforeEach(async () => {
|
|
55
|
+
await applySchema(env.DB);
|
|
56
|
+
// Guarantee a clean data slate per test whether or not storage is isolated.
|
|
57
|
+
for (const t of USER_TABLES) {
|
|
58
|
+
await env.DB.prepare(`DELETE FROM ${t}`).run();
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
interface ReqOpts {
|
|
63
|
+
email?: string;
|
|
64
|
+
method?: string;
|
|
65
|
+
body?: unknown;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// Fire a request at the worker through SELF. Sets the Cloudflare Access header
|
|
69
|
+
// only when an email is provided so the anonymous fallback is exercisable.
|
|
70
|
+
function req(path: string, opts: ReqOpts = {}): Promise<Response> {
|
|
71
|
+
const headers = new Headers();
|
|
72
|
+
if (opts.email) headers.set("cf-access-authenticated-user-email", opts.email);
|
|
73
|
+
if (opts.body !== undefined) headers.set("content-type", "application/json");
|
|
74
|
+
return SELF.fetch(`https://prism.test${path}`, {
|
|
75
|
+
method: opts.method ?? "GET",
|
|
76
|
+
headers,
|
|
77
|
+
body: opts.body !== undefined ? JSON.stringify(opts.body) : undefined,
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
async function seedChat(
|
|
82
|
+
email: string,
|
|
83
|
+
opts: {
|
|
84
|
+
model?: string;
|
|
85
|
+
model_type?: string;
|
|
86
|
+
user_input?: string;
|
|
87
|
+
output?: string;
|
|
88
|
+
conversation_id?: string | null;
|
|
89
|
+
turn_index?: number;
|
|
90
|
+
output_artifact?: string | null;
|
|
91
|
+
} = {},
|
|
92
|
+
): Promise<void> {
|
|
93
|
+
await env.DB.prepare(
|
|
94
|
+
`INSERT INTO chats
|
|
95
|
+
(user_email, model, model_type, user_input, output, conversation_id, turn_index, status, output_artifact)
|
|
96
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, 'done', ?)`,
|
|
97
|
+
)
|
|
98
|
+
.bind(
|
|
99
|
+
email,
|
|
100
|
+
opts.model ?? "@cf/meta/llama-3.3-70b-instruct-fp8-fast",
|
|
101
|
+
opts.model_type ?? "chat",
|
|
102
|
+
opts.user_input ?? "hello",
|
|
103
|
+
opts.output ?? "hi there",
|
|
104
|
+
opts.conversation_id ?? null,
|
|
105
|
+
opts.turn_index ?? 0,
|
|
106
|
+
opts.output_artifact ?? null,
|
|
107
|
+
)
|
|
108
|
+
.run();
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// A Workers AI chat model (no explicit provider) needs no CF_AIG token, so the
|
|
112
|
+
// gateway 412 gate is the ONLY thing standing between a request and dispatch.
|
|
113
|
+
const WORKERS_AI_CHAT = MODELS.find((m) => m.type === "chat" && !m.provider)!.id;
|
|
114
|
+
|
|
115
|
+
describe("routing + 404 fallthrough", () => {
|
|
116
|
+
it("GET /health returns 200 without any binding access", async () => {
|
|
117
|
+
const res = await req("/health");
|
|
118
|
+
expect(res.status).toBe(200);
|
|
119
|
+
const body = (await res.json()) as { ok: boolean };
|
|
120
|
+
expect(body.ok).toBe(true);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it("GET /api/models matches the models route", async () => {
|
|
124
|
+
const res = await req("/api/models", { email: ALICE });
|
|
125
|
+
expect(res.status).toBe(200);
|
|
126
|
+
const body = (await res.json()) as { models: unknown[]; user: string };
|
|
127
|
+
expect(Array.isArray(body.models)).toBe(true);
|
|
128
|
+
expect(body.models.length).toBeGreaterThan(0);
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it("an unmatched path falls through to ASSETS", async () => {
|
|
132
|
+
const res = await req("/no/such/route", { email: ALICE });
|
|
133
|
+
expect(res.status).toBe(404);
|
|
134
|
+
expect(await res.text()).toBe("assets-fallthrough");
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it("a matched path with the wrong method falls through to ASSETS", async () => {
|
|
138
|
+
// /api/history is GET-only; POST must NOT be handled by the history route.
|
|
139
|
+
const res = await req("/api/history", { email: ALICE, method: "POST" });
|
|
140
|
+
expect(res.status).toBe(404);
|
|
141
|
+
expect(await res.text()).toBe("assets-fallthrough");
|
|
142
|
+
});
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
describe("getUserEmail header / anonymous fallback (src/index.ts:204)", () => {
|
|
146
|
+
it("falls back to anonymous when the Access header is absent", async () => {
|
|
147
|
+
const res = await req("/api/models");
|
|
148
|
+
const body = (await res.json()) as { user: string };
|
|
149
|
+
expect(body.user).toBe("anonymous");
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it("echoes the Access header email when present", async () => {
|
|
153
|
+
const res = await req("/api/models", { email: ALICE });
|
|
154
|
+
const body = (await res.json()) as { user: string };
|
|
155
|
+
expect(body.user).toBe(ALICE);
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
describe("per-user scoping: /api/history", () => {
|
|
160
|
+
it("returns only the callers rows", async () => {
|
|
161
|
+
await seedChat(ALICE, { user_input: "alice-one" });
|
|
162
|
+
await seedChat(ALICE, { user_input: "alice-two" });
|
|
163
|
+
await seedChat(BOB, { user_input: "bob-one" });
|
|
164
|
+
|
|
165
|
+
const aliceRes = await req("/api/history", { email: ALICE });
|
|
166
|
+
const alice = (await aliceRes.json()) as { user: string; chats: Array<{ user_input: string }> };
|
|
167
|
+
expect(alice.user).toBe(ALICE);
|
|
168
|
+
expect(alice.chats).toHaveLength(2);
|
|
169
|
+
expect(alice.chats.map((c) => c.user_input).sort()).toEqual(["alice-one", "alice-two"]);
|
|
170
|
+
|
|
171
|
+
const bobRes = await req("/api/history", { email: BOB });
|
|
172
|
+
const bob = (await bobRes.json()) as { chats: Array<{ user_input: string }> };
|
|
173
|
+
expect(bob.chats).toHaveLength(1);
|
|
174
|
+
expect(bob.chats[0].user_input).toBe("bob-one");
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
it("anonymous sees none of a named users rows", async () => {
|
|
178
|
+
await seedChat(ALICE, { user_input: "alice-secret" });
|
|
179
|
+
const res = await req("/api/history");
|
|
180
|
+
const body = (await res.json()) as { user: string; chats: unknown[] };
|
|
181
|
+
expect(body.user).toBe("anonymous");
|
|
182
|
+
expect(body.chats).toHaveLength(0);
|
|
183
|
+
});
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
describe("per-user scoping: /api/conversations", () => {
|
|
187
|
+
it("groups only the callers conversations", async () => {
|
|
188
|
+
await seedChat(ALICE, { conversation_id: "conv-a", turn_index: 0, user_input: "a-0" });
|
|
189
|
+
await seedChat(ALICE, { conversation_id: "conv-a", turn_index: 1, user_input: "a-1" });
|
|
190
|
+
await seedChat(ALICE, { conversation_id: "conv-b", turn_index: 0, user_input: "b-0" });
|
|
191
|
+
await seedChat(BOB, { conversation_id: "conv-c", turn_index: 0, user_input: "c-0" });
|
|
192
|
+
|
|
193
|
+
const aliceRes = await req("/api/conversations", { email: ALICE });
|
|
194
|
+
const alice = (await aliceRes.json()) as {
|
|
195
|
+
conversations: Array<{ conversation_id: string; turn_count: number }>;
|
|
196
|
+
};
|
|
197
|
+
const ids = alice.conversations.map((c) => c.conversation_id).sort();
|
|
198
|
+
expect(ids).toEqual(["conv-a", "conv-b"]);
|
|
199
|
+
const convA = alice.conversations.find((c) => c.conversation_id === "conv-a")!;
|
|
200
|
+
expect(convA.turn_count).toBe(2);
|
|
201
|
+
|
|
202
|
+
const bobRes = await req("/api/conversations", { email: BOB });
|
|
203
|
+
const bob = (await bobRes.json()) as { conversations: Array<{ conversation_id: string }> };
|
|
204
|
+
expect(bob.conversations.map((c) => c.conversation_id)).toEqual(["conv-c"]);
|
|
205
|
+
});
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
describe("R2 ownership gate: /api/artifact/*", () => {
|
|
209
|
+
const KEY = "out/owned.png";
|
|
210
|
+
const BYTES = new Uint8Array([1, 2, 3, 4]);
|
|
211
|
+
|
|
212
|
+
beforeEach(async () => {
|
|
213
|
+
await env.R2.put(KEY, BYTES, { customMetadata: { user_email: ALICE } });
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
it("serves the object to its owner", async () => {
|
|
217
|
+
const res = await req(`/api/artifact/${KEY}`, { email: ALICE });
|
|
218
|
+
expect(res.status).toBe(200);
|
|
219
|
+
expect(new Uint8Array(await res.arrayBuffer())).toEqual(BYTES);
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
it("rejects a foreign user with 403 (customMetadata.user_email mismatch)", async () => {
|
|
223
|
+
const res = await req(`/api/artifact/${KEY}`, { email: BOB });
|
|
224
|
+
expect(res.status).toBe(403);
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
it("rejects anonymous with 403", async () => {
|
|
228
|
+
const res = await req(`/api/artifact/${KEY}`);
|
|
229
|
+
expect(res.status).toBe(403);
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
it("returns 404 for a missing object", async () => {
|
|
233
|
+
const res = await req("/api/artifact/out/does-not-exist.png", { email: ALICE });
|
|
234
|
+
expect(res.status).toBe(404);
|
|
235
|
+
});
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
describe("/api/prefs GET/PATCH round-trip", () => {
|
|
239
|
+
it("GET reports unconfigured for a fresh user", async () => {
|
|
240
|
+
const res = await req("/api/prefs", { email: ALICE });
|
|
241
|
+
expect(res.status).toBe(200);
|
|
242
|
+
const body = (await res.json()) as {
|
|
243
|
+
gateway_id: string | null;
|
|
244
|
+
cf_aig_token_set: boolean;
|
|
245
|
+
configured: boolean;
|
|
246
|
+
};
|
|
247
|
+
expect(body.gateway_id).toBeNull();
|
|
248
|
+
expect(body.cf_aig_token_set).toBe(false);
|
|
249
|
+
expect(body.configured).toBe(false);
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
it("PATCH persists per user and never echoes the raw token", async () => {
|
|
253
|
+
const RAW_TOKEN = "cf-aig-supersecret-token-1234567890";
|
|
254
|
+
const patchRes = await req("/api/prefs", {
|
|
255
|
+
email: ALICE,
|
|
256
|
+
method: "PATCH",
|
|
257
|
+
body: { gateway_id: "alice-gw", cf_aig_token: RAW_TOKEN },
|
|
258
|
+
});
|
|
259
|
+
expect(patchRes.status).toBe(200);
|
|
260
|
+
const patched = (await patchRes.json()) as {
|
|
261
|
+
gateway_id: string | null;
|
|
262
|
+
cf_aig_token_set: boolean;
|
|
263
|
+
cf_aig_token_preview: string | null;
|
|
264
|
+
configured: boolean;
|
|
265
|
+
};
|
|
266
|
+
expect(patched.gateway_id).toBe("alice-gw");
|
|
267
|
+
expect(patched.cf_aig_token_set).toBe(true);
|
|
268
|
+
expect(patched.configured).toBe(true);
|
|
269
|
+
// Secret hygiene: the response carries a mask, never the raw token.
|
|
270
|
+
expect(patched.cf_aig_token_preview).not.toBe(RAW_TOKEN);
|
|
271
|
+
expect(patched.cf_aig_token_preview).not.toContain(RAW_TOKEN.slice(0, 8));
|
|
272
|
+
|
|
273
|
+
// Round-trip: a fresh GET reads the persisted prefs back.
|
|
274
|
+
const getRes = await req("/api/prefs", { email: ALICE });
|
|
275
|
+
const got = (await getRes.json()) as { gateway_id: string | null; configured: boolean };
|
|
276
|
+
expect(got.gateway_id).toBe("alice-gw");
|
|
277
|
+
expect(got.configured).toBe(true);
|
|
278
|
+
|
|
279
|
+
// Isolation: bobs prefs are untouched.
|
|
280
|
+
const bobRes = await req("/api/prefs", { email: BOB });
|
|
281
|
+
const bob = (await bobRes.json()) as { gateway_id: string | null; configured: boolean };
|
|
282
|
+
expect(bob.gateway_id).toBeNull();
|
|
283
|
+
expect(bob.configured).toBe(false);
|
|
284
|
+
});
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
describe("gateway 412 refusal path", () => {
|
|
288
|
+
it("POST /api/chat returns 412 when no gateway id resolves", async () => {
|
|
289
|
+
const res = await req("/api/chat", {
|
|
290
|
+
email: ALICE,
|
|
291
|
+
method: "POST",
|
|
292
|
+
body: { model: WORKERS_AI_CHAT, user_input: "hi" },
|
|
293
|
+
});
|
|
294
|
+
expect(res.status).toBe(412);
|
|
295
|
+
const body = (await res.json()) as { error: string };
|
|
296
|
+
expect(body.error).toContain("AI Gateway not configured");
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
it("positive control: once a gateway id is set, the 412 gate no longer fires", async () => {
|
|
300
|
+
await req("/api/prefs", {
|
|
301
|
+
email: ALICE,
|
|
302
|
+
method: "PATCH",
|
|
303
|
+
body: { gateway_id: "alice-gw" },
|
|
304
|
+
});
|
|
305
|
+
// Past the gate the stubbed AI binding makes dispatch fail (500), which is
|
|
306
|
+
// fine: the point is only that the 412 refusal is no longer returned, so
|
|
307
|
+
// the gate is proven non-vacuous rather than always-on.
|
|
308
|
+
let status: number;
|
|
309
|
+
try {
|
|
310
|
+
status = (
|
|
311
|
+
await req("/api/chat", {
|
|
312
|
+
email: ALICE,
|
|
313
|
+
method: "POST",
|
|
314
|
+
body: { model: WORKERS_AI_CHAT, user_input: "hi" },
|
|
315
|
+
})
|
|
316
|
+
).status;
|
|
317
|
+
} catch {
|
|
318
|
+
status = 500;
|
|
319
|
+
}
|
|
320
|
+
expect(status).not.toBe(412);
|
|
321
|
+
});
|
|
322
|
+
});
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"module": "ES2022",
|
|
5
|
+
"moduleResolution": "Bundler",
|
|
6
|
+
"lib": ["ES2022"],
|
|
7
|
+
"types": ["@cloudflare/workers-types"],
|
|
8
|
+
"strict": true,
|
|
9
|
+
"esModuleInterop": true,
|
|
10
|
+
"skipLibCheck": true,
|
|
11
|
+
"forceConsistentCasingInFileNames": true,
|
|
12
|
+
"noEmit": true,
|
|
13
|
+
"isolatedModules": true,
|
|
14
|
+
"resolveJsonModule": true
|
|
15
|
+
},
|
|
16
|
+
"include": ["src/**/*.ts"],
|
|
17
|
+
"exclude": ["node_modules"]
|
|
18
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { defineConfig } from "vitest/config";
|
|
2
|
+
|
|
3
|
+
// Root Vitest config (v0.164.4): aggregates two projects so a single
|
|
4
|
+
// `npm test` (`vitest run`) runs both suites, locally and in CI.
|
|
5
|
+
//
|
|
6
|
+
// - "node" -> vitest.node.config.ts pure-function tests (Node env)
|
|
7
|
+
// - "workers" -> vitest.workers.config.ts fetch-handler integration tests
|
|
8
|
+
// (workerd via pool-workers)
|
|
9
|
+
//
|
|
10
|
+
// See each project config for why the split exists.
|
|
11
|
+
export default defineConfig({
|
|
12
|
+
test: {
|
|
13
|
+
projects: [
|
|
14
|
+
"./vitest.node.config.ts",
|
|
15
|
+
"./vitest.workers.config.ts",
|
|
16
|
+
],
|
|
17
|
+
},
|
|
18
|
+
});
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { defineConfig } from "vitest/config";
|
|
2
|
+
|
|
3
|
+
// Node-env pure-function suite (v0.18.0, split out in v0.164.4).
|
|
4
|
+
//
|
|
5
|
+
// The parsers under test (parseBedrockEventStreamFrames, SSE framers, chunking,
|
|
6
|
+
// output extraction, param builders, Discord parsing) use only standard web
|
|
7
|
+
// APIs (TextDecoder, Uint8Array, DataView) available natively in Node as well
|
|
8
|
+
// as the Workers runtime. They do NOT need the Workers runtime, so they run
|
|
9
|
+
// under plain Vitest in Node: substantially faster than booting workerd.
|
|
10
|
+
//
|
|
11
|
+
// The fetch-handler integration suite lives in the separate Workers-runtime
|
|
12
|
+
// project (vitest.workers.config.ts); both are aggregated by vitest.config.ts.
|
|
13
|
+
export default defineConfig({
|
|
14
|
+
test: {
|
|
15
|
+
name: "node",
|
|
16
|
+
include: ["tests/**/*.test.ts"],
|
|
17
|
+
environment: "node",
|
|
18
|
+
},
|
|
19
|
+
});
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { defineConfig } from "vitest/config";
|
|
2
|
+
import { cloudflareTest } from "@cloudflare/vitest-pool-workers";
|
|
3
|
+
|
|
4
|
+
// Workers-runtime integration suite (v0.164.4, issue #84).
|
|
5
|
+
//
|
|
6
|
+
// Runs the actual worker fetch handler (src/index.ts) inside workerd via
|
|
7
|
+
// @cloudflare/vitest-pool-workers, so routing, getUserEmail, per-user D1
|
|
8
|
+
// scoping, the R2 ownership gate, /api/prefs, and the gateway 412 path are
|
|
9
|
+
// under CI instead of verified only by live wrangler dev smoke.
|
|
10
|
+
//
|
|
11
|
+
// Bindings are Miniflare-local and fork-safe (no secrets, no network):
|
|
12
|
+
// - DB (D1) and R2 are real local Miniflare simulators; the schema is
|
|
13
|
+
// applied per test from schema.sql (see tests-integration/worker.test.ts).
|
|
14
|
+
// - AI, VEC, LONGRUN, STT_SESSION are inert JSON stubs. The routes under
|
|
15
|
+
// test never call them; the gateway 412 gate short-circuits before any
|
|
16
|
+
// env.AI.run, so no Workers AI / gateway network call is ever made.
|
|
17
|
+
// - ASSETS is a mock Fetcher returning 404, so the "no route matched"
|
|
18
|
+
// fallthrough is observable without shipping the public/ tree into the
|
|
19
|
+
// runtime.
|
|
20
|
+
//
|
|
21
|
+
// compatibilityDate mirrors wrangler.example.toml so the module parses under
|
|
22
|
+
// the same runtime semantics as production.
|
|
23
|
+
export default defineConfig({
|
|
24
|
+
plugins: [
|
|
25
|
+
cloudflareTest({
|
|
26
|
+
main: "./src/index.ts",
|
|
27
|
+
miniflare: {
|
|
28
|
+
compatibilityDate: "2026-05-01",
|
|
29
|
+
d1Databases: ["DB"],
|
|
30
|
+
r2Buckets: ["R2"],
|
|
31
|
+
bindings: {
|
|
32
|
+
AI: {},
|
|
33
|
+
VEC: {},
|
|
34
|
+
LONGRUN: {},
|
|
35
|
+
STT_SESSION: {},
|
|
36
|
+
},
|
|
37
|
+
serviceBindings: {
|
|
38
|
+
ASSETS: () => new Response("assets-fallthrough", { status: 404 }),
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
}),
|
|
42
|
+
],
|
|
43
|
+
test: {
|
|
44
|
+
name: "workers",
|
|
45
|
+
include: ["tests-integration/**/*.test.ts"],
|
|
46
|
+
},
|
|
47
|
+
});
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
name = "skyphusion-llm"
|
|
2
|
+
main = "src/index.ts"
|
|
3
|
+
compatibility_date = "2026-05-01"
|
|
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).
|
|
10
|
+
workers_dev = false
|
|
11
|
+
|
|
12
|
+
# No scheduled tasks. The render-notify sweep that used a `*/3` cron (resolving
|
|
13
|
+
# fire-and-forget renders) moved to vivijure; the scheduled() handler was stripped
|
|
14
|
+
# from src/index.ts. An empty crons list is explicit on purpose: omitting the
|
|
15
|
+
# [triggers] block leaves a previously-deployed cron in place (wrangler only syncs
|
|
16
|
+
# triggers it can see), so this clears the stale schedule on the next deploy.
|
|
17
|
+
[triggers]
|
|
18
|
+
crons = []
|
|
19
|
+
|
|
20
|
+
# AI binding. The unified binding handles Workers AI models directly and routes
|
|
21
|
+
# third-party Unified Billing models (chat partners, video, music, proxied image)
|
|
22
|
+
# through the Cloudflare AI Gateway. Anthropic, xAI, and Google chat hit the
|
|
23
|
+
# gateway's per-provider endpoints keylessly via cf-aig-authorization.
|
|
24
|
+
[ai]
|
|
25
|
+
binding = "AI"
|
|
26
|
+
|
|
27
|
+
# D1 database for chat history. Fill in database_id after running:
|
|
28
|
+
# npx wrangler d1 create skyphusion-llm
|
|
29
|
+
[[d1_databases]]
|
|
30
|
+
binding = "DB"
|
|
31
|
+
database_name = "skyphusion-llm"
|
|
32
|
+
database_id = "PLACEHOLDER_RUN_wrangler_d1_create_THEN_PASTE_HERE"
|
|
33
|
+
# D1 schema migrations live in ./migrations/ (wrangler d1 migrations apply).
|
|
34
|
+
# CI applies pending migrations before every main deploy; see .github/workflows/ci.yml.
|
|
35
|
+
|
|
36
|
+
# R2 bucket for input + output artifacts (images, audio, video frames).
|
|
37
|
+
# Create with:
|
|
38
|
+
# npx wrangler r2 bucket create skyphusion-llm
|
|
39
|
+
[[r2_buckets]]
|
|
40
|
+
binding = "R2"
|
|
41
|
+
bucket_name = "skyphusion-llm"
|
|
42
|
+
|
|
43
|
+
# Vectorize index for RAG document embeddings. 768 dimensions matches
|
|
44
|
+
# @cf/baai/bge-base-en-v1.5; cosine similarity.
|
|
45
|
+
# Create with:
|
|
46
|
+
# npx wrangler vectorize create skyphusion-llm-vec --dimensions=768 --metric=cosine
|
|
47
|
+
[[vectorize]]
|
|
48
|
+
binding = "VEC"
|
|
49
|
+
index_name = "skyphusion-llm-vec"
|
|
50
|
+
|
|
51
|
+
# Static frontend (HTML, CSS, JS).
|
|
52
|
+
[assets]
|
|
53
|
+
directory = "./public"
|
|
54
|
+
binding = "ASSETS"
|
|
55
|
+
|
|
56
|
+
# Cloudflare Workflow for long-running Unified Billing video and music gen
|
|
57
|
+
# (v0.12.0). env.AI.run for these models blocks until completion (30s-3min),
|
|
58
|
+
# which exceeds the ~30s waitUntil budget after an HTTP response. Workflows
|
|
59
|
+
# provide a durable execution primitive: the LongRunWorkflow class invokes the
|
|
60
|
+
# model, downloads the result, uploads to R2, and finalizes the D1 row across
|
|
61
|
+
# independently-retryable steps. Each instance is observable in the Cloudflare
|
|
62
|
+
# dashboard.
|
|
63
|
+
[[workflows]]
|
|
64
|
+
name = "skyphusion-longrun"
|
|
65
|
+
binding = "LONGRUN"
|
|
66
|
+
class_name = "LongRunWorkflow"
|
|
67
|
+
|
|
68
|
+
# v0.108.0: per-session DO wrapping a @cf/deepgram/flux conversational STT
|
|
69
|
+
# WebSocket; persists the final transcript to /history on close. Source:
|
|
70
|
+
# src/stt-session.ts.
|
|
71
|
+
[[durable_objects.bindings]]
|
|
72
|
+
name = "STT_SESSION"
|
|
73
|
+
class_name = "SttSession"
|
|
74
|
+
|
|
75
|
+
# Migration history (v1-v6): all tags are cursor-advance-only for new deployments.
|
|
76
|
+
#
|
|
77
|
+
# The live skyphusion-llm worker was built up incrementally via direct CF API calls
|
|
78
|
+
# and out-of-band dashboard ops, NOT purely through wrangler migration tags. By the
|
|
79
|
+
# time wrangler.example.toml was introduced as a deploy template, the live worker
|
|
80
|
+
# already had:
|
|
81
|
+
# - SttSession DO namespace registered with use_sqlite=true (v3 intent applied)
|
|
82
|
+
# - AudioBeatSyncContainer, ImagePrepContainer, VideoFinishContainer DO namespaces
|
|
83
|
+
# deleted (v5 intent applied)
|
|
84
|
+
#
|
|
85
|
+
# Wrangler tracks migration cursor server-side. If the cursor is behind v6, trying
|
|
86
|
+
# to apply any of v1-v5 fails: new_sqlite_classes errors on existing instances
|
|
87
|
+
# (code 10074), and deleted_classes errors because those classes are no longer
|
|
88
|
+
# exported in the current script (also 10074). All tags below are therefore
|
|
89
|
+
# cursor-advance-only -- zero DO operations -- so wrangler skips past them cleanly
|
|
90
|
+
# to v6 regardless of where the cursor sits. DO state is correct on the live worker;
|
|
91
|
+
# these tags just bring the cursor in sync. (v0.164.2)
|
|
92
|
+
[[migrations]]
|
|
93
|
+
tag = "v1"
|
|
94
|
+
|
|
95
|
+
[[migrations]]
|
|
96
|
+
tag = "v2"
|
|
97
|
+
|
|
98
|
+
[[migrations]]
|
|
99
|
+
tag = "v3"
|
|
100
|
+
|
|
101
|
+
[[migrations]]
|
|
102
|
+
tag = "v4"
|
|
103
|
+
|
|
104
|
+
[[migrations]]
|
|
105
|
+
tag = "v5"
|
|
106
|
+
|
|
107
|
+
[[migrations]]
|
|
108
|
+
tag = "v6"
|
|
109
|
+
|
|
110
|
+
# Per-instance observability for the worker (lets you tail logs in the
|
|
111
|
+
# dashboard). Workflows have their own dashboard surface; this enables it
|
|
112
|
+
# for the rest of the worker too.
|
|
113
|
+
[observability]
|
|
114
|
+
enabled = true
|
|
115
|
+
|
|
116
|
+
# Deployment auth plane (v0.167.0, issue #80). A plain var, not a secret.
|
|
117
|
+
# AUTH_MODE = "public" -> first-party username/password accounts + session
|
|
118
|
+
# cookies; identity is the opaque account id; per-user AI Gateway credentials
|
|
119
|
+
# ONLY (worker GATEWAY_ID / CF_AIG_TOKEN are ignored so visitor inference
|
|
120
|
+
# never bills the host). Do NOT set GATEWAY_ID / CF_AIG_TOKEN on a public
|
|
121
|
+
# worker. No Cloudflare Access needed.
|
|
122
|
+
# unset or "access" -> Cloudflare Access in front, identity from the
|
|
123
|
+
# Cf-Access-Authenticated-User-Email header (private self-host). Worker-secret
|
|
124
|
+
# gateway fallback stays available. This is the default (back-compat).
|
|
125
|
+
# [vars]
|
|
126
|
+
# AUTH_MODE = "public"
|
|
127
|
+
|
|
128
|
+
# Note: GATEWAY_ID is set as a worker secret rather than a var in this file
|
|
129
|
+
# (v0.12.0+). This keeps deployer-specific values out of the committed template.
|
|
130
|
+
# Set it once after first deploy:
|
|
131
|
+
# echo "your-gateway-slug" | npx wrangler secret put GATEWAY_ID
|
|
132
|
+
# For local development, also add it to .dev.vars:
|
|
133
|
+
# GATEWAY_ID=your-gateway-slug
|
|
134
|
+
#
|
|
135
|
+
# Public demo mode (v0.164.0+): omit GATEWAY_ID and CF_AIG_TOKEN on the worker.
|
|
136
|
+
# 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.
|
|
138
|
+
#
|
|
139
|
+
# Other worker secrets (also set via `npx wrangler secret put <NAME>`):
|
|
140
|
+
# CF_AIG_TOKEN (required for Anthropic, xAI, OpenAI chat, Google, and proxied
|
|
141
|
+
# image/video/music on Unified Billing when using worker-level
|
|
142
|
+
# credentials; optional in public demo mode where users supply
|
|
143
|
+
# their own token via /api/prefs; v0.93.0+),
|
|
144
|
+
# SEARXNG_URL (optional, v0.166.0; base URL of a self-hosted SearXNG instance
|
|
145
|
+
# for the opt-in web-search source, e.g. https://search.skyphusion.org.
|
|
146
|
+
# May be a plain [vars] entry instead of a secret. Skipped when
|
|
147
|
+
# unset; Wikipedia still runs keyless),
|
|
148
|
+
# SEARXNG_ACCESS_CLIENT_ID / SEARXNG_ACCESS_CLIENT_SECRET (optional, v0.166.0;
|
|
149
|
+
# Cloudflare Access service-token halves for a gated SearXNG
|
|
150
|
+
# instance. Sent as CF-Access-Client-Id / CF-Access-Client-Secret
|
|
151
|
+
# only when BOTH are set; leave unset for an un-gated instance).
|