Skip to content

Commit 1cc5210

Browse files
Normalize Cloudflare JSON stream responses
1 parent c5cdf35 commit 1cc5210

6 files changed

Lines changed: 48 additions & 5 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33
All notable changes to `@stackbilt/llm-providers` are documented here.
44
Format follows [Keep a Changelog](https://keepachangelog.com/). Versions use [Semantic Versioning](https://semver.org/).
55

6+
## [1.14.4] — 2026-06-10
7+
8+
Patch compatibility fix for Cloudflare local gateway streaming.
9+
10+
### Fixed
11+
- **Cloudflare JSON-to-stream normalization** — `CloudflareProvider.streamResponse()` now normalizes non-streaming chat-completion JSON responses through the same parser used by `generateResponse()`. This supports local gateway shims that request JSON from Cloudflare and synthesize Claude/Codex client streams.
12+
613
## [1.14.3] — 2026-06-10
714

815
Patch compatibility fix for Cloudflare reasoning model responses.

‎package-lock.json‎

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@stackbilt/llm-providers",
3-
"version": "1.14.3",
3+
"version": "1.14.4",
44
"description": "Multi-LLM failover with circuit breakers, cost tracking, and intelligent retry. Cloudflare Workers native.",
55
"author": "Stackbilt <[email protected]>",
66
"license": "Apache-2.0",

‎src/__tests__/cloudflare.test.ts‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,42 @@ describe('CloudflareProvider', () => {
481481
expect(usage?.outputTokens).toBeGreaterThan(0);
482482
expect(usage?.totalTokens).toBe((usage?.inputTokens ?? 0) + (usage?.outputTokens ?? 0));
483483
});
484+
485+
it('normalizes chat-completion text when local REST shim returns JSON for a stream request', async () => {
486+
mockAiRun.mockResolvedValueOnce({
487+
id: 'chatcmpl-cf-kimi',
488+
model: '@cf/moonshotai/kimi-k2.6',
489+
choices: [
490+
{
491+
index: 0,
492+
message: {
493+
role: 'assistant',
494+
content: 'cf-stream-ok'
495+
},
496+
finish_reason: 'stop'
497+
}
498+
],
499+
usage: {
500+
prompt_tokens: 18,
501+
completion_tokens: 4,
502+
total_tokens: 22
503+
}
504+
});
505+
506+
const stream = await provider.streamResponse({
507+
...testRequest,
508+
model: '@cf/moonshotai/kimi-k2.6',
509+
stream: true
510+
});
511+
const usagePromise = getStreamUsage(stream);
512+
513+
expect(await readStream(stream)).toBe('cf-stream-ok');
514+
await expect(usagePromise).resolves.toMatchObject({
515+
inputTokens: 18,
516+
outputTokens: 4,
517+
totalTokens: 22
518+
});
519+
});
484520
});
485521

486522
describe('vision', () => {

‎src/providers/cloudflare.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -989,7 +989,7 @@ export class CloudflareProvider extends BaseProvider {
989989
}
990990
} else {
991991
// Non-streaming response, send all at once
992-
const content = typeof stream === 'string' ? stream : (stream as WorkersAIResult)?.response || '';
992+
const content = typeof stream === 'string' ? stream : this.extractText(stream as WorkersAIResult);
993993
if (typeof stream === 'object' && stream !== null && !Array.isArray(stream)) {
994994
const result = stream as WorkersAIResult;
995995
if (result.usage) {

‎src/version.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
// Generated by scripts/sync-version.mjs; do not edit manually.
2-
export const VERSION = '1.14.3';
2+
export const VERSION = '1.14.4';

0 commit comments

Comments
 (0)