@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/README.md +356 -19
- package/dist/index.cjs +314 -1
- package/dist/index.d.cts +232 -2
- package/dist/index.d.ts +232 -2
- package/dist/index.js +314 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -359,7 +359,7 @@ interface SessionStats {
|
|
|
359
359
|
*/
|
|
360
360
|
|
|
361
361
|
/** Current SDK version */
|
|
362
|
-
declare const SDK_VERSION = "0.
|
|
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 };
|
package/dist/index.js
CHANGED
|
@@ -408,7 +408,7 @@ function getAmountUsd(requirements) {
|
|
|
408
408
|
}
|
|
409
409
|
|
|
410
410
|
// src/agent-wallet.ts
|
|
411
|
-
var SDK_VERSION = "0.
|
|
411
|
+
var SDK_VERSION = "0.4.0";
|
|
412
412
|
var DEFAULT_BASE_URL = process.env.MIXRPAY_BASE_URL || "https://www.mixrpay.com";
|
|
413
413
|
var DEFAULT_TIMEOUT = 3e4;
|
|
414
414
|
var NETWORKS = {
|
|
@@ -1582,6 +1582,319 @@ Timestamp: ${timestamp}`;
|
|
|
1582
1582
|
latencyMs: mixrpay.latencyMs
|
|
1583
1583
|
};
|
|
1584
1584
|
}
|
|
1585
|
+
// ===========================================================================
|
|
1586
|
+
// Agent Runtime API
|
|
1587
|
+
// ===========================================================================
|
|
1588
|
+
/**
|
|
1589
|
+
* Run an AI agent with LLM and tool execution.
|
|
1590
|
+
*
|
|
1591
|
+
* This method orchestrates a full agentic loop:
|
|
1592
|
+
* - Multi-turn reasoning with LLM
|
|
1593
|
+
* - Automatic tool execution
|
|
1594
|
+
* - Bundled billing (single charge at end)
|
|
1595
|
+
* - Optional streaming via SSE
|
|
1596
|
+
*
|
|
1597
|
+
* @param options - Agent run options
|
|
1598
|
+
* @returns Agent run result with response and cost breakdown
|
|
1599
|
+
*
|
|
1600
|
+
* @example Basic usage
|
|
1601
|
+
* ```typescript
|
|
1602
|
+
* const result = await wallet.runAgent({
|
|
1603
|
+
* sessionId: 'sess_abc123',
|
|
1604
|
+
* messages: [{ role: 'user', content: 'Find AI startups in SF' }],
|
|
1605
|
+
* });
|
|
1606
|
+
*
|
|
1607
|
+
* console.log(result.response);
|
|
1608
|
+
* console.log(`Cost: $${result.cost.totalUsd.toFixed(4)}`);
|
|
1609
|
+
* ```
|
|
1610
|
+
*
|
|
1611
|
+
* @example With custom config
|
|
1612
|
+
* ```typescript
|
|
1613
|
+
* const result = await wallet.runAgent({
|
|
1614
|
+
* sessionId: 'sess_abc123',
|
|
1615
|
+
* messages: [{ role: 'user', content: 'Research quantum computing' }],
|
|
1616
|
+
* config: {
|
|
1617
|
+
* model: 'gpt-4o',
|
|
1618
|
+
* maxIterations: 15,
|
|
1619
|
+
* tools: ['platform/exa-search', 'platform/firecrawl-scrape'],
|
|
1620
|
+
* systemPrompt: 'You are a research assistant.',
|
|
1621
|
+
* },
|
|
1622
|
+
* });
|
|
1623
|
+
* ```
|
|
1624
|
+
*
|
|
1625
|
+
* @example With streaming
|
|
1626
|
+
* ```typescript
|
|
1627
|
+
* await wallet.runAgent({
|
|
1628
|
+
* sessionId: 'sess_abc123',
|
|
1629
|
+
* messages: [{ role: 'user', content: 'Analyze this company' }],
|
|
1630
|
+
* stream: true,
|
|
1631
|
+
* onEvent: (event) => {
|
|
1632
|
+
* if (event.type === 'llm_chunk') {
|
|
1633
|
+
* process.stdout.write(event.delta);
|
|
1634
|
+
* } else if (event.type === 'tool_call') {
|
|
1635
|
+
* console.log(`Calling tool: ${event.tool}`);
|
|
1636
|
+
* }
|
|
1637
|
+
* },
|
|
1638
|
+
* });
|
|
1639
|
+
* ```
|
|
1640
|
+
*/
|
|
1641
|
+
async runAgent(options) {
|
|
1642
|
+
const {
|
|
1643
|
+
sessionId,
|
|
1644
|
+
messages,
|
|
1645
|
+
config = {},
|
|
1646
|
+
stream = false,
|
|
1647
|
+
idempotencyKey,
|
|
1648
|
+
onEvent
|
|
1649
|
+
} = options;
|
|
1650
|
+
this.logger.debug("runAgent", { sessionId, messageCount: messages.length, config, stream });
|
|
1651
|
+
const body = {
|
|
1652
|
+
session_id: sessionId,
|
|
1653
|
+
messages: messages.map((m) => ({ role: m.role, content: m.content })),
|
|
1654
|
+
config: {
|
|
1655
|
+
model: config.model,
|
|
1656
|
+
max_iterations: config.maxIterations,
|
|
1657
|
+
tools: config.tools,
|
|
1658
|
+
system_prompt: config.systemPrompt
|
|
1659
|
+
},
|
|
1660
|
+
stream,
|
|
1661
|
+
idempotency_key: idempotencyKey
|
|
1662
|
+
};
|
|
1663
|
+
const AGENT_RUN_TIMEOUT = 18e4;
|
|
1664
|
+
if (!stream) {
|
|
1665
|
+
const response = await fetch(`${this.baseUrl}/api/v2/agent/run`, {
|
|
1666
|
+
method: "POST",
|
|
1667
|
+
headers: {
|
|
1668
|
+
"Content-Type": "application/json",
|
|
1669
|
+
"X-Mixr-Session": sessionId
|
|
1670
|
+
},
|
|
1671
|
+
body: JSON.stringify(body),
|
|
1672
|
+
signal: AbortSignal.timeout(AGENT_RUN_TIMEOUT)
|
|
1673
|
+
});
|
|
1674
|
+
if (!response.ok) {
|
|
1675
|
+
const error = await response.json().catch(() => ({}));
|
|
1676
|
+
throw new MixrPayError(error.error || `Agent run failed: ${response.status}`);
|
|
1677
|
+
}
|
|
1678
|
+
const data = await response.json();
|
|
1679
|
+
if (data.cost?.total_usd > 0) {
|
|
1680
|
+
const payment = {
|
|
1681
|
+
amountUsd: data.cost.total_usd,
|
|
1682
|
+
recipient: "mixrpay-agent-run",
|
|
1683
|
+
txHash: data.tx_hash,
|
|
1684
|
+
timestamp: /* @__PURE__ */ new Date(),
|
|
1685
|
+
description: `Agent run: ${data.run_id}`,
|
|
1686
|
+
url: `${this.baseUrl}/api/v2/agent/run`
|
|
1687
|
+
};
|
|
1688
|
+
this.payments.push(payment);
|
|
1689
|
+
this.totalSpentUsd += data.cost.total_usd;
|
|
1690
|
+
this.logger.payment(data.cost.total_usd, "agent-run", data.run_id);
|
|
1691
|
+
if (this.onPayment) {
|
|
1692
|
+
this.onPayment(payment);
|
|
1693
|
+
}
|
|
1694
|
+
}
|
|
1695
|
+
return {
|
|
1696
|
+
runId: data.run_id,
|
|
1697
|
+
status: data.status,
|
|
1698
|
+
response: data.response,
|
|
1699
|
+
iterations: data.iterations,
|
|
1700
|
+
toolsUsed: data.tools_used,
|
|
1701
|
+
cost: {
|
|
1702
|
+
llmUsd: data.cost.llm_usd,
|
|
1703
|
+
toolsUsd: data.cost.tools_usd,
|
|
1704
|
+
totalUsd: data.cost.total_usd
|
|
1705
|
+
},
|
|
1706
|
+
tokens: data.tokens,
|
|
1707
|
+
sessionRemainingUsd: data.session_remaining_usd,
|
|
1708
|
+
txHash: data.tx_hash
|
|
1709
|
+
};
|
|
1710
|
+
}
|
|
1711
|
+
return this.runAgentStreaming(sessionId, body, onEvent);
|
|
1712
|
+
}
|
|
1713
|
+
/**
|
|
1714
|
+
* Internal: Handle streaming agent run via SSE
|
|
1715
|
+
*/
|
|
1716
|
+
async runAgentStreaming(sessionId, body, onEvent) {
|
|
1717
|
+
const STREAMING_TIMEOUT = 3e5;
|
|
1718
|
+
const response = await fetch(`${this.baseUrl}/api/v2/agent/run`, {
|
|
1719
|
+
method: "POST",
|
|
1720
|
+
headers: {
|
|
1721
|
+
"Content-Type": "application/json",
|
|
1722
|
+
"X-Mixr-Session": sessionId
|
|
1723
|
+
},
|
|
1724
|
+
body: JSON.stringify(body),
|
|
1725
|
+
signal: AbortSignal.timeout(STREAMING_TIMEOUT)
|
|
1726
|
+
});
|
|
1727
|
+
if (!response.ok) {
|
|
1728
|
+
const error = await response.json().catch(() => ({}));
|
|
1729
|
+
throw new MixrPayError(error.error || `Agent run failed: ${response.status}`);
|
|
1730
|
+
}
|
|
1731
|
+
const reader = response.body?.getReader();
|
|
1732
|
+
if (!reader) {
|
|
1733
|
+
throw new MixrPayError("No response body for streaming");
|
|
1734
|
+
}
|
|
1735
|
+
const decoder = new TextDecoder();
|
|
1736
|
+
let buffer = "";
|
|
1737
|
+
let result = null;
|
|
1738
|
+
while (true) {
|
|
1739
|
+
const { done, value } = await reader.read();
|
|
1740
|
+
if (done) break;
|
|
1741
|
+
buffer += decoder.decode(value, { stream: true });
|
|
1742
|
+
const lines = buffer.split("\n");
|
|
1743
|
+
buffer = lines.pop() || "";
|
|
1744
|
+
let currentEvent = "";
|
|
1745
|
+
for (const line of lines) {
|
|
1746
|
+
if (line.startsWith("event: ")) {
|
|
1747
|
+
currentEvent = line.slice(7).trim();
|
|
1748
|
+
} else if (line.startsWith("data: ") && currentEvent) {
|
|
1749
|
+
try {
|
|
1750
|
+
const data = JSON.parse(line.slice(6));
|
|
1751
|
+
const event = this.parseSSEEvent(currentEvent, data);
|
|
1752
|
+
if (event) {
|
|
1753
|
+
onEvent?.(event);
|
|
1754
|
+
if (currentEvent === "complete") {
|
|
1755
|
+
result = {
|
|
1756
|
+
runId: data.run_id,
|
|
1757
|
+
status: "completed",
|
|
1758
|
+
response: data.response,
|
|
1759
|
+
iterations: data.iterations,
|
|
1760
|
+
toolsUsed: data.tools_used,
|
|
1761
|
+
cost: {
|
|
1762
|
+
llmUsd: 0,
|
|
1763
|
+
// Not provided in streaming complete
|
|
1764
|
+
toolsUsd: 0,
|
|
1765
|
+
totalUsd: data.total_cost_usd
|
|
1766
|
+
},
|
|
1767
|
+
tokens: { prompt: 0, completion: 0 },
|
|
1768
|
+
sessionRemainingUsd: 0,
|
|
1769
|
+
txHash: data.tx_hash
|
|
1770
|
+
};
|
|
1771
|
+
if (data.total_cost_usd > 0) {
|
|
1772
|
+
const payment = {
|
|
1773
|
+
amountUsd: data.total_cost_usd,
|
|
1774
|
+
recipient: "mixrpay-agent-run",
|
|
1775
|
+
txHash: data.tx_hash,
|
|
1776
|
+
timestamp: /* @__PURE__ */ new Date(),
|
|
1777
|
+
description: `Agent run: ${data.run_id}`,
|
|
1778
|
+
url: `${this.baseUrl}/api/v2/agent/run`
|
|
1779
|
+
};
|
|
1780
|
+
this.payments.push(payment);
|
|
1781
|
+
this.totalSpentUsd += data.total_cost_usd;
|
|
1782
|
+
if (this.onPayment) {
|
|
1783
|
+
this.onPayment(payment);
|
|
1784
|
+
}
|
|
1785
|
+
}
|
|
1786
|
+
}
|
|
1787
|
+
if (currentEvent === "error") {
|
|
1788
|
+
throw new MixrPayError(data.error || "Agent run failed");
|
|
1789
|
+
}
|
|
1790
|
+
}
|
|
1791
|
+
} catch (e) {
|
|
1792
|
+
if (e instanceof MixrPayError) throw e;
|
|
1793
|
+
this.logger.warn("Failed to parse SSE event:", e);
|
|
1794
|
+
}
|
|
1795
|
+
currentEvent = "";
|
|
1796
|
+
}
|
|
1797
|
+
}
|
|
1798
|
+
}
|
|
1799
|
+
if (!result) {
|
|
1800
|
+
throw new MixrPayError("Agent run completed without final result");
|
|
1801
|
+
}
|
|
1802
|
+
return result;
|
|
1803
|
+
}
|
|
1804
|
+
/**
|
|
1805
|
+
* Internal: Parse SSE event into typed event object
|
|
1806
|
+
*/
|
|
1807
|
+
parseSSEEvent(eventType, data) {
|
|
1808
|
+
switch (eventType) {
|
|
1809
|
+
case "run_start":
|
|
1810
|
+
return { type: "run_start", runId: data.run_id };
|
|
1811
|
+
case "iteration_start":
|
|
1812
|
+
return { type: "iteration_start", iteration: data.iteration };
|
|
1813
|
+
case "llm_chunk":
|
|
1814
|
+
return { type: "llm_chunk", delta: data.delta };
|
|
1815
|
+
case "tool_call":
|
|
1816
|
+
return { type: "tool_call", tool: data.tool, arguments: data.arguments };
|
|
1817
|
+
case "tool_result":
|
|
1818
|
+
return {
|
|
1819
|
+
type: "tool_result",
|
|
1820
|
+
tool: data.tool,
|
|
1821
|
+
success: data.success,
|
|
1822
|
+
costUsd: data.cost_usd,
|
|
1823
|
+
error: data.error
|
|
1824
|
+
};
|
|
1825
|
+
case "iteration_complete":
|
|
1826
|
+
return {
|
|
1827
|
+
type: "iteration_complete",
|
|
1828
|
+
iteration: data.iteration,
|
|
1829
|
+
tokens: data.tokens,
|
|
1830
|
+
costUsd: data.cost_usd
|
|
1831
|
+
};
|
|
1832
|
+
case "complete":
|
|
1833
|
+
return {
|
|
1834
|
+
type: "complete",
|
|
1835
|
+
runId: data.run_id,
|
|
1836
|
+
response: data.response,
|
|
1837
|
+
totalCostUsd: data.total_cost_usd,
|
|
1838
|
+
txHash: data.tx_hash,
|
|
1839
|
+
iterations: data.iterations,
|
|
1840
|
+
toolsUsed: data.tools_used
|
|
1841
|
+
};
|
|
1842
|
+
case "error":
|
|
1843
|
+
return {
|
|
1844
|
+
type: "error",
|
|
1845
|
+
error: data.error,
|
|
1846
|
+
partialCostUsd: data.partial_cost_usd
|
|
1847
|
+
};
|
|
1848
|
+
default:
|
|
1849
|
+
return null;
|
|
1850
|
+
}
|
|
1851
|
+
}
|
|
1852
|
+
/**
|
|
1853
|
+
* Get the status of an agent run by ID.
|
|
1854
|
+
*
|
|
1855
|
+
* @param runId - The agent run ID
|
|
1856
|
+
* @param sessionId - The session ID (for authentication)
|
|
1857
|
+
* @returns Agent run status and results
|
|
1858
|
+
*
|
|
1859
|
+
* @example
|
|
1860
|
+
* ```typescript
|
|
1861
|
+
* const status = await wallet.getAgentRunStatus('run_abc123', 'sess_xyz789');
|
|
1862
|
+
* console.log(`Status: ${status.status}`);
|
|
1863
|
+
* if (status.status === 'completed') {
|
|
1864
|
+
* console.log(status.response);
|
|
1865
|
+
* }
|
|
1866
|
+
* ```
|
|
1867
|
+
*/
|
|
1868
|
+
async getAgentRunStatus(runId, sessionId) {
|
|
1869
|
+
this.logger.debug("getAgentRunStatus", { runId, sessionId });
|
|
1870
|
+
const response = await fetch(`${this.baseUrl}/api/v2/agent/run/${runId}`, {
|
|
1871
|
+
headers: {
|
|
1872
|
+
"X-Mixr-Session": sessionId
|
|
1873
|
+
}
|
|
1874
|
+
});
|
|
1875
|
+
if (!response.ok) {
|
|
1876
|
+
const error = await response.json().catch(() => ({}));
|
|
1877
|
+
throw new MixrPayError(error.error || `Failed to get run status: ${response.status}`);
|
|
1878
|
+
}
|
|
1879
|
+
const data = await response.json();
|
|
1880
|
+
return {
|
|
1881
|
+
runId: data.run_id,
|
|
1882
|
+
status: data.status,
|
|
1883
|
+
response: data.response,
|
|
1884
|
+
iterations: data.iterations,
|
|
1885
|
+
toolsUsed: data.tools_used,
|
|
1886
|
+
cost: {
|
|
1887
|
+
llmUsd: data.cost.llm_usd,
|
|
1888
|
+
toolsUsd: data.cost.tools_usd,
|
|
1889
|
+
totalUsd: data.cost.total_usd
|
|
1890
|
+
},
|
|
1891
|
+
tokens: data.tokens,
|
|
1892
|
+
txHash: data.tx_hash,
|
|
1893
|
+
error: data.error,
|
|
1894
|
+
startedAt: new Date(data.started_at),
|
|
1895
|
+
completedAt: data.completed_at ? new Date(data.completed_at) : void 0
|
|
1896
|
+
};
|
|
1897
|
+
}
|
|
1585
1898
|
/**
|
|
1586
1899
|
* Call an MCP tool using session authorization (pre-authorized spending limit).
|
|
1587
1900
|
*
|