@mixrpay/agent-sdk 0.3.3 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -442,7 +442,7 @@ function getAmountUsd(requirements) {
442
442
  }
443
443
 
444
444
  // src/agent-wallet.ts
445
- var SDK_VERSION = "0.3.1";
445
+ var SDK_VERSION = "0.4.0";
446
446
  var DEFAULT_BASE_URL = process.env.MIXRPAY_BASE_URL || "https://www.mixrpay.com";
447
447
  var DEFAULT_TIMEOUT = 3e4;
448
448
  var NETWORKS = {
@@ -1616,6 +1616,319 @@ Timestamp: ${timestamp}`;
1616
1616
  latencyMs: mixrpay.latencyMs
1617
1617
  };
1618
1618
  }
1619
+ // ===========================================================================
1620
+ // Agent Runtime API
1621
+ // ===========================================================================
1622
+ /**
1623
+ * Run an AI agent with LLM and tool execution.
1624
+ *
1625
+ * This method orchestrates a full agentic loop:
1626
+ * - Multi-turn reasoning with LLM
1627
+ * - Automatic tool execution
1628
+ * - Bundled billing (single charge at end)
1629
+ * - Optional streaming via SSE
1630
+ *
1631
+ * @param options - Agent run options
1632
+ * @returns Agent run result with response and cost breakdown
1633
+ *
1634
+ * @example Basic usage
1635
+ * ```typescript
1636
+ * const result = await wallet.runAgent({
1637
+ * sessionId: 'sess_abc123',
1638
+ * messages: [{ role: 'user', content: 'Find AI startups in SF' }],
1639
+ * });
1640
+ *
1641
+ * console.log(result.response);
1642
+ * console.log(`Cost: $${result.cost.totalUsd.toFixed(4)}`);
1643
+ * ```
1644
+ *
1645
+ * @example With custom config
1646
+ * ```typescript
1647
+ * const result = await wallet.runAgent({
1648
+ * sessionId: 'sess_abc123',
1649
+ * messages: [{ role: 'user', content: 'Research quantum computing' }],
1650
+ * config: {
1651
+ * model: 'gpt-4o',
1652
+ * maxIterations: 15,
1653
+ * tools: ['platform/exa-search', 'platform/firecrawl-scrape'],
1654
+ * systemPrompt: 'You are a research assistant.',
1655
+ * },
1656
+ * });
1657
+ * ```
1658
+ *
1659
+ * @example With streaming
1660
+ * ```typescript
1661
+ * await wallet.runAgent({
1662
+ * sessionId: 'sess_abc123',
1663
+ * messages: [{ role: 'user', content: 'Analyze this company' }],
1664
+ * stream: true,
1665
+ * onEvent: (event) => {
1666
+ * if (event.type === 'llm_chunk') {
1667
+ * process.stdout.write(event.delta);
1668
+ * } else if (event.type === 'tool_call') {
1669
+ * console.log(`Calling tool: ${event.tool}`);
1670
+ * }
1671
+ * },
1672
+ * });
1673
+ * ```
1674
+ */
1675
+ async runAgent(options) {
1676
+ const {
1677
+ sessionId,
1678
+ messages,
1679
+ config = {},
1680
+ stream = false,
1681
+ idempotencyKey,
1682
+ onEvent
1683
+ } = options;
1684
+ this.logger.debug("runAgent", { sessionId, messageCount: messages.length, config, stream });
1685
+ const body = {
1686
+ session_id: sessionId,
1687
+ messages: messages.map((m) => ({ role: m.role, content: m.content })),
1688
+ config: {
1689
+ model: config.model,
1690
+ max_iterations: config.maxIterations,
1691
+ tools: config.tools,
1692
+ system_prompt: config.systemPrompt
1693
+ },
1694
+ stream,
1695
+ idempotency_key: idempotencyKey
1696
+ };
1697
+ const AGENT_RUN_TIMEOUT = 18e4;
1698
+ if (!stream) {
1699
+ const response = await fetch(`${this.baseUrl}/api/v2/agent/run`, {
1700
+ method: "POST",
1701
+ headers: {
1702
+ "Content-Type": "application/json",
1703
+ "X-Mixr-Session": sessionId
1704
+ },
1705
+ body: JSON.stringify(body),
1706
+ signal: AbortSignal.timeout(AGENT_RUN_TIMEOUT)
1707
+ });
1708
+ if (!response.ok) {
1709
+ const error = await response.json().catch(() => ({}));
1710
+ throw new MixrPayError(error.error || `Agent run failed: ${response.status}`);
1711
+ }
1712
+ const data = await response.json();
1713
+ if (data.cost?.total_usd > 0) {
1714
+ const payment = {
1715
+ amountUsd: data.cost.total_usd,
1716
+ recipient: "mixrpay-agent-run",
1717
+ txHash: data.tx_hash,
1718
+ timestamp: /* @__PURE__ */ new Date(),
1719
+ description: `Agent run: ${data.run_id}`,
1720
+ url: `${this.baseUrl}/api/v2/agent/run`
1721
+ };
1722
+ this.payments.push(payment);
1723
+ this.totalSpentUsd += data.cost.total_usd;
1724
+ this.logger.payment(data.cost.total_usd, "agent-run", data.run_id);
1725
+ if (this.onPayment) {
1726
+ this.onPayment(payment);
1727
+ }
1728
+ }
1729
+ return {
1730
+ runId: data.run_id,
1731
+ status: data.status,
1732
+ response: data.response,
1733
+ iterations: data.iterations,
1734
+ toolsUsed: data.tools_used,
1735
+ cost: {
1736
+ llmUsd: data.cost.llm_usd,
1737
+ toolsUsd: data.cost.tools_usd,
1738
+ totalUsd: data.cost.total_usd
1739
+ },
1740
+ tokens: data.tokens,
1741
+ sessionRemainingUsd: data.session_remaining_usd,
1742
+ txHash: data.tx_hash
1743
+ };
1744
+ }
1745
+ return this.runAgentStreaming(sessionId, body, onEvent);
1746
+ }
1747
+ /**
1748
+ * Internal: Handle streaming agent run via SSE
1749
+ */
1750
+ async runAgentStreaming(sessionId, body, onEvent) {
1751
+ const STREAMING_TIMEOUT = 3e5;
1752
+ const response = await fetch(`${this.baseUrl}/api/v2/agent/run`, {
1753
+ method: "POST",
1754
+ headers: {
1755
+ "Content-Type": "application/json",
1756
+ "X-Mixr-Session": sessionId
1757
+ },
1758
+ body: JSON.stringify(body),
1759
+ signal: AbortSignal.timeout(STREAMING_TIMEOUT)
1760
+ });
1761
+ if (!response.ok) {
1762
+ const error = await response.json().catch(() => ({}));
1763
+ throw new MixrPayError(error.error || `Agent run failed: ${response.status}`);
1764
+ }
1765
+ const reader = response.body?.getReader();
1766
+ if (!reader) {
1767
+ throw new MixrPayError("No response body for streaming");
1768
+ }
1769
+ const decoder = new TextDecoder();
1770
+ let buffer = "";
1771
+ let result = null;
1772
+ while (true) {
1773
+ const { done, value } = await reader.read();
1774
+ if (done) break;
1775
+ buffer += decoder.decode(value, { stream: true });
1776
+ const lines = buffer.split("\n");
1777
+ buffer = lines.pop() || "";
1778
+ let currentEvent = "";
1779
+ for (const line of lines) {
1780
+ if (line.startsWith("event: ")) {
1781
+ currentEvent = line.slice(7).trim();
1782
+ } else if (line.startsWith("data: ") && currentEvent) {
1783
+ try {
1784
+ const data = JSON.parse(line.slice(6));
1785
+ const event = this.parseSSEEvent(currentEvent, data);
1786
+ if (event) {
1787
+ onEvent?.(event);
1788
+ if (currentEvent === "complete") {
1789
+ result = {
1790
+ runId: data.run_id,
1791
+ status: "completed",
1792
+ response: data.response,
1793
+ iterations: data.iterations,
1794
+ toolsUsed: data.tools_used,
1795
+ cost: {
1796
+ llmUsd: 0,
1797
+ // Not provided in streaming complete
1798
+ toolsUsd: 0,
1799
+ totalUsd: data.total_cost_usd
1800
+ },
1801
+ tokens: { prompt: 0, completion: 0 },
1802
+ sessionRemainingUsd: 0,
1803
+ txHash: data.tx_hash
1804
+ };
1805
+ if (data.total_cost_usd > 0) {
1806
+ const payment = {
1807
+ amountUsd: data.total_cost_usd,
1808
+ recipient: "mixrpay-agent-run",
1809
+ txHash: data.tx_hash,
1810
+ timestamp: /* @__PURE__ */ new Date(),
1811
+ description: `Agent run: ${data.run_id}`,
1812
+ url: `${this.baseUrl}/api/v2/agent/run`
1813
+ };
1814
+ this.payments.push(payment);
1815
+ this.totalSpentUsd += data.total_cost_usd;
1816
+ if (this.onPayment) {
1817
+ this.onPayment(payment);
1818
+ }
1819
+ }
1820
+ }
1821
+ if (currentEvent === "error") {
1822
+ throw new MixrPayError(data.error || "Agent run failed");
1823
+ }
1824
+ }
1825
+ } catch (e) {
1826
+ if (e instanceof MixrPayError) throw e;
1827
+ this.logger.warn("Failed to parse SSE event:", e);
1828
+ }
1829
+ currentEvent = "";
1830
+ }
1831
+ }
1832
+ }
1833
+ if (!result) {
1834
+ throw new MixrPayError("Agent run completed without final result");
1835
+ }
1836
+ return result;
1837
+ }
1838
+ /**
1839
+ * Internal: Parse SSE event into typed event object
1840
+ */
1841
+ parseSSEEvent(eventType, data) {
1842
+ switch (eventType) {
1843
+ case "run_start":
1844
+ return { type: "run_start", runId: data.run_id };
1845
+ case "iteration_start":
1846
+ return { type: "iteration_start", iteration: data.iteration };
1847
+ case "llm_chunk":
1848
+ return { type: "llm_chunk", delta: data.delta };
1849
+ case "tool_call":
1850
+ return { type: "tool_call", tool: data.tool, arguments: data.arguments };
1851
+ case "tool_result":
1852
+ return {
1853
+ type: "tool_result",
1854
+ tool: data.tool,
1855
+ success: data.success,
1856
+ costUsd: data.cost_usd,
1857
+ error: data.error
1858
+ };
1859
+ case "iteration_complete":
1860
+ return {
1861
+ type: "iteration_complete",
1862
+ iteration: data.iteration,
1863
+ tokens: data.tokens,
1864
+ costUsd: data.cost_usd
1865
+ };
1866
+ case "complete":
1867
+ return {
1868
+ type: "complete",
1869
+ runId: data.run_id,
1870
+ response: data.response,
1871
+ totalCostUsd: data.total_cost_usd,
1872
+ txHash: data.tx_hash,
1873
+ iterations: data.iterations,
1874
+ toolsUsed: data.tools_used
1875
+ };
1876
+ case "error":
1877
+ return {
1878
+ type: "error",
1879
+ error: data.error,
1880
+ partialCostUsd: data.partial_cost_usd
1881
+ };
1882
+ default:
1883
+ return null;
1884
+ }
1885
+ }
1886
+ /**
1887
+ * Get the status of an agent run by ID.
1888
+ *
1889
+ * @param runId - The agent run ID
1890
+ * @param sessionId - The session ID (for authentication)
1891
+ * @returns Agent run status and results
1892
+ *
1893
+ * @example
1894
+ * ```typescript
1895
+ * const status = await wallet.getAgentRunStatus('run_abc123', 'sess_xyz789');
1896
+ * console.log(`Status: ${status.status}`);
1897
+ * if (status.status === 'completed') {
1898
+ * console.log(status.response);
1899
+ * }
1900
+ * ```
1901
+ */
1902
+ async getAgentRunStatus(runId, sessionId) {
1903
+ this.logger.debug("getAgentRunStatus", { runId, sessionId });
1904
+ const response = await fetch(`${this.baseUrl}/api/v2/agent/run/${runId}`, {
1905
+ headers: {
1906
+ "X-Mixr-Session": sessionId
1907
+ }
1908
+ });
1909
+ if (!response.ok) {
1910
+ const error = await response.json().catch(() => ({}));
1911
+ throw new MixrPayError(error.error || `Failed to get run status: ${response.status}`);
1912
+ }
1913
+ const data = await response.json();
1914
+ return {
1915
+ runId: data.run_id,
1916
+ status: data.status,
1917
+ response: data.response,
1918
+ iterations: data.iterations,
1919
+ toolsUsed: data.tools_used,
1920
+ cost: {
1921
+ llmUsd: data.cost.llm_usd,
1922
+ toolsUsd: data.cost.tools_usd,
1923
+ totalUsd: data.cost.total_usd
1924
+ },
1925
+ tokens: data.tokens,
1926
+ txHash: data.tx_hash,
1927
+ error: data.error,
1928
+ startedAt: new Date(data.started_at),
1929
+ completedAt: data.completed_at ? new Date(data.completed_at) : void 0
1930
+ };
1931
+ }
1619
1932
  /**
1620
1933
  * Call an MCP tool using session authorization (pre-authorized spending limit).
1621
1934
  *
package/dist/index.d.cts CHANGED
@@ -359,7 +359,7 @@ interface SessionStats {
359
359
  */
360
360
 
361
361
  /** Current SDK version */
362
- declare const SDK_VERSION = "0.3.1";
362
+ declare const SDK_VERSION = "0.4.0";
363
363
  /** Supported networks */
364
364
  declare const NETWORKS: {
365
365
  readonly BASE_MAINNET: {
@@ -808,6 +808,85 @@ declare class AgentWallet {
808
808
  * ```
809
809
  */
810
810
  callMCPTool(toolName: string, args?: Record<string, unknown>): Promise<MCPToolResult>;
811
+ /**
812
+ * Run an AI agent with LLM and tool execution.
813
+ *
814
+ * This method orchestrates a full agentic loop:
815
+ * - Multi-turn reasoning with LLM
816
+ * - Automatic tool execution
817
+ * - Bundled billing (single charge at end)
818
+ * - Optional streaming via SSE
819
+ *
820
+ * @param options - Agent run options
821
+ * @returns Agent run result with response and cost breakdown
822
+ *
823
+ * @example Basic usage
824
+ * ```typescript
825
+ * const result = await wallet.runAgent({
826
+ * sessionId: 'sess_abc123',
827
+ * messages: [{ role: 'user', content: 'Find AI startups in SF' }],
828
+ * });
829
+ *
830
+ * console.log(result.response);
831
+ * console.log(`Cost: $${result.cost.totalUsd.toFixed(4)}`);
832
+ * ```
833
+ *
834
+ * @example With custom config
835
+ * ```typescript
836
+ * const result = await wallet.runAgent({
837
+ * sessionId: 'sess_abc123',
838
+ * messages: [{ role: 'user', content: 'Research quantum computing' }],
839
+ * config: {
840
+ * model: 'gpt-4o',
841
+ * maxIterations: 15,
842
+ * tools: ['platform/exa-search', 'platform/firecrawl-scrape'],
843
+ * systemPrompt: 'You are a research assistant.',
844
+ * },
845
+ * });
846
+ * ```
847
+ *
848
+ * @example With streaming
849
+ * ```typescript
850
+ * await wallet.runAgent({
851
+ * sessionId: 'sess_abc123',
852
+ * messages: [{ role: 'user', content: 'Analyze this company' }],
853
+ * stream: true,
854
+ * onEvent: (event) => {
855
+ * if (event.type === 'llm_chunk') {
856
+ * process.stdout.write(event.delta);
857
+ * } else if (event.type === 'tool_call') {
858
+ * console.log(`Calling tool: ${event.tool}`);
859
+ * }
860
+ * },
861
+ * });
862
+ * ```
863
+ */
864
+ runAgent(options: AgentRunOptions): Promise<AgentRunResult>;
865
+ /**
866
+ * Internal: Handle streaming agent run via SSE
867
+ */
868
+ private runAgentStreaming;
869
+ /**
870
+ * Internal: Parse SSE event into typed event object
871
+ */
872
+ private parseSSEEvent;
873
+ /**
874
+ * Get the status of an agent run by ID.
875
+ *
876
+ * @param runId - The agent run ID
877
+ * @param sessionId - The session ID (for authentication)
878
+ * @returns Agent run status and results
879
+ *
880
+ * @example
881
+ * ```typescript
882
+ * const status = await wallet.getAgentRunStatus('run_abc123', 'sess_xyz789');
883
+ * console.log(`Status: ${status.status}`);
884
+ * if (status.status === 'completed') {
885
+ * console.log(status.response);
886
+ * }
887
+ * ```
888
+ */
889
+ getAgentRunStatus(runId: string, sessionId: string): Promise<AgentRunStatusResult>;
811
890
  /**
812
891
  * Call an MCP tool using session authorization (pre-authorized spending limit).
813
892
  *
@@ -869,6 +948,157 @@ interface MCPToolResult {
869
948
  /** Execution latency in milliseconds */
870
949
  latencyMs?: number;
871
950
  }
