feat: add in retry logic #4
124
dist/index.js
vendored
124
dist/index.js
vendored
@@ -32845,7 +32845,7 @@ const debug = (core.getInput("debug") || "false").toLowerCase() === "true";
|
||||
|
||||
const repoFull = process.env.GITHUB_REPOSITORY;
|
||||
const [owner, repo] = repoFull.split("/");
|
||||
const segment_id = ["docs", owner, repo].join(".");
|
||||
const segment_id = ["docs", owner].join(".");
|
||||
|
||||
const serverUrl = (
|
||||
process.env.GITHUB_SERVER_URL ||
|
||||
@@ -32868,49 +32868,95 @@ if (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) {
|
||||
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 = {
|
||||
type: "input",
|
||||
route,
|
||||
argumentId: "plain",
|
||||
force: true,
|
||||
instanceId: null,
|
||||
inputs: {
|
||||
method,
|
||||
inputs: {
|
||||
segment_id,
|
||||
document_id: file,
|
||||
embed_text: content,
|
||||
store_text: content,
|
||||
},
|
||||
},
|
||||
};
|
||||
const chunks = [];
|
||||
let current = [];
|
||||
|
||||
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)}`);
|
||||
for (const line of lines) {
|
||||
if (/^##\s+/.test(line)) {
|
||||
if (current.length) {
|
||||
chunks.push(current.join("\n").trim());
|
||||
}
|
||||
current = [line];
|
||||
} else {
|
||||
current.push(line);
|
||||
}
|
||||
}
|
||||
|
||||
fetch(apiUrl, {
|
||||
method: "POST",
|
||||
headers,
|
||||
body: JSON.stringify(requestPayload),
|
||||
})
|
||||
.then((response) => {
|
||||
if (!response.ok) {
|
||||
core.setFailed(
|
||||
`Agent API request failed (${response.status}): ${responseText}`,
|
||||
);
|
||||
} else {
|
||||
core.info(`Agent response: ${response.text()}`);
|
||||
}
|
||||
})
|
||||
.catch((error) => core.setFailed(`Error sending task to agent: ${error}`));
|
||||
if (current.length) {
|
||||
chunks.push(current.join("\n").trim());
|
||||
}
|
||||
|
||||
const normalizedChunks =
|
||||
chunks.length > 0
|
||||
? chunks.map((chunk) => {
|
||||
let chunkLines = chunk.split(/\r?\n/);
|
||||
if (h1Line && chunkLines[0] === h1Line) {
|
||||
chunkLines = chunkLines.slice(1);
|
||||
}
|
||||
const body = chunkLines.join("\n").trim();
|
||||
return [h1Line, body].filter(Boolean).join("\n");
|
||||
})
|
||||
: [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__;
|
||||
|
||||
122
index.js
122
index.js
@@ -13,7 +13,7 @@ const debug = (core.getInput("debug") || "false").toLowerCase() === "true";
|
||||
|
||||
const repoFull = process.env.GITHUB_REPOSITORY;
|
||||
const [owner, repo] = repoFull.split("/");
|
||||
const segment_id = ["docs", owner, repo].join(".");
|
||||
const segment_id = ["docs", owner].join(".");
|
||||
|
||||
const serverUrl = (
|
||||
process.env.GITHUB_SERVER_URL ||
|
||||
@@ -36,47 +36,93 @@ if (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) {
|
||||
const content = fs.readFileSync(file, "utf8").trim();
|
||||
const lines = content.split(/\r?\n/);
|
||||
const h1Line = lines.find((line) => /^#\s+/.test(line)) || "";
|
||||
|
||||
const requestPayload = {
|
||||
type: "input",
|
||||
route,
|
||||
argumentId: "plain",
|
||||
force: true,
|
||||
instanceId: null,
|
||||
inputs: {
|
||||
method,
|
||||
inputs: {
|
||||
segment_id,
|
||||
document_id: file,
|
||||
embed_text: content,
|
||||
store_text: content,
|
||||
},
|
||||
},
|
||||
};
|
||||
const chunks = [];
|
||||
let current = [];
|
||||
|
||||
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)}`);
|
||||
for (const line of lines) {
|
||||
if (/^##\s+/.test(line)) {
|
||||
if (current.length) {
|
||||
chunks.push(current.join("\n").trim());
|
||||
}
|
||||
current = [line];
|
||||
} else {
|
||||
current.push(line);
|
||||
}
|
||||
}
|
||||
|
||||
fetch(apiUrl, {
|
||||
method: "POST",
|
||||
headers,
|
||||
body: JSON.stringify(requestPayload),
|
||||
})
|
||||
.then((response) => {
|
||||
if (!response.ok) {
|
||||
core.setFailed(
|
||||
`Agent API request failed (${response.status}): ${responseText}`,
|
||||
);
|
||||
} else {
|
||||
core.info(`Agent response: ${response.text()}`);
|
||||
}
|
||||
})
|
||||
.catch((error) => core.setFailed(`Error sending task to agent: ${error}`));
|
||||
if (current.length) {
|
||||
chunks.push(current.join("\n").trim());
|
||||
}
|
||||
|
||||
const normalizedChunks =
|
||||
chunks.length > 0
|
||||
? chunks.map((chunk) => {
|
||||
let chunkLines = chunk.split(/\r?\n/);
|
||||
if (h1Line && chunkLines[0] === h1Line) {
|
||||
chunkLines = chunkLines.slice(1);
|
||||
}
|
||||
const body = chunkLines.join("\n").trim();
|
||||
return [h1Line, body].filter(Boolean).join("\n");
|
||||
})
|
||||
: [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);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user