Skip to content

Commit

Permalink
Add makePostContent(fromPlainText
Browse files Browse the repository at this point in the history
  • Loading branch information
kean committed May 14, 2024
1 parent 00b651e commit 7aea18e
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
6 changes: 3 additions & 3 deletions Sources/CoreAPI/WordPressComRestApi.swift
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ open class WordPressComRestApi: NSObject {
return "\(String(describing: oAuthToken)),\(String(describing: userAgent))".hashValue
}

private func requestBuilder(URLString: String) throws -> HTTPRequestBuilder {
func requestBuilder(URLString: String) throws -> HTTPRequestBuilder {
guard let url = URL(string: URLString, relativeTo: baseURL) else {
throw URLError(.badURL)
}
Expand Down Expand Up @@ -414,9 +414,9 @@ open class WordPressComRestApi: NSObject {
return await perform(request: builder, fulfilling: progress, decoder: decoder)
}

private func perform<T>(
func perform<T>(
request: HTTPRequestBuilder,
fulfilling progress: Progress?,
fulfilling progress: Progress? = nil,
decoder: @escaping (Data) throws -> T,
taskCreated: ((Int) -> Void)? = nil,
session: URLSession? = nil
Expand Down
50 changes: 50 additions & 0 deletions Sources/WordPressKit/Services/JetpackAIServiceRemote.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,54 @@ public final class JetpackAIServiceRemote: SiteServiceRemoteWordPressComREST {
}
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

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
}
}

0 comments on commit 7aea18e

Please sign in to comment.