@mcoda/agents 0.1.38 → 0.1.41
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.
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AgentService.d.ts","sourceRoot":"","sources":["../../src/AgentService/AgentService.ts"],"names":[],"mappings":"AAGA,OAAO,EACL,KAAK,EACL,iBAAiB,EACjB,WAAW,EACX,mBAAmB,EAKpB,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,gBAAgB,EAAE,MAAM,WAAW,CAAC;AAY7C,OAAO,EAAE,YAAY,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,MAAM,6BAA6B,CAAC;
|
|
1
|
+
{"version":3,"file":"AgentService.d.ts","sourceRoot":"","sources":["../../src/AgentService/AgentService.ts"],"names":[],"mappings":"AAGA,OAAO,EACL,KAAK,EACL,iBAAiB,EACjB,WAAW,EACX,mBAAmB,EAKpB,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,gBAAgB,EAAE,MAAM,WAAW,CAAC;AAY7C,OAAO,EAAE,YAAY,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,MAAM,6BAA6B,CAAC;AAyMhG,UAAU,mBAAmB;IAC3B,GAAG,CAAC,EAAE,MAAM,MAAM,CAAC;IACnB,KAAK,CAAC,EAAE,CAAC,EAAE,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IACtC,sBAAsB,CAAC,EAAE,MAAM,OAAO,CAAC,OAAO,CAAC,CAAC;IAChD,0BAA0B,CAAC,EAAE,MAAM,CAAC;CACrC;AAED,qBAAa,YAAY;IAErB,OAAO,CAAC,IAAI;IACZ,OAAO,CAAC,OAAO;gBADP,IAAI,EAAE,gBAAgB,EACtB,OAAO,GAAE,mBAAwB;WAG9B,MAAM,IAAI,OAAO,CAAC,YAAY,CAAC;IAKtC,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAItB,YAAY,CAAC,UAAU,EAAE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MswarmCloudAgent = (agent) => {
|
|
60
|
+
const config = agent.config;
|
|
61
|
+
if (!config || typeof config !== "object" || Array.isArray(config)) {
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
const managed = config.mswarmCloud;
|
|
65
|
+
return Boolean(managed &&
|
|
66
|
+
typeof managed === "object" &&
|
|
67
|
+
!Array.isArray(managed) &&
|
|
68
|
+
managed.managed === true);
|
|
69
|
+
};
|
|
59
70
|
const isIoEnabled = () => {
|
|
60
71
|
const raw = process.env[IO_ENV];
|
|
61
72
|
if (!raw)
|
|
@@ -266,6 +277,10 @@ export class AgentService {
|
|
|
266
277
|
if (adapterType.endsWith("-api")) {
|
|
267
278
|
if (hasSecret)
|
|
268
279
|
return adapterType;
|
|
280
|
+
if (adapterType === "openai-api" && isManagedMswarmCloudAgent(agent)) {
|
|
281
|
+
const label = agent.slug ?? agent.id;
|
|
282
|
+
throw new Error(`AUTH_REQUIRED: Managed mswarm cloud agent ${label} is missing the synced API key; run \`mcoda config set mswarm-api-key <KEY>\` and \`mcoda cloud agent sync\`.`);
|
|
283
|
+
}
|
|
269
284
|
if (adapterType === "codex-api" || adapterType === "openai-api") {
|
|
270
285
|
// Default to the codex CLI when API creds are missing.
|
|
271
286
|
adapterType = "codex-cli";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"OpenAiAdapter.d.ts","sourceRoot":"","sources":["../../../src/adapters/openai/OpenAiAdapter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5C,OAAO,EAAE,aAAa,EAAE,YAAY,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;
|
|
1
|
+
{"version":3,"file":"OpenAiAdapter.d.ts","sourceRoot":"","sources":["../../../src/adapters/openai/OpenAiAdapter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5C,OAAO,EAAE,aAAa,EAA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+
const RATE_LIMIT_HEADER_NAMES = [
|
|
5
|
+
"retry-after",
|
|
6
|
+
"x-ratelimit-reset-after",
|
|
7
|
+
"x-ratelimit-reset",
|
|
8
|
+
"x-ratelimit-reset-at",
|
|
9
|
+
"x-ratelimit-remaining",
|
|
10
|
+
];
|
|
2
11
|
const asString = (value) => (typeof value === "string" ? value : undefined);
|
|
3
12
|
const resolveString = (value) => {
|
|
4
13
|
const raw = asString(value)?.trim();
|
|
@@ -11,6 +20,37 @@ const normalizeBaseUrl = (value) => {
|
|
|
11
20
|
return undefined;
|
|
12
21
|
return str.endsWith("/") ? str.slice(0, -1) : str;
|
|
13
22
|
};
|
|
23
|
+
const buildRateLimitProbeMessage = (response, responseText) => {
|
|
24
|
+
const parts = [`openai_probe http ${response.status}`];
|
|
25
|
+
const retryAfter = response.headers.get("retry-after")?.trim();
|
|
26
|
+
if (retryAfter) {
|
|
27
|
+
const retryAfterSeconds = Number.parseInt(retryAfter, 10);
|
|
28
|
+
parts.push(Number.isFinite(retryAfterSeconds) && retryAfterSeconds > 0
|
|
29
|
+
? `Retry after ${retryAfterSeconds} seconds`
|
|
30
|
+
: `Retry after ${retryAfter}`);
|
|
31
|
+
}
|
|
32
|
+
for (const headerName of RATE_LIMIT_HEADER_NAMES) {
|
|
33
|
+
if (headerName === "retry-after")
|
|
34
|
+
continue;
|
|
35
|
+
const headerValue = response.headers.get(headerName)?.trim();
|
|
36
|
+
if (headerValue) {
|
|
37
|
+
parts.push(`${headerName}: ${headerValue}`);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
const trimmedResponse = responseText.trim();
|
|
41
|
+
if (trimmedResponse) {
|
|
42
|
+
parts.push(trimmedResponse);
|
|
43
|
+
}
|
|
44
|
+
return parts.join(". ");
|
|
45
|
+
};
|
|
46
|
+
const resolveRetryAfterMs = (resetAt, nowMs) => {
|
|
47
|
+
if (!resetAt)
|
|
48
|
+
return undefined;
|
|
49
|
+
const timestampMs = Date.parse(resetAt);
|
|
50
|
+
if (!Number.isFinite(timestampMs))
|
|
51
|
+
return undefined;
|
|
52
|
+
return Math.max(0, timestampMs - nowMs);
|
|
53
|
+
};
|
|
14
54
|
const resolveBaseUrl = (config) => {
|
|
15
55
|
const anyConfig = config;
|
|
16
56
|
const agentConfig = config.agent?.config;
|
|
@@ -128,16 +168,105 @@ export class OpenAiAdapter {
|
|
|
128
168
|
agentId: this.config.agent.id,
|
|
129
169
|
status: "unreachable",
|
|
130
170
|
lastCheckedAt: new Date().toISOString(),
|
|
131
|
-
details: {
|
|
171
|
+
details: {
|
|
172
|
+
adapter: "openai-api",
|
|
173
|
+
source: "openai_probe",
|
|
174
|
+
model: this.config.model,
|
|
175
|
+
baseUrl: this.baseUrl,
|
|
176
|
+
reason: "missing_api_key",
|
|
177
|
+
},
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
const startedAt = Date.now();
|
|
181
|
+
try {
|
|
182
|
+
const model = this.ensureModel();
|
|
183
|
+
const apiKey = this.ensureApiKey();
|
|
184
|
+
const url = this.ensureBaseUrl();
|
|
185
|
+
const response = await fetch(`${url}/chat/completions`, {
|
|
186
|
+
method: "POST",
|
|
187
|
+
headers: this.buildHeaders(apiKey, false),
|
|
188
|
+
body: JSON.stringify(this.buildHealthCheckBody(model)),
|
|
189
|
+
});
|
|
190
|
+
const responseText = await response.text().catch(() => "");
|
|
191
|
+
const checkedAtMs = Date.now();
|
|
192
|
+
const lastCheckedAt = new Date(checkedAtMs).toISOString();
|
|
193
|
+
const latencyMs = checkedAtMs - startedAt;
|
|
194
|
+
if (!response.ok) {
|
|
195
|
+
if (response.status === 429) {
|
|
196
|
+
const parsedLimit = parseUsageLimitError(new Error(buildRateLimitProbeMessage(response, responseText)), checkedAtMs);
|
|
197
|
+
return {
|
|
198
|
+
agentId: this.config.agent.id,
|
|
199
|
+
status: "healthy",
|
|
200
|
+
lastCheckedAt,
|
|
201
|
+
latencyMs,
|
|
202
|
+
details: {
|
|
203
|
+
adapter: "openai-api",
|
|
204
|
+
source: "openai_probe",
|
|
205
|
+
model,
|
|
206
|
+
baseUrl: url,
|
|
207
|
+
reason: "rate_limited",
|
|
208
|
+
transient: true,
|
|
209
|
+
rateLimited: true,
|
|
210
|
+
httpStatus: response.status,
|
|
211
|
+
response: responseText.slice(0, MAX_RESPONSE_DETAIL_CHARS),
|
|
212
|
+
resetAt: parsedLimit?.resetAt,
|
|
213
|
+
resetAtSource: parsedLimit?.resetAtSource,
|
|
214
|
+
retryAfterMs: resolveRetryAfterMs(parsedLimit?.resetAt, checkedAtMs),
|
|
215
|
+
windowTypes: parsedLimit?.windowTypes,
|
|
216
|
+
},
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
return {
|
|
220
|
+
agentId: this.config.agent.id,
|
|
221
|
+
status: "unreachable",
|
|
222
|
+
lastCheckedAt,
|
|
223
|
+
latencyMs,
|
|
224
|
+
details: {
|
|
225
|
+
adapter: "openai-api",
|
|
226
|
+
source: "openai_probe",
|
|
227
|
+
model,
|
|
228
|
+
baseUrl: url,
|
|
229
|
+
reason: "http_error",
|
|
230
|
+
httpStatus: response.status,
|
|
231
|
+
response: responseText.slice(0, MAX_RESPONSE_DETAIL_CHARS),
|
|
232
|
+
},
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
return {
|
|
236
|
+
agentId: this.config.agent.id,
|
|
237
|
+
status: "healthy",
|
|
238
|
+
lastCheckedAt,
|
|
239
|
+
latencyMs,
|
|
240
|
+
details: {
|
|
241
|
+
adapter: "openai-api",
|
|
242
|
+
source: "openai_probe",
|
|
243
|
+
model,
|
|
244
|
+
baseUrl: url,
|
|
245
|
+
},
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
catch (error) {
|
|
249
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
250
|
+
const reason = /model is not configured/i.test(message)
|
|
251
|
+
? "missing_model"
|
|
252
|
+
: /missing api key/i.test(message)
|
|
253
|
+
? "missing_api_key"
|
|
254
|
+
: "probe_failed";
|
|
255
|
+
return {
|
|
256
|
+
agentId: this.config.agent.id,
|
|
257
|
+
status: "unreachable",
|
|
258
|
+
lastCheckedAt: new Date().toISOString(),
|
|
259
|
+
latencyMs: Date.now() - startedAt,
|
|
260
|
+
details: {
|
|
261
|
+
adapter: "openai-api",
|
|
262
|
+
source: "openai_probe",
|
|
263
|
+
model: this.config.model,
|
|
264
|
+
baseUrl: this.baseUrl,
|
|
265
|
+
reason,
|
|
266
|
+
error: message,
|
|
267
|
+
},
|
|
132
268
|
};
|
|
133
269
|
}
|
|
134
|
-
return {
|
|
135
|
-
agentId: this.config.agent.id,
|
|
136
|
-
status: "healthy",
|
|
137
|
-
lastCheckedAt: new Date().toISOString(),
|
|
138
|
-
latencyMs: 0,
|
|
139
|
-
details: { adapter: "openai-api", model: this.config.model, baseUrl: this.baseUrl },
|
|
140
|
-
};
|
|
141
270
|
}
|
|
142
271
|
async invoke(request) {
|
|
143
272
|
const url = this.ensureBaseUrl();
|
|
@@ -321,4 +450,14 @@ export class OpenAiAdapter {
|
|
|
321
450
|
}
|
|
322
451
|
return body;
|
|
323
452
|
}
|
|
453
|
+
buildHealthCheckBody(model) {
|
|
454
|
+
const body = this.buildBody("healthcheck", model, false);
|
|
455
|
+
if (body.max_tokens === undefined && body.max_completion_tokens === undefined) {
|
|
456
|
+
body.max_tokens = 1;
|
|
457
|
+
}
|
|
458
|
+
if (body.temperature === undefined) {
|
|
459
|
+
body.temperature = 0;
|
|
460
|
+
}
|
|
461
|
+
return body;
|
|
462
|
+
}
|
|
324
463
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mcoda/agents",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.41",
|
|
4
4
|
"description": "Agent registry and capabilities for mcoda.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -30,8 +30,8 @@
|
|
|
30
30
|
"access": "public"
|
|
31
31
|
},
|
|
32
32
|
"dependencies": {
|
|
33
|
-
"@mcoda/shared": "0.1.
|
|
34
|
-
"@mcoda/db": "0.1.
|
|
33
|
+
"@mcoda/shared": "0.1.41",
|
|
34
|
+
"@mcoda/db": "0.1.41"
|
|
35
35
|
},
|
|
36
36
|
"scripts": {
|
|
37
37
|
"build": "tsc -p tsconfig.json",
|