summaryrefslogtreecommitdiffhomepage
path: root/.rules/plans/image-attachment-part2.md
blob: c09fa187001d675b133c49de0abb3af9de915c8e (plain)
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
# Image Attachment — Part 2: Frontend (UI, Chat View, CSS)

## Overview

This part adds the user-facing UI for image attachments: the attach button, file picker, thumbnail preview strip, and the integration in `handleSend()` that converts images and injects the context note for the AI.

**Prerequisites:** Part 1 must be completed first (image-attachments module, save_image tool, pre-validation).

---

## Step 5: Update `src/chat-view.ts`

### New Imports

```typescript
import { setCurrentAttachments, clearCurrentAttachments } from "./image-attachments";
import type { ImageAttachment } from "./image-attachments";
```

### New Class Fields

```typescript
interface PendingAttachment {
    file: File;
    dataUrl: string;
    mimeType: string;
}

// In the ChatView class:
private pendingAttachments: PendingAttachment[] = [];
private attachmentStrip: HTMLDivElement | null = null;
private attachButton: HTMLButtonElement | null = null;
private fileInput: HTMLInputElement | null = null;
```

### UI Construction (in `onOpen()`)

Update the input row section. Current structure:

```
[textarea] [send]
```

New structure:

```
[attachment-strip (above input row, hidden when empty)]
[attach-btn] [textarea] [send]
```

#### Attachment Strip (above input row)

```typescript
// Before the inputRow creation:
this.attachmentStrip = messagesArea.createDiv({ cls: "ai-pulse-attachment-strip" });
this.attachmentStrip.style.display = "none";
```

#### Attach Button (left of textarea)

```typescript
// Inside the inputRow, before the textarea:
this.attachButton = inputRow.createEl("button", {
    cls: "ai-pulse-attach-btn",
    attr: { "aria-label": "Attach image" },
});
setIcon(this.attachButton, "image-plus");

// Hidden file input
this.fileInput = inputRow.createEl("input", {
    type: "file",
    attr: {
        accept: "image/jpeg,image/png,image/gif,image/webp,image/bmp,image/svg+xml",
        multiple: "",
        style: "display:none",
    },
});

this.attachButton.addEventListener("click", () => {
    this.fileInput?.click();
});

this.fileInput.addEventListener("change", () => {
    if (this.fileInput === null || this.fileInput.files === null) return;
    void this.handleFileSelection(this.fileInput.files);
    this.fileInput.value = ""; // Reset so same file can be re-selected
});
```

### File Selection Handler

```typescript
private async handleFileSelection(files: FileList): Promise<void> {
    for (let i = 0; i < files.length; i++) {
        const file = files[i];
        if (file === undefined) continue;
        if (!file.type.startsWith("image/")) continue;

        const dataUrl = await this.readFileAsDataUrl(file);
        this.pendingAttachments.push({
            file,
            dataUrl,
            mimeType: file.type,
        });
    }
    this.renderAttachmentStrip();
}

private readFileAsDataUrl(file: File): Promise<string> {
    return new Promise<string>((resolve, reject) => {
        const reader = new FileReader();
        reader.onload = () => {
            if (typeof reader.result === "string") {
                resolve(reader.result);
            } else {
                reject(new Error("Failed to read file as data URL."));
            }
        };
        reader.onerror = () => reject(new Error("File read error."));
        reader.readAsDataURL(file);
    });
}
```

### Thumbnail Strip Rendering

```typescript
private renderAttachmentStrip(): void {
    if (this.attachmentStrip === null) return;
    this.attachmentStrip.empty();

    if (this.pendingAttachments.length === 0) {
        this.attachmentStrip.style.display = "none";
        return;
    }

    this.attachmentStrip.style.display = "flex";

    for (let i = 0; i < this.pendingAttachments.length; i++) {
        const attachment = this.pendingAttachments[i];
        if (attachment === undefined) continue;

        const thumb = this.attachmentStrip.createDiv({ cls: "ai-pulse-attachment-thumb" });
        const img = thumb.createEl("img", {
            attr: { src: attachment.dataUrl, alt: attachment.file.name },
        });
        void img; // suppress unused

        const removeBtn = thumb.createEl("button", {
            cls: "ai-pulse-attachment-remove",
            attr: { "aria-label": "Remove" },
        });
        setIcon(removeBtn, "x");

        const index = i;
        removeBtn.addEventListener("click", () => {
            this.pendingAttachments.splice(index, 1);
            this.renderAttachmentStrip();
        });
    }
}
```

### Update `handleSend()`

In `handleSend()`, after getting the text and before pushing the user message:

