mirror of
https://github.com/bitbaum/botsmann.git
synced 2026-12-03 20:17:09 +01:00
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>
42 lines
1.0 KiB
TypeScript
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),
|
|
};
|
|
}
|