-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathExample-AgentSDK.swift
More file actions
executable file
·81 lines (63 loc) · 3.02 KB
/
Example-AgentSDK.swift
File metadata and controls
executable file
·81 lines (63 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env swift
// Example: Using the Agent SDK Backend
// This demonstrates the simplest possible migration from headless to Agent SDK
//
// Run: swift Example-AgentSDK.swift
// (Make sure you have: npm install -g @anthropic-ai/claude-agent-sdk)
import Foundation
// Add the ClaudeCodeSDK directory to the import path when running as a script
// In a real project, you'd import it normally: import ClaudeCodeSDK
print("🚀 Agent SDK Backend Example\n")
// STEP 1: Check if Agent SDK is available
print("Checking Agent SDK installation...")
let npmCheck = Process()
npmCheck.executableURL = URL(fileURLWithPath: "/usr/bin/env")
npmCheck.arguments = ["npm", "list", "-g", "@anthropic-ai/claude-agent-sdk"]
npmCheck.standardOutput = Pipe()
npmCheck.standardError = Pipe()
do {
try npmCheck.run()
npmCheck.waitUntilExit()
if npmCheck.terminationStatus == 0 {
print("✅ Agent SDK is installed\n")
print("To use the Agent SDK backend in your code:")
print("─────────────────────────────────────────────\n")
print("""
import ClaudeCodeSDK
// Configure for Agent SDK
var config = ClaudeCodeConfiguration.default
config.backend = .agentSDK // 👈 Just add this line!
let client = try ClaudeCodeClient(configuration: config)
// Run a prompt (use .streamJson for Agent SDK)
let result = try await client.runSinglePrompt(
prompt: "Explain what Swift is",
outputFormat: .streamJson,
options: nil
)
// Handle the streaming response
if case .stream(let publisher) = result {
for await message in publisher.values {
print(message)
}
}
""")
print("\n─────────────────────────────────────────────")
print("\n📚 See AGENT_SDK_MIGRATION.md for complete examples")
} else {
print("❌ Agent SDK is NOT installed\n")
print("Install it with:")
print(" npm install -g @anthropic-ai/claude-agent-sdk\n")
}
} catch {
print("❌ Error checking npm: \(error)")
}
print("\n💡 Quick comparison:")
print("┌──────────────┬─────────────────┬─────────────────┐")
print("│ Feature │ Headless │ Agent SDK │")
print("├──────────────┼─────────────────┼─────────────────┤")
print("│ Setup │ .headless │ .agentSDK │")
print("│ Speed │ Baseline │ 2-10x faster │")
print("│ Output │ .json/.text │ .streamJson │")
print("│ Sessions │ Full support │ Full support │")
print("│ MCP Servers │ ✅ │ ✅ │")
print("└──────────────┴─────────────────┴─────────────────┘")