Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions app/api/shape/route.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { NextRequest, NextResponse } from "next/server";
import { shape } from "@/lib/shape";
import { reportShapeError } from "@/lib/monitoring";
import type { ShapeEngine, ShapeProfile } from "@/lib/types";

const VALID_PROFILES = ["narrative_segment_v0", "concept_blob_v0"];
const VALID_ENGINES = ["openai", "local"];

export async function POST(request: NextRequest) {
let engineForReport: ShapeEngine | "unknown" = "unknown";
let profileForReport: ShapeProfile | "unknown" | "default" = "unknown";
try {
const body = await request.json();
const text = body?.text;
Expand All @@ -27,11 +30,17 @@ export async function POST(request: NextRequest) {
engine && VALID_ENGINES.includes(engine)
? (engine as ShapeEngine)
: "openai";
engineForReport = engineOverride;
profileForReport = profileOverride ?? "default";

const result = await shape(text, profileOverride, engineOverride);
return NextResponse.json(result);
} catch (err: unknown) {
const message = err instanceof Error ? err.message : "Unknown error";
await reportShapeError(err, "POST /api/shape", {
engine: engineForReport,
profile: profileForReport,
});
return NextResponse.json({ error: message }, { status: 500 });
}
}
8 changes: 7 additions & 1 deletion lib/engine.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { buildSystemPrompt } from "./prompt";
import { runLocalShape } from "./local-engine";
import { runOpenAIShapePrompt } from "./model";
import { reportShapeError } from "./monitoring";
import type { ShapeEngine, ShapeProfile } from "./types";

export async function runShapeEngine({
Expand All @@ -21,7 +22,12 @@ export async function runShapeEngine({

try {
return JSON.parse(raw);
} catch {
} catch (err) {
await reportShapeError(err, "runShapeEngine.parseModelJson", {
engine,
profile,
raw_preview: raw.slice(0, 200),
});
throw new Error("Model returned invalid JSON");
}
}
68 changes: 68 additions & 0 deletions lib/monitoring.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Production error monitoring for Shape's critical paths.
//
// When SHAPE_MONITORING_DSN is configured, server-side errors in the
// Shape API route and engine are reported to the monitoring backend
// (Sentry-compatible payload). When the DSN is unset, this module
// is a no-op so local development and unauthenticated previews don't
// emit telemetry.
//
// Use reportShapeError(err, context, extra?) at every catch site that
// sits on a request path. Without it, model failures, JSON parse
// errors, and provider outages disappear into a 500 with no alerting.

const DSN = process.env.SHAPE_MONITORING_DSN || "";
const RELEASE = process.env.SHAPE_RELEASE || "dev";
const ENVIRONMENT = process.env.SHAPE_ENVIRONMENT || process.env.NODE_ENV || "unknown";

export function isMonitoringEnabled(): boolean {
return DSN !== "";
}

export interface ShapeErrorContext {
context: string;
release: string;
environment: string;
message: string;
stack?: string;
timestamp: string;
[key: string]: unknown;
}

/**
* Send a structured error report to the monitoring backend.
*
* Fire-and-forget. Failures inside the monitor itself MUST NOT crash the
* caller — a request path that depends on its own alerting being healthy
* is its own outage source.
*/
export async function reportShapeError(
error: unknown,
context: string,
additionalData?: Record<string, unknown>,
): Promise<void> {
if (!isMonitoringEnabled()) {
return;
}
const payload: ShapeErrorContext = {
context,
release: RELEASE,
environment: ENVIRONMENT,
message: error instanceof Error ? error.message : String(error),
stack: error instanceof Error ? error.stack : undefined,
timestamp: new Date().toISOString(),
...(additionalData ?? {}),
};
try {
await fetch(DSN, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(payload),
keepalive: true,
});
} catch {
// Monitoring egress failures should not break the request path.
// The local console line below survives regardless of DSN state
// so a developer tailing logs still sees the original error.
console.error(`[shape:monitor:dropped] ${context}:`, error);
}
}