Separate About-the-user memory (global + per-persona) from craft RAG, add tabbed settings with persona export/import and craft clear APIs. Co-authored-by: Cursor <cursoragent@cursor.com>
82 lines
3.0 KiB
JavaScript
82 lines
3.0 KiB
JavaScript
/**
|
|
* Swarm Assistent — patch detection / extraction / alias normalization.
|
|
* Loaded before assistent.js; mirrors AssistentPatch.cs on the server side.
|
|
*/
|
|
window.SA = window.SA || {};
|
|
|
|
(function () {
|
|
const PATCH_KEYS = [
|
|
'prompt', 'loras', 'width', 'height', 'steps', 'cfg', 'seed', 'sigma_shift', 'sampler',
|
|
'actions', 'search_query', 'civitai_query',
|
|
'use_init_image', 'clear_init_image', 'init_creativity', 'denoise',
|
|
'use_mask_image', 'clear_mask_image', 'mask_blur', 'mask_grow',
|
|
'look_at', 'vision_from', 'vision_slots', 'slot_to_init', 'slot_to_mask',
|
|
'snapshot_generate', 'select_slot', 'aspect', 'images', 'batch', 'vary', 'lock_seed',
|
|
'creativity', 'intensity', 'complexity', 'movement',
|
|
'clear_prompt_images', 'slot_to_prompt_image', 'pack',
|
|
'memories', 'memory_query', 'memory_kind', 'tag_query', 'user_prefs',
|
|
'controls', 'persona_clone', 'persona_shelves', 'persona', 'notes',
|
|
];
|
|
|
|
const FENCE_RE = /```(?:json)?\s*([\s\S]*?)```/gi;
|
|
|
|
function has(obj, key) {
|
|
return obj[key] !== undefined && obj[key] !== null;
|
|
}
|
|
|
|
/** True when the object looks like a generation patch rather than arbitrary JSON. */
|
|
function isPatchObject(obj) {
|
|
if (!obj || typeof obj !== 'object') {
|
|
return false;
|
|
}
|
|
return PATCH_KEYS.some((k) => has(obj, k));
|
|
}
|
|
|
|
/** Maps alias fields onto canonical names, keeping the aliases in place. */
|
|
function normalizePatch(patch) {
|
|
if (!patch || typeof patch !== 'object') {
|
|
return patch;
|
|
}
|
|
if (!has(patch, 'search_query') && has(patch, 'civitai_query')) {
|
|
patch.search_query = patch.civitai_query;
|
|
}
|
|
if (!has(patch, 'init_creativity') && has(patch, 'denoise')) {
|
|
patch.init_creativity = patch.denoise;
|
|
}
|
|
if (!has(patch, 'look_at')) {
|
|
if (has(patch, 'vision_from')) {
|
|
patch.look_at = patch.vision_from;
|
|
} else if (has(patch, 'vision_slots')) {
|
|
patch.look_at = patch.vision_slots;
|
|
}
|
|
}
|
|
return patch;
|
|
}
|
|
|
|
/** Splits a reply into prose and the last fenced patch object found in it. */
|
|
function extractPatch(text) {
|
|
if (!text) {
|
|
return { prose: text || '', patch: null };
|
|
}
|
|
const re = new RegExp(FENCE_RE.source, 'gi');
|
|
let match;
|
|
let lastPatch = null;
|
|
let prose = text;
|
|
while ((match = re.exec(text)) !== null) {
|
|
try {
|
|
const obj = JSON.parse(match[1].trim());
|
|
if (isPatchObject(obj)) {
|
|
lastPatch = normalizePatch(obj);
|
|
prose = (text.slice(0, match.index) + text.slice(match.index + match[0].length)).trim();
|
|
}
|
|
} catch (e) { /* not json */ }
|
|
}
|
|
return { prose, patch: lastPatch };
|
|
}
|
|
|
|
SA.PATCH_KEYS = PATCH_KEYS;
|
|
SA.isPatchObject = isPatchObject;
|
|
SA.normalizePatch = normalizePatch;
|
|
SA.extractPatch = extractPatch;
|
|
})();
|