call-ai 0.7.0-dev-preview-6 → 0.7.0-dev-preview-7
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/dist/api.d.ts +2 -1
- package/dist/api.js +58 -2
- package/dist/types.d.ts +6 -0
- package/package.json +1 -1
package/dist/api.d.ts
CHANGED
|
@@ -7,6 +7,7 @@ import { CallAIOptions, Message, StreamResponse } from "./types";
|
|
|
7
7
|
* @param prompt User prompt as string or an array of message objects
|
|
8
8
|
* @param options Configuration options including optional schema for structured output
|
|
9
9
|
* @returns A Promise that resolves to the complete response string when streaming is disabled,
|
|
10
|
-
* or
|
|
10
|
+
* or a Promise that resolves to an AsyncGenerator when streaming is enabled.
|
|
11
|
+
* The AsyncGenerator yields partial responses as they arrive.
|
|
11
12
|
*/
|
|
12
13
|
export declare function callAI(prompt: string | Message[], options?: CallAIOptions): Promise<string | StreamResponse>;
|
package/dist/api.js
CHANGED
|
@@ -12,7 +12,8 @@ const FALLBACK_MODEL = "openrouter/auto";
|
|
|
12
12
|
* @param prompt User prompt as string or an array of message objects
|
|
13
13
|
* @param options Configuration options including optional schema for structured output
|
|
14
14
|
* @returns A Promise that resolves to the complete response string when streaming is disabled,
|
|
15
|
-
* or
|
|
15
|
+
* or a Promise that resolves to an AsyncGenerator when streaming is enabled.
|
|
16
|
+
* The AsyncGenerator yields partial responses as they arrive.
|
|
16
17
|
*/
|
|
17
18
|
function callAI(prompt, options = {}) {
|
|
18
19
|
// Check if we need to force streaming based on model strategy
|
|
@@ -27,7 +28,8 @@ function callAI(prompt, options = {}) {
|
|
|
27
28
|
return callAINonStreaming(prompt, options);
|
|
28
29
|
}
|
|
29
30
|
// Handle streaming mode - return a Promise that resolves to an AsyncGenerator
|
|
30
|
-
|
|
31
|
+
// but also supports legacy non-awaited usage for backward compatibility
|
|
32
|
+
const streamPromise = (async () => {
|
|
31
33
|
// Do setup and validation before returning the generator
|
|
32
34
|
const { endpoint, requestOptions, model, schemaStrategy } = prepareRequestParams(prompt, { ...options, stream: true });
|
|
33
35
|
// Make the fetch request and handle errors before creating the generator
|
|
@@ -69,6 +71,15 @@ function callAI(prompt, options = {}) {
|
|
|
69
71
|
// Only if response is OK, create and return the streaming generator
|
|
70
72
|
return createStreamingGenerator(response, options, schemaStrategy, model);
|
|
71
73
|
})();
|
|
74
|
+
// For backward compatibility with v0.6.x where users didn't await the result
|
|
75
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
76
|
+
console.warn(`[callAI:${PACKAGE_VERSION}] WARNING: Using callAI with streaming without await is deprecated. ` +
|
|
77
|
+
`Please use 'const generator = await callAI(...)' instead of 'const generator = callAI(...)'. ` +
|
|
78
|
+
`This backward compatibility will be removed in a future version.`);
|
|
79
|
+
}
|
|
80
|
+
// Create a proxy object that acts both as a Promise and an AsyncGenerator for backward compatibility
|
|
81
|
+
// @ts-ignore - We're deliberately implementing a proxy with dual behavior
|
|
82
|
+
return createBackwardCompatStreamingProxy(streamPromise);
|
|
72
83
|
}
|
|
73
84
|
/**
|
|
74
85
|
* Buffer streaming results into a single response for cases where
|
|
@@ -96,6 +107,51 @@ async function bufferStreamingResults(prompt, options) {
|
|
|
96
107
|
handleApiError(error, "Streaming buffer error", options.debug);
|
|
97
108
|
}
|
|
98
109
|
}
|
|
110
|
+
/**
|
|
111
|
+
* Standardized API error handler
|
|
112
|
+
*/
|
|
113
|
+
/**
|
|
114
|
+
* Create a proxy that acts both as a Promise and an AsyncGenerator for backward compatibility
|
|
115
|
+
* @internal This is for internal use only, not part of public API
|
|
116
|
+
*/
|
|
117
|
+
function createBackwardCompatStreamingProxy(promise) {
|
|
118
|
+
// Create a proxy that forwards methods to the Promise or AsyncGenerator as appropriate
|
|
119
|
+
return new Proxy({}, {
|
|
120
|
+
get(target, prop) {
|
|
121
|
+
// First check if it's an AsyncGenerator method (needed for for-await)
|
|
122
|
+
if (prop === 'next' || prop === 'throw' || prop === 'return' || prop === Symbol.asyncIterator) {
|
|
123
|
+
// Create wrapper functions that await the Promise first
|
|
124
|
+
if (prop === Symbol.asyncIterator) {
|
|
125
|
+
return function () {
|
|
126
|
+
return {
|
|
127
|
+
// Implement async iterator that gets the generator first
|
|
128
|
+
async next(value) {
|
|
129
|
+
try {
|
|
130
|
+
const generator = await promise;
|
|
131
|
+
return generator.next(value);
|
|
132
|
+
}
|
|
133
|
+
catch (error) {
|
|
134
|
+
// Turn Promise rejection into iterator result with error thrown
|
|
135
|
+
return Promise.reject(error);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
// Methods like next, throw, return
|
|
142
|
+
return async function (value) {
|
|
143
|
+
const generator = await promise;
|
|
144
|
+
return generator[prop](value);
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
// Then check if it's a Promise method
|
|
148
|
+
if (prop === 'then' || prop === 'catch' || prop === 'finally') {
|
|
149
|
+
return promise[prop].bind(promise);
|
|
150
|
+
}
|
|
151
|
+
return undefined;
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
}
|
|
99
155
|
/**
|
|
100
156
|
* Standardized API error handler
|
|
101
157
|
*/
|
package/dist/types.d.ts
CHANGED
|
@@ -68,6 +68,12 @@ export interface SchemaStrategy {
|
|
|
68
68
|
* Return type for streaming API calls
|
|
69
69
|
*/
|
|
70
70
|
export type StreamResponse = AsyncGenerator<string, string, unknown>;
|
|
71
|
+
/**
|
|
72
|
+
* @internal
|
|
73
|
+
* Internal type for backward compatibility with v0.6.x
|
|
74
|
+
* This type is not exposed in public API documentation
|
|
75
|
+
*/
|
|
76
|
+
export type ThenableStreamResponse = AsyncGenerator<string, string, unknown> & Promise<StreamResponse>;
|
|
71
77
|
export interface CallAIOptions {
|
|
72
78
|
/**
|
|
73
79
|
* API key for authentication
|