-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathAgentConfigurationWidgetFeature.swift
More file actions
65 lines (56 loc) · 1.96 KB
/
AgentConfigurationWidgetFeature.swift
File metadata and controls
65 lines (56 loc) · 1.96 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
import ComposableArchitecture
import Foundation
import SuggestionBasic
import XcodeInspector
import ChatTab
import ConversationTab
import ChatService
import ConversationServiceProvider
@Reducer
public struct AgentConfigurationWidgetFeature {
@ObservableState
public struct State: Equatable {
public var focusedEditor: SourceEditor? = nil
public var isPanelDisplayed: Bool = false
public var currentMode: ConversationMode? = nil
public var lineHeight: Double = 16.0
}
public enum Action: Equatable {
case setCurrentMode(ConversationMode?)
case onFocusedEditorChanged(SourceEditor?)
}
public init() {}
public var body: some ReducerOf<Self> {
Reduce { state, action in
switch action {
case .onFocusedEditorChanged(let editor):
state.focusedEditor = editor
return .run { send in
let currentMode = await getCurrentMode(for: editor)
await send(.setCurrentMode(currentMode))
}
case .setCurrentMode(let mode):
state.currentMode = mode
return .none
}
}
}
}
private func getCurrentMode(for focusedEditor: SourceEditor?) async -> ConversationMode? {
guard let documentURL = focusedEditor?.realtimeDocumentURL,
documentURL.pathExtension == "md",
documentURL.lastPathComponent.hasSuffix(".agent.md") else {
return nil
}
// Load all conversation modes
guard let modes = await SharedChatService.shared.loadConversationModes() else {
return nil
}
// Find the mode that matches the current document URL
let documentURLString = documentURL.absoluteString
let mode = modes.first { mode in
guard let modeURI = mode.uri else { return false }
return modeURI == documentURLString || URL(string: modeURI)?.path == documentURL.path
}
return mode
}