call-ai 0.0.0-dev-prompts
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/LICENSE.md +232 -0
- package/README.md +264 -0
- package/api-core.d.ts +13 -0
- package/api-core.js +238 -0
- package/api-core.js.map +1 -0
- package/api.d.ts +4 -0
- package/api.js +365 -0
- package/api.js.map +1 -0
- package/api.ts.off +595 -0
- package/env.d.ts +22 -0
- package/env.js +65 -0
- package/env.js.map +1 -0
- package/error-handling.d.ts +14 -0
- package/error-handling.js +144 -0
- package/error-handling.js.map +1 -0
- package/image.d.ts +2 -0
- package/image.js +72 -0
- package/image.js.map +1 -0
- package/index.d.ts +7 -0
- package/index.js +8 -0
- package/index.js.map +1 -0
- package/index.ts.bak +16 -0
- package/key-management.d.ts +29 -0
- package/key-management.js +190 -0
- package/key-management.js.map +1 -0
- package/non-streaming.d.ts +7 -0
- package/non-streaming.js +206 -0
- package/non-streaming.js.map +1 -0
- package/package.json +43 -0
- 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/strategies/model-strategies.js +138 -0
- 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/streaming.js +365 -0
- package/streaming.js.map +1 -0
- package/streaming.ts.off +571 -0
- package/tsconfig.json +18 -0
- package/types.d.ts +228 -0
- package/types.js +33 -0
- package/types.js.map +1 -0
- package/utils.d.ts +8 -0
- package/utils.js +42 -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/non-streaming.js
ADDED
|
@@ -0,0 +1,206 @@
|
|
|
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";
|
|
6
|
+
const FALLBACK_MODEL = "openrouter/auto";
|
|
7
|
+
async function callAINonStreaming(prompt, options = {}, isRetry = false) {
|
|
8
|
+
initKeyStore();
|
|
9
|
+
const messages = Array.isArray(prompt) ? prompt : [{ role: "user", content: prompt }];
|
|
10
|
+
const apiKey = options.apiKey;
|
|
11
|
+
const model = options.model || "openai/gpt-3.5-turbo";
|
|
12
|
+
const endpoint = options.endpoint || "https://openrouter.ai/api/v1";
|
|
13
|
+
const url = `${endpoint}/chat/completions`;
|
|
14
|
+
const schemaStrategy = options.schemaStrategy;
|
|
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;
|
|
20
|
+
if (debug) {
|
|
21
|
+
console.log(`[callAi:${PACKAGE_VERSION}] Making non-streaming request to: ${url}`);
|
|
22
|
+
console.log(`[callAi:${PACKAGE_VERSION}] With model: ${model}`);
|
|
23
|
+
}
|
|
24
|
+
const requestBody = {
|
|
25
|
+
model,
|
|
26
|
+
messages,
|
|
27
|
+
max_tokens: options.maxTokens || 2048,
|
|
28
|
+
temperature: options.temperature !== undefined ? options.temperature : 0.7,
|
|
29
|
+
top_p: options.topP !== undefined ? options.topP : 1,
|
|
30
|
+
stream: false,
|
|
31
|
+
};
|
|
32
|
+
if (responseFormat === "json") {
|
|
33
|
+
requestBody.response_format = { type: "json_object" };
|
|
34
|
+
}
|
|
35
|
+
if (options.schema) {
|
|
36
|
+
Object.assign(requestBody, schemaStrategy.prepareRequest(options.schema, messages));
|
|
37
|
+
}
|
|
38
|
+
const headers = {
|
|
39
|
+
Authorization: `Bearer ${apiKey}`,
|
|
40
|
+
"HTTP-Referer": options.referer || "https://vibes.diy",
|
|
41
|
+
"X-Title": options.title || "Vibes",
|
|
42
|
+
"Content-Type": "application/json",
|
|
43
|
+
};
|
|
44
|
+
if (options.headers) {
|
|
45
|
+
Object.assign(headers, options.headers);
|
|
46
|
+
}
|
|
47
|
+
Object.keys(options).forEach((key) => {
|
|
48
|
+
if (![
|
|
49
|
+
"apiKey",
|
|
50
|
+
"model",
|
|
51
|
+
"endpoint",
|
|
52
|
+
"stream",
|
|
53
|
+
"schema",
|
|
54
|
+
"maxTokens",
|
|
55
|
+
"temperature",
|
|
56
|
+
"topP",
|
|
57
|
+
"responseFormat",
|
|
58
|
+
"referer",
|
|
59
|
+
"title",
|
|
60
|
+
"headers",
|
|
61
|
+
"skipRefresh",
|
|
62
|
+
"debug",
|
|
63
|
+
].includes(key)) {
|
|
64
|
+
requestBody[key] = options[key];
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
if (debug) {
|
|
68
|
+
console.log(`[callAi:${PACKAGE_VERSION}] Request headers:`, headers);
|
|
69
|
+
console.log(`[callAi:${PACKAGE_VERSION}] Request body:`, requestBody);
|
|
70
|
+
}
|
|
71
|
+
const meta = {
|
|
72
|
+
model,
|
|
73
|
+
endpoint,
|
|
74
|
+
timing: {
|
|
75
|
+
startTime: Date.now(),
|
|
76
|
+
endTime: 0,
|
|
77
|
+
duration: 0,
|
|
78
|
+
},
|
|
79
|
+
};
|
|
80
|
+
try {
|
|
81
|
+
const response = await callAiFetch(options)(url, {
|
|
82
|
+
method: "POST",
|
|
83
|
+
headers,
|
|
84
|
+
body: JSON.stringify(requestBody),
|
|
85
|
+
});
|
|
86
|
+
if (!response.ok) {
|
|
87
|
+
const { isInvalidModel, errorData } = await checkForInvalidModelError(response, model, debug);
|
|
88
|
+
if (isInvalidModel && !isRetry && !options.skipRetry) {
|
|
89
|
+
if (debug) {
|
|
90
|
+
console.log(`[callAi:${PACKAGE_VERSION}] Invalid model "${model}", falling back to "${FALLBACK_MODEL}"`);
|
|
91
|
+
}
|
|
92
|
+
return callAINonStreaming(prompt, {
|
|
93
|
+
...options,
|
|
94
|
+
model: FALLBACK_MODEL,
|
|
95
|
+
}, true);
|
|
96
|
+
}
|
|
97
|
+
const errorText = errorData ? JSON.stringify(errorData) : `HTTP error! Status: ${response.status}`;
|
|
98
|
+
throw new Error(errorText);
|
|
99
|
+
}
|
|
100
|
+
let result;
|
|
101
|
+
try {
|
|
102
|
+
if (/claude/.test(model)) {
|
|
103
|
+
result = await extractClaudeResponse(response);
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
const json = await response.json();
|
|
107
|
+
result = extractContent(json, schemaStrategy);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
catch (parseError) {
|
|
111
|
+
throw new Error(`Failed to parse API response: ${parseError instanceof Error ? parseError.message : String(parseError)}`);
|
|
112
|
+
}
|
|
113
|
+
const endTime = Date.now();
|
|
114
|
+
meta.timing.endTime = endTime;
|
|
115
|
+
meta.timing.duration = endTime - meta.timing.startTime;
|
|
116
|
+
const resultString = typeof result === "string" ? result : JSON.stringify(result);
|
|
117
|
+
const boxed = boxString(resultString);
|
|
118
|
+
responseMetadata.set(boxed, meta);
|
|
119
|
+
return resultString;
|
|
120
|
+
}
|
|
121
|
+
catch (error) {
|
|
122
|
+
const isNetworkError = error instanceof Error && (error.message.includes("Network") || error.name === "TypeError");
|
|
123
|
+
if (isNetworkError) {
|
|
124
|
+
if (debug) {
|
|
125
|
+
console.error(`[callAi:${PACKAGE_VERSION}] Network error during fetch:`, error);
|
|
126
|
+
}
|
|
127
|
+
throw error;
|
|
128
|
+
}
|
|
129
|
+
await handleApiError(error, "Non-streaming API call", options.debug, {
|
|
130
|
+
apiKey: apiKey || undefined,
|
|
131
|
+
endpoint: options.endpoint || undefined,
|
|
132
|
+
skipRefresh: options.skipRefresh,
|
|
133
|
+
});
|
|
134
|
+
if (keyStore().current && keyStore().current !== apiKey) {
|
|
135
|
+
if (debug) {
|
|
136
|
+
console.log(`[callAi:${PACKAGE_VERSION}] Retrying with refreshed API key`);
|
|
137
|
+
}
|
|
138
|
+
return callAINonStreaming(prompt, {
|
|
139
|
+
...options,
|
|
140
|
+
apiKey: keyStore().current,
|
|
141
|
+
}, isRetry);
|
|
142
|
+
}
|
|
143
|
+
throw error;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
function extractContent(result, schemaStrategy) {
|
|
147
|
+
if (!result) {
|
|
148
|
+
return "";
|
|
149
|
+
}
|
|
150
|
+
if (result.choices && result.choices.length > 0) {
|
|
151
|
+
const choice = result.choices[0];
|
|
152
|
+
if (choice.message && choice.message.content) {
|
|
153
|
+
return schemaStrategy.processResponse(choice.message.content);
|
|
154
|
+
}
|
|
155
|
+
if (choice.message && choice.message.function_call) {
|
|
156
|
+
return schemaStrategy.processResponse(choice.message.function_call);
|
|
157
|
+
}
|
|
158
|
+
if (choice.message && choice.message.tool_calls) {
|
|
159
|
+
return schemaStrategy.processResponse(choice.message.tool_calls);
|
|
160
|
+
}
|
|
161
|
+
if (choice.message && Array.isArray(choice.message.content)) {
|
|
162
|
+
let textContent = "";
|
|
163
|
+
let toolUse = null;
|
|
164
|
+
for (const block of choice.message.content) {
|
|
165
|
+
if (block.type === "text") {
|
|
166
|
+
textContent += block.text || "";
|
|
167
|
+
}
|
|
168
|
+
else if (block.type === "tool_use") {
|
|
169
|
+
toolUse = block;
|
|
170
|
+
break;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
if (toolUse) {
|
|
174
|
+
return schemaStrategy.processResponse(toolUse);
|
|
175
|
+
}
|
|
176
|
+
return schemaStrategy.processResponse(textContent);
|
|
177
|
+
}
|
|
178
|
+
if (choice.text) {
|
|
179
|
+
return schemaStrategy.processResponse(choice.text);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
if (typeof result !== "string") {
|
|
183
|
+
throw new Error(`Failed to extract content from API response: ${JSON.stringify(result)}`);
|
|
184
|
+
}
|
|
185
|
+
return result;
|
|
186
|
+
}
|
|
187
|
+
async function extractClaudeResponse(response) {
|
|
188
|
+
try {
|
|
189
|
+
const timeoutPromise = new Promise((_, reject) => {
|
|
190
|
+
setTimeout(() => {
|
|
191
|
+
reject(new Error("Timeout extracting Claude response"));
|
|
192
|
+
}, 5000);
|
|
193
|
+
});
|
|
194
|
+
const responsePromise = response.json();
|
|
195
|
+
const json = await Promise.race([responsePromise, timeoutPromise]);
|
|
196
|
+
if (json.choices && json.choices.length > 0 && json.choices[0].message && json.choices[0].message.content) {
|
|
197
|
+
return json.choices[0].message.content;
|
|
198
|
+
}
|
|
199
|
+
return json;
|
|
200
|
+
}
|
|
201
|
+
catch (error) {
|
|
202
|
+
throw new Error(`Failed to extract Claude response: ${error instanceof Error ? error.message : String(error)}`);
|
|
203
|
+
}
|
|
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;AAGzC,KAAK,UAAU,kBAAkB,CAAC,MAA0B,EAAE,OAAO,GAAkB,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@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "call-ai",
|
|
3
|
+
"version": "0.0.0-dev-prompts",
|
|
4
|
+
"description": "Lightweight library for making AI API calls with streaming support",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/fireproof-storage/call-ai.git"
|
|
9
|
+
},
|
|
10
|
+
"homepage": "https://github.com/fireproof-storage/call-ai",
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/fireproof-storage/call-ai/issues"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"ai",
|
|
16
|
+
"llm",
|
|
17
|
+
"api",
|
|
18
|
+
"call",
|
|
19
|
+
"openai",
|
|
20
|
+
"streaming",
|
|
21
|
+
"openrouter"
|
|
22
|
+
],
|
|
23
|
+
"contributors": [
|
|
24
|
+
"J Chris Anderson",
|
|
25
|
+
"Meno Abels"
|
|
26
|
+
],
|
|
27
|
+
"license": "Apache-2.0",
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@fireproof/core-cli": "^0.23.11",
|
|
30
|
+
"@types/node": "^24.0.15",
|
|
31
|
+
"typescript": "^5.8.3"
|
|
32
|
+
},
|
|
33
|
+
"engines": {
|
|
34
|
+
"node": ">=20.0.0"
|
|
35
|
+
},
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"@adviser/cement": "^0.4.30",
|
|
38
|
+
"@fireproof/core-runtime": "^0.23.13"
|
|
39
|
+
},
|
|
40
|
+
"scripts": {
|
|
41
|
+
"build": "core-cli tsc"
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -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;
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { isToolUseType, } from "../types.js";
|
|
2
|
+
import { recursivelyAddAdditionalProperties } from "../utils.js";
|
|
3
|
+
export const openAIStrategy = {
|
|
4
|
+
name: "openai",
|
|
5
|
+
prepareRequest: (schema) => {
|
|
6
|
+
if (!schema)
|
|
7
|
+
throw new Error("Schema strategy not implemented");
|
|
8
|
+
const requiredFields = schema.required || Object.keys(schema.properties || {});
|
|
9
|
+
const processedSchema = recursivelyAddAdditionalProperties({
|
|
10
|
+
type: "object",
|
|
11
|
+
properties: schema.properties || {},
|
|
12
|
+
required: requiredFields,
|
|
13
|
+
additionalProperties: schema.additionalProperties !== undefined ? schema.additionalProperties : false,
|
|
14
|
+
...Object.fromEntries(Object.entries(schema).filter(([key]) => !["name", "properties", "required", "additionalProperties"].includes(key))),
|
|
15
|
+
});
|
|
16
|
+
return {
|
|
17
|
+
response_format: {
|
|
18
|
+
type: "json_schema",
|
|
19
|
+
json_schema: {
|
|
20
|
+
name: schema.name || "result",
|
|
21
|
+
strict: true,
|
|
22
|
+
schema: processedSchema,
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
};
|
|
26
|
+
},
|
|
27
|
+
processResponse: (content) => {
|
|
28
|
+
if (typeof content !== "string") {
|
|
29
|
+
return JSON.stringify(content);
|
|
30
|
+
}
|
|
31
|
+
return content;
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
export const geminiStrategy = {
|
|
35
|
+
name: "gemini",
|
|
36
|
+
prepareRequest: openAIStrategy.prepareRequest,
|
|
37
|
+
processResponse: (content) => {
|
|
38
|
+
if (typeof content !== "string") {
|
|
39
|
+
return JSON.stringify(content);
|
|
40
|
+
}
|
|
41
|
+
const jsonMatch = content.match(/```json\s*([\s\S]*?)\s*```/) ||
|
|
42
|
+
content.match(/```\s*([\s\S]*?)\s*```/) ||
|
|
43
|
+
content.match(/\{[\s\S]*\}/) || [null, content];
|
|
44
|
+
return jsonMatch[1] || content;
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
export const claudeStrategy = {
|
|
48
|
+
name: "anthropic",
|
|
49
|
+
shouldForceStream: true,
|
|
50
|
+
prepareRequest: (schema) => {
|
|
51
|
+
if (!schema)
|
|
52
|
+
throw new Error("Schema strategy not implemented");
|
|
53
|
+
const processedSchema = {
|
|
54
|
+
type: "object",
|
|
55
|
+
properties: schema.properties || {},
|
|
56
|
+
required: schema.required || Object.keys(schema.properties || {}),
|
|
57
|
+
additionalProperties: schema.additionalProperties !== undefined ? schema.additionalProperties : false,
|
|
58
|
+
};
|
|
59
|
+
return {
|
|
60
|
+
tools: [
|
|
61
|
+
{
|
|
62
|
+
type: "function",
|
|
63
|
+
function: {
|
|
64
|
+
name: schema.name || "generate_structured_data",
|
|
65
|
+
description: "Generate data according to the required schema",
|
|
66
|
+
parameters: processedSchema,
|
|
67
|
+
},
|
|
68
|
+
},
|
|
69
|
+
],
|
|
70
|
+
tool_choice: {
|
|
71
|
+
type: "function",
|
|
72
|
+
function: {
|
|
73
|
+
name: schema.name || "generate_structured_data",
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
},
|
|
78
|
+
processResponse: (content) => {
|
|
79
|
+
if (isToolUseType(content)) {
|
|
80
|
+
if (content.type === "tool_use") {
|
|
81
|
+
return JSON.stringify(content.input);
|
|
82
|
+
}
|
|
83
|
+
if (content.tool_calls && Array.isArray(content.tool_calls) && content.tool_calls.length > 0) {
|
|
84
|
+
const toolCall = content.tool_calls[0];
|
|
85
|
+
if (toolCall.function && toolCall.function.arguments) {
|
|
86
|
+
return JSON.stringify(toolCall.function.arguments);
|
|
87
|
+
// }
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return JSON.stringify(content);
|
|
91
|
+
}
|
|
92
|
+
if (typeof content !== "string") {
|
|
93
|
+
return JSON.stringify(content);
|
|
94
|
+
}
|
|
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;
|
|
97
|
+
},
|
|
98
|
+
};
|
|
99
|
+
export const systemMessageStrategy = {
|
|
100
|
+
name: "system_message",
|
|
101
|
+
prepareRequest: (schema, messages) => {
|
|
102
|
+
if (!schema)
|
|
103
|
+
return { messages };
|
|
104
|
+
const hasSystemMessage = messages.some((m) => m.role === "system");
|
|
105
|
+
if (!hasSystemMessage) {
|
|
106
|
+
const schemaProperties = Object.entries(schema.properties || {})
|
|
107
|
+
.map(([key, value]) => {
|
|
108
|
+
const type = value.type || "string";
|
|
109
|
+
const description = value.description ? ` // ${value.description}` : "";
|
|
110
|
+
return ` "${key}": ${type}${description}`;
|
|
111
|
+
})
|
|
112
|
+
.join(",\n");
|
|
113
|
+
const systemMessage = {
|
|
114
|
+
role: "system",
|
|
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.`,
|
|
116
|
+
};
|
|
117
|
+
return { messages: [systemMessage, ...messages] };
|
|
118
|
+
}
|
|
119
|
+
return { messages };
|
|
120
|
+
},
|
|
121
|
+
processResponse: (content) => {
|
|
122
|
+
if (typeof content !== "string") {
|
|
123
|
+
return JSON.stringify(content);
|
|
124
|
+
}
|
|
125
|
+
const jsonMatch = content.match(/```json\s*([\s\S]*?)\s*```/) ||
|
|
126
|
+
content.match(/```\s*([\s\S]*?)\s*```/) ||
|
|
127
|
+
content.match(/\{[\s\S]*\}/) || [null, content];
|
|
128
|
+
return jsonMatch[1] || content;
|
|
129
|
+
},
|
|
130
|
+
};
|
|
131
|
+
export const defaultStrategy = {
|
|
132
|
+
name: "default",
|
|
133
|
+
prepareRequest: () => {
|
|
134
|
+
throw new Error("Schema strategy not implemented");
|
|
135
|
+
},
|
|
136
|
+
processResponse: (content) => (typeof content === "string" ? content : JSON.stringify(content)),
|
|
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,M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KAAK,CAAC,aAAa,CAAC,IAAI,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;QAElD,OAAO,SAAS,CAAC,CAAC,CAAC,IAAI,OAAO,CAAC;IAAA,CAChC;CACF,CAAC;AAKF,MAAM,CAAC,MAAM,eAAe,GAAkB;IAC5C,IAAI,EAAE,SAAS;IACf,cAAc,EAAE,GAAG,EAAE,CAAC;QACpB,MAAM,IAAI,KAAK,CAAC,iCAAiC,CAAC,CAAC;IAAA,CACpD;IACD,eAAe,EAAE,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,OAAO,OAAO,KAAK,QAAQ,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;CAChG,CAAC"}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { claudeStrategy, defaultStrategy, geminiStrategy, openAIStrategy, systemMessageStrategy } from "./model-strategies.js";
|
|
2
|
+
export function chooseSchemaStrategy(model, schema) {
|
|
3
|
+
const resolvedModel = model || (schema ? "openai/gpt-4o" : "openrouter/auto");
|
|
4
|
+
if (!schema) {
|
|
5
|
+
return {
|
|
6
|
+
strategy: "none",
|
|
7
|
+
model: resolvedModel,
|
|
8
|
+
prepareRequest: defaultStrategy.prepareRequest,
|
|
9
|
+
processResponse: defaultStrategy.processResponse,
|
|
10
|
+
shouldForceStream: false,
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
if (/claude/i.test(resolvedModel)) {
|
|
14
|
+
return {
|
|
15
|
+
strategy: "tool_mode",
|
|
16
|
+
model: resolvedModel,
|
|
17
|
+
prepareRequest: claudeStrategy.prepareRequest,
|
|
18
|
+
processResponse: claudeStrategy.processResponse,
|
|
19
|
+
shouldForceStream: !!claudeStrategy.shouldForceStream,
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
if (/gemini/i.test(resolvedModel)) {
|
|
23
|
+
return {
|
|
24
|
+
strategy: "json_schema",
|
|
25
|
+
model: resolvedModel,
|
|
26
|
+
prepareRequest: geminiStrategy.prepareRequest,
|
|
27
|
+
processResponse: geminiStrategy.processResponse,
|
|
28
|
+
shouldForceStream: !!geminiStrategy.shouldForceStream,
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
if (/gpt-4-turbo/i.test(resolvedModel)) {
|
|
32
|
+
return {
|
|
33
|
+
strategy: "system_message",
|
|
34
|
+
model: resolvedModel,
|
|
35
|
+
prepareRequest: systemMessageStrategy.prepareRequest,
|
|
36
|
+
processResponse: systemMessageStrategy.processResponse,
|
|
37
|
+
shouldForceStream: !!systemMessageStrategy.shouldForceStream,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
if (/openai|gpt/i.test(resolvedModel)) {
|
|
41
|
+
return {
|
|
42
|
+
strategy: "json_schema",
|
|
43
|
+
model: resolvedModel,
|
|
44
|
+
prepareRequest: openAIStrategy.prepareRequest,
|
|
45
|
+
processResponse: openAIStrategy.processResponse,
|
|
46
|
+
shouldForceStream: !!openAIStrategy.shouldForceStream,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
if (/llama-3|deepseek/i.test(resolvedModel)) {
|
|
50
|
+
return {
|
|
51
|
+
strategy: "system_message",
|
|
52
|
+
model: resolvedModel,
|
|
53
|
+
prepareRequest: systemMessageStrategy.prepareRequest,
|
|
54
|
+
processResponse: systemMessageStrategy.processResponse,
|
|
55
|
+
shouldForceStream: !!systemMessageStrategy.shouldForceStream,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
return {
|
|
59
|
+
strategy: "system_message",
|
|
60
|
+
model: resolvedModel,
|
|
61
|
+
prepareRequest: systemMessageStrategy.prepareRequest,
|
|
62
|
+
processResponse: systemMessageStrategy.processResponse,
|
|
63
|
+
shouldForceStream: !!systemMessageStrategy.shouldForceStream,
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
//# sourceMappingURL=strategy-selector.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
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AE,aAAa;YACpB,cAAc,EAAE,qBAAqB,CAAC,cAAc;YACpD,eAAe,EAAE,qBAAqB,CAAC,eAAe;YACtD,iBAAiB,EAAE,CAAC,CAAC,qBAAqB,CAAC,iBAAiB;SAC7D,CAAC;IACJ,CAAC;IAGD,IAAI,aAAa,CAAC,IAAI,CAAC,aAAa,CAAC,EAAE,CAAC;QACtC,OAAO;YACL,QAAQ,EAAE,aAAa;YACvB,KAAK,EAAE,aAAa;YACpB,cAAc,EAAE,cAAc,CAAC,cAAc;YAC7C,eAAe,EAAE,cAAc,CAAC,eAAe;YAC/C,iBAAiB,EAAE,CAAC,CAAC,cAAc,CAAC,iBAAiB;SACtD,CAAC;IACJ,CAAC;IAGD,IAAI,mBAAmB,CAAC,IAAI,CAAC,aAAa,CAAC,EAAE,CAAC;QAC5C,OAAO;YACL,QAAQ,EAAE,gBAAgB;YAC1B,KAAK,EAAE,aAAa;YACpB,cAAc,EAAE,qBAAqB,CAAC,cAAc;YACpD,eAAe,EAAE,qBAAqB,CAAC,eAAe;YACtD,iBAAiB,EAAE,CAAC,CAAC,qBAAqB,CAAC,iBAAiB;SAC7D,CAAC;IACJ,CAAC;IAGD,OAAO;QACL,QAAQ,EAAE,gBAAgB;QAC1B,KAAK,EAAE,aAAa;QACpB,cAAc,EAAE,qBAAqB,CAAC,cAAc;QACpD,eAAe,EAAE,qBAAqB,CAAC,eAAe;QACtD,iBAAiB,EAAE,CAAC,CAAC,qBAAqB,CAAC,iBAAiB;KAC7D,CAAC;AAAA,CACH"}
|
package/streaming.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { CallAIOptions, Message, SchemaStrategy } from "./types.js";
|
|
2
|
+
declare function createStreamingGenerator(response: Response, options: CallAIOptions, schemaStrategy: SchemaStrategy, model: string): AsyncGenerator<string, string, unknown>;
|
|
3
|
+
declare function callAIStreaming(prompt: string | Message[], options?: CallAIOptions, isRetry?: boolean): AsyncGenerator<string, string, unknown>;
|
|
4
|
+
export { createStreamingGenerator, callAIStreaming };
|