@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,171 @@
|
|
|
1
|
+
// Tests for extractOutput / extractUsage (v0.21.0).
|
|
2
|
+
//
|
|
3
|
+
// These two functions normalize every response shape that reaches the
|
|
4
|
+
// non-streaming chat path. The OpenAI proxied chat models added in v0.21.0
|
|
5
|
+
// rest entirely on extractOutput parsing the OpenAI shapes correctly, so the
|
|
6
|
+
// chat-completions and Responses API cases are the load-bearing ones here;
|
|
7
|
+
// the rest are regression coverage for the shapes that already shipped.
|
|
8
|
+
|
|
9
|
+
import { describe, it, expect } from "vitest";
|
|
10
|
+
import { extractOutput, extractUsage, detectProviderFailure, extractProxiedImageUrl } from "../src/output-extract";
|
|
11
|
+
|
|
12
|
+
describe("extractOutput", () => {
|
|
13
|
+
it("returns a bare string unchanged", () => {
|
|
14
|
+
expect(extractOutput("hello")).toBe("hello");
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it("reads Workers AI { response }", () => {
|
|
18
|
+
expect(extractOutput({ response: "from workers ai" })).toBe("from workers ai");
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it("reads a top-level string { result }", () => {
|
|
22
|
+
expect(extractOutput({ result: "from result" })).toBe("from result");
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
// --- OpenAI proxied: the v0.21.0 load-bearing cases ---
|
|
26
|
+
|
|
27
|
+
it("reads the OpenAI chat-completions shape { choices[0].message.content }", () => {
|
|
28
|
+
const r = {
|
|
29
|
+
choices: [{ index: 0, message: { role: "assistant", content: "openai chat reply" } }],
|
|
30
|
+
usage: { prompt_tokens: 12, completion_tokens: 34 },
|
|
31
|
+
};
|
|
32
|
+
expect(extractOutput(r)).toBe("openai chat reply");
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it("reads the OpenAI Responses API shape { output[].content[] } (output_text)", () => {
|
|
36
|
+
const r = {
|
|
37
|
+
output: [
|
|
38
|
+
{
|
|
39
|
+
type: "message",
|
|
40
|
+
content: [
|
|
41
|
+
{ type: "output_text", text: "first" },
|
|
42
|
+
{ type: "output_text", text: " second" },
|
|
43
|
+
],
|
|
44
|
+
},
|
|
45
|
+
],
|
|
46
|
+
usage: { prompt_tokens: 5, completion_tokens: 7 },
|
|
47
|
+
};
|
|
48
|
+
expect(extractOutput(r)).toBe("first second");
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("reads Responses API blocks typed plain 'text' as well as 'output_text'", () => {
|
|
52
|
+
const r = { output: [{ content: [{ type: "text", text: "plain text block" }] }] };
|
|
53
|
+
expect(extractOutput(r)).toBe("plain text block");
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
// --- Existing-shape regression coverage ---
|
|
57
|
+
|
|
58
|
+
it("reads the Anthropic Messages content array, text blocks only", () => {
|
|
59
|
+
const r = {
|
|
60
|
+
content: [
|
|
61
|
+
{ type: "thinking", text: "ignored" },
|
|
62
|
+
{ type: "text", text: "anthropic " },
|
|
63
|
+
{ type: "text", text: "answer" },
|
|
64
|
+
],
|
|
65
|
+
};
|
|
66
|
+
expect(extractOutput(r)).toBe("anthropic answer");
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it("reads Gemini candidates content parts", () => {
|
|
70
|
+
const r = { candidates: [{ content: { parts: [{ text: "gemini " }, { text: "answer" }] } }], modelVersion: "gemini-3.1-pro-preview" };
|
|
71
|
+
expect(extractOutput(r)).toBe("gemini answer");
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it("reads LLaVA image-to-text { description }", () => {
|
|
75
|
+
expect(extractOutput({ description: "a cat on a mat" })).toBe("a cat on a mat");
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it("falls back to JSON.stringify on an unrecognized shape", () => {
|
|
79
|
+
const weird = { nope: true };
|
|
80
|
+
expect(extractOutput(weird)).toBe(JSON.stringify(weird));
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("does not throw on null / undefined", () => {
|
|
84
|
+
expect(() => extractOutput(null)).not.toThrow();
|
|
85
|
+
expect(() => extractOutput(undefined)).not.toThrow();
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
describe("extractUsage", () => {
|
|
90
|
+
it("reads OpenAI prompt_tokens / completion_tokens", () => {
|
|
91
|
+
expect(extractUsage({ usage: { prompt_tokens: 100, completion_tokens: 50 } }))
|
|
92
|
+
.toEqual({ in_: 100, out_: 50 });
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it("reads Anthropic input_tokens / output_tokens", () => {
|
|
96
|
+
expect(extractUsage({ usage: { input_tokens: 8, output_tokens: 4 } }))
|
|
97
|
+
.toEqual({ in_: 8, out_: 4 });
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it("reads Gemini usageMetadata promptTokenCount / candidatesTokenCount", () => {
|
|
101
|
+
expect(extractUsage({ usageMetadata: { promptTokenCount: 8, candidatesTokenCount: 589, thoughtsTokenCount: 363 } }))
|
|
102
|
+
.toEqual({ in_: 8, out_: 589 });
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it("returns nulls when there is no usage object", () => {
|
|
106
|
+
expect(extractUsage({ response: "no usage here" })).toEqual({ in_: null, out_: null });
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it("does not throw on null / undefined", () => {
|
|
110
|
+
expect(() => extractUsage(null)).not.toThrow();
|
|
111
|
+
expect(() => extractUsage(undefined)).not.toThrow();
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
describe("detectProviderFailure", () => {
|
|
116
|
+
it("returns the upstream error from a { state: 'Failed', error } envelope", () => {
|
|
117
|
+
const r = { state: "Failed", error: "Model execution failed (Upstream provider is unavailable)" };
|
|
118
|
+
expect(detectProviderFailure(r)).toBe("Model execution failed (Upstream provider is unavailable)");
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it("falls back to the state when a Failed envelope has no error string", () => {
|
|
122
|
+
expect(detectProviderFailure({ state: "Failed" })).toBe('provider returned state "Failed"');
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it("treats any non-Completed state as a failure", () => {
|
|
126
|
+
expect(detectProviderFailure({ state: "Cancelled" })).toBe('provider returned state "Cancelled"');
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it("returns null for a Completed envelope (the async success shape)", () => {
|
|
130
|
+
expect(detectProviderFailure({ state: "Completed", result: { video: "https://..." } })).toBeNull();
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it("returns null for normal sync chat responses (no state field)", () => {
|
|
134
|
+
expect(detectProviderFailure({ choices: [{ message: { content: "hi" } }] })).toBeNull();
|
|
135
|
+
expect(detectProviderFailure({ content: [{ type: "text", text: "hi" }] })).toBeNull();
|
|
136
|
+
expect(detectProviderFailure({ response: "hi" })).toBeNull();
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it("does not throw on null / undefined / string", () => {
|
|
140
|
+
expect(detectProviderFailure(null)).toBeNull();
|
|
141
|
+
expect(detectProviderFailure(undefined)).toBeNull();
|
|
142
|
+
expect(detectProviderFailure("plain string")).toBeNull();
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
describe("extractProxiedImageUrl", () => {
|
|
147
|
+
it("reads the wrapped { state, result: { image } } envelope", () => {
|
|
148
|
+
const r = { state: "Completed", result: { image: "https://r2.dev/cat/img.png" }, gatewayMetadata: { keySource: "Unified" } };
|
|
149
|
+
expect(extractProxiedImageUrl(r)).toBe("https://r2.dev/cat/img.png");
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it("reads the bare { image } shape (no binding wrapper)", () => {
|
|
153
|
+
expect(extractProxiedImageUrl({ image: "https://r2.dev/x.jpg" })).toBe("https://r2.dev/x.jpg");
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
it("prefers the wrapped result.image over a top-level image", () => {
|
|
157
|
+
const r = { result: { image: "https://r2.dev/wrapped.png" }, image: "https://r2.dev/bare.png" };
|
|
158
|
+
expect(extractProxiedImageUrl(r)).toBe("https://r2.dev/wrapped.png");
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it("returns null when no image url is present", () => {
|
|
162
|
+
expect(extractProxiedImageUrl({ state: "Completed", result: {} })).toBeNull();
|
|
163
|
+
expect(extractProxiedImageUrl({ choices: [] })).toBeNull();
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
it("does not throw on null / undefined / string", () => {
|
|
167
|
+
expect(extractProxiedImageUrl(null)).toBeNull();
|
|
168
|
+
expect(extractProxiedImageUrl(undefined)).toBeNull();
|
|
169
|
+
expect(extractProxiedImageUrl("nope")).toBeNull();
|
|
170
|
+
});
|
|
171
|
+
});
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
// Tests for buildProxiedImageParams (v0.22.0).
|
|
2
|
+
//
|
|
3
|
+
// The proxied image models each have an additionalProperties:false schema, so
|
|
4
|
+
// the load-bearing property is that each provider gets ONLY its accepted keys
|
|
5
|
+
// and never the @cf shape (width/height/steps/negative_prompt). The openai
|
|
6
|
+
// case is the one that matters most: it must carry background:"transparent"
|
|
7
|
+
// (the whole reason gpt-image-1.5 was added) plus a transparency-capable
|
|
8
|
+
// output_format.
|
|
9
|
+
|
|
10
|
+
import { describe, it, expect } from "vitest";
|
|
11
|
+
import { buildProxiedImageParams } from "../src/proxied-image-params";
|
|
12
|
+
|
|
13
|
+
describe("buildProxiedImageParams", () => {
|
|
14
|
+
it("google (nano-banana) sends prompt + png output_format only", () => {
|
|
15
|
+
expect(buildProxiedImageParams("google", "a coin")).toEqual({
|
|
16
|
+
prompt: "a coin",
|
|
17
|
+
output_format: "png",
|
|
18
|
+
});
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it("openai (gpt-image-1.5) sends only proxy-valid fields (opaque; proxy rejects background/output_format)", () => {
|
|
22
|
+
const params = buildProxiedImageParams("openai", "a coin sprite");
|
|
23
|
+
expect(params).toEqual({ prompt: "a coin sprite", quality: "high", size: "1024x1024" });
|
|
24
|
+
// The proxy 7003-rejects these; they must NOT be sent on this path.
|
|
25
|
+
expect("background" in params).toBe(false);
|
|
26
|
+
expect("output_format" in params).toBe(false);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("recraft (recraftv4) sends size + style, never a background flag", () => {
|
|
30
|
+
const params = buildProxiedImageParams("recraft", "a logo");
|
|
31
|
+
expect(params).toEqual({
|
|
32
|
+
prompt: "a logo",
|
|
33
|
+
size: "1024x1024",
|
|
34
|
+
style: "digital_illustration",
|
|
35
|
+
});
|
|
36
|
+
expect("background" in params).toBe(false);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("never leaks the @cf request shape for any proxied provider", () => {
|
|
40
|
+
for (const p of ["google", "openai", "recraft"] as const) {
|
|
41
|
+
const params = buildProxiedImageParams(p, "x");
|
|
42
|
+
for (const forbidden of ["width", "height", "steps", "negative_prompt"]) {
|
|
43
|
+
expect(forbidden in params).toBe(false);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("falls back to a bare prompt for an unknown/undefined provider", () => {
|
|
49
|
+
expect(buildProxiedImageParams(undefined, "x")).toEqual({ prompt: "x" });
|
|
50
|
+
});
|
|
51
|
+
});
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// Pure-logic tests for the rate-limit window decision (v0.167.0, issue #80).
|
|
2
|
+
|
|
3
|
+
import { describe, expect, it } from "vitest";
|
|
4
|
+
import { rateLimitDecision } from "../src/rate-limit";
|
|
5
|
+
|
|
6
|
+
const LIMIT = 5;
|
|
7
|
+
const WINDOW = 900; // 15 min
|
|
8
|
+
|
|
9
|
+
describe("rateLimitDecision", () => {
|
|
10
|
+
it("allows and opens a fresh window when no prior record", () => {
|
|
11
|
+
const d = rateLimitDecision(1000, null, 0, LIMIT, WINDOW);
|
|
12
|
+
expect(d).toEqual({ allowed: true, nextCount: 1, nextWindowStart: 1000 });
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it("increments within a live window and allows up to the limit", () => {
|
|
16
|
+
// 4 prior attempts, this is the 5th -> still allowed at the limit.
|
|
17
|
+
const d = rateLimitDecision(1100, 1000, 4, LIMIT, WINDOW);
|
|
18
|
+
expect(d.allowed).toBe(true);
|
|
19
|
+
expect(d.nextCount).toBe(5);
|
|
20
|
+
expect(d.nextWindowStart).toBe(1000);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it("denies once the count would exceed the limit", () => {
|
|
24
|
+
const d = rateLimitDecision(1100, 1000, 5, LIMIT, WINDOW);
|
|
25
|
+
expect(d.allowed).toBe(false);
|
|
26
|
+
expect(d.nextCount).toBe(6);
|
|
27
|
+
expect(d.nextWindowStart).toBe(1000);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it("resets to a fresh window once the window has fully elapsed", () => {
|
|
31
|
+
const d = rateLimitDecision(1000 + WINDOW, 1000, 5, LIMIT, WINDOW);
|
|
32
|
+
expect(d).toEqual({ allowed: true, nextCount: 1, nextWindowStart: 1000 + WINDOW });
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it("denied attempts still count, keeping the bucket closed until rollover", () => {
|
|
36
|
+
let count = 5;
|
|
37
|
+
const start = 1000;
|
|
38
|
+
for (let i = 0; i < 3; i++) {
|
|
39
|
+
const d = rateLimitDecision(1200, start, count, LIMIT, WINDOW);
|
|
40
|
+
expect(d.allowed).toBe(false);
|
|
41
|
+
count = d.nextCount;
|
|
42
|
+
}
|
|
43
|
+
expect(count).toBe(8);
|
|
44
|
+
});
|
|
45
|
+
});
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { mapSearxngResults } from "../src/searxng";
|
|
3
|
+
|
|
4
|
+
describe("mapSearxngResults", () => {
|
|
5
|
+
it("maps results into title/url/snippet hits", () => {
|
|
6
|
+
const data = {
|
|
7
|
+
query: "cloudflare workers",
|
|
8
|
+
number_of_results: 2,
|
|
9
|
+
results: [
|
|
10
|
+
{ title: "Example", url: "https://example.com/a", content: "First hit." },
|
|
11
|
+
{ title: "Other", url: "https://example.com/b", content: "Second hit." },
|
|
12
|
+
],
|
|
13
|
+
};
|
|
14
|
+
expect(mapSearxngResults(data, 5)).toEqual([
|
|
15
|
+
{ title: "Example", url: "https://example.com/a", snippet: "First hit." },
|
|
16
|
+
{ title: "Other", url: "https://example.com/b", snippet: "Second hit." },
|
|
17
|
+
]);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("drops rows missing title or url and respects maxResults", () => {
|
|
21
|
+
const data = {
|
|
22
|
+
results: [
|
|
23
|
+
{ title: "Keep", url: "https://example.com/keep", content: "ok" },
|
|
24
|
+
{ title: "No URL", content: "skip" },
|
|
25
|
+
{ url: "https://example.com/no-title", content: "skip" },
|
|
26
|
+
{ title: "Third", url: "https://example.com/third", content: "ok" },
|
|
27
|
+
],
|
|
28
|
+
};
|
|
29
|
+
expect(mapSearxngResults(data, 1)).toEqual([
|
|
30
|
+
{ title: "Keep", url: "https://example.com/keep", snippet: "ok" },
|
|
31
|
+
]);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("collapses whitespace in snippets and tolerates a missing content field", () => {
|
|
35
|
+
const data = {
|
|
36
|
+
results: [
|
|
37
|
+
{ title: "Spacey", url: "https://example.com/s", content: " multi line\n\ttext " },
|
|
38
|
+
{ title: "NoContent", url: "https://example.com/n" },
|
|
39
|
+
],
|
|
40
|
+
};
|
|
41
|
+
expect(mapSearxngResults(data, 5)).toEqual([
|
|
42
|
+
{ title: "Spacey", url: "https://example.com/s", snippet: "multi line text" },
|
|
43
|
+
{ title: "NoContent", url: "https://example.com/n", snippet: "" },
|
|
44
|
+
]);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("returns an empty array when results is missing", () => {
|
|
48
|
+
expect(mapSearxngResults({}, 5)).toEqual([]);
|
|
49
|
+
expect(mapSearxngResults({ results: [] }, 5)).toEqual([]);
|
|
50
|
+
});
|
|
51
|
+
});
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
// Tests for extractSSEDataPayloads (v0.18.1), the shared SSE framer used by
|
|
2
|
+
// all three streaming provider parsers (xAI, Workers AI, Anthropic).
|
|
3
|
+
//
|
|
4
|
+
// Behavior under test is exactly what the inline implementations in
|
|
5
|
+
// callXaiStream / callWorkersAIStream / callAnthropicStream did before
|
|
6
|
+
// extraction; new framer is a pure relocation, not a redesign.
|
|
7
|
+
|
|
8
|
+
import { describe, it, expect } from "vitest";
|
|
9
|
+
import { extractSSEDataPayloads } from "../src/parsers/sse-framer";
|
|
10
|
+
|
|
11
|
+
describe("extractSSEDataPayloads", () => {
|
|
12
|
+
describe("buffer states", () => {
|
|
13
|
+
it("returns no payloads and empty remainder for empty buffer", () => {
|
|
14
|
+
const { payloads, remainder } = extractSSEDataPayloads("");
|
|
15
|
+
expect(payloads).toEqual([]);
|
|
16
|
+
expect(remainder).toBe("");
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it("returns no payloads and full buffer as remainder when no event boundary present", () => {
|
|
20
|
+
const buf = "data: {\"partial\":";
|
|
21
|
+
const { payloads, remainder } = extractSSEDataPayloads(buf);
|
|
22
|
+
expect(payloads).toEqual([]);
|
|
23
|
+
expect(remainder).toBe(buf);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("preserves incomplete trailing event as remainder", () => {
|
|
27
|
+
const buf = 'data: {"complete":1}\n\ndata: {"part';
|
|
28
|
+
const { payloads, remainder } = extractSSEDataPayloads(buf);
|
|
29
|
+
expect(payloads).toEqual(['{"complete":1}']);
|
|
30
|
+
expect(remainder).toBe('data: {"part');
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
describe("payload extraction", () => {
|
|
35
|
+
it("extracts a single complete event", () => {
|
|
36
|
+
const { payloads, remainder } = extractSSEDataPayloads('data: {"x":1}\n\n');
|
|
37
|
+
expect(payloads).toEqual(['{"x":1}']);
|
|
38
|
+
expect(remainder).toBe("");
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("extracts multiple events in order", () => {
|
|
42
|
+
const buf = 'data: {"a":1}\n\ndata: {"b":2}\n\ndata: {"c":3}\n\n';
|
|
43
|
+
const { payloads, remainder } = extractSSEDataPayloads(buf);
|
|
44
|
+
expect(payloads).toEqual(['{"a":1}', '{"b":2}', '{"c":3}']);
|
|
45
|
+
expect(remainder).toBe("");
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("handles compact `data:` prefix (no trailing space)", () => {
|
|
49
|
+
const { payloads } = extractSSEDataPayloads('data:{"compact":true}\n\n');
|
|
50
|
+
expect(payloads).toEqual(['{"compact":true}']);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it("handles mixed spaced and compact prefixes across events", () => {
|
|
54
|
+
const buf = 'data: {"a":1}\n\ndata:{"b":2}\n\n';
|
|
55
|
+
const { payloads } = extractSSEDataPayloads(buf);
|
|
56
|
+
expect(payloads).toEqual(['{"a":1}', '{"b":2}']);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it("ignores event:, id:, retry: lines and extracts only the data: payload", () => {
|
|
60
|
+
const buf =
|
|
61
|
+
'event: message_start\nid: msg_01\nretry: 5000\ndata: {"type":"message_start"}\n\n';
|
|
62
|
+
const { payloads } = extractSSEDataPayloads(buf);
|
|
63
|
+
expect(payloads).toEqual(['{"type":"message_start"}']);
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
describe("dropped events", () => {
|
|
68
|
+
it("drops the [DONE] sentinel", () => {
|
|
69
|
+
const buf = 'data: {"x":1}\n\ndata: [DONE]\n\n';
|
|
70
|
+
const { payloads } = extractSSEDataPayloads(buf);
|
|
71
|
+
expect(payloads).toEqual(['{"x":1}']);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it("drops whitespace-only events", () => {
|
|
75
|
+
const buf = ' \n\ndata: {"x":1}\n\n';
|
|
76
|
+
const { payloads } = extractSSEDataPayloads(buf);
|
|
77
|
+
expect(payloads).toEqual(['{"x":1}']);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it("drops events whose only data: line has empty content", () => {
|
|
81
|
+
// `data:` with nothing after produces an empty payload, which is skipped.
|
|
82
|
+
const buf = 'data:\n\ndata: {"x":1}\n\n';
|
|
83
|
+
const { payloads } = extractSSEDataPayloads(buf);
|
|
84
|
+
expect(payloads).toEqual(['{"x":1}']);
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
describe("multi-data-line behavior (last wins)", () => {
|
|
89
|
+
// SSE spec says multiple data: lines in one event should concat with
|
|
90
|
+
// newlines, but none of our three providers emit multi-line data fields.
|
|
91
|
+
// The pre-extraction implementations used "last data: line wins"; the
|
|
92
|
+
// framer preserves that exactly.
|
|
93
|
+
it("uses the last data: line when an event has multiple", () => {
|
|
94
|
+
const buf = 'data: {"first":1}\ndata: {"second":2}\n\n';
|
|
95
|
+
const { payloads } = extractSSEDataPayloads(buf);
|
|
96
|
+
expect(payloads).toEqual(['{"second":2}']);
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
describe("split-across-reads behavior", () => {
|
|
101
|
+
it("yields complete events and preserves partial remainder across reads", () => {
|
|
102
|
+
// First read: one complete event + start of second
|
|
103
|
+
const r1 = extractSSEDataPayloads('data: {"a":1}\n\ndata: {"b":');
|
|
104
|
+
expect(r1.payloads).toEqual(['{"a":1}']);
|
|
105
|
+
expect(r1.remainder).toBe('data: {"b":');
|
|
106
|
+
|
|
107
|
+
// Caller appends next chunk and re-frames
|
|
108
|
+
const r2 = extractSSEDataPayloads(r1.remainder + '2}\n\ndata: {"c":3}\n\n');
|
|
109
|
+
expect(r2.payloads).toEqual(['{"b":2}', '{"c":3}']);
|
|
110
|
+
expect(r2.remainder).toBe("");
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
});
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// Tests for the SttSession Durable Object's pure helpers. The DO itself bridges
|
|
2
|
+
// a hibernatable browser socket to an upstream @cf/deepgram/flux socket and
|
|
3
|
+
// persists the final transcript to /history on close; the socket lifecycle is
|
|
4
|
+
// exercised live, not unit-tested. buildTranscript (turn join) and
|
|
5
|
+
// sanitizeCloseCode (WebSocket close-code legality) are the testable pure bits.
|
|
6
|
+
|
|
7
|
+
import { describe, it, expect } from "vitest";
|
|
8
|
+
import { buildTranscript, sanitizeCloseCode } from "../src/stt-util";
|
|
9
|
+
|
|
10
|
+
describe("buildTranscript", () => {
|
|
11
|
+
it("joins non-empty, trimmed turns with single spaces", () => {
|
|
12
|
+
expect(
|
|
13
|
+
buildTranscript([{ text: "hello" }, { text: " world " }, { text: "" }, { text: " " }, { text: "again" }]),
|
|
14
|
+
).toBe("hello world again");
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it("returns an empty string when there are no usable turns", () => {
|
|
18
|
+
expect(buildTranscript([])).toBe("");
|
|
19
|
+
expect(buildTranscript([{ text: "" }, { text: " " }])).toBe("");
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
describe("sanitizeCloseCode", () => {
|
|
24
|
+
it("passes legal codes through (1000 and 3000-4999)", () => {
|
|
25
|
+
expect(sanitizeCloseCode(1000)).toBe(1000);
|
|
26
|
+
expect(sanitizeCloseCode(3000)).toBe(3000);
|
|
27
|
+
expect(sanitizeCloseCode(4999)).toBe(4999);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it("maps illegal/reserved codes to 1011", () => {
|
|
31
|
+
for (const code of [0, 1001, 1005, 1006, 1011, 2999, 5000]) {
|
|
32
|
+
expect(sanitizeCloseCode(code)).toBe(1011);
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
});
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// Tests for the base64 encode/decode pair in utils (bytesToBase64 added
|
|
2
|
+
// v0.21.6 to inline R2 source images as data: URIs for image-to-video).
|
|
3
|
+
// The key property is a clean round-trip with base64ToBytes, including across
|
|
4
|
+
// the 0x8000-char chunk boundary (the reason btoa(String.fromCharCode(...))
|
|
5
|
+
// can't be used naively on large inputs).
|
|
6
|
+
|
|
7
|
+
import { describe, it, expect } from "vitest";
|
|
8
|
+
import { bytesToBase64, base64ToBytes, parseDataUrl } from "../src/utils";
|
|
9
|
+
|
|
10
|
+
describe("bytesToBase64 / base64ToBytes round-trip", () => {
|
|
11
|
+
it("round-trips a short byte sequence", () => {
|
|
12
|
+
const bytes = new Uint8Array([0, 1, 2, 254, 255, 13, 10, 65]);
|
|
13
|
+
expect(Array.from(base64ToBytes(bytesToBase64(bytes)))).toEqual(Array.from(bytes));
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it("round-trips an empty array", () => {
|
|
17
|
+
expect(bytesToBase64(new Uint8Array([]))).toBe("");
|
|
18
|
+
expect(base64ToBytes("").length).toBe(0);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it("round-trips a buffer larger than the 0x8000 chunk boundary", () => {
|
|
22
|
+
const n = 0x8000 * 2 + 123; // spans three chunks
|
|
23
|
+
const bytes = new Uint8Array(n);
|
|
24
|
+
for (let i = 0; i < n; i++) bytes[i] = (i * 31 + 7) & 0xff;
|
|
25
|
+
const round = base64ToBytes(bytesToBase64(bytes));
|
|
26
|
+
expect(round.length).toBe(n);
|
|
27
|
+
expect(round[0]).toBe(bytes[0]);
|
|
28
|
+
expect(round[0x8000]).toBe(bytes[0x8000]); // first byte of chunk 2
|
|
29
|
+
expect(round[n - 1]).toBe(bytes[n - 1]);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("produces a data URI that parseDataUrl can read back", () => {
|
|
33
|
+
const bytes = new Uint8Array([137, 80, 78, 71]); // PNG magic
|
|
34
|
+
const uri = `data:image/png;base64,${bytesToBase64(bytes)}`;
|
|
35
|
+
const parsed = parseDataUrl(uri);
|
|
36
|
+
expect(parsed?.mime).toBe("image/png");
|
|
37
|
+
expect(Array.from(base64ToBytes(parsed!.base64))).toEqual([137, 80, 78, 71]);
|
|
38
|
+
});
|
|
39
|
+
});
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
// Tests for interpretWorkersAISSEFrame (v0.18.1). Behavior preserved from
|
|
2
|
+
// the inline implementation in callWorkersAIStream pre-extraction.
|
|
3
|
+
|
|
4
|
+
import { describe, it, expect } from "vitest";
|
|
5
|
+
import { interpretWorkersAISSEFrame } from "../src/parsers/workers-ai-sse";
|
|
6
|
+
|
|
7
|
+
describe("interpretWorkersAISSEFrame", () => {
|
|
8
|
+
it("extracts a text event from a `response` field", () => {
|
|
9
|
+
expect(interpretWorkersAISSEFrame({ response: "hello" })).toEqual([
|
|
10
|
+
{ type: "text", text: "hello" },
|
|
11
|
+
]);
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it("ignores empty-string response (typical for the final usage-only frame)", () => {
|
|
15
|
+
expect(interpretWorkersAISSEFrame({ response: "" })).toEqual([]);
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it("ignores frames with no response field", () => {
|
|
19
|
+
expect(interpretWorkersAISSEFrame({ usage: { prompt_tokens: 1, completion_tokens: 2 } })).toEqual([
|
|
20
|
+
{ type: "usage", in_: 1, out_: 2 },
|
|
21
|
+
]);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it("extracts usage with OpenAI naming", () => {
|
|
25
|
+
const data = { usage: { prompt_tokens: 42, completion_tokens: 17 } };
|
|
26
|
+
expect(interpretWorkersAISSEFrame(data)).toEqual([
|
|
27
|
+
{ type: "usage", in_: 42, out_: 17 },
|
|
28
|
+
]);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it("falls back to Anthropic-style naming when OpenAI fields are missing", () => {
|
|
32
|
+
// Some Anthropic-derived adapters on Workers AI emit input_tokens/output_tokens.
|
|
33
|
+
const data = { usage: { input_tokens: 100, output_tokens: 50 } };
|
|
34
|
+
expect(interpretWorkersAISSEFrame(data)).toEqual([
|
|
35
|
+
{ type: "usage", in_: 100, out_: 50 },
|
|
36
|
+
]);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("prefers OpenAI naming when both are present", () => {
|
|
40
|
+
const data = {
|
|
41
|
+
usage: { prompt_tokens: 1, completion_tokens: 2, input_tokens: 99, output_tokens: 99 },
|
|
42
|
+
};
|
|
43
|
+
expect(interpretWorkersAISSEFrame(data)).toEqual([
|
|
44
|
+
{ type: "usage", in_: 1, out_: 2 },
|
|
45
|
+
]);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("passes through reasoning-model <think> blocks without modification", () => {
|
|
49
|
+
// gpt-oss-120b/20b, qwq-32b, deepseek-r1-distill-qwen-32b emit reasoning
|
|
50
|
+
// tags inline in `response`. The interpreter doesn't strip them; UI decides.
|
|
51
|
+
const data = { response: "<think>let me work this out</think>The answer is 42." };
|
|
52
|
+
expect(interpretWorkersAISSEFrame(data)).toEqual([
|
|
53
|
+
{ type: "text", text: "<think>let me work this out</think>The answer is 42." },
|
|
54
|
+
]);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("yields both text and usage when a single frame has both", () => {
|
|
58
|
+
const data = {
|
|
59
|
+
response: "done",
|
|
60
|
+
usage: { prompt_tokens: 5, completion_tokens: 3 },
|
|
61
|
+
};
|
|
62
|
+
expect(interpretWorkersAISSEFrame(data)).toEqual([
|
|
63
|
+
{ type: "text", text: "done" },
|
|
64
|
+
{ type: "usage", in_: 5, out_: 3 },
|
|
65
|
+
]);
|
|
66
|
+
});
|
|
67
|
+
});
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
// Tests for interpretXaiSSEFrame (v0.18.1). Behavior preserved from the
|
|
2
|
+
// inline implementation in callXaiStream pre-extraction.
|
|
3
|
+
|
|
4
|
+
import { describe, it, expect } from "vitest";
|
|
5
|
+
import { interpretXaiSSEFrame } from "../src/parsers/xai-sse";
|
|
6
|
+
|
|
7
|
+
describe("interpretXaiSSEFrame", () => {
|
|
8
|
+
it("extracts a text event from a delta frame", () => {
|
|
9
|
+
const data = {
|
|
10
|
+
id: "chatcmpl-1",
|
|
11
|
+
object: "chat.completion.chunk",
|
|
12
|
+
choices: [{ delta: { content: "hello" } }],
|
|
13
|
+
};
|
|
14
|
+
expect(interpretXaiSSEFrame(data)).toEqual([{ type: "text", text: "hello" }]);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it("extracts a usage event from a usage frame", () => {
|
|
18
|
+
const data = {
|
|
19
|
+
usage: { prompt_tokens: 42, completion_tokens: 17 },
|
|
20
|
+
};
|
|
21
|
+
expect(interpretXaiSSEFrame(data)).toEqual([
|
|
22
|
+
{ type: "usage", in_: 42, out_: 17 },
|
|
23
|
+
]);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("yields both events when a single frame has text delta and usage", () => {
|
|
27
|
+
// Rare in practice (usage typically arrives in a separate final frame)
|
|
28
|
+
// but the parser handles both fields independently.
|
|
29
|
+
const data = {
|
|
30
|
+
choices: [{ delta: { content: "fin." } }],
|
|
31
|
+
usage: { prompt_tokens: 10, completion_tokens: 5 },
|
|
32
|
+
};
|
|
33
|
+
expect(interpretXaiSSEFrame(data)).toEqual([
|
|
34
|
+
{ type: "text", text: "fin." },
|
|
35
|
+
{ type: "usage", in_: 10, out_: 5 },
|
|
36
|
+
]);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("ignores empty-string content (final pre-usage frame)", () => {
|
|
40
|
+
const data = { choices: [{ delta: { content: "" } }] };
|
|
41
|
+
expect(interpretXaiSSEFrame(data)).toEqual([]);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("ignores frames with no choices field", () => {
|
|
45
|
+
const data = { id: "chatcmpl-1", object: "chat.completion.chunk" };
|
|
46
|
+
expect(interpretXaiSSEFrame(data)).toEqual([]);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("ignores delta with no content field", () => {
|
|
50
|
+
const data = { choices: [{ delta: { role: "assistant" } }] };
|
|
51
|
+
expect(interpretXaiSSEFrame(data)).toEqual([]);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("returns null for missing token fields in usage", () => {
|
|
55
|
+
const data = { usage: { prompt_tokens: 42 } };
|
|
56
|
+
expect(interpretXaiSSEFrame(data)).toEqual([
|
|
57
|
+
{ type: "usage", in_: 42, out_: null },
|
|
58
|
+
]);
|
|
59
|
+
});
|
|
60
|
+
});
|