forked from Lilly-Protocol/agentlily-runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpayment-prep-action.ts
More file actions
80 lines (72 loc) · 2.34 KB
/
Copy pathpayment-prep-action.ts
File metadata and controls
80 lines (72 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { RuntimeError } from "../errors/runtime-errors.js";
import { assertNonEmptyValue } from "../guards/runtime-guards.js";
import type { ToolDefinition, ToolInvocation } from "../tools/types.js";
export interface PaymentPrepPayload {
walletId: string;
amount: string | number;
recipientId?: string | undefined;
assetCode?: string | undefined;
memo?: string | undefined;
metadata?: Record<string, unknown> | undefined;
}
export interface PaymentPrepResult {
status: "prepared";
walletId: string;
amount: string;
recipientId?: string | undefined;
assetCode: string;
memo?: string | undefined;
preparedAt: string;
transactionStubId: string;
isSimulated: true;
metadata?: Record<string, unknown> | undefined;
}
export const PAYMENT_PREP_TOOL_NAME = "wallet.prepare_payment";
export function createPaymentPrepTool(): ToolDefinition<
PaymentPrepPayload,
PaymentPrepResult
> {
return {
name: PAYMENT_PREP_TOOL_NAME,
description:
"Prepares and validates payment context and transaction stub for AgentLily wallet tasks without performing live Stellar network calls.",
execute({
payload,
context
}: ToolInvocation<PaymentPrepPayload>): PaymentPrepResult {
assertNonEmptyValue(payload.walletId, "walletId");
if (
payload.amount === undefined ||
payload.amount === null ||
String(payload.amount).trim().length === 0
) {
throw new RuntimeError("INVALID_TASK", "amount must be specified.", {
fieldName: "amount"
});
}
const amountStr = String(payload.amount);
const parsedAmount = Number(amountStr);
if (!Number.isFinite(parsedAmount) || parsedAmount <= 0) {
throw new RuntimeError(
"INVALID_TASK",
"amount must be a positive finite number.",
{ amount: payload.amount }
);
}
const preparedAt = context.now || new Date().toISOString();
const transactionStubId = `stellar-stub-${context.taskId}-${payload.walletId}`;
return {
status: "prepared",
walletId: payload.walletId,
amount: amountStr,
recipientId: payload.recipientId,
assetCode: payload.assetCode ?? "XLM",
memo: payload.memo,
preparedAt,
transactionStubId,
isSimulated: true,
metadata: payload.metadata
};
}
};
}