opencode-agora 0.3.0 → 0.4.1

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.
Files changed (280) hide show
  1. package/README.md +86 -255
  2. package/dist/atomic-write.d.ts +10 -0
  3. package/dist/atomic-write.d.ts.map +1 -0
  4. package/dist/atomic-write.js +23 -0
  5. package/dist/atomic-write.js.map +1 -0
  6. package/dist/auth/refresh.d.ts +17 -0
  7. package/dist/auth/refresh.d.ts.map +1 -0
  8. package/dist/auth/refresh.js +50 -0
  9. package/dist/auth/refresh.js.map +1 -0
  10. package/dist/cli/app.d.ts +13 -18
  11. package/dist/cli/app.d.ts.map +1 -1
  12. package/dist/cli/app.js +184 -1187
  13. package/dist/cli/app.js.map +1 -1
  14. package/dist/cli/chat-renderer.d.ts +31 -0
  15. package/dist/cli/chat-renderer.d.ts.map +1 -0
  16. package/dist/cli/chat-renderer.js +275 -0
  17. package/dist/cli/chat-renderer.js.map +1 -0
  18. package/dist/cli/commands/browse.d.ts +4 -0
  19. package/dist/cli/commands/browse.d.ts.map +1 -0
  20. package/dist/cli/commands/browse.js +80 -0
  21. package/dist/cli/commands/browse.js.map +1 -0
  22. package/dist/cli/commands/chat.d.ts +4 -0
  23. package/dist/cli/commands/chat.d.ts.map +1 -0
  24. package/dist/cli/commands/chat.js +125 -0
  25. package/dist/cli/commands/chat.js.map +1 -0
  26. package/dist/cli/commands/community.d.ts +12 -0
  27. package/dist/cli/commands/community.d.ts.map +1 -0
  28. package/dist/cli/commands/community.js +453 -0
  29. package/dist/cli/commands/community.js.map +1 -0
  30. package/dist/cli/commands/export.d.ts +3 -0
  31. package/dist/cli/commands/export.d.ts.map +1 -0
  32. package/dist/cli/commands/export.js +108 -0
  33. package/dist/cli/commands/export.js.map +1 -0
  34. package/dist/cli/commands/init.d.ts +4 -0
  35. package/dist/cli/commands/init.d.ts.map +1 -0
  36. package/dist/cli/commands/init.js +299 -0
  37. package/dist/cli/commands/init.js.map +1 -0
  38. package/dist/cli/commands/learn.d.ts +4 -0
  39. package/dist/cli/commands/learn.d.ts.map +1 -0
  40. package/dist/cli/commands/learn.js +62 -0
  41. package/dist/cli/commands/learn.js.map +1 -0
  42. package/dist/cli/commands/marketplace.d.ts +9 -0
  43. package/dist/cli/commands/marketplace.d.ts.map +1 -0
  44. package/dist/cli/commands/marketplace.js +321 -0
  45. package/dist/cli/commands/marketplace.js.map +1 -0
  46. package/dist/cli/commands/notify.d.ts +3 -0
  47. package/dist/cli/commands/notify.d.ts.map +1 -0
  48. package/dist/cli/commands/notify.js +59 -0
  49. package/dist/cli/commands/notify.js.map +1 -0
  50. package/dist/cli/commands/operations.d.ts +16 -0
  51. package/dist/cli/commands/operations.d.ts.map +1 -0
  52. package/dist/cli/commands/operations.js +1006 -0
  53. package/dist/cli/commands/operations.js.map +1 -0
  54. package/dist/cli/commands/ping.d.ts +3 -0
  55. package/dist/cli/commands/ping.d.ts.map +1 -0
  56. package/dist/cli/commands/ping.js +56 -0
  57. package/dist/cli/commands/ping.js.map +1 -0
  58. package/dist/cli/commands/today.d.ts +3 -0
  59. package/dist/cli/commands/today.d.ts.map +1 -0
  60. package/dist/cli/commands/today.js +142 -0
  61. package/dist/cli/commands/today.js.map +1 -0
  62. package/dist/cli/commands/types.d.ts +5 -0
  63. package/dist/cli/commands/types.d.ts.map +1 -0
  64. package/dist/cli/commands/types.js +2 -0
  65. package/dist/cli/commands/types.js.map +1 -0
  66. package/dist/cli/commands/watch.d.ts +3 -0
  67. package/dist/cli/commands/watch.d.ts.map +1 -0
  68. package/dist/cli/commands/watch.js +41 -0
  69. package/dist/cli/commands/watch.js.map +1 -0
  70. package/dist/cli/commands/welcome.d.ts +3 -0
  71. package/dist/cli/commands/welcome.d.ts.map +1 -0
  72. package/dist/cli/commands/welcome.js +97 -0
  73. package/dist/cli/commands/welcome.js.map +1 -0
  74. package/dist/cli/commands-meta.d.ts +21 -0
  75. package/dist/cli/commands-meta.d.ts.map +1 -0
  76. package/dist/cli/commands-meta.js +828 -0
  77. package/dist/cli/commands-meta.js.map +1 -0
  78. package/dist/cli/completions-gen.d.ts +2 -0
  79. package/dist/cli/completions-gen.d.ts.map +1 -0
  80. package/dist/cli/completions-gen.js +195 -0
  81. package/dist/cli/completions-gen.js.map +1 -0
  82. package/dist/cli/completions.d.ts +18 -0
  83. package/dist/cli/completions.d.ts.map +1 -0
  84. package/dist/cli/completions.js +227 -0
  85. package/dist/cli/completions.js.map +1 -0
  86. package/dist/cli/flags.d.ts +19 -0
  87. package/dist/cli/flags.d.ts.map +1 -0
  88. package/dist/cli/flags.js +91 -0
  89. package/dist/cli/flags.js.map +1 -0
  90. package/dist/cli/format.d.ts +19 -0
  91. package/dist/cli/format.d.ts.map +1 -0
  92. package/dist/cli/format.js +249 -0
  93. package/dist/cli/format.js.map +1 -0
  94. package/dist/cli/helpers.d.ts +95 -0
  95. package/dist/cli/helpers.d.ts.map +1 -0
  96. package/dist/cli/helpers.js +301 -0
  97. package/dist/cli/helpers.js.map +1 -0
  98. package/dist/cli/mcp-server.d.ts +4 -0
  99. package/dist/cli/mcp-server.d.ts.map +1 -0
  100. package/dist/cli/mcp-server.js +277 -0
  101. package/dist/cli/mcp-server.js.map +1 -0
  102. package/dist/cli/menu.d.ts +7 -0
  103. package/dist/cli/menu.d.ts.map +1 -0
  104. package/dist/cli/menu.js +172 -0
  105. package/dist/cli/menu.js.map +1 -0
  106. package/dist/cli/pages/community.d.ts +9 -0
  107. package/dist/cli/pages/community.d.ts.map +1 -0
  108. package/dist/cli/pages/community.js +1094 -0
  109. package/dist/cli/pages/community.js.map +1 -0
  110. package/dist/cli/pages/helpers.d.ts +37 -0
  111. package/dist/cli/pages/helpers.d.ts.map +1 -0
  112. package/dist/cli/pages/helpers.js +98 -0
  113. package/dist/cli/pages/helpers.js.map +1 -0
  114. package/dist/cli/pages/home.d.ts +4 -0
  115. package/dist/cli/pages/home.d.ts.map +1 -0
  116. package/dist/cli/pages/home.js +231 -0
  117. package/dist/cli/pages/home.js.map +1 -0
  118. package/dist/cli/pages/marketplace.d.ts +5 -0
  119. package/dist/cli/pages/marketplace.d.ts.map +1 -0
  120. package/dist/cli/pages/marketplace.js +583 -0
  121. package/dist/cli/pages/marketplace.js.map +1 -0
  122. package/dist/cli/pages/news.d.ts +31 -0
  123. package/dist/cli/pages/news.d.ts.map +1 -0
  124. package/dist/cli/pages/news.js +688 -0
  125. package/dist/cli/pages/news.js.map +1 -0
  126. package/dist/cli/pages/settings.d.ts +3 -0
  127. package/dist/cli/pages/settings.d.ts.map +1 -0
  128. package/dist/cli/pages/settings.js +296 -0
  129. package/dist/cli/pages/settings.js.map +1 -0
  130. package/dist/cli/pages/types.d.ts +67 -0
  131. package/dist/cli/pages/types.d.ts.map +1 -0
  132. package/dist/cli/pages/types.js +2 -0
  133. package/dist/cli/pages/types.js.map +1 -0
  134. package/dist/cli/prompter.d.ts +135 -0
  135. package/dist/cli/prompter.d.ts.map +1 -0
  136. package/dist/cli/prompter.js +710 -0
  137. package/dist/cli/prompter.js.map +1 -0
  138. package/dist/cli/shell.d.ts +23 -0
  139. package/dist/cli/shell.d.ts.map +1 -0
  140. package/dist/cli/shell.js +1106 -0
  141. package/dist/cli/shell.js.map +1 -0
  142. package/dist/cli/tui.d.ts +7 -0
  143. package/dist/cli/tui.d.ts.map +1 -0
  144. package/dist/cli/tui.js +419 -0
  145. package/dist/cli/tui.js.map +1 -0
  146. package/dist/cli.js +1 -1
  147. package/dist/cli.js.map +1 -1
  148. package/dist/commands.d.ts +14 -0
  149. package/dist/commands.d.ts.map +1 -0
  150. package/dist/commands.js +28 -0
  151. package/dist/commands.js.map +1 -0
  152. package/dist/community/client.d.ts +84 -0
  153. package/dist/community/client.d.ts.map +1 -0
  154. package/dist/community/client.js +340 -0
  155. package/dist/community/client.js.map +1 -0
  156. package/dist/community/search.d.ts +25 -0
  157. package/dist/community/search.d.ts.map +1 -0
  158. package/dist/community/search.js +62 -0
  159. package/dist/community/search.js.map +1 -0
  160. package/dist/community/types.d.ts +71 -0
  161. package/dist/community/types.d.ts.map +1 -0
  162. package/dist/community/types.js +11 -0
  163. package/dist/community/types.js.map +1 -0
  164. package/dist/config.d.ts +1 -7
  165. package/dist/config.d.ts.map +1 -1
  166. package/dist/config.js +0 -32
  167. package/dist/config.js.map +1 -1
  168. package/dist/data.d.ts +1 -1
  169. package/dist/data.d.ts.map +1 -1
  170. package/dist/data.js +778 -40
  171. package/dist/data.js.map +1 -1
  172. package/dist/format.d.ts +5 -39
  173. package/dist/format.d.ts.map +1 -1
  174. package/dist/format.js +5 -120
  175. package/dist/format.js.map +1 -1
  176. package/dist/history.d.ts +13 -0
  177. package/dist/history.d.ts.map +1 -0
  178. package/dist/history.js +37 -0
  179. package/dist/history.js.map +1 -0
  180. package/dist/hubs/cache.d.ts +6 -0
  181. package/dist/hubs/cache.d.ts.map +1 -0
  182. package/dist/hubs/cache.js +46 -0
  183. package/dist/hubs/cache.js.map +1 -0
  184. package/dist/hubs/enrichment.d.ts +43 -0
  185. package/dist/hubs/enrichment.d.ts.map +1 -0
  186. package/dist/hubs/enrichment.js +239 -0
  187. package/dist/hubs/enrichment.js.map +1 -0
  188. package/dist/hubs/github.d.ts +12 -0
  189. package/dist/hubs/github.d.ts.map +1 -0
  190. package/dist/hubs/github.js +54 -0
  191. package/dist/hubs/github.js.map +1 -0
  192. package/dist/hubs/huggingface.d.ts +27 -0
  193. package/dist/hubs/huggingface.d.ts.map +1 -0
  194. package/dist/hubs/huggingface.js +88 -0
  195. package/dist/hubs/huggingface.js.map +1 -0
  196. package/dist/hubs/quality.d.ts +26 -0
  197. package/dist/hubs/quality.d.ts.map +1 -0
  198. package/dist/hubs/quality.js +57 -0
  199. package/dist/hubs/quality.js.map +1 -0
  200. package/dist/hubs/types.d.ts +30 -0
  201. package/dist/hubs/types.d.ts.map +1 -0
  202. package/dist/hubs/types.js +2 -0
  203. package/dist/hubs/types.js.map +1 -0
  204. package/dist/index.d.ts.map +1 -1
  205. package/dist/index.js +188 -224
  206. package/dist/index.js.map +1 -1
  207. package/dist/init.d.ts.map +1 -1
  208. package/dist/init.js +6 -11
  209. package/dist/init.js.map +1 -1
  210. package/dist/live.d.ts +14 -0
  211. package/dist/live.d.ts.map +1 -1
  212. package/dist/live.js +35 -3
  213. package/dist/live.js.map +1 -1
  214. package/dist/marketplace.d.ts +25 -3
  215. package/dist/marketplace.d.ts.map +1 -1
  216. package/dist/marketplace.js +279 -22
  217. package/dist/marketplace.js.map +1 -1
  218. package/dist/news/cache.d.ts +13 -0
  219. package/dist/news/cache.d.ts.map +1 -0
  220. package/dist/news/cache.js +66 -0
  221. package/dist/news/cache.js.map +1 -0
  222. package/dist/news/score.d.ts +4 -0
  223. package/dist/news/score.d.ts.map +1 -0
  224. package/dist/news/score.js +43 -0
  225. package/dist/news/score.js.map +1 -0
  226. package/dist/news/sources/arxiv.d.ts +9 -0
  227. package/dist/news/sources/arxiv.d.ts.map +1 -0
  228. package/dist/news/sources/arxiv.js +107 -0
  229. package/dist/news/sources/arxiv.js.map +1 -0
  230. package/dist/news/sources/github-trending.d.ts +9 -0
  231. package/dist/news/sources/github-trending.d.ts.map +1 -0
  232. package/dist/news/sources/github-trending.js +97 -0
  233. package/dist/news/sources/github-trending.js.map +1 -0
  234. package/dist/news/sources/hn.d.ts +9 -0
  235. package/dist/news/sources/hn.d.ts.map +1 -0
  236. package/dist/news/sources/hn.js +57 -0
  237. package/dist/news/sources/hn.js.map +1 -0
  238. package/dist/news/sources/reddit.d.ts +9 -0
  239. package/dist/news/sources/reddit.d.ts.map +1 -0
  240. package/dist/news/sources/reddit.js +69 -0
  241. package/dist/news/sources/reddit.js.map +1 -0
  242. package/dist/news/sources/rss.d.ts +13 -0
  243. package/dist/news/sources/rss.d.ts.map +1 -0
  244. package/dist/news/sources/rss.js +14 -0
  245. package/dist/news/sources/rss.js.map +1 -0
  246. package/dist/news/types.d.ts +42 -0
  247. package/dist/news/types.d.ts.map +1 -0
  248. package/dist/news/types.js +56 -0
  249. package/dist/news/types.js.map +1 -0
  250. package/dist/preferences.d.ts +14 -0
  251. package/dist/preferences.d.ts.map +1 -0
  252. package/dist/preferences.js +31 -0
  253. package/dist/preferences.js.map +1 -0
  254. package/dist/settings.d.ts +26 -0
  255. package/dist/settings.d.ts.map +1 -0
  256. package/dist/settings.js +251 -0
  257. package/dist/settings.js.map +1 -0
  258. package/dist/state.d.ts +9 -2
  259. package/dist/state.d.ts.map +1 -1
  260. package/dist/state.js +41 -19
  261. package/dist/state.js.map +1 -1
  262. package/dist/transcript.d.ts +28 -0
  263. package/dist/transcript.d.ts.map +1 -0
  264. package/dist/transcript.js +79 -0
  265. package/dist/transcript.js.map +1 -0
  266. package/dist/types.d.ts +19 -1
  267. package/dist/types.d.ts.map +1 -1
  268. package/dist/ui.d.ts +157 -0
  269. package/dist/ui.d.ts.map +1 -0
  270. package/dist/ui.js +296 -0
  271. package/dist/ui.js.map +1 -0
  272. package/package.json +11 -9
  273. package/dist/api.d.ts +0 -72
  274. package/dist/api.d.ts.map +0 -1
  275. package/dist/api.js +0 -109
  276. package/dist/api.js.map +0 -1
  277. package/dist/logger.d.ts +0 -20
  278. package/dist/logger.d.ts.map +0 -1
  279. package/dist/logger.js +0 -59
  280. package/dist/logger.js.map +0 -1
