function currentAssetVersion() {
try {
const search = self.location && self.location.search;
const match = search && /[?&]v=([^&]+)/.exec(search);
return match ? decodeURIComponent(match[1].replace(/\+/g, " ")) : "";
} catch (_error) {
return "";
}
}
function withAssetVersion(url) {
const version = currentAssetVersion();
if (!version) return url;
return `${url}${url.includes("?") ? "&" : "?"}v=${encodeURIComponent(
version,
)}`;
}
try {
importScripts(withAssetVersion("seed_loader.js"));
} catch (_error) {
}
let wasm;
let mode = "wasm worker";
const FALLBACK_IDENTITY_ANSWER =
"I am formal-ai, a deterministic symbolic AI proof of concept that answers from local Links Notation rules and OpenAI-compatible API shapes. I do not perform neural inference in this demo.";
const FALLBACK_GREETING_ANSWER = "Hi, how may I help you?";
const FALLBACK_UNKNOWN_ANSWER =
"I do not have a learned symbolic rule for that prompt yet. Add a Links Notation fact or rule, then run the request again.";
let MULTILINGUAL_ANSWERS = {
greeting: {
en: { text: FALLBACK_GREETING_ANSWER, variants: [FALLBACK_GREETING_ANSWER] },
},
identity: {
en: { text: FALLBACK_IDENTITY_ANSWER, variants: [FALLBACK_IDENTITY_ANSWER] },
},
unknown: {
en: { text: FALLBACK_UNKNOWN_ANSWER, variants: [FALLBACK_UNKNOWN_ANSWER] },
},
};
let CONCEPTS = [];
let CONCEPT_CONTEXTS = [];
let TOOLS = [];
let SEED_RAW = {};
let AGENT_INFO = {};
let LANGUAGE_RULES = [
{ language: "ru", start: 0x0400, end: 0x04ff },
{ language: "hi", start: 0x0900, end: 0x097f },
{ language: "zh", start: 0x4e00, end: 0x9fff },
];
let PROMPT_PATTERNS = [];
let INTENT_ROUTING = {
intents: [
{
id: "intent_greeting",
slug: "greeting",
responseLink: "response:greeting",
keywords: ["hi", "hello", "hey", "привет", "здравствуйте", "नमस्ते", "你好", "您好"],
phrases: [],
tokens: ["greet"],
combos: [],
},
{
id: "intent_identity",
slug: "identity",
responseLink: "response:identity",
keywords: [],
phrases: [
"who are you",
"what are you",
"who is formal ai",
"what is formal ai",
"who is formalai",
"what is formalai",
"tell me about yourself",
"introduce yourself",
"кто ты",
"что ты",
"तुम कौन हो",
"你是谁",
"你是誰",
],
tokens: [],
combos: [
["who", "you"],
["what", "you"],
["tell", "yourself"],
["introduce", "yourself"],
["кто", "ты"],
["что", "ты"],
["who", "formal", "ai"],
["what", "formal", "ai"],
],
},
],
articlePrefixes: ["the ", "a ", "an "],
tracePrefixes: ["answer_", "trace_"],
};
function fallbackEntry(intent) {
if (intent === "greeting") {
return { text: FALLBACK_GREETING_ANSWER, variants: [FALLBACK_GREETING_ANSWER] };
}
if (intent === "identity") {
return { text: FALLBACK_IDENTITY_ANSWER, variants: [FALLBACK_IDENTITY_ANSWER] };
}
return { text: FALLBACK_UNKNOWN_ANSWER, variants: [FALLBACK_UNKNOWN_ANSWER] };
}
function normalizeEntry(value, intent) {
if (value && typeof value === "object" && typeof value.text === "string") {
const variants =
Array.isArray(value.variants) && value.variants.length > 0
? value.variants
: [value.text];
return { text: value.text, variants: variants };
}
if (typeof value === "string") {
return { text: value, variants: [value] };
}
return fallbackEntry(intent);
}
function answerFor(intent, language, options) {
const opts = options || {};
const table = MULTILINGUAL_ANSWERS[intent] || {};
const raw = table[language] || table.en || fallbackEntry(intent);
const entry = normalizeEntry(raw, intent);
if (opts.randomize && Array.isArray(entry.variants) && entry.variants.length > 1) {
const idx = Math.floor(Math.random() * entry.variants.length);
return entry.variants[idx] || entry.text;
}
return entry.text;
}
function detectLanguage(prompt) {
const text = String(prompt || "");
for (const ch of text) {
const code = ch.codePointAt(0);
for (const rule of LANGUAGE_RULES) {
if (
rule.language !== "en" &&
code >= rule.start &&
code <= rule.end
) {
return rule.language;
}
}
}
if (/[a-zA-Z]/.test(text)) return "en";
return AGENT_INFO.default_language || "en";
}
function normalizePrompt(prompt) {
return prompt.toLowerCase().replace(/[^a-z0-9]+/g, " ").trim();
}
function normalizeConceptTerm(value) {
let lower = String(value || "").toLowerCase();
for (const prefix of ["the ", "a ", "an "]) {
if (lower.startsWith(prefix)) {
lower = lower.slice(prefix.length);
break;
}
}
return lower.trim().replace(/[?.!,;:]+$/g, "").trim();
}
function recordMatchesTerm(record, normalized) {
return (
normalizeConceptTerm(record.term) === normalized ||
normalizeConceptTerm(record.slug) === normalized ||
(Array.isArray(record.aliases) &&
record.aliases.some(
(alias) => normalizeConceptTerm(alias) === normalized,
))
);
}
function contextRecordMatches(contextRecord, contextNormalized) {
if (!contextRecord) return false;
if (
Array.isArray(contextRecord.aliases) &&
contextRecord.aliases.some(
(alias) => normalizeConceptTerm(alias) === contextNormalized,
)
) {
return true;
}
return (
Array.isArray(contextRecord.labels) &&
contextRecord.labels.some(
(label) => normalizeConceptTerm(label.text) === contextNormalized,
)
);
}
function resolveContextRecord(contextNormalized) {
if (!contextNormalized) return null;
for (const record of CONCEPT_CONTEXTS) {
if (contextRecordMatches(record, contextNormalized)) return record;
}
return null;
}
function recordHasContext(record, contextNormalized) {
if (
Array.isArray(record.contexts) &&
record.contexts.some(
(candidate) => normalizeConceptTerm(candidate) === contextNormalized,
)
) {
return true;
}
const contextRecord = resolveContextRecord(contextNormalized);
if (contextRecord && Array.isArray(record.contextLinks)) {
return record.contextLinks.some(
(slug) => String(slug).trim() === contextRecord.slug,
);
}
return false;
}
function localizedConceptFor(record, language) {
if (!record || !Array.isArray(record.localized)) return null;
return (
record.localized.find((loc) => loc && loc.language === language) ||
record.localized.find((loc) => loc && loc.language === "en") ||
null
);
}
function contextLabelFor(contextRecord, language) {
if (!contextRecord || !Array.isArray(contextRecord.labels)) {
return null;
}
const exact = contextRecord.labels.find(
(label) => label && label.language === language,
);
if (exact && exact.text) return exact.text;
const english = contextRecord.labels.find(
(label) => label && label.language === "en",
);
if (english && english.text) return english.text;
return contextRecord.slug || null;
}
function rankConceptForPair(termRaw, contextRaw) {
const normalized = normalizeConceptTerm(termRaw);
if (!normalized) return null;
const contextNormalized = contextRaw ? normalizeConceptTerm(contextRaw) : "";
const termMatches = CONCEPTS.filter((record) =>
recordMatchesTerm(record, normalized),
);
if (termMatches.length === 0) return null;
if (contextNormalized) {
const ctxHit = termMatches.find((record) =>
recordHasContext(record, contextNormalized),
);
if (ctxHit) {
return {
record: ctxHit,
contextMatch: true,
context: contextNormalized,
};
}
}
termMatches.sort((a, b) => {
const ac = (Array.isArray(a.contexts) && a.contexts.length > 0) ? 1 : 0;
const bc = (Array.isArray(b.contexts) && b.contexts.length > 0) ? 1 : 0;
return ac - bc;
});
return {
record: termMatches[0],
contextMatch: false,
context: contextNormalized || null,
};
}
function lookupConceptQuery(query) {
if (!query) return null;
const direct = rankConceptForPair(query.term, query.context);
if (direct) return direct;
if (query.context) {
const reversed = rankConceptForPair(query.context, query.term);
if (reversed) return reversed;
}
return null;
}
function lookupConcept(term) {
const hit = lookupConceptQuery({ term: term, context: null });
return hit ? hit.record : null;
}
const DEFAULT_CONCEPT_SUFFIXES = [
" क्या होता है",
" क्या है",
" कौन हैं",
" कौन है",
"是甚麼",
"是什么",
"是誰",
"是谁",
];
const DEFAULT_CONCEPT_PREFIXES = [
"what is a ",
"what is an ",
"what is the ",
"what is ",
"what's a ",
"what's an ",
"what's the ",
"what's ",
"what does ",
"tell me about ",
"tell me what ",
"define ",
"explain ",
"describe ",
"who is ",
"who was ",
"что такое ",
"что это ",
"кто такой ",
"кто такая ",
"кто это ",
"расскажи о ",
"расскажи про ",
"опиши ",
"объясни ",
"什么是",
"甚麼是",
"请解释",
"请说说",
"介绍一下",
];
function conceptPatternsByKind(kind) {
const matches = PROMPT_PATTERNS.filter(
(p) => p && p.intent === "concept_lookup" && p.kind === kind && p.text,
).map((p) => p.text);
matches.sort((a, b) => b.length - a.length);
if (matches.length > 0) return matches;
if (kind === "suffix") return DEFAULT_CONCEPT_SUFFIXES;
if (kind === "prefix") return DEFAULT_CONCEPT_PREFIXES;
return [];
}
function splitTermAndContext(bodyOriginal, bodyLower) {
const delimiters = conceptPatternsByKind("context_delimiter");
for (const delimiter of delimiters) {
const idx = bodyLower.indexOf(delimiter);
if (idx >= 0) {
const term = bodyLower.slice(0, idx).trim();
const context = bodyLower.slice(idx + delimiter.length).trim();
const termOriginal = bodyOriginal.slice(0, idx).trim();
const contextOriginal = bodyOriginal
.slice(idx + delimiter.length)
.trim();
if (term && context) {
return {
term: term,
context: context,
termOriginal: termOriginal || term,
contextOriginal: contextOriginal || context,
};
}
}
}
return {
term: bodyLower,
context: null,
termOriginal: bodyOriginal || bodyLower,
contextOriginal: null,
};
}
function extractConceptQuery(prompt) {
const trimmedRaw = String(prompt || "")
.trim()
.replace(/[?。.!!,,;:]+$/g, "")
.trim();
if (!trimmedRaw) return null;
const suffixes = conceptPatternsByKind("suffix");
for (const suffix of suffixes) {
if (trimmedRaw.endsWith(suffix)) {
return finalizeConceptBody(
trimmedRaw.slice(0, -suffix.length).trim(),
);
}
}
const lower = trimmedRaw.toLowerCase();
const prefixes = conceptPatternsByKind("prefix");
let body = null;
for (const prefix of prefixes) {
if (lower.startsWith(prefix)) {
body = trimmedRaw.slice(prefix.length);
break;
}
}
if (!body) return null;
return finalizeConceptBody(body);
}
function extractConceptTerm(prompt) {
const query = extractConceptQuery(prompt);
return query ? query.term : null;
}
function humanizeUrl(url) {
if (typeof url !== "string" || url.length === 0) return url;
if (!url.includes("%")) return url;
try {
return decodeURI(url);
} catch (_error) {
return url;
}
}
function renderSourceLink(source) {
const human = humanizeUrl(source);
return human === source ? source : `[${human}](${source})`;
}
function finalizeConceptBody(body) {
let originalBase = String(body || "")
.trim()
.replace(/[?。.!!,,;:]+$/g, "")
.trim();
if (!originalBase) return null;
let original = originalBase;
let lower = original.toLowerCase();
for (const suffix of [" mean", " stand for"]) {
if (lower.endsWith(suffix)) {
original = original.slice(0, -suffix.length).trim();
lower = lower.slice(0, -suffix.length).trim();
break;
}
}
if (!lower) return null;
return splitTermAndContext(original, lower);
}
function tokenizeArithmetic(input) {
const tokens = [];
let i = 0;
while (i < input.length) {
const ch = input[i];
if (ch === " " || ch === "\t" || ch === "_" || ch === ",") {
i += 1;
continue;
}
if (ch === "+") {
tokens.push({ kind: "+" });
i += 1;
} else if (ch === "-" || ch === "−") {
tokens.push({ kind: "-" });
i += 1;
} else if (ch === "*" || ch === "×" || ch === "·") {
tokens.push({ kind: "*" });
i += 1;
} else if (ch === "/" || ch === "÷") {
tokens.push({ kind: "/" });
i += 1;
} else if (ch === "%") {
tokens.push({ kind: "%" });
i += 1;
} else if (ch === "(") {
tokens.push({ kind: "(" });
i += 1;
} else if (ch === ")") {
tokens.push({ kind: ")" });
i += 1;
} else if ((ch >= "0" && ch <= "9") || ch === ".") {
let j = i;
while (
j < input.length &&
((input[j] >= "0" && input[j] <= "9") || input[j] === ".")
) {
j += 1;
}
const slice = input.slice(i, j);
const value = Number(slice);
if (Number.isNaN(value)) {
throw new Error("unparseable");
}
tokens.push({ kind: "num", value });
i = j;
} else {
throw new Error("unparseable");
}
}
return tokens;
}
function evaluateArithmetic(expression) {
const lower = expression.toLowerCase();
const normalized = lower
.replace(/\s+multiplied by\s+/g, " * ")
.replace(/\s+divided by\s+/g, " / ")
.replace(/\s+times\s+/g, " * ")
.replace(/\s+plus\s+/g, " + ")
.replace(/\s+minus\s+/g, " - ")
.replace(/\s+modulo\s+/g, " % ")
.replace(/\s+mod\s+/g, " % ");
const tokens = tokenizeArithmetic(normalized);
if (tokens.length === 0) {
throw new Error("empty");
}
let cursor = 0;
const peek = () => tokens[cursor];
const advance = () => tokens[cursor++];
function parsePrimary() {
const tok = advance();
if (!tok) throw new Error("unparseable");
if (tok.kind === "num") return tok.value;
if (tok.kind === "(") {
const inner = parseAdditive();
const close = advance();
if (!close || close.kind !== ")") throw new Error("unbalanced");
return inner;
}
throw new Error("unparseable");
}
function parseUnary() {
const tok = peek();
if (tok && tok.kind === "-") {
advance();
return -parseUnary();
}
if (tok && tok.kind === "+") {
advance();
return parseUnary();
}
return parsePrimary();
}
function parseMultiplicative() {
let left = parseUnary();
while (true) {
const tok = peek();
if (!tok || (tok.kind !== "*" && tok.kind !== "/" && tok.kind !== "%")) {
break;
}
const op = tok.kind;
advance();
const right = parseUnary();
if (op === "*") {
left = left * right;
} else if (right === 0) {
throw new Error("division by zero");
} else if (op === "/") {
left = left / right;
} else {
left = left % right;
}
if (!Number.isFinite(left)) throw new Error("overflow");
}
return left;
}
function parseAdditive() {
let left = parseMultiplicative();
while (true) {
const tok = peek();
if (!tok || (tok.kind !== "+" && tok.kind !== "-")) break;
const isPlus = tok.kind === "+";
advance();
const right = parseMultiplicative();
left = isPlus ? left + right : left - right;
if (!Number.isFinite(left)) throw new Error("overflow");
}
return left;
}
const value = parseAdditive();
if (cursor !== tokens.length) {
throw new Error("unparseable");
}
return value;
}
function formatArithmeticResult(value) {
if (!Number.isFinite(value)) return "non-finite";
if (Math.abs(value % 1) === 0 && Math.abs(value) < 1e15) {
return value.toFixed(0);
}
const rendered = value.toFixed(10);
const trimmed = rendered.replace(/0+$/, "").replace(/\.$/, "");
return trimmed === "" || trimmed === "-" ? "0" : trimmed;
}
function extractArithmeticExpression(prompt) {
const trimmed = String(prompt || "").trim();
if (!trimmed) return null;
const lower = trimmed.toLowerCase();
const prefixes = [
"what is ",
"what's ",
"what does ",
"calculate ",
"compute ",
"evaluate ",
"how much is ",
"solve ",
];
let working = trimmed;
for (const prefix of prefixes) {
if (lower.startsWith(prefix)) {
working = trimmed.slice(prefix.length);
break;
}
}
working = working.replace(/[?.!]+$/g, "").trim();
working = working
.replace(/\s+equals?$/i, "")
.replace(/\s+=$/g, "")
.trim();
if (!working) return null;
const workingLower = working.toLowerCase();
const hasSymbolic = /[+\-*/%×·÷−]/.test(working);
const hasWord =
/ plus | minus | times | multiplied by | divided by | modulo | mod /.test(
` ${workingLower} `,
);
const hasDigit = /[0-9]/.test(working);
if (!hasDigit) return null;
if (!hasSymbolic && !hasWord) return null;
const allowed = /^[0-9+\-*/%().\s_×·÷−,a-zA-Z]+$/;
if (!allowed.test(working)) return null;
return working;
}
function extractFencedBlock(text, languages) {
const fence = "```";
let cursor = 0;
while (true) {
const open = text.indexOf(fence, cursor);
if (open === -1) return null;
const infoStart = open + fence.length;
const newlineRel = text.indexOf("\n", infoStart);
const infoEnd = newlineRel === -1 ? text.length : newlineRel;
const info = text.slice(infoStart, infoEnd).trim().toLowerCase();
const bodyStart = Math.min(infoEnd + 1, text.length);
const closeRel = text.indexOf(fence, bodyStart);
if (closeRel === -1) return null;
const body = text.slice(bodyStart, closeRel).replace(/\n+$/, "");
if (info === "" || languages.some((lang) => info === lang)) {
return body;
}
cursor = closeRel + fence.length;
}
}
function extractJavaScriptProgram(prompt) {
const lower = String(prompt || "").toLowerCase();
const asksToRun =
lower.includes("run this javascript") ||
lower.includes("run this js") ||
lower.includes("execute this javascript") ||
lower.includes("execute this js") ||
lower.includes("run the following javascript") ||
lower.includes("run the following js") ||
lower.includes("evaluate this javascript") ||
lower.includes("evaluate this js");
if (!asksToRun) return null;
const fenced = extractFencedBlock(prompt, ["javascript", "js"]);
if (fenced !== null) return fenced;
const backticks = prompt.match(/`([^`]+)`/);
if (backticks) return backticks[1];
const quoted = prompt.match(/"([^"]+)"/);
return quoted ? quoted[1] : null;
}
function findIntentRoute(id) {
if (!INTENT_ROUTING || !Array.isArray(INTENT_ROUTING.intents)) return null;
for (const route of INTENT_ROUTING.intents) {
if (route && route.id === id) return route;
}
return null;
}
function tokensOf(normalized) {
return normalized ? normalized.split(/\s+/).filter(Boolean) : [];
}
function tokenContains(normalized, expected) {
return tokensOf(normalized).includes(String(expected || ""));
}
function matchesIntentRoute(normalized, rawPrompt, id) {
const route = findIntentRoute(id);
if (!route) return false;
const raw = String(rawPrompt || "")
.toLowerCase()
.replace(/[?。.!!,,;:]+$/g, "")
.trim();
if (route.keywords && route.keywords.some((kw) => kw === normalized || kw === raw)) {
return true;
}
if (route.phrases && route.phrases.some((ph) => ph === normalized || ph === raw)) {
return true;
}
if (route.tokens && route.tokens.some((tok) => tokenContains(normalized, tok))) {
return true;
}
if (
route.combos &&
route.combos.some(
(combo) =>
Array.isArray(combo) &&
combo.length > 0 &&
combo.every((tok) => tokenContains(normalized, tok)),
)
) {
return true;
}
return false;
}
function isIdentityPrompt(normalized, rawPrompt) {
return matchesIntentRoute(normalized, rawPrompt, "intent_identity");
}
function isGreetingPrompt(normalized, rawPrompt) {
return matchesIntentRoute(normalized, rawPrompt, "intent_greeting");
}
function extractName(text) {
const patterns = [
/\bmy name is\s+([A-Z][a-zA-Z'-]+(?:\s+[A-Z][a-zA-Z'-]+)*)/,
/\bi am\s+([A-Z][a-zA-Z'-]+(?:\s+[A-Z][a-zA-Z'-]+)*)/,
/\bi'm\s+([A-Z][a-zA-Z'-]+(?:\s+[A-Z][a-zA-Z'-]+)*)/,
/\bcall me\s+([A-Z][a-zA-Z'-]+(?:\s+[A-Z][a-zA-Z'-]+)*)/,
];
for (const pattern of patterns) {
const match = pattern.exec(text);
if (match) return match[1];
}
return null;
}
function tryRecallName(history) {
if (!Array.isArray(history) || history.length === 0) return null;
for (let i = history.length - 1; i >= 0; i -= 1) {
const turn = history[i];
if (turn && turn.role === "user") {
const name = extractName(String(turn.content || ""));
if (name) {
return {
intent: "recall_name",
content: `Your name is ${name}.`,
confidence: 0.95,
evidence: [`recall_name:${name}`, "prior_turn:user"],
};
}
}
}
return null;
}
function tryRecallLastQuestion(history) {
if (!Array.isArray(history) || history.length === 0) return null;
for (let i = history.length - 1; i >= 0; i -= 1) {
const turn = history[i];
if (turn && turn.role === "user") {
const content = String(turn.content || "").trim();
if (content) {
return {
intent: "recall_last_question",
content: `Your previous question was: ${content}`,
confidence: 0.9,
evidence: ["recall_last_question", "prior_turn:user"],
};
}
}
}
return null;
}
function trySummarizeConversation(history) {
if (!Array.isArray(history) || history.length === 0) return null;
const turns = history.filter((turn) => turn && turn.content);
if (turns.length === 0) return null;
let userCount = 0;
let assistantCount = 0;
const intentCounts = new Map();
const languages = new Map();
const concepts = new Set();
const calculations = [];
const helloWorlds = new Set();
const unanswered = [];
let lastUser = null;
for (const turn of turns) {
const role = turn.role || "assistant";
const language = detectLanguage(turn.content);
languages.set(language, (languages.get(language) || 0) + 1);
if (role === "user") {
userCount += 1;
lastUser = turn.content;
} else {
assistantCount += 1;
if (lastUser) {
lastUser = null;
}
const intent = String(turn.intent || "unknown");
intentCounts.set(intent, (intentCounts.get(intent) || 0) + 1);
if (intent === "calculation" && typeof turn.content === "string") {
const match = turn.content.match(/^([^=]+=\s*[^\n]+)/);
if (match) calculations.push(match[1].trim());
}
if (intent.startsWith("hello_world_")) {
helloWorlds.add(intent.slice("hello_world_".length));
}
if (intent.startsWith("concept_lookup")) {
const evidence = Array.isArray(turn.evidence) ? turn.evidence : [];
for (const item of evidence) {
if (typeof item !== "string") continue;
const conceptMatch = item.match(/^concept_lookup:request:(.+)$/);
if (conceptMatch) concepts.add(conceptMatch[1]);
}
}
}
}
if (lastUser) {
unanswered.push(lastUser);
}
const lines = [];
lines.push("## Conversation summary");
lines.push("");
lines.push(
`- ${turns.length} turn(s): ${userCount} user, ${assistantCount} assistant`,
);
if (languages.size > 0) {
const list = Array.from(languages.entries())
.sort((a, b) => b[1] - a[1])
.map(([lang, count]) => `${lang} (${count})`)
.join(", ");
lines.push(`- Languages: ${list}`);
}
if (intentCounts.size > 0) {
const list = Array.from(intentCounts.entries())
.sort((a, b) => b[1] - a[1])
.map(([intent, count]) => `${intent} (${count})`)
.join(", ");
lines.push(`- Intents: ${list}`);
}
if (concepts.size > 0) {
lines.push(`- Concepts looked up: ${Array.from(concepts).join(", ")}`);
}
if (calculations.length > 0) {
lines.push(`- Calculations: ${calculations.join("; ")}`);
}
if (helloWorlds.size > 0) {
lines.push(
`- Hello-world programs generated for: ${Array.from(helloWorlds).join(", ")}`,
);
}
if (unanswered.length > 0) {
lines.push(`- Unanswered: ${unanswered.join(" | ")}`);
}
const evidence = [
"summarize_conversation",
`turns:${turns.length}`,
`users:${userCount}`,
`assistants:${assistantCount}`,
];
if (intentCounts.size > 0) {
evidence.push(`intents:${Array.from(intentCounts.keys()).join("|")}`);
}
return {
intent: "summarize_conversation",
content: lines.join("\n"),
confidence: 0.9,
evidence,
};
}
function tryArithmetic(prompt) {
const expression = extractArithmeticExpression(prompt);
if (!expression) return null;
try {
const value = evaluateArithmetic(expression);
const formatted = formatArithmeticResult(value);
return {
intent: "calculation",
content: `${expression.trim()} = ${formatted}`,
confidence: 1.0,
evidence: [`calculation:${expression.trim()}=${formatted}`],
};
} catch (error) {
const message = String(error && error.message ? error.message : error);
return {
intent: "calculation_error",
content: `I could not evaluate \`${expression.trim()}\`: ${message}.`,
confidence: 0.4,
evidence: [`calculation_error:${message}`],
};
}
}
function renderConceptInContext(language, context, record) {
const contextNormalized = normalizeConceptTerm(context);
const contextRecord = resolveContextRecord(contextNormalized);
const contextLabel =
(contextRecord && contextLabelFor(contextRecord, language)) || context;
const sameAsLabel =
String(contextLabel).trim().toLowerCase() ===
String(context).trim().toLowerCase();
const intentVariant = sameAsLabel
? "concept_lookup_in_context_no_alias"
: "concept_lookup_in_context";
const variantTable = MULTILINGUAL_ANSWERS[intentVariant] || {};
const baseTable = MULTILINGUAL_ANSWERS.concept_lookup_in_context || {};
const templateEntry =
variantTable[language] ||
variantTable.en ||
baseTable[language] ||
baseTable.en ||
null;
const template = templateEntry
? (typeof templateEntry === "string" ? templateEntry : templateEntry.text)
: "In the context of {context} ({context_label}), {term} ({category}) means: {summary}\n\nSource: {source} ({source_kind}).";
const localized = localizedConceptFor(record, language);
const term = (localized && localized.term) || record.term;
const summary = (localized && localized.summary) || record.summary;
const source = (localized && localized.source) || record.source;
const sourceKind =
(localized && localized.sourceKind) || record.sourceKind;
const sourceMarkup = renderSourceLink(source);
return template
.replace(/\{context_label\}/g, contextLabel)
.replace(/\{context\}/g, context)
.replace(/\{term\}/g, term)
.replace(/\{category\}/g, record.category)
.replace(/\{summary\}/g, summary)
.replace(/\{source\}/g, sourceMarkup)
.replace(/\{source_kind\}/g, sourceKind);
}
function renderConceptPlain(language, record) {
const localized = localizedConceptFor(record, language);
const term = (localized && localized.term) || record.term;
const summary = (localized && localized.summary) || record.summary;
const source = (localized && localized.source) || record.source;
const sourceKind =
(localized && localized.sourceKind) || record.sourceKind;
const sourceMarkup = renderSourceLink(source);
return `${term} (${record.category}): ${summary}\n\nSource: ${sourceMarkup} (${sourceKind}).`;
}
function tryConceptLookup(prompt) {
const query = extractConceptQuery(prompt);
if (!query) return null;
const evidence = [`concept_lookup:request:${query.term}`];
if (query.context) {
evidence.push(`concept_lookup:context:${query.context}`);
}
const lookup = lookupConceptQuery(query);
if (!lookup) {
return null;
}
const record = lookup.record;
const language = detectLanguage(prompt);
const localized = localizedConceptFor(record, language);
const effectiveSource = (localized && localized.source) || record.source;
const humanSource = humanizeUrl(effectiveSource);
evidence.push(`concept_lookup:hit:${record.slug}`);
evidence.push(`source:${humanSource}`);
if (record.wikidata) {
evidence.push(`wikidata:${record.wikidata}`);
}
if (lookup.contextMatch && lookup.context) {
evidence.push(`concept_lookup:context-match:${lookup.context}`);
const body = renderConceptInContext(language, lookup.context, record);
return {
intent: "concept_lookup_in_context",
content: body,
confidence: 0.9,
evidence,
};
}
if (lookup.context) {
evidence.push(`concept_lookup:context-mismatch:${lookup.context}`);
}
const body = renderConceptPlain(language, record);
return {
intent: "concept_lookup",
content: body,
confidence: 0.9,
evidence,
};
}
const WIKIPEDIA_HOSTS = {
en: "https://en.wikipedia.org/api/rest_v1/page/summary",
ru: "https://ru.wikipedia.org/api/rest_v1/page/summary",
hi: "https://hi.wikipedia.org/api/rest_v1/page/summary",
zh: "https://zh.wikipedia.org/api/rest_v1/page/summary",
};
function wikipediaHostsFor(language) {
const ordered = [language, "en"].filter(
(value, index, array) => value && array.indexOf(value) === index,
);
return ordered.map((lang) => ({
language: lang,
url: WIKIPEDIA_HOSTS[lang] || WIKIPEDIA_HOSTS.en,
}));
}
function capitalizeWords(term) {
return term
.split(/(\s+)/)
.map((part) =>
/\S/.test(part) ? part.charAt(0).toUpperCase() + part.slice(1) : part,
)
.join("");
}
function wikipediaTermVariants(term) {
const seen = new Set();
const variants = [];
const push = (value) => {
if (!value) return;
const slug = String(value)
.trim()
.replace(/\s+/g, "_")
.replace(/_+/g, "_");
if (!slug || seen.has(slug)) return;
seen.add(slug);
variants.push(slug);
};
const trimmed = String(term || "").trim();
push(trimmed);
push(capitalizeWords(trimmed));
push(capitalizeWords(trimmed.toLowerCase()));
push(trimmed.toLowerCase());
const words = trimmed.split(/\s+/).filter(Boolean);
if (words.length === 2) {
const swapped = `${words[1]}, ${words[0]}`;
push(swapped);
push(capitalizeWords(swapped.toLowerCase()));
}
return variants;
}
async function fetchWikipediaSummary(term, language) {
if (typeof fetch !== "function") return null;
const hosts = wikipediaHostsFor(language);
const variants = wikipediaTermVariants(term);
for (const host of hosts) {
for (const slug of variants) {
const url = `${host.url}/${encodeURIComponent(slug)}`;
try {
const response = await fetch(url, {
headers: {
accept: "application/json",
"api-user-agent":
"formal-ai-demo (https://github.com/link-assistant/formal-ai)",
},
});
if (!response || !response.ok) continue;
const data = await response.json();
if (!data || typeof data !== "object") continue;
if (data.type === "disambiguation") continue;
const extract = String(data.extract || "").trim();
if (!extract) continue;
const title = String(data.title || term);
const pageUrl =
(data.content_urls &&
data.content_urls.desktop &&
data.content_urls.desktop.page) ||
url;
return {
title,
extract,
url: pageUrl,
language: host.language,
};
} catch (_error) {
}
}
}
return null;
}
async function tryWikipediaLookup(prompt, language) {
const query = extractConceptQuery(prompt);
if (!query) return null;
if (lookupConceptQuery(query)) return null;
const wikiTerm = query.termOriginal || query.term;
const summary = await fetchWikipediaSummary(wikiTerm, language);
if (!summary) return null;
const humanUrl = humanizeUrl(summary.url);
const body =
`${summary.title}: ${summary.extract}\n\n` +
`Source: [${humanUrl}](${summary.url}) (wikipedia).`;
return {
intent: "wikipedia_lookup",
content: body,
confidence: 0.85,
evidence: [
`wikipedia_lookup:${summary.title}`,
`source:${humanUrl}`,
`language:${summary.language}`,
],
};
}
function tryJavaScriptExecution(prompt) {
const program = extractJavaScriptProgram(prompt);
if (program === null) return null;
const logs = [];
const captureConsole = {
log: (...args) =>
logs.push(
args
.map((value) =>
typeof value === "string" ? value : JSON.stringify(value),
)
.join(" "),
),
};
let result;
let error = null;
try {
const runner = new Function(
"console",
`"use strict"; return (function(){ ${program}\n })();`,
);
result = runner(captureConsole);
} catch (err) {
error = err;
}
const lines = [];
lines.push("Execution status: ran in the demo's Web Worker sandbox.");
lines.push("Source:");
lines.push("```javascript");
lines.push(program);
lines.push("```");
if (error) {
lines.push("");
lines.push(`Error: ${error.message || String(error)}`);
} else {
if (logs.length > 0) {
lines.push("");
lines.push("Output:");
lines.push("```text");
lines.push(logs.join("\n"));
lines.push("```");
}
if (result !== undefined) {
lines.push("");
lines.push(`Returned: \`${String(result)}\``);
}
if (logs.length === 0 && result === undefined) {
lines.push("");
lines.push("Program completed without output or return value.");
}
}
lines.push("");
lines.push(
"Note: the browser worker has no DOM or network access, so side effects are limited.",
);
return {
intent: error ? "javascript_execution_error" : "javascript_execution",
content: lines.join("\n"),
confidence: error ? 0.5 : 0.95,
evidence: [
`execution_status:javascript:${error ? "error" : "ran"}`,
"language:javascript",
],
};
}
function helloWorldLanguage(prompt) {
const tokens = normalizePrompt(prompt).split(/\s+/);
if (!(tokens.includes("hello") && tokens.includes("world"))) return null;
if (tokens.includes("rust") || tokens.includes("rs")) return "rust";
if (tokens.includes("python") || tokens.includes("py")) return "python";
if (tokens.includes("typescript") || tokens.includes("ts"))
return "typescript";
if (
tokens.includes("javascript") ||
tokens.includes("js") ||
tokens.includes("node")
)
return "javascript";
if (tokens.includes("go") || tokens.includes("golang")) return "go";
if (tokens.includes("c")) return "c";
return null;
}
function tryHelloWorld(prompt) {
const language = helloWorldLanguage(prompt);
if (!language) return null;
const seeds = {
rust: {
fence: "rust",
code: 'fn main() {\n println!("Hello, world!");\n}',
},
python: {
fence: "python",
code: 'print("Hello, world!")',
},
javascript: {
fence: "javascript",
code: 'console.log("Hello, world!");',
},
typescript: {
fence: "typescript",
code: 'console.log("Hello, world!");',
},
go: {
fence: "go",
code:
'package main\n\nimport "fmt"\n\nfunc main() {\n fmt.Println("Hello, world!")\n}',
},
c: {
fence: "c",
code:
'#include <stdio.h>\n\nint main(void) {\n puts("Hello, world!");\n return 0;\n}',
},
};
const { fence, code } = seeds[language];
const lines = [];
lines.push(`Here is a minimal ${language} hello world program:`);
lines.push("");
lines.push("```" + fence);
lines.push(code);
lines.push("```");
lines.push("");
if (language === "javascript") {
const logs = [];
try {
const runner = new Function(
"console",
`"use strict"; ${code}`,
);
runner({ log: (...args) => logs.push(args.join(" ")) });
lines.push("Execution status: ran in the demo's Web Worker sandbox.");
lines.push("Output:");
lines.push("```text");
lines.push(logs.join("\n") || "(no output)");
lines.push("```");
} catch (error) {
lines.push(
`Execution status: failed in sandbox — ${error.message || String(error)}.`,
);
}
} else {
lines.push(
`Execution status: not run — the browser sandbox cannot invoke a ${language} toolchain. Copy the snippet into a ${language} environment to verify.`,
);
}
return {
intent: `hello_world_${language}`,
content: lines.join("\n"),
confidence: 0.9,
evidence: [
`hello_world:${language}`,
`execution_status:${language}:${language === "javascript" ? "ran" : "unavailable"}`,
],
};
}
function tryHistorical(prompt, history) {
const normalized = normalizePrompt(prompt);
if (isSummarizePrompt(prompt, normalized)) {
return trySummarizeConversation(history);
}
if (!normalized) return null;
if (normalized === "what is my name" || normalized === "what s my name") {
const hit = tryRecallName(history);
if (hit) return hit;
}
if (
normalized === "what was my previous question" ||
normalized === "what was the previous question" ||
normalized === "what was my last question"
) {
return tryRecallLastQuestion(history);
}
return null;
}
function isSummarizePrompt(prompt, normalized) {
const raw = String(prompt || "").trim().toLowerCase();
if (
normalized === "summarize" ||
normalized === "summarise" ||
normalized === "summarize chat" ||
normalized === "summarise chat" ||
normalized === "summarize so far" ||
normalized === "summarise so far" ||
normalized === "summary"
) {
return true;
}
if (
normalized.startsWith("summarize the conversation") ||
normalized.startsWith("summarise the conversation") ||
normalized.startsWith("summarize this conversation") ||
normalized.startsWith("summarise this conversation") ||
normalized.startsWith("summarize our conversation") ||
normalized.startsWith("summarise our conversation") ||
normalized.startsWith("summarize the chat") ||
normalized.startsWith("summarise the chat") ||
normalized.startsWith("summarize this chat") ||
normalized.startsWith("summarise this chat") ||
normalized.startsWith("give me a summary") ||
normalized.startsWith("can you summarize") ||
normalized.startsWith("can you summarise") ||
normalized.startsWith("please summarize") ||
normalized.startsWith("please summarise")
) {
return true;
}
if (
/^(суммируй|резюмируй|подведи\s+итог|кратк(ое|ий)\s+резюме|сделай\s+резюме|резюме\s+(беседы|разговора|чата))/.test(
raw,
)
) {
return true;
}
if (/^(सारांश|संक्षेप|सार\s+दो|सारांश\s+दो)/.test(raw)) {
return true;
}
if (/^(总结|總結|概括|摘要)/.test(raw)) {
return true;
}
return false;
}
async function solve(prompt, history, prefs) {
const preferences = prefs || {};
const steps = [];
const toolCalls = [];
const events = [`impulse:${prompt}`];
steps.push({ step: "impulse", detail: prompt });
const normalized = normalizePrompt(prompt);
events.push(`formalization:${normalized || "(empty)"}`);
steps.push({ step: "formalize", detail: normalized || "(empty)" });
const language = detectLanguage(prompt);
events.push(`language:${language}`);
steps.push({ step: "detect_language", detail: language });
if (isGreetingPrompt(normalized, prompt)) {
events.push("rule:greeting");
steps.push({ step: "match_rule", detail: "greeting" });
const randomize = preferences.greetingVariations !== false;
return finalize(events, steps, toolCalls, {
intent: "greeting",
content: answerFor("greeting", language, { randomize: randomize }),
confidence: 1.0,
evidence: [
"rule:greeting",
`language:${language}`,
`variation:${randomize ? "random" : "canonical"}`,
],
});
}
if (isIdentityPrompt(normalized, prompt)) {
events.push("rule:identity");
steps.push({ step: "match_rule", detail: "identity" });
return finalize(events, steps, toolCalls, {
intent: "identity",
content: answerFor("identity", language),
confidence: 1.0,
evidence: ["rule:identity", `language:${language}`],
});
}
const syncHandlers = [
{ name: "tryHistorical", run: () => tryHistorical(prompt, history) },
{ name: "tryArithmetic", run: () => tryArithmetic(prompt) },
{ name: "tryJavaScriptExecution", run: () => tryJavaScriptExecution(prompt) },
{ name: "tryConceptLookup", run: () => tryConceptLookup(prompt) },
{ name: "tryHelloWorld", run: () => tryHelloWorld(prompt) },
];
for (const handler of syncHandlers) {
const hit = handler.run();
if (hit) {
events.push(`handler:${hit.intent}`);
steps.push({ step: "dispatch_handler", detail: handler.name });
if (hit.intent === "javascript_execution" || hit.intent === "javascript_execution_error") {
toolCalls.push({
tool: "eval_js",
inputs: { prompt },
outputs: { intent: hit.intent, confidence: hit.confidence },
});
}
if (
hit.intent === "concept_lookup" ||
hit.intent === "concept_lookup_in_context"
) {
toolCalls.push({
tool: "concept_lookup",
inputs: { prompt },
outputs: { intent: hit.intent, confidence: hit.confidence },
});
}
return finalize(events, steps, toolCalls, hit);
}
}
steps.push({ step: "invoke_tool", detail: "wikipedia_lookup" });
const wiki = await tryWikipediaLookup(prompt, language);
if (wiki) {
events.push(`handler:${wiki.intent}`);
steps.push({ step: "dispatch_handler", detail: "tryWikipediaLookup" });
toolCalls.push({
tool: "wikipedia_lookup",
inputs: { prompt, language },
outputs: { intent: wiki.intent, confidence: wiki.confidence },
});
return finalize(events, steps, toolCalls, wiki);
}
toolCalls.push({
tool: "wikipedia_lookup",
inputs: { prompt, language },
outputs: { intent: "no_match" },
});
events.push("fallback:unknown");
steps.push({ step: "fallback", detail: "unknown" });
return finalize(events, steps, toolCalls, {
intent: "unknown",
content: answerFor("unknown", language),
confidence: 0.1,
evidence: ["fallback:unknown", `language:${language}`],
});
}
function finalize(events, steps, toolCalls, answer) {
const evidence = Array.isArray(answer.evidence) ? answer.evidence : [];
const trace = events.map((event) => `trace:${event}`);
return {
intent: answer.intent,
content: answer.content,
confidence: answer.confidence,
evidence: [...evidence, ...trace],
steps,
toolCalls,
};
}
let seedLoaded = false;
async function loadSeed() {
if (seedLoaded) return;
seedLoaded = true;
if (typeof self.FormalAiSeed !== "object" || self.FormalAiSeed === null) {
return;
}
try {
const seed = await self.FormalAiSeed.loadAll();
SEED_RAW = (seed && seed.raw) || {};
if (seed && seed.responses) {
const merged = {};
const intents = new Set(
Object.keys(MULTILINGUAL_ANSWERS).concat(Object.keys(seed.responses)),
);
intents.forEach((intent) => {
const base = MULTILINGUAL_ANSWERS[intent] || {};
const next = seed.responses[intent] || {};
const byLanguage = {};
const langs = new Set(Object.keys(base).concat(Object.keys(next)));
langs.forEach((language) => {
const incoming = next[language];
if (incoming !== undefined) {
byLanguage[language] = normalizeEntry(incoming, intent);
} else {
byLanguage[language] = normalizeEntry(base[language], intent);
}
});
merged[intent] = byLanguage;
});
MULTILINGUAL_ANSWERS = merged;
}
if (Array.isArray(seed && seed.concepts) && seed.concepts.length > 0) {
CONCEPTS = seed.concepts;
}
if (
Array.isArray(seed && seed.conceptContexts) &&
seed.conceptContexts.length > 0
) {
CONCEPT_CONTEXTS = seed.conceptContexts;
}
if (Array.isArray(seed && seed.tools) && seed.tools.length > 0) {
TOOLS = seed.tools;
}
if (seed && seed.agentInfo && typeof seed.agentInfo === "object") {
AGENT_INFO = Object.assign({}, AGENT_INFO, seed.agentInfo);
}
if (Array.isArray(seed && seed.languageRules) && seed.languageRules.length > 0) {
LANGUAGE_RULES = seed.languageRules
.filter((rule) => rule && rule.language && rule.start && rule.end)
.map((rule) => ({
language: rule.language,
start: Number(rule.start),
end: Number(rule.end),
}));
}
if (Array.isArray(seed && seed.promptPatterns) && seed.promptPatterns.length > 0) {
PROMPT_PATTERNS = seed.promptPatterns;
}
if (
seed &&
seed.intentRouting &&
Array.isArray(seed.intentRouting.intents) &&
seed.intentRouting.intents.length > 0
) {
INTENT_ROUTING = {
intents: seed.intentRouting.intents,
articlePrefixes:
seed.intentRouting.articlePrefixes && seed.intentRouting.articlePrefixes.length
? seed.intentRouting.articlePrefixes
: INTENT_ROUTING.articlePrefixes,
tracePrefixes:
seed.intentRouting.tracePrefixes && seed.intentRouting.tracePrefixes.length
? seed.intentRouting.tracePrefixes
: INTENT_ROUTING.tracePrefixes,
};
}
} catch (_error) {
}
}
async function init() {
if (wasm !== undefined) return;
await loadSeed();
try {
const source = await fetch(withAssetVersion("formal_ai_worker.wasm"));
const bytes = await source.arrayBuffer();
const module = await WebAssembly.instantiate(bytes, {});
wasm = module.instance.exports;
} catch (_error) {
wasm = null;
mode = "js fallback";
}
postMessage({
kind: "ready",
mode,
seed: {
responseIntents: Object.keys(MULTILINGUAL_ANSWERS),
conceptCount: CONCEPTS.length,
conceptContextCount: CONCEPT_CONTEXTS.length,
toolCount: TOOLS.length,
files: Object.keys(SEED_RAW),
},
});
}
self.onmessage = async (event) => {
await init();
const data = event.data || {};
if (data.kind === "seed_dump") {
postMessage({
kind: "seed_dump",
requestId: data.requestId,
raw: SEED_RAW,
responses: MULTILINGUAL_ANSWERS,
concepts: CONCEPTS,
conceptContexts: CONCEPT_CONTEXTS,
tools: TOOLS,
agentInfo: AGENT_INFO,
languageRules: LANGUAGE_RULES,
promptPatterns: PROMPT_PATTERNS,
});
return;
}
const prompt = data.prompt || "";
const history = Array.isArray(data.history) ? data.history : [];
const prefs = (data.prefs && typeof data.prefs === "object") ? data.prefs : {};
const answer = await solve(prompt, history, prefs);
postMessage({
kind: "message",
requestId: data.requestId,
intent: answer.intent,
content: answer.content,
confidence: answer.confidence,
evidence: answer.evidence,
steps: answer.steps,
toolCalls: answer.toolCalls,
});
};
init();