* fix: fix 3 bugs found during code review 1. formatQuestionsForPrompt called .join() on QuizOption objects, producing "[object Object]" in AI prompts instead of readable option text (e.g. "A. Option1, B. Option2"). 2. json-repair Fix 1 regex double-escaped valid JSON escapes (\t, \n, \r, \b, \f) and couldn't handle strings with escaped quotes. Now uses a proper string-aware regex and preserves valid escapes. 3. PlaybackEngine spotlight/laser actions used synchronous recursion via processNext(), risking stack overflow with many consecutive non-speech actions. Now uses queueMicrotask() to break recursion. https://claude.ai/code/session_01JdigqQtiAvomeifvWSQbjm * fix: ensure allowedActions whitelist applies to non-slide scenes The early return in Step 6 (slide-only action filter) caused Step 7 (allowedActions whitelist) to be skipped for non-slide scenes. This meant hallucinated actions from agents in quiz/interactive/pbl scenes were not filtered by the role-based allowedActions whitelist. Refactored to use a mutable result variable instead of early returns so both filters always apply. https://claude.ai/code/session_01JdigqQtiAvomeifvWSQbjm * fix: prevent video playback hang and division by zero in CJK detection 1. executePlayVideo could hang forever if the video element was invalid or the state change was missed between playVideo() and subscribe(). Added a 5-minute safety timeout to prevent indefinite blocking. 2. CJK language detection in browser TTS divided by chunkText.length without checking for empty strings, producing NaN. Now guards against zero-length text. https://claude.ai/code/session_01JdigqQtiAvomeifvWSQbjm * fix: prevent ordered entry with invalid index when partial text has no remaining content When a previously-partial text item completed but all content had already been streamed as deltas (remaining is empty), the code still pushed an ordered entry pointing to textChunks.length - 1, which could be -1 or reference an unrelated chunk. This caused spurious warnings in director-graph and potential text loss. Now only pushes the ordered entry when there is actual remaining content to emit. https://claude.ai/code/session_01JdigqQtiAvomeifvWSQbjm * fix: validate quiz points parameter and fix parse-pdf metadata spread order 1. quiz-grade: `points` from request body was used without validation, causing NaN in score calculation and invalid JSON responses when points was undefined, negative, or non-numeric. Now validates it is a positive finite number. 2. parse-pdf: metadata spread order put the default `pageCount` before `...result.metadata`, so the spread would overwrite the fallback. Fixed by spreading metadata first, then applying defaults after. Also changed `||` to `??` so pageCount=0 is preserved correctly. https://claude.ai/code/session_01JdigqQtiAvomeifvWSQbjm * fix: crash in moveUpElement, dead-code in video options, wrong parens in canCombine 1. use-order-element.ts: moveUpElement crashed with TypeError when a grouped element was already at the top of the z-order. The code accessed copyOfElementList[maxLevel + 1] without bounds checking, then dereferenced .groupId on undefined. Added early return guard matching the pattern used in the non-grouped branch. 2. video-providers.ts: normalizeVideoOptions had a dead-code ternary inside an if-block that already proved the condition was false. Simplified to directly use the fallback value. 3. use-canvas-operations.ts: canCombine check had incorrect parenthesization: (el.groupId && el.groupId) === firstGroupId evaluated the && first, which is equivalent to just el.groupId. Fixed to: el.groupId && el.groupId === firstGroupId. https://claude.ai/code/session_01JdigqQtiAvomeifvWSQbjm * style: fix prettier formatting in 3 files https://claude.ai/code/session_01JdigqQtiAvomeifvWSQbjm * fix: use valid ApiErrorCode in quiz-grade route INVALID_PARAMETER is not a member of API_ERROR_CODES. Use INVALID_REQUEST instead, which is the appropriate code for invalid request parameters. https://claude.ai/code/session_01JdigqQtiAvomeifvWSQbjm --------- Co-authored-by: Claude <noreply@anthropic.com>
154 lines
5.3 KiB
TypeScript
154 lines
5.3 KiB
TypeScript
/**
|
|
* Action Parser - converts structured JSON Array output to Action[]
|
|
*
|
|
* Bridges the stateless-generate parser (used for online streaming) with the
|
|
* offline generation pipeline, producing typed Action objects that preserve
|
|
* the original interleaving order from the LLM output.
|
|
*
|
|
* For complete (non-streaming) responses, uses JSON.parse with partial-json
|
|
* fallback for robustness.
|
|
*/
|
|
|
|
import type { Action, ActionType } from '@/lib/types/action';
|
|
import { SLIDE_ONLY_ACTIONS } from '@/lib/types/action';
|
|
import { nanoid } from 'nanoid';
|
|
import { parse as parsePartialJson, Allow } from 'partial-json';
|
|
import { jsonrepair } from 'jsonrepair';
|
|
import { createLogger } from '@/lib/logger';
|
|
const log = createLogger('ActionParser');
|
|
|
|
/**
|
|
* Strip markdown code fences (```json ... ``` or ``` ... ```) from a response string.
|
|
*/
|
|
function stripCodeFences(text: string): string {
|
|
// Remove opening ```json or ``` and closing ```
|
|
return text.replace(/^```(?:json)?\s*\n?/i, '').replace(/\n?\s*```\s*$/i, '');
|
|
}
|
|
|
|
/**
|
|
* Parse a complete LLM response in JSON Array format into an ordered Action[] array.
|
|
*
|
|
* Expected format (new):
|
|
* [{"type":"action","name":"spotlight","params":{"elementId":"..."}},
|
|
* {"type":"text","content":"speech content"},...]
|
|
*
|
|
* Also supports legacy format:
|
|
* [{"type":"action","tool_name":"spotlight","parameters":{"elementId":"..."}},...]
|
|
*
|
|
* Text items become `speech` actions; action items are converted to their
|
|
* respective action types (spotlight, discussion, etc.).
|
|
* The original interleaving order is preserved.
|
|
*/
|
|
export function parseActionsFromStructuredOutput(
|
|
response: string,
|
|
sceneType?: string,
|
|
allowedActions?: string[],
|
|
): Action[] {
|
|
// Step 1: Strip markdown code fences if present
|
|
const cleaned = stripCodeFences(response.trim());
|
|
|
|
// Step 2: Find the JSON array range
|
|
const startIdx = cleaned.indexOf('[');
|
|
const endIdx = cleaned.lastIndexOf(']');
|
|
|
|
if (startIdx === -1) {
|
|
log.warn('No JSON array found in response');
|
|
return [];
|
|
}
|
|
|
|
const jsonStr = endIdx > startIdx ? cleaned.slice(startIdx, endIdx + 1) : cleaned.slice(startIdx); // unclosed array — let partial-json handle it
|
|
|
|
// Step 3: Parse — try JSON.parse first, then jsonrepair, fallback to partial-json
|
|
let items: unknown[];
|
|
try {
|
|
items = JSON.parse(jsonStr);
|
|
} catch {
|
|
// Try jsonrepair to fix malformed JSON (e.g. unescaped quotes in Chinese text)
|
|
try {
|
|
items = JSON.parse(jsonrepair(jsonStr));
|
|
log.info('Recovered malformed JSON via jsonrepair');
|
|
} catch {
|
|
try {
|
|
items = parsePartialJson(
|
|
jsonStr,
|
|
Allow.ARR | Allow.OBJ | Allow.STR | Allow.NUM | Allow.BOOL | Allow.NULL,
|
|
);
|
|
} catch (e) {
|
|
log.warn('Failed to parse JSON array:', (e as Error).message);
|
|
return [];
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!Array.isArray(items)) {
|
|
log.warn('Parsed result is not an array');
|
|
return [];
|
|
}
|
|
|
|
// Step 4: Convert items to Action[]
|
|
const actions: Action[] = [];
|
|
|
|
for (const item of items) {
|
|
if (!item || typeof item !== 'object' || !('type' in item)) continue;
|
|
const typedItem = item as Record<string, unknown>;
|
|
|
|
if (typedItem.type === 'text') {
|
|
const text = ((typedItem.content as string) || '').trim();
|
|
if (text) {
|
|
actions.push({
|
|
id: `action_${nanoid(8)}`,
|
|
type: 'speech',
|
|
text,
|
|
});
|
|
}
|
|
} else if (typedItem.type === 'action') {
|
|
try {
|
|
// Support both new format (name/params) and legacy format (tool_name/parameters)
|
|
const actionName = typedItem.name || typedItem.tool_name;
|
|
const actionParams = (typedItem.params || typedItem.parameters || {}) as Record<
|
|
string,
|
|
unknown
|
|
>;
|
|
actions.push({
|
|
id: (typedItem.action_id || typedItem.tool_id || `action_${nanoid(8)}`) as string,
|
|
type: actionName as Action['type'],
|
|
...actionParams,
|
|
} as Action);
|
|
} catch (_e) {
|
|
log.warn('Invalid action item, skipping:', JSON.stringify(typedItem).slice(0, 100));
|
|
}
|
|
}
|
|
}
|
|
|
|
// Step 5: Post-processing — discussion must be the last action, and at most one
|
|
const discussionIdx = actions.findIndex((a) => a.type === 'discussion');
|
|
if (discussionIdx !== -1 && discussionIdx < actions.length - 1) {
|
|
actions.splice(discussionIdx + 1);
|
|
}
|
|
|
|
// Step 6: Filter out slide-only actions for non-slide scenes (defense in depth)
|
|
let result = actions;
|
|
if (sceneType && sceneType !== 'slide') {
|
|
const before = result.length;
|
|
result = result.filter((a) => !SLIDE_ONLY_ACTIONS.includes(a.type as ActionType));
|
|
if (result.length < before) {
|
|
log.info(`Stripped ${before - result.length} slide-only action(s) from ${sceneType} scene`);
|
|
}
|
|
}
|
|
|
|
// Step 7: Filter by allowedActions whitelist (defense in depth for role-based isolation)
|
|
// Catches hallucinated actions not in the agent's permitted set, e.g. a student agent
|
|
// mimicking spotlight/laser after seeing teacher actions in chat history.
|
|
if (allowedActions && allowedActions.length > 0) {
|
|
const before = result.length;
|
|
result = result.filter((a) => a.type === 'speech' || allowedActions.includes(a.type));
|
|
if (result.length < before) {
|
|
log.info(
|
|
`Stripped ${before - result.length} disallowed action(s) by allowedActions whitelist`,
|
|
);
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|