Restore full state when cycling through presets for none preset (#3272)
Added OriginalState interface to manage preset state and updated preset clearing logic to restore original state.
This commit is contained in:
@@ -97,10 +97,17 @@ function loadPresets(cwd: string): PresetsConfig {
|
|||||||
return { ...globalPresets, ...projectPresets };
|
return { ...globalPresets, ...projectPresets };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface OriginalState {
|
||||||
|
model: import("@mariozechner/pi-coding-agent").Model<any> | undefined;
|
||||||
|
thinkingLevel: "off" | "minimal" | "low" | "medium" | "high" | "xhigh";
|
||||||
|
tools: string[];
|
||||||
|
}
|
||||||
|
|
||||||
export default function presetExtension(pi: ExtensionAPI) {
|
export default function presetExtension(pi: ExtensionAPI) {
|
||||||
let presets: PresetsConfig = {};
|
let presets: PresetsConfig = {};
|
||||||
let activePresetName: string | undefined;
|
let activePresetName: string | undefined;
|
||||||
let activePreset: Preset | undefined;
|
let activePreset: Preset | undefined;
|
||||||
|
let originalState: OriginalState | undefined;
|
||||||
|
|
||||||
// Register --preset CLI flag
|
// Register --preset CLI flag
|
||||||
pi.registerFlag("preset", {
|
pi.registerFlag("preset", {
|
||||||
@@ -112,6 +119,15 @@ export default function presetExtension(pi: ExtensionAPI) {
|
|||||||
* Apply a preset configuration.
|
* Apply a preset configuration.
|
||||||
*/
|
*/
|
||||||
async function applyPreset(name: string, preset: Preset, ctx: ExtensionContext): Promise<boolean> {
|
async function applyPreset(name: string, preset: Preset, ctx: ExtensionContext): Promise<boolean> {
|
||||||
|
// Snapshot state before the first preset is applied (i.e. only when transitioning from no-preset)
|
||||||
|
if (activePresetName === undefined) {
|
||||||
|
originalState = {
|
||||||
|
model: ctx.model,
|
||||||
|
thinkingLevel: pi.getThinkingLevel(),
|
||||||
|
tools: pi.getActiveTools(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
// Apply model if specified
|
// Apply model if specified
|
||||||
if (preset.provider && preset.model) {
|
if (preset.provider && preset.model) {
|
||||||
const model = ctx.modelRegistry.find(preset.provider, preset.model);
|
const model = ctx.modelRegistry.find(preset.provider, preset.model);
|
||||||
@@ -248,10 +264,18 @@ export default function presetExtension(pi: ExtensionAPI) {
|
|||||||
if (!result) return;
|
if (!result) return;
|
||||||
|
|
||||||
if (result === "(none)") {
|
if (result === "(none)") {
|
||||||
// Clear preset and restore defaults
|
// Clear preset and restore original state
|
||||||
activePresetName = undefined;
|
activePresetName = undefined;
|
||||||
activePreset = undefined;
|
activePreset = undefined;
|
||||||
|
if (originalState) {
|
||||||
|
if (originalState.model) {
|
||||||
|
await pi.setModel(originalState.model);
|
||||||
|
}
|
||||||
|
pi.setThinkingLevel(originalState.thinkingLevel);
|
||||||
|
pi.setActiveTools(originalState.tools);
|
||||||
|
} else {
|
||||||
pi.setActiveTools(["read", "bash", "edit", "write"]);
|
pi.setActiveTools(["read", "bash", "edit", "write"]);
|
||||||
|
}
|
||||||
ctx.ui.notify("Preset cleared, defaults restored", "info");
|
ctx.ui.notify("Preset cleared, defaults restored", "info");
|
||||||
updateStatus(ctx);
|
updateStatus(ctx);
|
||||||
return;
|
return;
|
||||||
@@ -296,7 +320,15 @@ export default function presetExtension(pi: ExtensionAPI) {
|
|||||||
if (nextName === "(none)") {
|
if (nextName === "(none)") {
|
||||||
activePresetName = undefined;
|
activePresetName = undefined;
|
||||||
activePreset = undefined;
|
activePreset = undefined;
|
||||||
|
if (originalState) {
|
||||||
|
if (originalState.model) {
|
||||||
|
await pi.setModel(originalState.model);
|
||||||
|
}
|
||||||
|
pi.setThinkingLevel(originalState.thinkingLevel);
|
||||||
|
pi.setActiveTools(originalState.tools);
|
||||||
|
} else {
|
||||||
pi.setActiveTools(["read", "bash", "edit", "write"]);
|
pi.setActiveTools(["read", "bash", "edit", "write"]);
|
||||||
|
}
|
||||||
ctx.ui.notify("Preset cleared, defaults restored", "info");
|
ctx.ui.notify("Preset cleared, defaults restored", "info");
|
||||||
updateStatus(ctx);
|
updateStatus(ctx);
|
||||||
return;
|
return;
|
||||||
|
|||||||
Reference in New Issue
Block a user