Files
embed-issues/index.js
Yusuf Ali 2b4d3f23e5
All checks were successful
Dependabot Auto-Merge / dependabot (pull_request) Has been skipped
Dependabot Auto-Merge / devopsbot (pull_request) Has been skipped
Dependabot Auto-Merge / rennovatebot (pull_request) Has been skipped
COMMIT LINT / commitlint (pull_request) Successful in 1m40s
Unit Tests / unittest (pull_request) Successful in 48s
fix: first needs to be issue body
2026-01-10 05:28:59 +00:00

182 lines
4.6 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=30`;
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;
const sortedComments = comments
.slice()
.sort((a, b) => {
if (a?.created_at && b?.created_at) {
return new Date(a.created_at) - new Date(b.created_at);
}
if (Number.isFinite(a?.id) && Number.isFinite(b?.id)) {
return a.id - b.id;
}
return 0;
})
.map((v) => v.body);
return sortedComments;
} catch (error) {
core.warning(`Error fetching issue comments: ${error}`);
return null;
}
}
async function issueBody() {
if (!repoFullName || !issueNumber) return null;
const url = `${serverUrl}/api/v1/repos/${repoFullName}/issues/${issueNumber}`;
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 body (${response.status}): ${response.statusText}`,
);
return null;
}
const issue = await response.json();
if (!issue?.body) return null;
return String(issue.body);
} catch (error) {
core.warning(`Error fetching issue body: ${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 [body, comments] = await Promise.all([issueBody(), 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 embedText = body ? body.trim() : String(comments[0]).trim();
const storeText = String(comments[comments.length - 1]).trim();
const requestPayload = {
type: "input",
route,
argumentId: "plain",
force: true,
instanceId: null,
inputs: {
method,
inputs: {
segment_id,
document_id,
embed_text: embedText,
store_text: storeText,
},
},
};
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();