@@ -0,0 +1,688 @@
1
+ import { DEFAULT_NEWS_CONFIG, hostFromUrl } from '../../news/types.js';
2
+ import { rankItems } from '../../news/score.js';
3
+ import { readCache, writeCache, isStale, readNewsMeta, writeNewsMeta } from '../../news/cache.js';
4
+ import { formatNumber } from '../../format.js';
5
+ import { hnSource } from '../../news/sources/hn.js';
6
+ import { redditSource } from '../../news/sources/reddit.js';
7
+ import { githubTrendingSource } from '../../news/sources/github-trending.js';
8
+ import { arxivSource } from '../../news/sources/arxiv.js';
9
+ import { spawn } from 'node:child_process';
10
+ import { join } from 'node:path';
11
+ import { homedir } from 'node:os';
12
+ import { vlen, rail, noRail, frame, scrollbar, sep } from './helpers.js';
13
+ import { FREE_MODELS } from '../commands/chat.js';
14
+ const SOURCE_LABELS = {
15
+ hn: 'HN',
16
+ reddit: 'R ',
17
+ 'github-trending': 'GH',
18
+ arxiv: 'XR',
19
+ rss: 'RS'
20
+ };
21
+ const SOURCE_CYCLE = [
22
+ 'all',
23
+ 'hn',
24
+ 'reddit',
25
+ 'github-trending',
26
+ 'arxiv',
27
+ 'rss'
28
+ ];
29
+ const ITEM_LINES = 3;
30
+ const TABS = [
31
+ { id: 'all', label: 'All', match: (_tags) => true },
32
+ { id: 'mcp', label: 'Mcp', match: (tags) => tags.some((t) => t.includes('mcp')) },
33
+ { id: 'tools', label: 'Tools', match: (tags) => tags.some((t) => t.includes('tool')) },
34
+ {
35
+ id: 'skills',
36
+ label: 'Skills',
37
+ match: (tags) => tags.some((t) => t.includes('skill'))
38
+ },
39
+ { id: 'llms', label: 'Llms', match: (tags) => tags.some((t) => t.includes('llm')) },
40
+ {
41
+ id: 'repos',
42
+ label: 'Repos',
43
+ match: (tags) => tags.some((t) => t.includes('repo') || t.includes('github'))
44
+ },
45
+ {
46
+ id: 'market',
47
+ label: 'Market',
48
+ match: (tags) => tags.some((t) => t.includes('market'))
49
+ },
50
+ {
51
+ id: 'search',
52
+ label: 'Search',
53
+ match: (tags) => tags.some((t) => t.includes('search'))
54
+ }
55
+ ];
56
+ const state = {
57
+ cursor: 0,
58
+ tab: 0,
59
+ source: 'all',
60
+ filter: '',
61
+ filtering: false,
62
+ savedOnly: false,
63
+ unreadOnly: false,
64
+ items: [],
65
+ read: new Set(),
66
+ saved: new Set(),
67
+ loading: true,
68
+ view: 'list',
69
+ previewItem: null,
70
+ previewContent: null,
71
+ previewLines: [],
72
+ previewScroll: 0,
73
+ previewLoading: false,
74
+ previewPhase: '',
75
+ previewAbort: null,
76
+ previewChild: null,
77
+ dataDir: null
78
+ };
79
+ function detectDataDir(ctx) {
80
+ const env = ctx.io.env ?? {};
81
+ const configured = env.AGORA_HOME || process.env.AGORA_HOME;
82
+ if (configured)
83
+ return configured;
84
+ const xdg = env.XDG_CONFIG_HOME || process.env.XDG_CONFIG_HOME || join(homedir(), '.config');
85
+ return join(xdg, 'agora');
86
+ }
87
+ function fetchWithTimeout(fn, timeoutMs = 8000) {
88
+ return Promise.race([
89
+ fn(),
90
+ new Promise((_, reject) => setTimeout(() => reject(new Error('timeout')), timeoutMs))
91
+ ]);
92
+ }
93
+ async function refreshNews(ctx) {
94
+ const dataDir = detectDataDir(ctx);
95
+ const cached = readCache(dataDir);
96
+ const now = new Date();
97
+ const config = DEFAULT_NEWS_CONFIG;
98
+ const adapters = [
99
+ ['hn', hnSource],
100
+ ['reddit', redditSource],
101
+ ['github-trending', githubTrendingSource],
102
+ ['arxiv', arxivSource]
103
+ ];
104
+ // Fetch all stale sources in parallel rather than sequentially — cold-start
105
+ // refresh is bounded by the slowest source instead of the sum.
106
+ const cachedSnapshot = [...cached];
107
+ const results = await Promise.all(adapters.map(async ([source, adapter]) => {
108
+ const cfg = config.sources[source];
109
+ if (!cfg?.enabled || !isStale(cachedSnapshot, source, cfg.ttlMinutes, now)) {
110
+ return { source, fresh: null };
111
+ }
112
+ try {
113
+ const fresh = await fetchWithTimeout(() => adapter.fetch({}));
114
+ return { source, fresh };
115
+ }
116
+ catch {
117
+ return { source, fresh: null };
118
+ }
119
+ }));
120
+ let merged = cachedSnapshot;
121
+ for (const { source, fresh } of results) {
122
+ if (fresh) {
123
+ merged = merged.filter((i) => i.source !== source);
124
+ merged.push(...fresh);
125
+ }
126
+ }
127
+ const ranked = rankItems(merged, config, now);
128
+ writeCache(dataDir, merged);
129
+ state.items = ranked;
130
+ state.loading = false;
131
+ }
132
+ function htmlToText(html) {
133
+ let text = html.replace(/<script[^>]*>[\s\S]*?<\/script>/gi, '');
134
+ text = text.replace(/<style[^>]*>[\s\S]*?<\/style>/gi, '');
135
+ text = text.replace(/<(p|div|br|h[1-6]|li|tr|blockquote|section|article|header|footer)[^>]*>/gi, '\n');
136
+ text = text.replace(/<a[^>]*>([\s\S]*?)<\/a>/gi, '$1');
137
+ text = text.replace(/<[^>]*>/g, '');
138
+ text = text.replace(/&amp;/g, '&');
139
+ text = text.replace(/&lt;/g, '<');
140
+ text = text.replace(/&gt;/g, '>');
141
+ text = text.replace(/&quot;/g, '"');
142
+ text = text.replace(/&#39;/g, "'");
143
+ text = text.replace(/&nbsp;/g, ' ');
144
+ text = text.replace(/\n{3,}/g, '\n\n');
145
+ text = text
146
+ .split('\n')
147
+ .map((l) => l.trim())
148
+ .join('\n');
149
+ return text.trim();
150
+ }
151
+ function fmtAge(date) {
152
+ const h = (Date.now() - date.getTime()) / 3600000;
153
+ if (h < 1)
154
+ return Math.round(h * 60) + 'm ago';
155
+ if (h < 24)
156
+ return Math.round(h) + 'h ago';
157
+ return Math.round(h / 24) + 'd ago';
158
+ }
159
+ function wordWrap(text, maxWidth) {
160
+ const result = [];
161
+ for (const para of text.split('\n')) {
162
+ if (para.trim() === '') {
163
+ if (result.length > 0 && result[result.length - 1] !== '')
164
+ result.push('');
165
+ continue;
166
+ }
167
+ const words = para.split(/\s+/);
168
+ let line = '';
169
+ for (const word of words) {
170
+ if (!word)
171
+ continue;
172
+ const test = line ? line + ' ' + word : word;
173
+ if (test.length > maxWidth) {
174
+ if (line) {
175
+ result.push(line);
176
+ line = word;
177
+ }
178
+ else {
179
+ result.push(word.slice(0, maxWidth));
180
+ line = word.slice(maxWidth);
181
+ }
182
+ }
183
+ else {
184
+ line = test;
185
+ }
186
+ }
187
+ if (line)
188
+ result.push(line);
189
+ }
190
+ while (result.length > 0 && result[result.length - 1] === '')
191
+ result.pop();
192
+ return result;
193
+ }
194
+ function trySummarize(text) {
195
+ const model = FREE_MODELS[0];
196
+ const modelArg = model.includes('/') ? model : `opencode/${model}`;
197
+ const maxChars = 12000;
198
+ const trimmed = text.length > maxChars ? text.slice(0, maxChars) + '\n...(truncated)' : text;
199
+ const prompt = `<system>\nYou are a news summarizer. Summarize the following article concisely in 2-4 paragraphs. Focus on key facts and conclusions. Be objective.\n<user>\n${trimmed}`;
200
+ return new Promise((resolve) => {
201
+ const child = spawn('opencode', ['run', '--format', 'json', '--model', modelArg, prompt], {
202
+ stdio: ['ignore', 'pipe', 'pipe'],
203
+ shell: false
204
+ });
205
+ state.previewChild = child;
206
+ let response = '';
207
+ const timer = setTimeout(() => {
208
+ child.kill();
209
+ resolve(null);
210
+ }, 30000);
211
+ child.stdout?.on('data', (chunk) => {
212
+ for (const line of chunk.toString().split('\n').filter(Boolean)) {
213
+ try {
214
+ const ev = JSON.parse(line);
215
+ if (ev.type === 'text' && ev.part?.text)
216
+ response += ev.part.text;
217
+ }
218
+ catch {
219
+ /* skip */
220
+ }
221
+ }
222
+ });
223
+ child.on('close', () => {
224
+ clearTimeout(timer);
225
+ if (state.previewChild === child)
226
+ state.previewChild = null;
227
+ resolve(response || null);
228
+ });
229
+ child.on('error', () => {
230
+ clearTimeout(timer);
231
+ if (state.previewChild === child)
232
+ state.previewChild = null;
233
+ resolve(null);
234
+ });
235
+ });
236
+ }
237
+ async function fetchArticlePreview(url, signal) {
238
+ try {
239
+ const resp = await fetch(url, {
240
+ signal,
241
+ headers: { 'User-Agent': 'Agora/0.4.1 (+https://agora.opencode.ai)' }
242
+ });
243
+ if (!resp.ok)
244
+ return `(error: HTTP ${resp.status})`;
245
+ const html = await resp.text();
246
+ const text = htmlToText(html);
247
+ const lines = text.split('\n').filter((l) => l.length > 0);
248
+ return lines.length > 5 ? text : '(could not extract article content from this page)';
249
+ }
250
+ catch (e) {
251
+ return '(failed to fetch: ' + (e instanceof Error ? e.message : String(e)) + ')';
252
+ }
253
+ }
254
+ async function startPreview(item, ctx) {
255
+ if (state.previewAbort)
256
+ state.previewAbort.abort();
257
+ if (state.previewChild && !state.previewChild.killed)
258
+ state.previewChild.kill();
259
+ const ac = new AbortController();
260
+ const timeoutId = setTimeout(() => ac.abort(), 8000);
261
+ state.previewAbort = ac;
262
+ state.view = 'preview';
263
+ state.previewScroll = 0;
264
+ state.previewItem = item;
265
+ state.previewContent = item.url;
266
+ state.previewLines = [];
267
+ state.previewLoading = true;
268
+ state.previewPhase = 'Fetching article…';
269
+ ctx.repaint();
270
+ await sleep(100);
271
+ const rawText = await fetchArticlePreview(item.url, ac.signal);
272
+ clearTimeout(timeoutId);
273
+ if (state.previewAbort !== ac)
274
+ return; // superseded — bail without writing state
275
+ if (rawText.startsWith('(failed') ||
276
+ rawText.startsWith('(error') ||
277
+ rawText.startsWith('(could not')) {
278
+ state.previewLines = wordWrap(rawText, ctx.width - 4);
279
+ state.previewLoading = false;
280
+ ctx.repaint();
281
+ return;
282
+ }
283
+ state.previewPhase = 'Summarizing…';
284
+ ctx.repaint();
285
+ await sleep(100);
286
+ const summary = await trySummarize(rawText);
287
+ if (state.previewAbort !== ac)
288
+ return; // superseded mid-summarize
289
+ state.previewLines = wordWrap(summary ?? rawText, ctx.width - 4);
290
+ state.previewLoading = false;
291
+ state.previewAbort = null;
292
+ ctx.repaint();
293
+ }
294
+ function sleep(ms) {
295
+ return new Promise((r) => setTimeout(r, ms));
296
+ }
297
+ function persistMeta() {
298
+ if (!state.dataDir)
299
+ return;
300
+ writeNewsMeta(state.dataDir, {
301
+ read: [...state.read],
302
+ saved: [...state.saved]
303
+ });
304
+ }
305
+ export function visible(st = state) {
306
+ const tab = TABS[st.tab];
307
+ return st.items.filter((s) => (st.source === 'all' || s.source === st.source) &&
308
+ tab.match(s.tags) &&
309
+ (!st.filter || s.title.toLowerCase().includes(st.filter.toLowerCase())) &&
310
+ (!st.savedOnly || st.saved.has(s.id)) &&
311
+ (!st.unreadOnly || !st.read.has(s.id)));
312
+ }
313
+ export const newsPage = {
314
+ id: 'news',
315
+ title: 'NEWS',
316
+ navLabel: 'News',
317
+ navIcon: 'N',
318
+ handlesTab: true,
319
+ hotkeys: [
320
+ { key: 'j/k/Pg', label: 'nav' },
321
+ { key: 'g/G', label: 'jump' },
322
+ { key: 'Enter', label: 'detail' },
323
+ { key: 's', label: 'save' },
324
+ { key: 'b', label: 'saved' },
325
+ { key: 'u', label: 'unread' },
326
+ { key: 'S', label: 'source' },
327
+ { key: 'p', label: 'preview' },
328
+ { key: '/', label: 'filter' },
329
+ { key: 'Tab', label: 'category' },
330
+ { key: 'r', label: 'refresh' },
331
+ { key: 'o', label: 'open' }
332
+ ],
333
+ mount(ctx) {
334
+ const dataDir = detectDataDir(ctx);
335
+ state.dataDir = dataDir;
336
+ const cached = readCache(dataDir);
337
+ if (cached.length > 0) {
338
+ const config = DEFAULT_NEWS_CONFIG;
339
+ state.items = rankItems(cached, config, new Date());
340
+ state.loading = false;
341
+ }
342
+ const meta = readNewsMeta(dataDir);
343
+ state.read = new Set(meta.read);
344
+ state.saved = new Set(meta.saved);
345
+ refreshNews(ctx);
346
+ },
347
+ unmount() {
348
+ persistMeta();
349
+ if (state.previewAbort) {
350
+ state.previewAbort.abort();
351
+ state.previewAbort = null;
352
+ }
353
+ if (state.previewChild && !state.previewChild.killed) {
354
+ state.previewChild.kill();
355
+ state.previewChild = null;
356
+ }
357
+ state.view = 'list';
358
+ state.previewLoading = false;
359
+ },
360
+ render(ctx) {
361
+ const { style, width, height } = ctx;
362
+ if (state.view === 'preview') {
363
+ const lines = [];
364
+ const item = state.previewItem;
365
+ if (state.previewLoading) {
366
+ const dots = state.previewPhase === 'Summarizing…' ? '◔' : '◙';
367
+ lines.push(' ' + dots + ' ' + style.dim(state.previewPhase || 'Loading…'));
368
+ return frame(lines, width, height);
369
+ }
370
+ if (!item) {
371
+ lines.push(' ' + style.dim('No article selected.'));
372
+ return frame(lines, width, height);
373
+ }
374
+ const headerLines = [
375
+ ' ' + style.bold(item.title),
376
+ ' ' +
377
+ style.dim(SOURCE_LABELS[item.source] ?? item.source.toUpperCase()) +
378
+ style.dim(' · ' + fmtAge(new Date(item.publishedAt))) +
379
+ style.dim(' · ↑ ' + formatNumber(item.engagement)) +
380
+ style.dim(' · s' + item.score.toFixed(2)),
381
+ ' ' + sep('', width - 2, style)
382
+ ];
383
+ lines.push(...headerLines);
384
+ const footerLines = [
385
+ ' ' +
386
+ style.accent('o') +
387
+ style.dim(' open in browser ') +
388
+ style.accent('Esc') +
389
+ style.dim(' back ') +
390
+ style.accent('j/k') +
391
+ style.dim(' nav')
392
+ ];
393
+ const hdr = headerLines.length;
394
+ const ftr = footerLines.length;
395
+ const max = height - hdr - ftr;
396
+ if (state.previewLines.length === 0) {
397
+ lines.push(' ' + style.dim('No content available.'));
398
+ }
399
+ else {
400
+ const total = state.previewLines.length;
401
+ if (state.previewScroll > total - max)
402
+ state.previewScroll = Math.max(0, total - max);
403
+ const end = Math.min(total, state.previewScroll + max);
404
+ const sbar = scrollbar(total, max, state.previewScroll, style);
405
+ for (let i = state.previewScroll; i < end; i++) {
406
+ lines.push(' ' + state.previewLines[i] + ' ' + (sbar[i - state.previewScroll] ?? ''));
407
+ }
408
+ const pad = hdr + max - lines.length;
409
+ for (let i = 0; i < pad; i++)
410
+ lines.push('');
411
+ }
412
+ lines.push(...footerLines);
413
+ return frame(lines, width, height);
414
+ }
415
+ if (state.view === 'detail') {
416
+ const s = visible()[state.cursor];
417
+ if (!s) {
418
+ state.view = 'list';
419
+ }
420
+ else {
421
+ const lines = [];
422
+ const ageH = (Date.now() - new Date(s.publishedAt).getTime()) / 3600000;
423
+ const age = Math.round(ageH) + 'h ago';
424
+ lines.push(' ' + style.bold(s.title));
425
+ lines.push(' ' +
426
+ style.dim(SOURCE_LABELS[s.source] ?? s.source.toUpperCase()) +
427
+ style.dim(' · ' + age) +
428
+ style.dim(' · ↑ ' + formatNumber(s.engagement)) +
429
+ style.dim(' · s' + s.score.toFixed(2)));
430
+ lines.push(' ' + style.accent(s.url));
431
+ if (s.tags && s.tags.length > 0) {
432
+ lines.push(' ' + style.dim('Tags: ') + s.tags.map((t) => style.accent(t)).join(', '));
433
+ }
434
+ if (s.summary) {
435
+ lines.push(' ' + sep('summary', width - 2, style));
436
+ lines.push(' ' + s.summary);
437
+ }
438
+ lines.push(' ' + sep('', width - 2, style));
439
+ lines.push(' ' + style.accent('o') + style.dim(' open in browser '));
440
+ lines.push(' ' + style.accent('p') + style.dim(' preview article '));
441
+ lines.push(' ' + style.accent('Esc') + style.dim(' back'));
442
+ return frame(lines, width, height);
443
+ }
444
+ }
445
+ const list = visible();
446
+ ctx.app.unread.news = state.loading ? 0 : list.length;
447
+ state.cursor = Math.min(state.cursor, Math.max(0, list.length - 1));
448
+ const lines = [];
449
+ const srcLabel = state.source === 'all' ? 'all' : (SOURCE_LABELS[state.source] ?? state.source);
450
+ const head = ' ' + style.bold(style.accent('NEWS'));
451
+ const pos = list.length > 0 ? style.dim(' [' + (state.cursor + 1) + '/' + list.length + ']') : '';
452
+ const filterBadge = (state.savedOnly ? style.accent(' saved-only') : '') +
453
+ (state.unreadOnly ? style.accent(' unread-only') : '');
454
+ const right = pos +
455
+ style.dim(' ' + list.length + ' stories · src: ') +
456
+ style.accent(srcLabel) +
457
+ filterBadge;
458
+ const gap = Math.max(2, width - vlen(head) - vlen(right) - 2);
459
+ lines.push(head + ' '.repeat(gap) + right);
460
+ const tabLine = ' ' +
461
+ TABS.map((t, i) => (i === state.tab ? style.accent(t.label) : style.dim(t.label))).join(style.dim(' ') + '·' + style.dim(' '));
462
+ lines.push(tabLine);
463
+ lines.push(' ' + style.dim('─'.repeat(Math.max(0, width - 2))));
464
+ if (state.filtering) {
465
+ lines.push(' ' + style.accent('/') + ' ' + state.filter + style.dim('▏'));
466
+ }
467
+ if (state.loading) {
468
+ lines.push(' ' + style.dim('Loading news…'));
469
+ return frame(lines, width, height);
470
+ }
471
+ if (list.length === 0) {
472
+ lines.push(' ' + style.dim('Empty feed. Press ') + style.accent('r') + style.dim(' to refresh.'));
473
+ return frame(lines, width, height);
474
+ }
475
+ const used = lines.length;
476
+ const maxItems = Math.max(1, Math.floor((height - used) / ITEM_LINES));
477
+ const half = Math.floor(maxItems / 2);
478
+ let start = Math.max(0, state.cursor - half);
479
+ if (start + maxItems > list.length)
480
+ start = Math.max(0, list.length - maxItems);
481
+ const end = Math.min(list.length, start + maxItems);
482
+ const sbar = scrollbar(list.length, end - start, state.cursor, style);
483
+ for (let si = start; si < end; si++) {
484
+ const s = list[si];
485
+ const sel = si === state.cursor;
486
+ const lead = sel ? rail(style) : noRail();
487
+ const rank = (si + 1).toString().padStart(2);
488
+ const src = style.accent((SOURCE_LABELS[s.source] ?? s.source.toUpperCase()).padEnd(6));
489
+ const ageH = (Date.now() - new Date(s.publishedAt).getTime()) / 3600000;
490
+ const age = style.dim((Math.round(ageH) + 'h').padEnd(4));
491
+ const up = style.accent(('↑ ' + formatNumber(s.engagement)).padStart(7));
492
+ const score = style.dim('s' + s.score.toFixed(2));
493
+ const isRead = state.read.has(s.id);
494
+ const isSaved = state.saved.has(s.id);
495
+ const titleColor = isRead ? style.dim(s.title) : sel ? style.bold(s.title) : s.title;
496
+ lines.push(' ' +
497
+ lead +
498
+ style.dim(rank + '. ') +
499
+ src +
500
+ ' ' +
501
+ age +
502
+ ' ' +
503
+ up +
504
+ ' ' +
505
+ score +
506
+ ' ' +
507
+ titleColor +
508
+ ' ' +
509
+ sbar[si - start]);
510
+ lines.push(' ' + style.dim(hostFromUrl(s.url)) + (isSaved ? style.accent(' saved') : ''));
511
+ lines.push(' ' + style.dim('─'.repeat(Math.max(0, width - 2))));
512
+ }
513
+ return frame(lines, width, height);
514
+ },
515
+ handleKey(event, ctx) {
516
+ if (state.view === 'preview') {
517
+ if (event.key === 'esc') {
518
+ state.view = 'detail';
519
+ return { kind: 'none' };
520
+ }
521
+ if (event.key === 'o') {
522
+ const pi = state.previewItem;
523
+ return pi ? { kind: 'open-url', url: pi.url } : { kind: 'none' };
524
+ }
525
+ if (event.key === 'j' || event.key === 'down') {
526
+ state.previewScroll = Math.min(state.previewLines.length - 1, state.previewScroll + 1);
527
+ return { kind: 'none' };
528
+ }
529
+ if (event.key === 'k' || event.key === 'up') {
530
+ state.previewScroll = Math.max(0, state.previewScroll - 1);
531
+ return { kind: 'none' };
532
+ }
533
+ if (event.key === 'pageup') {
534
+ state.previewScroll = Math.max(0, state.previewScroll - 20);
535
+ return { kind: 'none' };
536
+ }
537
+ if (event.key === 'pagedown') {
538
+ state.previewScroll = Math.min(state.previewLines.length - 1, state.previewScroll + 20);
539
+ return { kind: 'none' };
540
+ }
541
+ if (event.key === 'home') {
542
+ state.previewScroll = 0;
543
+ return { kind: 'none' };
544
+ }
545
+ if (event.key === 'end') {
546
+ state.previewScroll = state.previewLines.length - 1;
547
+ return { kind: 'none' };
548
+ }
549
+ return { kind: 'none' };
550
+ }
551
+ if (state.view === 'detail') {
552
+ if (event.key === 'esc') {
553
+ state.view = 'list';
554
+ return { kind: 'none' };
555
+ }
556
+ if (event.key === 'o') {
557
+ const s = visible()[state.cursor];
558
+ return s ? { kind: 'open-url', url: s.url } : { kind: 'none' };
559
+ }
560
+ if (event.key === 'p') {
561
+ const s = visible()[state.cursor];
562
+ if (s && state.previewContent !== s.url)
563
+ startPreview(s, ctx);
564
+ return { kind: 'none' };
565
+ }
566
+ return { kind: 'none' };
567
+ }
568
+ if (state.filtering) {
569
+ if (event.key === 'esc') {
570
+ state.filtering = false;
571
+ state.filter = '';
572
+ return { kind: 'none' };
573
+ }
574
+ if (event.key === 'enter') {
575
+ state.filtering = false;
576
+ return { kind: 'none' };
577
+ }
578
+ if (event.key === 'backspace') {
579
+ state.filter = state.filter.slice(0, -1);
580
+ return { kind: 'none' };
581
+ }
582
+ if (event.key.length === 1 && !event.ctrl) {
583
+ state.filter += event.key;
584
+ return { kind: 'none' };
585
+ }
586
+ return { kind: 'none' };
587
+ }
588
+ const list = visible();
589
+ switch (event.key) {
590
+ case 'tab':
591
+ state.tab = (state.tab + 1) % TABS.length;
592
+ state.cursor = 0;
593
+ return { kind: 'none' };
594
+ case 'left':
595
+ state.tab = state.tab > 0 ? state.tab - 1 : TABS.length - 1;
596
+ state.cursor = 0;
597
+ return { kind: 'none' };
598
+ case 'right':
599
+ state.tab = (state.tab + 1) % TABS.length;
600
+ state.cursor = 0;
601
+ return { kind: 'none' };
602
+ case 'j':
603
+ case 'down':
604
+ state.cursor = Math.min(list.length - 1, state.cursor + 1);
605
+ return { kind: 'none' };
606
+ case 'k':
607
+ case 'up':
608
+ state.cursor = Math.max(0, state.cursor - 1);
609
+ return { kind: 'none' };
610
+ case 'g':
611
+ state.cursor = 0;
612
+ return { kind: 'none' };
613
+ case 'G':
614
+ state.cursor = Math.max(0, list.length - 1);
615
+ return { kind: 'none' };
616
+ case 'pageup':
617
+ state.cursor = Math.max(0, state.cursor - 20);
618
+ return { kind: 'none' };
619
+ case 'pagedown':
620
+ state.cursor = Math.min(list.length - 1, state.cursor + 20);
621
+ return { kind: 'none' };
622
+ case 'home':
623
+ state.cursor = 0;
624
+ return { kind: 'none' };
625
+ case 'end':
626
+ state.cursor = list.length - 1;
627
+ return { kind: 'none' };
628
+ case 'enter':
629
+ if (list.length > 0) {
630
+ state.view = 'detail';
631
+ }
632
+ return { kind: 'none' };
633
+ case 's': {
634
+ const it = list[state.cursor];
635
+ if (it) {
636
+ if (state.saved.has(it.id))
637
+ state.saved.delete(it.id);
638
+ else
639
+ state.saved.add(it.id);
640
+ persistMeta();
641
+ }
642
+ return { kind: 'none' };
643
+ }
644
+ case 'S': {
645
+ const idx = SOURCE_CYCLE.indexOf(state.source);
646
+ state.source = SOURCE_CYCLE[(idx + 1) % SOURCE_CYCLE.length] ?? 'all';
647
+ state.cursor = 0;
648
+ return { kind: 'none' };
649
+ }
650
+ case 'b':
651
+ state.savedOnly = !state.savedOnly;
652
+ state.cursor = 0;
653
+ return { kind: 'none' };
654
+ case 'u':
655
+ state.unreadOnly = !state.unreadOnly;
656
+ state.cursor = 0;
657
+ return { kind: 'none' };
658
+ case 'm': {
659
+ const it = list[state.cursor];
660
+ if (it) {
661
+ state.read.add(it.id);
662
+ persistMeta();
663
+ }
664
+ return { kind: 'none' };
665
+ }
666
+ case '/':
667
+ state.filtering = true;
668
+ return { kind: 'none' };
669
+ case 'p': {
670
+ const pi = list[state.cursor];
671
+ if (pi && state.previewContent !== pi.url)
672
+ startPreview(pi, ctx);
673
+ return { kind: 'none' };
674
+ }
675
+ case 'o': {
676
+ const it = list[state.cursor];
677
+ return it ? { kind: 'open-url', url: it.url } : { kind: 'none' };
678
+ }
679
+ case 'r':
680
+ state.loading = true;
681
+ refreshNews(ctx);
682
+ return { kind: 'status', message: 'refreshing...' };
683
+ default:
684
+ return { kind: 'none' };
685
+ }
686
+ }
687
+ };
688
+ //# sourceMappingURL=news.js.map