ceulen 0.5.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +317 -12
- package/README.md +87 -91
- package/extensions/lib/advisor-marker.ts +33 -0
- package/extensions/lib/panel.ts +25 -2
- package/extensions/lib/rate.ts +24 -0
- package/extensions/lib/registry.ts +93 -7
- package/extensions/lib/tools.ts +47 -0
- package/extensions/lib/usage-store.ts +33 -0
- package/extensions/modules/advisor/commands/advisor.ts +244 -0
- package/extensions/modules/advisor/configPanel.ts +194 -0
- package/extensions/modules/advisor/index.ts +217 -0
- package/extensions/modules/advisor/lib/config.ts +248 -0
- package/extensions/modules/advisor/lib/emission-guard.ts +153 -0
- package/extensions/modules/advisor/lib/isolated-model.ts +160 -0
- package/extensions/modules/advisor/lib/model-picker.ts +47 -0
- package/extensions/modules/advisor/lib/watcher.ts +286 -0
- package/extensions/modules/advisor/test/compliance.test.ts +125 -0
- package/extensions/modules/advisor/test/config.test.ts +202 -0
- package/extensions/modules/advisor/test/configPanel.test.ts +223 -0
- package/extensions/modules/advisor/test/emission-guard.test.ts +201 -0
- package/extensions/modules/advisor/test/isolated-model.test.ts +311 -0
- package/extensions/modules/advisor/test/model-picker.test.ts +32 -0
- package/extensions/modules/advisor/test/module.test.ts +586 -0
- package/extensions/modules/advisor/test/watcher.test.ts +478 -0
- package/extensions/modules/classifier/configPanel.ts +104 -0
- package/extensions/modules/classifier/index.ts +297 -0
- package/extensions/modules/classifier/lib/settings.ts +95 -0
- package/extensions/modules/classifier/test/classifier.test.ts +475 -0
- package/extensions/modules/composer/index.ts +152 -3
- package/extensions/modules/composer/lib/footer.ts +119 -0
- package/extensions/modules/composer/lib/shapes.ts +265 -26
- package/extensions/modules/composer/test/editor.test.ts +1 -1
- package/extensions/modules/composer/test/footer.test.ts +96 -0
- package/extensions/modules/composer/test/module.test.ts +113 -11
- package/extensions/modules/composer/test/screen.test.ts +29 -1
- package/extensions/modules/composer/test/shapes.test.ts +224 -8
- package/extensions/modules/config/defaultTools.ts +96 -0
- package/extensions/modules/config/index.ts +147 -7
- package/extensions/modules/config/piSettings.ts +254 -69
- package/extensions/modules/config/test/defaultTools.test.ts +122 -0
- package/extensions/modules/config/test/menu.test.ts +64 -0
- package/extensions/modules/config/test/panel.test.ts +38 -23
- package/extensions/modules/config/test/piSettings.test.ts +130 -3
- package/extensions/modules/config/test/renderTools.test.ts +71 -0
- package/extensions/modules/config/test/tools.test.ts +146 -0
- package/extensions/modules/fff/index.ts +2094 -0
- package/extensions/modules/fff/lib/query.ts +129 -0
- package/extensions/modules/fff/test/index.test.ts +607 -0
- package/extensions/modules/fff/test/query.test.ts +71 -0
- package/extensions/modules/munin/configPanel.ts +105 -0
- package/extensions/modules/munin/index.ts +641 -0
- package/extensions/modules/munin/lib/helpers.ts +471 -0
- package/extensions/modules/munin/lib/retry.ts +20 -0
- package/extensions/modules/munin/lib/sdk.ts +218 -0
- package/extensions/modules/munin/test/config.test.ts +168 -0
- package/extensions/modules/munin/test/configPanel.test.ts +161 -0
- package/extensions/modules/munin/test/extension.test.ts +224 -0
- package/extensions/modules/munin/test/helpers.test.ts +523 -0
- package/extensions/modules/munin/test/retry.test.ts +145 -0
- package/extensions/modules/munin/test/stale-protocol.test.ts +554 -0
- package/extensions/modules/plan/configPanel.ts +152 -0
- package/extensions/modules/plan/index.ts +917 -0
- package/extensions/modules/plan/lib/plan-tools.ts +72 -0
- package/extensions/modules/plan/lib/prompt.ts +65 -0
- package/extensions/modules/plan/lib/settings.ts +166 -0
- package/extensions/modules/plan/lib/shell-gate.ts +200 -0
- package/extensions/modules/plan/test/configPanel.test.ts +96 -0
- package/extensions/modules/plan/test/harness.ts +133 -0
- package/extensions/modules/plan/test/module.test.ts +305 -0
- package/extensions/modules/plan/test/settings.test.ts +114 -0
- package/extensions/modules/plan/test/shell-gate.test.ts +103 -0
- package/extensions/modules/ponytail/index.ts +23 -73
- package/extensions/modules/ponytail/lib/config.ts +1 -6
- package/extensions/modules/ponytail/test/extension.test.ts +1 -85
- package/extensions/modules/repair/configPanel.ts +96 -0
- package/extensions/modules/repair/index.ts +414 -0
- package/extensions/modules/repair/lib/auto-bg.ts +333 -0
- package/extensions/modules/repair/lib/edit-repair.ts +155 -0
- package/extensions/modules/repair/lib/editor.ts +251 -0
- package/extensions/modules/repair/lib/guards.ts +34 -0
- package/extensions/modules/repair/lib/input-repair.ts +324 -0
- package/extensions/modules/repair/lib/patch.ts +481 -0
- package/extensions/modules/repair/lib/settings.ts +114 -0
- package/extensions/modules/repair/test/auto-bg.test.ts +405 -0
- package/extensions/modules/repair/test/edit-repair.test.ts +224 -0
- package/extensions/modules/repair/test/editor.test.ts +141 -0
- package/extensions/modules/repair/test/guards.test.ts +64 -0
- package/extensions/modules/repair/test/input-repair.test.ts +231 -0
- package/extensions/modules/repair/test/patch.test.ts +563 -0
- package/extensions/modules/repair/test/settings.test.ts +167 -0
- package/extensions/modules/repair/test/wrapper.test.ts +210 -0
- package/extensions/modules/router/commands/commands.ts +4 -0
- package/extensions/modules/router/lib/client.ts +61 -0
- package/extensions/modules/router/lib/provider.ts +34 -13
- package/extensions/modules/router/lib/systemone.ts +152 -0
- package/extensions/modules/router/test/unit.test.ts +179 -0
- package/extensions/modules/rtk/findFallback.js +68 -0
- package/extensions/modules/rtk/index.ts +248 -0
- package/extensions/modules/rtk/safe-rewrite.js +74 -0
- package/extensions/modules/rtk/test/findFallback.test.js +66 -0
- package/extensions/modules/rtk/test/index.test.js +131 -0
- package/extensions/modules/rtk/test/safe-rewrite.test.js +96 -0
- package/extensions/modules/rtk/version-gate.js +34 -0
- package/extensions/modules/serena/index.ts +629 -0
- package/extensions/modules/serena/lib/detect.ts +73 -0
- package/extensions/modules/serena/lib/env.ts +76 -0
- package/extensions/modules/serena/lib/guidance.ts +26 -0
- package/extensions/modules/serena/lib/normalize.ts +27 -0
- package/extensions/modules/serena/lib/symbol-key.ts +20 -0
- package/extensions/modules/serena/lib/truncate.ts +20 -0
- package/extensions/modules/serena/test/env.test.ts +122 -0
- package/extensions/modules/serena/test/index.test.ts +347 -0
- package/extensions/modules/serena/test/worker.test.ts +562 -0
- package/extensions/modules/serena/worker.ts +858 -0
- package/extensions/modules/steering/configPanel.ts +122 -0
- package/extensions/modules/steering/index.ts +616 -0
- package/extensions/modules/steering/lib/anchor.ts +175 -0
- package/extensions/modules/steering/lib/errors.ts +253 -0
- package/extensions/modules/steering/lib/family.ts +33 -0
- package/extensions/modules/steering/lib/guidance.ts +172 -0
- package/extensions/modules/steering/lib/history.ts +170 -0
- package/extensions/modules/steering/lib/settings.ts +127 -0
- package/extensions/modules/steering/test/anchor-regressions.test.ts +93 -0
- package/extensions/modules/steering/test/anchor-wiring.test.ts +99 -0
- package/extensions/modules/steering/test/anchor.test.ts +160 -0
- package/extensions/modules/steering/test/cache-stats.test.ts +43 -0
- package/extensions/modules/steering/test/errors.test.ts +309 -0
- package/extensions/modules/steering/test/family.test.ts +43 -0
- package/extensions/modules/steering/test/guidance.test.ts +357 -0
- package/extensions/modules/steering/test/harness.ts +93 -0
- package/extensions/modules/steering/test/history.test.ts +118 -0
- package/extensions/modules/steering/test/settings.test.ts +182 -0
- package/extensions/modules/steering/test/status.test.ts +71 -0
- package/extensions/modules/steering/test/toolCall.test.ts +99 -0
- package/extensions/modules/subagent/agents/planner.md +14 -0
- package/extensions/modules/subagent/agents/reviewer.md +42 -0
- package/extensions/modules/subagent/agents/scout.md +47 -0
- package/extensions/modules/subagent/agents/tester.md +12 -0
- package/extensions/modules/subagent/agents/worker.md +18 -0
- package/extensions/modules/subagent/configPanel.ts +219 -0
- package/extensions/modules/subagent/index.ts +2451 -0
- package/extensions/modules/subagent/lib/agents.ts +408 -0
- package/extensions/modules/subagent/lib/background.ts +383 -0
- package/extensions/modules/subagent/lib/herdr.ts +1019 -0
- package/extensions/modules/subagent/lib/history.ts +126 -0
- package/extensions/modules/subagent/lib/model.ts +156 -0
- package/extensions/modules/subagent/lib/render.ts +335 -0
- package/extensions/modules/subagent/lib/result.ts +110 -0
- package/extensions/modules/subagent/lib/roles.ts +296 -0
- package/extensions/modules/subagent/lib/routing.ts +223 -0
- package/extensions/modules/subagent/lib/runner.ts +559 -0
- package/extensions/modules/subagent/lib/schema.ts +21 -0
- package/extensions/modules/subagent/lib/security.ts +752 -0
- package/extensions/modules/subagent/lib/settings.ts +159 -0
- package/extensions/modules/subagent/lib/thread-viewer.ts +319 -0
- package/extensions/modules/subagent/lib/threads.ts +135 -0
- package/extensions/modules/subagent/lib/types.ts +18 -0
- package/extensions/modules/subagent/lib/widget.ts +475 -0
- package/extensions/modules/subagent/test/configPanel.test.ts +132 -0
- package/extensions/modules/subagent/test/herdr-advisor.test.ts +40 -0
- package/extensions/modules/subagent/test/routing.test.ts +350 -0
- package/extensions/modules/subagent/test/widget.test.ts +208 -0
- package/extensions/modules/usage/index.ts +59 -16
- package/extensions/modules/usage/test/genrate.test.ts +70 -0
- package/extensions/modules/usage/test/parsers.test.ts +13 -0
- package/extensions/modules/ux/UPSTREAM +24 -0
- package/extensions/modules/ux/index.ts +393 -0
- package/extensions/modules/ux/lib/audit.ts +562 -0
- package/extensions/modules/ux/lib/config.ts +136 -0
- package/extensions/modules/ux/lib/instructions.ts +77 -0
- package/extensions/modules/ux/test/audit.test.ts +756 -0
- package/extensions/modules/ux/test/extension.test.ts +394 -0
- package/extensions/modules/ux/test/instructions.test.ts +19 -0
- package/extensions/modules/web/configPanel.ts +193 -0
- package/extensions/modules/web/index.ts +978 -0
- package/extensions/modules/web/lib/agy.ts +209 -0
- package/extensions/modules/web/lib/brave.ts +42 -0
- package/extensions/modules/web/lib/cdp.ts +686 -0
- package/extensions/modules/web/lib/chatapi.ts +101 -0
- package/extensions/modules/web/lib/chatgpt.ts +796 -0
- package/extensions/modules/web/lib/chrome.ts +328 -0
- package/extensions/modules/web/lib/config.ts +136 -0
- package/extensions/modules/web/lib/content.ts +99 -0
- package/extensions/modules/web/lib/crawl4ai.ts +172 -0
- package/extensions/modules/web/lib/extract.ts +235 -0
- package/extensions/modules/web/lib/firecrawl.ts +56 -0
- package/extensions/modules/web/lib/format.ts +197 -0
- package/extensions/modules/web/lib/gemini-auth.ts +286 -0
- package/extensions/modules/web/lib/gemini-dr.ts +338 -0
- package/extensions/modules/web/lib/gemini.ts +551 -0
- package/extensions/modules/web/lib/imageapi.ts +559 -0
- package/extensions/modules/web/lib/retry.ts +71 -0
- package/extensions/modules/web/lib/search.ts +237 -0
- package/extensions/modules/web/lib/searxng.ts +69 -0
- package/extensions/modules/web/lib/settings.ts +148 -0
- package/extensions/modules/web/scripts/cdp-smoke.ts +77 -0
- package/extensions/modules/web/scripts/gemini-smoke.ts +107 -0
- package/extensions/modules/web/test/agy.test.ts +383 -0
- package/extensions/modules/web/test/cdp.test.ts +717 -0
- package/extensions/modules/web/test/chat.test.ts +171 -0
- package/extensions/modules/web/test/chatgpt.test.ts +633 -0
- package/extensions/modules/web/test/chrome.test.ts +282 -0
- package/extensions/modules/web/test/config.test.ts +218 -0
- package/extensions/modules/web/test/configPanel.test.ts +132 -0
- package/extensions/modules/web/test/crawl.test.ts +134 -0
- package/extensions/modules/web/test/crawl4ai.test.ts +105 -0
- package/extensions/modules/web/test/extension.test.ts +118 -0
- package/extensions/modules/web/test/extract.test.ts +436 -0
- package/extensions/modules/web/test/fixtures/dr-confirm.bin +12 -0
- package/extensions/modules/web/test/fixtures/dr-plan.bin +20 -0
- package/extensions/modules/web/test/fixtures/interaction.html +31 -0
- package/extensions/modules/web/test/format.test.ts +230 -0
- package/extensions/modules/web/test/gemini-auth.test.ts +465 -0
- package/extensions/modules/web/test/gemini-dr.test.ts +249 -0
- package/extensions/modules/web/test/gemini.test.ts +472 -0
- package/extensions/modules/web/test/hook.test.ts +101 -0
- package/extensions/modules/web/test/image.test.ts +1089 -0
- package/extensions/modules/web/test/retry.test.ts +119 -0
- package/extensions/modules/web/test/screenshot.test.ts +109 -0
- package/extensions/modules/web/test/search.test.ts +210 -0
- package/extensions/modules/web/test/settings.test.ts +165 -0
- package/extensions/modules/web/test/skill.test.ts +72 -0
- package/extensions/modules/web/types.d.ts +3 -0
- package/extensions/modules/web/vendor/package.json +1 -0
- package/extensions/modules/web/vendor/readability/LICENSE.md +13 -0
- package/extensions/modules/web/vendor/readability/Readability-readerable.js +123 -0
- package/extensions/modules/web/vendor/readability/Readability.js +2787 -0
- package/extensions/modules/web/vendor/readability/index.d.ts +64 -0
- package/extensions/modules/web/vendor/readability/index.js +9 -0
- package/extensions/modules/web/vendor/turndown/LICENSE +21 -0
- package/extensions/modules/web/vendor/turndown/turndown.cjs.js +784 -0
- package/extensions/modules/web/vendor/turndown-plugin-gfm/LICENSE +21 -0
- package/extensions/modules/web/vendor/turndown-plugin-gfm/turndown-plugin-gfm.cjs.js +163 -0
- package/extensions/modules/zai/configPanel.ts +151 -0
- package/extensions/modules/zai/index.ts +186 -0
- package/extensions/modules/zai/lib/anthropic.ts +159 -0
- package/extensions/modules/zai/lib/settings.ts +198 -0
- package/extensions/modules/zai/lib/signing.ts +718 -0
- package/extensions/modules/zai/lib/throttle.ts +198 -0
- package/extensions/modules/zai/test/anthropic.test.ts +132 -0
- package/extensions/modules/zai/test/configPanel.test.ts +207 -0
- package/extensions/modules/zai/test/extension.test.ts +249 -0
- package/extensions/modules/zai/test/settings.test.ts +183 -0
- package/extensions/modules/zai/test/signing-wiring.test.ts +108 -0
- package/extensions/modules/zai/test/signing.test.ts +345 -0
- package/extensions/modules/zai/test/throttle.test.ts +329 -0
- package/package.json +19 -4
- package/scripts/validate-themes.mjs +75 -0
- package/skills/munin/SKILL.md +117 -0
- package/skills/ux-capture/SKILL.md +202 -0
- package/skills/ux-design/SKILL.md +279 -0
- package/skills/ux-presets/SKILL.md +484 -0
- package/skills/ux-routing/SKILL.md +46 -0
- package/skills/web/SKILL.md +153 -0
- package/themes/alabaster.json +93 -0
- package/themes/amethyst.json +96 -0
- package/themes/anthracite.json +93 -0
- package/themes/basalt.json +91 -0
- package/themes/birch.json +95 -0
- package/themes/catppuccin-mocha.json +91 -0
- package/themes/dark-abyss.json +91 -0
- package/themes/dark-arctic.json +104 -0
- package/themes/dark-aurora.json +95 -0
- package/themes/dark-catppuccin.json +107 -0
- package/themes/dark-cavern.json +91 -0
- package/themes/dark-celestial.json +106 -0
- package/themes/dark-copper.json +95 -0
- package/themes/dark-cosmos.json +90 -0
- package/themes/dark-cyberpunk.json +102 -0
- package/themes/dark-dracula.json +98 -0
- package/themes/dark-eclipse.json +91 -0
- package/themes/dark-ember.json +95 -0
- package/themes/dark-equinox.json +90 -0
- package/themes/dark-forest.json +96 -0
- package/themes/dark-github.json +105 -0
- package/themes/dark-gruvbox.json +112 -0
- package/themes/dark-lavender.json +95 -0
- package/themes/dark-lunar.json +89 -0
- package/themes/dark-midnight.json +95 -0
- package/themes/dark-monochrome.json +94 -0
- package/themes/dark-monokai.json +98 -0
- package/themes/dark-nebula.json +90 -0
- package/themes/dark-neon-noir.json +100 -0
- package/themes/dark-nord.json +98 -0
- package/themes/dark-ocean.json +101 -0
- package/themes/dark-one.json +100 -0
- package/themes/dark-poimandres.json +115 -0
- package/themes/dark-rainforest.json +91 -0
- package/themes/dark-reef.json +91 -0
- package/themes/dark-retro.json +92 -0
- package/themes/dark-rose-pine.json +96 -0
- package/themes/dark-sakura.json +95 -0
- package/themes/dark-slate.json +95 -0
- package/themes/dark-solarized.json +97 -0
- package/themes/dark-solstice.json +90 -0
- package/themes/dark-starfall.json +91 -0
- package/themes/dark-sunset.json +99 -0
- package/themes/dark-swamp.json +90 -0
- package/themes/dark-synthwave.json +103 -0
- package/themes/dark-taiga.json +91 -0
- package/themes/dark-terminal.json +95 -0
- package/themes/dark-tokyo-night.json +101 -0
- package/themes/dark-tundra.json +91 -0
- package/themes/dark-twilight.json +91 -0
- package/themes/dark-volcanic.json +91 -0
- package/themes/dark.json +80 -0
- package/themes/graphite.json +92 -0
- package/themes/light-arctic.json +107 -0
- package/themes/light-aurora-day.json +91 -0
- package/themes/light-canyon.json +91 -0
- package/themes/light-catppuccin.json +106 -0
- package/themes/light-cirrus.json +90 -0
- package/themes/light-coral.json +95 -0
- package/themes/light-cyberpunk.json +96 -0
- package/themes/light-dawn.json +90 -0
- package/themes/light-dunes.json +91 -0
- package/themes/light-eucalyptus.json +95 -0
- package/themes/light-forest.json +100 -0
- package/themes/light-frost.json +95 -0
- package/themes/light-github.json +115 -0
- package/themes/light-glacier.json +91 -0
- package/themes/light-gruvbox.json +108 -0
- package/themes/light-haze.json +90 -0
- package/themes/light-honeycomb.json +95 -0
- package/themes/light-lagoon.json +91 -0
- package/themes/light-lavender.json +95 -0
- package/themes/light-meadow.json +91 -0
- package/themes/light-mint.json +95 -0
- package/themes/light-monochrome.json +101 -0
- package/themes/light-ocean.json +99 -0
- package/themes/light-one.json +99 -0
- package/themes/light-opal.json +91 -0
- package/themes/light-orchard.json +91 -0
- package/themes/light-paper.json +95 -0
- package/themes/light-poimandres.json +115 -0
- package/themes/light-prism.json +90 -0
- package/themes/light-retro.json +98 -0
- package/themes/light-sand.json +95 -0
- package/themes/light-savanna.json +91 -0
- package/themes/light-solarized.json +102 -0
- package/themes/light-soleil.json +90 -0
- package/themes/light-sunset.json +99 -0
- package/themes/light-synthwave.json +98 -0
- package/themes/light-tokyo-night.json +111 -0
- package/themes/light-wetland.json +91 -0
- package/themes/light-zenith.json +89 -0
- package/themes/light.json +80 -0
- package/themes/limestone.json +94 -0
- package/themes/mahogany.json +97 -0
- package/themes/marble.json +93 -0
- package/themes/mirage.json +79 -0
- package/themes/obsidian.json +91 -0
- package/themes/onyx.json +91 -0
- package/themes/pearl.json +93 -0
- package/themes/porcelain.json +91 -0
- package/themes/quartz.json +96 -0
- package/themes/sandstone.json +95 -0
- package/themes/titanium.json +90 -0
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* advisor's /config contribution — Model tab, Advisor section.
|
|
3
|
+
*
|
|
4
|
+
* The working copy is the EFFECTIVE settings (agent-dir file, trusted project
|
|
5
|
+
* file over it); saving writes the full `advisor` section back to the
|
|
6
|
+
* agent-dir settings.json and applies it to the live session through the
|
|
7
|
+
* module bridge — no /reload. Enable + per-tool rows are prepended/appended by
|
|
8
|
+
* the config module automatically.
|
|
9
|
+
*
|
|
10
|
+
* The model menu reads the catalogue (provider/id) from the registry stash the
|
|
11
|
+
* module fills at session_start — config factories receive no ctx (classifier
|
|
12
|
+
* precedent). `menu` and inline `completions` are mutually exclusive on one
|
|
13
|
+
* row, hence the primary picker and the comma-separated fallback row.
|
|
14
|
+
*/
|
|
15
|
+
import type { ExtensionContext, ModelRegistry } from "@earendil-works/pi-coding-agent";
|
|
16
|
+
import { row, toInt, type PanelCompletionItem, type PanelGroup, type PanelMenuOption } from "../../lib/panel.js";
|
|
17
|
+
import type { ModuleConfig } from "../../lib/registry.js";
|
|
18
|
+
import {
|
|
19
|
+
chainFromSettings,
|
|
20
|
+
loadAdvisorSettings,
|
|
21
|
+
projectShadow,
|
|
22
|
+
settingsFromChain,
|
|
23
|
+
THINKING_LEVELS,
|
|
24
|
+
writeAdvisorSettings,
|
|
25
|
+
type AdvisorChain,
|
|
26
|
+
type AdvisorConfig,
|
|
27
|
+
} from "./lib/config.js";
|
|
28
|
+
import { modelRef } from "./lib/model-picker.js";
|
|
29
|
+
|
|
30
|
+
/** The panel's working copy: the chain rows plus the non-chain settings. */
|
|
31
|
+
export interface AdvisorPanelCfg extends AdvisorChain {
|
|
32
|
+
enabled: boolean;
|
|
33
|
+
minToolCalls: number;
|
|
34
|
+
immuneTurns: number;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Installed by the module at load — effective settings + live apply. */
|
|
38
|
+
export interface AdvisorBridge {
|
|
39
|
+
read(): AdvisorConfig;
|
|
40
|
+
apply(next: AdvisorConfig, ctx: ExtensionContext): void | Promise<void>;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
let bridge: AdvisorBridge | undefined;
|
|
44
|
+
export function setAdvisorBridge(value: AdvisorBridge | undefined): void {
|
|
45
|
+
bridge = value;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Registry stash — set by the module's session_start; `/config` always opens
|
|
49
|
+
* after a session exists. Exported for tests. */
|
|
50
|
+
let registry: ModelRegistry | undefined;
|
|
51
|
+
export function setAdvisorRegistry(value: ModelRegistry | undefined): void {
|
|
52
|
+
registry = value;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function modelOptions(): PanelMenuOption[] {
|
|
56
|
+
return (registry?.getAvailable() ?? [])
|
|
57
|
+
.map((model) => {
|
|
58
|
+
const ref = modelRef(model);
|
|
59
|
+
return { value: ref, label: ref, ...(model.name && model.name !== ref ? { description: model.name } : {}) };
|
|
60
|
+
})
|
|
61
|
+
.sort((a, b) => a.value.localeCompare(b.value));
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Build the advisor panel groups over a working copy (mutated by row setters).
|
|
65
|
+
* Exported for tests. */
|
|
66
|
+
export function buildAdvisorGroups(cfg: AdvisorPanelCfg): PanelGroup[] {
|
|
67
|
+
const modelMenu = (): PanelMenuOption[] => [
|
|
68
|
+
{ value: "", label: "(none)", description: "No primary model — the advisor stays inactive." },
|
|
69
|
+
...modelOptions(),
|
|
70
|
+
];
|
|
71
|
+
const thinkingMenu = (): PanelMenuOption[] => [
|
|
72
|
+
{ value: "", label: "(model default)", description: "No pinned level — the provider default applies." },
|
|
73
|
+
...THINKING_LEVELS.filter((level) => level !== "off").map((level) => ({ value: level, label: level })),
|
|
74
|
+
];
|
|
75
|
+
const modelCompletions = (): PanelCompletionItem[] =>
|
|
76
|
+
modelOptions().map((option) => ({ value: option.value, label: option.label, description: option.description }));
|
|
77
|
+
|
|
78
|
+
return [
|
|
79
|
+
{
|
|
80
|
+
key: "advisor",
|
|
81
|
+
label: "Advisor",
|
|
82
|
+
tab: "Model",
|
|
83
|
+
icon: "🧭",
|
|
84
|
+
rows: [
|
|
85
|
+
row("advisor.enabled", "Review settled turns", "toggle", cfg.enabled, (v) => {
|
|
86
|
+
cfg.enabled = Boolean(v);
|
|
87
|
+
}, {
|
|
88
|
+
description: "Automatic background review: after a settled turn the reviewer may steer one note in. Off = no review — the consult tool still works. /advisor on|off overrides it for this session.",
|
|
89
|
+
defaultValue: true,
|
|
90
|
+
}),
|
|
91
|
+
row("advisor.model", "Primary model", "string", cfg.model, (v) => {
|
|
92
|
+
cfg.model = String(v ?? "").trim();
|
|
93
|
+
}, {
|
|
94
|
+
menu: modelMenu,
|
|
95
|
+
description: "Reviewer model (provider/id), picked from the catalogue. Empty = advisor inactive.",
|
|
96
|
+
warning: cfg.model ? undefined : "No model set — the advisor will not run.",
|
|
97
|
+
defaultValue: "",
|
|
98
|
+
}),
|
|
99
|
+
row("advisor.thinking", "Thinking", "string", cfg.thinking, (v) => {
|
|
100
|
+
cfg.thinking = String(v ?? "").trim();
|
|
101
|
+
}, {
|
|
102
|
+
menu: thinkingMenu,
|
|
103
|
+
description: "Thinking level pinned on the primary chain entry (saved as its :level suffix).",
|
|
104
|
+
defaultValue: "",
|
|
105
|
+
}),
|
|
106
|
+
row("advisor.fallbacks", "Fallback chain", "string", cfg.fallbacks, (v) => {
|
|
107
|
+
cfg.fallbacks = String(v ?? "").trim();
|
|
108
|
+
}, {
|
|
109
|
+
completions: modelCompletions,
|
|
110
|
+
description: "Ordered fallbacks, comma-separated provider/id (optionally :level). Served when the primary fails.",
|
|
111
|
+
defaultValue: "",
|
|
112
|
+
}),
|
|
113
|
+
row("advisor.watch.minToolCalls", "Min tool calls per review", "number", cfg.minToolCalls, (v) => {
|
|
114
|
+
cfg.minToolCalls = Math.max(0, toInt(v, cfg.minToolCalls));
|
|
115
|
+
}, {
|
|
116
|
+
description: "Turns with fewer new tool calls than this are skipped (0 = review every turn).",
|
|
117
|
+
defaultValue: 3,
|
|
118
|
+
}),
|
|
119
|
+
row("advisor.watch.immuneTurns", "Immune turns", "number", cfg.immuneTurns, (v) => {
|
|
120
|
+
cfg.immuneTurns = Math.max(0, toInt(v, cfg.immuneTurns));
|
|
121
|
+
}, {
|
|
122
|
+
description: "Settled turns after a steer during which further nits are deferred (also the dedupe window).",
|
|
123
|
+
defaultValue: 3,
|
|
124
|
+
}),
|
|
125
|
+
],
|
|
126
|
+
},
|
|
127
|
+
];
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const OWNED_PREFIX = "advisor.";
|
|
131
|
+
|
|
132
|
+
/** advisor's ModuleConfig for the central /config panel. Reads go through the
|
|
133
|
+
* module bridge (which knows the session's cwd + trust); tests and headless
|
|
134
|
+
* opens fall back to an untrusted agent-dir read. */
|
|
135
|
+
export function advisorConfig(): ModuleConfig {
|
|
136
|
+
const before = (bridge?.read ?? (() => loadAdvisorSettings(process.cwd(), false)))();
|
|
137
|
+
const working: AdvisorPanelCfg = {
|
|
138
|
+
...chainFromSettings(before.models),
|
|
139
|
+
enabled: before.enabled,
|
|
140
|
+
minToolCalls: before.watch.minToolCalls,
|
|
141
|
+
immuneTurns: before.watch.immuneTurns,
|
|
142
|
+
};
|
|
143
|
+
const originalModels = [...before.models];
|
|
144
|
+
const original = { enabled: before.enabled, minToolCalls: before.watch.minToolCalls, immuneTurns: before.watch.immuneTurns };
|
|
145
|
+
|
|
146
|
+
return {
|
|
147
|
+
groups: () => buildAdvisorGroups(working),
|
|
148
|
+
save: async (edited, ctx) => {
|
|
149
|
+
if (![...edited].some((k) => k.startsWith(OWNED_PREFIX))) return;
|
|
150
|
+
const next: AdvisorConfig = {
|
|
151
|
+
enabled: working.enabled,
|
|
152
|
+
models: settingsFromChain(working),
|
|
153
|
+
watch: { minToolCalls: working.minToolCalls, immuneTurns: working.immuneTurns },
|
|
154
|
+
};
|
|
155
|
+
if (
|
|
156
|
+
next.enabled === original.enabled &&
|
|
157
|
+
next.watch.minToolCalls === original.minToolCalls &&
|
|
158
|
+
next.watch.immuneTurns === original.immuneTurns &&
|
|
159
|
+
next.models.join("\u0000") === originalModels.join("\u0000")
|
|
160
|
+
) {
|
|
161
|
+
ctx.ui.notify("Advisor: no changes.", "info");
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
let file: string;
|
|
165
|
+
try {
|
|
166
|
+
file = writeAdvisorSettings(next);
|
|
167
|
+
} catch (e) {
|
|
168
|
+
ctx.ui.notify(`Advisor save failed: ${e instanceof Error ? e.message : e}`, "error");
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
const notes = [
|
|
172
|
+
next.models.length > 0
|
|
173
|
+
? `Advisor saved to ${file}: ${next.models.join(" → ")}`
|
|
174
|
+
: `Advisor chain cleared — saved to ${file}`,
|
|
175
|
+
next.enabled
|
|
176
|
+
? `auto-review on (minToolCalls=${next.watch.minToolCalls}, immuneTurns=${next.watch.immuneTurns})`
|
|
177
|
+
: "auto-review off",
|
|
178
|
+
];
|
|
179
|
+
try {
|
|
180
|
+
await bridge?.apply(next, ctx);
|
|
181
|
+
notes.push("Applied to this session.");
|
|
182
|
+
} catch (e) {
|
|
183
|
+
notes.push(`Saved to the file, but the live session update failed: ${e instanceof Error ? e.message : e}`);
|
|
184
|
+
}
|
|
185
|
+
// The project layer wins over a global save — say so (pi-advisor's own
|
|
186
|
+
// warning), for both the new and the legacy section name.
|
|
187
|
+
const shadow = projectShadow(ctx.cwd, ctx.isProjectTrusted?.() === true);
|
|
188
|
+
if (shadow.length > 0) {
|
|
189
|
+
notes.push(`This project's .pi/settings.json sets "${shadow[0]}" — the project layer overrides this save.`);
|
|
190
|
+
}
|
|
191
|
+
ctx.ui.notify(notes.join(" "), shadow.length > 0 ? "warning" : "info");
|
|
192
|
+
},
|
|
193
|
+
};
|
|
194
|
+
}
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* advisor — second-model reviewer (ported from `@bacnh85/pi-advisor` 0.3.8).
|
|
3
|
+
*
|
|
4
|
+
* After every settled turn with real work, an isolated reviewer model examines
|
|
5
|
+
* the transcript and may emit ONE severity-routed note (nit / concern /
|
|
6
|
+
* blocker), delivered as a follow-up instruction or — inside the post-steer
|
|
7
|
+
* calm-down window — deferred to the next turn as an LLM-visible aside. The
|
|
8
|
+
* same configured chain backs the on-demand `advisor` tool.
|
|
9
|
+
*
|
|
10
|
+
* Config lives in the `advisor` section of the agent-dir settings.json (trusted
|
|
11
|
+
* project `.pi/settings.json` overrides; the standalone package's `pi-advisor`
|
|
12
|
+
* section is read-compatible and migrated on first save) — `/config` → Model →
|
|
13
|
+
* Advisor is the editor, applied live. See lib/config.ts for the file contract.
|
|
14
|
+
*/
|
|
15
|
+
import {
|
|
16
|
+
getMarkdownTheme,
|
|
17
|
+
type BeforeAgentStartEventResult,
|
|
18
|
+
type ExtensionAPI,
|
|
19
|
+
type ExtensionContext,
|
|
20
|
+
type Theme,
|
|
21
|
+
} from "@earendil-works/pi-coding-agent";
|
|
22
|
+
import { Box, Markdown, Text } from "@earendil-works/pi-tui";
|
|
23
|
+
|
|
24
|
+
import { DEFAULTS, loadAdvisorSettings, writeAdvisorSettings, type AdvisorConfig } from "./lib/config.js";
|
|
25
|
+
import { MAX_NOTE_LENGTH, isSeverity, sanitizeNote, type Severity } from "./lib/emission-guard.js";
|
|
26
|
+
import { createGuard } from "./lib/emission-guard.js";
|
|
27
|
+
import { REVIEW_ENTRY, createRuntime, latestEntryId, reseedCursor, reviewTurn, type IsolatedCall, type WatcherRuntime } from "./lib/watcher.js";
|
|
28
|
+
import { registerAdvisor, type AdvisorHandle } from "./commands/advisor.js";
|
|
29
|
+
import { setAdvisorBridge, setAdvisorRegistry } from "./configPanel.js";
|
|
30
|
+
|
|
31
|
+
// ponytail: test-only injection — real calls use runIsolatedChain (watcher's default); tests swap it.
|
|
32
|
+
let testIsolated: IsolatedCall | undefined;
|
|
33
|
+
export function __setIsolatedForTest(fn: IsolatedCall | undefined): void {
|
|
34
|
+
testIsolated = fn;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
interface NoteData {
|
|
38
|
+
severity: Severity;
|
|
39
|
+
note: string;
|
|
40
|
+
timestamp: number;
|
|
41
|
+
deferred?: boolean;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Shared card body for the entry renderer and the message-renderer fallback. */
|
|
45
|
+
function renderNoteCard(raw: NoteData | undefined, expanded: boolean, theme: Theme): Box {
|
|
46
|
+
const data = raw && isSeverity(raw.severity) && typeof raw.note === "string"
|
|
47
|
+
? { ...raw, note: sanitizeNote(raw.note).slice(0, MAX_NOTE_LENGTH) }
|
|
48
|
+
: { severity: "nit" as Severity, note: "(unavailable)", timestamp: 0 };
|
|
49
|
+
const box = new Box(1, 1, (text) => theme.bg("customMessageBg", text));
|
|
50
|
+
const label = data.deferred ? "Advisor (deferred — next turn)" : "Advisor";
|
|
51
|
+
const sev = data.severity === "blocker"
|
|
52
|
+
? theme.fg("error", data.severity)
|
|
53
|
+
: data.severity === "concern"
|
|
54
|
+
? theme.fg("warning", data.severity)
|
|
55
|
+
: theme.fg("dim", data.severity);
|
|
56
|
+
box.addChild(new Text(`${theme.fg("accent", theme.bold(label))} ${sev} ${theme.fg("dim", data.timestamp ? new Date(data.timestamp).toLocaleTimeString() : "")}`, 0, 0));
|
|
57
|
+
box.addChild(new Markdown(data.note, 0, 0, getMarkdownTheme()));
|
|
58
|
+
if (expanded && data.timestamp) box.addChild(new Text(theme.fg("dim", new Date(data.timestamp).toLocaleString()), 0, 0));
|
|
59
|
+
return box;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export default function advisorModule(pi: ExtensionAPI): void {
|
|
63
|
+
let runtime: WatcherRuntime | undefined;
|
|
64
|
+
let runtimeSessionId: string | undefined;
|
|
65
|
+
/** Effective settings as last loaded/applied — the base for partial writes. */
|
|
66
|
+
let settings: AdvisorConfig = DEFAULTS;
|
|
67
|
+
let cwd = process.cwd();
|
|
68
|
+
let trusted = false;
|
|
69
|
+
/** Session-scoped watch flag: defaults to the persisted master, toggled by
|
|
70
|
+
* /advisor on|off, never written. */
|
|
71
|
+
let watchEnabled = false;
|
|
72
|
+
let handle: AdvisorHandle | undefined;
|
|
73
|
+
|
|
74
|
+
/** Adopt committed settings in the live session (the file write is the caller's). */
|
|
75
|
+
function applySettings(next: AdvisorConfig, ctx: ExtensionContext): void {
|
|
76
|
+
const masterChanged = next.enabled !== settings.enabled;
|
|
77
|
+
settings = next;
|
|
78
|
+
const rt = runtime;
|
|
79
|
+
const wasEmpty = !rt || rt.models.length === 0;
|
|
80
|
+
if (rt) {
|
|
81
|
+
rt.config = next;
|
|
82
|
+
rt.models = next.models;
|
|
83
|
+
// Re-enabling clears an accumulated 3-strike pause (same as /advisor on).
|
|
84
|
+
if (next.enabled) {
|
|
85
|
+
rt.stats.paused = false;
|
|
86
|
+
rt.failures = 0;
|
|
87
|
+
}
|
|
88
|
+
// Enabling mid-session must not replay history.
|
|
89
|
+
if (wasEmpty && next.models.length > 0 && watchEnabled) reseedCursor(rt, ctx);
|
|
90
|
+
}
|
|
91
|
+
// An explicit master change re-arms/disarms the session; otherwise a
|
|
92
|
+
// /advisor watch-off override stays in place.
|
|
93
|
+
if (masterChanged) watchEnabled = next.enabled;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
pi.registerEntryRenderer<NoteData>(REVIEW_ENTRY, (entry, { expanded }, theme) => {
|
|
97
|
+
return renderNoteCard(entry.data, expanded, theme);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
// Message renderer for next-turn asides (LLM-visible deferred notes). In the
|
|
101
|
+
// TUI the deferred message is display:false (the immediate card is the visible
|
|
102
|
+
// surface), so this is a fallback for non-TUI surfaces that render flushed
|
|
103
|
+
// messages.
|
|
104
|
+
pi.registerMessageRenderer<NoteData>(REVIEW_ENTRY, (message, { expanded }, theme) => {
|
|
105
|
+
const raw = (message as unknown as { details?: unknown }).details as NoteData | undefined;
|
|
106
|
+
return renderNoteCard(raw, expanded, theme);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
pi.on("before_agent_start", (event, ctx): BeforeAgentStartEventResult | undefined => {
|
|
110
|
+
// Headless runs can't receive watch notes — don't make them pay dead
|
|
111
|
+
// prompt text claiming otherwise.
|
|
112
|
+
if (ctx.mode !== "tui" || !watchEnabled || !runtime || runtime.models.length === 0 || runtime.stats.paused) return;
|
|
113
|
+
// ponytail: pi passes a plain string here (omp forks expose string[]).
|
|
114
|
+
const base = typeof event?.systemPrompt === "string" ? event.systemPrompt : "";
|
|
115
|
+
// Every turn the agent sees the authority line (static per session,
|
|
116
|
+
// cache-safe): messages starting 'Advisor review' are reviewer findings.
|
|
117
|
+
const line = "Advisor notes: messages starting 'Advisor review' are authoritative reviewer findings. Fix or explicitly justify ignoring each finding.";
|
|
118
|
+
if (base.includes(line)) return;
|
|
119
|
+
return { systemPrompt: `${base}${base ? "\n\n" : ""}${line}` };
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
pi.on("session_start", (_event, ctx) => {
|
|
123
|
+
const sessionId = ctx.sessionManager.getSessionId?.();
|
|
124
|
+
const fresh = runtimeSessionId !== sessionId;
|
|
125
|
+
runtimeSessionId = sessionId;
|
|
126
|
+
cwd = ctx.cwd ?? process.cwd();
|
|
127
|
+
trusted = ctx.isProjectTrusted?.() === true;
|
|
128
|
+
if (runtime && !fresh) return; // same session — already initialized
|
|
129
|
+
settings = loadAdvisorSettings(cwd, trusted);
|
|
130
|
+
runtime = createRuntime(settings, settings.models);
|
|
131
|
+
watchEnabled = settings.enabled;
|
|
132
|
+
setAdvisorRegistry(ctx.modelRegistry);
|
|
133
|
+
// Watch is gated on both the master switch and a configured chain — no self-review.
|
|
134
|
+
if (!watchEnabled || settings.models.length === 0) return;
|
|
135
|
+
// Seed the cursor to the current transcript tail so the first review
|
|
136
|
+
// covers only work that happens after the advisor was loaded.
|
|
137
|
+
runtime.cursor = latestEntryId(ctx.sessionManager.getEntries() as any[]);
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
// Self-disarm on session teardown: session_shutdown is emitted and awaited
|
|
141
|
+
// BEFORE the runner invalidates (agent-session-runtime teardownCurrent), so
|
|
142
|
+
// flipping the flag here makes live() false in this (about-to-be-orphaned)
|
|
143
|
+
// closure — an in-flight fire-and-forget review is discarded silently
|
|
144
|
+
// instead of throwing the stale-ctx error into the .catch and toasting the
|
|
145
|
+
// NEW session. The factory re-runs per session, so this never touches a
|
|
146
|
+
// live session's flag.
|
|
147
|
+
pi.on("session_shutdown", () => { watchEnabled = false; });
|
|
148
|
+
|
|
149
|
+
// Transcript rewritten (compaction) or a different conversation becoming
|
|
150
|
+
// active (switch/resume): the cursor points at pre-rewrite entry ids and
|
|
151
|
+
// the guard's dedupe history covers notes against the OLD transcript —
|
|
152
|
+
// reseed both (omp parity: reviewer state resets across history rewrites).
|
|
153
|
+
// Skipped when the runtime belongs to a replaced session — that runtime's
|
|
154
|
+
// factory self-disarmed via session_shutdown.
|
|
155
|
+
function resetForRewrite(ctx: ExtensionContext): void {
|
|
156
|
+
if (!runtime || (runtimeSessionId !== undefined && runtimeSessionId !== ctx.sessionManager.getSessionId?.())) return;
|
|
157
|
+
reseedCursor(runtime, ctx);
|
|
158
|
+
runtime.guard = createGuard();
|
|
159
|
+
}
|
|
160
|
+
pi.on("session_compact", (_event, ctx) => resetForRewrite(ctx));
|
|
161
|
+
pi.on("session_before_switch", (_event, ctx) => resetForRewrite(ctx));
|
|
162
|
+
|
|
163
|
+
pi.on("agent_settled", async (_event, ctx) => {
|
|
164
|
+
if (!runtime || !watchEnabled || runtime.stats.paused) return;
|
|
165
|
+
// Only TUI: a floating review would die at process exit in headless
|
|
166
|
+
// modes, and a note there fired an unrequested follow-up run. Fail-safe:
|
|
167
|
+
// unknown/mode-less contexts skip too (missed review < surprise run).
|
|
168
|
+
if (ctx.mode !== "tui") return;
|
|
169
|
+
// ponytail: fire-and-forget — pi core awaits agent_settled handlers before
|
|
170
|
+
// the TUI regains input, so awaiting the 10-90s+ review here froze the UI
|
|
171
|
+
// after every turn. Notes deliver via sendUserMessage, which the SDK
|
|
172
|
+
// queues as a steer when a run is active or fires as a follow-up turn
|
|
173
|
+
// when idle. A still-running review makes the next settle skip (rt
|
|
174
|
+
// reviewing flag) — bounded loss, not queued.
|
|
175
|
+
const rt = runtime;
|
|
176
|
+
// Liveness guard evaluated at delivery time (not just settle time): a
|
|
177
|
+
// fresh session (/new) replaces runtime, /advisor off clears the chain,
|
|
178
|
+
// watch-off flips the flag, repeated failures pause — a review in flight
|
|
179
|
+
// across any of these must deliver nothing.
|
|
180
|
+
const live = () => runtime === rt && watchEnabled && rt.models.length > 0 && !rt.stats.paused;
|
|
181
|
+
void reviewTurn(rt, ctx, {
|
|
182
|
+
sendMessage: (message, options) => { if (live()) pi.sendMessage(message, options as never); },
|
|
183
|
+
sendUserMessage: (content, options) => { if (live()) pi.sendUserMessage(content, options); },
|
|
184
|
+
appendEntry: (customType, data) => { if (live()) pi.appendEntry(customType, data); },
|
|
185
|
+
notify: (message) => { if (live()) ctx.ui.notify(message, "error"); },
|
|
186
|
+
}, testIsolated).catch((err) => { if (live()) ctx.ui.notify(`Advisor review failed: ${String(err)}`, "error"); });
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
handle = registerAdvisor(pi, {
|
|
190
|
+
getSettings: () => settings,
|
|
191
|
+
isEnabled: () => settings.enabled,
|
|
192
|
+
isWatchEnabled: () => watchEnabled,
|
|
193
|
+
setWatchEnabled: (value) => { watchEnabled = value; },
|
|
194
|
+
getRuntime: () => runtime,
|
|
195
|
+
onEnableWatch: (ctx) => { if (runtime) reseedCursor(runtime, ctx); },
|
|
196
|
+
setModels: (models, ctx) => {
|
|
197
|
+
const previous = settings.models;
|
|
198
|
+
settings = { ...settings, models };
|
|
199
|
+
writeAdvisorSettings(settings);
|
|
200
|
+
if (!runtime) return;
|
|
201
|
+
runtime.config = settings;
|
|
202
|
+
runtime.models = models;
|
|
203
|
+
// Chain enabled mid-session: review only future work.
|
|
204
|
+
if (previous.length === 0 && models.length > 0 && watchEnabled) reseedCursor(runtime, ctx);
|
|
205
|
+
},
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
// /config bridge: the panel reads effective settings and, on save, applies
|
|
209
|
+
// them here — the section edits need no /reload (munin/classifier precedent).
|
|
210
|
+
setAdvisorBridge({
|
|
211
|
+
read: () => loadAdvisorSettings(cwd, trusted),
|
|
212
|
+
apply: (next, ctx) => {
|
|
213
|
+
applySettings(next, ctx);
|
|
214
|
+
handle?.sync(ctx);
|
|
215
|
+
},
|
|
216
|
+
});
|
|
217
|
+
}
|
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* advisor settings — ceulen module `advisor`, ported from pi-advisor 0.3.8.
|
|
3
|
+
*
|
|
4
|
+
* ~/.pi/agent/settings.json (write target, always)
|
|
5
|
+
* <cwd>/.pi/settings.json (read only when the project is trusted)
|
|
6
|
+
*
|
|
7
|
+
* "advisor": {
|
|
8
|
+
* "enabled": true,
|
|
9
|
+
* "models": ["router/zai/glm-5.3-flash:high", "opencode-go/deepseek-v4-pro"],
|
|
10
|
+
* "watch": { "minToolCalls": 3, "immuneTurns": 3 }
|
|
11
|
+
* }
|
|
12
|
+
*
|
|
13
|
+
* The standalone package's `pi-advisor` section is a READ-ONLY alias (new name
|
|
14
|
+
* wins per field inside a file); the first save deletes it plus the legacy
|
|
15
|
+
* `model` string and `watch.enabled` key, so nothing can shadow the new state
|
|
16
|
+
* (pi-advisor's own rule for its legacy `model` key). Upstream's `watch.enabled`
|
|
17
|
+
* — a second, finer-grained suppression — is folded into `enabled` at read time
|
|
18
|
+
* so the runtime has exactly ONE switch.
|
|
19
|
+
*
|
|
20
|
+
* The pi-plan `advisorModel` one-shot migration is deliberately NOT ported:
|
|
21
|
+
* pi-plan gets its own port and owns that key's migration then.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs";
|
|
25
|
+
import { homedir } from "node:os";
|
|
26
|
+
import { dirname, join } from "node:path";
|
|
27
|
+
import { CONFIG_DIR_NAME } from "@earendil-works/pi-coding-agent";
|
|
28
|
+
|
|
29
|
+
export const SECTION = "advisor";
|
|
30
|
+
export const LEGACY_SECTION = "pi-advisor";
|
|
31
|
+
|
|
32
|
+
export interface AdvisorWatchConfig {
|
|
33
|
+
/** Trivial-turn gate: a review needs at least this many new tool calls. */
|
|
34
|
+
minToolCalls: number;
|
|
35
|
+
/** Post-steer calm-down window in settled turns (nit deferral + dedupe window). */
|
|
36
|
+
immuneTurns: number;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface AdvisorConfig {
|
|
40
|
+
/** Master switch: background review + the on-demand advisor tool. */
|
|
41
|
+
enabled: boolean;
|
|
42
|
+
/** Ordered fallback chain; empty = advisor off. First entry is primary. */
|
|
43
|
+
models: string[];
|
|
44
|
+
watch: AdvisorWatchConfig;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export const DEFAULT_WATCH: AdvisorWatchConfig = { minToolCalls: 3, immuneTurns: 3 };
|
|
48
|
+
export const DEFAULTS: AdvisorConfig = { enabled: true, models: [], watch: DEFAULT_WATCH };
|
|
49
|
+
|
|
50
|
+
type Raw = Record<string, unknown>;
|
|
51
|
+
|
|
52
|
+
/** Global agent-dir settings.json — ceulen's agent dir convention. */
|
|
53
|
+
export function agentSettingsPath(): string {
|
|
54
|
+
const dir = process.env.PI_CODING_AGENT_DIR || join(homedir(), CONFIG_DIR_NAME, "agent");
|
|
55
|
+
return join(dir, "settings.json");
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Project settings file (read only when trusted — an untrusted checkout must
|
|
59
|
+
* not redirect the advisor's model, i.e. where the primary's key is spent). */
|
|
60
|
+
export function projectSettingsPath(cwd: string): string {
|
|
61
|
+
return join(cwd, CONFIG_DIR_NAME, "settings.json");
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function readJson(file: string): Raw {
|
|
65
|
+
try {
|
|
66
|
+
const value = JSON.parse(readFileSync(file, "utf8")) as Raw;
|
|
67
|
+
return value && typeof value === "object" ? value : {};
|
|
68
|
+
} catch {
|
|
69
|
+
return {}; // missing/unreadable → no layer
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const str = (raw: Raw, key: string): string | undefined => {
|
|
74
|
+
const item = raw[key];
|
|
75
|
+
return typeof item === "string" && item.trim() ? item.trim() : undefined;
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
const num = (raw: Raw, key: string): number | undefined => {
|
|
79
|
+
const item = raw[key];
|
|
80
|
+
return typeof item === "number" && Number.isFinite(item) && item >= 0 ? item : undefined;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
/** Normalize a chain value (array or comma string) into trimmed, deduped entries. */
|
|
84
|
+
function parseModels(raw: Raw): string[] | undefined {
|
|
85
|
+
const value = raw.models;
|
|
86
|
+
if (value === undefined) return undefined;
|
|
87
|
+
const entries = typeof value === "string" ? value.split(",") : Array.isArray(value) ? value : [];
|
|
88
|
+
const models = entries.map((entry) => String(entry).trim()).filter(Boolean);
|
|
89
|
+
return [...new Set(models)];
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** One section object (either name) → the flat field set we resolve over. */
|
|
93
|
+
interface Parsed {
|
|
94
|
+
enabled?: boolean;
|
|
95
|
+
models?: string[];
|
|
96
|
+
/** Legacy single-model string (wraps to a one-entry chain). */
|
|
97
|
+
model?: string;
|
|
98
|
+
/** Legacy `watch.enabled` — folded into `enabled` by resolveSettings. */
|
|
99
|
+
watchEnabled?: boolean;
|
|
100
|
+
minToolCalls?: number;
|
|
101
|
+
immuneTurns?: number;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export function parseSection(raw: unknown): Parsed {
|
|
105
|
+
const s = raw && typeof raw === "object" ? (raw as Raw) : {};
|
|
106
|
+
const watch = s.watch && typeof s.watch === "object" ? (s.watch as Raw) : {};
|
|
107
|
+
const out: Parsed = {};
|
|
108
|
+
if (typeof s.enabled === "boolean") out.enabled = s.enabled;
|
|
109
|
+
const models = parseModels(s);
|
|
110
|
+
if (models !== undefined) out.models = models;
|
|
111
|
+
const model = str(s, "model");
|
|
112
|
+
if (model) out.model = model;
|
|
113
|
+
if (typeof watch.enabled === "boolean") out.watchEnabled = watch.enabled;
|
|
114
|
+
const minToolCalls = num(watch, "minToolCalls");
|
|
115
|
+
if (minToolCalls !== undefined) out.minToolCalls = minToolCalls;
|
|
116
|
+
const immuneTurns = num(watch, "immuneTurns");
|
|
117
|
+
if (immuneTurns !== undefined) out.immuneTurns = immuneTurns;
|
|
118
|
+
return out;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** Only-defined-keys merge (an explicit `undefined` never erases a lower layer). */
|
|
122
|
+
function over(base: Parsed, layer: Parsed): Parsed {
|
|
123
|
+
const out = { ...base };
|
|
124
|
+
for (const key of Object.keys(layer) as (keyof Parsed)[]) {
|
|
125
|
+
if (layer[key] !== undefined) (out as Raw)[key] = layer[key];
|
|
126
|
+
}
|
|
127
|
+
return out;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** One settings file's effective section: current name wins over the legacy alias. */
|
|
131
|
+
export function readLayer(settings: Raw): Parsed {
|
|
132
|
+
return over(parseSection(settings[LEGACY_SECTION]), parseSection(settings[SECTION]));
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** Resolve layers in increasing precedence (global, then trusted project). */
|
|
136
|
+
export function resolveSettings(layers: Parsed[]): AdvisorConfig {
|
|
137
|
+
const merged = layers.reduce(over, {} as Parsed);
|
|
138
|
+
const models = merged.models && merged.models.length > 0 ? merged.models : merged.model ? [merged.model] : [];
|
|
139
|
+
return {
|
|
140
|
+
// Legacy `watch.enabled` folds into the master switch — one switch at runtime.
|
|
141
|
+
enabled: (merged.enabled ?? true) && (merged.watchEnabled ?? true),
|
|
142
|
+
models,
|
|
143
|
+
watch: {
|
|
144
|
+
minToolCalls: merged.minToolCalls ?? DEFAULT_WATCH.minToolCalls,
|
|
145
|
+
immuneTurns: merged.immuneTurns ?? DEFAULT_WATCH.immuneTurns,
|
|
146
|
+
},
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** Effective settings: global file, then the trusted project file over it. */
|
|
151
|
+
export function loadAdvisorSettings(cwd: string = process.cwd(), trusted = false): AdvisorConfig {
|
|
152
|
+
const layers: Parsed[] = [readLayer(readJson(agentSettingsPath()))];
|
|
153
|
+
if (trusted) layers.push(readLayer(readJson(projectSettingsPath(cwd))));
|
|
154
|
+
return resolveSettings(layers);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** Which advisor sections a trusted project file sets — the panel/command warn
|
|
158
|
+
* that the project layer overrides a global save. Empty = no shadowing. */
|
|
159
|
+
export function projectShadow(cwd: string, trusted: boolean): string[] {
|
|
160
|
+
if (!trusted) return [];
|
|
161
|
+
const file = projectSettingsPath(cwd);
|
|
162
|
+
if (!existsSync(file)) return [];
|
|
163
|
+
const raw = readJson(file);
|
|
164
|
+
return [LEGACY_SECTION, SECTION].filter((key) => raw[key] !== undefined);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** Full-section write to the agent-dir settings.json (merge, never clobber;
|
|
168
|
+
* atomic tmp+rename; a corrupt file refuses to clobber). The caller passes the
|
|
169
|
+
* resolved settings, so deleting the folded legacy keys loses nothing. */
|
|
170
|
+
export function writeAdvisorSettings(settings: AdvisorConfig): string {
|
|
171
|
+
const file = agentSettingsPath();
|
|
172
|
+
let raw: Raw;
|
|
173
|
+
try {
|
|
174
|
+
raw = existsSync(file) ? (JSON.parse(readFileSync(file, "utf8")) as Raw) : {};
|
|
175
|
+
} catch {
|
|
176
|
+
throw new Error(`${file} is not valid JSON — fix or remove it before saving.`);
|
|
177
|
+
}
|
|
178
|
+
delete raw[LEGACY_SECTION];
|
|
179
|
+
const section = (raw[SECTION] && typeof raw[SECTION] === "object" ? (raw[SECTION] as Raw) : {});
|
|
180
|
+
section.enabled = settings.enabled;
|
|
181
|
+
const models = [...new Set(settings.models.map((m) => m.trim()).filter(Boolean))];
|
|
182
|
+
if (models.length > 0) section.models = models;
|
|
183
|
+
else delete section.models;
|
|
184
|
+
delete section.model; // legacy single-model string can never shadow a saved chain
|
|
185
|
+
const watch = (section.watch && typeof section.watch === "object" ? (section.watch as Raw) : {});
|
|
186
|
+
watch.minToolCalls = settings.watch.minToolCalls;
|
|
187
|
+
watch.immuneTurns = settings.watch.immuneTurns;
|
|
188
|
+
delete watch.enabled; // legacy suppression, folded into `enabled` above
|
|
189
|
+
section.watch = watch;
|
|
190
|
+
raw[SECTION] = section;
|
|
191
|
+
mkdirSync(dirname(file), { recursive: true });
|
|
192
|
+
const tmp = file + ".tmp";
|
|
193
|
+
writeFileSync(tmp, JSON.stringify(raw, null, 2) + "\n", { mode: 0o600 });
|
|
194
|
+
renameSync(tmp, file);
|
|
195
|
+
return file;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
export function parseModel(value: string): { provider: string; id: string } | undefined {
|
|
199
|
+
const slash = value.indexOf("/");
|
|
200
|
+
if (slash <= 0 || slash === value.length - 1) return undefined;
|
|
201
|
+
return { provider: value.slice(0, slash), id: value.slice(slash + 1) };
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** Thinking levels accepted as a trailing `:level` on a chain entry.
|
|
205
|
+
* `off` maps to "no explicit level" (SimpleStreamOptions.reasoning has no off). */
|
|
206
|
+
export const THINKING_LEVELS: readonly string[] = ["off", "minimal", "low", "medium", "high", "xhigh", "max"];
|
|
207
|
+
|
|
208
|
+
/** Split a trailing `:thinking` suffix. Strict trailing match only — openrouter
|
|
209
|
+
* ids like `model:free` stay intact. */
|
|
210
|
+
export function splitThinkingSuffix(entry: string): { name: string; thinking?: string } {
|
|
211
|
+
const idx = entry.lastIndexOf(":");
|
|
212
|
+
if (idx <= 0 || idx >= entry.length - 1) return { name: entry };
|
|
213
|
+
const suffix = entry.slice(idx + 1);
|
|
214
|
+
if (!THINKING_LEVELS.includes(suffix)) return { name: entry };
|
|
215
|
+
return { name: entry.slice(0, idx), thinking: suffix };
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/** The three `/config` model rows ⇄ the saved chain. */
|
|
219
|
+
export interface AdvisorChain {
|
|
220
|
+
/** Primary `provider/id`. */
|
|
221
|
+
model: string;
|
|
222
|
+
/** Pinned thinking for the primary (`""` = model default, `off` = no suffix). */
|
|
223
|
+
thinking: string;
|
|
224
|
+
/** Comma-separated `provider/id[:level]` fallbacks, in priority order. */
|
|
225
|
+
fallbacks: string;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
export function chainFromSettings(models: readonly string[]): AdvisorChain {
|
|
229
|
+
const [primary = "", ...rest] = models;
|
|
230
|
+
const { name, thinking } = splitThinkingSuffix(String(primary).trim());
|
|
231
|
+
return {
|
|
232
|
+
model: name,
|
|
233
|
+
thinking: thinking && thinking !== "off" ? thinking : "",
|
|
234
|
+
fallbacks: rest.map((m) => String(m).trim()).filter(Boolean).join(", "),
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
export function settingsFromChain(chain: AdvisorChain): string[] {
|
|
239
|
+
const out: string[] = [];
|
|
240
|
+
const model = chain.model.trim();
|
|
241
|
+
const thinking = chain.thinking.trim();
|
|
242
|
+
if (model) out.push(thinking && thinking !== "off" ? `${model}:${thinking}` : model);
|
|
243
|
+
for (const entry of chain.fallbacks.split(",")) {
|
|
244
|
+
const trimmed = entry.trim();
|
|
245
|
+
if (trimmed) out.push(trimmed);
|
|
246
|
+
}
|
|
247
|
+
return [...new Set(out)];
|
|
248
|
+
}
|