@juspay/neurolink 9.67.0 → 9.67.1

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.
@@ -35,6 +35,7 @@ export * from "./middleware.js";
35
35
  export * from "./model.js";
36
36
  export * from "./multimodal.js";
37
37
  export * from "./observability.js";
38
+ export * from "./openaiCompatible.js";
38
39
  export * from "./ppt.js";
39
40
  export * from "./processor.js";
40
41
  export * from "./providers.js";
@@ -36,6 +36,7 @@ export * from "./middleware.js";
36
36
  export * from "./model.js";
37
37
  export * from "./multimodal.js";
38
38
  export * from "./observability.js";
39
+ export * from "./openaiCompatible.js";
39
40
  export * from "./ppt.js";
40
41
  export * from "./processor.js";
41
42
  export * from "./providers.js";
@@ -3,7 +3,7 @@ import type { EvaluationData, GetPromptFunction } from "./evaluation.js";
3
3
  import type { AuthenticatedUser, RouteDefinition, ServerContext } from "./server.js";
4
4
  import type { LanguageModelMiddleware as BaseLanguageModelMiddleware } from "ai";
5
5
  export type { LanguageModelMiddleware } from "ai";
6
- export type { LanguageModelV3, LanguageModelV3CallOptions, LanguageModelV3Message, LanguageModelV3StreamPart, LanguageModelV3ToolCall, LanguageModelV3ToolChoice, LanguageModelV3Source, LanguageModelV3Middleware, JSONSchema7, } from "@ai-sdk/provider";
6
+ export type { LanguageModelV3, LanguageModelV3CallOptions, LanguageModelV3Message, LanguageModelV3Prompt, LanguageModelV3StreamPart, LanguageModelV3ToolCall, LanguageModelV3ToolChoice, LanguageModelV3Source, LanguageModelV3Middleware, JSONSchema7, } from "@ai-sdk/provider";
7
7
  import type { LanguageModelV3 } from "@ai-sdk/provider";
8
8
  export type LanguageModelV3GenerateResult = Awaited<ReturnType<LanguageModelV3["doGenerate"]>>;
9
9
  export type LanguageModelV3StreamResult = Awaited<ReturnType<LanguageModelV3["doStream"]>>;
