Skip to content

Use Claude Code with your ChatGPT, Kimi, Cursor or Grok subscription via a local Anthropic-compatible proxy

Rust

share cursor sse framing

Raine Virtaaac24d083305
diff --git a/src/providers/cursor/sse-framing.ts b/src/providers/cursor/sse-framing.ts
new file mode 100644
index 0000000..309ffd3
--- /dev/null
+++ b/src/providers/cursor/sse-framing.ts
@@ -0,0 +1,150 @@
+import type { CursorUsage } from "./client.ts";
+
+type CursorSseStopReason = "end_turn" | "tool_use";
+
+interface CursorSseFramingOptions {
+ messageId: string;
+ model: string;
+ emit: (event: string, data: unknown) => boolean;
+ mapUsage: (usage?: CursorUsage) => unknown;
+}
+
+export interface CursorSseFramer {
+ emitThinkingDelta(text: string): void;
+ emitTextDelta(text: string): void;
+ emitFinalMessage(stopReason: CursorSseStopReason, usage?: CursorUsage): void;
+ emitError(error: unknown): void;
+ emitToolPauseMessage(usage: CursorUsage | undefined, emitToolUse: (index: number) => void): void;
+ closeOpenBlocks(): void;
+ ensureStart(): void;
+ nextContentBlockIndex(): number;
+}
+
+export function createCursorSseFramer(opts: CursorSseFramingOptions): CursorSseFramer {
+ let started = false;
+ let thinkingOpen = false;
+ let textOpen = false;
+ let nextIndex = 0;
+ let thinkingIndex = -1;
+ let textIndex = -1;
+
+ const emit = (event: string, data: unknown) => {
+ opts.emit(event, data);
+ };
+
+ const nextContentBlockIndex = () => nextIndex++;
+
+ const ensureStart = () => {
+ if (started) return;
+ started = true;
+ emit("message_start", {
+ type: "message_start",
+ message: {
+ id: opts.messageId,
+ type: "message",
+ role: "assistant",
+ model: opts.model,
+ content: [],
+ stop_reason: null,
+ stop_sequence: null,
+ usage: {
+ input_tokens: 0,
+ output_tokens: 0,
+ cache_creation_input_tokens: 0,
+ cache_read_input_tokens: 0,
+ },
+ },
+ });
+ emit("ping", { type: "ping" });
+ };
+
+ const openThinking = () => {
+ if (thinkingOpen) return;
+ ensureStart();
+ thinkingOpen = true;
+ thinkingIndex = nextIndex++;
+ emit("content_block_start", {
+ type: "content_block_start",
+ index: thinkingIndex,
+ content_block: { type: "thinking", thinking: "", signature: "" },
+ });
+ };
+
+ const openText = () => {
+ if (textOpen) return;
+ ensureStart();
+ textOpen = true;
+ textIndex = nextIndex++;
+ emit("content_block_start", {
+ type: "content_block_start",
+ index: textIndex,
+ content_block: { type: "text", text: "" },
+ });
+ };
+
+ const closeOpenBlocks = () => {
+ if (thinkingOpen) {
+ emit("content_block_stop", { type: "content_block_stop", index: thinkingIndex });
+ thinkingOpen = false;
+ }
+ if (textOpen) {
+ emit("content_block_stop", { type: "content_block_stop", index: textIndex });
+ textOpen = false;
+ }
+ };
+
+ const emitFinalMessage = (stopReason: CursorSseStopReason, usage?: CursorUsage) => {
+ ensureStart();
+ closeOpenBlocks();
+ emit("message_delta", {
+ type: "message_delta",
+ delta: { stop_reason: stopReason, stop_sequence: null },
+ usage: opts.mapUsage(usage),
+ });
+ emit("message_stop", { type: "message_stop" });
+ };
+
+ return {
+ emitThinkingDelta(text) {
+ openThinking();
+ emit("content_block_delta", {
+ type: "content_block_delta",
+ index: thinkingIndex,
+ delta: { type: "thinking_delta", thinking: text },
+ });
+ },
+ emitTextDelta(text) {
+ if (thinkingOpen) {
+ emit("content_block_stop", { type: "content_block_stop", index: thinkingIndex });
+ thinkingOpen = false;
+ }
+ openText();
+ emit("content_block_delta", {
+ type: "content_block_delta",
+ index: textIndex,
+ delta: { type: "text_delta", text },
+ });
+ },
+ emitFinalMessage,
+ emitError(error) {
+ ensureStart();
+ closeOpenBlocks();
+ emit("error", {
+ type: "error",
+ error: { type: "api_error", message: String(error) },
+ });
+ },
+ emitToolPauseMessage(usage, emitToolUse) {
+ closeOpenBlocks();
+ ensureStart();
+ const index = nextContentBlockIndex();
+ emitToolUse(index);
+ emitFinalMessage("tool_use", usage);
+ },
+ closeOpenBlocks,
+ ensureStart,
+ nextContentBlockIndex() {
+ return nextContentBlockIndex();
+ },
+ };
+}
diff --git a/src/providers/cursor/tool-bridge.ts b/src/providers/cursor/tool-bridge.ts
index 26f7a78..f54da33 100644
--- a/src/providers/cursor/tool-bridge.ts
+++ b/src/providers/cursor/tool-bridge.ts
@@ -19,6 +19,7 @@ import {
} from "./client.ts";
import type { CursorProto } from "./proto-loader.ts";
import { cursorUsageToAnthropic } from "./translate/response.ts";
+import { createCursorSseFramer } from "./sse-framing.ts";
interface PendingToolBase {
toolUseId: string;
@@ -333,12 +334,6 @@ function streamBridgeUntilToolOrEnd(
return new ReadableStream<Uint8Array>({
async start(controller) {
let closed = false;
- let started = false;
- let thinkingOpen = false;
- let textOpen = false;
- let nextIndex = 0;
- let thinkingIndex = -1;
- let textIndex = -1;
let finalUsage: CursorUsage | undefined;
const emit = (event: string, data: unknown) => {
@@ -348,92 +343,41 @@ function streamBridgeUntilToolOrEnd(
return true;
};
- const ensureStart = () => {
- if (started) return;
- started = true;
- emit("message_start", {
- type: "message_start",
- message: {
- id: state.messageId,
- type: "message",
- role: "assistant",
- model: state.model,
- content: [],
- stop_reason: null,
- stop_sequence: null,
- usage: {
- input_tokens: 0,
- output_tokens: 0,
- cache_creation_input_tokens: 0,
- cache_read_input_tokens: 0,
- },
- },
- });
- emit("ping", { type: "ping" });
- };
+ const framing = createCursorSseFramer({
+ messageId: state.messageId,
+ model: state.model,
+ emit,
+ mapUsage: cursorUsageToAnthropic,
+ });
- const openThinking = () => {
- if (thinkingOpen) return;
- ensureStart();
- thinkingOpen = true;
- thinkingIndex = nextIndex++;
- emit("content_block_start", {
- type: "content_block_start",
- index: thinkingIndex,
- content_block: { type: "thinking", thinking: "", signature: "" },
+ const emitToolUseAndPause = (tool: PendingNativeTool) => {
+ framing.emitToolPauseMessage(finalUsage, (index) => {
+ const input = toolUseInput(tool);
+ emit("content_block_start", {
+ type: "content_block_start",
+ index,
+ content_block: {
+ type: "tool_use",
+ id: tool.toolUseId,
+ name: tool.kind,
+ input: {},
+ },
+ });
+ emit("content_block_delta", {
+ type: "content_block_delta",
+ index,
+ delta: { type: "input_json_delta", partial_json: input },
+ });
+ emit("content_block_stop", { type: "content_block_stop", index });
});
};
- const openText = () => {
- if (textOpen) return;
- ensureStart();
- textOpen = true;
- textIndex = nextIndex++;
- emit("content_block_start", {
- type: "content_block_start",
- index: textIndex,
- content_block: { type: "text", text: "" },
- });
+ const emitMessageEnd = () => {
+ framing.emitFinalMessage("end_turn", finalUsage);
};
- const closeOpenBlocks = () => {
- if (thinkingOpen) {
- emit("content_block_stop", { type: "content_block_stop", index: thinkingIndex });
- thinkingOpen = false;
- }
- if (textOpen) {
- emit("content_block_stop", { type: "content_block_stop", index: textIndex });
- textOpen = false;
- }
- };
-
- const emitToolUseAndPause = (tool: PendingNativeTool) => {
- closeOpenBlocks();
- ensureStart();
- const index = nextIndex++;
- const input = toolUseInput(tool);
- emit("content_block_start", {
- type: "content_block_start",
- index,
- content_block: {
- type: "tool_use",
- id: tool.toolUseId,
- name: tool.kind,
- input: {},
- },
- });
- emit("content_block_delta", {
- type: "content_block_delta",
- index,
- delta: { type: "input_json_delta", partial_json: input },
- });
- emit("content_block_stop", { type: "content_block_stop", index });
- emit("message_delta", {
- type: "message_delta",
- delta: { stop_reason: "tool_use", stop_sequence: null },
- usage: cursorUsageToAnthropic(finalUsage),
- });
- emit("message_stop", { type: "message_stop" });
+ const emitStreamError = (err: unknown) => {
+ framing.emitError(err);
};
try {
@@ -459,24 +403,10 @@ function streamBridgeUntilToolOrEnd(
state.onSession?.(event.sessionId);
break;
case "thinking_delta":
- openThinking();
- emit("content_block_delta", {
- type: "content_block_delta",
- index: thinkingIndex,
- delta: { type: "thinking_delta", thinking: event.text },
- });
+ framing.emitThinkingDelta(event.text);
break;
case "text_delta":
- if (thinkingOpen) {
- emit("content_block_stop", { type: "content_block_stop", index: thinkingIndex });
- thinkingOpen = false;
- }
- openText();
- emit("content_block_delta", {
- type: "content_block_delta",
- index: textIndex,
- delta: { type: "text_delta", text: event.text },
- });
+ framing.emitTextDelta(event.text);
break;
case "usage":
finalUsage = event.usage;
@@ -486,23 +416,11 @@ function streamBridgeUntilToolOrEnd(
}
}
- ensureStart();
- closeOpenBlocks();
- emit("message_delta", {
- type: "message_delta",
- delta: { stop_reason: "end_turn", stop_sequence: null },
- usage: cursorUsageToAnthropic(finalUsage),
- });
- emit("message_stop", { type: "message_stop" });
+ emitMessageEnd();
bridgeStates.delete(state.sessionId);
} catch (err) {
state.log.warn("cursor bridge stream error", { err: String(err) });
- ensureStart();
- closeOpenBlocks();
- emit("error", {
- type: "error",
- error: { type: "api_error", message: String(err) },
- });
+ emitStreamError(err);
bridgeStates.delete(state.sessionId);
} finally {
closed = true;
diff --git a/src/providers/cursor/translate/response.ts b/src/providers/cursor/translate/response.ts
index a2c205d..f9dedf8 100644
--- a/src/providers/cursor/translate/response.ts
+++ b/src/providers/cursor/translate/response.ts
@@ -3,6 +3,7 @@ import type { Logger } from "../../../log.ts";
import type { TrafficCapture } from "../../types.ts";
import { decodeCursorStream, type CursorStreamEvent, type CursorUsage } from "../client.ts";
import type { CursorProto } from "../proto-loader.ts";
+import { createCursorSseFramer } from "../sse-framing.ts";
export interface AnthropicCursorResponse {
id: string;
@@ -113,12 +114,6 @@ export function translateCursorStream(
return new ReadableStream<Uint8Array>({
async start(controller) {
let closed = false;
- let started = false;
- let thinkingOpen = false;
- let textOpen = false;
- let nextIndex = 0;
- let thinkingIndex = -1;
- let textIndex = -1;
const emit = (event: string, data: unknown) => {
if (closed || opts.signal?.aborted || controller.desiredSize === null) return false;
@@ -127,64 +122,12 @@ export function translateCursorStream(
return true;
};
- const ensureStart = () => {
- if (started) return;
- started = true;
- emit("message_start", {
- type: "message_start",
- message: {
- id: opts.messageId,
- type: "message",
- role: "assistant",
- model: opts.model,
- content: [],
- stop_reason: null,
- stop_sequence: null,
- usage: {
- input_tokens: 0,
- output_tokens: 0,
- cache_creation_input_tokens: 0,
- cache_read_input_tokens: 0,
- },
- },
- });
- emit("ping", { type: "ping" });
- };
-
- const openThinking = () => {
- if (thinkingOpen) return;
- ensureStart();
- thinkingOpen = true;
- thinkingIndex = nextIndex++;
- emit("content_block_start", {
- type: "content_block_start",
- index: thinkingIndex,
- content_block: { type: "thinking", thinking: "", signature: "" },
- });
- };
-
- const openText = () => {
- if (textOpen) return;
- ensureStart();
- textOpen = true;
- textIndex = nextIndex++;
- emit("content_block_start", {
- type: "content_block_start",
- index: textIndex,
- content_block: { type: "text", text: "" },
- });
- };
-
- const closeOpenBlocks = () => {
- if (thinkingOpen) {
- emit("content_block_stop", { type: "content_block_stop", index: thinkingIndex });
- thinkingOpen = false;
- }
- if (textOpen) {
- emit("content_block_stop", { type: "content_block_stop", index: textIndex });
- textOpen = false;
- }
- };
+ const framing = createCursorSseFramer({
+ messageId: opts.messageId,
+ model: opts.model,
+ emit,
+ mapUsage: cursorUsageToAnthropic,
+ });
try {
let finalUsage: CursorUsage | undefined;
@@ -196,24 +139,10 @@ export function translateCursorStream(
opts.onSession?.(event.sessionId);
break;
case "thinking_delta":
- openThinking();
- emit("content_block_delta", {
- type: "content_block_delta",
- index: thinkingIndex,
- delta: { type: "thinking_delta", thinking: event.text },
- });
+ framing.emitThinkingDelta(event.text);
break;
case "text_delta":
- if (thinkingOpen) {
- emit("content_block_stop", { type: "content_block_stop", index: thinkingIndex });
- thinkingOpen = false;
- }
- openText();
- emit("content_block_delta", {
- type: "content_block_delta",
- index: textIndex,
- delta: { type: "text_delta", text: event.text },
- });
+ framing.emitTextDelta(event.text);
break;
case "usage":
finalUsage = event.usage;
@@ -222,22 +151,10 @@ export function translateCursorStream(
break;
}
}
- ensureStart();
- closeOpenBlocks();
- emit("message_delta", {
- type: "message_delta",
- delta: { stop_reason: "end_turn", stop_sequence: null },
- usage: cursorUsageToAnthropic(finalUsage),
- });
- emit("message_stop", { type: "message_stop" });
+ framing.emitFinalMessage("end_turn", finalUsage);
} catch (err) {
opts.log.warn("cursor stream error", { err: String(err) });
- ensureStart();
- closeOpenBlocks();
- emit("error", {
- type: "error",
- error: { type: "api_error", message: String(err) },
- });
+ framing.emitError(err);
} finally {
closed = true;
try {