pi-advisor-flow 0.15.1 → 0.15.2-dev.1
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 +5 -0
- package/README.md +16 -1
- package/dist/index.js +1157 -502
- package/docs/README.md +9 -0
- package/docs/benchmark.md +22 -0
- package/docs/configuration.md +194 -0
- package/docs/development.md +38 -0
- package/docs/herdr.md +169 -0
- package/docs/privacy.md +102 -0
- package/extensions/index.ts +23 -0
- package/locales/de.json +330 -0
- package/locales/en.json +331 -0
- package/locales/es.json +3 -0
- package/locales/fr.json +330 -0
- package/locales/pt-BR.json +3 -0
- package/locales/pt.json +3 -0
- package/locales/ru.json +3 -0
- package/locales/uk.json +3 -0
- package/locales/zh.json +3 -0
- package/package.json +15 -2
- package/skills/pi-advisor-flow/SKILL.md +62 -0
- package/src/commands/activation-preparation.ts +9 -2
- package/src/commands/activation.ts +28 -8
- package/src/commands/configuration-tool-models.ts +143 -0
- package/src/commands/configuration-tool-policy.ts +177 -0
- package/src/commands/configuration-tool-preview.ts +228 -0
- package/src/commands/configuration-tool-runtime.ts +40 -0
- package/src/commands/configuration-tool-transaction.ts +120 -0
- package/src/commands/configuration-tool-update.ts +230 -0
- package/src/commands/configuration-tool.ts +89 -0
- package/src/commands/lifecycle.ts +6 -1
- package/src/commands/manual-consultation.ts +6 -1
- package/src/commands/manual-progress.ts +4 -3
- package/src/commands/model-commands.ts +2 -1
- package/src/commands/model-options.ts +4 -6
- package/src/commands/model-picker.ts +16 -6
- package/src/commands/registration.ts +2 -0
- package/src/commands/runtime.ts +17 -4
- package/src/commands/settings-commands.ts +30 -7
- package/src/config/schema-validators.ts +104 -0
- package/src/config/schema.ts +35 -102
- package/src/config/storage.ts +160 -7
- package/src/i18n.ts +31 -0
- package/src/scout-group-serialization.ts +66 -0
- package/src/scout-groups.ts +3 -66
- package/src/scout-selection.ts +99 -0
- package/src/scout.ts +6 -93
- package/src/session-state.ts +27 -137
- package/src/tool-input-normalization.ts +119 -0
- package/src/tools/gate-policy.ts +13 -5
- package/src/tools/jev-filter.ts +2 -1
- package/src/tools/jev-turn-gate.ts +2 -1
- package/src/tools/register-lifecycle.ts +5 -1
- package/src/tools/register-outcome.ts +5 -1
- package/src/tools/register-renderers.ts +5 -2
- package/src/tools/render-advisor-result.ts +20 -8
- package/src/tools/render-common.ts +16 -4
- package/src/tools/scout-status.ts +10 -2
- package/src/ui/jev-classifier-submenu.ts +143 -38
- package/src/ui/jev-filter-submenu.ts +52 -14
- package/src/ui/jev-provider-submenu-view.ts +72 -20
- package/src/ui/jev-provider-submenu.ts +40 -12
- package/src/ui/jev-setup-support.ts +48 -19
- package/src/ui/manual-dialog-render.ts +71 -25
- package/src/ui/searchable-model-list.ts +18 -4
- package/src/ui/settings-formatting.ts +5 -2
- package/src/ui/settings-items-jev.ts +216 -0
- package/src/ui/settings-items.ts +216 -259
- package/src/ui/settings-selector.ts +9 -2
- package/src/ui/text-setting-submenu.ts +8 -1
- package/src/usage.ts +9 -3
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import type { AdvisorConfig } from "../config/types.ts";
|
|
2
|
+
import type { RecordValue } from "../content-utils.ts";
|
|
3
|
+
import type { AdvisorConfigPatch } from "./configuration-tool-policy.ts";
|
|
4
|
+
import { sameConfigValue } from "./configuration-tool-preview.ts";
|
|
5
|
+
import { getConfiguredModelRefs } from "./model-options.ts";
|
|
6
|
+
import { selectAdvisorModels } from "./model-picker.ts";
|
|
7
|
+
import type {
|
|
8
|
+
AdvisorModelPickerOptions,
|
|
9
|
+
AdvisorModelSelection,
|
|
10
|
+
} from "./model-picker.ts";
|
|
11
|
+
|
|
12
|
+
const MODEL_KEYS = [
|
|
13
|
+
"advisor",
|
|
14
|
+
"advisorEffort",
|
|
15
|
+
"advisorFallbackModel",
|
|
16
|
+
"executor",
|
|
17
|
+
"executorEffort",
|
|
18
|
+
] as const satisfies readonly (keyof AdvisorConfig)[];
|
|
19
|
+
|
|
20
|
+
type ModelKey = (typeof MODEL_KEYS)[number];
|
|
21
|
+
|
|
22
|
+
type ModelRefKey = "advisor" | "advisorFallbackModel" | "executor";
|
|
23
|
+
|
|
24
|
+
const MODEL_REF_KEYS: readonly ModelRefKey[] = [
|
|
25
|
+
"advisor",
|
|
26
|
+
"advisorFallbackModel",
|
|
27
|
+
"executor",
|
|
28
|
+
];
|
|
29
|
+
|
|
30
|
+
export type SelectAdvisorModels = typeof selectAdvisorModels;
|
|
31
|
+
|
|
32
|
+
const isModelChange = (changes: AdvisorConfigPatch) =>
|
|
33
|
+
MODEL_KEYS.some((key) => Object.hasOwn(changes, key));
|
|
34
|
+
|
|
35
|
+
const validateRequestedModels = (
|
|
36
|
+
changes: AdvisorConfigPatch,
|
|
37
|
+
modelRefs: string[]
|
|
38
|
+
) => {
|
|
39
|
+
for (const key of MODEL_REF_KEYS) {
|
|
40
|
+
const requested = changes[key];
|
|
41
|
+
|
|
42
|
+
if (
|
|
43
|
+
requested !== null &&
|
|
44
|
+
requested !== undefined &&
|
|
45
|
+
!modelRefs.includes(requested)
|
|
46
|
+
) {
|
|
47
|
+
throw new Error(
|
|
48
|
+
`${key} must name an available provider/model. Choose it from /advisor-models or use the model picker opened by advisor_config.`
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
const pickerOptions = (
|
|
55
|
+
changes: AdvisorConfigPatch,
|
|
56
|
+
before: AdvisorConfig
|
|
57
|
+
): AdvisorModelPickerOptions => ({
|
|
58
|
+
advisor: changes.advisor ?? before.advisor,
|
|
59
|
+
advisorEffort:
|
|
60
|
+
changes.advisorEffort === null
|
|
61
|
+
? undefined
|
|
62
|
+
: (changes.advisorEffort ?? before.advisorEffort),
|
|
63
|
+
advisorFallbackModel:
|
|
64
|
+
changes.advisorFallbackModel === null
|
|
65
|
+
? undefined
|
|
66
|
+
: (changes.advisorFallbackModel ?? before.advisorFallbackModel),
|
|
67
|
+
executor:
|
|
68
|
+
changes.executor === null ? "" : (changes.executor ?? before.executor),
|
|
69
|
+
executorEffort:
|
|
70
|
+
changes.executorEffort === null
|
|
71
|
+
? undefined
|
|
72
|
+
: (changes.executorEffort ?? before.executorEffort),
|
|
73
|
+
selectAdvisor:
|
|
74
|
+
Object.hasOwn(changes, "advisor") ||
|
|
75
|
+
Object.hasOwn(changes, "advisorEffort") ||
|
|
76
|
+
!before.advisor,
|
|
77
|
+
selectExecutor:
|
|
78
|
+
Object.hasOwn(changes, "executor") ||
|
|
79
|
+
Object.hasOwn(changes, "executorEffort"),
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
const selectedModelChanges = (
|
|
83
|
+
changes: AdvisorConfigPatch,
|
|
84
|
+
selection: AdvisorModelSelection,
|
|
85
|
+
persisted: RecordValue
|
|
86
|
+
): AdvisorConfigPatch => {
|
|
87
|
+
const selected = {
|
|
88
|
+
advisor: selection.advisor,
|
|
89
|
+
advisorEffort: selection.advisorEffort ?? null,
|
|
90
|
+
advisorFallbackModel: selection.advisorFallbackModel ?? null,
|
|
91
|
+
executor: selection.executor || null,
|
|
92
|
+
executorEffort: selection.executorEffort ?? null,
|
|
93
|
+
} satisfies Record<ModelKey, string | null>;
|
|
94
|
+
|
|
95
|
+
const resolved: Partial<Record<ModelKey, string | null>> = {};
|
|
96
|
+
|
|
97
|
+
for (const key of MODEL_KEYS) {
|
|
98
|
+
const persistedValue = persisted[key] ?? null;
|
|
99
|
+
|
|
100
|
+
if (!sameConfigValue(selected[key], persistedValue)) {
|
|
101
|
+
resolved[key] = selected[key];
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const {
|
|
106
|
+
advisor: _requestedAdvisor,
|
|
107
|
+
advisorEffort: _requestedAdvisorEffort,
|
|
108
|
+
advisorFallbackModel: _requestedFallback,
|
|
109
|
+
executor: _requestedExecutor,
|
|
110
|
+
executorEffort: _requestedExecutorEffort,
|
|
111
|
+
...otherChanges
|
|
112
|
+
} = changes;
|
|
113
|
+
|
|
114
|
+
return { ...otherChanges, ...resolved };
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
export const resolveModelChanges = async (
|
|
118
|
+
ctx: Parameters<typeof selectAdvisorModels>[0],
|
|
119
|
+
changes: AdvisorConfigPatch,
|
|
120
|
+
before: AdvisorConfig,
|
|
121
|
+
persisted: RecordValue,
|
|
122
|
+
selectModels: SelectAdvisorModels = selectAdvisorModels
|
|
123
|
+
): Promise<AdvisorConfigPatch | undefined> => {
|
|
124
|
+
if (!isModelChange(changes)) {
|
|
125
|
+
return changes;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
if (ctx.mode !== "tui") {
|
|
129
|
+
throw new Error(
|
|
130
|
+
"Model changes require Pi's interactive TUI so you can choose from the existing model picker. No settings were changed."
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
validateRequestedModels(changes, getConfiguredModelRefs(ctx));
|
|
135
|
+
|
|
136
|
+
const selection = await selectModels(ctx, pickerOptions(changes, before));
|
|
137
|
+
|
|
138
|
+
if (!selection) {
|
|
139
|
+
return undefined;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
return selectedModelChanges(changes, selection, persisted);
|
|
143
|
+
};
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { CONFIG_SCHEMA, configKeys } from "../config/schema.ts";
|
|
2
|
+
import type { ConfigKey } from "../config/schema.ts";
|
|
3
|
+
import type { AdvisorConfig } from "../config/types.ts";
|
|
4
|
+
import { validateConfig } from "../config/validation.ts";
|
|
5
|
+
import { isNumber, isRecord, isString } from "../content-utils.ts";
|
|
6
|
+
import type { JsonValue, RecordValue } from "../content-utils.ts";
|
|
7
|
+
|
|
8
|
+
const UI_ONLY_KEYS = new Set<ConfigKey>([
|
|
9
|
+
"advisorJevBaseUrl",
|
|
10
|
+
"advisorJevClassifier",
|
|
11
|
+
"advisorJevKeyProvider",
|
|
12
|
+
"advisorJevModel",
|
|
13
|
+
"advisorJevPricePerMtok",
|
|
14
|
+
"advisorJevTransport",
|
|
15
|
+
]);
|
|
16
|
+
|
|
17
|
+
const WRITABLE_KEYS = new Set<string>(
|
|
18
|
+
configKeys.filter((key) => !UI_ONLY_KEYS.has(key))
|
|
19
|
+
);
|
|
20
|
+
|
|
21
|
+
export const ADVISOR_CONFIG_MODEL_KEYS = [
|
|
22
|
+
"advisor",
|
|
23
|
+
"advisorEffort",
|
|
24
|
+
"advisorFallbackModel",
|
|
25
|
+
"executor",
|
|
26
|
+
"executorEffort",
|
|
27
|
+
] as const;
|
|
28
|
+
|
|
29
|
+
export const ADVISOR_CONFIG_EFFORT_LEVELS = [
|
|
30
|
+
"off",
|
|
31
|
+
"minimal",
|
|
32
|
+
"low",
|
|
33
|
+
"medium",
|
|
34
|
+
"high",
|
|
35
|
+
"xhigh",
|
|
36
|
+
"max",
|
|
37
|
+
] as const;
|
|
38
|
+
|
|
39
|
+
export const ADVISOR_CONFIG_WRITABLE_KEYS = configKeys.filter(
|
|
40
|
+
(key) => !UI_ONLY_KEYS.has(key)
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
export type AdvisorConfigPatch = {
|
|
44
|
+
[Key in keyof AdvisorConfig]?: AdvisorConfig[Key] | null;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export type AdvisorConfigSnapshot = AdvisorConfig;
|
|
48
|
+
|
|
49
|
+
export const getAdvisorConfigSnapshot = (): AdvisorConfigSnapshot => {
|
|
50
|
+
const snapshot = Object.fromEntries(
|
|
51
|
+
configKeys.map((key) => [key, CONFIG_SCHEMA[key].current()])
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
// SAFETY: CONFIG_SCHEMA is exhaustive over AdvisorConfig, so this entry list has the exact config keys.
|
|
55
|
+
return snapshot as AdvisorConfigSnapshot;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
export const getAdvisorConfigForAgent = (persisted: RecordValue) => {
|
|
59
|
+
const snapshot = getAdvisorConfigSnapshot();
|
|
60
|
+
const keys = configKeys.filter((key) => !UI_ONLY_KEYS.has(key));
|
|
61
|
+
|
|
62
|
+
const effectiveSettings = Object.fromEntries(
|
|
63
|
+
keys.map((key) => [key, snapshot[key] ?? null])
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
const globalOverrides = Object.fromEntries(
|
|
67
|
+
keys
|
|
68
|
+
.filter((key) => Object.hasOwn(persisted, key))
|
|
69
|
+
.map((key) => [key, persisted[key]])
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
return {
|
|
73
|
+
effectiveSettings,
|
|
74
|
+
globalOverrides,
|
|
75
|
+
jevClassifierConfigured: Boolean(persisted.advisorJevClassifier),
|
|
76
|
+
readOnlyKeys: [...UI_ONLY_KEYS].toSorted(),
|
|
77
|
+
writableKeys: [...ADVISOR_CONFIG_WRITABLE_KEYS].toSorted(),
|
|
78
|
+
};
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
export const validateAdvisorConfigPatch = (
|
|
82
|
+
value: JsonValue,
|
|
83
|
+
jevClassifierConfigured: boolean
|
|
84
|
+
): value is RecordValue & AdvisorConfigPatch => {
|
|
85
|
+
if (!isRecord(value)) {
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const keys = Object.keys(value);
|
|
90
|
+
|
|
91
|
+
if (keys.length === 0) {
|
|
92
|
+
throw new TypeError("Provide at least one Advisor setting to update.");
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (keys.length > 16) {
|
|
96
|
+
throw new TypeError("Update at most 16 Advisor settings at a time.");
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const unsupported = keys.filter((key) => !WRITABLE_KEYS.has(key));
|
|
100
|
+
|
|
101
|
+
if (unsupported.length > 0) {
|
|
102
|
+
throw new TypeError(
|
|
103
|
+
`These Advisor settings require their dedicated UI flow: ${unsupported.map((key) => JSON.stringify(key)).join(", ")}. Use /advisor-settings for classifier and credential setup.`
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const setValues: RecordValue = {};
|
|
108
|
+
|
|
109
|
+
for (const [key, setting] of Object.entries(value)) {
|
|
110
|
+
if (setting !== null) {
|
|
111
|
+
setValues[key] = setting;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (!validateConfig(setValues, "advisor_config changes")) {
|
|
116
|
+
return false;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const effortLevels: readonly string[] = ADVISOR_CONFIG_EFFORT_LEVELS;
|
|
120
|
+
|
|
121
|
+
for (const key of ["advisorEffort", "executorEffort"] as const) {
|
|
122
|
+
const effort = setValues[key];
|
|
123
|
+
|
|
124
|
+
if (isString(effort) && !effortLevels.includes(effort)) {
|
|
125
|
+
throw new TypeError(
|
|
126
|
+
`${key} must be one of ${ADVISOR_CONFIG_EFFORT_LEVELS.join(", ")}; use null for the model default.`
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if (value.advisor === null) {
|
|
132
|
+
throw new TypeError(
|
|
133
|
+
"The Advisor model cannot be cleared from chat. Choose another model with the picker or use /advisor-models."
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const turnGateEveryTurns = isNumber(setValues.advisorJevTurnGateEveryTurns)
|
|
138
|
+
? setValues.advisorJevTurnGateEveryTurns
|
|
139
|
+
: 0;
|
|
140
|
+
|
|
141
|
+
if (
|
|
142
|
+
(setValues.advisorJevFilterEnabled === true || turnGateEveryTurns > 0) &&
|
|
143
|
+
!jevClassifierConfigured
|
|
144
|
+
) {
|
|
145
|
+
throw new TypeError(
|
|
146
|
+
"Select and verify a Jev classifier in /advisor-settings before enabling Jev screening or the turn gate. No settings were changed."
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
return true;
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
export const addAdvisorConfigDependencies = (changes: AdvisorConfigPatch) => {
|
|
154
|
+
const normalized = { ...changes };
|
|
155
|
+
|
|
156
|
+
if (changes.advisorToolsEnabled === false) {
|
|
157
|
+
if (changes.alwaysOn === true) {
|
|
158
|
+
throw new TypeError(
|
|
159
|
+
"Advisor tools cannot be disabled while Always on is enabled in the same update. Set both to false."
|
|
160
|
+
);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
normalized.alwaysOn = false;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
if (changes.alwaysOn === true) {
|
|
167
|
+
if (changes.advisorToolsEnabled === false) {
|
|
168
|
+
throw new TypeError(
|
|
169
|
+
"Always on requires Advisor tools. Enable both or keep Always on off."
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
normalized.advisorToolsEnabled = true;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
return normalized;
|
|
177
|
+
};
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
import { configKeys } from "../config/schema.ts";
|
|
2
|
+
import type { ConfigKey } from "../config/schema.ts";
|
|
3
|
+
import type { JsonValue, RecordValue } from "../content-utils.ts";
|
|
4
|
+
import type {
|
|
5
|
+
AdvisorConfigPatch,
|
|
6
|
+
AdvisorConfigSnapshot,
|
|
7
|
+
} from "./configuration-tool-policy.ts";
|
|
8
|
+
|
|
9
|
+
export const sameConfigValue = (
|
|
10
|
+
left: JsonValue | undefined,
|
|
11
|
+
right: JsonValue | undefined
|
|
12
|
+
) => JSON.stringify(left) === JSON.stringify(right);
|
|
13
|
+
|
|
14
|
+
const orderedPatchEntries = (patch: AdvisorConfigPatch) =>
|
|
15
|
+
configKeys
|
|
16
|
+
.filter((key) => Object.hasOwn(patch, key))
|
|
17
|
+
.map((key) => [key, patch[key]]);
|
|
18
|
+
|
|
19
|
+
export const sameConfigPatch = (
|
|
20
|
+
left: AdvisorConfigPatch,
|
|
21
|
+
right: AdvisorConfigPatch
|
|
22
|
+
) =>
|
|
23
|
+
JSON.stringify(orderedPatchEntries(left)) ===
|
|
24
|
+
JSON.stringify(orderedPatchEntries(right));
|
|
25
|
+
|
|
26
|
+
export const effectiveChanges = (
|
|
27
|
+
changes: AdvisorConfigPatch,
|
|
28
|
+
persisted: RecordValue
|
|
29
|
+
): AdvisorConfigPatch => {
|
|
30
|
+
const entries: [string, JsonValue][] = [];
|
|
31
|
+
|
|
32
|
+
for (const key of configKeys) {
|
|
33
|
+
const value = changes[key];
|
|
34
|
+
|
|
35
|
+
if (value === undefined) {
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (value === null) {
|
|
40
|
+
if (Object.hasOwn(persisted, key)) {
|
|
41
|
+
entries.push([key, null]);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (!sameConfigValue(value, persisted[key] ?? null)) {
|
|
48
|
+
entries.push([key, value]);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// SAFETY: entries are built only from configKeys and the validated sparse patch.
|
|
53
|
+
return Object.fromEntries(entries) as AdvisorConfigPatch;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
export const staleCheckKeys = (changes: AdvisorConfigPatch): ConfigKey[] => {
|
|
57
|
+
const keys = new Set(configKeys.filter((key) => Object.hasOwn(changes, key)));
|
|
58
|
+
|
|
59
|
+
if (keys.has("advisorToolsEnabled") || keys.has("alwaysOn")) {
|
|
60
|
+
keys.add("advisorToolsEnabled");
|
|
61
|
+
keys.add("alwaysOn");
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (
|
|
65
|
+
keys.has("advisorJevFilterEnabled") ||
|
|
66
|
+
keys.has("advisorJevTurnGateEveryTurns")
|
|
67
|
+
) {
|
|
68
|
+
keys.add("advisorJevClassifier");
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return [...keys];
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
export const summarizeAdvisorConfigPatch = (
|
|
75
|
+
changes: AdvisorConfigPatch,
|
|
76
|
+
before: AdvisorConfigSnapshot,
|
|
77
|
+
persisted: RecordValue
|
|
78
|
+
) =>
|
|
79
|
+
configKeys
|
|
80
|
+
.filter((key) => Object.hasOwn(changes, key))
|
|
81
|
+
.map((key) => {
|
|
82
|
+
const value = changes[key];
|
|
83
|
+
|
|
84
|
+
const previous = Object.hasOwn(persisted, key)
|
|
85
|
+
? JSON.stringify(persisted[key])
|
|
86
|
+
: `unset (extension default; current session ${JSON.stringify(before[key] ?? null)})`;
|
|
87
|
+
|
|
88
|
+
const next =
|
|
89
|
+
value === null ? "reset to extension default" : JSON.stringify(value);
|
|
90
|
+
|
|
91
|
+
return `- ${key}: ${previous} → ${next}`;
|
|
92
|
+
})
|
|
93
|
+
.join("\n");
|
|
94
|
+
|
|
95
|
+
const automationRiskNotes = (
|
|
96
|
+
changes: AdvisorConfigPatch,
|
|
97
|
+
before: AdvisorConfigSnapshot
|
|
98
|
+
) => {
|
|
99
|
+
const notes: string[] = [];
|
|
100
|
+
|
|
101
|
+
if (changes.simpleMode === true) {
|
|
102
|
+
notes.push(
|
|
103
|
+
"Simple mode disables automatic gates, gate blocking, and configured call budgets; voluntary Advisor calls remain available."
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (changes.advisorToolsEnabled === false) {
|
|
108
|
+
notes.push(
|
|
109
|
+
"Advisor tools will be disabled immediately. advisor_config remains available to re-enable them."
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (changes.alwaysOn === true) {
|
|
114
|
+
notes.push("Always on enables automatic activation for future sessions.");
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (
|
|
118
|
+
changes.advisorPlanGate === true ||
|
|
119
|
+
changes.advisorFailureGate === true ||
|
|
120
|
+
changes.advisorCompletionGate === true ||
|
|
121
|
+
changes.advisorAutoLoopGate === true
|
|
122
|
+
) {
|
|
123
|
+
notes.push(
|
|
124
|
+
"Automatic gates may request extra Advisor calls; loop-gate failures can block a tool or session under the selected failure policy."
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
if (
|
|
129
|
+
changes.gateFailureMode === "block-session" ||
|
|
130
|
+
changes.advisorBlockOnBlocked === true
|
|
131
|
+
) {
|
|
132
|
+
notes.push("A blocked gate may stop the current session.");
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
if (changes.advisorToolsEnabled === true) {
|
|
136
|
+
notes.push(
|
|
137
|
+
before.alwaysOn === true || changes.alwaysOn === true
|
|
138
|
+
? "Advisor tools become available, and saved Always on will activate the flow in future sessions."
|
|
139
|
+
: "Advisor tools become available; this does not activate Always on."
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
return notes;
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
const disclosureRiskNotes = (changes: AdvisorConfigPatch) => {
|
|
147
|
+
const notes: string[] = [];
|
|
148
|
+
|
|
149
|
+
if (changes.advisorGitContext === "full") {
|
|
150
|
+
notes.push(
|
|
151
|
+
"Future Advisor requests may include repository patch contents."
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
if (changes.advisorTrackedFileContent === true) {
|
|
156
|
+
notes.push(
|
|
157
|
+
"This enables explicitly requested tracked file contents to be sent to the Advisor."
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
if (changes.advisorUntrackedContent === true) {
|
|
162
|
+
notes.push(
|
|
163
|
+
"This enables explicitly requested untracked file contents to be sent to the Advisor."
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
if (changes.advisorAgentsMdContext === true) {
|
|
168
|
+
notes.push(
|
|
169
|
+
"Trusted AGENTS.md instructions may be included in Advisor requests."
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (
|
|
174
|
+
changes.advisorToolPolicies &&
|
|
175
|
+
Object.values(changes.advisorToolPolicies).includes("full")
|
|
176
|
+
) {
|
|
177
|
+
notes.push(
|
|
178
|
+
"Tools set to full may send their arguments and result content to the Advisor."
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
if (changes.advisorRedactSecrets === false) {
|
|
183
|
+
notes.push(
|
|
184
|
+
"Credential-pattern redaction will be off for Advisor requests."
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
return notes;
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
const costRiskNotes = (changes: AdvisorConfigPatch) => {
|
|
192
|
+
const notes: string[] = [];
|
|
193
|
+
|
|
194
|
+
if (changes.advisorOutcomeLogging === true) {
|
|
195
|
+
notes.push(
|
|
196
|
+
"Outcome logging stores bounded adoption and validation metadata in the local ledger."
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
if (changes.advisorMaxCallsPerSession === null) {
|
|
201
|
+
notes.push(
|
|
202
|
+
"This removes the per-session Advisor call limit; calls may be unlimited."
|
|
203
|
+
);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
if ((changes.advisorJevTurnGateEveryTurns ?? 0) > 0) {
|
|
207
|
+
notes.push(
|
|
208
|
+
"The Jev turn gate can initiate proactive Advisor calls and add cost."
|
|
209
|
+
);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
return notes;
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
export const advisorConfigRiskNotes = (
|
|
216
|
+
changes: AdvisorConfigPatch,
|
|
217
|
+
before: AdvisorConfigSnapshot,
|
|
218
|
+
persisted: RecordValue
|
|
219
|
+
) => [
|
|
220
|
+
...automationRiskNotes(changes, {
|
|
221
|
+
...before,
|
|
222
|
+
alwaysOn: Object.hasOwn(persisted, "alwaysOn")
|
|
223
|
+
? persisted.alwaysOn === true
|
|
224
|
+
: before.alwaysOn,
|
|
225
|
+
}),
|
|
226
|
+
...disclosureRiskNotes(changes),
|
|
227
|
+
...costRiskNotes(changes),
|
|
228
|
+
];
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
|
|
3
|
+
import { getAdvisorSettings } from "../config/state.ts";
|
|
4
|
+
import { setAdvisorToolsActive } from "./advisor-tools.ts";
|
|
5
|
+
import type { CommandRuntime } from "./types.ts";
|
|
6
|
+
|
|
7
|
+
export const applyAdvisorConfigRuntimeSettings = (
|
|
8
|
+
runtime: CommandRuntime,
|
|
9
|
+
ctx: ExtensionContext,
|
|
10
|
+
previous: ReturnType<typeof getAdvisorSettings>
|
|
11
|
+
) => {
|
|
12
|
+
const next = getAdvisorSettings();
|
|
13
|
+
const shouldEnable = next.advisorToolsEnabled ?? true;
|
|
14
|
+
const toolsAreActive = runtime.pi.getActiveTools().includes("ask_advisor");
|
|
15
|
+
|
|
16
|
+
if (
|
|
17
|
+
previous.advisorToolsEnabled !== next.advisorToolsEnabled ||
|
|
18
|
+
toolsAreActive !== shouldEnable
|
|
19
|
+
) {
|
|
20
|
+
runtime.noteAdvisorToolsStateChange();
|
|
21
|
+
setAdvisorToolsActive(runtime.pi, shouldEnable);
|
|
22
|
+
|
|
23
|
+
if (!shouldEnable) {
|
|
24
|
+
runtime.resetSameModelNotice();
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
runtime.updateSameModelNotice(ctx);
|
|
29
|
+
runtime.updateAdvisorUsageStatus(ctx);
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export const cancelledAdvisorConfigResult = (message: string) => ({
|
|
33
|
+
content: [{ text: message, type: "text" as const }],
|
|
34
|
+
details: { action: "update", applied: false, cancelled: true },
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
export const unchangedAdvisorConfigResult = (message: string) => ({
|
|
38
|
+
content: [{ text: message, type: "text" as const }],
|
|
39
|
+
details: { action: "update", applied: false, unchanged: true },
|
|
40
|
+
});
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { withFileMutationQueue } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
3
|
+
|
|
4
|
+
import type { getAdvisorSettings } from "../config/state.ts";
|
|
5
|
+
import {
|
|
6
|
+
readGlobalAdvisorConfig,
|
|
7
|
+
saveGlobalAdvisorConfigPatch,
|
|
8
|
+
saveGlobalOutcomeLogging,
|
|
9
|
+
} from "../config/storage.ts";
|
|
10
|
+
import type { RecordValue } from "../content-utils.ts";
|
|
11
|
+
import { addAdvisorConfigDependencies } from "./configuration-tool-policy.ts";
|
|
12
|
+
import type {
|
|
13
|
+
AdvisorConfigPatch,
|
|
14
|
+
AdvisorConfigSnapshot,
|
|
15
|
+
} from "./configuration-tool-policy.ts";
|
|
16
|
+
import {
|
|
17
|
+
advisorConfigRiskNotes,
|
|
18
|
+
effectiveChanges,
|
|
19
|
+
sameConfigPatch,
|
|
20
|
+
sameConfigValue,
|
|
21
|
+
staleCheckKeys,
|
|
22
|
+
summarizeAdvisorConfigPatch,
|
|
23
|
+
} from "./configuration-tool-preview.ts";
|
|
24
|
+
|
|
25
|
+
export interface PreparedAdvisorConfigUpdate {
|
|
26
|
+
before: AdvisorConfigSnapshot;
|
|
27
|
+
beforeSettings: ReturnType<typeof getAdvisorSettings>;
|
|
28
|
+
configPath: string;
|
|
29
|
+
existing: RecordValue;
|
|
30
|
+
proposed: AdvisorConfigPatch;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export const confirmAdvisorConfigUpdate = async (
|
|
34
|
+
ctx: ExtensionContext,
|
|
35
|
+
prepared: PreparedAdvisorConfigUpdate,
|
|
36
|
+
signal?: AbortSignal
|
|
37
|
+
) => {
|
|
38
|
+
const preview = summarizeAdvisorConfigPatch(
|
|
39
|
+
prepared.proposed,
|
|
40
|
+
prepared.before,
|
|
41
|
+
prepared.existing
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
const risks = advisorConfigRiskNotes(
|
|
45
|
+
prepared.proposed,
|
|
46
|
+
prepared.before,
|
|
47
|
+
prepared.existing
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
const confirmation = [
|
|
51
|
+
"These changes apply to global Advisor settings, not a project config:",
|
|
52
|
+
preview,
|
|
53
|
+
...risks.map((risk) => `\nRisk: ${risk}`),
|
|
54
|
+
].join("\n");
|
|
55
|
+
|
|
56
|
+
const confirmed = await ctx.ui.confirm(
|
|
57
|
+
"Apply Advisor settings?",
|
|
58
|
+
confirmation,
|
|
59
|
+
{ signal }
|
|
60
|
+
);
|
|
61
|
+
|
|
62
|
+
return confirmed && !signal?.aborted;
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
export const persistAdvisorConfigUpdate = (
|
|
66
|
+
prepared: PreparedAdvisorConfigUpdate,
|
|
67
|
+
signal?: AbortSignal
|
|
68
|
+
) => {
|
|
69
|
+
const touched = staleCheckKeys(prepared.proposed);
|
|
70
|
+
|
|
71
|
+
return withFileMutationQueue(prepared.configPath, () =>
|
|
72
|
+
Promise.resolve().then(() => {
|
|
73
|
+
if (signal?.aborted) {
|
|
74
|
+
throw new Error(
|
|
75
|
+
"Update cancelled before writing; no settings changed."
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const latest = readGlobalAdvisorConfig();
|
|
80
|
+
|
|
81
|
+
const stale = touched.filter(
|
|
82
|
+
(key) =>
|
|
83
|
+
Object.hasOwn(latest, key) !==
|
|
84
|
+
Object.hasOwn(prepared.existing, key) ||
|
|
85
|
+
!sameConfigValue(latest[key] ?? null, prepared.existing[key] ?? null)
|
|
86
|
+
);
|
|
87
|
+
|
|
88
|
+
if (stale.length > 0) {
|
|
89
|
+
throw new Error(
|
|
90
|
+
`Advisor settings changed while confirmation was open (${stale.join(", ")}). Read the settings again and retry; nothing was written.`
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const refreshed = effectiveChanges(
|
|
95
|
+
addAdvisorConfigDependencies(prepared.proposed),
|
|
96
|
+
latest
|
|
97
|
+
);
|
|
98
|
+
|
|
99
|
+
if (!sameConfigPatch(refreshed, prepared.proposed)) {
|
|
100
|
+
throw new Error(
|
|
101
|
+
"Advisor setting dependencies changed while confirmation was open. Read the settings again and retry; nothing was written."
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (Object.hasOwn(refreshed, "advisorOutcomeLogging")) {
|
|
106
|
+
saveGlobalOutcomeLogging(
|
|
107
|
+
refreshed.advisorOutcomeLogging === null
|
|
108
|
+
? null
|
|
109
|
+
: refreshed.advisorOutcomeLogging === true,
|
|
110
|
+
prepared.existing,
|
|
111
|
+
touched
|
|
112
|
+
);
|
|
113
|
+
} else {
|
|
114
|
+
saveGlobalAdvisorConfigPatch(refreshed, prepared.existing, touched);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return prepared.configPath;
|
|
118
|
+
})
|
|
119
|
+
);
|
|
120
|
+
};
|