Files
Netcatty/domain/codingCliProviderMatch.ts
T
陈大猫andCursor c6bdcffb89 feat: dynamic terminal tab titles (fixes #1572) (#1576)
* feat: add dynamic terminal tab titles for shell-reported session names

Follow OSC 0/2 window titles on session tabs so tools like Claude Code can show live session state, with a per-host opt-out to keep connection names instead.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix: address dynamic tab title review findings

Restore the missing cn import, clear dynamic titles when the shell sends an empty OSC title, and drop stored dynamic titles when a host disables the feature.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix: preserve renamed session labels and align focus sidebar titles

Prefer user-assigned customName over shell titles, skip no-op dynamic title updates, and use resolveSessionTabTitle in the workspace focus sidebar.

Co-authored-by: Cursor <cursoragent@cursor.com>

* feat: show coding agent icons on session tabs

Detect Claude Code, Codex, OpenCode, Droid, and other CLIs from OSC titles and launch commands, reusing shared agent icon assets inspired by freshell pane icons.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix: harden coding CLI detection across SSH and startup paths

Wire output scanning through attachSessionToTerminal, seed providers from
auto startup commands, prevent title signals from overriding command
detection, clear sticky state on shell titles, and release output scanners
on session close.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix: reset CLI output scanner when provider state clears

Prevent exited-agent banners from immediately re-sticking the tab icon
after the shell title returns to a normal shell name.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix: update sticky CLI provider when title signals a different agent

Allow agent switches in the same terminal while still resetting the output
scanner whenever the provider identity changes.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix: avoid clearing CLI provider on blank titles and cap output scan

Ignore transient empty OSC titles and stop banner scanning after 16KB of
unmatched output so regular SSH sessions do not pay perpetual scan cost.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix: prefer runtime CLI provider over host startup command on tabs

Runtime detection from commands, output, and titles should win over static
host startup commands when resolving the session tab icon.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix: satisfy eslint for terminal escape stripping regexes

Co-authored-by: Cursor <cursoragent@cursor.com>

---------

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-23 14:20:05 +08:00

138 lines
4.2 KiB
TypeScript

import { CODING_CLI_PROVIDERS, getCodingCliProvider, type CodingCliProvider, type CodingCliProviderId } from './codingCliProviders';
import {
inferCodingCliProviderFromTitleSignals,
titleIncludesPhrase,
} from './codingCliTitleParse';
import type { Host, TerminalSession } from '../types';
import { isDynamicTabTitleDisabled } from './sessionTabTitle';
export type SessionCodingCliSource = Pick<
TerminalSession,
| 'dynamicTitle'
| 'startupCommand'
| 'customName'
| 'hostLabel'
| 'localShell'
| 'localShellName'
| 'codingCliProviderId'
> & {
hostStartupCommand?: Host['startupCommand'];
};
export function getCodingCliCommandBasename(commandLine: string): string {
const trimmed = commandLine.trim();
if (!trimmed) return '';
const firstToken = trimmed.split(/\s+/)[0] ?? '';
const segments = firstToken.split(/[\\/]/);
const basename = (segments.pop() || '').toLowerCase();
return basename.replace(/\.(exe|cmd|bat|ps1)$/i, '');
}
export function matchCodingCliProviderFromCommand(commandLine: string): CodingCliProvider | undefined {
const basename = getCodingCliCommandBasename(commandLine);
if (!basename) return undefined;
return CODING_CLI_PROVIDERS.find((provider) => (
provider.command === basename
|| provider.aliases?.some((alias) => alias === basename)
));
}
export function matchCodingCliProviderFromTitle(title: string): CodingCliProvider | undefined {
const inferredId = inferCodingCliProviderFromTitleSignals(title);
if (inferredId) {
return getCodingCliProvider(inferredId);
}
const normalized = title.toLowerCase();
if (!normalized.trim()) return undefined;
const ranked = [...CODING_CLI_PROVIDERS].sort((left, right) => {
const leftHints = [
...(left.titleHints ?? []),
left.label,
left.command,
...(left.aliases ?? []),
];
const rightHints = [
...(right.titleHints ?? []),
right.label,
right.command,
...(right.aliases ?? []),
];
const leftMax = Math.max(...leftHints.map((hint) => hint.length), 0);
const rightMax = Math.max(...rightHints.map((hint) => hint.length), 0);
return rightMax - leftMax;
});
for (const provider of ranked) {
const hints = [
...(provider.titleHints ?? []),
provider.label,
provider.command,
...(provider.aliases ?? []),
];
if (hints.some((hint) => titleIncludesPhrase(normalized, hint))) {
return provider;
}
}
return undefined;
}
/**
* Resolve the active coding CLI for a terminal session from launch commands
* and shell-reported window titles.
*/
export function resolveCodingCliProviderFromCommandCandidates(
source: Pick<SessionCodingCliSource, 'startupCommand' | 'localShell'>,
host?: Pick<Host, 'startupCommand'>,
): CodingCliProvider | undefined {
const commandCandidates = [
source.startupCommand,
host?.startupCommand,
source.localShell,
].filter((value): value is string => Boolean(value?.trim()));
for (const commandLine of commandCandidates) {
const provider = matchCodingCliProviderFromCommand(commandLine);
if (provider) return provider;
}
return undefined;
}
export function resolveSessionCodingCliProvider(
source: SessionCodingCliSource,
host?: Pick<Host, 'disableDynamicTabTitle' | 'startupCommand'>,
): CodingCliProvider | undefined {
if (source.codingCliProviderId) {
const sticky = getCodingCliProvider(source.codingCliProviderId);
if (sticky) return sticky;
}
const commandProvider = resolveCodingCliProviderFromCommandCandidates(source, host);
if (commandProvider) return commandProvider;
if (!isDynamicTabTitleDisabled(host) && !source.customName) {
const dynamicTitle = source.dynamicTitle?.trim();
if (dynamicTitle) {
const provider = matchCodingCliProviderFromTitle(dynamicTitle);
if (provider) return provider;
}
}
if (source.localShellName) {
return matchCodingCliProviderFromTitle(source.localShellName);
}
return undefined;
}
export function resolveSessionCodingCliProviderId(
source: SessionCodingCliSource,
host?: Pick<Host, 'disableDynamicTabTitle' | 'startupCommand'>,
): CodingCliProviderId | undefined {
return resolveSessionCodingCliProvider(source, host)?.id;
}