用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
直接命令不会经过审查 Prompt;运行前请先检查来源。
npx skills add https://github.com/tomevault-io/skills-registry --skill swift-ondevice-ai-language-model-patterns命令会保持在同一行。复制前请横向滚动并检查完整内容。
想先保存到本地?可下载 SkillsMP 当前能够提供的文件。
| Use when this capability is needed.
> Use when this capability is needed.
Review architecture and API design for the vfs-s3 project. Use when the user mentions @architect, asks to review an issue's design, discuss module boundaries, API shape, or architectural decisions for vfs-s3. Also trigger when the user wants to create an ADR (Architecture Decision Record) or evaluate a technical approach for the project. Intended for dispatch from Codex automation or Claude routines; GitHub trigger phrase: @vfs-s3-bot please prepare design doc Use when this capability is needed.
基于 SOC 职业分类
正在显示 SKILL.md
| name | swift-ondevice-ai-language-model-patterns |
| description | >- Use when this capability is needed. |
Apple's Foundation Models framework
(WWDC25) exposes the on-device LLM behind Apple Intelligence to your app. You always gate on
availability first, then run a LanguageModelSession,
get type-safe output via guided generation
(@Generable +
@Guide), stream partial results, and optionally extend the model with the
Tool protocol. This is a
hardware-gated capability, not just a UI pattern — the framework leads with availability for a
reason.
Announce on invoke: "Using swift-ondevice-ai-language-model-patterns to gate availability, run a LanguageModelSession, and generate @Generable output."
Do not reach for this for tasks needing world knowledge, long context, or guaranteed availability — the on-device model is small (4,096-token window) and absent on older hardware. For those, a server model behind your own API is the right call.
| API | Signature / form (verified) | Use |
|---|---|---|
SystemLanguageModel.default | static var default: SystemLanguageModel | The on-device model handle |
.availability | var availability: SystemLanguageModel.Availability → .available / .unavailable(UnavailableReason) | The mandatory pre-flight gate |
UnavailableReason | .appleIntelligenceNotEnabled, .deviceNotEligible, .modelNotReady | Why it's off |
LanguageModelSession | final class; init(instructions:), init(model:tools:instructions:) | A stateful generation session |
respond(to:options:) | returns LanguageModelSession.Response<String> | One-shot text response |
respond(generating:includeSchemaInPrompt:options:prompt:) | returns Response<Content> (guided) | Structured output |
streamResponse(to:generating:includeSchemaInPrompt:options:) | returns LanguageModelSession.ResponseStream<Content> | Streamed structured output |
@Generable | macro → Generable : ConvertibleFromGeneratedContent, ConvertibleToGeneratedContent | Mark a type the model can produce |
@Guide(description:…) | property macro; supports guides like .count(_:), .range(_:) | Constrain / describe a field |
Tool | protocol Tool<Arguments, Output> : Sendable; call(arguments:) | Function calling |
GenerationOptions | maximumResponseTokens, sampling, temperature | Tune the request |
SystemLanguageModel.default.availability BEFORE constructing a sessionConstructing/using a session on an unsupported device fails. Check availability and branch to a fallback. The unavailable reasons are actionable (prompt the user to enable Apple Intelligence vs. hide the feature on ineligible hardware):
switch SystemLanguageModel.default.availability {
case .available:
// proceed
case .unavailable(.appleIntelligenceNotEnabled):
// deep-link to Settings, or show "turn on Apple Intelligence"
case .unavailable(.deviceNotEligible), .unavailable(.modelNotReady):
// hide the feature / degrade gracefully
}
@Generable type with @Guided fieldsAnnotate the type with @Generable; describe properties with @Guide. The framework uses
constrained sampling so the model can't produce malformed output. Keep descriptions short — they
consume the context window.
@Generable
struct SearchSuggestions {
@Guide(description: "Suggested search terms.", .count(4))
var terms: [String]
}
ResponseStream of partial snapshots — handle progressive revealstreamResponse(...) returns a LanguageModelSession.ResponseStream<Content>; iterate it for
Snapshots where the generated content is filled in progressively (a PartiallyGenerated mirror
with optional fields). Your UI must render half-filled state, then settle. Call .collect() to await
the final value instead.
Instructions + all prompts + all outputs share one 4,096-token budget. Exceeding it throws
exceededContextWindowSize(_:). For long inputs, chunk the work and run each chunk in a new
LanguageModelSession, then combine. Tool definitions also consume the window.
Sendable; the model decides when to call themA Tool carries a name + description (the model uses them to decide invocation) and a
call(arguments:) whose Arguments are themselves @Generable. Tools run concurrently, so the
protocol requires Sendable.
@MainActor @Observable capability providerSurface availability and streamed state to the View layer through an @Observable object on the main
actor. The same provider can gate other Apple Intelligence features (Writing Tools, Image Playground)
behind one availability check.
import FoundationModels
@Generable
struct Recipe: Sendable {
@Guide(description: "The recipe title.") var title: String
@Guide(description: "Ingredients with quantities.") var ingredients: [String]
@Guide(description: "Ordered preparation steps.") var steps: [String]
}
@MainActor @Observable
final class RecipeGenerator {
enum State { case unsupported(SystemLanguageModel.Availability), idle, streaming(Recipe.PartiallyGenerated), done(Recipe) }
private(set) var state: State = .idle
func start() {
if case .unavailable = SystemLanguageModel.default.availability {
state = .unsupported(SystemLanguageModel.default.availability); return // fail soft
}
state = .idle
}
func generate(prompt: String) async {
.available .default.availability { }
session (
instructions:
)
{
stream session.streamResponse(to: prompt, generating: .)
snapshot stream {
state .streaming(snapshot.content)
}
stream.collect()
state .done(.content)
} {
}
}
}
.modelNotReady; re-check, don't cache "available" forever..deviceNotEligible
— design the fallback (hide, or route to a server) as a first-class path.global-skills/apple/swift-clean-architecture-module-scaffold/SKILL.md — the @MainActor
@Observable capability provider and Sendable Tool isolation.global-skills/apple/swift-feature-scaffold-mvvm-clean-arch/SKILL.md — wiring the streamed state
into a feature's State enum.global-skills/meta/skill-pattern-freshness-audit/SKILL.md — re-verify Foundation Models symbols
after each WWDC (the model + API surface is new and evolving).Last verified: 2026-06-03 against Apple Developer docs (live). Resolved draft open questions:
@Generable/@Guide confirmed real (Generable protocol page, sample uses .count(4)/.range(1...10));
streamResponse(to:generating:includeSchemaInPrompt:options:) returns ResponseStream<Content>; Tool
is protocol Tool<Arguments, Output> : Sendable; the on-device context window is 4,096 tokens (Apple
docs), correcting the ~2000 estimate.
Re-check after: WWDC26, or by 2026-12-01. Decay risk: medium (new framework; model size and API
surface may shift).
Found a drift? Run /skill-pattern-freshness-audit apple.
Source: esaldgut/ai-native-engineering-workspace — distributed by TomeVault.