forked from github/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunctionMessage.swift
More file actions
163 lines (145 loc) · 6.34 KB
/
Copy pathFunctionMessage.swift
File metadata and controls
163 lines (145 loc) · 6.34 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import Foundation
import SwiftUI
import ChatService
import SharedUIComponents
import ComposableArchitecture
import ChatTab
import GitHubCopilotService
struct FunctionMessage: View {
let text: String
let chat: StoreOf<Chat>
@AppStorage(\.chatFontSize) var chatFontSize
@Environment(\.openURL) private var openURL
private var isFreePlanUser: Bool {
text.contains("30-day free trial")
}
private var isOrgUser: Bool {
text.contains("reach out to your organization's Copilot admin")
}
private var isBYOKUser: Bool {
text.contains("You've reached your quota limit for your BYOK model")
}
private var isTBBMessage: Bool {
text.contains("AI Credits") || text.contains("additional overages")
}
private var switchToFallbackModelText: String {
guard !isTBBMessage else { return "" }
if let fallbackModelName = CopilotModelManager.getFallbackLLM(
scope: chat.isAgentMode ? .agentPanel : .chatPanel
)?.modelName {
return "We have automatically switched you to \(fallbackModelName) which is included with your plan."
}
return ""
}
private var quotaActionButtons: [(title: String, urlString: String, isProminent: Bool)] {
let lower = text.lowercased()
let hasEnableOverage = lower.contains("enable additional overages")
let hasIncreaseBudget = lower.contains("increase budget")
let hasOverage = hasEnableOverage || hasIncreaseBudget
var buttons: [(String, String, Bool)] = []
if hasEnableOverage {
buttons.append(("Enable Additional Overage", "https://aka.ms/github-copilot-manage-overage", true))
}
if hasIncreaseBudget {
buttons.append(("Increase Budget", "https://aka.ms/github-copilot-manage-overage", true))
}
if lower.contains("upgrade your plan") {
buttons.append(("Upgrade Plan", "https://aka.ms/github-copilot-upgrade-plan", !hasOverage))
}
return buttons
}
private var errorContent: Text {
switch (isFreePlanUser, isOrgUser, isBYOKUser) {
case (true, _, _):
return Text("Monthly message limit reached. Upgrade to Copilot Pro (30-day free trial) or wait for your limit to reset.")
case (_, true, _):
let parts = [
"You have exceeded your free request allowance.",
switchToFallbackModelText,
"To enable additional paid premium requests, contact your organization admin."
].filter { !$0.isEmpty }
return Text(attributedString(from: parts))
case (_, _, true):
let sentences = splitBYOKQuotaMessage(text)
guard sentences.count == 2 else { fallthrough }
let parts = [sentences[0], switchToFallbackModelText, sentences[1]].filter { !$0.isEmpty }
return Text(attributedString(from: parts))
default:
let parts = [text, switchToFallbackModelText].filter { !$0.isEmpty }
return Text(attributedString(from: parts))
}
}
private func attributedString(from parts: [String]) -> AttributedString {
do {
return try AttributedString(markdown: parts.joined(separator: " "))
} catch {
return AttributedString(parts.joined(separator: " "))
}
}
private func splitBYOKQuotaMessage(_ message: String) -> [String] {
// Fast path: find the first period followed by a space + capital P (for "Please")
let boundary = ". Please check with"
if let range = message.range(of: boundary) {
// First sentence ends at the period just before " Please"
let firstSentence = String(message[..<range.lowerBound]) + "."
// Second sentence starts at "Please check with ..."
let secondSentenceStart = message.index(range.lowerBound, offsetBy: 2) // skip ". "
let secondSentence = String(message[secondSentenceStart...]).trimmingCharacters(in: .whitespacesAndNewlines)
return [firstSentence, secondSentence]
}
return [message]
}
var body: some View {
NotificationBanner(style: .warning) {
errorContent
if isFreePlanUser {
Button("Update to Copilot Pro") {
if let url = URL(string: "https://aka.ms/github-copilot-upgrade-plan") {
openURL(url)
}
}
.buttonStyle(.borderedProminent)
.controlSize(.regular)
.scaledFont(.body)
.onHover { isHovering in
if isHovering {
NSCursor.pointingHand.push()
} else {
NSCursor.pop()
}
}
}
if !quotaActionButtons.isEmpty {
HStack(spacing: 8) {
ForEach(quotaActionButtons, id: \.title) { button in
Group {
if button.isProminent {
Button(button.title) {
if let url = URL(string: button.urlString) { openURL(url) }
}.buttonStyle(.borderedProminent)
} else {
Button(button.title) {
if let url = URL(string: button.urlString) { openURL(url) }
}.buttonStyle(.bordered)
}
}
.controlSize(.regular)
.scaledFont(.body)
.onHover { if $0 { NSCursor.pointingHand.push() } else { NSCursor.pop() } }
}
}
}
}
}
}
struct FunctionMessage_Previews: PreviewProvider {
static var previews: some View {
let chatTabInfo = ChatTabInfo(id: "id", workspacePath: "path", username: "name")
FunctionMessage(
text: "You've reached your monthly chat limit. Upgrade to Copilot Pro (30-day free trial) or wait until 1/17/2025, 8:00:00 AM for your limit to reset.",
chat: .init(initialState: .init(), reducer: { Chat(service: ChatService.service(for: chatTabInfo)) })
)
.padding()
.fixedSize()
}
}