تخطَّ إلى المحتوى

iOS (Swift)

التثبيت

في Xcode: File ← Add Package Dependencies ← https://github.com/heysada/sada-ios، ثم أضف مكتبة HeysadaRafiq إلى هدف تطبيقك.

حزمة Swift واحدة تضم كل حزمنا، فلها إصدار واحد لكل المنتجات: تُحدَّث معاً. وكل مكتبة تربط الملف الثنائي نفسه HeysadaKit، فـالتطبيق يحمل شيفرة كل المنتجات أياً كانت المكتبات التي يستعملها (تطبيق iOS لا يحمل إلا بيئة Kotlin واحدة، فتشترك المنتجات في إطار واحد).

شرح الاستخدام أدناه بالإنجليزية، كما يظهر في سجل الحزمة، والشيفرة نفسها في كل لغة.

The assistant in a SwiftUI app: the person’s chats with the AI, what it knows of their workspace and its settings, drawn from your app’s published design. It runs the Kotlin Multiplatform SDK inside (the same screens as on Android) with a Swift face: the session, your client tools and your own API calls are Swift closures. iOS 15 and later.

import HeysadaRafiq
import SwiftUI

let assistant = RafiqAssistant(
    baseUrl: "https://assistant.example.com",   // the panel's Install screen fills these in
    appKey: "pk_…",
    // Optional: the signed-in person, from your server (HS256, the app's identity secret).
    userToken: { try await myBackend.rafiqToken() },
    // Your app's tools: offered to the model with every message, run here on the device.
    tools: [
        "open_screen": RafiqClientTool(
            description: "Opens one of the app's screens",
            parameters: ["type": "object", "properties": ["screen": ["type": "string"]]]
        ) { input in navigate(to: input["screen"] as? String); return "opened" },
    ],
    // A client connection's calls (a change the person confirmed first) go to your API with your
    // own session, and never anywhere else.
    hostCaller: urlSessionHostCaller(base: URL(string: "https://api.example.com")!) {
        ["Authorization": "Bearer \(myToken())"]
    }
)

struct HelpScreen: View {
    var body: some View { RafiqChatView(assistant: assistant) }
}

With appKey alone the SDK starts its own session: a guest Rafiq names, kept in the app’s UserDefaults, or the signed-in person when userToken gives your server’s token. The app’s bundle id goes as X-App-Id, which a live app’s key is held to. RafiqAssistant(baseUrl:session:) stays for a session your backend mints with the app’s secret.

  • RafiqChatView(assistant:chatId:onClose:), RafiqKnowledgeView, RafiqSettingsView: the screens, each a SwiftUI view.
  • assistant.rafiq is the Kotlin SDK itself: every route (rafiq.api), and a chat to follow from your own screens (rafiq.chat(chatId:), then watch(onChange:) for its state on the main thread).
  • The session JWT is the only credential: no app key, and nothing else is called.