@oh-my-pi/pi-coding-agent 13.12.8 → 13.12.10
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 +28 -0
- package/package.json +7 -7
- package/src/capability/mcp.ts +7 -1
- package/src/cli/session-picker.ts +38 -28
- package/src/discovery/builtin.ts +18 -2
- package/src/discovery/mcp-json.ts +12 -2
- package/src/ipy/prelude.py +25 -8
- package/src/mcp/oauth-flow.ts +91 -1
- package/src/mcp/types.ts +3 -0
- package/src/modes/components/session-selector.ts +113 -13
- package/src/modes/controllers/mcp-command-controller.ts +20 -15
- package/src/modes/controllers/selector-controller.ts +82 -6
- package/src/modes/interactive-mode.ts +4 -0
- package/src/modes/types.ts +1 -0
- package/src/sdk.ts +17 -4
- package/src/session/agent-session.ts +116 -26
- package/src/session/agent-storage.ts +48 -8
- package/src/session/history-storage.ts +44 -3
- package/src/session/session-manager.ts +49 -1
- package/src/session/session-storage.ts +27 -0
- package/src/slash-commands/builtin-registry.ts +14 -2
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,34 @@
|
|
|
2
2
|
|
|
3
3
|
## [Unreleased]
|
|
4
4
|
|
|
5
|
+
## [13.12.10] - 2026-03-17
|
|
6
|
+
### Added
|
|
7
|
+
|
|
8
|
+
- Added `args` field to ShellResult to capture the executed command
|
|
9
|
+
- Added `exit_code` property to ShellResult as an alias for `returncode`
|
|
10
|
+
- Added `check_returncode()` method to ShellResult to raise CalledProcessError on non-zero exit codes
|
|
11
|
+
|
|
12
|
+
### Changed
|
|
13
|
+
|
|
14
|
+
- Renamed `code` field to `returncode` in ShellResult (accessible via `code` property for backward compatibility)
|
|
15
|
+
- Updated `run()` command documentation to clarify available ShellResult fields
|
|
16
|
+
|
|
17
|
+
## [13.12.9] - 2026-03-17
|
|
18
|
+
### Added
|
|
19
|
+
|
|
20
|
+
- Added `/session delete` command to delete current session with confirmation and return to session selector
|
|
21
|
+
- Added session deletion in session selector via Delete key with confirmation dialog
|
|
22
|
+
|
|
23
|
+
### Changed
|
|
24
|
+
|
|
25
|
+
- Changed session deletion callback to return a boolean indicating success, allowing callers to distinguish between failed deletions and upstream cancellations
|
|
26
|
+
|
|
27
|
+
### Fixed
|
|
28
|
+
|
|
29
|
+
- Fixed OAuth redirect URI validation to preserve exact configured values without adding trailing slashes
|
|
30
|
+
- Fixed session deletion error handling to display error messages in the session selector UI instead of silently failing
|
|
31
|
+
- Added `oauth.redirectUri`, `oauth.clientSecret`, and `oauth.callbackPath` support for MCP server OAuth config so providers can use exact registered redirect URIs while preserving local callback listener settings ([#445](https://github.com/can1357/oh-my-pi/issues/445))
|
|
32
|
+
|
|
5
33
|
## [13.12.8] - 2026-03-16
|
|
6
34
|
|
|
7
35
|
### Breaking Changes
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"type": "module",
|
|
3
3
|
"name": "@oh-my-pi/pi-coding-agent",
|
|
4
|
-
"version": "13.12.
|
|
4
|
+
"version": "13.12.10",
|
|
5
5
|
"description": "Coding agent CLI with read, bash, edit, write tools and session management",
|
|
6
6
|
"homepage": "https://github.com/can1357/oh-my-pi",
|
|
7
7
|
"author": "Can Boluk",
|
|
@@ -41,12 +41,12 @@
|
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
43
|
"@mozilla/readability": "^0.6",
|
|
44
|
-
"@oh-my-pi/omp-stats": "13.12.
|
|
45
|
-
"@oh-my-pi/pi-agent-core": "13.12.
|
|
46
|
-
"@oh-my-pi/pi-ai": "13.12.
|
|
47
|
-
"@oh-my-pi/pi-natives": "13.12.
|
|
48
|
-
"@oh-my-pi/pi-tui": "13.12.
|
|
49
|
-
"@oh-my-pi/pi-utils": "13.12.
|
|
44
|
+
"@oh-my-pi/omp-stats": "13.12.10",
|
|
45
|
+
"@oh-my-pi/pi-agent-core": "13.12.10",
|
|
46
|
+
"@oh-my-pi/pi-ai": "13.12.10",
|
|
47
|
+
"@oh-my-pi/pi-natives": "13.12.10",
|
|
48
|
+
"@oh-my-pi/pi-tui": "13.12.10",
|
|
49
|
+
"@oh-my-pi/pi-utils": "13.12.10",
|
|
50
50
|
"@sinclair/typebox": "^0.34",
|
|
51
51
|
"@xterm/headless": "^6.0",
|
|
52
52
|
"ajv": "^8.18",
|
package/src/capability/mcp.ts
CHANGED
|
@@ -31,11 +31,17 @@ export interface MCPServer {
|
|
|
31
31
|
auth?: {
|
|
32
32
|
type: "oauth" | "apikey";
|
|
33
33
|
credentialId?: string;
|
|
34
|
+
tokenUrl?: string;
|
|
35
|
+
clientId?: string;
|
|
36
|
+
clientSecret?: string;
|
|
34
37
|
};
|
|
35
|
-
/** OAuth configuration (clientId, callbackPort) for servers requiring explicit client credentials */
|
|
38
|
+
/** OAuth configuration (clientId, clientSecret, redirectUri, callbackPort, callbackPath) for servers requiring explicit client credentials */
|
|
36
39
|
oauth?: {
|
|
37
40
|
clientId?: string;
|
|
41
|
+
clientSecret?: string;
|
|
42
|
+
redirectUri?: string;
|
|
38
43
|
callbackPort?: number;
|
|
44
|
+
callbackPath?: string;
|
|
39
45
|
};
|
|
40
46
|
/** Transport type */
|
|
41
47
|
transport?: "stdio" | "sse" | "http";
|
|
@@ -1,42 +1,52 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* TUI session selector for --resume flag
|
|
3
|
-
*/
|
|
4
1
|
import { ProcessTerminal, TUI } from "@oh-my-pi/pi-tui";
|
|
5
2
|
import { SessionSelectorComponent } from "../modes/components/session-selector";
|
|
6
3
|
import type { SessionInfo } from "../session/session-manager";
|
|
4
|
+
import { FileSessionStorage } from "../session/session-storage";
|
|
7
5
|
|
|
8
6
|
/** Show TUI session selector and return selected session path or null if cancelled */
|
|
9
7
|
export async function selectSession(sessions: SessionInfo[]): Promise<string | null> {
|
|
10
8
|
const { promise, resolve } = Promise.withResolvers<string | null>();
|
|
11
9
|
const ui = new TUI(new ProcessTerminal());
|
|
12
10
|
let resolved = false;
|
|
13
|
-
const
|
|
14
|
-
sessions,
|
|
15
|
-
(path: string) => {
|
|
16
|
-
if (!resolved) {
|
|
17
|
-
resolved = true;
|
|
18
|
-
ui.stop();
|
|
19
|
-
resolve(path);
|
|
20
|
-
}
|
|
21
|
-
},
|
|
22
|
-
() => {
|
|
23
|
-
if (!resolved) {
|
|
24
|
-
resolved = true;
|
|
25
|
-
ui.stop();
|
|
26
|
-
resolve(null);
|
|
27
|
-
}
|
|
28
|
-
},
|
|
29
|
-
() => {
|
|
30
|
-
if (!resolved) {
|
|
31
|
-
resolved = true;
|
|
32
|
-
ui.stop();
|
|
33
|
-
process.exit(0);
|
|
34
|
-
}
|
|
35
|
-
},
|
|
36
|
-
);
|
|
11
|
+
const storage = new FileSessionStorage();
|
|
37
12
|
|
|
13
|
+
const showSelector = () => {
|
|
14
|
+
const selector = new SessionSelectorComponent(
|
|
15
|
+
sessions,
|
|
16
|
+
(path: string) => {
|
|
17
|
+
if (!resolved) {
|
|
18
|
+
resolved = true;
|
|
19
|
+
ui.stop();
|
|
20
|
+
resolve(path);
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
() => {
|
|
24
|
+
if (!resolved) {
|
|
25
|
+
resolved = true;
|
|
26
|
+
ui.stop();
|
|
27
|
+
resolve(null);
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
() => {
|
|
31
|
+
if (!resolved) {
|
|
32
|
+
resolved = true;
|
|
33
|
+
ui.stop();
|
|
34
|
+
process.exit(0);
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
async (session: SessionInfo) => {
|
|
38
|
+
// Delete handler - SessionList will show confirmation internally
|
|
39
|
+
await storage.deleteSessionWithArtifacts(session.path);
|
|
40
|
+
return true;
|
|
41
|
+
},
|
|
42
|
+
);
|
|
43
|
+
return selector;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const selector = showSelector();
|
|
47
|
+
selector.setOnRequestRender(() => ui.requestRender());
|
|
38
48
|
ui.addChild(selector);
|
|
39
|
-
ui.setFocus(selector
|
|
49
|
+
ui.setFocus(selector);
|
|
40
50
|
ui.start();
|
|
41
51
|
return promise;
|
|
42
52
|
}
|
package/src/discovery/builtin.ts
CHANGED
|
@@ -160,8 +160,24 @@ async function loadMCPServers(ctx: LoadContext): Promise<LoadResult<MCPServer>>
|
|
|
160
160
|
env: serverConfig.env as Record<string, string> | undefined,
|
|
161
161
|
url: serverConfig.url as string | undefined,
|
|
162
162
|
headers: serverConfig.headers as Record<string, string> | undefined,
|
|
163
|
-
auth: serverConfig.auth as
|
|
164
|
-
|
|
163
|
+
auth: serverConfig.auth as
|
|
164
|
+
| {
|
|
165
|
+
type: "oauth" | "apikey";
|
|
166
|
+
credentialId?: string;
|
|
167
|
+
tokenUrl?: string;
|
|
168
|
+
clientId?: string;
|
|
169
|
+
clientSecret?: string;
|
|
170
|
+
}
|
|
171
|
+
| undefined,
|
|
172
|
+
oauth: serverConfig.oauth as
|
|
173
|
+
| {
|
|
174
|
+
clientId?: string;
|
|
175
|
+
clientSecret?: string;
|
|
176
|
+
redirectUri?: string;
|
|
177
|
+
callbackPort?: number;
|
|
178
|
+
callbackPath?: string;
|
|
179
|
+
}
|
|
180
|
+
| undefined,
|
|
165
181
|
transport: serverConfig.type as "stdio" | "sse" | "http" | undefined,
|
|
166
182
|
_source: createSourceMeta(PROVIDER_ID, path, level),
|
|
167
183
|
});
|
|
@@ -34,9 +34,18 @@ interface MCPConfigFile {
|
|
|
34
34
|
auth?: {
|
|
35
35
|
type: "oauth" | "apikey";
|
|
36
36
|
credentialId?: string;
|
|
37
|
+
tokenUrl?: string;
|
|
38
|
+
clientId?: string;
|
|
39
|
+
clientSecret?: string;
|
|
37
40
|
};
|
|
38
41
|
type?: "stdio" | "sse" | "http";
|
|
39
|
-
oauth?: {
|
|
42
|
+
oauth?: {
|
|
43
|
+
clientId?: string;
|
|
44
|
+
clientSecret?: string;
|
|
45
|
+
redirectUri?: string;
|
|
46
|
+
callbackPort?: number;
|
|
47
|
+
callbackPath?: string;
|
|
48
|
+
};
|
|
40
49
|
}
|
|
41
50
|
>;
|
|
42
51
|
}
|
|
@@ -93,7 +102,8 @@ function transformMCPConfig(config: MCPConfigFile, source: SourceMeta): MCPServe
|
|
|
93
102
|
if (server.env) server.env = expandEnvVarsDeep(server.env);
|
|
94
103
|
if (server.url) server.url = expandEnvVarsDeep(server.url);
|
|
95
104
|
if (server.headers) server.headers = expandEnvVarsDeep(server.headers);
|
|
96
|
-
|
|
105
|
+
if (server.auth) server.auth = expandEnvVarsDeep(server.auth);
|
|
106
|
+
if (server.oauth) server.oauth = expandEnvVarsDeep(server.oauth);
|
|
97
107
|
servers.push(server);
|
|
98
108
|
}
|
|
99
109
|
}
|
package/src/ipy/prelude.py
CHANGED
|
@@ -305,23 +305,40 @@ if "__omp_prelude_loaded__" not in globals():
|
|
|
305
305
|
|
|
306
306
|
class ShellResult:
|
|
307
307
|
"""Result from shell command execution."""
|
|
308
|
-
__slots__ = ("stdout", "stderr", "
|
|
309
|
-
def __init__(self, stdout: str, stderr: str,
|
|
308
|
+
__slots__ = ("args", "stdout", "stderr", "returncode")
|
|
309
|
+
def __init__(self, args: str, stdout: str, stderr: str, returncode: int):
|
|
310
|
+
self.args = args
|
|
310
311
|
self.stdout = stdout
|
|
311
312
|
self.stderr = stderr
|
|
312
|
-
self.
|
|
313
|
+
self.returncode = returncode
|
|
314
|
+
|
|
315
|
+
@property
|
|
316
|
+
def code(self) -> int:
|
|
317
|
+
return self.returncode
|
|
318
|
+
|
|
319
|
+
@property
|
|
320
|
+
def exit_code(self) -> int:
|
|
321
|
+
return self.returncode
|
|
322
|
+
|
|
323
|
+
def check_returncode(self) -> None:
|
|
324
|
+
if self.returncode != 0:
|
|
325
|
+
raise subprocess.CalledProcessError(
|
|
326
|
+
self.returncode, self.args, output=self.stdout, stderr=self.stderr
|
|
327
|
+
)
|
|
328
|
+
|
|
313
329
|
def __repr__(self):
|
|
314
|
-
if self.
|
|
330
|
+
if self.returncode == 0:
|
|
315
331
|
return ""
|
|
316
|
-
return f"exit code {self.
|
|
332
|
+
return f"exit code {self.returncode}"
|
|
333
|
+
|
|
317
334
|
def __bool__(self):
|
|
318
|
-
return self.
|
|
335
|
+
return self.returncode == 0
|
|
319
336
|
|
|
320
337
|
def _make_shell_result(proc: subprocess.CompletedProcess[str], cmd: str) -> ShellResult:
|
|
321
338
|
"""Create ShellResult and emit status."""
|
|
322
339
|
output = proc.stdout + proc.stderr if proc.stderr else proc.stdout
|
|
323
340
|
_emit_status("sh", cmd=cmd[:80], code=proc.returncode, output=output[:500])
|
|
324
|
-
return ShellResult(proc.stdout, proc.stderr, proc.returncode)
|
|
341
|
+
return ShellResult(cmd, proc.stdout, proc.stderr, proc.returncode)
|
|
325
342
|
|
|
326
343
|
import signal as _signal
|
|
327
344
|
|
|
@@ -356,7 +373,7 @@ if "__omp_prelude_loaded__" not in globals():
|
|
|
356
373
|
|
|
357
374
|
@_category("Shell")
|
|
358
375
|
def run(cmd: str, *, cwd: str | Path | None = None, timeout: int | None = None) -> ShellResult:
|
|
359
|
-
"""Run a shell command."""
|
|
376
|
+
"""Run a shell command. Returns ShellResult with stdout/stderr and returncode/exit_code fields."""
|
|
360
377
|
shell_path = shutil.which("bash") or shutil.which("sh") or "/bin/sh"
|
|
361
378
|
args = [shell_path, "-c", cmd]
|
|
362
379
|
return _run_with_interrupt(args, str(cwd) if cwd else None, timeout, cmd)
|
package/src/mcp/oauth-flow.ts
CHANGED
|
@@ -6,11 +6,97 @@
|
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
import type { OAuthController, OAuthCredentials } from "@oh-my-pi/pi-ai";
|
|
9
|
+
import type { OAuthCallbackFlowOptions } from "@oh-my-pi/pi-ai/utils/oauth/callback-server";
|
|
9
10
|
import { OAuthCallbackFlow } from "@oh-my-pi/pi-ai/utils/oauth/callback-server";
|
|
10
11
|
|
|
11
12
|
const DEFAULT_PORT = 3000;
|
|
12
13
|
const CALLBACK_PATH = "/callback";
|
|
13
14
|
|
|
15
|
+
function isLoopbackHostname(hostname: string): boolean {
|
|
16
|
+
return hostname === "localhost" || hostname === "127.0.0.1";
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function resolveRedirectUri(redirectUri: string | undefined): string | undefined {
|
|
20
|
+
const configured = redirectUri;
|
|
21
|
+
const trimmed = configured?.trim();
|
|
22
|
+
if (!trimmed) return undefined;
|
|
23
|
+
if (trimmed !== configured) {
|
|
24
|
+
throw new Error("OAuth redirect URI must not include surrounding whitespace");
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const parsed = new URL(configured);
|
|
28
|
+
if (parsed.protocol !== "http:" && parsed.protocol !== "https:") {
|
|
29
|
+
throw new Error("OAuth redirect URI must use http or https");
|
|
30
|
+
}
|
|
31
|
+
return configured;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function parseRedirectUri(redirectUri: string | undefined): URL | undefined {
|
|
35
|
+
return redirectUri ? new URL(redirectUri) : undefined;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function getUriPort(uri: URL): number {
|
|
39
|
+
if (uri.port !== "") return Number(uri.port);
|
|
40
|
+
return uri.protocol === "https:" ? 443 : 80;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function validateRedirectConfig(config: MCPOAuthConfig, redirectUri: string | undefined): void {
|
|
44
|
+
const parsed = parseRedirectUri(redirectUri);
|
|
45
|
+
if (!parsed || parsed.protocol !== "https:" || !isLoopbackHostname(parsed.hostname)) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (config.callbackPort === undefined) {
|
|
50
|
+
throw new Error(
|
|
51
|
+
"HTTPS loopback redirect URIs require oauth.callbackPort to point at the local HTTP callback listener behind your TLS terminator",
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (config.callbackPort === getUriPort(parsed)) {
|
|
56
|
+
throw new Error(
|
|
57
|
+
"HTTPS loopback redirect URIs cannot reuse the same local port; terminate TLS separately and forward to oauth.callbackPort",
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function resolveCallbackPort(callbackPort: number | undefined, redirectUri: string | undefined): number {
|
|
63
|
+
if (callbackPort !== undefined) return callbackPort;
|
|
64
|
+
|
|
65
|
+
const parsed = parseRedirectUri(redirectUri);
|
|
66
|
+
if (!parsed || parsed.protocol !== "http:" || !isLoopbackHostname(parsed.hostname)) {
|
|
67
|
+
return DEFAULT_PORT;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const port = getUriPort(parsed);
|
|
71
|
+
return Number.isFinite(port) && port > 0 ? port : DEFAULT_PORT;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function resolveCallbackPath(callbackPath: string | undefined, redirectUri: string | undefined): string {
|
|
75
|
+
const trimmed = callbackPath?.trim();
|
|
76
|
+
if (trimmed) return trimmed.startsWith("/") ? trimmed : `/${trimmed}`;
|
|
77
|
+
|
|
78
|
+
const parsed = parseRedirectUri(redirectUri);
|
|
79
|
+
if (parsed?.pathname) return parsed.pathname;
|
|
80
|
+
return CALLBACK_PATH;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function resolveCallbackHostname(redirectUri: string | undefined): string | undefined {
|
|
84
|
+
const parsed = parseRedirectUri(redirectUri);
|
|
85
|
+
if (!parsed || !isLoopbackHostname(parsed.hostname)) return undefined;
|
|
86
|
+
return parsed.hostname;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function resolveCallbackOptions(config: MCPOAuthConfig): OAuthCallbackFlowOptions {
|
|
90
|
+
const redirectUri = resolveRedirectUri(config.redirectUri);
|
|
91
|
+
validateRedirectConfig(config, redirectUri);
|
|
92
|
+
return {
|
|
93
|
+
preferredPort: resolveCallbackPort(config.callbackPort, redirectUri),
|
|
94
|
+
callbackPath: resolveCallbackPath(config.callbackPath, redirectUri),
|
|
95
|
+
callbackHostname: resolveCallbackHostname(redirectUri),
|
|
96
|
+
redirectUri,
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
|
|
14
100
|
export interface MCPOAuthConfig {
|
|
15
101
|
/** Authorization endpoint URL */
|
|
16
102
|
authorizationUrl: string;
|
|
@@ -22,8 +108,12 @@ export interface MCPOAuthConfig {
|
|
|
22
108
|
clientSecret?: string;
|
|
23
109
|
/** OAuth scopes (space-separated) */
|
|
24
110
|
scopes?: string;
|
|
111
|
+
/** Exact redirect URI to advertise to the provider */
|
|
112
|
+
redirectUri?: string;
|
|
25
113
|
/** Custom callback port (default: 3000) */
|
|
26
114
|
callbackPort?: number;
|
|
115
|
+
/** Custom callback path (default: /callback or redirectUri pathname) */
|
|
116
|
+
callbackPath?: string;
|
|
27
117
|
}
|
|
28
118
|
|
|
29
119
|
/**
|
|
@@ -39,7 +129,7 @@ export class MCPOAuthFlow extends OAuthCallbackFlow {
|
|
|
39
129
|
private config: MCPOAuthConfig,
|
|
40
130
|
ctrl: OAuthController,
|
|
41
131
|
) {
|
|
42
|
-
super(ctrl, config
|
|
132
|
+
super(ctrl, resolveCallbackOptions(config));
|
|
43
133
|
this.#resolvedClientId = this.#resolveClientId(config);
|
|
44
134
|
}
|
|
45
135
|
|
package/src/mcp/types.ts
CHANGED
|
@@ -68,7 +68,10 @@ interface MCPServerConfigBase {
|
|
|
68
68
|
/** OAuth configuration for servers requiring explicit client credentials */
|
|
69
69
|
oauth?: {
|
|
70
70
|
clientId?: string;
|
|
71
|
+
clientSecret?: string;
|
|
72
|
+
redirectUri?: string;
|
|
71
73
|
callbackPort?: number;
|
|
74
|
+
callbackPath?: string;
|
|
72
75
|
};
|
|
73
76
|
}
|
|
74
77
|
|
|
@@ -4,6 +4,7 @@ import {
|
|
|
4
4
|
Input,
|
|
5
5
|
matchesKey,
|
|
6
6
|
padding,
|
|
7
|
+
replaceTabs,
|
|
7
8
|
Spacer,
|
|
8
9
|
Text,
|
|
9
10
|
truncateToWidth,
|
|
@@ -13,6 +14,7 @@ import { theme } from "../../modes/theme/theme";
|
|
|
13
14
|
import type { SessionInfo } from "../../session/session-manager";
|
|
14
15
|
import { fuzzyFilter } from "../../utils/fuzzy";
|
|
15
16
|
import { DynamicBorder } from "./dynamic-border";
|
|
17
|
+
import { HookSelectorComponent } from "./hook-selector";
|
|
16
18
|
|
|
17
19
|
/**
|
|
18
20
|
* Custom session list component with multi-line items and search
|
|
@@ -26,6 +28,8 @@ class SessionList implements Component {
|
|
|
26
28
|
onExit: () => void = () => {};
|
|
27
29
|
#maxVisible: number = 5; // Max sessions visible (each session is 3 lines: msg + metadata + blank)
|
|
28
30
|
|
|
31
|
+
onDeleteRequest?: (session: SessionInfo) => void;
|
|
32
|
+
|
|
29
33
|
constructor(
|
|
30
34
|
private readonly allSessions: SessionInfo[],
|
|
31
35
|
private readonly showCwd = false,
|
|
@@ -59,6 +63,18 @@ class SessionList implements Component {
|
|
|
59
63
|
this.#selectedIndex = Math.min(this.#selectedIndex, Math.max(0, this.#filteredSessions.length - 1));
|
|
60
64
|
}
|
|
61
65
|
|
|
66
|
+
removeSession(sessionPath: string): void {
|
|
67
|
+
const index = this.allSessions.findIndex(s => s.path === sessionPath);
|
|
68
|
+
if (index === -1) return;
|
|
69
|
+
this.allSessions.splice(index, 1);
|
|
70
|
+
// Re-filter to update filteredSessions
|
|
71
|
+
this.#filterSessions(this.#searchInput.getValue());
|
|
72
|
+
// Adjust selectedIndex if we deleted the last item or beyond
|
|
73
|
+
if (this.#selectedIndex >= this.#filteredSessions.length) {
|
|
74
|
+
this.#selectedIndex = Math.max(0, this.#filteredSessions.length - 1);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
62
78
|
invalidate(): void {
|
|
63
79
|
// No cached state to invalidate currently
|
|
64
80
|
}
|
|
@@ -157,78 +173,105 @@ class SessionList implements Component {
|
|
|
157
173
|
lines.push(scrollInfo);
|
|
158
174
|
}
|
|
159
175
|
|
|
176
|
+
// Add keybinding hint
|
|
177
|
+
lines.push("");
|
|
178
|
+
lines.push(theme.fg("muted", " [Del to delete, Enter to select, Esc to cancel]"));
|
|
179
|
+
|
|
160
180
|
return lines;
|
|
161
181
|
}
|
|
162
182
|
|
|
163
183
|
handleInput(keyData: string): void {
|
|
184
|
+
// Delete key - request delete confirmation from parent
|
|
185
|
+
if (matchesKey(keyData, "delete")) {
|
|
186
|
+
const selected = this.#filteredSessions[this.#selectedIndex];
|
|
187
|
+
if (selected && this.onDeleteRequest) {
|
|
188
|
+
this.onDeleteRequest(selected);
|
|
189
|
+
}
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
|
|
164
193
|
// Up arrow
|
|
165
194
|
if (matchesKey(keyData, "up")) {
|
|
166
195
|
this.#selectedIndex = Math.max(0, this.#selectedIndex - 1);
|
|
196
|
+
return;
|
|
167
197
|
}
|
|
168
198
|
// Down arrow
|
|
169
|
-
|
|
199
|
+
if (matchesKey(keyData, "down")) {
|
|
170
200
|
this.#selectedIndex = Math.min(this.#filteredSessions.length - 1, this.#selectedIndex + 1);
|
|
201
|
+
return;
|
|
171
202
|
}
|
|
172
203
|
// Page up - jump up by maxVisible items
|
|
173
|
-
|
|
204
|
+
if (matchesKey(keyData, "pageUp")) {
|
|
174
205
|
this.#selectedIndex = Math.max(0, this.#selectedIndex - this.#maxVisible);
|
|
206
|
+
return;
|
|
175
207
|
}
|
|
176
208
|
// Page down - jump down by maxVisible items
|
|
177
|
-
|
|
209
|
+
if (matchesKey(keyData, "pageDown")) {
|
|
178
210
|
this.#selectedIndex = Math.min(this.#filteredSessions.length - 1, this.#selectedIndex + this.#maxVisible);
|
|
211
|
+
return;
|
|
179
212
|
}
|
|
180
213
|
// Enter
|
|
181
|
-
|
|
214
|
+
if (matchesKey(keyData, "enter") || matchesKey(keyData, "return") || keyData === "\n") {
|
|
182
215
|
const selected = this.#filteredSessions[this.#selectedIndex];
|
|
183
216
|
if (selected && this.onSelect) {
|
|
184
217
|
this.onSelect(selected.path);
|
|
185
218
|
}
|
|
219
|
+
return;
|
|
186
220
|
}
|
|
187
221
|
// Escape - cancel
|
|
188
|
-
|
|
222
|
+
if (matchesKey(keyData, "escape") || matchesKey(keyData, "esc")) {
|
|
189
223
|
if (this.onCancel) {
|
|
190
224
|
this.onCancel();
|
|
191
225
|
}
|
|
226
|
+
return;
|
|
192
227
|
}
|
|
193
228
|
// Ctrl+C - exit
|
|
194
|
-
|
|
229
|
+
if (matchesKey(keyData, "ctrl+c")) {
|
|
195
230
|
this.onExit();
|
|
231
|
+
return;
|
|
196
232
|
}
|
|
197
233
|
// Pass everything else to search input
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
this.#filterSessions(this.#searchInput.getValue());
|
|
201
|
-
}
|
|
234
|
+
this.#searchInput.handleInput(keyData);
|
|
235
|
+
this.#filterSessions(this.#searchInput.getValue());
|
|
202
236
|
}
|
|
203
237
|
}
|
|
204
238
|
|
|
205
239
|
/**
|
|
206
|
-
* Component that renders a session selector
|
|
240
|
+
* Component that renders a session selector with optional confirmation dialog
|
|
207
241
|
*/
|
|
208
242
|
export class SessionSelectorComponent extends Container {
|
|
209
243
|
#sessionList: SessionList;
|
|
244
|
+
#confirmationDialog: HookSelectorComponent | null = null;
|
|
245
|
+
#messageContainer: Container;
|
|
246
|
+
#onDelete?: (session: SessionInfo) => Promise<boolean>;
|
|
247
|
+
#onRequestRender?: () => void;
|
|
210
248
|
|
|
211
249
|
constructor(
|
|
212
250
|
sessions: SessionInfo[],
|
|
213
251
|
onSelect: (sessionPath: string) => void,
|
|
214
252
|
onCancel: () => void,
|
|
215
253
|
onExit: () => void,
|
|
254
|
+
onDelete?: (session: SessionInfo) => Promise<boolean>,
|
|
216
255
|
) {
|
|
217
256
|
super();
|
|
218
257
|
|
|
258
|
+
this.#messageContainer = new Container();
|
|
259
|
+
this.#onDelete = onDelete;
|
|
219
260
|
// Add header
|
|
220
261
|
this.addChild(new Spacer(1));
|
|
221
262
|
this.addChild(new Text(theme.bold("Resume Session"), 1, 0));
|
|
222
263
|
this.addChild(new Spacer(1));
|
|
223
264
|
this.addChild(new DynamicBorder());
|
|
224
265
|
this.addChild(new Spacer(1));
|
|
225
|
-
|
|
266
|
+
this.addChild(this.#messageContainer);
|
|
226
267
|
// Create session list
|
|
227
268
|
this.#sessionList = new SessionList(sessions);
|
|
228
269
|
this.#sessionList.onSelect = onSelect;
|
|
229
270
|
this.#sessionList.onCancel = onCancel;
|
|
230
271
|
this.#sessionList.onExit = onExit;
|
|
231
|
-
|
|
272
|
+
this.#sessionList.onDeleteRequest = (session: SessionInfo) => {
|
|
273
|
+
this.#showDeleteConfirmation(session);
|
|
274
|
+
};
|
|
232
275
|
this.addChild(this.#sessionList);
|
|
233
276
|
|
|
234
277
|
// Add bottom border
|
|
@@ -236,6 +279,63 @@ export class SessionSelectorComponent extends Container {
|
|
|
236
279
|
this.addChild(new DynamicBorder());
|
|
237
280
|
}
|
|
238
281
|
|
|
282
|
+
setOnRequestRender(callback: () => void): void {
|
|
283
|
+
this.#onRequestRender = callback;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
#clearError(): void {
|
|
287
|
+
this.#messageContainer.clear();
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
#showError(message: string): void {
|
|
291
|
+
this.#messageContainer.clear();
|
|
292
|
+
this.#messageContainer.addChild(new Text(theme.fg("error", `Error: ${replaceTabs(message)}`), 1, 0));
|
|
293
|
+
this.#messageContainer.addChild(new Spacer(1));
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
#showDeleteConfirmation(session: SessionInfo): void {
|
|
297
|
+
const displayName = session.title || session.firstMessage.slice(0, 40) || session.id;
|
|
298
|
+
this.#confirmationDialog = new HookSelectorComponent(
|
|
299
|
+
`Delete session?\n${displayName}`,
|
|
300
|
+
["Yes", "No"],
|
|
301
|
+
async (option: string) => {
|
|
302
|
+
if (option === "Yes" && this.#onDelete) {
|
|
303
|
+
this.#clearError();
|
|
304
|
+
try {
|
|
305
|
+
const deleted = await this.#onDelete(session);
|
|
306
|
+
if (deleted) {
|
|
307
|
+
this.#sessionList.removeSession(session.path);
|
|
308
|
+
}
|
|
309
|
+
} catch (err) {
|
|
310
|
+
this.#showError(err instanceof Error ? err.message : String(err));
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
// Close confirmation dialog
|
|
314
|
+
this.removeChild(this.#confirmationDialog!);
|
|
315
|
+
this.#confirmationDialog = null;
|
|
316
|
+
// Request rerender
|
|
317
|
+
this.#onRequestRender?.();
|
|
318
|
+
},
|
|
319
|
+
() => {
|
|
320
|
+
// Cancel - close confirmation dialog
|
|
321
|
+
this.removeChild(this.#confirmationDialog!);
|
|
322
|
+
this.#confirmationDialog = null;
|
|
323
|
+
// Request rerender
|
|
324
|
+
this.#onRequestRender?.();
|
|
325
|
+
},
|
|
326
|
+
);
|
|
327
|
+
// Show confirmation dialog
|
|
328
|
+
this.addChild(this.#confirmationDialog);
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
handleInput(keyData: string): void {
|
|
332
|
+
if (this.#confirmationDialog) {
|
|
333
|
+
this.#confirmationDialog.handleInput(keyData);
|
|
334
|
+
} else {
|
|
335
|
+
this.#sessionList.handleInput(keyData);
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
|
|
239
339
|
getSessionList(): SessionList {
|
|
240
340
|
return this.#sessionList;
|
|
241
341
|
}
|