feat: implement English Style Converter

- SvelteKit project scaffolded with TypeScript
- Type definitions for Style, StyleCategory, ConversionRequest, ConversionResponse, LLMConfig
- Style definitions with 6 categories and 25 sub-styles
- Intensity mapping (1-5) with prompt modifier placeholders
- LLM client using OpenAI-compatible API (Ollama default)
- POST /api/convert endpoint with input validation
- Animated loading modal with per-letter animations
- Main page UI with category/style selectors, intensity slider
- Copy to clipboard, collapsible prompt display
- Vitest tests for styles, LLM prompt building, and API validation
- Environment configuration for LLM settings
This commit is contained in:
2026-04-12 21:53:27 -04:00
parent fcf80638e1
commit a12afb792e
16 changed files with 1464 additions and 37 deletions

View File

@@ -1 +0,0 @@
<svg xmlns="http://www.w3.org/2000/svg" width="107" height="128" viewBox="0 0 107 128"><title>svelte-logo</title><path d="M94.157 22.819c-10.4-14.885-30.94-19.297-45.792-9.835L22.282 29.608A29.92 29.92 0 0 0 8.764 49.65a31.5 31.5 0 0 0 3.108 20.231 30 30 0 0 0-4.477 11.183 31.9 31.9 0 0 0 5.448 24.116c10.402 14.887 30.942 19.297 45.791 9.835l26.083-16.624A29.92 29.92 0 0 0 98.235 78.35a31.53 31.53 0 0 0-3.105-20.232 30 30 0 0 0 4.474-11.182 31.88 31.88 0 0 0-5.447-24.116" style="fill:#ff3e00"/><path d="M45.817 106.582a20.72 20.72 0 0 1-22.237-8.243 19.17 19.17 0 0 1-3.277-14.503 18 18 0 0 1 .624-2.435l.49-1.498 1.337.981a33.6 33.6 0 0 0 10.203 5.098l.97.294-.09.968a5.85 5.85 0 0 0 1.052 3.878 6.24 6.24 0 0 0 6.695 2.485 5.8 5.8 0 0 0 1.603-.704L69.27 76.28a5.43 5.43 0 0 0 2.45-3.631 5.8 5.8 0 0 0-.987-4.371 6.24 6.24 0 0 0-6.698-2.487 5.7 5.7 0 0 0-1.6.704l-9.953 6.345a19 19 0 0 1-5.296 2.326 20.72 20.72 0 0 1-22.237-8.243 19.17 19.17 0 0 1-3.277-14.502 17.99 17.99 0 0 1 8.13-12.052l26.081-16.623a19 19 0 0 1 5.3-2.329 20.72 20.72 0 0 1 22.237 8.243 19.17 19.17 0 0 1 3.277 14.503 18 18 0 0 1-.624 2.435l-.49 1.498-1.337-.98a33.6 33.6 0 0 0-10.203-5.1l-.97-.294.09-.968a5.86 5.86 0 0 0-1.052-3.878 6.24 6.24 0 0 0-6.696-2.485 5.8 5.8 0 0 0-1.602.704L37.73 51.72a5.42 5.42 0 0 0-2.449 3.63 5.79 5.79 0 0 0 .986 4.372 6.24 6.24 0 0 0 6.698 2.486 5.8 5.8 0 0 0 1.602-.704l9.952-6.342a19 19 0 0 1 5.295-2.328 20.72 20.72 0 0 1 22.237 8.242 19.17 19.17 0 0 1 3.277 14.503 18 18 0 0 1-8.13 12.053l-26.081 16.622a19 19 0 0 1-5.3 2.328" style="fill:#fff"/></svg>

Before

Width:  |  Height:  |  Size: 1.5 KiB

View File

