(function (global) {
"use strict";
var DEFAULT_FILES = [
"seed/agent-info.lino",
"seed/multilingual-responses.lino",
"seed/concepts.lino",
"seed/concept-contexts.lino",
"seed/facts.lino",
"seed/brainstorm-seeds.lino",
"seed/personas.lino",
"seed/summary-topics.lino",
"seed/coreference.lino",
"seed/tools.lino",
"seed/language-detection.lino",
"seed/prompt-patterns.lino",
"seed/intent-routing.lino",
"seed/greetings.lino",
"seed/identity.lino",
"seed/hello-world-programs.lino",
"seed/demo-dialogs.lino",
"seed/environments.lino",
];
function isWorker() {
return (
typeof global.WorkerGlobalScope !== "undefined" &&
global instanceof global.WorkerGlobalScope
);
}
function assetVersion() {
if (typeof global.FORMAL_AI_ASSET_VERSION === "string") {
return global.FORMAL_AI_ASSET_VERSION;
}
try {
var search = global.location && global.location.search;
var match = search && /[?&]v=([^&]+)/.exec(search);
return match ? decodeURIComponent(match[1].replace(/\+/g, " ")) : "";
} catch (_error) {
return "";
}
}
function withAssetVersion(url) {
var version = assetVersion();
if (!version) return url;
if (
url.indexOf("://") !== -1 ||
url.indexOf("//") === 0 ||
url.indexOf("data:") === 0
) {
return url;
}
return (
url +
(url.indexOf("?") === -1 ? "?" : "&") +
"v=" +
encodeURIComponent(version)
);
}
function unescapeValue(value) {
return String(value || "")
.replace(/\\n/g, "\n")
.replace(/\\"/g, '"')
.replace(/\\\\/g, "\\");
}
function parseLino(text) {
var lines = String(text || "").split(/\r?\n/);
var root = { name: "", id: "", value: "", children: [], indent: -1 };
var stack = [root];
for (var i = 0; i < lines.length; i += 1) {
var line = lines[i];
if (!line || /^\s*$/.test(line)) continue;
var indentMatch = /^(\s*)/.exec(line);
var indent = indentMatch ? indentMatch[1].length : 0;
var content = line.slice(indent);
while (stack.length > 1 && stack[stack.length - 1].indent >= indent) {
stack.pop();
}
var parent = stack[stack.length - 1];
var node = parseLinoLine(content, indent);
parent.children.push(node);
stack.push(node);
}
return root.children.length === 1 ? root.children[0] : root;
}
function parseLinoLine(content, indent) {
var node = {
name: "",
id: "",
value: "",
children: [],
indent: indent,
};
var first = content.indexOf(' "');
if (first === -1) {
node.name = content.trim();
return node;
}
node.name = content.slice(0, first).trim();
var rest = content.slice(first + 1).trim();
if (rest.length === 0 || rest[0] !== '"') return node;
var closing = -1;
for (var i = 1; i < rest.length; i += 1) {
if (rest[i] === "\\") {
i += 1;
continue;
}
if (rest[i] === '"') {
closing = i;
break;
}
}
if (closing === -1) return node;
node.id = unescapeValue(rest.slice(1, closing));
node.value = node.id;
return node;
}
function findChildren(node, name) {
if (!node || !Array.isArray(node.children)) return [];
return node.children.filter(function (child) {
return child.name === name;
});
}
function findChildValue(node, name) {
var match = findChildren(node, name)[0];
return match ? match.id : "";
}
function extractMultilingualResponses(node) {
var responses = {};
if (!node) return responses;
var entries = findChildren(node, "response");
for (var i = 0; i < entries.length; i += 1) {
var entry = entries[i];
var intent = findChildValue(entry, "intent");
var language = findChildValue(entry, "language");
var text = findChildValue(entry, "text");
if (!intent || !language || !text) continue;
var variantNodes = findChildren(entry, "variant");
var variants = variantNodes
.map(function (variant) {
return variant.id || "";
})
.filter(function (value) {
return value && value.length > 0;
});
if (!responses[intent]) responses[intent] = {};
responses[intent][language] = {
text: text,
variants: variants.length > 0 ? variants : [text],
};
}
return responses;
}
function extractConcepts(root) {
if (!root || !Array.isArray(root.children)) return [];
var concepts = [];
var iterate = function (item) {
if (!item || !item.name) return;
if (!item.name.startsWith("concept_")) return;
var aliases = findChildValue(item, "aliases");
var contexts = findChildValue(item, "contexts");
var contextLinks = findChildValue(item, "context_links");
var localized = findChildren(item, "localized").map(function (loc) {
var locAliases = findChildValue(loc, "aliases");
return {
language: loc.id,
term: findChildValue(loc, "term"),
aliases: locAliases ? locAliases.split("|").map(trim).filter(Boolean) : [],
summary: findChildValue(loc, "summary"),
source: findChildValue(loc, "source"),
sourceKind: findChildValue(loc, "source_kind"),
};
});
concepts.push({
slug: item.name,
term: findChildValue(item, "term"),
category: findChildValue(item, "category") || "concept",
summary: findChildValue(item, "summary"),
source: findChildValue(item, "source"),
sourceKind: findChildValue(item, "source_kind") || "project-docs",
wikidata: findChildValue(item, "wikidata"),
aliases: aliases ? aliases.split("|").map(trim).filter(Boolean) : [],
contexts: contexts ? contexts.split("|").map(trim).filter(Boolean) : [],
contextLinks: contextLinks
? contextLinks.split("|").map(trim).filter(Boolean)
: [],
localized: localized,
});
};
if (root.name && root.name.startsWith("concept_")) {
iterate(root);
} else if (Array.isArray(root.children)) {
root.children.forEach(iterate);
}
return concepts;
}
function extractConceptContexts(root) {
if (!root || !Array.isArray(root.children)) return [];
var out = [];
var visit = function (parent) {
var entries = findChildren(parent, "context");
for (var i = 0; i < entries.length; i += 1) {
var entry = entries[i];
if (!entry.id) continue;
var aliases = findChildValue(entry, "aliases");
var labels = findChildren(entry, "label").map(function (label) {
return { language: label.id, text: findChildValue(label, "text") };
});
out.push({
slug: entry.id,
wikidata: findChildValue(entry, "wikidata"),
aliases: aliases ? aliases.split("|").map(trim).filter(Boolean) : [],
labels: labels,
});
}
};
visit(root);
for (var i = 0; i < root.children.length; i += 1) {
visit(root.children[i]);
}
return out;
}
function extractFacts(root) {
if (!root || !Array.isArray(root.children)) return [];
var facts = [];
var visit = function (item) {
if (!item || !item.name || item.name.indexOf("fact_") !== 0) return;
var localized = findChildren(item, "localized").map(function (loc) {
return {
language: loc.id,
subjectLabel: findChildValue(loc, "subject_label"),
valueLabel: findChildValue(loc, "value_label"),
summary: findChildValue(loc, "summary"),
source: findChildValue(loc, "source"),
sourceKind: findChildValue(loc, "source_kind"),
};
});
facts.push({
slug: item.name,
intent: findChildValue(item, "intent") || "fact_lookup",
category: findChildValue(item, "category"),
wikidata: splitList(findChildValue(item, "wikidata")),
relation: findChildValue(item, "relation"),
subjectQid: findChildValue(item, "subject_qid"),
valueQid: findChildValue(item, "value_qid"),
subjectLabel: findChildValue(item, "subject_label"),
valueLabel: findChildValue(item, "value_label"),
subjectAliases: splitList(findChildValue(item, "subject_aliases")).map(toLower),
questionKeywords: splitList(findChildValue(item, "question_keywords")).map(toLower),
summary: findChildValue(item, "summary"),
source: findChildValue(item, "source"),
sourceKind: findChildValue(item, "source_kind"),
localized: localized,
});
};
if (root.name && root.name.indexOf("fact_") === 0) {
visit(root);
} else {
root.children.forEach(visit);
}
return facts;
}
function extractBrainstormSeeds(root) {
var seeds = { triggers: [], categories: [] };
if (!root || !Array.isArray(root.children)) return seeds;
var section = root.name === "brainstorm_seeds" ? root : findChildren(root, "brainstorm_seeds")[0];
if (!section) return seeds;
seeds.triggers = splitList(findChildValue(section, "trigger")).map(toLower);
var categories = findChildren(section, "category");
for (var i = 0; i < categories.length; i += 1) {
var category = categories[i];
var items = findChildren(category, "item")
.map(function (item) {
return item.id || "";
})
.filter(Boolean);
if (!items.length) continue;
seeds.categories.push({
slug: category.id,
intent: findChildValue(category, "intent"),
detectionKeywords: splitList(findChildValue(category, "detection_keywords")).map(toLower),
items: items,
});
}
return seeds;
}
function extractPersonas(root) {
var seeds = {
triggers: [],
defaultPersona: "",
bodyTemplate: "",
fallbackBody: "",
personas: [],
topics: [],
};
if (!root || !Array.isArray(root.children)) return seeds;
var section = root.name === "personas" ? root : findChildren(root, "personas")[0];
if (!section) return seeds;
seeds.triggers = splitList(findChildValue(section, "trigger")).map(toLower);
seeds.defaultPersona = findChildValue(section, "default_persona");
seeds.bodyTemplate = findChildValue(section, "body_template");
seeds.fallbackBody = findChildValue(section, "fallback_body");
var personaEntries = findChildren(section, "persona");
for (var i = 0; i < personaEntries.length; i += 1) {
var persona = personaEntries[i];
seeds.personas.push({
displayName: persona.id,
aliases: splitList(findChildValue(persona, "aliases")).map(toLower),
wikidata: findChildValue(persona, "wikidata"),
});
}
var topicEntries = findChildren(section, "topic");
for (var j = 0; j < topicEntries.length; j += 1) {
var topic = topicEntries[j];
seeds.topics.push({
slug: topic.id,
detectionKeywords: splitList(findChildValue(topic, "detection_keywords")).map(toLower),
body: findChildValue(topic, "body"),
});
}
return seeds;
}
function extractTools(node) {
var tools = [];
if (!node) return tools;
var entries = findChildren(node, "tool");
for (var i = 0; i < entries.length; i += 1) {
var entry = entries[i];
var localized = findChildren(entry, "localized").map(function (loc) {
return {
language: loc.id,
name: findChildValue(loc, "name"),
description: findChildValue(loc, "description"),
};
});
tools.push({
id: entry.id,
name: findChildValue(entry, "name"),
description: findChildValue(entry, "description"),
mode: findChildValue(entry, "mode") || "thinking",
inputs: splitList(findChildValue(entry, "inputs")),
outputs: splitList(findChildValue(entry, "outputs")),
isolation: findChildValue(entry, "isolation"),
sources: splitList(findChildValue(entry, "sources")),
localized: localized,
});
}
return tools;
}
function extractAgentInfo(node) {
var info = {};
if (!node) return info;
var entries = findChildren(node, "field");
for (var i = 0; i < entries.length; i += 1) {
var entry = entries[i];
var key = entry.id;
var value = findChildValue(entry, "value");
if (key) info[key] = value;
}
return info;
}
function extractLanguageRules(node) {
var rules = [];
if (!node) return rules;
var entries = findChildren(node, "rule");
for (var i = 0; i < entries.length; i += 1) {
var entry = entries[i];
rules.push({
id: entry.id,
language: findChildValue(entry, "language"),
label: findChildValue(entry, "label"),
start: parseCodepoint(findChildValue(entry, "start")),
end: parseCodepoint(findChildValue(entry, "end")),
note: findChildValue(entry, "note"),
});
}
return rules;
}
function parseCodepoint(value) {
if (!value) return 0;
var str = String(value).trim();
if (str.indexOf("0x") === 0 || str.indexOf("0X") === 0) {
return parseInt(str.slice(2), 16) || 0;
}
return parseInt(str, 10) || 0;
}
function extractEnvironmentDirectory(root) {
var directory = {
environments: [],
migrationDescription: "",
flows: [],
};
if (!root || !Array.isArray(root.children)) return directory;
for (var i = 0; i < root.children.length; i += 1) {
var section = root.children[i];
if (!section || !section.name) continue;
if (section.name === "environments") {
var envEntries = findChildren(section, "environment");
for (var j = 0; j < envEntries.length; j += 1) {
var entry = envEntries[j];
directory.environments.push({
id: entry.id,
label: findChildValue(entry, "label"),
runtime: findChildValue(entry, "runtime"),
seedPath: findChildValue(entry, "seed_path"),
memoryStore: findChildValue(entry, "memory_store"),
memoryExport: findChildValue(entry, "memory_export_command"),
bundleExport: findChildValue(entry, "bundle_export_command"),
bundleImport: findChildValue(entry, "bundle_import_command"),
tools: splitList(findChildValue(entry, "tools")),
});
}
} else if (section.name === "migration") {
directory.migrationDescription = findChildValue(section, "description");
var flowEntries = findChildren(section, "flow");
for (var k = 0; k < flowEntries.length; k += 1) {
var flow = flowEntries[k];
directory.flows.push({
id: flow.id,
description: findChildValue(flow, "description"),
fileFormat: findChildValue(flow, "file_format"),
});
}
}
}
return directory;
}
function extractPromptPatterns(node) {
var patterns = [];
if (!node) return patterns;
var entries = findChildren(node, "pattern");
for (var i = 0; i < entries.length; i += 1) {
var entry = entries[i];
patterns.push({
id: entry.id,
intent: findChildValue(entry, "intent"),
language: findChildValue(entry, "language") || "en",
kind: findChildValue(entry, "kind"),
text: findChildValue(entry, "text"),
});
}
return patterns;
}
function extractIntentRouting(node) {
var routing = {
intents: [],
articlePrefixes: [],
tracePrefixes: [],
};
if (!node || !Array.isArray(node.children)) return routing;
for (var i = 0; i < node.children.length; i += 1) {
var child = node.children[i];
if (!child || !child.name) continue;
if (child.name === "intent") {
var route = {
id: child.id,
slug: findChildValue(child, "slug"),
responseLink: findChildValue(child, "response_link"),
keywords: [],
phrases: [],
tokens: [],
combos: [],
};
for (var j = 0; j < child.children.length; j += 1) {
var entry = child.children[j];
if (entry.name === "keyword") route.keywords.push(entry.id);
else if (entry.name === "phrase") route.phrases.push(entry.id);
else if (entry.name === "token") route.tokens.push(entry.id);
else if (entry.name === "combo") {
route.combos.push(
String(entry.id || "")
.split("+")
.map(trim)
.filter(Boolean),
);
}
}
routing.intents.push(route);
} else if (child.name === "article") {
routing.articlePrefixes.push(child.id);
} else if (child.name === "trace_prefix") {
routing.tracePrefixes.push(child.id);
}
}
return routing;
}
function splitList(value) {
if (!value) return [];
return String(value)
.split("|")
.map(trim)
.filter(Boolean);
}
function trim(value) {
return String(value || "").trim();
}
function toLower(value) {
return trim(value).toLowerCase();
}
function fetchText(url) {
if (typeof global.fetch !== "function") {
return Promise.resolve("");
}
return global.fetch(url).then(function (response) {
if (!response || !response.ok) return "";
return response.text();
}, function () {
return "";
});
}
function loadAll(files) {
var target = Array.isArray(files) && files.length ? files : DEFAULT_FILES;
return Promise.all(
target.map(function (file) {
return fetchText(withAssetVersion(file)).then(function (text) {
return { file: file, text: text };
});
}),
).then(buildSeed);
}
function mergeResponses(a, b) {
var out = {};
var keys = Object.keys(a).concat(Object.keys(b || {}));
for (var i = 0; i < keys.length; i += 1) {
var key = keys[i];
out[key] = Object.assign({}, a[key] || {}, (b || {})[key] || {});
}
return out;
}
function parseBundle(text) {
var lines = String(text || "").split(/\r?\n/);
var sections = [];
var current = null;
for (var i = 0; i < lines.length; i += 1) {
var line = lines[i];
if (line === "") {
if (current) current.text += "\n";
continue;
}
var indentMatch = /^(\s*)/.exec(line);
var indent = indentMatch ? indentMatch[1].length : 0;
var trimmed = line.slice(indent);
if (indent === 0) {
if (current) sections.push(current);
current = null;
continue;
}
if (indent === 2 && trimmed.indexOf("file ") === 0) {
if (current) sections.push(current);
current = null;
var rest = trimmed.slice("file ".length).trim();
if (rest[0] === '"') {
var inner = rest.slice(1);
for (var j = 0; j < inner.length; j += 1) {
if (inner[j] === "\\") {
j += 1;
continue;
}
if (inner[j] === '"') {
current = { file: unescapeValue(inner.slice(0, j)), text: "" };
break;
}
}
}
continue;
}
if (current) {
var body = line.indexOf(" ") === 0 ? line.slice(4) : trimmed;
current.text += body + "\n";
}
}
if (current) sections.push(current);
return sections;
}
function loadFromBundle(text) {
var sections = parseBundle(text);
return buildSeed(sections);
}
function buildSeed(results) {
var seed = {
responses: {},
concepts: [],
conceptContexts: [],
facts: [],
brainstormSeeds: { triggers: [], categories: [] },
personas: {
triggers: [],
defaultPersona: "",
bodyTemplate: "",
fallbackBody: "",
personas: [],
topics: [],
},
tools: [],
agentInfo: {},
languageRules: [],
promptPatterns: [],
intentRouting: { intents: [], articlePrefixes: [], tracePrefixes: [] },
environments: { environments: [], migrationDescription: "", flows: [] },
raw: {},
};
for (var i = 0; i < results.length; i += 1) {
var item = results[i];
seed.raw[item.file] = item.text;
if (!item.text) continue;
var root = parseLino(item.text);
if (item.file.indexOf("multilingual") !== -1) {
seed.responses = mergeResponses(
seed.responses,
extractMultilingualResponses(root),
);
} else if (item.file.indexOf("facts") !== -1) {
seed.facts = seed.facts.concat(extractFacts(root));
} else if (item.file.indexOf("brainstorm-seeds") !== -1) {
seed.brainstormSeeds = extractBrainstormSeeds(root);
} else if (item.file.indexOf("personas") !== -1) {
seed.personas = extractPersonas(root);
} else if (item.file.indexOf("concept-contexts") !== -1) {
seed.conceptContexts = seed.conceptContexts.concat(
extractConceptContexts(root),
);
} else if (item.file.indexOf("concepts") !== -1) {
seed.concepts = seed.concepts.concat(extractConcepts(root));
} else if (item.file.indexOf("tools") !== -1) {
seed.tools = seed.tools.concat(extractTools(root));
} else if (item.file.indexOf("agent-info") !== -1) {
Object.assign(seed.agentInfo, extractAgentInfo(root));
} else if (item.file.indexOf("language-detection") !== -1) {
seed.languageRules = seed.languageRules.concat(extractLanguageRules(root));
} else if (item.file.indexOf("prompt-patterns") !== -1) {
seed.promptPatterns = seed.promptPatterns.concat(extractPromptPatterns(root));
} else if (item.file.indexOf("intent-routing") !== -1) {
seed.intentRouting = extractIntentRouting(root);
} else if (item.file.indexOf("environments") !== -1) {
seed.environments = extractEnvironmentDirectory(root);
}
}
return seed;
}
global.FormalAiSeed = {
parse: parseLino,
loadAll: loadAll,
loadFromBundle: loadFromBundle,
parseBundle: parseBundle,
extractMultilingualResponses: extractMultilingualResponses,
extractAgentInfo: extractAgentInfo,
extractLanguageRules: extractLanguageRules,
extractPromptPatterns: extractPromptPatterns,
extractConcepts: extractConcepts,
extractConceptContexts: extractConceptContexts,
extractFacts: extractFacts,
extractBrainstormSeeds: extractBrainstormSeeds,
extractPersonas: extractPersonas,
extractTools: extractTools,
extractIntentRouting: extractIntentRouting,
extractEnvironmentDirectory: extractEnvironmentDirectory,
DEFAULT_FILES: DEFAULT_FILES,
isWorker: isWorker(),
};
})(typeof self !== "undefined" ? self : globalThis);