Skip to main content

EdgeSession API reference

EdgeSession contains SDK-owned conversation orchestration helpers. It keeps chat history, mode transitions, cancellation, timeout handling, optional tool loops, and memory-policy compaction out of product-specific app code.

Developer Preview boundary

EdgeSession does not own model loading, product prompts, tool implementations, or business data. Apps provide an EdgeGenerationClient that bridges to their loaded LLMEngine or VLMEngine.

ChatSessionController

@MainActor
public final class ChatSessionController: ObservableObject

Stateful controller for multi-turn chat.

Property or methodDescription
historyCurrent compacted ChatMessage history.
isGeneratingWhether a turn is active.
lastMetricsInference metrics reported by the generation client.
lastEventLast session event, including reset reason and compaction audit when available.
init(client:maxHistoryMessages:historyCharacterBudget:)Creates a session around an app-provided generation client.
runTurn(userText:systemPrompt:mode:images:tools:onToolCall:parameters:memoryPolicy:timeoutSeconds:watchdogConfiguration:onChunk:)Appends a user turn, prepares history, generates output, and streams chunks.
generatePrepared(messages:mode:images:tools:onToolCall:parameters:memoryPolicy:timeoutSeconds:watchdogConfiguration:onChunk:)Runs generation for an already prepared message list.
replaceHistory(_:mode:)Replaces history using the compactor budget.
reset(systemPrompt:reason:)Clears session state and asks the client to reset runtime state.
cancel(reason:)Cancels local session generation and asks the client to reset runtime state.

EdgeGenerationClient

@MainActor
public protocol EdgeGenerationClient: AnyObject

Protocol boundary between EdgeSession and the app-owned inference runtime.

RequirementDescription
currentInferenceMetricsOptional metrics from the current loaded engine.
generate(messages:ciImages:tools:onToolCall:parameters:onChunk:)Runs one generation through the app-owned engine.
resetRuntime(reason:)Releases or resets runtime state after cancellation, mode changes, or explicit reset.

ChatSessionController.Mode

CaseDescription
.plainText-only turn.
.imageVision turn with CIImage inputs.
.toolTool-capable turn.
.isolated(String)Explicit isolation mode for app-defined boundaries.

ChatSessionMemoryPolicy

public struct ChatSessionMemoryPolicy: Sendable

Applies the compaction portion of an EdgeInference memory-policy plan to a chat session. It intentionally does not activate recall, mesh, or quality-loop behavior.

APIDescription
init(plan:estimatedCharactersPerToken:minimumCharacterBudget:)Creates a compaction policy from MemoryPolicyPlanner.Plan.
compactorConfig(base:)Tightens history compaction budget when the plan says to compact.
compactionAudit(...)Produces a raw-free audit record describing the compaction decision.

ToolChatLoop

public enum ToolChatLoop

Utility for bounded tool orchestration inside a session. Apps still own tool schemas, permission policy, and tool execution.

TypeDescription
RequestMessages, mode, allowed tool names, planned calls, limits, and timeout settings.
HooksApp-provided execution and observation callbacks.
PlannedToolCallApp-planned tool call from an app-owned planner.
ToolResultTool result text and source metadata.

Storage types

TypeDescription
EdgeConversationRoleCodable role mirror for persisted messages.
EdgeConversationMessageSDK-owned persisted message shape without UI payloads.
EdgeConversationSDK-owned conversation metadata.
ConversationStoreLocal conversation persistence helper.

Safety boundaries

  • Do not put product prompts, business rules, or app data schemas into EdgeSession.
  • Keep tool execution in the app layer and pass only generic tool specs/results through the session boundary.
  • Treat memory-policy audit fields as diagnostics, not evidence that answer quality improved.