Skip to content

Commit 44cc3e3

Browse files
Merge pull request #13 from Compiler-Inc/feat/add-tool-call-content-type
feat: add tool call content type
2 parents 579be34 + e29aec5 commit 44cc3e3

File tree

3 files changed

+28
-10
lines changed

3 files changed

+28
-10
lines changed

Sources/CompilerSwiftAI/Model Calling/Domain Models/ChatCompletionChunk.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public struct ChatDelta: Decodable, Sendable {
6666
}
6767

6868
/// Represents a delta for a tool call in a streamed response
69-
public struct ToolCallDelta: Decodable, Sendable {
69+
public struct ToolCallDelta: Codable, Sendable, Equatable {
7070
/// The index of this tool call in the array
7171
public let index: Int
7272
/// The ID of the tool call (only present in first delta)
@@ -78,7 +78,7 @@ public struct ToolCallDelta: Decodable, Sendable {
7878
}
7979

8080
/// Represents a delta for a function call in a streamed response
81-
public struct FunctionCallDelta: Decodable, Sendable {
81+
public struct FunctionCallDelta: Codable, Sendable, Equatable {
8282
/// The name of the function (only present in first delta)
8383
public let name: String?
8484
/// The arguments being built up

Sources/CompilerSwiftAI/Model Calling/Domain Models/Message.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,24 @@ public struct Message: Sendable, Equatable {
1818
public enum Content: Sendable, Equatable {
1919
case text(String)
2020
case image(String)
21+
case toolCall(ToolCallDelta)
22+
case toolCallResult(String)
2123

2224
var type: ContentType {
2325
switch self {
2426
case .text: return .text
2527
case .image: return .imageUrl
28+
case .toolCall: return .text
29+
case .toolCallResult: return .text
2630
}
2731
}
2832

2933
var value: String {
3034
switch self {
3135
case .text(let text): return text
3236
case .image(let url): return url
37+
case .toolCall(let toolCall): return "Function call: \(toolCall.function?.name ?? "")"
38+
case .toolCallResult(let result): return result
3339
}
3440
}
3541
}

Sources/CompilerSwiftAI/Network Models/MessageDTO.swift

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
1-
// Copyright © 2025 Compiler, Inc. All rights reserved.
1+
// Copyright 2025 Compiler, Inc. All rights reserved.
22

33
import Foundation
44

55
public struct MessageDTO: Codable, Sendable {
66
public enum ContentType: String, Codable, Sendable {
77
case text = "text"
88
case imageUrl = "image_url"
9+
case toolCall = "tool_call"
10+
case toolCallResult = "tool_call_result"
911
}
1012

1113
public struct Content: Codable, Sendable {
1214
let type: ContentType
1315
let text: String?
1416
let imageUrl: ImageUrl?
17+
let toolCall: ToolCallDelta?
1518

1619
private enum CodingKeys: String, CodingKey {
1720
case type
1821
case text
1922
case imageUrl = "image_url"
23+
case toolCall = "tool_call"
2024
}
2125
}
2226

@@ -34,9 +38,13 @@ public struct MessageDTO: Codable, Sendable {
3438
self.content = message.content.map { content in
3539
switch content {
3640
case .text(let text):
37-
return Content(type: .text, text: text, imageUrl: nil)
41+
return Content(type: .text, text: text, imageUrl: nil, toolCall: nil)
3842
case .image(let url):
39-
return Content(type: .imageUrl, text: nil, imageUrl: ImageUrl(url: url))
43+
return Content(type: .imageUrl, text: nil, imageUrl: ImageUrl(url: url), toolCall: nil)
44+
case .toolCall(let delta):
45+
return Content(type: .toolCall, text: nil, imageUrl: nil, toolCall: delta)
46+
case .toolCallResult(let result):
47+
return Content(type: .toolCallResult, text: result, imageUrl: nil, toolCall: nil)
4048
}
4149
}
4250
}
@@ -46,12 +54,16 @@ public struct MessageDTO: Codable, Sendable {
4654
id: id,
4755
role: .init(rawValue: role) ?? .user,
4856
content: content.compactMap { content in
49-
if let text = content.text {
50-
return .text(text)
51-
} else if let imageUrl = content.imageUrl {
52-
return .image(imageUrl.url)
57+
switch content.type {
58+
case .text:
59+
return content.text.map { .text($0) }
60+
case .imageUrl:
61+
return content.imageUrl.map { .image($0.url) }
62+
case .toolCall:
63+
return content.toolCall.map { .toolCall($0) }
64+
case .toolCallResult:
65+
return content.text.map { .toolCallResult($0) }
5366
}
54-
return nil
5567
}
5668
)
5769
}

0 commit comments

Comments
 (0)