@tangle-network/agent-runtime 0.50.0 → 0.52.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/dist/agent.js +1 -1
  2. package/dist/{chunk-CM2IK7VS.js → chunk-2OU7ZQPD.js} +38 -8
  3. package/dist/chunk-2OU7ZQPD.js.map +1 -0
  4. package/dist/{chunk-OM3YNZIW.js → chunk-4JI4BCBI.js} +5 -360
  5. package/dist/chunk-4JI4BCBI.js.map +1 -0
  6. package/dist/{chunk-NDM5VXZW.js → chunk-7SP2OVYZ.js} +7 -5
  7. package/dist/{chunk-NDM5VXZW.js.map → chunk-7SP2OVYZ.js.map} +1 -1
  8. package/dist/{chunk-RHW75JW5.js → chunk-BERLUBAP.js} +2 -2
  9. package/dist/{chunk-BKAIVNFA.js → chunk-COAVO6QB.js} +3 -3
  10. package/dist/chunk-G3RGMA7C.js +361 -0
  11. package/dist/chunk-G3RGMA7C.js.map +1 -0
  12. package/dist/{chunk-ML4IXGTV.js → chunk-V2K35HF2.js} +2 -2
  13. package/dist/improvement.d.ts +96 -8
  14. package/dist/improvement.js +191 -9
  15. package/dist/improvement.js.map +1 -1
  16. package/dist/index.d.ts +114 -4
  17. package/dist/index.js +144 -18
  18. package/dist/index.js.map +1 -1
  19. package/dist/intelligence.d.ts +423 -0
  20. package/dist/intelligence.js +427 -0
  21. package/dist/intelligence.js.map +1 -0
  22. package/dist/loop-runner-bin.js +4 -3
  23. package/dist/loops.d.ts +2 -1
  24. package/dist/loops.js +3 -1
  25. package/dist/mcp/bin.js +5 -4
  26. package/dist/mcp/bin.js.map +1 -1
  27. package/dist/mcp/index.js +6 -5
  28. package/dist/mcp/index.js.map +1 -1
  29. package/dist/platform.d.ts +120 -62
  30. package/dist/platform.js +68 -26
  31. package/dist/platform.js.map +1 -1
  32. package/dist/runtime.d.ts +47 -8
  33. package/dist/runtime.js +3 -1
  34. package/dist/workflow.js +1 -1
  35. package/package.json +6 -1
  36. package/skills/agent-runtime-adoption/SKILL.md +41 -26
  37. package/skills/build-with-agent-runtime/SKILL.md +143 -0
  38. package/skills/loop-writer/SKILL.md +6 -7
  39. package/dist/chunk-CM2IK7VS.js.map +0 -1
  40. package/dist/chunk-OM3YNZIW.js.map +0 -1
  41. /package/dist/{chunk-RHW75JW5.js.map → chunk-BERLUBAP.js.map} +0 -0
  42. /package/dist/{chunk-BKAIVNFA.js.map → chunk-COAVO6QB.js.map} +0 -0
  43. /package/dist/{chunk-ML4IXGTV.js.map → chunk-V2K35HF2.js.map} +0 -0
package/dist/index.js CHANGED
@@ -1,8 +1,8 @@
1
1
  import {
2
2
  mcpToolsForRuntimeMcp,
3
3
  mcpToolsForRuntimeMcpSubset
4
- } from "./chunk-ML4IXGTV.js";
5
- import "./chunk-RHW75JW5.js";
4
+ } from "./chunk-V2K35HF2.js";
5
+ import "./chunk-BERLUBAP.js";
6
6
  import {
7
7
  DELEGATED_LOOP_MODES,
8
8
  auditLoopRunner,
@@ -15,8 +15,11 @@ import {
15
15
  runDelegatedLoop,
16
16
  runLoopRunnerCli,
17
17
  selfImproveLoopRunner
18
- } from "./chunk-BKAIVNFA.js";
18
+ } from "./chunk-COAVO6QB.js";
19
19
  import "./chunk-FNMGYYSS.js";
20
+ import "./chunk-4JI4BCBI.js";
21
+ import "./chunk-KADIJAD4.js";
22
+ import "./chunk-HNUXAZIJ.js";
20
23
  import {
21
24
  INTELLIGENCE_WIRE_VERSION,
22
25
  buildLoopOtelSpans,
@@ -24,15 +27,13 @@ import {
24
27
  createOtelExporter,
25
28
  exportEvalRuns,
26
29
  loopEventToOtelSpan
27
- } from "./chunk-OM3YNZIW.js";
28
- import "./chunk-KADIJAD4.js";
29
- import "./chunk-HNUXAZIJ.js";
30
+ } from "./chunk-G3RGMA7C.js";
30
31
  import {
31
32
  composeRuntimeHooks,
32
33
  defineRuntimeHooks,
33
34
  notifyRuntimeDecisionPoint,
34
35
  notifyRuntimeHookEvent
35
- } from "./chunk-CM2IK7VS.js";
36
+ } from "./chunk-2OU7ZQPD.js";
36
37
  import {
37
38
  AgentEvalError,
38
39
  BackendTransportError,
@@ -1663,6 +1664,116 @@ var SqlConversationJournal = class {
1663
1664
  }
1664
1665
  };
1665
1666
 
1667
+ // src/conversation/run-persona.ts
1668
+ function withProfilePrompt(inner, systemPrompt, counter) {
1669
+ return {
1670
+ kind: inner.kind,
1671
+ start: inner.start ? (input, ctx) => inner.start(input, ctx) : void 0,
1672
+ resume: inner.resume ? (session, input, ctx) => inner.resume(session, input, ctx) : void 0,
1673
+ stop: inner.stop ? (session, reason) => inner.stop(session, reason) : void 0,
1674
+ async *stream(input, context) {
1675
+ const base = input.messages ?? (input.message ? [{ role: "user", content: input.message }] : []);
1676
+ const messages = base[0]?.role === "system" ? base : [{ role: "system", content: systemPrompt }, ...base];
1677
+ for await (const event of inner.stream({ ...input, messages }, context)) {
1678
+ if (counter && event.type === "llm_call") {
1679
+ counter.tokensIn += event.tokensIn ?? 0;
1680
+ counter.tokensOut += event.tokensOut ?? 0;
1681
+ counter.costUsd += event.costUsd ?? 0;
1682
+ }
1683
+ yield event;
1684
+ }
1685
+ }
1686
+ };
1687
+ }
1688
+ function scriptedPersonaBackend(turns) {
1689
+ let idx = 0;
1690
+ return createIterableBackend({
1691
+ kind: "persona-user",
1692
+ async *stream(_input, context) {
1693
+ const text = turns[idx];
1694
+ if (text === void 0) {
1695
+ throw new Error(
1696
+ `persona-user: ran out of scripted turns at index ${idx} (had ${turns.length})`
1697
+ );
1698
+ }
1699
+ idx += 1;
1700
+ yield {
1701
+ type: "text_delta",
1702
+ task: context.task,
1703
+ session: context.session,
1704
+ text,
1705
+ timestamp: (/* @__PURE__ */ new Date()).toISOString()
1706
+ };
1707
+ }
1708
+ });
1709
+ }
1710
+ async function runPersonaConversation(opts) {
1711
+ const counter = { tokensIn: 0, tokensOut: 0, costUsd: 0 };
1712
+ const workerName = opts.workerName ?? "agent";
1713
+ const worker = withProfilePrompt(
1714
+ opts.backendFor(opts.worker, "worker"),
1715
+ opts.systemPromptOf(opts.worker),
1716
+ counter
1717
+ );
1718
+ let persona;
1719
+ let maxTurns;
1720
+ if (opts.persona.kind === "scripted") {
1721
+ if (opts.persona.turns.length === 0) {
1722
+ throw new Error("runPersonaConversation: scripted persona has no turns");
1723
+ }
1724
+ persona = scriptedPersonaBackend(opts.persona.turns);
1725
+ maxTurns = opts.maxTurns ?? 2 * opts.persona.turns.length;
1726
+ } else {
1727
+ persona = withProfilePrompt(
1728
+ opts.backendFor(opts.persona.profile, "persona"),
1729
+ opts.systemPromptOf(opts.persona.profile)
1730
+ );
1731
+ if (opts.maxTurns === void 0) {
1732
+ throw new Error("runPersonaConversation: maxTurns is required for a profile-driven persona");
1733
+ }
1734
+ maxTurns = opts.maxTurns;
1735
+ }
1736
+ const conversation = defineConversation({
1737
+ // Persona leads (participant 0): the seed routes to it, it produces the
1738
+ // user turn, the worker answers, alternate.
1739
+ participants: [
1740
+ { name: "user", backend: persona },
1741
+ { name: workerName, backend: worker }
1742
+ ],
1743
+ policy: { maxTurns, turnOrder: "alternate" }
1744
+ });
1745
+ const result = await runConversation(conversation, {
1746
+ seed: opts.seed ?? "Begin.",
1747
+ signal: opts.signal
1748
+ });
1749
+ const costUsd = counter.costUsd > 0 ? counter.costUsd : opts.persona.kind === "scripted" ? result.spentCreditsCents / 100 : 0;
1750
+ return {
1751
+ transcript: result.transcript,
1752
+ turns: result.turns,
1753
+ halted: result.halted,
1754
+ costUsd,
1755
+ tokensIn: counter.tokensIn,
1756
+ tokensOut: counter.tokensOut
1757
+ };
1758
+ }
1759
+ function runPersonaDispatch(config) {
1760
+ return async (worker, scenario, ctx) => {
1761
+ const result = await runPersonaConversation({
1762
+ worker,
1763
+ persona: config.personaOf(scenario),
1764
+ backendFor: config.backendFor,
1765
+ systemPromptOf: config.systemPromptOf,
1766
+ maxTurns: config.maxTurns?.(scenario),
1767
+ seed: config.seed?.(scenario),
1768
+ signal: ctx.signal,
1769
+ workerName: config.workerName
1770
+ });
1771
+ ctx.cost.observe(result.costUsd, "persona-conversation");
1772
+ ctx.cost.observeTokens({ input: result.tokensIn, output: result.tokensOut });
1773
+ return config.artifactOf(result.transcript, scenario);
1774
+ };
1775
+ }
1776
+
1666
1777
  // src/durable/chat-engine.ts
1667
1778
  var encoder = new TextEncoder();
1668
1779
  function encodeLine(event) {
@@ -2739,6 +2850,23 @@ var RUNAWAY_BACKSTOP_TURNS = 200;
2739
2850
  var DEFAULT_DECISION_CONTEXT_CHARS = 12e3;
2740
2851
  var FAILURE_RECOVERY_ACTIONS = ["retry", "verify", "continue", "stop"];
2741
2852
  var STUCK_LOOP_THRESHOLD = 3;
2853
+ function toolCallId(call) {
2854
+ return call.toolCallId ?? `call_${call.toolName}`;
2855
+ }
2856
+ function assistantToolCallMessage(turnText, pending) {
2857
+ return {
2858
+ role: "assistant",
2859
+ content: turnText.trim() || null,
2860
+ tool_calls: pending.map((call) => ({
2861
+ id: toolCallId(call),
2862
+ type: "function",
2863
+ function: { name: call.toolName, arguments: JSON.stringify(call.args) }
2864
+ }))
2865
+ };
2866
+ }
2867
+ function toolResultMessage(call, content) {
2868
+ return { role: "tool", tool_call_id: toolCallId(call), content };
2869
+ }
2742
2870
  function defaultRender(label, outcome) {
2743
2871
  if (outcome.ok) return `- ${label} \u2192 ok: ${JSON.stringify(outcome.result)}`;
2744
2872
  return `- ${label} \u2192 failed (${outcome.code}): ${outcome.message}`;
@@ -2793,8 +2921,7 @@ async function runToolLoop(opts) {
2793
2921
  observer.loopAfter({ turns, toolResults: toolResults.length, stopReason: "backstop" });
2794
2922
  return { finalText, toolResults, turns, stopReason: "backstop", cappedOut: true };
2795
2923
  }
2796
- if (turnText.trim()) messages.push({ role: "assistant", content: turnText });
2797
- const lines = [];
2924
+ messages.push(assistantToolCallMessage(turnText, pending));
2798
2925
  const outcomes = [];
2799
2926
  for (const [callIndex, call] of pending.entries()) {
2800
2927
  const callHash = canonicalCallHash(call);
@@ -2828,6 +2955,7 @@ async function runToolLoop(opts) {
2828
2955
  if (accumulatedCostUsd >= opts.maxCostUsd) {
2829
2956
  const label2 = labelFor(call);
2830
2957
  toolResults.push({ call, label: label2, outcome });
2958
+ messages.push(toolResultMessage(call, render(label2, outcome)));
2831
2959
  observer.toolCallAfter(toolTurn, callEventId, call, outcome);
2832
2960
  observer.turnAfter(toolTurn, turnEventId, {
2833
2961
  pendingToolCalls: pending.length,
@@ -2840,8 +2968,8 @@ async function runToolLoop(opts) {
2840
2968
  const label = labelFor(call);
2841
2969
  const rendered = render(label, outcome);
2842
2970
  toolResults.push({ call, label, outcome });
2843
- lines.push(rendered);
2844
2971
  outcomes.push({ call, label, outcome, rendered });
2972
+ messages.push(toolResultMessage(call, rendered));
2845
2973
  observer.toolCallAfter(toolTurn, callEventId, call, outcome);
2846
2974
  }
2847
2975
  observer.failureRecovery({
@@ -2859,8 +2987,6 @@ async function runToolLoop(opts) {
2859
2987
  })),
2860
2988
  failedToolCalls: outcomes.filter((item) => !item.outcome.ok).length
2861
2989
  });
2862
- messages.push({ role: "user", content: `Tool results:
2863
- ${lines.join("\n")}` });
2864
2990
  }
2865
2991
  observer.loopAfter({ turns, toolResults: toolResults.length, stopReason: "completed" });
2866
2992
  return { finalText, toolResults, turns, stopReason: "completed", cappedOut: false };
@@ -2909,8 +3035,7 @@ async function* streamToolLoop(opts) {
2909
3035
  yield { kind: "capped", pending: pending.length, stopReason: "backstop" };
2910
3036
  return;
2911
3037
  }
2912
- if (turnText.trim()) messages.push({ role: "assistant", content: turnText });
2913
- const lines = [];
3038
+ messages.push(assistantToolCallMessage(turnText, pending));
2914
3039
  const outcomes = [];
2915
3040
  for (const [callIndex, call] of pending.entries()) {
2916
3041
  const callHash = canonicalCallHash(call);
@@ -2951,6 +3076,7 @@ async function* streamToolLoop(opts) {
2951
3076
  label: label2,
2952
3077
  outcome
2953
3078
  };
3079
+ messages.push(toolResultMessage(call, render(label2, outcome)));
2954
3080
  observer.toolCallAfter(toolTurn, callEventId, call, outcome);
2955
3081
  observer.turnAfter(toolTurn, turnEventId, {
2956
3082
  pendingToolCalls: pending.length,
@@ -2970,8 +3096,8 @@ async function* streamToolLoop(opts) {
2970
3096
  outcome
2971
3097
  };
2972
3098
  const rendered = render(label, outcome);
2973
- lines.push(rendered);
2974
3099
  outcomes.push({ call, label, outcome, rendered });
3100
+ messages.push(toolResultMessage(call, rendered));
2975
3101
  observer.toolCallAfter(toolTurn, callEventId, call, outcome);
2976
3102
  }
2977
3103
  observer.failureRecovery({
@@ -2989,8 +3115,6 @@ async function* streamToolLoop(opts) {
2989
3115
  })),
2990
3116
  failedToolCalls: outcomes.filter((item) => !item.outcome.ok).length
2991
3117
  });
2992
- messages.push({ role: "user", content: `Tool results:
2993
- ${lines.join("\n")}` });
2994
3118
  }
2995
3119
  }
2996
3120
  function createToolLoopObserver(hooks, runId, scenarioId) {
@@ -3168,7 +3292,7 @@ function failureMetadata(outcome) {
3168
3292
  }
3169
3293
  function renderDecisionContext(messages, turnText, outcomes) {
3170
3294
  const recent = messages.slice(-6).map((message) => `[${message.role}]
3171
- ${message.content}`);
3295
+ ${message.content ?? ""}`);
3172
3296
  const assistant = turnText.trim() ? [`[assistant]
3173
3297
  ${turnText}`] : [];
3174
3298
  const toolResults = [`[tool results]
@@ -3268,6 +3392,8 @@ export {
3268
3392
  runConversationStream,
3269
3393
  runDelegatedLoop,
3270
3394
  runLoopRunnerCli,
3395
+ runPersonaConversation,
3396
+ runPersonaDispatch,
3271
3397
  runToolLoop,
3272
3398
  runtimeStreamServerSentEvent,
3273
3399
  sanitizeAgentRuntimeEvent,