forked from Blendman974/faceclaw
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
+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));
|
||||
}
|
||||
Reference in new issue
Block a user