@juspay/neurolink 9.65.2 → 9.66.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.
Files changed (32) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/dist/browser/neurolink.min.js +362 -354
  3. package/dist/cli/commands/proxy.js +154 -5
  4. package/dist/lib/proxy/modelRouter.d.ts +5 -1
  5. package/dist/lib/proxy/modelRouter.js +8 -0
  6. package/dist/lib/proxy/openaiFormat.d.ts +137 -0
  7. package/dist/lib/proxy/openaiFormat.js +801 -0
  8. package/dist/lib/proxy/proxyTranslationEngine.d.ts +124 -0
  9. package/dist/lib/proxy/proxyTranslationEngine.js +679 -0
  10. package/dist/lib/server/routes/claudeProxyRoutes.d.ts +6 -5
  11. package/dist/lib/server/routes/claudeProxyRoutes.js +22 -355
  12. package/dist/lib/server/routes/index.d.ts +1 -0
  13. package/dist/lib/server/routes/index.js +10 -2
  14. package/dist/lib/server/routes/openaiProxyRoutes.d.ts +30 -0
  15. package/dist/lib/server/routes/openaiProxyRoutes.js +337 -0
  16. package/dist/lib/types/proxy.d.ts +179 -0
  17. package/dist/lib/types/server.d.ts +3 -0
  18. package/dist/proxy/modelRouter.d.ts +5 -1
  19. package/dist/proxy/modelRouter.js +8 -0
  20. package/dist/proxy/openaiFormat.d.ts +137 -0
  21. package/dist/proxy/openaiFormat.js +800 -0
  22. package/dist/proxy/proxyTranslationEngine.d.ts +124 -0
  23. package/dist/proxy/proxyTranslationEngine.js +678 -0
  24. package/dist/server/routes/claudeProxyRoutes.d.ts +6 -5
  25. package/dist/server/routes/claudeProxyRoutes.js +22 -355
  26. package/dist/server/routes/index.d.ts +1 -0
  27. package/dist/server/routes/index.js +10 -2
  28. package/dist/server/routes/openaiProxyRoutes.d.ts +30 -0
  29. package/dist/server/routes/openaiProxyRoutes.js +336 -0
  30. package/dist/types/proxy.d.ts +179 -0
  31. package/dist/types/server.d.ts +3 -0
  32. package/package.json +1 -1