```typescript
// Convert pending attachments to ImageAttachment format for the tool
let messageContent = text;
if (this.pendingAttachments.length > 0) {
    const imageAttachments: ImageAttachment[] = [];
    for (const pa of this.pendingAttachments) {
        const arrayBuffer = await pa.file.arrayBuffer();
        const bytes = new Uint8Array(arrayBuffer);
        let binary = "";
        for (const b of bytes) binary += String.fromCharCode(b);
        const base64 = btoa(binary);

        imageAttachments.push({
            base64,
            mimeType: pa.mimeType,
            originalName: pa.file.name,
            arrayBuffer,
        });
    }

    // Set the module-level attachments for the save_image tool to access
    setCurrentAttachments(imageAttachments);

    // Prepend context note to the message for the LLM
    const count = this.pendingAttachments.length;
    messageContent = `[${count} image(s) are attached to this message. You MUST use the save_image tool to save them to the vault. Infer from the user's message how and where these images should be saved and embedded. Assume the user wants the images attached to whatever note they are asking you to create or edit.]\n\n${text}`;

    // Clear the UI attachments
    this.pendingAttachments = [];
    this.renderAttachmentStrip();
}

// Display the original text (without the context prefix) in the chat
this.appendMessage("user", text);

// Track the augmented message in history for the LLM
this.messages.push({ role: "user", content: messageContent });
```

**Important:** The existing code pushes `text` as the message content. Change it so:
- `appendMessage("user", text)` — shows original text in UI
- `this.messages.push({ role: "user", content: messageContent })` — sends augmented content to LLM

### Clean Up on Close

In `onClose()`:

```typescript
this.pendingAttachments = [];
clearCurrentAttachments();
this.attachmentStrip = null;
this.attachButton = null;
this.fileInput = null;
```

### Clean Up on Error/Abort

In the `catch` block of `handleSend()`, if the send fails:

```typescript
clearCurrentAttachments();
```

This ensures stale attachments don't leak to the next message.

### Streaming State

When streaming starts, disable the attach button:

```typescript
private setStreamingState(streaming: boolean): void {
    // ...existing code...
    if (this.attachButton !== null) {
        this.attachButton.disabled = streaming;
    }
}
```

---

## Step 6: CSS Changes (`styles.css`)

### Attach Button

```css
.ai-pulse-attach-btn {
    flex-shrink: 0;
    background: none;
    border: none;
    cursor: pointer;
    padding: 4px;
    border-radius: 4px;
    color: var(--text-muted);
    display: flex;
    align-items: center;
    justify-content: center;
}

.ai-pulse-attach-btn:hover {
    color: var(--text-normal);
    background: var(--background-modifier-hover);
}

.ai-pulse-attach-btn:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}
```

### Attachment Preview Strip

```css
.ai-pulse-attachment-strip {
    display: flex;
    gap: 6px;
    padding: 6px 8px;
    overflow-x: auto;
    flex-wrap: nowrap;
}

.ai-pulse-attachment-thumb {
    position: relative;
    width: 48px;
    height: 48px;
    border-radius: 4px;
    overflow: visible;
    flex-shrink: 0;
    border: 1px solid var(--background-modifier-border);
}

.ai-pulse-attachment-thumb img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    border-radius: 4px;
}

.ai-pulse-attachment-remove {
    position: absolute;
    top: -6px;
    right: -6px;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    background: var(--background-secondary);
    border: 1px solid var(--background-modifier-border);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    color: var(--text-muted);
}

.ai-pulse-attachment-remove:hover {
    background: var(--background-modifier-error);
    color: var(--text-on-accent);
}

.ai-pulse-attachment-remove svg {
    width: 12px;
    height: 12px;
}
```

### Input Row Update

Ensure the input row uses flexbox with proper alignment:

```css
.ai-pulse-input-row {
    display: flex;
    align-items: flex-end;
    gap: 4px;
    /* ...existing padding/margin styles... */
}
```

---

## Step 7: Testing

### Desktop
1. Open the chat panel
2. Click the attach button → file picker opens
3. Select one or more images → thumbnails appear in the strip
4. Click × on a thumbnail → it's removed
5. Type a message like "save this image to my notes/photos folder"
6. Send → AI calls save_image → approval prompt → image saved → AI embeds in note

### Mobile
1. Same flow as desktop
2. File picker should open the camera roll / gallery
3. Verify the thumbnails render correctly on small screens
4. Verify the attach button is accessible and doesn't crowd the input

### Edge Cases
- Send message with no text but images attached (should still work — the context note provides enough info)
- Abort mid-stream → attachments should be cleared
- Clear chat while attachments are pending → attachments should be cleared
- Multiple images with same name → numbered suffixes prevent collisions

---

## Summary of All File Changes

### Files Created (Part 1)
- `src/image-attachments.ts`
- `src/context/tools/save-image.json`

### Files Modified (Part 1)
- `src/tools.ts` — new tool entry + execute function
- `src/ollama-client.ts` — pre-validation

### Files Modified (Part 2)
- `src/chat-view.ts` — attach button, file picker, thumbnail strip, handleSend() changes
- `styles.css` — new CSS classes for attachment UI