@@ -0,0 +1,256 @@
<script lang="ts">
const loadingWords = [
'Bamboozling',
'Razzmatazzing',
'Transmogrifying',
'Alakazamming',
'Prestidigitating',
'Metamorphosizing',
'Enchanting',
'Voodooing',
'Witchcrafting',
'Sorcerizing',
'Spellcasting',
'Hocus-pocusing',
'Incantating',
'Conjurating',
'Charmweaving'
];
const colors = [
'coral',
'teal',
'violet',
'amber',
'emerald',
'rose',
'skyblue',
'fuchsia',
'orange',
'indigo'
];
const colorValues: Record<string, string> = {
coral: '#FF6B6B',
teal: '#2EC4B6',
violet: '#9B5DE5',
amber: '#F5B041',
emerald: '#2ECC71',
rose: '#E74C6F',
skyblue: '#5DADE2',
fuchsia: '#D63384',
orange: '#F39C12',
indigo: '#5B2C6F'
};
const animationStyles = [
'slide-up',
'bounce-in',
'drop-in',
'scale-up',
'fade-rotate',
'spring-left'
];
let currentWordIndex = $state(0);
let currentColor = $state(colors[0]);
let currentAnimation = $state('slide-up');
let letters = $state<string[]>([]);
$effect(() => {
const interval = setInterval(() => {
currentWordIndex = Math.floor(Math.random() * loadingWords.length);
currentColor = colors[Math.floor(Math.random() * colors.length)];
currentAnimation = animationStyles[Math.floor(Math.random() * animationStyles.length)];
letters = loadingWords[currentWordIndex].split('');
}, 2000);
// Initialize immediately
currentWordIndex = Math.floor(Math.random() * loadingWords.length);
currentColor = colors[Math.floor(Math.random() * colors.length)];
currentAnimation = animationStyles[Math.floor(Math.random() * animationStyles.length)];
letters = loadingWords[currentWordIndex].split('');
return () => clearInterval(interval);
});
</script>
<div class="modal-overlay" role="dialog" aria-label="Loading">
<div class="modal-content">
<div class="loading-letters" style="color: {colorValues[currentColor]}">
{#each letters as letter, i}
<span
class="letter {currentAnimation}"
style="--delay: {i * 60}ms; --color: {colorValues[currentColor]}"
>
{letter}
</span>
{/each}
</div>
<p class="loading-subtitle">Transforming your text...</p>
</div>
</div>
<style>
.modal-overlay {
position: fixed;
inset: 0;
background: rgba(255, 255, 255, 0.85);
backdrop-filter: blur(8px);
display: flex;
align-items: center;
justify-content: center;
z-index: 1000;
animation: fade-in 0.2s ease-out;
}
.modal-content {
text-align: center;
padding: 3rem;
}
.loading-letters {
font-size: clamp(2rem, 5vw, 3.5rem);
font-weight: 700;
letter-spacing: 0.05em;
min-height: 4rem;
display: flex;
justify-content: center;
flex-wrap: wrap;
}
.letter {
display: inline-block;
opacity: 0;
animation-fill-mode: forwards;
animation-duration: 0.4s;
animation-delay: var(--delay);
}
/* Slide up */
.slide-up {
animation-name: slide-up;
}
@keyframes slide-up {
from {
opacity: 0;
transform: translateY(30px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
/* Bounce in */
.bounce-in {
animation-name: bounce-in;
}
@keyframes bounce-in {
0% {
opacity: 0;
transform: translateY(-40px);
}
60% {
opacity: 1;
transform: translateY(8px);
}
100% {
opacity: 1;
transform: translateY(0);
}
}
/* Drop in */
.drop-in {
animation-name: drop-in;
}
@keyframes drop-in {
0% {
opacity: 0;
transform: translateY(-60px);
}
70% {
opacity: 1;
transform: translateY(4px);
}
85% {
transform: translateY(-2px);
}
100% {
opacity: 1;
transform: translateY(0);
}
}
/* Scale up */
.scale-up {
animation-name: scale-up;
}
@keyframes scale-up {
from {
opacity: 0;
transform: scale(0.3);
}
to {
opacity: 1;
transform: scale(1);
}
}
/* Fade rotate */
.fade-rotate {
animation-name: fade-rotate;
}
@keyframes fade-rotate {
from {
opacity: 0;
transform: rotate(-15deg) scale(0.5);
}
to {
opacity: 1;
transform: rotate(0deg) scale(1);
}
}
/* Spring from left */
.spring-left {
animation-name: spring-left;
}
@keyframes spring-left {
0% {
opacity: 0;
transform: translateX(-40px);
}
70% {
opacity: 1;
transform: translateX(5px);
}
100% {
opacity: 1;
transform: translateX(0);
}
}
.loading-subtitle {
margin-top: 1rem;
color: #6b7280;
font-size: 1rem;
font-weight: 400;
}
@keyframes fade-in {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
</style>

34
src/lib/llm.test.ts Normal file
View File

@@ -0,0 +1,34 @@
import { describe, it, expect } from 'vitest';
import { buildSystemPrompt, buildUserMessage } from '$lib/llm';
describe('buildSystemPrompt', () => {
it('fills in {style} placeholder from intensity instruction', () => {
const result = buildSystemPrompt(
'Rewrite in a sarcastic, snarky tone with biting wit',
'rewrite strongly in a {style} style'
);
expect(result).toContain('rewrite strongly in a Rewrite in a sarcastic, snarky tone with biting wit style');
expect(result).not.toContain('{style}');
});
it('includes the style modifier on its own line', () => {
const result = buildSystemPrompt('some modifier', 'lightly hint at a {style} tone');
expect(result).toContain('some modifier');
});
it('includes the core instruction text', () => {
const result = buildSystemPrompt('test', 'rewrite with a {style} tone');
expect(result).toContain('You are an expert English style converter');
expect(result).toContain('Output ONLY the converted text');
});
});
describe('buildUserMessage', () => {
it('returns the text as-is', () => {
expect(buildUserMessage('Hello world')).toBe('Hello world');
});
it('preserves whitespace', () => {
expect(buildUserMessage(' spaced ')).toBe(' spaced ');
});
});

77
src/lib/llm.ts Normal file
View File

@@ -0,0 +1,77 @@
import { env } from '$env/dynamic/private';
import type { LLMConfig } from './types';
const DEFAULT_CONFIG: LLMConfig = {
baseUrl: 'http://localhost:11434/v1',
apiKey: 'ollama',
model: 'llama3'
};
function getConfig(): LLMConfig {
return {
baseUrl: env.OPENAI_BASE_URL || DEFAULT_CONFIG.baseUrl,
apiKey: env.OPENAI_API_KEY || DEFAULT_CONFIG.apiKey,
model: env.OPENAI_MODEL || DEFAULT_CONFIG.model
};
}
export interface ConvertResult {
converted: string;
systemPrompt: string;
userMessage: string;
}
export function buildSystemPrompt(styleModifier: string, intensityInstruction: string): string {
const intensityFilled = intensityInstruction.replace('{style}', styleModifier);
return `You are an expert English style converter.
${intensityFilled}.
${styleModifier}
Preserve the core meaning but fully transform the voice and tone.
Output ONLY the converted text — no explanations, no labels, no quotes.`;
}
export function buildUserMessage(text: string): string {
return text;
}
export async function convertText(
text: string,
styleModifier: string,
intensityInstruction: string,
overrides?: Partial<LLMConfig>
): Promise<ConvertResult> {
const merged: LLMConfig = { ...DEFAULT_CONFIG, ...getConfig(), ...overrides };
const systemPrompt = buildSystemPrompt(styleModifier, intensityInstruction);
const userMessage = buildUserMessage(text);
const response = await fetch(`${merged.baseUrl}/chat/completions`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${merged.apiKey}`
},
body: JSON.stringify({
model: merged.model,
messages: [
{ role: 'system', content: systemPrompt },
{ role: 'user', content: userMessage }
],
temperature: 0.8
})
});
if (!response.ok) {
const errorText = await response.text().catch(() => 'Unknown error');
throw new Error(`LLM request failed (${response.status}): ${errorText}`);
}
const data = await response.json();
const converted = data.choices?.[0]?.message?.content?.trim();
if (!converted) {
throw new Error('LLM returned empty response');
}
return { converted, systemPrompt, userMessage };
}

112
src/lib/styles.test.ts Normal file
View File

@@ -0,0 +1,112 @@
import { describe, it, expect } from 'vitest';
import {
styles,
categories,
intensityMap,
getStylesByCategory,
getStyleById,
getCategoryById,
getIntensityConfig
} from '$lib/styles';
describe('styles', () => {
it('all styles have valid ids', () => {
for (const style of styles) {
expect(style.id).toBeTruthy();
expect(typeof style.id).toBe('string');
}
});
it('all styles have valid promptModifiers', () => {
for (const style of styles) {
expect(style.promptModifier).toBeTruthy();
expect(typeof style.promptModifier).toBe('string');
expect(style.promptModifier.length).toBeGreaterThan(5);
}
});
it('all styles reference existing categories', () => {
const categoryIds = new Set(categories.map((c) => c.id));
for (const style of styles) {
expect(categoryIds.has(style.categoryId)).toBe(true);
}
});
it('all style ids are unique', () => {
const ids = styles.map((s) => s.id);
const uniqueIds = new Set(ids);
expect(ids.length).toBe(uniqueIds.size);
});
});
describe('getStylesByCategory', () => {
it('returns styles for a known category', () => {
const result = getStylesByCategory('general');
expect(result.length).toBeGreaterThan(0);
for (const style of result) {
expect(style.categoryId).toBe('general');
}
});
it('returns empty array for unknown category', () => {
const result = getStylesByCategory('nonexistent');
expect(result).toEqual([]);
});
it('general category has 6 styles', () => {
const result = getStylesByCategory('general');
expect(result.length).toBe(6);
});
});
describe('getStyleById', () => {
it('returns a style for a valid id', () => {
const result = getStyleById('sarcastic');
expect(result).toBeDefined();
expect(result!.id).toBe('sarcastic');
});
it('returns undefined for unknown id', () => {
const result = getStyleById('nonexistent');
expect(result).toBeUndefined();
});
});
describe('getCategoryById', () => {
it('returns a category for a valid id', () => {
const result = getCategoryById('general');
expect(result).toBeDefined();
expect(result!.id).toBe('general');
});
it('returns undefined for unknown id', () => {
const result = getCategoryById('nonexistent');
expect(result).toBeUndefined();
});
});
describe('getIntensityConfig', () => {
it('returns config for valid intensity levels 1-5', () => {
for (let i = 1; i <= 5; i++) {
const config = getIntensityConfig(i);
expect(config).toBeDefined();
expect(config!.label).toBeTruthy();
expect(config!.instruction).toContain('{style}');
}
});
it('returns undefined for intensity 0', () => {
expect(getIntensityConfig(0)).toBeUndefined();
});
it('returns undefined for intensity 6', () => {
expect(getIntensityConfig(6)).toBeUndefined();
});
it('all intensity instructions contain {style} placeholder', () => {
for (let i = 1; i <= 5; i++) {
const config = getIntensityConfig(i);
expect(config!.instruction).toContain('{style}');
}
});
});

View File

@@ -40,7 +40,7 @@ export const styles: Style[] = [
{ id: 'gen-z', label: 'Gen Z Slang', categoryId: 'fun', promptModifier: 'Rewrite in Gen Z slang with no cap, fr, and modern internet vernacular' },
// Game of Thrones
{ id: 'got-kings-landing', label: "King's Landing", categoryId: 'got', promptModifier: 'Rewrite as a scheming noble from King'\''s Landing would speak' },
{ id: 'got-kings-landing', label: "King's Landing", categoryId: 'got', promptModifier: 'Rewrite as a scheming noble from Kings Landing would speak' },
{ id: 'got-wildlings', label: 'Wildlings', categoryId: 'got', promptModifier: 'Rewrite in the rough, free-spirited manner of the Free Folk wildlings' },
{ id: 'got-winterfell', label: 'Winterfell', categoryId: 'got', promptModifier: 'Rewrite with the honor-bound stoicism of a Stark from Winterfell' },