refactor: tidy directive parsing + queue status

This commit is contained in:
Peter Steinberger
2026-01-08 03:40:39 +01:00
parent e657e59b46
commit 8aa3efb9e8
7 changed files with 342 additions and 211 deletions

View File

@@ -50,6 +50,7 @@ import {
extractQueueDirective,
type QueueDropPolicy,
type QueueMode,
resolveQueueSettings,
} from "./queue.js";
const SYSTEM_MARK = "⚙️";
@@ -328,6 +329,7 @@ export async function handleDirectiveOnly(params: {
allowedModelKeys,
allowedModelCatalog,
resetModelOverride,
provider,
initialModelLabel,
formatModelSwitchEvent,
currentThinkLevel,
@@ -433,6 +435,33 @@ export async function handleDirectiveOnly(params: {
return { text: "elevated is not available right now." };
}
if (
directives.hasQueueDirective &&
!directives.queueMode &&
!directives.queueReset &&
!directives.hasQueueOptions &&
directives.rawQueueMode === undefined &&
directives.rawDebounce === undefined &&
directives.rawCap === undefined &&
directives.rawDrop === undefined
) {
const settings = resolveQueueSettings({
cfg: params.cfg,
provider,
sessionEntry,
});
const debounceLabel =
typeof settings.debounceMs === "number"
? `${settings.debounceMs}ms`
: "default";
const capLabel =
typeof settings.cap === "number" ? String(settings.cap) : "default";
const dropLabel = settings.dropPolicy ?? "default";
return {
text: `Current queue settings: mode=${settings.mode}, debounce=${debounceLabel}, cap=${capLabel}, drop=${dropLabel}.`,
};
}
const queueModeInvalid =
directives.hasQueueDirective &&
!directives.queueMode &&

View File

@@ -9,6 +9,81 @@ import {
type VerboseLevel,
} from "../thinking.js";
type ExtractedLevel<T> = {
cleaned: string;
level?: T;
rawLevel?: string;
hasDirective: boolean;
};
const escapeRegExp = (value: string) =>
value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
const matchLevelDirective = (
body: string,
names: string[],
): { start: number; end: number; rawLevel?: string } | null => {
const namePattern = names.map(escapeRegExp).join("|");
const match = body.match(
new RegExp(`(?:^|\\s)\\/(?:${namePattern})(?=$|\\s|:)`, "i"),
);
if (!match || match.index === undefined) return null;
const start = match.index;
let end = match.index + match[0].length;
let i = end;
while (i < body.length && /\s/.test(body[i])) i += 1;
if (body[i] === ":") {
i += 1;
while (i < body.length && /\s/.test(body[i])) i += 1;
}
const argStart = i;
while (i < body.length && /[A-Za-z-]/.test(body[i])) i += 1;
const rawLevel = i > argStart ? body.slice(argStart, i) : undefined;
end = i;
return { start, end, rawLevel };
};
const extractLevelDirective = <T>(
body: string,
names: string[],
normalize: (raw?: string) => T | undefined,
): ExtractedLevel<T> => {
const match = matchLevelDirective(body, names);
if (!match) {
return { cleaned: body.trim(), hasDirective: false };
}
const rawLevel = match.rawLevel;
const level = normalize(rawLevel);
const cleaned = body
.slice(0, match.start)
.concat(body.slice(match.end))
.replace(/\s+/g, " ")
.trim();
return {
cleaned,
level,
rawLevel,
hasDirective: true,
};
};
const extractSimpleDirective = (
body: string,
names: string[],
): { cleaned: string; hasDirective: boolean } => {
const namePattern = names.map(escapeRegExp).join("|");
const match = body.match(
new RegExp(`(?:^|\\s)\\/(?:${namePattern})(?=$|\\s|:)(?:\\s*:\\s*)?`, "i"),
);
const cleaned = match
? body.replace(match[0], "").replace(/\s+/g, " ").trim()
: body.trim();
return {
cleaned,
hasDirective: Boolean(match),
};
};
export function extractThinkDirective(body?: string): {
cleaned: string;
thinkLevel?: ThinkLevel;
@@ -16,19 +91,16 @@ export function extractThinkDirective(body?: string): {
hasDirective: boolean;
} {
if (!body) return { cleaned: "", hasDirective: false };
// Match with optional argument - require word boundary via lookahead after keyword
const match = body.match(
/(?:^|\s)\/(?:thinking|think|t)(?=$|\s|:)(?:\s*:?\s*(?:([a-zA-Z-]+)\b)?)?/i,
const extracted = extractLevelDirective(
body,
["thinking", "think", "t"],
normalizeThinkLevel,
);
const thinkLevel = normalizeThinkLevel(match?.[1]);
const cleaned = match
? body.replace(match[0], "").replace(/\s+/g, " ").trim()
: body.trim();
return {
cleaned,
thinkLevel,
rawLevel: match?.[1],
hasDirective: !!match,
cleaned: extracted.cleaned,
thinkLevel: extracted.level,
rawLevel: extracted.rawLevel,
hasDirective: extracted.hasDirective,
};
}
@@ -39,18 +111,16 @@ export function extractVerboseDirective(body?: string): {
hasDirective: boolean;
} {
if (!body) return { cleaned: "", hasDirective: false };
const match = body.match(
/(?:^|\s)\/(?:verbose|v)(?=$|\s|:)(?:\s*:?\s*(?:([a-zA-Z-]+)\b)?)?/i,
const extracted = extractLevelDirective(
body,
["verbose", "v"],
normalizeVerboseLevel,
);
const verboseLevel = normalizeVerboseLevel(match?.[1]);
const cleaned = match
? body.replace(match[0], "").replace(/\s+/g, " ").trim()
: body.trim();
return {
cleaned,
verboseLevel,
rawLevel: match?.[1],
hasDirective: !!match,
cleaned: extracted.cleaned,
verboseLevel: extracted.level,
rawLevel: extracted.rawLevel,
hasDirective: extracted.hasDirective,
};
}
@@ -61,18 +131,16 @@ export function extractElevatedDirective(body?: string): {
hasDirective: boolean;
} {
if (!body) return { cleaned: "", hasDirective: false };
const match = body.match(
/(?:^|\s)\/(?:elevated|elev)(?=$|\s|:)(?:\s*:?\s*(?:([a-zA-Z-]+)\b)?)?/i,
const extracted = extractLevelDirective(
body,
["elevated", "elev"],
normalizeElevatedLevel,
);
const elevatedLevel = normalizeElevatedLevel(match?.[1]);
const cleaned = match
? body.replace(match[0], "").replace(/\s+/g, " ").trim()
: body.trim();
return {
cleaned,
elevatedLevel,
rawLevel: match?.[1],
hasDirective: !!match,
cleaned: extracted.cleaned,
elevatedLevel: extracted.level,
rawLevel: extracted.rawLevel,
hasDirective: extracted.hasDirective,
};
}
@@ -83,18 +151,16 @@ export function extractReasoningDirective(body?: string): {
hasDirective: boolean;
} {
if (!body) return { cleaned: "", hasDirective: false };
const match = body.match(
/(?:^|\s)\/(?:reasoning|reason)(?=$|\s|:)(?:\s*:?\s*(?:([a-zA-Z-]+)\b)?)?/i,
const extracted = extractLevelDirective(
body,
["reasoning", "reason"],
normalizeReasoningLevel,
);
const reasoningLevel = normalizeReasoningLevel(match?.[1]);
const cleaned = match
? body.replace(match[0], "").replace(/\s+/g, " ").trim()
: body.trim();
return {
cleaned,
reasoningLevel,
rawLevel: match?.[1],
hasDirective: !!match,
cleaned: extracted.cleaned,
reasoningLevel: extracted.level,
rawLevel: extracted.rawLevel,
hasDirective: extracted.hasDirective,
};
}
@@ -103,14 +169,7 @@ export function extractStatusDirective(body?: string): {
hasDirective: boolean;
} {
if (!body) return { cleaned: "", hasDirective: false };
const match = body.match(/(?:^|\s)\/status(?=$|\s|:)(?:\s*:\s*)?/i);
const cleaned = match
? body.replace(match[0], "").replace(/\s+/g, " ").trim()
: body.trim();
return {
cleaned,
hasDirective: !!match,
};
return extractSimpleDirective(body, ["status"]);
}
export type { ElevatedLevel, ReasoningLevel, ThinkLevel, VerboseLevel };