Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

Stop a skill import fetch when the bot's turn ends

AdityaUmale9ef5004bd4f3
diff --git a/server/index.ts b/server/index.ts
index b90bf33edc..679c9d095c 100644
--- a/server/index.ts
+++ b/server/index.ts
@@ -13531,7 +13531,7 @@ const handleRequest = async (req: IncomingMessage, res: ServerResponse) => {
return json(res, 403, { error: "source conversation does not belong to sender" });
}
if (isSkillSwitchAction(received.action)) { const staged = skillSwitch.stage(from, fromThreadId, received.action, received, appliesDirectly(from.id, fromThreadId)); return json(res, staged.status, staged.body); }
- const body = await importedSkillRequest(received); if ("importError" in body) return json(res, 422, { error: body.importError }); if (!internalCapabilityIsActive(internalCapability)) return json(res, 401, { error: TURN_ENDED_REFUSAL }); if (!skillAuthoringEnabled(cfg)) return json(res, 403, { error: "skill authoring is not enabled in Settings" });
+ const body = await importedSkillRequest(received, fetch, callSignal); if ("importError" in body) return json(res, 422, { error: body.importError }); if (!internalCapabilityIsActive(internalCapability)) return json(res, 401, { error: TURN_ENDED_REFUSAL }); if (!skillAuthoringEnabled(cfg)) return json(res, 403, { error: "skill authoring is not enabled in Settings" });
const direct = appliesDirectly(from.id, fromThreadId);
const persistence = skillProposalPersistence(from.id, fromThreadId, !direct);
if (!persistence.ok) return json(res, persistence.status, { error: persistence.error });
diff --git a/server/skill-fetch.ts b/server/skill-fetch.ts
index 855127e3f8..bbf952f73f 100644
--- a/server/skill-fetch.ts
+++ b/server/skill-fetch.ts
@@ -16,11 +16,12 @@ class ImportLimitError extends Error {}
class GitHubApiError extends Error {}
// One budget for the whole import, enforced while reading the stream.
-function boundedImportFetch(fetcher: typeof fetch): typeof fetch {
+function boundedImportFetch(fetcher: typeof fetch, cancel?: AbortSignal): typeof fetch {
let apiRequests = 0;
let downloads = 0;
let bytes = 0;
- const signal = AbortSignal.timeout(IMPORT_LIMITS.ms);
+ const timeout = AbortSignal.timeout(IMPORT_LIMITS.ms);
+ const signal = cancel ? AbortSignal.any([timeout, cancel]) : timeout;
return async (input, init) => {
const listing = String(input).startsWith(`${API}/`);
if (listing ? ++apiRequests > IMPORT_LIMITS.apiRequests : ++downloads > IMPORT_LIMITS.downloads) {
@@ -357,10 +358,11 @@ async function fetchSkills(target: Target, fetcher: typeof fetch): Promise<Fetch
export async function fetchSkillFromSource(
input: string,
fetcher: typeof fetch = fetch,
+ cancel?: AbortSignal,
): Promise<{ skills: FetchedSkill[] } | { error: string }> {
const parsed = parseSkillSource(input);
if ("error" in parsed) return parsed;
- fetcher = boundedImportFetch(fetcher);
+ fetcher = boundedImportFetch(fetcher, cancel);
try {
return { skills: await fetchSkills(parsed, fetcher) };
} catch (error) {
diff --git a/server/skill-request-card.test.ts b/server/skill-request-card.test.ts
index 65b13050ad..2ff56d9457 100644
--- a/server/skill-request-card.test.ts
+++ b/server/skill-request-card.test.ts
@@ -40,4 +40,13 @@ describe("importedSkillRequest", () => {
expect(await importing("acme/skills")).toEqual({ importError: expect.stringContaining("acme/skills has 2 skills (") });
expect(await importing("acme/notes")).toEqual({ importError: expect.stringMatching(/^Could not import a skill from acme\/notes: /) });
});
+
+ it("stops fetching once the turn's signal aborts", async () => {
+ const turnEnded = new AbortController();
+ turnEnded.abort();
+ const before = github.requests.length;
+ const body = await importedSkillRequest({ action: "import", source: "https://github.com/acme/skills/tree/main/skills/pdf" }, github.fetch, turnEnded.signal);
+ expect(body).toEqual({ importError: expect.stringMatching(/^Could not import a skill from /) });
+ expect(github.requests.length).toBe(before);
+ });
});
diff --git a/server/skill-request-card.ts b/server/skill-request-card.ts
index 98a5d11376..3ad639c69b 100644
--- a/server/skill-request-card.ts
+++ b/server/skill-request-card.ts
@@ -56,11 +56,11 @@ export function parseSkillManageReferences(value: unknown): SkillFile[] | { erro
* the person's computer) and turn it into a create request, so it gets the same card, scan and name checks as one the
* bot wrote. Only SKILL.md and its references/<topic>.md files come in; the card says what was left behind.
*/
-export async function importedSkillRequest<T extends Record<string, unknown>>(body: T, fetcher: typeof fetch = fetch): Promise<T | { importError: string }> {
+export async function importedSkillRequest<T extends Record<string, unknown>>(body: T, fetcher: typeof fetch = fetch, cancel?: AbortSignal): Promise<T | { importError: string }> {
if (body.action !== "import") return body;
const source = typeof body.source === "string" ? body.source.trim() : "";
if (!source) return { importError: "skill_manage import needs source: owner/repo, a GitHub link to a skill folder or its SKILL.md, or a skills.sh link." };
- const fetched = await fetchSkillFromSource(source, fetcher);
+ const fetched = await fetchSkillFromSource(source, fetcher, cancel);
if ("error" in fetched) return { importError: `Could not import a skill from ${source}: ${fetched.error}` };
if (fetched.skills.length !== 1) {
const names = fetched.skills.map((skill) => skill.source.split("/").pop()).join(", ");