@@ -0,0 +1,137 @@
1
+ /**
2
+ * OpenAI Chat Completions API format conversion layer.
3
+ *
4
+ * Provides a request parser (OpenAI -> NeuroLink), a response serializer
5
+ * (NeuroLink -> OpenAI), a streaming SSE state machine, and an error
6
+ * envelope helper. Together they allow NeuroLink to act as a
7
+ * drop-in OpenAI API proxy.
8
+ *
9
+ * Reference: https://platform.openai.com/docs/api-reference/chat/create
10
+ */
11
+ import type { ClaudeRequest, ClaudeResponse, InternalResult, OpenAICompletionRequest, OpenAICompletionResponse, OpenAIErrorResponse, ParsedOpenAIRequest, StreamLifecycleState } from "../types/index.js";
12
+ /** Generate a unique chat completion ID in the OpenAI format. */
13
+ export declare function generateChatCompletionId(): string;
14
+ /** Generate an OpenAI-format tool call ID (`call_` + random chars). */
15
+ export declare function generateOpenAIToolCallId(): string;
16
+ /**
17
+ * Parse an incoming OpenAI Chat Completions request into an intermediate
18
+ * representation consumable by NeuroLink's generate/stream pipeline.
19
+ *
20
+ * Handles:
21
+ * - System prompt extraction from system messages
22
+ * - Message flattening (text + image parts)
23
+ * - Tool definition conversion
24
+ * - tool_choice mapping
25
+ * - top_p, temperature, max_tokens pass-through
26
+ */
27
+ export declare function parseOpenAIRequest(body: OpenAICompletionRequest): ParsedOpenAIRequest;
28
+ /**
29
+ * Serialize a NeuroLink GenerateResult into an OpenAI Chat Completions response.
30
+ */
31
+ export declare function serializeOpenAIResponse(result: InternalResult, requestModel: string): OpenAICompletionResponse;
32
+ /**
33
+ * Build an OpenAI-compatible error envelope.
34
+ */
35
+ export declare function buildOpenAIError(status: number, message: string): OpenAIErrorResponse;
36
+ /**
37
+ * Format a single OpenAI SSE frame.
38
+ * OpenAI uses only `data:` lines (no `event:` prefix unlike Claude).
39
+ */
40
+ export declare function formatOpenAISSE(data: unknown): string;
41
+ /**
42
+ * Stateful SSE serializer that emits a well-formed OpenAI streaming response.
43
+ *
44
+ * Tracks lifecycle state (`idle` -> `streaming` -> `done`) and the current
45
+ * tool call index for multi-tool streaming.
46
+ *
47
+ * Usage:
48
+ * ```ts
49
+ * const sse = new OpenAIStreamSerializer(requestModel);
50
+ *
51
+ * // Start the stream
52
+ * yield* sse.start();
53
+ *
54
+ * // Text deltas
55
+ * for await (const chunk of textStream) {
56
+ * yield* sse.pushDelta(chunk);
57
+ * }
58
+ *
59
+ * // Tool use
60
+ * yield* sse.pushToolUse(toolId, toolName, toolInput);
61
+ *
62
+ * // Finalize
63
+ * yield* sse.finish("stop", usage);
64
+ * ```
65
+ */
66
+ export declare class OpenAIStreamSerializer {
67
+ private state;
68
+ private readonly id;
69
+ private readonly model;
70
+ private started;
71
+ private toolCallIndex;
72
+ constructor(model: string);
73
+ /** Current lifecycle state (exposed for testing). */
74
+ getState(): StreamLifecycleState;
75
+ private makeChunk;
76
+ /**
77
+ * Emit the opening frame with `role: "assistant"`.
78
+ */
79
+ start(): Generator<string>;
80
+ /**
81
+ * Push a text content delta.
82
+ */
83
+ pushDelta(text: string): Generator<string>;
84
+ /**
85
+ * Push the start of a tool call (id, name, empty arguments).
86
+ */
87
+ pushToolCallStart(id: string, name: string): Generator<string>;
88
+ /**
89
+ * Push an arguments delta for the current tool call.
90
+ */
91
+ pushToolCallArgDelta(index: number, argsChunk: string): Generator<string>;
92
+ /**
93
+ * Push a complete tool use: emits tool call start followed by chunked
94
+ * argument deltas (~100 chars per chunk).
95
+ */
96
+ pushToolUse(id: string, name: string, input: unknown): Generator<string>;
97
+ /**
98
+ * Finalize the stream: emit finish_reason chunk, then `data: [DONE]`.
99
+ */
100
+ finish(finishReason?: string, usage?: {
101
+ input: number;
102
+ output: number;
103
+ total: number;
104
+ }): Generator<string>;
105
+ /**
106
+ * Emit an error event. Transitions to terminal ERROR state.
107
+ */
108
+ emitError(message: string): Generator<string>;
109
+ }
110
+ /**
111
+ * Convert an OpenAI Chat Completions request to a Claude Messages API request.
112
+ *
113
+ * Used by the OpenAI proxy endpoint to internally loopback requests targeting
114
+ * Claude models through the proxy's native /v1/messages passthrough path,
115
+ * so they benefit from OAuth account rotation, retry, SSE interception, etc.
116
+ */
117
+ export declare function convertOpenAIToClaudeRequest(openai: OpenAICompletionRequest): ClaudeRequest;
118
+ /**
119
+ * Convert a non-streaming Claude Messages response to an OpenAI Chat
120
+ * Completions response by bridging through {@link InternalResult}.
121
+ */
122
+ export declare function convertClaudeToOpenAIResponse(claude: ClaudeResponse, requestModel: string): OpenAICompletionResponse;
123
+ /**
124
+ * Create a TransformStream that parses Claude Messages API SSE events from
125
+ * the upstream response and re-emits them as OpenAI Chat Completions SSE
126
+ * frames.
127
+ *
128
+ * Handles the canonical Claude SSE event types:
129
+ * - message_start -> emits the opening `role: "assistant"` chunk
130
+ * - content_block_start -> text block: no-op; tool_use block: emit tool call start
131
+ * - content_block_delta -> text_delta: emit content delta;
132
+ * input_json_delta: emit tool call argument delta
133
+ * - content_block_stop -> no-op
134
+ * - message_delta -> captures stop_reason and output token usage
135
+ * - message_stop -> emits the final `finish_reason` chunk + `[DONE]`
136
+ */
137
+ export declare function createClaudeToOpenAIStreamTransform(requestModel: string): TransformStream<Uint8Array, Uint8Array>;