@@ -0,0 +1,250 @@
1
+ import type { NeuroLinkEvents, TypedEventEmitter } from "./common.js";
2
+ import type { JSONSchema7 } from "./middleware.js";
3
+ import type { LanguageModelV3, LanguageModelV3CallOptions } from "./middleware.js";
4
+ import type { StreamOptions } from "./stream.js";
5
+ import type { Tool } from "./tools.js";
6
+ export type OpenAICompatChatRole = "system" | "user" | "assistant" | "tool";
7
+ export type OpenAICompatTextContent = {
8
+ type: "text";
9
+ text: string;
10
+ };
11
+ export type OpenAICompatImageURLContent = {
12
+ type: "image_url";
13
+ image_url: {
14
+ url: string;
15
+ detail?: "auto" | "low" | "high";
16
+ };
17
+ };
18
+ export type OpenAICompatMessageContent = OpenAICompatTextContent | OpenAICompatImageURLContent;
19
+ export type OpenAICompatToolCallWire = {
20
+ id: string;
21
+ type: "function";
22
+ function: {
23
+ name: string;
24
+ arguments: string;
25
+ };
26
+ };
27
+ export type OpenAICompatChatMessage = {
28
+ role: "system";
29
+ content: string | OpenAICompatMessageContent[];
30
+ } | {
31
+ role: "user";
32
+ content: string | OpenAICompatMessageContent[];
33
+ } | {
34
+ role: "assistant";
35
+ content?: string | OpenAICompatMessageContent[] | null;
36
+ tool_calls?: OpenAICompatToolCallWire[];
37
+ } | {
38
+ role: "tool";
39
+ content: string;
40
+ tool_call_id: string;
41
+ };
42
+ export type OpenAICompatChatTool = {
43
+ type: "function";
44
+ function: {
45
+ name: string;
46
+ description?: string;
47
+ parameters: JSONSchema7;
48
+ strict?: boolean;
49
+ };
50
+ };
51
+ export type OpenAICompatToolChoiceWire = "auto" | "none" | "required" | {
52
+ type: "function";
53
+ function: {
54
+ name: string;
55
+ };
56
+ };
57
+ export type OpenAICompatResponseFormat = {
58
+ type: "text";
59
+ } | {
60
+ type: "json_object";
61
+ } | {
62
+ type: "json_schema";
63
+ json_schema: {
64
+ name: string;
65
+ schema: JSONSchema7;
66
+ description?: string;
67
+ strict?: boolean;
68
+ };
69
+ };
70
+ export type OpenAICompatChatRequest = {
71
+ model: string;
72
+ messages: OpenAICompatChatMessage[];
73
+ stream?: boolean;
74
+ stream_options?: {
75
+ include_usage?: boolean;
76
+ };
77
+ max_tokens?: number;
78
+ max_completion_tokens?: number;
79
+ temperature?: number;
80
+ top_p?: number;
81
+ presence_penalty?: number;
82
+ frequency_penalty?: number;
83
+ seed?: number;
84
+ stop?: string[];
85
+ tools?: OpenAICompatChatTool[];
86
+ tool_choice?: OpenAICompatToolChoiceWire;
87
+ response_format?: OpenAICompatResponseFormat;
88
+ parallel_tool_calls?: boolean;
89
+ user?: string;
90
+ };
91
+ export type OpenAICompatUsage = {
92
+ prompt_tokens?: number;
93
+ completion_tokens?: number;
94
+ total_tokens?: number;
95
+ prompt_tokens_details?: {
96
+ cached_tokens?: number;
97
+ };
98
+ completion_tokens_details?: {
99
+ reasoning_tokens?: number;
100
+ };
101
+ };
102
+ export type OpenAICompatChatChoiceMessage = {
103
+ role: "assistant";
104
+ content?: string | null;
105
+ tool_calls?: OpenAICompatToolCallWire[];
106
+ refusal?: string | null;
107
+ };
108
+ export type OpenAICompatChatChoice = {
109
+ index: number;
110
+ message: OpenAICompatChatChoiceMessage;
111
+ finish_reason: "stop" | "length" | "tool_calls" | "function_call" | "content_filter" | null;
112
+ };
113
+ export type OpenAICompatChatResponse = {
114
+ id?: string;
115
+ object?: string;
116
+ created?: number;
117
+ model?: string;
118
+ choices: OpenAICompatChatChoice[];
119
+ usage?: OpenAICompatUsage;
120
+ system_fingerprint?: string;
121
+ };
122
+ export type OpenAICompatStreamDelta = {
123
+ role?: OpenAICompatChatRole;
124
+ content?: string | null;
125
+ tool_calls?: Array<{
126
+ index: number;
127
+ id?: string;
128
+ type?: "function";
129
+ function?: {
130
+ name?: string;
131
+ arguments?: string;
132
+ };
133
+ }>;
134
+ refusal?: string | null;
135
+ };
136
+ export type OpenAICompatStreamChunkChoice = {
137
+ index: number;
138
+ delta: OpenAICompatStreamDelta;
139
+ finish_reason: "stop" | "length" | "tool_calls" | "function_call" | "content_filter" | null;
140
+ };
141
+ export type OpenAICompatChatStreamChunk = {
142
+ id?: string;
143
+ object?: string;
144
+ created?: number;
145
+ model?: string;
146
+ choices: OpenAICompatStreamChunkChoice[];
147
+ usage?: OpenAICompatUsage;
148
+ };
149
+ export type OpenAICompatErrorBody = {
150
+ error?: {
151
+ message?: string;
152
+ type?: string;
153
+ code?: string | number;
154
+ param?: string | null;
155
+ };
156
+ };
157
+ export type OpenAICompatConfig = {
158
+ provider: string;
159
+ modelId: string;
160
+ baseURL: string;
161
+ apiKey?: string;
162
+ headers?: Record<string, string> | (() => Record<string, string>);
163
+ fetch?: typeof globalThis.fetch;
164
+ };
165
+ export type OpenAICompatToolWarning = NonNullable<Awaited<ReturnType<LanguageModelV3["doGenerate"]>>["warnings"]>[number];
166
+ export type OpenAICompatV3Content = Awaited<ReturnType<LanguageModelV3["doGenerate"]>>["content"][number];
167
+ export type OpenAICompatV3FinishReason = Awaited<ReturnType<LanguageModelV3["doGenerate"]>>["finishReason"];
168
+ export type OpenAICompatV3Usage = Awaited<ReturnType<LanguageModelV3["doGenerate"]>>["usage"];
169
+ export type OpenAICompatV3StreamPart = Awaited<ReturnType<LanguageModelV3["doStream"]>>["stream"] extends ReadableStream<infer P> ? P : never;
170
+ export type OpenAICompatV3CallTools = NonNullable<LanguageModelV3CallOptions["tools"]>;
171
+ export type OpenAICompatV3CallToolChoice = NonNullable<LanguageModelV3CallOptions["toolChoice"]>;
172
+ export type OpenAICompatUserOrAssistantPart = {
173
+ type: "text";
174
+ text: string;
175
+ } | {
176
+ type: "file";
177
+ mediaType: string;
178
+ data: Uint8Array | string | URL;
179
+ filename?: string;
180
+ };
181
+ export type OpenAICompatMessage = {
182
+ role: "system" | "user" | "assistant" | "tool";
183
+ content: unknown;
184
+ toolCallId?: string;
185
+ toolName?: string;
186
+ };
187
+ export type OpenAICompatSSEResult = {
188
+ text: string;
189
+ toolCalls: Map<number, {
190
+ id: string;
191
+ name: string;
192
+ argsBuffered: string;
193
+ }>;
194
+ finishReason: "stop" | "length" | "tool_calls" | "function_call" | "content_filter" | null;
195
+ usage?: OpenAICompatUsage;
196
+ };
197
+ export type OpenAICompatStreamChunk = {
198
+ content: string;
199
+ } | {
200
+ done: true;
201
+ };
202
+ export type ToolExecutionSummaryInternal = {
203
+ toolCallId: string;
204
+ toolName: string;
205
+ input: unknown;
206
+ output?: unknown;
207
+ error?: string;
208
+ startTime: Date;
209
+ endTime: Date;
210
+ };
211
+ export type StreamLoopArgs = {
212
+ maxSteps: number;
213
+ modelId: string;
214
+ url: string;
215
+ apiKey: string;
216
+ fetchImpl: typeof fetch;
217
+ abortSignal: AbortSignal | undefined;
218
+ options: StreamOptions;
219
+ conversation: OpenAICompatChatMessage[];
220
+ openAITools: OpenAICompatChatTool[] | undefined;
221
+ openAIToolChoice: OpenAICompatToolChoiceWire | undefined;
222
+ toolsRecord: Record<string, Tool>;
223
+ emitter: TypedEventEmitter<NeuroLinkEvents> | undefined;
224
+ toolsUsed: string[];
225
+ toolExecutionSummaries: ToolExecutionSummaryInternal[];
226
+ pushChunk: (chunk: OpenAICompatStreamChunk) => void;
227
+ resolveUsage: (u: {
228
+ promptTokens: number;
229
+ completionTokens: number;
230
+ totalTokens: number;
231
+ }) => void;
232
+ resolveFinish: (reason: string) => void;
233
+ };
234
+ export type OpenAICompatBuildBodyArgs = {
235
+ modelId: string;
236
+ messages: OpenAICompatChatMessage[];
237
+ options: {
238
+ maxTokens?: number | null;
239
+ temperature?: number | null;
240
+ topP?: number | null;
241
+ presencePenalty?: number | null;
242
+ frequencyPenalty?: number | null;
243
+ seed?: number | null;
244
+ stopSequences?: string[];
245
+ };
246
+ tools?: OpenAICompatChatTool[];
247
+ toolChoice?: OpenAICompatToolChoiceWire;
248
+ streaming: boolean;
249
+ responseFormat?: OpenAICompatResponseFormat;
250
+ };
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=openaiCompatible.js.map
@@ -1,16 +1,17 @@
1
1
  import type { AIProviderName } from "../constants/enums.js";
