Skip to content

JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.

Python

fix(tui): Add /<skill> <query> as an alternative for /skills use

dreamofapsychiccat12cb9c115601
diff --git a/jiuwenclaw/channels/tui/frontend/src/core/commands/CommandService.ts b/jiuwenclaw/channels/tui/frontend/src/core/commands/CommandService.ts
index ca6e6ded07..bb2d500106 100644
--- a/jiuwenclaw/channels/tui/frontend/src/core/commands/CommandService.ts
+++ b/jiuwenclaw/channels/tui/frontend/src/core/commands/CommandService.ts
@@ -1,5 +1,5 @@
import type { CommandContext, CommandSuggestion, SlashCommand } from "./types.js";
-import { makeItem, parseArgs } from "./helpers.js";
+import { flattenArrayPayload, makeItem, parseArgs } from "./helpers.js";
export function parseSlashCommand(raw: string, commands: readonly SlashCommand[]) {
const trimmed = raw.trim();
@@ -46,10 +46,23 @@ export function parseSlashCommand(raw: string, commands: readonly SlashCommand[]
};
}
+export interface InstalledSkillEntry {
+ name: string;
+ description: string;
+}
+
export class CommandService {
private commands = new Map<string, SlashCommand>();
private aliases = new Map<string, string>();
private topLevelCommands: SlashCommand[] = [];
+ private installedSkills: InstalledSkillEntry[] = [];
+
+ /**
+ * Optional callback invoked whenever the installed-skills cache is successfully
+ * refreshed. The UI layer registers this to rebuild its autocomplete provider
+ * so that `/<skillName>` shorthands appear in the command-name dropdown.
+ */
+ onInstalledSkillsChange?: (skills: readonly InstalledSkillEntry[]) => void;
register(commands: readonly SlashCommand[]): void {
this.topLevelCommands = [...commands];
@@ -80,10 +93,70 @@ export class CommandService {
.sort((a, b) => a.name.localeCompare(b.name));
}
+ /**
+ * Fetches the current installed-skill list from the backend via `ctx` and
+ * stores it in `this.installedSkills`. Called on every `execute()` so that
+ * the cache stays fresh without any extra wiring. This function is also
+ * called by the first WebSocket connection. (From app-screen.ts)
+ */
+ async refreshSkills(
+ ctx: CommandContext,
+ ): Promise<void> {
+ try {
+ const payload = await ctx.request("skills.list", {});
+ const skills = flattenArrayPayload(payload);
+ this.installedSkills = skills.flatMap((item) => {
+ if (item && typeof item === "object") {
+ const obj = item as Record<string, unknown>;
+ if (obj.installed === true && typeof obj.name === "string") {
+ return [{
+ name: obj.name as string,
+ description: typeof obj.description === "string" ? obj.description : "",
+ }];
+ }
+ }
+ return [];
+ });
+ // Notify the UI so it can rebuild the autocomplete provider with the
+ // fresh `/<skillName>` shorthands.
+ this.onInstalledSkillsChange?.(this.installedSkills);
+ } catch {
+ // Keep the previous cache if the RPC fails.
+ }
+ }
+
+ getInstalledSkills(): readonly InstalledSkillEntry[] {
+ return this.installedSkills;
+ }
+
async execute(raw: string, ctx: CommandContext): Promise<void> {
+ // Refresh the installed-skills cache on every execution for /<skill-name> autocompletion
+ await this.refreshSkills(ctx);
+
const parsed = parseSlashCommand(raw.trim(), this.getAll());
const command = parsed.command;
if (!command) {
+ // /<skill> <query> shorthand: check if the unknown name matches an installed skill.
+ const skillName = parsed.name;
+ if (skillName && this.installedSkills.some((s) => s.name.toLowerCase() === skillName.toLowerCase())) {
+ const skillsCommand = this.resolve("skills");
+ const useSubCommand = skillsCommand?.subCommands?.find((s) => s.name === "use");
+ if (useSubCommand) {
+ // parsed.args contains the full remainder starting with the skill name token
+ // (e.g. for `/pdf foo bar`, parsed.args = "pdf foo bar"). Strip the leading
+ // skill-name word so the "use" action receives only the user's query.
+ const query = parsed.args
+ .replace(new RegExp(`^${skillName.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}\\s*`, "i"), "")
+ .trim();
+ try {
+ await useSubCommand.action(ctx, `${skillName}, ${query}`);
+ } catch (error) {
+ const message = error instanceof Error ? error.message : String(error);
+ ctx.addItem(makeItem(ctx.sessionId, "error", message));
+ }
+ return;
+ }
+ }
ctx.addItem(makeItem(ctx.sessionId, "error", `Unknown command: /${parsed.name || ""}`));
return;
}
@@ -95,6 +168,7 @@ export class CommandService {
}
}
+ // Note: Command suggestions use the pi-tui library from app-screen.ts. This function is currently unused.
async getSuggestions(partial: string, ctx?: CommandContext): Promise<CommandSuggestion[]> {
const normalized = partial.replace(/^\//, "").toLowerCase();
const parts = parseArgs(normalized);
diff --git a/jiuwenclaw/channels/tui/frontend/src/ui/app-screen.ts b/jiuwenclaw/channels/tui/frontend/src/ui/app-screen.ts
index 1e0299b0b0..8701ce8008 100644
--- a/jiuwenclaw/channels/tui/frontend/src/ui/app-screen.ts
+++ b/jiuwenclaw/channels/tui/frontend/src/ui/app-screen.ts
@@ -24,7 +24,7 @@ import {
isSupportedAttachment,
syncComposerImageTokens,
} from "../core/attachments.js";
-import { CommandService, parseSlashCommand } from "../core/commands/CommandService.js";
+import { CommandService, parseSlashCommand, type InstalledSkillEntry } from "../core/commands/CommandService.js";
import { addCommandEcho, addError, addInfo } from "../core/commands/helpers.js";
import type { FileAttachment } from "../core/protocol.js";
import {
@@ -440,7 +440,7 @@ function buildResumeSessionItems(sessions: SessionMeta[]): SelectItem[] {
export class AppScreen implements Component, Focusable {
private readonly editor: Editor;
private readonly unsubscribe: () => void;
- private readonly composerAutocompleteProvider: AutocompleteProvider;
+ private composerAutocompleteProvider: AutocompleteProvider;
private _focused = false;
private activeQuestionId: string | null = null;
private activeQuestionIndex = 0;
@@ -469,6 +469,8 @@ export class AppScreen implements Component, Focusable {
private animationPhase = 0;
private runningStartedAtMs: number | null = null;
private runningStoppedAtMs: number | null = null;
+ /** Whether the eager skill-cache fetch on first WebSocket connection has already been fired. */
+ private didEagerFetchSkills = false;
private pendingSubmittedInput: string | null = null;
private pendingSubmittedBaseline = 0;
private pendingSubmittedSessionId: string | null = null;
@@ -482,14 +484,16 @@ export class AppScreen implements Component, Focusable {
private readonly exit: () => void,
) {
this.editor = new Editor(tui, editorTheme, { paddingX: 1, autocompleteMaxVisible: 6 });
- this.composerAutocompleteProvider = new ComposerAutocompleteProvider(
- new CombinedAutocompleteProvider(
- this.buildSlashCommands(),
- getTrustedDirs()[0] || process.cwd(),
- resolveFdBinary(),
- ),
- );
+ this.composerAutocompleteProvider = this.rebuildAutocompleteProvider();
this.editor.setAutocompleteProvider(this.composerAutocompleteProvider);
+ // Whenever CommandService refreshes its installed-skills cache (on first
+ // WebSocket connection and after every execute() call), rebuild the
+ // CombinedAutocompleteProvider so that the /<skillName> shorthands appear
+ // in the command-name dropdown.
+ this.commands.onInstalledSkillsChange = (skills: readonly InstalledSkillEntry[]) => {
+ this.composerAutocompleteProvider = this.rebuildAutocompleteProvider(skills);
+ this.editor.setAutocompleteProvider(this.composerAutocompleteProvider);
+ };
this.editor.onChange = () => {
this.tui.requestRender();
};
@@ -1102,6 +1106,11 @@ export class AppScreen implements Component, Focusable {
private handleStateChange(): void {
const snapshot = this.state.getSnapshot();
+ // Populate the skill cache as soon as the WebSocket connection is established
+ if (!this.didEagerFetchSkills && snapshot.connectionStatus === "connected") {
+ this.didEagerFetchSkills = true;
+ void this.commands.refreshSkills(this.state.getCommandContext());
+ }
if (
this.pendingSubmittedInput &&
(snapshot.sessionId !== this.pendingSubmittedSessionId ||
@@ -2483,6 +2492,37 @@ export class AppScreen implements Component, Focusable {
return suffix || null;
}
+ /**
+ * Builds a fresh {@link ComposerAutocompleteProvider} wrapping a new
+ * {@link CombinedAutocompleteProvider}. Skill shorthands are prepended to
+ * the regular slash-command list so they appear first in the dropdown.
+ *
+ * @param skills - snapshot of the installed-skills cache; defaults to the
+ * current cache exposed by {@link CommandService.getInstalledSkills}.
+ */
+ private rebuildAutocompleteProvider(
+ skills: readonly InstalledSkillEntry[] = this.commands.getInstalledSkills(),
+ ): ComposerAutocompleteProvider {
+ // Convert each installed skill to a TuiSlashCommand so CombinedAutocompleteProvider
+ // treats /<skillName> exactly like any other slash command for name completion.
+ const registeredNames = new Set(this.commands.getAll().map((c) => c.name));
+ const skillCommands: TuiSlashCommand[] = skills
+ .filter((skill) => !registeredNames.has(skill.name))
+ .map((skill) => ({
+ name: skill.name,
+ description: skill.description || `Use the "${skill.name}" skill`,
+ }));
+
+ return new ComposerAutocompleteProvider(
+ new CombinedAutocompleteProvider(
+ // Skill shorthands come last so they appear at the bottom of the dropdown.
+ [...this.buildSlashCommands(), ...skillCommands],
+ getTrustedDirs()[0] || process.cwd(),
+ resolveFdBinary(),
+ ),
+ );
+ }
+
private buildSlashCommands(): TuiSlashCommand[] {
return this.commands.getAll().map((command) => ({
name: command.name,