Skip to content

Commit b17a879

Browse files
committed
refactoring for RCA Tool
1 parent 6706142 commit b17a879

File tree

1 file changed

+76
-17
lines changed

1 file changed

+76
-17
lines changed

src/tools/rca-agent.ts

Lines changed: 76 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,47 @@ import { getRCAData } from "./rca-agent-utils/rca-data.js";
1010
import { formatRCAData } from "./rca-agent-utils/format-rca.js";
1111
import { TestStatus } from "./rca-agent-utils/types.js";
1212

13+
// Tool function to fetch build ID
14+
export async function getBuildIdTool(
15+
args: {
16+
projectName: string;
17+
buildName: string;
18+
},
19+
config: BrowserStackConfig,
20+
): Promise<CallToolResult> {
21+
try {
22+
const { projectName, buildName } = args;
23+
const authString = getBrowserStackAuth(config);
24+
const [username, accessKey] = authString.split(":");
25+
const buildId = await getBuildId(
26+
username,
27+
accessKey,
28+
projectName,
29+
buildName,
30+
);
31+
return {
32+
content: [
33+
{
34+
type: "text",
35+
text: buildId,
36+
},
37+
],
38+
};
39+
} catch (error) {
40+
logger.error("Error fetching build ID", error);
41+
const errorMessage =
42+
error instanceof Error ? error.message : "Unknown error";
43+
return {
44+
content: [
45+
{
46+
type: "text",
47+
text: `Error fetching build ID: ${errorMessage}`,
48+
},
49+
],
50+
};
51+
}
52+
}
53+
1354
// Tool function that fetches RCA data
1455
export async function fetchRCADataTool(
1556
args: { testId: string[] },
@@ -41,24 +82,14 @@ export async function fetchRCADataTool(
4182

4283
export async function listTestIdsTool(
4384
args: {
44-
projectName: string;
45-
buildName: string;
85+
buildId: string;
4686
status?: TestStatus;
4787
},
4888
config: BrowserStackConfig,
4989
): Promise<CallToolResult> {
5090
try {
51-
const { projectName, buildName, status } = args;
91+
const { buildId, status } = args;
5292
const authString = getBrowserStackAuth(config);
53-
const [username, accessKey] = authString.split(":");
54-
55-
// Get build ID if not provided
56-
const buildId = await getBuildId(
57-
username,
58-
accessKey,
59-
projectName,
60-
buildName,
61-
);
6293

6394
// Get test IDs
6495
const testIds = await getTestIds(buildId, authString, status);
@@ -122,19 +153,47 @@ export default function addRCATools(
122153
},
123154
);
124155

125-
tools.listTestIds = server.tool(
126-
"listTestIds",
127-
"List test IDs from a BrowserStack Automate build, optionally filtered by status",
156+
tools.getBuildId = server.tool(
157+
"getBuildId",
158+
"Get the BrowserStack build ID for a given project and build name.",
128159
{
129160
projectName: z
130161
.string()
131162
.describe(
132-
"The Browserstack project name used while creation of test run. Check browserstack.yml or similar project configuration files. If found extract it and provide to user,IF not found or unsure, prompt the user for this value. Do not make assumptions",
163+
"The Browserstack project name used while creation of test run. Check browserstack.yml or similar project configuration files. If found extract it and provide to user, IF not found or unsure, prompt the user for this value. Do not make assumptions",
133164
),
134165
buildName: z
135166
.string()
136167
.describe(
137-
"The Browserstack build name used while creation of test run. Check browserstack.yml or similar project configuration files. If found extract it and provide to user,IF not found or unsure, prompt the user for this value. Do not make assumptions",
168+
"The Browserstack build name used while creation of test run. Check browserstack.yml or similar project configuration files. If found extract it and provide to user, IF not found or unsure, prompt the user for this value. Do not make assumptions",
169+
),
170+
},
171+
async (args) => {
172+
try {
173+
return await getBuildIdTool(args, config);
174+
} catch (error) {
175+
const errorMessage =
176+
error instanceof Error ? error.message : "Unknown error";
177+
return {
178+
content: [
179+
{
180+
type: "text",
181+
text: `Error during fetching build ID: ${errorMessage}`,
182+
},
183+
],
184+
};
185+
}
186+
},
187+
);
188+
189+
tools.listTestIds = server.tool(
190+
"listTestIds",
191+
"List test IDs from a BrowserStack Automate build, optionally filtered by status",
192+
{
193+
buildId: z
194+
.string()
195+
.describe(
196+
"The Browserstack Build ID of the test run. If not known, use the getBuildId tool to fetch it using project and build name",
138197
),
139198
status: z
140199
.nativeEnum(TestStatus)

0 commit comments

Comments
 (0)