2
2
  import { BaseProvider } from "../core/baseProvider.js";
3
- import type { StreamOptions, StreamResult, ZodUnknownSchema } from "../types/index.js";
4
- import type { LanguageModel, Schema } from "../types/index.js";
3
+ import type { LanguageModel, Schema, StreamOptions, StreamResult, ZodUnknownSchema } from "../types/index.js";
5
4
  /**
6
- * OpenAI Compatible Provider - BaseProvider Implementation
7
- * Provides access to one of the OpenAI-compatible endpoint (OpenRouter, vLLM, LiteLLM, etc.)
5
+ * OpenAI Compatible Provider direct HTTP, no AI SDK.
6
+ *
7
+ * Talks to any OpenAI chat-completions-shaped endpoint (LiteLLM, vLLM,
8
+ * OpenRouter, etc.). The entire request/stream/tool-loop is inline above;
9
+ * no `streamText`, no `LanguageModelV3`, no `@ai-sdk/openai`.
8
10
  */
9
11
  export declare class OpenAICompatibleProvider extends BaseProvider {
10
- private model?;
11
12
  private config;
13
+ private resolvedModel?;
12
14
  private discoveredModel?;
13
- private customOpenAI;
14
15
  constructor(modelName?: string, sdk?: unknown, _region?: string, credentials?: {
15
16
  apiKey?: string;
16
17
  baseURL?: string;
@@ -18,33 +19,59 @@ export declare class OpenAICompatibleProvider extends BaseProvider {
18
19
  protected getProviderName(): AIProviderName;
19
20
  protected getDefaultModel(): string;
20
21
  /**
21
- * Returns the Vercel AI SDK model instance for OpenAI Compatible endpoints
22
- * Handles auto-discovery if no model was specified
22
+ * Abstract from BaseProvider used by the parent's generate() path which
23
+ * still goes through `generateText`. Returns a thin LanguageModelV3-shaped
24
+ * object that delegates to the same HTTP helpers used by executeStream.
25
+ * Stays inside this file so no AI-SDK-named import is needed here.
23
26
  */
24
27
  protected getAISDKModel(): Promise<LanguageModel>;
25
- protected formatProviderError(error: unknown): Error;
28
+ private resolveModelName;
26
29
  /**
27
- * OpenAI Compatible endpoints support tools for compatible models
30
+ * Returns a minimal V3-shaped model. Only used by BaseProvider's
31
+ * `generate()` non-streaming path which still relies on the parent's
32
+ * `generateText`. The streaming path bypasses this entirely.
28
33
  */
34
+ private buildDelegatingModel;
35
+ protected formatProviderError(error: unknown): Error;
29
36
  supportsTools(): boolean;
30
37
  /**
31
- * Provider-specific streaming implementation
32
- * Note: This is only used when tools are disabled
38
+ * Streaming path — drives the OpenAI endpoint directly. No streamText,
39
+ * no AI SDK orchestrator. Tool calls, multi-step loops, telemetry,
40
+ * abort handling all inline.
33
41
  */
34
42
  protected executeStream(options: StreamOptions, _analysisSchema?: ZodUnknownSchema | Schema<unknown>): Promise<StreamResult>;
35
43
  /**
36
- * Get available models from OpenAI Compatible endpoint
44
+ * Multi-step streaming orchestrator. One iteration per model turn:
37
45
  *
38
- * Fetches from the /v1/models endpoint to discover available models.
39
- * This is useful for auto-discovery when no model is specified.
46
+ * 1. POST /chat/completions with stream:true
47
+ * 2. Parse SSE; push text deltas to the consumer queue
48
+ * 3. If the step emitted tool_calls → execute each, append to
49
+ * conversation, loop again
50
+ * 4. Otherwise resolve the deferred analytics promises and exit
51
+ *
52
+ * Bounded by `args.maxSteps`. Any thrown error rejects loopPromise and
53
+ * is surfaced to the consumer via `await loopPromise` in the stream
54
+ * generator.
40
55
  */
41
- getAvailableModels(): Promise<string[]>;
56
+ private runStreamLoop;
42
57
  /**
43
- * Get the first available model for auto-selection
58
+ * One streaming round-trip: POST chat-completions, parse SSE, push text
59
+ * deltas to the consumer queue. Returns the accumulated SSE result so
60
+ * the caller can decide whether to run tools and re-stream.
44
61
  */
45
- getFirstAvailableModel(): Promise<string>;
62
+ private streamOneStep;
46
63
  /**
47
- * Fallback models when discovery fails
64
+ * Execute every tool_call collected from one streaming step:
65
+ *
66
+ * - append an `assistant` turn carrying the tool_calls
67
+ * - resolve each tool from the local registry and run it
68
+ * - emit tool:start/tool:end events
69
+ * - push per-execution summaries
70
+ * - append a `tool` turn per result so the next step can see them
71
+ * - mirror BaseProvider's tool-events + storage hooks
48
72
  */
73
+ private executeToolBatch;
74
+ getAvailableModels(): Promise<string[]>;
75
+ getFirstAvailableModel(): Promise<string>;
49
76
  private getFallbackModels;
50
77
  }