diff --git a/next-app/app/api/chat/stream/route.ts b/next-app/app/api/chat/stream/route.ts index 1e7bf8b7..e0b32b7d 100644 --- a/next-app/app/api/chat/stream/route.ts +++ b/next-app/app/api/chat/stream/route.ts @@ -10,6 +10,16 @@ function* fakeStream(text: string) { import { preFilter, steerPrompt, postModerate } from '@/lib/safety/pipeline'; +/** + * Streams a message as a server-sent event. + * + * This function creates a ReadableStream that processes a given message through several stages: + * it applies pre-filtering, steering, and post-moderation to generate a reply. The stream emits + * metadata and each chunk of the reply as events, handling errors by sending an error event if + * any exceptions occur during processing. + * + * @param message - The input message to be processed and streamed. + */ function streamForMessage(message: string) { const ctrl = new AbortController(); const stream = new ReadableStream({ diff --git a/next-app/app/api/consent/route.ts b/next-app/app/api/consent/route.ts index 704ba53c..8ed65296 100644 --- a/next-app/app/api/consent/route.ts +++ b/next-app/app/api/consent/route.ts @@ -3,6 +3,15 @@ import { appendConsentEvent, exportConsent } from '@/lib/privacy/consentLedger'; export const runtime = 'nodejs'; +/** + * Handles POST requests to process user consent actions. + * + * This function extracts the userId, sessionId, and action from the request body. + * It validates the action against a predefined list and returns a 400 response for invalid actions. + * If the action is valid, it appends a consent event using the appendConsentEvent function and returns the result as a JSON response. + * + * @param req - The NextRequest object containing the request data. + */ export async function POST(req: NextRequest) { const { userId = 'demo', sessionId, action } = await req.json(); if (!['persist_on','persist_off','export'].includes(action)) return new Response('bad action', { status: 400 }); @@ -10,6 +19,9 @@ export async function POST(req: NextRequest) { return Response.json(ev); } +/** + * Handles GET requests and returns consent data for a user. + */ export async function GET(req: NextRequest) { const { searchParams } = new URL(req.url); const userId = searchParams.get('userId') ?? 'demo'; diff --git a/next-app/app/api/risk/scores/route.ts b/next-app/app/api/risk/scores/route.ts index e160a18f..4f95578c 100644 --- a/next-app/app/api/risk/scores/route.ts +++ b/next-app/app/api/risk/scores/route.ts @@ -1,4 +1,7 @@ export const runtime = 'nodejs'; +/** + * Handles the GET request and returns a mock time-series risk per layer. + */ export async function GET() { // Mock time-series risk per layer: core/operational/context const now = Date.now(); @@ -8,4 +11,7 @@ export async function GET() { })); return Response.json({ series }); } +/** + * Clamps a value between a minimum and maximum range. + */ function clamp(min:number,max:number,v:number){return Math.max(min,Math.min(max,v));} diff --git a/next-app/app/chat/page.tsx b/next-app/app/chat/page.tsx index 9427427e..c75b9c9c 100644 --- a/next-app/app/chat/page.tsx +++ b/next-app/app/chat/page.tsx @@ -2,6 +2,13 @@ import { useEffect, useRef, useState } from 'react'; import { ProvenanceBadge } from '@/components/ProvenanceBadge'; +/** + * Renders the chat page interface and handles message sending and streaming. + * + * The function manages the state for user input, messages, and streaming status. It sets up an EventSource to listen for incoming messages and updates the message list accordingly. It also handles fallback scenarios and cleans up the EventSource on component unmount. + * + * @returns {JSX.Element} The rendered chat page component. + */ export default function ChatPage() { const [input, setInput] = useState(""); const [messages, setMessages] = useState<{ role: 'user'|'assistant'; content: string; meta?: any }[]>([]); diff --git a/next-app/app/docs/governance-terms-mapping/page.tsx b/next-app/app/docs/governance-terms-mapping/page.tsx index 979cdbb1..3c77d3f4 100644 --- a/next-app/app/docs/governance-terms-mapping/page.tsx +++ b/next-app/app/docs/governance-terms-mapping/page.tsx @@ -1,6 +1,9 @@ import { readFileSync } from 'fs'; import path from 'path'; export const dynamic = 'force-static'; +/** + * Renders the content of a markdown file as preformatted text. + */ export default function Page() { const md = readFileSync(path.join(process.cwd(), 'next-app', 'docs', 'governance-terms-mapping.md'), 'utf8'); return
{md}
;