openpondai/agents/news-intelligence-roundup

OpenTool app

2Branches0Tags
GL
glucryptoSync production with master template updates
ca0f4f02 months ago31Commits
typescript
import type { ToolProfile } from "opentool"; import { retrieve, store } from "opentool/store"; import { NEWS_INTELLIGENCE_ROUNDUP_TEMPLATE_CONFIG, requestOverrideSchema, resolveRuntimeConfig, } from "../src/config"; import { runNewsIntelligenceRoundup } from "../src/news-intelligence-roundup"; export const schema = requestOverrideSchema; export const profile: ToolProfile = { description: "Topic-based roundup over the gateway news-intelligence corpus.", category: "tracker", templatePreview: { subtitle: "Query recent news windows and return a structured roundup.", description: `Runs a query against the news-intelligence pipeline for one or more lookback windows. Builds a structured summary with per-window coverage and top matched articles. Returns machine-readable JSON suitable for cron ingestion and landing-page feeds.`, }, templateConfig: NEWS_INTELLIGENCE_ROUNDUP_TEMPLATE_CONFIG, }; async function readLatestSuccessfulRoundup() { const history = await retrieve({ source: "news-intelligence-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 runNewsIntelligenceRoundup(config); await store({ source: "news-intelligence-roundup", ref: `news-intelligence-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: "news-intelligence-roundup", ref: `news-intelligence-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" }, }); } }