8000 Normalize provider message content extraction by pntech20 · Pull Request #32 · Mnehmos/LLM-Chess · GitHub
[go: up one dir, main page]

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletion 8000 s src/llm/content.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export function extractMessageContent(content: unknown): string {
if (typeof content === 'string') return content;
if (Array.isArray(content)) {
return content.map(extractMessageContentPart).join('');
}
return extractMessageContentPart(content);
}

function extractMessageContentPart(part: unknown): string {
if (typeof part === 'string') return part;
if (!part || typeof part !== 'object') return '';

const record = part as Record<string, unknown>;
if (typeof record.text === 'string') return record.text;
if (typeof record.content === 'string') return record.content;
if (Array.isArray(record.content)) return record.content.map(extractMessageContentPart).join('');

return '';
}
7 changes: 4 additions & 3 deletions src/llm/ollama.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { ChatMessage } from './prompts';
import type { LLMRawResponse } from './parser';
import { buildChessPrompt, buildRetryPrompt, buildRetryPromptForReason, buildCommentaryPrompt, type CommentaryContext, type RetryReason } from './prompts';
import { buildResponseFormat, shouldStream, buildReasoningParams, getAdaptiveMoveTokenBudget } from './model-capabilities';
import { extractMessageContent } from './content';

const DEFAULT_BASE_URL = 'http://localhost:11434';

Expand Down Expand Up @@ -112,7 +113,7 @@ export class OllamaClient {
if (!choice) throw new Error('No choices in Ollama response');

return {
content: choice.message?.content || '',
content: extractMessageContent(choice.message?.content),
model: data.model || model,
responseTimeMs: Date.now() - startTime,
tokensUsed: data.usage?.total_tokens,
Expand Down Expand Up @@ -156,7 +157,7 @@ export class OllamaClient {

try {
const parsed = JSON.parse(data);
const delta = parsed.choices?.[0]?.delta?.content;
const delta = extractMessageContent(parsed.choices?.[0]?.delta?.content);
if (delta) {
if (ttftMs === undefined) ttftMs = Date.now() - startTime;
lastTokenMs = Date.now();
Expand Down Expand Up @@ -218,7 +219,7 @@ export class OllamaClient {

if (!response.ok) return '';
const data = await response.json();
return data.choices?.[0]?.message?.content || '';
return extractMessageContent(data.choices?.[0]?.message?.content);
} catch {
return '';
}
Expand Down
24 changes: 4 additions & 20 deletions src/llm/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { buildReasoningParams, buildResponseFormat, downgradeModel, getAdaptiveM
import { PermanentAPIError, RateLimitError } from './errors';
import type { LLMModel, MoveResponseOptions } from './client';
import { StreamLoopGuard } from './stream-loop-guard';
import { extractMessageContent } from './content';

const OPENAI_BASE = 'https://api.openai.com/v1';

Expand Down Expand Up @@ -220,7 +221,7 @@ export class OpenAIClient {
if (!choice) throw new Error('No choices in OpenAI response');

return {
content: extractTextContent(choice.message?.content),
content: extractMessageContent(choice.message?.content),
model: data.model || model,
responseTimeMs: Date.now() - startTime,
tokensUsed: data.usage?.total_tokens,
Expand Down Expand Up @@ -268,7 +269,7 @@ export class OpenAIClient {
try {
const parsed = JSON.parse(data);
const deltaRaw = parsed.choices?.[0]?.delta?.content;
const delta = extractTextContent(deltaRaw);
const delta = extractMessageContent(deltaRaw);
if (delta) {
if (ttftMs === undefined) ttftMs = Date.now() - startTime;
lastTokenMs = Date.now();
Expand Down Expand Up @@ -351,7 +352,7 @@ export class OpenAIClient {
return '';
}
const data = await response.json();
return extractTextContent(data.choices?.[0]?.message?.content);
return extractMessageContent(data.choices?.[0]?.message?.content);
}

async requestCommentaryStream(
Expand Down Expand Up @@ -433,23 +434,6 @@ export class OpenAIClient {
}
}

function extractTextContent(content: unknown): string {
if (typeof content === 'string') return content;
if (Array.isArray(content)) {
return content
.map((part) => {
if (typeof part === 'string') return part;
if (!part || typeof part !== 'object') return '';
if ('text' in part && typeof (part as { text?: unknown }).text === 'string') {
return (part as { text: string }).text;
}
return '';
})
.join('');
}
return '';
}

function mapReasoningEffort(rawEffort: string): 'minimal' | 'low' | 'medium' | 'high' | undefined {
switch (rawEffort.toLowerCase()) {
case 'none':
Expand Down
9 changes: 5 additions & 4 deletions src/llm/openrouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { buildChessPrompt, buildRetryPrompt, buildRetryPromptForReason, buildCom
import { buildResponseFormat, shouldStream, downgradeModel, buildReasoningParams, getAdaptiveMoveTokenBudget } from './model-capabilities';
import { PermanentAPIError, RateLimitError } from './errors';
import { StreamLoopGuard } from './stream-loop-guard';
import { extractMessageContent } from './content';

const OPENROUTER_BASE = 'https://openrouter.ai/api/v1';
const MODEL_RATE_LIMIT_UNTIL = new Map<string, number>();
Expand Down Expand Up @@ -239,7 +240,7 @@ export class OpenRouterClient {
if (!choice) throw new Error('No choices in OpenRouter response');

return {
content: choice.message?.content || '',
content: extractMessageContent(choice.message?.content),
model: data.model || model,
responseTimeMs: Date.now() - startTime,
tokensUsed: data.usage?.total_tokens,
Expand Down Expand Up @@ -288,7 +289,7 @@ export class OpenRouterClient {
try {
const parsed = JSON.parse(data);
const choice = parsed.choices?.[0];
const deltaContent = choice?.delta?.content;
const deltaContent = extractMessageContent(choice?.delta?.content);
const deltaReasoning = choice?.delta?.reasoning;
if (deltaReasoning) {
// Thinking tokens — pass as-is so callers can show reasoning if desired
Expand Down Expand Up @@ -379,7 +380,7 @@ export class OpenRouterClient {

if (!response.ok) return '';
const data = await response.json();
return data.choices?.[0]?.message?.content || '';
return extractMessageContent(data.choices?.[0]?.message?.content);
}

async requestCommentaryStream(
Expand Down Expand Up @@ -436,7 +437,7 @@ export class OpenRouterClient {
});
if (retryResp.ok) {
const retryData = await retryResp.json();
const retryContent = retryData.choices?.[0]?.message?.content;
const retryContent = extractMessageContent(retryData.choices?.[0]?.message?.content);
if (retryContent) {
onChunk(retryContent);
return retryContent;
Expand Down
0