@uselemma/opencode 0.1.1 → 0.1.3
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 +3 -1
- package/package.json +12 -2
- package/runtime/lemma.mjs +1378 -236
- package/scripts/setup.mjs +71 -2
package/runtime/lemma.mjs
CHANGED
|
@@ -44,10 +44,8 @@ var require_debug_delivery = __commonJS({
|
|
|
44
44
|
exports.isValidProjectId = isValidProjectId;
|
|
45
45
|
exports.apiKeySuffix = apiKeySuffix;
|
|
46
46
|
exports.buildConfigWarnings = buildConfigWarnings;
|
|
47
|
-
exports.ingestFailureHint = ingestFailureHint;
|
|
48
47
|
exports.pickResponseHeaders = pickResponseHeaders;
|
|
49
48
|
exports.pickResponseHeadersFromRecord = pickResponseHeadersFromRecord;
|
|
50
|
-
exports.sleep = sleep;
|
|
51
49
|
exports.PRODUCTION_BASE_URL = "https://api.uselemma.ai";
|
|
52
50
|
exports.INGEST_PATH = "/traces/ingest";
|
|
53
51
|
exports.INGEST_STATUS_PATH = "/traces/ingest-status";
|
|
@@ -87,20 +85,6 @@ var require_debug_delivery = __commonJS({
|
|
|
87
85
|
}
|
|
88
86
|
return warnings;
|
|
89
87
|
}
|
|
90
|
-
function ingestFailureHint(status) {
|
|
91
|
-
switch (status) {
|
|
92
|
-
case 401:
|
|
93
|
-
return "check LEMMA_API_KEY";
|
|
94
|
-
case 403:
|
|
95
|
-
return "API key doesn't own this project_id";
|
|
96
|
-
case 429:
|
|
97
|
-
return "ingest rate limit exceeded; retry with backoff";
|
|
98
|
-
case 404:
|
|
99
|
-
return "baseUrl likely wrong (not Lemma API)";
|
|
100
|
-
default:
|
|
101
|
-
return void 0;
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
88
|
function pickResponseHeaders(headers) {
|
|
105
89
|
const picked = {};
|
|
106
90
|
for (const name of ["cf-ray", "server", "date"]) {
|
|
@@ -120,9 +104,6 @@ var require_debug_delivery = __commonJS({
|
|
|
120
104
|
}
|
|
121
105
|
return picked;
|
|
122
106
|
}
|
|
123
|
-
function sleep(ms) {
|
|
124
|
-
return new Promise((resolve2) => setTimeout(resolve2, ms));
|
|
125
|
-
}
|
|
126
107
|
}
|
|
127
108
|
});
|
|
128
109
|
|
|
@@ -246,6 +227,181 @@ var require_error_message = __commonJS({
|
|
|
246
227
|
}
|
|
247
228
|
});
|
|
248
229
|
|
|
230
|
+
// ../../packages/ts/tracing/dist/delivery.js
|
|
231
|
+
var require_delivery = __commonJS({
|
|
232
|
+
"../../packages/ts/tracing/dist/delivery.js"(exports) {
|
|
233
|
+
"use strict";
|
|
234
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
235
|
+
exports.INGEST_RETRY_MAX_DELAY_MS = exports.INGEST_RETRY_BASE_DELAY_MS = exports.INGEST_REQUEST_TIMEOUT_MS = exports.INGEST_MAX_ATTEMPTS = exports.INGEST_STRICT_ATTEMPTS = void 0;
|
|
236
|
+
exports.ingestFailureHint = ingestFailureHint;
|
|
237
|
+
exports.isRetryableIngestStatus = isRetryableIngestStatus;
|
|
238
|
+
exports.parseRetryAfterMs = parseRetryAfterMs;
|
|
239
|
+
exports.ingestRetryDelayMs = ingestRetryDelayMs;
|
|
240
|
+
exports.sleep = sleep;
|
|
241
|
+
exports.withIngestTimeout = withIngestTimeout;
|
|
242
|
+
exports.failedBeforeSending = failedBeforeSending;
|
|
243
|
+
exports.deliver = deliver;
|
|
244
|
+
var debug_delivery_1 = require_debug_delivery();
|
|
245
|
+
var debug_mode_1 = require_debug_mode();
|
|
246
|
+
var error_message_1 = require_error_message();
|
|
247
|
+
exports.INGEST_STRICT_ATTEMPTS = 1;
|
|
248
|
+
exports.INGEST_MAX_ATTEMPTS = 3;
|
|
249
|
+
exports.INGEST_REQUEST_TIMEOUT_MS = 1e4;
|
|
250
|
+
exports.INGEST_RETRY_BASE_DELAY_MS = 500;
|
|
251
|
+
exports.INGEST_RETRY_MAX_DELAY_MS = 2e3;
|
|
252
|
+
function ingestFailureHint(status) {
|
|
253
|
+
switch (status) {
|
|
254
|
+
case 401:
|
|
255
|
+
return "check LEMMA_API_KEY";
|
|
256
|
+
case 403:
|
|
257
|
+
return "API key doesn't own this project_id";
|
|
258
|
+
case 429:
|
|
259
|
+
return "ingest rate limit exceeded; lower the trace rate if it persists";
|
|
260
|
+
case 404:
|
|
261
|
+
return "baseUrl likely wrong (not Lemma API)";
|
|
262
|
+
default:
|
|
263
|
+
return void 0;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
function isRetryableIngestStatus(status) {
|
|
267
|
+
return status === 408 || status === 429 || status >= 500;
|
|
268
|
+
}
|
|
269
|
+
function parseRetryAfterMs(value) {
|
|
270
|
+
if (!value?.trim())
|
|
271
|
+
return null;
|
|
272
|
+
const seconds = Number(value);
|
|
273
|
+
return Number.isFinite(seconds) && seconds >= 0 ? seconds * 1e3 : null;
|
|
274
|
+
}
|
|
275
|
+
function ingestRetryDelayMs(retry, retryAfterMs, random = Math.random) {
|
|
276
|
+
const backoff = exports.INGEST_RETRY_BASE_DELAY_MS * 2 ** (retry - 1);
|
|
277
|
+
const jittered = backoff / 2 + random() * (backoff / 2);
|
|
278
|
+
return Math.min(exports.INGEST_RETRY_MAX_DELAY_MS, Math.max(jittered, retryAfterMs ?? 0));
|
|
279
|
+
}
|
|
280
|
+
function sleep(ms) {
|
|
281
|
+
return new Promise((resolve2) => setTimeout(resolve2, ms));
|
|
282
|
+
}
|
|
283
|
+
async function withIngestTimeout(run) {
|
|
284
|
+
const controller = new AbortController();
|
|
285
|
+
let timer;
|
|
286
|
+
const timedOut = new Promise((_, reject) => {
|
|
287
|
+
timer = setTimeout(() => {
|
|
288
|
+
const error = new Error(`ingest request timed out after ${exports.INGEST_REQUEST_TIMEOUT_MS}ms`);
|
|
289
|
+
error.name = "TimeoutError";
|
|
290
|
+
controller.abort(error);
|
|
291
|
+
reject(error);
|
|
292
|
+
}, exports.INGEST_REQUEST_TIMEOUT_MS);
|
|
293
|
+
});
|
|
294
|
+
try {
|
|
295
|
+
return await Promise.race([run(controller.signal), timedOut]);
|
|
296
|
+
} finally {
|
|
297
|
+
clearTimeout(timer);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
function failedBeforeSending(error) {
|
|
301
|
+
return { ok: false, error, cause: `before sending: ${(0, error_message_1.describeError)(error)}` };
|
|
302
|
+
}
|
|
303
|
+
function failedAfter(attempts, attempt) {
|
|
304
|
+
return {
|
|
305
|
+
ok: false,
|
|
306
|
+
error: attempt.error,
|
|
307
|
+
cause: `after ${attempts} attempt${attempts === 1 ? "" : "s"}: ${attempt.reason}`
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
async function sendOnce(post, log) {
|
|
311
|
+
try {
|
|
312
|
+
return await withIngestTimeout(async (signal) => {
|
|
313
|
+
const response = await post(signal);
|
|
314
|
+
if (response.ok)
|
|
315
|
+
return { ok: true, response };
|
|
316
|
+
const body = await response.text().catch(() => "");
|
|
317
|
+
const hint = ingestFailureHint(response.status);
|
|
318
|
+
(0, debug_mode_1.lemmaDebug)("client", "trace ingest failed", {
|
|
319
|
+
...log,
|
|
320
|
+
status: response.status,
|
|
321
|
+
body,
|
|
322
|
+
...(0, debug_delivery_1.pickResponseHeaders)(response.headers),
|
|
323
|
+
...hint ? { hint } : {}
|
|
324
|
+
});
|
|
325
|
+
return {
|
|
326
|
+
ok: false,
|
|
327
|
+
error: new Error(`@uselemma/tracing: failed to ingest trace (${response.status})${body ? `: ${body}` : ""}`),
|
|
328
|
+
reason: `ingest returned ${response.status}${hint ? ` (${hint})` : ""}`,
|
|
329
|
+
retryable: isRetryableIngestStatus(response.status),
|
|
330
|
+
retryAfterMs: parseRetryAfterMs(response.headers.get("retry-after"))
|
|
331
|
+
};
|
|
332
|
+
});
|
|
333
|
+
} catch (error) {
|
|
334
|
+
(0, debug_mode_1.lemmaDebug)("client", "trace ingest failed", {
|
|
335
|
+
...log,
|
|
336
|
+
error: (0, error_message_1.failureMessage)(error) ?? String(error)
|
|
337
|
+
});
|
|
338
|
+
return {
|
|
339
|
+
ok: false,
|
|
340
|
+
error,
|
|
341
|
+
reason: (0, error_message_1.describeError)(error),
|
|
342
|
+
retryable: true,
|
|
343
|
+
retryAfterMs: null
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
async function deliver(post, log, maxAttempts) {
|
|
348
|
+
for (let attempts = 1; ; attempts += 1) {
|
|
349
|
+
const attempt = await sendOnce(post, log);
|
|
350
|
+
if (attempt.ok)
|
|
351
|
+
return { ok: true, response: attempt.response };
|
|
352
|
+
if (!attempt.retryable || attempts >= maxAttempts) {
|
|
353
|
+
return failedAfter(attempts, attempt);
|
|
354
|
+
}
|
|
355
|
+
const delayMs = ingestRetryDelayMs(attempts, attempt.retryAfterMs);
|
|
356
|
+
(0, debug_mode_1.lemmaDebug)("client", "retrying trace ingest", {
|
|
357
|
+
...log,
|
|
358
|
+
attempt: attempts,
|
|
359
|
+
maxAttempts,
|
|
360
|
+
delayMs
|
|
361
|
+
});
|
|
362
|
+
await sleep(delayMs);
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
});
|
|
367
|
+
|
|
368
|
+
// ../../packages/ts/tracing/dist/dropped-traces.js
|
|
369
|
+
var require_dropped_traces = __commonJS({
|
|
370
|
+
"../../packages/ts/tracing/dist/dropped-traces.js"(exports) {
|
|
371
|
+
"use strict";
|
|
372
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
373
|
+
exports.DROPPED_TRACE_WARNING_WINDOW_MS = void 0;
|
|
374
|
+
exports.reportDroppedTrace = reportDroppedTrace;
|
|
375
|
+
exports.resetDroppedTraceWarnings = resetDroppedTraceWarnings;
|
|
376
|
+
var debug_mode_1 = require_debug_mode();
|
|
377
|
+
exports.DROPPED_TRACE_WARNING_WINDOW_MS = 6e4;
|
|
378
|
+
var PREFIX = "@uselemma/tracing:";
|
|
379
|
+
var WINDOW_SECONDS = exports.DROPPED_TRACE_WARNING_WINDOW_MS / 1e3;
|
|
380
|
+
var lastWarnedAt = null;
|
|
381
|
+
var suppressed = 0;
|
|
382
|
+
function reportDroppedTrace(traceId, cause) {
|
|
383
|
+
const line = `${PREFIX} dropped trace ${traceId} ${cause}.`;
|
|
384
|
+
if ((0, debug_mode_1.isDebugModeEnabled)()) {
|
|
385
|
+
console.warn(line);
|
|
386
|
+
return;
|
|
387
|
+
}
|
|
388
|
+
const now = Date.now();
|
|
389
|
+
if (lastWarnedAt !== null && now - lastWarnedAt < exports.DROPPED_TRACE_WARNING_WINDOW_MS) {
|
|
390
|
+
suppressed += 1;
|
|
391
|
+
return;
|
|
392
|
+
}
|
|
393
|
+
const since = suppressed > 0 ? ` ${suppressed} more dropped since the last warning.` : "";
|
|
394
|
+
console.warn(`${line}${since} Drops in the next ${WINDOW_SECONDS}s are counted, not logged; set LEMMA_DEBUG=1 to log each one.`);
|
|
395
|
+
lastWarnedAt = now;
|
|
396
|
+
suppressed = 0;
|
|
397
|
+
}
|
|
398
|
+
function resetDroppedTraceWarnings() {
|
|
399
|
+
lastWarnedAt = null;
|
|
400
|
+
suppressed = 0;
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
});
|
|
404
|
+
|
|
249
405
|
// ../../packages/ts/tracing/dist/release.js
|
|
250
406
|
var require_release = __commonJS({
|
|
251
407
|
"../../packages/ts/tracing/dist/release.js"(exports) {
|
|
@@ -497,6 +653,16 @@ var require_usage = __commonJS({
|
|
|
497
653
|
}
|
|
498
654
|
});
|
|
499
655
|
|
|
656
|
+
// ../../packages/ts/tracing/dist/version.js
|
|
657
|
+
var require_version = __commonJS({
|
|
658
|
+
"../../packages/ts/tracing/dist/version.js"(exports) {
|
|
659
|
+
"use strict";
|
|
660
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
661
|
+
exports.SDK_USER_AGENT = void 0;
|
|
662
|
+
exports.SDK_USER_AGENT = "@uselemma/tracing/7.14.0";
|
|
663
|
+
}
|
|
664
|
+
});
|
|
665
|
+
|
|
500
666
|
// ../../packages/ts/tracing/dist/client.js
|
|
501
667
|
var require_client = __commonJS({
|
|
502
668
|
"../../packages/ts/tracing/dist/client.js"(exports) {
|
|
@@ -505,9 +671,12 @@ var require_client = __commonJS({
|
|
|
505
671
|
exports.Lemma = exports.TraceHandle = exports.TraceContext = exports.NoopSpanHandle = exports.SpanHandle = exports.toWireTokenUsage = exports.normalizeTokenUsage = exports.attachResultUsage = void 0;
|
|
506
672
|
var debug_delivery_1 = require_debug_delivery();
|
|
507
673
|
var debug_mode_1 = require_debug_mode();
|
|
674
|
+
var delivery_1 = require_delivery();
|
|
675
|
+
var dropped_traces_1 = require_dropped_traces();
|
|
508
676
|
var error_message_1 = require_error_message();
|
|
509
677
|
var release_1 = require_release();
|
|
510
678
|
var usage_1 = require_usage();
|
|
679
|
+
var version_1 = require_version();
|
|
511
680
|
var usage_2 = require_usage();
|
|
512
681
|
Object.defineProperty(exports, "attachResultUsage", { enumerable: true, get: function() {
|
|
513
682
|
return usage_2.attachResultUsage;
|
|
@@ -599,7 +768,10 @@ var require_client = __commonJS({
|
|
|
599
768
|
const attributes = {};
|
|
600
769
|
addDefined(attributes, "input.mime_type", options.inputMimeType);
|
|
601
770
|
addDefined(attributes, "output.mime_type", options.outputMimeType);
|
|
602
|
-
|
|
771
|
+
const modelIdentity = options.llmModelName ?? options.model;
|
|
772
|
+
addDefined(attributes, "llm.model_name", modelIdentity);
|
|
773
|
+
addDefined(attributes, "gen_ai.request.model", modelIdentity);
|
|
774
|
+
addDefined(attributes, "ai.model.id", modelIdentity);
|
|
603
775
|
addDefined(attributes, "llm.provider", options.llmProvider);
|
|
604
776
|
addDefined(attributes, "llm.system", options.llmSystem);
|
|
605
777
|
addDefined(attributes, "gen_ai.system", options.llmSystem ?? options.llmProvider);
|
|
@@ -700,6 +872,7 @@ var require_client = __commonJS({
|
|
|
700
872
|
startedAt: this.options.startedAt ?? /* @__PURE__ */ new Date(),
|
|
701
873
|
endedAt: this.options.endedAt ?? null
|
|
702
874
|
});
|
|
875
|
+
this.trace.registerSpanHandle(this);
|
|
703
876
|
}
|
|
704
877
|
end(options = {}) {
|
|
705
878
|
if (this.ended)
|
|
@@ -710,11 +883,30 @@ var require_client = __commonJS({
|
|
|
710
883
|
...options,
|
|
711
884
|
id: this.id,
|
|
712
885
|
startedAt: this.options.startedAt,
|
|
713
|
-
endedAt: options.endedAt ?? /* @__PURE__ */ new Date()
|
|
886
|
+
endedAt: options.endedAt ?? /* @__PURE__ */ new Date(),
|
|
887
|
+
// Keep a start-time model when end() omits it or would overwrite
|
|
888
|
+
// with undefined. Fill in from the response only when unset.
|
|
889
|
+
model: this.options.model ?? options.model
|
|
714
890
|
}, this.options.type ?? "span"));
|
|
715
891
|
this.trace.spanEnded(this.payload);
|
|
716
892
|
this.trace.changed();
|
|
717
893
|
}
|
|
894
|
+
/**
|
|
895
|
+
* Fill input or output after end() when a child model call finishes later.
|
|
896
|
+
* Input is written only while this span still has none. Output is replaced
|
|
897
|
+
* when the span did not carry its own output.
|
|
898
|
+
*/
|
|
899
|
+
fillContainerIo(input, output, ownOutput) {
|
|
900
|
+
if (!this.ended)
|
|
901
|
+
return;
|
|
902
|
+
if (this.payload.input === void 0 && input !== void 0) {
|
|
903
|
+
this.payload.input = input;
|
|
904
|
+
}
|
|
905
|
+
if (!ownOutput && output !== void 0) {
|
|
906
|
+
this.payload.output = output;
|
|
907
|
+
}
|
|
908
|
+
this.trace.changed();
|
|
909
|
+
}
|
|
718
910
|
/**
|
|
719
911
|
* Extend this span's end time if a child finished later.
|
|
720
912
|
* Only applies after end() — open parents are left alone so a later end()
|
|
@@ -824,6 +1016,7 @@ var require_client = __commonJS({
|
|
|
824
1016
|
options;
|
|
825
1017
|
onChange;
|
|
826
1018
|
spans = [];
|
|
1019
|
+
spanHandles = /* @__PURE__ */ new Map();
|
|
827
1020
|
traceOutput;
|
|
828
1021
|
traceError = null;
|
|
829
1022
|
id;
|
|
@@ -859,6 +1052,23 @@ var require_client = __commonJS({
|
|
|
859
1052
|
this.traceError = (0, error_message_1.failureMessage)(error);
|
|
860
1053
|
this.changed();
|
|
861
1054
|
}
|
|
1055
|
+
/** Root identity for a versioned turn context token. */
|
|
1056
|
+
identity() {
|
|
1057
|
+
return {
|
|
1058
|
+
name: this.options.name ?? "trace",
|
|
1059
|
+
threadId: this.options.threadId,
|
|
1060
|
+
userId: this.options.userId
|
|
1061
|
+
};
|
|
1062
|
+
}
|
|
1063
|
+
registerSpanHandle(handle) {
|
|
1064
|
+
this.spanHandles.set(handle.id, handle);
|
|
1065
|
+
}
|
|
1066
|
+
spanHandle(id) {
|
|
1067
|
+
return this.spanHandles.get(id);
|
|
1068
|
+
}
|
|
1069
|
+
hasSpan(id) {
|
|
1070
|
+
return this.spans.some((span) => span.id === id);
|
|
1071
|
+
}
|
|
862
1072
|
changed() {
|
|
863
1073
|
this.onChange?.();
|
|
864
1074
|
}
|
|
@@ -1015,14 +1225,20 @@ var require_client = __commonJS({
|
|
|
1015
1225
|
projectId;
|
|
1016
1226
|
baseUrl;
|
|
1017
1227
|
fetchImpl;
|
|
1228
|
+
/** Identity and credentials on every request; spread last so nothing overrides them. */
|
|
1229
|
+
baseHeaders;
|
|
1018
1230
|
release;
|
|
1019
1231
|
traces = /* @__PURE__ */ new Map();
|
|
1020
1232
|
configLogged = false;
|
|
1021
1233
|
constructor(options = {}) {
|
|
1022
1234
|
this.apiKey = required(options.apiKey ?? process.env.LEMMA_API_KEY, "LEMMA_API_KEY");
|
|
1023
1235
|
this.projectId = required(options.projectId ?? process.env.LEMMA_PROJECT_ID, "LEMMA_PROJECT_ID");
|
|
1024
|
-
this.baseUrl = (options.baseUrl ??
|
|
1236
|
+
this.baseUrl = (options.baseUrl ?? (process.env.LEMMA_BASE_URL?.trim() || debug_delivery_1.PRODUCTION_BASE_URL)).replace(/\/+$/, "");
|
|
1025
1237
|
this.fetchImpl = options.fetch ?? fetch;
|
|
1238
|
+
this.baseHeaders = {
|
|
1239
|
+
"User-Agent": options.userAgent ?? version_1.SDK_USER_AGENT,
|
|
1240
|
+
Authorization: `Bearer ${this.apiKey}`
|
|
1241
|
+
};
|
|
1026
1242
|
this.release = (0, release_1.normalizeRelease)(options.release ?? process.env.LEMMA_RELEASE);
|
|
1027
1243
|
this.logInitConfigOnce();
|
|
1028
1244
|
}
|
|
@@ -1054,14 +1270,7 @@ var require_client = __commonJS({
|
|
|
1054
1270
|
const url = `${this.baseUrl}${debug_delivery_1.INGEST_PATH}`;
|
|
1055
1271
|
let response;
|
|
1056
1272
|
try {
|
|
1057
|
-
response = await this.
|
|
1058
|
-
method: "POST",
|
|
1059
|
-
headers: {
|
|
1060
|
-
Authorization: `Bearer ${this.apiKey}`,
|
|
1061
|
-
"Content-Type": "application/json"
|
|
1062
|
-
},
|
|
1063
|
-
body
|
|
1064
|
-
});
|
|
1273
|
+
response = await (0, delivery_1.withIngestTimeout)((signal) => this.postIngest(body, signal));
|
|
1065
1274
|
} catch {
|
|
1066
1275
|
hints.push("ingest request failed (network error)");
|
|
1067
1276
|
return {
|
|
@@ -1074,7 +1283,7 @@ var require_client = __commonJS({
|
|
|
1074
1283
|
const ingestWarnings = [];
|
|
1075
1284
|
if (response.status !== debug_delivery_1.EXPECTED_INGEST_SUCCESS_STATUS) {
|
|
1076
1285
|
ingestWarnings.push(`expected status ${debug_delivery_1.EXPECTED_INGEST_SUCCESS_STATUS}, got ${response.status}`);
|
|
1077
|
-
const hint = (0,
|
|
1286
|
+
const hint = (0, delivery_1.ingestFailureHint)(response.status);
|
|
1078
1287
|
if (hint)
|
|
1079
1288
|
hints.push(hint);
|
|
1080
1289
|
}
|
|
@@ -1117,7 +1326,7 @@ var require_client = __commonJS({
|
|
|
1117
1326
|
if (!fn) {
|
|
1118
1327
|
const handle = new TraceHandle(traceOptions, async (trace, startedAt2, endedAt) => {
|
|
1119
1328
|
try {
|
|
1120
|
-
await this.
|
|
1329
|
+
await this.flushTraceAutomatic(trace, startedAt2, endedAt);
|
|
1121
1330
|
} finally {
|
|
1122
1331
|
this.traces.delete(trace.id);
|
|
1123
1332
|
}
|
|
@@ -1136,18 +1345,19 @@ var require_client = __commonJS({
|
|
|
1136
1345
|
name: traceOptions.name ?? "trace"
|
|
1137
1346
|
});
|
|
1138
1347
|
return (async () => {
|
|
1348
|
+
let result;
|
|
1139
1349
|
try {
|
|
1140
|
-
|
|
1141
|
-
if (traceOptions.output === void 0) {
|
|
1142
|
-
context.output(result);
|
|
1143
|
-
}
|
|
1144
|
-
await this.flushTrace(context, startedAt, /* @__PURE__ */ new Date());
|
|
1145
|
-
return result;
|
|
1350
|
+
result = await fn(context);
|
|
1146
1351
|
} catch (error) {
|
|
1147
1352
|
context.fail(error);
|
|
1148
|
-
await this.
|
|
1353
|
+
await this.flushTraceAutomatic(context, startedAt, /* @__PURE__ */ new Date());
|
|
1149
1354
|
throw error;
|
|
1150
1355
|
}
|
|
1356
|
+
if (traceOptions.output === void 0) {
|
|
1357
|
+
context.output(result);
|
|
1358
|
+
}
|
|
1359
|
+
await this.flushTraceAutomatic(context, startedAt, /* @__PURE__ */ new Date());
|
|
1360
|
+
return result;
|
|
1151
1361
|
})();
|
|
1152
1362
|
}
|
|
1153
1363
|
/**
|
|
@@ -1169,7 +1379,9 @@ var require_client = __commonJS({
|
|
|
1169
1379
|
* never mutates the trace's status, so a failed send can be retried as-is.
|
|
1170
1380
|
*/
|
|
1171
1381
|
async ingest(context, options) {
|
|
1172
|
-
await this.
|
|
1382
|
+
const failure = await this.sendTrace(context, options.startedAt, options.endedAt ?? /* @__PURE__ */ new Date(), delivery_1.INGEST_STRICT_ATTEMPTS);
|
|
1383
|
+
if (failure)
|
|
1384
|
+
throw failure.error;
|
|
1173
1385
|
}
|
|
1174
1386
|
recordSpan(options) {
|
|
1175
1387
|
const context = this.detachedTraceFor(options.traceId, "span");
|
|
@@ -1309,13 +1521,16 @@ var require_client = __commonJS({
|
|
|
1309
1521
|
async fetchIngestStatus(otelTraceId) {
|
|
1310
1522
|
const url = `${this.baseUrl}${debug_delivery_1.INGEST_STATUS_PATH}?project_id=${encodeURIComponent(this.projectId)}&otel_trace_id=${encodeURIComponent(otelTraceId)}`;
|
|
1311
1523
|
try {
|
|
1312
|
-
|
|
1313
|
-
|
|
1524
|
+
return await (0, delivery_1.withIngestTimeout)(async (signal) => {
|
|
1525
|
+
const response = await this.fetchImpl(url, {
|
|
1526
|
+
headers: { ...this.baseHeaders },
|
|
1527
|
+
signal
|
|
1528
|
+
});
|
|
1529
|
+
if (!response.ok)
|
|
1530
|
+
return null;
|
|
1531
|
+
const data = await response.json();
|
|
1532
|
+
return (0, debug_delivery_1.parseIngestStatus)(data.status);
|
|
1314
1533
|
});
|
|
1315
|
-
if (!response.ok)
|
|
1316
|
-
return null;
|
|
1317
|
-
const data = await response.json();
|
|
1318
|
-
return (0, debug_delivery_1.parseIngestStatus)(data.status);
|
|
1319
1534
|
} catch {
|
|
1320
1535
|
return null;
|
|
1321
1536
|
}
|
|
@@ -1335,7 +1550,7 @@ var require_client = __commonJS({
|
|
|
1335
1550
|
const remaining = deadline - Date.now();
|
|
1336
1551
|
if (remaining <= 0)
|
|
1337
1552
|
return status;
|
|
1338
|
-
await (0,
|
|
1553
|
+
await (0, delivery_1.sleep)(Math.min(debug_delivery_1.INGEST_STATUS_POLL_INTERVAL_MS, remaining));
|
|
1339
1554
|
}
|
|
1340
1555
|
}
|
|
1341
1556
|
async verifyIngestDelivery(otelTraceId) {
|
|
@@ -1355,41 +1570,73 @@ var require_client = __commonJS({
|
|
|
1355
1570
|
payload.trace.release = this.release;
|
|
1356
1571
|
return payload;
|
|
1357
1572
|
}
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
});
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1573
|
+
/**
|
|
1574
|
+
* Automatic SDK delivery. Retries transient failures, then fails open with
|
|
1575
|
+
* a rate-limited warning so Lemma being down cannot fail the caller's app.
|
|
1576
|
+
* {@link Lemma.ingest} sends once and throws, because queue/plugin producers
|
|
1577
|
+
* keep their own retry backlog.
|
|
1578
|
+
*/
|
|
1579
|
+
async flushTraceAutomatic(context, startedAt, endedAt) {
|
|
1580
|
+
const failure = await this.sendTrace(context, startedAt, endedAt, delivery_1.INGEST_MAX_ATTEMPTS);
|
|
1581
|
+
if (failure)
|
|
1582
|
+
(0, dropped_traces_1.reportDroppedTrace)(context.id, failure.cause);
|
|
1583
|
+
}
|
|
1584
|
+
/**
|
|
1585
|
+
* Build, send, and check one trace; undefined when it was delivered. A
|
|
1586
|
+
* payload that cannot be built comes back as a failure, so strict and
|
|
1587
|
+
* automatic delivery handle it once.
|
|
1588
|
+
*/
|
|
1589
|
+
async sendTrace(context, startedAt, endedAt, maxAttempts) {
|
|
1590
|
+
const built = this.buildIngestBody(context, startedAt, endedAt);
|
|
1591
|
+
if (!built.ok)
|
|
1592
|
+
return built;
|
|
1593
|
+
const result = await (0, delivery_1.deliver)((signal) => this.postIngest(built.body, signal), { traceId: context.id, projectId: this.projectId }, maxAttempts);
|
|
1594
|
+
if (!result.ok)
|
|
1595
|
+
return result;
|
|
1596
|
+
try {
|
|
1597
|
+
await this.afterTraceSent(built.payload, result.response);
|
|
1598
|
+
} catch (error) {
|
|
1599
|
+
console.warn(`@uselemma/tracing: trace ${context.id} was delivered, but its post-send check failed: ${(0, error_message_1.describeError)(error)}`);
|
|
1600
|
+
}
|
|
1601
|
+
return void 0;
|
|
1602
|
+
}
|
|
1603
|
+
/**
|
|
1604
|
+
* Serialize once, so every attempt resends the same span IDs and ingest
|
|
1605
|
+
* dedupes an earlier attempt that landed unseen.
|
|
1606
|
+
*/
|
|
1607
|
+
buildIngestBody(context, startedAt, endedAt) {
|
|
1608
|
+
try {
|
|
1609
|
+
const payload = this.stampRelease(context.toPayload(this.projectId, startedAt, endedAt));
|
|
1610
|
+
const body = JSON.stringify(payload);
|
|
1611
|
+
(0, debug_mode_1.lemmaDebug)("client", "sending trace", {
|
|
1384
1612
|
traceId: payload.trace.id,
|
|
1613
|
+
name: payload.trace.name,
|
|
1614
|
+
spanCount: payload.trace.spans.length,
|
|
1385
1615
|
projectId: payload.project_id,
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
...hint ? { hint } : {}
|
|
1616
|
+
bodyBytes: new TextEncoder().encode(body).length,
|
|
1617
|
+
requestedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1618
|
+
url: `${this.baseUrl}${debug_delivery_1.INGEST_PATH}`
|
|
1390
1619
|
});
|
|
1391
|
-
|
|
1620
|
+
return { ok: true, payload, body };
|
|
1621
|
+
} catch (error) {
|
|
1622
|
+
(0, debug_mode_1.lemmaDebug)("client", "trace ingest failed", {
|
|
1623
|
+
traceId: context.id,
|
|
1624
|
+
projectId: this.projectId,
|
|
1625
|
+
error: (0, error_message_1.failureMessage)(error) ?? String(error)
|
|
1626
|
+
});
|
|
1627
|
+
return (0, delivery_1.failedBeforeSending)(error);
|
|
1392
1628
|
}
|
|
1629
|
+
}
|
|
1630
|
+
postIngest(body, signal) {
|
|
1631
|
+
return this.fetchImpl(`${this.baseUrl}${debug_delivery_1.INGEST_PATH}`, {
|
|
1632
|
+
method: "POST",
|
|
1633
|
+
headers: { "Content-Type": "application/json", ...this.baseHeaders },
|
|
1634
|
+
body,
|
|
1635
|
+
signal
|
|
1636
|
+
});
|
|
1637
|
+
}
|
|
1638
|
+
async afterTraceSent(payload, response) {
|
|
1639
|
+
const responseHeaders = (0, debug_delivery_1.pickResponseHeaders)(response.headers);
|
|
1393
1640
|
const sentWarnings = [];
|
|
1394
1641
|
if (response.status !== debug_delivery_1.EXPECTED_INGEST_SUCCESS_STATUS) {
|
|
1395
1642
|
sentWarnings.push(`expected status ${debug_delivery_1.EXPECTED_INGEST_SUCCESS_STATUS}, got ${response.status} (wrong endpoint?)`);
|
|
@@ -1410,6 +1657,309 @@ var require_client = __commonJS({
|
|
|
1410
1657
|
}
|
|
1411
1658
|
});
|
|
1412
1659
|
|
|
1660
|
+
// ../../packages/ts/tracing/dist/turn.js
|
|
1661
|
+
var require_turn = __commonJS({
|
|
1662
|
+
"../../packages/ts/tracing/dist/turn.js"(exports) {
|
|
1663
|
+
"use strict";
|
|
1664
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1665
|
+
exports.TurnHandle = exports.AttachedTurn = exports.AttachedSpanHandle = exports.TURN_JOURNAL_VERSION = exports.TURN_CONTEXT_VERSION = void 0;
|
|
1666
|
+
exports.parseTurnContextToken = parseTurnContextToken;
|
|
1667
|
+
exports.applyTurnJournal = applyTurnJournal;
|
|
1668
|
+
exports.assembleTurn = assembleTurn;
|
|
1669
|
+
exports.startTurn = startTurn2;
|
|
1670
|
+
exports.attachTurn = attachTurn;
|
|
1671
|
+
var client_1 = require_client();
|
|
1672
|
+
exports.TURN_CONTEXT_VERSION = 1;
|
|
1673
|
+
exports.TURN_JOURNAL_VERSION = 1;
|
|
1674
|
+
function compact(value) {
|
|
1675
|
+
return Object.fromEntries(Object.entries(value).filter(([, entry]) => entry !== void 0));
|
|
1676
|
+
}
|
|
1677
|
+
function isoNow() {
|
|
1678
|
+
return (/* @__PURE__ */ new Date()).toISOString();
|
|
1679
|
+
}
|
|
1680
|
+
function asIso(value) {
|
|
1681
|
+
if (value == null)
|
|
1682
|
+
return value;
|
|
1683
|
+
return value instanceof Date ? value.toISOString() : value;
|
|
1684
|
+
}
|
|
1685
|
+
function parseTurnContextToken(value) {
|
|
1686
|
+
const token = typeof value === "string" ? JSON.parse(value) : value;
|
|
1687
|
+
if (!token || typeof token !== "object" || token.version !== exports.TURN_CONTEXT_VERSION || typeof token.traceId !== "string" || token.traceId.length === 0 || typeof token.startedAt !== "string" || token.startedAt.length === 0) {
|
|
1688
|
+
throw new Error("@uselemma/tracing: invalid turn context token");
|
|
1689
|
+
}
|
|
1690
|
+
return token;
|
|
1691
|
+
}
|
|
1692
|
+
function parseJournal(input) {
|
|
1693
|
+
const value = typeof input === "string" ? JSON.parse(input) : input;
|
|
1694
|
+
if (Array.isArray(value)) {
|
|
1695
|
+
return { records: value };
|
|
1696
|
+
}
|
|
1697
|
+
if (value && typeof value === "object" && "op" in value) {
|
|
1698
|
+
return { records: [value] };
|
|
1699
|
+
}
|
|
1700
|
+
if (value && typeof value === "object" && "records" in value && Array.isArray(value.records)) {
|
|
1701
|
+
const journal = value;
|
|
1702
|
+
if (journal.version !== void 0 && journal.version !== exports.TURN_JOURNAL_VERSION) {
|
|
1703
|
+
throw new Error(`@uselemma/tracing: unsupported turn journal version ${String(journal.version)}`);
|
|
1704
|
+
}
|
|
1705
|
+
return { token: journal.token, records: journal.records };
|
|
1706
|
+
}
|
|
1707
|
+
throw new Error("@uselemma/tracing: invalid turn journal");
|
|
1708
|
+
}
|
|
1709
|
+
var END_IDENTITY_KEYS = {
|
|
1710
|
+
id: true,
|
|
1711
|
+
parentId: true,
|
|
1712
|
+
parentSpanId: true,
|
|
1713
|
+
name: true,
|
|
1714
|
+
type: true,
|
|
1715
|
+
startedAt: true
|
|
1716
|
+
};
|
|
1717
|
+
function toSpanOptions(record, fallbackParentId) {
|
|
1718
|
+
const { op: _op, ...fields } = record;
|
|
1719
|
+
return compact({
|
|
1720
|
+
...fields,
|
|
1721
|
+
parentId: record.parentId ?? fallbackParentId,
|
|
1722
|
+
name: record.name ?? "span"
|
|
1723
|
+
});
|
|
1724
|
+
}
|
|
1725
|
+
function endOptionsFromRecord(record, fallbackParentId) {
|
|
1726
|
+
const options = toSpanOptions(record, fallbackParentId);
|
|
1727
|
+
return compact(Object.fromEntries(Object.entries(options).filter(([key]) => !Object.prototype.hasOwnProperty.call(END_IDENTITY_KEYS, key))));
|
|
1728
|
+
}
|
|
1729
|
+
function startFromRecord(context, options) {
|
|
1730
|
+
const type = options.type ?? "span";
|
|
1731
|
+
if (type === "generation") {
|
|
1732
|
+
return context.startGeneration(options);
|
|
1733
|
+
}
|
|
1734
|
+
if (type === "tool") {
|
|
1735
|
+
return context.startTool(options);
|
|
1736
|
+
}
|
|
1737
|
+
return context.startSpan(options);
|
|
1738
|
+
}
|
|
1739
|
+
function recordFromRecord(context, options) {
|
|
1740
|
+
const type = options.type ?? "span";
|
|
1741
|
+
if (type === "generation") {
|
|
1742
|
+
context.recordGeneration(options);
|
|
1743
|
+
return;
|
|
1744
|
+
}
|
|
1745
|
+
if (type === "tool") {
|
|
1746
|
+
context.recordTool(options);
|
|
1747
|
+
return;
|
|
1748
|
+
}
|
|
1749
|
+
context.recordSpan(options);
|
|
1750
|
+
}
|
|
1751
|
+
function applyTurnJournal(context, input) {
|
|
1752
|
+
const { token, records } = parseJournal(input);
|
|
1753
|
+
const fallbackParentId = token?.parentSpanId ?? null;
|
|
1754
|
+
for (const record of records) {
|
|
1755
|
+
if (!record?.id || !record.op)
|
|
1756
|
+
continue;
|
|
1757
|
+
const options = toSpanOptions(record, fallbackParentId);
|
|
1758
|
+
if (record.op === "start") {
|
|
1759
|
+
if (context.hasSpan(record.id))
|
|
1760
|
+
continue;
|
|
1761
|
+
startFromRecord(context, { ...options, endedAt: void 0 });
|
|
1762
|
+
continue;
|
|
1763
|
+
}
|
|
1764
|
+
if (record.op === "end") {
|
|
1765
|
+
const handle = context.spanHandle(record.id);
|
|
1766
|
+
if (handle) {
|
|
1767
|
+
handle.end(endOptionsFromRecord(record, fallbackParentId));
|
|
1768
|
+
continue;
|
|
1769
|
+
}
|
|
1770
|
+
if (context.hasSpan(record.id))
|
|
1771
|
+
continue;
|
|
1772
|
+
recordFromRecord(context, options);
|
|
1773
|
+
continue;
|
|
1774
|
+
}
|
|
1775
|
+
if (context.hasSpan(record.id))
|
|
1776
|
+
continue;
|
|
1777
|
+
recordFromRecord(context, options);
|
|
1778
|
+
}
|
|
1779
|
+
}
|
|
1780
|
+
function assembleTurn(token, journal, options = {}) {
|
|
1781
|
+
const parsed = parseTurnContextToken(token);
|
|
1782
|
+
const context = new client_1.TraceContext({
|
|
1783
|
+
id: parsed.traceId,
|
|
1784
|
+
name: options.name ?? parsed.name ?? "trace",
|
|
1785
|
+
input: options.input,
|
|
1786
|
+
output: options.output,
|
|
1787
|
+
metadata: options.metadata,
|
|
1788
|
+
threadId: options.threadId ?? parsed.threadId,
|
|
1789
|
+
userId: options.userId ?? parsed.userId,
|
|
1790
|
+
durationMs: options.durationMs,
|
|
1791
|
+
startedAt: options.startedAt ?? parsed.startedAt
|
|
1792
|
+
});
|
|
1793
|
+
if (journal !== void 0)
|
|
1794
|
+
applyTurnJournal(context, journal);
|
|
1795
|
+
return { context, startedAt: new Date(parsed.startedAt) };
|
|
1796
|
+
}
|
|
1797
|
+
function spanFields(options, fallbackParentId) {
|
|
1798
|
+
const { parentSpanId, ...fields } = options;
|
|
1799
|
+
return compact({
|
|
1800
|
+
...fields,
|
|
1801
|
+
id: options.id ?? crypto.randomUUID(),
|
|
1802
|
+
parentId: options.parentId ?? parentSpanId ?? fallbackParentId ?? null,
|
|
1803
|
+
startedAt: asIso(options.startedAt),
|
|
1804
|
+
endedAt: asIso(options.endedAt)
|
|
1805
|
+
});
|
|
1806
|
+
}
|
|
1807
|
+
function asNamedOptions(options) {
|
|
1808
|
+
return typeof options === "string" ? { name: options } : options;
|
|
1809
|
+
}
|
|
1810
|
+
function attachedSpanOps(writeStart, writeRecord, defaultParentId) {
|
|
1811
|
+
const nest = (options) => ({
|
|
1812
|
+
...options,
|
|
1813
|
+
parentId: options.parentId ?? defaultParentId() ?? null
|
|
1814
|
+
});
|
|
1815
|
+
return {
|
|
1816
|
+
startSpan(options) {
|
|
1817
|
+
return writeStart("span", nest(asNamedOptions(options)));
|
|
1818
|
+
},
|
|
1819
|
+
startGeneration(options) {
|
|
1820
|
+
return writeStart("generation", nest(asNamedOptions(options)));
|
|
1821
|
+
},
|
|
1822
|
+
startTool(options) {
|
|
1823
|
+
return writeStart("tool", nest(asNamedOptions(options)));
|
|
1824
|
+
},
|
|
1825
|
+
recordSpan(options) {
|
|
1826
|
+
return writeRecord("span", nest(asNamedOptions(options)));
|
|
1827
|
+
},
|
|
1828
|
+
recordGeneration(options) {
|
|
1829
|
+
writeRecord("generation", nest(asNamedOptions(options)));
|
|
1830
|
+
},
|
|
1831
|
+
recordTool(options) {
|
|
1832
|
+
writeRecord("tool", nest(asNamedOptions(options)));
|
|
1833
|
+
}
|
|
1834
|
+
};
|
|
1835
|
+
}
|
|
1836
|
+
var AttachedSpanHandle = class {
|
|
1837
|
+
recorder;
|
|
1838
|
+
fields;
|
|
1839
|
+
ended;
|
|
1840
|
+
id;
|
|
1841
|
+
startSpan;
|
|
1842
|
+
startGeneration;
|
|
1843
|
+
startTool;
|
|
1844
|
+
recordSpan;
|
|
1845
|
+
recordGeneration;
|
|
1846
|
+
recordTool;
|
|
1847
|
+
constructor(recorder, fields, ended = false) {
|
|
1848
|
+
this.recorder = recorder;
|
|
1849
|
+
this.fields = fields;
|
|
1850
|
+
this.ended = ended;
|
|
1851
|
+
this.id = fields.id;
|
|
1852
|
+
const ops = attachedSpanOps((type, options) => this.recorder.writeStart(type, options), (type, options) => this.recorder.writeRecord(type, options), () => this.id);
|
|
1853
|
+
this.startSpan = ops.startSpan;
|
|
1854
|
+
this.startGeneration = ops.startGeneration;
|
|
1855
|
+
this.startTool = ops.startTool;
|
|
1856
|
+
this.recordSpan = ops.recordSpan;
|
|
1857
|
+
this.recordGeneration = ops.recordGeneration;
|
|
1858
|
+
this.recordTool = ops.recordTool;
|
|
1859
|
+
}
|
|
1860
|
+
end(options = {}) {
|
|
1861
|
+
if (this.ended)
|
|
1862
|
+
return;
|
|
1863
|
+
this.ended = true;
|
|
1864
|
+
this.recorder.append({
|
|
1865
|
+
op: "end",
|
|
1866
|
+
...spanFields({
|
|
1867
|
+
...this.fields,
|
|
1868
|
+
...options,
|
|
1869
|
+
id: this.id,
|
|
1870
|
+
name: this.fields.name ?? "span"
|
|
1871
|
+
}, this.fields.parentId),
|
|
1872
|
+
id: this.id,
|
|
1873
|
+
startedAt: this.fields.startedAt
|
|
1874
|
+
});
|
|
1875
|
+
}
|
|
1876
|
+
};
|
|
1877
|
+
exports.AttachedSpanHandle = AttachedSpanHandle;
|
|
1878
|
+
var AttachedTurn = class {
|
|
1879
|
+
token;
|
|
1880
|
+
journal = [];
|
|
1881
|
+
startSpan;
|
|
1882
|
+
startGeneration;
|
|
1883
|
+
startTool;
|
|
1884
|
+
recordSpan;
|
|
1885
|
+
recordGeneration;
|
|
1886
|
+
recordTool;
|
|
1887
|
+
constructor(token) {
|
|
1888
|
+
this.token = parseTurnContextToken(token);
|
|
1889
|
+
const ops = attachedSpanOps((type, options) => this.writeStart(type, options), (type, options) => this.writeRecord(type, options), () => this.token.parentSpanId);
|
|
1890
|
+
this.startSpan = ops.startSpan;
|
|
1891
|
+
this.startGeneration = ops.startGeneration;
|
|
1892
|
+
this.startTool = ops.startTool;
|
|
1893
|
+
this.recordSpan = ops.recordSpan;
|
|
1894
|
+
this.recordGeneration = ops.recordGeneration;
|
|
1895
|
+
this.recordTool = ops.recordTool;
|
|
1896
|
+
}
|
|
1897
|
+
get id() {
|
|
1898
|
+
return this.token.traceId;
|
|
1899
|
+
}
|
|
1900
|
+
append(record) {
|
|
1901
|
+
this.journal.push(record);
|
|
1902
|
+
}
|
|
1903
|
+
records() {
|
|
1904
|
+
return {
|
|
1905
|
+
version: exports.TURN_JOURNAL_VERSION,
|
|
1906
|
+
token: this.token,
|
|
1907
|
+
records: this.journal
|
|
1908
|
+
};
|
|
1909
|
+
}
|
|
1910
|
+
writeStart(type, options) {
|
|
1911
|
+
const fields = spanFields({
|
|
1912
|
+
...options,
|
|
1913
|
+
type,
|
|
1914
|
+
id: options.id ?? crypto.randomUUID(),
|
|
1915
|
+
startedAt: options.startedAt ?? isoNow()
|
|
1916
|
+
}, this.token.parentSpanId);
|
|
1917
|
+
this.append({ op: "start", ...fields, type });
|
|
1918
|
+
return new AttachedSpanHandle(this, { ...fields, type });
|
|
1919
|
+
}
|
|
1920
|
+
writeRecord(type, options) {
|
|
1921
|
+
const now = isoNow();
|
|
1922
|
+
const fields = spanFields({
|
|
1923
|
+
...options,
|
|
1924
|
+
type,
|
|
1925
|
+
id: options.id ?? crypto.randomUUID(),
|
|
1926
|
+
startedAt: options.startedAt ?? now,
|
|
1927
|
+
endedAt: options.endedAt === void 0 ? now : options.endedAt
|
|
1928
|
+
}, this.token.parentSpanId);
|
|
1929
|
+
this.append({ op: "record", ...fields, type });
|
|
1930
|
+
return new AttachedSpanHandle(this, { ...fields, type }, true);
|
|
1931
|
+
}
|
|
1932
|
+
};
|
|
1933
|
+
exports.AttachedTurn = AttachedTurn;
|
|
1934
|
+
var TurnHandle = class extends client_1.TraceHandle {
|
|
1935
|
+
export(options = {}) {
|
|
1936
|
+
const identity = this.identity();
|
|
1937
|
+
return compact({
|
|
1938
|
+
version: exports.TURN_CONTEXT_VERSION,
|
|
1939
|
+
traceId: this.id,
|
|
1940
|
+
parentSpanId: options.parentSpanId ?? null,
|
|
1941
|
+
threadId: identity.threadId,
|
|
1942
|
+
userId: identity.userId,
|
|
1943
|
+
startedAt: this.startedAt.toISOString(),
|
|
1944
|
+
name: identity.name
|
|
1945
|
+
});
|
|
1946
|
+
}
|
|
1947
|
+
apply(input) {
|
|
1948
|
+
applyTurnJournal(this, input);
|
|
1949
|
+
}
|
|
1950
|
+
};
|
|
1951
|
+
exports.TurnHandle = TurnHandle;
|
|
1952
|
+
function startTurn2(lemma, options = {}) {
|
|
1953
|
+
return new TurnHandle(options, async (trace, startedAt, endedAt) => {
|
|
1954
|
+
await lemma.ingest(trace, { startedAt, endedAt });
|
|
1955
|
+
});
|
|
1956
|
+
}
|
|
1957
|
+
function attachTurn(token) {
|
|
1958
|
+
return new AttachedTurn(token);
|
|
1959
|
+
}
|
|
1960
|
+
}
|
|
1961
|
+
});
|
|
1962
|
+
|
|
1413
1963
|
// ../../packages/ts/tracing/dist/coding-agent.js
|
|
1414
1964
|
var require_coding_agent = __commonJS({
|
|
1415
1965
|
"../../packages/ts/tracing/dist/coding-agent.js"(exports) {
|
|
@@ -1620,31 +2170,60 @@ var require_tool_result = __commonJS({
|
|
|
1620
2170
|
const record = asResultRecord(output);
|
|
1621
2171
|
if (!record)
|
|
1622
2172
|
return null;
|
|
1623
|
-
if (record
|
|
1624
|
-
|
|
1625
|
-
}
|
|
1626
|
-
const content = record.content;
|
|
1627
|
-
if (Array.isArray(content)) {
|
|
1628
|
-
const text = content.map((part) => {
|
|
1629
|
-
if (!part || typeof part !== "object")
|
|
1630
|
-
return "";
|
|
1631
|
-
const textValue = part.text;
|
|
1632
|
-
return typeof textValue === "string" ? textValue : "";
|
|
1633
|
-
}).filter(Boolean).join("\n").trim();
|
|
2173
|
+
if (isFlaggedFailure(record)) {
|
|
2174
|
+
const text = contentText(record.content);
|
|
1634
2175
|
if (text)
|
|
1635
2176
|
return text;
|
|
2177
|
+
const flaggedError = nonEmptyString(record.error);
|
|
2178
|
+
if (flaggedError)
|
|
2179
|
+
return flaggedError;
|
|
2180
|
+
const message = nonEmptyString(record.message);
|
|
2181
|
+
if (message)
|
|
2182
|
+
return message;
|
|
2183
|
+
const encoded = encodedPayloadError(record);
|
|
2184
|
+
if (encoded)
|
|
2185
|
+
return encoded;
|
|
2186
|
+
try {
|
|
2187
|
+
return JSON.stringify(record);
|
|
2188
|
+
} catch {
|
|
2189
|
+
return "Tool returned an error result";
|
|
2190
|
+
}
|
|
1636
2191
|
}
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
2192
|
+
return encodedPayloadError(record);
|
|
2193
|
+
}
|
|
2194
|
+
function isFlaggedFailure(record) {
|
|
2195
|
+
return record.isError === true || record.is_error === true || record.error === true || record.status === "error" || record.status === "failed" || record.success === false;
|
|
2196
|
+
}
|
|
2197
|
+
function encodedPayloadError(record) {
|
|
2198
|
+
const direct = nonEmptyString(record.error);
|
|
2199
|
+
if (direct)
|
|
2200
|
+
return direct;
|
|
2201
|
+
const structured = asResultRecord(record.structuredContent);
|
|
2202
|
+
const nested = structured ? nonEmptyString(structured.error) : null;
|
|
2203
|
+
if (nested)
|
|
2204
|
+
return nested;
|
|
2205
|
+
const text = contentText(record.content);
|
|
2206
|
+
if (!text)
|
|
2207
|
+
return null;
|
|
2208
|
+
const parsed = asResultRecord(text);
|
|
2209
|
+
return parsed ? nonEmptyString(parsed.error) : null;
|
|
2210
|
+
}
|
|
2211
|
+
function contentText(content) {
|
|
2212
|
+
if (!Array.isArray(content))
|
|
2213
|
+
return null;
|
|
2214
|
+
const text = content.map((part) => {
|
|
2215
|
+
if (!part || typeof part !== "object")
|
|
2216
|
+
return "";
|
|
2217
|
+
const textValue = part.text;
|
|
2218
|
+
return typeof textValue === "string" ? textValue : "";
|
|
2219
|
+
}).filter(Boolean).join("\n").trim();
|
|
2220
|
+
return text || null;
|
|
2221
|
+
}
|
|
2222
|
+
function nonEmptyString(value) {
|
|
2223
|
+
if (typeof value !== "string")
|
|
2224
|
+
return null;
|
|
2225
|
+
const trimmed = value.trim();
|
|
2226
|
+
return trimmed ? trimmed : null;
|
|
1648
2227
|
}
|
|
1649
2228
|
function asResultRecord(output) {
|
|
1650
2229
|
if (output == null)
|
|
@@ -1671,6 +2250,173 @@ var require_tool_result = __commonJS({
|
|
|
1671
2250
|
}
|
|
1672
2251
|
});
|
|
1673
2252
|
|
|
2253
|
+
// ../../packages/ts/tracing/dist/model.js
|
|
2254
|
+
var require_model = __commonJS({
|
|
2255
|
+
"../../packages/ts/tracing/dist/model.js"(exports) {
|
|
2256
|
+
"use strict";
|
|
2257
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
2258
|
+
exports.pickModelIdentity = pickModelIdentity;
|
|
2259
|
+
exports.pickGenerationModelIdentity = pickGenerationModelIdentity;
|
|
2260
|
+
var MODEL_KEYS = [
|
|
2261
|
+
"model",
|
|
2262
|
+
"model_name",
|
|
2263
|
+
"modelName",
|
|
2264
|
+
"model_id",
|
|
2265
|
+
"modelId",
|
|
2266
|
+
"ls_model_name"
|
|
2267
|
+
];
|
|
2268
|
+
var NESTED_CONTAINERS = [
|
|
2269
|
+
"response_metadata",
|
|
2270
|
+
"responseMetadata",
|
|
2271
|
+
"llm_output",
|
|
2272
|
+
"llmOutput",
|
|
2273
|
+
"generationInfo",
|
|
2274
|
+
"generation_info",
|
|
2275
|
+
"kwargs",
|
|
2276
|
+
"additional_kwargs",
|
|
2277
|
+
"message",
|
|
2278
|
+
"response",
|
|
2279
|
+
"_response"
|
|
2280
|
+
];
|
|
2281
|
+
function asNonEmptyString(value) {
|
|
2282
|
+
if (typeof value === "string" && value.trim())
|
|
2283
|
+
return value.trim();
|
|
2284
|
+
return void 0;
|
|
2285
|
+
}
|
|
2286
|
+
function asRecord(value) {
|
|
2287
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
2288
|
+
return void 0;
|
|
2289
|
+
}
|
|
2290
|
+
return value;
|
|
2291
|
+
}
|
|
2292
|
+
function pickModelIdentity(raw) {
|
|
2293
|
+
const record = asRecord(raw);
|
|
2294
|
+
if (!record)
|
|
2295
|
+
return void 0;
|
|
2296
|
+
for (const key of MODEL_KEYS) {
|
|
2297
|
+
const value = record[key];
|
|
2298
|
+
const asString = asNonEmptyString(value);
|
|
2299
|
+
if (asString)
|
|
2300
|
+
return asString;
|
|
2301
|
+
if (value && typeof value === "object" && !Array.isArray(value)) {
|
|
2302
|
+
const nested = pickModelIdentity(value);
|
|
2303
|
+
if (nested)
|
|
2304
|
+
return nested;
|
|
2305
|
+
}
|
|
2306
|
+
}
|
|
2307
|
+
for (const key of NESTED_CONTAINERS) {
|
|
2308
|
+
const nested = pickModelIdentity(record[key]);
|
|
2309
|
+
if (nested)
|
|
2310
|
+
return nested;
|
|
2311
|
+
}
|
|
2312
|
+
return void 0;
|
|
2313
|
+
}
|
|
2314
|
+
function pickGenerationModelIdentity(raw) {
|
|
2315
|
+
const direct = pickModelIdentity(raw);
|
|
2316
|
+
if (direct)
|
|
2317
|
+
return direct;
|
|
2318
|
+
const record = asRecord(raw);
|
|
2319
|
+
if (!record)
|
|
2320
|
+
return void 0;
|
|
2321
|
+
const generations = record.generations;
|
|
2322
|
+
if (!Array.isArray(generations))
|
|
2323
|
+
return void 0;
|
|
2324
|
+
for (const group of generations) {
|
|
2325
|
+
if (!Array.isArray(group)) {
|
|
2326
|
+
const fromGroup = pickModelIdentity(group);
|
|
2327
|
+
if (fromGroup)
|
|
2328
|
+
return fromGroup;
|
|
2329
|
+
continue;
|
|
2330
|
+
}
|
|
2331
|
+
for (const item of group) {
|
|
2332
|
+
const fromItem = pickModelIdentity(item);
|
|
2333
|
+
if (fromItem)
|
|
2334
|
+
return fromItem;
|
|
2335
|
+
}
|
|
2336
|
+
}
|
|
2337
|
+
return void 0;
|
|
2338
|
+
}
|
|
2339
|
+
}
|
|
2340
|
+
});
|
|
2341
|
+
|
|
2342
|
+
// ../../packages/ts/tracing/dist/reply-text.js
|
|
2343
|
+
var require_reply_text = __commonJS({
|
|
2344
|
+
"../../packages/ts/tracing/dist/reply-text.js"(exports) {
|
|
2345
|
+
"use strict";
|
|
2346
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
2347
|
+
exports.isReplyReasoning = isReplyReasoning;
|
|
2348
|
+
exports.readContentReply = readContentReply;
|
|
2349
|
+
exports.textFromGenerationOutput = textFromGenerationOutput;
|
|
2350
|
+
function isReplyReasoning(record) {
|
|
2351
|
+
return record.type === "reasoning" || typeof record.encrypted_content === "string";
|
|
2352
|
+
}
|
|
2353
|
+
function readContentReply(content) {
|
|
2354
|
+
if (!Array.isArray(content))
|
|
2355
|
+
return void 0;
|
|
2356
|
+
const parts = [];
|
|
2357
|
+
let recognized = false;
|
|
2358
|
+
for (const block of content) {
|
|
2359
|
+
if (!block || typeof block !== "object")
|
|
2360
|
+
continue;
|
|
2361
|
+
const record = block;
|
|
2362
|
+
if (isReplyReasoning(record)) {
|
|
2363
|
+
recognized = true;
|
|
2364
|
+
continue;
|
|
2365
|
+
}
|
|
2366
|
+
if ((record.type === "text" || record.type === "output_text") && typeof record.text === "string") {
|
|
2367
|
+
parts.push(record.text);
|
|
2368
|
+
recognized = true;
|
|
2369
|
+
}
|
|
2370
|
+
}
|
|
2371
|
+
return { parts, recognized };
|
|
2372
|
+
}
|
|
2373
|
+
function textFromContentParts(content) {
|
|
2374
|
+
if (typeof content === "string")
|
|
2375
|
+
return content;
|
|
2376
|
+
const reply = readContentReply(content);
|
|
2377
|
+
if (!reply || !reply.recognized)
|
|
2378
|
+
return void 0;
|
|
2379
|
+
return reply.parts.join("");
|
|
2380
|
+
}
|
|
2381
|
+
function textFromGenerationOutput(output) {
|
|
2382
|
+
if (!Array.isArray(output))
|
|
2383
|
+
return output;
|
|
2384
|
+
let text = "";
|
|
2385
|
+
let sawUnhandled = false;
|
|
2386
|
+
for (const item of output) {
|
|
2387
|
+
if (!item || typeof item !== "object") {
|
|
2388
|
+
sawUnhandled = true;
|
|
2389
|
+
continue;
|
|
2390
|
+
}
|
|
2391
|
+
const record = item;
|
|
2392
|
+
if (isReplyReasoning(record))
|
|
2393
|
+
continue;
|
|
2394
|
+
if (typeof record.text === "string") {
|
|
2395
|
+
text += record.text;
|
|
2396
|
+
continue;
|
|
2397
|
+
}
|
|
2398
|
+
if (typeof record.content === "string") {
|
|
2399
|
+
text += record.content;
|
|
2400
|
+
continue;
|
|
2401
|
+
}
|
|
2402
|
+
if (Array.isArray(record.content)) {
|
|
2403
|
+
const fromParts = textFromContentParts(record.content);
|
|
2404
|
+
if (fromParts !== void 0) {
|
|
2405
|
+
text += fromParts;
|
|
2406
|
+
continue;
|
|
2407
|
+
}
|
|
2408
|
+
}
|
|
2409
|
+
sawUnhandled = true;
|
|
2410
|
+
}
|
|
2411
|
+
if (text)
|
|
2412
|
+
return text;
|
|
2413
|
+
if (!sawUnhandled && output.length > 0)
|
|
2414
|
+
return "";
|
|
2415
|
+
return output;
|
|
2416
|
+
}
|
|
2417
|
+
}
|
|
2418
|
+
});
|
|
2419
|
+
|
|
1674
2420
|
// ../../packages/ts/tracing/dist/langchain.js
|
|
1675
2421
|
var require_langchain = __commonJS({
|
|
1676
2422
|
"../../packages/ts/tracing/dist/langchain.js"(exports) {
|
|
@@ -1683,6 +2429,8 @@ var require_langchain = __commonJS({
|
|
|
1683
2429
|
var error_message_1 = require_error_message();
|
|
1684
2430
|
var schedule_1 = require_schedule();
|
|
1685
2431
|
var tool_result_1 = require_tool_result();
|
|
2432
|
+
var model_1 = require_model();
|
|
2433
|
+
var reply_text_1 = require_reply_text();
|
|
1686
2434
|
var usage_1 = require_usage();
|
|
1687
2435
|
var INTEGRATION_ATTRS = {
|
|
1688
2436
|
"lemma.sdk.integration": "langchain"
|
|
@@ -1739,26 +2487,6 @@ var require_langchain = __commonJS({
|
|
|
1739
2487
|
}
|
|
1740
2488
|
return fallback;
|
|
1741
2489
|
}
|
|
1742
|
-
function modelName(serialized, extraParams) {
|
|
1743
|
-
const kwargs = serialized?.kwargs;
|
|
1744
|
-
const sources = [kwargs, serialized, extraParams];
|
|
1745
|
-
for (const source of sources) {
|
|
1746
|
-
if (!source)
|
|
1747
|
-
continue;
|
|
1748
|
-
for (const key of [
|
|
1749
|
-
"model",
|
|
1750
|
-
"modelName",
|
|
1751
|
-
"model_name",
|
|
1752
|
-
"model_id",
|
|
1753
|
-
"modelId"
|
|
1754
|
-
]) {
|
|
1755
|
-
const value = source[key];
|
|
1756
|
-
if (typeof value === "string" && value)
|
|
1757
|
-
return value;
|
|
1758
|
-
}
|
|
1759
|
-
}
|
|
1760
|
-
return void 0;
|
|
1761
|
-
}
|
|
1762
2490
|
function lookupString(sources, keys) {
|
|
1763
2491
|
for (const source of sources) {
|
|
1764
2492
|
if (!source)
|
|
@@ -1944,11 +2672,24 @@ var require_langchain = __commonJS({
|
|
|
1944
2672
|
}
|
|
1945
2673
|
return input;
|
|
1946
2674
|
}
|
|
2675
|
+
function textFromContentBlocks(content) {
|
|
2676
|
+
const reply = (0, reply_text_1.readContentReply)(content);
|
|
2677
|
+
if (!reply || reply.parts.length === 0)
|
|
2678
|
+
return void 0;
|
|
2679
|
+
return reply.parts.join("\n");
|
|
2680
|
+
}
|
|
2681
|
+
function flattenRootDisplay(value) {
|
|
2682
|
+
const display = textFromContentBlocks(value);
|
|
2683
|
+
return display !== void 0 ? display : value;
|
|
2684
|
+
}
|
|
1947
2685
|
function rootTraceOutput(output) {
|
|
1948
2686
|
if (output == null)
|
|
1949
2687
|
return output;
|
|
1950
2688
|
if (typeof output === "string")
|
|
1951
2689
|
return output;
|
|
2690
|
+
const display = textFromContentBlocks(output);
|
|
2691
|
+
if (display !== void 0)
|
|
2692
|
+
return display;
|
|
1952
2693
|
if (output && typeof output === "object" && !Array.isArray(output)) {
|
|
1953
2694
|
const record = output;
|
|
1954
2695
|
if (record.role === "assistant" && (record.tool_calls != null || record.toolCalls != null)) {
|
|
@@ -1960,10 +2701,10 @@ var require_langchain = __commonJS({
|
|
|
1960
2701
|
for (let i = messages.length - 1; i >= 0; i--) {
|
|
1961
2702
|
const normalized = normalizeMessage(messages[i]);
|
|
1962
2703
|
if (normalized.role === "assistant") {
|
|
1963
|
-
return structuredAssistantOutput(normalized);
|
|
2704
|
+
return flattenRootDisplay(structuredAssistantOutput(normalized));
|
|
1964
2705
|
}
|
|
1965
2706
|
}
|
|
1966
|
-
return structuredAssistantOutput(normalizeMessage(messages[messages.length - 1]));
|
|
2707
|
+
return flattenRootDisplay(structuredAssistantOutput(normalizeMessage(messages[messages.length - 1])));
|
|
1967
2708
|
}
|
|
1968
2709
|
if (output && typeof output === "object" && !Array.isArray(output)) {
|
|
1969
2710
|
const record = output;
|
|
@@ -1971,10 +2712,16 @@ var require_langchain = __commonJS({
|
|
|
1971
2712
|
const value = record[key];
|
|
1972
2713
|
if (typeof value === "string" && value)
|
|
1973
2714
|
return value;
|
|
2715
|
+
const fromBlocks = textFromContentBlocks(value);
|
|
2716
|
+
if (fromBlocks !== void 0)
|
|
2717
|
+
return fromBlocks;
|
|
1974
2718
|
if (value && typeof value === "object") {
|
|
1975
2719
|
const nested = value;
|
|
1976
2720
|
if (typeof nested.content === "string")
|
|
1977
2721
|
return nested.content;
|
|
2722
|
+
const nestedBlocks = textFromContentBlocks(nested.content);
|
|
2723
|
+
if (nestedBlocks !== void 0)
|
|
2724
|
+
return nestedBlocks;
|
|
1978
2725
|
}
|
|
1979
2726
|
}
|
|
1980
2727
|
}
|
|
@@ -2119,9 +2866,14 @@ var require_langchain = __commonJS({
|
|
|
2119
2866
|
}
|
|
2120
2867
|
return void 0;
|
|
2121
2868
|
}
|
|
2122
|
-
function llmProvider(serialized, extraParams) {
|
|
2869
|
+
function llmProvider(serialized, extraParams, metadata) {
|
|
2123
2870
|
const kwargs = serialized?.kwargs;
|
|
2124
|
-
const sources = [
|
|
2871
|
+
const sources = [
|
|
2872
|
+
kwargs,
|
|
2873
|
+
serialized,
|
|
2874
|
+
extraParams,
|
|
2875
|
+
metadata
|
|
2876
|
+
];
|
|
2125
2877
|
for (const source of sources) {
|
|
2126
2878
|
if (!source)
|
|
2127
2879
|
continue;
|
|
@@ -2152,9 +2904,22 @@ var require_langchain = __commonJS({
|
|
|
2152
2904
|
}
|
|
2153
2905
|
return void 0;
|
|
2154
2906
|
}
|
|
2907
|
+
function resolveGenerationIdentity(serialized, extraParams, metadata) {
|
|
2908
|
+
return {
|
|
2909
|
+
provider: llmProvider(serialized, extraParams, metadata),
|
|
2910
|
+
model: (0, model_1.pickModelIdentity)(serialized?.kwargs) ?? (0, model_1.pickModelIdentity)(serialized) ?? (0, model_1.pickModelIdentity)(extraParams) ?? (0, model_1.pickModelIdentity)(metadata)
|
|
2911
|
+
};
|
|
2912
|
+
}
|
|
2155
2913
|
function durationMs(start, end) {
|
|
2156
2914
|
return Math.max(0, end.getTime() - start.getTime());
|
|
2157
2915
|
}
|
|
2916
|
+
var NO_OUTPUT = { result: "none" };
|
|
2917
|
+
function recordedSpanOutput(output, ownsTrace, status) {
|
|
2918
|
+
if (output == null && !ownsTrace && status !== "ERROR") {
|
|
2919
|
+
return { ...NO_OUTPUT };
|
|
2920
|
+
}
|
|
2921
|
+
return output;
|
|
2922
|
+
}
|
|
2158
2923
|
function langchainAttributes(runId, parentRunId, runType) {
|
|
2159
2924
|
return Object.fromEntries(Object.entries({
|
|
2160
2925
|
...INTEGRATION_ATTRS,
|
|
@@ -2197,6 +2962,12 @@ var require_langchain = __commonJS({
|
|
|
2197
2962
|
var LemmaLangChainCallbackHandler = class {
|
|
2198
2963
|
options;
|
|
2199
2964
|
name = "lemma";
|
|
2965
|
+
raise_error = false;
|
|
2966
|
+
run_inline = false;
|
|
2967
|
+
ignore_llm = false;
|
|
2968
|
+
ignore_chain = false;
|
|
2969
|
+
ignore_agent = false;
|
|
2970
|
+
ignore_retriever = false;
|
|
2200
2971
|
lemma;
|
|
2201
2972
|
runs = /* @__PURE__ */ new Map();
|
|
2202
2973
|
traces = /* @__PURE__ */ new Map();
|
|
@@ -2217,7 +2988,13 @@ var require_langchain = __commonJS({
|
|
|
2217
2988
|
}
|
|
2218
2989
|
resolveThreadId(metadata, tags) {
|
|
2219
2990
|
const key = this.options.threadIdKey ?? "threadId";
|
|
2220
|
-
const keys = [
|
|
2991
|
+
const keys = [
|
|
2992
|
+
key,
|
|
2993
|
+
"threadId",
|
|
2994
|
+
"thread_id",
|
|
2995
|
+
"conversation_id",
|
|
2996
|
+
"session_id"
|
|
2997
|
+
];
|
|
2221
2998
|
return lookupString([metadata, this.options.metadata], keys) ?? tagValue(tags, keys);
|
|
2222
2999
|
}
|
|
2223
3000
|
resolveUserId(metadata, tags) {
|
|
@@ -2348,6 +3125,14 @@ var require_langchain = __commonJS({
|
|
|
2348
3125
|
this.runs.delete(runId);
|
|
2349
3126
|
}
|
|
2350
3127
|
}
|
|
3128
|
+
endStoredRun(run, options) {
|
|
3129
|
+
if (!run.handle)
|
|
3130
|
+
return;
|
|
3131
|
+
run.handle.end({
|
|
3132
|
+
...options,
|
|
3133
|
+
output: recordedSpanOutput(options.output, run.ownsTrace, options.status)
|
|
3134
|
+
});
|
|
3135
|
+
}
|
|
2351
3136
|
async finalizeTrace(owningTraceId, stored) {
|
|
2352
3137
|
this.traces.delete(owningTraceId);
|
|
2353
3138
|
this.forgetTraceRuns(owningTraceId);
|
|
@@ -2451,13 +3236,11 @@ var require_langchain = __commonJS({
|
|
|
2451
3236
|
return;
|
|
2452
3237
|
const endedAt = /* @__PURE__ */ new Date();
|
|
2453
3238
|
const stored = this.storedTrace(run.owningTraceId);
|
|
2454
|
-
|
|
2455
|
-
|
|
2456
|
-
|
|
2457
|
-
|
|
2458
|
-
|
|
2459
|
-
});
|
|
2460
|
-
}
|
|
3239
|
+
this.endStoredRun(run, {
|
|
3240
|
+
output: outputs,
|
|
3241
|
+
endedAt,
|
|
3242
|
+
durationMs: durationMs(run.startedAt, endedAt)
|
|
3243
|
+
});
|
|
2461
3244
|
if (stored) {
|
|
2462
3245
|
this.noteBounds(stored, run.startedAt, endedAt);
|
|
2463
3246
|
if (run.ownsTrace) {
|
|
@@ -2476,14 +3259,12 @@ var require_langchain = __commonJS({
|
|
|
2476
3259
|
const endedAt = /* @__PURE__ */ new Date();
|
|
2477
3260
|
const message = (0, error_message_1.describeError)(error);
|
|
2478
3261
|
const stored = this.storedTrace(run.owningTraceId);
|
|
2479
|
-
|
|
2480
|
-
|
|
2481
|
-
|
|
2482
|
-
|
|
2483
|
-
|
|
2484
|
-
|
|
2485
|
-
});
|
|
2486
|
-
}
|
|
3262
|
+
this.endStoredRun(run, {
|
|
3263
|
+
status: "ERROR",
|
|
3264
|
+
error: message,
|
|
3265
|
+
endedAt,
|
|
3266
|
+
durationMs: durationMs(run.startedAt, endedAt)
|
|
3267
|
+
});
|
|
2487
3268
|
if (stored) {
|
|
2488
3269
|
this.noteBounds(stored, run.startedAt, endedAt);
|
|
2489
3270
|
if (run.ownsTrace) {
|
|
@@ -2495,7 +3276,7 @@ var require_langchain = __commonJS({
|
|
|
2495
3276
|
await this.finalizeTrace(run.owningTraceId, stored);
|
|
2496
3277
|
}
|
|
2497
3278
|
}
|
|
2498
|
-
handleLLMStart(serialized, prompts, runId, parentRunId, extraParams, tags, metadata) {
|
|
3279
|
+
handleLLMStart(serialized, prompts, runId, parentRunId, extraParams, tags, metadata, _runName) {
|
|
2499
3280
|
const startedAt = /* @__PURE__ */ new Date();
|
|
2500
3281
|
const attachment = this.resolveAttachment(runId, parentRunId, () => this.createOwnedTrace(runId, this.traceName(serialized, "langchain-llm"), prompts, "llm", metadata, tags));
|
|
2501
3282
|
if (attachment.ownsTrace) {
|
|
@@ -2503,8 +3284,7 @@ var require_langchain = __commonJS({
|
|
|
2503
3284
|
}
|
|
2504
3285
|
this.applyIdentity(attachment.stored, metadata, tags);
|
|
2505
3286
|
this.noteBounds(attachment.stored, startedAt, void 0);
|
|
2506
|
-
const provider =
|
|
2507
|
-
const model = modelName(serialized, extraParams);
|
|
3287
|
+
const { provider, model } = resolveGenerationIdentity(serialized, extraParams, metadata);
|
|
2508
3288
|
const handle = attachment.stored.handle.startGeneration({
|
|
2509
3289
|
name: serializedName(serialized, "langchain-llm"),
|
|
2510
3290
|
parentId: attachment.parentId,
|
|
@@ -2527,7 +3307,7 @@ var require_langchain = __commonJS({
|
|
|
2527
3307
|
ownsTrace: attachment.ownsTrace
|
|
2528
3308
|
});
|
|
2529
3309
|
}
|
|
2530
|
-
handleChatModelStart(serialized, messages, runId, parentRunId, extraParams, tags, metadata) {
|
|
3310
|
+
handleChatModelStart(serialized, messages, runId, parentRunId, extraParams, tags, metadata, _runName) {
|
|
2531
3311
|
const startedAt = /* @__PURE__ */ new Date();
|
|
2532
3312
|
const flatMessages = messages.flat();
|
|
2533
3313
|
const normalized = normalizeMessages(flatMessages);
|
|
@@ -2537,8 +3317,7 @@ var require_langchain = __commonJS({
|
|
|
2537
3317
|
}
|
|
2538
3318
|
this.applyIdentity(attachment.stored, metadata, tags);
|
|
2539
3319
|
this.noteBounds(attachment.stored, startedAt, void 0);
|
|
2540
|
-
const provider =
|
|
2541
|
-
const model = modelName(serialized, extraParams);
|
|
3320
|
+
const { provider, model } = resolveGenerationIdentity(serialized, extraParams, metadata);
|
|
2542
3321
|
const handle = attachment.stored.handle.startGeneration({
|
|
2543
3322
|
name: serializedName(serialized, "langchain-chat-model"),
|
|
2544
3323
|
parentId: attachment.parentId,
|
|
@@ -2578,14 +3357,16 @@ var require_langchain = __commonJS({
|
|
|
2578
3357
|
const outputMessages = llmOutputMessages(output);
|
|
2579
3358
|
const softError = (0, tool_result_1.toolResultError)(structured);
|
|
2580
3359
|
const awaitingTools = !softError && hasToolCalls(structured);
|
|
2581
|
-
|
|
3360
|
+
const model = (0, model_1.pickGenerationModelIdentity)(output);
|
|
3361
|
+
this.endStoredRun(run, {
|
|
2582
3362
|
output: softError ? void 0 : structured,
|
|
2583
3363
|
error: softError ?? void 0,
|
|
2584
3364
|
status: softError ? "ERROR" : void 0,
|
|
2585
3365
|
endedAt,
|
|
2586
3366
|
durationMs: durationMs(run.startedAt, endedAt),
|
|
2587
3367
|
llmOutputMessages: softError ? void 0 : outputMessages,
|
|
2588
|
-
usage: llmTokenUsage(output)
|
|
3368
|
+
usage: llmTokenUsage(output),
|
|
3369
|
+
...model ? { model } : {}
|
|
2589
3370
|
});
|
|
2590
3371
|
const stored = this.storedTrace(run.owningTraceId);
|
|
2591
3372
|
if (stored) {
|
|
@@ -2631,7 +3412,7 @@ var require_langchain = __commonJS({
|
|
|
2631
3412
|
return;
|
|
2632
3413
|
const endedAt = /* @__PURE__ */ new Date();
|
|
2633
3414
|
const message = (0, error_message_1.describeError)(error);
|
|
2634
|
-
|
|
3415
|
+
this.endStoredRun(run, {
|
|
2635
3416
|
status: "ERROR",
|
|
2636
3417
|
error: message,
|
|
2637
3418
|
endedAt,
|
|
@@ -2650,7 +3431,7 @@ var require_langchain = __commonJS({
|
|
|
2650
3431
|
}
|
|
2651
3432
|
void this.finalizeDeferredOwner(run.owningTraceId, endedAt, message);
|
|
2652
3433
|
}
|
|
2653
|
-
handleToolStart(serialized, input, runId, parentRunId, tags, metadata) {
|
|
3434
|
+
handleToolStart(serialized, input, runId, parentRunId, tags, metadata, _runName, _toolCallId) {
|
|
2654
3435
|
const startedAt = /* @__PURE__ */ new Date();
|
|
2655
3436
|
const attachment = this.resolveAttachment(runId, parentRunId, () => this.createOwnedTrace(runId, this.traceName(serialized, "langchain-tool"), input, "tool", metadata, tags));
|
|
2656
3437
|
if (attachment.ownsTrace) {
|
|
@@ -2685,14 +3466,14 @@ var require_langchain = __commonJS({
|
|
|
2685
3466
|
const endedAt = /* @__PURE__ */ new Date();
|
|
2686
3467
|
const softError = (0, tool_result_1.toolResultError)(output);
|
|
2687
3468
|
if (softError) {
|
|
2688
|
-
|
|
3469
|
+
this.endStoredRun(run, {
|
|
2689
3470
|
status: "ERROR",
|
|
2690
3471
|
error: softError,
|
|
2691
3472
|
endedAt,
|
|
2692
3473
|
durationMs: durationMs(run.startedAt, endedAt)
|
|
2693
3474
|
});
|
|
2694
3475
|
} else {
|
|
2695
|
-
|
|
3476
|
+
this.endStoredRun(run, {
|
|
2696
3477
|
output,
|
|
2697
3478
|
endedAt,
|
|
2698
3479
|
durationMs: durationMs(run.startedAt, endedAt)
|
|
@@ -2717,7 +3498,7 @@ var require_langchain = __commonJS({
|
|
|
2717
3498
|
return;
|
|
2718
3499
|
const endedAt = /* @__PURE__ */ new Date();
|
|
2719
3500
|
const message = (0, error_message_1.describeError)(error);
|
|
2720
|
-
|
|
3501
|
+
this.endStoredRun(run, {
|
|
2721
3502
|
status: "ERROR",
|
|
2722
3503
|
error: message,
|
|
2723
3504
|
endedAt,
|
|
@@ -2736,7 +3517,7 @@ var require_langchain = __commonJS({
|
|
|
2736
3517
|
}
|
|
2737
3518
|
void this.finalizeDeferredOwner(run.owningTraceId, endedAt, message);
|
|
2738
3519
|
}
|
|
2739
|
-
handleRetrieverStart(serialized, query, runId, parentRunId, tags, metadata) {
|
|
3520
|
+
handleRetrieverStart(serialized, query, runId, parentRunId, tags, metadata, _name) {
|
|
2740
3521
|
const startedAt = /* @__PURE__ */ new Date();
|
|
2741
3522
|
const attachment = this.resolveAttachment(runId, parentRunId, () => this.createOwnedTrace(runId, this.traceName(serialized, "langchain-retriever"), query, "retriever", metadata, tags));
|
|
2742
3523
|
if (attachment.ownsTrace) {
|
|
@@ -2767,7 +3548,7 @@ var require_langchain = __commonJS({
|
|
|
2767
3548
|
if (!run)
|
|
2768
3549
|
return;
|
|
2769
3550
|
const endedAt = /* @__PURE__ */ new Date();
|
|
2770
|
-
|
|
3551
|
+
this.endStoredRun(run, {
|
|
2771
3552
|
output: documents,
|
|
2772
3553
|
endedAt,
|
|
2773
3554
|
durationMs: durationMs(run.startedAt, endedAt)
|
|
@@ -2787,7 +3568,7 @@ var require_langchain = __commonJS({
|
|
|
2787
3568
|
return;
|
|
2788
3569
|
const endedAt = /* @__PURE__ */ new Date();
|
|
2789
3570
|
const message = (0, error_message_1.describeError)(error);
|
|
2790
|
-
|
|
3571
|
+
this.endStoredRun(run, {
|
|
2791
3572
|
status: "ERROR",
|
|
2792
3573
|
error: message,
|
|
2793
3574
|
endedAt,
|
|
@@ -2837,6 +3618,7 @@ var require_mastra = __commonJS({
|
|
|
2837
3618
|
var client_1 = require_client();
|
|
2838
3619
|
var error_message_1 = require_error_message();
|
|
2839
3620
|
var schedule_1 = require_schedule();
|
|
3621
|
+
var model_1 = require_model();
|
|
2840
3622
|
var tool_result_1 = require_tool_result();
|
|
2841
3623
|
var usage_1 = require_usage();
|
|
2842
3624
|
var INTEGRATION_ATTRS = {
|
|
@@ -3087,7 +3869,7 @@ var require_mastra = __commonJS({
|
|
|
3087
3869
|
trace.recordGeneration({
|
|
3088
3870
|
...base,
|
|
3089
3871
|
name: this.options.generationName ?? span.name,
|
|
3090
|
-
model:
|
|
3872
|
+
model: (0, model_1.pickModelIdentity)(attrs) ?? (0, model_1.pickModelIdentity)(span.output),
|
|
3091
3873
|
llmProvider: attributeString(attrs, "provider"),
|
|
3092
3874
|
llmInvocationParameters: attrs?.parameters,
|
|
3093
3875
|
llmInputMessages,
|
|
@@ -3196,6 +3978,231 @@ var require_mastra = __commonJS({
|
|
|
3196
3978
|
}
|
|
3197
3979
|
});
|
|
3198
3980
|
|
|
3981
|
+
// ../../packages/ts/tracing/dist/openai-agents-generation.js
|
|
3982
|
+
var require_openai_agents_generation = __commonJS({
|
|
3983
|
+
"../../packages/ts/tracing/dist/openai-agents-generation.js"(exports) {
|
|
3984
|
+
"use strict";
|
|
3985
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3986
|
+
exports.asResponseRecord = asResponseRecord;
|
|
3987
|
+
exports.responseRecord = responseRecord;
|
|
3988
|
+
exports.spanDataForAttribute = spanDataForAttribute;
|
|
3989
|
+
exports.generationSnapshot = generationSnapshot;
|
|
3990
|
+
exports.replyFromResponse = replyFromResponse;
|
|
3991
|
+
var usage_1 = require_usage();
|
|
3992
|
+
var RESPONSE_SETTING_KEYS = [
|
|
3993
|
+
"temperature",
|
|
3994
|
+
"top_p",
|
|
3995
|
+
"max_output_tokens",
|
|
3996
|
+
"max_tool_calls",
|
|
3997
|
+
"tool_choice",
|
|
3998
|
+
"parallel_tool_calls",
|
|
3999
|
+
"truncation",
|
|
4000
|
+
"reasoning",
|
|
4001
|
+
"text",
|
|
4002
|
+
"service_tier"
|
|
4003
|
+
];
|
|
4004
|
+
var HISTORY_ITEM_ROLES = {
|
|
4005
|
+
function_call: "assistant",
|
|
4006
|
+
reasoning: "assistant",
|
|
4007
|
+
function_call_result: "tool",
|
|
4008
|
+
function_call_output: "tool"
|
|
4009
|
+
};
|
|
4010
|
+
function asResponseRecord(value) {
|
|
4011
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
4012
|
+
return void 0;
|
|
4013
|
+
}
|
|
4014
|
+
const toJSON = value.toJSON;
|
|
4015
|
+
if (typeof toJSON === "function") {
|
|
4016
|
+
try {
|
|
4017
|
+
const dumped = toJSON.call(value);
|
|
4018
|
+
if (dumped && typeof dumped === "object" && !Array.isArray(dumped)) {
|
|
4019
|
+
return { ...dumped };
|
|
4020
|
+
}
|
|
4021
|
+
} catch {
|
|
4022
|
+
}
|
|
4023
|
+
}
|
|
4024
|
+
return { ...value };
|
|
4025
|
+
}
|
|
4026
|
+
function responseRecord(data) {
|
|
4027
|
+
return asResponseRecord(data["response"] ?? data["_response"]);
|
|
4028
|
+
}
|
|
4029
|
+
var RESPONSE_BODY_KEYS = [
|
|
4030
|
+
"output",
|
|
4031
|
+
"output_text",
|
|
4032
|
+
"instructions",
|
|
4033
|
+
"input"
|
|
4034
|
+
];
|
|
4035
|
+
function spanDataForAttribute(data) {
|
|
4036
|
+
const copy = { ...data };
|
|
4037
|
+
delete copy._input;
|
|
4038
|
+
if (copy.type === "response") {
|
|
4039
|
+
for (const key of RESPONSE_BODY_KEYS)
|
|
4040
|
+
delete copy[key];
|
|
4041
|
+
}
|
|
4042
|
+
for (const key of ["response", "_response"]) {
|
|
4043
|
+
if (!(key in copy))
|
|
4044
|
+
continue;
|
|
4045
|
+
const response = asResponseRecord(copy[key]);
|
|
4046
|
+
if (!response)
|
|
4047
|
+
continue;
|
|
4048
|
+
for (const bodyKey of RESPONSE_BODY_KEYS)
|
|
4049
|
+
delete response[bodyKey];
|
|
4050
|
+
copy[key] = response;
|
|
4051
|
+
}
|
|
4052
|
+
return copy;
|
|
4053
|
+
}
|
|
4054
|
+
function instructionsText(value) {
|
|
4055
|
+
return typeof value === "string" && value ? value : void 0;
|
|
4056
|
+
}
|
|
4057
|
+
function isSameSystemMessage(message, instructions) {
|
|
4058
|
+
if (!message || typeof message !== "object" || Array.isArray(message)) {
|
|
4059
|
+
return false;
|
|
4060
|
+
}
|
|
4061
|
+
const record = message;
|
|
4062
|
+
return record.role === "system" && record.content === instructions;
|
|
4063
|
+
}
|
|
4064
|
+
function withHistoryRole(message) {
|
|
4065
|
+
if (!message || typeof message !== "object" || Array.isArray(message)) {
|
|
4066
|
+
return message;
|
|
4067
|
+
}
|
|
4068
|
+
const record = message;
|
|
4069
|
+
if (typeof record.role === "string" && record.role)
|
|
4070
|
+
return message;
|
|
4071
|
+
const role = typeof record.type === "string" ? HISTORY_ITEM_ROLES[record.type] : void 0;
|
|
4072
|
+
if (!role)
|
|
4073
|
+
return message;
|
|
4074
|
+
return { ...record, role };
|
|
4075
|
+
}
|
|
4076
|
+
function historyMessages(messages) {
|
|
4077
|
+
return messages.map(withHistoryRole);
|
|
4078
|
+
}
|
|
4079
|
+
function inputMessages(input, response) {
|
|
4080
|
+
const instructions = instructionsText(response?.instructions);
|
|
4081
|
+
const messages = Array.isArray(input) ? input : void 0;
|
|
4082
|
+
if (!instructions)
|
|
4083
|
+
return messages ? historyMessages(messages) : void 0;
|
|
4084
|
+
if (messages?.some((message) => isSameSystemMessage(message, instructions))) {
|
|
4085
|
+
return historyMessages(messages);
|
|
4086
|
+
}
|
|
4087
|
+
const system = { role: "system", content: instructions };
|
|
4088
|
+
if (!messages) {
|
|
4089
|
+
if (typeof input === "string" && input) {
|
|
4090
|
+
return historyMessages([system, { role: "user", content: input }]);
|
|
4091
|
+
}
|
|
4092
|
+
return historyMessages([system]);
|
|
4093
|
+
}
|
|
4094
|
+
return historyMessages([system, ...messages]);
|
|
4095
|
+
}
|
|
4096
|
+
function generationTools(data, response) {
|
|
4097
|
+
if (response && response.tools != null)
|
|
4098
|
+
return response.tools;
|
|
4099
|
+
if (data["tools"] != null)
|
|
4100
|
+
return data["tools"];
|
|
4101
|
+
return void 0;
|
|
4102
|
+
}
|
|
4103
|
+
function invocationParameters(data, response) {
|
|
4104
|
+
if (data["model_config"] != null)
|
|
4105
|
+
return data["model_config"];
|
|
4106
|
+
if (!response)
|
|
4107
|
+
return void 0;
|
|
4108
|
+
const settings = {};
|
|
4109
|
+
for (const key of RESPONSE_SETTING_KEYS) {
|
|
4110
|
+
const value = response[key];
|
|
4111
|
+
if (value != null)
|
|
4112
|
+
settings[key] = value;
|
|
4113
|
+
}
|
|
4114
|
+
return Object.keys(settings).length > 0 ? settings : void 0;
|
|
4115
|
+
}
|
|
4116
|
+
function outputMessages(data, response) {
|
|
4117
|
+
if (data.type !== "generation" && data.type !== "response")
|
|
4118
|
+
return void 0;
|
|
4119
|
+
if (data.type === "generation" && Array.isArray(data["output"])) {
|
|
4120
|
+
return data["output"];
|
|
4121
|
+
}
|
|
4122
|
+
if (response && Array.isArray(response.output)) {
|
|
4123
|
+
return response.output;
|
|
4124
|
+
}
|
|
4125
|
+
if (Array.isArray(data["output"]))
|
|
4126
|
+
return data["output"];
|
|
4127
|
+
return void 0;
|
|
4128
|
+
}
|
|
4129
|
+
function usageFor(data, response) {
|
|
4130
|
+
const direct = (0, usage_1.normalizeTokenUsage)(data["usage"]);
|
|
4131
|
+
if (direct)
|
|
4132
|
+
return direct;
|
|
4133
|
+
if (!response)
|
|
4134
|
+
return void 0;
|
|
4135
|
+
return (0, usage_1.normalizeTokenUsage)(response.usage);
|
|
4136
|
+
}
|
|
4137
|
+
function generationSnapshot(data, input) {
|
|
4138
|
+
const response = responseRecord(data);
|
|
4139
|
+
const messages = inputMessages(input, response);
|
|
4140
|
+
const parameters = invocationParameters(data, response);
|
|
4141
|
+
const tools = generationTools(data, response);
|
|
4142
|
+
return {
|
|
4143
|
+
response,
|
|
4144
|
+
fields: {
|
|
4145
|
+
...messages ? { llmInputMessages: messages } : {},
|
|
4146
|
+
...parameters !== void 0 ? { llmInvocationParameters: parameters } : {},
|
|
4147
|
+
...tools !== void 0 ? { llmTools: tools } : {}
|
|
4148
|
+
},
|
|
4149
|
+
outputMessages: outputMessages(data, response),
|
|
4150
|
+
usage: usageFor(data, response)
|
|
4151
|
+
};
|
|
4152
|
+
}
|
|
4153
|
+
function replyFromResponse(data, response, textFromList) {
|
|
4154
|
+
if (!response) {
|
|
4155
|
+
const raw = data["response"] ?? data["_response"];
|
|
4156
|
+
if (raw == null)
|
|
4157
|
+
return textFromList(data["output"]);
|
|
4158
|
+
return raw;
|
|
4159
|
+
}
|
|
4160
|
+
if (typeof response.output_text === "string")
|
|
4161
|
+
return response.output_text;
|
|
4162
|
+
if (Array.isArray(response.output))
|
|
4163
|
+
return textFromList(response.output);
|
|
4164
|
+
return response;
|
|
4165
|
+
}
|
|
4166
|
+
}
|
|
4167
|
+
});
|
|
4168
|
+
|
|
4169
|
+
// ../../packages/ts/tracing/dist/openai-agents-containers.js
|
|
4170
|
+
var require_openai_agents_containers = __commonJS({
|
|
4171
|
+
"../../packages/ts/tracing/dist/openai-agents-containers.js"(exports) {
|
|
4172
|
+
"use strict";
|
|
4173
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4174
|
+
exports.isContainerType = isContainerType;
|
|
4175
|
+
exports.propagateGenerationIo = propagateGenerationIo;
|
|
4176
|
+
function isContainerType(type) {
|
|
4177
|
+
return type === "agent" || type === "task" || type === "turn";
|
|
4178
|
+
}
|
|
4179
|
+
function propagateGenerationIo(parentId, io, open, ended) {
|
|
4180
|
+
if (io.input === void 0 && io.output === void 0)
|
|
4181
|
+
return;
|
|
4182
|
+
const seen = /* @__PURE__ */ new Set();
|
|
4183
|
+
let id = parentId ?? null;
|
|
4184
|
+
while (id && !seen.has(id)) {
|
|
4185
|
+
seen.add(id);
|
|
4186
|
+
const endedRecord = ended.get(id);
|
|
4187
|
+
const record = endedRecord ?? open.get(id);
|
|
4188
|
+
if (!record)
|
|
4189
|
+
return;
|
|
4190
|
+
if (isContainerType(record.type)) {
|
|
4191
|
+
if (endedRecord) {
|
|
4192
|
+
endedRecord.handle.fillContainerIo(io.input, io.output, endedRecord.ownOutput);
|
|
4193
|
+
} else {
|
|
4194
|
+
if (record.childInput === void 0)
|
|
4195
|
+
record.childInput = io.input;
|
|
4196
|
+
if (io.output !== void 0)
|
|
4197
|
+
record.childOutput = io.output;
|
|
4198
|
+
}
|
|
4199
|
+
}
|
|
4200
|
+
id = record.parentId ?? null;
|
|
4201
|
+
}
|
|
4202
|
+
}
|
|
4203
|
+
}
|
|
4204
|
+
});
|
|
4205
|
+
|
|
3199
4206
|
// ../../packages/ts/tracing/dist/openai-agents.js
|
|
3200
4207
|
var require_openai_agents = __commonJS({
|
|
3201
4208
|
"../../packages/ts/tracing/dist/openai-agents.js"(exports) {
|
|
@@ -3206,7 +4213,10 @@ var require_openai_agents = __commonJS({
|
|
|
3206
4213
|
var error_message_1 = require_error_message();
|
|
3207
4214
|
var schedule_1 = require_schedule();
|
|
3208
4215
|
var tool_result_1 = require_tool_result();
|
|
3209
|
-
var
|
|
4216
|
+
var openai_agents_generation_1 = require_openai_agents_generation();
|
|
4217
|
+
var openai_agents_containers_1 = require_openai_agents_containers();
|
|
4218
|
+
var model_1 = require_model();
|
|
4219
|
+
var reply_text_1 = require_reply_text();
|
|
3210
4220
|
var INTEGRATION_ATTRS = {
|
|
3211
4221
|
"lemma.sdk.integration": "openai-agents"
|
|
3212
4222
|
};
|
|
@@ -3254,37 +4264,6 @@ var require_openai_agents = __commonJS({
|
|
|
3254
4264
|
}
|
|
3255
4265
|
return messageContent(input[input.length - 1]);
|
|
3256
4266
|
}
|
|
3257
|
-
function textFromGenerationOutput(output) {
|
|
3258
|
-
if (!Array.isArray(output))
|
|
3259
|
-
return output;
|
|
3260
|
-
const text = output.map((item) => {
|
|
3261
|
-
if (!item || typeof item !== "object")
|
|
3262
|
-
return "";
|
|
3263
|
-
const record = item;
|
|
3264
|
-
if (typeof record["text"] === "string")
|
|
3265
|
-
return record["text"];
|
|
3266
|
-
if (typeof record["content"] === "string")
|
|
3267
|
-
return record["content"];
|
|
3268
|
-
return "";
|
|
3269
|
-
}).join("");
|
|
3270
|
-
return text || output;
|
|
3271
|
-
}
|
|
3272
|
-
function responseOutput(data) {
|
|
3273
|
-
const response = data["response"] ?? data["_response"];
|
|
3274
|
-
if (response == null) {
|
|
3275
|
-
return textFromGenerationOutput(data["output"]);
|
|
3276
|
-
}
|
|
3277
|
-
if (typeof response === "object" && response !== null) {
|
|
3278
|
-
const record = response;
|
|
3279
|
-
if (typeof record.output_text === "string")
|
|
3280
|
-
return record.output_text;
|
|
3281
|
-
if (Array.isArray(record.output)) {
|
|
3282
|
-
return textFromGenerationOutput(record.output);
|
|
3283
|
-
}
|
|
3284
|
-
return response;
|
|
3285
|
-
}
|
|
3286
|
-
return response;
|
|
3287
|
-
}
|
|
3288
4267
|
function spanName(data) {
|
|
3289
4268
|
if (typeof data["name"] === "string" && data["name"]) {
|
|
3290
4269
|
return data["name"];
|
|
@@ -3317,22 +4296,9 @@ var require_openai_agents = __commonJS({
|
|
|
3317
4296
|
"openai.agents.parent_id": span.parentId,
|
|
3318
4297
|
"openai.agents.span_type": span.spanData.type,
|
|
3319
4298
|
"openai.agents.trace_metadata": span.traceMetadata ? JSON.stringify(span.traceMetadata) : void 0,
|
|
3320
|
-
"openai.agents.span_data": JSON.stringify(span.spanData)
|
|
4299
|
+
"openai.agents.span_data": JSON.stringify((0, openai_agents_generation_1.spanDataForAttribute)(span.spanData))
|
|
3321
4300
|
}).filter(([, value]) => value !== void 0 && value !== null));
|
|
3322
4301
|
}
|
|
3323
|
-
function generationUsage(data) {
|
|
3324
|
-
const direct = (0, usage_1.normalizeTokenUsage)(data["usage"]);
|
|
3325
|
-
if (direct)
|
|
3326
|
-
return direct;
|
|
3327
|
-
const response = data["response"] ?? data["_response"];
|
|
3328
|
-
if (response && typeof response === "object") {
|
|
3329
|
-
const record = response;
|
|
3330
|
-
const fromResponse = (0, usage_1.normalizeTokenUsage)(record.usage);
|
|
3331
|
-
if (fromResponse)
|
|
3332
|
-
return fromResponse;
|
|
3333
|
-
}
|
|
3334
|
-
return void 0;
|
|
3335
|
-
}
|
|
3336
4302
|
function coerceDate(value) {
|
|
3337
4303
|
if (value == null)
|
|
3338
4304
|
return void 0;
|
|
@@ -3452,6 +4418,15 @@ var require_openai_agents = __commonJS({
|
|
|
3452
4418
|
traces.set(trace.traceId, stored);
|
|
3453
4419
|
return stored;
|
|
3454
4420
|
}
|
|
4421
|
+
function blankRecord(span, handle) {
|
|
4422
|
+
return {
|
|
4423
|
+
handle,
|
|
4424
|
+
traceId: span.traceId,
|
|
4425
|
+
type: span.spanData.type,
|
|
4426
|
+
parentId: span.parentId,
|
|
4427
|
+
ownOutput: false
|
|
4428
|
+
};
|
|
4429
|
+
}
|
|
3455
4430
|
function startSpan(span) {
|
|
3456
4431
|
const trace = traces.get(span.traceId);
|
|
3457
4432
|
if (!trace)
|
|
@@ -3476,10 +4451,8 @@ var require_openai_agents = __commonJS({
|
|
|
3476
4451
|
if (isGenerationType(data.type)) {
|
|
3477
4452
|
return trace.handle.startGeneration({
|
|
3478
4453
|
...base,
|
|
3479
|
-
model:
|
|
3480
|
-
llmProvider: "openai"
|
|
3481
|
-
llmInputMessages: Array.isArray(input) ? input : void 0,
|
|
3482
|
-
llmInvocationParameters: data["model_config"]
|
|
4454
|
+
model: (0, model_1.pickModelIdentity)(data),
|
|
4455
|
+
llmProvider: "openai"
|
|
3483
4456
|
});
|
|
3484
4457
|
}
|
|
3485
4458
|
if (data.type === "function") {
|
|
@@ -3492,9 +4465,11 @@ var require_openai_agents = __commonJS({
|
|
|
3492
4465
|
}
|
|
3493
4466
|
function endSpan(span) {
|
|
3494
4467
|
const storedTrace = traces.get(span.traceId);
|
|
3495
|
-
const
|
|
4468
|
+
const existing = spans.get(span.spanId);
|
|
4469
|
+
const handle = existing?.handle ?? startSpan(span);
|
|
3496
4470
|
if (!handle || !storedTrace)
|
|
3497
4471
|
return;
|
|
4472
|
+
const record = existing ?? blankRecord(span, handle);
|
|
3498
4473
|
spans.delete(span.spanId);
|
|
3499
4474
|
applyIdentity(storedTrace, void 0, span.traceMetadata);
|
|
3500
4475
|
const data = span.spanData;
|
|
@@ -3502,11 +4477,12 @@ var require_openai_agents = __commonJS({
|
|
|
3502
4477
|
if (isGenerationType(data.type)) {
|
|
3503
4478
|
noteRootInput(storedTrace, input);
|
|
3504
4479
|
}
|
|
4480
|
+
const snapshot = isGenerationType(data.type) ? (0, openai_agents_generation_1.generationSnapshot)(data, input) : void 0;
|
|
3505
4481
|
let rawOutput;
|
|
3506
4482
|
if (data.type === "generation") {
|
|
3507
|
-
rawOutput = textFromGenerationOutput(data["output"]);
|
|
4483
|
+
rawOutput = (0, reply_text_1.textFromGenerationOutput)(data["output"]);
|
|
3508
4484
|
} else if (data.type === "response") {
|
|
3509
|
-
rawOutput =
|
|
4485
|
+
rawOutput = (0, openai_agents_generation_1.replyFromResponse)(data, snapshot?.response, reply_text_1.textFromGenerationOutput);
|
|
3510
4486
|
} else {
|
|
3511
4487
|
rawOutput = parseMaybeJson(data["output"] ?? data["_response"]);
|
|
3512
4488
|
}
|
|
@@ -3523,18 +4499,41 @@ var require_openai_agents = __commonJS({
|
|
|
3523
4499
|
if (!errorMessage && isGenerationType(data.type) && parsedOutput != null) {
|
|
3524
4500
|
noteRootOutput(storedTrace, parsedOutput);
|
|
3525
4501
|
}
|
|
4502
|
+
let endedInput = input;
|
|
4503
|
+
let endedOutput = parsedOutput;
|
|
4504
|
+
if ((0, openai_agents_containers_1.isContainerType)(data.type)) {
|
|
4505
|
+
record.ownOutput = data["output"] != null;
|
|
4506
|
+
if (endedInput == null)
|
|
4507
|
+
endedInput = record.childInput;
|
|
4508
|
+
if (!record.ownOutput && !errorMessage && record.childOutput !== void 0) {
|
|
4509
|
+
endedOutput = record.childOutput;
|
|
4510
|
+
}
|
|
4511
|
+
}
|
|
4512
|
+
const messages = snapshot?.outputMessages;
|
|
3526
4513
|
handle.end({
|
|
4514
|
+
// The Agents SDK fills spanData.input after onSpanStart (tool arguments,
|
|
4515
|
+
// generation messages), so re-read it here. Skip an absent input so it
|
|
4516
|
+
// can't erase one captured at start.
|
|
4517
|
+
...endedInput != null ? { input: endedInput } : {},
|
|
4518
|
+
...snapshot ? snapshot.fields : {},
|
|
4519
|
+
attributes: openAIAttributes(span),
|
|
3527
4520
|
// Failures must not invent an output — record error instead.
|
|
3528
|
-
output:
|
|
4521
|
+
output: endedOutput,
|
|
3529
4522
|
error: errorMessage,
|
|
3530
4523
|
status: errorMessage ? "ERROR" : void 0,
|
|
3531
|
-
model:
|
|
4524
|
+
model: (0, model_1.pickModelIdentity)(data),
|
|
3532
4525
|
endedAt: spanEndedAt,
|
|
3533
4526
|
durationMs: durationMs(spanStartedAt, spanEndedAt),
|
|
3534
|
-
|
|
3535
|
-
usage:
|
|
4527
|
+
...errorMessage || !messages ? {} : { llmOutputMessages: messages },
|
|
4528
|
+
usage: snapshot?.usage
|
|
3536
4529
|
});
|
|
3537
|
-
endedSpans.set(span.spanId,
|
|
4530
|
+
endedSpans.set(span.spanId, record);
|
|
4531
|
+
if (isGenerationType(data.type)) {
|
|
4532
|
+
(0, openai_agents_containers_1.propagateGenerationIo)(record.parentId, {
|
|
4533
|
+
input: input ?? void 0,
|
|
4534
|
+
output: parsedOutput
|
|
4535
|
+
}, spans, endedSpans);
|
|
4536
|
+
}
|
|
3538
4537
|
if (span.parentId && data.type === "function") {
|
|
3539
4538
|
const parent = spans.get(span.parentId)?.handle ?? endedSpans.get(span.parentId)?.handle;
|
|
3540
4539
|
parent?.ensureEndedAt(spanEndedAt);
|
|
@@ -3603,7 +4602,7 @@ var require_openai_agents = __commonJS({
|
|
|
3603
4602
|
return;
|
|
3604
4603
|
const handle = startSpan(span);
|
|
3605
4604
|
if (handle)
|
|
3606
|
-
spans.set(span.spanId,
|
|
4605
|
+
spans.set(span.spanId, blankRecord(span, handle));
|
|
3607
4606
|
},
|
|
3608
4607
|
onSpanEnd: async (span) => {
|
|
3609
4608
|
if (!traces.has(span.traceId))
|
|
@@ -3724,12 +4723,58 @@ var require_vercel_ai = __commonJS({
|
|
|
3724
4723
|
}
|
|
3725
4724
|
return void 0;
|
|
3726
4725
|
}
|
|
3727
|
-
function
|
|
3728
|
-
if (
|
|
3729
|
-
return
|
|
4726
|
+
function instructionText(value) {
|
|
4727
|
+
if (typeof value === "string" && value)
|
|
4728
|
+
return value;
|
|
4729
|
+
if (Array.isArray(value)) {
|
|
4730
|
+
const parts = value.map((part) => instructionText(part)).filter((part) => Boolean(part));
|
|
4731
|
+
return parts.length > 0 ? parts.join("\n") : void 0;
|
|
4732
|
+
}
|
|
4733
|
+
if (!value || typeof value !== "object")
|
|
4734
|
+
return void 0;
|
|
4735
|
+
const content = value.content;
|
|
4736
|
+
return typeof content === "string" && content ? content : void 0;
|
|
4737
|
+
}
|
|
4738
|
+
function siblingSystem(event) {
|
|
4739
|
+
if (!event || typeof event !== "object")
|
|
4740
|
+
return void 0;
|
|
4741
|
+
const record = event;
|
|
4742
|
+
return instructionText(record.instructions) ?? instructionText(record.system);
|
|
4743
|
+
}
|
|
4744
|
+
function firstMessageRole(messages) {
|
|
4745
|
+
const first = messages[0];
|
|
4746
|
+
if (!first || typeof first !== "object")
|
|
4747
|
+
return void 0;
|
|
4748
|
+
return first.role;
|
|
4749
|
+
}
|
|
4750
|
+
function firstMessageIsSystem(messages, system) {
|
|
4751
|
+
const first = messages[0];
|
|
4752
|
+
return Boolean(first && typeof first === "object" && first.role === "system" && first.content === system);
|
|
4753
|
+
}
|
|
4754
|
+
function resolveSystemForMessages(event, messages, fallbackSystem) {
|
|
4755
|
+
const sibling = siblingSystem(event);
|
|
4756
|
+
if (sibling)
|
|
4757
|
+
return sibling;
|
|
4758
|
+
if (Array.isArray(messages) && firstMessageRole(messages) === "system") {
|
|
4759
|
+
return void 0;
|
|
4760
|
+
}
|
|
4761
|
+
return fallbackSystem;
|
|
4762
|
+
}
|
|
4763
|
+
function withSystemMessage(system, messages) {
|
|
4764
|
+
if (!Array.isArray(messages))
|
|
4765
|
+
return void 0;
|
|
4766
|
+
if (!system || firstMessageIsSystem(messages, system))
|
|
4767
|
+
return messages;
|
|
4768
|
+
return [{ role: "system", content: system }, ...messages];
|
|
4769
|
+
}
|
|
4770
|
+
function v6NormalizedMessages(event, fallbackSystem) {
|
|
4771
|
+
const system = resolveSystemForMessages(event, "messages" in event ? event.messages : void 0, fallbackSystem);
|
|
4772
|
+
if (Array.isArray(event.messages)) {
|
|
4773
|
+
return withSystemMessage(system, event.messages);
|
|
4774
|
+
}
|
|
3730
4775
|
const messages = [];
|
|
3731
|
-
if (
|
|
3732
|
-
messages.push({ role: "system", content:
|
|
4776
|
+
if (system) {
|
|
4777
|
+
messages.push({ role: "system", content: system });
|
|
3733
4778
|
}
|
|
3734
4779
|
if ("prompt" in event && event.prompt !== void 0) {
|
|
3735
4780
|
if (Array.isArray(event.prompt)) {
|
|
@@ -3741,8 +4786,8 @@ var require_vercel_ai = __commonJS({
|
|
|
3741
4786
|
}
|
|
3742
4787
|
return messages.length > 0 ? messages : void 0;
|
|
3743
4788
|
}
|
|
3744
|
-
function v6Input(event) {
|
|
3745
|
-
const normalized = v6NormalizedMessages(event);
|
|
4789
|
+
function v6Input(event, fallbackSystem) {
|
|
4790
|
+
const normalized = v6NormalizedMessages(event, fallbackSystem);
|
|
3746
4791
|
if (normalized)
|
|
3747
4792
|
return normalized;
|
|
3748
4793
|
if ("prompt" in event)
|
|
@@ -4036,9 +5081,10 @@ var require_vercel_ai = __commonJS({
|
|
|
4036
5081
|
const name = typeof options.generationName === "function" ? options.generationName(event) : options.generationName ?? "vercel-ai-generation";
|
|
4037
5082
|
const output = v6Output(event);
|
|
4038
5083
|
const generationError = "error" in event && event.error != null ? (0, error_message_1.describeError)(event.error) : void 0;
|
|
5084
|
+
const fallbackSystem = siblingSystem(v6Starts.at(-1)?.event);
|
|
4039
5085
|
const generation = {
|
|
4040
5086
|
name,
|
|
4041
|
-
input: stored?.event && v6Input(stored.event),
|
|
5087
|
+
input: stored?.event && v6Input(stored.event, fallbackSystem),
|
|
4042
5088
|
output: generationError ? void 0 : output,
|
|
4043
5089
|
metadata: options.metadata,
|
|
4044
5090
|
attributes: withIntegrationAttrs(),
|
|
@@ -4047,7 +5093,7 @@ var require_vercel_ai = __commonJS({
|
|
|
4047
5093
|
endedAt,
|
|
4048
5094
|
durationMs,
|
|
4049
5095
|
llmProvider: event.model.provider,
|
|
4050
|
-
llmInputMessages: stored?.event ? v6NormalizedMessages(stored.event) : void 0,
|
|
5096
|
+
llmInputMessages: stored?.event ? v6NormalizedMessages(stored.event, fallbackSystem) : void 0,
|
|
4051
5097
|
llmOutputMessages: output === void 0 || generationError ? void 0 : [{ role: "assistant", content: output }],
|
|
4052
5098
|
llmTools: stored?.event.tools,
|
|
4053
5099
|
status: generationError ? "ERROR" : void 0,
|
|
@@ -4085,15 +5131,16 @@ var require_vercel_ai = __commonJS({
|
|
|
4085
5131
|
performance: {}
|
|
4086
5132
|
}) : options.generationName ?? "vercel-ai-generation";
|
|
4087
5133
|
const startedAt = /* @__PURE__ */ new Date();
|
|
5134
|
+
const messages = withSystemMessage(resolveSystemForMessages(event, event.messages, siblingSystem(v6Starts.at(-1)?.event)), event.messages);
|
|
4088
5135
|
const handle = trace.startGeneration({
|
|
4089
5136
|
name,
|
|
4090
|
-
input:
|
|
5137
|
+
input: messages,
|
|
4091
5138
|
metadata: options.metadata,
|
|
4092
5139
|
attributes: withIntegrationAttrs(),
|
|
4093
5140
|
model: event.modelId,
|
|
4094
5141
|
startedAt,
|
|
4095
5142
|
llmProvider: event.provider,
|
|
4096
|
-
llmInputMessages:
|
|
5143
|
+
llmInputMessages: messages,
|
|
4097
5144
|
llmTools: event.tools
|
|
4098
5145
|
});
|
|
4099
5146
|
const stored = { event, startedAt, handle };
|
|
@@ -4111,7 +5158,8 @@ var require_vercel_ai = __commonJS({
|
|
|
4111
5158
|
stepNumber: "stepNumber" in event ? event.stepNumber : 0,
|
|
4112
5159
|
model: event.model
|
|
4113
5160
|
}) : options.generationName ?? "vercel-ai-generation";
|
|
4114
|
-
const
|
|
5161
|
+
const fallbackSystem = siblingSystem(v6Starts.at(-1)?.event);
|
|
5162
|
+
const input = v6Input(event, fallbackSystem);
|
|
4115
5163
|
const handle = trace.startGeneration({
|
|
4116
5164
|
name,
|
|
4117
5165
|
input,
|
|
@@ -4120,7 +5168,7 @@ var require_vercel_ai = __commonJS({
|
|
|
4120
5168
|
model: event.model.modelId,
|
|
4121
5169
|
startedAt,
|
|
4122
5170
|
llmProvider: event.model.provider,
|
|
4123
|
-
llmInputMessages: v6NormalizedMessages(event),
|
|
5171
|
+
llmInputMessages: v6NormalizedMessages(event, fallbackSystem),
|
|
4124
5172
|
llmTools: event.tools
|
|
4125
5173
|
});
|
|
4126
5174
|
latestV6GenerationId = handle.id;
|
|
@@ -4426,7 +5474,7 @@ var require_dist = __commonJS({
|
|
|
4426
5474
|
"../../packages/ts/tracing/dist/index.js"(exports) {
|
|
4427
5475
|
"use strict";
|
|
4428
5476
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4429
|
-
exports.vercelAI = exports.openAIAgents = exports.mastra = exports.LemmaMastraExporter = exports.langGraph = exports.langChain = exports.LemmaLangChainCallbackHandler = exports.LangChainCallbackHandler = exports.lemmaDebug = exports.isDebugVerifyEnabled = exports.isDebugModeEnabled = exports.enableDebugMode = exports.disableDebugMode = exports.RELEASE_MAX_LENGTH = exports.normalizeRelease = exports.startCodingAgentTurn = exports.recordCodingAgentToolStart = exports.recordCodingAgentToolResult = exports.completeCodingAgentTurn = exports.codingAgentTurnTrace = exports.toWireTokenUsage = exports.normalizeTokenUsage = exports.attachResultUsage = exports.TraceHandle = exports.TraceContext = exports.SpanHandle = exports.NoopSpanHandle = exports.Lemma = void 0;
|
|
5477
|
+
exports.vercelAI = exports.openAIAgents = exports.mastra = exports.LemmaMastraExporter = exports.langGraph = exports.langChain = exports.LemmaLangChainCallbackHandler = exports.LangChainCallbackHandler = exports.lemmaDebug = exports.isDebugVerifyEnabled = exports.isDebugModeEnabled = exports.enableDebugMode = exports.disableDebugMode = exports.RELEASE_MAX_LENGTH = exports.normalizeRelease = exports.startCodingAgentTurn = exports.recordCodingAgentToolStart = exports.recordCodingAgentToolResult = exports.completeCodingAgentTurn = exports.codingAgentTurnTrace = exports.TurnHandle = exports.TURN_JOURNAL_VERSION = exports.TURN_CONTEXT_VERSION = exports.AttachedTurn = exports.AttachedSpanHandle = exports.startTurn = exports.parseTurnContextToken = exports.assembleTurn = exports.applyTurnJournal = exports.attachTurn = exports.toWireTokenUsage = exports.normalizeTokenUsage = exports.attachResultUsage = exports.TraceHandle = exports.TraceContext = exports.SpanHandle = exports.NoopSpanHandle = exports.Lemma = void 0;
|
|
4430
5478
|
var client_1 = require_client();
|
|
4431
5479
|
Object.defineProperty(exports, "Lemma", { enumerable: true, get: function() {
|
|
4432
5480
|
return client_1.Lemma;
|
|
@@ -4452,6 +5500,37 @@ var require_dist = __commonJS({
|
|
|
4452
5500
|
Object.defineProperty(exports, "toWireTokenUsage", { enumerable: true, get: function() {
|
|
4453
5501
|
return client_1.toWireTokenUsage;
|
|
4454
5502
|
} });
|
|
5503
|
+
var turn_1 = require_turn();
|
|
5504
|
+
Object.defineProperty(exports, "attachTurn", { enumerable: true, get: function() {
|
|
5505
|
+
return turn_1.attachTurn;
|
|
5506
|
+
} });
|
|
5507
|
+
Object.defineProperty(exports, "applyTurnJournal", { enumerable: true, get: function() {
|
|
5508
|
+
return turn_1.applyTurnJournal;
|
|
5509
|
+
} });
|
|
5510
|
+
Object.defineProperty(exports, "assembleTurn", { enumerable: true, get: function() {
|
|
5511
|
+
return turn_1.assembleTurn;
|
|
5512
|
+
} });
|
|
5513
|
+
Object.defineProperty(exports, "parseTurnContextToken", { enumerable: true, get: function() {
|
|
5514
|
+
return turn_1.parseTurnContextToken;
|
|
5515
|
+
} });
|
|
5516
|
+
Object.defineProperty(exports, "startTurn", { enumerable: true, get: function() {
|
|
5517
|
+
return turn_1.startTurn;
|
|
5518
|
+
} });
|
|
5519
|
+
Object.defineProperty(exports, "AttachedSpanHandle", { enumerable: true, get: function() {
|
|
5520
|
+
return turn_1.AttachedSpanHandle;
|
|
5521
|
+
} });
|
|
5522
|
+
Object.defineProperty(exports, "AttachedTurn", { enumerable: true, get: function() {
|
|
5523
|
+
return turn_1.AttachedTurn;
|
|
5524
|
+
} });
|
|
5525
|
+
Object.defineProperty(exports, "TURN_CONTEXT_VERSION", { enumerable: true, get: function() {
|
|
5526
|
+
return turn_1.TURN_CONTEXT_VERSION;
|
|
5527
|
+
} });
|
|
5528
|
+
Object.defineProperty(exports, "TURN_JOURNAL_VERSION", { enumerable: true, get: function() {
|
|
5529
|
+
return turn_1.TURN_JOURNAL_VERSION;
|
|
5530
|
+
} });
|
|
5531
|
+
Object.defineProperty(exports, "TurnHandle", { enumerable: true, get: function() {
|
|
5532
|
+
return turn_1.TurnHandle;
|
|
5533
|
+
} });
|
|
4455
5534
|
var coding_agent_1 = require_coding_agent();
|
|
4456
5535
|
Object.defineProperty(exports, "codingAgentTurnTrace", { enumerable: true, get: function() {
|
|
4457
5536
|
return coding_agent_1.codingAgentTurnTrace;
|
|
@@ -5033,8 +6112,7 @@ import {
|
|
|
5033
6112
|
import { join as join3 } from "node:path";
|
|
5034
6113
|
|
|
5035
6114
|
// src/delivery.ts
|
|
5036
|
-
|
|
5037
|
-
function createDeliveryFetch(fetchImplementation = fetch, timeoutMilliseconds = DELIVERY_TIMEOUT_MS) {
|
|
6115
|
+
function createDeliveryFetch(fetchImplementation, remainingMilliseconds) {
|
|
5038
6116
|
return async (request, init) => {
|
|
5039
6117
|
const controller = new AbortController();
|
|
5040
6118
|
const signal = init?.signal ? AbortSignal.any([init.signal, controller.signal]) : controller.signal;
|
|
@@ -5043,7 +6121,7 @@ function createDeliveryFetch(fetchImplementation = fetch, timeoutMilliseconds =
|
|
|
5043
6121
|
timeout = setTimeout(() => {
|
|
5044
6122
|
controller.abort();
|
|
5045
6123
|
reject(new Error("Lemma OpenCode trace delivery timed out"));
|
|
5046
|
-
},
|
|
6124
|
+
}, remainingMilliseconds);
|
|
5047
6125
|
});
|
|
5048
6126
|
const requestAndBody = (async () => {
|
|
5049
6127
|
const response = await fetchImplementation(request, { ...init, signal });
|
|
@@ -5063,6 +6141,69 @@ function createDeliveryFetch(fetchImplementation = fetch, timeoutMilliseconds =
|
|
|
5063
6141
|
};
|
|
5064
6142
|
}
|
|
5065
6143
|
|
|
6144
|
+
// package.json
|
|
6145
|
+
var package_default = {
|
|
6146
|
+
name: "@uselemma/opencode",
|
|
6147
|
+
version: "0.1.3",
|
|
6148
|
+
description: "Lemma SDK tracing for OpenCode coding-agent sessions",
|
|
6149
|
+
license: "MIT",
|
|
6150
|
+
author: "Lemma",
|
|
6151
|
+
repository: {
|
|
6152
|
+
type: "git",
|
|
6153
|
+
url: "https://github.com/uselemma/lemma",
|
|
6154
|
+
directory: "plugins/lemma-opencode"
|
|
6155
|
+
},
|
|
6156
|
+
homepage: "https://github.com/uselemma/lemma/tree/main/plugins/lemma-opencode#readme",
|
|
6157
|
+
bugs: {
|
|
6158
|
+
url: "https://github.com/uselemma/lemma/issues"
|
|
6159
|
+
},
|
|
6160
|
+
type: "module",
|
|
6161
|
+
main: "./runtime/lemma.mjs",
|
|
6162
|
+
types: "./runtime/lemma.d.ts",
|
|
6163
|
+
exports: {
|
|
6164
|
+
".": {
|
|
6165
|
+
types: "./runtime/lemma.d.ts",
|
|
6166
|
+
import: "./runtime/lemma.mjs",
|
|
6167
|
+
default: "./runtime/lemma.mjs"
|
|
6168
|
+
}
|
|
6169
|
+
},
|
|
6170
|
+
bin: {
|
|
6171
|
+
"lemma-opencode": "./scripts/setup.mjs"
|
|
6172
|
+
},
|
|
6173
|
+
files: [
|
|
6174
|
+
"runtime/lemma.mjs",
|
|
6175
|
+
"runtime/lemma.d.ts",
|
|
6176
|
+
"scripts/setup.mjs",
|
|
6177
|
+
"README.md"
|
|
6178
|
+
],
|
|
6179
|
+
keywords: [
|
|
6180
|
+
"opencode",
|
|
6181
|
+
"observability",
|
|
6182
|
+
"tracing"
|
|
6183
|
+
],
|
|
6184
|
+
scripts: {
|
|
6185
|
+
build: "node build.mjs",
|
|
6186
|
+
"type-check": "tsc --noEmit",
|
|
6187
|
+
test: "vitest run",
|
|
6188
|
+
"verify:local": "pnpm run build && node verify-local.mjs",
|
|
6189
|
+
prepublishOnly: "pnpm run type-check && pnpm run build"
|
|
6190
|
+
},
|
|
6191
|
+
dependencies: {
|
|
6192
|
+
"@opencode-ai/plugin": "1.18.19",
|
|
6193
|
+
"@uselemma/tracing": "workspace:^"
|
|
6194
|
+
},
|
|
6195
|
+
devDependencies: {
|
|
6196
|
+
"@opencode-ai/sdk": "1.18.19",
|
|
6197
|
+
"@types/node": "^22.18.8",
|
|
6198
|
+
esbuild: "^0.25.11",
|
|
6199
|
+
typescript: "^5.9.3",
|
|
6200
|
+
vitest: "^3.2.4"
|
|
6201
|
+
}
|
|
6202
|
+
};
|
|
6203
|
+
|
|
6204
|
+
// src/user-agent.ts
|
|
6205
|
+
var PLUGIN_USER_AGENT = `${package_default.name}/${package_default.version}`;
|
|
6206
|
+
|
|
5066
6207
|
// src/flush.ts
|
|
5067
6208
|
var FLUSH_LOCK_STALE_MS = 3e4;
|
|
5068
6209
|
var FLUSH_LOCK_OWNER_FILE = "owner.json";
|
|
@@ -5179,9 +6320,10 @@ async function flushPendingTurns(options = {}) {
|
|
|
5179
6320
|
projectId: credentials.projectId,
|
|
5180
6321
|
baseUrl: credentials.apiUrl,
|
|
5181
6322
|
fetch: createDeliveryFetch(
|
|
5182
|
-
options.fetch,
|
|
5183
|
-
|
|
5184
|
-
)
|
|
6323
|
+
options.fetch ?? fetch,
|
|
6324
|
+
remainingMilliseconds
|
|
6325
|
+
),
|
|
6326
|
+
userAgent: PLUGIN_USER_AGENT
|
|
5185
6327
|
}).ingest(trace.context, {
|
|
5186
6328
|
startedAt: new Date(trace.startedAt),
|
|
5187
6329
|
endedAt: new Date(trace.endedAt)
|