forked from Blendman974/faceclaw
iOS porting: Config pull and push scripts; Terminal works; voice input with Siri works
This commit is contained in:
1 parent
b07ae0b479
commit
319d3dc44b
21 files changed
+339
-9
No files matched your search
@@ -0,0 +1,6 @@
|
||||
#import <Foundation/Foundation.h>
|
||||
/** Developer config transfer through the app container, before workers start. */
|
||||
@interface FaceclawConfigPort : NSObject
|
||||
+ (void)processPendingRequest;
|
||||
+ (void)processDirectory:(NSString *)directory defaults:(NSUserDefaults *)defaults domain:(NSString *)domain;
|
||||
@end
|
||||
@@ -0,0 +1,59 @@
|
||||
#import "FaceclawConfigPort.h"
|
||||
@implementation FaceclawConfigPort
|
||||
+ (void)processPendingRequest {
|
||||
NSString *library = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).firstObject;
|
||||
[self processDirectory:[library stringByAppendingPathComponent:@"FaceclawConfigPort"]
|
||||
defaults:NSUserDefaults.standardUserDefaults domain:NSBundle.mainBundle.bundleIdentifier];
|
||||
}
|
||||
+ (BOOL)validSettings:(id)settings {
|
||||
if (![settings isKindOfClass:NSDictionary.class] || [settings count] > 4096) return NO;
|
||||
for (id key in settings) {
|
||||
id value = settings[key];
|
||||
if (![key isKindOfClass:NSString.class] || ![key length] || [key length] > 512 ||
|
||||
!([value isKindOfClass:NSString.class] || [value isKindOfClass:NSNumber.class])) return NO;
|
||||
}
|
||||
return YES;
|
||||
}
|
||||
+ (void)processDirectory:(NSString *)directory defaults:(NSUserDefaults *)defaults domain:(NSString *)domain {
|
||||
NSString *requestPath = [directory stringByAppendingPathComponent:@"request.json"];
|
||||
NSData *data = [NSData dataWithContentsOfFile:requestPath];
|
||||
if (!data) return;
|
||||
NSFileManager *fm = NSFileManager.defaultManager;
|
||||
// Consume once. A failed import must never be replayed at a later launch.
|
||||
[fm removeItemAtPath:requestPath error:nil];
|
||||
id request = data.length <= 2 * 1024 * 1024 ? [NSJSONSerialization JSONObjectWithData:data options:0 error:nil] : nil;
|
||||
BOOL valid = [request isKindOfClass:NSDictionary.class];
|
||||
NSString *identifier = valid && [request[@"id"] isKindOfClass:NSString.class] ? request[@"id"] : @"";
|
||||
NSMutableDictionary *reply = [@{@"id":identifier, @"ok":@NO, @"message":@"Invalid config request"} mutableCopy];
|
||||
[defaults synchronize];
|
||||
NSDictionary *before = [defaults persistentDomainForName:domain] ?: @{};
|
||||
if (valid && [request[@"operation"] isEqual:@"pull"]) {
|
||||
NSMutableDictionary *settings = [NSMutableDictionary new];
|
||||
for (NSString *key in before) {
|
||||
id value = before[key];
|
||||
if ([value isKindOfClass:NSString.class] || [value isKindOfClass:NSNumber.class]) settings[key] = value;
|
||||
}
|
||||
[settings removeObjectForKey:@"ios.settings.changeToken"];
|
||||
reply[@"config"] = @{@"schema":@1, @"settings":settings}; reply[@"ok"] = @YES;
|
||||
reply[@"message"] = @"Config exported";
|
||||
} else if (valid && [request[@"operation"] isEqual:@"push"] && [self validSettings:request[@"settings"]]) {
|
||||
NSError *error = nil;
|
||||
NSData *backup = [NSPropertyListSerialization dataWithPropertyList:before format:NSPropertyListBinaryFormat_v1_0 options:0 error:&error];
|
||||
if (backup && [backup writeToFile:[directory stringByAppendingPathComponent:@"previous.plist"] options:NSDataWritingAtomic error:&error]) {
|
||||
NSMutableDictionary *next = [before mutableCopy];
|
||||
[next addEntriesFromDictionary:request[@"settings"]];
|
||||
next[@"ios.settings.changeToken"] = NSUUID.UUID.UUIDString;
|
||||
[defaults setPersistentDomain:next forName:domain];
|
||||
if ([defaults synchronize]) {
|
||||
reply[@"ok"] = @YES; reply[@"message"] = @"Config imported";
|
||||
reply[@"count"] = @([request[@"settings"] count]);
|
||||
} else {
|
||||
[defaults setPersistentDomain:before forName:domain]; [defaults synchronize];
|
||||
reply[@"message"] = @"Could not persist config; restored previous settings";
|
||||
}
|
||||
} else reply[@"message"] = @"Could not back up existing settings; import skipped";
|
||||
}
|
||||
NSData *response = [NSJSONSerialization dataWithJSONObject:reply options:NSJSONWritingPrettyPrinted error:nil];
|
||||
[response writeToFile:[directory stringByAppendingPathComponent:@"response.json"] options:NSDataWritingAtomic error:nil];
|
||||
}
|
||||
@end
|
||||
@@ -1,5 +1,6 @@
|
||||
#import "FaceclawSpeech.h"
|
||||
#import "FaceclawLc3Decoder.h"
|
||||
#import "FaceclawSpeechTranscript.h"
|
||||
#import <Speech/Speech.h>
|
||||
#import <AVFoundation/AVFoundation.h>
|
||||
#import <math.h>
|
||||
@@ -14,6 +15,7 @@
|
||||
@property(nonatomic) NSUInteger pendingPackets;
|
||||
@property(nonatomic) BOOL accepting;
|
||||
@property(nonatomic, copy) NSString *bestText;
|
||||
@property(nonatomic, strong) FaceclawSpeechTranscript *transcript;
|
||||
@end
|
||||
@implementation FaceclawSpeech
|
||||
- (instancetype)init {
|
||||
@@ -49,6 +51,7 @@
|
||||
self.request.taskHint = SFSpeechRecognitionTaskHintDictation;
|
||||
if (@available(iOS 16.0, *)) self.request.addsPunctuation = YES;
|
||||
self.bestText = @""; self.accepting = YES;
|
||||
self.transcript = [FaceclawSpeechTranscript new];
|
||||
NSUInteger generation = self.generation;
|
||||
__weak FaceclawSpeech *weakSelf = self;
|
||||
self.task = [self.recognizer recognitionTaskWithRequest:self.request resultHandler:^(SFSpeechRecognitionResult *result, NSError *error) {
|
||||
@@ -56,7 +59,13 @@
|
||||
FaceclawSpeech *owner = weakSelf;
|
||||
if (!owner || owner.generation != generation) return;
|
||||
if (result) {
|
||||
owner.bestText = result.bestTranscription.formattedString ?: @"";
|
||||
SFSpeechRecognitionMetadata *metadata = result.speechRecognitionMetadata;
|
||||
SFTranscriptionSegment *first = result.bestTranscription.segments.firstObject;
|
||||
SFTranscriptionSegment *last = result.bestTranscription.segments.lastObject;
|
||||
NSTimeInterval start = metadata ? metadata.speechStartTimestamp : first.timestamp;
|
||||
NSTimeInterval duration = metadata ? metadata.speechDuration : last.timestamp + last.duration - start;
|
||||
owner.bestText = [owner.transcript updateText:result.bestTranscription.formattedString ?: @""
|
||||
start:start duration:duration settled:metadata != nil || result.final final:result.final];
|
||||
if (!result.final) [owner emit:@{@"kind":@"transcript", @"text":owner.bestText, @"final":@NO}];
|
||||
}
|
||||
if (result.final || error) {
|
||||
@@ -131,6 +140,6 @@
|
||||
self.generation++;
|
||||
self.accepting = NO; self.pendingPackets = 0;
|
||||
[self.task cancel]; self.task = nil; self.request = nil; self.decoder = nil; self.recognizer = nil;
|
||||
self.bestText = @"";
|
||||
self.bestText = @""; self.transcript = nil;
|
||||
}
|
||||
@end
|
||||
@@ -0,0 +1,6 @@
|
||||
#import <Foundation/Foundation.h>
|
||||
/** Accumulates on-device utterances while allowing revisions within each one. */
|
||||
@interface FaceclawSpeechTranscript : NSObject
|
||||
@property(nonatomic, readonly) NSString *text;
|
||||
- (NSString *)updateText:(NSString *)text start:(NSTimeInterval)start duration:(NSTimeInterval)duration settled:(BOOL)settled final:(BOOL)final;
|
||||
@end
|
||||
@@ -0,0 +1,38 @@
|
||||
#import "FaceclawSpeechTranscript.h"
|
||||
#import <math.h>
|
||||
@interface FaceclawSpeechTranscript ()
|
||||
@property(nonatomic, strong) NSMutableArray<NSDictionary *> *utterances;
|
||||
@end
|
||||
@implementation FaceclawSpeechTranscript
|
||||
- (instancetype)init {
|
||||
if ((self = [super init])) _utterances = [NSMutableArray new];
|
||||
return self;
|
||||
}
|
||||
- (NSString *)text {
|
||||
NSMutableArray *parts = [NSMutableArray new];
|
||||
for (NSDictionary *utterance in self.utterances) [parts addObject:utterance[@"text"]];
|
||||
return [parts componentsJoinedByString:@" "];
|
||||
}
|
||||
- (NSString *)updateText:(NSString *)text start:(NSTimeInterval)start duration:(NSTimeInterval)duration settled:(BOOL)settled final:(BOOL)final {
|
||||
text = [text stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet];
|
||||
// Apple's final callback can be empty after a pause. Keep the best result.
|
||||
if (!text.length || (final && [text isEqual:self.text])) return self.text;
|
||||
BOOL timed = isfinite(start) && start >= 0 && isfinite(duration) && duration > 0;
|
||||
NSUInteger index = self.utterances.count;
|
||||
if (timed) {
|
||||
// Audio timestamps identify revisions, repeated callbacks and cumulative
|
||||
// results. Replace the covered suffix, preserving earlier utterances.
|
||||
for (NSUInteger i = 0; i < self.utterances.count; i++) {
|
||||
NSDictionary *item = self.utterances[i];
|
||||
if (![item[@"settled"] boolValue] || start < [item[@"end"] doubleValue] - 0.01) { index = i; break; }
|
||||
}
|
||||
} else if (index && (final || ![self.utterances.lastObject[@"settled"] boolValue])) {
|
||||
index--; // An ordinary partial corrects the active utterance.
|
||||
}
|
||||
// Untimed partials following a metadata-bearing result begin a new
|
||||
// utterance even when the words are identical ("yes" ... "yes").
|
||||
if (index < self.utterances.count) [self.utterances removeObjectsInRange:NSMakeRange(index, self.utterances.count - index)];
|
||||
[self.utterances addObject:@{@"text":text, @"end":@(timed ? start + duration : 0), @"settled":@(settled)}];
|
||||
return self.text;
|
||||
}
|
||||
@end
|
||||
@@ -5,5 +5,6 @@ module FaceclawGraphics {
|
||||
header "FaceclawSocket.h"
|
||||
header "FaceclawLc3Decoder.h"
|
||||
header "FaceclawSpeech.h"
|
||||
header "FaceclawConfigPort.h"
|
||||
export *
|
||||
}
|
||||
Reference in new issue
Block a user