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
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Mocks } from "./types.js";
|
|
2
|
+
declare function handleApiError(ierror: unknown, context: string, debug?: boolean, options?: {
|
|
3
|
+
apiKey?: string;
|
|
4
|
+
endpoint?: string;
|
|
5
|
+
skipRefresh?: boolean;
|
|
6
|
+
refreshToken?: string;
|
|
7
|
+
updateRefreshToken?: (currentToken: string) => Promise<string>;
|
|
8
|
+
mock?: Mocks;
|
|
9
|
+
}): Promise<void>;
|
|
10
|
+
declare function checkForInvalidModelError(response: Response, model: string, debug?: boolean): Promise<{
|
|
11
|
+
isInvalidModel: boolean;
|
|
12
|
+
errorData?: unknown;
|
|
13
|
+
}>;
|
|
14
|
+
export { handleApiError, checkForInvalidModelError };
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { keyStore, globalDebug, isNewKeyError, refreshApiKey } from "./key-management.js";
|
|
2
|
+
import { CallAIError } from "./types.js";
|
|
3
|
+
async function handleApiError(ierror, context, debug = globalDebug, options = {}) {
|
|
4
|
+
const error = ierror;
|
|
5
|
+
const errorMessage = error?.message || String(error);
|
|
6
|
+
const status = error?.status ||
|
|
7
|
+
error?.statusCode ||
|
|
8
|
+
error?.response?.status ||
|
|
9
|
+
(errorMessage.match(/status: (\d+)/i)?.[1] && parseInt(errorMessage.match(/status: (\d+)/i)?.[1] ?? "500"));
|
|
10
|
+
const isMissingKeyError = errorMessage.includes("API key is required");
|
|
11
|
+
if (debug) {
|
|
12
|
+
console.error(`[callAi:error] ${context} error:`, {
|
|
13
|
+
message: errorMessage,
|
|
14
|
+
status,
|
|
15
|
+
name: error?.name,
|
|
16
|
+
cause: error?.cause,
|
|
17
|
+
isMissingKey: isMissingKeyError,
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
if (options.skipRefresh) {
|
|
21
|
+
throw error;
|
|
22
|
+
}
|
|
23
|
+
const needsNewKey = isNewKeyError(error, debug) || isMissingKeyError;
|
|
24
|
+
if (needsNewKey) {
|
|
25
|
+
if (debug) {
|
|
26
|
+
console.log(`[callAi:key-refresh] Error suggests API key issue, attempting refresh...`);
|
|
27
|
+
}
|
|
28
|
+
try {
|
|
29
|
+
const currentKey = options.apiKey || keyStore().current;
|
|
30
|
+
const endpoint = options.endpoint || keyStore().refreshEndpoint;
|
|
31
|
+
const refreshToken = options.refreshToken || keyStore().refreshToken;
|
|
32
|
+
try {
|
|
33
|
+
const { apiKey, topup } = await refreshApiKey(options, currentKey, endpoint, refreshToken, debug);
|
|
34
|
+
if (keyStore().current !== apiKey) {
|
|
35
|
+
keyStore().current = apiKey;
|
|
36
|
+
}
|
|
37
|
+
if (debug) {
|
|
38
|
+
console.log(`[callAi:key-refresh] ${topup ? "Topped up" : "Refreshed"} API key successfully`);
|
|
39
|
+
}
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
catch (initialRefreshError) {
|
|
43
|
+
if (options.updateRefreshToken && refreshToken) {
|
|
44
|
+
if (debug) {
|
|
45
|
+
console.log(`[callAi:key-refresh] Initial refresh failed, attempting to update refresh token`);
|
|
46
|
+
}
|
|
47
|
+
try {
|
|
48
|
+
const newRefreshToken = await options.updateRefreshToken(refreshToken);
|
|
49
|
+
if (newRefreshToken && newRefreshToken !== refreshToken) {
|
|
50
|
+
if (debug) {
|
|
51
|
+
console.log(`[callAi:key-refresh] Got new refresh token, retrying key refresh`);
|
|
52
|
+
}
|
|
53
|
+
keyStore().refreshToken = newRefreshToken;
|
|
54
|
+
const { apiKey, topup } = await refreshApiKey(options, currentKey, endpoint, newRefreshToken, debug);
|
|
55
|
+
if (keyStore().current !== apiKey) {
|
|
56
|
+
keyStore().current = apiKey;
|
|
57
|
+
}
|
|
58
|
+
if (debug) {
|
|
59
|
+
console.log(`[callAi:key-refresh] ${topup ? "Topped up" : "Refreshed"} API key successfully with new refresh token`);
|
|
60
|
+
}
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
if (debug) {
|
|
65
|
+
console.log(`[callAi:key-refresh] No new refresh token provided or same token returned, cannot retry`);
|
|
66
|
+
}
|
|
67
|
+
throw initialRefreshError;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
catch (tokenUpdateError) {
|
|
71
|
+
if (debug) {
|
|
72
|
+
console.error(`[callAi:key-refresh] Failed to update refresh token:`, tokenUpdateError);
|
|
73
|
+
}
|
|
74
|
+
throw initialRefreshError;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
throw initialRefreshError;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
catch (refreshError) {
|
|
83
|
+
if (debug) {
|
|
84
|
+
console.error(`[callAi:key-refresh] API key refresh failed:`, refreshError);
|
|
85
|
+
}
|
|
86
|
+
const detailedError = new CallAIError({
|
|
87
|
+
message: `${errorMessage} (Key refresh failed: ${refreshError instanceof Error ? refreshError.message : String(refreshError)})`,
|
|
88
|
+
originalError: error,
|
|
89
|
+
refreshError,
|
|
90
|
+
status: status || 401,
|
|
91
|
+
contentType: "text/plain",
|
|
92
|
+
});
|
|
93
|
+
throw detailedError;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
const detailedError = new CallAIError({
|
|
97
|
+
message: `${context}: ${errorMessage}`,
|
|
98
|
+
originalError: error,
|
|
99
|
+
status: status || 500,
|
|
100
|
+
errorType: error.name || "Error",
|
|
101
|
+
});
|
|
102
|
+
throw detailedError;
|
|
103
|
+
}
|
|
104
|
+
async function checkForInvalidModelError(response, model, debug = globalDebug) {
|
|
105
|
+
if (response.status < 400 || response.status >= 500) {
|
|
106
|
+
return { isInvalidModel: false };
|
|
107
|
+
}
|
|
108
|
+
const responseClone = response.clone();
|
|
109
|
+
let errorData;
|
|
110
|
+
try {
|
|
111
|
+
errorData = await responseClone.json();
|
|
112
|
+
}
|
|
113
|
+
catch (e) {
|
|
114
|
+
try {
|
|
115
|
+
const text = await responseClone.text();
|
|
116
|
+
errorData = { error: text };
|
|
117
|
+
}
|
|
118
|
+
catch (e) {
|
|
119
|
+
errorData = { error: `Error ${response.status}: ${response.statusText}` };
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
const isInvalidModelError = response.status === 404 ||
|
|
123
|
+
response.status === 400 ||
|
|
124
|
+
(errorData &&
|
|
125
|
+
((typeof errorData.error === "string" &&
|
|
126
|
+
(errorData.error.toLowerCase().includes("model") ||
|
|
127
|
+
errorData.error.toLowerCase().includes("engine") ||
|
|
128
|
+
errorData.error.toLowerCase().includes("not found") ||
|
|
129
|
+
errorData.error.toLowerCase().includes("invalid") ||
|
|
130
|
+
errorData.error.toLowerCase().includes("unavailable"))) ||
|
|
131
|
+
(errorData.error?.message &&
|
|
132
|
+
typeof errorData.error.message === "string" &&
|
|
133
|
+
(errorData.error.message.toLowerCase().includes("model") ||
|
|
134
|
+
errorData.error.message.toLowerCase().includes("engine") ||
|
|
135
|
+
errorData.error.message.toLowerCase().includes("not found") ||
|
|
136
|
+
errorData.error.message.toLowerCase().includes("invalid") ||
|
|
137
|
+
errorData.error.message.toLowerCase().includes("unavailable")))));
|
|
138
|
+
if (debug && isInvalidModelError) {
|
|
139
|
+
console.log(`[callAi:model-fallback] Detected invalid model error for "${model}":`, errorData);
|
|
140
|
+
}
|
|
141
|
+
return { isInvalidModel: isInvalidModelError, errorData };
|
|
142
|
+
}
|
|
143
|
+
export { handleApiError, checkForInvalidModelError };
|
|
144
|
+
//# sourceMappingURL=error-handling.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"error-handling.js","sourceRoot":"","sources":["../jsr/error-handling.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,QAAQ,EAAE,WAAW,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAC1F,OAAO,EAAE,WAAW,EAA4B,MAAM,YAAY,CAAC;AAOnE,KAAK,UAAU,cAAc,CAC3B,MAAe,EACf,OAAe,EACf,KAAK,GAAY,WAAW,EAC5B,OAAO,GAOH,EAAE,EACS;IACf,MAAM,KAAK,GAAG,MAA2B,CAAC;IAG1C,MAAM,YAAY,G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CAAC,WAAW,EAAE,CAAC,QAAQ,CAAC,SAAS,CAAC;oBACjD,SAAS,CAAC,KAAK,CAAC,WAAW,EAAE,CAAC,QAAQ,CAAC,aAAa,CAAC,CAAC,CAAC;gBACzD,CAAC,SAAS,CAAC,KAAK,EAAE,OAAO;oBACvB,OAAO,SAAS,CAAC,KAAK,CAAC,OAAO,KAAK,QAAQ;oBAC3C,CAAC,SAAS,CAAC,KAAK,CAAC,OAAO,CAAC,WAAW,EAAE,CAAC,QAAQ,CAAC,OAAO,CAAC;wBACtD,SAAS,CAAC,KAAK,CAAC,OAAO,CAAC,WAAW,EAAE,CAAC,QAAQ,CAAC,QAAQ,CAAC;wBACxD,SAAS,CAAC,KAAK,CAAC,OAAO,CAAC,WAAW,EAAE,CAAC,QAAQ,CAAC,WAAW,CAAC;wBAC3D,SAAS,CAAC,KAAK,CAAC,OAAO,CAAC,WAAW,EAAE,CAAC,QAAQ,CAAC,SAAS,CAAC;wBACzD,SAAS,CAAC,KAAK,CAAC,OAAO,CAAC,WAAW,EAAE,CAAC,QAAQ,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IAE5E,IAAI,KAAK,IAAI,mBAAmB,EAAE,CAAC;QACjC,OAAO,CAAC,GAAG,CAAC,6DAA6D,KAAK,IAAI,EAAE,SAAS,CAAC,CAAC;IACjG,CAAC;IAED,OAAO,EAAE,cAAc,EAAE,mBAAmB,EAAE,SAAS,EAAE,CAAC;AAAA,CAC3D;AAED,OAAO,EAAE,cAAc,EAAE,yBAAyB,EAAE,CAAC"}
|
package/image.d.ts
ADDED
package/image.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { callAiFetch } from "./utils.js";
|
|
2
|
+
import { callAiEnv } from "./env.js";
|
|
3
|
+
import { PACKAGE_VERSION } from "./version.js";
|
|
4
|
+
export async function imageGen(prompt, options = {}) {
|
|
5
|
+
const { model = "gpt-image-1", apiKey = "VIBES_DIY", debug = false, size = "1024x1024" } = options;
|
|
6
|
+
if (debug) {
|
|
7
|
+
console.log(`[imageGen:${PACKAGE_VERSION}] Generating image with prompt: ${prompt.substring(0, 50)}...`);
|
|
8
|
+
console.log(`[imageGen:${PACKAGE_VERSION}] Using model: ${model}`);
|
|
9
|
+
}
|
|
10
|
+
const customOrigin = options.imgUrl || callAiEnv.CALLAI_IMG_URL;
|
|
11
|
+
try {
|
|
12
|
+
if (!options.images || options.images.length === 0) {
|
|
13
|
+
const origin = customOrigin || (typeof document !== "undefined" ? document.location.origin : "");
|
|
14
|
+
const generateEndpoint = `${origin}/api/openai-image/generate`;
|
|
15
|
+
const response = await callAiFetch(options)(generateEndpoint, {
|
|
16
|
+
method: "POST",
|
|
17
|
+
headers: {
|
|
18
|
+
Authorization: `Bearer ${apiKey}`,
|
|
19
|
+
"Content-Type": "application/json",
|
|
20
|
+
},
|
|
21
|
+
body: JSON.stringify({
|
|
22
|
+
model,
|
|
23
|
+
prompt,
|
|
24
|
+
size,
|
|
25
|
+
...(options.quality && { quality: options.quality }),
|
|
26
|
+
...(options.style && { style: options.style }),
|
|
27
|
+
}),
|
|
28
|
+
});
|
|
29
|
+
if (!response.ok) {
|
|
30
|
+
const errorData = await response.text();
|
|
31
|
+
throw new Error(`Image generation failed: ${response.status} ${response.statusText} - ${errorData}`);
|
|
32
|
+
}
|
|
33
|
+
const result = await response.json();
|
|
34
|
+
return result;
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
const formData = new FormData();
|
|
38
|
+
formData.append("model", model);
|
|
39
|
+
formData.append("prompt", prompt);
|
|
40
|
+
options.images.forEach((image, index) => {
|
|
41
|
+
formData.append(`image_${index}`, image);
|
|
42
|
+
});
|
|
43
|
+
formData.append("size", size);
|
|
44
|
+
if (options.quality)
|
|
45
|
+
formData.append("quality", options.quality);
|
|
46
|
+
if (options.style)
|
|
47
|
+
formData.append("style", options.style);
|
|
48
|
+
const origin = customOrigin || (typeof document !== "undefined" ? document.location.origin : "");
|
|
49
|
+
const editEndpoint = `${origin}/api/openai-image/edit`;
|
|
50
|
+
const response = await callAiFetch(options)(editEndpoint, {
|
|
51
|
+
method: "POST",
|
|
52
|
+
headers: {
|
|
53
|
+
Authorization: `Bearer ${apiKey}`,
|
|
54
|
+
},
|
|
55
|
+
body: formData,
|
|
56
|
+
});
|
|
57
|
+
if (!response.ok) {
|
|
58
|
+
const errorData = await response.text();
|
|
59
|
+
throw new Error(`Image editing failed: ${response.status} ${response.statusText} - ${errorData}`);
|
|
60
|
+
}
|
|
61
|
+
const result = await response.json();
|
|
62
|
+
return result;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
catch (error) {
|
|
66
|
+
if (debug) {
|
|
67
|
+
console.error(`[imageGen:${PACKAGE_VERSION}] Error:`, error);
|
|
68
|
+
}
|
|
69
|
+
throw error;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
//# sourceMappingURL=image.js.map
|
package/image.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"image.js","sourceRoot":"","sources":["../jsr/image.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AACzC,OAAO,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AACrC,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAU/C,MAAM,CAAC,KAAK,UAAU,QAAQ,CAAC,MAAc,EAAE,OAAO,GAAoB,EAAE,EAA0B;IACpG,MAAM,EAAE,KAAK,GAAG,aAAa,EAAE,MAAM,GAAG,WAAW,EAAE,KAAK,GAAG,KAAK,EAAE,IAAI,GAAG,WAAW,EAAE,GAAG,OAAO,CAAC;IAEnG,IAAI,KAAK,EAAE,CAAC;QACV,OAAO,CAAC,GAAG,CAAC,aAAa,eAAe,mCAAmC,MAAM,CAAC,SAAS,CAAC,CAAC,EAAE,EAAE,CAAC,KAAK,CAAC,CAAC;QACzG,OAAO,CAAC,GAAG,CAAC,aAAa,eAAe,kBAAkB,K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AAC,MAAM,IAAI,QAAQ,CAAC,UAAU,MAAM,SAAS,EAAE,CAAC,CAAC;YACpG,CAAC;YAED,MAAM,MAAM,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC;YACrC,OAAO,MAAM,CAAC;QAChB,CAAC;IACH,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,KAAK,EAAE,CAAC;YACV,OAAO,CAAC,KAAK,CAAC,aAAa,eAAe,UAAU,EAAE,KAAK,CAAC,CAAC;QAC/D,CAAC;QACD,MAAM,KAAK,CAAC;IACd,CAAC;AAAA,CACF"}
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export * from "./types.js";
|
|
2
|
+
export { callAi } from "./api.js";
|
|
3
|
+
export { callAi as callAI } from "./api.js";
|
|
4
|
+
export { getMeta } from "./response-metadata.js";
|
|
5
|
+
export { imageGen } from "./image.js";
|
|
6
|
+
export { entriesHeaders } from "./utils.js";
|
|
7
|
+
export { callAiEnv } from "./env.js";
|
package/index.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export * from "./types.js";
|
|
2
|
+
export { callAi } from "./api.js";
|
|
3
|
+
export { callAi as callAI } from "./api.js";
|
|
4
|
+
export { getMeta } from "./response-metadata.js";
|
|
5
|
+
export { imageGen } from "./image.js";
|
|
6
|
+
export { entriesHeaders } from "./utils.js";
|
|
7
|
+
export { callAiEnv } from "./env.js";
|
|
8
|
+
//# sourceMappingURL=index.js.map
|
package/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../jsr/index.ts"],"names":[],"mappings":"AAKA,cAAc,YAAY,CAAC;AAG3B,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAElC,OAAO,EAAE,MAAM,IAAI,MAAM,EAAE,MAAM,UAAU,CAAC;AAE5C,OAAO,EAAE,OAAO,EAAE,MAAM,wBAAwB,CAAC;AAGjD,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAEtC,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAC5C,OAAO,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC"}
|
package/index.ts.bak
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* call-ai: A lightweight library for making AI API calls
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
// Export public types
|
|
6
|
+
export * from "./types";
|
|
7
|
+
|
|
8
|
+
// Export API functions
|
|
9
|
+
export { callAi, getMeta } from "./api";
|
|
10
|
+
|
|
11
|
+
// Export image generation function
|
|
12
|
+
export { imageGen } from "./image";
|
|
13
|
+
|
|
14
|
+
// Export strategies and utilities for advanced use cases
|
|
15
|
+
export * from "./strategies";
|
|
16
|
+
export * from "./utils";
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { Falsy, Mocks } from "./types.js";
|
|
2
|
+
export interface KeyMetadata {
|
|
3
|
+
key: string;
|
|
4
|
+
hash: string;
|
|
5
|
+
created: Date;
|
|
6
|
+
expires: Date;
|
|
7
|
+
remaining: number;
|
|
8
|
+
limit: number;
|
|
9
|
+
}
|
|
10
|
+
export declare function keyStore(): {
|
|
11
|
+
current: string | undefined;
|
|
12
|
+
refreshEndpoint: string;
|
|
13
|
+
refreshToken: string | 0 | false | null | undefined;
|
|
14
|
+
isRefreshing: boolean;
|
|
15
|
+
lastRefreshAttempt: number;
|
|
16
|
+
metadata: Record<string, Partial<KeyMetadata>>;
|
|
17
|
+
};
|
|
18
|
+
declare let globalDebug: boolean;
|
|
19
|
+
declare function initKeyStore(): void;
|
|
20
|
+
declare function isNewKeyError(ierror: unknown, debug?: boolean): boolean;
|
|
21
|
+
declare function refreshApiKey(options: {
|
|
22
|
+
mock?: Mocks;
|
|
23
|
+
}, currentKey: string | Falsy, endpoint: string | Falsy, refreshToken: string | Falsy, debug?: boolean): Promise<{
|
|
24
|
+
apiKey: string;
|
|
25
|
+
topup: boolean;
|
|
26
|
+
}>;
|
|
27
|
+
declare function getHashFromKey(key: string): string | null;
|
|
28
|
+
declare function storeKeyMetadata(data: KeyMetadata): void;
|
|
29
|
+
export { globalDebug, initKeyStore, isNewKeyError, refreshApiKey, getHashFromKey, storeKeyMetadata };
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import { callAiFetch, entriesHeaders } from "./utils.js";
|
|
2
|
+
import { callAiEnv } from "./env.js";
|
|
3
|
+
export function keyStore() {
|
|
4
|
+
return {
|
|
5
|
+
current: undefined,
|
|
6
|
+
refreshEndpoint: "https://vibecode.garden",
|
|
7
|
+
refreshToken: "use-vibes",
|
|
8
|
+
isRefreshing: false,
|
|
9
|
+
lastRefreshAttempt: 0,
|
|
10
|
+
metadata: {},
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
let globalDebug = false;
|
|
14
|
+
function initKeyStore() {
|
|
15
|
+
keyStore().current = callAiEnv.CALLAI_API_KEY;
|
|
16
|
+
keyStore().refreshEndpoint = callAiEnv.CALLAI_REFRESH_ENDPOINT ?? "https://vibecode.garden";
|
|
17
|
+
keyStore().refreshToken = callAiEnv.CALL_AI_REFRESH_TOKEN ?? "use-vibes";
|
|
18
|
+
globalDebug = !!callAiEnv.CALLAI_DEBUG;
|
|
19
|
+
}
|
|
20
|
+
function isNewKeyError(ierror, debug = false) {
|
|
21
|
+
const error = ierror;
|
|
22
|
+
let status = error?.status || error?.statusCode || error?.response?.status || 450;
|
|
23
|
+
const errorMessage = String(error || "").toLowerCase();
|
|
24
|
+
if (!status && errorMessage.includes("status:")) {
|
|
25
|
+
const statusMatch = errorMessage.match(/status:\\s*(\\d+)/i);
|
|
26
|
+
if (statusMatch && statusMatch[1]) {
|
|
27
|
+
status = parseInt(statusMatch[1], 10);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
const is4xx = status >= 400 && status < 500;
|
|
31
|
+
const isAuthError = status === 401 ||
|
|
32
|
+
status === 403 ||
|
|
33
|
+
errorMessage.includes("unauthorized") ||
|
|
34
|
+
errorMessage.includes("forbidden") ||
|
|
35
|
+
errorMessage.includes("authentication") ||
|
|
36
|
+
errorMessage.includes("api key") ||
|
|
37
|
+
errorMessage.includes("apikey") ||
|
|
38
|
+
errorMessage.includes("auth");
|
|
39
|
+
const isInvalidKeyError = errorMessage.includes("invalid api key") ||
|
|
40
|
+
errorMessage.includes("invalid key") ||
|
|
41
|
+
errorMessage.includes("incorrect api key") ||
|
|
42
|
+
errorMessage.includes("incorrect key") ||
|
|
43
|
+
errorMessage.includes("authentication failed") ||
|
|
44
|
+
errorMessage.includes("not authorized");
|
|
45
|
+
const isOpenAIKeyError = errorMessage.includes("openai") && (errorMessage.includes("api key") || errorMessage.includes("authentication"));
|
|
46
|
+
const isRateLimitError = status === 429 ||
|
|
47
|
+
errorMessage.includes("rate limit") ||
|
|
48
|
+
errorMessage.includes("too many requests") ||
|
|
49
|
+
errorMessage.includes("quota") ||
|
|
50
|
+
errorMessage.includes("exceed");
|
|
51
|
+
const isBillingError = errorMessage.includes("billing") ||
|
|
52
|
+
errorMessage.includes("payment") ||
|
|
53
|
+
errorMessage.includes("subscription") ||
|
|
54
|
+
errorMessage.includes("account");
|
|
55
|
+
const needsNewKey = is4xx && (isAuthError || isInvalidKeyError || isOpenAIKeyError || isRateLimitError || isBillingError);
|
|
56
|
+
if (debug && needsNewKey) {
|
|
57
|
+
console.log(`[callAi:key-refresh] Detected error requiring key refresh: ${errorMessage}`);
|
|
58
|
+
}
|
|
59
|
+
return needsNewKey;
|
|
60
|
+
}
|
|
61
|
+
async function refreshApiKey(options, currentKey, endpoint, refreshToken, debug = globalDebug) {
|
|
62
|
+
if (!endpoint) {
|
|
63
|
+
throw new Error("No API key refresh endpoint specified");
|
|
64
|
+
}
|
|
65
|
+
if (!refreshToken) {
|
|
66
|
+
throw new Error("No API key refresh token specified");
|
|
67
|
+
}
|
|
68
|
+
if (keyStore().isRefreshing) {
|
|
69
|
+
if (debug) {
|
|
70
|
+
console.log("API key refresh already in progress, waiting...");
|
|
71
|
+
}
|
|
72
|
+
return new Promise((resolve) => {
|
|
73
|
+
const checkInterval = setInterval(() => {
|
|
74
|
+
if (!keyStore().isRefreshing && keyStore().current) {
|
|
75
|
+
clearInterval(checkInterval);
|
|
76
|
+
const ck = keyStore().current;
|
|
77
|
+
if (!ck) {
|
|
78
|
+
throw new Error("API key refresh failed");
|
|
79
|
+
}
|
|
80
|
+
resolve({ apiKey: ck, topup: false });
|
|
81
|
+
}
|
|
82
|
+
}, 100);
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
const now = Date.now();
|
|
86
|
+
const timeSinceLastRefresh = now - keyStore().lastRefreshAttempt;
|
|
87
|
+
const minRefreshInterval = 2000;
|
|
88
|
+
if (timeSinceLastRefresh < minRefreshInterval) {
|
|
89
|
+
if (debug) {
|
|
90
|
+
console.log(`Rate limiting key refresh, last attempt was ${timeSinceLastRefresh}ms ago`);
|
|
91
|
+
}
|
|
92
|
+
await new Promise((resolve) => setTimeout(resolve, minRefreshInterval - timeSinceLastRefresh));
|
|
93
|
+
}
|
|
94
|
+
keyStore().isRefreshing = true;
|
|
95
|
+
keyStore().lastRefreshAttempt = Date.now();
|
|
96
|
+
const apiPath = "/api/keys";
|
|
97
|
+
const baseUrl = endpoint.endsWith("/") ? endpoint.slice(0, -1) : endpoint;
|
|
98
|
+
const url = `${baseUrl}${apiPath}`;
|
|
99
|
+
if (debug) {
|
|
100
|
+
console.log(`Refreshing API key from: ${url}`);
|
|
101
|
+
}
|
|
102
|
+
try {
|
|
103
|
+
const requestPayload = {
|
|
104
|
+
key: currentKey,
|
|
105
|
+
hash: currentKey ? getHashFromKey(currentKey) : null,
|
|
106
|
+
name: "call-ai-client",
|
|
107
|
+
};
|
|
108
|
+
if (debug) {
|
|
109
|
+
console.log(`[callAi:key-refresh] Request URL: ${url}`);
|
|
110
|
+
console.log(`[callAi:key-refresh] Request headers:`, {
|
|
111
|
+
"Content-Type": "application/json",
|
|
112
|
+
Authorization: `Bearer ${refreshToken}`,
|
|
113
|
+
});
|
|
114
|
+
console.log(`[callAi:key-refresh] Request payload:`, requestPayload);
|
|
115
|
+
}
|
|
116
|
+
const response = await callAiFetch(options)(url, {
|
|
117
|
+
method: "POST",
|
|
118
|
+
headers: {
|
|
119
|
+
"Content-Type": "application/json",
|
|
120
|
+
Authorization: `Bearer ${refreshToken}`,
|
|
121
|
+
},
|
|
122
|
+
body: JSON.stringify(requestPayload),
|
|
123
|
+
});
|
|
124
|
+
if (debug) {
|
|
125
|
+
console.log(`[callAi:key-refresh] Response status: ${response.status} ${response.statusText}`);
|
|
126
|
+
console.log(`[callAi:key-refresh] Response headers:`, Object.fromEntries([...entriesHeaders(response.headers)]));
|
|
127
|
+
}
|
|
128
|
+
if (!response.ok) {
|
|
129
|
+
const errorText = await response.text();
|
|
130
|
+
if (debug) {
|
|
131
|
+
console.log(`[callAi:key-refresh] Error response body: ${errorText}`);
|
|
132
|
+
}
|
|
133
|
+
throw new Error(`API key refresh failed: ${response.status} ${response.statusText}${errorText ? ` - ${errorText}` : ""}`);
|
|
134
|
+
}
|
|
135
|
+
const data = await response.json();
|
|
136
|
+
if (debug) {
|
|
137
|
+
console.log(`[callAi:key-refresh] Full response structure:`, JSON.stringify(data, null, 2));
|
|
138
|
+
}
|
|
139
|
+
let newKey;
|
|
140
|
+
if (data.key && typeof data.key === "object" && data.key.key) {
|
|
141
|
+
newKey = data.key.key;
|
|
142
|
+
}
|
|
143
|
+
// Check for old format where data.key is the string key directly
|
|
144
|
+
else if (data.key && typeof data.key === "string") {
|
|
145
|
+
newKey = data.key;
|
|
146
|
+
}
|
|
147
|
+
// Handle error case
|
|
148
|
+
else {
|
|
149
|
+
throw new Error("Invalid response from key refresh endpoint: missing or malformed key");
|
|
150
|
+
}
|
|
151
|
+
if (debug) {
|
|
152
|
+
console.log(`API key refreshed successfully: ${newKey.substring(0, 10)}...`);
|
|
153
|
+
}
|
|
154
|
+
if (data.metadata || (data.key && typeof data.key === "object" && data.key.metadata)) {
|
|
155
|
+
const metadata = data.metadata || data.key.metadata;
|
|
156
|
+
storeKeyMetadata(metadata);
|
|
157
|
+
}
|
|
158
|
+
keyStore().current = newKey;
|
|
159
|
+
const hashValue = data.hash || (data.key && typeof data.key === "object" && data.key.hash);
|
|
160
|
+
const isTopup = currentKey && hashValue && hashValue === getHashFromKey(currentKey);
|
|
161
|
+
keyStore().isRefreshing = false;
|
|
162
|
+
return {
|
|
163
|
+
apiKey: newKey, // Return the string key, not the object
|
|
164
|
+
topup: isTopup,
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
catch (error) {
|
|
168
|
+
keyStore().isRefreshing = false;
|
|
169
|
+
throw error;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
function getHashFromKey(key) {
|
|
173
|
+
if (!key)
|
|
174
|
+
return null;
|
|
175
|
+
const metaKey = Object.keys(keyStore().metadata).find((k) => k === key);
|
|
176
|
+
return metaKey ? keyStore().metadata[metaKey].hash || null : null;
|
|
177
|
+
}
|
|
178
|
+
function storeKeyMetadata(data) {
|
|
179
|
+
if (!data || !data.key)
|
|
180
|
+
return;
|
|
181
|
+
keyStore().metadata[data.key] = {
|
|
182
|
+
hash: data.hash,
|
|
183
|
+
created: data.created || Date.now(),
|
|
184
|
+
expires: data.expires,
|
|
185
|
+
remaining: data.remaining,
|
|
186
|
+
limit: data.limit,
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
export { globalDebug, initKeyStore, isNewKeyError, refreshApiKey, getHashFromKey, storeKeyMetadata };
|
|
190
|
+
//# sourceMappingURL=key-management.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"key-management.js","sourceRoot":"","sources":["../jsr/key-management.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,WAAW,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AACzD,OAAO,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AAYrC,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CAAC,KAAK;KAClB,CAAC;AAAA,CACH;AAED,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,aAAa,EAAE,aAAa,EAAE,cAAc,EAAE,gBAAgB,EAAE,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { AIResult, CallAIOptions, Message, SchemaStrategy } from "./types.js";
|
|
2
|
+
import { PACKAGE_VERSION } from "./version.js";
|
|
3
|
+
declare const FALLBACK_MODEL = "openrouter/auto";
|
|
4
|
+
declare function callAINonStreaming(prompt: string | Message[], options?: CallAIOptions, isRetry?: boolean): Promise<string>;
|
|
5
|
+
declare function extractContent(result: AIResult, schemaStrategy: SchemaStrategy): string;
|
|
6
|
+
declare function extractClaudeResponse(response: Response): Promise<NonNullable<unknown>>;
|
|
7
|
+
export { callAINonStreaming, extractContent, extractClaudeResponse, PACKAGE_VERSION, FALLBACK_MODEL };
|