-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathui.ts
More file actions
152 lines (126 loc) · 4.87 KB
/
ui.ts
File metadata and controls
152 lines (126 loc) · 4.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import chalk from "chalk";
import type { Session, Agent, SearchResult } from "./types";
export function formatSession(session: Session, index?: number): string {
const prefix = index !== undefined ? chalk.dim(`${index + 1}.`) : "";
const id = chalk.cyan(session.id.slice(0, 8));
const project = chalk.yellow(session.project);
const time = formatRelativeTime(session.modifiedAt);
const msgs = chalk.dim(`${session.messageCount} msgs`);
const preview = chalk.white(truncate(session.firstMessage, 60));
return `${prefix} ${id} ${project} ${chalk.dim("•")} ${time} ${chalk.dim("•")} ${msgs}
${preview}`;
}
export function formatSearchResult(result: SearchResult, index: number): string {
const score = chalk.green(`${(1 - result.score).toFixed(0)}%`);
const id = chalk.cyan(result.session.id.slice(0, 8));
const project = chalk.yellow(result.session.project);
const preview = chalk.white(truncate(result.matchedContent, 80));
const relevance = getRelevanceHint(result);
return `${chalk.dim(`${index + 1}.`)} ${score} ${id} ${project}
${preview}
${chalk.dim(relevance)}`;
}
function getRelevanceHint(result: SearchResult): string {
if (!result.query) {
return "Semantic match";
}
const contentLower = result.matchedContent.toLowerCase();
const queryTerms = result.query
.toLowerCase()
.split(/\s+/)
.filter((t) => t.length > 2);
// Find exact term matches
const exactMatches = queryTerms.filter((term) => contentLower.includes(term));
if (exactMatches.length > 0) {
return `✓ Contains: ${exactMatches.join(", ")}`;
}
// Find semantic concepts by checking for related words
const conceptMap: Record<string, string[]> = {
backup: ["backup", "save", "restore", "copy", "sync"],
deploy: ["deploy", "release", "push", "ship", "launch"],
error: ["error", "bug", "issue", "fail", "crash", "exception"],
performance: [
"performance",
"speed",
"slow",
"optimize",
"fast",
"latency",
],
security: ["security", "auth", "password", "token", "encrypt", "ssl"],
database: ["database", "db", "sql", "query", "table", "index"],
api: ["api", "endpoint", "request", "response", "http", "rest"],
testing: ["test", "unit", "integration", "jest", "vitest", "debug"],
};
const foundConcepts: string[] = [];
for (const [concept, keywords] of Object.entries(conceptMap)) {
const queryHasKeyword = queryTerms.some((term) => keywords.includes(term));
const contentHasKeyword = keywords.some((keyword) =>
contentLower.includes(keyword)
);
if (queryHasKeyword && contentHasKeyword) {
foundConcepts.push(concept);
}
}
if (foundConcepts.length > 0) {
return `→ Related concepts: ${foundConcepts.join(", ")}`;
}
return "→ Semantic similarity (meaning-based match)";
}
export function formatAgent(agent: Agent): string {
const name = chalk.cyan.bold(agent.name);
const desc = chalk.white(agent.description);
const tools = chalk.dim(agent.tools.join(", "));
return `${name} - ${desc}
${chalk.dim("Tools:")} ${tools}`;
}
export function formatAgentChoice(agent: Agent): { name: string; value: string } {
return {
name: `${chalk.cyan.bold(agent.name.padEnd(12))} ${chalk.dim(agent.description)}`,
value: agent.name,
};
}
export function formatSessionChoice(session: Session): { name: string; value: string } {
const time = formatRelativeTime(session.modifiedAt);
const project = session.project.padEnd(20).slice(0, 20);
const preview = truncate(session.firstMessage, 50);
return {
name: `${chalk.yellow(project)} ${chalk.dim(time.padEnd(12))} ${preview}`,
value: session.id,
};
}
export function header(text: string): void {
console.log();
console.log(chalk.bold.blue(`◆ ${text}`));
console.log(chalk.dim("─".repeat(50)));
}
export function success(text: string): void {
console.log(chalk.green(`✓ ${text}`));
}
export function error(text: string): void {
console.log(chalk.red(`✗ ${text}`));
}
export function info(text: string): void {
console.log(chalk.dim(` ${text}`));
}
export function highlight(text: string): string {
return chalk.cyan.bold(text);
}
function truncate(text: string, length: number): string {
const clean = text.replace(/\n/g, " ").replace(/\s+/g, " ").trim();
if (clean.length <= length) return clean;
return clean.slice(0, length - 1) + "…";
}
function formatRelativeTime(date: Date): string {
const now = new Date();
const diffMs = now.getTime() - date.getTime();
const diffMins = Math.floor(diffMs / 60000);
const diffHours = Math.floor(diffMins / 60);
const diffDays = Math.floor(diffHours / 24);
if (diffMins < 1) return "just now";
if (diffMins < 60) return `${diffMins}m ago`;
if (diffHours < 24) return `${diffHours}h ago`;
if (diffDays < 7) return `${diffDays}d ago`;
if (diffDays < 30) return `${Math.floor(diffDays / 7)}w ago`;
return date.toLocaleDateString();
}