Add an on-device AI agent to a Swift app
In Xcode, choose File > Add Package Dependencies, enter https://github.com/ondeinference/ed-swift and add the Ed product to your target.
A tool and an agent
import Ed
struct BatteryTool: EdTool {
let definition = EdToolDefinition.readOnly(
name: "battery_level",
description: "Read the device battery percentage",
parametersSchema: #"{"type":"object","properties":{}}"#
)
func execute(arguments: String) async throws -> String {
// Read the real value from your platform API here.
"The battery is at 73 percent."
}
}
let ed = EdAgent { call in
// This callback is used only for mutating tools.
let userApproved = await showApproval(for: call)
return userApproved ? .allowOnce : .deny
}
try await ed.register(BatteryTool())
try await ed.loadDefaultAgentModel(
systemPrompt: "You are a concise assistant inside my app."
)
let reply = try await ed.run("How much battery do I have?")
print(reply.text)
for await event in ed.events() {
print(event)
}
Use .readOnly for tools that only inspect state. Use .mutating for tools that save, send, purchase or delete. Ed sends every mutating call through your approval callback.