var we = Object.defineProperty; var ye = (s, e, t) => e in s ? we(s, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : s[e] = t; var g = (s, e, t) => ye(s, typeof e != "symbol" ? e + "" : e, t); class se { constructor(e = "/api") { g(this, "apiUrl"); this.apiUrl = e; } async sendMessage(e, t) { var p, o, l, d, h; const n = await fetch(`${this.apiUrl}/chat`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ messages: e, model: "gpt-4o-mini" }) }); if (!n.ok) throw new Error(`HTTP error! status: ${n.status}`); const r = (p = n.body) == null ? void 0 : p.getReader(), i = new TextDecoder(); let c = ""; if (!r) throw new Error("No response body"); let a = ""; for (; ; ) { const { done: k, value: u } = await r.read(); if (k) break; const x = i.decode(u, { stream: !0 }); a += x; const v = a.split(` `); a = v.pop() || ""; for (const $ of v) if ($.trim() !== "") try { const b = JSON.parse($); if (console.log("Received chunk:", b), t) switch (b.type) { case "text-delta": b.textDelta && (c += b.textDelta, t({ type: "text", content: b.textDelta })); break; case "tool-call-delta": b.toolCallDelta && t({ type: "tool-call", toolName: b.toolCallDelta.toolName, args: b.toolCallDelta.args }); break; case "tool-call": t({ type: "tool-call", toolName: (o = b.toolCall) == null ? void 0 : o.toolName, args: (l = b.toolCall) == null ? void 0 : l.args }); break; case "tool-result": t({ type: "tool-result", toolName: (d = b.toolResult) == null ? void 0 : d.toolName, result: (h = b.toolResult) == null ? void 0 : h.result }); break; case "step-start": t({ type: "step-start", stepType: b.stepType }); break; case "step-finish": t({ type: "step-finish", stepType: b.stepType }); break; case "finish": t({ type: "finish" }); break; case "error": t({ type: "error", content: b.error }); break; } } catch (b) { console.error("Error parsing chunk:", b, "Line:", $); } } return c || "No response received"; } async connectMCPServer(e, t, n) { const r = await fetch(`${this.apiUrl}/mcp/connect`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ serverName: e, transportType: t, transportConfig: n }) }); if (!r.ok) throw new Error(`Failed to connect to MCP server: ${r.status}`); } async getMCPServers() { const e = await fetch(`${this.apiUrl}/mcp/servers`); if (!e.ok) throw new Error(`Failed to get MCP servers: ${e.status}`); return (await e.json()).servers; } } function U() { return { async: !1, breaks: !1, extensions: null, gfm: !0, hooks: null, pedantic: !1, renderer: null, silent: !1, tokenizer: null, walkTokens: null }; } var z = U(); function le(s) { z = s; } var I = { exec: () => null }; function m(s, e = "") { let t = typeof s == "string" ? s : s.source; const n = { replace: (r, i) => { let c = typeof i == "string" ? i : i.source; return c = c.replace(w.caret, "$1"), t = t.replace(r, c), n; }, getRegex: () => new RegExp(t, e) }; return n; } var w = { codeRemoveIndent: /^(?: {1,4}| {0,3}\t)/gm, outputLinkReplace: /\\([\[\]])/g, indentCodeCompensation: /^(\s+)(?:```)/, beginningSpace: /^\s+/, endingHash: /#$/, startingSpaceChar: /^ /, endingSpaceChar: / $/, nonSpaceChar: /[^ ]/, newLineCharGlobal: /\n/g, tabCharGlobal: /\t/g, multipleSpaceGlobal: /\s+/g, blankLine: /^[ \t]*$/, doubleBlankLine: /\n[ \t]*\n[ \t]*$/, blockquoteStart: /^ {0,3}>/, blockquoteSetextReplace: /\n {0,3}((?:=+|-+) *)(?=\n|$)/g, blockquoteSetextReplace2: /^ {0,3}>[ \t]?/gm, listReplaceTabs: /^\t+/, listReplaceNesting: /^ {1,4}(?=( {4})*[^ ])/g, listIsTask: /^\[[ xX]\] /, listReplaceTask: /^\[[ xX]\] +/, anyLine: /\n.*\n/, hrefBrackets: /^<(.*)>$/, tableDelimiter: /[:|]/, tableAlignChars: /^\||\| *$/g, tableRowBlankLine: /\n[ \t]*$/, tableAlignRight: /^ *-+: *$/, tableAlignCenter: /^ *:-+: *$/, tableAlignLeft: /^ *:-+ *$/, startATag: /^/i, startPreScriptTag: /^<(pre|code|kbd|script)(\s|>)/i, endPreScriptTag: /^<\/(pre|code|kbd|script)(\s|>)/i, startAngleBracket: /^$/, pedanticHrefTitle: /^([^'"]*[^\s])\s+(['"])(.*)\2/, unicodeAlphaNumeric: /[\p{L}\p{N}]/u, escapeTest: /[&<>"']/, escapeReplace: /[&<>"']/g, escapeTestNoEncode: /[<>"']|&(?!(#\d{1,7}|#[Xx][a-fA-F0-9]{1,6}|\w+);)/, escapeReplaceNoEncode: /[<>"']|&(?!(#\d{1,7}|#[Xx][a-fA-F0-9]{1,6}|\w+);)/g, unescapeTest: /&(#(?:\d+)|(?:#x[0-9A-Fa-f]+)|(?:\w+));?/ig, caret: /(^|[^\[])\^/g, percentDecode: /%25/g, findPipe: /\|/g, splitPipe: / \|/, slashPipe: /\\\|/g, carriageReturn: /\r\n|\r/g, spaceLine: /^ +$/gm, notSpaceStart: /^\S*/, endingNewline: /\n$/, listItemRegex: (s) => new RegExp(`^( {0,3}${s})((?:[ ][^\\n]*)?(?:\\n|$))`), nextBulletRegex: (s) => new RegExp(`^ {0,${Math.min(3, s - 1)}}(?:[*+-]|\\d{1,9}[.)])((?:[ ][^\\n]*)?(?:\\n|$))`), hrRegex: (s) => new RegExp(`^ {0,${Math.min(3, s - 1)}}((?:- *){3,}|(?:_ *){3,}|(?:\\* *){3,})(?:\\n+|$)`), fencesBeginRegex: (s) => new RegExp(`^ {0,${Math.min(3, s - 1)}}(?:\`\`\`|~~~)`), headingBeginRegex: (s) => new RegExp(`^ {0,${Math.min(3, s - 1)}}#`), htmlBeginRegex: (s) => new RegExp(`^ {0,${Math.min(3, s - 1)}}<(?:[a-z].*>|!--)`, "i") }, ve = /^(?:[ \t]*(?:\n|$))+/, $e = /^((?: {4}| {0,3}\t)[^\n]+(?:\n(?:[ \t]*(?:\n|$))*)?)+/, Se = /^ {0,3}(`{3,}(?=[^`\n]*(?:\n|$))|~{3,})([^\n]*)(?:\n|$)(?:|([\s\S]*?)(?:\n|$))(?: {0,3}\1[~`]* *(?=\n|$)|$)/, P = /^ {0,3}((?:-[\t ]*){3,}|(?:_[ \t]*){3,}|(?:\*[ \t]*){3,})(?:\n+|$)/, Re = /^ {0,3}(#{1,6})(?=\s|$)(.*)(?:\n+|$)/, Q = /(?:[*+-]|\d{1,9}[.)])/, ce = /^(?!bull |blockCode|fences|blockquote|heading|html|table)((?:.|\n(?!\s*?\n|bull |blockCode|fences|blockquote|heading|html|table))+?)\n {0,3}(=+|-+) *(?:\n+|$)/, pe = m(ce).replace(/bull/g, Q).replace(/blockCode/g, /(?: {4}| {0,3}\t)/).replace(/fences/g, / {0,3}(?:`{3,}|~{3,})/).replace(/blockquote/g, / {0,3}>/).replace(/heading/g, / {0,3}#{1,6}/).replace(/html/g, / {0,3}<[^\n>]+>\n/).replace(/\|table/g, "").getRegex(), Te = m(ce).replace(/bull/g, Q).replace(/blockCode/g, /(?: {4}| {0,3}\t)/).replace(/fences/g, / {0,3}(?:`{3,}|~{3,})/).replace(/blockquote/g, / {0,3}>/).replace(/heading/g, / {0,3}#{1,6}/).replace(/html/g, / {0,3}<[^\n>]+>\n/).replace(/table/g, / {0,3}\|?(?:[:\- ]*\|)+[\:\- ]*\n/).getRegex(), F = /^([^\n]+(?:\n(?!hr|heading|lheading|blockquote|fences|list|html|table| +\n)[^\n]+)*)/, ze = /^[^\n]+/, J = /(?!\s*\])(?:\\.|[^\[\]\\])+/, Ce = m(/^ {0,3}\[(label)\]: *(?:\n[ \t]*)?([^<\s][^\s]*|<.*?>)(?:(?: +(?:\n[ \t]*)?| *\n[ \t]*)(title))? *(?:\n+|$)/).replace("label", J).replace("title", /(?:"(?:\\"?|[^"\\])*"|'[^'\n]*(?:\n[^'\n]+)*\n?'|\([^()]*\))/).getRegex(), Ae = m(/^( {0,3}bull)([ \t][^\n]+?)?(?:\n|$)/).replace(/bull/g, Q).getRegex(), N = "address|article|aside|base|basefont|blockquote|body|caption|center|col|colgroup|dd|details|dialog|dir|div|dl|dt|fieldset|figcaption|figure|footer|form|frame|frameset|h[1-6]|head|header|hr|html|iframe|legend|li|link|main|menu|menuitem|meta|nav|noframes|ol|optgroup|option|p|param|search|section|summary|table|tbody|td|tfoot|th|thead|title|tr|track|ul", X = /|$))/, _e = m( "^ {0,3}(?:<(script|pre|style|textarea)[\\s>][\\s\\S]*?(?:[^\\n]*\\n+|$)|comment[^\\n]*(\\n+|$)|<\\?[\\s\\S]*?(?:\\?>\\n*|$)|\\n*|$)|\\n*|$)|)[\\s\\S]*?(?:(?:\\n[ ]*)+\\n|$)|<(?!script|pre|style|textarea)([a-z][\\w-]*)(?:attribute)*? */?>(?=[ \\t]*(?:\\n|$))[\\s\\S]*?(?:(?:\\n[ ]*)+\\n|$)|(?=[ \\t]*(?:\\n|$))[\\s\\S]*?(?:(?:\\n[ ]*)+\\n|$))", "i" ).replace("comment", X).replace("tag", N).replace("attribute", / +[a-zA-Z:_][\w.:-]*(?: *= *"[^"\n]*"| *= *'[^'\n]*'| *= *[^\s"'=<>`]+)?/).getRegex(), he = m(F).replace("hr", P).replace("heading", " {0,3}#{1,6}(?:\\s|$)").replace("|lheading", "").replace("|table", "").replace("blockquote", " {0,3}>").replace("fences", " {0,3}(?:`{3,}(?=[^`\\n]*\\n)|~{3,})[^\\n]*\\n").replace("list", " {0,3}(?:[*+-]|1[.)]) ").replace("html", ")|<(?:script|pre|style|textarea|!--)").replace("tag", N).getRegex(), Ie = m(/^( {0,3}> ?(paragraph|[^\n]*)(?:\n|$))+/).replace("paragraph", he).getRegex(), W = { blockquote: Ie, code: $e, def: Ce, fences: Se, heading: Re, hr: P, html: _e, lheading: pe, list: Ae, newline: ve, paragraph: he, table: I, text: ze }, ne = m( "^ *([^\\n ].*)\\n {0,3}((?:\\| *)?:?-+:? *(?:\\| *:?-+:? *)*(?:\\| *)?)(?:\\n((?:(?! *\\n|hr|heading|blockquote|code|fences|list|html).*(?:\\n|$))*)\\n*|$)" ).replace("hr", P).replace("heading", " {0,3}#{1,6}(?:\\s|$)").replace("blockquote", " {0,3}>").replace("code", "(?: {4}| {0,3} )[^\\n]").replace("fences", " {0,3}(?:`{3,}(?=[^`\\n]*\\n)|~{3,})[^\\n]*\\n").replace("list", " {0,3}(?:[*+-]|1[.)]) ").replace("html", ")|<(?:script|pre|style|textarea|!--)").replace("tag", N).getRegex(), Pe = { ...W, lheading: Te, table: ne, paragraph: m(F).replace("hr", P).replace("heading", " {0,3}#{1,6}(?:\\s|$)").replace("|lheading", "").replace("table", ne).replace("blockquote", " {0,3}>").replace("fences", " {0,3}(?:`{3,}(?=[^`\\n]*\\n)|~{3,})[^\\n]*\\n").replace("list", " {0,3}(?:[*+-]|1[.)]) ").replace("html", ")|<(?:script|pre|style|textarea|!--)").replace("tag", N).getRegex() }, Le = { ...W, html: m( `^ *(?:comment *(?:\\n|\\s*$)|<(tag)[\\s\\S]+? *(?:\\n{2,}|\\s*$)|\\s]*)*?/?> *(?:\\n{2,}|\\s*$))` ).replace("comment", X).replace(/tag/g, "(?!(?:a|em|strong|small|s|cite|q|dfn|abbr|data|time|code|var|samp|kbd|sub|sup|i|b|u|mark|ruby|rt|rp|bdi|bdo|span|br|wbr|ins|del|img)\\b)\\w+(?!:|[^\\w\\s@]*@)\\b").getRegex(), def: /^ *\[([^\]]+)\]: *]+)>?(?: +(["(][^\n]+[")]))? *(?:\n+|$)/, heading: /^(#{1,6})(.*)(?:\n+|$)/, fences: I, // fences not supported lheading: /^(.+?)\n {0,3}(=+|-+) *(?:\n+|$)/, paragraph: m(F).replace("hr", P).replace("heading", ` *#{1,6} *[^ ]`).replace("lheading", pe).replace("|table", "").replace("blockquote", " {0,3}>").replace("|fences", "").replace("|list", "").replace("|html", "").replace("|tag", "").getRegex() }, Be = /^\\([!"#$%&'()*+,\-./:;<=>?@\[\]\\^_`{|}~])/, Me = /^(`+)([^`]|[^`][\s\S]*?[^`])\1(?!`)/, ue = /^( {2,}|\\)\n(?!\s*$)/, Ee = /^(`+|[^`])(?:(?= {2,}\n)|[\s\S]*?(?:(?=[\\]*?>/g, fe = /^(?:\*+(?:((?!\*)punct)|[^\s*]))|^_+(?:((?!_)punct)|([^\s_]))/, Ze = m(fe, "u").replace(/punct/g, D).getRegex(), He = m(fe, "u").replace(/punct/g, de).getRegex(), me = "^[^_*]*?__[^_*]*?\\*[^_*]*?(?=__)|[^*]+(?=[^*])|(?!\\*)punct(\\*+)(?=[\\s]|$)|notPunctSpace(\\*+)(?!\\*)(?=punctSpace|$)|(?!\\*)punctSpace(\\*+)(?=notPunctSpace)|[\\s](\\*+)(?!\\*)(?=punct)|(?!\\*)punct(\\*+)(?!\\*)(?=punct)|notPunctSpace(\\*+)(?=notPunctSpace)", je = m(me, "gu").replace(/notPunctSpace/g, ge).replace(/punctSpace/g, K).replace(/punct/g, D).getRegex(), Ge = m(me, "gu").replace(/notPunctSpace/g, De).replace(/punctSpace/g, Ne).replace(/punct/g, de).getRegex(), Ue = m( "^[^_*]*?\\*\\*[^_*]*?_[^_*]*?(?=\\*\\*)|[^_]+(?=[^_])|(?!_)punct(_+)(?=[\\s]|$)|notPunctSpace(_+)(?!_)(?=punctSpace|$)|(?!_)punctSpace(_+)(?=notPunctSpace)|[\\s](_+)(?!_)(?=punct)|(?!_)punct(_+)(?!_)(?=punct)", "gu" ).replace(/notPunctSpace/g, ge).replace(/punctSpace/g, K).replace(/punct/g, D).getRegex(), Qe = m(/\\(punct)/, "gu").replace(/punct/g, D).getRegex(), Fe = m(/^<(scheme:[^\s\x00-\x1f<>]*|email)>/).replace("scheme", /[a-zA-Z][a-zA-Z0-9+.-]{1,31}/).replace("email", /[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+(@)[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+(?![-_])/).getRegex(), Je = m(X).replace("(?:-->|$)", "-->").getRegex(), Xe = m( "^comment|^|^<[a-zA-Z][\\w-]*(?:attribute)*?\\s*/?>|^<\\?[\\s\\S]*?\\?>|^|^" ).replace("comment", Je).replace("attribute", /\s+[a-zA-Z:_][\w.:-]*(?:\s*=\s*"[^"]*"|\s*=\s*'[^']*'|\s*=\s*[^\s"'=<>`]+)?/).getRegex(), M = /(?:\[(?:\\.|[^\[\]\\])*\]|\\.|`[^`]*`|[^\[\]\\`])*?/, We = m(/^!?\[(label)\]\(\s*(href)(?:(?:[ \t]*(?:\n[ \t]*)?)(title))?\s*\)/).replace("label", M).replace("href", /<(?:\\.|[^\n<>\\])+>|[^ \t\n\x00-\x1f]*/).replace("title", /"(?:\\"?|[^"\\])*"|'(?:\\'?|[^'\\])*'|\((?:\\\)?|[^)\\])*\)/).getRegex(), ke = m(/^!?\[(label)\]\[(ref)\]/).replace("label", M).replace("ref", J).getRegex(), xe = m(/^!?\[(ref)\](?:\[\])?/).replace("ref", J).getRegex(), Ke = m("reflink|nolink(?!\\()", "g").replace("reflink", ke).replace("nolink", xe).getRegex(), V = { _backpedal: I, // only used for GFM url anyPunctuation: Qe, autolink: Fe, blockSkip: Oe, br: ue, code: Me, del: I, emStrongLDelim: Ze, emStrongRDelimAst: je, emStrongRDelimUnd: Ue, escape: Be, link: We, nolink: xe, punctuation: qe, reflink: ke, reflinkSearch: Ke, tag: Xe, text: Ee, url: I }, Ve = { ...V, link: m(/^!?\[(label)\]\((.*?)\)/).replace("label", M).getRegex(), reflink: m(/^!?\[(label)\]\s*\[([^\]]*)\]/).replace("label", M).getRegex() }, H = { ...V, emStrongRDelimAst: Ge, emStrongLDelim: He, url: m(/^((?:ftp|https?):\/\/|www\.)(?:[a-zA-Z0-9\-]+\.?)+[^\s<]*|^email/, "i").replace("email", /[A-Za-z0-9._+-]+(@)[a-zA-Z0-9-_]+(?:\.[a-zA-Z0-9-_]*[a-zA-Z0-9])+(?![-_])/).getRegex(), _backpedal: /(?:[^?!.,:;*_'"~()&]+|\([^)]*\)|&(?![a-zA-Z0-9]+;$)|[?!.,:;*_'"~)]+(?!$))+/, del: /^(~~?)(?=[^\s~])((?:\\.|[^\\])*?(?:\\.|[^\s~\\]))\1(?=[^~]|$)/, text: /^([`~]+|[^`~])(?:(?= {2,}\n)|(?=[a-zA-Z0-9.!#$%&'*+\/=?_`{\|}~-]+@)|[\s\S]*?(?:(?=[\\": ">", '"': """, "'": "'" }, re = (s) => et[s]; function y(s, e) { if (e) { if (w.escapeTest.test(s)) return s.replace(w.escapeReplace, re); } else if (w.escapeTestNoEncode.test(s)) return s.replace(w.escapeReplaceNoEncode, re); return s; } function ie(s) { try { s = encodeURI(s).replace(w.percentDecode, "%"); } catch { return null; } return s; } function ae(s, e) { var i; const t = s.replace(w.findPipe, (c, a, p) => { let o = !1, l = a; for (; --l >= 0 && p[l] === "\\"; ) o = !o; return o ? "|" : " |"; }), n = t.split(w.splitPipe); let r = 0; if (n[0].trim() || n.shift(), n.length > 0 && !((i = n.at(-1)) != null && i.trim()) && n.pop(), e) if (n.length > e) n.splice(e); else for (; n.length < e; ) n.push(""); for (; r < n.length; r++) n[r] = n[r].trim().replace(w.slashPipe, "|"); return n; } function _(s, e, t) { const n = s.length; if (n === 0) return ""; let r = 0; for (; r < n && s.charAt(n - r - 1) === e; ) r++; return s.slice(0, n - r); } function tt(s, e) { if (s.indexOf(e[1]) === -1) return -1; let t = 0; for (let n = 0; n < s.length; n++) if (s[n] === "\\") n++; else if (s[n] === e[0]) t++; else if (s[n] === e[1] && (t--, t < 0)) return n; return t > 0 ? -2 : -1; } function oe(s, e, t, n, r) { const i = e.href, c = e.title || null, a = s[1].replace(r.other.outputLinkReplace, "$1"); n.state.inLink = !0; const p = { type: s[0].charAt(0) === "!" ? "image" : "link", raw: t, href: i, title: c, text: a, tokens: n.inlineTokens(a) }; return n.state.inLink = !1, p; } function st(s, e, t) { const n = s.match(t.other.indentCodeCompensation); if (n === null) return e; const r = n[1]; return e.split(` `).map((i) => { const c = i.match(t.other.beginningSpace); if (c === null) return i; const [a] = c; return a.length >= r.length ? i.slice(r.length) : i; }).join(` `); } var E = class { // set by the lexer constructor(s) { g(this, "options"); g(this, "rules"); // set by the lexer g(this, "lexer"); this.options = s || z; } space(s) { const e = this.rules.block.newline.exec(s); if (e && e[0].length > 0) return { type: "space", raw: e[0] }; } code(s) { const e = this.rules.block.code.exec(s); if (e) { const t = e[0].replace(this.rules.other.codeRemoveIndent, ""); return { type: "code", raw: e[0], codeBlockStyle: "indented", text: this.options.pedantic ? t : _(t, ` `) }; } } fences(s) { const e = this.rules.block.fences.exec(s); if (e) { const t = e[0], n = st(t, e[3] || "", this.rules); return { type: "code", raw: t, lang: e[2] ? e[2].trim().replace(this.rules.inline.anyPunctuation, "$1") : e[2], text: n }; } } heading(s) { const e = this.rules.block.heading.exec(s); if (e) { let t = e[2].trim(); if (this.rules.other.endingHash.test(t)) { const n = _(t, "#"); (this.options.pedantic || !n || this.rules.other.endingSpaceChar.test(n)) && (t = n.trim()); } return { type: "heading", raw: e[0], depth: e[1].length, text: t, tokens: this.lexer.inline(t) }; } } hr(s) { const e = this.rules.block.hr.exec(s); if (e) return { type: "hr", raw: _(e[0], ` `) }; } blockquote(s) { const e = this.rules.block.blockquote.exec(s); if (e) { let t = _(e[0], ` `).split(` `), n = "", r = ""; const i = []; for (; t.length > 0; ) { let c = !1; const a = []; let p; for (p = 0; p < t.length; p++) if (this.rules.other.blockquoteStart.test(t[p])) a.push(t[p]), c = !0; else if (!c) a.push(t[p]); else break; t = t.slice(p); const o = a.join(` `), l = o.replace(this.rules.other.blockquoteSetextReplace, ` $1`).replace(this.rules.other.blockquoteSetextReplace2, ""); n = n ? `${n} ${o}` : o, r = r ? `${r} ${l}` : l; const d = this.lexer.state.top; if (this.lexer.state.top = !0, this.lexer.blockTokens(l, i, !0), this.lexer.state.top = d, t.length === 0) break; const h = i.at(-1); if ((h == null ? void 0 : h.type) === "code") break; if ((h == null ? void 0 : h.type) === "blockquote") { const k = h, u = k.raw + ` ` + t.join(` `), x = this.blockquote(u); i[i.length - 1] = x, n = n.substring(0, n.length - k.raw.length) + x.raw, r = r.substring(0, r.length - k.text.length) + x.text; break; } else if ((h == null ? void 0 : h.type) === "list") { const k = h, u = k.raw + ` ` + t.join(` `), x = this.list(u); i[i.length - 1] = x, n = n.substring(0, n.length - h.raw.length) + x.raw, r = r.substring(0, r.length - k.raw.length) + x.raw, t = u.substring(i.at(-1).raw.length).split(` `); continue; } } return { type: "blockquote", raw: n, tokens: i, text: r }; } } list(s) { let e = this.rules.block.list.exec(s); if (e) { let t = e[1].trim(); const n = t.length > 1, r = { type: "list", raw: "", ordered: n, start: n ? +t.slice(0, -1) : "", loose: !1, items: [] }; t = n ? `\\d{1,9}\\${t.slice(-1)}` : `\\${t}`, this.options.pedantic && (t = n ? t : "[*+-]"); const i = this.rules.other.listItemRegex(t); let c = !1; for (; s; ) { let p = !1, o = "", l = ""; if (!(e = i.exec(s)) || this.rules.block.hr.test(s)) break; o = e[0], s = s.substring(o.length); let d = e[2].split(` `, 1)[0].replace(this.rules.other.listReplaceTabs, ($) => " ".repeat(3 * $.length)), h = s.split(` `, 1)[0], k = !d.trim(), u = 0; if (this.options.pedantic ? (u = 2, l = d.trimStart()) : k ? u = e[1].length + 1 : (u = e[2].search(this.rules.other.nonSpaceChar), u = u > 4 ? 1 : u, l = d.slice(u), u += e[1].length), k && this.rules.other.blankLine.test(h) && (o += h + ` `, s = s.substring(h.length + 1), p = !0), !p) { const $ = this.rules.other.nextBulletRegex(u), b = this.rules.other.hrRegex(u), ee = this.rules.other.fencesBeginRegex(u), te = this.rules.other.headingBeginRegex(u), be = this.rules.other.htmlBeginRegex(u); for (; s; ) { const O = s.split(` `, 1)[0]; let C; if (h = O, this.options.pedantic ? (h = h.replace(this.rules.other.listReplaceNesting, " "), C = h) : C = h.replace(this.rules.other.tabCharGlobal, " "), ee.test(h) || te.test(h) || be.test(h) || $.test(h) || b.test(h)) break; if (C.search(this.rules.other.nonSpaceChar) >= u || !h.trim()) l += ` ` + C.slice(u); else { if (k || d.replace(this.rules.other.tabCharGlobal, " ").search(this.rules.other.nonSpaceChar) >= 4 || ee.test(d) || te.test(d) || b.test(d)) break; l += ` ` + h; } !k && !h.trim() && (k = !0), o += O + ` `, s = s.substring(O.length + 1), d = C.slice(u); } } r.loose || (c ? r.loose = !0 : this.rules.other.doubleBlankLine.test(o) && (c = !0)); let x = null, v; this.options.gfm && (x = this.rules.other.listIsTask.exec(l), x && (v = x[0] !== "[ ] ", l = l.replace(this.rules.other.listReplaceTask, ""))), r.items.push({ type: "list_item", raw: o, task: !!x, checked: v, loose: !1, text: l, tokens: [] }), r.raw += o; } const a = r.items.at(-1); if (a) a.raw = a.raw.trimEnd(), a.text = a.text.trimEnd(); else return; r.raw = r.raw.trimEnd(); for (let p = 0; p < r.items.length; p++) if (this.lexer.state.top = !1, r.items[p].tokens = this.lexer.blockTokens(r.items[p].text, []), !r.loose) { const o = r.items[p].tokens.filter((d) => d.type === "space"), l = o.length > 0 && o.some((d) => this.rules.other.anyLine.test(d.raw)); r.loose = l; } if (r.loose) for (let p = 0; p < r.items.length; p++) r.items[p].loose = !0; return r; } } html(s) { const e = this.rules.block.html.exec(s); if (e) return { type: "html", block: !0, raw: e[0], pre: e[1] === "pre" || e[1] === "script" || e[1] === "style", text: e[0] }; } def(s) { const e = this.rules.block.def.exec(s); if (e) { const t = e[1].toLowerCase().replace(this.rules.other.multipleSpaceGlobal, " "), n = e[2] ? e[2].replace(this.rules.other.hrefBrackets, "$1").replace(this.rules.inline.anyPunctuation, "$1") : "", r = e[3] ? e[3].substring(1, e[3].length - 1).replace(this.rules.inline.anyPunctuation, "$1") : e[3]; return { type: "def", tag: t, raw: e[0], href: n, title: r }; } } table(s) { var c; const e = this.rules.block.table.exec(s); if (!e || !this.rules.other.tableDelimiter.test(e[2])) return; const t = ae(e[1]), n = e[2].replace(this.rules.other.tableAlignChars, "").split("|"), r = (c = e[3]) != null && c.trim() ? e[3].replace(this.rules.other.tableRowBlankLine, "").split(` `) : [], i = { type: "table", raw: e[0], header: [], align: [], rows: [] }; if (t.length === n.length) { for (const a of n) this.rules.other.tableAlignRight.test(a) ? i.align.push("right") : this.rules.other.tableAlignCenter.test(a) ? i.align.push("center") : this.rules.other.tableAlignLeft.test(a) ? i.align.push("left") : i.align.push(null); for (let a = 0; a < t.length; a++) i.header.push({ text: t[a], tokens: this.lexer.inline(t[a]), header: !0, align: i.align[a] }); for (const a of r) i.rows.push(ae(a, i.header.length).map((p, o) => ({ text: p, tokens: this.lexer.inline(p), header: !1, align: i.align[o] }))); return i; } } lheading(s) { const e = this.rules.block.lheading.exec(s); if (e) return { type: "heading", raw: e[0], depth: e[2].charAt(0) === "=" ? 1 : 2, text: e[1], tokens: this.lexer.inline(e[1]) }; } paragraph(s) { const e = this.rules.block.paragraph.exec(s); if (e) { const t = e[1].charAt(e[1].length - 1) === ` ` ? e[1].slice(0, -1) : e[1]; return { type: "paragraph", raw: e[0], text: t, tokens: this.lexer.inline(t) }; } } text(s) { const e = this.rules.block.text.exec(s); if (e) return { type: "text", raw: e[0], text: e[0], tokens: this.lexer.inline(e[0]) }; } escape(s) { const e = this.rules.inline.escape.exec(s); if (e) return { type: "escape", raw: e[0], text: e[1] }; } tag(s) { const e = this.rules.inline.tag.exec(s); if (e) return !this.lexer.state.inLink && this.rules.other.startATag.test(e[0]) ? this.lexer.state.inLink = !0 : this.lexer.state.inLink && this.rules.other.endATag.test(e[0]) && (this.lexer.state.inLink = !1), !this.lexer.state.inRawBlock && this.rules.other.startPreScriptTag.test(e[0]) ? this.lexer.state.inRawBlock = !0 : this.lexer.state.inRawBlock && this.rules.other.endPreScriptTag.test(e[0]) && (this.lexer.state.inRawBlock = !1), { type: "html", raw: e[0], inLink: this.lexer.state.inLink, inRawBlock: this.lexer.state.inRawBlock, block: !1, text: e[0] }; } link(s) { const e = this.rules.inline.link.exec(s); if (e) { const t = e[2].trim(); if (!this.options.pedantic && this.rules.other.startAngleBracket.test(t)) { if (!this.rules.other.endAngleBracket.test(t)) return; const i = _(t.slice(0, -1), "\\"); if ((t.length - i.length) % 2 === 0) return; } else { const i = tt(e[2], "()"); if (i === -2) return; if (i > -1) { const a = (e[0].indexOf("!") === 0 ? 5 : 4) + e[1].length + i; e[2] = e[2].substring(0, i), e[0] = e[0].substring(0, a).trim(), e[3] = ""; } } let n = e[2], r = ""; if (this.options.pedantic) { const i = this.rules.other.pedanticHrefTitle.exec(n); i && (n = i[1], r = i[3]); } else r = e[3] ? e[3].slice(1, -1) : ""; return n = n.trim(), this.rules.other.startAngleBracket.test(n) && (this.options.pedantic && !this.rules.other.endAngleBracket.test(t) ? n = n.slice(1) : n = n.slice(1, -1)), oe(e, { href: n && n.replace(this.rules.inline.anyPunctuation, "$1"), title: r && r.replace(this.rules.inline.anyPunctuation, "$1") }, e[0], this.lexer, this.rules); } } reflink(s, e) { let t; if ((t = this.rules.inline.reflink.exec(s)) || (t = this.rules.inline.nolink.exec(s))) { const n = (t[2] || t[1]).replace(this.rules.other.multipleSpaceGlobal, " "), r = e[n.toLowerCase()]; if (!r) { const i = t[0].charAt(0); return { type: "text", raw: i, text: i }; } return oe(t, r, t[0], this.lexer, this.rules); } } emStrong(s, e, t = "") { let n = this.rules.inline.emStrongLDelim.exec(s); if (!n || n[3] && t.match(this.rules.other.unicodeAlphaNumeric)) return; if (!(n[1] || n[2] || "") || !t || this.rules.inline.punctuation.exec(t)) { const i = [...n[0]].length - 1; let c, a, p = i, o = 0; const l = n[0][0] === "*" ? this.rules.inline.emStrongRDelimAst : this.rules.inline.emStrongRDelimUnd; for (l.lastIndex = 0, e = e.slice(-1 * s.length + i); (n = l.exec(e)) != null; ) { if (c = n[1] || n[2] || n[3] || n[4] || n[5] || n[6], !c) continue; if (a = [...c].length, n[3] || n[4]) { p += a; continue; } else if ((n[5] || n[6]) && i % 3 && !((i + a) % 3)) { o += a; continue; } if (p -= a, p > 0) continue; a = Math.min(a, a + p + o); const d = [...n[0]][0].length, h = s.slice(0, i + n.index + d + a); if (Math.min(i, a) % 2) { const u = h.slice(1, -1); return { type: "em", raw: h, text: u, tokens: this.lexer.inlineTokens(u) }; } const k = h.slice(2, -2); return { type: "strong", raw: h, text: k, tokens: this.lexer.inlineTokens(k) }; } } } codespan(s) { const e = this.rules.inline.code.exec(s); if (e) { let t = e[2].replace(this.rules.other.newLineCharGlobal, " "); const n = this.rules.other.nonSpaceChar.test(t), r = this.rules.other.startingSpaceChar.test(t) && this.rules.other.endingSpaceChar.test(t); return n && r && (t = t.substring(1, t.length - 1)), { type: "codespan", raw: e[0], text: t }; } } br(s) { const e = this.rules.inline.br.exec(s); if (e) return { type: "br", raw: e[0] }; } del(s) { const e = this.rules.inline.del.exec(s); if (e) return { type: "del", raw: e[0], text: e[2], tokens: this.lexer.inlineTokens(e[2]) }; } autolink(s) { const e = this.rules.inline.autolink.exec(s); if (e) { let t, n; return e[2] === "@" ? (t = e[1], n = "mailto:" + t) : (t = e[1], n = t), { type: "link", raw: e[0], text: t, href: n, tokens: [ { type: "text", raw: t, text: t } ] }; } } url(s) { var t; let e; if (e = this.rules.inline.url.exec(s)) { let n, r; if (e[2] === "@") n = e[0], r = "mailto:" + n; else { let i; do i = e[0], e[0] = ((t = this.rules.inline._backpedal.exec(e[0])) == null ? void 0 : t[0]) ?? ""; while (i !== e[0]); n = e[0], e[1] === "www." ? r = "http://" + e[0] : r = e[0]; } return { type: "link", raw: e[0], text: n, href: r, tokens: [ { type: "text", raw: n, text: n } ] }; } } inlineText(s) { const e = this.rules.inline.text.exec(s); if (e) { const t = this.lexer.state.inRawBlock; return { type: "text", raw: e[0], text: e[0], escaped: t }; } } }, S = class j { constructor(e) { g(this, "tokens"); g(this, "options"); g(this, "state"); g(this, "tokenizer"); g(this, "inlineQueue"); this.tokens = [], this.tokens.links = /* @__PURE__ */ Object.create(null), this.options = e || z, this.options.tokenizer = this.options.tokenizer || new E(), this.tokenizer = this.options.tokenizer, this.tokenizer.options = this.options, this.tokenizer.lexer = this, this.inlineQueue = [], this.state = { inLink: !1, inRawBlock: !1, top: !0 }; const t = { other: w, block: L.normal, inline: A.normal }; this.options.pedantic ? (t.block = L.pedantic, t.inline = A.pedantic) : this.options.gfm && (t.block = L.gfm, this.options.breaks ? t.inline = A.breaks : t.inline = A.gfm), this.tokenizer.rules = t; } /** * Expose Rules */ static get rules() { return { block: L, inline: A }; } /** * Static Lex Method */ static lex(e, t) { return new j(t).lex(e); } /** * Static Lex Inline Method */ static lexInline(e, t) { return new j(t).inlineTokens(e); } /** * Preprocessing */ lex(e) { e = e.replace(w.carriageReturn, ` `), this.blockTokens(e, this.tokens); for (let t = 0; t < this.inlineQueue.length; t++) { const n = this.inlineQueue[t]; this.inlineTokens(n.src, n.tokens); } return this.inlineQueue = [], this.tokens; } blockTokens(e, t = [], n = !1) { var r, i, c; for (this.options.pedantic && (e = e.replace(w.tabCharGlobal, " ").replace(w.spaceLine, "")); e; ) { let a; if ((i = (r = this.options.extensions) == null ? void 0 : r.block) != null && i.some((o) => (a = o.call({ lexer: this }, e, t)) ? (e = e.substring(a.raw.length), t.push(a), !0) : !1)) continue; if (a = this.tokenizer.space(e)) { e = e.substring(a.raw.length); const o = t.at(-1); a.raw.length === 1 && o !== void 0 ? o.raw += ` ` : t.push(a); continue; } if (a = this.tokenizer.code(e)) { e = e.substring(a.raw.length); const o = t.at(-1); (o == null ? void 0 : o.type) === "paragraph" || (o == null ? void 0 : o.type) === "text" ? (o.raw += ` ` + a.raw, o.text += ` ` + a.text, this.inlineQueue.at(-1).src = o.text) : t.push(a); continue; } if (a = this.tokenizer.fences(e)) { e = e.substring(a.raw.length), t.push(a); continue; } if (a = this.tokenizer.heading(e)) { e = e.substring(a.raw.length), t.push(a); continue; } if (a = this.tokenizer.hr(e)) { e = e.substring(a.raw.length), t.push(a); continue; } if (a = this.tokenizer.blockquote(e)) { e = e.substring(a.raw.length), t.push(a); continue; } if (a = this.tokenizer.list(e)) { e = e.substring(a.raw.length), t.push(a); continue; } if (a = this.tokenizer.html(e)) { e = e.substring(a.raw.length), t.push(a); continue; } if (a = this.tokenizer.def(e)) { e = e.substring(a.raw.length); const o = t.at(-1); (o == null ? void 0 : o.type) === "paragraph" || (o == null ? void 0 : o.type) === "text" ? (o.raw += ` ` + a.raw, o.text += ` ` + a.raw, this.inlineQueue.at(-1).src = o.text) : this.tokens.links[a.tag] || (this.tokens.links[a.tag] = { href: a.href, title: a.title }); continue; } if (a = this.tokenizer.table(e)) { e = e.substring(a.raw.length), t.push(a); continue; } if (a = this.tokenizer.lheading(e)) { e = e.substring(a.raw.length), t.push(a); continue; } let p = e; if ((c = this.options.extensions) != null && c.startBlock) { let o = 1 / 0; const l = e.slice(1); let d; this.options.extensions.startBlock.forEach((h) => { d = h.call({ lexer: this }, l), typeof d == "number" && d >= 0 && (o = Math.min(o, d)); }), o < 1 / 0 && o >= 0 && (p = e.substring(0, o + 1)); } if (this.state.top && (a = this.tokenizer.paragraph(p))) { const o = t.at(-1); n && (o == null ? void 0 : o.type) === "paragraph" ? (o.raw += ` ` + a.raw, o.text += ` ` + a.text, this.inlineQueue.pop(), this.inlineQueue.at(-1).src = o.text) : t.push(a), n = p.length !== e.length, e = e.substring(a.raw.length); continue; } if (a = this.tokenizer.text(e)) { e = e.substring(a.raw.length); const o = t.at(-1); (o == null ? void 0 : o.type) === "text" ? (o.raw += ` ` + a.raw, o.text += ` ` + a.text, this.inlineQueue.pop(), this.inlineQueue.at(-1).src = o.text) : t.push(a); continue; } if (e) { const o = "Infinite loop on byte: " + e.charCodeAt(0); if (this.options.silent) { console.error(o); break; } else throw new Error(o); } } return this.state.top = !0, t; } inline(e, t = []) { return this.inlineQueue.push({ src: e, tokens: t }), t; } /** * Lexing/Compiling */ inlineTokens(e, t = []) { var a, p, o; let n = e, r = null; if (this.tokens.links) { const l = Object.keys(this.tokens.links); if (l.length > 0) for (; (r = this.tokenizer.rules.inline.reflinkSearch.exec(n)) != null; ) l.includes(r[0].slice(r[0].lastIndexOf("[") + 1, -1)) && (n = n.slice(0, r.index) + "[" + "a".repeat(r[0].length - 2) + "]" + n.slice(this.tokenizer.rules.inline.reflinkSearch.lastIndex)); } for (; (r = this.tokenizer.rules.inline.anyPunctuation.exec(n)) != null; ) n = n.slice(0, r.index) + "++" + n.slice(this.tokenizer.rules.inline.anyPunctuation.lastIndex); for (; (r = this.tokenizer.rules.inline.blockSkip.exec(n)) != null; ) n = n.slice(0, r.index) + "[" + "a".repeat(r[0].length - 2) + "]" + n.slice(this.tokenizer.rules.inline.blockSkip.lastIndex); let i = !1, c = ""; for (; e; ) { i || (c = ""), i = !1; let l; if ((p = (a = this.options.extensions) == null ? void 0 : a.inline) != null && p.some((h) => (l = h.call({ lexer: this }, e, t)) ? (e = e.substring(l.raw.length), t.push(l), !0) : !1)) continue; if (l = this.tokenizer.escape(e)) { e = e.substring(l.raw.length), t.push(l); continue; } if (l = this.tokenizer.tag(e)) { e = e.substring(l.raw.length), t.push(l); continue; } if (l = this.tokenizer.link(e)) { e = e.substring(l.raw.length), t.push(l); continue; } if (l = this.tokenizer.reflink(e, this.tokens.links)) { e = e.substring(l.raw.length); const h = t.at(-1); l.type === "text" && (h == null ? void 0 : h.type) === "text" ? (h.raw += l.raw, h.text += l.text) : t.push(l); continue; } if (l = this.tokenizer.emStrong(e, n, c)) { e = e.substring(l.raw.length), t.push(l); continue; } if (l = this.tokenizer.codespan(e)) { e = e.substring(l.raw.length), t.push(l); continue; } if (l = this.tokenizer.br(e)) { e = e.substring(l.raw.length), t.push(l); continue; } if (l = this.tokenizer.del(e)) { e = e.substring(l.raw.length), t.push(l); continue; } if (l = this.tokenizer.autolink(e)) { e = e.substring(l.raw.length), t.push(l); continue; } if (!this.state.inLink && (l = this.tokenizer.url(e))) { e = e.substring(l.raw.length), t.push(l); continue; } let d = e; if ((o = this.options.extensions) != null && o.startInline) { let h = 1 / 0; const k = e.slice(1); let u; this.options.extensions.startInline.forEach((x) => { u = x.call({ lexer: this }, k), typeof u == "number" && u >= 0 && (h = Math.min(h, u)); }), h < 1 / 0 && h >= 0 && (d = e.substring(0, h + 1)); } if (l = this.tokenizer.inlineText(d)) { e = e.substring(l.raw.length), l.raw.slice(-1) !== "_" && (c = l.raw.slice(-1)), i = !0; const h = t.at(-1); (h == null ? void 0 : h.type) === "text" ? (h.raw += l.raw, h.text += l.text) : t.push(l); continue; } if (e) { const h = "Infinite loop on byte: " + e.charCodeAt(0); if (this.options.silent) { console.error(h); break; } else throw new Error(h); } } return t; } }, q = class { // set by the parser constructor(s) { g(this, "options"); g(this, "parser"); this.options = s || z; } space(s) { return ""; } code({ text: s, lang: e, escaped: t }) { var i; const n = (i = (e || "").match(w.notSpaceStart)) == null ? void 0 : i[0], r = s.replace(w.endingNewline, "") + ` `; return n ? '
' + (t ? r : y(r, !0)) + `
` : "
" + (t ? r : y(r, !0)) + `
`; } blockquote({ tokens: s }) { return `
${this.parser.parse(s)}
`; } html({ text: s }) { return s; } heading({ tokens: s, depth: e }) { return `${this.parser.parseInline(s)} `; } hr(s) { return `
`; } list(s) { const e = s.ordered, t = s.start; let n = ""; for (let c = 0; c < s.items.length; c++) { const a = s.items[c]; n += this.listitem(a); } const r = e ? "ol" : "ul", i = e && t !== 1 ? ' start="' + t + '"' : ""; return "<" + r + i + `> ` + n + " `; } listitem(s) { var t; let e = ""; if (s.task) { const n = this.checkbox({ checked: !!s.checked }); s.loose ? ((t = s.tokens[0]) == null ? void 0 : t.type) === "paragraph" ? (s.tokens[0].text = n + " " + s.tokens[0].text, s.tokens[0].tokens && s.tokens[0].tokens.length > 0 && s.tokens[0].tokens[0].type === "text" && (s.tokens[0].tokens[0].text = n + " " + y(s.tokens[0].tokens[0].text), s.tokens[0].tokens[0].escaped = !0)) : s.tokens.unshift({ type: "text", raw: n + " ", text: n + " ", escaped: !0 }) : e += n + " "; } return e += this.parser.parse(s.tokens, !!s.loose), `
  • ${e}
  • `; } checkbox({ checked: s }) { return "'; } paragraph({ tokens: s }) { return `

    ${this.parser.parseInline(s)}

    `; } table(s) { let e = "", t = ""; for (let r = 0; r < s.header.length; r++) t += this.tablecell(s.header[r]); e += this.tablerow({ text: t }); let n = ""; for (let r = 0; r < s.rows.length; r++) { const i = s.rows[r]; t = ""; for (let c = 0; c < i.length; c++) t += this.tablecell(i[c]); n += this.tablerow({ text: t }); } return n && (n = `${n}`), ` ` + e + ` ` + n + `
    `; } tablerow({ text: s }) { return ` ${s} `; } tablecell(s) { const e = this.parser.parseInline(s.tokens), t = s.header ? "th" : "td"; return (s.align ? `<${t} align="${s.align}">` : `<${t}>`) + e + ` `; } /** * span level renderer */ strong({ tokens: s }) { return `${this.parser.parseInline(s)}`; } em({ tokens: s }) { return `${this.parser.parseInline(s)}`; } codespan({ text: s }) { return `${y(s, !0)}`; } br(s) { return "
    "; } del({ tokens: s }) { return `${this.parser.parseInline(s)}`; } link({ href: s, title: e, tokens: t }) { const n = this.parser.parseInline(t), r = ie(s); if (r === null) return n; s = r; let i = '
    ", i; } image({ href: s, title: e, text: t, tokens: n }) { n && (t = this.parser.parseInline(n, this.parser.textRenderer)); const r = ie(s); if (r === null) return y(t); s = r; let i = `${t} { const p = c[a].flat(1 / 0); t = t.concat(this.walkTokens(p, e)); }) : c.tokens && (t = t.concat(this.walkTokens(c.tokens, e))); } } return t; } use(...s) { const e = this.defaults.extensions || { renderers: {}, childTokens: {} }; return s.forEach((t) => { const n = { ...t }; if (n.async = this.defaults.async || n.async || !1, t.extensions && (t.extensions.forEach((r) => { if (!r.name) throw new Error("extension name required"); if ("renderer" in r) { const i = e.renderers[r.name]; i ? e.renderers[r.name] = function(...c) { let a = r.renderer.apply(this, c); return a === !1 && (a = i.apply(this, c)), a; } : e.renderers[r.name] = r.renderer; } if ("tokenizer" in r) { if (!r.level || r.level !== "block" && r.level !== "inline") throw new Error("extension level must be 'block' or 'inline'"); const i = e[r.level]; i ? i.unshift(r.tokenizer) : e[r.level] = [r.tokenizer], r.start && (r.level === "block" ? e.startBlock ? e.startBlock.push(r.start) : e.startBlock = [r.start] : r.level === "inline" && (e.startInline ? e.startInline.push(r.start) : e.startInline = [r.start])); } "childTokens" in r && r.childTokens && (e.childTokens[r.name] = r.childTokens); }), n.extensions = e), t.renderer) { const r = this.defaults.renderer || new q(this.defaults); for (const i in t.renderer) { if (!(i in r)) throw new Error(`renderer '${i}' does not exist`); if (["options", "parser"].includes(i)) continue; const c = i, a = t.renderer[c], p = r[c]; r[c] = (...o) => { let l = a.apply(r, o); return l === !1 && (l = p.apply(r, o)), l || ""; }; } n.renderer = r; } if (t.tokenizer) { const r = this.defaults.tokenizer || new E(this.defaults); for (const i in t.tokenizer) { if (!(i in r)) throw new Error(`tokenizer '${i}' does not exist`); if (["options", "rules", "lexer"].includes(i)) continue; const c = i, a = t.tokenizer[c], p = r[c]; r[c] = (...o) => { let l = a.apply(r, o); return l === !1 && (l = p.apply(r, o)), l; }; } n.tokenizer = r; } if (t.hooks) { const r = this.defaults.hooks || new B(); for (const i in t.hooks) { if (!(i in r)) throw new Error(`hook '${i}' does not exist`); if (["options", "block"].includes(i)) continue; const c = i, a = t.hooks[c], p = r[c]; B.passThroughHooks.has(i) ? r[c] = (o) => { if (this.defaults.async) return Promise.resolve(a.call(r, o)).then((d) => p.call(r, d)); const l = a.call(r, o); return p.call(r, l); } : r[c] = (...o) => { let l = a.apply(r, o); return l === !1 && (l = p.apply(r, o)), l; }; } n.hooks = r; } if (t.walkTokens) { const r = this.defaults.walkTokens, i = t.walkTokens; n.walkTokens = function(c) { let a = []; return a.push(i.call(this, c)), r && (a = a.concat(r.call(this, c))), a; }; } this.defaults = { ...this.defaults, ...n }; }), this; } setOptions(s) { return this.defaults = { ...this.defaults, ...s }, this; } lexer(s, e) { return S.lex(s, e ?? this.defaults); } parser(s, e) { return R.parse(s, e ?? this.defaults); } parseMarkdown(s) { return (t, n) => { const r = { ...n }, i = { ...this.defaults, ...r }, c = this.onError(!!i.silent, !!i.async); if (this.defaults.async === !0 && r.async === !1) return c(new Error("marked(): The async option was set to true by an extension. Remove async: false from the parse options object to return a Promise.")); if (typeof t > "u" || t === null) return c(new Error("marked(): input parameter is undefined or null")); if (typeof t != "string") return c(new Error("marked(): input parameter is of type " + Object.prototype.toString.call(t) + ", string expected")); i.hooks && (i.hooks.options = i, i.hooks.block = s); const a = i.hooks ? i.hooks.provideLexer() : s ? S.lex : S.lexInline, p = i.hooks ? i.hooks.provideParser() : s ? R.parse : R.parseInline; if (i.async) return Promise.resolve(i.hooks ? i.hooks.preprocess(t) : t).then((o) => a(o, i)).then((o) => i.hooks ? i.hooks.processAllTokens(o) : o).then((o) => i.walkTokens ? Promise.all(this.walkTokens(o, i.walkTokens)).then(() => o) : o).then((o) => p(o, i)).then((o) => i.hooks ? i.hooks.postprocess(o) : o).catch(c); try { i.hooks && (t = i.hooks.preprocess(t)); let o = a(t, i); i.hooks && (o = i.hooks.processAllTokens(o)), i.walkTokens && this.walkTokens(o, i.walkTokens); let l = p(o, i); return i.hooks && (l = i.hooks.postprocess(l)), l; } catch (o) { return c(o); } }; } onError(s, e) { return (t) => { if (t.message += ` Please report this to https://github.com/markedjs/marked.`, s) { const n = "

    An error occurred:

    " + y(t.message + "", !0) + "
    "; return e ? Promise.resolve(n) : n; } if (e) return Promise.reject(t); throw t; }; } }, T = new nt(); function f(s, e) { return T.parse(s, e); } f.options = f.setOptions = function(s) { return T.setOptions(s), f.defaults = T.defaults, le(f.defaults), f; }; f.getDefaults = U; f.defaults = z; f.use = function(...s) { return T.use(...s), f.defaults = T.defaults, le(f.defaults), f; }; f.walkTokens = function(s, e) { return T.walkTokens(s, e); }; f.parseInline = T.parseInline; f.Parser = R; f.parser = R.parse; f.Renderer = q; f.TextRenderer = Y; f.Lexer = S; f.lexer = S.lex; f.Tokenizer = E; f.Hooks = B; f.parse = f; f.options; f.setOptions; f.use; f.walkTokens; f.parseInline; R.parse; S.lex; class rt extends HTMLElement { constructor() { super(); g(this, "chatClient"); g(this, "shadow"); g(this, "isOpen", !1); g(this, "isProcessing", !1); g(this, "messages", []); this.shadow = this.attachShadow({ mode: "open" }), this.chatClient = new se(this.getAttribute("api-url") || "/api"); } // Define which attributes to observe for changes static get observedAttributes() { return ["primary-color", "title", "api-url", "context-messages"]; } // Getter methods for customizable properties get primaryColor() { return this.getAttribute("primary-color") || "#0084ff"; } get title() { return this.getAttribute("title") || "AI Assistant"; } get contextMessages() { const t = parseInt(this.getAttribute("context-messages") || "10", 10); return isNaN(t) || t < 1 ? 10 : Math.min(t, 100); } connectedCallback() { this.configureMarked(), this.render(), this.attachEventListeners(); } configureMarked() { f.setOptions({ breaks: !0, // Convert line breaks to
    gfm: !0, // GitHub flavored markdown pedantic: !1, smartLists: !0, // Better list handling smartypants: !0 // Better typography }); } attributeChangedCallback(t, n, r) { if (n !== r) switch (t) { case "primary-color": this.style.setProperty("--primary-color", this.primaryColor); break; case "title": const i = this.shadow.querySelector(".chat-title"); i && (i.textContent = this.title); break; case "api-url": this.chatClient = new se(this.getAttribute("api-url") || "/api"); break; } } render() { this.style.setProperty("--primary-color", this.primaryColor), this.shadow.innerHTML = `
    ${this.title}
    ${this.messages.map((t) => `
    ${this.escapeHtml(t.content)}
    `).join("")}
    `; } attachEventListeners() { const t = this.shadow.getElementById("toggleButton"), n = this.shadow.getElementById("closeButton"), r = this.shadow.getElementById("sendButton"), i = this.shadow.getElementById("messageInput"), c = this.shadow.getElementById("chatWindow"); t == null || t.addEventListener("click", () => { this.isOpen = !0, c == null || c.classList.remove("hidden"); }), n == null || n.addEventListener("click", () => { this.isOpen = !1, c == null || c.classList.add("hidden"); }), r == null || r.addEventListener("click", () => this.sendMessage()), i == null || i.addEventListener("keypress", (a) => { a.key === "Enter" && !a.shiftKey && (a.preventDefault(), this.sendMessage()); }); } async sendMessage() { var i; const t = this.shadow.getElementById("messageInput"), n = this.shadow.getElementById("sendButton"), r = t.value.trim(); if (!(!r || this.isProcessing)) { this.messages.push({ role: "user", content: r }), this.updateMessages(), t.value = "", this.isProcessing = !0, n.disabled = !0, t.disabled = !0, t.placeholder = "AI is thinking...", this.messages.push({ role: "assistant", content: "" }), this.updateMessages(!0); try { const c = this.messages.slice(0, -1).filter((o) => o.role !== "tool").map((o) => ({ role: o.role, content: o.content })), a = this.contextMessages, p = c.slice(-a); await this.chatClient.sendMessage(p, (o) => { var l, d, h; switch (o.type) { case "text": const k = (l = this.messages.map((v, $) => ({ msg: v, index: $ })).filter((v) => v.msg.role === "assistant").pop()) == null ? void 0 : l.index; k !== void 0 ? this.messages[k].content += o.content || "" : this.messages.push({ role: "assistant", content: o.content || "" }), this.updateMessages(!0); break; case "tool-call": this.messages.push({ role: "tool", content: `🔧 Calling ${o.toolName}: ${JSON.stringify(o.args, null, 2)}`, toolName: o.toolName, isToolCall: !0 }), this.updateMessages(!0); break; case "tool-result": const u = typeof o.result == "string" ? o.result : JSON.stringify(o.result, null, 2); this.messages.push({ role: "tool", content: `✓ ${o.toolName} result: ${u}`, toolName: o.toolName }), this.messages.push({ role: "assistant", content: "" }), this.updateMessages(!0); break; case "step-start": this.messages.push({ role: "tool", content: `🔄 Starting ${o.stepType} step...` }), this.updateMessages(!0); break; case "step-finish": const x = this.messages.length - 1; ((d = this.messages[x]) == null ? void 0 : d.role) === "tool" && ((h = this.messages[x]) != null && h.content.includes("Starting")) && (this.messages[x].content = `✅ Completed ${o.stepType} step`), this.updateMessages(!0); break; case "error": this.messages.push({ role: "tool", content: `❌ Error: ${o.content}` }), this.updateMessages(!1), this.isProcessing = !1, n.disabled = !1, t.disabled = !1, t.placeholder = "Type a message...", t.focus(); break; case "finish": this.updateMessages(!1), this.isProcessing = !1, n.disabled = !1, t.disabled = !1, t.placeholder = "Type a message...", t.focus(); break; } }), this.messages = this.messages.filter( (o) => o.role !== "assistant" || o.content.trim() !== "" ), this.updateMessages(!1); } catch (c) { console.error("Error sending message:", c); const a = (i = this.messages.map((p, o) => ({ msg: p, index: o })).filter((p) => p.msg.role === "assistant").pop()) == null ? void 0 : i.index; a !== void 0 ? this.messages[a].content = "Sorry, I encountered an error. Please try again." : this.messages.push({ role: "assistant", content: "Sorry, I encountered an error. Please try again." }), this.updateMessages(!1), this.isProcessing = !1, n.disabled = !1, t.disabled = !1, t.placeholder = "Type a message..."; } finally { t.focus(); } } } updateMessages(t = !1) { const n = this.shadow.getElementById("messagesContainer"); n && (n.innerHTML = this.messages.map((r, i) => { const c = i === this.messages.length - 1 && r.role === "assistant", a = t && c ? " streaming" : ""; return r.role === "tool" ? `
    ${r.isToolCall ? '
    Tool Call
    ' : ""}
    ${this.escapeHtml(r.content)}
    ` : `
    ${r.role === "assistant" ? this.parseMarkdown(r.content) : this.escapeHtml(r.content)}
    `; }).join(""), n.scrollTop = n.scrollHeight); } escapeHtml(t) { const n = { "&": "&", "<": "<", ">": ">", '"': """, "'": "'" }; return t.replace(/[&<>"']/g, (r) => n[r]); } parseMarkdown(t) { if (!t) return ""; try { return f.parse(t).replace(/