@glassly/engine 0.3.3-dev.670 → 0.3.4-beta.685
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 +38 -14
- package/build/engine.d.ts.map +1 -1
- package/build/engine.js +3 -0
- package/build/engine.js.map +1 -1
- package/build/facades/ai.d.ts +32 -0
- package/build/facades/ai.d.ts.map +1 -0
- package/build/facades/ai.js +38 -0
- package/build/facades/ai.js.map +1 -0
- package/build/facades/glasses.d.ts +30 -24
- package/build/facades/glasses.d.ts.map +1 -1
- package/build/facades/glasses.js +49 -30
- package/build/facades/glasses.js.map +1 -1
- package/build/facades/glassesSettings.d.ts.map +1 -1
- package/build/facades/glassesSettings.js +1 -0
- package/build/facades/glassesSettings.js.map +1 -1
- package/build/facades/pairing.d.ts +6 -5
- package/build/facades/pairing.d.ts.map +1 -1
- package/build/facades/pairing.js +14 -14
- package/build/facades/pairing.js.map +1 -1
- package/build/generated/releaseMetadata.js +4 -4
- package/build/generated/releaseMetadata.js.map +1 -1
- package/build/index.d.ts +5 -2
- package/build/index.d.ts.map +1 -1
- package/build/index.js +2 -0
- package/build/index.js.map +1 -1
- package/build/services/DeviceEventRouter.d.ts.map +1 -1
- package/build/services/DeviceEventRouter.js +9 -1
- package/build/services/DeviceEventRouter.js.map +1 -1
- package/build/services/GestureDotsService.d.ts +14 -3
- package/build/services/GestureDotsService.d.ts.map +1 -1
- package/build/services/GestureDotsService.js +48 -13
- package/build/services/GestureDotsService.js.map +1 -1
- package/build/services/GlassesTouchInput.d.ts.map +1 -1
- package/build/services/GlassesTouchInput.js +11 -9
- package/build/services/GlassesTouchInput.js.map +1 -1
- package/build/services/LlmRouting.d.ts +35 -0
- package/build/services/LlmRouting.d.ts.map +1 -0
- package/build/services/LlmRouting.js +25 -0
- package/build/services/LlmRouting.js.map +1 -0
- package/build/services/LocalLlmModelManager.d.ts +79 -0
- package/build/services/LocalLlmModelManager.d.ts.map +1 -0
- package/build/services/LocalLlmModelManager.js +278 -0
- package/build/services/LocalLlmModelManager.js.map +1 -0
- package/build/services/LocalLlmPrompt.d.ts +90 -0
- package/build/services/LocalLlmPrompt.d.ts.map +1 -0
- package/build/services/LocalLlmPrompt.js +189 -0
- package/build/services/LocalLlmPrompt.js.map +1 -0
- package/build/services/LocalLlmProvider.d.ts +35 -0
- package/build/services/LocalLlmProvider.d.ts.map +1 -0
- package/build/services/LocalLlmProvider.js +218 -0
- package/build/services/LocalLlmProvider.js.map +1 -0
- package/build/services/LocalMiniappRuntime.d.ts +9 -1
- package/build/services/LocalMiniappRuntime.d.ts.map +1 -1
- package/build/services/LocalMiniappRuntime.js +91 -17
- package/build/services/LocalMiniappRuntime.js.map +1 -1
- package/build/services/STTModelManager.d.ts +25 -2
- package/build/services/STTModelManager.d.ts.map +1 -1
- package/build/services/STTModelManager.js +137 -6
- package/build/services/STTModelManager.js.map +1 -1
- package/build/services/controllers.d.ts +21 -0
- package/build/services/controllers.d.ts.map +1 -0
- package/build/services/controllers.js +62 -0
- package/build/services/controllers.js.map +1 -0
- package/build/services/effectiveCapabilities.d.ts.map +1 -1
- package/build/services/effectiveCapabilities.js +18 -24
- package/build/services/effectiveCapabilities.js.map +1 -1
- package/build/services/neuralBand/NeuralBandInput.d.ts +17 -6
- package/build/services/neuralBand/NeuralBandInput.d.ts.map +1 -1
- package/build/services/neuralBand/NeuralBandInput.js +40 -23
- package/build/services/neuralBand/NeuralBandInput.js.map +1 -1
- package/build/services/neuralBand/NeuralBandSettingsSync.d.ts.map +1 -1
- package/build/services/neuralBand/NeuralBandSettingsSync.js +23 -26
- package/build/services/neuralBand/NeuralBandSettingsSync.js.map +1 -1
- package/build/services/settingsBridge.d.ts +26 -18
- package/build/services/settingsBridge.d.ts.map +1 -1
- package/build/services/settingsBridge.js +36 -15
- package/build/services/settingsBridge.js.map +1 -1
- package/build/stores/bluetoothSettingKeys.d.ts.map +1 -1
- package/build/stores/bluetoothSettingKeys.js +3 -1
- package/build/stores/bluetoothSettingKeys.js.map +1 -1
- package/build/stores/core.d.ts.map +1 -1
- package/build/stores/core.js +0 -1
- package/build/stores/core.js.map +1 -1
- package/build/stores/glasses.d.ts.map +1 -1
- package/build/stores/glasses.js +2 -6
- package/build/stores/glasses.js.map +1 -1
- package/build/stores/settings.d.ts.map +1 -1
- package/build/stores/settings.js +29 -3
- package/build/stores/settings.js.map +1 -1
- package/package.json +12 -8
- package/src/engine.ts +3 -0
- package/src/facades/ai.ts +43 -0
- package/src/facades/glasses.ts +60 -31
- package/src/facades/glassesSettings.ts +1 -0
- package/src/facades/pairing.ts +18 -14
- package/src/generated/releaseMetadata.ts +4 -4
- package/src/index.ts +16 -2
- package/src/services/DeviceEventRouter.ts +8 -1
- package/src/services/GestureDotsService.ts +52 -15
- package/src/services/GlassesTouchInput.ts +11 -9
- package/src/services/LlmRouting.ts +49 -0
- package/src/services/LocalLlmModelManager.ts +327 -0
- package/src/services/LocalLlmPrompt.ts +246 -0
- package/src/services/LocalLlmProvider.ts +290 -0
- package/src/services/LocalMiniappRuntime.ts +137 -21
- package/src/services/STTModelManager.ts +140 -8
- package/src/services/controllers.ts +77 -0
- package/src/services/effectiveCapabilities.ts +16 -32
- package/src/services/neuralBand/NeuralBandInput.ts +50 -26
- package/src/services/neuralBand/NeuralBandSettingsSync.ts +28 -26
- package/src/services/settingsBridge.ts +60 -22
- package/src/stores/bluetoothSettingKeys.ts +3 -1
- package/src/stores/core.ts +0 -1
- package/src/stores/glasses.ts +2 -6
- package/src/stores/settings.ts +29 -3
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LocalLlmPrompt — the pure half of the on-device LLM path: turning a neutral
|
|
3
|
+
* session.ai request into chat-template messages, fitting them into the
|
|
4
|
+
* model's context, and reading the model's text back into the neutral result
|
|
5
|
+
* (tool calls, JSON mode, stop reason). No native imports, so it is
|
|
6
|
+
* bun-testable; LocalLlmProvider owns the runner.
|
|
7
|
+
*
|
|
8
|
+
* Written against Qwen3's chat template: tools are advertised by the
|
|
9
|
+
* template itself, calls come back as `<tool_call>{json}</tool_call>`, and
|
|
10
|
+
* thinking is a `<think>…</think>` block we never ask for but still strip.
|
|
11
|
+
*/
|
|
12
|
+
import { jsonrepair } from "jsonrepair";
|
|
13
|
+
/** Appended to the system prompt under `jsonMode`; small models need it said twice. */
|
|
14
|
+
export const JSON_MODE_INSTRUCTION = "Respond with exactly one JSON object and nothing else: no prose before or after it, no markdown fences.";
|
|
15
|
+
/**
|
|
16
|
+
* Neutral messages → template messages. A user turn carrying toolResults
|
|
17
|
+
* becomes one `tool` message per result (the template groups consecutive
|
|
18
|
+
* tool messages into a single user block), followed by the user text if any.
|
|
19
|
+
*/
|
|
20
|
+
export function buildTemplateMessages(request) {
|
|
21
|
+
const out = [];
|
|
22
|
+
const systemParts = [request.system?.trim(), request.jsonMode ? JSON_MODE_INSTRUCTION : undefined].filter((part) => Boolean(part));
|
|
23
|
+
if (systemParts.length)
|
|
24
|
+
out.push({ role: "system", content: systemParts.join("\n\n") });
|
|
25
|
+
for (const message of request.messages) {
|
|
26
|
+
if (message.role === "assistant") {
|
|
27
|
+
const toolCalls = message.toolCalls?.map((call) => ({
|
|
28
|
+
id: call.id,
|
|
29
|
+
type: "function",
|
|
30
|
+
function: { name: call.name, arguments: call.input },
|
|
31
|
+
}));
|
|
32
|
+
out.push(toolCalls?.length
|
|
33
|
+
? { role: "assistant", content: message.content, tool_calls: toolCalls }
|
|
34
|
+
: { role: "assistant", content: message.content });
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
for (const result of message.toolResults ?? []) {
|
|
38
|
+
out.push({
|
|
39
|
+
role: "tool",
|
|
40
|
+
tool_call_id: result.toolCallId,
|
|
41
|
+
content: result.isError ? `Error: ${result.content}` : result.content,
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
if (message.content.trim().length > 0 || !message.toolResults?.length) {
|
|
45
|
+
out.push({ role: "user", content: message.content });
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return out;
|
|
49
|
+
}
|
|
50
|
+
export function toTemplateTools(tools) {
|
|
51
|
+
if (!tools?.length)
|
|
52
|
+
return undefined;
|
|
53
|
+
return tools.map((tool) => ({
|
|
54
|
+
type: "function",
|
|
55
|
+
function: { name: tool.name, description: tool.description, parameters: tool.inputSchema },
|
|
56
|
+
}));
|
|
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
|
+
/**
|
|
75
|
+
* Fit the conversation into `budgetTokens`. History is grouped into turns
|
|
76
|
+
* (a user message plus everything up to the next user message, so a tool
|
|
77
|
+
* round stays with the question it answers); the oldest turns go first and
|
|
78
|
+
* the last turn is never dropped — the model must at least see the question.
|
|
79
|
+
* When that last turn alone still does not fit, tool outputs are cut to
|
|
80
|
+
* their head.
|
|
81
|
+
*/
|
|
82
|
+
export function fitToBudget(messages, budgetTokens, toolTokens = 0) {
|
|
83
|
+
const total = (list) => list.reduce((sum, m) => sum + estimateMessageTokens(m), toolTokens);
|
|
84
|
+
const system = messages[0]?.role === "system" ? [messages[0]] : [];
|
|
85
|
+
const turns = [];
|
|
86
|
+
for (const message of messages.slice(system.length)) {
|
|
87
|
+
if (message.role === "user" || turns.length === 0)
|
|
88
|
+
turns.push([message]);
|
|
89
|
+
else
|
|
90
|
+
turns[turns.length - 1].push(message);
|
|
91
|
+
}
|
|
92
|
+
while (turns.length > 1 && total([...system, ...turns.flat()]) > budgetTokens) {
|
|
93
|
+
turns.shift();
|
|
94
|
+
}
|
|
95
|
+
let fitted = [...system, ...turns.flat()];
|
|
96
|
+
if (total(fitted) > budgetTokens) {
|
|
97
|
+
fitted = fitted.map((m) => m.role === "tool" && m.content.length > MIN_TOOL_CONTENT_CHARS
|
|
98
|
+
? { ...m, content: `${m.content.slice(0, MIN_TOOL_CONTENT_CHARS)}\n…[truncated]` }
|
|
99
|
+
: m);
|
|
100
|
+
}
|
|
101
|
+
return fitted;
|
|
102
|
+
}
|
|
103
|
+
/** Remove a leading `<think>…</think>` block (or an unterminated one). */
|
|
104
|
+
export function stripThinking(text) {
|
|
105
|
+
const trimmed = text.replace(/^\s*<think>[\s\S]*?<\/think>\s*/u, "");
|
|
106
|
+
if (trimmed !== text)
|
|
107
|
+
return trimmed;
|
|
108
|
+
return text.replace(/^\s*<think>[\s\S]*$/u, "").trimStart();
|
|
109
|
+
}
|
|
110
|
+
const TOOL_CALL_RE = /<tool_call>\s*([\s\S]*?)\s*<\/tool_call>/gu;
|
|
111
|
+
/** Pull `<tool_call>` blocks out of the reply. Unparseable blocks are dropped, not surfaced. */
|
|
112
|
+
export function parseToolCalls(text, makeId) {
|
|
113
|
+
const toolCalls = [];
|
|
114
|
+
let index = 0;
|
|
115
|
+
const remaining = text
|
|
116
|
+
.replace(TOOL_CALL_RE, (_match, body) => {
|
|
117
|
+
const parsed = parseJsonLoose(body);
|
|
118
|
+
if (parsed && typeof parsed === "object" && typeof parsed.name === "string") {
|
|
119
|
+
const { name, arguments: args } = parsed;
|
|
120
|
+
toolCalls.push({
|
|
121
|
+
id: makeId(index++),
|
|
122
|
+
name,
|
|
123
|
+
input: args && typeof args === "object" && !Array.isArray(args) ? args : {},
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
return "";
|
|
127
|
+
})
|
|
128
|
+
.trim();
|
|
129
|
+
return { toolCalls, text: remaining };
|
|
130
|
+
}
|
|
131
|
+
function parseJsonLoose(body) {
|
|
132
|
+
try {
|
|
133
|
+
return JSON.parse(body);
|
|
134
|
+
}
|
|
135
|
+
catch {
|
|
136
|
+
try {
|
|
137
|
+
return JSON.parse(jsonrepair(body));
|
|
138
|
+
}
|
|
139
|
+
catch {
|
|
140
|
+
return undefined;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* The first JSON object in the reply, repaired if the model left it slightly
|
|
146
|
+
* broken (trailing commas, single quotes, a dropped brace). Null when there
|
|
147
|
+
* is nothing object-shaped: the caller keeps the raw text and the miniapp's
|
|
148
|
+
* own parser decides.
|
|
149
|
+
*/
|
|
150
|
+
export function extractJsonObject(text) {
|
|
151
|
+
const unfenced = text.replace(/```(?:json)?/giu, "").trim();
|
|
152
|
+
const start = unfenced.indexOf("{");
|
|
153
|
+
if (start < 0)
|
|
154
|
+
return null;
|
|
155
|
+
const end = unfenced.lastIndexOf("}");
|
|
156
|
+
const candidate = end > start ? unfenced.slice(start, end + 1) : unfenced.slice(start);
|
|
157
|
+
try {
|
|
158
|
+
JSON.parse(candidate);
|
|
159
|
+
return candidate;
|
|
160
|
+
}
|
|
161
|
+
catch {
|
|
162
|
+
try {
|
|
163
|
+
const repaired = jsonrepair(candidate);
|
|
164
|
+
JSON.parse(repaired);
|
|
165
|
+
return repaired;
|
|
166
|
+
}
|
|
167
|
+
catch {
|
|
168
|
+
return null;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
/** Raw generation → neutral completion. */
|
|
173
|
+
export function finalizeLocalResult(generation, opts) {
|
|
174
|
+
const usage = { inputTokens: generation.promptTokens, outputTokens: generation.generatedTokens };
|
|
175
|
+
let text = stripThinking(generation.text);
|
|
176
|
+
if (opts.toolsOffered) {
|
|
177
|
+
const parsed = parseToolCalls(text, opts.makeToolCallId);
|
|
178
|
+
if (parsed.toolCalls.length) {
|
|
179
|
+
return { text: parsed.text, stopReason: "tool_use", toolCalls: parsed.toolCalls, usage };
|
|
180
|
+
}
|
|
181
|
+
text = parsed.text;
|
|
182
|
+
}
|
|
183
|
+
if (opts.jsonMode) {
|
|
184
|
+
text = extractJsonObject(text) ?? text;
|
|
185
|
+
}
|
|
186
|
+
const stopReason = generation.generatedTokens >= generation.maxNewTokens ? "max_tokens" : "end_turn";
|
|
187
|
+
return { text: text.trim(), stopReason, usage };
|
|
188
|
+
}
|
|
189
|
+
//# sourceMappingURL=LocalLlmPrompt.js.map
|
|
@@ -0,0 +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,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IACH,CAAC;AACH,CAAC;AAgBD,2CAA2C;AAC3C,MAAM,UAAU,mBAAmB,CACjC,UAA2B,EAC3B,IAA2F;IAE3F,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"]}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { LlmCompleteRequest } from "@glassly/cloud-protocol/llm";
|
|
2
|
+
import { type LocalLlmModelId } from "./LocalLlmModelManager";
|
|
3
|
+
import { type LocalCompletion } from "./LocalLlmPrompt";
|
|
4
|
+
export interface LocalLlmCompleteResult extends LocalCompletion {
|
|
5
|
+
modelId: LocalLlmModelId;
|
|
6
|
+
}
|
|
7
|
+
declare class LocalLlmProvider {
|
|
8
|
+
private static instance;
|
|
9
|
+
private loaded;
|
|
10
|
+
private loading;
|
|
11
|
+
private queue;
|
|
12
|
+
private idleTimer;
|
|
13
|
+
private rne;
|
|
14
|
+
private constructor();
|
|
15
|
+
static getInstance(): LocalLlmProvider;
|
|
16
|
+
/** Whether a runner is resident right now (diagnostics; not a readiness check). */
|
|
17
|
+
isLoaded(): boolean;
|
|
18
|
+
loadedModelId(): LocalLlmModelId | null;
|
|
19
|
+
/** Serve one neutral completion on-device. Throws when the model is missing or generation fails. */
|
|
20
|
+
complete(request: LlmCompleteRequest): Promise<LocalLlmCompleteResult>;
|
|
21
|
+
/** Release the resident runner (idle, model switch, memory pressure). Safe to call repeatedly. */
|
|
22
|
+
unload(): Promise<void>;
|
|
23
|
+
/** Synchronous dispose; callers guarantee no generation is in flight. */
|
|
24
|
+
private releaseRunner;
|
|
25
|
+
private completeInner;
|
|
26
|
+
private ensureLoaded;
|
|
27
|
+
private load;
|
|
28
|
+
private runtime;
|
|
29
|
+
private armIdleTimer;
|
|
30
|
+
private clearIdleTimer;
|
|
31
|
+
}
|
|
32
|
+
declare const localLlmProvider: LocalLlmProvider;
|
|
33
|
+
export { LocalLlmProvider };
|
|
34
|
+
export default localLlmProvider;
|
|
35
|
+
//# sourceMappingURL=LocalLlmProvider.d.ts.map
|
|
@@ -0,0 +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,EAML,KAAK,eAAe,EACrB,MAAM,kBAAkB,CAAA;AAmCzB,MAAM,WAAW,sBAAuB,SAAQ,eAAe;IAC7D,OAAO,EAAE,eAAe,CAAA;CACzB;AAqBD,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,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;IAMtE,kGAAkG;IAC5F,MAAM,IAAI,OAAO,CAAC,IAAI,CAAC;IAe7B,yEAAyE;IACzE,OAAO,CAAC,aAAa;YAWP,aAAa;YA6Db,YAAY;YAgBZ,IAAI;YA6BJ,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"}
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LocalLlmProvider — session.ai.complete served by the phone itself: an
|
|
3
|
+
* ExecuTorch runner (react-native-executorch) over the Qwen3 export that
|
|
4
|
+
* LocalLlmModelManager downloaded. Stateless per request like the vendor
|
|
5
|
+
* providers: every call renders the whole conversation through the model's
|
|
6
|
+
* chat template, resets the KV cache, and generates on ExecuTorch's worklet
|
|
7
|
+
* thread so the JS thread stays free.
|
|
8
|
+
*
|
|
9
|
+
* The runner is loaded on the first request and kept resident while calls
|
|
10
|
+
* keep coming (loading a 1–3 GB model is seconds, not milliseconds); it is
|
|
11
|
+
* dropped after an idle period and whenever the selected model changes.
|
|
12
|
+
* Requests are serialized — one model, one KV cache.
|
|
13
|
+
*
|
|
14
|
+
* react-native-executorch is imported lazily so the engine module graph
|
|
15
|
+
* loads without the native runtime (bun tests, hosts without the dependency).
|
|
16
|
+
*/
|
|
17
|
+
import * as RNFS from "@dr.pogodin/react-native-fs";
|
|
18
|
+
import { Template } from "@huggingface/jinja";
|
|
19
|
+
import { BgTimer } from "../utils/timers";
|
|
20
|
+
import localLlmModelManager from "./LocalLlmModelManager";
|
|
21
|
+
import { buildTemplateMessages, estimateTokens, finalizeLocalResult, fitToBudget, toTemplateTools, } from "./LocalLlmPrompt";
|
|
22
|
+
import { SETTINGS, useSettingsStore } from "../stores/settings";
|
|
23
|
+
const LOG_TAG = "LOCAL_LLM";
|
|
24
|
+
/** Drop the resident model after this long without a request. */
|
|
25
|
+
const IDLE_UNLOAD_MS = 5 * 60_000;
|
|
26
|
+
/** Hard stop on one generation — the SDK request times out at 120 s anyway. */
|
|
27
|
+
const GENERATION_DEADLINE_MS = 100_000;
|
|
28
|
+
const DEFAULT_MAX_NEW_TOKENS = 512;
|
|
29
|
+
const MAX_NEW_TOKENS_CEILING = 1_024;
|
|
30
|
+
/** Headroom between the estimated prompt and the context end; estimates are rough. */
|
|
31
|
+
const CONTEXT_MARGIN_TOKENS = 96;
|
|
32
|
+
/**
|
|
33
|
+
* Runs on ExecuTorch's worklet runtime: `runner.generate` blocks its thread
|
|
34
|
+
* until EOS, `maxNewTokens`, or `runner.stop()` from the JS thread.
|
|
35
|
+
*/
|
|
36
|
+
function generateWorklet(runner, prompt, config, stopTokens) {
|
|
37
|
+
"worklet";
|
|
38
|
+
let text = "";
|
|
39
|
+
const stats = runner.generate(prompt, config, (token) => {
|
|
40
|
+
if (stopTokens.includes(token))
|
|
41
|
+
return;
|
|
42
|
+
text += token;
|
|
43
|
+
});
|
|
44
|
+
return { text, stats };
|
|
45
|
+
}
|
|
46
|
+
class LocalLlmProvider {
|
|
47
|
+
static instance;
|
|
48
|
+
loaded = null;
|
|
49
|
+
loading = null;
|
|
50
|
+
queue = Promise.resolve();
|
|
51
|
+
idleTimer = null;
|
|
52
|
+
rne = null;
|
|
53
|
+
constructor() {
|
|
54
|
+
// A different model picked in Settings invalidates the resident runner.
|
|
55
|
+
useSettingsStore.subscribe((state) => state.settings[SETTINGS.local_llm_model.key], () => {
|
|
56
|
+
void this.unload();
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
static getInstance() {
|
|
60
|
+
if (!LocalLlmProvider.instance)
|
|
61
|
+
LocalLlmProvider.instance = new LocalLlmProvider();
|
|
62
|
+
return LocalLlmProvider.instance;
|
|
63
|
+
}
|
|
64
|
+
/** Whether a runner is resident right now (diagnostics; not a readiness check). */
|
|
65
|
+
isLoaded() {
|
|
66
|
+
return this.loaded !== null;
|
|
67
|
+
}
|
|
68
|
+
loadedModelId() {
|
|
69
|
+
return this.loaded?.modelId ?? null;
|
|
70
|
+
}
|
|
71
|
+
/** Serve one neutral completion on-device. Throws when the model is missing or generation fails. */
|
|
72
|
+
complete(request) {
|
|
73
|
+
const run = this.queue.then(() => this.completeInner(request));
|
|
74
|
+
this.queue = run.catch(() => undefined);
|
|
75
|
+
return run;
|
|
76
|
+
}
|
|
77
|
+
/** Release the resident runner (idle, model switch, memory pressure). Safe to call repeatedly. */
|
|
78
|
+
async unload() {
|
|
79
|
+
this.clearIdleTimer();
|
|
80
|
+
// Never dispose under a running generation: let the queue drain first.
|
|
81
|
+
await this.queue;
|
|
82
|
+
const pending = this.loading;
|
|
83
|
+
if (pending) {
|
|
84
|
+
try {
|
|
85
|
+
await pending;
|
|
86
|
+
}
|
|
87
|
+
catch {
|
|
88
|
+
// A failed load left nothing to release.
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
this.releaseRunner();
|
|
92
|
+
}
|
|
93
|
+
/** Synchronous dispose; callers guarantee no generation is in flight. */
|
|
94
|
+
releaseRunner() {
|
|
95
|
+
const loaded = this.loaded;
|
|
96
|
+
this.loaded = null;
|
|
97
|
+
if (!loaded)
|
|
98
|
+
return;
|
|
99
|
+
try {
|
|
100
|
+
loaded.runner.dispose();
|
|
101
|
+
}
|
|
102
|
+
catch (error) {
|
|
103
|
+
console.warn(`${LOG_TAG}: runner dispose failed:`, error);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
async completeInner(request) {
|
|
107
|
+
this.clearIdleTimer();
|
|
108
|
+
try {
|
|
109
|
+
const model = await this.ensureLoaded();
|
|
110
|
+
const jsonMode = request.jsonMode === true;
|
|
111
|
+
const tools = toTemplateTools(request.tools);
|
|
112
|
+
const toolTokens = tools ? estimateTokens(JSON.stringify(tools)) + 64 : 0;
|
|
113
|
+
const maxNewTokens = Math.max(16, Math.min(request.maxTokens ?? DEFAULT_MAX_NEW_TOKENS, MAX_NEW_TOKENS_CEILING, Math.floor(model.maxSeqLen / 4)));
|
|
114
|
+
const promptBudget = model.maxSeqLen - maxNewTokens - CONTEXT_MARGIN_TOKENS;
|
|
115
|
+
const messages = fitToBudget(buildTemplateMessages({ ...request, jsonMode }), promptBudget, toolTokens);
|
|
116
|
+
const prompt = model.template.render({
|
|
117
|
+
messages,
|
|
118
|
+
tools,
|
|
119
|
+
add_generation_prompt: true,
|
|
120
|
+
enable_thinking: false,
|
|
121
|
+
});
|
|
122
|
+
model.runner.reset(0);
|
|
123
|
+
const deadline = BgTimer.setTimeout(() => {
|
|
124
|
+
console.warn(`${LOG_TAG}: generation deadline hit, stopping`);
|
|
125
|
+
model.runner.stop();
|
|
126
|
+
}, GENERATION_DEADLINE_MS);
|
|
127
|
+
let generated;
|
|
128
|
+
const startedAt = Date.now();
|
|
129
|
+
try {
|
|
130
|
+
generated = await model.generate(model.runner, prompt, { maxNewTokens, temperature: request.temperature ?? 0.7 }, model.stopTokens);
|
|
131
|
+
}
|
|
132
|
+
finally {
|
|
133
|
+
BgTimer.clearTimeout(deadline);
|
|
134
|
+
}
|
|
135
|
+
const elapsedMs = Date.now() - startedAt;
|
|
136
|
+
console.log(`${LOG_TAG}: ${model.modelId} prompt=${generated.stats.numPromptTokens} out=${generated.stats.numGeneratedTokens} tokens in ${elapsedMs} ms`);
|
|
137
|
+
const seed = `${Date.now().toString(36)}${Math.random().toString(36).slice(2, 6)}`;
|
|
138
|
+
const completion = finalizeLocalResult({
|
|
139
|
+
text: generated.text,
|
|
140
|
+
promptTokens: generated.stats.numPromptTokens,
|
|
141
|
+
generatedTokens: generated.stats.numGeneratedTokens,
|
|
142
|
+
maxNewTokens,
|
|
143
|
+
}, { jsonMode, toolsOffered: Boolean(tools), makeToolCallId: (index) => `local_${seed}_${index}` });
|
|
144
|
+
return { ...completion, modelId: model.modelId };
|
|
145
|
+
}
|
|
146
|
+
finally {
|
|
147
|
+
this.armIdleTimer();
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
async ensureLoaded() {
|
|
151
|
+
const modelId = localLlmModelManager.selectedModelId();
|
|
152
|
+
if (this.loaded?.modelId === modelId)
|
|
153
|
+
return this.loaded;
|
|
154
|
+
if (this.loading)
|
|
155
|
+
return this.loading;
|
|
156
|
+
this.loading = this.load(modelId)
|
|
157
|
+
.then((model) => {
|
|
158
|
+
this.loaded = model;
|
|
159
|
+
return model;
|
|
160
|
+
})
|
|
161
|
+
.finally(() => {
|
|
162
|
+
this.loading = null;
|
|
163
|
+
});
|
|
164
|
+
return this.loading;
|
|
165
|
+
}
|
|
166
|
+
async load(modelId) {
|
|
167
|
+
// Runs inside the request queue, so nothing is generating; unload() would
|
|
168
|
+
// wait on that same queue.
|
|
169
|
+
this.releaseRunner();
|
|
170
|
+
if (!(await localLlmModelManager.isModelAvailable(modelId))) {
|
|
171
|
+
throw new Error(`on-device model ${modelId} is not downloaded`);
|
|
172
|
+
}
|
|
173
|
+
const rne = await this.runtime();
|
|
174
|
+
const paths = localLlmModelManager.paths(modelId);
|
|
175
|
+
const tokenizerConfig = rne.llm.parseTokenizerConfig(JSON.parse(await RNFS.readFile(paths.tokenizerConfig, "utf8")));
|
|
176
|
+
const template = new Template(tokenizerConfig.chatTemplate);
|
|
177
|
+
const startedAt = Date.now();
|
|
178
|
+
const createRunner = rne.wrapAsync(rne.llm.createLLMRunner);
|
|
179
|
+
const runner = await createRunner(paths.model, paths.tokenizer, []);
|
|
180
|
+
const maxSeqLen = runner.getKVCacheState().maxSeqLen;
|
|
181
|
+
console.log(`${LOG_TAG}: loaded ${modelId} (context ${maxSeqLen}) in ${Date.now() - startedAt} ms`);
|
|
182
|
+
return {
|
|
183
|
+
modelId,
|
|
184
|
+
runner,
|
|
185
|
+
template,
|
|
186
|
+
stopTokens: [...tokenizerConfig.stopTokens],
|
|
187
|
+
maxSeqLen,
|
|
188
|
+
generate: rne.wrapAsync(generateWorklet),
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
async runtime() {
|
|
192
|
+
if (this.rne)
|
|
193
|
+
return this.rne;
|
|
194
|
+
const rne = await import("react-native-executorch");
|
|
195
|
+
// Software Mansion's anonymous download analytics; the host has its own telemetry.
|
|
196
|
+
rne.setTelemetryEnabled(false);
|
|
197
|
+
this.rne = rne;
|
|
198
|
+
return rne;
|
|
199
|
+
}
|
|
200
|
+
armIdleTimer() {
|
|
201
|
+
this.clearIdleTimer();
|
|
202
|
+
this.idleTimer = BgTimer.setTimeout(() => {
|
|
203
|
+
this.idleTimer = null;
|
|
204
|
+
console.log(`${LOG_TAG}: idle, unloading ${this.loaded?.modelId ?? "nothing"}`);
|
|
205
|
+
void this.unload();
|
|
206
|
+
}, IDLE_UNLOAD_MS);
|
|
207
|
+
}
|
|
208
|
+
clearIdleTimer() {
|
|
209
|
+
if (this.idleTimer) {
|
|
210
|
+
BgTimer.clearTimeout(this.idleTimer);
|
|
211
|
+
this.idleTimer = null;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
const localLlmProvider = LocalLlmProvider.getInstance();
|
|
216
|
+
export { LocalLlmProvider };
|
|
217
|
+
export default localLlmProvider;
|
|
218
|
+
//# sourceMappingURL=LocalLlmProvider.js.map
|
|
@@ -0,0 +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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Ac,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 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"]}
|
|
@@ -305,7 +305,7 @@ declare class LocalMiniappRuntime {
|
|
|
305
305
|
/**
|
|
306
306
|
* Push bridged settings as they change: stored keys from the store, the
|
|
307
307
|
* derived keys whenever their inputs move (phone activity, the display,
|
|
308
|
-
* the apps store, the
|
|
308
|
+
* the apps store, the controllers, the dashboard role). Subscribers declared
|
|
309
309
|
* PLATFORM_SETTINGS (gated at SUBSCRIBE), so this is a no-op fan-out for
|
|
310
310
|
* everyone else.
|
|
311
311
|
*/
|
|
@@ -455,6 +455,14 @@ declare class LocalMiniappRuntime {
|
|
|
455
455
|
* host-side only, unlike session.keys.get() which deliberately exposes it.
|
|
456
456
|
*/
|
|
457
457
|
private handleAiComplete;
|
|
458
|
+
/**
|
|
459
|
+
* Serve one ai.complete on the phone's own model (LocalLlmProvider). With
|
|
460
|
+
* no model on disk the download starts (or continues) in the background
|
|
461
|
+
* and the miniapp gets AI_LOCAL_UNAVAILABLE naming the state — unless
|
|
462
|
+
* `primaryError` is given (a failed fallback), in which case the primary's
|
|
463
|
+
* own error is what the miniapp sees.
|
|
464
|
+
*/
|
|
465
|
+
private serveLocalAi;
|
|
458
466
|
/**
|
|
459
467
|
* ai.generate — agentic visual generation. Always relayed to the cloud
|
|
460
468
|
* runtime (the server drives the provider's agentic loop; there is no
|
|
@@ -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;AAoH9F,OAAO,EAA+B,KAAK,gCAAgC,EAAC,MAAM,6BAA6B,CAAA;AAmB/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,EAA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