This commit is contained in:
2025-11-12 15:31:22 +08:00
parent 1dbe04cdf9
commit c317afc0fe
3 changed files with 53 additions and 44 deletions

View File

@@ -312,7 +312,7 @@ static NSString * const kKBStreamDemoURL = @"http://192.168.1.144:7529/api/demo/
- (void)kb_startNetworkStreamingWithSeed:(NSString *)seedTitle fallbackToMock:(BOOL)fallback {
[self kb_stopNetworkStreaming];
if (![[KBFullAccessManager shared] hasFullAccess]) { if (fallback) [self kb_startMockStreamingWithSeed:seedTitle]; return; }
if (![[KBFullAccessManager shared] hasFullAccess]) { if (fallback) /*[self kb_startMockStreamingWithSeed:seedTitle]*/; return; }
NSURL *url = [NSURL URLWithString:kKBStreamDemoURL];
if (!url) { if (fallback) [self kb_startMockStreamingWithSeed:seedTitle]; return; }
@@ -323,8 +323,10 @@ static NSString * const kKBStreamDemoURL = @"http://192.168.1.144:7529/api/demo/
// /t->\t \tfetcher
fetcher.disableCompression = YES;
fetcher.acceptEventStream = NO; // SSE
fetcher.treatSlashTAsTab = NO;
fetcher.trimLeadingTabOnce = NO;
// \t \t Fetcher UI
fetcher.treatSlashTAsTab = YES;
fetcher.trimLeadingTabOnce = YES;
fetcher.flushInterval = 0.05; //
fetcher.onChunk = ^(NSString *chunk) {
__strong typeof(weakSelf) self = weakSelf; if (!self) return;
[self kb_appendChunkToStreamView:chunk];
@@ -351,27 +353,11 @@ static NSString * const kKBStreamDemoURL = @"http://192.168.1.144:7529/api/demo/
#pragma mark - Helpers
/// KBStreamTextView
/// - "/t" "\t"
/// - "\t"
/// -
/// - KBStreamFetcher /t->\t \t
/// - UI
- (void)kb_appendChunkToStreamView:(NSString *)chunk {
if (chunk.length == 0 || !self.streamTextView) return;
NSString *text = [chunk stringByReplacingOccurrencesOfString:@"/t" withString:@"\t"];
if (!self.streamHasOutput) {
NSUInteger i = 0; //
while (i < text.length) {
unichar c = [text characterAtIndex:i];
if (c == ' ' || c == '\r' || c == '\n') { i++; continue; }
break;
}
if (i < text.length && [text characterAtIndex:i] == '\t') {
NSMutableString *m = [text mutableCopy];
[m deleteCharactersInRange:NSMakeRange(i, 1)];
text = m;
}
}
if (text.length == 0) return;
[self.streamTextView appendStreamText:text];
[self.streamTextView appendStreamText:chunk];
self.streamHasOutput = YES;
}