Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
refactor(mcp-host): share the image source projection between Codex and Grok
Alfredo Lopez72cb8d828c0a
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.jsonindex 3aa545b8f..b4fc20acb 100644--- a/mcp-host/package-lock.json+++ b/mcp-host/package-lock.json@@ -1,12 +1,12 @@{"name": "clerum-mcp-host",- "version": "0.1.468",+ "version": "0.1.469","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-mcp-host",- "version": "0.1.468",+ "version": "0.1.469","license": "SEE LICENSE IN LICENSE","dependencies": {"@anthropic-ai/sdk": "^0.32.0",diff --git a/mcp-host/package.json b/mcp-host/package.jsonindex dcc62db72..c6c798718 100644--- a/mcp-host/package.json+++ b/mcp-host/package.json@@ -1,6 +1,6 @@{"name": "clerum-mcp-host",- "version": "0.1.468",+ "version": "0.1.469","description": "MCP Host - reads Host CRD and provides LLM access via OpenAI/Claude","main": "dist/main.js","engines": {diff --git a/mcp-host/src/llm/codexSubscription.ts b/mcp-host/src/llm/codexSubscription.tsindex 47359c900..5fba1c046 100644--- a/mcp-host/src/llm/codexSubscription.ts+++ b/mcp-host/src/llm/codexSubscription.ts@@ -11,7 +11,6 @@ import {ChatMessage as CoreChatMessage,FinishReason,MessageContentImageSource,- MessageContentPart,ToolCompletionResponse,ToolDefinition,textContentFromParts,@@ -23,6 +22,7 @@ import {} from './attachmentBudgetRefusal'import { CodexLlmProxyClient, CodexProxyError } from './codexLlmProxyClient'import { classifyUnknown } from './errorClassification'+import { projectImageSource } from './imageSource'import { CodexAuthorizeError, ProviderAttemptAuthorizer } from './providerAttemptAuthorizer'import { rateLimitRetryDelayMs, waitBeforeRetry } from './rateLimitRetry'import { type LlmProvider, descriptorFor } from './registryCore'@@ -162,7 +162,6 @@ export type CodexSubscriptionDeps = {* itself cannot succeed, so neither may trigger a retry or a provider fallback.*/const CODEX_REQUEST_INVALID = 'invalid_request'-const CODEX_IMAGE_SOURCE_INVALID = 'image_source_invalid'/** A local image budget refusal (`ATTACHMENT_BUDGET_REFUSALS`). */const CODEX_ATTACHMENT_TOO_LARGE = 'attachment_too_large'@@ -184,45 +183,6 @@ type ProjectedMessage = {toolCalls?: Array<{ id: string; name: string; arguments: Record<string, unknown> }>}-/**- * Provenance for one image part. The shared contract requires a source on every- * Codex V2 image and bounds its id charset; this checks only presence and shape,- * so the contract stays the single owner of format and size limits.- */-function imageSourceError(detail: string): CodexAuthorizeError {- return new CodexAuthorizeError(- CODEX_IMAGE_SOURCE_INVALID,- `image part has no usable provenance source (${detail}); host producers must attach the attachment or tool call it came from`- )-}--function projectImageSource(- part: Extract<MessageContentPart, { type: 'image' }>-): MessageContentImageSource {- const source = part.source- if (!source) throw imageSourceError('missing source')- if (source.kind === 'attachment') {- if (!source.attachmentId?.trim()) throw imageSourceError('empty attachmentId')- if (!source.messageId?.trim()) throw imageSourceError('empty messageId')- return {- kind: 'attachment',- attachmentId: source.attachmentId,- messageId: source.messageId,- }- }- if (source.kind === 'tool') {- if (!source.attachmentId?.trim()) throw imageSourceError('empty attachmentId')- if (!source.toolCallId?.trim()) throw imageSourceError('empty toolCallId')- return { kind: 'tool', attachmentId: source.attachmentId, toolCallId: source.toolCallId }- }- if (source.kind === 'gfs') {- if (!source.attachmentId?.trim()) throw imageSourceError('empty attachmentId')- if (!source.toolCallId?.trim()) throw imageSourceError('empty toolCallId')- return { kind: 'tool', attachmentId: source.attachmentId, toolCallId: source.toolCallId }- }- throw imageSourceError('unknown source kind')-}-function assertTerminalCodexOutcome(result: {text: stringtoolCalls: Array<{ id: string; name: string; arguments: Record<string, unknown> }>diff --git a/mcp-host/src/llm/grokSubscription.ts b/mcp-host/src/llm/grokSubscription.tsindex d07b82886..cb58fe9a0 100644--- a/mcp-host/src/llm/grokSubscription.ts+++ b/mcp-host/src/llm/grokSubscription.ts@@ -12,7 +12,6 @@ import {ChatMessage as CoreChatMessage,FinishReason,MessageContentImageSource,- MessageContentPart,ToolCompletionResponse,ToolDefinition,textContentFromParts,@@ -24,6 +23,7 @@ import {} from './attachmentBudgetRefusal'import { classifyUnknown } from './errorClassification'import { GrokLlmProxyClient, GrokProxyError } from './grokLlmProxyClient'+import { projectImageSource } from './imageSource'import { CodexAuthorizeError, ProviderAttemptAuthorizer } from './providerAttemptAuthorizer'import { rateLimitRetryDelayMs, waitBeforeRetry } from './rateLimitRetry'import { type LlmProvider, descriptorFor } from './registryCore'@@ -127,7 +127,6 @@ const ATTACHMENT_BUDGET_REFUSALS = buildAttachmentBudgetRefusals({* itself cannot succeed, so neither may trigger a retry or a provider fallback.*/const GROK_REQUEST_INVALID = 'invalid_request'-const GROK_IMAGE_SOURCE_INVALID = 'image_source_invalid'/** A local image budget refusal (`ATTACHMENT_BUDGET_REFUSALS`). */const GROK_ATTACHMENT_TOO_LARGE = 'attachment_too_large'@@ -149,48 +148,6 @@ type ProjectedMessage = {toolCalls?: Array<{ id: string; name: string; arguments: Record<string, unknown> }>}-/**- * Provenance for one image part. The Grok contract requires a source on every- * V2 image and bounds its id charset; this checks only presence and shape, so- * the contract stays the single owner of format and size limits. The twin of- * `projectImageSource` in `codexSubscription.ts`.- */-function imageSourceError(detail: string): CodexAuthorizeError {- return new CodexAuthorizeError(- GROK_IMAGE_SOURCE_INVALID,- `image part has no usable provenance source (${detail}); host producers must attach the attachment or tool call it came from`- )-}--function projectImageSource(- part: Extract<MessageContentPart, { type: 'image' }>-): MessageContentImageSource {- const source = part.source- if (!source) throw imageSourceError('missing source')- if (source.kind === 'attachment') {- if (!source.attachmentId?.trim()) throw imageSourceError('empty attachmentId')- if (!source.messageId?.trim()) throw imageSourceError('empty messageId')- return {- kind: 'attachment',- attachmentId: source.attachmentId,- messageId: source.messageId,- }- }- if (source.kind === 'tool') {- if (!source.attachmentId?.trim()) throw imageSourceError('empty attachmentId')- if (!source.toolCallId?.trim()) throw imageSourceError('empty toolCallId')- return { kind: 'tool', attachmentId: source.attachmentId, toolCallId: source.toolCallId }- }- // A GFS read (#670) is a tool result: the contract names it by its read's- // attachment and tool call, as Codex does.- if (source.kind === 'gfs') {- if (!source.attachmentId?.trim()) throw imageSourceError('empty attachmentId')- if (!source.toolCallId?.trim()) throw imageSourceError('empty toolCallId')- return { kind: 'tool', attachmentId: source.attachmentId, toolCallId: source.toolCallId }- }- throw imageSourceError('unknown source kind')-}-function mapGrokUsage(usage?: { inputTokens: number; outputTokens: number }): {usage: { input_tokens: number; output_tokens: number; total_tokens: number }usage_reported: booleandiff --git a/mcp-host/src/llm/imageSource.ts b/mcp-host/src/llm/imageSource.tsnew file mode 100644index 000000000..b413c1eb6--- /dev/null+++ b/mcp-host/src/llm/imageSource.ts@@ -0,0 +1,47 @@+import type { MessageContentImageSource, MessageContentPart } from '../core/types'+import { CodexAuthorizeError } from './providerAttemptAuthorizer'++/** A pre-authorize refusal: known and non-retryable in both providers. */+export const IMAGE_SOURCE_INVALID = 'image_source_invalid'++function imageSourceError(detail: string): CodexAuthorizeError {+ return new CodexAuthorizeError(+ IMAGE_SOURCE_INVALID,+ `image part has no usable provenance source (${detail}); host producers must attach the attachment or tool call it came from`+ )+}++/**+ * Provenance for one image part, shared by the Codex and Grok providers. Both+ * contracts require a source on every V2 image and bound its id charset; this+ * checks only presence and shape, so each contract stays the single owner of+ * format and size limits.+ */+export function projectImageSource(+ part: Extract<MessageContentPart, { type: 'image' }>+): MessageContentImageSource {+ const source = part.source+ if (!source) throw imageSourceError('missing source')+ if (source.kind === 'attachment') {+ if (!source.attachmentId?.trim()) throw imageSourceError('empty attachmentId')+ if (!source.messageId?.trim()) throw imageSourceError('empty messageId')+ return {+ kind: 'attachment',+ attachmentId: source.attachmentId,+ messageId: source.messageId,+ }+ }+ if (source.kind === 'tool') {+ if (!source.attachmentId?.trim()) throw imageSourceError('empty attachmentId')+ if (!source.toolCallId?.trim()) throw imageSourceError('empty toolCallId')+ return { kind: 'tool', attachmentId: source.attachmentId, toolCallId: source.toolCallId }+ }+ // A GFS read (#670) is a tool result: both contracts name it by its read's+ // attachment and tool call.+ if (source.kind === 'gfs') {+ if (!source.attachmentId?.trim()) throw imageSourceError('empty attachmentId')+ if (!source.toolCallId?.trim()) throw imageSourceError('empty toolCallId')+ return { kind: 'tool', attachmentId: source.attachmentId, toolCallId: source.toolCallId }+ }+ throw imageSourceError('unknown source kind')+}