This commit is contained in:
2025-12-08 19:48:13 +08:00
parent fd8c08316b
commit 2cc93e0b48
6 changed files with 49 additions and 31 deletions

View File

@@ -24,6 +24,12 @@ typedef void (^KBStreamFetcherFinishHandler)(NSError *_Nullable error);
// 必填:请求地址
@property (nonatomic, strong) NSURL *url;
/// HTTP Method默认为 GET
@property (nonatomic, copy, nullable) NSString *httpMethod;
/// 自定义请求体(例如 POST 的 JSON body
@property (nonatomic, strong, nullable) NSData *httpBody;
// 可选 Header
@property (nonatomic, copy, nullable) NSDictionary<NSString *, NSString *> *extraHeaders;

View File

@@ -55,6 +55,7 @@ static NSUInteger kb_validUTF8PrefixLen(NSData *data) {
- (instancetype)init {
if (self = [super init]) {
_httpMethod = @"GET";
_acceptEventStream = NO;
_disableCompression = YES;
_treatSlashTAsTab = YES;
@@ -83,7 +84,11 @@ static NSUInteger kb_validUTF8PrefixLen(NSData *data) {
self.session = [NSURLSession sessionWithConfiguration:cfg delegate:self delegateQueue:[NSOperationQueue mainQueue]];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:self.url];
req.HTTPMethod = @"GET";
NSString *method = self.httpMethod.length > 0 ? self.httpMethod : @"GET";
req.HTTPMethod = method;
if (self.httpBody.length > 0) {
req.HTTPBody = self.httpBody;
}
if (self.disableCompression) { [req setValue:@"identity" forHTTPHeaderField:@"Accept-Encoding"]; }
if (self.acceptEventStream) { [req setValue:@"text/event-stream" forHTTPHeaderField:@"Accept"]; }
[req setValue:@"no-cache" forHTTPHeaderField:@"Cache-Control"];
@@ -290,10 +295,10 @@ static NSUInteger kb_validUTF8PrefixLen(NSData *data) {
if (rawText.length == 0) return;
// 便
if (self.loggingEnabled) {
NSLog(@"[KBStream] RAW chunk#%ld len=%lu text=\"%@\"",
(long)(self.emittedChunkCount + 1),
(unsigned long)rawText.length,
KBPrintableSnippet(rawText, 160));
// NSLog(@"[KBStream] RAW chunk#%ld len=%lu text=\"%@\"",
// (long)(self.emittedChunkCount + 1),
// (unsigned long)rawText.length,
// KBPrintableSnippet(rawText, 160));
}
NSString *text = rawText;
// 0) \r/\n "\n\t""\r\n\t""\r\t" "\t"

View File

@@ -268,7 +268,7 @@
}
#pragma mark - Network Streaming (GET)
#pragma mark - Network Streaming (POST)
// ATS target Info.plist HTTP
//static NSString * const kKBStreamDemoURL = @"http://192.168.1.144:7529/api/demo/talk";
@@ -278,23 +278,42 @@
if (![[KBFullAccessManager shared] hasFullAccess]) { return; }
NSString *apiUrl = [NSString stringWithFormat:@"%@%@", KB_BASE_URL, API_AI_TALK];
// seed query
if (seedTitle.length > 0) {
NSString *encodedSeed = [seedTitle stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]] ?: @"";
if (encodedSeed.length > 0) {
if ([apiUrl containsString:@"?"]) {
apiUrl = [apiUrl stringByAppendingFormat:@"&userInput=%@", encodedSeed];
} else {
apiUrl = [apiUrl stringByAppendingFormat:@"?userInput=%@", encodedSeed];
}
}
}
NSURL *url = [NSURL URLWithString:apiUrl];
if (!url) { return; }
// POST Body
NSInteger characterId = 0;
if (self.loadingTagIndex != nil) {
NSInteger idx = self.loadingTagIndex.integerValue;
if (idx >= 0 && idx < self.modelArray.count) {
KBTagItemModel *model = self.modelArray[idx];
characterId = model.characterId;
}
}
NSString *message = seedTitle.length > 0 ? seedTitle : @"";
NSInteger resolvedCharacterId = (characterId > 0) ? characterId : 75;
NSDictionary *payload = @{
@"characterId": @(resolvedCharacterId),
@"message": message.length > 0 ? message : @"aliqua non cupidatat"
};
NSError *bodyError = nil;
NSData *bodyData = [NSJSONSerialization dataWithJSONObject:payload options:0 error:&bodyError];
if (bodyError || bodyData.length == 0) {
NSLog(@"[KBFunction] build body failed: %@", bodyError);
return;
}
self.streamHasOutput = NO; //
__weak typeof(self) weakSelf = self;
KBStreamFetcher *fetcher = [KBStreamFetcher fetcherWithURL:url];
fetcher.httpMethod = @"POST";
fetcher.httpBody = bodyData;
NSMutableDictionary *headers = [@{ @"Content-Type": @"application/json" } mutableCopy];
NSString *token = KBAuthManager.shared.current.accessToken ?: @"";
if (token.length > 0) {
headers[@"auth-token"] = token;
}
fetcher.extraHeaders = headers;
// /t->\t \tfetcher
fetcher.disableCompression = YES;
fetcher.acceptEventStream = NO; // SSE