You are a code explanation assistant. Your task is to analyze the provided…
- User
Assistant: You are a code explanation assistant. Your task is to analyze the provided code and explain:
- The purpose of the code
- The key components and how they work
- The overall code flow
- Any potential optimizations or improvements
- Any security concerns if applicable
- Do not include any code in your response.
Explain the code in a clear, structured way that would be helpful for programmers of any skill level. Focus on the code's functionality, design patterns, and important aspects. If the code is in a specific language (javascript), highlight any language-specific features being used. User: Please explain this javascript code:
import { setTimeout as wait } from "node:timers/promises";
import { GptTemplate, getHistoryMode } from "./gpt-template.js"; import { get as getHistoryEntry, add as addHistoryEntry } from "./history-control.js"; import { GptContext, ModelData } from "./index.js"; import type { GotResponse } from "supi-core";
type NexraDetails = { created: number; time: number; duration: string; }; type NexraInitial = { id: string; status: "initiation"; };
type NexraPendingData = { status: "pending"; detail: NexraDetails; }; type NexraFailure = { status: "error" | "not_found"; }; type NexraComplementSuccess = { status: "completed"; message: string; model: string; search: boolean; original: unknown; }; type NexraSuccess = { status: "completed"; gpt: string; model: string; original: unknown; }; type PartialData = NexraPendingData | NexraFailure | T;
const NO_YAPPING_PREFIX = "Answer briefly and do not go on any tangents."; const getHistoryEntries = async (context: GptContext) => { const { historyMode } = await getHistoryMode(context); return (historyMode === "enabled") ? await getHistoryEntry(context.user) : []; };
type TaskProcessResult = { error: NexraFailure | undefined; response: GotResponse | undefined; }; const processResponseTask = async <T extends NexraSuccess | NexraComplementSuccess> (taskId: string): Promise<TaskProcessResult> => { let response: GotResponse | undefined; let error: NexraFailure | undefined;
for (let i = 0; i < 30; i++) { const partialResponse = await core.Got.get("GenericAPI")<PartialData<T>>({ method: "GET", url: `https://nexra.aryahcr.cc/api/chat/task/${encodeURIComponent(taskId)}`, throwHttpErrors: false, responseType: "json" }); const { body } = partialResponse; if (body.status === "pending") { await wait(1000); } else if (body.status === "error" || body.status === "not_found") { error = body; break; } else { response = partialResponse as GotResponse<T>; break; } } return { error, response };};
export const GptNexra = { async execute (context: GptContext, query: string, modelData: ModelData) { const messages = await getHistoryEntries(context); if (context.params.context) { messages.unshift({ role: "user", content: context.params.context }); } else { messages.unshift({ role: "user", content: "Respond concisely, to the point, maximum 300 characters; unless I ask you for further detail." }); }
- Assistant