pi-advisor-flow 0.5.1 → 0.5.3

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.
Files changed (59) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/README.md +3 -2
  3. package/extensions/index.ts +7 -7
  4. package/package.json +2 -1
  5. package/src/commands/activation-preparation.ts +117 -0
  6. package/src/commands/activation.ts +135 -0
  7. package/src/commands/lifecycle.ts +83 -0
  8. package/src/commands/manual-command.ts +107 -0
  9. package/src/commands/manual-consultation.ts +144 -0
  10. package/src/commands/manual-progress.ts +93 -0
  11. package/src/commands/model-commands.ts +66 -0
  12. package/src/commands/model-options.ts +181 -0
  13. package/src/commands/model-picker.ts +110 -0
  14. package/src/commands/registration.ts +27 -0
  15. package/src/commands/renderers.ts +76 -0
  16. package/src/commands/runtime.ts +114 -0
  17. package/src/commands/settings-commands.ts +80 -0
  18. package/src/commands/settings-persistence.ts +81 -0
  19. package/src/commands/types.ts +78 -0
  20. package/src/commands.ts +9 -1259
  21. package/src/config/args.ts +37 -0
  22. package/src/config/defaults.ts +190 -0
  23. package/src/config/state.ts +199 -0
  24. package/src/config/storage.ts +224 -0
  25. package/src/config/types.ts +63 -0
  26. package/src/config/validation.ts +234 -0
  27. package/src/config.ts +104 -814
  28. package/src/conversation.ts +4 -2
  29. package/src/herdr.ts +1 -1
  30. package/src/model-stream.ts +15 -1
  31. package/src/scout-context.ts +2 -2
  32. package/src/scout.ts +1 -1
  33. package/src/tools/consultation.ts +337 -0
  34. package/src/tools/gate-policy.ts +132 -0
  35. package/src/tools/gate-protocol.ts +127 -0
  36. package/src/tools/loop-gate.ts +210 -0
  37. package/src/tools/prompts.ts +159 -0
  38. package/src/tools/register-ask-advisor.ts +247 -0
  39. package/src/tools/register-lifecycle.ts +119 -0
  40. package/src/tools/register-outcome.ts +75 -0
  41. package/src/tools/register-renderers.ts +88 -0
  42. package/src/tools/registration.ts +35 -0
  43. package/src/tools/render-advisor-result.ts +168 -0
  44. package/src/tools/render-common.ts +120 -0
  45. package/src/tools/scout-status.ts +206 -0
  46. package/src/tools/session.ts +3 -0
  47. package/src/tools/types.ts +119 -0
  48. package/src/tools.ts +42 -1865
  49. package/src/ui/manual-dialog-render.ts +150 -0
  50. package/src/ui/manual-dialog.ts +305 -0
  51. package/src/ui/model-selector.ts +166 -0
  52. package/src/ui/settings-formatting.ts +133 -0
  53. package/src/ui/settings-items.ts +323 -0
  54. package/src/ui/settings-list-adapter.ts +103 -0
  55. package/src/ui/settings-mutations.ts +80 -0
  56. package/src/ui/settings-selector.ts +161 -0
  57. package/src/ui/text-setting-submenu.ts +66 -0
  58. package/src/ui/types.ts +111 -0
  59. package/src/ui.ts +9 -1418
