feat: integrate Pi backend and Pi Web

This commit is contained in:
luckyyzh
2026-07-30 19:37:53 +08:00
commit 7392ab9dd7
1390 changed files with 337197 additions and 0 deletions
@@ -0,0 +1,87 @@
{
"$schema": "https://raw.githubusercontent.com/earendil-works/pi/main/packages/coding-agent/src/modes/interactive/theme/theme-schema.json",
"name": "dark",
"vars": {
"cyan": "#00d7ff",
"blue": "#5f87ff",
"green": "#b5bd68",
"red": "#cc6666",
"yellow": "#ffff00",
"text": "#d4d4d4",
"gray": "#808080",
"dimGray": "#666666",
"darkGray": "#505050",
"accent": "#8abeb7",
"selectedBg": "#3a3a4a",
"userMsgBg": "#343541",
"toolPendingBg": "#282832",
"toolSuccessBg": "#283228",
"toolErrorBg": "#3c2828",
"customMsgBg": "#2d2838"
},
"colors": {
"accent": "accent",
"border": "blue",
"borderAccent": "cyan",
"borderMuted": "darkGray",
"success": "green",
"error": "red",
"warning": "yellow",
"muted": "gray",
"dim": "dimGray",
"text": "text",
"thinkingText": "gray",
"selectedBg": "selectedBg",
"userMessageBg": "userMsgBg",
"userMessageText": "text",
"customMessageBg": "customMsgBg",
"customMessageText": "text",
"customMessageLabel": "#9575cd",
"toolPendingBg": "toolPendingBg",
"toolSuccessBg": "toolSuccessBg",
"toolErrorBg": "toolErrorBg",
"toolTitle": "text",
"toolOutput": "gray",
"mdHeading": "#f0c674",
"mdLink": "#81a2be",
"mdLinkUrl": "dimGray",
"mdCode": "accent",
"mdCodeBlock": "green",
"mdCodeBlockBorder": "gray",
"mdQuote": "gray",
"mdQuoteBorder": "gray",
"mdHr": "gray",
"mdListBullet": "accent",
"toolDiffAdded": "green",
"toolDiffRemoved": "red",
"toolDiffContext": "gray",
"syntaxComment": "#6A9955",
"syntaxKeyword": "#569CD6",
"syntaxFunction": "#DCDCAA",
"syntaxVariable": "#9CDCFE",
"syntaxString": "#CE9178",
"syntaxNumber": "#B5CEA8",
"syntaxType": "#4EC9B0",
"syntaxOperator": "#D4D4D4",
"syntaxPunctuation": "#D4D4D4",
"thinkingOff": "darkGray",
"thinkingMinimal": "#6e6e6e",
"thinkingLow": "#5f87af",
"thinkingMedium": "#81a2be",
"thinkingHigh": "#b294bb",
"thinkingXhigh": "#d183e8",
"thinkingMax": "#ff5fff",
"bashMode": "green"
},
"export": {
"pageBg": "#18181e",
"cardBg": "#1e1e24",
"infoBg": "#3c3728"
}
}
@@ -0,0 +1,86 @@
{
"$schema": "https://raw.githubusercontent.com/earendil-works/pi/main/packages/coding-agent/src/modes/interactive/theme/theme-schema.json",
"name": "light",
"vars": {
"teal": "#5a8080",
"blue": "#547da7",
"green": "#588458",
"red": "#aa5555",
"yellow": "#9a7326",
"text": "#1f2328",
"mediumGray": "#6c6c6c",
"dimGray": "#767676",
"lightGray": "#b0b0b0",
"selectedBg": "#d0d0e0",
"userMsgBg": "#e8e8e8",
"toolPendingBg": "#e8e8f0",
"toolSuccessBg": "#e8f0e8",
"toolErrorBg": "#f0e8e8",
"customMsgBg": "#ede7f6"
},
"colors": {
"accent": "teal",
"border": "blue",
"borderAccent": "teal",
"borderMuted": "lightGray",
"success": "green",
"error": "red",
"warning": "yellow",
"muted": "mediumGray",
"dim": "dimGray",
"text": "text",
"thinkingText": "mediumGray",
"selectedBg": "selectedBg",
"userMessageBg": "userMsgBg",
"userMessageText": "text",
"customMessageBg": "customMsgBg",
"customMessageText": "text",
"customMessageLabel": "#7e57c2",
"toolPendingBg": "toolPendingBg",
"toolSuccessBg": "toolSuccessBg",
"toolErrorBg": "toolErrorBg",
"toolTitle": "text",
"toolOutput": "mediumGray",
"mdHeading": "yellow",
"mdLink": "blue",
"mdLinkUrl": "dimGray",
"mdCode": "teal",
"mdCodeBlock": "green",
"mdCodeBlockBorder": "mediumGray",
"mdQuote": "mediumGray",
"mdQuoteBorder": "mediumGray",
"mdHr": "mediumGray",
"mdListBullet": "green",
"toolDiffAdded": "green",
"toolDiffRemoved": "red",
"toolDiffContext": "mediumGray",
"syntaxComment": "#008000",
"syntaxKeyword": "#0000FF",
"syntaxFunction": "#795E26",
"syntaxVariable": "#001080",
"syntaxString": "#A31515",
"syntaxNumber": "#098658",
"syntaxType": "#267F99",
"syntaxOperator": "#000000",
"syntaxPunctuation": "#000000",
"thinkingOff": "lightGray",
"thinkingMinimal": "#767676",
"thinkingLow": "blue",
"thinkingMedium": "teal",
"thinkingHigh": "#875f87",
"thinkingXhigh": "#8b008b",
"thinkingMax": "#af005f",
"bashMode": "green"
},
"export": {
"pageBg": "#f8f8f8",
"cardBg": "#ffffff",
"infoBg": "#fffae6"
}
}
@@ -0,0 +1,126 @@
import type { TUI } from "@earendil-works/pi-tui";
import type { SettingsManager } from "../../../core/settings-manager.ts";
import {
detectTerminalBackgroundFromEnv,
detectTerminalBackgroundTheme,
detectTerminalThemeForAuto,
initTheme,
parseAutoThemeSetting,
resolveThemeSetting,
setTheme,
setThemeInstance,
type TerminalTheme,
type Theme,
} from "./theme.ts";
type ThemeResult = { success: boolean; error?: string };
export class InteractiveThemeController {
private readonly ui: TUI;
private readonly settingsManager: SettingsManager;
private readonly showError: (message: string) => void;
private readonly onChanged: () => void;
private terminalTheme: TerminalTheme = detectTerminalBackgroundFromEnv().theme;
private activeThemeName: string | undefined;
private autoSyncEnabled = false;
constructor(ui: TUI, settingsManager: SettingsManager, showError: (message: string) => void, onChanged: () => void) {
this.ui = ui;
this.settingsManager = settingsManager;
this.showError = showError;
this.onChanged = onChanged;
this.activeThemeName = resolveThemeSetting(this.settingsManager.getThemeSetting(), this.terminalTheme);
initTheme(this.activeThemeName, true);
this.ui.onTerminalColorSchemeChange((terminalTheme) => this.applyTerminalTheme(terminalTheme));
}
async applyFromSettings(): Promise<void> {
const themeSetting = this.settingsManager.getThemeSetting();
const autoTheme = parseAutoThemeSetting(themeSetting);
if (autoTheme) {
this.terminalTheme = await detectTerminalThemeForAuto({ ui: this.ui, timeoutMs: 100 });
this.setAutoSync(true);
this.applyThemeName(this.terminalTheme === "light" ? autoTheme.lightTheme : autoTheme.darkTheme, true);
return;
}
this.setAutoSync(false);
if (themeSetting !== undefined) {
this.applyThemeName(themeSetting, true);
return;
}
const detection = await detectTerminalBackgroundTheme({ ui: this.ui, timeoutMs: 100 });
this.terminalTheme = detection.theme;
if (!this.applyThemeName(detection.theme).success) return;
if (detection.confidence === "high") {
this.settingsManager.setTheme(detection.theme);
await this.settingsManager.flush();
}
}
setThemeName(themeName: string, showError = false): ThemeResult {
this.setAutoSync(false);
return this.applyThemeName(themeName, showError);
}
setThemeInstance(themeInstance: Theme): ThemeResult {
this.setAutoSync(false);
setThemeInstance(themeInstance);
this.activeThemeName = "<in-memory>";
this.notifyChanged();
return { success: true };
}
preview(themeSettingOrName: string): void {
const themeName = resolveThemeSetting(themeSettingOrName, this.terminalTheme) ?? this.activeThemeName;
if (!themeName) return;
if (setTheme(themeName, true).success) {
this.ui.invalidate();
this.ui.requestRender();
}
}
disableAutoSync(): void {
this.setAutoSync(false);
}
getTerminalTheme(): TerminalTheme {
return this.terminalTheme;
}
private applyThemeName(themeName: string, showError = false): ThemeResult {
const result = setTheme(themeName, true);
this.activeThemeName = result.success ? themeName : "dark";
this.notifyChanged();
if (!result.success && showError) {
this.showError(`Failed to load theme "${themeName}": ${result.error}\nFell back to dark theme.`);
}
return result;
}
private notifyChanged(): void {
this.ui.invalidate();
this.onChanged();
}
private setAutoSync(enabled: boolean): void {
if (this.autoSyncEnabled === enabled) return;
this.autoSyncEnabled = enabled;
this.ui.setTerminalColorSchemeNotifications(enabled);
}
private applyTerminalTheme(terminalTheme: TerminalTheme): void {
if (!this.autoSyncEnabled) return;
this.terminalTheme = terminalTheme;
const autoTheme = parseAutoThemeSetting(this.settingsManager.getThemeSetting());
if (!autoTheme) {
this.setAutoSync(false);
return;
}
const themeName = terminalTheme === "light" ? autoTheme.lightTheme : autoTheme.darkTheme;
if (themeName !== this.activeThemeName) {
this.applyThemeName(themeName);
}
}
}
@@ -0,0 +1,340 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Pi Coding Agent Theme",
"description": "Theme schema for Pi coding agent",
"type": "object",
"required": ["name", "colors"],
"properties": {
"$schema": {
"type": "string",
"description": "JSON schema reference"
},
"name": {
"type": "string",
"pattern": "^[^/]+$",
"description": "Theme name. Must not contain '/' because it is reserved for automatic light/dark theme settings."
},
"vars": {
"type": "object",
"description": "Reusable color variables",
"additionalProperties": {
"oneOf": [
{
"type": "string",
"description": "Hex color (#RRGGBB), variable reference, or empty string for terminal default"
},
{
"type": "integer",
"minimum": 0,
"maximum": 255,
"description": "256-color palette index (0-255)"
}
]
}
},
"colors": {
"type": "object",
"description": "Theme color definitions (thinkingMax is optional and falls back to thinkingXhigh)",
"required": [
"accent",
"border",
"borderAccent",
"borderMuted",
"success",
"error",
"warning",
"muted",
"dim",
"text",
"thinkingText",
"selectedBg",
"userMessageBg",
"userMessageText",
"customMessageBg",
"customMessageText",
"customMessageLabel",
"toolPendingBg",
"toolSuccessBg",
"toolErrorBg",
"toolTitle",
"toolOutput",
"mdHeading",
"mdLink",
"mdLinkUrl",
"mdCode",
"mdCodeBlock",
"mdCodeBlockBorder",
"mdQuote",
"mdQuoteBorder",
"mdHr",
"mdListBullet",
"toolDiffAdded",
"toolDiffRemoved",
"toolDiffContext",
"syntaxComment",
"syntaxKeyword",
"syntaxFunction",
"syntaxVariable",
"syntaxString",
"syntaxNumber",
"syntaxType",
"syntaxOperator",
"syntaxPunctuation",
"thinkingOff",
"thinkingMinimal",
"thinkingLow",
"thinkingMedium",
"thinkingHigh",
"thinkingXhigh",
"bashMode"
],
"properties": {
"accent": {
"$ref": "#/$defs/colorValue",
"description": "Primary accent color (logo, selected items, cursor)"
},
"border": {
"$ref": "#/$defs/colorValue",
"description": "Normal borders"
},
"borderAccent": {
"$ref": "#/$defs/colorValue",
"description": "Highlighted borders"
},
"borderMuted": {
"$ref": "#/$defs/colorValue",
"description": "Subtle borders"
},
"success": {
"$ref": "#/$defs/colorValue",
"description": "Success states"
},
"error": {
"$ref": "#/$defs/colorValue",
"description": "Error states"
},
"warning": {
"$ref": "#/$defs/colorValue",
"description": "Warning states"
},
"muted": {
"$ref": "#/$defs/colorValue",
"description": "Secondary/dimmed text"
},
"dim": {
"$ref": "#/$defs/colorValue",
"description": "Very dimmed text (more subtle than muted)"
},
"text": {
"$ref": "#/$defs/colorValue",
"description": "Default text color (usually empty string)"
},
"thinkingText": {
"$ref": "#/$defs/colorValue",
"description": "Thinking block text color"
},
"selectedBg": {
"$ref": "#/$defs/colorValue",
"description": "Selected item background"
},
"userMessageBg": {
"$ref": "#/$defs/colorValue",
"description": "User message background"
},
"userMessageText": {
"$ref": "#/$defs/colorValue",
"description": "User message text color"
},
"customMessageBg": {
"$ref": "#/$defs/colorValue",
"description": "Custom message background (hook-injected messages)"
},
"customMessageText": {
"$ref": "#/$defs/colorValue",
"description": "Custom message text color"
},
"customMessageLabel": {
"$ref": "#/$defs/colorValue",
"description": "Custom message type label color"
},
"toolPendingBg": {
"$ref": "#/$defs/colorValue",
"description": "Tool execution box (pending state)"
},
"toolSuccessBg": {
"$ref": "#/$defs/colorValue",
"description": "Tool execution box (success state)"
},
"toolErrorBg": {
"$ref": "#/$defs/colorValue",
"description": "Tool execution box (error state)"
},
"toolTitle": {
"$ref": "#/$defs/colorValue",
"description": "Tool execution box title color"
},
"toolOutput": {
"$ref": "#/$defs/colorValue",
"description": "Tool execution box output text color"
},
"mdHeading": {
"$ref": "#/$defs/colorValue",
"description": "Markdown heading text"
},
"mdLink": {
"$ref": "#/$defs/colorValue",
"description": "Markdown link text"
},
"mdLinkUrl": {
"$ref": "#/$defs/colorValue",
"description": "Markdown link URL"
},
"mdCode": {
"$ref": "#/$defs/colorValue",
"description": "Markdown inline code"
},
"mdCodeBlock": {
"$ref": "#/$defs/colorValue",
"description": "Markdown code block content"
},
"mdCodeBlockBorder": {
"$ref": "#/$defs/colorValue",
"description": "Markdown code block fences"
},
"mdQuote": {
"$ref": "#/$defs/colorValue",
"description": "Markdown blockquote text"
},
"mdQuoteBorder": {
"$ref": "#/$defs/colorValue",
"description": "Markdown blockquote border"
},
"mdHr": {
"$ref": "#/$defs/colorValue",
"description": "Markdown horizontal rule"
},
"mdListBullet": {
"$ref": "#/$defs/colorValue",
"description": "Markdown list bullets/numbers"
},
"toolDiffAdded": {
"$ref": "#/$defs/colorValue",
"description": "Added lines in tool diffs"
},
"toolDiffRemoved": {
"$ref": "#/$defs/colorValue",
"description": "Removed lines in tool diffs"
},
"toolDiffContext": {
"$ref": "#/$defs/colorValue",
"description": "Context lines in tool diffs"
},
"syntaxComment": {
"$ref": "#/$defs/colorValue",
"description": "Syntax highlighting: comments"
},
"syntaxKeyword": {
"$ref": "#/$defs/colorValue",
"description": "Syntax highlighting: keywords"
},
"syntaxFunction": {
"$ref": "#/$defs/colorValue",
"description": "Syntax highlighting: function names"
},
"syntaxVariable": {
"$ref": "#/$defs/colorValue",
"description": "Syntax highlighting: variable names"
},
"syntaxString": {
"$ref": "#/$defs/colorValue",
"description": "Syntax highlighting: string literals"
},
"syntaxNumber": {
"$ref": "#/$defs/colorValue",
"description": "Syntax highlighting: number literals"
},
"syntaxType": {
"$ref": "#/$defs/colorValue",
"description": "Syntax highlighting: type names"
},
"syntaxOperator": {
"$ref": "#/$defs/colorValue",
"description": "Syntax highlighting: operators"
},
"syntaxPunctuation": {
"$ref": "#/$defs/colorValue",
"description": "Syntax highlighting: punctuation"
},
"thinkingOff": {
"$ref": "#/$defs/colorValue",
"description": "Thinking level border: off"
},
"thinkingMinimal": {
"$ref": "#/$defs/colorValue",
"description": "Thinking level border: minimal"
},
"thinkingLow": {
"$ref": "#/$defs/colorValue",
"description": "Thinking level border: low"
},
"thinkingMedium": {
"$ref": "#/$defs/colorValue",
"description": "Thinking level border: medium"
},
"thinkingHigh": {
"$ref": "#/$defs/colorValue",
"description": "Thinking level border: high"
},
"thinkingXhigh": {
"$ref": "#/$defs/colorValue",
"description": "Thinking level border: xhigh"
},
"thinkingMax": {
"$ref": "#/$defs/colorValue",
"description": "Thinking level border: max (falls back to thinkingXhigh when omitted)"
},
"bashMode": {
"$ref": "#/$defs/colorValue",
"description": "Editor border color in bash mode"
}
},
"additionalProperties": false
},
"export": {
"type": "object",
"description": "Optional colors for HTML export (defaults derived from userMessageBg if not specified)",
"properties": {
"pageBg": {
"$ref": "#/$defs/colorValue",
"description": "Page background color"
},
"cardBg": {
"$ref": "#/$defs/colorValue",
"description": "Card/container background color"
},
"infoBg": {
"$ref": "#/$defs/colorValue",
"description": "Info sections background (system prompt, notices)"
}
},
"additionalProperties": false
}
},
"additionalProperties": false,
"$defs": {
"colorValue": {
"oneOf": [
{
"type": "string",
"description": "Hex color (#RRGGBB), variable reference, or empty string for terminal default"
},
{
"type": "integer",
"minimum": 0,
"maximum": 255,
"description": "256-color palette index (0-255)"
}
]
}
}
}
File diff suppressed because it is too large Load Diff