@glassly/engine 0.3.5 → 0.3.6-beta.851
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/engine.d.ts +11 -1
- package/build/engine.d.ts.map +1 -1
- package/build/engine.js +13 -1
- package/build/engine.js.map +1 -1
- package/build/facades/ai.d.ts +3 -3
- package/build/facades/ai.d.ts.map +1 -1
- package/build/facades/ai.js +3 -3
- package/build/facades/ai.js.map +1 -1
- package/build/facades/glasses.d.ts +5 -1
- package/build/facades/glasses.d.ts.map +1 -1
- package/build/facades/glasses.js +39 -30
- package/build/facades/glasses.js.map +1 -1
- package/build/facades/memory.d.ts +10 -2
- package/build/facades/memory.d.ts.map +1 -1
- package/build/facades/memory.js +10 -0
- package/build/facades/memory.js.map +1 -1
- package/build/facades/miniapps.d.ts +2 -0
- package/build/facades/miniapps.d.ts.map +1 -1
- package/build/facades/miniapps.js +2 -0
- package/build/facades/miniapps.js.map +1 -1
- package/build/facades/pairing.d.ts.map +1 -1
- package/build/facades/pairing.js +3 -2
- package/build/facades/pairing.js.map +1 -1
- package/build/facades/providerKeys.d.ts +6 -2
- package/build/facades/providerKeys.d.ts.map +1 -1
- package/build/facades/providerKeys.js +6 -2
- package/build/facades/providerKeys.js.map +1 -1
- package/build/generated/releaseMetadata.js +4 -4
- package/build/generated/releaseMetadata.js.map +1 -1
- package/build/services/AiTierResolution.d.ts +3 -2
- package/build/services/AiTierResolution.d.ts.map +1 -1
- package/build/services/AiTierResolution.js +7 -2
- package/build/services/AiTierResolution.js.map +1 -1
- package/build/services/AppRegistry.d.ts +4 -1
- package/build/services/AppRegistry.d.ts.map +1 -1
- package/build/services/AppRegistry.js +16 -2
- package/build/services/AppRegistry.js.map +1 -1
- package/build/services/BlobStore.js +3 -3
- package/build/services/BlobStore.js.map +1 -1
- package/build/services/BluetoothRestoreLaunch.d.ts +8 -0
- package/build/services/BluetoothRestoreLaunch.d.ts.map +1 -0
- package/build/services/BluetoothRestoreLaunch.js +34 -0
- package/build/services/BluetoothRestoreLaunch.js.map +1 -0
- package/build/services/CloudClientService.d.ts +4 -4
- package/build/services/ForegroundLocationPermission.d.ts +1 -1
- package/build/services/ForegroundLocationPermission.d.ts.map +1 -1
- package/build/services/ForegroundLocationPermission.js.map +1 -1
- package/build/services/GestureDotsService.d.ts.map +1 -1
- package/build/services/GestureDotsService.js +2 -1
- package/build/services/GestureDotsService.js.map +1 -1
- package/build/services/GlassesTouchInput.d.ts +6 -4
- package/build/services/GlassesTouchInput.d.ts.map +1 -1
- package/build/services/GlassesTouchInput.js +81 -41
- package/build/services/GlassesTouchInput.js.map +1 -1
- package/build/services/LocalDisplayManager.d.ts +12 -4
- package/build/services/LocalDisplayManager.d.ts.map +1 -1
- package/build/services/LocalDisplayManager.js +21 -7
- package/build/services/LocalDisplayManager.js.map +1 -1
- package/build/services/LocalLlmModelManager.d.ts +3 -1
- package/build/services/LocalLlmModelManager.d.ts.map +1 -1
- package/build/services/LocalLlmModelManager.js +13 -5
- package/build/services/LocalLlmModelManager.js.map +1 -1
- package/build/services/LocalLlmPrompt.d.ts +10 -9
- package/build/services/LocalLlmPrompt.d.ts.map +1 -1
- package/build/services/LocalLlmPrompt.js +43 -32
- package/build/services/LocalLlmPrompt.js.map +1 -1
- package/build/services/LocalLlmProvider.d.ts +1 -0
- package/build/services/LocalLlmProvider.d.ts.map +1 -1
- package/build/services/LocalLlmProvider.js +62 -22
- package/build/services/LocalLlmProvider.js.map +1 -1
- package/build/services/LocalMiniappRuntime.d.ts +28 -6
- package/build/services/LocalMiniappRuntime.d.ts.map +1 -1
- package/build/services/LocalMiniappRuntime.js +190 -148
- package/build/services/LocalMiniappRuntime.js.map +1 -1
- package/build/services/MemoryStore.d.ts +13 -1
- package/build/services/MemoryStore.d.ts.map +1 -1
- package/build/services/MemoryStore.js +98 -1
- package/build/services/MemoryStore.js.map +1 -1
- package/build/services/MiniappExitGesture.d.ts +6 -3
- package/build/services/MiniappExitGesture.d.ts.map +1 -1
- package/build/services/MiniappExitGesture.js +4 -4
- package/build/services/MiniappExitGesture.js.map +1 -1
- package/build/services/MiniappWidgetMode.d.ts +6 -4
- package/build/services/MiniappWidgetMode.d.ts.map +1 -1
- package/build/services/MiniappWidgetMode.js +7 -6
- package/build/services/MiniappWidgetMode.js.map +1 -1
- package/build/services/ProviderKeyValidation.d.ts +2 -0
- package/build/services/ProviderKeyValidation.d.ts.map +1 -1
- package/build/services/ProviderKeyValidation.js +4 -0
- package/build/services/ProviderKeyValidation.js.map +1 -1
- package/build/services/SceneRenderer.d.ts.map +1 -1
- package/build/services/SceneRenderer.js +3 -1
- package/build/services/SceneRenderer.js.map +1 -1
- package/build/services/ScreenshotFixture.d.ts +56 -0
- package/build/services/ScreenshotFixture.d.ts.map +1 -0
- package/build/services/ScreenshotFixture.js +72 -0
- package/build/services/ScreenshotFixture.js.map +1 -0
- package/build/services/actionInvoke.d.ts +37 -0
- package/build/services/actionInvoke.d.ts.map +1 -0
- package/build/services/actionInvoke.js +89 -0
- package/build/services/actionInvoke.js.map +1 -0
- package/build/services/appleWatch/AppleWatchLink.d.ts +42 -0
- package/build/services/appleWatch/AppleWatchLink.d.ts.map +1 -0
- package/build/services/appleWatch/AppleWatchLink.js +259 -0
- package/build/services/appleWatch/AppleWatchLink.js.map +1 -0
- package/build/services/controllers.d.ts.map +1 -1
- package/build/services/controllers.js +16 -0
- package/build/services/controllers.js.map +1 -1
- package/build/services/manifestActions.d.ts +4 -2
- package/build/services/manifestActions.d.ts.map +1 -1
- package/build/services/manifestActions.js +9 -3
- package/build/services/manifestActions.js.map +1 -1
- package/build/services/memoryBackupFile.d.ts +9 -0
- package/build/services/memoryBackupFile.d.ts.map +1 -0
- package/build/services/memoryBackupFile.js +47 -0
- package/build/services/memoryBackupFile.js.map +1 -0
- package/build/services/settingsBridge.d.ts +4 -4
- package/build/services/settingsBridge.d.ts.map +1 -1
- package/build/services/settingsBridge.js +9 -4
- package/build/services/settingsBridge.js.map +1 -1
- package/build/services/widgetPlacements.d.ts +8 -7
- package/build/services/widgetPlacements.d.ts.map +1 -1
- package/build/services/widgetPlacements.js +7 -6
- package/build/services/widgetPlacements.js.map +1 -1
- package/build/stores/apps.d.ts +6 -0
- package/build/stores/apps.d.ts.map +1 -1
- package/build/stores/apps.js +29 -12
- package/build/stores/apps.js.map +1 -1
- package/build/stores/bluetoothSettingKeys.d.ts +2 -0
- package/build/stores/bluetoothSettingKeys.d.ts.map +1 -1
- package/build/stores/bluetoothSettingKeys.js +17 -0
- package/build/stores/bluetoothSettingKeys.js.map +1 -1
- package/build/stores/settings.d.ts.map +1 -1
- package/build/stores/settings.js +42 -2
- package/build/stores/settings.js.map +1 -1
- package/build/types/applet.d.ts +5 -1
- package/build/types/applet.d.ts.map +1 -1
- package/build/types/applet.js.map +1 -1
- package/build/types/capabilities/rayneo-io.d.ts.map +1 -1
- package/build/types/capabilities/rayneo-io.js +2 -0
- package/build/types/capabilities/rayneo-io.js.map +1 -1
- package/build/types/enums.d.ts +2 -1
- package/build/types/enums.d.ts.map +1 -1
- package/build/types/enums.js +1 -0
- package/build/types/enums.js.map +1 -1
- package/build/utils/cloudClient/RnUdpAdapter.d.ts.map +1 -1
- package/build/utils/cloudClient/RnUdpAdapter.js +13 -2
- package/build/utils/cloudClient/RnUdpAdapter.js.map +1 -1
- package/build/utils/display/scene/index.d.ts +1 -1
- package/build/utils/display/scene/index.d.ts.map +1 -1
- package/build/utils/display/scene/index.js.map +1 -1
- package/build/utils/display/scene/process.d.ts.map +1 -1
- package/build/utils/display/scene/process.js +20 -1
- package/build/utils/display/scene/process.js.map +1 -1
- package/build/utils/display/scene/types.d.ts +1 -1
- package/build/utils/display/scene/types.d.ts.map +1 -1
- package/build/utils/display/scene/types.js.map +1 -1
- package/build/utils/miniappGlobals.d.ts +6 -0
- package/build/utils/miniappGlobals.d.ts.map +1 -1
- package/build/utils/miniappGlobals.js +28 -4
- package/build/utils/miniappGlobals.js.map +1 -1
- package/package.json +9 -9
- package/src/engine.ts +13 -1
- package/src/facades/ai.ts +3 -3
- package/src/facades/glasses.ts +65 -42
- package/src/facades/memory.ts +11 -2
- package/src/facades/miniapps.ts +3 -0
- package/src/facades/pairing.ts +4 -2
- package/src/facades/providerKeys.ts +6 -1
- package/src/generated/releaseMetadata.ts +4 -4
- package/src/services/AiTierResolution.ts +9 -3
- package/src/services/AppRegistry.ts +18 -3
- package/src/services/BlobStore.ts +3 -3
- package/src/services/BluetoothRestoreLaunch.ts +37 -0
- package/src/services/ForegroundLocationPermission.ts +1 -1
- package/src/services/GestureDotsService.ts +2 -1
- package/src/services/GlassesTouchInput.ts +86 -38
- package/src/services/LocalDisplayManager.ts +22 -8
- package/src/services/LocalLlmModelManager.ts +16 -6
- package/src/services/LocalLlmPrompt.ts +56 -37
- package/src/services/LocalLlmProvider.ts +74 -24
- package/src/services/LocalMiniappRuntime.ts +228 -147
- package/src/services/MemoryStore.ts +111 -2
- package/src/services/MiniappExitGesture.ts +8 -5
- package/src/services/MiniappWidgetMode.ts +9 -7
- package/src/services/ProviderKeyValidation.ts +5 -0
- package/src/services/SceneRenderer.ts +3 -1
- package/src/services/ScreenshotFixture.ts +113 -0
- package/src/services/actionInvoke.ts +105 -0
- package/src/services/appleWatch/AppleWatchLink.ts +297 -0
- package/src/services/controllers.ts +17 -0
- package/src/services/manifestActions.ts +16 -4
- package/src/services/memoryBackupFile.ts +50 -0
- package/src/services/settingsBridge.ts +9 -4
- package/src/services/widgetPlacements.ts +23 -9
- package/src/stores/apps.ts +35 -11
- package/src/stores/bluetoothSettingKeys.ts +20 -0
- package/src/stores/settings.ts +44 -2
- package/src/types/applet.ts +5 -1
- package/src/types/capabilities/rayneo-io.ts +2 -0
- package/src/types/enums.ts +1 -0
- package/src/utils/cloudClient/RnUdpAdapter.ts +13 -1
- package/src/utils/display/scene/index.ts +1 -0
- package/src/utils/display/scene/process.ts +17 -1
- package/src/utils/display/scene/types.ts +1 -0
- package/src/utils/miniappGlobals.ts +30 -4
|
@@ -55,32 +55,16 @@ export function toTemplateTools(tools) {
|
|
|
55
55
|
function: { name: tool.name, description: tool.description, parameters: tool.inputSchema },
|
|
56
56
|
}));
|
|
57
57
|
}
|
|
58
|
-
/** Rough tokens-per-character for a BPE vocabulary over mixed prose/JSON. */
|
|
59
|
-
const CHARS_PER_TOKEN = 3.2;
|
|
60
|
-
/** Template framing per message (`<|im_start|>role … <|im_end|>`). */
|
|
61
|
-
const TOKENS_PER_MESSAGE = 6;
|
|
62
|
-
export function estimateTokens(text) {
|
|
63
|
-
return Math.ceil(text.length / CHARS_PER_TOKEN);
|
|
64
|
-
}
|
|
65
|
-
export function estimateMessageTokens(message) {
|
|
66
|
-
let total = TOKENS_PER_MESSAGE + estimateTokens(message.content);
|
|
67
|
-
if (message.role === "assistant" && message.tool_calls) {
|
|
68
|
-
for (const call of message.tool_calls)
|
|
69
|
-
total += estimateTokens(JSON.stringify(call.function)) + 8;
|
|
70
|
-
}
|
|
71
|
-
return total;
|
|
72
|
-
}
|
|
73
|
-
const MIN_TOOL_CONTENT_CHARS = 1_200;
|
|
74
58
|
/**
|
|
75
|
-
*
|
|
76
|
-
*
|
|
77
|
-
*
|
|
78
|
-
*
|
|
79
|
-
* When that last turn alone still does not fit, tool outputs are cut to
|
|
80
|
-
* their head.
|
|
59
|
+
* Measure the complete rendered prompt with the model's tokenizer, including
|
|
60
|
+
* template framing, system instructions and tool schemas. Drop old turns first,
|
|
61
|
+
* then shorten tool results / message text in the final turn. Never cut template
|
|
62
|
+
* delimiters, system instructions, or structured tool calls to make them fit.
|
|
81
63
|
*/
|
|
82
|
-
export function
|
|
83
|
-
|
|
64
|
+
export async function fitPromptToContext(messages, budgetTokens, render, countTokens) {
|
|
65
|
+
if (!Number.isInteger(budgetTokens) || budgetTokens < 1) {
|
|
66
|
+
throw new Error("On-device model has no room for a prompt after reserving output tokens");
|
|
67
|
+
}
|
|
84
68
|
const system = messages[0]?.role === "system" ? [messages[0]] : [];
|
|
85
69
|
const turns = [];
|
|
86
70
|
for (const message of messages.slice(system.length)) {
|
|
@@ -89,16 +73,43 @@ export function fitToBudget(messages, budgetTokens, toolTokens = 0) {
|
|
|
89
73
|
else
|
|
90
74
|
turns[turns.length - 1].push(message);
|
|
91
75
|
}
|
|
92
|
-
while (turns.length > 1 && total([...system, ...turns.flat()]) > budgetTokens) {
|
|
93
|
-
turns.shift();
|
|
94
|
-
}
|
|
95
76
|
let fitted = [...system, ...turns.flat()];
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
77
|
+
let truncated = false;
|
|
78
|
+
const marker = "\n…[truncated]…\n";
|
|
79
|
+
while (true) {
|
|
80
|
+
const prompt = render(fitted);
|
|
81
|
+
const promptTokens = await countTokens(prompt);
|
|
82
|
+
if (!Number.isInteger(promptTokens) || promptTokens < 1) {
|
|
83
|
+
throw new Error("On-device tokenizer returned an invalid prompt token count");
|
|
84
|
+
}
|
|
85
|
+
if (promptTokens <= budgetTokens)
|
|
86
|
+
return { prompt, promptTokens, truncated };
|
|
87
|
+
truncated = true;
|
|
88
|
+
if (turns.length > 1) {
|
|
89
|
+
turns.shift();
|
|
90
|
+
fitted = [...system, ...turns.flat()];
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
// Preserve the user's question while tool output can still be shortened.
|
|
94
|
+
const candidates = fitted
|
|
95
|
+
.map((message, index) => ({ message, index }))
|
|
96
|
+
.filter(({ message }) => message.role !== "system" && Array.from(message.content).length > marker.length * 2)
|
|
97
|
+
.sort((a, b) => Number(b.message.role === "tool") - Number(a.message.role === "tool") ||
|
|
98
|
+
b.message.content.length - a.message.content.length);
|
|
99
|
+
const candidate = candidates[0];
|
|
100
|
+
if (!candidate) {
|
|
101
|
+
throw new Error(`On-device prompt instructions and tool definitions exceed the context budget (${promptTokens} > ${budgetTokens} tokens)`);
|
|
102
|
+
}
|
|
103
|
+
// Keep both ends of long transcripts and questions, without splitting a
|
|
104
|
+
// Unicode code point. Each pass shrinks strictly, then remeasures the whole
|
|
105
|
+
// rendered prompt; character ratios are never used as a safety check.
|
|
106
|
+
const chars = Array.from(candidate.message.content);
|
|
107
|
+
const keep = Math.floor(chars.length / 4);
|
|
108
|
+
fitted[candidate.index] = {
|
|
109
|
+
...candidate.message,
|
|
110
|
+
content: `${chars.slice(0, keep).join("")}${marker}${chars.slice(-keep).join("")}`,
|
|
111
|
+
};
|
|
100
112
|
}
|
|
101
|
-
return fitted;
|
|
102
113
|
}
|
|
103
114
|
/** Remove a leading `<think>…</think>` block (or an unterminated one). */
|
|
104
115
|
export function stripThinking(text) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"LocalLlmPrompt.js","sourceRoot":"","sources":["../../src/services/LocalLlmPrompt.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,OAAO,EAAC,UAAU,EAAC,MAAM,YAAY,CAAA;AAoBrC,uFAAuF;AACvF,MAAM,CAAC,MAAM,qBAAqB,GAChC,yGAAyG,CAAA;AAE3G;;;;GAIG;AACH,MAAM,UAAU,qBAAqB,CAAC,OAIrC;IACC,MAAM,GAAG,GAAsB,EAAE,CAAA;IACjC,MAAM,WAAW,GAAG,CAAC,OAAO,CAAC,MAAM,EAAE,IAAI,EAAE,EAAE,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAC,qBAAqB,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,MAAM,CACvG,CAAC,IAAI,EAAkB,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,CACxC,CAAA;IACD,IAAI,WAAW,CAAC,MAAM;QAAE,GAAG,CAAC,IAAI,CAAC,EAAC,IAAI,EAAE,QAAQ,EAAE,OAAO,EAAE,WAAW,CAAC,IAAI,CAAC,MAAM,CAAC,EAAC,CAAC,CAAA;IAErF,KAAK,MAAM,OAAO,IAAI,OAAO,CAAC,QAAQ,EAAE,CAAC;QACvC,IAAI,OAAO,CAAC,IAAI,KAAK,WAAW,EAAE,CAAC;YACjC,MAAM,SAAS,GAAG,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F,MAAM,KAAK,GAAG,EAAC,WAAW,EAAE,UAAU,CAAC,YAAY,EAAE,YAAY,EAAE,UAAU,CAAC,eAAe,EAAC,CAAA;IAC9F,IAAI,IAAI,GAAG,aAAa,CAAC,UAAU,CAAC,IAAI,CAAC,CAAA;IAEzC,IAAI,IAAI,CAAC,YAAY,EAAE,CAAC;QACtB,MAAM,MAAM,GAAG,cAAc,CAAC,IAAI,EAAE,IAAI,CAAC,cAAc,CAAC,CAAA;QACxD,IAAI,MAAM,CAAC,SAAS,CAAC,MAAM,EAAE,CAAC;YAC5B,OAAO,EAAC,IAAI,EAAE,MAAM,CAAC,IAAI,EAAE,UAAU,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,CAAC,SAAS,EAAE,KAAK,EAAC,CAAA;QACxF,CAAC;QACD,IAAI,GAAG,MAAM,CAAC,IAAI,CAAA;IACpB,CAAC;IAED,IAAI,IAAI,CAAC,QAAQ,EAAE,CAAC;QAClB,IAAI,GAAG,iBAAiB,CAAC,IAAI,CAAC,IAAI,IAAI,CAAA;IACxC,CAAC;IAED,MAAM,UAAU,GAAkB,UAAU,CAAC,eAAe,IAAI,UAAU,CAAC,YAAY,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,UAAU,CAAA;IACnH,OAAO,EAAC,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,EAAE,UAAU,EAAE,KAAK,EAAC,CAAA;AAC/C,CAAC","sourcesContent":["/**\n * LocalLlmPrompt — the pure half of the on-device LLM path: turning a neutral\n * session.ai request into chat-template messages, fitting them into the\n * model's context, and reading the model's text back into the neutral result\n * (tool calls, JSON mode, stop reason). No native imports, so it is\n * bun-testable; LocalLlmProvider owns the runner.\n *\n * Written against Qwen3's chat template: tools are advertised by the\n * template itself, calls come back as `<tool_call>{json}</tool_call>`, and\n * thinking is a `<think>…</think>` block we never ask for but still strip.\n */\nimport {jsonrepair} from \"jsonrepair\"\n\nimport type {LlmMessage, LlmStopReason, LlmTool, LlmToolCall} from \"@glassly/cloud-protocol/llm\"\n\nexport interface TemplateToolCall {\n id: string\n type: \"function\"\n function: {name: string; arguments: Record<string, unknown>}\n}\n\nexport type TemplateMessage =\n | {role: \"system\" | \"user\"; content: string}\n | {role: \"assistant\"; content: string; tool_calls?: TemplateToolCall[]}\n | {role: \"tool\"; content: string; name?: string; tool_call_id?: string}\n\nexport interface TemplateTool {\n type: \"function\"\n function: {name: string; description: string; parameters: Record<string, unknown>}\n}\n\n/** Appended to the system prompt under `jsonMode`; small models need it said twice. */\nexport const JSON_MODE_INSTRUCTION =\n \"Respond with exactly one JSON object and nothing else: no prose before or after it, no markdown fences.\"\n\n/**\n * Neutral messages → template messages. A user turn carrying toolResults\n * becomes one `tool` message per result (the template groups consecutive\n * tool messages into a single user block), followed by the user text if any.\n */\nexport function buildTemplateMessages(request: {\n messages: LlmMessage[]\n system?: string\n jsonMode?: boolean\n}): TemplateMessage[] {\n const out: TemplateMessage[] = []\n const systemParts = [request.system?.trim(), request.jsonMode ? JSON_MODE_INSTRUCTION : undefined].filter(\n (part): part is string => Boolean(part),\n )\n if (systemParts.length) out.push({role: \"system\", content: systemParts.join(\"\\n\\n\")})\n\n for (const message of request.messages) {\n if (message.role === \"assistant\") {\n const toolCalls = message.toolCalls?.map<TemplateToolCall>((call) => ({\n id: call.id,\n type: \"function\",\n function: {name: call.name, arguments: call.input},\n }))\n out.push(\n toolCalls?.length\n ? {role: \"assistant\", content: message.content, tool_calls: toolCalls}\n : {role: \"assistant\", content: message.content},\n )\n continue\n }\n for (const result of message.toolResults ?? []) {\n out.push({\n role: \"tool\",\n tool_call_id: result.toolCallId,\n content: result.isError ? `Error: ${result.content}` : result.content,\n })\n }\n if (message.content.trim().length > 0 || !message.toolResults?.length) {\n out.push({role: \"user\", content: message.content})\n }\n }\n return out\n}\n\nexport function toTemplateTools(tools?: LlmTool[]): TemplateTool[] | undefined {\n if (!tools?.length) return undefined\n return tools.map((tool) => ({\n type: \"function\",\n function: {name: tool.name, description: tool.description, parameters: tool.inputSchema},\n }))\n}\n\n/** Rough tokens-per-character for a BPE vocabulary over mixed prose/JSON. */\nconst CHARS_PER_TOKEN = 3.2\n/** Template framing per message (`<|im_start|>role … <|im_end|>`). */\nconst TOKENS_PER_MESSAGE = 6\n\nexport function estimateTokens(text: string): number {\n return Math.ceil(text.length / CHARS_PER_TOKEN)\n}\n\nexport function estimateMessageTokens(message: TemplateMessage): number {\n let total = TOKENS_PER_MESSAGE + estimateTokens(message.content)\n if (message.role === \"assistant\" && message.tool_calls) {\n for (const call of message.tool_calls) total += estimateTokens(JSON.stringify(call.function)) + 8\n }\n return total\n}\n\nconst MIN_TOOL_CONTENT_CHARS = 1_200\n\n/**\n * Fit the conversation into `budgetTokens`. History is grouped into turns\n * (a user message plus everything up to the next user message, so a tool\n * round stays with the question it answers); the oldest turns go first and\n * the last turn is never dropped — the model must at least see the question.\n * When that last turn alone still does not fit, tool outputs are cut to\n * their head.\n */\nexport function fitToBudget(messages: TemplateMessage[], budgetTokens: number, toolTokens = 0): TemplateMessage[] {\n const total = (list: TemplateMessage[]) => list.reduce((sum, m) => sum + estimateMessageTokens(m), toolTokens)\n const system = messages[0]?.role === \"system\" ? [messages[0]] : []\n const turns: TemplateMessage[][] = []\n for (const message of messages.slice(system.length)) {\n if (message.role === \"user\" || turns.length === 0) turns.push([message])\n else turns[turns.length - 1]!.push(message)\n }\n\n while (turns.length > 1 && total([...system, ...turns.flat()]) > budgetTokens) {\n turns.shift()\n }\n\n let fitted = [...system, ...turns.flat()]\n if (total(fitted) > budgetTokens) {\n fitted = fitted.map((m) =>\n m.role === \"tool\" && m.content.length > MIN_TOOL_CONTENT_CHARS\n ? {...m, content: `${m.content.slice(0, MIN_TOOL_CONTENT_CHARS)}\\n…[truncated]`}\n : m,\n )\n }\n return fitted\n}\n\n/** Remove a leading `<think>…</think>` block (or an unterminated one). */\nexport function stripThinking(text: string): string {\n const trimmed = text.replace(/^\\s*<think>[\\s\\S]*?<\\/think>\\s*/u, \"\")\n if (trimmed !== text) return trimmed\n return text.replace(/^\\s*<think>[\\s\\S]*$/u, \"\").trimStart()\n}\n\nconst TOOL_CALL_RE = /<tool_call>\\s*([\\s\\S]*?)\\s*<\\/tool_call>/gu\n\n/** Pull `<tool_call>` blocks out of the reply. Unparseable blocks are dropped, not surfaced. */\nexport function parseToolCalls(\n text: string,\n makeId: (index: number) => string,\n): {toolCalls: LlmToolCall[]; text: string} {\n const toolCalls: LlmToolCall[] = []\n let index = 0\n const remaining = text\n .replace(TOOL_CALL_RE, (_match, body: string) => {\n const parsed = parseJsonLoose(body)\n if (parsed && typeof parsed === \"object\" && typeof (parsed as {name?: unknown}).name === \"string\") {\n const {name, arguments: args} = parsed as {name: string; arguments?: unknown}\n toolCalls.push({\n id: makeId(index++),\n name,\n input: args && typeof args === \"object\" && !Array.isArray(args) ? (args as Record<string, unknown>) : {},\n })\n }\n return \"\"\n })\n .trim()\n return {toolCalls, text: remaining}\n}\n\nfunction parseJsonLoose(body: string): unknown {\n try {\n return JSON.parse(body)\n } catch {\n try {\n return JSON.parse(jsonrepair(body))\n } catch {\n return undefined\n }\n }\n}\n\n/**\n * The first JSON object in the reply, repaired if the model left it slightly\n * broken (trailing commas, single quotes, a dropped brace). Null when there\n * is nothing object-shaped: the caller keeps the raw text and the miniapp's\n * own parser decides.\n */\nexport function extractJsonObject(text: string): string | null {\n const unfenced = text.replace(/```(?:json)?/giu, \"\").trim()\n const start = unfenced.indexOf(\"{\")\n if (start < 0) return null\n const end = unfenced.lastIndexOf(\"}\")\n const candidate = end > start ? unfenced.slice(start, end + 1) : unfenced.slice(start)\n try {\n JSON.parse(candidate)\n return candidate\n } catch {\n try {\n const repaired = jsonrepair(candidate)\n JSON.parse(repaired)\n return repaired\n } catch {\n return null\n }\n }\n}\n\nexport interface LocalGeneration {\n text: string\n promptTokens: number\n generatedTokens: number\n maxNewTokens: number\n}\n\nexport interface LocalCompletion {\n text: string\n stopReason: LlmStopReason\n toolCalls?: LlmToolCall[]\n usage: {inputTokens: number; outputTokens: number}\n}\n\n/** Raw generation → neutral completion. */\nexport function finalizeLocalResult(\n generation: LocalGeneration,\n opts: {jsonMode: boolean; toolsOffered: boolean; makeToolCallId: (index: number) => string},\n): LocalCompletion {\n const usage = {inputTokens: generation.promptTokens, outputTokens: generation.generatedTokens}\n let text = stripThinking(generation.text)\n\n if (opts.toolsOffered) {\n const parsed = parseToolCalls(text, opts.makeToolCallId)\n if (parsed.toolCalls.length) {\n return {text: parsed.text, stopReason: \"tool_use\", toolCalls: parsed.toolCalls, usage}\n }\n text = parsed.text\n }\n\n if (opts.jsonMode) {\n text = extractJsonObject(text) ?? text\n }\n\n const stopReason: LlmStopReason = generation.generatedTokens >= generation.maxNewTokens ? \"max_tokens\" : \"end_turn\"\n return {text: text.trim(), stopReason, usage}\n}\n"]}
|
|
1
|
+
{"version":3,"file":"LocalLlmPrompt.js","sourceRoot":"","sources":["../../src/services/LocalLlmPrompt.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,OAAO,EAAC,UAAU,EAAC,MAAM,YAAY,CAAA;AAoBrC,uFAAuF;AACvF,MAAM,CAAC,MAAM,qBAAqB,GAChC,yGAAyG,CAAA;AAE3G;;;;GAIG;AACH,MAAM,UAAU,qBAAqB,CAAC,OAIrC;IACC,MAAM,GAAG,GAAsB,EAAE,CAAA;IACjC,MAAM,WAAW,GAAG,CAAC,OAAO,CAAC,MAAM,EAAE,IAAI,EAAE,EAAE,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAC,qBAAqB,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,MAAM,CACvG,CAAC,IAAI,EAAkB,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,CACxC,CAAA;IACD,IAAI,WAAW,CAAC,MAAM;QAAE,GAAG,CAAC,IAAI,CAAC,EAAC,IAAI,EAAE,QAAQ,EAAE,OAAO,EAAE,WAAW,CAAC,IAAI,CAAC,MAAM,CAAC,EAAC,CAAC,CAAA;IAErF,KA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AC,IAAI,EAAE,MAAM,CAAC,IAAI,EAAE,UAAU,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,CAAC,SAAS,EAAE,KAAK,EAAC,CAAA;QACxF,CAAC;QACD,IAAI,GAAG,MAAM,CAAC,IAAI,CAAA;IACpB,CAAC;IAED,IAAI,IAAI,CAAC,QAAQ,EAAE,CAAC;QAClB,IAAI,GAAG,iBAAiB,CAAC,IAAI,CAAC,IAAI,IAAI,CAAA;IACxC,CAAC;IAED,MAAM,UAAU,GAAkB,UAAU,CAAC,eAAe,IAAI,UAAU,CAAC,YAAY,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,UAAU,CAAA;IACnH,OAAO,EAAC,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,EAAE,UAAU,EAAE,KAAK,EAAC,CAAA;AAC/C,CAAC","sourcesContent":["/**\n * LocalLlmPrompt — the pure half of the on-device LLM path: turning a neutral\n * session.ai request into chat-template messages, fitting them into the\n * model's context, and reading the model's text back into the neutral result\n * (tool calls, JSON mode, stop reason). No native imports, so it is\n * bun-testable; LocalLlmProvider owns the runner.\n *\n * Written against Qwen3's chat template: tools are advertised by the\n * template itself, calls come back as `<tool_call>{json}</tool_call>`, and\n * thinking is a `<think>…</think>` block we never ask for but still strip.\n */\nimport {jsonrepair} from \"jsonrepair\"\n\nimport type {LlmMessage, LlmStopReason, LlmTool, LlmToolCall} from \"@glassly/cloud-protocol/llm\"\n\nexport interface TemplateToolCall {\n id: string\n type: \"function\"\n function: {name: string; arguments: Record<string, unknown>}\n}\n\nexport type TemplateMessage =\n | {role: \"system\" | \"user\"; content: string}\n | {role: \"assistant\"; content: string; tool_calls?: TemplateToolCall[]}\n | {role: \"tool\"; content: string; name?: string; tool_call_id?: string}\n\nexport interface TemplateTool {\n type: \"function\"\n function: {name: string; description: string; parameters: Record<string, unknown>}\n}\n\n/** Appended to the system prompt under `jsonMode`; small models need it said twice. */\nexport const JSON_MODE_INSTRUCTION =\n \"Respond with exactly one JSON object and nothing else: no prose before or after it, no markdown fences.\"\n\n/**\n * Neutral messages → template messages. A user turn carrying toolResults\n * becomes one `tool` message per result (the template groups consecutive\n * tool messages into a single user block), followed by the user text if any.\n */\nexport function buildTemplateMessages(request: {\n messages: LlmMessage[]\n system?: string\n jsonMode?: boolean\n}): TemplateMessage[] {\n const out: TemplateMessage[] = []\n const systemParts = [request.system?.trim(), request.jsonMode ? JSON_MODE_INSTRUCTION : undefined].filter(\n (part): part is string => Boolean(part),\n )\n if (systemParts.length) out.push({role: \"system\", content: systemParts.join(\"\\n\\n\")})\n\n for (const message of request.messages) {\n if (message.role === \"assistant\") {\n const toolCalls = message.toolCalls?.map<TemplateToolCall>((call) => ({\n id: call.id,\n type: \"function\",\n function: {name: call.name, arguments: call.input},\n }))\n out.push(\n toolCalls?.length\n ? {role: \"assistant\", content: message.content, tool_calls: toolCalls}\n : {role: \"assistant\", content: message.content},\n )\n continue\n }\n for (const result of message.toolResults ?? []) {\n out.push({\n role: \"tool\",\n tool_call_id: result.toolCallId,\n content: result.isError ? `Error: ${result.content}` : result.content,\n })\n }\n if (message.content.trim().length > 0 || !message.toolResults?.length) {\n out.push({role: \"user\", content: message.content})\n }\n }\n return out\n}\n\nexport function toTemplateTools(tools?: LlmTool[]): TemplateTool[] | undefined {\n if (!tools?.length) return undefined\n return tools.map((tool) => ({\n type: \"function\",\n function: {name: tool.name, description: tool.description, parameters: tool.inputSchema},\n }))\n}\n\nexport interface FittedLocalPrompt {\n prompt: string\n promptTokens: number\n truncated: boolean\n}\n\n/**\n * Measure the complete rendered prompt with the model's tokenizer, including\n * template framing, system instructions and tool schemas. Drop old turns first,\n * then shorten tool results / message text in the final turn. Never cut template\n * delimiters, system instructions, or structured tool calls to make them fit.\n */\nexport async function fitPromptToContext(\n messages: TemplateMessage[],\n budgetTokens: number,\n render: (messages: TemplateMessage[]) => string,\n countTokens: (prompt: string) => Promise<number>,\n): Promise<FittedLocalPrompt> {\n if (!Number.isInteger(budgetTokens) || budgetTokens < 1) {\n throw new Error(\"On-device model has no room for a prompt after reserving output tokens\")\n }\n const system = messages[0]?.role === \"system\" ? [messages[0]] : []\n const turns: TemplateMessage[][] = []\n for (const message of messages.slice(system.length)) {\n if (message.role === \"user\" || turns.length === 0) turns.push([message])\n else turns[turns.length - 1]!.push(message)\n }\n let fitted = [...system, ...turns.flat()]\n let truncated = false\n const marker = \"\\n…[truncated]…\\n\"\n while (true) {\n const prompt = render(fitted)\n const promptTokens = await countTokens(prompt)\n if (!Number.isInteger(promptTokens) || promptTokens < 1) {\n throw new Error(\"On-device tokenizer returned an invalid prompt token count\")\n }\n if (promptTokens <= budgetTokens) return {prompt, promptTokens, truncated}\n truncated = true\n if (turns.length > 1) {\n turns.shift()\n fitted = [...system, ...turns.flat()]\n continue\n }\n // Preserve the user's question while tool output can still be shortened.\n const candidates = fitted\n .map((message, index) => ({message, index}))\n .filter(({message}) => message.role !== \"system\" && Array.from(message.content).length > marker.length * 2)\n .sort(\n (a, b) =>\n Number(b.message.role === \"tool\") - Number(a.message.role === \"tool\") ||\n b.message.content.length - a.message.content.length,\n )\n const candidate = candidates[0]\n if (!candidate) {\n throw new Error(\n `On-device prompt instructions and tool definitions exceed the context budget (${promptTokens} > ${budgetTokens} tokens)`,\n )\n }\n // Keep both ends of long transcripts and questions, without splitting a\n // Unicode code point. Each pass shrinks strictly, then remeasures the whole\n // rendered prompt; character ratios are never used as a safety check.\n const chars = Array.from(candidate.message.content)\n const keep = Math.floor(chars.length / 4)\n fitted[candidate.index] = {\n ...candidate.message,\n content: `${chars.slice(0, keep).join(\"\")}${marker}${chars.slice(-keep).join(\"\")}`,\n }\n }\n}\n\n/** Remove a leading `<think>…</think>` block (or an unterminated one). */\nexport function stripThinking(text: string): string {\n const trimmed = text.replace(/^\\s*<think>[\\s\\S]*?<\\/think>\\s*/u, \"\")\n if (trimmed !== text) return trimmed\n return text.replace(/^\\s*<think>[\\s\\S]*$/u, \"\").trimStart()\n}\n\nconst TOOL_CALL_RE = /<tool_call>\\s*([\\s\\S]*?)\\s*<\\/tool_call>/gu\n\n/** Pull `<tool_call>` blocks out of the reply. Unparseable blocks are dropped, not surfaced. */\nexport function parseToolCalls(\n text: string,\n makeId: (index: number) => string,\n): {toolCalls: LlmToolCall[]; text: string} {\n const toolCalls: LlmToolCall[] = []\n let index = 0\n const remaining = text\n .replace(TOOL_CALL_RE, (_match, body: string) => {\n const parsed = parseJsonLoose(body)\n if (parsed && typeof parsed === \"object\" && typeof (parsed as {name?: unknown}).name === \"string\") {\n const {name, arguments: args} = parsed as {name: string; arguments?: unknown}\n toolCalls.push({\n id: makeId(index++),\n name,\n input: args && typeof args === \"object\" && !Array.isArray(args) ? (args as Record<string, unknown>) : {},\n })\n }\n return \"\"\n })\n .trim()\n return {toolCalls, text: remaining}\n}\n\nfunction parseJsonLoose(body: string): unknown {\n try {\n return JSON.parse(body)\n } catch {\n try {\n return JSON.parse(jsonrepair(body))\n } catch {\n return undefined\n }\n }\n}\n\n/**\n * The first JSON object in the reply, repaired if the model left it slightly\n * broken (trailing commas, single quotes, a dropped brace). Null when there\n * is nothing object-shaped: the caller keeps the raw text and the miniapp's\n * own parser decides.\n */\nexport function extractJsonObject(text: string): string | null {\n const unfenced = text.replace(/```(?:json)?/giu, \"\").trim()\n const start = unfenced.indexOf(\"{\")\n if (start < 0) return null\n const end = unfenced.lastIndexOf(\"}\")\n const candidate = end > start ? unfenced.slice(start, end + 1) : unfenced.slice(start)\n try {\n JSON.parse(candidate)\n return candidate\n } catch {\n try {\n const repaired = jsonrepair(candidate)\n JSON.parse(repaired)\n return repaired\n } catch {\n return null\n }\n }\n}\n\nexport interface LocalGeneration {\n text: string\n promptTokens: number\n generatedTokens: number\n maxNewTokens: number\n}\n\nexport interface LocalCompletion {\n text: string\n stopReason: LlmStopReason\n toolCalls?: LlmToolCall[]\n usage: {inputTokens: number; outputTokens: number}\n}\n\n/** Raw generation → neutral completion. */\nexport function finalizeLocalResult(\n generation: LocalGeneration,\n opts: {jsonMode: boolean; toolsOffered: boolean; makeToolCallId: (index: number) => string},\n): LocalCompletion {\n const usage = {inputTokens: generation.promptTokens, outputTokens: generation.generatedTokens}\n let text = stripThinking(generation.text)\n\n if (opts.toolsOffered) {\n const parsed = parseToolCalls(text, opts.makeToolCallId)\n if (parsed.toolCalls.length) {\n return {text: parsed.text, stopReason: \"tool_use\", toolCalls: parsed.toolCalls, usage}\n }\n text = parsed.text\n }\n\n if (opts.jsonMode) {\n text = extractJsonObject(text) ?? text\n }\n\n const stopReason: LlmStopReason = generation.generatedTokens >= generation.maxNewTokens ? \"max_tokens\" : \"end_turn\"\n return {text: text.trim(), stopReason, usage}\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"LocalLlmProvider.d.ts","sourceRoot":"","sources":["../../src/services/LocalLlmProvider.ts"],"names":[],"mappings":"AAmBA,OAAO,KAAK,EAAC,kBAAkB,EAAC,MAAM,6BAA6B,CAAA;AAEnE,OAA6B,EAAC,KAAK,eAAe,EAAC,MAAM,wBAAwB,CAAA;AACjF,OAAO,
|
|
1
|
+
{"version":3,"file":"LocalLlmProvider.d.ts","sourceRoot":"","sources":["../../src/services/LocalLlmProvider.ts"],"names":[],"mappings":"AAmBA,OAAO,KAAK,EAAC,kBAAkB,EAAC,MAAM,6BAA6B,CAAA;AAEnE,OAA6B,EAAC,KAAK,eAAe,EAAC,MAAM,wBAAwB,CAAA;AACjF,OAAO,EAKL,KAAK,eAAe,EACrB,MAAM,kBAAkB,CAAA;AA6CzB,MAAM,WAAW,sBAAuB,SAAQ,eAAe;IAC7D,OAAO,EAAE,eAAe,CAAA;CACzB;AA0BD,cAAM,gBAAgB;IACpB,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAkB;IAEzC,OAAO,CAAC,MAAM,CAA2B;IACzC,OAAO,CAAC,OAAO,CAAoC;IACnD,OAAO,CAAC,KAAK,CAAsC;IACnD,OAAO,CAAC,YAAY,CAAI;IACxB,OAAO,CAAC,SAAS,CAAsB;IACvC,OAAO,CAAC,GAAG,CAAmB;IAE9B,OAAO;IAUP,MAAM,CAAC,WAAW,IAAI,gBAAgB;IAKtC,mFAAmF;IACnF,QAAQ,IAAI,OAAO;IAInB,aAAa,IAAI,eAAe,GAAG,IAAI;IAIvC,oGAAoG;IACpG,QAAQ,CAAC,OAAO,EAAE,kBAAkB,GAAG,OAAO,CAAC,sBAAsB,CAAC;IAqBtE,kGAAkG;IAC5F,MAAM,IAAI,OAAO,CAAC,IAAI,CAAC;IAe7B,yEAAyE;IACzE,OAAO,CAAC,aAAa;YAaP,aAAa;YAmEb,YAAY;YAgBZ,IAAI;YAyCJ,OAAO;IASrB,OAAO,CAAC,YAAY;IASpB,OAAO,CAAC,cAAc;CAMvB;AAED,QAAA,MAAM,gBAAgB,kBAAiC,CAAA;AACvD,OAAO,EAAC,gBAAgB,EAAC,CAAA;AACzB,eAAe,gBAAgB,CAAA"}
|
|
@@ -18,16 +18,23 @@ import * as RNFS from "@dr.pogodin/react-native-fs";
|
|
|
18
18
|
import { Template } from "@huggingface/jinja";
|
|
19
19
|
import { BgTimer } from "../utils/timers";
|
|
20
20
|
import localLlmModelManager from "./LocalLlmModelManager";
|
|
21
|
-
import { buildTemplateMessages,
|
|
21
|
+
import { buildTemplateMessages, finalizeLocalResult, fitPromptToContext, toTemplateTools, } from "./LocalLlmPrompt";
|
|
22
22
|
import { SETTINGS, useSettingsStore } from "../stores/settings";
|
|
23
23
|
const LOG_TAG = "LOCAL_LLM";
|
|
24
24
|
/** Drop the resident model after this long without a request. */
|
|
25
25
|
const IDLE_UNLOAD_MS = 5 * 60_000;
|
|
26
26
|
/** Hard stop on one generation — the SDK request times out at 120 s anyway. */
|
|
27
27
|
const GENERATION_DEADLINE_MS = 100_000;
|
|
28
|
+
// One generating, one waiting. Assist's proactive insights re-request every
|
|
29
|
+
// time the SDK's 60 s timeout fires while a slow on-device run is still going,
|
|
30
|
+
// and an unbounded queue kept the phone generating for callers that had
|
|
31
|
+
// already given up (one reporter saw 472 s runs and 15 s touch latency).
|
|
32
|
+
const MAX_PENDING_REQUESTS = 2;
|
|
33
|
+
// Under the SDK's 60 s request timeout: a request that waited longer has no caller.
|
|
34
|
+
const QUEUE_WAIT_LIMIT_MS = 55_000;
|
|
28
35
|
const DEFAULT_MAX_NEW_TOKENS = 512;
|
|
29
36
|
const MAX_NEW_TOKENS_CEILING = 1_024;
|
|
30
|
-
/**
|
|
37
|
+
/** Reserve space for runner-added BOS/EOS tokens and context boundary bookkeeping. */
|
|
31
38
|
const CONTEXT_MARGIN_TOKENS = 96;
|
|
32
39
|
/**
|
|
33
40
|
* Runs on ExecuTorch's worklet runtime: `runner.generate` blocks its thread
|
|
@@ -43,11 +50,16 @@ function generateWorklet(runner, prompt, config, stopTokens) {
|
|
|
43
50
|
});
|
|
44
51
|
return { text, stats };
|
|
45
52
|
}
|
|
53
|
+
function countTokensWorklet(tokenizer, prompt) {
|
|
54
|
+
"worklet";
|
|
55
|
+
return tokenizer.encode(prompt).length;
|
|
56
|
+
}
|
|
46
57
|
class LocalLlmProvider {
|
|
47
58
|
static instance;
|
|
48
59
|
loaded = null;
|
|
49
60
|
loading = null;
|
|
50
61
|
queue = Promise.resolve();
|
|
62
|
+
pendingCount = 0;
|
|
51
63
|
idleTimer = null;
|
|
52
64
|
rne = null;
|
|
53
65
|
constructor() {
|
|
@@ -70,9 +82,22 @@ class LocalLlmProvider {
|
|
|
70
82
|
}
|
|
71
83
|
/** Serve one neutral completion on-device. Throws when the model is missing or generation fails. */
|
|
72
84
|
complete(request) {
|
|
73
|
-
|
|
85
|
+
if (this.pendingCount >= MAX_PENDING_REQUESTS) {
|
|
86
|
+
return Promise.reject(new Error(`The on-device model is busy (${this.pendingCount} requests pending); try again shortly.`));
|
|
87
|
+
}
|
|
88
|
+
this.pendingCount += 1;
|
|
89
|
+
const queuedAt = Date.now();
|
|
90
|
+
const run = this.queue.then(() => {
|
|
91
|
+
const waitedMs = Date.now() - queuedAt;
|
|
92
|
+
if (waitedMs > QUEUE_WAIT_LIMIT_MS) {
|
|
93
|
+
throw new Error(`The on-device model was busy for ${Math.round(waitedMs / 1000)} s; request dropped.`);
|
|
94
|
+
}
|
|
95
|
+
return this.completeInner(request);
|
|
96
|
+
});
|
|
74
97
|
this.queue = run.catch(() => undefined);
|
|
75
|
-
return run
|
|
98
|
+
return run.finally(() => {
|
|
99
|
+
this.pendingCount -= 1;
|
|
100
|
+
});
|
|
76
101
|
}
|
|
77
102
|
/** Release the resident runner (idle, model switch, memory pressure). Safe to call repeatedly. */
|
|
78
103
|
async unload() {
|
|
@@ -102,6 +127,9 @@ class LocalLlmProvider {
|
|
|
102
127
|
catch (error) {
|
|
103
128
|
console.warn(`${LOG_TAG}: runner dispose failed:`, error);
|
|
104
129
|
}
|
|
130
|
+
finally {
|
|
131
|
+
loaded.tokenizer.dispose();
|
|
132
|
+
}
|
|
105
133
|
}
|
|
106
134
|
async completeInner(request) {
|
|
107
135
|
this.clearIdleTimer();
|
|
@@ -109,16 +137,10 @@ class LocalLlmProvider {
|
|
|
109
137
|
const model = await this.ensureLoaded();
|
|
110
138
|
const jsonMode = request.jsonMode === true;
|
|
111
139
|
const tools = toTemplateTools(request.tools);
|
|
112
|
-
const toolTokens = tools ? estimateTokens(JSON.stringify(tools)) + 64 : 0;
|
|
113
140
|
const maxNewTokens = Math.max(16, Math.min(request.maxTokens ?? DEFAULT_MAX_NEW_TOKENS, MAX_NEW_TOKENS_CEILING, Math.floor(model.maxSeqLen / 4)));
|
|
114
141
|
const promptBudget = model.maxSeqLen - maxNewTokens - CONTEXT_MARGIN_TOKENS;
|
|
115
|
-
const
|
|
116
|
-
|
|
117
|
-
messages,
|
|
118
|
-
tools,
|
|
119
|
-
add_generation_prompt: true,
|
|
120
|
-
enable_thinking: false,
|
|
121
|
-
});
|
|
142
|
+
const { prompt, promptTokens, truncated } = await fitPromptToContext(buildTemplateMessages({ ...request, jsonMode }), promptBudget, (messages) => model.template.render({ messages, tools, add_generation_prompt: true, enable_thinking: false }), (text) => model.countTokens(model.tokenizer, text));
|
|
143
|
+
console.log(`${LOG_TAG}: ${model.modelId} prompt=${promptTokens} budget=${promptBudget} context=${model.maxSeqLen} maxNewTokens=${maxNewTokens} truncated=${truncated}`);
|
|
122
144
|
model.runner.reset(0);
|
|
123
145
|
const deadline = BgTimer.setTimeout(() => {
|
|
124
146
|
console.warn(`${LOG_TAG}: generation deadline hit, stopping`);
|
|
@@ -129,6 +151,10 @@ class LocalLlmProvider {
|
|
|
129
151
|
try {
|
|
130
152
|
generated = await model.generate(model.runner, prompt, { maxNewTokens, temperature: request.temperature ?? 0.7 }, model.stopTokens);
|
|
131
153
|
}
|
|
154
|
+
catch (error) {
|
|
155
|
+
console.warn(`${LOG_TAG}: generation failed: model=${model.modelId} prompt=${promptTokens} context=${model.maxSeqLen} maxNewTokens=${maxNewTokens}`, error);
|
|
156
|
+
throw error;
|
|
157
|
+
}
|
|
132
158
|
finally {
|
|
133
159
|
BgTimer.clearTimeout(deadline);
|
|
134
160
|
}
|
|
@@ -177,16 +203,30 @@ class LocalLlmProvider {
|
|
|
177
203
|
const startedAt = Date.now();
|
|
178
204
|
const createRunner = rne.wrapAsync(rne.llm.createLLMRunner);
|
|
179
205
|
const runner = await createRunner(paths.model, paths.tokenizer, []);
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
maxSeqLen
|
|
188
|
-
|
|
189
|
-
|
|
206
|
+
let tokenizer;
|
|
207
|
+
try {
|
|
208
|
+
const maxSeqLen = runner.getKVCacheState().maxSeqLen;
|
|
209
|
+
if (!Number.isInteger(maxSeqLen) || maxSeqLen <= CONTEXT_MARGIN_TOKENS + 16) {
|
|
210
|
+
throw new Error(`On-device model ${modelId} reported an invalid context size: ${maxSeqLen}`);
|
|
211
|
+
}
|
|
212
|
+
tokenizer = await rne.wrapAsync(rne.nlp.loadTokenizer)(paths.tokenizer);
|
|
213
|
+
console.log(`${LOG_TAG}: loaded ${modelId} (context ${maxSeqLen}) in ${Date.now() - startedAt} ms`);
|
|
214
|
+
return {
|
|
215
|
+
modelId,
|
|
216
|
+
runner,
|
|
217
|
+
tokenizer,
|
|
218
|
+
countTokens: rne.wrapAsync(countTokensWorklet),
|
|
219
|
+
template,
|
|
220
|
+
stopTokens: [...tokenizerConfig.stopTokens],
|
|
221
|
+
maxSeqLen,
|
|
222
|
+
generate: rne.wrapAsync(generateWorklet),
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
catch (error) {
|
|
226
|
+
tokenizer?.dispose();
|
|
227
|
+
runner.dispose();
|
|
228
|
+
throw error;
|
|
229
|
+
}
|
|
190
230
|
}
|
|
191
231
|
async runtime() {
|
|
192
232
|
if (this.rne)
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"LocalLlmProvider.js","sourceRoot":"","sources":["../../src/services/LocalLlmProvider.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AACH,OAAO,KAAK,IAAI,MAAM,6BAA6B,CAAA;AACnD,OAAO,EAAC,QAAQ,EAAC,MAAM,oBAAoB,CAAA;AAG3C,OAAO,EAAC,OAAO,EAAC,MAAM,iBAAiB,CAAA;AACvC,OAAO,oBAA4C,MAAM,wBAAwB,CAAA;AACjF,OAAO,EACL,qBAAqB,EACrB,cAAc,EACd,mBAAmB,EACnB,WAAW,EACX,eAAe,GAEhB,MAAM,kBAAkB,CAAA;AACzB,OAAO,EAAC,QAAQ,EAAE,gBAAgB,EAAC,MAAM,oBAAoB,CAAA;AAS7D,MAAM,OAAO,GAAG,WAAW,CAAA;AAE3B,iEAAiE;AACjE,MAAM,cAAc,GAAG,CAAC,GAAG,MAAM,CAAA;AACjC,+EAA+E;AAC/E,MAAM,sBAAsB,GAAG,OAAO,CAAA;AACtC,MAAM,sBAAsB,GAAG,GAAG,CAAA;AAClC,MAAM,sBAAsB,GAAG,KAAK,CAAA;AACpC,sFAAsF;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EO,cAAc;QACpB,IAAI,IAAI,CAAC,SAAS,EAAE,CAAC;YACnB,OAAO,CAAC,YAAY,CAAC,IAAI,CAAC,SAAS,CAAC,CAAA;YACpC,IAAI,CAAC,SAAS,GAAG,IAAI,CAAA;QACvB,CAAC;IACH,CAAC;CACF;AAED,MAAM,gBAAgB,GAAG,gBAAgB,CAAC,WAAW,EAAE,CAAA;AACvD,OAAO,EAAC,gBAAgB,EAAC,CAAA;AACzB,eAAe,gBAAgB,CAAA","sourcesContent":["/**\n * LocalLlmProvider — session.ai.complete served by the phone itself: an\n * ExecuTorch runner (react-native-executorch) over the Qwen3 export that\n * LocalLlmModelManager downloaded. Stateless per request like the vendor\n * providers: every call renders the whole conversation through the model's\n * chat template, resets the KV cache, and generates on ExecuTorch's worklet\n * thread so the JS thread stays free.\n *\n * The runner is loaded on the first request and kept resident while calls\n * keep coming (loading a 1–3 GB model is seconds, not milliseconds); it is\n * dropped after an idle period and whenever the selected model changes.\n * Requests are serialized — one model, one KV cache.\n *\n * react-native-executorch is imported lazily so the engine module graph\n * loads without the native runtime (bun tests, hosts without the dependency).\n */\nimport * as RNFS from \"@dr.pogodin/react-native-fs\"\nimport {Template} from \"@huggingface/jinja\"\n\nimport type {LlmCompleteRequest} from \"@glassly/cloud-protocol/llm\"\nimport {BgTimer} from \"../utils/timers\"\nimport localLlmModelManager, {type LocalLlmModelId} from \"./LocalLlmModelManager\"\nimport {\n buildTemplateMessages,\n estimateTokens,\n finalizeLocalResult,\n fitToBudget,\n toTemplateTools,\n type LocalCompletion,\n} from \"./LocalLlmPrompt\"\nimport {SETTINGS, useSettingsStore} from \"../stores/settings\"\n\ntype Rne = typeof import(\"react-native-executorch\")\n// The runner API lives on the `llm` namespace; derive its types from there so\n// the import stays a type-only reference (no native module at load time).\ntype LLMRunner = ReturnType<Rne[\"llm\"][\"createLLMRunner\"]>\ntype LLMGenerationStats = ReturnType<LLMRunner[\"generate\"]>\ntype LLMGenerationConfig = NonNullable<Parameters<LLMRunner[\"generate\"]>[1]>\n\nconst LOG_TAG = \"LOCAL_LLM\"\n\n/** Drop the resident model after this long without a request. */\nconst IDLE_UNLOAD_MS = 5 * 60_000\n/** Hard stop on one generation — the SDK request times out at 120 s anyway. */\nconst GENERATION_DEADLINE_MS = 100_000\nconst DEFAULT_MAX_NEW_TOKENS = 512\nconst MAX_NEW_TOKENS_CEILING = 1_024\n/** Headroom between the estimated prompt and the context end; estimates are rough. */\nconst CONTEXT_MARGIN_TOKENS = 96\n\ninterface LoadedModel {\n modelId: LocalLlmModelId\n runner: LLMRunner\n template: Template\n stopTokens: string[]\n maxSeqLen: number\n generate: (\n runner: LLMRunner,\n prompt: string,\n config: LLMGenerationConfig,\n stopTokens: string[],\n ) => Promise<{text: string; stats: LLMGenerationStats}>\n}\n\nexport interface LocalLlmCompleteResult extends LocalCompletion {\n modelId: LocalLlmModelId\n}\n\n/**\n * Runs on ExecuTorch's worklet runtime: `runner.generate` blocks its thread\n * until EOS, `maxNewTokens`, or `runner.stop()` from the JS thread.\n */\nfunction generateWorklet(\n runner: LLMRunner,\n prompt: string,\n config: LLMGenerationConfig,\n stopTokens: string[],\n): {text: string; stats: LLMGenerationStats} {\n \"worklet\"\n let text = \"\"\n const stats = runner.generate(prompt, config, (token: string) => {\n if (stopTokens.includes(token)) return\n text += token\n })\n return {text, stats}\n}\n\nclass LocalLlmProvider {\n private static instance: LocalLlmProvider\n\n private loaded: LoadedModel | null = null\n private loading: Promise<LoadedModel> | null = null\n private queue: Promise<unknown> = Promise.resolve()\n private idleTimer: number | null = null\n private rne: Rne | null = null\n\n private constructor() {\n // A different model picked in Settings invalidates the resident runner.\n useSettingsStore.subscribe(\n (state) => state.settings[SETTINGS.local_llm_model.key],\n () => {\n void this.unload()\n },\n )\n }\n\n static getInstance(): LocalLlmProvider {\n if (!LocalLlmProvider.instance) LocalLlmProvider.instance = new LocalLlmProvider()\n return LocalLlmProvider.instance\n }\n\n /** Whether a runner is resident right now (diagnostics; not a readiness check). */\n isLoaded(): boolean {\n return this.loaded !== null\n }\n\n loadedModelId(): LocalLlmModelId | null {\n return this.loaded?.modelId ?? null\n }\n\n /** Serve one neutral completion on-device. Throws when the model is missing or generation fails. */\n complete(request: LlmCompleteRequest): Promise<LocalLlmCompleteResult> {\n const run = this.queue.then(() => this.completeInner(request))\n this.queue = run.catch(() => undefined)\n return run\n }\n\n /** Release the resident runner (idle, model switch, memory pressure). Safe to call repeatedly. */\n async unload(): Promise<void> {\n this.clearIdleTimer()\n // Never dispose under a running generation: let the queue drain first.\n await this.queue\n const pending = this.loading\n if (pending) {\n try {\n await pending\n } catch {\n // A failed load left nothing to release.\n }\n }\n this.releaseRunner()\n }\n\n /** Synchronous dispose; callers guarantee no generation is in flight. */\n private releaseRunner(): void {\n const loaded = this.loaded\n this.loaded = null\n if (!loaded) return\n try {\n loaded.runner.dispose()\n } catch (error) {\n console.warn(`${LOG_TAG}: runner dispose failed:`, error)\n }\n }\n\n private async completeInner(request: LlmCompleteRequest): Promise<LocalLlmCompleteResult> {\n this.clearIdleTimer()\n try {\n const model = await this.ensureLoaded()\n const jsonMode = request.jsonMode === true\n const tools = toTemplateTools(request.tools)\n const toolTokens = tools ? estimateTokens(JSON.stringify(tools)) + 64 : 0\n\n const maxNewTokens = Math.max(\n 16,\n Math.min(request.maxTokens ?? DEFAULT_MAX_NEW_TOKENS, MAX_NEW_TOKENS_CEILING, Math.floor(model.maxSeqLen / 4)),\n )\n const promptBudget = model.maxSeqLen - maxNewTokens - CONTEXT_MARGIN_TOKENS\n const messages = fitToBudget(buildTemplateMessages({...request, jsonMode}), promptBudget, toolTokens)\n\n const prompt = model.template.render({\n messages,\n tools,\n add_generation_prompt: true,\n enable_thinking: false,\n })\n\n model.runner.reset(0)\n const deadline = BgTimer.setTimeout(() => {\n console.warn(`${LOG_TAG}: generation deadline hit, stopping`)\n model.runner.stop()\n }, GENERATION_DEADLINE_MS)\n let generated: {text: string; stats: LLMGenerationStats}\n const startedAt = Date.now()\n try {\n generated = await model.generate(\n model.runner,\n prompt,\n {maxNewTokens, temperature: request.temperature ?? 0.7},\n model.stopTokens,\n )\n } finally {\n BgTimer.clearTimeout(deadline)\n }\n\n const elapsedMs = Date.now() - startedAt\n console.log(\n `${LOG_TAG}: ${model.modelId} prompt=${generated.stats.numPromptTokens} out=${generated.stats.numGeneratedTokens} tokens in ${elapsedMs} ms`,\n )\n\n const seed = `${Date.now().toString(36)}${Math.random().toString(36).slice(2, 6)}`\n const completion = finalizeLocalResult(\n {\n text: generated.text,\n promptTokens: generated.stats.numPromptTokens,\n generatedTokens: generated.stats.numGeneratedTokens,\n maxNewTokens,\n },\n {jsonMode, toolsOffered: Boolean(tools), makeToolCallId: (index) => `local_${seed}_${index}`},\n )\n return {...completion, modelId: model.modelId}\n } finally {\n this.armIdleTimer()\n }\n }\n\n private async ensureLoaded(): Promise<LoadedModel> {\n const modelId = localLlmModelManager.selectedModelId()\n if (this.loaded?.modelId === modelId) return this.loaded\n if (this.loading) return this.loading\n\n this.loading = this.load(modelId)\n .then((model) => {\n this.loaded = model\n return model\n })\n .finally(() => {\n this.loading = null\n })\n return this.loading\n }\n\n private async load(modelId: LocalLlmModelId): Promise<LoadedModel> {\n // Runs inside the request queue, so nothing is generating; unload() would\n // wait on that same queue.\n this.releaseRunner()\n if (!(await localLlmModelManager.isModelAvailable(modelId))) {\n throw new Error(`on-device model ${modelId} is not downloaded`)\n }\n const rne = await this.runtime()\n const paths = localLlmModelManager.paths(modelId)\n\n const tokenizerConfig = rne.llm.parseTokenizerConfig(JSON.parse(await RNFS.readFile(paths.tokenizerConfig, \"utf8\")))\n const template = new Template(tokenizerConfig.chatTemplate)\n\n const startedAt = Date.now()\n const createRunner = rne.wrapAsync(rne.llm.createLLMRunner)\n const runner = await createRunner(paths.model, paths.tokenizer, [])\n const maxSeqLen = runner.getKVCacheState().maxSeqLen\n console.log(`${LOG_TAG}: loaded ${modelId} (context ${maxSeqLen}) in ${Date.now() - startedAt} ms`)\n\n return {\n modelId,\n runner,\n template,\n stopTokens: [...tokenizerConfig.stopTokens],\n maxSeqLen,\n generate: rne.wrapAsync(generateWorklet),\n }\n }\n\n private async runtime(): Promise<Rne> {\n if (this.rne) return this.rne\n const rne: Rne = await import(\"react-native-executorch\")\n // Software Mansion's anonymous download analytics; the host has its own telemetry.\n rne.setTelemetryEnabled(false)\n this.rne = rne\n return rne\n }\n\n private armIdleTimer(): void {\n this.clearIdleTimer()\n this.idleTimer = BgTimer.setTimeout(() => {\n this.idleTimer = null\n console.log(`${LOG_TAG}: idle, unloading ${this.loaded?.modelId ?? \"nothing\"}`)\n void this.unload()\n }, IDLE_UNLOAD_MS)\n }\n\n private clearIdleTimer(): void {\n if (this.idleTimer) {\n BgTimer.clearTimeout(this.idleTimer)\n this.idleTimer = null\n }\n }\n}\n\nconst localLlmProvider = LocalLlmProvider.getInstance()\nexport {LocalLlmProvider}\nexport default localLlmProvider\n"]}
|
|
1
|
+
{"version":3,"file":"LocalLlmProvider.js","sourceRoot":"","sources":["../../src/services/LocalLlmProvider.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AACH,OAAO,KAAK,IAAI,MAAM,6BAA6B,CAAA;AACnD,OAAO,EAAC,QAAQ,EAAC,MAAM,oBAAoB,CAAA;AAG3C,OAAO,EAAC,OAAO,EAAC,MAAM,iBAAiB,CAAA;AACvC,OAAO,oBAA4C,MAAM,wBAAwB,CAAA;AACjF,OAAO,EACL,qBAAqB,EACrB,mBAAmB,EACnB,kBAAkB,EAClB,eAAe,GAEhB,MAAM,kBAAkB,CAAA;AACzB,OAAO,EAAC,QAAQ,EAAE,gBAAgB,EAAC,MAAM,oBAAoB,CAAA;AAU7D,MAAM,OAAO,GAAG,WAAW,CAAA;AAE3B,iEAAiE;AACjE,MAAM,cAAc,GAAG,CAAC,GAAG,MAAM,CAAA;AACjC,+EAA+E;AAC/E,MAAM,sBAAsB,GAAG,OAAO,CAAA;AACtC,4EAA4E;AAC5E,+EAA+E;AAC/E,wEAAwE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SAAS,EAAE,OAAO,EAAE,CAAA;YACpB,MAAM,CAAC,OAAO,EAAE,CAAA;YAChB,MAAM,KAAK,CAAA;QACb,CAAC;IACH,CAAC;IAEO,KAAK,CAAC,OAAO;QACnB,IAAI,IAAI,CAAC,GAAG;YAAE,OAAO,IAAI,CAAC,GAAG,CAAA;QAC7B,MAAM,GAAG,GAAQ,MAAM,MAAM,CAAC,yBAAyB,CAAC,CAAA;QACxD,mFAAmF;QACnF,GAAG,CAAC,mBAAmB,CAAC,KAAK,CAAC,CAAA;QAC9B,IAAI,CAAC,GAAG,GAAG,GAAG,CAAA;QACd,OAAO,GAAG,CAAA;IACZ,CAAC;IAEO,YAAY;QAClB,IAAI,CAAC,cAAc,EAAE,CAAA;QACrB,IAAI,CAAC,SAAS,GAAG,OAAO,CAAC,UAAU,CAAC,GAAG,EAAE;YACvC,IAAI,CAAC,SAAS,GAAG,IAAI,CAAA;YACrB,OAAO,CAAC,GAAG,CAAC,GAAG,OAAO,qBAAqB,IAAI,CAAC,MAAM,EAAE,OAAO,IAAI,SAAS,EAAE,CAAC,CAAA;YAC/E,KAAK,IAAI,CAAC,MAAM,EAAE,CAAA;QACpB,CAAC,EAAE,cAAc,CAAC,CAAA;IACpB,CAAC;IAEO,cAAc;QACpB,IAAI,IAAI,CAAC,SAAS,EAAE,CAAC;YACnB,OAAO,CAAC,YAAY,CAAC,IAAI,CAAC,SAAS,CAAC,CAAA;YACpC,IAAI,CAAC,SAAS,GAAG,IAAI,CAAA;QACvB,CAAC;IACH,CAAC;CACF;AAED,MAAM,gBAAgB,GAAG,gBAAgB,CAAC,WAAW,EAAE,CAAA;AACvD,OAAO,EAAC,gBAAgB,EAAC,CAAA;AACzB,eAAe,gBAAgB,CAAA","sourcesContent":["/**\n * LocalLlmProvider — session.ai.complete served by the phone itself: an\n * ExecuTorch runner (react-native-executorch) over the Qwen3 export that\n * LocalLlmModelManager downloaded. Stateless per request like the vendor\n * providers: every call renders the whole conversation through the model's\n * chat template, resets the KV cache, and generates on ExecuTorch's worklet\n * thread so the JS thread stays free.\n *\n * The runner is loaded on the first request and kept resident while calls\n * keep coming (loading a 1–3 GB model is seconds, not milliseconds); it is\n * dropped after an idle period and whenever the selected model changes.\n * Requests are serialized — one model, one KV cache.\n *\n * react-native-executorch is imported lazily so the engine module graph\n * loads without the native runtime (bun tests, hosts without the dependency).\n */\nimport * as RNFS from \"@dr.pogodin/react-native-fs\"\nimport {Template} from \"@huggingface/jinja\"\n\nimport type {LlmCompleteRequest} from \"@glassly/cloud-protocol/llm\"\nimport {BgTimer} from \"../utils/timers\"\nimport localLlmModelManager, {type LocalLlmModelId} from \"./LocalLlmModelManager\"\nimport {\n buildTemplateMessages,\n finalizeLocalResult,\n fitPromptToContext,\n toTemplateTools,\n type LocalCompletion,\n} from \"./LocalLlmPrompt\"\nimport {SETTINGS, useSettingsStore} from \"../stores/settings\"\n\ntype Rne = typeof import(\"react-native-executorch\")\n// The runner API lives on the `llm` namespace; derive its types from there so\n// the import stays a type-only reference (no native module at load time).\ntype Tokenizer = ReturnType<Rne[\"nlp\"][\"loadTokenizer\"]>\ntype LLMRunner = ReturnType<Rne[\"llm\"][\"createLLMRunner\"]>\ntype LLMGenerationStats = ReturnType<LLMRunner[\"generate\"]>\ntype LLMGenerationConfig = NonNullable<Parameters<LLMRunner[\"generate\"]>[1]>\n\nconst LOG_TAG = \"LOCAL_LLM\"\n\n/** Drop the resident model after this long without a request. */\nconst IDLE_UNLOAD_MS = 5 * 60_000\n/** Hard stop on one generation — the SDK request times out at 120 s anyway. */\nconst GENERATION_DEADLINE_MS = 100_000\n// One generating, one waiting. Assist's proactive insights re-request every\n// time the SDK's 60 s timeout fires while a slow on-device run is still going,\n// and an unbounded queue kept the phone generating for callers that had\n// already given up (one reporter saw 472 s runs and 15 s touch latency).\nconst MAX_PENDING_REQUESTS = 2\n// Under the SDK's 60 s request timeout: a request that waited longer has no caller.\nconst QUEUE_WAIT_LIMIT_MS = 55_000\nconst DEFAULT_MAX_NEW_TOKENS = 512\nconst MAX_NEW_TOKENS_CEILING = 1_024\n/** Reserve space for runner-added BOS/EOS tokens and context boundary bookkeeping. */\nconst CONTEXT_MARGIN_TOKENS = 96\n\ninterface LoadedModel {\n modelId: LocalLlmModelId\n runner: LLMRunner\n tokenizer: Tokenizer\n countTokens: (tokenizer: Tokenizer, prompt: string) => Promise<number>\n template: Template\n stopTokens: string[]\n maxSeqLen: number\n generate: (\n runner: LLMRunner,\n prompt: string,\n config: LLMGenerationConfig,\n stopTokens: string[],\n ) => Promise<{text: string; stats: LLMGenerationStats}>\n}\n\nexport interface LocalLlmCompleteResult extends LocalCompletion {\n modelId: LocalLlmModelId\n}\n\n/**\n * Runs on ExecuTorch's worklet runtime: `runner.generate` blocks its thread\n * until EOS, `maxNewTokens`, or `runner.stop()` from the JS thread.\n */\nfunction generateWorklet(\n runner: LLMRunner,\n prompt: string,\n config: LLMGenerationConfig,\n stopTokens: string[],\n): {text: string; stats: LLMGenerationStats} {\n \"worklet\"\n let text = \"\"\n const stats = runner.generate(prompt, config, (token: string) => {\n if (stopTokens.includes(token)) return\n text += token\n })\n return {text, stats}\n}\n\nfunction countTokensWorklet(tokenizer: Tokenizer, prompt: string): number {\n \"worklet\"\n return tokenizer.encode(prompt).length\n}\n\nclass LocalLlmProvider {\n private static instance: LocalLlmProvider\n\n private loaded: LoadedModel | null = null\n private loading: Promise<LoadedModel> | null = null\n private queue: Promise<unknown> = Promise.resolve()\n private pendingCount = 0\n private idleTimer: number | null = null\n private rne: Rne | null = null\n\n private constructor() {\n // A different model picked in Settings invalidates the resident runner.\n useSettingsStore.subscribe(\n (state) => state.settings[SETTINGS.local_llm_model.key],\n () => {\n void this.unload()\n },\n )\n }\n\n static getInstance(): LocalLlmProvider {\n if (!LocalLlmProvider.instance) LocalLlmProvider.instance = new LocalLlmProvider()\n return LocalLlmProvider.instance\n }\n\n /** Whether a runner is resident right now (diagnostics; not a readiness check). */\n isLoaded(): boolean {\n return this.loaded !== null\n }\n\n loadedModelId(): LocalLlmModelId | null {\n return this.loaded?.modelId ?? null\n }\n\n /** Serve one neutral completion on-device. Throws when the model is missing or generation fails. */\n complete(request: LlmCompleteRequest): Promise<LocalLlmCompleteResult> {\n if (this.pendingCount >= MAX_PENDING_REQUESTS) {\n return Promise.reject(\n new Error(`The on-device model is busy (${this.pendingCount} requests pending); try again shortly.`),\n )\n }\n this.pendingCount += 1\n const queuedAt = Date.now()\n const run = this.queue.then(() => {\n const waitedMs = Date.now() - queuedAt\n if (waitedMs > QUEUE_WAIT_LIMIT_MS) {\n throw new Error(`The on-device model was busy for ${Math.round(waitedMs / 1000)} s; request dropped.`)\n }\n return this.completeInner(request)\n })\n this.queue = run.catch(() => undefined)\n return run.finally(() => {\n this.pendingCount -= 1\n })\n }\n\n /** Release the resident runner (idle, model switch, memory pressure). Safe to call repeatedly. */\n async unload(): Promise<void> {\n this.clearIdleTimer()\n // Never dispose under a running generation: let the queue drain first.\n await this.queue\n const pending = this.loading\n if (pending) {\n try {\n await pending\n } catch {\n // A failed load left nothing to release.\n }\n }\n this.releaseRunner()\n }\n\n /** Synchronous dispose; callers guarantee no generation is in flight. */\n private releaseRunner(): void {\n const loaded = this.loaded\n this.loaded = null\n if (!loaded) return\n try {\n loaded.runner.dispose()\n } catch (error) {\n console.warn(`${LOG_TAG}: runner dispose failed:`, error)\n } finally {\n loaded.tokenizer.dispose()\n }\n }\n\n private async completeInner(request: LlmCompleteRequest): Promise<LocalLlmCompleteResult> {\n this.clearIdleTimer()\n try {\n const model = await this.ensureLoaded()\n const jsonMode = request.jsonMode === true\n const tools = toTemplateTools(request.tools)\n\n const maxNewTokens = Math.max(\n 16,\n Math.min(request.maxTokens ?? DEFAULT_MAX_NEW_TOKENS, MAX_NEW_TOKENS_CEILING, Math.floor(model.maxSeqLen / 4)),\n )\n const promptBudget = model.maxSeqLen - maxNewTokens - CONTEXT_MARGIN_TOKENS\n const {prompt, promptTokens, truncated} = await fitPromptToContext(\n buildTemplateMessages({...request, jsonMode}),\n promptBudget,\n (messages) => model.template.render({messages, tools, add_generation_prompt: true, enable_thinking: false}),\n (text) => model.countTokens(model.tokenizer, text),\n )\n console.log(\n `${LOG_TAG}: ${model.modelId} prompt=${promptTokens} budget=${promptBudget} context=${model.maxSeqLen} maxNewTokens=${maxNewTokens} truncated=${truncated}`,\n )\n\n model.runner.reset(0)\n const deadline = BgTimer.setTimeout(() => {\n console.warn(`${LOG_TAG}: generation deadline hit, stopping`)\n model.runner.stop()\n }, GENERATION_DEADLINE_MS)\n let generated: {text: string; stats: LLMGenerationStats}\n const startedAt = Date.now()\n try {\n generated = await model.generate(\n model.runner,\n prompt,\n {maxNewTokens, temperature: request.temperature ?? 0.7},\n model.stopTokens,\n )\n } catch (error) {\n console.warn(\n `${LOG_TAG}: generation failed: model=${model.modelId} prompt=${promptTokens} context=${model.maxSeqLen} maxNewTokens=${maxNewTokens}`,\n error,\n )\n throw error\n } finally {\n BgTimer.clearTimeout(deadline)\n }\n\n const elapsedMs = Date.now() - startedAt\n console.log(\n `${LOG_TAG}: ${model.modelId} prompt=${generated.stats.numPromptTokens} out=${generated.stats.numGeneratedTokens} tokens in ${elapsedMs} ms`,\n )\n\n const seed = `${Date.now().toString(36)}${Math.random().toString(36).slice(2, 6)}`\n const completion = finalizeLocalResult(\n {\n text: generated.text,\n promptTokens: generated.stats.numPromptTokens,\n generatedTokens: generated.stats.numGeneratedTokens,\n maxNewTokens,\n },\n {jsonMode, toolsOffered: Boolean(tools), makeToolCallId: (index) => `local_${seed}_${index}`},\n )\n return {...completion, modelId: model.modelId}\n } finally {\n this.armIdleTimer()\n }\n }\n\n private async ensureLoaded(): Promise<LoadedModel> {\n const modelId = localLlmModelManager.selectedModelId()\n if (this.loaded?.modelId === modelId) return this.loaded\n if (this.loading) return this.loading\n\n this.loading = this.load(modelId)\n .then((model) => {\n this.loaded = model\n return model\n })\n .finally(() => {\n this.loading = null\n })\n return this.loading\n }\n\n private async load(modelId: LocalLlmModelId): Promise<LoadedModel> {\n // Runs inside the request queue, so nothing is generating; unload() would\n // wait on that same queue.\n this.releaseRunner()\n if (!(await localLlmModelManager.isModelAvailable(modelId))) {\n throw new Error(`on-device model ${modelId} is not downloaded`)\n }\n const rne = await this.runtime()\n const paths = localLlmModelManager.paths(modelId)\n\n const tokenizerConfig = rne.llm.parseTokenizerConfig(JSON.parse(await RNFS.readFile(paths.tokenizerConfig, \"utf8\")))\n const template = new Template(tokenizerConfig.chatTemplate)\n\n const startedAt = Date.now()\n const createRunner = rne.wrapAsync(rne.llm.createLLMRunner)\n const runner = await createRunner(paths.model, paths.tokenizer, [])\n let tokenizer: Tokenizer | undefined\n try {\n const maxSeqLen = runner.getKVCacheState().maxSeqLen\n if (!Number.isInteger(maxSeqLen) || maxSeqLen <= CONTEXT_MARGIN_TOKENS + 16) {\n throw new Error(`On-device model ${modelId} reported an invalid context size: ${maxSeqLen}`)\n }\n tokenizer = await rne.wrapAsync(rne.nlp.loadTokenizer)(paths.tokenizer)\n console.log(`${LOG_TAG}: loaded ${modelId} (context ${maxSeqLen}) in ${Date.now() - startedAt} ms`)\n return {\n modelId,\n runner,\n tokenizer,\n countTokens: rne.wrapAsync(countTokensWorklet),\n template,\n stopTokens: [...tokenizerConfig.stopTokens],\n maxSeqLen,\n generate: rne.wrapAsync(generateWorklet),\n }\n } catch (error) {\n tokenizer?.dispose()\n runner.dispose()\n throw error\n }\n }\n\n private async runtime(): Promise<Rne> {\n if (this.rne) return this.rne\n const rne: Rne = await import(\"react-native-executorch\")\n // Software Mansion's anonymous download analytics; the host has its own telemetry.\n rne.setTelemetryEnabled(false)\n this.rne = rne\n return rne\n }\n\n private armIdleTimer(): void {\n this.clearIdleTimer()\n this.idleTimer = BgTimer.setTimeout(() => {\n this.idleTimer = null\n console.log(`${LOG_TAG}: idle, unloading ${this.loaded?.modelId ?? \"nothing\"}`)\n void this.unload()\n }, IDLE_UNLOAD_MS)\n }\n\n private clearIdleTimer(): void {\n if (this.idleTimer) {\n BgTimer.clearTimeout(this.idleTimer)\n this.idleTimer = null\n }\n }\n}\n\nconst localLlmProvider = LocalLlmProvider.getInstance()\nexport {LocalLlmProvider}\nexport default localLlmProvider\n"]}
|
|
@@ -72,6 +72,7 @@ declare class LocalMiniappRuntime {
|
|
|
72
72
|
private connectedApps;
|
|
73
73
|
/** Claimed gestures (CLAIM_GESTURES): a stack per gesture, the most recent claimer holding it. */
|
|
74
74
|
private gestureClaims;
|
|
75
|
+
private retainedRenders;
|
|
75
76
|
/**
|
|
76
77
|
* Packages that have completed their CONNECT handshake (sent
|
|
77
78
|
* `miniapp_connect`, got `CONNECT_ACK`). Distinct from `connectedApps`,
|
|
@@ -284,6 +285,8 @@ declare class LocalMiniappRuntime {
|
|
|
284
285
|
* becomes the REQUEST_RESULT data.
|
|
285
286
|
*/
|
|
286
287
|
private handleRender;
|
|
288
|
+
/** Put a promoted app's last main-view frame on the glasses; widget mode had refused it. */
|
|
289
|
+
private replayRetainedRender;
|
|
287
290
|
/**
|
|
288
291
|
* display.showView: the active dashboard takes or releases the overlay
|
|
289
292
|
* slot. Refused (NOT_PERMITTED) for an app that is not a dashboard;
|
|
@@ -736,6 +739,8 @@ declare class LocalMiniappRuntime {
|
|
|
736
739
|
private widgetCallSeq;
|
|
737
740
|
/** Elements per widget frame; a card is small, and the link is slow. */
|
|
738
741
|
private static readonly WIDGET_FRAME_ELEMENT_CAP;
|
|
742
|
+
/** Elements per expanded widget frame: the full screen, on the same link. */
|
|
743
|
+
private static readonly WIDGET_EXPANDED_FRAME_ELEMENT_CAP;
|
|
739
744
|
private requireWidgetHost;
|
|
740
745
|
private declaredWidget;
|
|
741
746
|
/** The target app and its declared widget, or a reply to the host saying why not. */
|
|
@@ -746,7 +751,7 @@ declare class LocalMiniappRuntime {
|
|
|
746
751
|
/**
|
|
747
752
|
* session.widgets.place: record the host's box, wake the provider in widget
|
|
748
753
|
* mode when it is not running, hand it the box, and answer with the last
|
|
749
|
-
*
|
|
754
|
+
* card and expanded frames the provider pushed for the widget.
|
|
750
755
|
*/
|
|
751
756
|
private handleWidgetPlace;
|
|
752
757
|
private handleWidgetUnplace;
|
|
@@ -756,18 +761,35 @@ declare class LocalMiniappRuntime {
|
|
|
756
761
|
private handleWidgetSend;
|
|
757
762
|
/** Provider → host: a WIDGET_RESULT for a pending send; box acknowledgements have no pending call and are dropped. */
|
|
758
763
|
private handleWidgetResult;
|
|
759
|
-
/** Provider
|
|
764
|
+
/** Provider: read, or set with `expandable`, whether a tap expands one of its own `EX` widgets. */
|
|
765
|
+
private handleWidgetExpandable;
|
|
766
|
+
/** Provider → host, one-shot: cache the frame for its view and push it to every host holding the widget. */
|
|
760
767
|
private handleWidgetUpdate;
|
|
761
768
|
/** A miniapp disconnected: as a host its placements go, as a provider its boxes are forgotten and its frames kept. */
|
|
762
769
|
private onWidgetPartyGone;
|
|
763
770
|
/** True iff `value` serializes larger than the action payload cap. */
|
|
764
771
|
private actionPayloadTooLarge;
|
|
772
|
+
/** Floored at 6s: a freshly-woken target buffers an undelivered ACTION_CALL for 5s (HANDLER_WAIT_MS in @glassly/miniapp's actions module) + 1s for the reply. Keep in sync. */
|
|
773
|
+
private static clampActionTimeout;
|
|
774
|
+
/** Caller identity the host presents for host-initiated actions (a system caller). */
|
|
775
|
+
private static readonly HOST_ACTION_CALLER;
|
|
776
|
+
/** Installed, compatible, and declares the action — the same gates `invokeActionFromHost` applies up front. */
|
|
777
|
+
hostActionAvailable(targetPackageName: string, actionId: string): boolean;
|
|
778
|
+
/**
|
|
779
|
+
* Host-initiated action call (e.g. the watch remote): same gates, headless
|
|
780
|
+
* wake and ACTION_CALL/ACTION_RESULT correlation as a miniapp's
|
|
781
|
+
* session.actions.invoke, with the host as a system caller.
|
|
782
|
+
* Rejects with an Error whose `code` is a MiniappErrorCode.
|
|
783
|
+
*/
|
|
784
|
+
invokeActionFromHost(targetPackageName: string, actionId: string, params?: Record<string, unknown>, timeoutMs?: number): Promise<unknown>;
|
|
785
|
+
/** session.actions.invoke → run the call, then relay the outcome to the caller's pending request. */
|
|
786
|
+
private handleActionInvoke;
|
|
765
787
|
/**
|
|
766
|
-
*
|
|
767
|
-
*
|
|
788
|
+
* Gate, wake the target headlessly, deliver an ACTION_CALL, and resolve when
|
|
789
|
+
* its ACTION_RESULT (or the timeout) arrives. Never rejects.
|
|
768
790
|
*/
|
|
769
|
-
private
|
|
770
|
-
/** Target → host:
|
|
791
|
+
private runActionInvoke;
|
|
792
|
+
/** Target → host: settle the pending invoke for this ACTION_RESULT. */
|
|
771
793
|
private handleActionResult;
|
|
772
794
|
private sendResult;
|
|
773
795
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"LocalMiniappRuntime.d.ts","sourceRoot":"","sources":["../../src/services/LocalMiniappRuntime.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAsBH,OAAO,KAAK,EAAkB,mBAAmB,EAAqB,MAAM,kBAAkB,CAAA;
|
|
1
|
+
{"version":3,"file":"LocalMiniappRuntime.d.ts","sourceRoot":"","sources":["../../src/services/LocalMiniappRuntime.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAsBH,OAAO,KAAK,EAAkB,mBAAmB,EAAqB,MAAM,kBAAkB,CAAA;AA+I9F,OAAO,EAA+B,KAAK,gCAAgC,EAAC,MAAM,6BAA6B,CAAA;AAqB/G,MAAM,WAAW,wBAAwB;IACvC,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB;4EACwE;IACxE,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,cAAc,CAAC,EAAE,MAAM,CAAA;IACvB,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,KAAK,CAAC,EAAE;QAAC,UAAU,CAAC,EAAE,MAAM,CAAC;QAAC,EAAE,CAAC,EAAE,MAAM,CAAA;KAAC,CAAA;IAC1C,WAAW,CAAC,EAAE,KAAK,CAAC;QAAC,IAAI,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