Study/server/ai/activity.ts
Elijah 25bc03ea75
All checks were successful
Verify and publish V2 container / build-and-push (push) Successful in 29s
Fourth revision: OpenRouter, live tool stages, Explain layout, share sections
- Interactive blocks are encouraged by default; Explain gets a reduced library
  and never writes practice questions.
- Assistant tool steps breathe while running, show live stages from generation
  and the Python check, and read in the past tense when done.
- The assistant asks before searching the web when class materials do not
  cover a question; clarifying questions list every class's exams and never
  ask about format (always NCLEX).
- Explain opens beside the question from 900px wide, moving the question left
  while open and back on the next one.
- Generate dialog drops the practice set/exam switch (presets cover it) and the
  Generate buttons lose their icon.
- Python check notes are green when every check passed, applied fixes neutral.
- Share pages group by exam, then Quizzes, Flashcards, Notes, Class Materials,
  hiding empty groups; the class page uses the same order. "Documents" is now
  "Class Materials".
- OpenRouter provider replaces the Ollama type; migration 6 turns Ollama
  connections into OpenAI-compatible ones at /v1.

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

76 lines
3.2 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';
/** While running: what the step is doing right now ("Checking the questions with Python"). */
stage?: string;
/** 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);
}