Files
bot/scripts/sync-moonshot-docs.ts
zeekay 635dffde60 refactor(bot): retire Lit control UI for @hanzo/gui v7 admin SPA
The legacy "openclaw-control-ui" Lit app at ui/ is replaced by
@hanzo/bot-admin (~/work/hanzo/gui/apps/admin-bot), built externally
in the gui workspace and synced into dist/control-ui/ via
scripts/sync-admin-ui.sh. Identical static-serve handler — the bot
already configures gateway.controlUi.basePath, just defaults to
/_/bot now to match the canonical hanzo-binary mount.

Changes:

- scripts/sync-admin-ui.sh — rsyncs the built admin-bot bundle into
  dist/control-ui/. Mirrors tasks/scripts/sync-admin-ui.sh exactly.

- src/gateway/iam-identity.ts (+ test) — pure-trust extractor for
  X-Org-Id / X-User-Id / X-User-Email headers set by hanzoai/gateway
  upstream after JWT validation. attachIamIdentity() runs once per
  HTTP request from server-http.ts handleRequest. The bot does NOT
  validate the JWT itself — that's the gateway's job.

- src/infra/control-ui-assets.ts — sentinel for repo root flipped
  from ui/vite.config.ts to scripts/sync-admin-ui.sh; existing
  resolveControlUiRootSync continues to look for dist/control-ui/
  unchanged. Test updated.

- src/data/moonshot-kimi-k2.ts — moved from the deleted ui/ tree
  (only consumer is scripts/sync-moonshot-docs.ts).

- scripts/ui.js — replaced with a 50-line shim:
    pnpm ui:build → calls scripts/sync-admin-ui.sh
    pnpm ui:dev   → prints the gui-workspace dev pointer
    pnpm ui:install → no-op (admin-bot inherits gui workspace deps)

- Dockerfile — drops the COPY of ui/package.json (ui/ is gone) and
  the RUN pnpm ui:build step (the synced dist/control-ui/ is now
  pulled via the existing COPY . . step).

- vitest.config.ts — drops the four ui/src/ui/ test paths and the
  ui/** coverage exclude.

- src/config/schema.{help,hints}.ts — basePath placeholder /openclaw
  replaced with the canonical /_/bot mount.

Identity flow (Node-runtime exception per HANZO_BINARY.md):
  hanzoai/gateway (JWT validate, strip+set X-* headers)
   → bot.handleRequest()
   → attachIamIdentity(req)
   → downstream handlers via getIamIdentity(req)

Tests: 43 passing across iam-identity (8), control-ui-routing (6),
control-ui.http (18), control-ui-csp (2), control-ui-assets (9).

Repo size: -105,289 / +328 lines.
2026-04-27 19:02:33 -07:00

126 lines
3.9 KiB
TypeScript

import { readFile, writeFile } from "node:fs/promises";
import path from "node:path";
import { fileURLToPath } from "node:url";
import {
MOONSHOT_KIMI_K2_CONTEXT_WINDOW,
MOONSHOT_KIMI_K2_COST,
MOONSHOT_KIMI_K2_INPUT,
MOONSHOT_KIMI_K2_MAX_TOKENS,
MOONSHOT_KIMI_K2_MODELS,
} from "../src/data/moonshot-kimi-k2";
const here = path.dirname(fileURLToPath(import.meta.url));
const repoRoot = path.resolve(here, "..");
function replaceBlockLines(
text: string,
startMarker: string,
endMarker: string,
lines: string[],
): string {
const startIndex = text.indexOf(startMarker);
if (startIndex === -1) {
throw new Error(`Missing start marker: ${startMarker}`);
}
const endIndex = text.indexOf(endMarker, startIndex);
if (endIndex === -1) {
throw new Error(`Missing end marker: ${endMarker}`);
}
const startLineStart = text.lastIndexOf("\n", startIndex);
const startLineStartIndex = startLineStart === -1 ? 0 : startLineStart + 1;
const indent = text.slice(startLineStartIndex, startIndex);
const endLineEnd = text.indexOf("\n", endIndex);
const endLineEndIndex = endLineEnd === -1 ? text.length : endLineEnd + 1;
const before = text.slice(0, startLineStartIndex);
const after = text.slice(endLineEndIndex);
const replacementLines = [
`${indent}${startMarker}`,
...lines.map((line) => `${indent}${line}`),
`${indent}${endMarker}`,
];
const replacement = replacementLines.join("\n");
if (!after) {
return `${before}${replacement}`;
}
return `${before}${replacement}\n${after}`;
}
function renderKimiK2Ids(prefix: string) {
return MOONSHOT_KIMI_K2_MODELS.map((model) => `- \`${prefix}${model.id}\``);
}
function renderMoonshotAliases() {
return MOONSHOT_KIMI_K2_MODELS.map((model, index) => {
const isLast = index === MOONSHOT_KIMI_K2_MODELS.length - 1;
const suffix = isLast ? "" : ",";
return `"moonshot/${model.id}": { alias: "${model.alias}" }${suffix}`;
});
}
function renderMoonshotModels() {
const input = JSON.stringify([...MOONSHOT_KIMI_K2_INPUT]);
const cost = `input: ${MOONSHOT_KIMI_K2_COST.input}, output: ${MOONSHOT_KIMI_K2_COST.output}, cacheRead: ${MOONSHOT_KIMI_K2_COST.cacheRead}, cacheWrite: ${MOONSHOT_KIMI_K2_COST.cacheWrite}`;
return MOONSHOT_KIMI_K2_MODELS.flatMap((model, index) => {
const isLast = index === MOONSHOT_KIMI_K2_MODELS.length - 1;
const closing = isLast ? "}" : "},";
return [
"{",
` id: "${model.id}",`,
` name: "${model.name}",`,
` reasoning: ${model.reasoning},`,
` input: ${input},`,
` cost: { ${cost} },`,
` contextWindow: ${MOONSHOT_KIMI_K2_CONTEXT_WINDOW},`,
` maxTokens: ${MOONSHOT_KIMI_K2_MAX_TOKENS}`,
closing,
];
});
}
async function syncMoonshotDocs() {
const moonshotDoc = path.join(repoRoot, "docs/providers/moonshot.md");
const conceptsDoc = path.join(repoRoot, "docs/concepts/model-providers.md");
let moonshotText = await readFile(moonshotDoc, "utf8");
moonshotText = replaceBlockLines(
moonshotText,
"{/_ moonshot-kimi-k2-ids:start _/ && null}",
"{/_ moonshot-kimi-k2-ids:end _/ && null}",
renderKimiK2Ids(""),
);
moonshotText = replaceBlockLines(
moonshotText,
"// moonshot-kimi-k2-aliases:start",
"// moonshot-kimi-k2-aliases:end",
renderMoonshotAliases(),
);
moonshotText = replaceBlockLines(
moonshotText,
"// moonshot-kimi-k2-models:start",
"// moonshot-kimi-k2-models:end",
renderMoonshotModels(),
);
let conceptsText = await readFile(conceptsDoc, "utf8");
conceptsText = replaceBlockLines(
conceptsText,
"{/_ moonshot-kimi-k2-model-refs:start _/ && null}",
"{/_ moonshot-kimi-k2-model-refs:end _/ && null}",
renderKimiK2Ids("moonshot/"),
);
await writeFile(moonshotDoc, moonshotText);
await writeFile(conceptsDoc, conceptsText);
}
syncMoonshotDocs().catch((error) => {
console.error(error);
process.exitCode = 1;
});