Skip to content

Commit 1d3b1df

Browse files
committed
various helpers
1 parent f358d3e commit 1d3b1df

File tree

5 files changed

+45
-16
lines changed

5 files changed

+45
-16
lines changed

src/Conversation.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ import Foundation
6565
}
6666

6767
private let client: ResponsesAPI
68-
6968
@MainActor private var config: Config
7069

7170
/// Creates a new conversation.

src/Models/File.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,11 @@ public extension File.Upload {
9898
///
9999
/// - Parameter url: The URL of the file to upload.
100100
/// - Parameter name: The name of the file.
101-
static func url(_ url: URL, name: String? = nil) async throws -> File.Upload {
101+
/// - Parameter contentType: The mime type of the file.
102+
static func url(_ url: URL, name: String? = nil, contentType: String = "application/octet-stream") async throws -> File.Upload {
102103
let name = name ?? url.lastPathComponent == "/" ? "unknown_file" : url.lastPathComponent
103104

104-
return try File.Upload(name: name, contents: Data(contentsOf: url))
105+
return try File.Upload(name: name, contents: Data(contentsOf: url), contentType: contentType)
105106
}
106107

107108
/// Creates a file upload from the given data.

src/Models/Input.swift

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,32 +59,37 @@ import MetaCodable
5959
/// - Parameter imageUrl: The URL of the image to be sent to the model. A fully qualified URL or base64 encoded image in a data URL.
6060
@CodedAs("input_image") case image(
6161
detail: ImageDetail = .auto,
62-
fileId: String?,
63-
imageUrl: String?
62+
fileId: String? = nil,
63+
imageUrl: String? = nil
6464
)
6565

6666
/// A file input to the model.
6767
/// - Parameter fileData: The content of the file to be sent to the model.
6868
/// - Parameter fileId: The ID of the file to be sent to the model.
6969
/// - Parameter filename: The name of the file to be sent to the model.
7070
@CodedAs("input_file") case file(
71-
fileData: String?,
72-
fileId: String?,
73-
filename: String?
71+
fileData: String? = nil,
72+
fileId: String? = nil,
73+
filename: String? = nil
7474
)
7575
}
7676

7777
/// A text input to the model, equivalent to a text input.
7878
case text(String)
79+
7980
/// A list of one or many content items to the model, containing different content types.
8081
case list([ContentItem])
81-
}
8282

83-
/// A text input to the model, equivalent to a text input with the user role.
84-
case text(String)
83+
/// Creates a new text input to the model.
84+
public init(_ text: String) {
85+
self = .text(text)
86+
}
8587

86-
/// A list of one or many input items to the model, containing different content types.
87-
case list([ListItem])
88+
/// Creates a new input to the model with a list of items.
89+
public init(_ items: [ContentItem]) {
90+
self = .list(items)
91+
}
92+
}
8893

8994
/// The messages contained in the input.
9095
public var messages: [Message] {
@@ -113,6 +118,22 @@ import MetaCodable
113118
}.joined(separator: " ")
114119
}
115120
}
121+
122+
/// A text input to the model, equivalent to a text input with the user role.
123+
case text(String)
124+
125+
/// A list of one or many input items to the model, containing different content types.
126+
case list([ListItem])
127+
128+
/// Creates a new text input to the model.
129+
public init(_ text: String) {
130+
self = .text(text)
131+
}
132+
133+
/// Creates a new input to the model with a list of items.
134+
public init(_ items: [ListItem]) {
135+
self = .list(items)
136+
}
116137
}
117138

118139
// MARK: - Text helpers

src/Models/Message.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,14 @@ public enum Message: Equatable, Hashable, Sendable {
117117
}
118118
}
119119

120+
/// The unique ID of the message, if available.
121+
public var id: String? {
122+
switch self {
123+
case .input: return nil
124+
case let .output(output): return output.id
125+
}
126+
}
127+
120128
/// The text content of the input.
121129
///
122130
/// > Note: This property does not include reasoning text.

src/Models/Tool.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -455,10 +455,10 @@ public extension Tool {
455455
/// This tool searches the web for relevant results to use in a response.
456456
///
457457
/// Learn more about the [web search tool](https://platform.openai.com/docs/guides/tools-web-search?api-mode=responses).
458-
/// - Parameter searchContextSize: High level guidance for the amount of context window space to use for the search.
458+
/// - Parameter contextSize: High level guidance for the amount of context window space to use for the search.
459459
/// - Parameter userLocation: Approximate location parameters for the search.
460-
static func webSearch(searchContextSize: WebSearch.ContextSize = .medium, userLocation: WebSearch.UserLocation? = nil) -> Self {
461-
.webSearch(WebSearch(searchContextSize: searchContextSize, userLocation: userLocation))
460+
static func webSearch(contextSize: WebSearch.ContextSize = .medium, userLocation: WebSearch.UserLocation? = nil) -> Self {
461+
.webSearch(WebSearch(searchContextSize: contextSize, userLocation: userLocation))
462462
}
463463
}
464464

0 commit comments

Comments
 (0)