Skip to content

Resolve a request context before each model request - #292

Merged
mattt merged 3 commits into
mainfrom
mattt/request-context
Oct 3, 2026
Merged

mattt merged 3 commits into
mainfrom
mattt/request-context

Conversation

@mattt

@mattt mattt commented Oct 3, 2026 •

Copy link
Copy Markdown
Collaborator

This PR is the first of three that bring in #273 by @qoli, which adds Foundation Models 27-style dynamic instructions. It adds LanguageModelSession.RequestContext, with the transcript, instructions, and tools for one model request, and resolvedRequestContext(), and moves every built-in adapter to read them from a context it resolves before each request. If a request produces tool calls, the adapter runs them with the tools from that same context, and resolves a new context for the continuation.

For the sessions that exist today, the context is the session's own transcript, instructions, and tools, so nothing changes. The next two PRs add the DynamicInstructions builder and sessions whose instructions and tools are resolved for each request.

This PR also fixes nonstreaming Ollama requests, which sent only the latest prompt, without the instructions or earlier turns. They now send the whole transcript, like streaming requests already did.

The adapter changes are @qoli's, with main merged in.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

The Ollama migration changes static non-streaming request history semantics despite the stated compatibility guarantee.

Review effort: Balanced
Findings: 1 Medium severity

Open (1)
What changed in this PR

Introduces request-scoped session context resolution and migrates built-in model adapters to consume context snapshots.

Changes:

  • Adds and tests LanguageModelSession.RequestContext.
  • Migrates model adapters and tool loops to request-scoped transcripts, instructions, and tools.
  • Documents the new integration API.
File Description
README.md Documents request-context access.
LanguageModelSession.swift Defines and resolves request contexts.
AnthropicLanguageModel.swift Uses context snapshots during requests and tool rounds.
CoreMLLanguageModel.swift Uses context inputs for generation.
FoundationLanguageModel.swift Builds Foundation Models sessions from contexts.
GeminiLanguageModel.swift Refreshes contexts between tool rounds.
LlamaLanguageModel.swift Applies contexts to prompts and tool execution.
MLXLanguageModel.swift Applies contexts across generation paths.
OllamaLanguageModel.swift Migrates requests and tool handling to contexts.
OpenAILanguageModel.swift Refreshes request inputs per tool round.
OpenResponsesLanguageModel.swift Refreshes request inputs per tool round.
PrivateCloudComputeLanguageModel.swift Uses context for feedback sessions.
SystemLanguageModel.swift Centralizes context-based session creation.
RequestContextTests.swift Tests static context snapshots.
MockLanguageModel.swift Resolves instructions through request contexts.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +131 to +135
let requestContext = session.resolvedRequestContext()
let ollamaTools = try requestContext.tools.map(convertToolToOllamaFormat)
var messages = try requestContext.transcript.toOllamaMessages()
if messages.isEmpty {
messages.append(.init(role: .user, content: prompt.description))
@mattt
mattt requested a balanced review from Copilot October 3, 2026 15:22

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟢 Approval recommended

The migration consistently preserves static behavior, request-scoped tool snapshots, and in-flight continuation history.

Review effort: Balanced
Findings: 1 Medium severity

Open (1)

@mattt
mattt merged commit 9ddf106 into main Oct 3, 2026
7 checks passed
@mattt
mattt deleted the mattt/request-context branch October 3, 2026 15:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants