Skip to content

Commit f7c0cc0

Browse files
committed
Move chat related models to ChatBasic
1 parent 8f94fd6 commit f7c0cc0

30 files changed

+349
-306
lines changed

Core/Sources/ChatContextCollectors/WebChatContextCollector/QueryWebsiteFunction.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import ChatBasic
12
import Foundation
23
import LangChain
34
import OpenAIService

Core/Sources/ChatContextCollectors/WebChatContextCollector/SearchFunction.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import BingSearchService
2+
import ChatBasic
23
import Foundation
34
import OpenAIService
45
import Preferences

Core/Sources/ChatContextCollectors/WebChatContextCollector/WebChatContextCollector.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import ChatBasic
12
import ChatContextCollector
23
import Foundation
34
import OpenAIService

Core/Sources/ChatService/ChatFunctionProvider.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import ChatBasic
12
import Foundation
23
import OpenAIService
34

Pro

Submodule Pro updated from 5e667a8 to a3a3b9c

Tool/Package.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,16 @@ let package = Package(
170170
name: "SuggestionModelTests",
171171
dependencies: ["SuggestionModel"]
172172
),
173+
174+
.target(
175+
name: "ChatBasic",
176+
dependencies: [
177+
"AIModel",
178+
"Preferences",
179+
"Keychain",
180+
.product(name: "CodableWrappers", package: "CodableWrappers"),
181+
]
182+
),
173183

174184
.target(name: "AXExtension"),
175185

@@ -205,8 +215,10 @@ let package = Package(
205215
name: "BuiltinExtension",
206216
dependencies: [
207217
"SuggestionModel",
218+
"ChatBasic",
208219
"Workspace",
209220
"ChatTab",
221+
"AIModel",
210222
.product(name: "CopilotForXcodeKit", package: "CopilotForXcodeKit"),
211223
]
212224
),
@@ -288,6 +300,7 @@ let package = Package(
288300
"OpenAIService",
289301
"ObjectiveCExceptionHandling",
290302
"USearchIndex",
303+
"ChatBasic",
291304
.product(name: "Parsing", package: "swift-parsing"),
292305
.product(name: "SwiftSoup", package: "SwiftSoup"),
293306
]
@@ -312,6 +325,7 @@ let package = Package(
312325
dependencies: [
313326
"LanguageClient",
314327
"SuggestionModel",
328+
"ChatBasic",
315329
"Logger",
316330
"Preferences",
317331
"Terminal",
@@ -354,6 +368,7 @@ let package = Package(
354368
"Preferences",
355369
"TokenEncoder",
356370
"Keychain",
371+
"BuiltinExtension",
357372
.product(name: "JSONRPC", package: "JSONRPC"),
358373
.product(name: "AsyncAlgorithms", package: "swift-async-algorithms"),
359374
.product(name: "GoogleGenerativeAI", package: "generative-ai-swift"),
@@ -390,6 +405,7 @@ let package = Package(
390405
name: "ChatContextCollector",
391406
dependencies: [
392407
"SuggestionModel",
408+
"ChatBasic",
393409
"OpenAIService",
394410
]
395411
),

Tool/Sources/OpenAIService/FucntionCall/ChatGPTFunction.swift renamed to Tool/Sources/ChatBasic/ChatGPTFunction.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,12 @@ public extension ChatGPTArgumentsCollectingFunction {
100100
}
101101
}
102102

103-
struct ChatGPTFunctionSchema: Codable, Equatable {
104-
var name: String
105-
var description: String
106-
var parameters: JSONSchemaValue
103+
public struct ChatGPTFunctionSchema: Codable, Equatable {
104+
public var name: String
105+
public var description: String
106+
public var parameters: JSONSchemaValue
107107

108-
init(name: String, description: String, parameters: JSONSchemaValue) {
108+
public init(name: String, description: String, parameters: JSONSchemaValue) {
109109
self.name = name
110110
self.description = description
111111
self.parameters = parameters
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
import CodableWrappers
2+
import Foundation
3+
4+
public struct ChatMessage: Equatable, Codable {
5+
public typealias ID = String
6+
7+
public enum Role: String, Codable, Equatable {
8+
case system
9+
case user
10+
case assistant
11+
}
12+
13+
public struct FunctionCall: Codable, Equatable {
14+
public var name: String
15+
public var arguments: String
16+
public init(name: String, arguments: String) {
17+
self.name = name
18+
self.arguments = arguments
19+
}
20+
}
21+
22+
public struct ToolCall: Codable, Equatable, Identifiable {
23+
public var id: String
24+
public var type: String
25+
public var function: FunctionCall
26+
public var response: ToolCallResponse
27+
public init(
28+
id: String,
29+
type: String,
30+
function: FunctionCall,
31+
response: ToolCallResponse? = nil
32+
) {
33+
self.id = id
34+
self.type = type
35+
self.function = function
36+
self.response = response ?? .init(content: "", summary: nil)
37+
}
38+
}
39+
40+
public struct ToolCallResponse: Codable, Equatable {
41+
public var content: String
42+
public var summary: String?
43+
public init(content: String, summary: String?) {
44+
self.content = content
45+
self.summary = summary
46+
}
47+
}
48+
49+
public struct Reference: Codable, Equatable {
50+
public enum Kind: String, Codable {
51+
case `class`
52+
case `struct`
53+
case `enum`
54+
case `actor`
55+
case `protocol`
56+
case `extension`
57+
case `case`
58+
case property
59+
case `typealias`
60+
case function
61+
case method
62+
case text
63+
case webpage
64+
case other
65+
}
66+
67+
public var title: String
68+
public var subTitle: String
69+
public var uri: String
70+
public var content: String
71+
public var startLine: Int?
72+
public var endLine: Int?
73+
@FallbackDecoding<ReferenceKindFallback>
74+
public var kind: Kind
75+
76+
public init(
77+
title: String,
78+
subTitle: String,
79+
content: String,
80+
uri: String,
81+
startLine: Int?,
82+
endLine: Int?,
83+
kind: Kind
84+
) {
85+
self.title = title
86+
self.subTitle = subTitle
87+
self.content = content
88+
self.uri = uri
89+
self.startLine = startLine
90+
self.endLine = endLine
91+
self.kind = kind
92+
}
93+
}
94+
95+
/// The role of a message.
96+
@FallbackDecoding<ChatMessageRoleFallback>
97+
public var role: Role
98+
99+
/// The content of the message, either the chat message, or a result of a function call.
100+
public var content: String? {
101+
didSet { tokensCount = nil }
102+
}
103+
104+
/// A function call from the bot.
105+
public var toolCalls: [ToolCall]? {
106+
didSet { tokensCount = nil }
107+
}
108+
109+
/// The function name of a reply to a function call.
110+
public var name: String? {
111+
didSet { tokensCount = nil }
112+
}
113+
114+
/// The summary of a message that is used for display.
115+
public var summary: String?
116+
117+
/// The id of the message.
118+
public var id: ID
119+
120+
/// The number of tokens of this message.
121+
public var tokensCount: Int?
122+
123+
/// The references of this message.
124+
@FallbackDecoding<EmptyArray<Reference>>
125+
public var references: [Reference]
126+
127+
/// Is the message considered empty.
128+
public var isEmpty: Bool {
129+
if let content, !content.isEmpty { return false }
130+
if let toolCalls, !toolCalls.isEmpty { return false }
131+
if let name, !name.isEmpty { return false }
132+
return true
133+
}
134+
135+
public init(
136+
id: String = UUID().uuidString,
137+
role: Role,
138+
content: String?,
139+
name: String? = nil,
140+
toolCalls: [ToolCall]? = nil,
141+
summary: String? = nil,
142+
tokenCount: Int? = nil,
143+
references: [Reference] = []
144+
) {
145+
self.role = role
146+
self.content = content
147+
self.name = name
148+
self.toolCalls = toolCalls
149+
self.summary = summary
150+
self.id = id
151+
tokensCount = tokenCount
152+
self.references = references
153+
}
154+
}
155+
156+
public struct ReferenceKindFallback: FallbackValueProvider {
157+
public static var defaultValue: ChatMessage.Reference.Kind { .other }
158+
}
159+
160+
public struct ChatMessageRoleFallback: FallbackValueProvider {
161+
public static var defaultValue: ChatMessage.Role { .user }
162+
}
163+
File renamed without changes.

Tool/Sources/ChatContextCollector/ChatContextCollector.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import ChatBasic
12
import Foundation
23
import OpenAIService
34
import Parsing
@@ -14,7 +15,7 @@ public struct ChatContext {
1415
public struct RetrievedContent {
1516
public var document: ChatMessage.Reference
1617
public var priority: Int
17-
18+
1819
public init(document: ChatMessage.Reference, priority: Int) {
1920
self.document = document
2021
self.priority = priority

0 commit comments

Comments
 (0)