-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.tsx
More file actions
291 lines (263 loc) · 8.67 KB
/
index.tsx
File metadata and controls
291 lines (263 loc) · 8.67 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import {
ActionPanel,
Action,
List,
Icon,
Color,
showToast,
Toast,
Form,
useNavigation,
Detail,
open,
} from "@raycast/api";
import { useState, useEffect } from "react";
import { api } from "./api";
import { Session, SessionEvent, Context } from './types';
function CreateContextForm({
onSubmit,
}: {
onSubmit: (name: string, description: string) => Promise<void>;
}) {
const [name, setName] = useState("");
const [description, setDescription] = useState("");
const { pop,push } = useNavigation();
return (
<Form
actions={
<ActionPanel>
<Action.SubmitForm
title="Create New Context"
onSubmit={async () => {
await onSubmit(name, description);
pop();
}}
/>
</ActionPanel>
}
>
<Form.TextField
id="name"
title="Context Name"
placeholder="Enter context name"
value={name}
onChange={setName}
/>
<Form.TextField
id="description"
title="Description"
placeholder="Enter context description"
value={description}
onChange={setDescription}
/>
</Form>
);
}
export default function Command() {
const [activeSession, setActiveSession] = useState<Session | null>(null);
const [sessionEvents, setSessionEvents] = useState<SessionEvent[]>([]);
const [isLoading, setIsLoading] = useState(true);
const [searchText, setSearchText] = useState("");
const [contexts, setContexts] = useState<Context[]>([]);
const { push } = useNavigation();
// Load active session on mount
useEffect(() => {
checkActiveSession();
const interval = setInterval(checkActiveSession, 5000); // Poll every 5 seconds
return () => clearInterval(interval);
}, []);
// Fetch active session and its events
async function checkActiveSession() {
try {
const data = await api.getActiveSessions();
if (data.count > 0) {
const session = data.active_sessions[0];
setActiveSession(session);
loadSessionEvents(session.session_id);
} else {
setActiveSession(null);
setSessionEvents([]);
}
} catch (error) {
console.error('Failed to check active session:', error);
await showToast(Toast.Style.Failure, 'Failed to check session status');
} finally {
setIsLoading(false);
}
}
async function loadSessionEvents(sessionId: number) {
try {
const events = await api.getSessionEvents(sessionId);
setSessionEvents(events);
} catch (error) {
console.error('Failed to load session events:', error);
}
}
async function startNewSession(contextName: string) {
try {
const context = await api.createContext(contextName);
const session = await api.startSession(context.context_id);
setActiveSession(session);
await showToast(Toast.Style.Success, `Started ${contextName} session`);
loadSessionEvents(session.session_id);
} catch (error) {
console.error('Failed to start session:', error);
await showToast(Toast.Style.Failure, 'Failed to start session');
}
}
async function endCurrentSession() {
if (!activeSession) return;
try {
const {session_id, summary} = await api.endSession(activeSession.session_id);
const { path } = await api.saveSession(session_id, "Save the session summary to meaningfully formatted markdown file." );
console.log(`Session ${session_id} ended`);
setActiveSession(null);
setSessionEvents([]);
// Open in Obsidian using the obsidian:// protocol
await open(`obsidian://open?vault=context-tracker&file=${encodeURIComponent(path)}`);
await showToast(Toast.Style.Success, 'Session saved and ended');
} catch (error) {
console.error('Failed to end session:', error);
await showToast(Toast.Style.Failure, 'Failed to end session');
}
}
async function generateSummary() {
if (!activeSession) return;
try {
const summary = await api.generateSummary(activeSession.session_id);
const markdownContent = `# Session Summary
## Overview
${summary.overview}
## Key Topics
${summary.key_topics.map(topic => `- ${topic}`).join('\n')}
## Learning Highlights
${summary.learning_highlights.map(highlight => `- ${highlight}`).join('\n')}
## Resources Used
${summary.resources_used.map(resource => `- ${resource}`).join('\n')}
## Conclusion
${summary.conclusion}
`;
await showToast(Toast.Style.Success, 'Summary generated');
push(
<Detail
markdown={markdownContent}
actions={
<ActionPanel>
<Action.CopyToClipboard
title="Copy Markdown"
content={markdownContent}
/>
</ActionPanel>
}
/>
);
} catch (error) {
console.error('Failed to generate summary:', error);
await showToast(Toast.Style.Failure, 'Failed to generate summary');
}
}
useEffect(() => {
loadContexts();
}, []);
async function loadContexts() {
try {
const availableContexts = await api.getContexts();
setContexts(availableContexts);
} catch (error) {
console.error('Failed to load contexts:', error);
await showToast(Toast.Style.Failure, 'Failed to load contexts');
}
}
return (
<List
isLoading={isLoading}
searchBarPlaceholder="Search sessions and events..."
searchText={searchText}
onSearchTextChange={setSearchText}
>
{/* Active Session Section */}
{activeSession && (
<List.Section title="Active Session">
<List.Item
icon={{ source: Icon.Video, tintColor: Color.Red }}
title={`Recording since ${(new Date(activeSession.start_time).toLocaleTimeString()).toString()}`}
accessories={[
{ icon: Icon.Clock },
{ text: "Recording" }
]}
actions={
<ActionPanel>
<Action
title="End Session"
icon={Icon.Stop}
onAction={endCurrentSession}
/>
<Action
title="Generate Summary"
icon={Icon.Document}
onAction={generateSummary}
/>
</ActionPanel>
}
/>
</List.Section>
)}
{/* Session Events Section */}
{sessionEvents.length > 0 && (
<List.Section title="Recent Events">
{sessionEvents
.filter(event =>
event.event_type?.toLowerCase().includes(searchText.toLowerCase())
)
.map(event => (
<List.Item
key={event.event_id}
icon={Icon.Circle}
title={event.event_type}
subtitle={new Date(event.timestamp).toLocaleTimeString()}
accessories={[{ text: event.event_data.summary || "" }]}
/>
))}
</List.Section>
)}
{/* Quick Actions Section */}
<List.Section title="Quick Actions">
<List.Item
icon={Icon.Plus}
title="New Context"
actions={
<ActionPanel>
<Action.Push
title="Create Custom Session"
target={
<CreateContextForm
onSubmit={async (name, description) => {
await api.createContext(name, description);
await loadContexts();
}}
/>
}
/>
</ActionPanel>
}
/>
{!activeSession && contexts.map(context => (
<List.Item
key={context.context_id}
icon={Icon.Circle}
title={`Start ${context.name} Session`}
subtitle={context.description}
actions={
<ActionPanel>
<Action
title="Start Session"
onAction={() => startNewSession(context.name)}
/>
</ActionPanel>
}
/>
))}
</List.Section>
</List>
);
}