@lemonriver/core 0.3.0 → 0.4.0
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.
- package/dist/{build-Bqo69DWP.js → build-Nl7EeJ6x.js} +1 -1
- package/dist/cli/bin.js +3 -3
- package/dist/{config-teo3kWz7.d.ts → config-CYNOR93W.d.ts} +1 -1
- package/dist/{config-DJ5rkMMg.js → config-CpC0nM3r.js} +697 -2
- package/dist/{dev-B2FJD5bJ.js → dev-BB3THd5I.js} +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/locale/index.d.ts +1 -1
- package/dist/locale/index.js +1 -1
- package/dist/{preview-C93BOZL1.js → preview-BGjgTg7H.js} +1 -1
- package/dist/{types-DFK02XVv.d.ts → types-tdaXSyah.d.ts} +1 -0
- package/dist/vite/index.d.ts +1 -1
- package/dist/vite/index.js +1 -1
- package/dist/{zh-tw-DwTAZKa6.js → zh-tw-DcrDeKxr.js} +5 -0
- package/package.json +1 -1
- package/src/agent/prompt.ts +2 -0
- package/src/app/components/agent/agent-panel.tsx +2 -0
- package/src/app/components/player.tsx +105 -23
- package/src/app/components/present/curtain.tsx +52 -0
- package/src/app/components/present/last-slide-hint.tsx +36 -0
- package/src/app/components/sidebar/sidebar-footer.tsx +6 -1
- package/src/app/components/thumbnail-rail.tsx +1 -0
- package/src/app/lemonriver/agent/lemon.tsx +16 -1
- package/src/app/lemonriver/agent-prefs/prefs.test.ts +173 -0
- package/src/app/lemonriver/agent-prefs/prefs.ts +137 -0
- package/src/app/lemonriver/agent-prefs/use-prefs.ts +36 -0
- package/src/app/lemonriver/brand/mascot.tsx +86 -0
- package/src/app/lemonriver/brand/mascots/design-done.png +0 -0
- package/src/app/lemonriver/brand/mascots/design-idle.png +0 -0
- package/src/app/lemonriver/brand/mascots/design-needs-you.png +0 -0
- package/src/app/lemonriver/brand/mascots/design-working.png +0 -0
- package/src/app/lemonriver/brand/mascots/outline-done.png +0 -0
- package/src/app/lemonriver/brand/mascots/outline-idle.png +0 -0
- package/src/app/lemonriver/brand/mascots/outline-needs-you.png +0 -0
- package/src/app/lemonriver/brand/mascots/outline-working.png +0 -0
- package/src/app/lemonriver/brand/mascots/research-done.png +0 -0
- package/src/app/lemonriver/brand/mascots/research-idle.png +0 -0
- package/src/app/lemonriver/brand/mascots/research-needs-you.png +0 -0
- package/src/app/lemonriver/brand/mascots/research-working.png +0 -0
- package/src/app/lemonriver/brand/mascots/review-done.png +0 -0
- package/src/app/lemonriver/brand/mascots/review-idle.png +0 -0
- package/src/app/lemonriver/brand/mascots/review-needs-you.png +0 -0
- package/src/app/lemonriver/brand/mascots/review-working.png +0 -0
- package/src/app/lemonriver/chrome/app-chrome.tsx +2 -0
- package/src/app/lemonriver/features.test.ts +5 -0
- package/src/app/lemonriver/features.ts +2 -1
- package/src/app/lemonriver/home/brief-card.tsx +8 -1
- package/src/app/lemonriver/home/home-page.tsx +11 -7
- package/src/app/lemonriver/onboarding/onboarding.tsx +182 -0
- package/src/app/lemonriver/outline/build-team.tsx +278 -0
- package/src/app/lemonriver/outline/describe-page.tsx +380 -0
- package/src/app/lemonriver/outline/files.test.ts +90 -0
- package/src/app/lemonriver/outline/files.ts +69 -0
- package/src/app/lemonriver/outline/model.test.ts +633 -0
- package/src/app/lemonriver/outline/model.ts +443 -0
- package/src/app/lemonriver/outline/outline-page.tsx +504 -0
- package/src/app/lemonriver/outline/pending-pages.tsx +39 -0
- package/src/app/lemonriver/outline/rule.ts +3 -0
- package/src/app/lemonriver/outline/steps.tsx +43 -0
- package/src/app/lemonriver/outline/use-build-driver.ts +258 -0
- package/src/app/lemonriver/routes.test.tsx +6 -6
- package/src/app/lemonriver/routes.tsx +28 -0
- package/src/app/lemonriver/settings/settings-page.tsx +211 -0
- package/src/app/lemonriver/slide-extensions.tsx +31 -24
- package/src/app/lemonriver/text/en.ts +149 -0
- package/src/app/lemonriver/text/es.ts +148 -0
- package/src/app/lemonriver/text/ja.ts +150 -0
- package/src/app/lemonriver/text/text.test.ts +33 -0
- package/src/app/lemonriver/text/types.ts +169 -0
- package/src/app/lemonriver/text/zh-cn.ts +146 -0
- package/src/app/lemonriver/text/zh-tw.ts +146 -0
- package/src/app/lemonriver/theme/app.css +12 -1
- package/src/app/lemonriver/theme/pages.test.ts +4 -0
- package/src/app/lemonriver/theme/pages.ts +11 -2
- package/src/app/lemonriver/update/beta-badge.tsx +26 -0
- package/src/app/lemonriver/update/prompt.test.ts +84 -0
- package/src/app/lemonriver/update/prompt.ts +48 -0
- package/src/app/lemonriver/update/update-prompt.tsx +113 -0
- package/src/app/routes/home.tsx +13 -5
- package/src/app/routes/slide.tsx +70 -13
- package/src/locale/en.ts +1 -0
- package/src/locale/es.ts +1 -0
- package/src/locale/ja.ts +1 -0
- package/src/locale/types.ts +1 -0
- package/src/locale/zh-cn.ts +1 -0
- package/src/locale/zh-tw.ts +1 -0
|
@@ -0,0 +1,380 @@
|
|
|
1
|
+
import { useEffect, useRef, useState } from 'react';
|
|
2
|
+
import { useLocation } from 'react-router-dom';
|
|
3
|
+
import { toast } from 'sonner';
|
|
4
|
+
import { ConnectView } from '@/components/agent/connect-view';
|
|
5
|
+
import { Button } from '@/components/ui/button';
|
|
6
|
+
import { Textarea } from '@/components/ui/textarea';
|
|
7
|
+
import {
|
|
8
|
+
createSession,
|
|
9
|
+
sendMessage,
|
|
10
|
+
setActiveSession,
|
|
11
|
+
useAgentStatus,
|
|
12
|
+
} from '@/lib/agent/use-agent';
|
|
13
|
+
import { uploadWithAutoRename } from '@/lib/assets';
|
|
14
|
+
import { format, useLocale } from '@/lib/use-locale';
|
|
15
|
+
import { cn } from '@/lib/utils';
|
|
16
|
+
import { readPrefs } from '../agent-prefs/prefs';
|
|
17
|
+
import { useAgentPrefs } from '../agent-prefs/use-prefs';
|
|
18
|
+
import { Mascot } from '../brand/mascot';
|
|
19
|
+
import { Onboarding } from '../onboarding/onboarding';
|
|
20
|
+
import { useLrText } from '../text';
|
|
21
|
+
import { checkFileObject, type FileVerdict, formatSize } from './files';
|
|
22
|
+
import { MAX_BRIEF, OUTLINE_PAGE_SIZES, type Outline, outlinePrompt } from './model';
|
|
23
|
+
import { Steps } from './steps';
|
|
24
|
+
|
|
25
|
+
const DRAFT_KEY = 'lemonriver:new-deck-draft';
|
|
26
|
+
|
|
27
|
+
type DraftFile = { key: string; file: File; verdict: FileVerdict };
|
|
28
|
+
|
|
29
|
+
type StoredDraft = { brief?: unknown; pages?: unknown };
|
|
30
|
+
|
|
31
|
+
function basePath(): string {
|
|
32
|
+
return import.meta.env.BASE_URL.replace(/\/$/, '');
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function readDraft(): StoredDraft {
|
|
36
|
+
try {
|
|
37
|
+
const raw = sessionStorage.getItem(DRAFT_KEY);
|
|
38
|
+
if (!raw) return {};
|
|
39
|
+
const parsed: unknown = JSON.parse(raw);
|
|
40
|
+
return parsed && typeof parsed === 'object' ? (parsed as StoredDraft) : {};
|
|
41
|
+
} catch {
|
|
42
|
+
return {};
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function writeDraft(brief: string, pages: number): void {
|
|
47
|
+
try {
|
|
48
|
+
sessionStorage.setItem(DRAFT_KEY, JSON.stringify({ brief, pages }));
|
|
49
|
+
} catch {}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function fileKey(file: File): string {
|
|
53
|
+
return `${file.name}:${file.size}:${file.lastModified}:${Math.random().toString(36).slice(2, 7)}`;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function DescribePage() {
|
|
57
|
+
const lr = useLrText();
|
|
58
|
+
const locale = useLocale();
|
|
59
|
+
const location = useLocation();
|
|
60
|
+
const prefs = useAgentPrefs();
|
|
61
|
+
const { connected, loading, provider } = useAgentStatus();
|
|
62
|
+
const deckId = new URLSearchParams(location.search).get('deck') ?? '';
|
|
63
|
+
const initialState = (location.state ?? {}) as { brief?: unknown };
|
|
64
|
+
|
|
65
|
+
const [brief, setBrief] = useState(() => {
|
|
66
|
+
if (typeof initialState.brief === 'string') return initialState.brief.slice(0, MAX_BRIEF);
|
|
67
|
+
const draft = readDraft();
|
|
68
|
+
return typeof draft.brief === 'string' ? draft.brief.slice(0, MAX_BRIEF) : '';
|
|
69
|
+
});
|
|
70
|
+
const [pages, setPages] = useState<number>(() => {
|
|
71
|
+
const draft = readDraft();
|
|
72
|
+
return OUTLINE_PAGE_SIZES.find((size) => size === draft.pages) ?? 8;
|
|
73
|
+
});
|
|
74
|
+
const [files, setFiles] = useState<DraftFile[]>([]);
|
|
75
|
+
const [uploaded, setUploaded] = useState<string[]>([]);
|
|
76
|
+
const [busy, setBusy] = useState(false);
|
|
77
|
+
const input = useRef<HTMLInputElement>(null);
|
|
78
|
+
const replacing = useRef<string | null>(null);
|
|
79
|
+
|
|
80
|
+
useEffect(() => {
|
|
81
|
+
writeDraft(brief, pages);
|
|
82
|
+
}, [brief, pages]);
|
|
83
|
+
|
|
84
|
+
useEffect(() => {
|
|
85
|
+
if (!deckId) return;
|
|
86
|
+
let cancelled = false;
|
|
87
|
+
fetch(`/__lr/outline?slideId=${encodeURIComponent(deckId)}`)
|
|
88
|
+
.then((res) => (res.ok ? res.json() : null))
|
|
89
|
+
.then((body: { outline: Outline | null } | null) => {
|
|
90
|
+
if (cancelled || !body?.outline) return;
|
|
91
|
+
setBrief(body.outline.brief);
|
|
92
|
+
setPages(body.outline.pages);
|
|
93
|
+
setUploaded(body.outline.files);
|
|
94
|
+
})
|
|
95
|
+
.catch(() => {});
|
|
96
|
+
return () => {
|
|
97
|
+
cancelled = true;
|
|
98
|
+
};
|
|
99
|
+
}, [deckId]);
|
|
100
|
+
|
|
101
|
+
const blocked = files.some((entry) => entry.verdict !== 'ok');
|
|
102
|
+
const canStart = brief.trim() !== '' && connected && !blocked && !busy;
|
|
103
|
+
const needsAgent = !loading && !connected;
|
|
104
|
+
|
|
105
|
+
async function takeFiles(list: FileList | null) {
|
|
106
|
+
if (!list || list.length === 0) return;
|
|
107
|
+
const incoming: DraftFile[] = [];
|
|
108
|
+
for (const file of Array.from(list)) {
|
|
109
|
+
incoming.push({ key: fileKey(file), file, verdict: await checkFileObject(file) });
|
|
110
|
+
}
|
|
111
|
+
const target = replacing.current;
|
|
112
|
+
replacing.current = null;
|
|
113
|
+
setFiles((prev) => [...prev.filter((entry) => entry.key !== target), ...incoming]);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function pick(replaceKey?: string) {
|
|
117
|
+
replacing.current = replaceKey ?? null;
|
|
118
|
+
input.current?.click();
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
async function start() {
|
|
122
|
+
if (!canStart || !provider) return;
|
|
123
|
+
setBusy(true);
|
|
124
|
+
try {
|
|
125
|
+
let slideId = deckId;
|
|
126
|
+
if (!slideId) {
|
|
127
|
+
const created = await fetch('/__slides', { method: 'POST' });
|
|
128
|
+
if (!created.ok) throw new Error('create failed');
|
|
129
|
+
slideId = ((await created.json()) as { slideId: string }).slideId;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const names = [...uploaded];
|
|
133
|
+
for (const entry of files) {
|
|
134
|
+
if (entry.verdict !== 'ok') continue;
|
|
135
|
+
const res = await uploadWithAutoRename(slideId, entry.file);
|
|
136
|
+
if (!res.ok || !res.entry) throw new Error('upload failed');
|
|
137
|
+
names.push(res.entry.name);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const outline: Outline = {
|
|
141
|
+
state: 'drafting',
|
|
142
|
+
brief: brief.trim(),
|
|
143
|
+
pages,
|
|
144
|
+
files: names,
|
|
145
|
+
items: [],
|
|
146
|
+
updatedAt: Date.now(),
|
|
147
|
+
built: 0,
|
|
148
|
+
paused: false,
|
|
149
|
+
stage: 'pages',
|
|
150
|
+
dispatched: null,
|
|
151
|
+
lastTurn: null,
|
|
152
|
+
prefs: readPrefs(localStorage),
|
|
153
|
+
decisions: [],
|
|
154
|
+
};
|
|
155
|
+
const started = await fetch('/__lr/outline/start', {
|
|
156
|
+
method: 'POST',
|
|
157
|
+
headers: { 'content-type': 'application/json' },
|
|
158
|
+
body: JSON.stringify({
|
|
159
|
+
slideId,
|
|
160
|
+
brief: outline.brief,
|
|
161
|
+
pages,
|
|
162
|
+
files: names,
|
|
163
|
+
prefs: outline.prefs,
|
|
164
|
+
}),
|
|
165
|
+
});
|
|
166
|
+
if (!started.ok) throw new Error('start failed');
|
|
167
|
+
|
|
168
|
+
const session = await createSession(provider.id, slideId);
|
|
169
|
+
setActiveSession(session.id);
|
|
170
|
+
await sendMessage(session.id, outlinePrompt(outline, slideId, locale.id), { slideId });
|
|
171
|
+
window.location.assign(`${basePath()}/s/${encodeURIComponent(slideId)}/outline`);
|
|
172
|
+
} catch {
|
|
173
|
+
toast.error(lr.outline.startFailed);
|
|
174
|
+
setBusy(false);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
return (
|
|
179
|
+
<div data-lr-new className="max-w-[760px]">
|
|
180
|
+
<Steps current={1} />
|
|
181
|
+
|
|
182
|
+
<header className="mt-6 mb-5">
|
|
183
|
+
<h1 className="font-heading text-[28px] font-bold leading-[34px] tracking-[-0.01em]">
|
|
184
|
+
{lr.outline.describeTitle}
|
|
185
|
+
</h1>
|
|
186
|
+
</header>
|
|
187
|
+
|
|
188
|
+
<section className="rounded-[16px] border border-border bg-card px-6 py-5 shadow-[var(--lr-shadow-1)]">
|
|
189
|
+
<Textarea
|
|
190
|
+
value={brief}
|
|
191
|
+
onChange={(e) => setBrief(e.target.value.slice(0, MAX_BRIEF))}
|
|
192
|
+
placeholder={lr.outline.describePlaceholder}
|
|
193
|
+
aria-label={lr.outline.describeTitle}
|
|
194
|
+
rows={3}
|
|
195
|
+
className="min-h-[88px] resize-none text-[15px]"
|
|
196
|
+
/>
|
|
197
|
+
|
|
198
|
+
<div className="mt-5">
|
|
199
|
+
<p className="text-[13px] font-semibold">{lr.outline.pagesLabel}</p>
|
|
200
|
+
<div role="radiogroup" aria-label={lr.outline.pagesLabel} className="mt-2 flex gap-2">
|
|
201
|
+
{OUTLINE_PAGE_SIZES.map((size) => (
|
|
202
|
+
// biome-ignore lint/a11y/useSemanticElements: the page count is a row of pills, as in the settings page
|
|
203
|
+
<button
|
|
204
|
+
key={size}
|
|
205
|
+
type="button"
|
|
206
|
+
role="radio"
|
|
207
|
+
aria-checked={pages === size}
|
|
208
|
+
aria-label={String(size)}
|
|
209
|
+
onClick={() => setPages(size)}
|
|
210
|
+
className={cn(
|
|
211
|
+
'h-8 min-w-[52px] rounded-[6px] border px-3 text-[13px] font-medium tabular-nums outline-none transition-colors focus-visible:ring-2 focus-visible:ring-ring/40',
|
|
212
|
+
pages === size
|
|
213
|
+
? 'border-[var(--lr-focus)] bg-[var(--lr-accent-soft)] text-foreground'
|
|
214
|
+
: 'border-border bg-card text-muted-foreground hover:border-[var(--lr-text-3)]',
|
|
215
|
+
)}
|
|
216
|
+
>
|
|
217
|
+
{size}
|
|
218
|
+
</button>
|
|
219
|
+
))}
|
|
220
|
+
</div>
|
|
221
|
+
</div>
|
|
222
|
+
|
|
223
|
+
<div className="mt-5">
|
|
224
|
+
<div className="flex items-center justify-between">
|
|
225
|
+
<p className="text-[13px] font-semibold">{lr.outline.filesLabel}</p>
|
|
226
|
+
<Button type="button" variant="outline" size="xs" onClick={() => pick()}>
|
|
227
|
+
{lr.outline.addFile}
|
|
228
|
+
</Button>
|
|
229
|
+
</div>
|
|
230
|
+
{(uploaded.length > 0 || files.length > 0) && (
|
|
231
|
+
<ul className="mt-2 flex flex-wrap gap-1.5">
|
|
232
|
+
{uploaded.map((name) => (
|
|
233
|
+
<li key={name}>
|
|
234
|
+
<span className="inline-flex items-center gap-1 rounded-full border border-border bg-muted/50 py-0.5 pr-1 pl-2.5 text-[12px]">
|
|
235
|
+
{name}
|
|
236
|
+
<button
|
|
237
|
+
type="button"
|
|
238
|
+
aria-label={`${lr.outline.removeFile}: ${name}`}
|
|
239
|
+
onClick={() => setUploaded((prev) => prev.filter((item) => item !== name))}
|
|
240
|
+
className="rounded-full px-1 text-muted-foreground hover:text-foreground"
|
|
241
|
+
>
|
|
242
|
+
×
|
|
243
|
+
</button>
|
|
244
|
+
</span>
|
|
245
|
+
</li>
|
|
246
|
+
))}
|
|
247
|
+
{files.map((entry) => (
|
|
248
|
+
<li key={entry.key}>
|
|
249
|
+
<span className="inline-flex items-center gap-1 rounded-full border border-border bg-muted/50 py-0.5 pr-1 pl-2.5 text-[12px]">
|
|
250
|
+
{entry.file.name}
|
|
251
|
+
<button
|
|
252
|
+
type="button"
|
|
253
|
+
aria-label={`${lr.outline.removeFile}: ${entry.file.name}`}
|
|
254
|
+
onClick={() =>
|
|
255
|
+
setFiles((prev) => prev.filter((item) => item.key !== entry.key))
|
|
256
|
+
}
|
|
257
|
+
className="rounded-full px-1 text-muted-foreground hover:text-foreground"
|
|
258
|
+
>
|
|
259
|
+
×
|
|
260
|
+
</button>
|
|
261
|
+
</span>
|
|
262
|
+
</li>
|
|
263
|
+
))}
|
|
264
|
+
</ul>
|
|
265
|
+
)}
|
|
266
|
+
<input
|
|
267
|
+
ref={input}
|
|
268
|
+
type="file"
|
|
269
|
+
multiple
|
|
270
|
+
hidden
|
|
271
|
+
data-lr-files
|
|
272
|
+
onChange={(e) => {
|
|
273
|
+
void takeFiles(e.target.files);
|
|
274
|
+
e.target.value = '';
|
|
275
|
+
}}
|
|
276
|
+
/>
|
|
277
|
+
</div>
|
|
278
|
+
</section>
|
|
279
|
+
|
|
280
|
+
{files
|
|
281
|
+
.filter((entry) => entry.verdict !== 'ok')
|
|
282
|
+
.map((entry) => (
|
|
283
|
+
<FileProblem
|
|
284
|
+
key={entry.key}
|
|
285
|
+
entry={entry}
|
|
286
|
+
onReplace={() => pick(entry.key)}
|
|
287
|
+
onRemove={() => setFiles((prev) => prev.filter((item) => item.key !== entry.key))}
|
|
288
|
+
/>
|
|
289
|
+
))}
|
|
290
|
+
|
|
291
|
+
{needsAgent && (
|
|
292
|
+
<div className="mt-5">
|
|
293
|
+
<ConnectView />
|
|
294
|
+
</div>
|
|
295
|
+
)}
|
|
296
|
+
|
|
297
|
+
<div className="mt-6 flex items-center justify-end">
|
|
298
|
+
<Button type="button" disabled={!canStart} onClick={() => void start()}>
|
|
299
|
+
{lr.outline.draftOutline}
|
|
300
|
+
</Button>
|
|
301
|
+
</div>
|
|
302
|
+
|
|
303
|
+
{prefs.outline === 'direct' && (
|
|
304
|
+
<p
|
|
305
|
+
data-lr-direct-notice
|
|
306
|
+
className="mt-2 text-right text-[12px] leading-snug text-muted-foreground"
|
|
307
|
+
>
|
|
308
|
+
{lr.prefs.directNotice}
|
|
309
|
+
</p>
|
|
310
|
+
)}
|
|
311
|
+
|
|
312
|
+
<Onboarding />
|
|
313
|
+
</div>
|
|
314
|
+
);
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
function FileProblem({
|
|
318
|
+
entry,
|
|
319
|
+
onReplace,
|
|
320
|
+
onRemove,
|
|
321
|
+
}: {
|
|
322
|
+
entry: DraftFile;
|
|
323
|
+
onReplace: () => void;
|
|
324
|
+
onRemove: () => void;
|
|
325
|
+
}) {
|
|
326
|
+
const t = useLrText().outline;
|
|
327
|
+
const name = entry.file.name;
|
|
328
|
+
const size = formatSize(entry.file.size);
|
|
329
|
+
|
|
330
|
+
const copy =
|
|
331
|
+
entry.verdict === 'too-large'
|
|
332
|
+
? {
|
|
333
|
+
title: t.tooLargeTitle,
|
|
334
|
+
hint: format(t.tooLargeHint, { name, size }),
|
|
335
|
+
actions: [
|
|
336
|
+
{ label: t.replaceFile, run: onReplace },
|
|
337
|
+
{ label: t.removeFile, run: onRemove },
|
|
338
|
+
],
|
|
339
|
+
}
|
|
340
|
+
: entry.verdict === 'locked'
|
|
341
|
+
? {
|
|
342
|
+
title: format(t.lockedTitle, { name }),
|
|
343
|
+
hint: t.lockedHint,
|
|
344
|
+
actions: [
|
|
345
|
+
{ label: t.reupload, run: onReplace },
|
|
346
|
+
{ label: t.skipFile, run: onRemove },
|
|
347
|
+
],
|
|
348
|
+
}
|
|
349
|
+
: {
|
|
350
|
+
title: format(t.unsupportedTitle, { name }),
|
|
351
|
+
hint: t.unsupportedHint,
|
|
352
|
+
actions: [{ label: t.removeFile, run: onRemove }],
|
|
353
|
+
};
|
|
354
|
+
|
|
355
|
+
return (
|
|
356
|
+
<div
|
|
357
|
+
data-lr-file-problem={entry.verdict}
|
|
358
|
+
className="mt-3 flex items-start gap-3 rounded-[12px] border border-border bg-card px-4 py-3"
|
|
359
|
+
>
|
|
360
|
+
<Mascot state="needs-you" size={30} />
|
|
361
|
+
<div className="min-w-0 flex-1">
|
|
362
|
+
<p className="text-[14px] font-semibold">{copy.title}</p>
|
|
363
|
+
<p className="mt-0.5 text-[12.5px] text-muted-foreground">{copy.hint}</p>
|
|
364
|
+
<div className="mt-2 flex flex-wrap gap-2">
|
|
365
|
+
{copy.actions.map((action) => (
|
|
366
|
+
<Button
|
|
367
|
+
key={action.label}
|
|
368
|
+
type="button"
|
|
369
|
+
variant="outline"
|
|
370
|
+
size="xs"
|
|
371
|
+
onClick={action.run}
|
|
372
|
+
>
|
|
373
|
+
{action.label}
|
|
374
|
+
</Button>
|
|
375
|
+
))}
|
|
376
|
+
</div>
|
|
377
|
+
</div>
|
|
378
|
+
</div>
|
|
379
|
+
);
|
|
380
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { format } from '../../../locale/format';
|
|
3
|
+
import { lrTextFor } from '../text/all';
|
|
4
|
+
import { checkFile, extensionOf, formatSize, MAX_FILE_BYTES } from './files';
|
|
5
|
+
|
|
6
|
+
const bytes = (values: number[], fill = 16): Uint8Array => {
|
|
7
|
+
const head = new Uint8Array(fill).fill(0x50);
|
|
8
|
+
head.set(values);
|
|
9
|
+
return head;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
const pdf = (text = '') => {
|
|
13
|
+
const head = new Uint8Array(1024);
|
|
14
|
+
head.set(new TextEncoder().encode(`%PDF-1.7\n${text}`));
|
|
15
|
+
return head;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
describe('checkFile', () => {
|
|
19
|
+
it('accepts an ordinary file of a supported kind', () => {
|
|
20
|
+
expect(checkFile({ name: 'notes.pdf', size: 1024, head: pdf() })).toBe('ok');
|
|
21
|
+
expect(checkFile({ name: 'sheet.xlsx', size: 2048, head: bytes([0x50, 0x4b, 3, 4]) })).toBe(
|
|
22
|
+
'ok',
|
|
23
|
+
);
|
|
24
|
+
expect(checkFile({ name: 'Assumptions.MD', size: 10, head: bytes([]) })).toBe('ok');
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('rejects anything over 25 MB', () => {
|
|
28
|
+
expect(checkFile({ name: 'big.pdf', size: MAX_FILE_BYTES + 1, head: pdf() })).toBe('too-large');
|
|
29
|
+
expect(checkFile({ name: 'exact.pdf', size: MAX_FILE_BYTES, head: pdf() })).toBe('ok');
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('rejects kinds it cannot hand over', () => {
|
|
33
|
+
expect(checkFile({ name: 'movie.mp4', size: 100, head: bytes([]) })).toBe('unsupported');
|
|
34
|
+
expect(checkFile({ name: 'noext', size: 100, head: bytes([]) })).toBe('unsupported');
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('catches a password-protected PDF', () => {
|
|
38
|
+
expect(checkFile({ name: 'locked.pdf', size: 100, head: pdf('/Encrypt 5 0 R') })).toBe(
|
|
39
|
+
'locked',
|
|
40
|
+
);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('ignores /Encrypt past the first kilobyte', () => {
|
|
44
|
+
const head = new Uint8Array(4096).fill(0x20);
|
|
45
|
+
head.set(new TextEncoder().encode('/Encrypt'), 2048);
|
|
46
|
+
expect(checkFile({ name: 'late.pdf', size: 4096, head })).toBe('ok');
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it('catches an encrypted Office document by its OLE2 header', () => {
|
|
50
|
+
expect(
|
|
51
|
+
checkFile({ name: 'locked.docx', size: 100, head: bytes([0xd0, 0xcf, 0x11, 0xe0]) }),
|
|
52
|
+
).toBe('locked');
|
|
53
|
+
expect(
|
|
54
|
+
checkFile({ name: 'locked.pptx', size: 100, head: bytes([0xd0, 0xcf, 0x11, 0xe0]) }),
|
|
55
|
+
).toBe('locked');
|
|
56
|
+
expect(
|
|
57
|
+
checkFile({ name: 'locked.xlsx', size: 100, head: bytes([0xd0, 0xcf, 0x11, 0xe0]) }),
|
|
58
|
+
).toBe('locked');
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it('leaves the old binary Office formats alone', () => {
|
|
62
|
+
expect(checkFile({ name: 'old.doc', size: 100, head: bytes([0xd0, 0xcf, 0x11, 0xe0]) })).toBe(
|
|
63
|
+
'ok',
|
|
64
|
+
);
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
describe('helpers', () => {
|
|
69
|
+
it('reads the extension case-insensitively', () => {
|
|
70
|
+
expect(extensionOf('a/b/Report.PDF')).toBe('pdf');
|
|
71
|
+
expect(extensionOf('no-dot')).toBe('');
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it('shows sizes as bare numbers', () => {
|
|
75
|
+
expect(formatSize(30 * 1024 * 1024)).toBe('30');
|
|
76
|
+
expect(formatSize(26.5 * 1024 * 1024)).toBe('27');
|
|
77
|
+
expect(formatSize(1.4 * 1024 * 1024)).toBe('1.4');
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it('states the unit once in the too-large hint', () => {
|
|
81
|
+
const size = formatSize(30 * 1024 * 1024);
|
|
82
|
+
const hint = (id: 'en' | 'zh-CN') =>
|
|
83
|
+
format(lrTextFor(id).outline.tooLargeHint, { name: 'expense-policy.pdf', size });
|
|
84
|
+
|
|
85
|
+
expect(hint('en')).toContain('expense-policy.pdf is 30 MB.');
|
|
86
|
+
expect(hint('zh-CN')).toContain('expense-policy.pdf 有 30 MB。');
|
|
87
|
+
expect(hint('en')).not.toContain('MB MB');
|
|
88
|
+
expect(hint('zh-CN')).not.toContain('MB MB');
|
|
89
|
+
});
|
|
90
|
+
});
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
export const MAX_FILE_BYTES = 25 * 1024 * 1024;
|
|
2
|
+
|
|
3
|
+
/** What the browser can hand the agent as a reference file. */
|
|
4
|
+
export const FILE_EXTENSIONS = [
|
|
5
|
+
'pdf',
|
|
6
|
+
'docx',
|
|
7
|
+
'doc',
|
|
8
|
+
'xlsx',
|
|
9
|
+
'xls',
|
|
10
|
+
'pptx',
|
|
11
|
+
'ppt',
|
|
12
|
+
'csv',
|
|
13
|
+
'txt',
|
|
14
|
+
'md',
|
|
15
|
+
'png',
|
|
16
|
+
'jpg',
|
|
17
|
+
'jpeg',
|
|
18
|
+
'webp',
|
|
19
|
+
'gif',
|
|
20
|
+
] as const;
|
|
21
|
+
|
|
22
|
+
const OFFICE_LOCKED_EXTENSIONS = new Set(['docx', 'xlsx', 'pptx']);
|
|
23
|
+
// Encrypted Office files are OLE2 containers; the normal ones are zip archives.
|
|
24
|
+
const OLE2_MAGIC = [0xd0, 0xcf, 0x11, 0xe0];
|
|
25
|
+
// A PDF with this in its header carries an encryption dictionary.
|
|
26
|
+
const PDF_ENCRYPT = '/Encrypt';
|
|
27
|
+
|
|
28
|
+
export type FileVerdict = 'ok' | 'too-large' | 'locked' | 'unsupported';
|
|
29
|
+
|
|
30
|
+
export type FileFacts = { name: string; size: number; head: Uint8Array };
|
|
31
|
+
|
|
32
|
+
export function extensionOf(name: string): string {
|
|
33
|
+
const dot = name.lastIndexOf('.');
|
|
34
|
+
return dot === -1 ? '' : name.slice(dot + 1).toLowerCase();
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function startsWith(head: Uint8Array, bytes: number[]): boolean {
|
|
38
|
+
if (head.length < bytes.length) return false;
|
|
39
|
+
return bytes.every((byte, i) => head[i] === byte);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function containsText(head: Uint8Array, text: string): boolean {
|
|
43
|
+
const needle = Array.from(text, (char) => char.charCodeAt(0));
|
|
44
|
+
if (head.length < needle.length) return false;
|
|
45
|
+
for (let start = 0; start <= head.length - needle.length; start++) {
|
|
46
|
+
if (needle.every((byte, i) => head[start + i] === byte)) return true;
|
|
47
|
+
}
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function checkFile({ name, size, head }: FileFacts): FileVerdict {
|
|
52
|
+
if (size > MAX_FILE_BYTES) return 'too-large';
|
|
53
|
+
const ext = extensionOf(name);
|
|
54
|
+
if (!(FILE_EXTENSIONS as readonly string[]).includes(ext)) return 'unsupported';
|
|
55
|
+
if (ext === 'pdf' && containsText(head.slice(0, 1024), PDF_ENCRYPT)) return 'locked';
|
|
56
|
+
if (OFFICE_LOCKED_EXTENSIONS.has(ext) && startsWith(head, OLE2_MAGIC)) return 'locked';
|
|
57
|
+
return 'ok';
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Reads only the bytes a verdict needs, so a 300 MB file is not loaded to reject it. */
|
|
61
|
+
export async function checkFileObject(file: File): Promise<FileVerdict> {
|
|
62
|
+
const head = new Uint8Array(await file.slice(0, 1024).arrayBuffer());
|
|
63
|
+
return checkFile({ name: file.name, size: file.size, head });
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function formatSize(bytes: number): string {
|
|
67
|
+
const mb = bytes / (1024 * 1024);
|
|
68
|
+
return String(mb >= 10 ? Math.round(mb) : Math.round(mb * 10) / 10);
|
|
69
|
+
}
|