-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathAppDeepLinkRoute.swift
More file actions
256 lines (223 loc) · 8.73 KB
/
Copy pathAppDeepLinkRoute.swift
File metadata and controls
256 lines (223 loc) · 8.73 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import Foundation
enum AppDeepLinkURLScheme {
static let canonical = "repoprompt-ce"
static func isSupported(_ scheme: String?) -> Bool {
scheme?.lowercased() == canonical
}
}
struct AgentSessionDeepLinkRoute: Equatable {
let windowID: Int?
let workspaceID: UUID
let tabID: UUID
let sessionID: UUID?
init(windowID: Int? = nil, workspaceID: UUID, tabID: UUID, sessionID: UUID? = nil) {
self.windowID = windowID
self.workspaceID = workspaceID
self.tabID = tabID
self.sessionID = sessionID
}
var notificationUserInfo: [AnyHashable: Any] {
var userInfo: [AnyHashable: Any] = [
AppDeepLinkRouteUserInfoKey.routeKind: AppDeepLinkRouteUserInfoValue.agentSessionKind,
AppDeepLinkRouteUserInfoKey.routeVersion: AppDeepLinkRouteUserInfoValue.currentRouteVersion,
AppDeepLinkRouteUserInfoKey.workspaceID: workspaceID.uuidString,
AppDeepLinkRouteUserInfoKey.tabID: tabID.uuidString
]
if let windowID {
userInfo[AppDeepLinkRouteUserInfoKey.windowID] = windowID
}
if let sessionID {
userInfo[AppDeepLinkRouteUserInfoKey.sessionID] = sessionID.uuidString
}
return userInfo
}
var url: URL {
var components = URLComponents()
components.scheme = AppDeepLinkURLScheme.canonical
components.host = "agent"
components.path = "/session"
var queryItems = [
URLQueryItem(name: AppDeepLinkRouteQueryItem.workspaceID, value: workspaceID.uuidString),
URLQueryItem(name: AppDeepLinkRouteQueryItem.tabID, value: tabID.uuidString)
]
if let sessionID {
queryItems.append(URLQueryItem(name: AppDeepLinkRouteQueryItem.sessionID, value: sessionID.uuidString))
}
if let windowID {
queryItems.append(URLQueryItem(name: AppDeepLinkRouteQueryItem.windowID, value: String(windowID)))
}
components.queryItems = queryItems
return components.url ?? URL(string: "\(AppDeepLinkURLScheme.canonical)://agent/session")!
}
static func parse(notificationUserInfo userInfo: [AnyHashable: Any]) -> AgentSessionDeepLinkRoute? {
guard stringValue(for: AppDeepLinkRouteUserInfoKey.routeKind, in: userInfo) == AppDeepLinkRouteUserInfoValue.agentSessionKind else {
return nil
}
guard intValue(for: AppDeepLinkRouteUserInfoKey.routeVersion, in: userInfo) == AppDeepLinkRouteUserInfoValue.currentRouteVersion else {
return nil
}
guard let workspaceID = uuidValue(for: AppDeepLinkRouteUserInfoKey.workspaceID, in: userInfo),
let tabID = uuidValue(for: AppDeepLinkRouteUserInfoKey.tabID, in: userInfo)
else {
return nil
}
let sessionID: UUID?
if value(for: AppDeepLinkRouteUserInfoKey.sessionID, in: userInfo) != nil {
guard let parsedSessionID = uuidValue(for: AppDeepLinkRouteUserInfoKey.sessionID, in: userInfo) else {
return nil
}
sessionID = parsedSessionID
} else {
sessionID = nil
}
return AgentSessionDeepLinkRoute(
windowID: intValue(for: AppDeepLinkRouteUserInfoKey.windowID, in: userInfo),
workspaceID: workspaceID,
tabID: tabID,
sessionID: sessionID
)
}
static func parse(url: URL) -> AgentSessionDeepLinkRoute? {
guard let components = URLComponents(url: url, resolvingAgainstBaseURL: false),
AppDeepLinkURLScheme.isSupported(components.scheme),
components.host?.lowercased() == "agent",
components.path == "/session"
else {
return nil
}
guard let workspaceID = uuidQueryItem(AppDeepLinkRouteQueryItem.workspaceID, in: components),
let tabID = uuidQueryItem(AppDeepLinkRouteQueryItem.tabID, in: components)
else {
return nil
}
let sessionID: UUID?
if queryItemValue(AppDeepLinkRouteQueryItem.sessionID, in: components) != nil {
guard let parsedSessionID = uuidQueryItem(AppDeepLinkRouteQueryItem.sessionID, in: components) else {
return nil
}
sessionID = parsedSessionID
} else {
sessionID = nil
}
return AgentSessionDeepLinkRoute(
windowID: intQueryItem(AppDeepLinkRouteQueryItem.windowID, in: components),
workspaceID: workspaceID,
tabID: tabID,
sessionID: sessionID
)
}
private static func value(for key: String, in userInfo: [AnyHashable: Any]) -> Any? {
userInfo.first { entry in
if let stringKey = entry.key.base as? String {
return stringKey == key
}
if let stringKey = entry.key.base as? NSString {
return stringKey as String == key
}
return false
}?.value
}
private static func stringValue(for key: String, in userInfo: [AnyHashable: Any]) -> String? {
guard let rawValue = value(for: key, in: userInfo) else { return nil }
if let string = rawValue as? String {
return string
}
if let string = rawValue as? NSString {
return string as String
}
return nil
}
private static func intValue(for key: String, in userInfo: [AnyHashable: Any]) -> Int? {
guard let rawValue = value(for: key, in: userInfo) else { return nil }
if let int = rawValue as? Int {
return int
}
if let number = rawValue as? NSNumber {
return number.intValue
}
if let string = rawValue as? String {
return Int(string)
}
return nil
}
private static func uuidValue(for key: String, in userInfo: [AnyHashable: Any]) -> UUID? {
guard let string = stringValue(for: key, in: userInfo) else { return nil }
return UUID(uuidString: string)
}
private static func queryItemValue(_ name: String, in components: URLComponents) -> String? {
components.queryItems?.last(where: { $0.name == name })?.value
}
private static func uuidQueryItem(_ name: String, in components: URLComponents) -> UUID? {
guard let value = queryItemValue(name, in: components) else { return nil }
return UUID(uuidString: value)
}
private static func intQueryItem(_ name: String, in components: URLComponents) -> Int? {
guard let value = queryItemValue(name, in: components) else { return nil }
return Int(value)
}
}
enum AppDeepLinkRoute: Equatable {
case agentSession(AgentSessionDeepLinkRoute)
case legacyURL(URL)
static func parse(url: URL) -> AppDeepLinkURLParseResult {
guard let components = URLComponents(url: url, resolvingAgainstBaseURL: false),
AppDeepLinkURLScheme.isSupported(components.scheme)
else {
return .unsupported
}
if components.host?.lowercased() == "agent" {
guard components.path == "/session",
let route = AgentSessionDeepLinkRoute.parse(url: url)
else {
return .invalidScopedRoute
}
return .route(.agentSession(route))
}
return .route(.legacyURL(url))
}
static func parse(notificationUserInfo userInfo: [AnyHashable: Any]) -> AppDeepLinkRoute? {
guard let route = AgentSessionDeepLinkRoute.parse(notificationUserInfo: userInfo) else {
return nil
}
return .agentSession(route)
}
}
enum AppDeepLinkURLParseResult: Equatable {
case route(AppDeepLinkRoute)
case invalidScopedRoute
case unsupported
}
enum AgentRouteSessionActivationResult: Equatable {
case ready
case sessionNotFound
case sessionWorkspaceMismatch
case sessionTabMismatch
case blockedByActiveDifferentSession
}
enum AgentSessionRouteResult: Equatable {
case routed
case workspaceUnavailable
case workspaceSwitchBlocked(String?)
case tabUnavailable
case sessionUnavailable
case sessionMismatch
case blockedByActiveDifferentSession
}
enum AppDeepLinkRouteUserInfoKey {
static let routeKind = "rp_route_kind"
static let routeVersion = "rp_route_version"
static let windowID = "window_id"
static let workspaceID = "workspace_id"
static let tabID = "tab_id"
static let sessionID = "session_id"
}
enum AppDeepLinkRouteUserInfoValue {
static let agentSessionKind = "agent_session"
static let currentRouteVersion = 1
}
enum AppDeepLinkRouteQueryItem {
static let windowID = "window_id"
static let workspaceID = "workspace_id"
static let tabID = "tab_id"
static let sessionID = "session_id"
}