victor HF Staff commited on
Commit
d2390f1
·
unverified ·
1 Parent(s): 1d449d6

Add image attachment support to router examples (#1883)

Browse files

Simplify image attachment implementation for router examples

- Simplified RouterExampleAttachment type to only require src field
- Removed resolveExampleSrc helper function in favor of direct path construction
- Let browser infer MIME types from blob response
- Reduced code complexity while maintaining functionality

src/lib/components/chat/ChatWindow.svelte CHANGED
@@ -286,8 +286,26 @@
286
  handleSubmit();
287
  }
288
 
289
- function startExample(example: RouterExample) {
290
  activeRouterExamplePrompt = example.prompt;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
291
  triggerPrompt(example.prompt);
292
  }
293
 
 
286
  handleSubmit();
287
  }
288
 
289
+ async function startExample(example: RouterExample) {
290
  activeRouterExamplePrompt = example.prompt;
291
+
292
+ if (browser && example.attachments?.length) {
293
+ const loadedFiles: File[] = [];
294
+ for (const attachment of example.attachments) {
295
+ try {
296
+ const response = await fetch(`${base}/${attachment.src}`);
297
+ if (!response.ok) continue;
298
+
299
+ const blob = await response.blob();
300
+ const name = attachment.src.split("/").pop() ?? "attachment";
301
+ loadedFiles.push(new File([blob], name, { type: blob.type || "application/octet-stream" }));
302
+ } catch (err) {
303
+ console.error("Error loading attachment:", err);
304
+ }
305
+ }
306
+ files = loadedFiles;
307
+ }
308
+
309
  triggerPrompt(example.prompt);
310
  }
311
 
src/lib/constants/routerExamples.ts CHANGED
@@ -3,10 +3,15 @@ export type RouterFollowUp = {
3
  prompt: string;
4
  };
5
 
 
 
 
 
6
  export type RouterExample = {
7
  title: string;
8
  prompt: string;
9
  followUps?: RouterFollowUp[];
 
10
  };
11
 
12
  export const routerExamples: RouterExample[] = [
@@ -30,6 +35,15 @@ export const routerExamples: RouterExample[] = [
30
  },
31
  ],
32
  },
 
 
 
 
 
 
 
 
 
33
  {
34
  title: "Landing page",
35
  prompt:
 
3
  prompt: string;
4
  };
5
 
6
+ export type RouterExampleAttachment = {
7
+ src: string;
8
+ };
9
+
10
  export type RouterExample = {
11
  title: string;
12
  prompt: string;
13
  followUps?: RouterFollowUp[];
14
+ attachments?: RouterExampleAttachment[];
15
  };
16
 
17
  export const routerExamples: RouterExample[] = [
 
35
  },
36
  ],
37
  },
38
+ {
39
+ title: "Weird painting",
40
+ prompt: "is this a real painting?",
41
+ attachments: [
42
+ {
43
+ src: "huggingchat/castle-example.jpg",
44
+ },
45
+ ],
46
+ },
47
  {
48
  title: "Landing page",
49
  prompt:
static/huggingchat/castle-example.jpg ADDED