Port assistant integration (with cloud AIs) to iOS
This commit is contained in:
1 parent
9e18745e53
commit
617cdc39eb
25 files changed
+508
-63
No files matched your search
@@ -0,0 +1,8 @@
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
/** Streaming POST. Events are polled by the owning JS isolate, including workers. */
|
||||
@interface FaceclawSseRequest : NSObject <NSURLSessionDataDelegate>
|
||||
- (instancetype)initWithURL:(NSString *)url body:(NSString *)body headers:(NSString *)headersJson;
|
||||
- (NSString *)takeEvents;
|
||||
- (void)cancel;
|
||||
@end
|
||||
@@ -0,0 +1,153 @@
|
||||
#import "FaceclawSseRequest.h"
|
||||
#import <TargetConditionals.h>
|
||||
#if TARGET_OS_IOS
|
||||
#import <UIKit/UIKit.h>
|
||||
#endif
|
||||
|
||||
@interface FaceclawSseRequest ()
|
||||
@property(nonatomic, strong) NSURLSession *session;
|
||||
@property(nonatomic, strong) NSURLSessionDataTask *task;
|
||||
@property(nonatomic, strong) NSMutableData *buffer;
|
||||
@property(nonatomic, strong) NSMutableArray *events;
|
||||
@property(nonatomic) NSUInteger queuedBytes;
|
||||
@property(nonatomic) NSInteger status;
|
||||
@property(nonatomic) BOOL finished;
|
||||
@property(nonatomic) BOOL cancelled;
|
||||
@property(nonatomic) BOOL firstLine;
|
||||
#if TARGET_OS_IOS
|
||||
@property(nonatomic) UIBackgroundTaskIdentifier backgroundTask;
|
||||
#endif
|
||||
@end
|
||||
|
||||
@implementation FaceclawSseRequest
|
||||
- (instancetype)initWithURL:(NSString *)url body:(NSString *)body headers:(NSString *)headersJson {
|
||||
if ((self = [super init])) {
|
||||
_buffer = [NSMutableData new]; _events = [NSMutableArray new]; _firstLine = YES;
|
||||
#if TARGET_OS_IOS
|
||||
_backgroundTask = UIBackgroundTaskInvalid;
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
@synchronized(self) {
|
||||
if (self.cancelled || self.finished) return;
|
||||
self.backgroundTask = [UIApplication.sharedApplication beginBackgroundTaskWithName:@"Assistant response" expirationHandler:^{
|
||||
[self finish:@{@"kind": @"failure", @"text": @"iOS background time expired. Open Faceclaw and retry."}];
|
||||
[self endBackgroundTask];
|
||||
}];
|
||||
}
|
||||
});
|
||||
#endif
|
||||
NSURL *target = [NSURL URLWithString:url];
|
||||
if (!target || ![@[@"https", @"http"] containsObject:target.scheme.lowercaseString]) {
|
||||
[self finish:@{@"kind": @"failure", @"text": @"Invalid streaming URL"}]; return self;
|
||||
}
|
||||
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:target];
|
||||
request.HTTPMethod = @"POST";
|
||||
request.HTTPBody = [body dataUsingEncoding:NSUTF8StringEncoding];
|
||||
[request setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
|
||||
[request setValue:@"text/event-stream" forHTTPHeaderField:@"Accept"];
|
||||
NSDictionary *headers = [NSJSONSerialization JSONObjectWithData:[headersJson dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
|
||||
if ([headers isKindOfClass:NSDictionary.class]) for (NSString *name in headers) {
|
||||
if ([headers[name] isKindOfClass:NSString.class]) [request setValue:headers[name] forHTTPHeaderField:name];
|
||||
}
|
||||
NSURLSessionConfiguration *config = NSURLSessionConfiguration.ephemeralSessionConfiguration;
|
||||
config.timeoutIntervalForRequest = 180;
|
||||
config.timeoutIntervalForResource = 180;
|
||||
_session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];
|
||||
_task = [_session dataTaskWithRequest:request];
|
||||
[_task resume];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
- (void)endBackgroundTask {
|
||||
#if TARGET_OS_IOS
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
@synchronized(self) {
|
||||
if (self.backgroundTask == UIBackgroundTaskInvalid) return;
|
||||
[UIApplication.sharedApplication endBackgroundTask:self.backgroundTask];
|
||||
self.backgroundTask = UIBackgroundTaskInvalid;
|
||||
}
|
||||
});
|
||||
#endif
|
||||
}
|
||||
- (void)finish:(NSDictionary *)event {
|
||||
@synchronized(self) {
|
||||
if (_finished || _cancelled) return;
|
||||
_finished = YES; [_events addObject:event];
|
||||
[_session invalidateAndCancel]; _session = nil; _task = nil;
|
||||
[_buffer setLength:0];
|
||||
}
|
||||
}
|
||||
- (NSString *)takeEvents {
|
||||
@synchronized(self) {
|
||||
NSData *data = [NSJSONSerialization dataWithJSONObject:_events options:0 error:nil];
|
||||
[_events removeAllObjects]; _queuedBytes = 0;
|
||||
if (_finished) [self endBackgroundTask];
|
||||
return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] ?: @"[]";
|
||||
}
|
||||
}
|
||||
- (void)cancel {
|
||||
@synchronized(self) {
|
||||
_cancelled = YES;
|
||||
[_events removeAllObjects]; [_buffer setLength:0]; _queuedBytes = 0;
|
||||
[_session invalidateAndCancel]; _session = nil; _task = nil;
|
||||
[self endBackgroundTask];
|
||||
}
|
||||
}
|
||||
- (void)line:(NSData *)data {
|
||||
NSString *line = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
|
||||
if (!line) { [self finish:@{@"kind": @"failure", @"text": @"Invalid UTF-8 in streaming response"}]; return; }
|
||||
if (_firstLine && [line hasPrefix:@"\uFEFF"]) line = [line substringFromIndex:1];
|
||||
_firstLine = NO;
|
||||
_queuedBytes += data.length;
|
||||
if (_queuedBytes > 8 * 1024 * 1024 || _events.count >= 16384) {
|
||||
[_events removeAllObjects];
|
||||
[self finish:@{@"kind": @"failure", @"text": @"Streaming response queue overflow"}]; return;
|
||||
}
|
||||
[_events addObject:@{@"kind": @"line", @"text": line}];
|
||||
}
|
||||
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)task didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler {
|
||||
@synchronized(self) { _status = [(NSHTTPURLResponse *)response statusCode]; }
|
||||
completionHandler(NSURLSessionResponseAllow);
|
||||
}
|
||||
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task willPerformHTTPRedirection:(NSHTTPURLResponse *)response newRequest:(NSURLRequest *)request completionHandler:(void (^)(NSURLRequest *))completionHandler {
|
||||
// Never forward provider credentials to a redirected endpoint.
|
||||
completionHandler(nil);
|
||||
}
|
||||
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)task didReceiveData:(NSData *)data {
|
||||
@synchronized(self) {
|
||||
if (_finished || _cancelled) return;
|
||||
if (_status < 200 || _status >= 300) {
|
||||
NSUInteger remaining = 65536 - _buffer.length;
|
||||
[_buffer appendData:[data subdataWithRange:NSMakeRange(0, MIN(remaining, data.length))]];
|
||||
return;
|
||||
}
|
||||
[_buffer appendData:data];
|
||||
NSUInteger start = 0;
|
||||
const uint8_t *bytes = _buffer.bytes;
|
||||
for (NSUInteger i = 0; i < _buffer.length; i++) {
|
||||
if (bytes[i] != '\r' && bytes[i] != '\n') continue;
|
||||
if (bytes[i] == '\r' && i + 1 == _buffer.length) break;
|
||||
[self line:[_buffer subdataWithRange:NSMakeRange(start, i - start)]];
|
||||
if (_finished) return;
|
||||
if (bytes[i] == '\r' && bytes[i + 1] == '\n') i++;
|
||||
start = i + 1;
|
||||
}
|
||||
if (start) [_buffer replaceBytesInRange:NSMakeRange(0, start) withBytes:NULL length:0];
|
||||
if (_buffer.length > 2 * 1024 * 1024) [self finish:@{@"kind": @"failure", @"text": @"Streaming response line too large"}];
|
||||
}
|
||||
}
|
||||
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
|
||||
@synchronized(self) {
|
||||
if (_finished || _cancelled) return;
|
||||
if (error) { [self finish:@{@"kind": @"failure", @"text": error.localizedDescription}]; return; }
|
||||
if (_status < 200 || _status >= 300) {
|
||||
NSString *body = [[NSString alloc] initWithData:_buffer encoding:NSUTF8StringEncoding] ?: @"";
|
||||
[self finish:@{@"kind": @"http-error", @"code": @(_status), @"text": body}]; return;
|
||||
}
|
||||
if (_buffer.length) {
|
||||
if (((const uint8_t *)_buffer.bytes)[_buffer.length - 1] == '\r') [_buffer setLength:_buffer.length - 1];
|
||||
[self line:_buffer];
|
||||
}
|
||||
[self finish:@{@"kind": @"complete"}];
|
||||
}
|
||||
}
|
||||
@end
|
||||
@@ -5,6 +5,7 @@ module FaceclawGraphics {
|
||||
header "FaceclawSocket.h"
|
||||
header "FaceclawLc3Decoder.h"
|
||||
header "FaceclawSpeech.h"
|
||||
header "FaceclawSseRequest.h"
|
||||
header "FaceclawLocation.h"
|
||||
header "FaceclawConfigPort.h"
|
||||
header "FaceclawEvenHubWebView.h"
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { GrayImage, type UiFont } from "../../graphics/image";
|
||||
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
|
||||
import { truncateText, wrapText } from "../../graphics/textwrap";
|
||||
import { ASSISTANT_MODEL_VALUES, assistantModelLabel, resolveAssistantModel } from "../../assistant/models";
|
||||
import { ASSISTANT_MODEL_CHOICES, assistantModelLabel, resolveAssistantModel } from "../../assistant/models";
|
||||
import type { AssistantTranscriptEntry } from "../../assistant/session";
|
||||
import type { AssistantConversations, ReasoningLevel } from "../../assistant/conversations";
|
||||
import { anthropicApiKeySetting, assistantBackendSetting, openAiApiKeySetting } from "../../ui/dashboard-settings";
|
||||
@@ -102,7 +102,7 @@ class AiChatLayer implements Layer {
|
||||
}
|
||||
case "long-press":
|
||||
if (!this.conversations.current().session?.isTurnActive()) {
|
||||
if (!shell.isAssistantAvailable()) { shell.showAlert("Set an API key or download the on-phone model in Settings."); return; }
|
||||
if (!shell.isAssistantAvailable()) { shell.showAlert(global.isIOS ? "Set an OpenAI or Anthropic API key in Settings." : "Set an API key or download the on-phone model in Settings."); return; }
|
||||
void this.draft.start();
|
||||
}
|
||||
return;
|
||||
@@ -126,7 +126,7 @@ class AiChatLayer implements Layer {
|
||||
onSelect: (ctx) => { if (this.conversations.select(item.id)) ctx.stack.clearToBase(); },
|
||||
}))) },
|
||||
{ label: `Model: ${assistantModelLabel(record.model)}`, disabled: () => external || busy(), onSelect: (ctx) => submenu(ctx, "Model",
|
||||
ASSISTANT_MODEL_VALUES.map((model) => ({
|
||||
ASSISTANT_MODEL_CHOICES.map((model) => ({
|
||||
label: `${model === record.model ? "✓ " : ""}${assistantModelLabel(model)}`,
|
||||
disabled: () => !this.conversations.available(model, record.reasoning),
|
||||
onSelect: (ctx) => { if (this.conversations.configure(model, record.reasoning)) ctx.stack.clearToBase(); },
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { AssistantSession, type AssistantBackendConfig, type AssistantSessionHistory } from "./session";
|
||||
import { ASSISTANT_MODEL_VALUES, type AssistantModel } from "./models";
|
||||
import { ASSISTANT_MODEL_VALUES, supportedAssistantModel, type AssistantModel } from "./models";
|
||||
|
||||
export type ReasoningLevel = "default" | "low" | "medium" | "high";
|
||||
export type Conversation = {
|
||||
@@ -31,7 +31,7 @@ export class AssistantConversations {
|
||||
typeof record.id === "string" && ASSISTANT_MODEL_VALUES.includes(record.model) &&
|
||||
["default", "low", "medium", "high"].includes(record.reasoning) &&
|
||||
Array.isArray(record.history?.messages) && Array.isArray(record.history?.transcript),
|
||||
).map((record) => ({ ...record, session: null }));
|
||||
).map((record) => ({ ...record, model: supportedAssistantModel(record.model), session: null }));
|
||||
this.selectedId = data.selectedId;
|
||||
}
|
||||
} catch { /* First launch or invalid saved state. */ }
|
||||
|
||||
@@ -51,10 +51,13 @@ export class DirectAssistantBackend {
|
||||
let cancelled = false;
|
||||
let streamHandle: LlmStreamHandle | null = null;
|
||||
let priorText = "";
|
||||
let deadline: ReturnType<typeof setTimeout> | null = null;
|
||||
const clearDeadline = () => { if (deadline !== null) clearTimeout(deadline); deadline = null; };
|
||||
|
||||
const finishError = (message: string) => {
|
||||
if (cancelled) return;
|
||||
cancelled = true;
|
||||
clearDeadline(); streamHandle?.cancel(); streamHandle = null;
|
||||
options.callbacks.onError(message);
|
||||
};
|
||||
|
||||
@@ -102,10 +105,11 @@ export class DirectAssistantBackend {
|
||||
block.type === "tool_use",
|
||||
);
|
||||
if (result.stopReason !== "tool_use" || toolUses.length === 0) {
|
||||
clearDeadline();
|
||||
options.callbacks.onTurnDone({ stopReason: result.stopReason });
|
||||
return;
|
||||
}
|
||||
void this.runToolCalls(toolUses, options)
|
||||
void this.runToolCalls(toolUses, options, () => cancelled)
|
||||
.then((toolResults) => {
|
||||
// Always record the results, even if cancelled mid-flight: an
|
||||
// assistant tool_use with no matching tool_result would make the
|
||||
@@ -131,11 +135,13 @@ export class DirectAssistantBackend {
|
||||
: streamAnthropicMessage(streamOptions);
|
||||
};
|
||||
|
||||
deadline = setTimeout(() => finishError("Assistant stopped: turn took too long"), MAX_TURN_MS);
|
||||
runIteration(0);
|
||||
|
||||
return {
|
||||
cancel: () => {
|
||||
cancelled = true;
|
||||
clearDeadline();
|
||||
streamHandle?.cancel();
|
||||
streamHandle = null;
|
||||
},
|
||||
@@ -145,9 +151,14 @@ export class DirectAssistantBackend {
|
||||
private async runToolCalls(
|
||||
toolUses: Array<Extract<LlmContentBlock, { type: "tool_use" }>>,
|
||||
options: DirectTurnOptions,
|
||||
isCancelled: () => boolean,
|
||||
): Promise<LlmContentBlock[]> {
|
||||
const results: LlmContentBlock[] = [];
|
||||
for (const toolUse of toolUses) {
|
||||
if (isCancelled()) {
|
||||
results.push({ type: "tool_result", tool_use_id: toolUse.id, content: "Cancelled", is_error: true });
|
||||
continue;
|
||||
}
|
||||
const canonicalName = options.resolveToolName(toolUse.name);
|
||||
options.callbacks.onToolActivity(canonicalName);
|
||||
const result = await options.registry.callTool(canonicalName, toolUse.input);
|
||||
|
||||
@@ -13,6 +13,13 @@ export const ASSISTANT_MODEL_VALUES = [
|
||||
] as const;
|
||||
|
||||
export type AssistantModel = (typeof ASSISTANT_MODEL_VALUES)[number];
|
||||
/** Keep saved model IDs portable, while offering only supported choices. */
|
||||
export const ASSISTANT_MODEL_CHOICES: readonly AssistantModel[] =
|
||||
global.isIOS ? ASSISTANT_MODEL_VALUES.filter(value => value !== "qwen") : ASSISTANT_MODEL_VALUES;
|
||||
export function supportedAssistantModel(value: AssistantModel): AssistantModel {
|
||||
return global.isIOS && value === "qwen" ? "auto" : value;
|
||||
}
|
||||
|
||||
export type AssistantProvider = "anthropic" | "openai" | "local";
|
||||
|
||||
export type AssistantApiKeys = {
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
* the app closed — so these always-available wrappers launch the app first,
|
||||
* wait for its tools to register, and forward the call.
|
||||
*/
|
||||
import { ensureFineLocationPermission } from "../g2/android-permissions";
|
||||
import { ensureFineLocationPermission } from "../native/location-permissions";
|
||||
import { toolRegistry, type ToolRegistry, type ToolResult } from "./tool-registry";
|
||||
import { callAppToolWithLaunch } from "./launch-on-call";
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ export function registerSystemTools(registry: ToolRegistry = toolRegistry): void
|
||||
registered = true;
|
||||
|
||||
// Keep a media listener warm so "what's playing" works without opening Music.
|
||||
void mediaControllerBridge.start();
|
||||
if (!global.isIOS) void mediaControllerBridge.start();
|
||||
|
||||
registry.registerSystemTool(
|
||||
{
|
||||
@@ -54,6 +54,9 @@ export function registerSystemTools(registry: ToolRegistry = toolRegistry): void
|
||||
},
|
||||
);
|
||||
|
||||
// Calendar, phone media and notifications have no iOS backend yet.
|
||||
if (global.isIOS) return;
|
||||
|
||||
registry.registerSystemTool(
|
||||
{
|
||||
name: "calendar.list_events",
|
||||
|
||||
@@ -1,3 +1,7 @@
|
||||
import { registerSystemTools } from '../assistant/system-tools'
|
||||
import { registerWindowTools } from '../assistant/window-tools'
|
||||
import { registerNavigateTools } from '../assistant/navigate-tools'
|
||||
import { registerRoamTools } from '../assistant/roam-tools'
|
||||
import { IosNavigationSensors } from '../native/ios-navigation-sensors'
|
||||
import { Utils } from '@nativescript/core'
|
||||
import { bindCompassSession, receiveCompassEvent } from '../native/compass.ios'
|
||||
@@ -124,6 +128,11 @@ export class IosPreviewController {
|
||||
shell.registerWindow(launcher)
|
||||
shell.wake('window')
|
||||
shell.focusWindow(launcher.windowId)
|
||||
registerSystemTools()
|
||||
registerWindowTools({ apps: ALL_APPS.filter(app => !iosAppUnavailableReason(app.appId)),
|
||||
launchApp: id => this.launchApp(id), requestShellRender: () => this.requestShellRender() })
|
||||
registerNavigateTools(id => this.launchApp(id))
|
||||
registerRoamTools(id => this.launchApp(id))
|
||||
for (const app of ALL_APPS) if (app.appId !== 'launcher' && !iosAppUnavailableReason(app.appId)) app.boot?.(this.buildAppContext(app))
|
||||
}
|
||||
|
||||
|
||||
+12
-21
@@ -8,13 +8,13 @@ import type {
|
||||
LlmToolDefinition,
|
||||
} from "../assistant/llm-protocol";
|
||||
|
||||
declare const com: any;
|
||||
import { openSseRequest, type SseListener } from "./sse";
|
||||
|
||||
/**
|
||||
* Minimal streaming client for the Anthropic Messages API. There is no
|
||||
* official SDK for the NativeScript runtime, so this speaks raw HTTP/SSE via
|
||||
* FaceclawSseRequest (okhttp on the Java side), which delivers the response
|
||||
* body line by line on this isolate's thread.
|
||||
* the platform transport (OkHttp on Android, URLSession on iOS), which
|
||||
* delivers response lines on this isolate's thread.
|
||||
*/
|
||||
|
||||
const ANTHROPIC_URL = "https://api.anthropic.com/v1/messages";
|
||||
@@ -55,22 +55,19 @@ export function streamAnthropicMessage(options: AnthropicStreamOptions): Anthrop
|
||||
const fail = (message: string) => {
|
||||
if (settled) return;
|
||||
settled = true;
|
||||
request?.cancel();
|
||||
options.onError(message);
|
||||
};
|
||||
const done = () => {
|
||||
if (settled) return;
|
||||
settled = true;
|
||||
request?.cancel();
|
||||
options.onDone({ text, content: finalizeBlocks(blocks), stopReason });
|
||||
};
|
||||
|
||||
if (!global.isAndroid) {
|
||||
// Deferred so the caller gets its handle back before the callback runs.
|
||||
setTimeout(() => fail("Anthropic API is only wired up on Android"), 0);
|
||||
return { cancel: () => {} };
|
||||
}
|
||||
if (!apiKey) {
|
||||
setTimeout(() => fail("No Anthropic API key set"), 0);
|
||||
return { cancel: () => {} };
|
||||
return { cancel: () => { settled = true; } };
|
||||
}
|
||||
|
||||
const body: any = {
|
||||
@@ -83,7 +80,7 @@ export function streamAnthropicMessage(options: AnthropicStreamOptions): Anthrop
|
||||
if (options.tools?.length) body.tools = options.tools;
|
||||
if (options.effort) body.output_config = { effort: options.effort };
|
||||
|
||||
const listener = new com.faceclaw.app.FaceclawSseListener({
|
||||
const listener: SseListener = {
|
||||
onLine: (line: string) => {
|
||||
if (settled) return;
|
||||
const event = parseSseDataLine(String(line));
|
||||
@@ -144,25 +141,19 @@ export function streamAnthropicMessage(options: AnthropicStreamOptions): Anthrop
|
||||
fail(describeHttpError(Number(code), String(errorBody)));
|
||||
},
|
||||
onComplete: () => {
|
||||
// Normally message_stop settles first; a clean EOF without it still
|
||||
// resolves with whatever streamed.
|
||||
done();
|
||||
// A transport EOF alone must not execute an unfinished tool call.
|
||||
fail("Anthropic stream ended before the response completed");
|
||||
},
|
||||
onFailure: (message: string) => {
|
||||
fail(`Anthropic connection failed: ${String(message)}`);
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
const headers = Array.create("java.lang.String", 4) as string[];
|
||||
headers[0] = "x-api-key";
|
||||
headers[1] = apiKey;
|
||||
headers[2] = "anthropic-version";
|
||||
headers[3] = ANTHROPIC_VERSION;
|
||||
try {
|
||||
request = new com.faceclaw.app.FaceclawSseRequest(ANTHROPIC_URL, JSON.stringify(body), headers, listener);
|
||||
request = openSseRequest(ANTHROPIC_URL, JSON.stringify(body), { "x-api-key": apiKey, "anthropic-version": ANTHROPIC_VERSION }, listener);
|
||||
} catch (error) {
|
||||
setTimeout(() => fail(`Anthropic request failed: ${String((error as Error)?.message ?? error)}`), 0);
|
||||
return { cancel: () => {} };
|
||||
return { cancel: () => { settled = true; } };
|
||||
}
|
||||
|
||||
return {
|
||||
|
||||
+11
-20
@@ -5,11 +5,11 @@ import type {
|
||||
LlmStreamOptions,
|
||||
} from "../assistant/llm-protocol";
|
||||
|
||||
declare const com: any;
|
||||
import { openSseRequest, type SseListener } from "./sse";
|
||||
|
||||
/**
|
||||
* Minimal streaming client for the OpenAI Responses API. NativeScript cannot
|
||||
* use the Node SDK, so this shares the okhttp/SSE bridge used by Anthropic.
|
||||
* use the Node SDK, so this shares the native streaming transport used by Anthropic.
|
||||
*/
|
||||
|
||||
const OPENAI_RESPONSES_URL = "https://api.openai.com/v1/responses";
|
||||
@@ -31,11 +31,13 @@ export function streamOpenAiResponse(options: LlmStreamOptions): LlmStreamHandle
|
||||
const fail = (message: string) => {
|
||||
if (settled) return;
|
||||
settled = true;
|
||||
request?.cancel();
|
||||
options.onError(message);
|
||||
};
|
||||
const done = () => {
|
||||
if (settled) return;
|
||||
settled = true;
|
||||
request?.cancel();
|
||||
const content = finalizeOutputs(outputs);
|
||||
const hasToolUse = content.some((block) => block.type === "tool_use");
|
||||
options.onDone({
|
||||
@@ -45,13 +47,9 @@ export function streamOpenAiResponse(options: LlmStreamOptions): LlmStreamHandle
|
||||
});
|
||||
};
|
||||
|
||||
if (!global.isAndroid) {
|
||||
setTimeout(() => fail("OpenAI API is only wired up on Android"), 0);
|
||||
return { cancel: () => {} };
|
||||
}
|
||||
if (!apiKey) {
|
||||
setTimeout(() => fail("No OpenAI API key set"), 0);
|
||||
return { cancel: () => {} };
|
||||
return { cancel: () => { settled = true; } };
|
||||
}
|
||||
|
||||
const body: any = {
|
||||
@@ -73,7 +71,7 @@ export function streamOpenAiResponse(options: LlmStreamOptions): LlmStreamHandle
|
||||
}
|
||||
if (options.effort) body.reasoning = { effort: options.effort };
|
||||
|
||||
const listener = new com.faceclaw.app.FaceclawSseListener({
|
||||
const listener: SseListener = {
|
||||
onLine: (line: string) => {
|
||||
if (settled) return;
|
||||
const event = parseSseDataLine(String(line));
|
||||
@@ -164,26 +162,19 @@ export function streamOpenAiResponse(options: LlmStreamOptions): LlmStreamHandle
|
||||
fail(describeHttpError(Number(code), String(errorBody)));
|
||||
},
|
||||
onComplete: () => {
|
||||
done();
|
||||
// A transport EOF alone must not execute an unfinished tool call.
|
||||
fail("OpenAI stream ended before the response completed");
|
||||
},
|
||||
onFailure: (message: string) => {
|
||||
fail(`OpenAI connection failed: ${String(message)}`);
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
const headers = Array.create("java.lang.String", 2) as string[];
|
||||
headers[0] = "Authorization";
|
||||
headers[1] = `Bearer ${apiKey}`;
|
||||
try {
|
||||
request = new com.faceclaw.app.FaceclawSseRequest(
|
||||
OPENAI_RESPONSES_URL,
|
||||
JSON.stringify(body),
|
||||
headers,
|
||||
listener,
|
||||
);
|
||||
request = openSseRequest(OPENAI_RESPONSES_URL, JSON.stringify(body), { Authorization: `Bearer ${apiKey}` }, listener);
|
||||
} catch (error) {
|
||||
setTimeout(() => fail(`OpenAI request failed: ${String((error as Error)?.message ?? error)}`), 0);
|
||||
return { cancel: () => {} };
|
||||
return { cancel: () => { settled = true; } };
|
||||
}
|
||||
|
||||
return {
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
export type SseListener = {
|
||||
onLine(line: string): void;
|
||||
onHttpError(code: number, body: string): void;
|
||||
onComplete(): void;
|
||||
onFailure(message: string): void;
|
||||
};
|
||||
@@ -0,0 +1,28 @@
|
||||
import { type SseListener } from './sse-types';
|
||||
import { withUserAgent } from '../util/http';
|
||||
export { type SseListener } from './sse-types';
|
||||
declare const FaceclawSseRequest: any;
|
||||
|
||||
/** Drain native events on the requesting JS isolate, never on a URLSession thread. */
|
||||
export function openSseRequest(url: string, body: string, headers: Record<string, string>, listener: SseListener): { cancel(): void } {
|
||||
const native = FaceclawSseRequest.alloc().initWithURLBodyHeaders(url, body, JSON.stringify(withUserAgent(headers)));
|
||||
let ended = false;
|
||||
const cancel = () => { if (ended) return; ended = true; clearInterval(timer); native.cancel(); };
|
||||
const timer = setInterval(() => {
|
||||
try {
|
||||
for (const event of JSON.parse(native.takeEvents())) {
|
||||
if (ended) break;
|
||||
if (event.kind === 'line') listener.onLine(event.text);
|
||||
else {
|
||||
cancel();
|
||||
if (event.kind === 'complete') listener.onComplete();
|
||||
else if (event.kind === 'http-error') listener.onHttpError(event.code, event.text);
|
||||
else listener.onFailure(event.text);
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
cancel(); listener.onFailure(String((error as Error)?.message ?? error));
|
||||
}
|
||||
}, 25);
|
||||
return { cancel };
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import { type SseListener } from './sse-types';
|
||||
export { type SseListener } from './sse-types';
|
||||
declare const com: any;
|
||||
|
||||
/** Android delivers callbacks on the requesting isolate's Looper. */
|
||||
export function openSseRequest(url: string, body: string, headers: Record<string, string>, listener: SseListener): { cancel(): void } {
|
||||
const entries = Object.entries(headers);
|
||||
const nativeHeaders = Array.create('java.lang.String', entries.length * 2);
|
||||
entries.forEach(([name, value], index) => { nativeHeaders[index * 2] = name; nativeHeaders[index * 2 + 1] = value; });
|
||||
return new com.faceclaw.app.FaceclawSseRequest(url, body, nativeHeaders, new com.faceclaw.app.FaceclawSseListener(listener));
|
||||
}
|
||||
@@ -10,7 +10,7 @@ import {
|
||||
import { getDefaultSmallFont } from "~/graphics/ui-fonts";
|
||||
import { wrapText } from "~/graphics/textwrap";
|
||||
import {
|
||||
ASSISTANT_MODEL_VALUES,
|
||||
ASSISTANT_MODEL_CHOICES,
|
||||
assistantModelLabel,
|
||||
assistantModelProvider,
|
||||
type AssistantModel,
|
||||
@@ -595,7 +595,7 @@ export const assistantSkipConfirmationSetting = new ConfigSettingBoolean({
|
||||
export type AssistantBackendKind = "direct" | "external";
|
||||
|
||||
const assistantBackendLabels: Record<AssistantBackendKind, string> = {
|
||||
direct: "On-phone",
|
||||
direct: global.isIOS ? "Cloud API" : "On-phone",
|
||||
external: "My own agent (bridge)",
|
||||
};
|
||||
|
||||
@@ -604,10 +604,10 @@ export const assistantBackendSetting = new ConfigSettingEnum<AssistantBackendKin
|
||||
label: "Assistant backend",
|
||||
storageKey: "assistant.backend",
|
||||
defaultValue: "direct",
|
||||
values: ["direct", "external"],
|
||||
values: global.isIOS ? ["direct"] : ["direct", "external"],
|
||||
formatValue: (value) => assistantBackendLabels[value] ?? value,
|
||||
description:
|
||||
"Who answers assistant queries: an LLM called from the phone (a cloud API with your key, or the downloaded on-phone model), or your own long-running agent (e.g. OpenClaw) reached through the faceclaw-agent-bridge plugin.",
|
||||
global.isIOS ? "Cloud models called with your OpenAI or Anthropic API key." : "Who answers assistant queries: an LLM called from the phone (a cloud API with your key, or the downloaded on-phone model), or your own long-running agent (e.g. OpenClaw) reached through the faceclaw-agent-bridge plugin.",
|
||||
});
|
||||
|
||||
export const assistantBridgeHostSetting = new ConfigSettingString({
|
||||
@@ -700,7 +700,7 @@ export const assistantModelSetting = new ConfigSettingEnum<AssistantModel>({
|
||||
label: "Assistant model",
|
||||
storageKey: "assistant.model",
|
||||
defaultValue: "auto",
|
||||
values: ASSISTANT_MODEL_VALUES,
|
||||
values: ASSISTANT_MODEL_CHOICES,
|
||||
formatValue: assistantModelLabel,
|
||||
isDisabled: (value) => {
|
||||
const provider = assistantModelProvider(value);
|
||||
@@ -710,7 +710,7 @@ export const assistantModelSetting = new ConfigSettingEnum<AssistantModel>({
|
||||
return false;
|
||||
},
|
||||
description:
|
||||
"Model used by the voice assistant. Auto prefers Terra when an OpenAI key is set, then Sonnet when an Anthropic key is set, then the downloaded on-phone model.",
|
||||
global.isIOS ? "Model used by the voice assistant. Auto prefers Terra with an OpenAI key, then Sonnet with an Anthropic key." : "Model used by the voice assistant. Auto prefers Terra when an OpenAI key is set, then Sonnet when an Anthropic key is set, then the downloaded on-phone model.",
|
||||
});
|
||||
|
||||
export const mapboxApiKeySetting = new ConfigSettingString({
|
||||
|
||||
@@ -130,14 +130,16 @@ function settingsSections(): SettingsSection[] {
|
||||
// On-phone LLM loop vs the user's own agent via the bridge plugin.
|
||||
enumSettingMenuItem(assistantBackendSetting),
|
||||
enumSettingMenuItem(assistantModelSetting),
|
||||
localModelMenuItem(),
|
||||
...(!global.isIOS ? [localModelMenuItem()] : []),
|
||||
// When on, a wakeword utterance goes straight to the assistant with no
|
||||
// Send/Type menu step.
|
||||
toggleSettingMenuItem(assistantSkipConfirmationSetting),
|
||||
...(!global.isIOS ? [
|
||||
textSettingMenuItem(assistantBridgeHostSetting),
|
||||
textSettingMenuItem(assistantBridgePortSetting),
|
||||
textSettingMenuItem(assistantBridgeTokenSetting),
|
||||
toggleSettingMenuItem(assistantAllowProactiveSetting),
|
||||
] : []),
|
||||
],
|
||||
},
|
||||
{
|
||||
|
||||
@@ -1067,7 +1067,7 @@ class Shell {
|
||||
*/
|
||||
private buildVoiceSendTargets(): VoiceSendTarget[] {
|
||||
const targets: VoiceSendTarget[] = [];
|
||||
if (!global.isIOS && this.isAssistantAvailable()) {
|
||||
if (this.isAssistantAvailable()) {
|
||||
targets.push({
|
||||
id: "assistant",
|
||||
label: "Send to Assistant",
|
||||
@@ -1254,7 +1254,7 @@ class Shell {
|
||||
this.showAlert(
|
||||
assistantBackendSetting.get() === "external"
|
||||
? "Configure the agent bridge host and token in Settings."
|
||||
: "Set an API key or download the on-phone model in Settings.",
|
||||
: global.isIOS ? "Set an OpenAI or Anthropic API key in Settings." : "Set an API key or download the on-phone model in Settings.",
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -224,7 +224,7 @@ export class VoiceInputLayer implements Layer {
|
||||
private menuRows(): Array<{ label: string; dim: boolean; onSelect: () => void }> {
|
||||
const text = this.displayText().trim();
|
||||
const hasText = text.length > 0 && !this.stoppingCapture;
|
||||
const hasLlmKey = !global.isIOS && anthropicApiKeySetting.get().trim().length > 0;
|
||||
const hasLlmKey = anthropicApiKeySetting.get().trim().length > 0;
|
||||
const rows: Array<{ label: string; dim: boolean; onSelect: () => void }> = [];
|
||||
for (const target of this.sendTargets) {
|
||||
rows.push({
|
||||
@@ -238,7 +238,7 @@ export class VoiceInputLayer implements Layer {
|
||||
});
|
||||
}
|
||||
rows.push({
|
||||
label: global.isIOS ? "Continue (Not available on iOS)" : hasLlmKey ? "Continue" : "Continue (Needs LLM API key)",
|
||||
label: hasLlmKey ? "Continue" : "Continue (Needs LLM API key)",
|
||||
dim: !hasLlmKey || this.stoppingCapture,
|
||||
onSelect: () => {
|
||||
if (hasLlmKey && !this.stoppingCapture) this.startContinuation();
|
||||
|
||||
@@ -46,7 +46,7 @@ function conversations(saved = '') {
|
||||
const env = sessions();
|
||||
const { AssistantConversations } = load('app/assistant/conversations.ts', {
|
||||
'./session': { AssistantSession: env.AssistantSession },
|
||||
'./models': { ASSISTANT_MODEL_VALUES: ['auto', 'terra', 'sonnet'] },
|
||||
'./models': { supportedAssistantModel: value => value, ASSISTANT_MODEL_VALUES: ['auto', 'terra', 'sonnet'] },
|
||||
});
|
||||
let stored = '';
|
||||
const store = new AssistantConversations((model, effort) => config(model, effort), () => 'terra', (value) => { stored = value; }, saved);
|
||||
|
||||
@@ -58,9 +58,9 @@ test('iOS worker frames preserve baked grayscale bytes through the message bound
|
||||
test('settings-driven repaint runs after font cache invalidation, regardless of subscription order', () => {
|
||||
const listeners = [], tasks = [], painted = [];
|
||||
let cachedFont = 'Light';
|
||||
const api = load('app/ui/dashboard-settings.ts', {
|
||||
const api = load('app/ui/dashboard-settings.ts', { global: { isIOS: true },
|
||||
require: id => id.includes('settings-store') ? { onSettingsStoreChanged: fn => listeners.push(fn) }
|
||||
: { Layer: class {}, ASSISTANT_MODEL_VALUES: [] },
|
||||
: { Layer: class {}, ASSISTANT_MODEL_CHOICES: [] },
|
||||
setTimeout: fn => tasks.push(fn),
|
||||
});
|
||||
api.onAnySettingChanged(() => painted.push(cachedFont));
|
||||
@@ -89,6 +89,10 @@ test('background glasses input still composites frames; phone resume preserves t
|
||||
}
|
||||
const settings = { onAnySettingChanged: () => () => {}, previewColorSetting: { get: () => 'white' } };
|
||||
const modules = {
|
||||
'../assistant/system-tools': { registerSystemTools() {} },
|
||||
'../assistant/window-tools': { registerWindowTools() {} },
|
||||
'../assistant/navigate-tools': { registerNavigateTools() {} },
|
||||
'../assistant/roam-tools': { registerRoamTools() {} },
|
||||
"../native/ios-navigation-sensors": {},
|
||||
'../native/compass.ios': { bindCompassSession() {}, receiveCompassEvent() {} },
|
||||
'@nativescript/core': { File: { fromPath: () => ({ writeTextSync() {} }) }, knownFolders: { documents: () => ({ path: '/tmp' }) }, path },
|
||||
@@ -158,6 +162,10 @@ test('iOS bandwidth footer toggles live, polls only in foreground and resets its
|
||||
on: (key, fn) => appEvents.set(key, fn), off: key => appEvents.delete(key) },
|
||||
Button: View, Color: class {}, Dialogs: {}, GridLayout: View, Image: View, Label: View, Page: View, StackLayout: View };
|
||||
const modules = {
|
||||
'../assistant/system-tools': { registerSystemTools() {} },
|
||||
'../assistant/window-tools': { registerWindowTools() {} },
|
||||
'../assistant/navigate-tools': { registerNavigateTools() {} },
|
||||
'../assistant/roam-tools': { registerRoamTools() {} },
|
||||
"../native/ios-navigation-sensors": {},
|
||||
'../native/compass.ios': { bindCompassSession() {}, receiveCompassEvent() {} },
|
||||
'@nativescript/core': core,
|
||||
|
||||
@@ -0,0 +1,129 @@
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
const { loader } = require('./helpers/load-typescript.cjs');
|
||||
const flush = () => new Promise(resolve => setImmediate(resolve));
|
||||
|
||||
function assistant(provider) {
|
||||
const requests = [], calls = [], deltas = [], errors = [], done = [];
|
||||
const registry = { listTools: () => [{ name: 'timer.set', description: 'Set a timer', inputSchema: { type: 'object' } }],
|
||||
callTool: async (name, args) => { calls.push({ name, args }); return { ok: true, content: 'Timer set' }; } };
|
||||
const load = loader({ global: { isIOS: true, isAndroid: false }, setTimeout, clearTimeout }, {
|
||||
'./sse': { openSseRequest: (url, body, headers, listener) => {
|
||||
const request = { url, body: JSON.parse(body), headers, listener, cancelled: false };
|
||||
requests.push(request); return { cancel: () => { request.cancelled = true; } };
|
||||
} },
|
||||
'../native/llama': { streamLocalQwen: () => assert.fail('iOS must not invoke a local LLM') },
|
||||
'./bridge-client': { assistantBridge: {} }, './tool-registry': { toolRegistry: registry },
|
||||
'../prompts': { ASSISTANT_SYSTEM_PROMPT_BASE: 'test', buildAssistantSystemPrompt: () => 'test', describeAssistantContext: () => '' },
|
||||
});
|
||||
const { AssistantSession } = load('app/assistant/session.ts');
|
||||
const session = new AssistantSession({ kind: 'direct', llm: { selection: 'auto', provider, model: 'fixture-model', apiKey: 'fixture-key', effort: 'low' } }, registry);
|
||||
const send = text => session.sendUtterance(text, {}, { onTextDelta: (_delta, full) => deltas.push(full),
|
||||
onToolActivity() {}, onError: error => errors.push(error), onTurnDone: result => done.push(result) });
|
||||
const event = (request, event) => request.listener.onLine('data: ' + JSON.stringify(event));
|
||||
function text(request, value) {
|
||||
if (provider === 'anthropic') {
|
||||
event(request, { type: 'content_block_start', index: 0, content_block: { type: 'text', text: '' } });
|
||||
event(request, { type: 'content_block_delta', index: 0, delta: { type: 'text_delta', text: value } });
|
||||
} else event(request, { type: 'response.output_text.delta', output_index: 0, delta: value });
|
||||
}
|
||||
function finish(request, tool = false) {
|
||||
if (provider === 'anthropic') {
|
||||
event(request, { type: 'message_delta', delta: { stop_reason: tool ? 'tool_use' : 'end_turn' } });
|
||||
event(request, { type: 'message_stop' });
|
||||
} else event(request, { type: 'response.completed' });
|
||||
}
|
||||
function tool(request) {
|
||||
if (provider === 'anthropic') {
|
||||
event(request, { type: 'content_block_start', index: 1, content_block: { type: 'tool_use', id: 'tool-1', name: 'timer_set' } });
|
||||
event(request, { type: 'content_block_delta', index: 1, delta: { type: 'input_json_delta', partial_json: '{"seconds":' } });
|
||||
event(request, { type: 'content_block_delta', index: 1, delta: { type: 'input_json_delta', partial_json: '60}' } });
|
||||
} else {
|
||||
event(request, { type: 'response.output_item.done', output_index: 1, item: { type: 'reasoning', id: 'reason', encrypted_content: 'opaque-fixture' } });
|
||||
event(request, { type: 'response.output_item.added', output_index: 2, item: { type: 'function_call', call_id: 'tool-1', id: 'item-1', name: 'timer_set', arguments: '' } });
|
||||
event(request, { type: 'response.function_call_arguments.delta', output_index: 2, delta: '{"seconds":' });
|
||||
event(request, { type: 'response.function_call_arguments.delta', output_index: 2, delta: '60}' });
|
||||
}
|
||||
}
|
||||
return { load, session, send, event, text, finish, tool, requests, calls, deltas, errors, done };
|
||||
}
|
||||
|
||||
for (const provider of ['openai', 'anthropic']) {
|
||||
test(`iOS ${provider} streams, calls shared tools, continues with results, and preserves follow-up history`, async () => {
|
||||
const h = assistant(provider); h.send('Set a one-minute timer');
|
||||
const first = h.requests[0]; assert.equal(first.body.stream, true);
|
||||
if (provider === 'openai') { assert.equal(first.headers.Authorization, 'Bearer fixture-key'); assert.equal(first.body.store, false); }
|
||||
else assert.equal(first.headers['x-api-key'], 'fixture-key');
|
||||
h.text(first, 'Setting π 👓'); h.tool(first); h.finish(first, true); await flush();
|
||||
assert.equal(first.cancelled, true); assert.equal(h.calls[0].name, 'timer.set'); assert.equal(h.calls[0].args.seconds, 60);
|
||||
assert.equal(h.requests.length, 2);
|
||||
const second = h.requests[1];
|
||||
assert.match(JSON.stringify(second.body), /Timer set/);
|
||||
if (provider === 'openai') assert.match(JSON.stringify(second.body), /opaque-fixture/);
|
||||
h.text(second, 'Your timer is running.'); h.finish(second);
|
||||
assert.equal(h.session.isTurnActive(), false); assert.equal(h.done.length, 1); assert.equal(h.errors.length, 0);
|
||||
h.send('What did you set?');
|
||||
assert.match(JSON.stringify(h.requests[2].body), /Your timer is running/);
|
||||
h.session.cancel(); assert.equal(h.requests[2].cancelled, true);
|
||||
});
|
||||
test(`iOS ${provider} cancellation, HTTP errors and premature EOF never execute incomplete tools`, async () => {
|
||||
const h = assistant(provider); h.send('test'); const first = h.requests[0]; h.session.cancel();
|
||||
h.text(first, 'late'); h.tool(first); h.finish(first, true); await flush();
|
||||
assert.equal(h.calls.length, 0); assert.equal(h.deltas.length, 0);
|
||||
h.send('retry'); h.requests[1].listener.onHttpError(401, '{"error":{"message":"Invalid API key"}}');
|
||||
assert.equal(h.session.isTurnActive(), false); assert.match(h.errors[0], /key/i);
|
||||
h.send('retry'); const third = h.requests[2]; h.text(third, 'Partial'); h.tool(third); third.listener.onComplete(); await flush();
|
||||
assert.match(h.errors.at(-1), /before.*completed/); assert.equal(h.calls.length, 0); assert.equal(third.cancelled, true);
|
||||
});
|
||||
}
|
||||
|
||||
test('iOS SSE adapter invokes listeners on its polling thread and stops polling at cancellation/EOF', () => {
|
||||
const timers = new Set(), instances = [], seen = [];
|
||||
const api = loader({
|
||||
setInterval: fn => { timers.add(fn); return fn; }, clearInterval: fn => timers.delete(fn),
|
||||
FaceclawSseRequest: { alloc: () => ({ initWithURLBodyHeaders(url, body, headers) {
|
||||
const instance = { events: [], cancelled: false, takeEvents() { const events = this.events; this.events = []; return JSON.stringify(events); }, cancel() { this.cancelled = true; } };
|
||||
instances.push(instance); assert.equal(JSON.parse(headers)['User-Agent'], 'fixture'); return instance;
|
||||
} }) },
|
||||
}, { '../util/http': { withUserAgent: headers => ({ ...headers, 'User-Agent': 'fixture' }) } })('app/native/sse.ios.ts');
|
||||
const listener = { onLine: line => seen.push(line), onComplete: () => seen.push('complete'), onHttpError() {}, onFailure: error => assert.fail(error) };
|
||||
const handle = api.openSseRequest('https://fixture', '{}', {}, listener);
|
||||
instances[0].events.push({ kind: 'line', text: 'first' }); assert.deepEqual(seen, []);
|
||||
for (const tick of timers) tick(); assert.deepEqual(seen, ['first']);
|
||||
handle.cancel(); assert.equal(timers.size, 0); assert.equal(instances[0].cancelled, true);
|
||||
api.openSseRequest('https://fixture', '{}', {}, listener);
|
||||
instances[1].events.push({ kind: 'line', text: 'last' }, { kind: 'complete' });
|
||||
for (const tick of [...timers]) tick(); assert.deepEqual(seen, ['first', 'last', 'complete']); assert.equal(timers.size, 0);
|
||||
});
|
||||
|
||||
test('iOS only offers cloud assistant models and preserves old local-model conversations as Auto', () => {
|
||||
const api = loader({ global: { isIOS: true } }, { '../native/llama': { LOCAL_MODEL: { label: 'Qwen', id: 'qwen' }, isLocalModelReady: () => false } })('app/assistant/models.ts');
|
||||
assert.equal(api.ASSISTANT_MODEL_CHOICES.includes('qwen'), false);
|
||||
assert.equal(api.supportedAssistantModel('qwen'), 'auto');
|
||||
assert.equal(api.resolveAssistantModel('auto', { openai: 'key', anthropic: '' }).provider, 'openai');
|
||||
assert.equal(api.resolveAssistantModel('auto', { openai: '', anthropic: 'key' }).provider, 'anthropic');
|
||||
assert.equal(api.resolveAssistantModel('auto', { openai: '', anthropic: '' }), null);
|
||||
const { AssistantConversations } = loader({}, { './models': api, './session': {} })('app/assistant/conversations.ts');
|
||||
const history = { messages: [], transcript: [{ role: 'user', text: 'Keep this conversation' }] };
|
||||
const saved = JSON.stringify({ selectedId: 'old', conversations: [{ id: 'old', model: 'qwen', reasoning: 'default', history }] });
|
||||
const conversations = new AssistantConversations(() => null, () => 'auto', () => {}, saved);
|
||||
assert.equal(conversations.current().model, 'auto'); assert.equal(conversations.title(), 'Keep this conversation');
|
||||
});
|
||||
|
||||
test('cancelling during a tool prevents subsequent tool side effects and a stalled turn times out', async () => {
|
||||
let stream, completeTool, deadline;
|
||||
const calls = [], failures = [];
|
||||
const { DirectAssistantBackend } = loader({ setTimeout: fn => { deadline = fn; return 1; }, clearTimeout() {} }, {
|
||||
'../native/openai': { streamOpenAiResponse: options => { stream = options; return { cancel() {} }; } },
|
||||
'../native/anthropic': {}, '../native/llama': {},
|
||||
})('app/assistant/direct-backend.ts');
|
||||
const backend = new DirectAssistantBackend();
|
||||
const options = { provider: 'openai', messages: [], buildTools: () => [], resolveToolName: name => name,
|
||||
registry: { callTool: name => { calls.push(name); return new Promise(resolve => { completeTool = resolve; }); } },
|
||||
callbacks: { onTextDelta() {}, onToolActivity() {}, onTurnDone() {}, onError: message => failures.push(message) } };
|
||||
const handle = backend.runTurn(options);
|
||||
stream.onDone({ text: '', stopReason: 'tool_use', content: [1, 2].map(id => ({ type: 'tool_use', id: String(id), name: 'tool' + id, input: {} })) });
|
||||
handle.cancel(); completeTool({ ok: true, content: 'done' }); await flush();
|
||||
assert.deepEqual(calls, ['tool1']);
|
||||
backend.runTurn({ ...options, messages: [] }); deadline(); assert.match(failures[0], /too long/);
|
||||
});
|
||||
@@ -68,6 +68,10 @@ function fixture() {
|
||||
paint: () => { boardStats.paints++; return new images.GrayImage(100, 80, 200); } }),
|
||||
};
|
||||
const modules = {
|
||||
'../assistant/system-tools': { registerSystemTools() {} },
|
||||
'../assistant/window-tools': { registerWindowTools() {} },
|
||||
'../assistant/navigate-tools': { registerNavigateTools() {} },
|
||||
'../assistant/roam-tools': { registerRoamTools() {} },
|
||||
"../native/ios-navigation-sensors": {},
|
||||
"../native/compass.ios": { bindCompassSession() {}, receiveCompassEvent() {} },
|
||||
'@nativescript/core': { File: { fromPath: () => ({ writeTextSync() {} }) },
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
const { execFileSync, spawn } = require('node:child_process');
|
||||
const { mkdtempSync, rmSync } = require('node:fs');
|
||||
const { tmpdir } = require('node:os');
|
||||
const { join, resolve } = require('node:path');
|
||||
const http = require('node:http');
|
||||
const assert = require('node:assert/strict');
|
||||
const root = resolve(__dirname, '..'), dir = mkdtempSync(join(tmpdir(), 'faceclaw-sse-'));
|
||||
execFileSync('xcrun', ['clang', '-fobjc-arc', '-framework', 'Foundation', '-I', 'App_Resources/iOS/src',
|
||||
'tests/native/ios-sse-test.m', 'App_Resources/iOS/src/FaceclawSseRequest.m', '-o', join(dir, 'sse-test')], { cwd: root, stdio: 'inherit' });
|
||||
const server = http.createServer(async (request, response) => {
|
||||
assert.notEqual(request.url, '/redirected', 'Credentials must not follow redirects');
|
||||
assert.equal(request.headers.authorization, 'Bearer fixture-key');
|
||||
assert.equal(request.headers['user-agent'], 'Faceclaw-test');
|
||||
assert.equal(request.method, 'POST');
|
||||
const chunks = []; for await (const chunk of request) chunks.push(chunk);
|
||||
assert.deepEqual(JSON.parse(Buffer.concat(chunks)), { test: true });
|
||||
if (request.url === '/error') { response.writeHead(401); response.end('{"error":{"type":"invalid_api_key"}}'); return; }
|
||||
if (request.url === '/redirect') { response.writeHead(302, { Location: '/redirected' }); response.end(); return; }
|
||||
response.writeHead(200, { 'Content-Type': 'text/event-stream', ...(request.url === '/disconnect' ? { 'Content-Length': '1000' } : {}) });
|
||||
if (request.url === '/overflow') { response.end('x'.repeat(2 * 1024 * 1024 + 1)); return; }
|
||||
if (request.url === '/disconnect') {
|
||||
response.write('data: partial'); setTimeout(() => response.destroy(), 50); return;
|
||||
}
|
||||
if (request.url === '/cancel') {
|
||||
response.write('data: first\n'); const timer = setTimeout(() => response.end('data: late\n'), 200);
|
||||
response.on('close', () => clearTimeout(timer)); return;
|
||||
}
|
||||
// Split UTF-8 characters and CRLF boundaries across individual network writes.
|
||||
const bytes = Buffer.from('\uFEFFdata: π 👓\r\n\r\n: ping\rdata: last');
|
||||
let index = 0;
|
||||
const timer = setInterval(() => {
|
||||
if (index === bytes.length) { clearInterval(timer); response.end(); }
|
||||
else response.write(bytes.subarray(index, ++index));
|
||||
}, 2);
|
||||
response.on('close', () => clearInterval(timer));
|
||||
});
|
||||
server.listen(0, '127.0.0.1', async () => {
|
||||
try {
|
||||
for (const mode of ['stream', 'error', 'redirect', 'cancel', 'overflow', 'disconnect']) await new Promise((resolve, reject) => {
|
||||
const child = spawn(join(dir, 'sse-test'), [`http://127.0.0.1:${server.address().port}/${mode}`, mode], { stdio: 'inherit' });
|
||||
child.on('error', reject); child.on('exit', code => code === 0 ? resolve() : reject(new Error(`${mode}: exit ${code}`)));
|
||||
});
|
||||
} catch (error) { console.error(error); process.exitCode = 1; }
|
||||
finally { server.closeAllConnections(); server.close(); rmSync(dir, { recursive: true, force: true }); }
|
||||
});
|
||||
@@ -0,0 +1,38 @@
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "FaceclawSseRequest.h"
|
||||
|
||||
int main(int argc, const char **argv) {
|
||||
@autoreleasepool {
|
||||
NSString *mode = [NSString stringWithUTF8String:argv[2]];
|
||||
FaceclawSseRequest *request = [[FaceclawSseRequest alloc] initWithURL:[NSString stringWithUTF8String:argv[1]]
|
||||
body:@"{\"test\":true}" headers:@"{\"Authorization\":\"Bearer fixture-key\",\"User-Agent\":\"Faceclaw-test\"}"];
|
||||
NSMutableArray *lines = [NSMutableArray new];
|
||||
NSDictionary *terminal = nil;
|
||||
NSDate *deadline = [NSDate dateWithTimeIntervalSinceNow:10];
|
||||
while (!terminal && deadline.timeIntervalSinceNow > 0) {
|
||||
NSArray *events = [NSJSONSerialization JSONObjectWithData:[[request takeEvents] dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
|
||||
for (NSDictionary *event in events) {
|
||||
if ([event[@"kind"] isEqual:@"line"]) [lines addObject:event[@"text"]];
|
||||
else terminal = event;
|
||||
}
|
||||
if ([mode isEqual:@"cancel"] && lines.count) {
|
||||
[request cancel];
|
||||
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.3]];
|
||||
if (![[request takeEvents] isEqual:@"[]"]) return 1;
|
||||
puts("PASS native SSE cancellation suppresses later callbacks"); return 0;
|
||||
}
|
||||
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.01]];
|
||||
}
|
||||
[request cancel];
|
||||
BOOL ok = NO;
|
||||
if ([mode isEqual:@"stream"]) ok = [terminal[@"kind"] isEqual:@"complete"] &&
|
||||
[lines isEqual:@[@"data: π 👓", @"", @": ping", @"data: last"]];
|
||||
if ([mode isEqual:@"error"]) ok = [terminal[@"kind"] isEqual:@"http-error"] &&
|
||||
[terminal[@"code"] intValue] == 401 && [terminal[@"text"] containsString:@"invalid_api_key"];
|
||||
if ([mode isEqual:@"redirect"]) ok = [terminal[@"kind"] isEqual:@"http-error"] && [terminal[@"code"] intValue] == 302;
|
||||
if ([mode isEqual:@"overflow"] || [mode isEqual:@"disconnect"]) ok = [terminal[@"kind"] isEqual:@"failure"];
|
||||
if (!ok) { NSLog(@"FAIL %@ lines=%@ terminal=%@", mode, lines, terminal); return 1; }
|
||||
printf("PASS native SSE %s\n", mode.UTF8String);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user