refactor(ai): split Message by role (#61195)

This commit is contained in:
Aiden Cline 2026-11-03 10:32:45 -06:00 • committed by GitHub
parent ccbcd96082
commit 0988cb8366
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
19 changed files with 194 additions and 161 deletions

View file

@ -11,7 +11,14 @@
// Manual `cache: CacheHint` placements on individual parts are preserved and
// count against the four-breakpoint budget; auto only fills remaining slots.
import { CacheHint, type CachePolicy, type CachePolicyObject, type CacheTTL } from "./schema/options.js"
import { type LLMRequest, Message, ToolDefinition, type ContentPart, type ToolEntry } from "./schema/messages.js"
import {
type LLMRequest,
Message,
SystemMessage,
ToolDefinition,
type ContentPart,
type ToolEntry,
} from "./schema/messages.js"
import { effortUpdate } from "./effort-updates.js"
const AUTO: CachePolicyObject = {
@ -141,8 +148,18 @@ const markMessageAt = (
const existing = target.content[markAt]
if (!existing || !isCacheablePart(existing) || existing.cache || budget.remaining === 0) return messages
budget.remaining -= 1
const nextContent = target.content.map((part, i) => (i === markAt ? { ...existing, cache: hint } : part))
const next = new Message({ ...target, content: nextContent })
const next =
target.role === "system"
? new SystemMessage({
...target,
content: target.content.map((part, i) =>
i === markAt && part.type === "text" ? { ...part, cache: hint } : part,
),
})
: Message.make({
...target,
content: target.content.map((part, i) => (i === markAt ? { ...existing, cache: hint } : part)),
})
// Single pass over `messages`, substituting the one updated entry. Long
// conversations call this on every request, so avoid `.map()` here — its
// closure dispatch and identity copies show up in profiling.

View file

@ -1,6 +1,7 @@
import { Effect, Stream } from "effect"
import {
AIError,
AssistantMessage,
InvalidProviderOutputError,
LLMResponse,
ResponseFold,
@ -38,8 +39,8 @@ const invalidCall = (event: ToolInputError) =>
ToolCallPart.make({ id: event.id, name: event.name, namespace: event.namespace, input: {} })
/** Add the replayed calls for invalid input to the assistant message, so each tool result has its call. */
const withInvalidCalls = (message: Message, calls: ReadonlyArray<ToolCallPart>) =>
calls.length === 0 ? message : new Message({ ...message, content: [...message.content, ...calls] })
const withInvalidCalls = (message: AssistantMessage, calls: ReadonlyArray<ToolCallPart>) =>
calls.length === 0 ? message : new AssistantMessage({ ...message, content: [...message.content, ...calls] })
/** The step's assistant message followed by one tool message per call, in call order. */
export const stepMessages = (
@ -168,7 +169,8 @@ export function collect<E, R>(
}
if (event.type === "tool-result" && betweenSteps) {
const call = invalid.find((item) => item.id === event.id)
if (call) messages[assistant] = withInvalidCalls(messages[assistant], [call])
const previous = messages[assistant]
if (call && previous.role === "assistant") messages[assistant] = withInvalidCalls(previous, [call])
messages.push(
Message.tool({
id: event.id,

View file

@ -65,27 +65,29 @@ const fromRequest = Effect.fn("MetaResponses.fromRequest")(function* (request: L
{
...request,
messages: request.messages.map((message) =>
Message.make({
...message,
content: message.content.map((part) => {
if (
part.type !== "tool-result" ||
!part.providerExecuted ||
part.name !== "image_generation" ||
part.result.type !== "content" ||
part.providerMetadata?.[key]?.itemId !== part.id
)
return part
// Meta's signed image ID carries edit state; replay the handle, not the image bytes as a user message.
return ToolResultPart.make({
...part,
result: {
type: "json",
value: { type: "image_generation_call", id: part.id, status: "completed", result: null },
},
})
}),
}),
message.role === "system"
? message
: Message.make({
...message,
content: message.content.map((part) => {
if (
part.type !== "tool-result" ||
!part.providerExecuted ||
part.name !== "image_generation" ||
part.result.type !== "content" ||
part.providerMetadata?.[key]?.itemId !== part.id
)
return part
// Meta's signed image ID carries edit state; replay the handle, not the image bytes as a user message.
return ToolResultPart.make({
...part,
result: {
type: "json",
value: { type: "image_generation_call", id: part.id, status: "completed", result: null },
},
})
}),
}),
),
},
adapter,

View file

@ -649,7 +649,10 @@ const lowerMessages = Effect.fnUntraced(function* (request: LLMRequest, adapter:
part.namespace === undefined ? custom.get(part.name) : undefined
for (const message of request.messages) {
const rawMetadata = message.providerMetadata?.[providerMetadataKey]
const rawMetadata =
message.role === "user" || message.role === "assistant"
? message.providerMetadata?.[providerMetadataKey]
: undefined
const metadata = rawMetadata === undefined ? undefined : yield* decodeMessageMetadata(rawMetadata)
if (message.role === "system") {
const update = effortUpdate(message)

View file

@ -15,15 +15,18 @@ import {
UnknownProviderError,
Usage,
type FinishReasonDetails,
type AssistantMessage,
type CacheHint,
type LLMRequest,
type MediaPart,
type ReasoningPart,
type ToolCallPart,
type ToolDefinition,
type ToolMessage,
type UserMessage,
} from "../schema/index.js"
import { classifyProviderFailure } from "../provider-error.js"
import { isRecord, JsonObject, optionalArray, optionalNull, ProviderShared } from "./shared.js"
import { JsonObject, optionalArray, optionalNull, ProviderShared } from "./shared.js"
import { placesMarkers, requestTTL } from "../cache-policy.js"
import * as Cache from "./utils/cache.js"
import { resolveParallelToolCalls } from "./open-responses.js"
@ -341,7 +344,6 @@ export const OpenAIChatEvent = Schema.StructWithRest(
export type OpenAIChatEvent = Schema.Schema.Type<typeof OpenAIChatEvent>
const DONE = "[DONE]" as const
const OpenAIChatStreamEvent = Schema.Union([Schema.Literal(DONE), Protocol.jsonEvent(OpenAIChatEvent)])
type OpenAIChatRequestMessage = LLMRequest["messages"][number]
interface PendingToolDelta {
readonly id?: string
@ -382,7 +384,7 @@ interface LoweringOptions {
readonly cacheControl?: (cache: CacheHint | undefined) => OpenAIChatCacheControl | undefined
readonly toolCallID?: (id: string) => string
/** Project provider-specific fields from the exact source, even when other messages are dropped during lowering. */
readonly assistant?: (source: LLMRequest["messages"][number], message: OpenAIChatMessage) => OpenAIChatMessage
readonly assistant?: (source: AssistantMessage, message: OpenAIChatMessage) => OpenAIChatMessage
}
type MessageLoweringOptions = LoweringOptions & {
@ -472,23 +474,18 @@ const lowerMedia = Effect.fnUntraced(function* (part: MediaPart) {
return { type: "image_url" as const, image_url: { url } }
})
const openAICompatibleReasoningContent = (native: unknown) =>
isRecord(native) && typeof native.reasoning_content === "string" ? native.reasoning_content : undefined
const reasoningField = (part: ReasoningPart, providerMetadataKey: string) => {
const field = part.providerMetadata?.[providerMetadataKey]?.reasoningField
return typeof field === "string" ? field : undefined
}
const reasoningDetails = (parts: ReadonlyArray<ReasoningPart>, native: unknown, providerMetadataKey: string) => {
const reasoningDetails = (parts: ReadonlyArray<ReasoningPart>, providerMetadataKey: string) => {
const observed = parts.flatMap((part) => {
const details = part.providerMetadata?.[providerMetadataKey]?.reasoningDetails
return Array.isArray(details) ? details : []
})
if (parts.some((part) => Array.isArray(part.providerMetadata?.[providerMetadataKey]?.reasoningDetails)))
return knownReasoningDetails(observed).map(lowerReasoningDetail)
if (isRecord(native) && Array.isArray(native.reasoning_details))
return knownReasoningDetails(native.reasoning_details).map(lowerReasoningDetail)
}
// Kimi rejects its stream-only `index` on requests
@ -501,7 +498,7 @@ const lowerReasoningDetail = (detail: ReasoningDetail) => {
const isKimiDetail = (detail: { readonly type: string }) => detail.type === "summary" || detail.type === "encrypted"
const lowerUserMessage = Effect.fnUntraced(function* (message: OpenAIChatRequestMessage, options: LoweringOptions) {
const lowerUserMessage = Effect.fnUntraced(function* (message: UserMessage, options: LoweringOptions) {
const content: OpenAIChatUserContent[] = []
for (const part of message.content) {
if (part.type === "text") {
@ -523,7 +520,7 @@ const lowerUserMessage = Effect.fnUntraced(function* (message: OpenAIChatRequest
})
const lowerAssistantMessage = Effect.fnUntraced(function* (
message: OpenAIChatRequestMessage,
message: AssistantMessage,
configuredField: string | undefined,
requireReasoning: boolean,
options: MessageLoweringOptions,
@ -548,31 +545,23 @@ const lowerAssistantMessage = Effect.fnUntraced(function* (
}
}
const text = reasoning.map((part) => part.text).join("")
const details = reasoningDetails(reasoning, message.native?.openaiCompatible, options.providerMetadataKey)
const details = reasoningDetails(reasoning, options.providerMetadataKey)
const observedField = reasoning
.map((part) => reasoningField(part, options.providerMetadataKey))
.find((value) => value !== undefined)
const nativeReasoning = openAICompatibleReasoningContent(message.native?.openaiCompatible)
const fullyStructured = reasoning.every((part) =>
Array.isArray(part.providerMetadata?.[options.providerMetadataKey]?.reasoningDetails),
)
const field = (() => {
if (configuredField !== undefined && (requireReasoning || reasoning.length > 0 || nativeReasoning !== undefined))
return configuredField
if (configuredField !== undefined && (requireReasoning || reasoning.length > 0)) return configuredField
if (reasoning.length === 0) return requireReasoning ? "reasoning_content" : undefined
if (observedField !== undefined) return observedField
if (nativeReasoning !== undefined) return "reasoning_content"
if (!fullyStructured || requireReasoning) return "reasoning_content"
// Kimi always expects `reasoning_content` on replayed assistant messages,
// even when thinking arrived only through structured details.
if (details?.some(isKimiDetail)) return "reasoning_content"
})()
const reasoningText = (() => {
if (configuredField !== undefined)
return reasoning.length === 0 ? (nativeReasoning ?? (requireReasoning ? "" : undefined)) : text
if (reasoning.length === 0) return nativeReasoning ?? (requireReasoning ? "" : undefined)
return text
})()
const reasoningText = reasoning.length === 0 ? (requireReasoning ? "" : undefined) : text
const result = {
role: "assistant" as const,
content: (() => {
@ -587,7 +576,7 @@ const lowerAssistantMessage = Effect.fnUntraced(function* (
return { ...result, [field]: reasoningText }
})
const lowerToolMessages = Effect.fnUntraced(function* (message: OpenAIChatRequestMessage, options: LoweringOptions) {
const lowerToolMessages = Effect.fnUntraced(function* (message: ToolMessage, options: LoweringOptions) {
const messages: OpenAIChatMessage[] = []
const attachments: OpenAIChatUserContent[] = []
for (const part of message.content) {
@ -622,17 +611,14 @@ const toolMessage = (toolCallID: string, text: string, cacheControl: OpenAIChatC
})
const lowerMessage = Effect.fnUntraced(function* (
message: OpenAIChatRequestMessage,
message: UserMessage | AssistantMessage,
reasoningField: string | undefined,
requireReasoning: boolean,
options: MessageLoweringOptions,
) {
if (message.role === "user") return [yield* lowerUserMessage(message, options)]
if (message.role === "assistant") {
const lowered = yield* lowerAssistantMessage(message, reasoningField, requireReasoning, options)
return [options.assistant?.(message, lowered) ?? lowered]
}
return (yield* lowerToolMessages(message, options)).messages
const lowered = yield* lowerAssistantMessage(message, reasoningField, requireReasoning, options)
return [options.assistant?.(message, lowered) ?? lowered]
})
const lowerMessages = Effect.fnUntraced(function* (

View file

@ -348,13 +348,14 @@ export const flattenTools = (tools: ReadonlyArray<ToolEntry>, path: ReadonlyArra
export const flattenToolRequest = (request: LLMRequest) => {
const messages = request.messages.map((message) => {
if (message.role === "system") return message
const content = message.content.map((part) => {
if ((part.type !== "tool-call" && part.type !== "tool-result") || part.namespace === undefined) return part
return { ...part, name: `${part.namespace}_${part.name}`, namespace: undefined }
})
return content.every((part, index) => part === message.content[index])
? message
: new Message({ ...message, content })
: Message.make({ ...message, content })
})
return {
tools: flattenTools(request.tools),

View file

@ -37,6 +37,10 @@ export {
EffortPart,
ContentPart,
Message,
SystemMessage,
UserMessage,
AssistantMessage,
ToolMessage,
Freeform,
ToolDefinition,
ToolNamespace,

View file

@ -2,6 +2,7 @@ import { Schema } from "effect"
import { LLM } from "@opencode/schema/llm"
import { ContentBlockID, ToolCallID } from "./ids.js"
import {
AssistantMessage,
Message,
CompactionPart,
ToolCallPart,
@ -290,7 +291,7 @@ export const Finish = Schema.Struct({
reason: FinishReasonDetails,
usage: Schema.optional(Usage),
/** The final assistant message, set by `ai.stream` and `ai.loop`. */
message: Schema.optional(Message),
message: Schema.optional(AssistantMessage),
providerMetadata: Schema.optional(ProviderMetadata),
}).annotate({ identifier: "LLM.Event.Finish" })
export type Finish = Schema.Schema.Type<typeof Finish>
@ -453,7 +454,7 @@ interface ToolInputAssembly {
interface ResponseState {
readonly events: LLMEvent[]
readonly message: Message
readonly message: AssistantMessage
readonly usage?: Usage
readonly finishReason?: FinishReasonDetails
readonly textParts: Readonly<Record<string, ContentAssembly>>
@ -669,7 +670,7 @@ const reduceResponseState = (state: ResponseState, event: LLMEvent): ResponseSta
}
export class LLMResponse extends Schema.Class<LLMResponse>("LLM.Response")({
message: Message,
message: AssistantMessage,
events: Schema.Array(LLMEvent),
usage: Schema.optional(Usage),
finishReason: FinishReasonDetails,

View file

@ -237,58 +237,115 @@ export const ContentPart = Schema.Union([
]).pipe(Schema.toTaggedUnion("type"))
export type ContentPart = Schema.Schema.Type<typeof ContentPart>
export class Message extends Schema.Class<Message>("LLM.Message")({
export class SystemMessage extends Schema.Class<SystemMessage>("LLM.SystemMessage")({
id: Schema.optional(Schema.String),
role: MessageRole,
role: Schema.Literal("system"),
content: Schema.Array(Schema.Union([TextPart, EffortPart]).pipe(Schema.toTaggedUnion("type"))),
}) {}
export class UserMessage extends Schema.Class<UserMessage>("LLM.UserMessage")({
id: Schema.optional(Schema.String),
role: Schema.Literal("user"),
content: Schema.Array(ContentPart),
providerMetadata: Schema.optional(ProviderMetadata),
native: Schema.optional(Schema.Record(Schema.String, Schema.Unknown)),
}) {}
export class AssistantMessage extends Schema.Class<AssistantMessage>("LLM.AssistantMessage")({
id: Schema.optional(Schema.String),
role: Schema.Literal("assistant"),
content: Schema.Array(ContentPart),
providerMetadata: Schema.optional(ProviderMetadata),
}) {}
export class ToolMessage extends Schema.Class<ToolMessage>("LLM.ToolMessage")({
id: Schema.optional(Schema.String),
role: Schema.Literal("tool"),
content: Schema.Array(ContentPart),
}) {}
export type Message = SystemMessage | UserMessage | AssistantMessage | ToolMessage
export type ContentPartInput = Exclude<ContentPart, MediaPart> | MediaPartInput
export namespace Message {
export type ContentInput = string | ContentPartInput | ReadonlyArray<ContentPartInput>
export type SystemContentInput = string | TextPart | ReadonlyArray<TextPart>
export type Input = Omit<ConstructorParameters<typeof Message>[0], "content"> & {
readonly content: ContentInput
type RoleInput<Props, Content> = Omit<Props, "content"> & { readonly content: Content }
export type SystemInput = RoleInput<
ConstructorParameters<typeof SystemMessage>[0],
string | SystemMessage["content"][number] | SystemMessage["content"]
>
export type UserInput = RoleInput<ConstructorParameters<typeof UserMessage>[0], ContentInput>
export type AssistantInput = RoleInput<ConstructorParameters<typeof AssistantMessage>[0], ContentInput>
export type ToolInput = RoleInput<ConstructorParameters<typeof ToolMessage>[0], ContentInput>
export type Input = SystemInput | UserInput | AssistantInput | ToolInput
}
const messageSchema = Schema.Union([SystemMessage, UserMessage, AssistantMessage, ToolMessage]).pipe(
Schema.toTaggedUnion("role"),
Schema.annotate({ identifier: "LLM.Message" }),
)
const text = (value: string): TextPart => ({ type: "text", text: value })
const isParts = <Part extends { readonly type: string }>(
input: Part | ReadonlyArray<Part>,
): input is ReadonlyArray<Part> => Array.isArray(input)
const content = <Part extends { readonly type: string }>(
input: string | Part | ReadonlyArray<Part>,
): Array<TextPart | Part> => (typeof input === "string" ? [text(input)] : isParts(input) ? [...input] : [input])
const contentWithMedia = (input: Message.ContentInput): ContentPart[] =>
content(input).map((part) => (part.type === "media" ? mediaPart(part) : part))
function make(input: SystemMessage | Message.SystemInput): SystemMessage
function make(input: UserMessage | Message.UserInput): UserMessage
function make(input: AssistantMessage | Message.AssistantInput): AssistantMessage
function make(input: ToolMessage | Message.ToolInput): ToolMessage
function make(input: Message | Message.Input): Message
function make(input: Message | Message.Input): Message {
switch (input.role) {
case "system":
return input instanceof SystemMessage ? input : new SystemMessage({ ...input, content: content(input.content) })
case "user":
return input instanceof UserMessage
? input
: new UserMessage({ ...input, content: contentWithMedia(input.content) })
case "assistant":
return input instanceof AssistantMessage
? input
: new AssistantMessage({ ...input, content: contentWithMedia(input.content) })
case "tool":
return input instanceof ToolMessage
? input
: new ToolMessage({ ...input, content: contentWithMedia(input.content) })
}
// Untyped callers can still pass an unknown role; reject it the way the schema does.
return Schema.decodeUnknownSync(messageSchema)(input)
}
export const text = (value: string): ContentPart => ({ type: "text", text: value })
export const media = (media: Media.Input, options?: Omit<MediaPartInput, "type" | "media">): MediaPart =>
mediaPart({ type: "media", media, ...options })
export const content = (input: ContentInput): ContentPart[] => {
if (typeof input === "string") return [text(input)]
return (Array.isArray(input) ? input : [input]).map((part: ContentPartInput) =>
part.type === "media" ? mediaPart(part) : part,
)
}
export const make = (input: Message | Input) => {
if (input instanceof Message) return input
return new Message({ ...input, content: content(input.content) })
}
export const user = (content: ContentInput) => make({ role: "user", content })
export const assistant = (content: ContentInput) => make({ role: "assistant", content })
export const Message = withStatics(messageSchema, {
text,
// Typed through `MediaPartInput` so packages that infer types from `Message` can still emit declarations.
media: (media: MediaPartInput["media"], options?: Omit<MediaPartInput, "type" | "media">): MediaPart =>
mediaPart({ type: "media", media, ...options }),
content: contentWithMedia,
make,
user: (input: Message.ContentInput) => make({ role: "user", content: input }),
assistant: (input: Message.ContentInput) => make({ role: "assistant", content: input }),
/**
* Add an operator-authored instruction at this chronological point in the
* conversation. This is distinct from the initial `LLMRequest.system`
* prompt. Keep raw retrieved, tool, and web content out of privileged system
* updates; pass that untrusted content through ordinary user/tool channels.
*/
export const system = (content: SystemContentInput) => make({ role: "system", content })
export const effort = (input: { readonly effort?: ReasoningEffort; readonly previous?: ReasoningEffort }) =>
make({ role: "system", content: [{ type: "effort", effort: input.effort, previous: input.previous }] })
export const tool = (result: ToolResultPart | Parameters<typeof ToolResultPart.make>[0]) =>
make({ role: "tool", content: ["type" in result ? result : ToolResultPart.make(result)] })
}
system: (input: Message.SystemContentInput) => make({ role: "system", content: input }),
effort: (input: { readonly effort?: ReasoningEffort; readonly previous?: ReasoningEffort }) =>
make({ role: "system", content: [{ type: "effort", effort: input.effort, previous: input.previous }] }),
tool: (result: ToolResultPart | Parameters<typeof ToolResultPart.make>[0]) =>
make({ role: "tool", content: ["type" in result ? result : ToolResultPart.make(result)] }),
})
/**
* Raw-text wire encoding for a tool with exactly one required string parameter. Models that accept the format

View file

@ -1,4 +1,4 @@
import { Message, ToolResultPart, type ToolCallPart } from "./schema/messages.js"
import { type Message, ToolMessage, ToolResultPart, type ToolCallPart } from "./schema/messages.js"
const EMPTY_TOOL_OUTPUT = "(no tool output)"
const MISSING_TOOL_RESULT = "Tool result missing"
@ -48,7 +48,7 @@ export function normalizeToolHistory(messages: ReadonlyArray<Message>) {
}
function missingToolResults(calls: Iterable<ToolCallPart>) {
return new Message({
return new ToolMessage({
role: "tool",
content: [...calls].map((call) =>
ToolResultPart.make({
@ -62,7 +62,7 @@ function missingToolResults(calls: Iterable<ToolCallPart>) {
})
}
function normalizeToolMessage(message: Message, pending: Map<string, ToolCallPart>): Message | undefined {
function normalizeToolMessage(message: ToolMessage, pending: Map<string, ToolCallPart>): Message | undefined {
const content = message.content.map((part) => {
if (part.type !== "tool-result" || part.providerExecuted === true) return part
const call = pending.get(part.id)
@ -71,13 +71,7 @@ function normalizeToolMessage(message: Message, pending: Map<string, ToolCallPar
})
if (content.length === 0) return undefined
if (content.every((part, index) => part === message.content[index])) return message
return new Message({
id: message.id,
role: message.role,
content,
providerMetadata: message.providerMetadata,
native: message.native,
})
return new ToolMessage({ ...message, content })
}
function normalizeToolResult(part: ToolResultPart, call: ToolCallPart | undefined): ToolResultPart {

View file

@ -8,10 +8,12 @@ import {
GenerationOptions,
Message,
LanguageModel,
SystemMessage,
ToolCallPart,
ToolChoice,
ToolDefinition,
ToolResultPart,
UserMessage,
} from "../src/schema/index.js"
import { fixedResponse } from "./lib/http.js"
import { sseEvents } from "./lib/sse.js"
@ -77,7 +79,7 @@ describe("llm constructors", () => {
expect(Object.getPrototypeOf(request)).toBe(Object.prototype)
expect(request.model).toBeInstanceOf(LanguageModel)
expect(request.messages[0]).toBeInstanceOf(Message)
expect(request.messages[0]).toBeInstanceOf(UserMessage)
expect(request.system).toEqual([{ type: "text", text: "You are concise." }])
expect(request.messages[0]?.content).toEqual([{ type: "text", text: "Say hello." }])
expect(request.generation).toBeUndefined()
@ -229,7 +231,7 @@ describe("llm constructors", () => {
messages: [Message.user("Review this."), update],
})
expect(update).toBeInstanceOf(Message)
expect(update).toBeInstanceOf(SystemMessage)
expect(update).toEqual({
role: "system",
content: [{ type: "text", text: "Use parameterized SQL.", cache: { type: "ephemeral" } }],

View file

@ -215,7 +215,8 @@ describe("Media", () => {
const json = Schema.encodeSync(codec)(message)
expect(json).not.toContain("null")
const restored = Schema.decodeSync(codec)(json)
const parts = restored.content.filter((part) => part.type === "media")
expect(restored.role).toBe("user")
const parts = restored.role === "user" ? restored.content.filter((part) => part.type === "media") : []
expect(parts).toHaveLength(2)
expect(parts[0].media).toBeInstanceOf(Media.Asset)
expect(parts[0].media.source).toEqual({ type: "base64", data: "AQID", mediaType: "image/png" })

View file

@ -388,22 +388,6 @@ describe("Anthropic Messages route", () => {
}),
)
it.effect("rejects non-text chronological system update content before send", () =>
Effect.gen(function* () {
const error = yield* compileRequest(
ai.request({
model: opus48,
messages: [
Message.user("Before."),
Message.make({ role: "system", content: { type: "media", media: Media.base64("AAECAw==", "image/png") } }),
],
}),
).pipe(Effect.flip)
expect(error.message).toContain("Anthropic Messages system messages only support text content for now")
}),
)
it.effect("falls back for unsupported native chronological system update placement", () =>
Effect.gen(function* () {
expect(

View file

@ -1926,28 +1926,6 @@ describe("OpenAI Chat route", () => {
}),
)
it.effect("replays native scalar reasoning alongside native details", () =>
Effect.gen(function* () {
const details = [{ type: "reasoning.encrypted", data: "opaque", index: 0 }]
const replay = yield* compileRequest(
ai.request({
model,
messages: [
Message.make({
role: "assistant",
content: [{ type: "reasoning", text: "thinking" }],
native: { openaiCompatible: { reasoning_content: "thinking", reasoning_details: details } },
}),
],
}),
)
expect(replay.body.messages).toEqual([
{ role: "assistant", content: "", reasoning_content: "thinking", reasoning_details: details },
])
}),
)
it.effect("assembles streamed tool call input", () =>
Effect.gen(function* () {
const body = sseEvents(

View file

@ -1,4 +1,5 @@
import { describe, expect, test } from "bun:test"
import { Schema } from "effect"
import { ai, type LLMRequest, Message, ToolCallPart, ToolDefinition } from "../src/effect.js"
import { OpenAIChat } from "../src/protocols.js"
import { Auth } from "../src/route.js"
@ -35,7 +36,7 @@ describe("sanitizeSurrogates", () => {
}),
)
expect(sanitized.messages.every((message) => message instanceof Message)).toBe(true)
expect(sanitized.messages.every((message) => Schema.is(Message)(message))).toBe(true)
expect(sanitized.tools[0]).toBe(tool)
expect(sanitized.system).toEqual([{ type: "text", text: "system \uFFFD" }])
expect(sanitized.messages).toEqual([

View file

@ -28,6 +28,7 @@ import {
type LLMRequest,
type Media,
type ToolDefinition,
type ToolMessage,
type UsageInput,
} from "@opencode/ai/effect"
import { Auth, Endpoint, ProviderShared, RequestExecutor, type AnyRoute, type HttpMiddleware } from "@opencode/ai/route"
@ -509,7 +510,7 @@ function prompt(request: LLMRequest): LanguageModelV3Prompt {
.join("\n\n")
const pending: UserContent = []
const messages = request.messages.flatMap((input, index) => {
if (input.role !== "tool") return message(input.role, input.content)
if (input.role !== "tool") return message(input)
const lowered = toolMessage(input)
pending.push(...lowered.media)
if (request.messages[index + 1]?.role === "tool" || pending.length === 0) return lowered.messages
@ -527,11 +528,8 @@ function prompt(request: LLMRequest): LanguageModelV3Prompt {
return [{ role: "system", content: system }, ...messages]
}
function message(
role: Exclude<LLMRequest["messages"][number]["role"], "tool">,
content: LLMRequest["messages"][number]["content"],
): LanguageModelV3Message[] {
switch (role) {
function message(input: Exclude<LLMRequest["messages"][number], ToolMessage>): LanguageModelV3Message[] {
switch (input.role) {
case "system":
// The initial privileged prompt lives in `request.system` and is prepended above. A system message here is a
// chronological instruction update, but opaque AI SDK providers do not uniformly allow the system role after
@ -542,19 +540,19 @@ function message(
content: [
{
type: "text",
text: ProviderShared.wrapSystemUpdate(content.filter((part) => part.type === "text")),
text: ProviderShared.wrapSystemUpdate(input.content.filter((part) => part.type === "text")),
},
],
},
]
case "user":
return [{ role: "user", content: content.flatMap(userPart) }]
return [{ role: "user", content: input.content.flatMap(userPart) }]
case "assistant":
return [{ role: "assistant", content: content.flatMap(assistantPart) }]
return [{ role: "assistant", content: input.content.flatMap(assistantPart) }]
}
}
function toolMessage(input: LLMRequest["messages"][number]) {
function toolMessage(input: ToolMessage) {
const media: UserContent = []
const content = input.content.flatMap((part) => {
if (part.type !== "tool-result" || part.result.type !== "content") return toolResultPart(part)

View file

@ -29,6 +29,7 @@ export const register = Effect.fn("ClaudeSessionHooks.register")(function* (ctx:
delete event.options.maxTokens
// Preserve tool error status before the AI SDK bridge flattens error results to plain text.
event.messages = event.messages.map((message) => {
if (message.role === "system") return message
const content = message.content.map((part) => {
if (part.type !== "tool-result" || part.result.type !== "error") return part
const flags = {

View file

@ -164,6 +164,7 @@ const replaceMedia = (
replace: (media: { mime: string; name: string | undefined; bytes: () => number }) => string | undefined,
) =>
messages.map((message) => {
if (message.role === "system") return message
const content = message.content.map((part) => {
if (part.type === "media") {
const text = replace({ mime: part.media.mediaType, name: part.filename, bytes: () => mediaBytes(part.media) })
@ -182,7 +183,7 @@ const replaceMedia = (
})
return content.every((part, index) => part === message.content[index])
? message
: new Message({ ...message, content })
: Message.make({ ...message, content })
})
export const unsupportedParts = (

View file

@ -1,5 +1,5 @@
import { describe, expect, test } from "bun:test"
import { Message, ToolResultPart, Media } from "@opencode/ai/effect"
import { Message, ToolMessage, ToolResultPart, Media, UserMessage } from "@opencode/ai/effect"
import { boundImages, unsupportedParts } from "@opencode/core/session/model-request"
const capabilities = (input: string[]) => ({ tools: true, input, output: ["text"] })
@ -155,7 +155,7 @@ describe("SessionModelRequest.unsupportedParts", () => {
expect(result[0]).toBe(text)
expect(result[1]).toBe(supported)
expect(result[2]).not.toBe(unsupported)
expect(result[2]).toBeInstanceOf(Message)
expect(result[2]).toBeInstanceOf(ToolMessage)
expect(result[2]?.content[1]).toEqual(Message.text("caption"))
expect(result[2]?.content[0]).toMatchObject({
type: "tool-result",
@ -195,7 +195,7 @@ describe("SessionModelRequest.boundImages", () => {
]
const result = boundImages(messages)
expect(result[0]).toBeInstanceOf(Message)
expect(result[0]).toBeInstanceOf(UserMessage)
expect(result[0]?.content).toEqual([Message.text(expect.stringContaining("image was removed"))])
expect(result[1]).toBe(text)
expect(result[2]).not.toBe(messages[2])