import { Installation } from "@/installation" import { Provider } from "@/provider/provider" import { Log } from "@/util/log" import { streamText, wrapLanguageModel, type ModelMessage, type StreamTextResult, type Tool, type ToolSet, tool, jsonSchema, } from "ai" import { mergeDeep, pipe } from "remeda" import { ProviderTransform } from "@/provider/transform" // altimate_change start — size and clamp the finalized provider request import { clampOutputTokens, clampReasoningBudget, effectiveContextWindow, estimateInputTokens, mergeRequestHeaders, } from "@/provider/output-token-budget" // altimate_change end // altimate_change start — tool retrieval import { Retrieval } from "@/tool/retrieval" // altimate_change end import { Config } from "@/config/config" import { Instance } from "@/project/instance" import type { Agent } from "@/agent/agent" import type { MessageV2 } from "./message-v2" import { Plugin } from "@/plugin" import { SystemPrompt } from "./system" import { Flag } from "@/flag/flag" import { PermissionNext } from "@/permission/next" import { Auth } from "@/auth" // altimate_change start — Effect Context.Service facade (upstream v1.17.9 shape) import { Context, Effect, Layer, Stream } from "effect" import { LayerNode } from "@opencode-ai/core/effect/layer-node" import type { LLMEvent } from "@opencode-ai/llm" import { LLMAISDK } from "./llm/ai-sdk" import { TraceContext } from "@/altimate/observability/trace-context" // altimate_change end // altimate_change start — capture effective request inputs through identity-bearing trace events import { asSchema } from "ai" import { normalizeBigInts } from "@/altimate/util/json" import { randomUUIDv7 } from "bun" import { Bus } from "@/bus" import { LLMRequestEvent } from "@/altimate/observability/tracing" // altimate_change end // altimate_change start — run deadlines override reasoning effort for each remaining request import { applyBudgetEffort, budgetMessages } from "@/altimate/session/budget-effort" import { activeRun, effortOverride, hasActiveRuns, toolsDisabled } from "@/altimate/session/run-budget-effort" import { analystEffortOptions } from "@/altimate/session/analyst-effort" // altimate_change start — Claude Agent provider request routing import { ClaudeAgent } from "@/altimate/provider/claude-agent" // altimate_change end import { Session } from "@/session" import { SessionID } from "@/session/schema" // altimate_change end export namespace LLM { const log = Log.create({ service: "llm" }) export const OUTPUT_TOKEN_MAX = ProviderTransform.OUTPUT_TOKEN_MAX export type StreamInput = { user: MessageV2.User // altimate_change start — identify the assistant generation independently of its user turn messageID?: string // altimate_change end sessionID: string model: Provider.Model agent: Agent.Info system: string[] // altimate_change start — upstream_fix: keep ambient context outside the reusable system prefix environment?: string[] // altimate_change end // altimate_change start — upstream_fix: step-only reminders kept out of the environment message reminders?: string[] // altimate_change end abort: AbortSignal messages: ModelMessage[] small?: boolean tools: Record retries?: number toolChoice?: "auto" | "required" | "none" } export type StreamOutput = StreamTextResult // altimate_change start — task subagents inherit a live ancestor's run effort without retaining state async function sessionRunBudget(sessionID: string) { if (!hasActiveRuns()) return undefined const visited = new Set() let current: string | undefined = sessionID while (current && !visited.has(current)) { if (activeRun(current)) return { effort: effortOverride(current), toolsDisabled: toolsDisabled(current) } visited.add(current) current = (await Session.get(SessionID.make(current)).catch(() => undefined))?.parentID } return undefined } // altimate_change end // altimate_change start — Altimate Base gateway session-scoped abuse control export function withManagedSessionHeaders( providerID: string, sessionID: string, headers: Record, ): Record { if (providerID !== "altimate-free") return headers return { ...Object.fromEntries(Object.entries(headers).filter(([key]) => key.toLowerCase() !== "x-session-id")), "X-Session-Id": sessionID, } } // altimate_change end export async function stream(input: StreamInput) { // altimate_change start — normalize native tool values before estimation and SDK serialization // Keep the stored tool result untouched and preserve binary/URL media objects. input = { ...input, messages: normalizeBigInts(input.messages) } // altimate_change end const l = log .clone() .tag("providerID", input.model.providerID) .tag("modelID", input.model.id) .tag("sessionID", input.sessionID) .tag("small", (input.small ?? false).toString()) .tag("agent", input.agent.name) .tag("mode", input.agent.mode) // altimate_change start — the client trace of this request's turn (its user message), for the // log lines below and the outgoing headers; resolved once so both use the same trace const trace = TraceContext.forMessage(input.user.id) if (trace) l.tag("trace", trace.traceId) // altimate_change end l.info("stream", { modelID: input.model.id, providerID: input.model.providerID, }) const [language, cfg, provider, auth] = await Promise.all([ Provider.getLanguage(input.model), Config.get(), Provider.getProvider(input.model.providerID), Auth.get(input.model.providerID), ]) const isCodex = provider.id === "openai" && auth?.type === "oauth" // altimate_change start — keep the request-budget input typed before the first push const system: string[] = [] // altimate_change end system.push( [ // use agent prompt otherwise provider prompt // For Codex sessions, skip SystemPrompt.provider() since it's sent via options.instructions ...(input.agent.prompt ? [input.agent.prompt] : isCodex ? [] : SystemPrompt.provider(input.model)), // any custom prompt passed into this call ...input.system, // any custom prompt from last user message ...(input.user.system ? [input.user.system] : []), ] .filter((x) => x) .join("\n"), ) // altimate_change start — upstream_fix: keep per-session environment last. Providers with explicit // cache breakpoints get it as its own trailing message (after the marked prefix), and step-only // reminders (task pin, plan switch) as one more message after it: the environment message carries // the last system breakpoint, so it stays byte-identical whether or not a reminder is present. // Others keep a single system message with the environment, then the reminders, at the end. // Either way plugins still see them. // Claude Agent: a utility request (title, summary) sends these system messages as Claude's prompt, // so the environment and reminders are left out there too. const claudeUtility = ClaudeAgent.matches(input.model) && ClaudeAgent.isUtility({ agent: input.agent.name, small: input.small }) const ambient = claudeUtility ? [] : [...(input.environment ?? []), ...(input.reminders ?? [])] const splitEnvironment = ambient.length > 0 && ProviderTransform.usesExplicitCacheBreakpoints(input.model) const tail = splitEnvironment ? [input.environment, input.reminders].filter((x) => x?.length).map((x) => x!.join("\n")) : [] if (ambient.length) { if (splitEnvironment) system.push(...tail) else system[0] = [system[0], ambient.join("\n")].filter((x) => x).join("\n") } const header = system[0] // Claude Agent chats send only the instruction sources (Claude Code supplies its own prompt and // environment), so plugins transform that list, and their edits reach Claude. Utility requests send // the system messages themselves, so plugins transform those, as for other providers. const claudeInstructions = ClaudeAgent.matches(input.model) && !claudeUtility ? [...input.system, input.user.system].filter((text): text is string => !!text) : undefined await Plugin.trigger( "experimental.chat.system.transform", { sessionID: input.sessionID, model: input.model }, { system: claudeInstructions ?? system }, ) // rejoin to maintain the header / rest / tail structure for caching if header unchanged if (system.length > 1 + Math.max(tail.length, 1) && system[0] === header) { const rest = system.slice(1) const kept: string[] = [] for (const message of tail) { const at = rest.lastIndexOf(message) if (at < 0) continue rest.splice(at, 1) kept.push(message) } system.length = 0 system.push(header, ...(rest.length ? [rest.join("\n")] : []), ...kept) } // The reminders message is sent flagged, so it never takes one of the two system cache marks: // with no environment message it would otherwise be the second system message and be marked. const reminderText = splitEnvironment && input.reminders?.length ? input.reminders.join("\n") : undefined const reminderAt = reminderText === undefined ? -1 : system.lastIndexOf(reminderText) // altimate_change end const variant = !input.small && input.model.variants && input.user.variant ? input.model.variants[input.user.variant] : {} const base = input.small ? ProviderTransform.smallOptions(input.model) : ProviderTransform.options({ model: input.model, sessionID: input.sessionID, providerOptions: provider.options, }) const options: Record = pipe( base, mergeDeep(input.model.options), // altimate_change start — analyst default reasoning effort; agent options and variants still win mergeDeep( analystEffortOptions({ agent: input.agent, model: input.model, variant: input.user.variant, small: input.small, }), ), // altimate_change end mergeDeep(input.agent.options), mergeDeep(variant), ) if (isCodex) { options.instructions = SystemPrompt.instructions() } // altimate_change start — Claude Agent: Claude Code's own prompt replaces the provider prompt, so the // agent prompt and instruction sources travel as options; the environment block is left out const claudeAgent = ClaudeAgent.matches(input.model) ? ClaudeAgent.request({ sessionID: input.sessionID, agent: input.agent.name, agentPrompt: input.agent.prompt, small: input.small, instructions: claudeInstructions ?? [], }) : undefined if (claudeAgent) Object.assign(options, claudeAgent.options) // altimate_change end // altimate_change start — pass maxOutputTokens INTO chat.params hook so plugins // (codex, github-copilot, third-party) can override it. Upstream PRs #21220 + #21225 // moved the codex/copilot exclusion logic into plugin chat.params hooks, but // session/llm.ts wasn't updated to plumb maxOutputTokens through the hook — // plugin overrides were silently a no-op. Compute the default first, let the hook // mutate it, then read back from params.maxOutputTokens (line 208). const params = await Plugin.trigger( "chat.params", { sessionID: input.sessionID, agent: input.agent, model: input.model, provider, message: input.user, }, { temperature: input.model.capabilities.temperature ? (input.agent.temperature ?? ProviderTransform.temperature(input.model)) : undefined, topP: input.agent.topP ?? ProviderTransform.topP(input.model), topK: ProviderTransform.topK(input.model), // Claude Agent: without an explicit cap from a plugin, Claude Code picks its own output budget maxOutputTokens: isCodex || provider.id.includes("github-copilot") || claudeAgent ? undefined : ProviderTransform.maxOutputTokens(input.model), options, }, ) // altimate_change end const { headers } = await Plugin.trigger( "chat.headers", { sessionID: input.sessionID, agent: input.agent, model: input.model, provider, message: input.user, }, { headers: {}, }, ) // altimate_change start — canonicalize the exact outgoing header precedence before budgeting const requestHeaders = mergeRequestHeaders( input.model.providerID.startsWith("opencode") ? { "x-opencode-project": Instance.project.id, "x-opencode-session": input.sessionID, "x-opencode-request": input.user.id, "x-opencode-client": Flag.OPENCODE_CLIENT, } : { "User-Agent": `altimate-code/${Installation.VERSION}`, }, input.model.headers, headers, ) // altimate_change end // altimate_change start — Claude Agent: request kind and session, read by its language model if (claudeAgent) Object.assign(requestHeaders, claudeAgent.headers) // altimate_change end // altimate_change start — finalization must have no executable catalog, even for unsolicited tool calls const runBudget = await sessionRunBudget(input.sessionID) const resolvedTools: Record = runBudget?.toolsDisabled ? {} : await resolveTools(input) // The AI SDK checks tool names by property lookup. A null-prototype catalog prevents // inherited Object properties (for example, "constructor") from masquerading as tools. const tools: Record = Object.assign(Object.create(null), resolvedTools) const toolChoice = runBudget?.toolsDisabled ? "none" : input.toolChoice // altimate_change end // altimate_change start — ensure tool definitions exist for all tool_use blocks in history // The Anthropic API (and proxies like LiteLLM) require every tool_use block in // message history to have a matching tool definition. When agents switch (Plan→Builder), // MCP tools disconnect, or tools are filtered by permissions, the history may reference // tools absent from the current set. Add stub definitions for any missing tools. // Fixes: https://github.com/AltimateAI/altimate-code/issues/678 const referencedTools = toolNamesFromMessages(input.messages) addHistoricalToolStubs(tools, referencedTools, toolChoice) // altimate_change end // altimate_change start — tool retrieval // Expose only the relevant top-k tools this turn (flag-gated). Keeps the // always-on core + any in-flight (referenced) tools; no-op for small sets. if (Retrieval.enabled()) { const lastUser = [...input.messages].reverse().find((m) => m.role === "user") const c = lastUser?.content as any const query = typeof c === "string" ? c : Array.isArray(c) ? c.map((p: any) => (typeof p === "string" ? p : (p?.text ?? ""))).join(" ") : "" const list = Object.entries(tools).map(([name, t]) => ({ name, description: (t as any)?.description })) const keep = Retrieval.select(query, list, { keep: referencedTools }) for (const name of Object.keys(tools)) { // Never delete "invalid": it's the AI-SDK fallback tool the runtime relies // on for malformed tool calls, not a user-facing tool, so it's exempt from // retrieval rather than being listed in Retrieval.CORE. if (name !== "invalid" && !keep.has(name)) delete tools[name] } } // altimate_change end // altimate_change start — clamp after every context-affecting request field is finalized. // Tool schemas and provider instructions consume the shared context window, while encoded // media bytes do not count as literal text. The estimator runs lazily so providers that omit // maxOutputTokens pay no serialization cost. Known context beta headers widen the catalog // limit before the clamp. Fixed reasoning budgets are reconciled with the final reservation. const maxOutputTokens = clampOutputTokens({ model: input.model, requested: params.maxOutputTokens, context: effectiveContextWindow({ model: input.model, // Provider defaults are lower precedence than the exact case-normalized outgoing record. headerSources: [provider.options, requestHeaders], }), inputTokens: () => estimateInputTokens({ system, messages: ProviderTransform.messagesForInputEstimate(input.messages, input.model), tools, instructions: params.options.instructions, }), }) // altimate_change start — deadline effort wins configured variants and plugin overrides const requestOptions = clampReasoningBudget( runBudget?.effort ? applyBudgetEffort(input.model, params.options) : params.options, maxOutputTokens, ) const requestMessages = budgetMessages(input.messages, input.model, requestOptions) // altimate_change end // altimate_change end // altimate_change start — detect a toolless request once, for the message flattening below const declaresNoTools = Object.keys(tools).filter((x) => x !== "invalid").length === 0 // altimate_change end // altimate_change start — record final system text and resolved tools before provider middleware // Restrict capture to these fields: provider options/auth/headers never enter the event. if (cfg.tracing?.enabled !== false) { try { await Bus.publish(LLMRequestEvent, { requestID: randomUUIDv7(), sessionID: input.sessionID, messageID: input.messageID ?? input.user.id, userMessageID: input.user.id, agent: input.agent.name, providerID: input.model.providerID, modelID: input.model.id, variant: input.small ? undefined : input.user.variant, startTime: Date.now(), system: [ ...system, ...input.messages.filter((message) => message.role === "system").map((message) => message.content), ].join("\n\n"), instructions: typeof requestOptions.instructions === "string" ? requestOptions.instructions : undefined, tools: await Promise.all( Object.entries(tools) .sort(([a], [b]) => a.localeCompare(b)) .map(async ([name, definition]) => ({ name, description: definition.description, inputSchema: await asSchema(definition.inputSchema).jsonSchema, active: name !== "invalid", providerToolID: definition.type === "provider" ? definition.id : undefined, })), ), }) } catch { l.debug("failed to capture LLM request inputs") } } // altimate_change end return streamText({ onError(error) { l.error("stream error", { error, }) }, // altimate_change start — upstream_fix: Copilot raw billing chunks // Copilot exposes totalNanoAiu only through provider raw chunks. includeRawChunks: input.model.providerID.includes("github-copilot"), // altimate_change end async experimental_repairToolCall(failed) { const lower = failed.toolCall.toolName.toLowerCase() if (lower !== failed.toolCall.toolName && Object.prototype.hasOwnProperty.call(tools, lower)) { l.info("repairing tool call", { tool: failed.toolCall.toolName, repaired: lower, }) return { ...failed.toolCall, toolName: lower, } } return { ...failed.toolCall, input: JSON.stringify({ tool: failed.toolCall.toolName, error: failed.error.message, }), toolName: "invalid", } }, temperature: params.temperature, topP: params.topP, topK: params.topK, // altimate_change start — use the reasoning options reconciled with the final output reservation providerOptions: ProviderTransform.providerOptions(input.model, requestOptions), // altimate_change end activeTools: Object.keys(tools).filter((x) => x !== "invalid"), tools, // altimate_change start — the finalization no-tools contract also suppresses historical stubs toolChoice, // altimate_change end // altimate_change start — use the plugin-selected reservation after context clamping maxOutputTokens, // altimate_change end abortSignal: input.abort, // altimate_change start — send the canonical headers used by the budget estimator, bound to the // current Altimate Base session, plus the turn's client trace for the Altimate gateways headers: withManagedSessionHeaders( input.model.providerID, input.sessionID, TraceContext.withHeaders(requestHeaders, trace, input.model.providerID, provider.options?.headers), ), // altimate_change end maxRetries: input.retries ?? 0, messages: [ // altimate_change start — upstream_fix: the reminders message is never a cache-marked one ...system.map( (x, index): ModelMessage => index > 0 && index === reminderAt ? ProviderTransform.unmarkedSystem(x) : { role: "system", content: x, }, ), // altimate_change end // altimate_change start — a request that declares no tools must not carry tool-call // messages. The toolless agents (compaction, title, summary) summarize a session's own // history, so they would otherwise send tool calls referencing functions the request // never declares. The Altimate gateway fails every provider in its fallback chain on // that shape and reports one generic error; see ProviderTransform.flattenToolParts. // altimate_change start — keep interrupted reasoning in storage without sending empty disabled-thinking turns ...(declaresNoTools ? ProviderTransform.flattenToolParts(requestMessages) : requestMessages), // altimate_change end // altimate_change end ], model: wrapLanguageModel({ model: language, middleware: [ { specificationVersion: "v3", async transformParams(args) { if (args.type === "stream") { // altimate_change start — transform messages with the reconciled reasoning options // @ts-expect-error args.params.prompt = ProviderTransform.message(args.params.prompt, input.model, requestOptions) // altimate_change end } return args.params }, }, ], }), experimental_telemetry: { isEnabled: cfg.experimental?.openTelemetry, metadata: { userId: cfg.username ?? "unknown", sessionId: input.sessionID, }, }, }) } // altimate_change start — one removal rule shared with prompt text that names tools // Tools removed before the model call: denied by the agent, or switched off on the request. export function removedTools(names: string[], input: Pick): Set { const disabled = PermissionNext.disabled(names, input.agent.permission) // The hidden SDK fallback reports rejected calls; it grants no tool capability. return new Set( names.filter((tool) => tool !== "invalid" && (input.user.tools?.[tool] === false || disabled.has(tool))), ) } async function resolveTools(input: Pick) { for (const tool of removedTools(Object.keys(input.tools), input)) delete input.tools[tool] return input.tools } // altimate_change end // altimate_change start — collect tool names from message history to prevent API validation errors // Anthropic API requires every tool_use block in message history to have a matching tool // definition. When agents switch (e.g. Plan→Builder) or MCP tools disconnect, the history // may reference tools no longer in the active set. This function extracts those names so // stub definitions can be added. Fixes #678. // // Tool names must match the API's allowed character set (Anthropic: `[a-zA-Z0-9_-]{1,64}`). // If a loaded session file was tampered with, names with shell metacharacters, ANSI escapes, // or excessive length are silently dropped rather than registered as stubs. const VALID_TOOL_NAME_RE = /^[a-zA-Z0-9_-]{1,64}$/ export function toolNamesFromMessages(messages: ModelMessage[]): Set { const names = new Set() for (const msg of messages) { if (!Array.isArray(msg.content)) continue for (const part of msg.content) { if (part.type === "tool-call" || part.type === "tool-result") { if (typeof part.toolName === "string" && VALID_TOOL_NAME_RE.test(part.toolName)) { names.add(part.toolName) } } } } return names } // Mutates `tools`, adding a stub definition for every referenced historical tool // name that has no real definition (see toolNamesFromMessages above / issue #678). // // Skip stub injection only for the explicit toolChoice "none" no-tool-call // contract (e.g. the compaction summarizer, which passes tools: {} and // toolChoice "none"). With an empty tool set AND toolChoice "none" the AI SDK // omits both `tools` and `tool_choice` from the request, which every provider // accepts — this is the compat fallback for providers whose OpenAI-compat // layer rejects toolChoice "none". Injecting stubs there would instead // advertise callable tools on a call that must produce text only. // // altimate_change start — upstream_fix: gating on `tools` being empty alone // (rather than toolChoice) also matched a NORMAL turn whose tool allowlist or // agent permissions stripped every tool but whose history still references // tool calls — skipping stubs there could reintroduce the Anthropic // "tool_use with no matching definition" 400 this function exists to fix. export function addHistoricalToolStubs( tools: Record, referenced: Iterable, toolChoice?: "auto" | "required" | "none", ) { if (toolChoice === "none" && Object.keys(tools).length === 0) return tools // altimate_change end for (const name of referenced) { if (!Object.hasOwn(tools, name)) { tools[name] = tool({ description: `[Historical] Tool no longer available in this session`, inputSchema: jsonSchema({ type: "object", properties: {} }), execute: async () => ({ output: "This tool is no longer available. Please use an alternative approach.", title: "", metadata: {}, }), }) } } return tools } // altimate_change end // altimate_change start — Effect Context.Service facade so the new upstream consumers // that do `yield* LLM.Service` / `LLM.defaultLayer` / `LLM.node` compile. The imperative // `LLM.stream(...)` Promise API above is preserved unchanged; this Service delegates to it. // The single `stream` method mirrors the upstream v1.17.9 shape: it returns an Effect // `Stream` of `@opencode-ai/llm` `LLMEvent`s. We acquire a scoped AbortController, run the // namespace `stream()` to get the AI-SDK `StreamTextResult`, then convert its `fullStream` // to `LLMEvent`s via the existing `LLMAISDK.toLLMEvents` adapter (per-stream adapter state). export interface Interface { readonly stream: (input: Omit) => Stream.Stream } export class Service extends Context.Service()("@opencode/LLM") {} export const layer = Layer.succeed( Service, Service.of({ stream(input) { return Stream.unwrap( Effect.gen(function* () { const ctrl = yield* Effect.acquireRelease( Effect.sync(() => new AbortController()), (c) => Effect.sync(() => c.abort()), ) const result = yield* Effect.promise(() => stream({ ...input, abort: ctrl.signal })) const state = LLMAISDK.adapterState() return Stream.fromAsyncIterable(result.fullStream, (e) => e instanceof Error ? e : new Error(String(e)), ).pipe( Stream.mapEffect((event) => LLMAISDK.toLLMEvents(state, event)), Stream.flatMap((events) => Stream.fromIterable(events)), ) }), ) }, }), ) export const defaultLayer = layer export const node = LayerNode.make(layer, []) // altimate_change end }