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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
# Server Utilities Reference

This document describes all utility modules available in `apps/server/src/lib/`. These utilities provide reusable functionality for image handling, prompt building, model resolution, conversation management, and error handling.

---

## Table of Contents

1. [Image Handler](#image-handler)
2. [Prompt Builder](#prompt-builder)
3. [Model Resolver](#model-resolver)
4. [Conversation Utils](#conversation-utils)
5. [Error Handler](#error-handler)
6. [Subprocess Manager](#subprocess-manager)
7. [Events](#events)
8. [Auth](#auth)
9. [Security](#security)

---

## Image Handler

**Location**: `apps/server/src/lib/image-handler.ts`

Centralized utilities for processing image files, including MIME type detection, base64 encoding, and content block generation for Claude SDK format.

### Functions

#### `getMimeTypeForImage(imagePath: string): string`

Get MIME type for an image file based on its extension.

**Supported formats**:

- `.jpg`, `.jpeg` β†’ `image/jpeg`
- `.png` β†’ `image/png`
- `.gif` β†’ `image/gif`
- `.webp` β†’ `image/webp`
- Default: `image/png`

**Example**:

```typescript
import { getMimeTypeForImage } from '../lib/image-handler.js';

const mimeType = getMimeTypeForImage('/path/to/image.jpg');
// Returns: "image/jpeg"
```

---

#### `readImageAsBase64(imagePath: string): Promise<ImageData>`

Read an image file and convert to base64 with metadata.

**Returns**: `ImageData`

```typescript
interface ImageData {
  base64: string; // Base64-encoded image data
  mimeType: string; // MIME type
  filename: string; // File basename
  originalPath: string; // Original file path
}
```

**Example**:

```typescript
const imageData = await readImageAsBase64('/path/to/photo.png');
console.log(imageData.base64); // "iVBORw0KG..."
console.log(imageData.mimeType); // "image/png"
console.log(imageData.filename); // "photo.png"
```

---

#### `convertImagesToContentBlocks(imagePaths: string[], workDir?: string): Promise<ImageContentBlock[]>`

Convert image paths to content blocks in Claude SDK format. Handles both relative and absolute paths.

**Parameters**:

- `imagePaths` - Array of image file paths
- `workDir` - Optional working directory for resolving relative paths

**Returns**: Array of `ImageContentBlock`

```typescript
interface ImageContentBlock {
  type: 'image';
  source: {
    type: 'base64';
    media_type: string;
    data: string;
  };
}
```

**Example**:

```typescript
const imageBlocks = await convertImagesToContentBlocks(
  ['./screenshot.png', '/absolute/path/diagram.jpg'],
  '/project/root'
);

// Use in prompt content
const contentBlocks = [{ type: 'text', text: 'Analyze these images:' }, ...imageBlocks];
```

---

#### `formatImagePathsForPrompt(imagePaths: string[]): string`

Format image paths as a bulleted list for inclusion in text prompts.

**Returns**: Formatted string with image paths, or empty string if no images.

**Example**:

```typescript
const pathsList = formatImagePathsForPrompt([
  '/screenshots/login.png',
  '/diagrams/architecture.png',
]);

// Returns:
// "\n\nAttached images:\n- /screenshots/login.png\n- /diagrams/architecture.png\n"
```

---

## Prompt Builder

**Location**: `apps/server/src/lib/prompt-builder.ts`

Standardized prompt building that combines text prompts with image attachments.

### Functions

#### `buildPromptWithImages(basePrompt: string, imagePaths?: string[], workDir?: string, includeImagePaths: boolean = false): Promise<PromptWithImages>`

Build a prompt with optional image attachments.

**Parameters**:

- `basePrompt` - The text prompt
- `imagePaths` - Optional array of image file paths
- `workDir` - Optional working directory for resolving relative paths
- `includeImagePaths` - Whether to append image paths to the text (default: false)

**Returns**: `PromptWithImages`

```typescript
interface PromptWithImages {
  content: PromptContent; // string | Array<ContentBlock>
  hasImages: boolean;
}

type PromptContent =
  | string
  | Array<{
      type: string;
      text?: string;
      source?: object;
    }>;
```

**Example**:

```typescript
import { buildPromptWithImages } from '../lib/prompt-builder.js';

// Without images
const { content } = await buildPromptWithImages('What is 2+2?');
// content: "What is 2+2?" (simple string)

// With images
const { content, hasImages } = await buildPromptWithImages(
  'Analyze this screenshot',
  ['/path/to/screenshot.png'],
  '/project/root',
  true // include image paths in text
);
// content: [
//   { type: "text", text: "Analyze this screenshot\n\nAttached images:\n- /path/to/screenshot.png\n" },
//   { type: "image", source: { type: "base64", media_type: "image/png", data: "..." } }
// ]
// hasImages: true
```

**Use Cases**:

- **AgentService**: Set `includeImagePaths: true` to list paths for Read tool access
- **AutoModeService**: Set `includeImagePaths: false` to avoid duplication in feature descriptions

---

## Model Resolver

**Location**: `apps/server/src/lib/model-resolver.ts`

Centralized model string mapping and resolution for handling model aliases and provider detection.

### Constants

#### `CLAUDE_MODEL_MAP`

Model alias mapping for Claude models.

```typescript
export const CLAUDE_MODEL_MAP: Record<string, string> = {
  haiku: 'claude-haiku-4-5',
  sonnet: 'claude-sonnet-4-20250514',
  opus: 'claude-opus-4-6',
} as const;
```

#### `DEFAULT_MODELS`

Default models per provider.

```typescript
export const DEFAULT_MODELS = {
  claude: 'claude-opus-4-6',
  openai: 'gpt-5.2',
} as const;
```

### Functions

#### `resolveModelString(modelKey?: string, defaultModel: string = DEFAULT_MODELS.claude): string`

Resolve a model key/alias to a full model string.

**Logic**:

1. If `modelKey` is undefined β†’ return `defaultModel`
2. If starts with `"gpt-"` or `"o"` β†’ pass through (OpenAI/Codex model)
3. If includes `"claude-"` β†’ pass through (full Claude model string)
4. If in `CLAUDE_MODEL_MAP` β†’ return mapped value
5. Otherwise β†’ return `defaultModel` with warning

**Example**:

```typescript
import { resolveModelString, DEFAULT_MODELS } from '../lib/model-resolver.js';

resolveModelString('opus');
// Returns: "claude-opus-4-6"
// Logs: "[ModelResolver] Resolved model alias: "opus" -> "claude-opus-4-6""

resolveModelString('gpt-5.2');
// Returns: "gpt-5.2"
// Logs: "[ModelResolver] Using OpenAI/Codex model: gpt-5.2"

resolveModelString('claude-sonnet-4-20250514');
// Returns: "claude-sonnet-4-20250514"
// Logs: "[ModelResolver] Using full Claude model string: claude-sonnet-4-20250514"

resolveModelString('invalid-model');
// Returns: "claude-opus-4-6"
// Logs: "[ModelResolver] Unknown model key "invalid-model", using default: "claude-opus-4-6""
```

---

#### `getEffectiveModel(explicitModel?: string, sessionModel?: string, defaultModel?: string): string`

Get the effective model from multiple sources with priority.

**Priority**: explicit model > session model > default model

**Example**:

```typescript
import { getEffectiveModel } from '../lib/model-resolver.js';

// Explicit model takes precedence
getEffectiveModel('sonnet', 'opus');
// Returns: "claude-sonnet-4-20250514"

// Falls back to session model
getEffectiveModel(undefined, 'haiku');
// Returns: "claude-haiku-4-5"

// Falls back to default
getEffectiveModel(undefined, undefined, 'gpt-5.2');
// Returns: "gpt-5.2"
```

---

## Conversation Utils

**Location**: `apps/server/src/lib/conversation-utils.ts`

Standardized conversation history processing for both SDK-based and CLI-based providers.

### Types

```typescript
import type { ConversationMessage } from '../providers/types.js';

interface ConversationMessage {
  role: 'user' | 'assistant';
  content: string | Array<{ type: string; text?: string; source?: object }>;
}
```

### Functions

#### `extractTextFromContent(content: string | Array<ContentBlock>): string`

Extract plain text from message content (handles both string and array formats).

**Example**:

```typescript
import { extractTextFromContent } from "../lib/conversation-utils.js";

// String content
extractTextFromContent("Hello world");
// Returns: "Hello world"

// Array content
extractTextFromContent([
  { type: "text", text: "Hello" },
  { type: "image", source: {...} },
  { type: "text", text: "world" }
]);
// Returns: "Hello\nworld"
```

---

#### `normalizeContentBlocks(content: string | Array<ContentBlock>): Array<ContentBlock>`

Normalize message content to array format.

**Example**:

```typescript
// String β†’ array
normalizeContentBlocks('Hello');
// Returns: [{ type: "text", text: "Hello" }]

// Array β†’ pass through
normalizeContentBlocks([{ type: 'text', text: 'Hello' }]);
// Returns: [{ type: "text", text: "Hello" }]
```

---

#### `formatHistoryAsText(history: ConversationMessage[]): string`

Format conversation history as plain text for CLI-based providers (e.g., Codex).

**Returns**: Formatted text with role labels, or empty string if no history.

**Example**:

```typescript
const history = [
  { role: 'user', content: 'What is 2+2?' },
  { role: 'assistant', content: '2+2 equals 4.' },
];

const formatted = formatHistoryAsText(history);
// Returns:
// "Previous conversation:
//
// User: What is 2+2?
//
// Assistant: 2+2 equals 4.
//
// ---
//
// "
```

---

#### `convertHistoryToMessages(history: ConversationMessage[]): Array<SDKMessage>`

Convert conversation history to Claude SDK message format.

**Returns**: Array of SDK-formatted messages ready to yield in async generator.

**Example**:

```typescript
const history = [
  { role: 'user', content: 'Hello' },
  { role: 'assistant', content: 'Hi there!' },
];

const messages = convertHistoryToMessages(history);
// Returns:
// [
//   {
//     type: "user",
//     session_id: "",
//     message: {
//       role: "user",
//       content: [{ type: "text", text: "Hello" }]
//     },
//     parent_tool_use_id: null
//   },
//   {
//     type: "assistant",
//     session_id: "",
//     message: {
//       role: "assistant",
//       content: [{ type: "text", text: "Hi there!" }]
//     },
//     parent_tool_use_id: null
//   }
// ]
```

---

## Error Handler

**Location**: `apps/server/src/lib/error-handler.ts`

Standardized error classification and handling utilities.

### Types

```typescript
export type ErrorType = 'authentication' | 'abort' | 'execution' | 'unknown';

export interface ErrorInfo {
  type: ErrorType;
  message: string;
  isAbort: boolean;
  isAuth: boolean;
  originalError: unknown;
}
```

### Functions

#### `isAbortError(error: unknown): boolean`

Check if an error is an abort/cancellation error.

**Example**:

```typescript
import { isAbortError } from '../lib/error-handler.js';

try {
  // ... operation
} catch (error) {
  if (isAbortError(error)) {
    console.log('Operation was cancelled');
    return { success: false, aborted: true };
  }
}
```

---

#### `isAuthenticationError(errorMessage: string): boolean`

Check if an error is an authentication/API key error.

**Detects**:

- "Authentication failed"
- "Invalid API key"
- "authentication_failed"
- "Fix external API key"

**Example**:

```typescript
if (isAuthenticationError(error.message)) {
  console.error('Please check your API key configuration');
}
```

---

#### `classifyError(error: unknown): ErrorInfo`

Classify an error into a specific type.

**Example**:

```typescript
import { classifyError } from '../lib/error-handler.js';

try {
  // ... operation
} catch (error) {
  const errorInfo = classifyError(error);

  switch (errorInfo.type) {
    case 'authentication':
      // Handle auth errors
      break;
    case 'abort':
      // Handle cancellation
      break;
    case 'execution':
      // Handle other errors
      break;
  }
}
```

---

#### `getUserFriendlyErrorMessage(error: unknown): string`

Get a user-friendly error message.

**Example**:

```typescript
try {
  // ... operation
} catch (error) {
  const friendlyMessage = getUserFriendlyErrorMessage(error);
  // "Operation was cancelled" for abort errors
  // "Authentication failed. Please check your API key." for auth errors
  // Original error message for other errors
}
```

---

## Subprocess Manager

**Location**: `apps/server/src/lib/subprocess-manager.ts`

Utilities for spawning CLI processes and parsing JSONL streams (used by Codex provider).

### Types

```typescript
export interface SubprocessOptions {
  command: string;
  args: string[];
  cwd: string;
  env?: Record<string, string>;
  abortController?: AbortController;
  timeout?: number; // Milliseconds of no output before timeout
}

export interface SubprocessResult {
  stdout: string;
  stderr: string;
  exitCode: number | null;
}
```

### Functions

#### `async function* spawnJSONLProcess(options: SubprocessOptions): AsyncGenerator<unknown>`

Spawns a subprocess and streams JSONL output line-by-line.

**Features**:

- Parses each line as JSON
- Handles abort signals
- 30-second timeout detection for hanging processes
- Collects stderr for error reporting
- Continues processing other lines if one fails to parse

**Example**:

```typescript
import { spawnJSONLProcess } from '../lib/subprocess-manager.js';

const stream = spawnJSONLProcess({
  command: 'codex',
  args: ['exec', '--model', 'gpt-5.2', '--json', '--full-auto', 'Fix the bug'],
  cwd: '/project/path',
  env: { OPENAI_API_KEY: 'sk-...' },
  abortController: new AbortController(),
  timeout: 30000,
});

for await (const event of stream) {
  console.log('Received event:', event);
  // Process JSONL events
}
```

---

#### `async function spawnProcess(options: SubprocessOptions): Promise<SubprocessResult>`

Spawns a subprocess and collects all output.

**Example**:

```typescript
const result = await spawnProcess({
  command: 'git',
  args: ['status'],
  cwd: '/project/path',
});

console.log(result.stdout); // Git status output
console.log(result.exitCode); // 0 for success
```

---

## Events

**Location**: `apps/server/src/lib/events.ts`

Event emitter system for WebSocket communication.

**Documented separately** - see existing codebase for event types and usage.

---

## Auth

**Location**: `apps/server/src/lib/auth.ts`

Authentication utilities for API endpoints.

**Documented separately** - see existing codebase for authentication flow.

---

## Security

**Location**: `apps/server/src/lib/security.ts`

Security utilities for input validation and sanitization.

**Documented separately** - see existing codebase for security patterns.

---

## Best Practices

### When to Use Which Utility

1. **Image handling** β†’ Always use `image-handler.ts` utilities
   - βœ… Do: `convertImagesToContentBlocks(imagePaths, workDir)`
   - ❌ Don't: Manually read files and encode base64

2. **Prompt building** β†’ Use `prompt-builder.ts` for consistency
   - βœ… Do: `buildPromptWithImages(text, images, workDir, includePathsInText)`
   - ❌ Don't: Manually construct content block arrays

3. **Model resolution** β†’ Use `model-resolver.ts` for all model handling
   - βœ… Do: `resolveModelString(feature.model, DEFAULT_MODELS.claude)`
   - ❌ Don't: Inline model mapping logic

4. **Error handling** β†’ Use `error-handler.ts` for classification
   - βœ… Do: `if (isAbortError(error)) { ... }`
   - ❌ Don't: `if (error instanceof AbortError || error.name === "AbortError") { ... }`

### Importing Utilities

Always use `.js` extension in imports for ESM compatibility:

```typescript
// βœ… Correct
import { buildPromptWithImages } from '../lib/prompt-builder.js';

// ❌ Incorrect
import { buildPromptWithImages } from '../lib/prompt-builder';
```

---

## Testing Utilities

When writing tests for utilities:

1. **Unit tests** - Test each function in isolation
2. **Integration tests** - Test utilities working together
3. **Mock external dependencies** - File system, child processes

Example:

```typescript
describe('image-handler', () => {
  it('should detect MIME type correctly', () => {
    expect(getMimeTypeForImage('photo.jpg')).toBe('image/jpeg');
    expect(getMimeTypeForImage('diagram.png')).toBe('image/png');
  });
});
```