feat: add in retry logic #4

Merged
drgroot merged 1 commits from ci into main 2026-01-10 15:14:47 +00:00
2 changed files with 169 additions and 77 deletions

124
dist/index.js vendored
View File

@@ -32845,7 +32845,7 @@ const debug = (core.getInput("debug") || "false").toLowerCase() === "true";
const repoFull = process.env.GITHUB_REPOSITORY; const repoFull = process.env.GITHUB_REPOSITORY;
const [owner, repo] = repoFull.split("/"); const [owner, repo] = repoFull.split("/");
const segment_id = ["docs", owner, repo].join("."); const segment_id = ["docs", owner].join(".");
const serverUrl = ( const serverUrl = (
process.env.GITHUB_SERVER_URL || process.env.GITHUB_SERVER_URL ||
@@ -32868,49 +32868,95 @@ if (apiToken) {
headers.Apitoken = apiToken; headers.Apitoken = apiToken;
} }
async function post(requestPayload, retries=0) {
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}`,
);
} else {
core.info(`Agent response: ${responseText}`);
}
} catch(e){
if(retries < 5){
return post(requestPayload, retries-1)
}
core.setFailed(`Error sending task to agent: ${error}`)
}
}
for (const file of markdownFiles) { for (const file of markdownFiles) {
const content = fs.readFileSync(file, "utf8"); const content = fs.readFileSync(file, "utf8").trim();
const lines = content.split(/\r?\n/);
const h1Line = lines.find((line) => /^#\s+/.test(line)) || "";
const requestPayload = { const chunks = [];
type: "input", let current = [];
route,
argumentId: "plain",
force: true,
instanceId: null,
inputs: {
method,
inputs: {
segment_id,
document_id: file,
embed_text: content,
store_text: content,
},
},
};
if (debug) { for (const line of lines) {
core.info(`API URL: ${apiUrl}`); if (/^##\s+/.test(line)) {
core.info(`Route: ${route}`); if (current.length) {
core.info(`Server URL: ${serverUrl}`); chunks.push(current.join("\n").trim());
core.info(`Using auth: ${Boolean(apiToken)}`); }
core.info(`Request payload: ${JSON.stringify(requestPayload)}`); current = [line];
} else {
current.push(line);
}
} }
fetch(apiUrl, { if (current.length) {
method: "POST", chunks.push(current.join("\n").trim());
headers, }
body: JSON.stringify(requestPayload),
}) const normalizedChunks =
.then((response) => { chunks.length > 0
if (!response.ok) { ? chunks.map((chunk) => {
core.setFailed( let chunkLines = chunk.split(/\r?\n/);
`Agent API request failed (${response.status}): ${responseText}`, if (h1Line && chunkLines[0] === h1Line) {
); chunkLines = chunkLines.slice(1);
} else { }
core.info(`Agent response: ${response.text()}`); const body = chunkLines.join("\n").trim();
} return [h1Line, body].filter(Boolean).join("\n");
}) })
.catch((error) => core.setFailed(`Error sending task to agent: ${error}`)); : [content];
normalizedChunks.forEach((chunk, index) => {
const requestPayload = {
type: "input",
route,
argumentId: "plain",
force: true,
instanceId: null,
inputs: {
method,
inputs: {
segment_id,
document_id: [repo, file.replace(".", ""), `part${index + 1}`].join(
".",
),
embed_text: chunk,
store_text: chunk,
},
},
};
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)}`);
}
post(requestPayload);
});
} }
module.exports = __webpack_exports__; module.exports = __webpack_exports__;

122
index.js
View File

@@ -13,7 +13,7 @@ const debug = (core.getInput("debug") || "false").toLowerCase() === "true";
const repoFull = process.env.GITHUB_REPOSITORY; const repoFull = process.env.GITHUB_REPOSITORY;
const [owner, repo] = repoFull.split("/"); const [owner, repo] = repoFull.split("/");
const segment_id = ["docs", owner, repo].join("."); const segment_id = ["docs", owner].join(".");
const serverUrl = ( const serverUrl = (
process.env.GITHUB_SERVER_URL || process.env.GITHUB_SERVER_URL ||
@@ -36,47 +36,93 @@ if (apiToken) {
headers.Apitoken = apiToken; headers.Apitoken = apiToken;
} }
async function post(requestPayload, retries=0) {
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}`,
);
} else {
core.info(`Agent response: ${responseText}`);
}
} catch(e){
if(retries < 5){
return post(requestPayload, retries-1)
}
core.setFailed(`Error sending task to agent: ${error}`)
}
}
for (const file of markdownFiles) { for (const file of markdownFiles) {
const content = fs.readFileSync(file, "utf8").trim(); const content = fs.readFileSync(file, "utf8").trim();
const lines = content.split(/\r?\n/);
const h1Line = lines.find((line) => /^#\s+/.test(line)) || "";
const requestPayload = { const chunks = [];
type: "input", let current = [];
route,
argumentId: "plain",
force: true,
instanceId: null,
inputs: {
method,
inputs: {
segment_id,
document_id: file,
embed_text: content,
store_text: content,
},
},
};
if (debug) { for (const line of lines) {
core.info(`API URL: ${apiUrl}`); if (/^##\s+/.test(line)) {
core.info(`Route: ${route}`); if (current.length) {
core.info(`Server URL: ${serverUrl}`); chunks.push(current.join("\n").trim());
core.info(`Using auth: ${Boolean(apiToken)}`); }
core.info(`Request payload: ${JSON.stringify(requestPayload)}`); current = [line];
} else {
current.push(line);
}
} }
fetch(apiUrl, { if (current.length) {
method: "POST", chunks.push(current.join("\n").trim());
headers, }
body: JSON.stringify(requestPayload),
}) const normalizedChunks =
.then((response) => { chunks.length > 0
if (!response.ok) { ? chunks.map((chunk) => {
core.setFailed( let chunkLines = chunk.split(/\r?\n/);
`Agent API request failed (${response.status}): ${responseText}`, if (h1Line && chunkLines[0] === h1Line) {
); chunkLines = chunkLines.slice(1);
} else { }
core.info(`Agent response: ${response.text()}`); const body = chunkLines.join("\n").trim();
} return [h1Line, body].filter(Boolean).join("\n");
}) })
.catch((error) => core.setFailed(`Error sending task to agent: ${error}`)); : [content];
normalizedChunks.forEach((chunk, index) => {
const requestPayload = {
type: "input",
route,
argumentId: "plain",
force: true,
instanceId: null,
inputs: {
method,
inputs: {
segment_id,
document_id: [repo, file.replace(".", ""), `part${index + 1}`].join(
".",
),
embed_text: chunk,
store_text: chunk,
},
},
};
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)}`);
}
post(requestPayload);
});
} }