This repository was archived by the owner on Sep 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathJetpackAIServiceRemote.swift
More file actions
86 lines (75 loc) · 3.4 KB
/
Copy pathJetpackAIServiceRemote.swift
File metadata and controls
86 lines (75 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import Foundation
public final class JetpackAIServiceRemote: SiteServiceRemoteWordPressComREST {
/// Returns information about your current tier, requests limit, and more.
public func getAssistantFeatureDetails() async throws -> JetpackAssistantFeatureDetails {
let path = path(forEndpoint: "sites/\(siteID)/jetpack-ai/ai-assistant-feature", withVersion: ._2_0)
let response = await wordPressComRestApi.perform(.get, URLString: path, type: JetpackAssistantFeatureDetails.self)
return try response.get().body
}
/// Returns short-lived JWT token (lifetime is in minutes).
public func getAuthorizationToken() async throws -> String {
struct Response: Decodable {
let token: String
}
let path = path(forEndpoint: "sites/\(siteID)/jetpack-openai-query/jwt", withVersion: ._2_0)
let response = await wordPressComRestApi.perform(.post, URLString: path, type: Response.self)
return try response.get().body.token
}
/// - parameter token: Token retrieved using ``JetpackAIServiceRemote/getAuthorizationToken``.
public func transcribeAudio(from fileURL: URL, token: String) async throws -> String {
let path = path(forEndpoint: "jetpack-ai-transcription?feature=voice-to-content", withVersion: ._2_0)
let file = FilePart(parameterName: "audio_file", url: fileURL, fileName: "voice_recording", mimeType: "audio/m4a")
let result = await wordPressComRestApi.upload(URLString: path, httpHeaders: [
"Authorization": "Bearer \(token)"
], fileParts: [file])
guard let body = try result.get().body as? [String: Any],
let text = body["text"] as? String else {
throw URLError(.unknown)
}
return text
}
/// - parameter token: Token retrieved using ``JetpackAIServiceRemote/getAuthorizationToken``.
public func makePostContent(fromPlainText plainText: String, token: String) async throws -> String {
let path = path(forEndpoint: "jetpack-ai-query", withVersion: ._2_0)
let request = JetpackAIQueryRequest(messages: [
.init(role: "jetpack-ai", context: .init(type: "voice-to-content-simple-draft", content: plainText))
], feature: "voice-to-content", stream: false)
let builder = try wordPressComRestApi.requestBuilder(URLString: path)
.method(.post)
.headers(["Authorization": "Bearer \(token)"])
.body(json: request, jsonEncoder: JSONEncoder())
let result = await wordPressComRestApi.perform(request: builder) { data in
try JSONDecoder().decode(JetpackAIQueryResponse.self, from: data)
}
let response = try result.get().body
guard let content = response.choices.first?.message.content else {
throw URLError(.unknown)
}
return content
}
}
private struct JetpackAIQueryRequest: Encodable {
let messages: [Message]
let feature: String
let stream: Bool
struct Message: Encodable {
let role: String
let context: Context
}
struct Context: Codable {
let type: String
let content: String
}
}
private struct JetpackAIQueryResponse: Decodable {
let model: String?
let choices: [Choice]
struct Choice: Codable {
let index: Int
let message: Message
}
struct Message: Codable {
let role: String?
let content: String
}
}