951
+ /**
952
+ * Message in an agent conversation
953
+ */
954
+ interface AgentMessage {
955
+ /** Message role */
956
+ role: 'user' | 'assistant' | 'system' | 'tool';
957
+ /** Message content */
958
+ content: string;
959
+ }
960
+ /**
961
+ * Configuration for an agent run
962
+ */
963
+ interface AgentRunConfig {
964
+ /** LLM model to use (default: gpt-4o-mini) */
965
+ model?: string;
966
+ /** Maximum iterations before stopping (default: 10, max: 25) */
967
+ maxIterations?: number;
968
+ /** Specific tools to enable (e.g., ['platform/exa-search', 'platform/firecrawl-scrape']) */
969
+ tools?: string[];
970
+ /** Custom system prompt for the agent */
971
+ systemPrompt?: string;
972
+ }
973
+ /**
974
+ * Options for running an agent
975
+ */
976
+ interface AgentRunOptions {
977
+ /** Session ID for authentication and billing */
978
+ sessionId: string;
979
+ /** Conversation messages */
980
+ messages: AgentMessage[];
981
+ /** Agent configuration */
982
+ config?: AgentRunConfig;
983
+ /** Enable SSE streaming (default: false) */
984
+ stream?: boolean;
985
+ /** Unique key for idempotency */
986
+ idempotencyKey?: string;
987
+ /** Callback for streaming events */
988
+ onEvent?: (event: AgentRunEvent) => void;
989
+ }
990
+ /**
991
+ * Result from an agent run
992
+ */
993
+ interface AgentRunResult {
994
+ /** Unique run ID */
995
+ runId: string;
996
+ /** Run status */
997
+ status: 'completed' | 'failed';
998
+ /** Final response from the agent */
999
+ response: string;
1000
+ /** Number of iterations performed */
1001
+ iterations: number;
1002
+ /** Tools used during the run */
1003
+ toolsUsed: string[];
1004
+ /** Cost breakdown */
1005
+ cost: {
1006
+ /** LLM cost in USD */
1007
+ llmUsd: number;
1008
+ /** Tool execution cost in USD */
1009
+ toolsUsd: number;
1010
+ /** Total cost in USD */
1011
+ totalUsd: number;
1012
+ };
1013
+ /** Token usage */
1014
+ tokens: {
1015
+ /** Prompt tokens used */
1016
+ prompt: number;
1017
+ /** Completion tokens used */
1018
+ completion: number;
1019
+ };
1020
+ /** Remaining session balance in USD */
1021
+ sessionRemainingUsd: number;
1022
+ /** On-chain transaction hash for payment */
1023
+ txHash?: string;
1024
+ }
1025
+ /**
1026
+ * Streaming events from an agent run
1027
+ */
1028
+ type AgentRunEvent = {
1029
+ type: 'run_start';
1030
+ runId: string;
1031
+ } | {
1032
+ type: 'iteration_start';
1033
+ iteration: number;
1034
+ } | {
1035
+ type: 'llm_chunk';
1036
+ delta: string;
1037
+ } | {
1038
+ type: 'tool_call';
1039
+ tool: string;
1040
+ arguments: unknown;
1041
+ } | {
1042
+ type: 'tool_result';
1043
+ tool: string;
1044
+ success: boolean;
1045
+ costUsd: number;
1046
+ error?: string;
1047
+ } | {
1048
+ type: 'iteration_complete';
1049
+ iteration: number;
1050
+ tokens?: {
1051
+ prompt?: number;
1052
+ completion?: number;
1053
+ };
1054
+ costUsd?: number;
1055
+ } | {
1056
+ type: 'complete';
1057
+ runId: string;
1058
+ response: string;
1059
+ totalCostUsd: number;
1060
+ txHash?: string;
1061
+ iterations: number;
1062
+ toolsUsed: string[];
1063
+ } | {
1064
+ type: 'error';
1065
+ error: string;
1066
+ partialCostUsd?: number;
1067
+ };
1068
+ /**
1069
+ * Result from getting agent run status
1070
+ */
1071
+ interface AgentRunStatusResult {
1072
+ /** Unique run ID */
1073
+ runId: string;
1074
+ /** Run status */
1075
+ status: 'running' | 'completed' | 'failed' | 'interrupted';
1076
+ /** Final response (if completed) */
1077
+ response?: string;
1078
+ /** Number of iterations performed */
1079
+ iterations: number;
1080
+ /** Tools used during the run */
1081
+ toolsUsed: string[];
1082
+ /** Cost breakdown */
1083
+ cost: {
1084
+ llmUsd: number;
1085
+ toolsUsd: number;
1086
+ totalUsd: number;
1087
+ };
1088
+ /** Token usage */
1089
+ tokens: {
1090
+ prompt: number;
1091
+ completion: number;
1092
+ };
1093
+ /** On-chain transaction hash */
1094
+ txHash?: string;
1095
+ /** Error message (if failed) */
1096
+ error?: string;
1097
+ /** When the run started */
1098
+ startedAt: Date;
1099
+ /** When the run completed */
1100
+ completedAt?: Date;
1101
+ }
872
1102
 
