Files
gods-eye-view/src/voice/chatAttachments.js
T
manjunath22466 da8b0dd8cd fix(voice): address review of typed AI chat
Typed chat sessions no longer end on the voice inactivity timeout, and a
failed or stopped reply keeps the conversation: Stop cancels only the
reply in progress, and map actions that already ran stay in the model's
context. Each upstream request and map action gets its own deadline, old
tool results shrink instead of the history overflowing, and step limits,
a missing or rejected key, the OAuth lock and throttling each get their
own message.

- Server: OpenAI chat is off under GEV_PREFER_CODEX_OAUTH; chat throttles
  default to the provider's throttle; replies carry API_KEY_MISSING,
  API_KEY_REJECTED, CODEX_OAUTH_REQUIRED and RATE_LIMITED codes; upstream
  Retry-After passes through, and a rejected key (Gemini's invalid-key 400
  included) cools down for a minute
- Pinokio forwards the chat throttle settings
- UI: the reply reveal publishes only the message list; the border orbit
  runs only while busy; layers attach with a button or Enter; the log is
  aria-busy while a reply types out; auto-scroll respects a reader who
  scrolled up; Escape closes the attach menu; the chat gear says Settings;
  a note says chat bills the OpenAI key when voice uses ChatGPT sign-in
- Layout does less work while panel height animates; unused blurs removed
- Gemini voice start ends chat; the voice preview clears on every new
  session
- Tests for the route gates, client limits, attachments and the fixes
2026-11-03 22:01:08 +05:30

150 lines
4.4 KiB
JavaScript

import { t } from '../i18n/index.js';
/**
* Draft-only attachments, held on the voice control presenter. Providers
* receive text only when the person sends.
*/
export function bindChatAttachments({ ui, dataManager, onChange, setStatus }) {
const items = new Map();
let reading = false;
let locked = false;
let disposed = false;
const publish = (patch = {}) =>
ui.update('chat', {
attachments: [...items].map(([key, item]) => ({ key, name: item.name })),
locked,
...patch,
});
function close() {
ui.update('chat', { attachMenuOpen: false });
}
function add(key, item) {
if (!items.has(key) && items.size >= 4)
throw new Error(t('You can attach up to four items.'));
const others = [...items].filter(([id]) => id !== key);
if (
others.reduce(
(total, [, value]) => total + JSON.stringify(value).length,
0,
) +
JSON.stringify(item).length >
10000
)
throw new Error(
t('Attachments are too large together. Use smaller files.'),
);
items.set(key, item);
publish();
onChange();
}
const offs = [
ui.on('chat-attach-toggle', () => {
if (locked) return;
const open = !ui.state.chat.attachMenuOpen;
publish({
attachMenuOpen: open,
layers: [...(dataManager?.layers || [])].map(([id, entry]) => ({
value: id,
label: entry.module.name || id,
})),
});
if (open) void ui.focus(['chatLayer']);
}),
ui.on('chat-layer', (id) => {
if (locked || !id) return;
const entry = dataManager?.layers?.get(id);
if (!entry) return;
try {
add(`layer:${id}`, {
kind: 'map-layer',
id,
name: entry.module.name || id,
});
close();
void ui.focus(['chatInput']);
} catch (error) {
setStatus(error.message);
}
}),
// Escape closes the menu and returns to its Attach button.
ui.on('chat-attach-keydown', (event) => {
if (event.key !== 'Escape' || !ui.state.chat.attachMenuOpen) return;
event.preventDefault();
event.stopPropagation();
close();
ui.element('chatPanel')?.querySelector?.('#gev-chat-attach')?.focus?.();
}),
ui.on('chat-file-button', () => {
if (!locked) ui.element('chatFile')?.click();
}),
ui.on('chat-files', async (fileInput) => {
if (reading || locked) return;
const files = [...(fileInput?.files || [])];
reading = true;
onChange();
try {
for (const file of files) {
if (!/\.(txt|md|csv|json|geojson)$/i.test(file.name))
throw new Error(
t('Choose a TXT, Markdown, CSV, JSON or GeoJSON file.'),
);
if (file.size > 10000)
throw new Error(t('Each file must be 10 KB or smaller.'));
const bytes = await file.arrayBuffer();
if (disposed) return;
let content = '';
try {
content = new TextDecoder('utf-8', { fatal: true }).decode(bytes);
} catch {
// Not UTF-8: the decoder's own message is not the person's words.
}
if (!content.trim() || /[\u0000-\u0008\u000e-\u001f]/.test(content))
throw new Error(t('Choose a non-empty UTF-8 text file.'));
add(`file:${file.name}`, { kind: 'file', name: file.name, content });
}
close();
void ui.focus(['chatInput']);
} catch (error) {
if (!disposed) setStatus(error.message);
} finally {
reading = false;
if (!disposed) {
if (fileInput) fileInput.value = '';
onChange();
}
}
}),
ui.on('chat-attachment-remove', (key) => {
if (locked || !items.delete(key)) return;
publish();
onChange();
}),
];
return {
setLocked(value) {
if (locked === Boolean(value)) return;
locked = Boolean(value);
publish(locked ? { attachMenuOpen: false } : {});
},
get reading() {
return reading;
},
close,
snapshot() {
return [...items.values()].map((item) =>
item.kind === 'map-layer'
? { ...item, enabled: Boolean(dataManager?.isEnabled?.(item.id)) }
: { ...item },
);
},
clear() {
items.clear();
publish({ attachMenuOpen: false });
},
destroy() {
disposed = true;
for (const off of offs) off();
},
};
}