call-ai 0.10.2 → 0.11.0-dev-preview3
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/README.md +60 -58
- package/api-core.d.ts +13 -0
- package/{dist/api-core.js → api-core.js} +51 -126
- package/api-core.js.map +1 -0
- package/api.d.ts +4 -0
- package/api.js +364 -0
- package/api.js.map +1 -0
- package/api.ts.off +595 -0
- package/{dist/error-handling.d.ts → error-handling.d.ts} +4 -2
- package/{dist/error-handling.js → error-handling.js} +34 -70
- package/error-handling.js.map +1 -0
- package/image.d.ts +2 -0
- package/{dist/image.js → image.js} +10 -33
- package/image.js.map +1 -0
- package/index.d.ts +6 -0
- package/index.js +7 -0
- package/index.js.map +1 -0
- package/index.ts.bak +16 -0
- package/key-management.d.ts +29 -0
- package/key-management.js +189 -0
- package/key-management.js.map +1 -0
- package/{dist/non-streaming.d.ts → non-streaming.d.ts} +5 -8
- package/{dist/non-streaming.js → non-streaming.js} +28 -87
- package/non-streaming.js.map +1 -0
- package/package.json +15 -31
- package/response-metadata.d.ts +6 -0
- package/response-metadata.js +22 -0
- package/response-metadata.js.map +1 -0
- package/strategies/index.d.ts +2 -0
- package/strategies/index.js +3 -0
- package/strategies/index.js.map +1 -0
- package/strategies/model-strategies.d.ts +6 -0
- package/{dist/strategies → strategies}/model-strategies.js +26 -72
- package/strategies/model-strategies.js.map +1 -0
- package/strategies/strategy-selector.d.ts +2 -0
- package/strategies/strategy-selector.js +66 -0
- package/strategies/strategy-selector.js.map +1 -0
- package/streaming.d.ts +4 -0
- package/{dist/streaming.js → streaming.js} +66 -184
- package/streaming.js.map +1 -0
- package/streaming.ts.off +571 -0
- package/tsconfig.json +18 -0
- package/types.d.ts +226 -0
- package/types.js +33 -0
- package/types.js.map +1 -0
- package/utils.d.ts +32 -0
- package/utils.js +129 -0
- package/utils.js.map +1 -0
- package/version.d.ts +1 -0
- package/version.js +2 -0
- package/version.js.map +1 -0
- package/dist/api-core.d.ts +0 -40
- package/dist/api.d.ts +0 -15
- package/dist/api.js +0 -498
- package/dist/image.d.ts +0 -12
- package/dist/index.d.ts +0 -7
- package/dist/index.js +0 -32
- package/dist/key-management.d.ts +0 -43
- package/dist/key-management.js +0 -312
- package/dist/response-metadata.d.ts +0 -18
- package/dist/response-metadata.js +0 -44
- package/dist/strategies/index.d.ts +0 -5
- package/dist/strategies/index.js +0 -21
- package/dist/strategies/model-strategies.d.ts +0 -24
- package/dist/strategies/strategy-selector.d.ts +0 -8
- package/dist/strategies/strategy-selector.js +0 -79
- package/dist/streaming.d.ts +0 -7
- package/dist/types.d.ts +0 -226
- package/dist/types.js +0 -5
- package/dist/utils.d.ts +0 -8
- package/dist/utils.js +0 -52
|
@@ -1,46 +1,26 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
exports.extractClaudeResponse = extractClaudeResponse;
|
|
7
|
-
const key_management_1 = require("./key-management");
|
|
8
|
-
const error_handling_1 = require("./error-handling");
|
|
9
|
-
const response_metadata_1 = require("./response-metadata");
|
|
10
|
-
// Import package version for debugging
|
|
11
|
-
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
12
|
-
const PACKAGE_VERSION = require("../package.json").version;
|
|
13
|
-
exports.PACKAGE_VERSION = PACKAGE_VERSION;
|
|
1
|
+
import { globalDebug, keyStore, initKeyStore } from "./key-management.js";
|
|
2
|
+
import { handleApiError, checkForInvalidModelError } from "./error-handling.js";
|
|
3
|
+
import { responseMetadata, boxString } from "./response-metadata.js";
|
|
4
|
+
import { PACKAGE_VERSION } from "./version.js";
|
|
5
|
+
import { callAiFetch } from "./utils.js";
|
|
14
6
|
const FALLBACK_MODEL = "openrouter/auto";
|
|
15
|
-
exports.FALLBACK_MODEL = FALLBACK_MODEL;
|
|
16
|
-
// Internal implementation for non-streaming API calls
|
|
17
7
|
async function callAINonStreaming(prompt, options = {}, isRetry = false) {
|
|
18
|
-
// Ensure keyStore is initialized first
|
|
19
|
-
const { initKeyStore } = require("./key-management");
|
|
20
8
|
initKeyStore();
|
|
21
|
-
|
|
22
|
-
const messages = Array.isArray(prompt)
|
|
23
|
-
? prompt
|
|
24
|
-
: [{ role: "user", content: prompt }];
|
|
25
|
-
// API key should be provided by options (validation happens in callAi)
|
|
9
|
+
const messages = Array.isArray(prompt) ? prompt : [{ role: "user", content: prompt }];
|
|
26
10
|
const apiKey = options.apiKey;
|
|
27
11
|
const model = options.model || "openai/gpt-3.5-turbo";
|
|
28
|
-
// Default endpoint compatible with OpenAI API
|
|
29
12
|
const endpoint = options.endpoint || "https://openrouter.ai/api/v1";
|
|
30
|
-
// Build the endpoint URL
|
|
31
13
|
const url = `${endpoint}/chat/completions`;
|
|
32
|
-
// Choose a schema strategy based on model
|
|
33
14
|
const schemaStrategy = options.schemaStrategy;
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
const debug = options.debug === undefined ?
|
|
15
|
+
if (!schemaStrategy) {
|
|
16
|
+
throw new Error("Schema strategy is required for non-streaming calls");
|
|
17
|
+
}
|
|
18
|
+
const responseFormat = options.responseFormat || /gpt-4/.test(model) || /gpt-3.5/.test(model) ? "json" : undefined;
|
|
19
|
+
const debug = options.debug === undefined ? globalDebug : options.debug;
|
|
39
20
|
if (debug) {
|
|
40
21
|
console.log(`[callAi:${PACKAGE_VERSION}] Making non-streaming request to: ${url}`);
|
|
41
22
|
console.log(`[callAi:${PACKAGE_VERSION}] With model: ${model}`);
|
|
42
23
|
}
|
|
43
|
-
// Build request body
|
|
44
24
|
const requestBody = {
|
|
45
25
|
model,
|
|
46
26
|
messages,
|
|
@@ -49,26 +29,21 @@ async function callAINonStreaming(prompt, options = {}, isRetry = false) {
|
|
|
49
29
|
top_p: options.topP !== undefined ? options.topP : 1,
|
|
50
30
|
stream: false,
|
|
51
31
|
};
|
|
52
|
-
// Add response_format if specified or for JSON handling
|
|
53
32
|
if (responseFormat === "json") {
|
|
54
33
|
requestBody.response_format = { type: "json_object" };
|
|
55
34
|
}
|
|
56
|
-
// Add schema-specific parameters (if schema is provided)
|
|
57
35
|
if (options.schema) {
|
|
58
36
|
Object.assign(requestBody, schemaStrategy.prepareRequest(options.schema, messages));
|
|
59
37
|
}
|
|
60
|
-
// Add HTTP referer and other options to help with abuse prevention
|
|
61
38
|
const headers = {
|
|
62
39
|
Authorization: `Bearer ${apiKey}`,
|
|
63
40
|
"HTTP-Referer": options.referer || "https://vibes.diy",
|
|
64
41
|
"X-Title": options.title || "Vibes",
|
|
65
42
|
"Content-Type": "application/json",
|
|
66
43
|
};
|
|
67
|
-
// Add any additional headers
|
|
68
44
|
if (options.headers) {
|
|
69
45
|
Object.assign(headers, options.headers);
|
|
70
46
|
}
|
|
71
|
-
// Copy any other options not explicitly handled above
|
|
72
47
|
Object.keys(options).forEach((key) => {
|
|
73
48
|
if (![
|
|
74
49
|
"apiKey",
|
|
@@ -93,7 +68,6 @@ async function callAINonStreaming(prompt, options = {}, isRetry = false) {
|
|
|
93
68
|
console.log(`[callAi:${PACKAGE_VERSION}] Request headers:`, headers);
|
|
94
69
|
console.log(`[callAi:${PACKAGE_VERSION}] Request body:`, requestBody);
|
|
95
70
|
}
|
|
96
|
-
// Create metadata object for this response
|
|
97
71
|
const meta = {
|
|
98
72
|
model,
|
|
99
73
|
endpoint,
|
|
@@ -104,36 +78,27 @@ async function callAINonStreaming(prompt, options = {}, isRetry = false) {
|
|
|
104
78
|
},
|
|
105
79
|
};
|
|
106
80
|
try {
|
|
107
|
-
|
|
108
|
-
const response = await fetch(url, {
|
|
81
|
+
const response = await callAiFetch(options)(url, {
|
|
109
82
|
method: "POST",
|
|
110
83
|
headers,
|
|
111
84
|
body: JSON.stringify(requestBody),
|
|
112
85
|
});
|
|
113
|
-
// Handle HTTP errors
|
|
114
86
|
if (!response.ok) {
|
|
115
|
-
|
|
116
|
-
const { isInvalidModel, errorData } = await (0, error_handling_1.checkForInvalidModelError)(response, model, debug);
|
|
87
|
+
const { isInvalidModel, errorData } = await checkForInvalidModelError(response, model, debug);
|
|
117
88
|
if (isInvalidModel && !isRetry && !options.skipRetry) {
|
|
118
89
|
if (debug) {
|
|
119
90
|
console.log(`[callAi:${PACKAGE_VERSION}] Invalid model "${model}", falling back to "${FALLBACK_MODEL}"`);
|
|
120
91
|
}
|
|
121
|
-
// Retry with the fallback model
|
|
122
92
|
return callAINonStreaming(prompt, {
|
|
123
93
|
...options,
|
|
124
94
|
model: FALLBACK_MODEL,
|
|
125
95
|
}, true);
|
|
126
96
|
}
|
|
127
|
-
|
|
128
|
-
const errorText = errorData
|
|
129
|
-
? JSON.stringify(errorData)
|
|
130
|
-
: `HTTP error! Status: ${response.status}`;
|
|
97
|
+
const errorText = errorData ? JSON.stringify(errorData) : `HTTP error! Status: ${response.status}`;
|
|
131
98
|
throw new Error(errorText);
|
|
132
99
|
}
|
|
133
|
-
// Parse response
|
|
134
100
|
let result;
|
|
135
101
|
try {
|
|
136
|
-
// For special cases like Claude, use a different extraction method
|
|
137
102
|
if (/claude/.test(model)) {
|
|
138
103
|
result = await extractClaudeResponse(response);
|
|
139
104
|
}
|
|
@@ -145,121 +110,97 @@ async function callAINonStreaming(prompt, options = {}, isRetry = false) {
|
|
|
145
110
|
catch (parseError) {
|
|
146
111
|
throw new Error(`Failed to parse API response: ${parseError instanceof Error ? parseError.message : String(parseError)}`);
|
|
147
112
|
}
|
|
148
|
-
// Update metadata with completion timing
|
|
149
113
|
const endTime = Date.now();
|
|
150
114
|
meta.timing.endTime = endTime;
|
|
151
115
|
meta.timing.duration = endTime - meta.timing.startTime;
|
|
152
|
-
// Store metadata for this response
|
|
153
116
|
const resultString = typeof result === "string" ? result : JSON.stringify(result);
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
response_metadata_1.responseMetadata.set(boxed, meta);
|
|
117
|
+
const boxed = boxString(resultString);
|
|
118
|
+
responseMetadata.set(boxed, meta);
|
|
157
119
|
return resultString;
|
|
158
120
|
}
|
|
159
121
|
catch (error) {
|
|
160
|
-
|
|
161
|
-
const isNetworkError = error instanceof Error &&
|
|
162
|
-
(error.message.includes("Network") || error.name === "TypeError");
|
|
122
|
+
const isNetworkError = error instanceof Error && (error.message.includes("Network") || error.name === "TypeError");
|
|
163
123
|
if (isNetworkError) {
|
|
164
|
-
// Direct re-throw for network errors (original implementation pattern)
|
|
165
124
|
if (debug) {
|
|
166
125
|
console.error(`[callAi:${PACKAGE_VERSION}] Network error during fetch:`, error);
|
|
167
126
|
}
|
|
168
127
|
throw error;
|
|
169
128
|
}
|
|
170
|
-
|
|
171
|
-
await (0, error_handling_1.handleApiError)(error, "Non-streaming API call", options.debug, {
|
|
129
|
+
await handleApiError(error, "Non-streaming API call", options.debug, {
|
|
172
130
|
apiKey: apiKey || undefined,
|
|
173
131
|
endpoint: options.endpoint || undefined,
|
|
174
132
|
skipRefresh: options.skipRefresh,
|
|
175
133
|
});
|
|
176
|
-
|
|
177
|
-
if (key_management_1.keyStore.current && key_management_1.keyStore.current !== apiKey) {
|
|
134
|
+
if (keyStore().current && keyStore().current !== apiKey) {
|
|
178
135
|
if (debug) {
|
|
179
136
|
console.log(`[callAi:${PACKAGE_VERSION}] Retrying with refreshed API key`);
|
|
180
137
|
}
|
|
181
|
-
// Retry the request with the new key
|
|
182
138
|
return callAINonStreaming(prompt, {
|
|
183
139
|
...options,
|
|
184
|
-
apiKey:
|
|
140
|
+
apiKey: keyStore().current,
|
|
185
141
|
}, isRetry);
|
|
186
142
|
}
|
|
187
|
-
// If we get here, handleApiError failed to recover, so we should never reach this
|
|
188
|
-
// But just in case, rethrow the error
|
|
189
143
|
throw error;
|
|
190
144
|
}
|
|
191
145
|
}
|
|
192
|
-
// Extract content from API response accounting for different formats
|
|
193
146
|
function extractContent(result, schemaStrategy) {
|
|
194
|
-
// Debug output has been removed for brevity
|
|
195
147
|
if (!result) {
|
|
196
148
|
return "";
|
|
197
149
|
}
|
|
198
|
-
// Handle different response formats
|
|
199
150
|
if (result.choices && result.choices.length > 0) {
|
|
200
151
|
const choice = result.choices[0];
|
|
201
|
-
// Handle OpenAI format - content directly in message
|
|
202
152
|
if (choice.message && choice.message.content) {
|
|
203
153
|
return schemaStrategy.processResponse(choice.message.content);
|
|
204
154
|
}
|
|
205
|
-
// Handle function call response - pass through the schemaStrategy
|
|
206
155
|
if (choice.message && choice.message.function_call) {
|
|
207
156
|
return schemaStrategy.processResponse(choice.message.function_call);
|
|
208
157
|
}
|
|
209
|
-
// Handle function/tools response (newer format)
|
|
210
158
|
if (choice.message && choice.message.tool_calls) {
|
|
211
159
|
return schemaStrategy.processResponse(choice.message.tool_calls);
|
|
212
160
|
}
|
|
213
|
-
// Handle anthropic/claude format with content blocks
|
|
214
161
|
if (choice.message && Array.isArray(choice.message.content)) {
|
|
215
162
|
let textContent = "";
|
|
216
163
|
let toolUse = null;
|
|
217
|
-
// Find text or tool_use blocks
|
|
218
164
|
for (const block of choice.message.content) {
|
|
219
165
|
if (block.type === "text") {
|
|
220
166
|
textContent += block.text || "";
|
|
221
167
|
}
|
|
222
168
|
else if (block.type === "tool_use") {
|
|
223
169
|
toolUse = block;
|
|
224
|
-
break;
|
|
170
|
+
break;
|
|
225
171
|
}
|
|
226
172
|
}
|
|
227
|
-
// If we have a tool_use block, that takes precedence
|
|
228
173
|
if (toolUse) {
|
|
229
174
|
return schemaStrategy.processResponse(toolUse);
|
|
230
175
|
}
|
|
231
|
-
// Otherwise use the accumulated text content
|
|
232
176
|
return schemaStrategy.processResponse(textContent);
|
|
233
177
|
}
|
|
234
|
-
// Fallback for simple text response
|
|
235
178
|
if (choice.text) {
|
|
236
179
|
return schemaStrategy.processResponse(choice.text);
|
|
237
180
|
}
|
|
238
181
|
}
|
|
239
|
-
|
|
182
|
+
if (typeof result !== "string") {
|
|
183
|
+
throw new Error(`Failed to extract content from API response: ${JSON.stringify(result)}`);
|
|
184
|
+
}
|
|
240
185
|
return result;
|
|
241
186
|
}
|
|
242
|
-
// Extract response from Claude API with timeout handling
|
|
243
187
|
async function extractClaudeResponse(response) {
|
|
244
188
|
try {
|
|
245
189
|
const timeoutPromise = new Promise((_, reject) => {
|
|
246
190
|
setTimeout(() => {
|
|
247
191
|
reject(new Error("Timeout extracting Claude response"));
|
|
248
|
-
}, 5000);
|
|
192
|
+
}, 5000);
|
|
249
193
|
});
|
|
250
194
|
const responsePromise = response.json();
|
|
251
|
-
// Race between timeout and response
|
|
252
195
|
const json = await Promise.race([responsePromise, timeoutPromise]);
|
|
253
|
-
if (json.choices &&
|
|
254
|
-
json.choices.length > 0 &&
|
|
255
|
-
json.choices[0].message &&
|
|
256
|
-
json.choices[0].message.content) {
|
|
196
|
+
if (json.choices && json.choices.length > 0 && json.choices[0].message && json.choices[0].message.content) {
|
|
257
197
|
return json.choices[0].message.content;
|
|
258
198
|
}
|
|
259
|
-
// If content not found in expected structure, return the whole JSON
|
|
260
199
|
return json;
|
|
261
200
|
}
|
|
262
201
|
catch (error) {
|
|
263
202
|
throw new Error(`Failed to extract Claude response: ${error instanceof Error ? error.message : String(error)}`);
|
|
264
203
|
}
|
|
265
204
|
}
|
|
205
|
+
export { callAINonStreaming, extractContent, extractClaudeResponse, PACKAGE_VERSION, FALLBACK_MODEL };
|
|
206
|
+
//# sourceMappingURL=non-streaming.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"non-streaming.js","sourceRoot":"","sources":["../jsr/non-streaming.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAC1E,OAAO,EAAE,cAAc,EAAE,yBAAyB,EAAE,MAAM,qBAAqB,CAAC;AAChF,OAAO,EAAE,gBAAgB,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AACrE,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAC/C,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAGzC,MAAM,cAAc,GAAG,iBAAiB,CAAC;A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,EAAE,eAAe,EAAE,cAAc,EAAE,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,13 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "call-ai",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.11.0-dev-preview3",
|
|
4
4
|
"description": "Lightweight library for making AI API calls with streaming support",
|
|
5
|
-
"
|
|
6
|
-
"browser": "dist/index.js",
|
|
7
|
-
"types": "dist/index.d.ts",
|
|
8
|
-
"files": [
|
|
9
|
-
"dist"
|
|
10
|
-
],
|
|
5
|
+
"type": "module",
|
|
11
6
|
"repository": {
|
|
12
7
|
"type": "git",
|
|
13
8
|
"url": "https://github.com/fireproof-storage/call-ai.git"
|
|
@@ -16,17 +11,6 @@
|
|
|
16
11
|
"bugs": {
|
|
17
12
|
"url": "https://github.com/fireproof-storage/call-ai/issues"
|
|
18
13
|
},
|
|
19
|
-
"scripts": {
|
|
20
|
-
"build": "tsc",
|
|
21
|
-
"test": "jest --testMatch=\"**/*unit*.test.ts\"",
|
|
22
|
-
"test:integration": "jest simple.integration",
|
|
23
|
-
"test:all": "pnpm test && pnpm test:integration",
|
|
24
|
-
"prepublishOnly": "npm run build",
|
|
25
|
-
"typecheck": "tsc --noEmit",
|
|
26
|
-
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
|
|
27
|
-
"coverage": "jest --coverage",
|
|
28
|
-
"check": "npm run typecheck && npm run format && npm run test && npm run build"
|
|
29
|
-
},
|
|
30
14
|
"keywords": [
|
|
31
15
|
"ai",
|
|
32
16
|
"llm",
|
|
@@ -36,20 +20,20 @@
|
|
|
36
20
|
"streaming",
|
|
37
21
|
"openrouter"
|
|
38
22
|
],
|
|
39
|
-
"
|
|
40
|
-
|
|
23
|
+
"contributors": [
|
|
24
|
+
"J Chris Anderson",
|
|
25
|
+
"Meno Abels"
|
|
26
|
+
],
|
|
27
|
+
"license": "Apache-2.0",
|
|
41
28
|
"devDependencies": {
|
|
42
|
-
"@
|
|
43
|
-
"@types/node": "^
|
|
44
|
-
"
|
|
45
|
-
"dotenv": "^16.4.7",
|
|
46
|
-
"jest": "^29.6.1",
|
|
47
|
-
"node-fetch": "^3.3.2",
|
|
48
|
-
"prettier": "^3.5.3",
|
|
49
|
-
"ts-jest": "^29.1.1",
|
|
50
|
-
"typescript": "^5.1.6"
|
|
29
|
+
"@fireproof/core-cli": "^0.23.6",
|
|
30
|
+
"@types/node": "^24.0.15",
|
|
31
|
+
"typescript": "^5.8.3"
|
|
51
32
|
},
|
|
52
33
|
"engines": {
|
|
53
|
-
"node": ">=
|
|
34
|
+
"node": ">=20.0.0"
|
|
35
|
+
},
|
|
36
|
+
"scripts": {
|
|
37
|
+
"build": "core-cli tsc"
|
|
54
38
|
}
|
|
55
|
-
}
|
|
39
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { ResponseMeta } from "./types.js";
|
|
2
|
+
declare const responseMetadata: WeakMap<object, ResponseMeta>;
|
|
3
|
+
declare const stringResponseMap: Map<string, object>;
|
|
4
|
+
declare function boxString(str: string): object;
|
|
5
|
+
declare function getMeta(response: string | AsyncGenerator<string, string, unknown>): ResponseMeta | undefined;
|
|
6
|
+
export { responseMetadata, stringResponseMap, boxString, getMeta };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
const responseMetadata = new WeakMap();
|
|
2
|
+
const stringResponseMap = new Map();
|
|
3
|
+
function boxString(str) {
|
|
4
|
+
if (stringResponseMap.has(str)) {
|
|
5
|
+
return stringResponseMap.get(str);
|
|
6
|
+
}
|
|
7
|
+
const box = Object.create(null);
|
|
8
|
+
stringResponseMap.set(str, box);
|
|
9
|
+
return box;
|
|
10
|
+
}
|
|
11
|
+
function getMeta(response) {
|
|
12
|
+
if (typeof response === "string") {
|
|
13
|
+
const box = stringResponseMap.get(response);
|
|
14
|
+
if (box) {
|
|
15
|
+
return responseMetadata.get(box);
|
|
16
|
+
}
|
|
17
|
+
return undefined;
|
|
18
|
+
}
|
|
19
|
+
return responseMetadata.get(response);
|
|
20
|
+
}
|
|
21
|
+
export { responseMetadata, stringResponseMap, boxString, getMeta };
|
|
22
|
+
//# sourceMappingURL=response-metadata.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"response-metadata.js","sourceRoot":"","sources":["../jsr/response-metadata.ts"],"names":[],"mappings":"AAOA,MAAM,gBAAgB,GAAG,IAAI,OAAO,EAAwB,CAAC;AAG7D,MAAM,iBAAiB,GAAG,IAAI,GAAG,EAAkB,CAAC;AAMpD,SAAS,SAAS,CAAC,GAAW,EAAU;IAEtC,IAAI,iBAAiB,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC;QAC/B,OAAO,iBAAiB,CAAC,GAAG,CAAC,GAAG,CAAW,CAAC;IAC9C,CAAC;IAED,MAAM,GAAG,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IAChC,iBAAiB,CAAC,GAAG,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;IAChC,OAAO,GAAG,CAAC;AAAA,CACZ;AAOD,SAAS,OAAO,CAAC,QAA0D,EAA4B;IACrG,IAAI,OAAO,QAAQ,KAAK,QAAQ,EAAE,CAAC;QACjC,MAAM,GAAG,GAAG,iBAAiB,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;QAC5C,IAAI,GAAG,EAAE,CAAC;YACR,OAAO,gBAAgB,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QACnC,CAAC;QACD,OAAO,SAAS,CAAC;IACnB,CAAC;IAED,OAAO,gBAAgB,CAAC,GAAG,CAAC,QAAkB,CAAC,CAAC;AAAA,CACjD;AAED,OAAO,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,SAAS,EAAE,OAAO,EAAE,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../jsr/strategies/index.ts"],"names":[],"mappings":"AAGA,cAAc,uBAAuB,CAAC;AACtC,cAAc,wBAAwB,CAAC"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { ModelStrategy } from "../types.js";
|
|
2
|
+
export declare const openAIStrategy: ModelStrategy;
|
|
3
|
+
export declare const geminiStrategy: ModelStrategy;
|
|
4
|
+
export declare const claudeStrategy: ModelStrategy;
|
|
5
|
+
export declare const systemMessageStrategy: ModelStrategy;
|
|
6
|
+
export declare const defaultStrategy: ModelStrategy;
|
|
@@ -1,31 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const utils_1 = require("../utils");
|
|
5
|
-
/**
|
|
6
|
-
* OpenAI/GPT strategy for handling JSON schema
|
|
7
|
-
*/
|
|
8
|
-
exports.openAIStrategy = {
|
|
1
|
+
import { isToolUseType, } from "../types.js";
|
|
2
|
+
import { recursivelyAddAdditionalProperties } from "../utils.js";
|
|
3
|
+
export const openAIStrategy = {
|
|
9
4
|
name: "openai",
|
|
10
5
|
prepareRequest: (schema) => {
|
|
11
6
|
if (!schema)
|
|
12
|
-
|
|
13
|
-
// Process schema for JSON schema approach
|
|
7
|
+
throw new Error("Schema strategy not implemented");
|
|
14
8
|
const requiredFields = schema.required || Object.keys(schema.properties || {});
|
|
15
|
-
const processedSchema =
|
|
9
|
+
const processedSchema = recursivelyAddAdditionalProperties({
|
|
16
10
|
type: "object",
|
|
17
11
|
properties: schema.properties || {},
|
|
18
12
|
required: requiredFields,
|
|
19
|
-
additionalProperties: schema.additionalProperties !== undefined
|
|
20
|
-
|
|
21
|
-
: false,
|
|
22
|
-
// Copy any additional schema properties
|
|
23
|
-
...Object.fromEntries(Object.entries(schema).filter(([key]) => ![
|
|
24
|
-
"name",
|
|
25
|
-
"properties",
|
|
26
|
-
"required",
|
|
27
|
-
"additionalProperties",
|
|
28
|
-
].includes(key))),
|
|
13
|
+
additionalProperties: schema.additionalProperties !== undefined ? schema.additionalProperties : false,
|
|
14
|
+
...Object.fromEntries(Object.entries(schema).filter(([key]) => !["name", "properties", "required", "additionalProperties"].includes(key))),
|
|
29
15
|
});
|
|
30
16
|
return {
|
|
31
17
|
response_format: {
|
|
@@ -45,40 +31,30 @@ exports.openAIStrategy = {
|
|
|
45
31
|
return content;
|
|
46
32
|
},
|
|
47
33
|
};
|
|
48
|
-
|
|
49
|
-
* Gemini strategy for handling JSON schema (similar to OpenAI)
|
|
50
|
-
*/
|
|
51
|
-
exports.geminiStrategy = {
|
|
34
|
+
export const geminiStrategy = {
|
|
52
35
|
name: "gemini",
|
|
53
|
-
prepareRequest:
|
|
36
|
+
prepareRequest: openAIStrategy.prepareRequest,
|
|
54
37
|
processResponse: (content) => {
|
|
55
38
|
if (typeof content !== "string") {
|
|
56
39
|
return JSON.stringify(content);
|
|
57
40
|
}
|
|
58
|
-
// Try to extract JSON from content if it might be wrapped
|
|
59
41
|
const jsonMatch = content.match(/```json\s*([\s\S]*?)\s*```/) ||
|
|
60
42
|
content.match(/```\s*([\s\S]*?)\s*```/) ||
|
|
61
43
|
content.match(/\{[\s\S]*\}/) || [null, content];
|
|
62
44
|
return jsonMatch[1] || content;
|
|
63
45
|
},
|
|
64
46
|
};
|
|
65
|
-
|
|
66
|
-
* Claude strategy using tool mode for structured output
|
|
67
|
-
*/
|
|
68
|
-
exports.claudeStrategy = {
|
|
47
|
+
export const claudeStrategy = {
|
|
69
48
|
name: "anthropic",
|
|
70
49
|
shouldForceStream: true,
|
|
71
50
|
prepareRequest: (schema) => {
|
|
72
51
|
if (!schema)
|
|
73
|
-
|
|
74
|
-
// Process schema for tool use - format for OpenRouter/Claude
|
|
52
|
+
throw new Error("Schema strategy not implemented");
|
|
75
53
|
const processedSchema = {
|
|
76
54
|
type: "object",
|
|
77
55
|
properties: schema.properties || {},
|
|
78
56
|
required: schema.required || Object.keys(schema.properties || {}),
|
|
79
|
-
additionalProperties: schema.additionalProperties !== undefined
|
|
80
|
-
? schema.additionalProperties
|
|
81
|
-
: false,
|
|
57
|
+
additionalProperties: schema.additionalProperties !== undefined ? schema.additionalProperties : false,
|
|
82
58
|
};
|
|
83
59
|
return {
|
|
84
60
|
tools: [
|
|
@@ -100,25 +76,15 @@ exports.claudeStrategy = {
|
|
|
100
76
|
};
|
|
101
77
|
},
|
|
102
78
|
processResponse: (content) => {
|
|
103
|
-
|
|
104
|
-
if (typeof content === "object") {
|
|
79
|
+
if (isToolUseType(content)) {
|
|
105
80
|
if (content.type === "tool_use") {
|
|
106
81
|
return JSON.stringify(content.input);
|
|
107
82
|
}
|
|
108
|
-
|
|
109
|
-
if (content.tool_calls &&
|
|
110
|
-
Array.isArray(content.tool_calls) &&
|
|
111
|
-
content.tool_calls.length > 0) {
|
|
83
|
+
if (content.tool_calls && Array.isArray(content.tool_calls) && content.tool_calls.length > 0) {
|
|
112
84
|
const toolCall = content.tool_calls[0];
|
|
113
85
|
if (toolCall.function && toolCall.function.arguments) {
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
return toolCall.function.arguments;
|
|
117
|
-
}
|
|
118
|
-
catch (e) {
|
|
119
|
-
// Return as is if not valid JSON
|
|
120
|
-
return JSON.stringify(toolCall.function.arguments);
|
|
121
|
-
}
|
|
86
|
+
return JSON.stringify(toolCall.function.arguments);
|
|
87
|
+
// }
|
|
122
88
|
}
|
|
123
89
|
}
|
|
124
90
|
return JSON.stringify(content);
|
|
@@ -126,31 +92,21 @@ exports.claudeStrategy = {
|
|
|
126
92
|
if (typeof content !== "string") {
|
|
127
93
|
return JSON.stringify(content);
|
|
128
94
|
}
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
content.match(/```\s*([\s\S]*?)\s*```/) ||
|
|
132
|
-
content.match(/\{[\s\S]*\}/) || [null, content];
|
|
133
|
-
return jsonMatch[1] || content;
|
|
95
|
+
const jsonMatch = content.match(/```json\s*([\s\S]*?)\s*```/) || content.match(/```\s*([\s\S]*?)\s*```/) || content.match(/\{[\s\S]*\}/);
|
|
96
|
+
return jsonMatch ? jsonMatch[1] : content;
|
|
134
97
|
},
|
|
135
98
|
};
|
|
136
|
-
|
|
137
|
-
* System message approach for other models (Llama, DeepSeek, etc.)
|
|
138
|
-
*/
|
|
139
|
-
exports.systemMessageStrategy = {
|
|
99
|
+
export const systemMessageStrategy = {
|
|
140
100
|
name: "system_message",
|
|
141
101
|
prepareRequest: (schema, messages) => {
|
|
142
102
|
if (!schema)
|
|
143
103
|
return { messages };
|
|
144
|
-
// Check if there's already a system message
|
|
145
104
|
const hasSystemMessage = messages.some((m) => m.role === "system");
|
|
146
105
|
if (!hasSystemMessage) {
|
|
147
|
-
// Build a schema description
|
|
148
106
|
const schemaProperties = Object.entries(schema.properties || {})
|
|
149
107
|
.map(([key, value]) => {
|
|
150
108
|
const type = value.type || "string";
|
|
151
|
-
const description = value.description
|
|
152
|
-
? ` // ${value.description}`
|
|
153
|
-
: "";
|
|
109
|
+
const description = value.description ? ` // ${value.description}` : "";
|
|
154
110
|
return ` "${key}": ${type}${description}`;
|
|
155
111
|
})
|
|
156
112
|
.join(",\n");
|
|
@@ -158,7 +114,6 @@ exports.systemMessageStrategy = {
|
|
|
158
114
|
role: "system",
|
|
159
115
|
content: `Please return your response as JSON following this schema exactly:\n{\n${schemaProperties}\n}\nDo not include any explanation or text outside of the JSON object.`,
|
|
160
116
|
};
|
|
161
|
-
// Return modified messages array with system message prepended
|
|
162
117
|
return { messages: [systemMessage, ...messages] };
|
|
163
118
|
}
|
|
164
119
|
return { messages };
|
|
@@ -167,18 +122,17 @@ exports.systemMessageStrategy = {
|
|
|
167
122
|
if (typeof content !== "string") {
|
|
168
123
|
return JSON.stringify(content);
|
|
169
124
|
}
|
|
170
|
-
// Try to extract JSON from content if it might be wrapped
|
|
171
125
|
const jsonMatch = content.match(/```json\s*([\s\S]*?)\s*```/) ||
|
|
172
126
|
content.match(/```\s*([\s\S]*?)\s*```/) ||
|
|
173
127
|
content.match(/\{[\s\S]*\}/) || [null, content];
|
|
174
128
|
return jsonMatch[1] || content;
|
|
175
129
|
},
|
|
176
130
|
};
|
|
177
|
-
|
|
178
|
-
* Default strategy for models without schema
|
|
179
|
-
*/
|
|
180
|
-
exports.defaultStrategy = {
|
|
131
|
+
export const defaultStrategy = {
|
|
181
132
|
name: "default",
|
|
182
|
-
prepareRequest: () =>
|
|
183
|
-
|
|
133
|
+
prepareRequest: () => {
|
|
134
|
+
throw new Error("Schema strategy not implemented");
|
|
135
|
+
},
|
|
136
|
+
processResponse: (content) => (typeof content === "string" ? content : JSON.stringify(content)),
|
|
184
137
|
};
|
|
138
|
+
//# sourceMappingURL=model-strategies.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"model-strategies.js","sourceRoot":"","sources":["../../jsr/strategies/model-strategies.ts"],"names":[],"mappings":"AAGA,OAAO,EACL,aAAa,GAQd,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,kCAAkC,EAAE,MAAM,aAAa,CAAC;AAKjE,MAAM,CAAC,MAAM,cAAc,GAAkB;IAC3C,IAAI,EAAE,QAAQ;IACd,cAAc,EAAE,CAAC,MAAM,EAAE,EAAE,CAAC;QAC1B,IAAI,CAAC,MAAM;YAAE,MAAM,IAAI,KAAK,CAAC,iCAAiC,CAAC,CAAC;QAGhE,MAAM,cAAc,GAAG,MAAM,CAAC,QAAQ,IAAI,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,UAAU,IAAI,EAAE,CAAC,CAAC;QAE/E,MAAM,eAAe,GAAG,kCAAkC,CAAC;YACzD,IAAI,EAAE,QAAQ;YACd,UAAU,EAAE,MAAM,CAAC,UAAU,IAAI,EAAE;YACnC,QAAQ,EAAE,cAAc;YACxB,oBAAoB,EAAE,MAAM,CAAC,oBAAoB,KAAK,SAAS,CAAC,CAAC,CAAC,MAAM,CAAC,oBAAoB,CAAC,CAAC,CAAC,KAAK;YAErG,GAAG,MAAM,CAAC,WAAW,CACnB,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,MAA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