Study/server/ai/activity.ts
Elijah c595e0ff2c
All checks were successful
Verify and publish V2 container / build-and-push (push) Successful in 1m0s
Third revision: web search, MCP sign-in, exams, intelligent UI, clarifying questions
- Built-in Parallel web search (keyless; optional sign-in for higher limits and research)
  with its own settings page; OAuth sign-in (RFC 9728/8414/7591, PKCE, RFC 8707) for any
  MCP server, with token refresh and pre-registered client support
- Friendly tool steps (icon, name, status; details on click) and ids stripped from results;
  per-chat tool switches; taller thinking box
- Exams within classes: exam tabs, scoped lists, uploads, imports and Generate; exam-only
  material for generation; class setting for what the groups are called
- Assistant can ask clarifying questions in an inline form before generating
- OpenUI blocks (charts, questions, flashcards, inputs) in assistant, Explain and share chats
- Explain beside the quiz on wide screens, grounded in the cited source pages
- Quiz results: percentage ring coloured by score band, rainbow at 100
- Share links: assistant button like the owner's, hidden during quizzes; documents readable
  in the browser
- Live draft and library updates over server-sent events

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 20:30:00 -07:00

74 lines
3 KiB
TypeScript

/**
* What the UI shows for a tool call: a compact line ("Searched the web for: sepsis bundle") with
* a status icon, and details (input, output, pages) when the user expands it.
*/
import type { McpToolBinding, ToolKind, WebSource } from './mcp.ts';
export interface ToolActivity {
id: string;
/** The model-facing tool name. */
name: string;
/** What is happening, in words ("Writing 15 quiz questions"). */
label: string;
status: 'running' | 'done' | 'error';
/** A short result ("12 questions") or error message, shown in the details. */
summary?: string;
kind?: ToolKind | 'tool';
/** Server name for MCP tools. */
server?: string;
/** The query, page or argument the call is about. */
subject?: string;
/** Pages a web tool returned. */
sources?: WebSource[];
/** Arguments and output, clipped, for the details view. */
input?: string;
output?: string;
}
const clip = (text: string, max: number) => (text.length > max ? `${text.slice(0, max - 1)}…` : text);
function hostOf(url: string): string {
try {
return new URL(url).hostname.replace(/^www\./, '');
} catch {
return url;
}
}
/** The subject of a call, from its arguments: the search, the pages, or the first text argument. */
export function toolSubject(kind: ToolKind | 'tool', input: Record<string, unknown>): string | undefined {
const str = (v: unknown) => (typeof v === 'string' && v.trim() ? v.trim() : undefined);
if (kind === 'web_search') {
const queries = Array.isArray(input.search_queries) ? input.search_queries.map(String).filter(Boolean) : [];
return clip(queries[0] ?? str(input.objective) ?? '', 120) || undefined;
}
if (kind === 'web_fetch') {
const urls = Array.isArray(input.urls) ? input.urls.map(String) : [];
const hosts = [...new Set(urls.map(hostOf))];
return hosts.length ? clip(hosts.slice(0, 3).join(', ') + (hosts.length > 3 ? ` and ${hosts.length - 3} more` : ''), 120) : undefined;
}
if (kind === 'web_research') return clip(str(input.input) ?? str(input.question) ?? str(input.query) ?? '', 120) || undefined;
for (const v of Object.values(input)) {
const s = str(v);
if (s) return clip(s, 100);
}
return undefined;
}
/** Activity fields for an MCP (or web) call before it runs. */
export function mcpActivity(binding: McpToolBinding, id: string, input: Record<string, unknown>): ToolActivity {
const subject = toolSubject(binding.kind, input ?? {});
const label =
binding.kind === 'web_search'
? `Searching the web${subject ? ` for ${subject}` : ''}`
: binding.kind === 'web_fetch'
? `Reading ${subject ?? 'web pages'}`
: binding.kind === 'web_research'
? `Researching${subject ? ` ${subject}` : ' on the web'}`
: `${binding.serverName}: ${binding.title}`;
return { id, name: binding.def.name, label, status: 'running', kind: binding.kind, server: binding.kind === 'mcp' ? binding.serverName : undefined, subject, input: clip(JSON.stringify(input ?? {}, null, 2), 2000) };
}
export function clipOutput(text: string): string {
return clip(text, 3000);
}