|
| 1 | +/** |
| 2 | + * DCP Manual mode command handler. |
| 3 | + * Handles toggling manual mode and triggering individual tool executions. |
| 4 | + * |
| 5 | + * Usage: |
| 6 | + * /dcp manual [on|off] - Toggle manual mode or set explicit state |
| 7 | + * /dcp prune [focus] - Trigger manual prune execution |
| 8 | + * /dcp distill [focus] - Trigger manual distill execution |
| 9 | + * /dcp compress [focus] - Trigger manual compress execution |
| 10 | + */ |
| 11 | + |
| 12 | +import type { Logger } from "../logger" |
| 13 | +import type { SessionState, WithParts } from "../state" |
| 14 | +import type { PluginConfig } from "../config" |
| 15 | +import { sendIgnoredMessage } from "../ui/notification" |
| 16 | +import { getCurrentParams } from "../strategies/utils" |
| 17 | +import { syncToolCache } from "../state/tool-cache" |
| 18 | +import { buildToolIdList } from "../messages/utils" |
| 19 | +import { buildPrunableToolsList } from "../messages/inject" |
| 20 | + |
| 21 | +const MANUAL_MODE_ON = |
| 22 | + "Manual mode is now ON. Use /dcp prune, /dcp distill, or /dcp compress to trigger context tools manually." |
| 23 | + |
| 24 | +const MANUAL_MODE_OFF = "Manual mode is now OFF." |
| 25 | + |
| 26 | +const NO_PRUNABLE_TOOLS = "No prunable tool outputs are currently available for manual triggering." |
| 27 | + |
| 28 | +const PRUNE_TRIGGER_PROMPT = [ |
| 29 | + "<prune triggered manually>", |
| 30 | + "Manual mode trigger received. You must now use the prune tool exactly once.", |
| 31 | + "Find the most significant set of prunable tool outputs to remove safely.", |
| 32 | + "Follow prune policy and avoid pruning outputs that may be needed later.", |
| 33 | + "Return after prune with a brief explanation of what you pruned and why.", |
| 34 | +].join("\n\n") |
| 35 | + |
| 36 | +const DISTILL_TRIGGER_PROMPT = [ |
| 37 | + "<distill triggered manually>", |
| 38 | + "Manual mode trigger received. You must now use the distill tool.", |
| 39 | + "Select the most information-dense prunable outputs and distill them into complete technical substitutes.", |
| 40 | + "Be exhaustive and preserve all critical technical details.", |
| 41 | + "Return after distill with a brief explanation of what was distilled and why.", |
| 42 | +].join("\n\n") |
| 43 | + |
| 44 | +const COMPRESS_TRIGGER_PROMPT = [ |
| 45 | + "<compress triggered manually>", |
| 46 | + "Manual mode trigger received. You must now use the compress tool.", |
| 47 | + "Find the most significant completed section of the conversation that can be compressed into a high-fidelity technical summary.", |
| 48 | + "Choose safe boundaries and preserve all critical implementation details.", |
| 49 | + "Return after compress with a brief explanation of what range was compressed.", |
| 50 | +].join("\n\n") |
| 51 | + |
| 52 | +function getTriggerPrompt( |
| 53 | + tool: "prune" | "distill" | "compress", |
| 54 | + context?: string, |
| 55 | + userFocus?: string, |
| 56 | +): string { |
| 57 | + const base = |
| 58 | + tool === "prune" |
| 59 | + ? PRUNE_TRIGGER_PROMPT |
| 60 | + : tool === "distill" |
| 61 | + ? DISTILL_TRIGGER_PROMPT |
| 62 | + : COMPRESS_TRIGGER_PROMPT |
| 63 | + |
| 64 | + const sections = [base] |
| 65 | + if (userFocus && userFocus.trim().length > 0) { |
| 66 | + sections.push(`Additional user focus:\n${userFocus.trim()}`) |
| 67 | + } |
| 68 | + if (context) { |
| 69 | + sections.push(context) |
| 70 | + } |
| 71 | + |
| 72 | + return sections.join("\n\n") |
| 73 | +} |
| 74 | + |
| 75 | +export interface ManualCommandContext { |
| 76 | + client: any |
| 77 | + state: SessionState |
| 78 | + config: PluginConfig |
| 79 | + logger: Logger |
| 80 | + sessionId: string |
| 81 | + messages: WithParts[] |
| 82 | +} |
| 83 | + |
| 84 | +export async function handleManualToggleCommand( |
| 85 | + ctx: ManualCommandContext, |
| 86 | + modeArg?: string, |
| 87 | +): Promise<void> { |
| 88 | + const { client, state, logger, sessionId, messages } = ctx |
| 89 | + |
| 90 | + if (modeArg === "on") { |
| 91 | + state.manualMode = true |
| 92 | + } else if (modeArg === "off") { |
| 93 | + state.manualMode = false |
| 94 | + } else { |
| 95 | + state.manualMode = !state.manualMode |
| 96 | + } |
| 97 | + |
| 98 | + const params = getCurrentParams(state, messages, logger) |
| 99 | + await sendIgnoredMessage( |
| 100 | + client, |
| 101 | + sessionId, |
| 102 | + state.manualMode ? MANUAL_MODE_ON : MANUAL_MODE_OFF, |
| 103 | + params, |
| 104 | + logger, |
| 105 | + ) |
| 106 | + |
| 107 | + logger.info("Manual mode toggled", { manualMode: state.manualMode }) |
| 108 | +} |
| 109 | + |
| 110 | +export async function handleManualTriggerCommand( |
| 111 | + ctx: ManualCommandContext, |
| 112 | + tool: "prune" | "distill" | "compress", |
| 113 | + userFocus?: string, |
| 114 | +): Promise<string | null> { |
| 115 | + const { client, state, config, logger, sessionId, messages } = ctx |
| 116 | + |
| 117 | + if (tool === "prune" || tool === "distill") { |
| 118 | + syncToolCache(state, config, logger, messages) |
| 119 | + buildToolIdList(state, messages, logger) |
| 120 | + const prunableToolsList = buildPrunableToolsList(state, config, logger) |
| 121 | + if (!prunableToolsList) { |
| 122 | + const params = getCurrentParams(state, messages, logger) |
| 123 | + await sendIgnoredMessage(client, sessionId, NO_PRUNABLE_TOOLS, params, logger) |
| 124 | + return null |
| 125 | + } |
| 126 | + |
| 127 | + return getTriggerPrompt(tool, prunableToolsList, userFocus) |
| 128 | + } |
| 129 | + |
| 130 | + return getTriggerPrompt("compress", undefined, userFocus) |
| 131 | +} |
0 commit comments