Skip to content

Commit 98a3fbe

Browse files
committed
init
0 parents  commit 98a3fbe

File tree

13 files changed

+1341
-0
lines changed

13 files changed

+1341
-0
lines changed

.github/workflows/dependencies.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: Swift Dependency Submission
2+
on:
3+
push:
4+
branches:
5+
- main
6+
7+
permissions:
8+
contents: write
9+
10+
jobs:
11+
swift-action-detection:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Checkout Repository
15+
uses: actions/checkout@v4
16+
17+
- name: Install Swift
18+
uses: vapor/[email protected]
19+
with:
20+
toolchain: latest
21+
22+
- name: Submit Dependencies
23+
uses: vapor-community/[email protected]

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.DS_Store
2+
/.build
3+
/Packages
4+
xcuserdata/
5+
DerivedData/
6+
.swiftpm/configuration/registries.json
7+
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
8+
.netrc
9+
Package.resolved

.spi.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
version: 1
2+
builder:
3+
configs:
4+
- documentation_targets: [OpenAI]

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) Miguel Piedrafita
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Package.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// swift-tools-version: 6.0
2+
3+
import PackageDescription
4+
5+
let package = Package(
6+
name: "OpenAI",
7+
platforms: [
8+
.iOS(.v13),
9+
.tvOS(.v13),
10+
.macOS(.v13),
11+
.watchOS(.v6),
12+
.visionOS(.v1)
13+
],
14+
products: [
15+
.library(name: "OpenAI", targets: ["OpenAI"]),
16+
],
17+
targets: [
18+
.target(name: "OpenAI", path: "./src"),
19+
]
20+
)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import Foundation
2+
3+
extension AsyncThrowingStream.Continuation where Failure == any Error {
4+
func yield(error: Failure) {
5+
yield(with: Result.failure(error))
6+
}
7+
}

