Files
embed-issues/index.js

137 lines
3.4 KiB
JavaScript

const fs = require("fs");
const core = require("@actions/core");
const github = require("@actions/github");
const apiUrl =
core.getInput("api_url") || "http://agents-api.servc-agents:3000";
const apiToken = core.getInput("api_token");
const route = core.getInput("route") || "agent-lake";
const method = core.getInput("method") || "embeddings_insert";
const debug = (core.getInput("debug") || "false").toLowerCase() === "true";
const payload = github.context.payload;
const githubToken = process.env.GITHUB_TOKEN;
const commentBody = payload?.comment?.body || "";
const issueNumber = payload?.issue?.number || 1;
const repoFullName = process.env.GITHUB_REPOSITORY;
const [owner, repo] = repoFullName.split("/");
const segment_id = "internal.examples.code";
const document_id = [owner, repo, String(issueNumber)].join(".");
const serverUrl = (
process.env.GITHUB_SERVER_URL ||
github.context.serverUrl ||
"https://git.yusufali.ca"
).replace(/\/$/, "");
async function issueComments() {
if (!repoFullName || !issueNumber) return null;
const url = `${serverUrl}/api/v1/repos/${repoFullName}/issues/${issueNumber}/comments?limit=1`;
const headers = { Accept: "application/vnd.github+json" };
if (githubToken) {
headers.Authorization = `Bearer ${githubToken}`;
}
try {
const response = await fetch(url, { headers });
if (!response.ok) {
core.warning(
`Failed to fetch issue comments (${response.status}): ${response.statusText}`,
);
return null;
}
const comments = await response.json();
if (!Array.isArray(comments) || comments.length === 0) return null;
return comments.map((v) => v.body);
} catch (error) {
core.warning(`Error fetching issue comments: ${error}`);
return null;
}
}
const headers = {
"Content-Type": "application/json",
};
if (apiToken) {
headers.Apitoken = apiToken;
}
async function run() {
if (!commentBody) {
if (!debug) {
core.setFailed("No comment body found in the event payload");
return;
}
core.info(
"No comment body found in the event payload, continuing because debug is enabled",
);
}
const comments = await issueComments();
if (!comments || comments.length < 1) {
if (!debug) {
core.setFailed("Unable to determine the task to send to the agent");
return;
}
core.info("Unable to determine the task, using debug placeholder");
task = "[debug] empty task";
}
const requestPayload = {
type: "input",
route,
argumentId: "plain",
force: true,
instanceId: null,
inputs: {
method,
inputs: {
segment_id,
document_id,
embed_text: comments[0],
store_text: comments[comments.length - 1],
},
},
};
if (debug) {
core.info(`API URL: ${apiUrl}`);
core.info(`Route: ${route}`);
core.info(`Server URL: ${serverUrl}`);
core.info(`Using auth: ${Boolean(apiToken)}`);
core.info(`Request payload: ${JSON.stringify(requestPayload)}`);
}
try {
const response = await fetch(apiUrl, {
method: "POST",
headers,
body: JSON.stringify(requestPayload),
});
const responseText = await response.text();
if (!response.ok) {
core.setFailed(
`Agent API request failed (${response.status}): ${responseText}`,
);
return;
}
core.info(`Agent response: ${responseText}`);
} catch (error) {
core.setFailed(`Error sending task to agent: ${error}`);
}
}
run();