Format: align thinking helpers
This commit is contained in:
@@ -20,6 +20,8 @@ type CommandReplyConfig = NonNullable<WarelayConfig["inbound"]>["reply"] & {
|
|||||||
|
|
||||||
type EnqueueRunner = typeof enqueueCommand;
|
type EnqueueRunner = typeof enqueueCommand;
|
||||||
|
|
||||||
|
type ThinkLevel = "off" | "minimal" | "low" | "medium" | "high";
|
||||||
|
|
||||||
type CommandReplyParams = {
|
type CommandReplyParams = {
|
||||||
reply: CommandReplyConfig;
|
reply: CommandReplyConfig;
|
||||||
templatingCtx: TemplateContext;
|
templatingCtx: TemplateContext;
|
||||||
@@ -31,6 +33,7 @@ type CommandReplyParams = {
|
|||||||
timeoutSeconds: number;
|
timeoutSeconds: number;
|
||||||
commandRunner: typeof runCommandWithTimeout;
|
commandRunner: typeof runCommandWithTimeout;
|
||||||
enqueue?: EnqueueRunner;
|
enqueue?: EnqueueRunner;
|
||||||
|
thinkLevel?: ThinkLevel;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type CommandReplyMeta = {
|
export type CommandReplyMeta = {
|
||||||
@@ -98,6 +101,25 @@ export function summarizeClaudeMetadata(payload: unknown): string | undefined {
|
|||||||
return parts.length ? parts.join(", ") : undefined;
|
return parts.length ? parts.join(", ") : undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function appendThinkingCue(body: string, level?: ThinkLevel): string {
|
||||||
|
if (!level || level === "off") return body;
|
||||||
|
const cue = (() => {
|
||||||
|
switch (level) {
|
||||||
|
case "high":
|
||||||
|
return "ultrathink";
|
||||||
|
case "medium":
|
||||||
|
return "think harder";
|
||||||
|
case "low":
|
||||||
|
return "think hard";
|
||||||
|
case "minimal":
|
||||||
|
return "think";
|
||||||
|
default:
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
return [body.trim(), cue].filter(Boolean).join(" ");
|
||||||
|
}
|
||||||
|
|
||||||
export async function runCommandReply(
|
export async function runCommandReply(
|
||||||
params: CommandReplyParams,
|
params: CommandReplyParams,
|
||||||
): Promise<CommandReplyResult> {
|
): Promise<CommandReplyResult> {
|
||||||
@@ -118,6 +140,7 @@ export async function runCommandReply(
|
|||||||
timeoutSeconds,
|
timeoutSeconds,
|
||||||
commandRunner,
|
commandRunner,
|
||||||
enqueue = enqueueCommand,
|
enqueue = enqueueCommand,
|
||||||
|
thinkLevel,
|
||||||
} = params;
|
} = params;
|
||||||
|
|
||||||
if (!reply.command?.length) {
|
if (!reply.command?.length) {
|
||||||
@@ -178,6 +201,23 @@ export async function runCommandReply(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (thinkLevel && thinkLevel !== "off") {
|
||||||
|
if (agentKind === "pi") {
|
||||||
|
const hasThinkingFlag = argv.some(
|
||||||
|
(p, i) =>
|
||||||
|
p === "--thinking" ||
|
||||||
|
(i > 0 && argv[i - 1] === "--thinking") ||
|
||||||
|
p.startsWith("--thinking="),
|
||||||
|
);
|
||||||
|
if (!hasThinkingFlag) {
|
||||||
|
argv.splice(bodyIndex, 0, "--thinking", thinkLevel);
|
||||||
|
bodyIndex += 2;
|
||||||
|
}
|
||||||
|
} else if (argv[bodyIndex]) {
|
||||||
|
argv[bodyIndex] = appendThinkingCue(argv[bodyIndex] ?? "", thinkLevel);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const shouldApplyAgent = agent.isInvocation(argv);
|
const shouldApplyAgent = agent.isInvocation(argv);
|
||||||
const finalArgv = shouldApplyAgent
|
const finalArgv = shouldApplyAgent
|
||||||
? agent.buildArgs({
|
? agent.buildArgs({
|
||||||
@@ -213,11 +253,12 @@ export async function runCommandReply(
|
|||||||
const run = async () => {
|
const run = async () => {
|
||||||
// Prefer long-lived tau RPC for pi agent to avoid cold starts.
|
// Prefer long-lived tau RPC for pi agent to avoid cold starts.
|
||||||
if (agentKind === "pi") {
|
if (agentKind === "pi") {
|
||||||
const body = finalArgv[bodyIndex] ?? "";
|
const promptIndex = finalArgv.length - 1;
|
||||||
|
const body = finalArgv[promptIndex] ?? "";
|
||||||
// Build rpc args without the prompt body; force --mode rpc.
|
// Build rpc args without the prompt body; force --mode rpc.
|
||||||
const rpcArgv = (() => {
|
const rpcArgv = (() => {
|
||||||
const copy = [...finalArgv];
|
const copy = [...finalArgv];
|
||||||
copy.splice(bodyIndex, 1);
|
copy.splice(promptIndex, 1);
|
||||||
const modeIdx = copy.indexOf("--mode");
|
const modeIdx = copy.indexOf("--mode");
|
||||||
if (modeIdx >= 0 && copy[modeIdx + 1]) {
|
if (modeIdx >= 0 && copy[modeIdx + 1]) {
|
||||||
copy.splice(modeIdx, 2, "--mode", "rpc");
|
copy.splice(modeIdx, 2, "--mode", "rpc");
|
||||||
|
|||||||
@@ -40,10 +40,13 @@ function normalizeThinkLevel(raw?: string | null): ThinkLevel | undefined {
|
|||||||
const key = raw.toLowerCase();
|
const key = raw.toLowerCase();
|
||||||
if (["off"].includes(key)) return "off";
|
if (["off"].includes(key)) return "off";
|
||||||
if (["min", "minimal"].includes(key)) return "minimal";
|
if (["min", "minimal"].includes(key)) return "minimal";
|
||||||
if (["low"].includes(key)) return "low";
|
if (["low", "thinkhard", "think-hard", "think_hard"].includes(key))
|
||||||
if (["med", "medium", "thinkhard", "think-harder", "thinkharder"].includes(key))
|
return "low";
|
||||||
|
if (["med", "medium", "thinkharder", "think-harder", "harder"].includes(key))
|
||||||
return "medium";
|
return "medium";
|
||||||
if (["high", "ultra", "ultrathink", "think-hard", "thinkhardest"].includes(key))
|
if (
|
||||||
|
["high", "ultra", "ultrathink", "think-hard", "thinkhardest"].includes(key)
|
||||||
|
)
|
||||||
return "high";
|
return "high";
|
||||||
if (["think"].includes(key)) return "minimal";
|
if (["think"].includes(key)) return "minimal";
|
||||||
return undefined;
|
return undefined;
|
||||||
@@ -61,17 +64,6 @@ function extractThinkDirective(body?: string): {
|
|||||||
return { cleaned, thinkLevel };
|
return { cleaned, thinkLevel };
|
||||||
}
|
}
|
||||||
|
|
||||||
function appendThinkingCue(body: string, level?: ThinkLevel): string {
|
|
||||||
if (!level || level === "off") return body;
|
|
||||||
const cue =
|
|
||||||
level === "high"
|
|
||||||
? "ultrathink"
|
|
||||||
: level === "medium"
|
|
||||||
? "think harder"
|
|
||||||
: "think";
|
|
||||||
return [body.trim(), cue].filter(Boolean).join(" ");
|
|
||||||
}
|
|
||||||
|
|
||||||
function isAbortTrigger(text?: string): boolean {
|
function isAbortTrigger(text?: string): boolean {
|
||||||
if (!text) return false;
|
if (!text) return false;
|
||||||
const normalized = text.trim().toLowerCase();
|
const normalized = text.trim().toLowerCase();
|
||||||
|
|||||||
Reference in New Issue
Block a user