@selesai/code 0.1.4 → 0.1.6
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/dist/config.d.ts +21 -4
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +71 -4
- package/dist/config.js.map +1 -1
- package/dist/core/package-manager.d.ts +1 -0
- package/dist/core/package-manager.d.ts.map +1 -1
- package/dist/core/package-manager.js +6 -1
- package/dist/core/package-manager.js.map +1 -1
- package/dist/core/resource-loader.d.ts.map +1 -1
- package/dist/core/resource-loader.js +3 -2
- package/dist/core/resource-loader.js.map +1 -1
- package/dist/core/settings-manager.d.ts +1 -0
- package/dist/core/settings-manager.d.ts.map +1 -1
- package/dist/core/settings-manager.js.map +1 -1
- package/dist/extensions/pi-intercom/LICENSE +21 -0
- package/dist/extensions/pi-intercom/README.md +484 -0
- package/dist/extensions/pi-intercom/broker/broker.ts +345 -0
- package/dist/extensions/pi-intercom/broker/client.ts +535 -0
- package/dist/extensions/pi-intercom/broker/framing.ts +57 -0
- package/dist/extensions/pi-intercom/broker/paths.ts +20 -0
- package/dist/extensions/pi-intercom/broker/spawn.ts +307 -0
- package/dist/extensions/pi-intercom/config.ts +108 -0
- package/dist/extensions/pi-intercom/index.ts +1778 -0
- package/dist/extensions/pi-intercom/package.json +38 -0
- package/dist/extensions/pi-intercom/reply-tracker.ts +102 -0
- package/dist/extensions/pi-intercom/skills/pi-intercom/SKILL.md +512 -0
- package/dist/extensions/pi-intercom/types.ts +46 -0
- package/dist/extensions/pi-intercom/ui/compose.ts +139 -0
- package/dist/extensions/pi-intercom/ui/inline-message.ts +76 -0
- package/dist/extensions/pi-intercom/ui/session-list.ts +162 -0
- package/dist/extensions/pi-rewind-hook/README.md +245 -0
- package/dist/extensions/pi-rewind-hook/index.ts +1445 -0
- package/dist/extensions/pi-rewind-hook/package.json +29 -0
- package/dist/extensions/pi-subagents/README.md +1290 -0
- package/dist/extensions/pi-subagents/agents/context-builder.md +46 -0
- package/dist/extensions/pi-subagents/agents/delegate.md +12 -0
- package/dist/extensions/pi-subagents/agents/oracle.md +73 -0
- package/dist/extensions/pi-subagents/agents/planner.md +55 -0
- package/dist/extensions/pi-subagents/agents/researcher.md +52 -0
- package/dist/extensions/pi-subagents/agents/reviewer.md +79 -0
- package/dist/extensions/pi-subagents/agents/scout.md +50 -0
- package/dist/extensions/pi-subagents/agents/worker.md +55 -0
- package/dist/extensions/pi-subagents/package.json +83 -0
- package/dist/extensions/pi-subagents/prompts/gather-context-and-clarify.md +13 -0
- package/dist/extensions/pi-subagents/prompts/parallel-cleanup.md +59 -0
- package/dist/extensions/pi-subagents/prompts/parallel-context-build.md +55 -0
- package/dist/extensions/pi-subagents/prompts/parallel-handoff-plan.md +61 -0
- package/dist/extensions/pi-subagents/prompts/parallel-research.md +50 -0
- package/dist/extensions/pi-subagents/prompts/parallel-review.md +54 -0
- package/dist/extensions/pi-subagents/prompts/review-loop.md +41 -0
- package/dist/extensions/pi-subagents/skills/pi-subagents/SKILL.md +855 -0
- package/dist/extensions/pi-subagents/src/agents/agent-management.ts +875 -0
- package/dist/extensions/pi-subagents/src/agents/agent-scope.ts +6 -0
- package/dist/extensions/pi-subagents/src/agents/agent-selection.ts +25 -0
- package/dist/extensions/pi-subagents/src/agents/agent-serializer.ts +110 -0
- package/dist/extensions/pi-subagents/src/agents/agents.ts +1379 -0
- package/dist/extensions/pi-subagents/src/agents/chain-serializer.ts +252 -0
- package/dist/extensions/pi-subagents/src/agents/frontmatter.ts +93 -0
- package/dist/extensions/pi-subagents/src/agents/identity.ts +30 -0
- package/dist/extensions/pi-subagents/src/agents/proactive-skills.ts +191 -0
- package/dist/extensions/pi-subagents/src/agents/skills.ts +729 -0
- package/dist/extensions/pi-subagents/src/extension/config.ts +16 -0
- package/dist/extensions/pi-subagents/src/extension/control-notices.ts +92 -0
- package/dist/extensions/pi-subagents/src/extension/doctor.ts +220 -0
- package/dist/extensions/pi-subagents/src/extension/fanout-child.ts +171 -0
- package/dist/extensions/pi-subagents/src/extension/index.ts +627 -0
- package/dist/extensions/pi-subagents/src/extension/schemas.ts +265 -0
- package/dist/extensions/pi-subagents/src/intercom/intercom-bridge.ts +412 -0
- package/dist/extensions/pi-subagents/src/intercom/result-intercom.ts +377 -0
- package/dist/extensions/pi-subagents/src/profiles/profiles.ts +637 -0
- package/dist/extensions/pi-subagents/src/runs/background/async-execution.ts +916 -0
- package/dist/extensions/pi-subagents/src/runs/background/async-job-tracker.ts +355 -0
- package/dist/extensions/pi-subagents/src/runs/background/async-resume.ts +391 -0
- package/dist/extensions/pi-subagents/src/runs/background/async-status.ts +338 -0
- package/dist/extensions/pi-subagents/src/runs/background/chain-append.ts +282 -0
- package/dist/extensions/pi-subagents/src/runs/background/chain-root-attachment.ts +161 -0
- package/dist/extensions/pi-subagents/src/runs/background/completion-dedupe.ts +63 -0
- package/dist/extensions/pi-subagents/src/runs/background/control-channel.ts +177 -0
- package/dist/extensions/pi-subagents/src/runs/background/notify.ts +108 -0
- package/dist/extensions/pi-subagents/src/runs/background/parallel-groups.ts +45 -0
- package/dist/extensions/pi-subagents/src/runs/background/result-watcher.ts +316 -0
- package/dist/extensions/pi-subagents/src/runs/background/run-id-resolver.ts +83 -0
- package/dist/extensions/pi-subagents/src/runs/background/run-status.ts +273 -0
- package/dist/extensions/pi-subagents/src/runs/background/stale-run-reconciler.ts +341 -0
- package/dist/extensions/pi-subagents/src/runs/background/subagent-runner.ts +2458 -0
- package/dist/extensions/pi-subagents/src/runs/background/top-level-async.ts +13 -0
- package/dist/extensions/pi-subagents/src/runs/foreground/chain-clarify.ts +1333 -0
- package/dist/extensions/pi-subagents/src/runs/foreground/chain-execution.ts +1217 -0
- package/dist/extensions/pi-subagents/src/runs/foreground/execution.ts +1092 -0
- package/dist/extensions/pi-subagents/src/runs/foreground/subagent-executor.ts +3125 -0
- package/dist/extensions/pi-subagents/src/runs/shared/acceptance.ts +856 -0
- package/dist/extensions/pi-subagents/src/runs/shared/chain-outputs.ts +116 -0
- package/dist/extensions/pi-subagents/src/runs/shared/completion-guard.ts +147 -0
- package/dist/extensions/pi-subagents/src/runs/shared/dynamic-fanout.ts +295 -0
- package/dist/extensions/pi-subagents/src/runs/shared/long-running-guard.ts +175 -0
- package/dist/extensions/pi-subagents/src/runs/shared/mcp-direct-tool-allowlist.ts +365 -0
- package/dist/extensions/pi-subagents/src/runs/shared/model-fallback.ts +141 -0
- package/dist/extensions/pi-subagents/src/runs/shared/nested-events.ts +819 -0
- package/dist/extensions/pi-subagents/src/runs/shared/nested-path.ts +52 -0
- package/dist/extensions/pi-subagents/src/runs/shared/nested-render.ts +115 -0
- package/dist/extensions/pi-subagents/src/runs/shared/parallel-utils.ts +149 -0
- package/dist/extensions/pi-subagents/src/runs/shared/pi-args.ts +240 -0
- package/dist/extensions/pi-subagents/src/runs/shared/pi-spawn.ts +115 -0
- package/dist/extensions/pi-subagents/src/runs/shared/run-history.ts +60 -0
- package/dist/extensions/pi-subagents/src/runs/shared/single-output.ts +178 -0
- package/dist/extensions/pi-subagents/src/runs/shared/structured-output.ts +77 -0
- package/dist/extensions/pi-subagents/src/runs/shared/subagent-control.ts +223 -0
- package/dist/extensions/pi-subagents/src/runs/shared/subagent-prompt-runtime.ts +222 -0
- package/dist/extensions/pi-subagents/src/runs/shared/workflow-graph.ts +206 -0
- package/dist/extensions/pi-subagents/src/runs/shared/worktree.ts +577 -0
- package/dist/extensions/pi-subagents/src/shared/artifacts.ts +98 -0
- package/dist/extensions/pi-subagents/src/shared/atomic-json.ts +73 -0
- package/dist/extensions/pi-subagents/src/shared/file-coalescer.ts +40 -0
- package/dist/extensions/pi-subagents/src/shared/fork-context.ts +76 -0
- package/dist/extensions/pi-subagents/src/shared/formatters.ts +133 -0
- package/dist/extensions/pi-subagents/src/shared/jsonl-writer.ts +81 -0
- package/dist/extensions/pi-subagents/src/shared/model-info.ts +78 -0
- package/dist/extensions/pi-subagents/src/shared/post-exit-stdio-guard.ts +85 -0
- package/dist/extensions/pi-subagents/src/shared/session-identity.ts +10 -0
- package/dist/extensions/pi-subagents/src/shared/session-tokens.ts +44 -0
- package/dist/extensions/pi-subagents/src/shared/settings.ts +448 -0
- package/dist/extensions/pi-subagents/src/shared/status-format.ts +49 -0
- package/dist/extensions/pi-subagents/src/shared/types.ts +1064 -0
- package/dist/extensions/pi-subagents/src/shared/utils.ts +469 -0
- package/dist/extensions/pi-subagents/src/slash/prompt-template-bridge.ts +420 -0
- package/dist/extensions/pi-subagents/src/slash/slash-bridge.ts +176 -0
- package/dist/extensions/pi-subagents/src/slash/slash-commands.ts +1165 -0
- package/dist/extensions/pi-subagents/src/slash/slash-live-state.ts +292 -0
- package/dist/extensions/pi-subagents/src/tui/render-helpers.ts +80 -0
- package/dist/extensions/pi-subagents/src/tui/render.ts +1728 -0
- package/dist/extensions/quick.ts +630 -0
- package/dist/modes/index.d.ts +1 -1
- package/dist/modes/index.d.ts.map +1 -1
- package/dist/modes/index.js.map +1 -1
- package/dist/modes/rpc/rpc-mode.d.ts +1 -1
- package/dist/modes/rpc/rpc-mode.d.ts.map +1 -1
- package/dist/modes/rpc/rpc-mode.js +4 -3
- package/dist/modes/rpc/rpc-mode.js.map +1 -1
- package/dist/modes/rpc/rpc-types.d.ts +3 -2
- package/dist/modes/rpc/rpc-types.d.ts.map +1 -1
- package/dist/modes/rpc/rpc-types.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
import type { AgentToolResult } from "@earendil-works/pi-agent-core";
|
|
2
|
+
import type { Message } from "@earendil-works/pi-ai";
|
|
3
|
+
import type { SubagentParamsLike } from "../runs/foreground/subagent-executor.ts";
|
|
4
|
+
import type { SlashSubagentResponse, SlashSubagentUpdate } from "./slash-bridge.ts";
|
|
5
|
+
import { type Details, type SingleResult, type Usage, SLASH_RESULT_TYPE } from "../shared/types.ts";
|
|
6
|
+
|
|
7
|
+
export interface SlashMessageDetails {
|
|
8
|
+
requestId: string;
|
|
9
|
+
result: AgentToolResult<Details>;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
interface SlashSnapshot {
|
|
13
|
+
result: AgentToolResult<Details>;
|
|
14
|
+
version: number;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
interface SequentialChainStepLike {
|
|
18
|
+
agent: string;
|
|
19
|
+
task?: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
interface ParallelChainStepLike {
|
|
23
|
+
parallel: Array<{ agent: string; task?: string }>;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
type ChainStepLike = SequentialChainStepLike | ParallelChainStepLike;
|
|
27
|
+
|
|
28
|
+
const liveSnapshots = new Map<string, SlashSnapshot>();
|
|
29
|
+
const finalSnapshots = new Map<string, SlashSnapshot>();
|
|
30
|
+
let versionCounter = 1;
|
|
31
|
+
|
|
32
|
+
const EMPTY_MESSAGES: Message[] = [];
|
|
33
|
+
const EMPTY_USAGE: Usage = {
|
|
34
|
+
input: 0,
|
|
35
|
+
output: 0,
|
|
36
|
+
cacheRead: 0,
|
|
37
|
+
cacheWrite: 0,
|
|
38
|
+
cost: 0,
|
|
39
|
+
turns: 0,
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
function nextVersion(): number {
|
|
43
|
+
return versionCounter++;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function cloneUsage(): Usage {
|
|
47
|
+
return { ...EMPTY_USAGE };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function createPlaceholderResult(
|
|
51
|
+
agent: string,
|
|
52
|
+
task: string,
|
|
53
|
+
status: "pending" | "running",
|
|
54
|
+
index?: number,
|
|
55
|
+
): SingleResult {
|
|
56
|
+
return {
|
|
57
|
+
agent,
|
|
58
|
+
task,
|
|
59
|
+
exitCode: 0,
|
|
60
|
+
messages: EMPTY_MESSAGES,
|
|
61
|
+
usage: cloneUsage(),
|
|
62
|
+
progress: {
|
|
63
|
+
...(index !== undefined ? { index } : {}),
|
|
64
|
+
agent,
|
|
65
|
+
status,
|
|
66
|
+
task,
|
|
67
|
+
recentTools: [],
|
|
68
|
+
recentOutput: [],
|
|
69
|
+
toolCount: 0,
|
|
70
|
+
tokens: 0,
|
|
71
|
+
durationMs: 0,
|
|
72
|
+
},
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function buildParallelInitialResult(params: SubagentParamsLike): AgentToolResult<Details> {
|
|
77
|
+
const tasks = params.tasks ?? [];
|
|
78
|
+
return {
|
|
79
|
+
content: [{ type: "text", text: tasks.map((task) => `${task.agent}: ${task.task}`).join("\n\n") }],
|
|
80
|
+
details: {
|
|
81
|
+
mode: "parallel",
|
|
82
|
+
...(params.context ? { context: params.context } : {}),
|
|
83
|
+
results: tasks.map((task, index) => createPlaceholderResult(task.agent, task.task, "running", index)),
|
|
84
|
+
progress: tasks.map((task, index) => ({
|
|
85
|
+
index,
|
|
86
|
+
agent: task.agent,
|
|
87
|
+
status: "running" as const,
|
|
88
|
+
task: task.task,
|
|
89
|
+
recentTools: [],
|
|
90
|
+
recentOutput: [],
|
|
91
|
+
toolCount: 0,
|
|
92
|
+
tokens: 0,
|
|
93
|
+
durationMs: 0,
|
|
94
|
+
})),
|
|
95
|
+
},
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function isParallelChainStep(step: ChainStepLike): step is ParallelChainStepLike {
|
|
100
|
+
return "parallel" in step && Array.isArray(step.parallel);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function chainStepLabel(step: ChainStepLike): string {
|
|
104
|
+
if (isParallelChainStep(step)) {
|
|
105
|
+
return `[${step.parallel.map((entry) => entry.agent).join("+")}]`;
|
|
106
|
+
}
|
|
107
|
+
return step.agent;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function flattenChainResults(chain: ChainStepLike[], fallbackTask: string | undefined): SingleResult[] {
|
|
111
|
+
const results: SingleResult[] = [];
|
|
112
|
+
let flatIndex = 0;
|
|
113
|
+
for (const step of chain) {
|
|
114
|
+
if (isParallelChainStep(step)) {
|
|
115
|
+
for (const task of step.parallel) {
|
|
116
|
+
results.push(createPlaceholderResult(task.agent, task.task ?? fallbackTask ?? "", results.length === 0 ? "running" : "pending", flatIndex));
|
|
117
|
+
flatIndex++;
|
|
118
|
+
}
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
results.push(createPlaceholderResult(step.agent, step.task ?? fallbackTask ?? "", results.length === 0 ? "running" : "pending", flatIndex));
|
|
122
|
+
flatIndex++;
|
|
123
|
+
}
|
|
124
|
+
return results;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function buildChainInitialResult(params: SubagentParamsLike): AgentToolResult<Details> {
|
|
128
|
+
const chain = (params.chain ?? []) as ChainStepLike[];
|
|
129
|
+
const results = flattenChainResults(chain, params.task);
|
|
130
|
+
return {
|
|
131
|
+
content: [{
|
|
132
|
+
type: "text",
|
|
133
|
+
text: results.map((result, index) => `Step ${index + 1}: ${result.agent}\n${result.task}`).join("\n\n"),
|
|
134
|
+
}],
|
|
135
|
+
details: {
|
|
136
|
+
mode: "chain",
|
|
137
|
+
...(params.context ? { context: params.context } : {}),
|
|
138
|
+
results,
|
|
139
|
+
progress: results.map((result, index) => ({
|
|
140
|
+
index,
|
|
141
|
+
agent: result.agent,
|
|
142
|
+
status: index === 0 ? "running" as const : "pending" as const,
|
|
143
|
+
task: result.task,
|
|
144
|
+
recentTools: [],
|
|
145
|
+
recentOutput: [],
|
|
146
|
+
toolCount: 0,
|
|
147
|
+
tokens: 0,
|
|
148
|
+
durationMs: 0,
|
|
149
|
+
})),
|
|
150
|
+
chainAgents: chain.map((step) => chainStepLabel(step)),
|
|
151
|
+
totalSteps: chain.length,
|
|
152
|
+
currentStepIndex: 0,
|
|
153
|
+
},
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function buildSingleInitialResult(params: SubagentParamsLike): AgentToolResult<Details> {
|
|
158
|
+
const agent = params.agent ?? "subagent";
|
|
159
|
+
const task = params.task ?? "";
|
|
160
|
+
return {
|
|
161
|
+
content: [{ type: "text", text: task }],
|
|
162
|
+
details: {
|
|
163
|
+
mode: "single",
|
|
164
|
+
...(params.context ? { context: params.context } : {}),
|
|
165
|
+
results: [createPlaceholderResult(agent, task, "running")],
|
|
166
|
+
progress: [{
|
|
167
|
+
agent,
|
|
168
|
+
status: "running",
|
|
169
|
+
task,
|
|
170
|
+
recentTools: [],
|
|
171
|
+
recentOutput: [],
|
|
172
|
+
toolCount: 0,
|
|
173
|
+
tokens: 0,
|
|
174
|
+
durationMs: 0,
|
|
175
|
+
}],
|
|
176
|
+
},
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export function buildSlashInitialResult(requestId: string, params: SubagentParamsLike): SlashMessageDetails {
|
|
181
|
+
const result = (params.tasks?.length ?? 0) > 0
|
|
182
|
+
? buildParallelInitialResult(params)
|
|
183
|
+
: (params.chain?.length ?? 0) > 0
|
|
184
|
+
? buildChainInitialResult(params)
|
|
185
|
+
: buildSingleInitialResult(params);
|
|
186
|
+
liveSnapshots.set(requestId, { result, version: nextVersion() });
|
|
187
|
+
finalSnapshots.delete(requestId);
|
|
188
|
+
return { requestId, result };
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function cloneResultsWithProgress(
|
|
192
|
+
results: SingleResult[],
|
|
193
|
+
progress: NonNullable<Details["progress"]> | undefined,
|
|
194
|
+
): SingleResult[] {
|
|
195
|
+
return results.map((result, index) => {
|
|
196
|
+
const nextProgress = progress?.find((entry) => entry.index === index)
|
|
197
|
+
?? progress?.[index]
|
|
198
|
+
?? result.progress;
|
|
199
|
+
return nextProgress ? { ...result, progress: nextProgress } : result;
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
export function applySlashUpdate(requestId: string, update: SlashSubagentUpdate): void {
|
|
204
|
+
const snapshot = liveSnapshots.get(requestId);
|
|
205
|
+
if (!snapshot) return;
|
|
206
|
+
const progress = update.progress;
|
|
207
|
+
if (!progress || !snapshot.result.details) return;
|
|
208
|
+
const currentStepIndex = progress.findIndex((entry) => entry.status === "running");
|
|
209
|
+
const nextDetails: Details = {
|
|
210
|
+
...snapshot.result.details,
|
|
211
|
+
progress,
|
|
212
|
+
results: cloneResultsWithProgress(snapshot.result.details.results, progress),
|
|
213
|
+
...(snapshot.result.details.mode === "chain" && currentStepIndex >= 0 ? { currentStepIndex } : {}),
|
|
214
|
+
};
|
|
215
|
+
liveSnapshots.set(requestId, {
|
|
216
|
+
result: {
|
|
217
|
+
...snapshot.result,
|
|
218
|
+
details: nextDetails,
|
|
219
|
+
},
|
|
220
|
+
version: nextVersion(),
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
export function finalizeSlashResult(response: SlashSubagentResponse): SlashMessageDetails {
|
|
225
|
+
const snapshot = {
|
|
226
|
+
result: response.result,
|
|
227
|
+
version: nextVersion(),
|
|
228
|
+
};
|
|
229
|
+
finalSnapshots.set(response.requestId, snapshot);
|
|
230
|
+
liveSnapshots.delete(response.requestId);
|
|
231
|
+
return {
|
|
232
|
+
requestId: response.requestId,
|
|
233
|
+
result: response.result,
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
export function failSlashResult(requestId: string, params: SubagentParamsLike, message: string): SlashMessageDetails {
|
|
238
|
+
const initial = buildSlashInitialResult(requestId, params).result;
|
|
239
|
+
const failedResults = initial.details.results.map((result) => ({
|
|
240
|
+
...result,
|
|
241
|
+
exitCode: 1,
|
|
242
|
+
error: message,
|
|
243
|
+
progress: result.progress ? { ...result.progress, status: "failed" as const } : result.progress,
|
|
244
|
+
}));
|
|
245
|
+
const result: AgentToolResult<Details> = {
|
|
246
|
+
content: [{ type: "text", text: message }],
|
|
247
|
+
details: {
|
|
248
|
+
...initial.details,
|
|
249
|
+
results: failedResults,
|
|
250
|
+
progress: failedResults.map((entry) => entry.progress!).filter(Boolean),
|
|
251
|
+
},
|
|
252
|
+
};
|
|
253
|
+
const snapshot = { result, version: nextVersion() };
|
|
254
|
+
finalSnapshots.set(requestId, snapshot);
|
|
255
|
+
liveSnapshots.delete(requestId);
|
|
256
|
+
return { requestId, result };
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
function isSlashMessageDetails(value: unknown): value is SlashMessageDetails {
|
|
260
|
+
if (!value || typeof value !== "object") return false;
|
|
261
|
+
const v = value as { requestId?: string; result?: { content?: unknown; details?: { results?: unknown } } };
|
|
262
|
+
if (typeof v.requestId !== "string" || !v.requestId) return false;
|
|
263
|
+
if (!v.result || !Array.isArray(v.result.content)) return false;
|
|
264
|
+
return !!v.result.details && Array.isArray(v.result.details.results);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
export function resolveSlashMessageDetails(value: unknown): SlashMessageDetails | undefined {
|
|
268
|
+
return isSlashMessageDetails(value) ? value : undefined;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
export function getSlashRenderableSnapshot(details: SlashMessageDetails): SlashSnapshot {
|
|
272
|
+
return finalSnapshots.get(details.requestId)
|
|
273
|
+
?? liveSnapshots.get(details.requestId)
|
|
274
|
+
?? { result: details.result, version: 0 };
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
export function restoreSlashFinalSnapshots(entries: unknown[]): void {
|
|
278
|
+
liveSnapshots.clear();
|
|
279
|
+
finalSnapshots.clear();
|
|
280
|
+
for (const entry of entries) {
|
|
281
|
+
const e = entry as { type?: string; customType?: string; details?: unknown };
|
|
282
|
+
if (e?.type !== "custom_message" || e.customType !== SLASH_RESULT_TYPE) continue;
|
|
283
|
+
const details = resolveSlashMessageDetails(e.details);
|
|
284
|
+
if (!details) continue;
|
|
285
|
+
finalSnapshots.set(details.requestId, { result: details.result, version: nextVersion() });
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
export function clearSlashSnapshots(): void {
|
|
290
|
+
liveSnapshots.clear();
|
|
291
|
+
finalSnapshots.clear();
|
|
292
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import type { Theme } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { truncateToWidth, visibleWidth } from "@earendil-works/pi-tui";
|
|
3
|
+
|
|
4
|
+
function fuzzyScore(query: string, text: string): number {
|
|
5
|
+
const lq = query.toLowerCase();
|
|
6
|
+
const lt = text.toLowerCase();
|
|
7
|
+
if (lt.includes(lq)) return 100 + (lq.length / lt.length) * 50;
|
|
8
|
+
let score = 0;
|
|
9
|
+
let qi = 0;
|
|
10
|
+
let consecutive = 0;
|
|
11
|
+
for (let i = 0; i < lt.length && qi < lq.length; i++) {
|
|
12
|
+
if (lt[i] === lq[qi]) {
|
|
13
|
+
score += 10 + consecutive;
|
|
14
|
+
consecutive += 5;
|
|
15
|
+
qi++;
|
|
16
|
+
} else {
|
|
17
|
+
consecutive = 0;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return qi === lq.length ? score : 0;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function fuzzyFilter<T extends { name: string; description: string; model?: string }>(items: T[], query: string): T[] {
|
|
24
|
+
const q = query.trim();
|
|
25
|
+
if (!q) return items;
|
|
26
|
+
return items
|
|
27
|
+
.map((item) => ({ item, score: Math.max(fuzzyScore(q, item.name), fuzzyScore(q, item.description) * 0.8, fuzzyScore(q, item.model ?? "") * 0.6) }))
|
|
28
|
+
.filter((x) => x.score > 0)
|
|
29
|
+
.sort((a, b) => b.score - a.score)
|
|
30
|
+
.map((x) => x.item);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function pad(s: string, len: number): string {
|
|
34
|
+
const vis = visibleWidth(s);
|
|
35
|
+
return s + " ".repeat(Math.max(0, len - vis));
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function row(content: string, width: number, theme: Theme): string {
|
|
39
|
+
const innerW = width - 2;
|
|
40
|
+
const singleLine = content.replace(/[\r\n]+/g, " ").replace(/\t/g, " ");
|
|
41
|
+
const clipped = truncateToWidth(singleLine, innerW);
|
|
42
|
+
return theme.fg("border", "│") + pad(clipped, innerW) + theme.fg("border", "│");
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function renderHeader(text: string, width: number, theme: Theme): string {
|
|
46
|
+
const innerW = width - 2;
|
|
47
|
+
const padLen = Math.max(0, innerW - visibleWidth(text));
|
|
48
|
+
const padLeft = Math.floor(padLen / 2);
|
|
49
|
+
const padRight = padLen - padLeft;
|
|
50
|
+
return (
|
|
51
|
+
theme.fg("border", "╭" + "─".repeat(padLeft)) +
|
|
52
|
+
theme.fg("accent", text) +
|
|
53
|
+
theme.fg("border", "─".repeat(padRight) + "╮")
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function formatPath(filePath: string): string {
|
|
58
|
+
const home = process.env.HOME;
|
|
59
|
+
if (home && filePath.startsWith(home)) return `~${filePath.slice(home.length)}`;
|
|
60
|
+
return filePath;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function formatScrollInfo(above: number, below: number): string {
|
|
64
|
+
let info = "";
|
|
65
|
+
if (above > 0) info += `↑ ${above} more`;
|
|
66
|
+
if (below > 0) info += `${info ? " " : ""}↓ ${below} more`;
|
|
67
|
+
return info;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function renderFooter(text: string, width: number, theme: Theme): string {
|
|
71
|
+
const innerW = width - 2;
|
|
72
|
+
const padLen = Math.max(0, innerW - visibleWidth(text));
|
|
73
|
+
const padLeft = Math.floor(padLen / 2);
|
|
74
|
+
const padRight = padLen - padLeft;
|
|
75
|
+
return (
|
|
76
|
+
theme.fg("border", "╰" + "─".repeat(padLeft)) +
|
|
77
|
+
theme.fg("dim", text) +
|
|
78
|
+
theme.fg("border", "─".repeat(padRight) + "╯")
|
|
79
|
+
);
|
|
80
|
+
}
|