From 8332932bbc946619101661faec89d359c2f6f9e1 Mon Sep 17 00:00:00 2001 From: Yusuf Ali Date: Sat, 10 Jan 2026 14:46:45 +0000 Subject: [PATCH] feat: add in retry logic --- dist/index.js | 124 ++++++++++++++++++++++++++++++++++---------------- index.js | 122 +++++++++++++++++++++++++++++++++---------------- 2 files changed, 169 insertions(+), 77 deletions(-) diff --git a/dist/index.js b/dist/index.js index 540e115..6862dca 100644 --- a/dist/index.js +++ b/dist/index.js @@ -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__; diff --git a/index.js b/index.js index ea6305b..742c28e 100644 --- a/index.js +++ b/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); + }); }