@uselemma/opencode 0.2.2 → 0.2.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uselemma/opencode",
3
- "version": "0.2.2",
3
+ "version": "0.2.3",
4
4
  "description": "Lemma SDK tracing for OpenCode coding-agent sessions",
5
5
  "license": "MIT",
6
6
  "author": "Lemma",
@@ -46,7 +46,7 @@
46
46
  "esbuild": "^0.25.11",
47
47
  "typescript": "^5.9.3",
48
48
  "vitest": "^3.2.4",
49
- "@uselemma/tracing": "^7.16.0"
49
+ "@uselemma/tracing": "^7.16.1"
50
50
  },
51
51
  "scripts": {
52
52
  "build": "node build.mjs",
package/runtime/lemma.mjs CHANGED
@@ -534,6 +534,145 @@ var require_dropped_traces = __commonJS({
534
534
  }
535
535
  });
536
536
 
537
+ // ../../packages/ts/tracing/dist/span-tree.js
538
+ var require_span_tree = __commonJS({
539
+ "../../packages/ts/tracing/dist/span-tree.js"(exports) {
540
+ "use strict";
541
+ Object.defineProperty(exports, "__esModule", { value: true });
542
+ exports.normalizeSpanTree = normalizeSpanTree;
543
+ function pushMinHeap(heap, value) {
544
+ heap.push(value);
545
+ let i = heap.length - 1;
546
+ while (i > 0) {
547
+ const parent = i - 1 >> 1;
548
+ if (heap[parent] <= heap[i])
549
+ break;
550
+ const tmp = heap[parent];
551
+ heap[parent] = heap[i];
552
+ heap[i] = tmp;
553
+ i = parent;
554
+ }
555
+ }
556
+ function popMinHeap(heap) {
557
+ const top = heap[0];
558
+ const last = heap.pop();
559
+ if (heap.length > 0) {
560
+ heap[0] = last;
561
+ let i = 0;
562
+ while (true) {
563
+ const left = 2 * i + 1;
564
+ const right = left + 1;
565
+ let smallest = i;
566
+ if (left < heap.length && heap[left] < heap[smallest])
567
+ smallest = left;
568
+ if (right < heap.length && heap[right] < heap[smallest])
569
+ smallest = right;
570
+ if (smallest === i)
571
+ break;
572
+ const tmp = heap[i];
573
+ heap[i] = heap[smallest];
574
+ heap[smallest] = tmp;
575
+ i = smallest;
576
+ }
577
+ }
578
+ return top;
579
+ }
580
+ function normalizeSpanTree(spans) {
581
+ if (spans.length === 0)
582
+ return spans;
583
+ const indexById = /* @__PURE__ */ new Map();
584
+ for (let i = 0; i < spans.length; i += 1) {
585
+ const id = spans[i].id;
586
+ if (typeof id === "string" && id.length > 0 && !indexById.has(id)) {
587
+ indexById.set(id, i);
588
+ }
589
+ }
590
+ let needsNormalization = false;
591
+ for (let i = 0; i < spans.length; i += 1) {
592
+ const span = spans[i];
593
+ const parentId = typeof span.parent_id === "string" ? span.parent_id : void 0;
594
+ if (parentId === void 0)
595
+ continue;
596
+ const parentIdx = indexById.get(parentId);
597
+ if (parentId === span.id || parentIdx === void 0 || parentIdx >= i) {
598
+ needsNormalization = true;
599
+ break;
600
+ }
601
+ }
602
+ if (!needsNormalization)
603
+ return spans;
604
+ const copies = spans.map((span) => ({ ...span }));
605
+ const parentOf = new Array(copies.length).fill(-1);
606
+ const childrenByParent = /* @__PURE__ */ new Map();
607
+ const inDegree = new Array(copies.length).fill(0);
608
+ for (let i = 0; i < copies.length; i += 1) {
609
+ const span = copies[i];
610
+ const parentId = typeof span.parent_id === "string" ? span.parent_id : void 0;
611
+ if (parentId === void 0)
612
+ continue;
613
+ const parentIdx = indexById.get(parentId);
614
+ if (parentId === span.id || parentIdx === void 0) {
615
+ delete span.parent_id;
616
+ continue;
617
+ }
618
+ parentOf[i] = parentIdx;
619
+ inDegree[i] = 1;
620
+ const children = childrenByParent.get(parentIdx);
621
+ if (children)
622
+ children.push(i);
623
+ else
624
+ childrenByParent.set(parentIdx, [i]);
625
+ }
626
+ const ready = [];
627
+ for (let i = 0; i < copies.length; i += 1) {
628
+ if (inDegree[i] === 0)
629
+ pushMinHeap(ready, i);
630
+ }
631
+ const emitted = new Array(copies.length).fill(false);
632
+ const ordered = [];
633
+ let scanCursor = 0;
634
+ while (ordered.length < copies.length) {
635
+ if (ready.length === 0) {
636
+ while (scanCursor < copies.length && emitted[scanCursor])
637
+ scanCursor += 1;
638
+ const visited = /* @__PURE__ */ new Map();
639
+ const path = [];
640
+ let cur = scanCursor;
641
+ while (cur !== -1 && !emitted[cur] && !visited.has(cur)) {
642
+ visited.set(cur, path.length);
643
+ path.push(cur);
644
+ cur = parentOf[cur];
645
+ }
646
+ const cycleStart = visited.get(cur) ?? 0;
647
+ let breakIdx = path[cycleStart];
648
+ for (let k = cycleStart + 1; k < path.length; k += 1) {
649
+ if (path[k] < breakIdx)
650
+ breakIdx = path[k];
651
+ }
652
+ delete copies[breakIdx].parent_id;
653
+ parentOf[breakIdx] = -1;
654
+ inDegree[breakIdx] = 0;
655
+ pushMinHeap(ready, breakIdx);
656
+ }
657
+ const idx = popMinHeap(ready);
658
+ if (emitted[idx])
659
+ continue;
660
+ emitted[idx] = true;
661
+ ordered.push(copies[idx]);
662
+ const children = childrenByParent.get(idx);
663
+ if (children) {
664
+ for (const childIdx of children) {
665
+ inDegree[childIdx] = inDegree[childIdx] - 1;
666
+ if (inDegree[childIdx] === 0)
667
+ pushMinHeap(ready, childIdx);
668
+ }
669
+ }
670
+ }
671
+ return ordered;
672
+ }
673
+ }
674
+ });
675
+
537
676
  // ../../packages/ts/tracing/dist/serialize.js
