Release v0.11.5

This commit is contained in:
Mario Zechner 2025-12-01 20:22:14 +01:00
parent cdc64c7569
commit 7a1884f85c
14 changed files with 341 additions and 40 deletions

View file

@ -11,6 +11,7 @@ import { exportFromFile } from "./export-html.js";
import { findModel, getApiKeyForModel, getAvailableModels } from "./model-config.js";
import { SessionManager } from "./session-manager.js";
import { SettingsManager } from "./settings-manager.js";
import { expandSlashCommand, loadSlashCommands } from "./slash-commands.js";
import { initTheme } from "./theme/theme.js";
import { allTools, codingTools, type ToolName } from "./tools/index.js";
import { ensureTool } from "./tools-manager.js";
@ -732,10 +733,13 @@ async function runInteractiveMode(
renderer.showWarning(modelFallbackMessage);
}
// Load file-based slash commands for expansion
const fileCommands = loadSlashCommands();
// Process initial message with attachments if provided (from @file args)
if (initialMessage) {
try {
await agent.prompt(initialMessage, initialAttachments);
await agent.prompt(expandSlashCommand(initialMessage, fileCommands), initialAttachments);
} catch (error: unknown) {
const errorMessage = error instanceof Error ? error.message : "Unknown error occurred";
renderer.showError(errorMessage);
@ -745,7 +749,7 @@ async function runInteractiveMode(
// Process remaining initial messages if provided (from CLI args)
for (const message of initialMessages) {
try {
await agent.prompt(message);
await agent.prompt(expandSlashCommand(message, fileCommands));
} catch (error: unknown) {
const errorMessage = error instanceof Error ? error.message : "Unknown error occurred";
renderer.showError(errorMessage);
@ -775,6 +779,9 @@ async function runSingleShotMode(
initialMessage?: string,
initialAttachments?: Attachment[],
): Promise<void> {
// Load file-based slash commands for expansion
const fileCommands = loadSlashCommands();
if (mode === "json") {
// Subscribe to all events and output as JSON
agent.subscribe((event) => {
@ -785,12 +792,12 @@ async function runSingleShotMode(
// Send initial message with attachments if provided
if (initialMessage) {
await agent.prompt(initialMessage, initialAttachments);
await agent.prompt(expandSlashCommand(initialMessage, fileCommands), initialAttachments);
}
// Send remaining messages
for (const message of messages) {
await agent.prompt(message);
await agent.prompt(expandSlashCommand(message, fileCommands));
}
// In text mode, only output the final assistant message

View file

@ -0,0 +1,206 @@
import { existsSync, readdirSync, readFileSync } from "fs";
import { homedir } from "os";
import { join, resolve } from "path";
/**
* Represents a custom slash command loaded from a file
*/
export interface FileSlashCommand {
name: string;
description: string;
content: string;
source: string; // e.g., "(user)", "(project)", "(project:frontend)"
}
/**
* Parse YAML frontmatter from markdown content
* Returns { frontmatter, content } where content has frontmatter stripped
*/
function parseFrontmatter(content: string): { frontmatter: Record<string, string>; content: string } {
const frontmatter: Record<string, string> = {};
if (!content.startsWith("---")) {
return { frontmatter, content };
}
const endIndex = content.indexOf("\n---", 3);
if (endIndex === -1) {
return { frontmatter, content };
}
const frontmatterBlock = content.slice(4, endIndex);
const remainingContent = content.slice(endIndex + 4).trim();
// Simple YAML parsing - just key: value pairs
for (const line of frontmatterBlock.split("\n")) {
const match = line.match(/^(\w+):\s*(.*)$/);
if (match) {
frontmatter[match[1]] = match[2].trim();
}
}
return { frontmatter, content: remainingContent };
}
/**
* Parse command arguments respecting quoted strings (bash-style)
* Returns array of arguments
*/
export function parseCommandArgs(argsString: string): string[] {
const args: string[] = [];
let current = "";
let inQuote: string | null = null;
for (let i = 0; i < argsString.length; i++) {
const char = argsString[i];
if (inQuote) {
if (char === inQuote) {
inQuote = null;
} else {
current += char;
}
} else if (char === '"' || char === "'") {
inQuote = char;
} else if (char === " " || char === "\t") {
if (current) {
args.push(current);
current = "";
}
} else {
current += char;
}
}
if (current) {
args.push(current);
}
return args;
}
/**
* Substitute argument placeholders in command content
* Supports $1, $2, ... for positional args and $@ for all args
*/
export function substituteArgs(content: string, args: string[]): string {
let result = content;
// Replace $@ with all args joined
result = result.replace(/\$@/g, args.join(" "));
// Replace $1, $2, etc. with positional args
result = result.replace(/\$(\d+)/g, (_, num) => {
const index = parseInt(num, 10) - 1;
return args[index] ?? "";
});
return result;
}
/**
* Recursively scan a directory for .md files and load them as slash commands
*/
function loadCommandsFromDir(dir: string, source: "user" | "project", subdir: string = ""): FileSlashCommand[] {
const commands: FileSlashCommand[] = [];
if (!existsSync(dir)) {
return commands;
}
try {
const entries = readdirSync(dir, { withFileTypes: true });
for (const entry of entries) {
const fullPath = join(dir, entry.name);
if (entry.isDirectory()) {
// Recurse into subdirectory
const newSubdir = subdir ? `${subdir}:${entry.name}` : entry.name;
commands.push(...loadCommandsFromDir(fullPath, source, newSubdir));
} else if (entry.isFile() && entry.name.endsWith(".md")) {
try {
const rawContent = readFileSync(fullPath, "utf-8");
const { frontmatter, content } = parseFrontmatter(rawContent);
const name = entry.name.slice(0, -3); // Remove .md extension
// Build source string
let sourceStr: string;
if (source === "user") {
sourceStr = subdir ? `(user:${subdir})` : "(user)";
} else {
sourceStr = subdir ? `(project:${subdir})` : "(project)";
}
// Get description from frontmatter or first non-empty line
let description = frontmatter.description || "";
if (!description) {
const firstLine = content.split("\n").find((line) => line.trim());
if (firstLine) {
// Truncate if too long
description = firstLine.slice(0, 60);
if (firstLine.length > 60) description += "...";
}
}
// Append source to description
description = description ? `${description} ${sourceStr}` : sourceStr;
commands.push({
name,
description,
content,
source: sourceStr,
});
} catch (error) {
// Silently skip files that can't be read
}
}
}
} catch (error) {
// Silently skip directories that can't be read
}
return commands;
}
/**
* Load all custom slash commands from:
* 1. Global: ~/.pi/agent/commands/
* 2. Project: ./.pi/commands/
*/
export function loadSlashCommands(): FileSlashCommand[] {
const commands: FileSlashCommand[] = [];
// 1. Load global commands from ~/.pi/agent/commands/
const homeDir = homedir();
const globalCommandsDir = resolve(process.env.PI_CODING_AGENT_DIR || join(homeDir, ".pi/agent/"), "commands");
commands.push(...loadCommandsFromDir(globalCommandsDir, "user"));
// 2. Load project commands from ./.pi/commands/
const projectCommandsDir = resolve(process.cwd(), ".pi/commands");
commands.push(...loadCommandsFromDir(projectCommandsDir, "project"));
return commands;
}
/**
* Expand a slash command if it matches a file-based command.
* Returns the expanded content or the original text if not a slash command.
*/
export function expandSlashCommand(text: string, fileCommands: FileSlashCommand[]): string {
if (!text.startsWith("/")) return text;
const spaceIndex = text.indexOf(" ");
const commandName = spaceIndex === -1 ? text.slice(1) : text.slice(1, spaceIndex);
const argsString = spaceIndex === -1 ? "" : text.slice(spaceIndex + 1);
const fileCommand = fileCommands.find((cmd) => cmd.name === commandName);
if (fileCommand) {
const args = parseCommandArgs(argsString);
return substituteArgs(fileCommand.content, args);
}
return text;
}

View file

@ -21,6 +21,7 @@ import { getApiKeyForModel, getAvailableModels } from "../model-config.js";
import { listOAuthProviders, login, logout } from "../oauth/index.js";
import type { SessionManager } from "../session-manager.js";
import type { SettingsManager } from "../settings-manager.js";
import { expandSlashCommand, type FileSlashCommand, loadSlashCommands } from "../slash-commands.js";
import { getEditorTheme, getMarkdownTheme, onThemeChange, setTheme, theme } from "../theme/theme.js";
import { AssistantMessageComponent } from "./assistant-message.js";
import { CustomEditor } from "./custom-editor.js";
@ -97,6 +98,9 @@ export class TuiRenderer {
// Agent subscription unsubscribe function
private unsubscribe?: () => void;
// File-based slash commands
private fileCommands: FileSlashCommand[] = [];
constructor(
agent: Agent,
sessionManager: SessionManager,
@ -179,6 +183,15 @@ export class TuiRenderer {
description: "Clear context and start a fresh session",
};
// Load file-based slash commands
this.fileCommands = loadSlashCommands();
// Convert file commands to SlashCommand format
const fileSlashCommands: SlashCommand[] = this.fileCommands.map((cmd) => ({
name: cmd.name,
description: cmd.description,
}));
// Setup autocomplete for file paths and slash commands
const autocompleteProvider = new CombinedAutocompleteProvider(
[
@ -193,6 +206,7 @@ export class TuiRenderer {
logoutCommand,
queueCommand,
clearCommand,
...fileSlashCommands,
],
process.cwd(),
fdPath,
@ -401,6 +415,9 @@ export class TuiRenderer {
return;
}
// Check for file-based slash commands
text = expandSlashCommand(text, this.fileCommands);
// Normal message submission - validate model and API key first
const currentModel = this.agent.state.model;
if (!currentModel) {