@@ -0,0 +1,206 @@
1
+ import type {
2
+ ExtensionAPI,
3
+ ExtensionContext,
4
+ Theme,
5
+ } from "@earendil-works/pi-coding-agent";
6
+ import { type Box, Text } from "@earendil-works/pi-tui";
7
+ import { executorRef, getAdvisorSettings } from "../config/state.js";
8
+ import type { ScoutLifecycleEvent } from "../scout.js";
9
+ import { formatAdvisorUsage, snapshotAdvisorUsage } from "../usage.js";
10
+ import { renderThinkingMarkdown, SPINNER_FRAMES } from "./render-common.js";
11
+ import type { ScoutToolDetails } from "./types.js";
12
+
13
+ export const scoutDetailsFromEvent = (
14
+ event: ScoutLifecycleEvent,
15
+ previous?: ScoutToolDetails
16
+ ): ScoutToolDetails => {
17
+ if (event.type === "call") {
18
+ return { model: event.model, status: "calling" };
19
+ }
20
+ if (event.type === "chunk") {
21
+ return {
22
+ ...previous,
23
+ model: event.model,
24
+ status: "streaming",
25
+ text: event.text,
26
+ thinking: event.thinking,
27
+ };
28
+ }
29
+ if (event.type === "cancelled") {
30
+ return {
31
+ model: previous ? previous.model : executorRef,
32
+ status: "cancelled",
33
+ };
34
+ }
35
+ const { outcome } = event;
36
+ return outcome.ok
37
+ ? {
38
+ availableCount: outcome.metrics.availableCount,
39
+ latencyMs: outcome.metrics.latencyMs,
40
+ model: outcome.model,
41
+ omittedBeforeScout: outcome.metrics.omittedBeforeScout,
42
+ selectedCount: outcome.metrics.selectedCount,
43
+ selectedLabels: outcome.selectedLabels,
44
+ status: "curated",
45
+ synthesis: outcome.selection.synthesis,
46
+ usage: snapshotAdvisorUsage(outcome.metrics.usage),
47
+ }
48
+ : {
49
+ availableCount: outcome.metrics.availableCount,
50
+ fallbackReason: `${outcome.category}: ${outcome.message}`,
51
+ latencyMs: outcome.metrics.latencyMs,
52
+ model: outcome.model,
53
+ omittedBeforeScout: outcome.metrics.omittedBeforeScout,
54
+ selectedCount: 0,
55
+ status: "fallback",
56
+ usage: snapshotAdvisorUsage(outcome.metrics.usage),
57
+ };
58
+ };
59
+
60
+ export const appendScoutLifecycleEntry = (
61
+ pi: ExtensionAPI,
62
+ event: ScoutLifecycleEvent,
63
+ previous?: ScoutToolDetails
64
+ ) => {
65
+ const scout = scoutDetailsFromEvent(event, previous);
66
+ if (
67
+ event.type === "success" ||
68
+ event.type === "fallback" ||
69
+ event.type === "cancelled"
70
+ ) {
71
+ pi.appendEntry?.("advisor-scout-result", scout);
72
+ }
73
+ return scout;
74
+ };
75
+
76
+ export class ScoutStatusManager {
77
+ readonly #active = new Set<symbol>();
78
+ readonly #known = new Set<symbol>();
79
+ readonly #retired = new Set<symbol>();
80
+ private readonly showStatus: boolean;
81
+
82
+ constructor(showStatus = true) {
83
+ this.showStatus = showStatus;
84
+ }
85
+
86
+ register(token: symbol) {
87
+ if (!this.#retired.has(token)) {
88
+ this.#known.add(token);
89
+ }
90
+ }
91
+
92
+ update(ctx: ExtensionContext, token: symbol, event: ScoutLifecycleEvent) {
93
+ if (this.#retired.has(token) || !ctx.hasUI) {
94
+ return;
95
+ }
96
+ this.#known.add(token);
97
+ if (event.type === "call" || event.type === "chunk") {
98
+ this.#active.add(token);
99
+ if (this.showStatus) {
100
+ ctx.ui.setStatus("advisor-scout", "Scout curating…");
101
+ }
102
+ return;
103
+ }
104
+ this.release(ctx, token);
105
+ }
106
+
107
+ release(ctx: ExtensionContext, token: symbol) {
108
+ this.#active.delete(token);
109
+ this.#known.delete(token);
110
+ this.#retired.add(token);
111
+ if (!(ctx.hasUI && this.showStatus)) {
112
+ return;
113
+ }
114
+ ctx.ui.setStatus(
115
+ "advisor-scout",
116
+ this.#active.size > 0 ? "Scout curating…" : undefined
117
+ );
118
+ }
119
+
120
+ clear(ctx: ExtensionContext) {
121
+ for (const token of this.#known) {
122
+ this.#retired.add(token);
123
+ }
124
+ this.#known.clear();
125
+ this.#active.clear();
126
+ if (ctx.hasUI && this.showStatus) {
127
+ ctx.ui.setStatus("advisor-scout", undefined);
128
+ }
129
+ }
130
+ }
131
+
132
+ const scoutTitle = (scout: ScoutToolDetails, frame: string) => {
133
+ if (scout.status === "calling" || scout.status === "streaming") {
134
+ return `◆ SCOUT ${frame} · CURATING…`;
135
+ }
136
+ if (scout.status === "curated") {
137
+ return "◆ SCOUT · CURATED";
138
+ }
139
+ if (scout.status === "cancelled") {
140
+ return "◆ SCOUT · CANCELLED";
141
+ }
142
+ return "◆ SCOUT · FALLBACK";
143
+ };
144
+
145
+ export const renderScoutDetails = (
146
+ box: Box,
147
+ scout: ScoutToolDetails,
148
+ expanded: boolean,
149
+ theme: Theme
150
+ ) => {
151
+ const active = scout.status === "calling" || scout.status === "streaming";
152
+ const frame =
153
+ SPINNER_FRAMES[Math.floor(Date.now() / 80) % SPINNER_FRAMES.length];
154
+ const title = scoutTitle(scout, frame);
155
+ const lines = [
156
+ theme.fg(
157
+ scout.status === "fallback" || scout.status === "cancelled"
158
+ ? "warning"
159
+ : "accent",
160
+ theme.bold(title)
161
+ ),
162
+ theme.fg(
163
+ "dim",
164
+ ` ${scout.model}${scout.selectedCount === undefined ? "" : ` · ${scout.selectedCount} kept / ${Math.max(0, (scout.availableCount ?? 0) - scout.selectedCount)} omitted`}${scout.latencyMs === undefined ? "" : ` · ${(scout.latencyMs / 1000).toFixed(1)}s`}`
165
+ ),
166
+ ];
167
+ if (getAdvisorSettings().showUsageDetails) {
168
+ const usage = formatAdvisorUsage(scout.usage);
169
+ if (usage) {
170
+ lines.push(theme.fg("dim", ` Usage: ${usage}`));
171
+ }
172
+ }
173
+ if (scout.fallbackReason) {
174
+ lines.push(theme.fg("warning", ` ${scout.fallbackReason}`));
175
+ }
176
+ const thinking = scout.thinking && active ? scout.thinking.slice(-200) : "";
177
+ box.addChild(new Text(lines.join("\n"), 0, 0));
178
+ if (thinking.trim()) {
179
+ box.addChild(renderThinkingMarkdown(thinking, theme));
180
+ }
181
+ const expandedLines: string[] = [];
182
+ if (expanded && scout.selectedLabels?.length) {
183
+ expandedLines.push(
184
+ theme.fg("dim", ` Selected: ${scout.selectedLabels.join("; ")}`)
185
+ );
186
+ }
187
+ if (expanded && scout.synthesis) {
188
+ expandedLines.push(
189
+ theme.fg(
190
+ "dim",
191
+ ` Scout synthesis (untrusted inference): ${scout.synthesis}`
192
+ )
193
+ );
194
+ }
195
+ if (expanded && scout.omittedBeforeScout) {
196
+ expandedLines.push(
197
+ theme.fg(
198
+ "dim",
199
+ ` ${scout.omittedBeforeScout} group(s) omitted before Scout`
200
+ )
201
+ );
202
+ }
203
+ if (expandedLines.length > 0) {
204
+ box.addChild(new Text(expandedLines.join("\n"), 0, 0));
205
+ }
206
+ };
@@ -0,0 +1,3 @@
1
+ import { AdvisorSessionState } from "../session-state.js";
2
+
3
+ export const advisorSessionState = new AdvisorSessionState();
@@ -0,0 +1,119 @@
1
+ import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
2
+ import type { appendOutcome } from "../outcomes.js";
3
+ import type { ScoutOutcome } from "../scout.js";
4
+ import type {
5
+ AdvisorSessionState,
6
+ ConsultationTrigger,
7
+ GateDecision,
8
+ GateTrigger,
9
+ } from "../session-state.js";
10
+ import type { consultAdvisor, runAdvisorGate } from "./consultation.js";
11
+ import type { ScoutStatusManager } from "./scout-status.js";
12
+
13
+ export type {
14
+ AdvisorInvocationRecord,
15
+ ConsultationTrigger,
16
+ GateDecision,
17
+ GateTrigger,
18
+ } from "../session-state.js";
19
+
20
+ export interface ScoutToolDetails {
21
+ availableCount?: number;
22
+ fallbackReason?: string;
23
+ latencyMs?: number;
24
+ model: string;
25
+ omittedBeforeScout?: number;
26
+ selectedCount?: number;
27
+ selectedLabels?: string[];
28
+ status: "calling" | "streaming" | "curated" | "fallback" | "cancelled";
29
+ synthesis?: string;
30
+ text?: string;
31
+ thinking?: string;
32
+ usage?: unknown;
33
+ }
34
+
35
+ export interface AdvisorGateFailure {
36
+ category: GateFailureCategory;
37
+ markdown?: string;
38
+ message: string;
39
+ ok: false;
40
+ usage?: unknown;
41
+ }
42
+
43
+ export interface AdvisorConsultationResult {
44
+ adviceId: string;
45
+ draftBytes?: number;
46
+ markdown: string;
47
+ model: string;
48
+ preferenceBytes?: number;
49
+ scout?: Exclude<ScoutOutcome, { cancelled: true }>;
50
+ thinkingText: string;
51
+ trackedBytes?: number;
52
+ trigger: ConsultationTrigger;
53
+ untrackedBytes?: number;
54
+ usage?: unknown;
55
+ }
56
+
57
+ export interface AdvisorGateResult {
58
+ decision: GateDecision;
59
+ markdown: string;
60
+ model: string;
61
+ ok: true;
62
+ thinkingText: string;
63
+ trigger: GateTrigger;
64
+ usage?: unknown;
65
+ }
66
+
67
+ export type AdvisorGateOutcome = AdvisorGateResult | AdvisorGateFailure;
68
+
69
+ export type GateFailureCategory =
70
+ | "provider-error"
71
+ | "empty-response"
72
+ | "missing-decision"
73
+ | "malformed-decision"
74
+ | "duplicate-decision"
75
+ | "contradictory-decision"
76
+ | "budget-exhausted";
77
+
78
+ export interface AdvisorToolDetails {
79
+ adviceId?: string;
80
+ advisor?: string;
81
+ draftBytes?: number;
82
+ preferenceBytes?: number;
83
+ question?: string;
84
+ scout?: ScoutToolDetails;
85
+ text?: string;
86
+ thinking?: string;
87
+ trackedBytes?: number;
88
+ untrackedBytes?: number;
89
+ usage?: unknown;
90
+ }
91
+
92
+ export interface AdvisorRenderState {
93
+ phase?: string;
94
+ scout?: ScoutToolDetails;
95
+ timerId?: ReturnType<typeof setInterval>;
96
+ }
97
+
98
+ export interface AdvisorToolContext {
99
+ invalidate: () => void;
100
+ lastComponent: unknown;
101
+ state: AdvisorRenderState;
102
+ }
103
+
104
+ export interface ToolRegistrationContext {
105
+ appendOutcome: typeof appendOutcome;
106
+ consult: typeof consultAdvisor;
107
+ pi: ExtensionAPI;
108
+ reservedCalls: Set<string>;
109
+ runGate: typeof runAdvisorGate;
110
+ scoutStatus: ScoutStatusManager;
111
+ session: AdvisorSessionState;
112
+ }
113
+
114
+ export interface ToolRegistrationDependencies {
115
+ appendOutcome?: typeof appendOutcome;
116
+ consult?: typeof consultAdvisor;
117
+ runGate?: typeof runAdvisorGate;
118
+ statusManager?: ScoutStatusManager;
119
+ }