Port assistant integration (with cloud AIs) to iOS

This commit is contained in:
James Babcock committed 2026-09-19 00:02:15 -07:00
1 parent 9e18745e53
commit 617cdc39eb
25 files changed
+508 -63

No files matched your search

+2 -2
View File
@@ -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. */ }
+12 -1
View File
@@ -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);
+7
View File
@@ -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 = {
+1 -1
View File
@@ -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";
+4 -1
View File
@@ -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",