feat(commands): add dynamic /<alias> model switching
This commit is contained in:
@@ -1,14 +1,36 @@
|
|||||||
export function extractModelDirective(body?: string): {
|
function escapeRegExp(value: string) {
|
||||||
|
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
||||||
|
}
|
||||||
|
|
||||||
|
export function extractModelDirective(
|
||||||
|
body?: string,
|
||||||
|
options?: { aliases?: string[] },
|
||||||
|
): {
|
||||||
cleaned: string;
|
cleaned: string;
|
||||||
rawModel?: string;
|
rawModel?: string;
|
||||||
rawProfile?: string;
|
rawProfile?: string;
|
||||||
hasDirective: boolean;
|
hasDirective: boolean;
|
||||||
} {
|
} {
|
||||||
if (!body) return { cleaned: "", hasDirective: false };
|
if (!body) return { cleaned: "", hasDirective: false };
|
||||||
const match = body.match(
|
|
||||||
|
const modelMatch = body.match(
|
||||||
/(?:^|\s)\/model(?=$|\s|:)\s*:?\s*([A-Za-z0-9_.:@-]+(?:\/[A-Za-z0-9_.:@-]+)?)?/i,
|
/(?:^|\s)\/model(?=$|\s|:)\s*:?\s*([A-Za-z0-9_.:@-]+(?:\/[A-Za-z0-9_.:@-]+)?)?/i,
|
||||||
);
|
);
|
||||||
const raw = match?.[1]?.trim();
|
|
||||||
|
const aliases = (options?.aliases ?? []).map((alias) => alias.trim()).filter(Boolean);
|
||||||
|
const aliasMatch =
|
||||||
|
modelMatch || aliases.length === 0
|
||||||
|
? null
|
||||||
|
: body.match(
|
||||||
|
new RegExp(
|
||||||
|
`(?:^|\\s)\\/(${aliases.map(escapeRegExp).join("|")})(?=$|\\s|:)`,
|
||||||
|
"i",
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
const match = modelMatch ?? aliasMatch;
|
||||||
|
const raw = modelMatch ? modelMatch?.[1]?.trim() : aliasMatch?.[1]?.trim();
|
||||||
|
|
||||||
let rawModel = raw;
|
let rawModel = raw;
|
||||||
let rawProfile: string | undefined;
|
let rawProfile: string | undefined;
|
||||||
if (raw?.includes("@")) {
|
if (raw?.includes("@")) {
|
||||||
@@ -16,9 +38,11 @@ export function extractModelDirective(body?: string): {
|
|||||||
rawModel = parts[0]?.trim();
|
rawModel = parts[0]?.trim();
|
||||||
rawProfile = parts.slice(1).join("@").trim() || undefined;
|
rawProfile = parts.slice(1).join("@").trim() || undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
const cleaned = match
|
const cleaned = match
|
||||||
? body.replace(match[0], "").replace(/\s+/g, " ").trim()
|
? body.replace(match[0], "").replace(/\s+/g, " ").trim()
|
||||||
: body.trim();
|
: body.trim();
|
||||||
|
|
||||||
return {
|
return {
|
||||||
cleaned,
|
cleaned,
|
||||||
rawModel,
|
rawModel,
|
||||||
|
|||||||
@@ -312,7 +312,12 @@ export async function getReplyFromConfig(
|
|||||||
rawDrop: undefined,
|
rawDrop: undefined,
|
||||||
hasQueueOptions: false,
|
hasQueueOptions: false,
|
||||||
});
|
});
|
||||||
let parsedDirectives = parseInlineDirectives(rawBody);
|
const configuredAliases = Object.values(cfg.agent?.models ?? {})
|
||||||
|
.map((entry) => entry.alias)
|
||||||
|
.filter((alias): alias is string => Boolean(alias));
|
||||||
|
let parsedDirectives = parseInlineDirectives(rawBody, {
|
||||||
|
modelAliases: configuredAliases,
|
||||||
|
});
|
||||||
const hasDirective =
|
const hasDirective =
|
||||||
parsedDirectives.hasThinkDirective ||
|
parsedDirectives.hasThinkDirective ||
|
||||||
parsedDirectives.hasVerboseDirective ||
|
parsedDirectives.hasVerboseDirective ||
|
||||||
|
|||||||
@@ -181,7 +181,10 @@ export type InlineDirectives = {
|
|||||||
hasQueueOptions: boolean;
|
hasQueueOptions: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
export function parseInlineDirectives(body: string): InlineDirectives {
|
export function parseInlineDirectives(
|
||||||
|
body: string,
|
||||||
|
options?: { modelAliases?: string[] },
|
||||||
|
): InlineDirectives {
|
||||||
const {
|
const {
|
||||||
cleaned: thinkCleaned,
|
cleaned: thinkCleaned,
|
||||||
thinkLevel,
|
thinkLevel,
|
||||||
@@ -213,7 +216,9 @@ export function parseInlineDirectives(body: string): InlineDirectives {
|
|||||||
rawModel,
|
rawModel,
|
||||||
rawProfile,
|
rawProfile,
|
||||||
hasDirective: hasModelDirective,
|
hasDirective: hasModelDirective,
|
||||||
} = extractModelDirective(statusCleaned);
|
} = extractModelDirective(statusCleaned, {
|
||||||
|
aliases: options?.modelAliases,
|
||||||
|
});
|
||||||
const {
|
const {
|
||||||
cleaned: queueCleaned,
|
cleaned: queueCleaned,
|
||||||
queueMode,
|
queueMode,
|
||||||
|
|||||||
Reference in New Issue
Block a user