From f185b602ea2769502268be6c3b2703d01ed179e4 Mon Sep 17 00:00:00 2001 From: Mikhail Filimonov Date: Tue, 21 Apr 2026 20:29:39 +0100 Subject: [PATCH] ap update --- .../Messages/TelegramEngineMessages.swift | 12 ++++ .../TelegramEngine/Messages/Translate.swift | 59 ++++++++++++++++++- 2 files changed, 70 insertions(+), 1 deletion(-) diff --git a/submodules/TelegramCore/Sources/TelegramEngine/Messages/TelegramEngineMessages.swift b/submodules/TelegramCore/Sources/TelegramEngine/Messages/TelegramEngineMessages.swift index 41812db628..4b5cb00909 100644 --- a/submodules/TelegramCore/Sources/TelegramEngine/Messages/TelegramEngineMessages.swift +++ b/submodules/TelegramCore/Sources/TelegramEngine/Messages/TelegramEngineMessages.swift @@ -677,6 +677,18 @@ public extension TelegramEngine { return _internal_deleteAITextStyle(account: self.account, id: id, accessHash: accessHash) } + public func unsaveAITextStyle(id: Int64, accessHash: Int64) -> Signal { + return _internal_unsaveAITextStyle(account: self.account, id: id, accessHash: accessHash) + } + + public func getAIComposeToneExample(slug: String, num: Int32) -> Signal { + return _internal_getAIComposeToneExample(network: self.account.network, slug: slug, num: num) + } + + public func getAIComposeToneExample(reference: TelegramComposeAIMessageMode.CloudStyle.Reference, num: Int32) -> Signal { + return _internal_getAIComposeToneExample(network: self.account.network, tone: reference, num: num) + } + public func translate(texts: [(String, [MessageTextEntity])], toLang: String, tone: TranslationTone = .neutral) -> Signal<[(String, [MessageTextEntity])], TranslationError> { return _internal_translateTexts(network: self.account.network, texts: texts, toLang: toLang, tone: tone) } diff --git a/submodules/TelegramCore/Sources/TelegramEngine/Messages/Translate.swift b/submodules/TelegramCore/Sources/TelegramEngine/Messages/Translate.swift index ea5ba30b09..d62f64c8b2 100644 --- a/submodules/TelegramCore/Sources/TelegramEngine/Messages/Translate.swift +++ b/submodules/TelegramCore/Sources/TelegramEngine/Messages/Translate.swift @@ -800,6 +800,7 @@ func _internal_composeAIMessage(account: Account, text: TextWithEntities, mode: public enum CreateAITextStyleError { case generic + case premiumRequired } func _internal_createAITextStyle(account: Account, displayAuthor: Bool, emojiFileId: Int64, title: String, prompt: String) -> Signal { @@ -813,7 +814,10 @@ func _internal_createAITextStyle(account: Account, displayAuthor: Bool, emojiFil title: title, prompt: prompt )) - |> mapError { _ -> CreateAITextStyleError in + |> mapError { error -> CreateAITextStyleError in + if error.errorDescription == "TONES_SAVED_TOO_MANY" { + return .premiumRequired + } return .generic } |> mapToSignal { result -> Signal in @@ -865,6 +869,42 @@ func _internal_editAITextStyle(account: Account, id: Int64, accessHash: Int64, d } } +public struct TelegramAIComposeToneExample: Equatable { + public let from: TextWithEntities + public let to: TextWithEntities + public init(from: TextWithEntities, to: TextWithEntities) { + self.from = from + self.to = to + } +} + +private func _internal_mapToneExample(_ apiExample: Api.AiComposeToneExample) -> TelegramAIComposeToneExample { + switch apiExample { + case let .aiComposeToneExample(data): + return TelegramAIComposeToneExample(from: TextWithEntities(apiValue: data.from), to: TextWithEntities(apiValue: data.to)) + } +} + +func _internal_getAIComposeToneExample(network: Network, tone: TelegramComposeAIMessageMode.CloudStyle.Reference, num: Int32) -> Signal { + return network.request(Api.functions.aicompose.getToneExample(tone: tone.apiInputStyle, num: num)) + |> map { apiExample -> TelegramAIComposeToneExample? in + return _internal_mapToneExample(apiExample) + } + |> `catch` { _ -> Signal in + return .single(nil) + } +} + +func _internal_getAIComposeToneExample(network: Network, slug: String, num: Int32) -> Signal { + return network.request(Api.functions.aicompose.getToneExample(tone: .inputAiComposeToneSlug(Api.InputAiComposeTone.Cons_inputAiComposeToneSlug(slug: slug)), num: num)) + |> map { apiExample -> TelegramAIComposeToneExample? in + return _internal_mapToneExample(apiExample) + } + |> `catch` { _ -> Signal in + return .single(nil) + } +} + public enum DeleteAITextStyleError { case generic } @@ -886,6 +926,23 @@ func _internal_deleteAITextStyle(account: Account, id: Int64, accessHash: Int64) } } +func _internal_unsaveAITextStyle(account: Account, id: Int64, accessHash: Int64) -> Signal { + return account.network.request(Api.functions.aicompose.saveTone(tone: .inputAiComposeToneID(Api.InputAiComposeTone.Cons_inputAiComposeToneID(id: id, accessHash: accessHash)), unsave: .boolTrue)) + |> mapError { _ -> DeleteAITextStyleError in + return .generic + } + |> mapToSignal { _ -> Signal in + return account.postbox.transaction { transaction -> Void in + let styles = _internal_cachedCloudAITextStyles(transaction: transaction) + var items = styles?.items ?? [] + items.removeAll(where: { $0.id == .style(.custom(id)) }) + _internal_setCachedCloudAITextStyles(transaction: transaction, styles: CachedCloudAITextStyles(items: items, hash: 0)) + } + |> castError(DeleteAITextStyleError.self) + |> ignoreValues + } +} + final class CachedCloudAITextStyles: Codable { let items: [TelegramComposeAIMessageMode.CloudStyle] let hash: Int64