improved imgs
This commit is contained in:
parent
8186e2adc1
commit
ea46448dd9
3 changed files with 131 additions and 44 deletions
17
README.md
17
README.md
|
|
@ -12,20 +12,18 @@ Clone of [T3 Chat](https://t3.chat/)
|
||||||
- **Framework**: SvelteKit
|
- **Framework**: SvelteKit
|
||||||
- **Language**: TypeScript
|
- **Language**: TypeScript
|
||||||
- **Styling**: Tailwind
|
- **Styling**: Tailwind
|
||||||
|
- **Backend**: Convex
|
||||||
|
- **Auth**: BetterAuth + Convex
|
||||||
- **Components**: Melt UI (next-gen)
|
- **Components**: Melt UI (next-gen)
|
||||||
- **Testing**: Humans
|
- **Testing**: Humans
|
||||||
- **Package Manager**: pnpm
|
- **Package Manager**: pnpm
|
||||||
- **Linting**: ESLint
|
- **Linting**: ESLint
|
||||||
- **Formatting**: Prettier
|
- **Formatting**: Prettier
|
||||||
|
|
||||||
### Discussion
|
|
||||||
|
|
||||||
- Vercel SDK?
|
|
||||||
- Nah, too limited
|
|
||||||
|
|
||||||
## 📦 Self-hosting
|
## 📦 Self-hosting
|
||||||
|
|
||||||
IDK, calm down
|
TODO: test self-hosting, including Convex self-hosting perhaps
|
||||||
|
TODO: add instructions
|
||||||
|
|
||||||
## TODO
|
## TODO
|
||||||
|
|
||||||
|
|
@ -39,14 +37,14 @@ IDK, calm down
|
||||||
- [ ] OpenAI
|
- [ ] OpenAI
|
||||||
- [ ] File upload
|
- [ ] File upload
|
||||||
- [x] Ensure responsiveness
|
- [x] Ensure responsiveness
|
||||||
- [ ] File support
|
- [x] Streams on the server (Resumable streams)
|
||||||
- [x] Streams on the server
|
|
||||||
- [x] Syntax highlighting with Shiki/markdown renderer
|
- [x] Syntax highlighting with Shiki/markdown renderer
|
||||||
- [ ] Eliminate FOUC
|
- [ ] Eliminate FOUC
|
||||||
- [ ] Cascade deletes and shit in Convex
|
- [x] Cascade deletes
|
||||||
- [ ] Error notification central, specially for BYOK models like o3
|
- [ ] Error notification central, specially for BYOK models like o3
|
||||||
- [ ] Google Auth
|
- [ ] Google Auth
|
||||||
- [ ] Fix light mode (urgh)
|
- [ ] Fix light mode (urgh)
|
||||||
|
- [ ] Streamer mode
|
||||||
|
|
||||||
### Chat
|
### Chat
|
||||||
|
|
||||||
|
|
@ -66,3 +64,4 @@ IDK, calm down
|
||||||
- [ ] Chat sharing
|
- [ ] Chat sharing
|
||||||
- [ ] 404 page/redirect
|
- [ ] 404 page/redirect
|
||||||
- [ ] Test link with free credits
|
- [ ] Test link with free credits
|
||||||
|
- [x] Cursor-like Rules (@ieedan's idea!)
|
||||||
|
|
|
||||||
73
src/lib/utils/image-compression.ts
Normal file
73
src/lib/utils/image-compression.ts
Normal file
|
|
@ -0,0 +1,73 @@
|
||||||
|
export function compressImage(file: File, maxSizeBytes: number = 1024 * 1024): Promise<File> {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
const canvas = document.createElement('canvas');
|
||||||
|
const ctx = canvas.getContext('2d');
|
||||||
|
const img = new Image();
|
||||||
|
|
||||||
|
img.onload = () => {
|
||||||
|
// Calculate new dimensions to maintain aspect ratio
|
||||||
|
let { width, height } = img;
|
||||||
|
const maxDimension = 1920; // Max width or height
|
||||||
|
|
||||||
|
if (width > maxDimension || height > maxDimension) {
|
||||||
|
if (width > height) {
|
||||||
|
height = (height * maxDimension) / width;
|
||||||
|
width = maxDimension;
|
||||||
|
} else {
|
||||||
|
width = (width * maxDimension) / height;
|
||||||
|
height = maxDimension;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
canvas.width = width;
|
||||||
|
canvas.height = height;
|
||||||
|
|
||||||
|
if (!ctx) {
|
||||||
|
reject(new Error('Could not get canvas context'));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Draw and compress
|
||||||
|
ctx.drawImage(img, 0, 0, width, height);
|
||||||
|
|
||||||
|
// Start with high quality and reduce until under size limit
|
||||||
|
let quality = 0.9;
|
||||||
|
let compressed: File | null = null;
|
||||||
|
|
||||||
|
const tryCompress = () => {
|
||||||
|
canvas.toBlob(
|
||||||
|
(blob) => {
|
||||||
|
if (!blob) {
|
||||||
|
reject(new Error('Failed to compress image'));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
compressed = new File([blob], file.name, {
|
||||||
|
type: 'image/jpeg',
|
||||||
|
lastModified: Date.now(),
|
||||||
|
});
|
||||||
|
|
||||||
|
// If under size limit or quality is too low, return result
|
||||||
|
if (compressed.size <= maxSizeBytes || quality <= 0.1) {
|
||||||
|
resolve(compressed);
|
||||||
|
} else {
|
||||||
|
// Reduce quality and try again
|
||||||
|
quality -= 0.1;
|
||||||
|
tryCompress();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
'image/jpeg',
|
||||||
|
quality
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
tryCompress();
|
||||||
|
};
|
||||||
|
|
||||||
|
img.onerror = () => {
|
||||||
|
reject(new Error('Failed to load image'));
|
||||||
|
};
|
||||||
|
|
||||||
|
img.src = URL.createObjectURL(file);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
@ -11,31 +11,32 @@
|
||||||
import * as Sidebar from '$lib/components/ui/sidebar';
|
import * as Sidebar from '$lib/components/ui/sidebar';
|
||||||
import Tooltip from '$lib/components/ui/tooltip.svelte';
|
import Tooltip from '$lib/components/ui/tooltip.svelte';
|
||||||
import { TextareaAutosize } from '$lib/spells/textarea-autosize.svelte.js';
|
import { TextareaAutosize } from '$lib/spells/textarea-autosize.svelte.js';
|
||||||
|
import { models } from '$lib/state/models.svelte';
|
||||||
import { usePrompt } from '$lib/state/prompt.svelte.js';
|
import { usePrompt } from '$lib/state/prompt.svelte.js';
|
||||||
import { session } from '$lib/state/session.svelte.js';
|
import { session } from '$lib/state/session.svelte.js';
|
||||||
import { settings } from '$lib/state/settings.svelte.js';
|
import { settings } from '$lib/state/settings.svelte.js';
|
||||||
|
import { Provider } from '$lib/types';
|
||||||
import { isString } from '$lib/utils/is.js';
|
import { isString } from '$lib/utils/is.js';
|
||||||
import { pick } from '$lib/utils/object.js';
|
import { supportsImages } from '$lib/utils/model-capabilities';
|
||||||
|
import { omit, pick } from '$lib/utils/object.js';
|
||||||
import { cn } from '$lib/utils/utils.js';
|
import { cn } from '$lib/utils/utils.js';
|
||||||
|
import { compressImage } from '$lib/utils/image-compression';
|
||||||
import { useConvexClient } from 'convex-svelte';
|
import { useConvexClient } from 'convex-svelte';
|
||||||
import { Popover } from 'melt/builders';
|
import { FileUpload, Popover } from 'melt/builders';
|
||||||
import { Avatar } from 'melt/components';
|
import { Avatar } from 'melt/components';
|
||||||
import { Debounced, ElementSize, IsMounted, ScrollState } from 'runed';
|
import { Debounced, ElementSize, IsMounted, ScrollState } from 'runed';
|
||||||
import SendIcon from '~icons/lucide/arrow-up';
|
import SendIcon from '~icons/lucide/arrow-up';
|
||||||
import ChevronDownIcon from '~icons/lucide/chevron-down';
|
import ChevronDownIcon from '~icons/lucide/chevron-down';
|
||||||
|
import ImageIcon from '~icons/lucide/image';
|
||||||
import LoaderCircleIcon from '~icons/lucide/loader-circle';
|
import LoaderCircleIcon from '~icons/lucide/loader-circle';
|
||||||
import PanelLeftIcon from '~icons/lucide/panel-left';
|
import PanelLeftIcon from '~icons/lucide/panel-left';
|
||||||
import PinIcon from '~icons/lucide/pin';
|
import PinIcon from '~icons/lucide/pin';
|
||||||
import PinOffIcon from '~icons/lucide/pin-off';
|
import PinOffIcon from '~icons/lucide/pin-off';
|
||||||
import Settings2Icon from '~icons/lucide/settings-2';
|
import Settings2Icon from '~icons/lucide/settings-2';
|
||||||
import XIcon from '~icons/lucide/x';
|
import XIcon from '~icons/lucide/x';
|
||||||
|
import UploadIcon from '~icons/lucide/upload';
|
||||||
import { callGenerateMessage } from '../api/generate-message/call.js';
|
import { callGenerateMessage } from '../api/generate-message/call.js';
|
||||||
import ModelPicker from './model-picker.svelte';
|
import ModelPicker from './model-picker.svelte';
|
||||||
import { models } from '$lib/state/models.svelte';
|
|
||||||
import { supportsImages } from '$lib/utils/model-capabilities';
|
|
||||||
import { Provider } from '$lib/types';
|
|
||||||
import { FileUpload } from 'melt/builders';
|
|
||||||
import ImageIcon from '~icons/lucide/image';
|
|
||||||
|
|
||||||
const client = useConvexClient();
|
const client = useConvexClient();
|
||||||
|
|
||||||
|
|
@ -182,7 +183,7 @@
|
||||||
const currentModelSupportsImages = $derived.by(() => {
|
const currentModelSupportsImages = $derived.by(() => {
|
||||||
if (!settings.modelId) return false;
|
if (!settings.modelId) return false;
|
||||||
const openRouterModels = models.from(Provider.OpenRouter);
|
const openRouterModels = models.from(Provider.OpenRouter);
|
||||||
const currentModel = openRouterModels.find(m => m.id === settings.modelId);
|
const currentModel = openRouterModels.find((m) => m.id === settings.modelId);
|
||||||
return currentModel ? supportsImages(currentModel) : false;
|
return currentModel ? supportsImages(currentModel) : false;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -200,15 +201,24 @@
|
||||||
|
|
||||||
try {
|
try {
|
||||||
for (const file of files) {
|
for (const file of files) {
|
||||||
|
// Skip non-image files
|
||||||
|
if (!file.type.startsWith('image/')) {
|
||||||
|
console.warn('Skipping non-image file:', file.name);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Compress image to max 1MB
|
||||||
|
const compressedFile = await compressImage(file, 1024 * 1024);
|
||||||
|
|
||||||
// Generate upload URL
|
// Generate upload URL
|
||||||
const uploadUrl = await client.mutation(api.storage.generateUploadUrl, {
|
const uploadUrl = await client.mutation(api.storage.generateUploadUrl, {
|
||||||
session_token: session.current.session.token,
|
session_token: session.current.session.token,
|
||||||
});
|
});
|
||||||
|
|
||||||
// Upload file
|
// Upload compressed file
|
||||||
const result = await fetch(uploadUrl, {
|
const result = await fetch(uploadUrl, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: file,
|
body: compressedFile,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!result.ok) {
|
if (!result.ok) {
|
||||||
|
|
@ -369,7 +379,10 @@
|
||||||
<title>Chat | Thom.chat</title>
|
<title>Chat | Thom.chat</title>
|
||||||
</svelte:head>
|
</svelte:head>
|
||||||
|
|
||||||
<Sidebar.Root class="h-screen overflow-clip">
|
<Sidebar.Root
|
||||||
|
class="h-screen overflow-clip"
|
||||||
|
{...currentModelSupportsImages ? omit(fileUpload.dropzone, ['onclick']) : {}}
|
||||||
|
>
|
||||||
<Sidebar.Sidebar class="flex flex-col overflow-clip p-2">
|
<Sidebar.Sidebar class="flex flex-col overflow-clip p-2">
|
||||||
<div class="flex place-items-center justify-center py-2">
|
<div class="flex place-items-center justify-center py-2">
|
||||||
<span class="text-center font-serif text-lg">Thom.chat</span>
|
<span class="text-center font-serif text-lg">Thom.chat</span>
|
||||||
|
|
@ -592,14 +605,20 @@
|
||||||
{/if}
|
{/if}
|
||||||
<div class="flex flex-grow flex-col">
|
<div class="flex flex-grow flex-col">
|
||||||
{#if selectedImages.length > 0}
|
{#if selectedImages.length > 0}
|
||||||
<div class="flex flex-wrap gap-2 mb-2">
|
<div class="mb-2 flex flex-wrap gap-2">
|
||||||
{#each selectedImages as image, index}
|
{#each selectedImages as image, index (image.storage_id)}
|
||||||
<div class="relative">
|
<div
|
||||||
<img src={image.url} alt="Uploaded" class="h-16 w-16 rounded-lg object-cover" />
|
class="group border-secondary-foreground/[0.08] bg-secondary-foreground/[0.02] hover:bg-secondary-foreground/10 relative flex h-12 w-12 max-w-full shrink-0 items-center justify-center gap-2 rounded-xl border border-solid p-0 transition-[width,height] duration-500"
|
||||||
|
>
|
||||||
|
<img
|
||||||
|
src={image.url}
|
||||||
|
alt="Uploaded"
|
||||||
|
class="size-10 rounded-lg object-cover opacity-100 transition-opacity"
|
||||||
|
/>
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
onclick={() => removeImage(index)}
|
onclick={() => removeImage(index)}
|
||||||
class="absolute -top-2 -right-2 flex h-5 w-5 items-center justify-center rounded-full bg-red-500 text-white hover:bg-red-600"
|
class="bg-secondary hover:bg-muted absolute -top-1 -right-1 cursor-pointer rounded-full p-1 opacity-0 transition group-hover:opacity-100"
|
||||||
>
|
>
|
||||||
<XIcon class="h-3 w-3" />
|
<XIcon class="h-3 w-3" />
|
||||||
</button>
|
</button>
|
||||||
|
|
@ -607,15 +626,7 @@
|
||||||
{/each}
|
{/each}
|
||||||
</div>
|
</div>
|
||||||
{/if}
|
{/if}
|
||||||
<div
|
<div class="relative flex flex-grow flex-row items-start">
|
||||||
{...fileUpload.dropzone}
|
|
||||||
class={cn(
|
|
||||||
"flex flex-grow flex-row items-start relative transition-colors",
|
|
||||||
{
|
|
||||||
'bg-blue-50 border-2 border-dashed border-blue-400 rounded-lg': fileUpload.isDragging && currentModelSupportsImages,
|
|
||||||
}
|
|
||||||
)}
|
|
||||||
>
|
|
||||||
<input {...fileUpload.input} bind:this={fileInput} />
|
<input {...fileUpload.input} bind:this={fileInput} />
|
||||||
<!-- TODO: Figure out better autofocus solution -->
|
<!-- TODO: Figure out better autofocus solution -->
|
||||||
<!-- svelte-ignore a11y_autofocus -->
|
<!-- svelte-ignore a11y_autofocus -->
|
||||||
|
|
@ -660,14 +671,6 @@
|
||||||
autocomplete="off"
|
autocomplete="off"
|
||||||
{@attach autosize.attachment}
|
{@attach autosize.attachment}
|
||||||
></textarea>
|
></textarea>
|
||||||
{#if fileUpload.isDragging && currentModelSupportsImages}
|
|
||||||
<div class="absolute inset-0 flex items-center justify-center bg-blue-50/90 rounded-lg">
|
|
||||||
<div class="text-blue-600 text-center">
|
|
||||||
<ImageIcon class="h-8 w-8 mx-auto mb-2" />
|
|
||||||
<p class="text-sm font-medium">Drop images here</p>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
{/if}
|
|
||||||
</div>
|
</div>
|
||||||
<div class="mt-2 -mb-px flex w-full flex-row-reverse justify-between">
|
<div class="mt-2 -mb-px flex w-full flex-row-reverse justify-between">
|
||||||
<div class="-mt-0.5 -mr-0.5 flex items-center justify-center gap-2">
|
<div class="-mt-0.5 -mr-0.5 flex items-center justify-center gap-2">
|
||||||
|
|
@ -682,7 +685,9 @@
|
||||||
{...tooltip.trigger}
|
{...tooltip.trigger}
|
||||||
>
|
>
|
||||||
{#if isUploading}
|
{#if isUploading}
|
||||||
<div class="animate-spin rounded-full h-4 w-4 border-2 border-current border-t-transparent"></div>
|
<div
|
||||||
|
class="h-4 w-4 animate-spin rounded-full border-2 border-current border-t-transparent"
|
||||||
|
></div>
|
||||||
{:else}
|
{:else}
|
||||||
<ImageIcon class="!size-4" />
|
<ImageIcon class="!size-4" />
|
||||||
{/if}
|
{/if}
|
||||||
|
|
@ -727,4 +732,14 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</Sidebar.Inset>
|
</Sidebar.Inset>
|
||||||
|
|
||||||
|
{#if fileUpload.isDragging && currentModelSupportsImages}
|
||||||
|
<div class="fixed inset-0 z-50 flex items-center justify-center bg-black/10 backdrop-blur-sm">
|
||||||
|
<div class="text-center">
|
||||||
|
<UploadIcon class="text-primary mx-auto mb-4 h-16 w-16" />
|
||||||
|
<p class="text-xl font-semibold">Add image</p>
|
||||||
|
<p class="mt-2 text-sm opacity-75">Drop an image here to attach it to your message.</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{/if}
|
||||||
</Sidebar.Root>
|
</Sidebar.Root>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue