Add cloud voice model support for iOS

This commit is contained in:
James Babcock committed 2026-10-10 22:56:07 -07:00
1 parent 5df873e5b1
commit 920cce6a26
24 files changed
+518 -176

No files matched your search

+20 -3
View File
@@ -8,12 +8,17 @@
@end
@implementation FaceclawSocket
- (instancetype)initWithURL:(NSString *)url {
return [self initWithURL:url headerName:nil headerValue:nil];
}
- (instancetype)initWithURL:(NSString *)url headerName:(NSString *)headerName headerValue:(NSString *)headerValue {
if ((self = [super init])) {
_events = [NSMutableArray array];
NSURLSessionConfiguration *config = [NSURLSessionConfiguration ephemeralSessionConfiguration];
config.timeoutIntervalForRequest = 20;
_session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];
_task = [_session webSocketTaskWithURL:[NSURL URLWithString:url]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
if (headerName.length && headerValue) [request setValue:headerValue forHTTPHeaderField:headerName];
_task = [_session webSocketTaskWithRequest:request];
_task.maximumMessageSize = 8 * 1024 * 1024;
[_task resume];
}
@@ -25,7 +30,7 @@
_queuedBytes += [event[@"text"] lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
if (_events.count >= 256 || _queuedBytes > 16 * 1024 * 1024) {
[_events removeAllObjects]; _queuedBytes = 0;
[_events addObject:@{@"kind": @"error", @"text": @"Terminal input queue overflow"}];
[_events addObject:@{@"kind": @"error", @"text": @"WebSocket receive queue overflow"}];
[self close];
} else [_events addObject:event];
}
@@ -56,7 +61,13 @@
[self close];
}
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
if (error) [self enqueue:@{@"kind": @"error", @"text": error.localizedDescription}];
if (error) {
// A refused handshake only says "bad response"; name the HTTP status
// so callers can tell a rejected key (401/403) from a network error.
NSInteger status = [task.response isKindOfClass:NSHTTPURLResponse.class] ? ((NSHTTPURLResponse *)task.response).statusCode : 0;
NSString *text = status >= 400 ? [NSString stringWithFormat:@"HTTP %ld: %@", (long)status, error.localizedDescription] : error.localizedDescription;
[self enqueue:@{@"kind": @"error", @"text": text}];
}
[self close];
}
- (void)sendText:(NSString *)text {
@@ -65,6 +76,12 @@
if (error) { [weakSelf enqueue:@{@"kind": @"error", @"text": error.localizedDescription}]; [weakSelf close]; }
}];
}
- (void)sendData:(NSData *)data {
__weak FaceclawSocket *weakSelf = self;
[_task sendMessage:[[NSURLSessionWebSocketMessage alloc] initWithData:data] completionHandler:^(NSError *error) {
if (error) { [weakSelf enqueue:@{@"kind": @"error", @"text": error.localizedDescription}]; [weakSelf close]; }
}];
}
- (void)close {
@synchronized(self) {
if (_ended) return; _ended = YES;