All skills
johnrogers avatar

/foundation-models

@d62eb71

Use when implementing on-device AI with Apple's Foundation Models framework (iOS 26+), building summarization/extraction/classification features, or using @Generable for type-safe structured output.

Use this Skill: https://skilld.dev/gh/johnrogers/claude-swift-engineering/foundation-models

This session only. Nothing lands on disk.

referencesgetting-started.md

≈584 tokens on demand. Your agent reads this file only when SKILL.md points to it.

Getting Started with Foundation Models

Availability Check

Always check before creating a session:

import FoundationModels

switch SystemLanguageModel.default.availability {
case .available:
    print("Foundation Models available")
case .unavailable(let reason):
    print("Unavailable: \(reason)")
}

Requirements: iPhone 15 Pro+, M1+ iPad/Mac, supported region, user opted in.

Creating a Session

// Basic
let session = LanguageModelSession()

// With instructions (define model's role)
let session = LanguageModelSession(instructions: """
    You are a helpful travel assistant.
    Respond concisely.
    """
)

Important: Never interpolate user input into instructions (security risk).

Basic Text Generation

let session = LanguageModelSession()
let response = try await session.respond(to: "Summarize this article...")
print(response.content)

Multi-Turn Conversations

Sessions retain transcript automatically:

let session = LanguageModelSession()

let first = try await session.respond(to: "Write a haiku about fishing")
// "Silent waters gleam..."

let second = try await session.respond(to: "Now one about golf")
// Model remembers context from first turn

Preventing Concurrent Requests

Button("Generate") {
    Task {
        result = try await session.respond(to: "Write a haiku").content
    }
}
.disabled(session.isResponding)

Prewarming for Performance

First generation takes 1-2s to load. Prewarm before user interaction:

class ViewModel: ObservableObject {
    private var session: LanguageModelSession?

    init() {
        Task { self.session = LanguageModelSession() }
    }

    func generate(prompt: String) async throws -> String {
        try await session!.respond(to: prompt).content
    }
}

SwiftUI Availability Pattern

struct AIFeatureView: View {
    var body: some View {
        switch SystemLanguageModel.default.availability {
        case .available:
            AIContentView()
        case .unavailable:
            Text("AI features require Apple Intelligence")
        }
    }
}

Model Specifications

  • Context Window: 4096 tokens (~12,000 chars)
  • Good for: Summarization, extraction, classification
  • Not for: World knowledge, complex reasoning

Source: SKILL.md on GitHub

No alerts16d5 checks · Risk SAFE
  • Gen Agent Trust Hub16d

    The skill provides instructions and examples for implementing on-device AI features. It introduces potential vulnerability surfaces for indirect prompt injection by teaching how to build agents that process untrusted input and interact with sensitive tools like contact lists and network services.

  • Socket16d

    No alerts

  • Snyk16d

    Risk: LOW · No issues

  • Runlayer7mo

    6 files scanned · No issues

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at d62eb71. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 months ago.

Dormantupdated 9 months ago

README badge

README badge for johnrogers/claude-swift-engineering/foundation-models