marona 0.2.7 → 0.2.18
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 +44 -0
- package/README.md +133 -54
- package/dist/index.d.ts +31 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +435 -18
- package/dist/index.js.map +1 -1
- package/package.json +2 -10
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
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
4
|
export const standardExecutionModes = ["online", "hybrid", "offline"];
|
|
@@ -13,13 +13,25 @@ export function normalizeExecutionModes(value) {
|
|
|
13
13
|
}
|
|
14
14
|
if (!modes.length)
|
|
15
15
|
return ["online"];
|
|
16
|
-
|
|
16
|
+
if (modes.includes("hybrid") || (modes.includes("online") && modes.includes("offline"))) {
|
|
17
|
+
return ["hybrid"];
|
|
18
|
+
}
|
|
19
|
+
return [modes[0]];
|
|
17
20
|
}
|
|
18
21
|
export function supportsExecutionMode(value, mode) {
|
|
19
|
-
|
|
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);
|
|
20
29
|
}
|
|
21
30
|
export function executionModeLabel(value) {
|
|
22
|
-
|
|
31
|
+
const modes = normalizeExecutionModes(value);
|
|
32
|
+
if (modes.includes("hybrid"))
|
|
33
|
+
return "Hybrid";
|
|
34
|
+
return modes.map(executionModeTitle).join(" + ");
|
|
23
35
|
}
|
|
24
36
|
function executionModeTitle(mode) {
|
|
25
37
|
if (mode === "online")
|
|
@@ -81,6 +93,50 @@ export class MaronaCache {
|
|
|
81
93
|
this.apps.set(slug, record);
|
|
82
94
|
}
|
|
83
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
|
+
}
|
|
84
140
|
getApp(slug) {
|
|
85
141
|
return this.apps.get(slug);
|
|
86
142
|
}
|
|
@@ -211,10 +267,11 @@ export class ModelManager {
|
|
|
211
267
|
});
|
|
212
268
|
if (response.ok) {
|
|
213
269
|
const payload = await response.json();
|
|
270
|
+
const modelId = selectModelId(payload, record.model);
|
|
214
271
|
return {
|
|
215
272
|
...record,
|
|
216
273
|
endpoint: base,
|
|
217
|
-
model:
|
|
274
|
+
model: modelId,
|
|
218
275
|
protocol: record.protocol ?? "chat_completions",
|
|
219
276
|
features: [...new Set([...(record.features ?? []), "text", "streaming"])],
|
|
220
277
|
status: "available",
|
|
@@ -376,12 +433,215 @@ export class RuntimeAttachment {
|
|
|
376
433
|
});
|
|
377
434
|
}
|
|
378
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
|
+
}
|
|
379
638
|
export class RuntimeMessageRequest {
|
|
380
639
|
text;
|
|
381
640
|
interfaceName;
|
|
382
641
|
userId;
|
|
383
642
|
identityToken;
|
|
384
643
|
conversationId;
|
|
644
|
+
developerInstructions;
|
|
385
645
|
execute;
|
|
386
646
|
attachments;
|
|
387
647
|
clientCapabilities;
|
|
@@ -392,6 +652,7 @@ export class RuntimeMessageRequest {
|
|
|
392
652
|
this.userId = cleanString(options.userId ?? options.user_id);
|
|
393
653
|
this.identityToken = cleanString(options.identityToken ?? options.identity_token);
|
|
394
654
|
this.conversationId = cleanString(options.conversationId ?? options.conversation_id);
|
|
655
|
+
this.developerInstructions = cleanString(options.developerInstructions ?? options.developer_instructions);
|
|
395
656
|
this.execute = options.execute ?? true;
|
|
396
657
|
this.attachments = (options.attachments ?? []).map((attachment) => attachment instanceof RuntimeAttachment ? attachment : new RuntimeAttachment(attachment));
|
|
397
658
|
this.clientCapabilities = options.clientCapabilities ?? options.client_capabilities;
|
|
@@ -407,6 +668,7 @@ export class RuntimeMessageRequest {
|
|
|
407
668
|
identity_token: this.identityToken,
|
|
408
669
|
conversation_id: this.conversationId,
|
|
409
670
|
text: this.text,
|
|
671
|
+
developer_instructions: this.developerInstructions,
|
|
410
672
|
execute: this.execute,
|
|
411
673
|
attachments: this.attachments.map((attachment) => attachment.toJson()),
|
|
412
674
|
client_capabilities: normalizeCapabilities(this.clientCapabilities),
|
|
@@ -725,6 +987,7 @@ export class Marona extends RuntimeTransport {
|
|
|
725
987
|
models;
|
|
726
988
|
mode;
|
|
727
989
|
modelFetchImpl;
|
|
990
|
+
developerInstructions;
|
|
728
991
|
constructor(options = {}) {
|
|
729
992
|
const mode = options.mode ?? "online";
|
|
730
993
|
if (!["online", "offline", "hybrid"].includes(mode)) {
|
|
@@ -733,6 +996,7 @@ export class Marona extends RuntimeTransport {
|
|
|
733
996
|
super(options.baseUrl ?? "https://edge.marona.ai", options);
|
|
734
997
|
this.mode = mode;
|
|
735
998
|
this.cache = options.cache ?? new MaronaCache();
|
|
999
|
+
this.developerInstructions = cleanString(options.developerInstructions ?? options.developer_instructions);
|
|
736
1000
|
this.modelFetchImpl = options.fetchImpl ?? globalThis.fetch;
|
|
737
1001
|
this.models = new ModelManager(this.cache, this.modelFetchImpl);
|
|
738
1002
|
this.hub = new MaronaHubClient(options.hubUrl ?? "https://hub.marona.ai", {
|
|
@@ -757,26 +1021,31 @@ export class Marona extends RuntimeTransport {
|
|
|
757
1021
|
}
|
|
758
1022
|
async message(text, options = {}) {
|
|
759
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 };
|
|
760
1029
|
if (this.mode === "online" && model?.location === "remote") {
|
|
761
|
-
const response = await this.localMessage(model, text,
|
|
762
|
-
this.cacheTurn(text, response,
|
|
1030
|
+
const response = await this.localMessage(model, text, messageOptions);
|
|
1031
|
+
this.cacheTurn(text, response, messageOptions);
|
|
763
1032
|
return response;
|
|
764
1033
|
}
|
|
765
1034
|
if (this.mode === "online") {
|
|
766
1035
|
const response = await super.message(text, {
|
|
767
|
-
...
|
|
1036
|
+
...messageOptions,
|
|
768
1037
|
metadata: { ...(options.metadata ?? {}), ...(options.tools ? { tools: options.tools } : {}) },
|
|
769
1038
|
});
|
|
770
|
-
this.cacheTurn(text, response,
|
|
1039
|
+
this.cacheTurn(text, response, messageOptions);
|
|
771
1040
|
return response;
|
|
772
1041
|
}
|
|
773
1042
|
if (model && (model.location === "local" || this.mode === "hybrid")) {
|
|
774
1043
|
try {
|
|
775
1044
|
const response = await this.localMessage(model, text, {
|
|
776
|
-
...
|
|
1045
|
+
...messageOptions,
|
|
777
1046
|
tools: options.tools?.length ? options.tools : cachedToolsForMode(this.cache, this.mode),
|
|
778
1047
|
});
|
|
779
|
-
this.cacheTurn(text, response,
|
|
1048
|
+
this.cacheTurn(text, response, messageOptions);
|
|
780
1049
|
return response;
|
|
781
1050
|
}
|
|
782
1051
|
catch (error) {
|
|
@@ -788,18 +1057,38 @@ export class Marona extends RuntimeTransport {
|
|
|
788
1057
|
throw new MaronaOfflineError("Offline mode requires a registered local model before calling message().");
|
|
789
1058
|
}
|
|
790
1059
|
const response = await super.message(text, {
|
|
791
|
-
...
|
|
1060
|
+
...messageOptions,
|
|
792
1061
|
metadata: { ...(options.metadata ?? {}), ...(options.tools ? { tools: options.tools } : {}) },
|
|
793
1062
|
});
|
|
794
|
-
this.cacheTurn(text, response,
|
|
1063
|
+
this.cacheTurn(text, response, messageOptions);
|
|
795
1064
|
return response;
|
|
796
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
|
+
}
|
|
797
1078
|
async localMessage(model, text, options) {
|
|
798
1079
|
const messages = [
|
|
799
1080
|
{
|
|
800
1081
|
role: "system",
|
|
801
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.",
|
|
802
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
|
+
: []),
|
|
803
1092
|
...privateContextMessages(this.cache),
|
|
804
1093
|
...this.cache.recentContext({ conversationId: options.conversationId ?? options.conversation_id }),
|
|
805
1094
|
{ role: "user", content: text },
|
|
@@ -911,10 +1200,36 @@ function modelHeaders(model) {
|
|
|
911
1200
|
};
|
|
912
1201
|
}
|
|
913
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) {
|
|
914
1213
|
const record = asRecord(payload);
|
|
915
1214
|
const data = Array.isArray(record?.data) ? record?.data : [];
|
|
916
|
-
|
|
917
|
-
|
|
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
|
+
};
|
|
918
1233
|
}
|
|
919
1234
|
function redactModel(model) {
|
|
920
1235
|
const { apiKey: _apiKey, ...safe } = model;
|
|
@@ -1195,8 +1510,106 @@ function privateContextMessages(cache) {
|
|
|
1195
1510
|
const privateContext = cache.loadPrivateContext();
|
|
1196
1511
|
if (!privateContext || Object.keys(privateContext).length === 0)
|
|
1197
1512
|
return [];
|
|
1198
|
-
|
|
1199
|
-
|
|
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)]));
|
|
1200
1613
|
}
|
|
1201
1614
|
function cachedToolsForMode(cache, mode) {
|
|
1202
1615
|
const payload = cache.loadSyncPayload() ?? {};
|
|
@@ -1275,7 +1688,11 @@ function executionModes(app) {
|
|
|
1275
1688
|
modes.push(value);
|
|
1276
1689
|
}
|
|
1277
1690
|
}
|
|
1278
|
-
|
|
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]];
|
|
1279
1696
|
}
|
|
1280
1697
|
function appSupportsMode(app, mode) {
|
|
1281
1698
|
const modes = new Set(executionModes(app));
|