-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
aws_sfn.ts
225 lines (192 loc) Β· 6.35 KB
/
aws_sfn.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import {
SFNClient as Client,
StartExecutionCommand as Invoker,
DescribeExecutionCommand as Describer,
SendTaskSuccessCommand as TaskSuccessSender,
} from "@aws-sdk/client-sfn";
import { Tool, ToolParams } from "@langchain/core/tools";
/**
* Interface for AWS Step Functions configuration.
*/
export interface SfnConfig {
stateMachineArn: string;
region?: string;
accessKeyId?: string;
secretAccessKey?: string;
}
/**
* Interface for AWS Step Functions client constructor arguments.
*/
interface SfnClientConstructorArgs {
region?: string;
credentials?: {
accessKeyId: string;
secretAccessKey: string;
};
}
/**
* Class for starting the execution of an AWS Step Function.
*/
export class StartExecutionAWSSfnTool extends Tool {
static lc_name() {
return "StartExecutionAWSSfnTool";
}
private sfnConfig: SfnConfig;
public name: string;
public description: string;
constructor({
name,
description,
...rest
}: SfnConfig & { name: string; description: string }) {
super();
this.name = name;
this.description = description;
this.sfnConfig = rest;
}
/**
* Generates a formatted description for the StartExecutionAWSSfnTool.
* @param name Name of the state machine.
* @param description Description of the state machine.
* @returns A formatted description string.
*/
static formatDescription(name: string, description: string): string {
return `Use to start executing the ${name} state machine. Use to run ${name} workflows. Whenever you need to start (or execute) an asynchronous workflow (or state machine) about ${description} you should ALWAYS use this. Input should be a valid JSON string.`;
}
/** @ignore */
async _call(input: string): Promise<string> {
const clientConstructorArgs: SfnClientConstructorArgs =
getClientConstructorArgs(this.sfnConfig);
const sfnClient = new Client(clientConstructorArgs);
return new Promise((resolve) => {
let payload;
try {
payload = JSON.parse(input);
} catch (e) {
console.error("Error starting state machine execution:", e);
resolve("failed to complete request");
}
const command = new Invoker({
stateMachineArn: this.sfnConfig.stateMachineArn,
input: JSON.stringify(payload),
});
sfnClient
.send(command)
.then((response) =>
resolve(
response.executionArn ? response.executionArn : "request completed."
)
)
.catch((error: Error) => {
console.error("Error starting state machine execution:", error);
resolve("failed to complete request");
});
});
}
}
/**
* Class for checking the status of an AWS Step Function execution.
*/
export class DescribeExecutionAWSSfnTool extends Tool {
static lc_name() {
return "DescribeExecutionAWSSfnTool";
}
name = "describe-execution-aws-sfn";
description =
"This tool should ALWAYS be used for checking the status of any AWS Step Function execution (aka. state machine execution). Input to this tool is a properly formatted AWS Step Function Execution ARN (executionArn). The output is a stringified JSON object containing the executionArn, name, status, startDate, stopDate, input, output, error, and cause of the execution.";
sfnConfig: Omit<SfnConfig, "stateMachineArn">;
constructor(config: Omit<SfnConfig, "stateMachineArn"> & ToolParams) {
super(config);
this.sfnConfig = config;
}
/** @ignore */
async _call(input: string) {
const clientConstructorArgs: SfnClientConstructorArgs =
getClientConstructorArgs(this.sfnConfig);
const sfnClient = new Client(clientConstructorArgs);
const command = new Describer({
executionArn: input,
});
return await sfnClient
.send(command)
.then((response) =>
response.executionArn
? JSON.stringify({
executionArn: response.executionArn,
name: response.name,
status: response.status,
startDate: response.startDate,
stopDate: response.stopDate,
input: response.input,
output: response.output,
error: response.error,
cause: response.cause,
})
: "{}"
)
.catch((error: Error) => {
console.error("Error describing state machine execution:", error);
return "failed to complete request";
});
}
}
/**
* Class for sending a task success signal to an AWS Step Function
* execution.
*/
export class SendTaskSuccessAWSSfnTool extends Tool {
static lc_name() {
return "SendTaskSuccessAWSSfnTool";
}
name = "send-task-success-aws-sfn";
description =
"This tool should ALWAYS be used for sending task success to an AWS Step Function execution (aka. statemachine exeuction). Input to this tool is a stringify JSON object containing the taskToken and output.";
sfnConfig: Omit<SfnConfig, "stateMachineArn">;
constructor(config: Omit<SfnConfig, "stateMachineArn"> & ToolParams) {
super(config);
this.sfnConfig = config;
}
/** @ignore */
async _call(input: string) {
const clientConstructorArgs: SfnClientConstructorArgs =
getClientConstructorArgs(this.sfnConfig);
const sfnClient = new Client(clientConstructorArgs);
let payload;
try {
payload = JSON.parse(input);
} catch (e) {
console.error("Error starting state machine execution:", e);
return "failed to complete request";
}
const command = new TaskSuccessSender({
taskToken: payload.taskToken,
output: JSON.stringify(payload.output),
});
return await sfnClient
.send(command)
.then(() => "request completed.")
.catch((error: Error) => {
console.error(
"Error sending task success to state machine execution:",
error
);
return "failed to complete request";
});
}
}
/**
* Helper function to construct the AWS SFN client.
*/
function getClientConstructorArgs(config: Partial<SfnConfig>) {
const clientConstructorArgs: SfnClientConstructorArgs = {};
if (config.region) {
clientConstructorArgs.region = config.region;
}
if (config.accessKeyId && config.secretAccessKey) {
clientConstructorArgs.credentials = {
accessKeyId: config.accessKeyId,
secretAccessKey: config.secretAccessKey,
};
}
return clientConstructorArgs;
}