mux-sesh 1.4.1 → 1.6.0
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/README.md +123 -7
- package/package.json +3 -3
- package/src/app.tsx +746 -173
- package/src/components/KeybindHelp.tsx +178 -111
- package/src/config/index.ts +413 -20
- package/src/config/listed-sessions.ts +36 -0
- package/src/config/session-rules.ts +101 -0
- package/src/handlers/actions.ts +93 -6
- package/src/handlers/keyboard.ts +299 -79
- package/src/items/order.ts +50 -0
- package/src/opencode/session.ts +1 -17
- package/src/opencode/stats.ts +1 -1
- package/src/preview/project.ts +235 -0
- package/src/projects/session-links.ts +31 -0
- package/src/search/index.ts +83 -47
- package/src/settings.ts +425 -0
- package/src/styles/opencode-themes-a.ts +1 -0
- package/src/styles/opencode-themes-b.ts +1 -0
- package/src/styles/theme.tsx +441 -0
- package/src/tmux/index.ts +58 -14
- package/src/tmux/projects.ts +151 -20
- package/src/tmux/workflows.ts +22 -0
- package/src/types/index.ts +114 -0
- package/src/ui/CommandsModal.tsx +145 -0
- package/src/ui/HighlightedText.tsx +6 -3
- package/src/ui/ItemList.tsx +47 -41
- package/src/ui/Modal.tsx +41 -0
- package/src/ui/OpencodeSessionGroup.tsx +50 -46
- package/src/ui/OpencodeStatsPanel.tsx +34 -39
- package/src/ui/RenameModal.tsx +42 -0
- package/src/ui/SearchInput.tsx +3 -1
- package/src/ui/SessionDetailsPanel.tsx +168 -72
- package/src/ui/SessionList.tsx +59 -34
- package/src/ui/SessionStats.tsx +5 -3
- package/src/ui/SettingEditorModal.tsx +49 -0
- package/src/ui/SettingOptionsModal.tsx +77 -0
- package/src/ui/SettingsModal.tsx +96 -0
- package/src/ui/Toast.tsx +8 -9
- package/src/ui/item-icon.ts +78 -0
- package/src/ui/list-window.ts +22 -0
- package/src/update/installation.ts +4 -15
- package/src/update/version.ts +2 -1
- package/src/util/glob.ts +47 -0
- package/src/util/terminal.ts +6 -20
- package/src/styles/theme.ts +0 -50
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { Item, SortOrder } from '../types'
|
|
2
|
+
|
|
3
|
+
function compareByTitle(left: Item, right: Item): number {
|
|
4
|
+
return left.title.localeCompare(right.title)
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
function isOpencodeSession(item: Item): boolean {
|
|
8
|
+
return item.isSession && item.title.startsWith('opencode-')
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function isConfiguredSession(item: Item): boolean {
|
|
12
|
+
return item.itemKind === 'configured'
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function sortByGroup(items: Item[], groups: Array<'live' | 'configured'>): Item[] {
|
|
16
|
+
const configuredItems = items.filter(isConfiguredSession).sort(compareByTitle)
|
|
17
|
+
const liveItems = items.filter(item => !isConfiguredSession(item)).sort(compareByTitle)
|
|
18
|
+
const groupedItems = new Map([
|
|
19
|
+
['configured', configuredItems],
|
|
20
|
+
['live', liveItems],
|
|
21
|
+
])
|
|
22
|
+
|
|
23
|
+
return groups.flatMap(group => groupedItems.get(group) ?? [])
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function orderSessionItems(items: Item[], sortOrder: SortOrder = 'live-first'): Item[] {
|
|
27
|
+
const opencodeItems = items.filter(isOpencodeSession).sort(compareByTitle)
|
|
28
|
+
const regularItems = items.filter(item => !isOpencodeSession(item))
|
|
29
|
+
|
|
30
|
+
const orderedRegularItems =
|
|
31
|
+
sortOrder === 'configured-first'
|
|
32
|
+
? sortByGroup(regularItems, ['configured', 'live'])
|
|
33
|
+
: sortOrder === 'alphabetical'
|
|
34
|
+
? [...regularItems].sort(compareByTitle)
|
|
35
|
+
: sortByGroup(regularItems, ['live', 'configured'])
|
|
36
|
+
|
|
37
|
+
return [...orderedRegularItems, ...opencodeItems]
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function orderProjectItems(items: Item[], sortOrder: SortOrder = 'live-first'): Item[] {
|
|
41
|
+
if (sortOrder === 'alphabetical') {
|
|
42
|
+
return [...items].sort(compareByTitle)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return items
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function getSessionSection(item: Item): 'live' | 'configured' {
|
|
49
|
+
return item.itemKind === 'configured' ? 'configured' : 'live'
|
|
50
|
+
}
|
package/src/opencode/session.ts
CHANGED
|
@@ -10,6 +10,7 @@ import {
|
|
|
10
10
|
} from './sqlite'
|
|
11
11
|
import { getLastMessageTokens } from './tokens'
|
|
12
12
|
import type { OpencodeStats } from './types'
|
|
13
|
+
import { getTmuxSessionDirectory } from '../tmux'
|
|
13
14
|
|
|
14
15
|
function toSummaryValue(value: number | null): number {
|
|
15
16
|
return value ?? 0
|
|
@@ -38,23 +39,6 @@ async function buildSessionStats(row: OpencodeSessionRow): Promise<OpencodeStats
|
|
|
38
39
|
}
|
|
39
40
|
}
|
|
40
41
|
|
|
41
|
-
export async function getTmuxSessionDirectory(sessionName: string): Promise<string> {
|
|
42
|
-
const proc = spawn(['tmux', 'display-message', '-t', sessionName, '-p', '#{pane_current_path}'])
|
|
43
|
-
const output = await new Response(proc.stdout).text()
|
|
44
|
-
await proc.exited
|
|
45
|
-
|
|
46
|
-
if (proc.exitCode !== 0) {
|
|
47
|
-
throw new Error(`Failed to resolve tmux directory for session '${sessionName}'`)
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
const directory = output.trim()
|
|
51
|
-
if (!directory) {
|
|
52
|
-
throw new Error(`tmux returned an empty directory for session '${sessionName}'`)
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
return directory
|
|
56
|
-
}
|
|
57
|
-
|
|
58
42
|
export async function getMessageCount(sessionID: string): Promise<number> {
|
|
59
43
|
if (!sessionID) {
|
|
60
44
|
return 0
|
package/src/opencode/stats.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import {
|
|
2
|
-
getTmuxSessionDirectory,
|
|
3
2
|
getActiveOpencodeSession,
|
|
4
3
|
getAggregatedOpencodeStats,
|
|
5
4
|
getMostRecentSessionForDirectory,
|
|
6
5
|
} from './session'
|
|
7
6
|
import type { OpencodeStats } from './types'
|
|
7
|
+
import { getTmuxSessionDirectory } from '../tmux'
|
|
8
8
|
|
|
9
9
|
export async function getOpencodeSessionStats(
|
|
10
10
|
tmuxSessionName: string
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
import { readdir } from 'fs/promises'
|
|
2
|
+
import { basename, join } from 'path'
|
|
3
|
+
import { spawn } from 'bun'
|
|
4
|
+
import { getSessionDetails } from '../tmux'
|
|
5
|
+
import type { Config, SessionDetails } from '../types'
|
|
6
|
+
import { getGitRoot, resolveProjectSession } from '../config/session-rules'
|
|
7
|
+
|
|
8
|
+
const MAX_PREVIEW_LINES = 24
|
|
9
|
+
const MAX_PREVIEW_LINE_LENGTH = 120
|
|
10
|
+
const PREVIEW_COMMAND_TIMEOUT_MS = 1500
|
|
11
|
+
|
|
12
|
+
export interface ProjectPreview {
|
|
13
|
+
path: string
|
|
14
|
+
sessionName: string
|
|
15
|
+
source: 'project' | 'wildcard' | 'default'
|
|
16
|
+
startupCommand?: string
|
|
17
|
+
previewCommand?: string
|
|
18
|
+
gitRoot?: string
|
|
19
|
+
gitBranch?: string
|
|
20
|
+
linkedSession?: SessionDetails
|
|
21
|
+
previewKind: 'command' | 'directory'
|
|
22
|
+
previewLabel: string
|
|
23
|
+
previewLines: string[]
|
|
24
|
+
previewNotice?: string
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
interface FormattedPreviewOutput {
|
|
28
|
+
lines: string[]
|
|
29
|
+
truncated: boolean
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
interface PackageSummary {
|
|
33
|
+
name?: string
|
|
34
|
+
scripts: string[]
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function displayPath(path: string): string {
|
|
38
|
+
return path.replace(process.env.HOME || '', '~')
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function stripAnsi(value: string): string {
|
|
42
|
+
return value.replace(/\u001b\[[0-9;?]*[ -/]*[@-~]/g, '')
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function interpolatePreviewCommand(command: string | undefined, projectPath: string): string | undefined {
|
|
46
|
+
if (!command) {
|
|
47
|
+
return undefined
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return command.replaceAll('{}', projectPath)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function formatPreviewOutput(output: string, maxLines = MAX_PREVIEW_LINES): FormattedPreviewOutput {
|
|
54
|
+
const cleanedLines = stripAnsi(output)
|
|
55
|
+
.replace(/\r/g, '')
|
|
56
|
+
.split('\n')
|
|
57
|
+
.map(line => line.trimEnd())
|
|
58
|
+
.filter(line => line.length > 0)
|
|
59
|
+
.map(line => (line.length > MAX_PREVIEW_LINE_LENGTH ? `${line.slice(0, MAX_PREVIEW_LINE_LENGTH - 1)}…` : line))
|
|
60
|
+
|
|
61
|
+
if (cleanedLines.length === 0) {
|
|
62
|
+
return {
|
|
63
|
+
lines: [],
|
|
64
|
+
truncated: false,
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const truncated = cleanedLines.length > maxLines
|
|
69
|
+
|
|
70
|
+
return {
|
|
71
|
+
lines: truncated ? cleanedLines.slice(0, maxLines) : cleanedLines,
|
|
72
|
+
truncated,
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async function getGitBranch(projectPath: string): Promise<string | undefined> {
|
|
77
|
+
try {
|
|
78
|
+
const proc = spawn(['git', '-C', projectPath, 'branch', '--show-current'], {
|
|
79
|
+
stderr: 'pipe',
|
|
80
|
+
})
|
|
81
|
+
const output = (await new Response(proc.stdout).text()).trim()
|
|
82
|
+
await proc.exited
|
|
83
|
+
|
|
84
|
+
if (proc.exitCode !== 0 || output.length === 0) {
|
|
85
|
+
return undefined
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return output
|
|
89
|
+
} catch {
|
|
90
|
+
return undefined
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
async function readPackageSummary(projectPath: string): Promise<PackageSummary | null> {
|
|
95
|
+
try {
|
|
96
|
+
const packageJson = (await Bun.file(join(projectPath, 'package.json')).json()) as {
|
|
97
|
+
name?: string
|
|
98
|
+
scripts?: Record<string, string>
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return {
|
|
102
|
+
name: packageJson.name,
|
|
103
|
+
scripts: Object.keys(packageJson.scripts ?? {}).slice(0, 6),
|
|
104
|
+
}
|
|
105
|
+
} catch {
|
|
106
|
+
return null
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
async function getDirectoryPreviewLines(projectPath: string): Promise<string[]> {
|
|
111
|
+
try {
|
|
112
|
+
const [entries, packageSummary] = await Promise.all([
|
|
113
|
+
readdir(projectPath, { withFileTypes: true }),
|
|
114
|
+
readPackageSummary(projectPath),
|
|
115
|
+
])
|
|
116
|
+
|
|
117
|
+
const visibleEntries = entries
|
|
118
|
+
.filter(entry => !entry.name.startsWith('.'))
|
|
119
|
+
.sort((left, right) => Number(right.isDirectory()) - Number(left.isDirectory()) || left.name.localeCompare(right.name))
|
|
120
|
+
.slice(0, 16)
|
|
121
|
+
|
|
122
|
+
const lines: string[] = []
|
|
123
|
+
|
|
124
|
+
if (packageSummary?.name) {
|
|
125
|
+
lines.push(`package ${packageSummary.name}`)
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
if (packageSummary && packageSummary.scripts.length > 0) {
|
|
129
|
+
lines.push(`scripts ${packageSummary.scripts.join(', ')}`)
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (lines.length > 0) {
|
|
133
|
+
lines.push('')
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
for (const entry of visibleEntries) {
|
|
137
|
+
lines.push(entry.isDirectory() ? `${entry.name}/` : entry.name)
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
if (lines.length === 0) {
|
|
141
|
+
lines.push(basename(projectPath))
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
return lines
|
|
145
|
+
} catch {
|
|
146
|
+
return [basename(projectPath)]
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
async function runPreviewCommand(command: string, cwd: string): Promise<{ output?: string; notice?: string }> {
|
|
151
|
+
try {
|
|
152
|
+
const proc = spawn(['sh', '-lc', command], {
|
|
153
|
+
cwd,
|
|
154
|
+
stderr: 'pipe',
|
|
155
|
+
})
|
|
156
|
+
|
|
157
|
+
const stdoutPromise = new Response(proc.stdout).text()
|
|
158
|
+
let timedOut = false
|
|
159
|
+
const timeout = setTimeout(() => {
|
|
160
|
+
timedOut = true
|
|
161
|
+
proc.kill()
|
|
162
|
+
}, PREVIEW_COMMAND_TIMEOUT_MS)
|
|
163
|
+
|
|
164
|
+
await proc.exited
|
|
165
|
+
clearTimeout(timeout)
|
|
166
|
+
|
|
167
|
+
if (timedOut) {
|
|
168
|
+
return { notice: 'Preview command timed out; showing directory listing instead.' }
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const output = await stdoutPromise
|
|
172
|
+
if (proc.exitCode !== 0) {
|
|
173
|
+
return { notice: 'Preview command failed; showing directory listing instead.' }
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
return { output }
|
|
177
|
+
} catch {
|
|
178
|
+
return { notice: 'Preview command failed; showing directory listing instead.' }
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export async function getProjectPreview(
|
|
183
|
+
projectPath: string,
|
|
184
|
+
config: Config,
|
|
185
|
+
linkedSessionName?: string
|
|
186
|
+
): Promise<ProjectPreview> {
|
|
187
|
+
const [resolvedSession, gitRootPath, gitBranch, linkedSession] = await Promise.all([
|
|
188
|
+
resolveProjectSession(projectPath, config),
|
|
189
|
+
getGitRoot(projectPath),
|
|
190
|
+
getGitBranch(projectPath),
|
|
191
|
+
linkedSessionName ? getSessionDetails(linkedSessionName).catch(() => null) : Promise.resolve(null),
|
|
192
|
+
])
|
|
193
|
+
|
|
194
|
+
const resolvedPreviewCommand = interpolatePreviewCommand(resolvedSession.previewCommand, projectPath)
|
|
195
|
+
const commandResult = resolvedPreviewCommand ? await runPreviewCommand(resolvedPreviewCommand, projectPath) : undefined
|
|
196
|
+
const formattedCommandOutput = commandResult?.output ? formatPreviewOutput(commandResult.output) : undefined
|
|
197
|
+
|
|
198
|
+
if (formattedCommandOutput && formattedCommandOutput.lines.length > 0) {
|
|
199
|
+
return {
|
|
200
|
+
path: displayPath(projectPath),
|
|
201
|
+
sessionName: resolvedSession.sessionName,
|
|
202
|
+
source: resolvedSession.source,
|
|
203
|
+
startupCommand: resolvedSession.startupCommand,
|
|
204
|
+
previewCommand: resolvedPreviewCommand,
|
|
205
|
+
gitRoot: gitRootPath ? displayPath(gitRootPath) : undefined,
|
|
206
|
+
gitBranch,
|
|
207
|
+
linkedSession: linkedSession ?? undefined,
|
|
208
|
+
previewKind: 'command',
|
|
209
|
+
previewLabel: 'Preview Output',
|
|
210
|
+
previewLines: formattedCommandOutput.lines,
|
|
211
|
+
previewNotice: formattedCommandOutput.truncated
|
|
212
|
+
? 'Preview output truncated to fit the panel.'
|
|
213
|
+
: undefined,
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
const fallbackLines = await getDirectoryPreviewLines(projectPath)
|
|
218
|
+
const formattedDirectoryOutput = formatPreviewOutput(fallbackLines.join('\n'))
|
|
219
|
+
|
|
220
|
+
return {
|
|
221
|
+
path: displayPath(projectPath),
|
|
222
|
+
sessionName: resolvedSession.sessionName,
|
|
223
|
+
source: resolvedSession.source,
|
|
224
|
+
startupCommand: resolvedSession.startupCommand,
|
|
225
|
+
previewCommand: resolvedPreviewCommand,
|
|
226
|
+
gitRoot: gitRootPath ? displayPath(gitRootPath) : undefined,
|
|
227
|
+
gitBranch,
|
|
228
|
+
linkedSession: linkedSession ?? undefined,
|
|
229
|
+
previewKind: 'directory',
|
|
230
|
+
previewLabel: 'Directory Listing',
|
|
231
|
+
previewLines: formattedDirectoryOutput.lines,
|
|
232
|
+
previewNotice: commandResult?.notice ??
|
|
233
|
+
(formattedDirectoryOutput.truncated ? 'Directory listing truncated to fit the panel.' : undefined),
|
|
234
|
+
}
|
|
235
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { Config, Item } from '../types'
|
|
2
|
+
import { resolveProjectSession } from '../config/session-rules'
|
|
3
|
+
|
|
4
|
+
export async function annotateProjectItemsWithSessionLinks(
|
|
5
|
+
projectItems: Item[],
|
|
6
|
+
liveSessions: Item[],
|
|
7
|
+
config: Config
|
|
8
|
+
): Promise<Item[]> {
|
|
9
|
+
const liveSessionsByTitle = new Map(liveSessions.map(session => [session.title, session]))
|
|
10
|
+
|
|
11
|
+
return Promise.all(
|
|
12
|
+
projectItems.map(async projectItem => {
|
|
13
|
+
const resolvedSession = await resolveProjectSession(projectItem.path, config)
|
|
14
|
+
const linkedSession = liveSessionsByTitle.get(resolvedSession.sessionName)
|
|
15
|
+
|
|
16
|
+
if (!linkedSession) {
|
|
17
|
+
return {
|
|
18
|
+
...projectItem,
|
|
19
|
+
linkedSessionName: undefined,
|
|
20
|
+
linkedSessionAttached: undefined,
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return {
|
|
25
|
+
...projectItem,
|
|
26
|
+
linkedSessionName: linkedSession.title,
|
|
27
|
+
linkedSessionAttached: linkedSession.isAttached,
|
|
28
|
+
}
|
|
29
|
+
})
|
|
30
|
+
)
|
|
31
|
+
}
|
package/src/search/index.ts
CHANGED
|
@@ -1,4 +1,21 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { basename } from 'path'
|
|
2
|
+
import type { FuzzyMatch, Item, SearchMatchMetadata, SearchResult } from '../types'
|
|
3
|
+
|
|
4
|
+
function withSearchMatch(item: Item, searchMatch?: SearchMatchMetadata): Item {
|
|
5
|
+
if (!searchMatch || (!searchMatch.titleIndices && !searchMatch.descIndices)) {
|
|
6
|
+
if (!item.searchMatch) {
|
|
7
|
+
return item
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const { searchMatch: _searchMatch, ...itemWithoutSearchMatch } = item
|
|
11
|
+
return itemWithoutSearchMatch
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
return {
|
|
15
|
+
...item,
|
|
16
|
+
searchMatch,
|
|
17
|
+
}
|
|
18
|
+
}
|
|
2
19
|
|
|
3
20
|
/**
|
|
4
21
|
* Performs fuzzy matching and returns match positions and score
|
|
@@ -10,12 +27,10 @@ export function fuzzyMatch(text: string, query: string): FuzzyMatch | null {
|
|
|
10
27
|
const textLower = text.toLowerCase()
|
|
11
28
|
const queryLower = query.toLowerCase()
|
|
12
29
|
|
|
13
|
-
// Track matched character positions
|
|
14
30
|
const indices: number[] = []
|
|
15
31
|
let textIndex = 0
|
|
16
32
|
let queryIndex = 0
|
|
17
33
|
|
|
18
|
-
// Find all query characters in order
|
|
19
34
|
while (queryIndex < queryLower.length && textIndex < textLower.length) {
|
|
20
35
|
if (textLower[textIndex] === queryLower[queryIndex]) {
|
|
21
36
|
indices.push(textIndex)
|
|
@@ -24,25 +39,20 @@ export function fuzzyMatch(text: string, query: string): FuzzyMatch | null {
|
|
|
24
39
|
textIndex++
|
|
25
40
|
}
|
|
26
41
|
|
|
27
|
-
// If we didn't match all characters, no match
|
|
28
42
|
if (queryIndex !== queryLower.length) {
|
|
29
43
|
return null
|
|
30
44
|
}
|
|
31
45
|
|
|
32
|
-
// Calculate score based on match quality
|
|
33
46
|
let score = 100
|
|
34
47
|
|
|
35
|
-
// Bonus for exact match
|
|
36
48
|
if (text.toLowerCase() === query.toLowerCase()) {
|
|
37
49
|
score += 10000
|
|
38
50
|
}
|
|
39
51
|
|
|
40
|
-
// Bonus for match at start
|
|
41
52
|
if (indices[0] === 0) {
|
|
42
53
|
score += 1000
|
|
43
54
|
}
|
|
44
55
|
|
|
45
|
-
// Bonus for consecutive characters
|
|
46
56
|
let consecutiveBonus = 0
|
|
47
57
|
for (let i = 1; i < indices.length; i++) {
|
|
48
58
|
if (indices[i] === indices[i - 1] + 1) {
|
|
@@ -51,7 +61,6 @@ export function fuzzyMatch(text: string, query: string): FuzzyMatch | null {
|
|
|
51
61
|
}
|
|
52
62
|
score += consecutiveBonus
|
|
53
63
|
|
|
54
|
-
// Bonus for matches after word boundaries (-, _, ., space, /)
|
|
55
64
|
let boundaryBonus = 0
|
|
56
65
|
for (const idx of indices) {
|
|
57
66
|
if (idx > 0) {
|
|
@@ -63,86 +72,113 @@ export function fuzzyMatch(text: string, query: string): FuzzyMatch | null {
|
|
|
63
72
|
}
|
|
64
73
|
score += boundaryBonus
|
|
65
74
|
|
|
66
|
-
// Bonus for case-sensitive exact character matches
|
|
67
75
|
let caseBonus = 0
|
|
68
|
-
for (
|
|
69
|
-
if (text[
|
|
76
|
+
for (let index = 0; index < indices.length; index += 1) {
|
|
77
|
+
if (text[indices[index]] === query[index]) {
|
|
70
78
|
caseBonus += 10
|
|
71
79
|
}
|
|
72
80
|
}
|
|
73
81
|
score += caseBonus
|
|
74
82
|
|
|
75
|
-
// Penalty for early gaps (characters not matched at the beginning)
|
|
76
83
|
const firstMatchPos = indices[0]
|
|
77
84
|
score -= firstMatchPos * 5
|
|
78
|
-
|
|
79
|
-
// Penalty for length of text (shorter matches are better)
|
|
80
85
|
score -= text.length * 2
|
|
81
86
|
|
|
82
|
-
// Bonus for matching a higher percentage of the text
|
|
83
87
|
const matchPercentage = indices.length / text.length
|
|
84
88
|
score += matchPercentage * 100
|
|
85
89
|
|
|
86
90
|
return { score, indices }
|
|
87
91
|
}
|
|
88
92
|
|
|
89
|
-
|
|
93
|
+
interface ScoredSearchResult extends SearchResult {
|
|
94
|
+
item: Item
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function calculateItemBonus(item: Item, query: string): number {
|
|
98
|
+
const normalizedQuery = query.toLowerCase()
|
|
99
|
+
const title = item.title.toLowerCase()
|
|
100
|
+
const pathBaseName = basename(item.path).toLowerCase()
|
|
101
|
+
const description = item.desc.toLowerCase()
|
|
102
|
+
|
|
103
|
+
let bonus = 0
|
|
104
|
+
|
|
105
|
+
if (title === normalizedQuery || pathBaseName === normalizedQuery) {
|
|
106
|
+
bonus += 5000
|
|
107
|
+
} else if (title.startsWith(normalizedQuery) || pathBaseName.startsWith(normalizedQuery)) {
|
|
108
|
+
bonus += 1200
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (item.path.toLowerCase().endsWith(`/${normalizedQuery}`) || description.endsWith(`/${normalizedQuery}`)) {
|
|
112
|
+
bonus += 2000
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (!item.isSession) {
|
|
116
|
+
const depth = item.path.split('/').filter(Boolean).length
|
|
117
|
+
bonus -= depth * 2
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
return bonus
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function buildSearchResult(item: Item, query: string): ScoredSearchResult | null {
|
|
90
124
|
const queryTrimmed = query.trim()
|
|
91
|
-
if (queryTrimmed === '')
|
|
125
|
+
if (queryTrimmed === '') {
|
|
126
|
+
return {
|
|
127
|
+
item: withSearchMatch(item),
|
|
128
|
+
score: 1,
|
|
129
|
+
}
|
|
130
|
+
}
|
|
92
131
|
|
|
93
132
|
let bestScore = 0
|
|
94
133
|
let hasMatch = false
|
|
134
|
+
let searchMatch: SearchMatchMetadata | undefined
|
|
95
135
|
|
|
96
|
-
// Try fuzzy matching on title
|
|
97
136
|
const titleMatch = fuzzyMatch(item.title, queryTrimmed)
|
|
98
137
|
if (titleMatch && titleMatch.score > 0) {
|
|
99
|
-
|
|
138
|
+
searchMatch = {
|
|
139
|
+
...searchMatch,
|
|
140
|
+
titleIndices: titleMatch.indices,
|
|
141
|
+
}
|
|
100
142
|
bestScore = Math.max(bestScore, titleMatch.score)
|
|
101
143
|
hasMatch = true
|
|
102
|
-
} else {
|
|
103
|
-
delete (item as any).titleMatchIndices
|
|
104
144
|
}
|
|
105
145
|
|
|
106
|
-
// Try fuzzy matching on description
|
|
107
146
|
if (item.desc) {
|
|
108
147
|
const descMatch = fuzzyMatch(item.desc, queryTrimmed)
|
|
109
148
|
if (descMatch && descMatch.score > 0) {
|
|
110
|
-
|
|
111
|
-
|
|
149
|
+
searchMatch = {
|
|
150
|
+
...searchMatch,
|
|
151
|
+
descIndices: descMatch.indices,
|
|
152
|
+
}
|
|
112
153
|
bestScore = Math.max(bestScore, descMatch.score * 0.8)
|
|
113
154
|
hasMatch = true
|
|
114
|
-
} else {
|
|
115
|
-
delete (item as any).descMatchIndices
|
|
116
155
|
}
|
|
117
156
|
}
|
|
118
157
|
|
|
119
|
-
|
|
158
|
+
if (!hasMatch) {
|
|
159
|
+
return null
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
return {
|
|
163
|
+
item: withSearchMatch(item, searchMatch),
|
|
164
|
+
score: bestScore + calculateItemBonus(item, queryTrimmed),
|
|
165
|
+
}
|
|
120
166
|
}
|
|
121
167
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
*/
|
|
125
|
-
export function clearMatchIndices(items: Item[]): void {
|
|
126
|
-
items.forEach(item => {
|
|
127
|
-
delete (item as any).titleMatchIndices
|
|
128
|
-
delete (item as any).descMatchIndices
|
|
129
|
-
})
|
|
168
|
+
export function clearMatchIndices(items: Item[]): Item[] {
|
|
169
|
+
return items.map(item => withSearchMatch(item))
|
|
130
170
|
}
|
|
131
171
|
|
|
132
172
|
export function filterAndSortItems(items: Item[], query: string): Item[] {
|
|
133
173
|
if (!query.trim()) {
|
|
134
|
-
|
|
135
|
-
clearMatchIndices(items)
|
|
136
|
-
return items
|
|
174
|
+
return clearMatchIndices(items)
|
|
137
175
|
}
|
|
138
176
|
|
|
139
|
-
|
|
140
|
-
.
|
|
141
|
-
item,
|
|
142
|
-
|
|
143
|
-
})
|
|
144
|
-
.filter(({ score }) => score > 0)
|
|
177
|
+
return items
|
|
178
|
+
.flatMap(item => {
|
|
179
|
+
const result = buildSearchResult(item, query)
|
|
180
|
+
return result ? [result] : []
|
|
181
|
+
})
|
|
145
182
|
.sort((a, b) => b.score - a.score)
|
|
146
|
-
|
|
147
|
-
return scored.map(({ item }) => item)
|
|
183
|
+
.map(({ item }) => item)
|
|
148
184
|
}
|