src/Models/ClientEvent.swift

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
2+
import Foundation
3+
4+
public enum ClientEvent {
5+
public struct SessionUpdateEvent: Encodable {
6+
/// Optional client-generated ID used to identify this event.
7+
var event_id: String?
8+
/// Session configuration to update.
9+
var session: Session
10+
11+
private let type = "session.update"
12+
}
13+
14+
public struct InputAudioBufferAppendEvent: Encodable {
15+
/// Optional client-generated ID used to identify this event.
16+
var event_id: String?
17+
/// Base64-encoded audio bytes.
18+
var audio: String
19+
20+
private let type = "input_audio_buffer.append"
21+
}
22+
23+
public struct InputAudioBufferCommitEvent: Encodable {
24+
/// Optional client-generated ID used to identify this event.
25+
var event_id: String?
26+
27+
private let type = "input_audio_buffer.commit"
28+
}
29+
30+
public struct InputAudioBufferClearEvent: Encodable {
31+
/// Optional client-generated ID used to identify this event.
32+
var event_id: String?
33+
34+
private let type = "input_audio_buffer.clear"
35+
}
36+
37+
public struct ConversationItemCreateEvent: Encodable {
38+
/// Optional client-generated ID used to identify this event.
39+
var event_id: String?
40+
/// The ID of the preceding item after which the new item will be inserted.
41+
var previous_item_id: String?
42+
/// The item to add to the conversation.
43+
var item: Item
44+
45+
private let type = "conversation.item.create"
46+
}
47+
48+
public struct ConversationItemTruncateEvent: Encodable {
49+
/// Optional client-generated ID used to identify this event.
50+
var event_id: String?
51+
/// The ID of the assistant message item to truncate.
52+
var item_id: String?
53+
/// The index of the content part to truncate.
54+
var content_index: Int
55+
/// Inclusive duration up to which audio is truncated, in milliseconds.
56+
var audio_end_ms: Int
57+
58+
private let type = "conversation.item.truncate"
59+
}
60+
61+
public struct ConversationItemDeleteEvent: Encodable {
62+
/// Optional client-generated ID used to identify this event.
63+
var event_id: String?
64+
/// The ID of the assistant message item to truncate.
65+
var item_id: String?
66+
/// The index of the content part to truncate.
67+
var content_index: Int
68+
/// Inclusive duration up to which audio is truncated, in milliseconds.
69+
var audio_end_ms: Int
70+
71+
private let type = "conversation.item.delete"
72+
}
73+
74+
public struct ResponseCreateEvent: Encodable {
75+
/// Optional client-generated ID used to identify this event.
76+
var event_id: String?
77+
/// Configuration for the response.
78+
var response: Response.Config?
79+
80+
private let type = "response.create"
81+
}
82+
83+
public struct ResponseCancelEvent: Encodable {
84+
/// Optional client-generated ID used to identify this event.
85+
var event_id: String?
86+
87+
private let type = "response.cancel"
88+
}
89+
90+
/// Send this event to update the session’s default configuration.
91+
case updateSession(SessionUpdateEvent)
92+
/// Send this event to append audio bytes to the input audio buffer.
93+
case appendInputAudioBuffer(InputAudioBufferAppendEvent)
94+
/// Send this event to commit audio bytes to a user message.
95+
case commitInputAudioBuffer(InputAudioBufferCommitEvent)
96+
/// Send this event to clear the audio bytes in the buffer.
97+
case clearInputAudioBuffer(InputAudioBufferClearEvent)
98+
/// Send this event when adding an item to the conversation.
99+
case createConversationItem(ConversationItemCreateEvent)
100+
/// Send this event when you want to truncate a previous assistant message’s audio.
101+
case truncateConversationItem(ConversationItemTruncateEvent)
102+
/// Send this event when you want to remove any item from the conversation history.
103+
case deleteConversationItem(ConversationItemDeleteEvent)
104+
/// Send this event to trigger a response generation.
105+
case createResponse(ResponseCreateEvent)
106+
/// Send this event to cancel an in-progress response.
107+
case cancelResponse(ResponseCancelEvent)
108+
}
109+
110+
extension ClientEvent {
111+
public static func updateSession(id: String? = nil, _ session: Session) -> Self {
112+
.updateSession(SessionUpdateEvent(event_id: id, session: session))
113+
}
114+
115+
public static func appendInputAudioBuffer(id: String? = nil, encoding audio: Data) -> Self {
116+
.appendInputAudioBuffer(InputAudioBufferAppendEvent(event_id: id, audio: audio.base64EncodedString()))
117+
}
118+
119+
public static func commitInputAudioBuffer(id: String? = nil) -> Self {
120+
.commitInputAudioBuffer(InputAudioBufferCommitEvent(event_id: id))
121+
}
122+
123+
public static func clearInputAudioBuffer(id: String? = nil) -> Self {
124+
.clearInputAudioBuffer(InputAudioBufferClearEvent(event_id: id))
125+
}
126+
127+
public static func createConversationItem(id: String? = nil, previous previousID: String? = nil, _ item: Item) -> Self {
128+
.createConversationItem(ConversationItemCreateEvent(event_id: id, previous_item_id: previousID, item: item))
129+
}
130+
131+
public static func truncateConversationItem(id event_id: String? = nil, for id: String? = nil, at index: Int, at_audio audio_index: Int) -> Self {
132+
.truncateConversationItem(ConversationItemTruncateEvent(event_id: event_id, item_id: id, content_index: index, audio_end_ms: audio_index))
133+
}
134+
135+
public static func deleteConversationItem(id event_id: String? = nil, for id: String? = nil, at index: Int, at_audio audio_index: Int) -> Self {
136+
.deleteConversationItem(ConversationItemDeleteEvent(event_id: event_id, item_id: id, content_index: index, audio_end_ms: audio_index))
137+
}
138+
139+
public static func createResponse(id: String? = nil, _ response: Response.Config? = nil) -> Self {
140+
.createResponse(ResponseCreateEvent(event_id: id, response: response))
141+
}
142+
143+
public static func cancelResponse(id: String? = nil) -> Self {
144+
.cancelResponse(ResponseCancelEvent(event_id: id))
145+
}
146+
}
147+
148+
extension ClientEvent: Encodable {
149+
private enum CodingKeys: String, CodingKey {
150+
case type
151+
}
152+
153+
public func encode(to encoder: Encoder) throws {
154+
switch self {
155+
case let .updateSession(event):
156+
try event.encode(to: encoder)
157+
case let .appendInputAudioBuffer(event):
158+
try event.encode(to: encoder)
159+
case let .commitInputAudioBuffer(event):
160+
try event.encode(to: encoder)
161+
case let .clearInputAudioBuffer(event):
162+
try event.encode(to: encoder)
163+
case let .createConversationItem(event):
164+
try event.encode(to: encoder)
165+
case let .truncateConversationItem(event):
166+
try event.encode(to: encoder)
167+
case let .deleteConversationItem(event):
168+
try event.encode(to: encoder)
169+
case let .createResponse(event):
170+
try event.encode(to: encoder)
171+
case let .cancelResponse(event):
172+
try event.encode(to: encoder)
173+
}
174+
}
175+
}

0 commit comments

Comments
 (0)