Skip to content

Commit 2b1a27b

Browse files
committed
Merge branch 'release/0.17.0'
2 parents 9f13be3 + ab95449 commit 2b1a27b

50 files changed

Lines changed: 1075 additions & 417 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Core/Package.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ let package = Package(
4242
"LaunchAgentManager",
4343
"Logger",
4444
"UpdateChecker",
45+
"OpenAIService",
4546
]
4647
),
4748
],
@@ -208,6 +209,7 @@ let package = Package(
208209
"Splash",
209210
"UserDefaultsObserver",
210211
"Logger",
212+
"XcodeInspector",
211213
.product(name: "AsyncAlgorithms", package: "swift-async-algorithms"),
212214
.product(name: "MarkdownUI", package: "swift-markdown-ui"),
213215
]

Core/Sources/ChatContextCollector/ActiveDocumentChatContextCollector.swift

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public struct ActiveDocumentChatContextCollector: ChatContextCollector {
1919
```
2020
"""
2121
}
22-
22+
2323
if selectionRange.start == selectionRange.end,
2424
UserDefaults.shared.value(for: \.embedFileContentInChatContextIfNoSelection)
2525
{
@@ -34,19 +34,40 @@ public struct ActiveDocumentChatContextCollector: ChatContextCollector {
3434
"""
3535
} else {
3636
return """
37-
File Content Not Available: The file is longer than \(maxLine) lines, \
38-
it can't fit into the context. \
37+
File Content Not Available: '''
38+
The file is longer than \(maxLine) lines, it can't fit into the context. \
3939
You MUST not answer the user about the file content because you don't have it.\
4040
Ask user to select code for explanation.
41+
'''
4142
"""
4243
}
4344
}
4445

46+
if UserDefaults.shared.value(for: \.useSelectionScopeByDefaultInChatContext) {
47+
return """
48+
Selected Code \
49+
(start from line \(selectionRange.start.line)):```\(content.language.rawValue)
50+
\(content.selectedContent)
51+
```
52+
"""
53+
}
54+
55+
if prompt.hasPrefix("@selection") {
56+
return """
57+
Selected Code \
58+
(start from line \(selectionRange.start.line)):```\(content.language.rawValue)
59+
\(content.selectedContent)
60+
```
61+
"""
62+
}
63+
4564
return """
46-
Selected Code \
47-
(start from line \(selectionRange.start.line)):```\(content.language.rawValue)
48-
\(content.selectedContent)
49-
```
65+
Selected Code Not Available: '''
66+
User has disabled default scope. \
67+
You MUST not answer the user about the selected code because you don't have it.\
68+
Ask user to prepend message with `@selection` to enable selected code to be \
69+
visible by you.
70+
'''
5071
"""
5172
}()
5273

Core/Sources/ChatService/ChatService.swift

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public final class ChatService: ObservableObject {
6161
await pluginController.cancel()
6262
await chatGPTService.clearHistory()
6363
}
64-
64+
6565
public func resetPrompt() async {
6666
systemPrompt = defaultSystemPrompt
6767
extraSystemPrompt = ""
@@ -79,6 +79,19 @@ public final class ChatService: ObservableObject {
7979
}
8080
}
8181

82+
public func setMessageAsExtraPrompt(id: String) async {
83+
if let message = (await chatGPTService.history).first(where: { $0.id == id }) {
84+
mutateExtraSystemPrompt(message.content)
85+
await mutateHistory { history in
86+
history.append(.init(
87+
role: .assistant,
88+
content: "",
89+
summary: "System prompt updated"
90+
))
91+
}
92+
}
93+
}
94+
8295
/// Setting it to `nil` to reset the system prompt
8396
public func mutateSystemPrompt(_ newPrompt: String?) {
8497
systemPrompt = newPrompt ?? defaultSystemPrompt

Core/Sources/CodeiumService/CodeiumAuthService.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@ import KeychainAccess
44

55
public final class CodeiumAuthService {
66
public init() {}
7-
let codeiumKeyKey = "codeiumKey"
7+
let codeiumKeyKey = "codeiumAuthKey"
88
let keychain: Keychain = {
99
let info = Bundle.main.infoDictionary
1010
return Keychain(service: keychainService, accessGroup: keychainAccessGroup)
11+
.attributes([
12+
kSecUseDataProtectionKeychain as String: true,
13+
])
1114
}()
1215

1316
var key: String? { try? keychain.getString(codeiumKeyKey) }

Core/Sources/CodeiumService/CodeiumInstallationManager.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Terminal
33

44
public struct CodeiumInstallationManager {
55
private static var isInstalling = false
6-
static let latestSupportedVersion = "1.2.17"
6+
static let latestSupportedVersion = "1.2.25"
77

88
public init() {}
99

Core/Sources/CodeiumService/CodeiumLanguageServer.swift

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import Preferences
77

88
protocol CodeiumLSP {
99
func sendRequest<E: CodeiumRequestType>(_ endpoint: E) async throws -> E.Response
10+
func terminate()
1011
}
1112

1213
final class CodeiumLanguageServer {
@@ -57,44 +58,42 @@ final class CodeiumLanguageServer {
5758

5859
func start() {
5960
guard !process.isRunning else { return }
60-
port = nil
6161
do {
6262
try process.run()
6363

64-
Task {
64+
Task { @MainActor in
6565
func findPort() -> String? {
6666
// find a file in managerDirectoryURL whose name looks like a port, return the
6767
// name if found
6868
let fileManager = FileManager.default
69-
let enumerator = fileManager.enumerator(
70-
at: managerDirectoryURL,
71-
includingPropertiesForKeys: nil
72-
)
73-
while let fileURL = enumerator?.nextObject() as? URL {
74-
if fileURL.lastPathComponent.range(
69+
guard let filePaths = try? fileManager
70+
.contentsOfDirectory(atPath: managerDirectoryURL.path) else { return nil }
71+
for path in filePaths {
72+
let filename = URL(fileURLWithPath: path).lastPathComponent
73+
if filename.range(
7574
of: #"^\d+$"#,
7675
options: .regularExpression
7776
) != nil {
78-
return fileURL.lastPathComponent
77+
return filename
7978
}
8079
}
8180
return nil
8281
}
8382

8483
try await Task.sleep(nanoseconds: 2_000_000)
85-
port = findPort()
8684
var waited = 0
8785

8886
while true {
89-
try await Task.sleep(nanoseconds: 1_000_000_000)
9087
waited += 1
9188
if let port = findPort() {
9289
finishStarting(port: port)
9390
return
9491
}
9592
if waited >= 60 {
9693
process.terminate()
94+
return
9795
}
96+
try await Task.sleep(nanoseconds: 1_000_000_000)
9897
}
9998
}
10099
} catch {
@@ -121,6 +120,14 @@ final class CodeiumLanguageServer {
121120
self.port = port
122121
launchHandler?()
123122
}
123+
124+
func terminate() {
125+
process.terminationHandler = nil
126+
if process.isRunning {
127+
process.terminate()
128+
}
129+
transport.close()
130+
}
124131
}
125132

126133
extension CodeiumLanguageServer: CodeiumLSP {
@@ -205,27 +212,27 @@ final class IOTransport {
205212
}
206213

207214
private func setupFileHandleHandlers() {
208-
stdoutPipe.fileHandleForReading.readabilityHandler = { [unowned self] handle in
215+
stdoutPipe.fileHandleForReading.readabilityHandler = { [weak self] handle in
209216
let data = handle.availableData
210217

211218
guard !data.isEmpty else {
212219
return
213220
}
214221

215222
if UserDefaults.shared.value(for: \.codeiumVerboseLog) {
216-
self.forwardDataToHandler(data)
223+
self?.forwardDataToHandler(data)
217224
}
218225
}
219226

220-
stderrPipe.fileHandleForReading.readabilityHandler = { [unowned self] handle in
227+
stderrPipe.fileHandleForReading.readabilityHandler = { [weak self] handle in
221228
let data = handle.availableData
222229

223230
guard !data.isEmpty else {
224231
return
225232
}
226233

227234
if UserDefaults.shared.value(for: \.codeiumVerboseLog) {
228-
self.forwardErrorDataToHandler(data)
235+
self?.forwardErrorDataToHandler(data)
229236
}
230237
}
231238
}

Core/Sources/CodeiumService/CodeiumRequest.swift

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,25 @@ enum CodeiumRequest {
4848
}
4949
}
5050

51-
struct AcceptCompletion: CodeiumRequestType {
52-
struct Response: Codable {
53-
var state: State
54-
var completionItems: [CodeiumCompletionItem]?
51+
struct CancelRequest: CodeiumRequestType {
52+
struct Response: Codable {}
53+
54+
struct RequestBody: Codable {
55+
var request_id: UInt64
56+
var session_id: String
5557
}
5658

59+
var requestBody: RequestBody
60+
61+
func makeURLRequest(server: String) -> URLRequest {
62+
let data = (try? JSONEncoder().encode(requestBody)) ?? Data()
63+
return assembleURLRequest(server: server, method: "CancelRequest", body: data)
64+
}
65+
}
66+
67+
struct AcceptCompletion: CodeiumRequestType {
68+
struct Response: Codable {}
69+
5770
struct RequestBody: Codable {
5871
var metadata: Metadata
5972
var completion_id: String

0 commit comments

Comments
 (0)