forked from elizaOS/eliza
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreateInvoice.ts
105 lines (96 loc) · 3.15 KB
/
createInvoice.ts
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import type { IAgentRuntime, Memory, State } from "@elizaos/core";
import {
composeContext,
generateObjectDeprecated,
ModelClass,
} from "@elizaos/core";
import {
initLightningProvider,
LightningProvider,
} from "../providers/lightning";
import { CreateInvoiceResult } from "astra-lightning";
import { CreateInvoiceArgs } from "../types";
import { createInvoiceTemplate } from "../templates";
export { createInvoiceTemplate };
export class CreateInvoiceAction {
constructor(private lightningProvider: LightningProvider) {
this.lightningProvider = lightningProvider;
}
async createInvoice(
params: CreateInvoiceArgs
): Promise<CreateInvoiceResult> {
if (!params.tokens) {
throw new Error("tokens is required.");
}
const retCreateInvoice =
await this.lightningProvider.createInvoice(params);
return retCreateInvoice;
}
}
export const createInvoiceAction = {
name: "CREATE_INVOICE",
description: "Create a Lightning invoice.",
handler: async (
runtime: IAgentRuntime,
_message: Memory,
state: State,
_options: any,
callback?: any
) => {
console.log("CreateInvoice action handler called");
const lightningProvider = await initLightningProvider(runtime);
const action = new CreateInvoiceAction(lightningProvider);
// Compose bridge context
const createInvoiceContext = composeContext({
state,
template: createInvoiceTemplate,
});
const content = await generateObjectDeprecated({
runtime,
context: createInvoiceContext,
modelClass: ModelClass.LARGE,
});
const createInvoiceOptions: CreateInvoiceArgs = {
tokens: content.tokens,
};
try {
const createInvoiceResp =
await action.createInvoice(createInvoiceOptions);
if (callback) {
callback({
text: `Successfully createInvoice ${createInvoiceResp.tokens}\r\nInvoice:${createInvoiceResp.request}`,
content: {
success: true,
invoice: createInvoiceResp.request,
},
});
}
return true;
} catch (error) {
console.error("Error in createInvoice handler:", error.message);
if (callback) {
callback({ text: `Error: ${error.message}` });
}
return false;
}
},
template: createInvoiceTemplate,
validate: async (runtime: IAgentRuntime) => {
const cert = runtime.getSetting("LND_TLS_CERT");
const macaroon = runtime.getSetting("LND_MACAROON");
const socket = runtime.getSetting("LND_SOCKET");
return !!cert && !!macaroon && !!socket;
},
examples: [
[
{
user: "user",
content: {
text: "Create an invoice for 1000 sats",
action: "CREATE_INVOICE",
},
},
],
],
similes: ["CREATE_INVOICE"],
};