@yuva-devlab/ai-client 0.2.5 → 0.3.0
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/index.cjs +221 -14
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +130 -2
- package/dist/index.d.ts +130 -2
- package/dist/index.js +215 -15
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -16,6 +16,8 @@ var AI_ENDPOINTS = Object.freeze({
|
|
|
16
16
|
ANTHROPIC_DEFAULT_BASE_URL: "https://api.anthropic.com/v1",
|
|
17
17
|
/** Google Gemini Generative Language v1beta endpoint */
|
|
18
18
|
GOOGLE_GEMINI_DEFAULT_BASE_URL: "https://generativelanguage.googleapis.com/v1beta",
|
|
19
|
+
/** Cline API Gateway v1 chat completions endpoint */
|
|
20
|
+
CLINE_DEFAULT_BASE_URL: "https://api.cline.bot/api/v1",
|
|
19
21
|
/** Required Anthropic API version header value */
|
|
20
22
|
ANTHROPIC_VERSION_HEADER: "2023-06-01"
|
|
21
23
|
});
|
|
@@ -31,7 +33,9 @@ var AI_DEFAULT_MODELS = Object.freeze({
|
|
|
31
33
|
/** Default OpenAI model */
|
|
32
34
|
OPENAI: "gpt-4o-mini",
|
|
33
35
|
/** Default Anthropic Claude model */
|
|
34
|
-
ANTHROPIC: "claude-3-5-haiku-latest"
|
|
36
|
+
ANTHROPIC: "claude-3-5-haiku-latest",
|
|
37
|
+
/** Default Cline API Gateway model (NVIDIA 120B reasoning model, free tier) */
|
|
38
|
+
CLINE: "nvidia/nemotron-3.5-super-vl:free"
|
|
35
39
|
});
|
|
36
40
|
|
|
37
41
|
// src/constants/execution.constants.ts
|
|
@@ -513,10 +517,13 @@ var GroqAdapter = class {
|
|
|
513
517
|
})
|
|
514
518
|
});
|
|
515
519
|
if (!response.ok || !response.body) {
|
|
520
|
+
const errorText = await response.text().catch(() => "");
|
|
521
|
+
const retryAfter = response.headers.get("retry-after") ?? response.headers.get("x-ratelimit-reset-tokens");
|
|
522
|
+
const retryDetail = retryAfter ? ` (retry-after: ${retryAfter})` : "";
|
|
516
523
|
throw new errors.DomainError(
|
|
517
|
-
`Groq streaming error HTTP ${response.status}`,
|
|
518
|
-
errors.ErrorCode.MODEL_PROVIDER_ERROR,
|
|
519
|
-
|
|
524
|
+
`Groq streaming error HTTP ${response.status}${retryDetail}${errorText ? `: ${errorText}` : ""}`,
|
|
525
|
+
response.status === 429 ? errors.ErrorCode.MODEL_RATE_LIMIT : errors.ErrorCode.MODEL_PROVIDER_ERROR,
|
|
526
|
+
response.status
|
|
520
527
|
);
|
|
521
528
|
}
|
|
522
529
|
const reader = response.body.getReader();
|
|
@@ -1040,7 +1047,195 @@ var AnthropicAdapter = class {
|
|
|
1040
1047
|
};
|
|
1041
1048
|
}
|
|
1042
1049
|
};
|
|
1043
|
-
|
|
1050
|
+
|
|
1051
|
+
// src/adapters/cline/cline.types.ts
|
|
1052
|
+
function sanitizeClineToolName(name) {
|
|
1053
|
+
return name.replace(/\./g, "_");
|
|
1054
|
+
}
|
|
1055
|
+
function restoreClineToolName(name) {
|
|
1056
|
+
return name.replace(/_/g, ".");
|
|
1057
|
+
}
|
|
1058
|
+
function mapClineMessages(messages) {
|
|
1059
|
+
return messages.map((m) => ({
|
|
1060
|
+
role: m.role === MessageRole.SYSTEM ? "system" : m.role === MessageRole.ASSISTANT ? "assistant" : "user",
|
|
1061
|
+
content: m.content
|
|
1062
|
+
}));
|
|
1063
|
+
}
|
|
1064
|
+
|
|
1065
|
+
// src/adapters/cline/cline.helpers.ts
|
|
1066
|
+
function extractClineToolCalls(choices) {
|
|
1067
|
+
const choice = choices?.[0];
|
|
1068
|
+
const toolCalls = choice?.message?.tool_calls;
|
|
1069
|
+
if (!toolCalls || toolCalls.length === 0) {
|
|
1070
|
+
return void 0;
|
|
1071
|
+
}
|
|
1072
|
+
return toolCalls.map((tc, index) => {
|
|
1073
|
+
let args = {};
|
|
1074
|
+
try {
|
|
1075
|
+
args = tc.function?.arguments ? JSON.parse(tc.function.arguments) : {};
|
|
1076
|
+
} catch {
|
|
1077
|
+
args = {};
|
|
1078
|
+
}
|
|
1079
|
+
return {
|
|
1080
|
+
id: tc.id ?? `call_${index}`,
|
|
1081
|
+
name: restoreClineToolName(tc.function?.name ?? ""),
|
|
1082
|
+
arguments: args
|
|
1083
|
+
};
|
|
1084
|
+
});
|
|
1085
|
+
}
|
|
1086
|
+
function mapClineFinishReason(reason) {
|
|
1087
|
+
if (reason === "tool_calls") return FinishReason.TOOL_CALLS;
|
|
1088
|
+
if (reason === "length") return FinishReason.LENGTH;
|
|
1089
|
+
return FinishReason.STOP;
|
|
1090
|
+
}
|
|
1091
|
+
function extractClineRetrySuffix(response) {
|
|
1092
|
+
const retryAfter = response.headers.get("retry-after") ?? response.headers.get("x-ratelimit-reset-tokens");
|
|
1093
|
+
return retryAfter ? ` (retry-after: ${retryAfter})` : "";
|
|
1094
|
+
}
|
|
1095
|
+
|
|
1096
|
+
// src/adapters/cline/cline.adapter.ts
|
|
1097
|
+
var logger6 = logger$1.loggerWithConfig(new logger$1.Logger("ClineAdapter"));
|
|
1098
|
+
var ClineAdapter = class {
|
|
1099
|
+
name = "cline";
|
|
1100
|
+
config;
|
|
1101
|
+
constructor(config = {}) {
|
|
1102
|
+
this.config = config;
|
|
1103
|
+
}
|
|
1104
|
+
async resolveCredentials() {
|
|
1105
|
+
const dynamic = this.config.credentials ? await this.config.credentials() : {};
|
|
1106
|
+
const apiKey = this.config.apiKey ?? dynamic.apiKey;
|
|
1107
|
+
const baseUrl = (this.config.baseUrl ?? dynamic.baseUrl ?? AI_ENDPOINTS.CLINE_DEFAULT_BASE_URL).replace(/\/+$/, "");
|
|
1108
|
+
const defaultModel = this.config.defaultModel ?? dynamic.defaultModel ?? AI_DEFAULT_MODELS.CLINE;
|
|
1109
|
+
if (!apiKey) {
|
|
1110
|
+
throw new MissingCredentialError("Cline", "apiKey");
|
|
1111
|
+
}
|
|
1112
|
+
return { apiKey, baseUrl, defaultModel };
|
|
1113
|
+
}
|
|
1114
|
+
async complete(request) {
|
|
1115
|
+
const { apiKey, baseUrl, defaultModel } = await this.resolveCredentials();
|
|
1116
|
+
const startTime = Date.now();
|
|
1117
|
+
const model = request.model || defaultModel;
|
|
1118
|
+
logger6.debug("[complete] Executing Cline completion", { model, baseUrl });
|
|
1119
|
+
const controller = new AbortController();
|
|
1120
|
+
const timeout = setTimeout(() => controller.abort(), request.timeoutMs);
|
|
1121
|
+
try {
|
|
1122
|
+
const response = await fetch(`${baseUrl}/chat/completions`, {
|
|
1123
|
+
method: "POST",
|
|
1124
|
+
headers: {
|
|
1125
|
+
"Content-Type": "application/json",
|
|
1126
|
+
Authorization: `Bearer ${apiKey}`
|
|
1127
|
+
},
|
|
1128
|
+
body: JSON.stringify({
|
|
1129
|
+
model,
|
|
1130
|
+
messages: mapClineMessages(request.messages),
|
|
1131
|
+
temperature: request.temperature,
|
|
1132
|
+
max_tokens: request.maxTokens,
|
|
1133
|
+
top_p: request.topP,
|
|
1134
|
+
stop: request.stop,
|
|
1135
|
+
response_format: request.responseFormat === "json_object" ? { type: "json_object" } : void 0
|
|
1136
|
+
}),
|
|
1137
|
+
signal: controller.signal
|
|
1138
|
+
});
|
|
1139
|
+
if (!response.ok) {
|
|
1140
|
+
const errorText = await response.text().catch(() => "");
|
|
1141
|
+
const suffix = extractClineRetrySuffix(response);
|
|
1142
|
+
logger6.error("[complete] Cline API error response", {
|
|
1143
|
+
status: response.status,
|
|
1144
|
+
errorText
|
|
1145
|
+
});
|
|
1146
|
+
throw new errors.DomainError(
|
|
1147
|
+
`Cline returned HTTP ${response.status}: ${errorText}${suffix}`,
|
|
1148
|
+
response.status === 429 ? errors.ErrorCode.MODEL_RATE_LIMIT : errors.ErrorCode.MODEL_PROVIDER_ERROR,
|
|
1149
|
+
response.status
|
|
1150
|
+
);
|
|
1151
|
+
}
|
|
1152
|
+
const raw = await response.json();
|
|
1153
|
+
const choices = raw.choices ?? raw.data?.choices ?? [];
|
|
1154
|
+
const choice = choices[0];
|
|
1155
|
+
const text = choice?.message?.content ?? "";
|
|
1156
|
+
const toolCalls = extractClineToolCalls(choices);
|
|
1157
|
+
const durationMs = Date.now() - startTime;
|
|
1158
|
+
return {
|
|
1159
|
+
text,
|
|
1160
|
+
finishReason: mapClineFinishReason(choice?.finish_reason),
|
|
1161
|
+
usage: {
|
|
1162
|
+
promptTokens: raw.usage?.prompt_tokens ?? 0,
|
|
1163
|
+
completionTokens: raw.usage?.completion_tokens ?? 0,
|
|
1164
|
+
totalTokens: raw.usage?.total_tokens ?? 0
|
|
1165
|
+
},
|
|
1166
|
+
toolCalls,
|
|
1167
|
+
model,
|
|
1168
|
+
durationMs
|
|
1169
|
+
};
|
|
1170
|
+
} catch (err) {
|
|
1171
|
+
if (err instanceof Error && err.name === "AbortError") {
|
|
1172
|
+
logger6.warn("[complete] Cline request timed out", {
|
|
1173
|
+
model,
|
|
1174
|
+
timeoutMs: request.timeoutMs
|
|
1175
|
+
});
|
|
1176
|
+
throw new errors.ModelTimeoutError(model, request.timeoutMs);
|
|
1177
|
+
}
|
|
1178
|
+
throw err;
|
|
1179
|
+
} finally {
|
|
1180
|
+
clearTimeout(timeout);
|
|
1181
|
+
}
|
|
1182
|
+
}
|
|
1183
|
+
async *stream(request) {
|
|
1184
|
+
const { apiKey, baseUrl, defaultModel } = await this.resolveCredentials();
|
|
1185
|
+
const model = request.model || defaultModel;
|
|
1186
|
+
const response = await fetch(`${baseUrl}/chat/completions`, {
|
|
1187
|
+
method: "POST",
|
|
1188
|
+
headers: {
|
|
1189
|
+
"Content-Type": "application/json",
|
|
1190
|
+
Authorization: `Bearer ${apiKey}`
|
|
1191
|
+
},
|
|
1192
|
+
body: JSON.stringify({
|
|
1193
|
+
model,
|
|
1194
|
+
messages: mapClineMessages(request.messages),
|
|
1195
|
+
temperature: request.temperature,
|
|
1196
|
+
stream: true
|
|
1197
|
+
})
|
|
1198
|
+
});
|
|
1199
|
+
if (!response.ok || !response.body) {
|
|
1200
|
+
const errorText = await response.text().catch(() => "");
|
|
1201
|
+
const suffix = extractClineRetrySuffix(response);
|
|
1202
|
+
throw new errors.DomainError(
|
|
1203
|
+
`Cline streaming error HTTP ${response.status}: ${errorText}${suffix}`,
|
|
1204
|
+
response.status === 429 ? errors.ErrorCode.MODEL_RATE_LIMIT : errors.ErrorCode.MODEL_PROVIDER_ERROR,
|
|
1205
|
+
response.status
|
|
1206
|
+
);
|
|
1207
|
+
}
|
|
1208
|
+
const reader = response.body.getReader();
|
|
1209
|
+
const decoder = new TextDecoder();
|
|
1210
|
+
let buffer = "";
|
|
1211
|
+
try {
|
|
1212
|
+
while (true) {
|
|
1213
|
+
const { done, value } = await reader.read();
|
|
1214
|
+
if (done) break;
|
|
1215
|
+
buffer += decoder.decode(value, { stream: true });
|
|
1216
|
+
const lines = buffer.split("\n");
|
|
1217
|
+
buffer = lines.pop() ?? "";
|
|
1218
|
+
for (const line of lines) {
|
|
1219
|
+
const trimmed = line.trim();
|
|
1220
|
+
if (!trimmed || !trimmed.startsWith("data:")) continue;
|
|
1221
|
+
const payload = trimmed.slice(5).trim();
|
|
1222
|
+
if (payload === "[DONE]") break;
|
|
1223
|
+
try {
|
|
1224
|
+
const parsed = JSON.parse(payload);
|
|
1225
|
+
const delta = parsed.choices?.[0]?.delta?.content ?? "";
|
|
1226
|
+
if (delta) {
|
|
1227
|
+
yield { delta };
|
|
1228
|
+
}
|
|
1229
|
+
} catch {
|
|
1230
|
+
}
|
|
1231
|
+
}
|
|
1232
|
+
}
|
|
1233
|
+
} finally {
|
|
1234
|
+
reader.releaseLock();
|
|
1235
|
+
}
|
|
1236
|
+
}
|
|
1237
|
+
};
|
|
1238
|
+
var logger7 = logger$1.loggerWithConfig(new logger$1.Logger("FallbackCascade"));
|
|
1044
1239
|
var FallbackCascade = class {
|
|
1045
1240
|
name = "fallback-cascade";
|
|
1046
1241
|
providers;
|
|
@@ -1069,7 +1264,7 @@ var FallbackCascade = class {
|
|
|
1069
1264
|
const errors$1 = [];
|
|
1070
1265
|
for (const provider of this.providers) {
|
|
1071
1266
|
try {
|
|
1072
|
-
|
|
1267
|
+
logger7.debug(
|
|
1073
1268
|
`[complete] Attempting provider '${provider.name}' in FallbackCascade`
|
|
1074
1269
|
);
|
|
1075
1270
|
const response = await provider.complete(request);
|
|
@@ -1077,7 +1272,7 @@ var FallbackCascade = class {
|
|
|
1077
1272
|
} catch (err) {
|
|
1078
1273
|
const errorMsg = err instanceof Error ? err.message : String(err);
|
|
1079
1274
|
errors$1.push({ provider: provider.name, error: errorMsg });
|
|
1080
|
-
|
|
1275
|
+
logger7.warn(
|
|
1081
1276
|
`[complete] Provider '${provider.name}' failed in FallbackCascade, cascading to next candidate`,
|
|
1082
1277
|
{
|
|
1083
1278
|
provider: provider.name,
|
|
@@ -1086,7 +1281,7 @@ var FallbackCascade = class {
|
|
|
1086
1281
|
);
|
|
1087
1282
|
}
|
|
1088
1283
|
}
|
|
1089
|
-
|
|
1284
|
+
logger7.error("[complete] All providers in FallbackCascade exhausted", {
|
|
1090
1285
|
errors: errors$1
|
|
1091
1286
|
});
|
|
1092
1287
|
throw new errors.DomainError(
|
|
@@ -1110,7 +1305,7 @@ var FallbackCascade = class {
|
|
|
1110
1305
|
return;
|
|
1111
1306
|
} catch (err) {
|
|
1112
1307
|
lastError = err;
|
|
1113
|
-
|
|
1308
|
+
logger7.warn(
|
|
1114
1309
|
`[stream] Provider '${provider.name}' failed streaming, attempting next provider in cascade`
|
|
1115
1310
|
);
|
|
1116
1311
|
}
|
|
@@ -1118,7 +1313,7 @@ var FallbackCascade = class {
|
|
|
1118
1313
|
throw lastError instanceof Error ? lastError : new Error(String(lastError));
|
|
1119
1314
|
}
|
|
1120
1315
|
};
|
|
1121
|
-
var
|
|
1316
|
+
var logger8 = logger$1.loggerWithConfig(new logger$1.Logger("StructuredOutput"));
|
|
1122
1317
|
function cleanJsonText(raw) {
|
|
1123
1318
|
const match = /```(?:json)?\s*([\s\S]*?)\s*```/i.exec(raw);
|
|
1124
1319
|
if (match && match[1]) {
|
|
@@ -1147,7 +1342,7 @@ async function extractStructuredOutput(options) {
|
|
|
1147
1342
|
return schema.parse(parsedJson);
|
|
1148
1343
|
} catch (parseOrValidationError) {
|
|
1149
1344
|
const errorDetail = parseOrValidationError instanceof Error ? parseOrValidationError.message : String(parseOrValidationError);
|
|
1150
|
-
|
|
1345
|
+
logger8.warn(
|
|
1151
1346
|
`[extractStructuredOutput] Structured output parsing failed (attempt ${attempts}/${maxRepairRetries + 1})`,
|
|
1152
1347
|
{
|
|
1153
1348
|
error: errorDetail
|
|
@@ -1179,9 +1374,9 @@ async function extractStructuredOutput(options) {
|
|
|
1179
1374
|
422
|
|
1180
1375
|
);
|
|
1181
1376
|
}
|
|
1182
|
-
var
|
|
1377
|
+
var logger9 = logger$1.loggerWithConfig(new logger$1.Logger("ProviderFactory"));
|
|
1183
1378
|
function createProvider(type, config = {}) {
|
|
1184
|
-
|
|
1379
|
+
logger9.debug(`[createProvider] Creating LLM provider instance: ${type}`);
|
|
1185
1380
|
switch (type) {
|
|
1186
1381
|
case "ollama":
|
|
1187
1382
|
return new OllamaAdapter(config);
|
|
@@ -1193,6 +1388,8 @@ function createProvider(type, config = {}) {
|
|
|
1193
1388
|
return new OpenAIAdapter(config);
|
|
1194
1389
|
case "anthropic":
|
|
1195
1390
|
return new AnthropicAdapter(config);
|
|
1391
|
+
case "cline":
|
|
1392
|
+
return new ClineAdapter(config);
|
|
1196
1393
|
case "cascade": {
|
|
1197
1394
|
const providers = config.providers ?? createCascadeCandidates();
|
|
1198
1395
|
return new FallbackCascade({ providers });
|
|
@@ -1222,6 +1419,9 @@ function createCascadeCandidates(config = {}) {
|
|
|
1222
1419
|
if (config.anthropic) {
|
|
1223
1420
|
candidates.push(new AnthropicAdapter(config.anthropic));
|
|
1224
1421
|
}
|
|
1422
|
+
if (config.cline) {
|
|
1423
|
+
candidates.push(new ClineAdapter(config.cline));
|
|
1424
|
+
}
|
|
1225
1425
|
if (candidates.length === 0) {
|
|
1226
1426
|
candidates.push(new OllamaAdapter(config.ollama ?? {}));
|
|
1227
1427
|
}
|
|
@@ -1229,7 +1429,7 @@ function createCascadeCandidates(config = {}) {
|
|
|
1229
1429
|
}
|
|
1230
1430
|
function createDefaultProvider(options = {}) {
|
|
1231
1431
|
if (options.provider) {
|
|
1232
|
-
|
|
1432
|
+
logger9.info(
|
|
1233
1433
|
`[createDefaultProvider] Using explicitly configured provider: ${options.provider}`
|
|
1234
1434
|
);
|
|
1235
1435
|
return createProvider(options.provider, options.config);
|
|
@@ -1244,6 +1444,7 @@ exports.AI_DEFAULT_MODELS = AI_DEFAULT_MODELS;
|
|
|
1244
1444
|
exports.AI_ENDPOINTS = AI_ENDPOINTS;
|
|
1245
1445
|
exports.AI_EXECUTION_DEFAULTS = AI_EXECUTION_DEFAULTS;
|
|
1246
1446
|
exports.AnthropicAdapter = AnthropicAdapter;
|
|
1447
|
+
exports.ClineAdapter = ClineAdapter;
|
|
1247
1448
|
exports.FallbackCascade = FallbackCascade;
|
|
1248
1449
|
exports.FinishReason = FinishReason;
|
|
1249
1450
|
exports.GoogleAdapter = GoogleAdapter;
|
|
@@ -1266,10 +1467,16 @@ exports.createSystemMessage = createSystemMessage;
|
|
|
1266
1467
|
exports.createToolResultMessage = createToolResultMessage;
|
|
1267
1468
|
exports.createUserMessage = createUserMessage;
|
|
1268
1469
|
exports.extractAnthropicSystemAndMessages = extractAnthropicSystemAndMessages;
|
|
1470
|
+
exports.extractClineRetrySuffix = extractClineRetrySuffix;
|
|
1471
|
+
exports.extractClineToolCalls = extractClineToolCalls;
|
|
1269
1472
|
exports.extractStructuredOutput = extractStructuredOutput;
|
|
1473
|
+
exports.mapClineFinishReason = mapClineFinishReason;
|
|
1474
|
+
exports.mapClineMessages = mapClineMessages;
|
|
1270
1475
|
exports.mapGroqMessages = mapGroqMessages;
|
|
1271
1476
|
exports.mapOllamaMessages = mapOllamaMessages;
|
|
1272
1477
|
exports.mapOpenAiMessages = mapOpenAiMessages;
|
|
1273
1478
|
exports.resolveEnvCredentials = resolveEnvCredentials;
|
|
1479
|
+
exports.restoreClineToolName = restoreClineToolName;
|
|
1480
|
+
exports.sanitizeClineToolName = sanitizeClineToolName;
|
|
1274
1481
|
//# sourceMappingURL=index.cjs.map
|
|
1275
1482
|
//# sourceMappingURL=index.cjs.map
|