marona 0.2.6 → 0.2.16
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/CHANGELOG.md +40 -0
- package/README.md +131 -53
- package/dist/index.d.ts +36 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +461 -15
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,6 +1,47 @@
|
|
|
1
|
-
export const packageVersion = "0.2.
|
|
1
|
+
export const packageVersion = "0.2.13";
|
|
2
2
|
export const standardInterfaces = ["api", "web", "mobile_app", "desktop", "whatsapp"];
|
|
3
3
|
export const interfaceNamePattern = "^[a-z][a-z0-9_-]{0,63}$";
|
|
4
|
+
export const standardExecutionModes = ["online", "hybrid", "offline"];
|
|
5
|
+
export function normalizeExecutionModes(value) {
|
|
6
|
+
const rawModes = Array.isArray(value) ? value : ["online"];
|
|
7
|
+
const modes = [];
|
|
8
|
+
for (const raw of rawModes) {
|
|
9
|
+
const mode = String(raw).trim().toLowerCase();
|
|
10
|
+
if (standardExecutionModes.includes(mode) && !modes.includes(mode)) {
|
|
11
|
+
modes.push(mode);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
if (!modes.length)
|
|
15
|
+
return ["online"];
|
|
16
|
+
if (modes.includes("hybrid") || (modes.includes("online") && modes.includes("offline"))) {
|
|
17
|
+
return ["hybrid"];
|
|
18
|
+
}
|
|
19
|
+
return [modes[0]];
|
|
20
|
+
}
|
|
21
|
+
export function supportsExecutionMode(value, mode) {
|
|
22
|
+
const requested = mode.trim().toLowerCase();
|
|
23
|
+
const modes = new Set(normalizeExecutionModes(value));
|
|
24
|
+
if (modes.has("hybrid"))
|
|
25
|
+
return standardExecutionModes.includes(requested);
|
|
26
|
+
if (requested === "hybrid")
|
|
27
|
+
return modes.has("online") || modes.has("offline");
|
|
28
|
+
return modes.has(requested);
|
|
29
|
+
}
|
|
30
|
+
export function executionModeLabel(value) {
|
|
31
|
+
const modes = normalizeExecutionModes(value);
|
|
32
|
+
if (modes.includes("hybrid"))
|
|
33
|
+
return "Hybrid";
|
|
34
|
+
return modes.map(executionModeTitle).join(" + ");
|
|
35
|
+
}
|
|
36
|
+
function executionModeTitle(mode) {
|
|
37
|
+
if (mode === "online")
|
|
38
|
+
return "Online";
|
|
39
|
+
if (mode === "hybrid")
|
|
40
|
+
return "Hybrid";
|
|
41
|
+
if (mode === "offline")
|
|
42
|
+
return "Offline";
|
|
43
|
+
return mode;
|
|
44
|
+
}
|
|
4
45
|
export class EdgeRuntimeError extends Error {
|
|
5
46
|
statusCode;
|
|
6
47
|
payload;
|
|
@@ -52,6 +93,50 @@ export class MaronaCache {
|
|
|
52
93
|
this.apps.set(slug, record);
|
|
53
94
|
}
|
|
54
95
|
}
|
|
96
|
+
importJson(payload) {
|
|
97
|
+
this.clear();
|
|
98
|
+
const syncPayload = asRecord(payload.sync_payload);
|
|
99
|
+
if (syncPayload)
|
|
100
|
+
this.saveSyncPayload(syncPayload);
|
|
101
|
+
const privateContext = asRecord(payload.private_context);
|
|
102
|
+
if (privateContext)
|
|
103
|
+
this.privateContext = { ...privateContext };
|
|
104
|
+
const apps = Array.isArray(payload.apps) ? payload.apps : [];
|
|
105
|
+
for (const app of apps) {
|
|
106
|
+
const record = asRecord(app);
|
|
107
|
+
const slug = stringValue(record?.slug);
|
|
108
|
+
if (record && slug)
|
|
109
|
+
this.apps.set(slug, record);
|
|
110
|
+
}
|
|
111
|
+
const models = Array.isArray(payload.models) ? payload.models : [];
|
|
112
|
+
for (const model of models) {
|
|
113
|
+
const record = asRecord(model);
|
|
114
|
+
if (record)
|
|
115
|
+
this.saveModel(modelFromJson(record));
|
|
116
|
+
}
|
|
117
|
+
const defaultModel = stringValue(payload.default_model);
|
|
118
|
+
if (defaultModel && this.models.has(defaultModel))
|
|
119
|
+
this.defaultModelName = defaultModel;
|
|
120
|
+
const turns = asRecord(payload.turns);
|
|
121
|
+
if (turns) {
|
|
122
|
+
for (const [key, value] of Object.entries(turns)) {
|
|
123
|
+
if (Array.isArray(value)) {
|
|
124
|
+
this.turns.set(key, value.map((item) => asRecord(item)).filter((item) => Boolean(item)));
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
exportJson() {
|
|
130
|
+
return omitEmpty({
|
|
131
|
+
version: 1,
|
|
132
|
+
sync_payload: this.syncPayload,
|
|
133
|
+
private_context: this.privateContext,
|
|
134
|
+
apps: [...this.apps.values()].map((app) => ({ ...app })),
|
|
135
|
+
models: [...this.models.values()].map(redactModel),
|
|
136
|
+
default_model: this.defaultModelName,
|
|
137
|
+
turns: Object.fromEntries(this.turns.entries()),
|
|
138
|
+
});
|
|
139
|
+
}
|
|
55
140
|
getApp(slug) {
|
|
56
141
|
return this.apps.get(slug);
|
|
57
142
|
}
|
|
@@ -182,10 +267,11 @@ export class ModelManager {
|
|
|
182
267
|
});
|
|
183
268
|
if (response.ok) {
|
|
184
269
|
const payload = await response.json();
|
|
270
|
+
const modelId = selectModelId(payload, record.model);
|
|
185
271
|
return {
|
|
186
272
|
...record,
|
|
187
273
|
endpoint: base,
|
|
188
|
-
model:
|
|
274
|
+
model: modelId,
|
|
189
275
|
protocol: record.protocol ?? "chat_completions",
|
|
190
276
|
features: [...new Set([...(record.features ?? []), "text", "streaming"])],
|
|
191
277
|
status: "available",
|
|
@@ -347,12 +433,215 @@ export class RuntimeAttachment {
|
|
|
347
433
|
});
|
|
348
434
|
}
|
|
349
435
|
}
|
|
436
|
+
function runtimeMessageFromInput(input) {
|
|
437
|
+
if (typeof input === "string")
|
|
438
|
+
return { text: input, attachments: [] };
|
|
439
|
+
const messages = Array.isArray(input) ? input : [input];
|
|
440
|
+
const textParts = [];
|
|
441
|
+
const developerParts = [];
|
|
442
|
+
const attachments = [];
|
|
443
|
+
for (const message of messages) {
|
|
444
|
+
if (typeof message === "string") {
|
|
445
|
+
const text = message.trim();
|
|
446
|
+
if (text)
|
|
447
|
+
textParts.push(text);
|
|
448
|
+
continue;
|
|
449
|
+
}
|
|
450
|
+
if (!isObject(message))
|
|
451
|
+
continue;
|
|
452
|
+
const role = String(message.role ?? "user").trim().toLowerCase();
|
|
453
|
+
const content = message.content;
|
|
454
|
+
if (role === "developer") {
|
|
455
|
+
const text = contentText(content);
|
|
456
|
+
if (text)
|
|
457
|
+
developerParts.push(text);
|
|
458
|
+
continue;
|
|
459
|
+
}
|
|
460
|
+
if (typeof content === "string") {
|
|
461
|
+
const text = content.trim();
|
|
462
|
+
if (text)
|
|
463
|
+
textParts.push(text);
|
|
464
|
+
continue;
|
|
465
|
+
}
|
|
466
|
+
if (!Array.isArray(content))
|
|
467
|
+
continue;
|
|
468
|
+
content.forEach((block, index) => {
|
|
469
|
+
if (typeof block === "string") {
|
|
470
|
+
const text = block.trim();
|
|
471
|
+
if (text)
|
|
472
|
+
textParts.push(text);
|
|
473
|
+
return;
|
|
474
|
+
}
|
|
475
|
+
if (!isObject(block))
|
|
476
|
+
return;
|
|
477
|
+
const blockType = String(block.type ?? "").trim().toLowerCase();
|
|
478
|
+
if (blockType === "input_text" || blockType === "text") {
|
|
479
|
+
const text = cleanString(block.text);
|
|
480
|
+
if (text)
|
|
481
|
+
textParts.push(text);
|
|
482
|
+
}
|
|
483
|
+
else if (blockType === "input_image") {
|
|
484
|
+
const attachment = attachmentFromImageBlock(block, index + 1);
|
|
485
|
+
if (attachment)
|
|
486
|
+
attachments.push(attachment);
|
|
487
|
+
}
|
|
488
|
+
else if (blockType === "input_file") {
|
|
489
|
+
const attachment = attachmentFromFileBlock(block, index + 1);
|
|
490
|
+
if (attachment)
|
|
491
|
+
attachments.push(attachment);
|
|
492
|
+
}
|
|
493
|
+
});
|
|
494
|
+
}
|
|
495
|
+
const text = textParts.join("\n").trim();
|
|
496
|
+
return {
|
|
497
|
+
text: text || "Use the provided input.",
|
|
498
|
+
attachments,
|
|
499
|
+
developerInstructions: combineText(developerParts),
|
|
500
|
+
};
|
|
501
|
+
}
|
|
502
|
+
function contentText(content) {
|
|
503
|
+
if (typeof content === "string")
|
|
504
|
+
return content.trim();
|
|
505
|
+
if (!Array.isArray(content))
|
|
506
|
+
return "";
|
|
507
|
+
const parts = [];
|
|
508
|
+
for (const block of content) {
|
|
509
|
+
if (typeof block === "string") {
|
|
510
|
+
const text = block.trim();
|
|
511
|
+
if (text)
|
|
512
|
+
parts.push(text);
|
|
513
|
+
continue;
|
|
514
|
+
}
|
|
515
|
+
if (!isObject(block))
|
|
516
|
+
continue;
|
|
517
|
+
const blockType = String(block.type ?? "").trim().toLowerCase();
|
|
518
|
+
if (blockType === "input_text" || blockType === "text") {
|
|
519
|
+
const text = cleanString(block.text);
|
|
520
|
+
if (text)
|
|
521
|
+
parts.push(text);
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
return parts.join("\n").trim();
|
|
525
|
+
}
|
|
526
|
+
function combineText(parts) {
|
|
527
|
+
const cleaned = parts.map((part) => part?.trim()).filter((part) => Boolean(part));
|
|
528
|
+
return cleaned.length ? cleaned.join("\n\n") : undefined;
|
|
529
|
+
}
|
|
530
|
+
function attachmentFromImageBlock(block, index) {
|
|
531
|
+
const value = cleanString(block.image_url ?? block.url ?? block.file_data);
|
|
532
|
+
if (!value)
|
|
533
|
+
return undefined;
|
|
534
|
+
const data = dataUriParts(value);
|
|
535
|
+
const url = data.dataBase64 ? undefined : value;
|
|
536
|
+
const filename = cleanString(block.filename) ?? filenameFromUrl(url) ?? `image-${index}`;
|
|
537
|
+
return new RuntimeAttachment({
|
|
538
|
+
filename,
|
|
539
|
+
mimeType: data.mimeType ?? guessInputMimeType(filename, "image/*"),
|
|
540
|
+
kind: "image",
|
|
541
|
+
dataBase64: data.dataBase64,
|
|
542
|
+
url,
|
|
543
|
+
metadata: inputBlockMetadata(block),
|
|
544
|
+
});
|
|
545
|
+
}
|
|
546
|
+
function attachmentFromFileBlock(block, index) {
|
|
547
|
+
const value = cleanString(block.file_data ?? block.file_url ?? block.url);
|
|
548
|
+
const text = cleanString(block.text);
|
|
549
|
+
if (!value && !text)
|
|
550
|
+
return undefined;
|
|
551
|
+
const filename = cleanString(block.filename) ?? `file-${index}`;
|
|
552
|
+
const data = dataUriParts(value ?? "");
|
|
553
|
+
const url = data.dataBase64 ? undefined : value;
|
|
554
|
+
const mimeType = data.mimeType ?? cleanString(block.mime_type) ?? guessInputMimeType(filename);
|
|
555
|
+
return new RuntimeAttachment({
|
|
556
|
+
filename,
|
|
557
|
+
mimeType,
|
|
558
|
+
kind: inputKindForMimeType(mimeType),
|
|
559
|
+
dataBase64: data.dataBase64,
|
|
560
|
+
url,
|
|
561
|
+
text,
|
|
562
|
+
metadata: inputBlockMetadata(block),
|
|
563
|
+
});
|
|
564
|
+
}
|
|
565
|
+
function inputBlockMetadata(block) {
|
|
566
|
+
const detail = cleanString(block.detail);
|
|
567
|
+
return detail ? { detail } : {};
|
|
568
|
+
}
|
|
569
|
+
function dataUriParts(value) {
|
|
570
|
+
if (!value.startsWith("data:"))
|
|
571
|
+
return {};
|
|
572
|
+
const comma = value.indexOf(",");
|
|
573
|
+
if (comma < 0)
|
|
574
|
+
return {};
|
|
575
|
+
const header = value.slice(0, comma);
|
|
576
|
+
const payload = value.slice(comma + 1);
|
|
577
|
+
const mimeType = header.slice(5).split(";")[0] || undefined;
|
|
578
|
+
if (header.includes(";base64"))
|
|
579
|
+
return { mimeType, dataBase64: payload };
|
|
580
|
+
return { mimeType, dataBase64: utf8Base64(payload) };
|
|
581
|
+
}
|
|
582
|
+
function filenameFromUrl(url) {
|
|
583
|
+
if (!url)
|
|
584
|
+
return undefined;
|
|
585
|
+
try {
|
|
586
|
+
const segments = new URL(url).pathname.split("/").filter(Boolean);
|
|
587
|
+
return cleanString(segments.at(-1));
|
|
588
|
+
}
|
|
589
|
+
catch {
|
|
590
|
+
return undefined;
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
function guessInputMimeType(filename, defaultValue = "application/octet-stream") {
|
|
594
|
+
const lower = filename.toLowerCase();
|
|
595
|
+
if (lower.endsWith(".pdf"))
|
|
596
|
+
return "application/pdf";
|
|
597
|
+
if (lower.endsWith(".png"))
|
|
598
|
+
return "image/png";
|
|
599
|
+
if (lower.endsWith(".jpg") || lower.endsWith(".jpeg"))
|
|
600
|
+
return "image/jpeg";
|
|
601
|
+
if (lower.endsWith(".gif"))
|
|
602
|
+
return "image/gif";
|
|
603
|
+
if (lower.endsWith(".webp"))
|
|
604
|
+
return "image/webp";
|
|
605
|
+
if (lower.endsWith(".mp3"))
|
|
606
|
+
return "audio/mpeg";
|
|
607
|
+
if (lower.endsWith(".wav"))
|
|
608
|
+
return "audio/wav";
|
|
609
|
+
if (lower.endsWith(".mp4"))
|
|
610
|
+
return "video/mp4";
|
|
611
|
+
if (lower.endsWith(".txt"))
|
|
612
|
+
return "text/plain";
|
|
613
|
+
return defaultValue;
|
|
614
|
+
}
|
|
615
|
+
function inputKindForMimeType(mimeType) {
|
|
616
|
+
if (mimeType.startsWith("image/"))
|
|
617
|
+
return "image";
|
|
618
|
+
if (mimeType.startsWith("audio/"))
|
|
619
|
+
return "audio";
|
|
620
|
+
if (mimeType.startsWith("video/"))
|
|
621
|
+
return "video";
|
|
622
|
+
if (mimeType === "application/pdf" || mimeType.startsWith("text/"))
|
|
623
|
+
return "document";
|
|
624
|
+
return "file";
|
|
625
|
+
}
|
|
626
|
+
function utf8Base64(value) {
|
|
627
|
+
if (typeof btoa === "function") {
|
|
628
|
+
return btoa(unescape(encodeURIComponent(value)));
|
|
629
|
+
}
|
|
630
|
+
const buffer = globalThis.Buffer;
|
|
631
|
+
if (buffer)
|
|
632
|
+
return buffer.from(value, "utf-8").toString("base64");
|
|
633
|
+
return base64Encode(new TextEncoder().encode(value));
|
|
634
|
+
}
|
|
635
|
+
function isObject(value) {
|
|
636
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
637
|
+
}
|
|
350
638
|
export class RuntimeMessageRequest {
|
|
351
639
|
text;
|
|
352
640
|
interfaceName;
|
|
353
641
|
userId;
|
|
354
642
|
identityToken;
|
|
355
643
|
conversationId;
|
|
644
|
+
developerInstructions;
|
|
356
645
|
execute;
|
|
357
646
|
attachments;
|
|
358
647
|
clientCapabilities;
|
|
@@ -363,6 +652,7 @@ export class RuntimeMessageRequest {
|
|
|
363
652
|
this.userId = cleanString(options.userId ?? options.user_id);
|
|
364
653
|
this.identityToken = cleanString(options.identityToken ?? options.identity_token);
|
|
365
654
|
this.conversationId = cleanString(options.conversationId ?? options.conversation_id);
|
|
655
|
+
this.developerInstructions = cleanString(options.developerInstructions ?? options.developer_instructions);
|
|
366
656
|
this.execute = options.execute ?? true;
|
|
367
657
|
this.attachments = (options.attachments ?? []).map((attachment) => attachment instanceof RuntimeAttachment ? attachment : new RuntimeAttachment(attachment));
|
|
368
658
|
this.clientCapabilities = options.clientCapabilities ?? options.client_capabilities;
|
|
@@ -378,6 +668,7 @@ export class RuntimeMessageRequest {
|
|
|
378
668
|
identity_token: this.identityToken,
|
|
379
669
|
conversation_id: this.conversationId,
|
|
380
670
|
text: this.text,
|
|
671
|
+
developer_instructions: this.developerInstructions,
|
|
381
672
|
execute: this.execute,
|
|
382
673
|
attachments: this.attachments.map((attachment) => attachment.toJson()),
|
|
383
674
|
client_capabilities: normalizeCapabilities(this.clientCapabilities),
|
|
@@ -696,6 +987,7 @@ export class Marona extends RuntimeTransport {
|
|
|
696
987
|
models;
|
|
697
988
|
mode;
|
|
698
989
|
modelFetchImpl;
|
|
990
|
+
developerInstructions;
|
|
699
991
|
constructor(options = {}) {
|
|
700
992
|
const mode = options.mode ?? "online";
|
|
701
993
|
if (!["online", "offline", "hybrid"].includes(mode)) {
|
|
@@ -704,6 +996,7 @@ export class Marona extends RuntimeTransport {
|
|
|
704
996
|
super(options.baseUrl ?? "https://edge.marona.ai", options);
|
|
705
997
|
this.mode = mode;
|
|
706
998
|
this.cache = options.cache ?? new MaronaCache();
|
|
999
|
+
this.developerInstructions = cleanString(options.developerInstructions ?? options.developer_instructions);
|
|
707
1000
|
this.modelFetchImpl = options.fetchImpl ?? globalThis.fetch;
|
|
708
1001
|
this.models = new ModelManager(this.cache, this.modelFetchImpl);
|
|
709
1002
|
this.hub = new MaronaHubClient(options.hubUrl ?? "https://hub.marona.ai", {
|
|
@@ -728,26 +1021,31 @@ export class Marona extends RuntimeTransport {
|
|
|
728
1021
|
}
|
|
729
1022
|
async message(text, options = {}) {
|
|
730
1023
|
const model = this.models.default();
|
|
1024
|
+
const developerInstructions = combineText([
|
|
1025
|
+
this.developerInstructions,
|
|
1026
|
+
options.developerInstructions ?? options.developer_instructions,
|
|
1027
|
+
]);
|
|
1028
|
+
const messageOptions = { ...options, developerInstructions };
|
|
731
1029
|
if (this.mode === "online" && model?.location === "remote") {
|
|
732
|
-
const response = await this.localMessage(model, text,
|
|
733
|
-
this.cacheTurn(text, response,
|
|
1030
|
+
const response = await this.localMessage(model, text, messageOptions);
|
|
1031
|
+
this.cacheTurn(text, response, messageOptions);
|
|
734
1032
|
return response;
|
|
735
1033
|
}
|
|
736
1034
|
if (this.mode === "online") {
|
|
737
1035
|
const response = await super.message(text, {
|
|
738
|
-
...
|
|
1036
|
+
...messageOptions,
|
|
739
1037
|
metadata: { ...(options.metadata ?? {}), ...(options.tools ? { tools: options.tools } : {}) },
|
|
740
1038
|
});
|
|
741
|
-
this.cacheTurn(text, response,
|
|
1039
|
+
this.cacheTurn(text, response, messageOptions);
|
|
742
1040
|
return response;
|
|
743
1041
|
}
|
|
744
1042
|
if (model && (model.location === "local" || this.mode === "hybrid")) {
|
|
745
1043
|
try {
|
|
746
1044
|
const response = await this.localMessage(model, text, {
|
|
747
|
-
...
|
|
1045
|
+
...messageOptions,
|
|
748
1046
|
tools: options.tools?.length ? options.tools : cachedToolsForMode(this.cache, this.mode),
|
|
749
1047
|
});
|
|
750
|
-
this.cacheTurn(text, response,
|
|
1048
|
+
this.cacheTurn(text, response, messageOptions);
|
|
751
1049
|
return response;
|
|
752
1050
|
}
|
|
753
1051
|
catch (error) {
|
|
@@ -759,18 +1057,38 @@ export class Marona extends RuntimeTransport {
|
|
|
759
1057
|
throw new MaronaOfflineError("Offline mode requires a registered local model before calling message().");
|
|
760
1058
|
}
|
|
761
1059
|
const response = await super.message(text, {
|
|
762
|
-
...
|
|
1060
|
+
...messageOptions,
|
|
763
1061
|
metadata: { ...(options.metadata ?? {}), ...(options.tools ? { tools: options.tools } : {}) },
|
|
764
1062
|
});
|
|
765
|
-
this.cacheTurn(text, response,
|
|
1063
|
+
this.cacheTurn(text, response, messageOptions);
|
|
766
1064
|
return response;
|
|
767
1065
|
}
|
|
1066
|
+
async client(options) {
|
|
1067
|
+
const { input, attachments, conversationId: _conversationId, conversation_id: _conversation_id, ...messageOptions } = options;
|
|
1068
|
+
const normalized = runtimeMessageFromInput(input);
|
|
1069
|
+
return this.message(normalized.text, {
|
|
1070
|
+
...messageOptions,
|
|
1071
|
+
developerInstructions: combineText([
|
|
1072
|
+
normalized.developerInstructions,
|
|
1073
|
+
messageOptions.developerInstructions ?? messageOptions.developer_instructions,
|
|
1074
|
+
]),
|
|
1075
|
+
attachments: [...normalized.attachments, ...(attachments ?? [])],
|
|
1076
|
+
});
|
|
1077
|
+
}
|
|
768
1078
|
async localMessage(model, text, options) {
|
|
769
1079
|
const messages = [
|
|
770
1080
|
{
|
|
771
1081
|
role: "system",
|
|
772
1082
|
content: "You are running in Marona local mode. Use available MCP tools when they can answer or perform the task. Use recent local conversation context and synced private context when relevant. If a fact or action is not available in local context or an available tool, say that it is not available in this mode.",
|
|
773
1083
|
},
|
|
1084
|
+
...(options.developerInstructions || options.developer_instructions
|
|
1085
|
+
? [
|
|
1086
|
+
{
|
|
1087
|
+
role: "system",
|
|
1088
|
+
content: `Developer instructions for this application/request:\n${options.developerInstructions ?? options.developer_instructions}`,
|
|
1089
|
+
},
|
|
1090
|
+
]
|
|
1091
|
+
: []),
|
|
774
1092
|
...privateContextMessages(this.cache),
|
|
775
1093
|
...this.cache.recentContext({ conversationId: options.conversationId ?? options.conversation_id }),
|
|
776
1094
|
{ role: "user", content: text },
|
|
@@ -882,10 +1200,36 @@ function modelHeaders(model) {
|
|
|
882
1200
|
};
|
|
883
1201
|
}
|
|
884
1202
|
function firstModelId(payload) {
|
|
1203
|
+
return modelIds(payload)[0];
|
|
1204
|
+
}
|
|
1205
|
+
function selectModelId(payload, requested) {
|
|
1206
|
+
const ids = modelIds(payload);
|
|
1207
|
+
const cleanRequested = requested?.trim();
|
|
1208
|
+
if (cleanRequested && (!ids.length || ids.includes(cleanRequested)))
|
|
1209
|
+
return cleanRequested;
|
|
1210
|
+
return ids[0] ?? cleanRequested;
|
|
1211
|
+
}
|
|
1212
|
+
function modelIds(payload) {
|
|
885
1213
|
const record = asRecord(payload);
|
|
886
1214
|
const data = Array.isArray(record?.data) ? record?.data : [];
|
|
887
|
-
|
|
888
|
-
|
|
1215
|
+
return data
|
|
1216
|
+
.map((item) => stringValue(asRecord(item)?.id)?.trim())
|
|
1217
|
+
.filter((item) => Boolean(item))
|
|
1218
|
+
.sort((a, b) => Number(a.includes(",")) - Number(b.includes(",")) || a.localeCompare(b));
|
|
1219
|
+
}
|
|
1220
|
+
function modelFromJson(payload) {
|
|
1221
|
+
return {
|
|
1222
|
+
name: stringValue(payload.name) ?? "",
|
|
1223
|
+
endpoint: stringValue(payload.endpoint) ?? "",
|
|
1224
|
+
model: stringValue(payload.model),
|
|
1225
|
+
protocol: stringValue(payload.protocol),
|
|
1226
|
+
features: Array.isArray(payload.features) ? payload.features.map(String) : [],
|
|
1227
|
+
location: (stringValue(payload.location) ?? inferLocation(stringValue(payload.endpoint) ?? "")),
|
|
1228
|
+
status: (stringValue(payload.status) ?? "unknown"),
|
|
1229
|
+
apiKey: stringValue(payload.api_key) ?? stringValue(payload.apiKey),
|
|
1230
|
+
metadata: asRecord(payload.metadata) ?? {},
|
|
1231
|
+
lastCheckedAt: stringValue(payload.last_checked_at) ?? stringValue(payload.lastCheckedAt),
|
|
1232
|
+
};
|
|
889
1233
|
}
|
|
890
1234
|
function redactModel(model) {
|
|
891
1235
|
const { apiKey: _apiKey, ...safe } = model;
|
|
@@ -1166,8 +1510,106 @@ function privateContextMessages(cache) {
|
|
|
1166
1510
|
const privateContext = cache.loadPrivateContext();
|
|
1167
1511
|
if (!privateContext || Object.keys(privateContext).length === 0)
|
|
1168
1512
|
return [];
|
|
1169
|
-
|
|
1170
|
-
|
|
1513
|
+
return [{ role: "system", content: localContextPacketText(privateContext) }];
|
|
1514
|
+
}
|
|
1515
|
+
function localContextPacketText(privateContext) {
|
|
1516
|
+
const memoryContext = asRecord(privateContext.memory_context) ?? privateContext;
|
|
1517
|
+
const sections = [
|
|
1518
|
+
"Marona local context packet. This is privacy-safe synced context for the current user and current interface. Treat it as authoritative for profile, preferences, recent context, and cached MCP context. Answer naturally from it. Never reveal internal IDs, tokens, or raw JSON unless the user explicitly asks for exported developer diagnostics.",
|
|
1519
|
+
];
|
|
1520
|
+
const profile = asRecord(memoryContext.profile);
|
|
1521
|
+
if (profile) {
|
|
1522
|
+
const profileFields = asRecord(profile.profile) ?? asRecord(profile.details);
|
|
1523
|
+
const preferences = asRecord(profile.preferences) ?? asRecord(profile.prefs);
|
|
1524
|
+
if (profileFields && Object.keys(profileFields).length)
|
|
1525
|
+
sections.push(`Profile:\n${formatContextFields(profileFields)}`);
|
|
1526
|
+
if (preferences && Object.keys(preferences).length)
|
|
1527
|
+
sections.push(`Preferences:\n${formatContextFields(preferences)}`);
|
|
1528
|
+
}
|
|
1529
|
+
const memories = Array.isArray(memoryContext.memories) ? memoryContext.memories : [];
|
|
1530
|
+
if (memories.length)
|
|
1531
|
+
sections.push(`Relevant memories:\n${formatContextList(memories, 6)}`);
|
|
1532
|
+
const recentMessages = Array.isArray(memoryContext.recent_messages)
|
|
1533
|
+
? memoryContext.recent_messages
|
|
1534
|
+
: Array.isArray(memoryContext.recentMessages)
|
|
1535
|
+
? memoryContext.recentMessages
|
|
1536
|
+
: [];
|
|
1537
|
+
if (recentMessages.length)
|
|
1538
|
+
sections.push(`Recent messages:\n${formatContextList(recentMessages, 8)}`);
|
|
1539
|
+
const recentArtifacts = Array.isArray(memoryContext.recent_artifacts)
|
|
1540
|
+
? memoryContext.recent_artifacts
|
|
1541
|
+
: Array.isArray(memoryContext.recentArtifacts)
|
|
1542
|
+
? memoryContext.recentArtifacts
|
|
1543
|
+
: [];
|
|
1544
|
+
if (recentArtifacts.length)
|
|
1545
|
+
sections.push(`Recent artifacts:\n${formatContextList(recentArtifacts, 8)}`);
|
|
1546
|
+
const content = stringValue(memoryContext.content);
|
|
1547
|
+
if (content)
|
|
1548
|
+
sections.push(`Context content:\n${capText(content, 6000)}`);
|
|
1549
|
+
sections.push(`Standard context data:\n${capText(JSON.stringify(sanitizeContextValue(privateContext)), 12_000)}`);
|
|
1550
|
+
return sections.join("\n\n");
|
|
1551
|
+
}
|
|
1552
|
+
function formatContextFields(fields) {
|
|
1553
|
+
const lines = [];
|
|
1554
|
+
for (const [key, value] of Object.entries(fields)) {
|
|
1555
|
+
if (isSensitiveContextKey(key) || value == null || value === "")
|
|
1556
|
+
continue;
|
|
1557
|
+
const label = key
|
|
1558
|
+
.replaceAll("_", " ")
|
|
1559
|
+
.split(" ")
|
|
1560
|
+
.filter(Boolean)
|
|
1561
|
+
.map((part) => `${part[0]?.toUpperCase() ?? ""}${part.slice(1)}`)
|
|
1562
|
+
.join(" ");
|
|
1563
|
+
lines.push(`- ${label}: ${capText(contextValueText(value), 1000)}`);
|
|
1564
|
+
}
|
|
1565
|
+
return lines.length ? lines.join("\n") : "- None";
|
|
1566
|
+
}
|
|
1567
|
+
function formatContextList(items, limit) {
|
|
1568
|
+
const lines = [];
|
|
1569
|
+
for (const item of items.slice(0, limit)) {
|
|
1570
|
+
const record = asRecord(item);
|
|
1571
|
+
if (record) {
|
|
1572
|
+
const compact = Object.fromEntries(Object.entries(record).filter(([key, value]) => !isSensitiveContextKey(key) && value != null && value !== ""));
|
|
1573
|
+
if (Object.keys(compact).length)
|
|
1574
|
+
lines.push(`- ${capText(JSON.stringify(compact), 1000)}`);
|
|
1575
|
+
}
|
|
1576
|
+
else if (item != null) {
|
|
1577
|
+
lines.push(`- ${capText(String(item), 1000)}`);
|
|
1578
|
+
}
|
|
1579
|
+
}
|
|
1580
|
+
return lines.length ? lines.join("\n") : "- None";
|
|
1581
|
+
}
|
|
1582
|
+
function contextValueText(value) {
|
|
1583
|
+
if (typeof value === "string")
|
|
1584
|
+
return value;
|
|
1585
|
+
if (typeof value === "number" || typeof value === "boolean")
|
|
1586
|
+
return String(value);
|
|
1587
|
+
return JSON.stringify(value);
|
|
1588
|
+
}
|
|
1589
|
+
function capText(value, maxChars) {
|
|
1590
|
+
return value.length <= maxChars ? value : `${value.slice(0, maxChars)}\n...[truncated]`;
|
|
1591
|
+
}
|
|
1592
|
+
function isSensitiveContextKey(key) {
|
|
1593
|
+
return new Set([
|
|
1594
|
+
"access_token",
|
|
1595
|
+
"refresh_token",
|
|
1596
|
+
"identity_token",
|
|
1597
|
+
"token",
|
|
1598
|
+
"user_id",
|
|
1599
|
+
"session_id",
|
|
1600
|
+
"runtime_session_id",
|
|
1601
|
+
"x_marona_user_id",
|
|
1602
|
+
]).has(key.toLowerCase().replaceAll("-", "_"));
|
|
1603
|
+
}
|
|
1604
|
+
function sanitizeContextValue(value) {
|
|
1605
|
+
if (Array.isArray(value))
|
|
1606
|
+
return value.map(sanitizeContextValue);
|
|
1607
|
+
const record = asRecord(value);
|
|
1608
|
+
if (!record)
|
|
1609
|
+
return value;
|
|
1610
|
+
return Object.fromEntries(Object.entries(record)
|
|
1611
|
+
.filter(([key]) => !isSensitiveContextKey(key))
|
|
1612
|
+
.map(([key, item]) => [key, sanitizeContextValue(item)]));
|
|
1171
1613
|
}
|
|
1172
1614
|
function cachedToolsForMode(cache, mode) {
|
|
1173
1615
|
const payload = cache.loadSyncPayload() ?? {};
|
|
@@ -1246,7 +1688,11 @@ function executionModes(app) {
|
|
|
1246
1688
|
modes.push(value);
|
|
1247
1689
|
}
|
|
1248
1690
|
}
|
|
1249
|
-
|
|
1691
|
+
if (!modes.length)
|
|
1692
|
+
return ["online"];
|
|
1693
|
+
if (modes.includes("hybrid") || (modes.includes("online") && modes.includes("offline")))
|
|
1694
|
+
return ["hybrid"];
|
|
1695
|
+
return [modes[0]];
|
|
1250
1696
|
}
|
|
1251
1697
|
function appSupportsMode(app, mode) {
|
|
1252
1698
|
const modes = new Set(executionModes(app));
|