mirror of
https://github.com/openclaw/openclaw.git
synced 2026-06-06 05:51:15 +08:00
refactor: centralize chat command interpretation
This commit is contained in:
@@ -1688,7 +1688,8 @@ export async function handleSendChat(
|
||||
const attachments = host.chatAttachments ?? [];
|
||||
const attachmentsToSend = messageOverride == null ? snapshotChatAttachments(attachments) : [];
|
||||
const hasAttachments = attachmentsToSend.length > 0;
|
||||
const isSkillWorkshopRevisionSend = Boolean(opts?.skillWorkshopRevision);
|
||||
const skillWorkshopRevision = opts?.skillWorkshopRevision;
|
||||
const shouldInterpretChatCommands = !skillWorkshopRevision;
|
||||
|
||||
if (!message && !hasAttachments) {
|
||||
return;
|
||||
@@ -1698,77 +1699,79 @@ export async function handleSendChat(
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isSkillWorkshopRevisionSend && isChatStopCommand(message)) {
|
||||
if (messageOverride == null) {
|
||||
recordNonTranscriptInputHistory(host, message);
|
||||
}
|
||||
await handleAbortChat(host);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isSkillWorkshopRevisionSend && isBtwCommand(message)) {
|
||||
const submitKey = chatSubmitKey(host, "btw", message, attachmentsToSend);
|
||||
await withChatSubmitGuard(host, submitKey, async () => {
|
||||
const modelSwitchReady = waitForPendingChatModelSwitch(host, submittedSessionKey);
|
||||
if (modelSwitchReady !== true && !(await modelSwitchReady)) {
|
||||
return;
|
||||
}
|
||||
if (host.sessionKey !== submittedSessionKey) {
|
||||
return;
|
||||
}
|
||||
const cleared =
|
||||
messageOverride == null
|
||||
? clearSubmittedComposerState(host, previousDraft, attachmentsToSend)
|
||||
: {};
|
||||
if (shouldInterpretChatCommands) {
|
||||
if (isChatStopCommand(message)) {
|
||||
if (messageOverride == null) {
|
||||
recordNonTranscriptInputHistory(host, message);
|
||||
}
|
||||
await sendDetachedBtwMessage(host, message, {
|
||||
previousDraft: cleared.previousDraft,
|
||||
attachments: hasAttachments ? attachmentsToSend : undefined,
|
||||
previousAttachments: cleared.previousAttachments,
|
||||
});
|
||||
});
|
||||
return;
|
||||
}
|
||||
await handleAbortChat(host);
|
||||
return;
|
||||
}
|
||||
|
||||
// Intercept local slash commands (/status, /model, /compact, etc.)
|
||||
const parsed = isSkillWorkshopRevisionSend ? null : parseSlashCommand(message);
|
||||
if (parsed?.command.executeLocal) {
|
||||
if (isChatBusy(host) && shouldQueueLocalSlashCommand(parsed.command.key)) {
|
||||
if (isBtwCommand(message)) {
|
||||
const submitKey = chatSubmitKey(host, "btw", message, attachmentsToSend);
|
||||
await withChatSubmitGuard(host, submitKey, async () => {
|
||||
const modelSwitchReady = waitForPendingChatModelSwitch(host, submittedSessionKey);
|
||||
if (modelSwitchReady !== true && !(await modelSwitchReady)) {
|
||||
return;
|
||||
}
|
||||
if (host.sessionKey !== submittedSessionKey) {
|
||||
return;
|
||||
}
|
||||
const cleared =
|
||||
messageOverride == null
|
||||
? clearSubmittedComposerState(host, previousDraft, attachmentsToSend)
|
||||
: {};
|
||||
if (messageOverride == null) {
|
||||
recordNonTranscriptInputHistory(host, message);
|
||||
}
|
||||
await sendDetachedBtwMessage(host, message, {
|
||||
previousDraft: cleared.previousDraft,
|
||||
attachments: hasAttachments ? attachmentsToSend : undefined,
|
||||
previousAttachments: cleared.previousAttachments,
|
||||
});
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
// Intercept local slash commands (/status, /model, /compact, etc.)
|
||||
const parsed = parseSlashCommand(message);
|
||||
if (parsed?.command.executeLocal) {
|
||||
if (isChatBusy(host) && shouldQueueLocalSlashCommand(parsed.command.key)) {
|
||||
if (messageOverride == null) {
|
||||
recordNonTranscriptInputHistory(host, message);
|
||||
host.chatMessage = "";
|
||||
host.chatAttachments = [];
|
||||
resetChatInputHistoryNavigation(host);
|
||||
}
|
||||
enqueueChatMessage(host, message, undefined, isChatResetCommand(message), {
|
||||
args: parsed.args,
|
||||
name: parsed.command.key,
|
||||
});
|
||||
return;
|
||||
}
|
||||
const prevDraft = messageOverride == null ? previousDraft : undefined;
|
||||
if (messageOverride == null) {
|
||||
recordNonTranscriptInputHistory(host, message);
|
||||
host.chatMessage = "";
|
||||
host.chatAttachments = [];
|
||||
resetChatInputHistoryNavigation(host);
|
||||
}
|
||||
enqueueChatMessage(host, message, undefined, isChatResetCommand(message), {
|
||||
args: parsed.args,
|
||||
name: parsed.command.key,
|
||||
await dispatchSlashCommand(host, parsed.command.key, parsed.args, {
|
||||
previousDraft: prevDraft,
|
||||
restoreDraft: Boolean(messageOverride && opts?.restoreDraft),
|
||||
});
|
||||
return;
|
||||
}
|
||||
const prevDraft = messageOverride == null ? previousDraft : undefined;
|
||||
if (messageOverride == null) {
|
||||
recordNonTranscriptInputHistory(host, message);
|
||||
host.chatMessage = "";
|
||||
host.chatAttachments = [];
|
||||
resetChatInputHistoryNavigation(host);
|
||||
}
|
||||
await dispatchSlashCommand(host, parsed.command.key, parsed.args, {
|
||||
previousDraft: prevDraft,
|
||||
restoreDraft: Boolean(messageOverride && opts?.restoreDraft),
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const refreshSessions = !isSkillWorkshopRevisionSend && isChatResetCommand(message);
|
||||
const refreshSessions = shouldInterpretChatCommands && isChatResetCommand(message);
|
||||
const submitKey = chatSubmitKey(
|
||||
host,
|
||||
"message",
|
||||
message,
|
||||
attachmentsToSend,
|
||||
opts?.skillWorkshopRevision,
|
||||
skillWorkshopRevision,
|
||||
);
|
||||
await withChatSubmitGuard(host, submitKey, async () => {
|
||||
if (host.sessionKey !== submittedSessionKey) {
|
||||
@@ -1791,7 +1794,7 @@ export async function handleSendChat(
|
||||
refreshSessions,
|
||||
submittedAtMs,
|
||||
waitingForModel ? "waiting-model" : undefined,
|
||||
opts?.skillWorkshopRevision,
|
||||
skillWorkshopRevision,
|
||||
);
|
||||
if (!queued) {
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user