forked from op7418/CodePilot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseCliToolsFetch.ts
More file actions
161 lines (145 loc) · 4.89 KB
/
useCliToolsFetch.ts
File metadata and controls
161 lines (145 loc) · 4.89 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
153
154
155
156
157
158
159
160
161
import { useState, useCallback } from 'react';
import type { CliToolItem, PopoverMode } from '@/types';
export interface UseCliToolsFetchReturn {
cliTools: CliToolItem[];
cliFilter: string;
setCliFilter: (filter: string) => void;
fetchCliTools: () => Promise<void>;
handleCliSelect: (tool: CliToolItem) => void;
handleOpenCliPopover: () => Promise<void>;
}
export function useCliToolsFetch(opts: {
popoverMode: PopoverMode;
closePopover: () => void;
setPopoverMode: (mode: PopoverMode) => void;
setSelectedIndex: React.Dispatch<React.SetStateAction<number>>;
inputValue: string;
locale: string;
textareaRef: React.RefObject<HTMLTextAreaElement | null>;
cliSearchRef: React.RefObject<HTMLInputElement | null>;
setCliBadge: (badge: { id: string; name: string } | null) => void;
setInputValue: (value: string) => void;
}): UseCliToolsFetchReturn {
const {
popoverMode,
closePopover,
setPopoverMode,
setSelectedIndex,
inputValue,
locale,
textareaRef,
cliSearchRef,
setCliBadge,
setInputValue,
} = opts;
const [cliTools, setCliTools] = useState<CliToolItem[]>([]);
const [cliFilter, setCliFilter] = useState('');
const fetchCliTools = useCallback(async () => {
try {
const [installedRes, catalogRes] = await Promise.all([
fetch('/api/cli-tools/installed'),
fetch('/api/cli-tools/catalog'),
]);
const installedData = await installedRes.json();
const catalogData = await catalogRes.json();
const catalogTools = catalogData.tools || [];
const runtimeInfos = installedData.tools || [];
const extraDetected = installedData.extra || [];
// Build lookup for catalog summaries
const catalogMap = new Map<string, { name: string; summaryZh: string; summaryEn: string }>();
for (const ct of catalogTools) {
catalogMap.set(ct.id, { name: ct.name, summaryZh: ct.summaryZh, summaryEn: ct.summaryEn });
}
// Extra well-known names lookup
const extraNames: Record<string, string> = {};
try {
const { EXTRA_WELL_KNOWN_BINS } = await import('@/lib/cli-tools-catalog');
for (const [id, name] of EXTRA_WELL_KNOWN_BINS) {
extraNames[id] = name;
}
} catch { /* ignore */ }
// Load cached AI descriptions
let autoDesc: Record<string, { zh: string; en: string }> = {};
try {
const cached = localStorage.getItem('cli-tools-auto-desc');
if (cached) autoDesc = JSON.parse(cached);
} catch { /* ignore */ }
const docLocale = document.documentElement.lang === 'zh' ? 'zh' : 'en';
const items: CliToolItem[] = [];
// Installed catalog tools
for (const ri of runtimeInfos) {
if (ri.status !== 'installed') continue;
const cat = catalogMap.get(ri.id);
const ad = autoDesc[ri.id];
const summary = ad
? (docLocale === 'zh' ? ad.zh : ad.en)
: cat
? (docLocale === 'zh' ? cat.summaryZh : cat.summaryEn)
: '';
items.push({
id: ri.id,
name: cat?.name || ri.id,
version: ri.version,
summary,
});
}
// Extra detected tools
for (const ri of extraDetected) {
const ad = autoDesc[ri.id];
const summary = ad ? (docLocale === 'zh' ? ad.zh : ad.en) : '';
items.push({
id: ri.id,
name: extraNames[ri.id] || ri.id,
version: ri.version,
summary,
});
}
setCliTools(items);
} catch {
setCliTools([]);
}
}, []);
const handleCliSelect = useCallback((tool: CliToolItem) => {
closePopover();
setCliFilter('');
if (!inputValue.trim()) {
// Empty input: prefill with prompt template
const prefix = locale === 'zh'
? `我想用 ${tool.name} 工具完成:`
: `I want to use ${tool.name} to: `;
setInputValue(prefix);
setTimeout(() => {
const ta = textareaRef.current;
if (ta) {
ta.focus();
ta.selectionStart = ta.selectionEnd = prefix.length;
}
}, 0);
} else {
// Non-empty input: set CLI badge
setCliBadge({ id: tool.id, name: tool.name });
setTimeout(() => textareaRef.current?.focus(), 0);
}
}, [inputValue, locale, closePopover, textareaRef, setCliBadge, setInputValue]);
const handleOpenCliPopover = useCallback(async () => {
if (popoverMode === 'cli') {
closePopover();
return;
}
closePopover();
setPopoverMode('cli');
setCliFilter('');
setSelectedIndex(0);
// Focus search input on next render (before fetch completes)
setTimeout(() => cliSearchRef.current?.focus(), 0);
fetchCliTools();
}, [popoverMode, closePopover, fetchCliTools, setPopoverMode, setSelectedIndex, cliSearchRef]);
return {
cliTools,
cliFilter,
setCliFilter,
fetchCliTools,
handleCliSelect,
handleOpenCliPopover,
};
}