ei-tui 1.6.3 → 1.6.5

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.
@@ -0,0 +1,98 @@
1
+ export async function getRecentSessionMessages(
2
+ sessionId: string | undefined,
3
+ hookSource: string | undefined,
4
+ transcriptPath: string | undefined
5
+ ): Promise<string[]> {
6
+ if (transcriptPath) {
7
+ try {
8
+ const text = await Bun.file(transcriptPath).text();
9
+
10
+ const { parseCodexRolloutMessages } = await import(
11
+ /* @vite-ignore */ "./integrations/codex/reader.js"
12
+ );
13
+ const codexMessages = parseCodexRolloutMessages(text, sessionId ?? "transcript");
14
+ if (codexMessages.length > 0) {
15
+ return codexMessages.slice(-5).map((m) => `${m.role}: ${m.content}`);
16
+ }
17
+
18
+ const messages: Array<{ content: string }> = [];
19
+
20
+ for (const line of text.split("\n")) {
21
+ const trimmed = line.trim();
22
+ if (!trimmed) continue;
23
+ let record: Record<string, unknown>;
24
+ try {
25
+ record = JSON.parse(trimmed) as Record<string, unknown>;
26
+ } catch {
27
+ continue;
28
+ }
29
+
30
+ if (record.type === "user") {
31
+ const msgContent = (record.message as Record<string, unknown>)?.content;
32
+ if (typeof msgContent === "string" && msgContent.trim()) {
33
+ messages.push({ content: msgContent.trim() });
34
+ }
35
+ } else if (record.type === "assistant") {
36
+ const msgContent = (record.message as Record<string, unknown>)?.content;
37
+ if (Array.isArray(msgContent)) {
38
+ const extracted = (msgContent as Array<Record<string, unknown>>)
39
+ .filter((b) => b.type === "text" && typeof b.text === "string")
40
+ .map((b) => b.text as string)
41
+ .join("\n\n")
42
+ .trim();
43
+ if (extracted) {
44
+ messages.push({ content: extracted });
45
+ }
46
+ }
47
+ }
48
+ }
49
+
50
+ return messages.slice(-5).map((m) => m.content);
51
+ } catch {
52
+ return [];
53
+ }
54
+ }
55
+
56
+ if (!sessionId || !hookSource) return [];
57
+
58
+ try {
59
+ if (hookSource === "opencode-plugin") {
60
+ const { createOpenCodeReader } = await import(
61
+ /* @vite-ignore */ "./integrations/opencode/reader-factory.js"
62
+ );
63
+ const reader = await createOpenCodeReader();
64
+ const messages = await reader.getMessagesForSession(sessionId);
65
+ return messages.slice(-5).map((m) => `${m.role}: ${m.content}`);
66
+ }
67
+
68
+ if (hookSource === "cursor") {
69
+ const { CursorReader } = await import(
70
+ /* @vite-ignore */ "./integrations/cursor/reader.js"
71
+ );
72
+ const reader = new CursorReader();
73
+ const sessions = await reader.getSessions();
74
+ const session =
75
+ sessions.find((s) => s.id === sessionId) ?? sessions[sessions.length - 1];
76
+ if (session) {
77
+ return session.messages.slice(-5).map((m) => `${m.type === 1 ? "user" : "assistant"}: ${m.text}`);
78
+ }
79
+ }
80
+
81
+ if (hookSource === "codex") {
82
+ const { CodexReader } = await import(
83
+ /* @vite-ignore */ "./integrations/codex/reader.js"
84
+ );
85
+ const reader = new CodexReader();
86
+ const sessions = await reader.getSessions();
87
+ const session =
88
+ sessions.find((s) => s.id === sessionId) ?? sessions[sessions.length - 1];
89
+ if (session) {
90
+ return session.messages.slice(-5).map((m) => `${m.role}: ${m.content}`);
91
+ }
92
+ }
93
+ } catch {
94
+ return [];
95
+ }
96
+
97
+ return [];
98
+ }