Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
209 changes: 207 additions & 2 deletions src/tui/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,23 @@ export default function App({
hourCalls: getGcCalls().length,
})
: null,
_skillList: skillList,
_executeSkill: (skillName, _args) => {
const skill = registry.get(skillName);
if (!skill) {
return { action: "skill", subAction: "error", message: `Skill "${skillName}" not found.` };
}
// Skills are prompt-based instructions for the agent to interpret and execute.
// Load the SKILL.md and pass it to the conversation so the agent can use it.
const body = registry.getSkillBody(skillName);
return {
action: "skill",
subAction: "load",
name: skillName,
skillBody: body || "",
message: body ? `Skill "${skillName}" loaded.\n${body}` : `Skill "${skillName}" loaded. No instructions found.`,
};
},
});
if (result.action === "quit") {
handleQuit();
Expand All @@ -166,8 +183,196 @@ export default function App({
setStatusMessage(result.message);
return;
}
setStatusMessage(result.message || result.action + " executed");
if (result.message && result.action !== "provider" && result.action !== "schedule") {
if (result.action === "skill" && result.subAction === "load" && result.skillBody) {
gcManager?.();
setStatusMessage("Streaming...");

const assistantTime = getTimestamp();
setMessages((prev) => [
...prev,
{
role: "assistant",
content: "",
time: assistantTime,
streaming: true,
toolCalls: [],
toolCallDisplay: "",
},
]);

let committedContent = "";
let committedReasoning = "";
let lastToolCallDisplay = "";
let todoStatusLines = "";

setTodoStreamingCallback((event) => {
if (event.type === "todo_status") {
const statusLine = event.message
? `- ${event.message}`
: `- Todo: ${event.action} ${event.key || ""}`;
todoStatusLines = (todoStatusLines ? todoStatusLines + "\n" : "") + statusLine;
setMessages((prev) => {
const cloned = [...prev];
const last = cloned[cloned.length - 1];
if (last.role === "assistant" && last.streaming) {
last.toolCallDisplay = lastToolCallDisplay
? lastToolCallDisplay + "\n" + todoStatusLines
: todoStatusLines;
}
return cloned;
});
}
});

try {
await dispatchProvider(
result.skillBody,
sessionState ? sessionState.getProvider() : null,
(event) => {
if (isQuittingRef.current) return;
try {
if (event.type === "text") {
committedContent = (committedContent || "") + event.text;
setMessages((prev) => {
const cloned = [...prev];
const last = cloned[cloned.length - 1];
if (last.role === "assistant" && last.streaming) {
last.content = committedContent + "\u2588";
}
return cloned;
});
} else if (event.type === "reasoning") {
committedReasoning = (committedReasoning || "") + event.text;
setMessages((prev) => {
const cloned = [...prev];
const last = cloned[cloned.length - 1];
if (last.role === "assistant" && last.streaming) {
last.reasoningContent = (committedReasoning || "") + "\u2588";
}
return cloned;
});
} else if (event.type === "tool_start") {
setMessages((prev) => {
const cloned = [...prev];
const last = cloned[cloned.length - 1];
if (last.role === "assistant" && last.streaming) {
last.activeToolCall = { name: event.toolName };
last.toolCallDisplay = lastToolCallDisplay;
}
return cloned;
});
} else if (event.type === "tool_end") {
const resultLine = event.data
? ` Result: ${JSON.stringify(event.data).slice(0, 200)}`
: "";
const displayLine = event.toolName
? `- Tool: ${event.toolName}${resultLine}`
: `- Tool: ${event.toolCallId || "unknown"}${resultLine}`;
lastToolCallDisplay =
(lastToolCallDisplay ? lastToolCallDisplay + "\n" : "") + displayLine;
setMessages((prev) => {
const cloned = [...prev];
const last = cloned[cloned.length - 1];
if (last.role === "assistant" && last.streaming) {
last.activeToolCall = null;
last.toolCallDisplay = lastToolCallDisplay;
}
return cloned;
});
} else if (event.type === "tool_error") {
const errorLine = event.toolName
? `- Tool: ${event.toolName} (error: ${event.error})`
: `- Tool call failed (${event.toolCallId || "unknown"})`;
lastToolCallDisplay =
(lastToolCallDisplay ? lastToolCallDisplay + "\n" : "") + errorLine;
setMessages((prev) => {
const cloned = [...prev];
const last = cloned[cloned.length - 1];
if (last.role === "assistant" && last.streaming) {
last.activeToolCall = null;
last.toolCallDisplay = lastToolCallDisplay;
}
return cloned;
});
} else if (event.type === "compaction_start") {
setStatusMessage("Compacting context...");
setMessages((prev) => {
const cloned = [...prev];
const last = cloned[cloned.length - 1];
if (last.role === "assistant" && last.streaming) {
last.activeToolCall = null;
last.toolCallDisplay = lastToolCallDisplay
? lastToolCallDisplay + "\nCompacting context..."
: "Compacting context...";
}
return cloned;
});
} else if (event.type === "compaction_end") {
setStatusMessage("Streaming...");
setMessages((prev) => {
const cloned = [...prev];
const last = cloned[cloned.length - 1];
if (last.role === "assistant" && last.streaming) {
last.activeToolCall = null;
last.toolCallDisplay = lastToolCallDisplay;
}
return cloned;
});
}
} catch (_cbErr) {
// Silently ignore streaming callback errors
}
},
);

const responseContent = committedContent;

if (isQuittingRef.current) return;

setMessages((prev) => {
const cloned = [...prev];
const last = cloned[cloned.length - 1];
if (last.role === "assistant" && last.streaming) {
last.content = responseContent;
last.reasoningContent = committedReasoning || undefined;
last.streaming = false;
last.activeToolCall = null;
if (lastToolCallDisplay) {
last.toolCallDisplay = lastToolCallDisplay;
}
if (todoStatusLines) {
last.toolCallDisplay = last.toolCallDisplay
? last.toolCallDisplay + "\n" + todoStatusLines
: todoStatusLines;
}
}
return cloned;
});

// Persist assistant response to session state
if (sessionState) {
sessionState.addExchange({
role: "assistant",
content: responseContent,
});
}

setStatusMessage("Done");
} catch (err) {
setMessages((prev) => {
const cloned = [...prev];
const last = cloned[cloned.length - 1];
if (last.role === "assistant" && last.streaming) {
last.streaming = false;
}
return cloned;
});
setStatusMessage(`Error: ${err.message}`);
}
} else if (result.action !== "help" && result.action !== "skill") {
setStatusMessage(result.message || result.action + " executed");
}
if (result.message && result.action !== "provider" && result.action !== "schedule" && result.action !== "skill") {
addMessage({ role: "system", content: result.message });
}
} catch (err) {
Expand Down
12 changes: 6 additions & 6 deletions src/tui/banner.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ const COMMAND_GROUPS = [
{
group: "Command:",
items: [
":help - show this list",
":provider [set <name>] - list or switch provider",
":schedule [list|pause|resume|run-now]",
":config set <path> <value> - update config",
":clear - clear conversation",
":quit - exit the app",
"/help - show this list",
"/provider [set <name>] - list or switch provider",
"/schedule [list|pause|resume|run-now]",
"/config set <path> <value> - update config",
"/clear - clear conversation",
"/quit - exit the app",
],
},
];
Expand Down
58 changes: 42 additions & 16 deletions src/tui/commandParser.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Command parser that handles `:command` syntax with a dispatch table.
* Supports commands like: `:config set`, `:provider set`, `:schedule list`,
* `:clear`, `:quit`, `:gc`, etc.
* Command parser that handles `/command` syntax with a dispatch table.
* Supports commands like: `/config set`, `/provider set`, `/schedule list`,
* `/clear`, `/quit`, `/gc`, etc.
*/
export class CommandParser {
#dispatch = new Map();
Expand Down Expand Up @@ -54,7 +54,7 @@ export class CommandParser {
};
}
}
return { action: "config", message: "Usage: :config set <path> <value>" };
return { action: "config", message: "Usage: /config set <path> <value>" };
});

