This commit is contained in:
@@ -1,4 +1,7 @@
|
|||||||
import SwiftUI
|
import SwiftUI
|
||||||
|
#if os(iOS)
|
||||||
|
import UIKit
|
||||||
|
#endif
|
||||||
|
|
||||||
struct SybilPromptField: View {
|
struct SybilPromptField: View {
|
||||||
var placeholder: String = "Enter Prompt"
|
var placeholder: String = "Enter Prompt"
|
||||||
@@ -6,9 +9,50 @@ struct SybilPromptField: View {
|
|||||||
@FocusState.Binding var focused: Bool
|
@FocusState.Binding var focused: Bool
|
||||||
var selection: Binding<TextSelection?>? = nil
|
var selection: Binding<TextSelection?>? = nil
|
||||||
var axis: Axis = .vertical
|
var axis: Axis = .vertical
|
||||||
|
var returnSubmits = false
|
||||||
var submit: () -> Void
|
var submit: () -> Void
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
|
Group {
|
||||||
|
#if os(iOS) && !targetEnvironment(macCatalyst)
|
||||||
|
if returnSubmits {
|
||||||
|
SybilReturnSubmittingTextView(
|
||||||
|
placeholder: placeholder,
|
||||||
|
text: $text,
|
||||||
|
focused: $focused,
|
||||||
|
selection: selection,
|
||||||
|
submit: submit
|
||||||
|
)
|
||||||
|
.overlay(alignment: .topLeading) {
|
||||||
|
if text.isEmpty {
|
||||||
|
Text(placeholder)
|
||||||
|
.font(.sybil(.body))
|
||||||
|
.foregroundStyle(SybilTheme.textMuted)
|
||||||
|
.padding(.horizontal, 12)
|
||||||
|
.padding(.vertical, 10)
|
||||||
|
.allowsHitTesting(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
swiftUITextField
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
swiftUITextField
|
||||||
|
.submitOnMacReturn(submit)
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
.padding(.horizontal, 12)
|
||||||
|
.padding(.vertical, 10)
|
||||||
|
.background(
|
||||||
|
RoundedRectangle(cornerRadius: 12)
|
||||||
|
.fill(SybilTheme.composerGradient)
|
||||||
|
.opacity(0.98)
|
||||||
|
)
|
||||||
|
.foregroundStyle(SybilTheme.text)
|
||||||
|
}
|
||||||
|
|
||||||
|
@ViewBuilder
|
||||||
|
private var swiftUITextField: some View {
|
||||||
Group {
|
Group {
|
||||||
if let selection {
|
if let selection {
|
||||||
TextField(placeholder, text: $text, selection: selection, axis: axis)
|
TextField(placeholder, text: $text, selection: selection, axis: axis)
|
||||||
@@ -23,18 +67,127 @@ struct SybilPromptField: View {
|
|||||||
.lineLimit(1 ... 6)
|
.lineLimit(1 ... 6)
|
||||||
.submitLabel(.send)
|
.submitLabel(.send)
|
||||||
.onSubmit(submit)
|
.onSubmit(submit)
|
||||||
.submitOnMacReturn(submit)
|
|
||||||
.padding(.horizontal, 12)
|
|
||||||
.padding(.vertical, 10)
|
|
||||||
.background(
|
|
||||||
RoundedRectangle(cornerRadius: 12)
|
|
||||||
.fill(SybilTheme.composerGradient)
|
|
||||||
.opacity(0.98)
|
|
||||||
)
|
|
||||||
.foregroundStyle(SybilTheme.text)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if os(iOS) && !targetEnvironment(macCatalyst)
|
||||||
|
private final class ReturnSubmittingUITextView: UITextView {
|
||||||
|
var shouldFocus = false
|
||||||
|
|
||||||
|
override func didMoveToWindow() {
|
||||||
|
super.didMoveToWindow()
|
||||||
|
if shouldFocus, window != nil, !isFirstResponder {
|
||||||
|
becomeFirstResponder()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private struct SybilReturnSubmittingTextView: UIViewRepresentable {
|
||||||
|
var placeholder: String
|
||||||
|
@Binding var text: String
|
||||||
|
@FocusState.Binding var focused: Bool
|
||||||
|
var selection: Binding<TextSelection?>?
|
||||||
|
var submit: () -> Void
|
||||||
|
|
||||||
|
func makeCoordinator() -> Coordinator {
|
||||||
|
Coordinator(parent: self)
|
||||||
|
}
|
||||||
|
|
||||||
|
func makeUIView(context: Context) -> ReturnSubmittingUITextView {
|
||||||
|
let textView = ReturnSubmittingUITextView()
|
||||||
|
SybilFontRegistry.registerIfNeeded()
|
||||||
|
textView.delegate = context.coordinator
|
||||||
|
textView.backgroundColor = .clear
|
||||||
|
textView.textColor = UIColor(SybilTheme.text)
|
||||||
|
let baseFont = UIFont(name: "Inter", size: 17) ?? .preferredFont(forTextStyle: .body)
|
||||||
|
textView.font = UIFontMetrics(forTextStyle: .body).scaledFont(for: baseFont)
|
||||||
|
textView.textContainerInset = .zero
|
||||||
|
textView.textContainer.lineFragmentPadding = 0
|
||||||
|
textView.isScrollEnabled = true
|
||||||
|
textView.returnKeyType = .send
|
||||||
|
textView.enablesReturnKeyAutomatically = false
|
||||||
|
textView.autocapitalizationType = .sentences
|
||||||
|
textView.autocorrectionType = .yes
|
||||||
|
textView.accessibilityLabel = placeholder
|
||||||
|
textView.accessibilityIdentifier = "quick-question-prompt"
|
||||||
|
return textView
|
||||||
|
}
|
||||||
|
|
||||||
|
func updateUIView(_ textView: ReturnSubmittingUITextView, context: Context) {
|
||||||
|
context.coordinator.parent = self
|
||||||
|
textView.shouldFocus = focused
|
||||||
|
if textView.text != text {
|
||||||
|
textView.text = text
|
||||||
|
}
|
||||||
|
if textView.isFirstResponder != focused {
|
||||||
|
if focused {
|
||||||
|
textView.becomeFirstResponder()
|
||||||
|
} else {
|
||||||
|
textView.resignFirstResponder()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if let selection,
|
||||||
|
let value = selection.wrappedValue,
|
||||||
|
let selectedRange = nsRange(for: value, in: text) {
|
||||||
|
if textView.selectedRange != selectedRange {
|
||||||
|
textView.selectedRange = selectedRange
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private func nsRange(for selection: TextSelection, in text: String) -> NSRange? {
|
||||||
|
switch selection.indices {
|
||||||
|
case .selection(let range):
|
||||||
|
return NSRange(range, in: text)
|
||||||
|
case .multiSelection:
|
||||||
|
return nil
|
||||||
|
@unknown default:
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func sizeThatFits(_ proposal: ProposedViewSize, uiView: UITextView, context: Context) -> CGSize? {
|
||||||
|
guard let width = proposal.width, width.isFinite, width > 0 else { return nil }
|
||||||
|
let measured = uiView.sizeThatFits(CGSize(width: width, height: .greatestFiniteMagnitude)).height
|
||||||
|
let lineHeight = uiView.font?.lineHeight ?? 21
|
||||||
|
return CGSize(width: width, height: min(max(lineHeight, measured), lineHeight * 6))
|
||||||
|
}
|
||||||
|
|
||||||
|
final class Coordinator: NSObject, UITextViewDelegate {
|
||||||
|
var parent: SybilReturnSubmittingTextView
|
||||||
|
|
||||||
|
init(parent: SybilReturnSubmittingTextView) {
|
||||||
|
self.parent = parent
|
||||||
|
}
|
||||||
|
|
||||||
|
func textViewDidChange(_ textView: UITextView) {
|
||||||
|
parent.text = textView.text
|
||||||
|
}
|
||||||
|
|
||||||
|
func textViewDidBeginEditing(_ textView: UITextView) {
|
||||||
|
parent.focused = true
|
||||||
|
}
|
||||||
|
|
||||||
|
func textViewDidEndEditing(_ textView: UITextView) {
|
||||||
|
parent.focused = false
|
||||||
|
}
|
||||||
|
|
||||||
|
func textViewDidChangeSelection(_ textView: UITextView) {
|
||||||
|
guard let binding = parent.selection,
|
||||||
|
let range = Range(textView.selectedRange, in: textView.text) else { return }
|
||||||
|
binding.wrappedValue = TextSelection(range: range)
|
||||||
|
}
|
||||||
|
|
||||||
|
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText replacement: String) -> Bool {
|
||||||
|
guard replacement == "\n" else { return true }
|
||||||
|
guard textView.markedTextRange == nil else { return true }
|
||||||
|
parent.submit()
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
struct SybilPromptSendButton: View {
|
struct SybilPromptSendButton: View {
|
||||||
var systemImage: String = "arrow.up"
|
var systemImage: String = "arrow.up"
|
||||||
var canSend: Bool
|
var canSend: Bool
|
||||||
|
|||||||
@@ -136,6 +136,7 @@ struct SybilQuickQuestionView: View {
|
|||||||
focused: $promptFocused,
|
focused: $promptFocused,
|
||||||
selection: $promptSelection,
|
selection: $promptSelection,
|
||||||
axis: .vertical,
|
axis: .vertical,
|
||||||
|
returnSubmits: true,
|
||||||
submit: submitQuestion
|
submit: submitQuestion
|
||||||
)
|
)
|
||||||
.accessibilityIdentifier("quick-question-prompt")
|
.accessibilityIdentifier("quick-question-prompt")
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ from aiohttp import web
|
|||||||
sockets = set()
|
sockets = set()
|
||||||
messages = []
|
messages = []
|
||||||
requests = {}
|
requests = {}
|
||||||
|
quick_questions = []
|
||||||
read_cursor = 0
|
read_cursor = 0
|
||||||
cursor = 0
|
cursor = 0
|
||||||
busy = False
|
busy = False
|
||||||
@@ -108,7 +109,7 @@ async def route(request):
|
|||||||
for task in reply_tasks:
|
for task in reply_tasks:
|
||||||
task.cancel()
|
task.cancel()
|
||||||
reply_tasks.clear()
|
reply_tasks.clear()
|
||||||
messages.clear(); requests.clear(); attempts.clear(); cursor = 0; read_cursor = 0; busy = False
|
messages.clear(); requests.clear(); quick_questions.clear(); attempts.clear(); cursor = 0; read_cursor = 0; busy = False
|
||||||
attachment_capability = True; next_upload_error = None; blocked_proxy = False; transcript_comparison = False
|
attachment_capability = True; next_upload_error = None; blocked_proxy = False; transcript_comparison = False
|
||||||
assistant_image_capability = False; fail_image_download = False
|
assistant_image_capability = False; fail_image_download = False
|
||||||
push_installations.clear(); push_retired.clear(); push_requests.clear(); log.clear()
|
push_installations.clear(); push_retired.clear(); push_requests.clear(); log.clear()
|
||||||
@@ -177,7 +178,7 @@ async def route(request):
|
|||||||
await socket.close(code=1012)
|
await socket.close(code=1012)
|
||||||
elif action == 'malformed':
|
elif action == 'malformed':
|
||||||
await broadcast(dict(type='messages', conversationId='fixture-root', messages=[{'id': 'broken'}], cursor='999999', beforeCursor=None, hasMore=False))
|
await broadcast(dict(type='messages', conversationId='fixture-root', messages=[{'id': 'broken'}], cursor='999999', beforeCursor=None, hasMore=False))
|
||||||
return web.json_response(dict(cursor=cursor, readCursor=read_cursor, messages=messages, requests=requests, log=log,
|
return web.json_response(dict(cursor=cursor, readCursor=read_cursor, messages=messages, requests=requests, quickQuestions=quick_questions, log=log,
|
||||||
pushRequests=push_requests, pushActive=len(push_installations), pushRetired=len(push_retired)))
|
pushRequests=push_requests, pushActive=len(push_installations), pushRetired=len(push_retired)))
|
||||||
if path == '/api/v1/auth/session':
|
if path == '/api/v1/auth/session':
|
||||||
if auth_required and request.headers.get('Authorization') != 'Bearer fixture-token':
|
if auth_required and request.headers.get('Authorization') != 'Bearer fixture-token':
|
||||||
@@ -295,6 +296,12 @@ async def route(request):
|
|||||||
if request.method == 'PUT':
|
if request.method == 'PUT':
|
||||||
read_cursor = max(read_cursor, int((await request.json())['cursor']))
|
read_cursor = max(read_cursor, int((await request.json())['cursor']))
|
||||||
return web.json_response(dict(cursor=str(read_cursor), updatedAt=stamp(), sourceId=PUSH_SOURCE))
|
return web.json_response(dict(cursor=str(read_cursor), updatedAt=stamp(), sourceId=PUSH_SOURCE))
|
||||||
|
if path == '/api/v1/quick-questions/stream' and request.method == 'POST':
|
||||||
|
quick_questions.append(await request.json())
|
||||||
|
return web.Response(
|
||||||
|
text='event: done\ndata: {"text":"Fixture answer"}\n\n',
|
||||||
|
content_type='text/event-stream',
|
||||||
|
)
|
||||||
if path == '/api/v1/agent/messages':
|
if path == '/api/v1/agent/messages':
|
||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
body = await request.json()
|
body = await request.json()
|
||||||
|
|||||||
@@ -49,6 +49,23 @@ final class QuickQuestionSelectionUITests: XCTestCase {
|
|||||||
|
|
||||||
func testQuickQuestionComposerStartsCompactAndGrowsForMultilineText() {
|
func testQuickQuestionComposerStartsCompactAndGrowsForMultilineText() {
|
||||||
app.launch()
|
app.launch()
|
||||||
|
let agentButton = app.buttons["agent-sidebar"]
|
||||||
|
if !agentButton.waitForExistence(timeout: 2) || !agentButton.isHittable {
|
||||||
|
let showSidebar = app.buttons["Show sidebar"]
|
||||||
|
XCTAssertTrue(showSidebar.waitForExistence(timeout: 15))
|
||||||
|
showSidebar.tap()
|
||||||
|
}
|
||||||
|
XCTAssertTrue(agentButton.waitForExistence(timeout: 5))
|
||||||
|
agentButton.tap()
|
||||||
|
|
||||||
|
let normalField = app.textFields["agent-composer"]
|
||||||
|
let normalView = app.textViews["agent-composer"]
|
||||||
|
XCTAssertTrue(normalField.waitForExistence(timeout: 5) || normalView.waitForExistence(timeout: 5))
|
||||||
|
let normalComposer = normalField.exists ? normalField : normalView
|
||||||
|
let normalHeight = normalComposer.frame.height
|
||||||
|
XCTAssertLessThan(normalHeight, 80, "The normal composer should start at one line.")
|
||||||
|
|
||||||
|
XCUIDevice.shared.press(.home)
|
||||||
openQuickQuestionFromHomeScreen()
|
openQuickQuestionFromHomeScreen()
|
||||||
|
|
||||||
let prompt = quickQuestionPromptElement()
|
let prompt = quickQuestionPromptElement()
|
||||||
@@ -56,6 +73,7 @@ final class QuickQuestionSelectionUITests: XCTestCase {
|
|||||||
XCTAssertTrue(app.keyboards.firstMatch.waitForExistence(timeout: 10))
|
XCTAssertTrue(app.keyboards.firstMatch.waitForExistence(timeout: 10))
|
||||||
let oneLineHeight = prompt.frame.height
|
let oneLineHeight = prompt.frame.height
|
||||||
XCTAssertLessThan(oneLineHeight, 80, "The Quick Question field must not fill the sheet when empty.")
|
XCTAssertLessThan(oneLineHeight, 80, "The Quick Question field must not fill the sheet when empty.")
|
||||||
|
XCTAssertEqual(oneLineHeight, normalHeight, accuracy: 4, "The six-line Return field must keep the normal composer’s compact first line.")
|
||||||
captureFrame("quick-question-empty-keyboard-visible", element: prompt)
|
captureFrame("quick-question-empty-keyboard-visible", element: prompt)
|
||||||
capture("quick-question-empty-keyboard-visible")
|
capture("quick-question-empty-keyboard-visible")
|
||||||
|
|
||||||
@@ -128,6 +146,63 @@ final class QuickQuestionSelectionUITests: XCTestCase {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func testQuickQuestionReturnSubmitsPromptExactlyOnceWithoutNewline() async throws {
|
||||||
|
app.launch()
|
||||||
|
openQuickQuestionFromHomeScreen()
|
||||||
|
|
||||||
|
let prompt = quickQuestionPromptElement()
|
||||||
|
XCTAssertTrue(prompt.waitForExistence(timeout: 10))
|
||||||
|
XCTAssertTrue(app.keyboards.firstMatch.waitForExistence(timeout: 10))
|
||||||
|
prompt.typeText("Return submits once\n")
|
||||||
|
|
||||||
|
XCTAssertTrue(app.staticTexts["Fixture answer"].waitForExistence(timeout: 10))
|
||||||
|
XCTAssertEqual(prompt.value as? String, "Return submits once", "Return must submit without adding a newline.")
|
||||||
|
let state = try await control(["action": "state"])
|
||||||
|
let submitted = try XCTUnwrap(state["quickQuestions"] as? [[String: Any]])
|
||||||
|
XCTAssertEqual(submitted.count, 1, "One Return press must submit exactly one quick question.")
|
||||||
|
XCTAssertEqual(submitted.first?["question"] as? String, "Return submits once")
|
||||||
|
}
|
||||||
|
|
||||||
|
func testQuickQuestionMultilinePasteDoesNotSubmit() async throws {
|
||||||
|
app.launch()
|
||||||
|
openQuickQuestionFromHomeScreen()
|
||||||
|
|
||||||
|
let prompt = quickQuestionPromptElement()
|
||||||
|
XCTAssertTrue(prompt.waitForExistence(timeout: 10))
|
||||||
|
XCTAssertTrue(app.keyboards.firstMatch.waitForExistence(timeout: 10))
|
||||||
|
let pasted = "First line\nSecond line"
|
||||||
|
UIPasteboard.general.string = pasted
|
||||||
|
prompt.press(forDuration: 1.2)
|
||||||
|
let paste = app.descendants(matching: .any)
|
||||||
|
.matching(NSPredicate(format: "label == %@", "Paste"))
|
||||||
|
.firstMatch
|
||||||
|
XCTAssertTrue(paste.waitForExistence(timeout: 5))
|
||||||
|
paste.tap()
|
||||||
|
|
||||||
|
XCTAssertEqual(prompt.value as? String, pasted)
|
||||||
|
let state = try await control(["action": "state"])
|
||||||
|
let submitted = try XCTUnwrap(state["quickQuestions"] as? [[String: Any]])
|
||||||
|
XCTAssertTrue(submitted.isEmpty, "Pasting multiline text must not submit the question.")
|
||||||
|
}
|
||||||
|
|
||||||
|
func testSharedNormalComposerReturnInsertsNewline() async throws {
|
||||||
|
app.launch()
|
||||||
|
let textField = app.textFields["Enter Prompt"]
|
||||||
|
let textView = app.textViews["Enter Prompt"]
|
||||||
|
XCTAssertTrue(textField.waitForExistence(timeout: 5) || textView.waitForExistence(timeout: 5))
|
||||||
|
let composer = textField.exists ? textField : textView
|
||||||
|
composer.tap()
|
||||||
|
composer.typeText("Normal chat keeps newline\n")
|
||||||
|
|
||||||
|
XCTAssertTrue((composer.value as? String)?.contains("\n") == true)
|
||||||
|
let state = try await control(["action": "state"])
|
||||||
|
let log = try XCTUnwrap(state["log"] as? [[String: Any]])
|
||||||
|
XCTAssertFalse(
|
||||||
|
log.contains { $0["method"] as? String == "POST" && ($0["path"] as? String)?.hasSuffix("/chat-completions/stream") == true },
|
||||||
|
"Plain Return in normal chat must insert a newline without submitting."
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
private func quickQuestionPromptElement() -> XCUIElement {
|
private func quickQuestionPromptElement() -> XCUIElement {
|
||||||
let textField = app.textFields["quick-question-prompt"]
|
let textField = app.textFields["quick-question-prompt"]
|
||||||
return textField.exists ? textField : app.textViews["quick-question-prompt"]
|
return textField.exists ? textField : app.textViews["quick-question-prompt"]
|
||||||
@@ -168,12 +243,14 @@ final class QuickQuestionSelectionUITests: XCTestCase {
|
|||||||
add(attachment)
|
add(attachment)
|
||||||
}
|
}
|
||||||
|
|
||||||
private func control(_ body: [String: Any]) async throws {
|
@discardableResult
|
||||||
|
private func control(_ body: [String: Any]) async throws -> [String: Any] {
|
||||||
var request = URLRequest(url: URL(string: "http://127.0.0.1:8799/control")!)
|
var request = URLRequest(url: URL(string: "http://127.0.0.1:8799/control")!)
|
||||||
request.httpMethod = "POST"
|
request.httpMethod = "POST"
|
||||||
request.httpBody = try JSONSerialization.data(withJSONObject: body)
|
request.httpBody = try JSONSerialization.data(withJSONObject: body)
|
||||||
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
|
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
|
||||||
let (_, response) = try await URLSession.shared.data(for: request)
|
let (data, response) = try await URLSession.shared.data(for: request)
|
||||||
XCTAssertEqual((response as? HTTPURLResponse)?.statusCode, 200)
|
XCTAssertEqual((response as? HTTPURLResponse)?.statusCode, 200)
|
||||||
|
return try XCTUnwrap(JSONSerialization.jsonObject(with: data) as? [String: Any])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user