fix(hud): state word first, layer names last; CJK cap by locale

Follow-up review fixes:

- The HUD line is STATE | text | LAYERS for the AI summary and the
  fallback line, so the narrow minimal and cyber layouts show both the
  warning and the summary. get_hud_caption builds its caption the same
  way. The unused "{state} {layers}" key is pruned from the catalogs.
- The 20-character cap applies only when the request locale is Japanese
  or Chinese; other languages keep five words around a Han place name.
- enabledLayers names are localized too, and the instructions name layers
  only from enabledLayerLabels, never from feedProvenance or its note.
- The keyless test restores GEV_RATELIMIT_OPENAI_PER_MIN again; comments
  and docs updated.
This commit is contained in:
German Khokhlov committed 2026-10-30 14:27:59 +04:00
1 parent f967218a04
commit 7b223d7d7b
17 files changed
+80 -51

No files matched your search

+7 -5
View File
@@ -4,6 +4,7 @@ import {
HUD_SUMMARY_RESPONSE_FORMAT,
hudSummaryInput,
hudSummaryInstructions,
hudSummaryIsUnspaced,
keylessHudSummaryResponse,
} from '../../../src/hudSummaryResponse.js';
import { enforceRateLimit, openAiRateLimiter } from './rate-limit.js';
@@ -23,7 +24,7 @@ function extractOpenAiResponseText(data) {
}
/** Parse the summary and normalize the canonical feed state. */
function parseHudSummaryReply(text) {
function parseHudSummaryReply(text, locale) {
let reply = null;
try {
reply = JSON.parse(text);
@@ -34,12 +35,12 @@ function parseHudSummaryReply(text) {
? reply.feedState
: null;
return {
summary: toFiveWordHudSummary(reply?.summary),
summary: toFiveWordHudSummary(reply?.summary, hudSummaryIsUnspaced(locale)),
feedState: feedState === 'none' ? null : feedState,
};
}
function toFiveWordHudSummary(value) {
function toFiveWordHudSummary(value, unspaced) {
const words = String(value || '')
.replace(/[^\p{L}\p{N}\s-]/gu, ' ')
.trim()
@@ -48,8 +49,8 @@ function toFiveWordHudSummary(value) {
.slice(0, 5)
.join(' ');
// Japanese and Chinese have no spaces to count words by: cap characters.
if (!/[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}]/u.test(words))
return words;
// Other languages keep five words, even around a Han place name.
if (!unspaced) return words;
const graphemes = new Intl.Segmenter(undefined, { granularity: 'grapheme' });
return [...graphemes.segment(words)]
.slice(0, HUD_SUMMARY_MAX_UNSPACED_CHARS)
@@ -117,6 +118,7 @@ async function handleHudSummary(req, res) {
const data = await response.json().catch(() => ({}));
const { summary, feedState } = parseHudSummaryReply(
extractOpenAiResponseText(data),
context?.locale,
);
res.statusCode = response.ok && summary ? 200 : response.status || 502;
res.setHeader('Content-Type', 'application/json; charset=utf-8');