538
677
  var require_serialize = __commonJS({
539
678
  "../../packages/ts/tracing/dist/serialize.js"(exports) {
@@ -2470,7 +2609,7 @@ var require_version = __commonJS({
2470
2609
  "use strict";
2471
2610
  Object.defineProperty(exports, "__esModule", { value: true });
2472
2611
  exports.SDK_USER_AGENT = void 0;
2473
- exports.SDK_USER_AGENT = "@uselemma/tracing/7.16.0";
2612
+ exports.SDK_USER_AGENT = "@uselemma/tracing/7.16.1";
2474
2613
  }
2475
2614
  });
2476
2615
 
@@ -2487,6 +2626,7 @@ var require_client = __commonJS({
2487
2626
  var dropped_traces_1 = require_dropped_traces();
2488
2627
  var env_1 = require_env();
2489
2628
  var error_message_1 = require_error_message();
2629
+ var span_tree_1 = require_span_tree();
2490
2630
  var messages_1 = require_messages();
2491
2631
  var payload_1 = require_payload();
2492
2632
  var release_1 = require_release();
@@ -3384,7 +3524,7 @@ var require_client = __commonJS({
3384
3524
  name: nonBlankString(this.options.name) ?? "trace",
3385
3525
  started_at: startedAt.toISOString(),
3386
3526
  ended_at: endedAt.toISOString(),
3387
- spans: this.spans
3527
+ spans: (0, span_tree_1.normalizeSpanTree)(this.spans)
3388
3528
  };
3389
3529
  const id = nonBlankString(this.options.id);
3390
3530
  if (id !== void 0)
@@ -6228,6 +6368,113 @@ var require_integration_client = __commonJS({
6228
6368
  }
6229
6369
  });
6230
6370
 
6371
+ // ../../packages/ts/tracing/dist/mastra-message-enrichment.js
6372
+ var require_mastra_message_enrichment = __commonJS({
6373
+ "../../packages/ts/tracing/dist/mastra-message-enrichment.js"(exports) {
6374
+ "use strict";
6375
+ Object.defineProperty(exports, "__esModule", { value: true });
6376
+ exports.enrichMastraInputMessages = enrichMastraInputMessages;
6377
+ var tool_call_id_1 = require_tool_call_id();
6378
+ var MASTRA_TOOL_RESULT_PLACEHOLDER_RE = /\[tool-result:\s*([^\]]+)\]/g;
6379
+ var MASTRA_TOOL_RESULT_ONLY_RE = /^(?:\s*\[tool-result:\s*[^\]]+\]\s*)+$/;
6380
+ function hasExplicitToolCallId(record) {
6381
+ for (const key of tool_call_id_1.TOOL_CALL_ID_KEYS) {
6382
+ const value = record[key];
6383
+ if (typeof value === "string" && value.trim())
6384
+ return true;
6385
+ }
6386
+ return false;
6387
+ }
6388
+ function enrichMastraInputMessages(messages, precedingExecutedTools) {
6389
+ if (precedingExecutedTools.length === 0)
6390
+ return messages;
6391
+ const used = /* @__PURE__ */ new Set();
6392
+ const claimTool = (hintName) => {
6393
+ const normalizedHint = hintName && hintName.trim() && hintName.trim() !== "unknown" ? hintName.trim() : void 0;
6394
+ if (normalizedHint) {
6395
+ for (let i = 0; i < precedingExecutedTools.length; i += 1) {
6396
+ if (!used.has(i) && precedingExecutedTools[i]?.name === normalizedHint) {
6397
+ used.add(i);
6398
+ return precedingExecutedTools[i];
6399
+ }
6400
+ }
6401
+ }
6402
+ for (let i = 0; i < precedingExecutedTools.length; i += 1) {
6403
+ if (!used.has(i)) {
6404
+ used.add(i);
6405
+ return precedingExecutedTools[i];
6406
+ }
6407
+ }
6408
+ return void 0;
6409
+ };
6410
+ const out = [];
6411
+ for (const message of messages) {
6412
+ if (!message || typeof message !== "object" || Array.isArray(message)) {
6413
+ out.push(message);
6414
+ continue;
6415
+ }
6416
+ const record = message;
6417
+ if (record.role !== "tool" || hasExplicitToolCallId(record) || Array.isArray(record.content)) {
6418
+ out.push(message);
6419
+ continue;
6420
+ }
6421
+ const existingName = typeof record.name === "string" && record.name.trim() ? record.name.trim() : typeof record.toolName === "string" && record.toolName.trim() ? record.toolName.trim() : void 0;
6422
+ if (typeof record.content === "string" && MASTRA_TOOL_RESULT_ONLY_RE.test(record.content)) {
6423
+ const placeholders = Array.from(record.content.matchAll(MASTRA_TOOL_RESULT_PLACEHOLDER_RE)).map((match) => match[1]?.trim() ?? "");
6424
+ if (placeholders.length > 1) {
6425
+ let matchedAny = false;
6426
+ const expanded = [];
6427
+ for (const hint of placeholders) {
6428
+ const matched2 = claimTool(hint || existingName);
6429
+ if (matched2) {
6430
+ matchedAny = true;
6431
+ expanded.push({
6432
+ ...record,
6433
+ tool_call_id: matched2.id,
6434
+ name: existingName ?? matched2.name ?? (hint || void 0),
6435
+ content: `[tool-result: ${hint || matched2.name || "unknown"}]`
6436
+ });
6437
+ } else {
6438
+ expanded.push({
6439
+ ...record,
6440
+ name: existingName ?? (hint || void 0),
6441
+ content: `[tool-result: ${hint || "unknown"}]`
6442
+ });
6443
+ }
6444
+ }
6445
+ if (matchedAny) {
6446
+ out.push(...expanded);
6447
+ continue;
6448
+ }
6449
+ } else if (placeholders.length === 1) {
6450
+ const hint = placeholders[0] || existingName;
6451
+ const matched2 = claimTool(hint);
6452
+ if (matched2) {
6453
+ out.push({
6454
+ ...record,
6455
+ tool_call_id: matched2.id,
6456
+ name: existingName ?? matched2.name ?? (hint || void 0)
6457
+ });
6458
+ continue;
6459
+ }
6460
+ }
6461
+ }
6462
+ const matched = claimTool(existingName);
6463
+ if (matched) {
6464
+ out.push({
6465
+ ...record,
6466
+ tool_call_id: matched.id,
6467
+ name: existingName ?? matched.name
6468
+ });
6469
+ continue;
6470
+ }
6471
+ out.push(message);
6472
+ }
6473
+ return out;
6474
+ }
6475
+ }
6476
+ });
6477
+
6231
6478
  // ../../packages/ts/tracing/dist/mastra.js
6232
6479
  var require_mastra = __commonJS({
6233
6480
  "../../packages/ts/tracing/dist/mastra.js"(exports) {
@@ -6240,6 +6487,7 @@ var require_mastra = __commonJS({
6240
6487
  var dropped_traces_1 = require_dropped_traces();
6241
6488
  var error_message_1 = require_error_message();
6242
6489
  var integration_client_1 = require_integration_client();
6490
+ var mastra_message_enrichment_1 = require_mastra_message_enrichment();
6243
6491
  var messages_1 = require_messages();
6244
6492
  var model_1 = require_model();
6245
6493
  var schedule_1 = require_schedule();
@@ -6578,7 +6826,7 @@ var require_mastra = __commonJS({
6578
6826
  }
6579
6827
  return lookupString([root.metadata, root.requestContext], ["userId", "resourceId"]);
6580
6828
  }
6581
- recordChild(trace, span, parentId, normalizerOptions) {
6829
+ recordChild(trace, span, parentId, normalizerOptions, executedTools) {
6582
6830
  const startedAt = toDate(span.startTime) ?? /* @__PURE__ */ new Date();
6583
6831
  const endedAt = toDate(span.endTime) ?? (span.isEvent ? startedAt : /* @__PURE__ */ new Date());
6584
6832
  const errorMessage = childErrorMessage(span);
@@ -6606,7 +6854,7 @@ var require_mastra = __commonJS({
6606
6854
  if (GENERATION_TYPES.has(span.type)) {
6607
6855
  const attrs = span.attributes;
6608
6856
  const rawInputMessages = asInputMessages(span.input);
6609
- const llmInputMessages = rawInputMessages ? (0, messages_1.normalizeMessages)(rawInputMessages, normalizerOptions) : void 0;
6857
+ const llmInputMessages = rawInputMessages ? (0, messages_1.normalizeMessages)((0, mastra_message_enrichment_1.enrichMastraInputMessages)(rawInputMessages, executedTools), normalizerOptions) : void 0;
6610
6858
  const llmOutputMessages = errorMessage ? void 0 : asOutputMessages(span.output, normalizerOptions);
6611
6859
  const finishReason = mastraFinishReason(span);
6612
6860
  const timeToFirstTokenMs = attributeNumber(attrs, [
@@ -6709,8 +6957,18 @@ var require_mastra = __commonJS({
6709
6957
  durationMs: durationMs(startedAt, endedAt),
6710
6958
  startedAt
6711
6959
  });
6960
+ const precedingExecutedTools = [];
6712
6961
  for (const child of orderedChildren) {
6713
- this.recordChild(trace, child, resolveParentId(child.parentSpanId, root.id, recordedIds), normalizerOptions);
6962
+ this.recordChild(trace, child, resolveParentId(child.parentSpanId, root.id, recordedIds), normalizerOptions, precedingExecutedTools);
6963
+ if (TOOL_TYPES.has(child.type)) {
6964
+ const callId = resolveToolCallId(child);
6965
+ if (callId) {
6966
+ precedingExecutedTools.push({
6967
+ id: callId,
6968
+ name: resolveToolName(child)
6969
+ });
6970
+ }
6971
+ }
6714
6972
  }
6715
6973
  if (this.pendingUsage !== void 0) {
6716
6974
  trace.applyGenerationUsage(this.pendingUsage);
@@ -10160,7 +10418,7 @@ var import_tracing2 = __toESM(require_dist(), 1);
10160
10418
  // package.json
10161
10419
  var package_default = {
10162
10420
  name: "@uselemma/opencode",
10163
- version: "0.2.2",
10421
+ version: "0.2.3",
10164
10422
  description: "Lemma SDK tracing for OpenCode coding-agent sessions",
10165
10423
  license: "MIT",
10166
10424
  author: "Lemma",
package/scripts/setup.mjs CHANGED
@@ -111,7 +111,7 @@ async function writeDataDirLocation(dataDir, options = {}) {
111
111
  // package.json
112
112
  var package_default = {
113
113
  name: "@uselemma/opencode",
114
- version: "0.2.2",
114
+ version: "0.2.3",
115
115
  description: "Lemma SDK tracing for OpenCode coding-agent sessions",
116
116
  license: "MIT",
117
117
  author: "Lemma",