this.#register("schedule", (args, ctx) => {
Expand Down Expand Up @@ -91,10 +91,14 @@ export class CommandParser {
});

this.#register("help", (_args, _ctx) => {
const cmds = Array.from(this.#dispatch.keys());
const cmds = Array.from(this.#dispatch.keys()).filter((k) => !k.startsWith("_"));
let message = `Available commands: /${cmds.join(", /")}`;
if (_ctx?._skillList && _ctx._skillList.length > 0) {
message += `\nSkills: /${_ctx._skillList.join(", /")} (execute with /skillName [args])`;
}
return {
action: "help",
message: `Available commands: ${cmds.join(", ")}`,
message,
};
});

Expand Down Expand Up @@ -123,6 +127,13 @@ export class CommandParser {
: `GC ${result.reason || "skipped"}`;
return { action: "gc", subAction: "run", ...result, message: msg };
});

// Skill execution: /skillName [args]
// Registered last so it catches unmatched commands and checks the registry
this.#register("_skillFallback", (_args, _ctx) => {
// This handler is never called — skill execution is handled in parse()
return { action: "skill", subAction: "error", message: "Skill not found" };
});
}

#register(name, handler) {
Expand All @@ -131,45 +142,60 @@ export class CommandParser {

/**
* Parse a raw input string and return a command result.
* @param {string} input - The raw input (e.g., ":config set telemetry.enabled true")
* Checks registered commands first, then falls back to skill registry.
* @param {string} input - The raw input (e.g., "/config set telemetry.enabled true")
* @param {Object} context - The execution context with module references
* @returns {Object|null} Parsed command result
*/
parse(input, context) {
if (!input || typeof input !== "string") return null;
const trimmed = input.trim();
if (!trimmed.startsWith(":")) return null;
if (!trimmed.startsWith("/")) return null;

const parts = trimmed.slice(1).trim().split(/\s+/);
const commandName = parts[0];
const args = parts.slice(1);

// 1. Check registered commands first
const handler = this.#dispatch.get(commandName);
if (!handler) {
if (handler) {
return handler(args, context);
}

// 2. Fall back to skill execution
if (context?._skillList && context._skillList.includes(commandName)) {
if (context._executeSkill) {
return context._executeSkill(commandName, args);
}
return {
action: "unknown",
message: `Unknown command: :${commandName}. Type :help for available commands.`,
action: "skill",
subAction: "error",
message: `Skill "${commandName}" not available in this context.`,
};
}

return handler(args, context);
// 3. Unknown command
return {
action: "unknown",
message: `Unknown command: /${commandName}. Type /help for available commands.`,
};
}

/**
* Check if an input is a command (starts with ":".)
* Check if an input is a command (starts with "/".)
* @param {string} input
* @returns {boolean}
*/
isCommand(input) {
return input && typeof input === "string" && input.trim().startsWith(":");
return input && typeof input === "string" && input.trim().startsWith("/");
}

/**
* Get a list of all registered commands.
* Get a list of all registered commands (excludes internal/fallback commands).
* @returns {string[]}
*/
listCommands() {
return Array.from(this.#dispatch.keys());
return Array.from(this.#dispatch.keys()).filter((k) => !k.startsWith("_"));
}

/**
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/cron_sync.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ describe("scheduler - Cron.sync", () => {
before(async () => {
await setupTestDir();
savedCrontab = saveRealCrontab();
// Clear any leftover madz entries for a clean slate
Cron.uninstall();
});

after(async () => {
Expand Down
Loading