mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 08:12:24 +00:00
Both stack parents (#837, #842) are on main, so the voice geometry branch now builds on main directly. Conflict resolutions keep main's behaviour and add the geometry work on top: osm_query gating on every provider, restored admin identity for Nominatim outlines, merged analyst follow-up memory, and the cancelled Globe View reset instruction ahead of the generic cancellation.
338 lines
11 KiB
JavaScript
338 lines
11 KiB
JavaScript
import {
|
|
planStepLabel,
|
|
progressStepLabel,
|
|
shownEntityLabel,
|
|
spokenLabel,
|
|
} from './speech.js';
|
|
import { presentResult } from './resultDisplay.js';
|
|
import { MAX_DISPLAYED_REFERENTS, normalizeReferents } from './referents.js';
|
|
|
|
import { getLocale, msg, t } from '../i18n/index.js';
|
|
/**
|
|
* Pure presentation of the voice card nested in the voice control: captions,
|
|
* the current plan, the latest result and its numbered referents.
|
|
*
|
|
* `reduceVoiceCard` folds provider-neutral session events (`state`,
|
|
* `interruption`, `transcript`, `action-call`, `progress`, `action-result`)
|
|
* plus the card's own `outline` and `dismiss` events into a snapshot;
|
|
* `voiceCardView` derives what each element shows.
|
|
*/
|
|
|
|
const MAX_STEPS = 6;
|
|
const MAX_LINES = 6;
|
|
const MAX_NOTES = 6;
|
|
const MAX_CAPTION = 220;
|
|
// Titles and lines wrap; the caps only stop runaway values. Translations run
|
|
// longer (resultDisplay.js), so they cap at 120. Error lines are never cut.
|
|
const MAX_TITLE = 64;
|
|
const MAX_TRANSLATED_TITLE = 120;
|
|
const MAX_LINE = 80;
|
|
|
|
const titleMax = () =>
|
|
getLocale() === 'en' ? MAX_TITLE : MAX_TRANSLATED_TITLE;
|
|
|
|
const STATUS_TEXT = Object.freeze({
|
|
running: msg('in progress'),
|
|
done: msg('done'),
|
|
failed: msg('failed'),
|
|
cancelled: msg('cancelled'),
|
|
});
|
|
|
|
/** An empty, hidden card. */
|
|
export function initialVoiceCardState() {
|
|
return {
|
|
visible: false,
|
|
dismissed: false,
|
|
user: null,
|
|
userAttribution: null,
|
|
pointer: null,
|
|
assistant: null,
|
|
assistantResponse: null,
|
|
steps: [],
|
|
stepSeq: 0,
|
|
result: null,
|
|
announce: '',
|
|
};
|
|
}
|
|
|
|
function caption(text) {
|
|
const clean = String(text || '')
|
|
.replace(/\s+/g, ' ')
|
|
.trim();
|
|
return clean.length > MAX_CAPTION
|
|
? `…${clean.slice(-(MAX_CAPTION - 1))}`
|
|
: clean;
|
|
}
|
|
|
|
function newTurn(state) {
|
|
return {
|
|
...state,
|
|
visible: true,
|
|
dismissed: false,
|
|
user: '',
|
|
userAttribution: null,
|
|
pointer: null,
|
|
assistant: null,
|
|
assistantResponse: null,
|
|
steps: [],
|
|
result: null,
|
|
};
|
|
}
|
|
|
|
function reveal(state) {
|
|
return state.dismissed ? state : { ...state, visible: true };
|
|
}
|
|
|
|
/**
|
|
* Patch the step an event belongs to: the step with the event's call id, or,
|
|
* for adapters without call ids, the latest matching step of that tool.
|
|
* Returns the same snapshot when nothing matches, so stale or unmatched
|
|
* events never re-render or reveal the card.
|
|
*/
|
|
function patchStep(state, event, match, patch) {
|
|
for (let index = state.steps.length - 1; index >= 0; index--) {
|
|
const step = state.steps[index];
|
|
const sameCall = event.callId
|
|
? step.callId === event.callId
|
|
: !step.callId && step.name === event.name;
|
|
if (!sameCall || !match(step)) continue;
|
|
const steps = state.steps.slice();
|
|
steps[index] = { ...step, ...patch(step) };
|
|
return { ...state, steps };
|
|
}
|
|
return state;
|
|
}
|
|
|
|
function annotationIds(result) {
|
|
return (Array.isArray(result.items) ? result.items : [])
|
|
.filter((item) => item?.ok && item.outlinePending && item.id)
|
|
.map((item) => String(item.id));
|
|
}
|
|
|
|
/**
|
|
* Fold one event into the card snapshot. Unknown, stale or unmatched events
|
|
* return the same snapshot, so callers can skip rendering.
|
|
*/
|
|
export function reduceVoiceCard(state, event) {
|
|
if (!event?.type) return state;
|
|
switch (event.type) {
|
|
case 'state':
|
|
return ['idle', 'error'].includes(event.state)
|
|
? initialVoiceCardState()
|
|
: state;
|
|
case 'interruption':
|
|
return newTurn(state);
|
|
case 'dismiss':
|
|
return { ...state, visible: false, dismissed: true };
|
|
case 'idle':
|
|
return { ...state, visible: false };
|
|
case 'pointer': {
|
|
// What "this"/"here" resolved to; a stale pointer clears the chip.
|
|
const pointer =
|
|
event.pointer?.label && ['this', 'here'].includes(event.pointer.kind)
|
|
? { kind: event.pointer.kind, label: event.pointer.label }
|
|
: null;
|
|
if (!pointer && !state.pointer) return state;
|
|
if (!pointer) return { ...state, pointer };
|
|
// The live region says what "this" meant, as the chip does.
|
|
const kind = pointer.kind === 'here' ? 'Here' : 'This';
|
|
return reveal({
|
|
...state,
|
|
pointer,
|
|
announce: `${kind}: ${spokenLabel(shownEntityLabel(pointer.label), 32)}`,
|
|
});
|
|
}
|
|
case 'transcript': {
|
|
const text = event.text || '';
|
|
if (event.role === 'user') {
|
|
return text
|
|
? reveal({
|
|
...state,
|
|
user: caption(text),
|
|
userAttribution:
|
|
event.attribution === 'unverified' ? 'unverified' : null,
|
|
})
|
|
: state;
|
|
}
|
|
if (event.role !== 'assistant' || !text) return state;
|
|
const sameResponse =
|
|
(event.responseId || null) === state.assistantResponse &&
|
|
state.assistant !== null;
|
|
// Deltas accumulate raw (their spacing is significant); only a final
|
|
// transcript is normalized.
|
|
const running = `${sameResponse ? state.assistant : ''}${text}`;
|
|
const assistant = event.final
|
|
? caption(text)
|
|
: running.length > MAX_CAPTION
|
|
? `…${running.slice(-(MAX_CAPTION - 1))}`
|
|
: running;
|
|
return reveal({
|
|
...state,
|
|
assistant,
|
|
assistantResponse: event.responseId || null,
|
|
announce: event.final ? caption(text) : state.announce,
|
|
});
|
|
}
|
|
case 'action-call': {
|
|
const stepSeq = state.stepSeq + 1;
|
|
return reveal({
|
|
...state,
|
|
stepSeq,
|
|
steps: [
|
|
...state.steps,
|
|
{
|
|
id: stepSeq,
|
|
callId: event.callId || null,
|
|
name: event.name,
|
|
label: planStepLabel(event.name, event.arguments || {}),
|
|
status: 'running',
|
|
detail: '',
|
|
annotationIds: [],
|
|
},
|
|
].slice(-MAX_STEPS),
|
|
});
|
|
}
|
|
case 'progress': {
|
|
const next = patchStep(
|
|
state,
|
|
event,
|
|
(step) => step.status === 'running',
|
|
() => ({ detail: progressStepLabel(event.step, event.label) }),
|
|
);
|
|
return next === state ? state : reveal(next);
|
|
}
|
|
case 'action-result': {
|
|
const result = event.result || {};
|
|
const next = patchStep(
|
|
state,
|
|
event,
|
|
(step) => step.status === 'running',
|
|
() => ({
|
|
// A question back ("which Punjab?") is an answer, not a failure.
|
|
status: result.cancelled
|
|
? 'cancelled'
|
|
: result.ok || result.needsClarification
|
|
? 'done'
|
|
: 'failed',
|
|
detail: result.outlinePending ? t('Tracing outline') : '',
|
|
annotationIds: result.outlinePending ? annotationIds(result) : [],
|
|
}),
|
|
);
|
|
// A result for a call this turn never showed is stale.
|
|
if (next === state) return state;
|
|
// One adapter decides what a result shows; a silent lookup or a
|
|
// refusal without a question informs the model and leaves the card.
|
|
const shown = presentResult(event.name, result);
|
|
if (!shown) return reveal(next);
|
|
return reveal({
|
|
...next,
|
|
result: {
|
|
display: shown.display,
|
|
// A failed result's lines are its error: shown whole, wrapped.
|
|
wholeLines: result.ok === false,
|
|
referents: normalizeReferents(
|
|
shown.referents,
|
|
MAX_DISPLAYED_REFERENTS,
|
|
),
|
|
},
|
|
announce: spokenLabel(shown.display.title, titleMax()),
|
|
});
|
|
}
|
|
case 'outline': {
|
|
const id = event.id == null ? null : String(event.id);
|
|
if (!id) return state;
|
|
for (let index = state.steps.length - 1; index >= 0; index--) {
|
|
const step = state.steps[index];
|
|
if (!step.annotationIds?.includes(id)) continue;
|
|
const remaining = step.annotationIds.filter((other) => other !== id);
|
|
const steps = state.steps.slice();
|
|
steps[index] = {
|
|
...step,
|
|
annotationIds: remaining,
|
|
detail: remaining.length
|
|
? t('Tracing outline')
|
|
: event.status === 'resolved'
|
|
? t('Outline traced')
|
|
: t('Outline unavailable'),
|
|
};
|
|
return reveal({ ...state, steps });
|
|
}
|
|
return state;
|
|
}
|
|
default:
|
|
return state;
|
|
}
|
|
}
|
|
|
|
/** Derive element content from a card snapshot. */
|
|
export function voiceCardView(state) {
|
|
const running = state.steps.some((step) => step.status === 'running');
|
|
const display = state.result?.display || null;
|
|
const allNotes = [
|
|
...(display?.notes || []),
|
|
...(display?.sources || []).map((source) =>
|
|
t('Source: {label}', { label: source.label }),
|
|
),
|
|
];
|
|
// Analyst notes carry count/scope/provenance qualifications. Preserve their
|
|
// full sanitized text in at most six existing rows, joining overflow into
|
|
// the last row. Other tool cards retain their compact note contract.
|
|
const completeNotes = display?.preserveNotes
|
|
? [
|
|
...new Set(
|
|
allNotes.map((note) => spokenLabel(note, Infinity)).filter(Boolean),
|
|
),
|
|
]
|
|
: null;
|
|
const notes = completeNotes
|
|
? completeNotes.length > MAX_NOTES
|
|
? [
|
|
...completeNotes.slice(0, MAX_NOTES - 1),
|
|
completeNotes.slice(MAX_NOTES - 1).join('; '),
|
|
]
|
|
: completeNotes
|
|
: allNotes.map((note) => spokenLabel(note, 96)).slice(0, MAX_NOTES);
|
|
return {
|
|
visible: Boolean(state.visible),
|
|
busy: running,
|
|
phase: running ? t('WORKING') : state.steps.length ? t('DONE') : '',
|
|
user: {
|
|
label:
|
|
state.userAttribution === 'unverified' ? t('Recent speech') : t('YOU'),
|
|
visible: state.user !== null,
|
|
text: state.user || '…',
|
|
},
|
|
pointer: {
|
|
visible: Boolean(state.pointer),
|
|
kind: state.pointer?.kind === 'here' ? t('HERE') : t('THIS'),
|
|
text: spokenLabel(shownEntityLabel(state.pointer?.label || ''), 32),
|
|
},
|
|
assistant: {
|
|
visible: Boolean(state.assistant),
|
|
text: state.assistant || '',
|
|
},
|
|
steps: state.steps.map((step) => ({
|
|
key: String(step.id),
|
|
label: step.label,
|
|
status: step.status,
|
|
statusText: STATUS_TEXT[step.status] ? t(STATUS_TEXT[step.status]) : '',
|
|
detail: step.detail || '',
|
|
})),
|
|
result: {
|
|
visible: Boolean(display?.title),
|
|
title: spokenLabel(display?.title || '', titleMax()),
|
|
chips: (display?.chips || []).map((chip) => spokenLabel(chip.label, 24)),
|
|
lines: (display?.lines || [])
|
|
.map((line) =>
|
|
spokenLabel(line, state.result?.wholeLines ? Infinity : MAX_LINE),
|
|
)
|
|
.slice(0, MAX_LINES),
|
|
referents: (state.result?.referents || [])
|
|
.slice(0, MAX_DISPLAYED_REFERENTS)
|
|
.map((ref) => ({ n: ref.n, label: spokenLabel(ref.label, 48) })),
|
|
notes,
|
|
},
|
|
announce: state.announce || '',
|
|
};
|
|
}
|