Files
g-butandClaude Opus 4.6 372fe46261 refactor(chat): extract shared utilities from 3 chat API routes
Create lib/chat/ module with shared utilities:
- embedding.ts: generateEmbeddingWithTimeout (embedding + timeout guard)
- settings.ts: getUserLLMSettings (provider/apiKey/ollamaUrl lookup)
- context.ts: truncateChunks + joinContext (context building)
- llm-errors.ts: handleLLMError (API key fallback, Ollama, AI unavailable)
- parse.ts: parseResponseWithSuggestions (extract >>> suggestions)

Update all 3 chat routes to use shared utilities, removing ~220 lines of
duplicated code. Also fix module-scope getServerEnv() call in llm-client
that caused SSG build failure.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-02 17:23:14 +01:00

42 lines
1.0 KiB
TypeScript

/**
* Parse LLM response to extract suggestions
* @module lib/chat/parse
*
* Lines prefixed with ">>>" are treated as follow-up suggestions.
*/
/**
* Parse an LLM response to separate main content from follow-up suggestions.
* Suggestions are lines starting with ">>>".
*/
export function parseResponseWithSuggestions(rawContent: string): {
content: string;
suggestions: string[];
} {
const lines = rawContent.split('\n');
const suggestions: string[] = [];
const contentLines: string[] = [];
for (const line of lines) {
const trimmed = line.trim();
if (trimmed.startsWith('>>>')) {
const suggestion = trimmed.slice(3).trim();
if (suggestion && suggestion.length > 3) {
suggestions.push(suggestion);
}
} else {
contentLines.push(line);
}
}
// Clean up trailing empty lines
while (contentLines.length > 0 && contentLines[contentLines.length - 1].trim() === '') {
contentLines.pop();
}
return {
content: contentLines.join('\n').trim(),
suggestions: suggestions.slice(0, 3),
};
}