2Branches0Tags
GL
glucryptoSync production with master template updates
075e1302 months ago14Commits
typescript
import type { ToolProfile } from "opentool"; import { retrieve, store } from "opentool/store"; import { IMPORTANT_DATES_ROUNDUP_TEMPLATE_CONFIG, requestOverrideSchema, resolveRuntimeConfig, } from "../src/config"; import { runImportantDatesRoundup } from "../src/important-dates-roundup"; export const schema = requestOverrideSchema; export const profile: ToolProfile = { description: "Upcoming important dates roundup over the gateway macro calendar.", category: "tracker", templatePreview: { subtitle: "Query upcoming important dates and return a structured date list.", description: `Fetches upcoming dates from the gateway macro calendar. Builds a deterministic list of the nearest releases and why they matter. Returns machine-readable JSON suitable for cron digests and email summaries.`, }, templateConfig: IMPORTANT_DATES_ROUNDUP_TEMPLATE_CONFIG, }; async function readLatestSuccessfulRoundup() { const history = await retrieve({ source: "important-dates-roundup", history: true, limit: 10, }); const latest = history.items.find((item) => { const metadata = item.metadata; return ( metadata && typeof metadata === "object" && !Array.isArray(metadata) && (metadata as { ok?: unknown }).ok === true ); }); if (!latest) { return null; } return latest.metadata ?? null; } export async function POST(req: Request) { const payload = await req.json().catch(() => null); if ( payload && typeof payload === "object" && !Array.isArray(payload) && (payload as { cachedOnly?: unknown }).cachedOnly === true ) { try { const latest = await readLatestSuccessfulRoundup(); if (!latest) { return new Response( JSON.stringify({ ok: false, error: "Not Found", }), { status: 404, headers: { "content-type": "application/json" }, }, ); } return Response.json(latest); } catch (error) { const message = error instanceof Error ? error.message : "unknown"; return new Response(JSON.stringify({ ok: false, error: message }), { status: 500, headers: { "content-type": "application/json" }, }); } } const parsed = requestOverrideSchema.safeParse(payload ?? {}); if (!parsed.success) { return new Response( JSON.stringify({ ok: false, error: parsed.error.issues[0]?.message ?? "invalid request payload", }), { status: 400, headers: { "content-type": "application/json" }, }, ); } try { const config = resolveRuntimeConfig(parsed.data); const result = await runImportantDatesRoundup(config); await store({ source: "important-dates-roundup", ref: `important-dates-roundup-${Date.now()}`, status: "info", action: "roundup", metadata: result, }); return Response.json(result); } catch (error) { const message = error instanceof Error ? error.message : "unknown"; await store({ source: "important-dates-roundup", ref: `important-dates-roundup-${Date.now()}`, status: "failed", action: "roundup", metadata: { ok: false, error: message }, }); return new Response(JSON.stringify({ ok: false, error: message }), { status: 400, headers: { "content-type": "application/json" }, }); } }