873
1103
  /**
874
1104
  * MixrPay Agent SDK - Custom Errors
@@ -1198,4 +1428,4 @@ declare class SessionRevokedError extends MixrPayError {
1198
1428
  */
1199
1429
  declare function isMixrPayError(error: unknown): error is MixrPayError;
1200
1430
 
1201
- export { AgentWallet, type AgentWalletConfig, type CallMerchantApiOptions, type ChargeResult, InsufficientBalanceError, InvalidSessionKeyError, MixrPayError, type PaymentEvent, PaymentFailedError, SDK_VERSION, type SessionAuthorization, SessionExpiredError, SessionKeyExpiredError, SessionLimitExceededError, SessionNotFoundError, SessionRevokedError, type SessionStats, SpendingLimitExceededError, type SpendingStats, isMixrPayError };
1431
+ export { type AgentMessage, type AgentRunConfig, type AgentRunEvent, type AgentRunOptions, type AgentRunResult, type AgentRunStatusResult, AgentWallet, type AgentWalletConfig, type CallMerchantApiOptions, type ChargeResult, InsufficientBalanceError, InvalidSessionKeyError, MixrPayError, type PaymentEvent, PaymentFailedError, SDK_VERSION, type SessionAuthorization, SessionExpiredError, SessionKeyExpiredError, SessionLimitExceededError, SessionNotFoundError, SessionRevokedError, type SessionStats, SpendingLimitExceededError, type SpendingStats, isMixrPayError };