diff --git a/worker/src/task/1-task.ts b/worker/src/task/1-task.ts index 6a37aef..2c62f8a 100644 --- a/worker/src/task/1-task.ts +++ b/worker/src/task/1-task.ts @@ -1,4 +1,4 @@ -import { getOrcaClient } from "@_koii/task-manager/extensions"; + import { namespaceWrapper, TASK_ID } from "@_koii/namespace-wrapper"; import "dotenv/config"; import { getRandomNodes } from "../utils/leader"; @@ -10,6 +10,8 @@ import { checkGitHub } from "../utils/githubCheck"; import { LogLevel } from "@_koii/namespace-wrapper/dist/types"; import { actionMessage } from "../utils/constant"; import { errorMessage } from "../utils/constant"; +import { handleOrcaClientCreation, handleRequest } from "../utils/orcaHandler/orcaHandler"; + dotenv.config(); @@ -23,7 +25,7 @@ export async function task(roundNumber: number): Promise { // No submission on Round 0 so no need to trigger fetch audit result before round 3 // Changed from 3 to 4 to have more time if (roundNumber >= 4) { - const triggerFetchAuditResult = await fetch(`${middleServerUrl}/api/builder/summarizer/trigger-fetch-audit-result`, { + const triggerFetchAuditResult = await fetch(`${middleServerUrl}/api/summarizer/trigger-fetch-audit-result`, { method: "POST", headers: { "Content-Type": "application/json" @@ -34,7 +36,14 @@ export async function task(roundNumber: number): Promise { } console.log(`[TASK] EXECUTE TASK FOR ROUND ${roundNumber}`); try { - const orcaClient = await getOrcaClient(); + let orcaClient; + try { + orcaClient = await handleOrcaClientCreation(); + }catch{ + await namespaceWrapper.logMessage(LogLevel.Error, errorMessage.NO_ORCA_CLIENT, actionMessage.NO_ORCA_CLIENT); + await namespaceWrapper.storeSet(`result-${roundNumber}`, status.NO_ORCA_CLIENT); + return; + } // check if the env variable is valid if (!process.env.ANTHROPIC_API_KEY) { await namespaceWrapper.logMessage(LogLevel.Error, errorMessage.ANTHROPIC_API_KEY_INVALID, actionMessage.ANTHROPIC_API_KEY_INVALID); @@ -63,11 +72,7 @@ export async function task(roundNumber: number): Promise { await namespaceWrapper.storeSet(`result-${roundNumber}`, status.GITHUB_CHECK_FAILED); return; } - if (!orcaClient) { - await namespaceWrapper.logMessage(LogLevel.Error, errorMessage.NO_ORCA_CLIENT, actionMessage.NO_ORCA_CLIENT); - await namespaceWrapper.storeSet(`result-${roundNumber}`, status.NO_ORCA_CLIENT); - return; - } + const stakingKeypair = await namespaceWrapper.getSubmitterAccount(); if (!stakingKeypair) { @@ -83,13 +88,7 @@ export async function task(roundNumber: number): Promise { const existingIssues = await getExistingIssues(); const githubUrls = existingIssues.map((issue) => issue.githubUrl); try { - await orcaClient.podCall(`star/${roundNumber}`, { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ taskId: TASK_ID, round_number: String(roundNumber), github_urls: githubUrls }), - }); + await handleRequest({orcaClient, route: `star/${roundNumber}`, bodyJSON: { taskId: TASK_ID, round_number: String(roundNumber), github_urls: githubUrls }}); } catch (error) { await namespaceWrapper.storeSet(`result-${roundNumber}`, status.STAR_ISSUE_FAILED); console.error("Error starring issues:", error); @@ -110,7 +109,7 @@ export async function task(roundNumber: number): Promise { // const initializedDocumentSummarizeIssues = await getInitializedDocumentSummarizeIssues(existingIssues); console.log(`[TASK] Making Request to Middle Server with taskId: ${TASK_ID} and round: ${roundNumber}`); - const requiredWorkResponse = await fetch(`${middleServerUrl}/api/builder/summarizer/fetch-summarizer-todo`, { + const requiredWorkResponse = await fetch(`${middleServerUrl}/api/summarizer/fetch-summarizer-todo`, { method: "POST", headers: { "Content-Type": "application/json" @@ -132,13 +131,7 @@ export async function task(roundNumber: number): Promise { }; console.log("[TASK] jsonBody: ", jsonBody); try { - const repoSummaryResponse = await orcaClient.podCall(`repo_summary/${roundNumber}`, { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify(jsonBody), - }); + const repoSummaryResponse = await handleRequest({orcaClient, route: `repo_summary/${roundNumber}`, bodyJSON: jsonBody}); console.log("[TASK] repoSummaryResponse: ", repoSummaryResponse); console.log("[TASK] repoSummaryResponse.data.result.data ", repoSummaryResponse.data.result.data); const payload = { @@ -156,7 +149,7 @@ export async function task(roundNumber: number): Promise { stakingKeypair.secretKey, ); console.log("[TASK] signature: ", signature); - const addPrToSummarizerTodoResponse = await fetch(`${middleServerUrl}/api/builder/summarizer/add-pr-to-summarizer-todo`, { + const addPrToSummarizerTodoResponse = await fetch(`${middleServerUrl}/api/summarizer/add-pr-to-summarizer-todo`, { method: "POST", headers: { "Content-Type": "application/json", @@ -170,42 +163,10 @@ export async function task(roundNumber: number): Promise { } await namespaceWrapper.storeSet(`result-${roundNumber}`, status.ISSUE_SUCCESSFULLY_SUMMARIZED); } else { - // post this summary response to slack` to notify the team - // THE HOOK IS ALREADY DISABLED - // try{ - // const slackResponse = await fetch('https://hooks.slack.com/services/', { - // method: "POST", - // headers: { - // "Content-Type": "application/json", - // }, - // body: JSON.stringify({ - // text: `[TASK] Error summarizing issue:\nStatus: ${repoSummaryResponse.status}\nData: ${JSON.stringify(repoSummaryResponse.data, null, 2)}` - // }), - // }); - // console.log("[TASK] slackResponse: ", slackResponse); - // }catch(error){ - // console.error("[TASK] Error posting to slack:", error); - // } - await namespaceWrapper.storeSet(`result-${roundNumber}`, status.ISSUE_FAILED_TO_BE_SUMMARIZED); } } catch (error) { await namespaceWrapper.storeSet(`result-${roundNumber}`, status.ISSUE_FAILED_TO_BE_SUMMARIZED); - - // try{ - // const slackResponse = await fetch('https://hooks.slack.com/services', { - // method: "POST", - // headers: { - // "Content-Type": "application/json", - // }, - // body: JSON.stringify({ - // text: `[TASK] Error summarizing issue:\n ${JSON.stringify(error)}` - // }), - // }); - // console.log("[TASK] slackResponse: ", slackResponse); - // }catch(error){ - // console.error("[TASK] Error posting to slack:", error); - // } console.error("[TASK] EXECUTE TASK ERROR:", error); } } catch (error) { diff --git a/worker/src/task/2-submission.ts b/worker/src/task/2-submission.ts index e7686d3..6869004 100644 --- a/worker/src/task/2-submission.ts +++ b/worker/src/task/2-submission.ts @@ -1,5 +1,5 @@ import { storeFile } from "../utils/ipfs"; -import { getOrcaClient } from "@_koii/task-manager/extensions"; +import { handleOrcaClientCreation, handleRequest } from "../utils/orcaHandler/orcaHandler"; import { namespaceWrapper, TASK_ID } from "@_koii/namespace-wrapper"; import { status } from "../utils/constant"; export async function submission(roundNumber: number) : Promise { @@ -13,11 +13,14 @@ export async function submission(roundNumber: number) : Promise { try { console.log("[SUBMISSION] Initializing Orca client..."); - const orcaClient = await getOrcaClient(); - if (!orcaClient) { + let orcaClient; + try { + orcaClient = await handleOrcaClientCreation(); + }catch{ console.error("[SUBMISSION] Failed to initialize Orca client"); return; } + console.log("[SUBMISSION] Orca client initialized successfully"); console.log(`[SUBMISSION] Fetching task result for round ${roundNumber}...`); @@ -34,7 +37,7 @@ export async function submission(roundNumber: number) : Promise { } console.log(`[SUBMISSION] Fetching submission data for round ${roundNumber}...`); - const result = await orcaClient.podCall(`submission/${roundNumber}`); + const result = await handleRequest({orcaClient, route: `submission/${roundNumber}`, bodyJSON: { taskId: TASK_ID, roundNumber }}); let submission; console.log("[SUBMISSION] Submission result:", result.data); diff --git a/worker/src/task/3-audit.ts b/worker/src/task/3-audit.ts index 7e3c5c1..9286e14 100644 --- a/worker/src/task/3-audit.ts +++ b/worker/src/task/3-audit.ts @@ -1,6 +1,6 @@ -import { getOrcaClient } from "@_koii/task-manager/extensions"; import { middleServerUrl, status } from "../utils/constant"; import { submissionJSONSignatureDecode } from "../utils/submissionJSONSignatureDecode"; +import { handleOrcaClientCreation, handleRequest } from "../utils/orcaHandler/orcaHandler"; // import { status } from '../utils/constant' export async function audit(cid: string, roundNumber: number, submitterKey: string): Promise { /** @@ -11,11 +11,13 @@ export async function audit(cid: string, roundNumber: number, submitterKey: stri */ try { - const orcaClient = await getOrcaClient(); - if (!orcaClient) { - // await namespaceWrapper.storeSet(`result-${roundNumber}`, status.NO_ORCA_CLIENT); + let orcaClient; + try { + orcaClient = await handleOrcaClientCreation(); + }catch{ return; } + // Check if the cid is one of the status if (Object.values(status).includes(cid)) { // This returns a dummy true @@ -29,7 +31,7 @@ export async function audit(cid: string, roundNumber: number, submitterKey: stri console.log(`[AUDIT] ✅ Signature decoded successfully`); console.log(`[AUDIT] Checking summarizer status for submitter ${submitterKey}`); - const checkSummarizerResponse = await fetch(`${middleServerUrl}/api/builder/summarizer/check-summarizer`, { + const checkSummarizerResponse = await fetch(`${middleServerUrl}/api/summarizer/check-summarizer`, { method: "POST", headers: { "Content-Type": "application/json" @@ -53,15 +55,9 @@ export async function audit(cid: string, roundNumber: number, submitterKey: stri console.log(`[AUDIT] Sending audit request for submitter: ${submitterKey}`); console.log(`[AUDIT] Submission data being sent to audit:`, decodeResult); - const result = await orcaClient.podCall(`audit/${roundNumber}`, { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - submission: decodeResult, - }), - }); + const result = await handleRequest({orcaClient, route: `audit/${roundNumber}`, bodyJSON: { + submission: decodeResult, + }}); console.log(`[AUDIT] Raw audit result:`, result); console.log(`[AUDIT] Audit result data type:`, typeof result.data); diff --git a/worker/src/task/4-distribution.ts b/worker/src/task/4-distribution.ts index 2186d13..964a35a 100644 --- a/worker/src/task/4-distribution.ts +++ b/worker/src/task/4-distribution.ts @@ -2,10 +2,6 @@ import { Submitter, DistributionList } from "@_koii/task-manager"; import { namespaceWrapper, TASK_ID } from "@_koii/namespace-wrapper"; import { customReward, status } from "../utils/constant"; import { Submission } from "@_koii/namespace-wrapper/dist/types"; -import { middleServerUrl } from "../utils/constant"; -import { getOrcaClient } from "@_koii/task-manager/extensions"; -import { submissionJSONSignatureDecode } from "../utils/submissionJSONSignatureDecode"; -import { getRandomNodes } from "../utils/leader"; const getSubmissionList = async (roundNumber: number): Promise> => { const submissionInfo = await namespaceWrapper.getTaskSubmissionInfo(roundNumber); return submissionInfo?.submissions[roundNumber] || {}; diff --git a/worker/src/utils/orcaHandler/orcaHandler.ts b/worker/src/utils/orcaHandler/orcaHandler.ts new file mode 100644 index 0000000..b5afe57 --- /dev/null +++ b/worker/src/utils/orcaHandler/orcaHandler.ts @@ -0,0 +1,46 @@ + +import dotenv from "dotenv"; +dotenv.config(); + + +import { getOrcaClient } from "@_koii/task-manager/extensions"; +export async function handleOrcaClientCreation(){ + try { + // if (process.env.NODE_ENV !== "development") { + // const { getOrcaClient } = await import("@_koii/task-manager/extensions"); + const orcaClient = await getOrcaClient(); + if (!orcaClient) { + throw new Error("Orca client not found"); + } + return orcaClient; + // }else{ + // return null; + // } + }catch{ + throw new Error("Orca client not found"); + } +} +export async function handleRequest({orcaClient, route, bodyJSON}:{orcaClient:any, route:string, bodyJSON:any}){ + // if (process.env.NODE_ENV === "development") { + // const response = await fetch(`${process.env.LOCAL_CONTAINER_TEST_URL}/${route}`, { + // method: "POST", + // headers: { + // "Content-Type": "application/json", + // }, + // body: JSON.stringify(bodyJSON), + // }); + // return response; + // }else{ + if (!orcaClient) { + throw new Error("Orca client not found"); + } + const response = await orcaClient.podCall(`${route}`, { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(bodyJSON), + }); + return response; + // } +}