"use strict";exports.id=7197,exports.ids=[7197],exports.modules={45903:(a,b,c)=>{c.d(b,{AP:()=>m,BY:()=>w,I6:()=>k,L7:()=>l,dX:()=>v,r:()=>j});var d=c(89718),e=c(33873),f=c.n(e),g=c(29021),h=c.n(g);let i={enabled:!1,type:"sqlite",sqlitePath:"",host:"",port:"",database:"",username:"",password:"",ssl:!1,createDatabase:!1,logClientRequest:!0,logSourceRequest:!0,logTargetRequest:!0,logProviderResponse:!1,logErrors:!0,retentionDays:30};function j(a){let b={...a||{}};return b.password&&(b.password="********"),b}function k(a={},b={}){let c={...i,...b||{},...a||{}};c.enabled=!!c.enabled,c.type=["sqlite","postgres","mysql"].includes(c.type)?c.type:"sqlite",c.ssl=!!c.ssl,c.createDatabase=!!c.createDatabase,c.logClientRequest=!1!==c.logClientRequest,c.logSourceRequest=!1!==c.logSourceRequest,c.logTargetRequest=!1!==c.logTargetRequest,c.logProviderResponse=!0===c.logProviderResponse,c.logErrors=!1!==c.logErrors;let d=Number(c.retentionDays);return c.retentionDays=Number.isFinite(d)&&d>0?Math.floor(d):30,"********"===c.password&&(c.password=b?.password||""),c.host=String(c.host||"").trim(),c.port=String(c.port||"").trim(),c.database=String(c.database||"").trim(),c.username=String(c.username||"").trim(),c.password=String(c.password||""),c.sqlitePath=String(c.sqlitePath||"").trim(),c}async function l(){let a=await (0,d.mt)();return{...i,...a?.promptLogDb||{}}}async function m(a){let b=await (0,d.mt)(),c=k(a,b?.promptLogDb||{});return await (0,d.Xx)({promptLogDb:c}),c}async function n(a){let b,d=a.sqlitePath||(b=process.env.DATA_DIR||"/var/lib/9router",f().join(b,"logs","promptLogs.sqlite"));h().mkdirSync(f().dirname(d),{recursive:!0});try{let a=await Promise.resolve().then(c.t.bind(c,87550,23)),b=new(a.default||a)(d);return b.pragma("journal_mode = WAL"),b.pragma("busy_timeout = 5000"),{type:"sqlite",sqlitePath:d,exec(a){b.exec(a)},all:(a,c=[])=>b.prepare(a).all(...c),close(){b.close()}}}catch{let a=new(await Promise.resolve().then(c.t.bind(c,80099,23))).DatabaseSync(d);return a.exec("PRAGMA journal_mode = WAL"),a.exec("PRAGMA busy_timeout = 5000"),{type:"sqlite",sqlitePath:d,exec(b){a.exec(b)},all:(b,c=[])=>a.prepare(b).all(...c),close(){a.close()}}}}async function o(a,b=null){let d=new(await Promise.resolve().then(c.bind(c,64939))).Client({host:a.host,port:Number(a.port||5432),database:b||a.database,user:a.username,password:a.password,ssl:!!a.ssl&&{rejectUnauthorized:!1}});return await d.connect(),{type:"postgres",client:d,async exec(a){await d.query(a)},async close(){await d.end()}}}async function p(a,b=!0){let d=await c.e(8491).then(c.t.bind(c,38491,19)),e=await d.createConnection({host:a.host,port:Number(a.port||3306),database:b?a.database:void 0,user:a.username,password:a.password,ssl:a.ssl?{}:void 0,multipleStatements:!0});return{type:"mysql",conn:e,async exec(a){await e.query(a)},async close(){await e.end()}}}async function q(a){let b=new Set(a.all("PRAGMA table_info(prompt_logs)").map(a=>a.name)),c=(c,d)=>{b.has(c)||a.exec(`ALTER TABLE prompt_logs ADD COLUMN ${c} ${d};`)};c("client_ip","TEXT"),c("client_port","TEXT"),c("client_user_agent","TEXT"),c("client_host","TEXT"),c("source_tool","TEXT"),c("source_tool_label","TEXT"),c("source_host","TEXT"),c("provider","TEXT"),c("account_name","TEXT"),c("connection_id","TEXT"),c("display_model","TEXT"),a.exec("CREATE INDEX IF NOT EXISTS idx_prompt_logs_client_ip ON prompt_logs(client_ip);"),a.exec("CREATE INDEX IF NOT EXISTS idx_prompt_logs_source_tool ON prompt_logs(source_tool);")}async function r(a){await a.exec(`
    ALTER TABLE prompt_logs ADD COLUMN IF NOT EXISTS client_ip TEXT;
    ALTER TABLE prompt_logs ADD COLUMN IF NOT EXISTS client_port TEXT;
    ALTER TABLE prompt_logs ADD COLUMN IF NOT EXISTS client_user_agent TEXT;
    ALTER TABLE prompt_logs ADD COLUMN IF NOT EXISTS client_host TEXT;
    ALTER TABLE prompt_logs ADD COLUMN IF NOT EXISTS source_tool TEXT;
    ALTER TABLE prompt_logs ADD COLUMN IF NOT EXISTS source_tool_label TEXT;
    ALTER TABLE prompt_logs ADD COLUMN IF NOT EXISTS source_host TEXT;
    ALTER TABLE prompt_logs ADD COLUMN IF NOT EXISTS provider TEXT;
    ALTER TABLE prompt_logs ADD COLUMN IF NOT EXISTS account_name TEXT;
    ALTER TABLE prompt_logs ADD COLUMN IF NOT EXISTS connection_id TEXT;
    ALTER TABLE prompt_logs ADD COLUMN IF NOT EXISTS display_model TEXT;

    CREATE INDEX IF NOT EXISTS idx_prompt_logs_client_ip
      ON prompt_logs(client_ip);

    CREATE INDEX IF NOT EXISTS idx_prompt_logs_source_tool
      ON prompt_logs(source_tool);
  `)}async function s(a,b,c,d){let[e]=await a.query(`
    SELECT 1 AS found
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_SCHEMA = ?
      AND TABLE_NAME = ?
      AND COLUMN_NAME = ?
    LIMIT 1
    `,[b,c,d]);return e.length>0}async function t(a,b,c,d){let[e]=await a.query(`
    SELECT 1 AS found
    FROM INFORMATION_SCHEMA.STATISTICS
    WHERE TABLE_SCHEMA = ?
      AND TABLE_NAME = ?
      AND INDEX_NAME = ?
    LIMIT 1
    `,[b,c,d]);return e.length>0}async function u(a,b){let c=async(c,d)=>{await s(a.conn,b.database,"prompt_logs",c)||await a.conn.query(`ALTER TABLE prompt_logs ADD COLUMN ${c} ${d}`)};await c("client_ip","VARCHAR(64)"),await c("client_port","VARCHAR(32)"),await c("client_user_agent","TEXT"),await c("client_host","TEXT"),await c("source_tool","VARCHAR(80)"),await c("source_tool_label","VARCHAR(120)"),await c("source_host","VARCHAR(255)"),await c("provider","VARCHAR(120)"),await c("account_name","VARCHAR(255)"),await c("connection_id","VARCHAR(120)"),await c("display_model","VARCHAR(512)"),await t(a.conn,b.database,"prompt_logs","idx_prompt_logs_client_ip")||await a.conn.query("CREATE INDEX idx_prompt_logs_client_ip ON prompt_logs(client_ip)"),await t(a.conn,b.database,"prompt_logs","idx_prompt_logs_source_tool")||await a.conn.query("CREATE INDEX idx_prompt_logs_source_tool ON prompt_logs(source_tool)")}async function v(a=null){let b=k(a||await l());if("sqlite"===b.type){let a=await n(b);try{return a.exec(`
    CREATE TABLE IF NOT EXISTS prompt_logs (
      id TEXT PRIMARY KEY,
      created_at TEXT NOT NULL,
      session_id TEXT,
      source_format TEXT,
      target_format TEXT,
      model TEXT,
      endpoint TEXT,
      stage TEXT,
      provider_url TEXT,
      provider TEXT,
      account_name TEXT,
      connection_id TEXT,
      display_model TEXT,
      client_ip TEXT,
      client_port TEXT,
      client_user_agent TEXT,
      client_host TEXT,
      source_tool TEXT,
      source_tool_label TEXT,
      source_host TEXT,
      prompt_text TEXT,
      full_request_json TEXT,
      response_text TEXT,
      status_code INTEGER,
      error_text TEXT,
      meta_json TEXT
    );

    CREATE INDEX IF NOT EXISTS idx_prompt_logs_created_at
      ON prompt_logs(created_at DESC);

    CREATE INDEX IF NOT EXISTS idx_prompt_logs_model
      ON prompt_logs(model);

    CREATE INDEX IF NOT EXISTS idx_prompt_logs_stage
      ON prompt_logs(stage);

    CREATE INDEX IF NOT EXISTS idx_prompt_logs_session
      ON prompt_logs(session_id);

    CREATE INDEX IF NOT EXISTS idx_prompt_logs_client_ip
      ON prompt_logs(client_ip);

    CREATE INDEX IF NOT EXISTS idx_prompt_logs_source_tool
      ON prompt_logs(source_tool);
  `),await q(a),{ok:!0,type:"sqlite",message:"SQLite prompt log schema is ready",sqlitePath:a.sqlitePath}}finally{a.close()}}if("postgres"===b.type){if(!b.host||!b.database||!b.username)throw Error("PostgreSQL host, database, and username are required");if(b.createDatabase){let a=await o(b,"postgres");try{let c=await a.client.query("SELECT 1 FROM pg_database WHERE datname = $1",[b.database]);if(0===c.rowCount){let c=b.database.replace(/"/g,'""');await a.client.query(`CREATE DATABASE "${c}"`)}}finally{await a.close()}}let a=await o(b);try{return await a.exec(`
    CREATE TABLE IF NOT EXISTS prompt_logs (
      id TEXT PRIMARY KEY,
      created_at TIMESTAMPTZ NOT NULL,
      session_id TEXT,
      source_format TEXT,
      target_format TEXT,
      model TEXT,
      endpoint TEXT,
      stage TEXT,
      provider_url TEXT,
      provider TEXT,
      account_name TEXT,
      connection_id TEXT,
      display_model TEXT,
      client_ip TEXT,
      client_port TEXT,
      client_user_agent TEXT,
      client_host TEXT,
      source_tool TEXT,
      source_tool_label TEXT,
      source_host TEXT,
      prompt_text TEXT,
      full_request_json JSONB,
      response_text TEXT,
      status_code INTEGER,
      error_text TEXT,
      meta_json JSONB
    );

    CREATE INDEX IF NOT EXISTS idx_prompt_logs_created_at
      ON prompt_logs(created_at DESC);

    CREATE INDEX IF NOT EXISTS idx_prompt_logs_model
      ON prompt_logs(model);

    CREATE INDEX IF NOT EXISTS idx_prompt_logs_stage
      ON prompt_logs(stage);

    CREATE INDEX IF NOT EXISTS idx_prompt_logs_session
      ON prompt_logs(session_id);

    CREATE INDEX IF NOT EXISTS idx_prompt_logs_client_ip
      ON prompt_logs(client_ip);

    CREATE INDEX IF NOT EXISTS idx_prompt_logs_source_tool
      ON prompt_logs(source_tool);
  `),await r(a),{ok:!0,type:"postgres",message:"PostgreSQL prompt log schema is ready"}}finally{await a.close()}}if("mysql"===b.type){if(!b.host||!b.database||!b.username)throw Error("MySQL host, database, and username are required");if(b.createDatabase){let a=await p(b,!1);try{let c=b.database.replace(/`/g,"``");await a.exec(`CREATE DATABASE IF NOT EXISTS \`${c}\` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci`)}finally{await a.close()}}let a=await p(b,!0);try{return await a.exec(`
    CREATE TABLE IF NOT EXISTS prompt_logs (
      id VARCHAR(80) PRIMARY KEY,
      created_at DATETIME(3) NOT NULL,
      session_id VARCHAR(255),
      source_format VARCHAR(80),
      target_format VARCHAR(80),
      model VARCHAR(255),
      endpoint TEXT,
      stage VARCHAR(80),
      provider_url TEXT,
      provider VARCHAR(120),
      account_name VARCHAR(255),
      connection_id VARCHAR(120),
      display_model VARCHAR(512),
      client_ip VARCHAR(64),
      client_port VARCHAR(32),
      client_user_agent TEXT,
      client_host TEXT,
      source_tool VARCHAR(80),
      source_tool_label VARCHAR(120),
      source_host VARCHAR(255),
      prompt_text LONGTEXT,
      full_request_json JSON,
      response_text LONGTEXT,
      status_code INT,
      error_text TEXT,
      meta_json JSON,
      INDEX idx_prompt_logs_created_at (created_at),
      INDEX idx_prompt_logs_model (model),
      INDEX idx_prompt_logs_stage (stage),
      INDEX idx_prompt_logs_session (session_id),
      INDEX idx_prompt_logs_client_ip (client_ip),
      INDEX idx_prompt_logs_source_tool (source_tool)
    );
  `),await u(a,b),{ok:!0,type:"mysql",message:"MySQL prompt log schema is ready"}}finally{await a.close()}}throw Error(`Unsupported database type: ${b.type}`)}async function w(a=null){let b=k(a||await l());if("sqlite"===b.type){let a=await n(b);try{return a.exec("SELECT 1;"),{ok:!0,type:"sqlite",message:"SQLite connection OK",sqlitePath:a.sqlitePath}}finally{a.close()}}if("postgres"===b.type){let a=await o(b);try{return await a.exec("SELECT 1;"),{ok:!0,type:"postgres",message:"PostgreSQL connection OK"}}finally{await a.close()}}if("mysql"===b.type){let a=await p(b,!0);try{return await a.exec("SELECT 1;"),{ok:!0,type:"mysql",message:"MySQL connection OK"}}finally{await a.close()}}throw Error(`Unsupported database type: ${b.type}`)}},50122:(a,b,c)=>{c.d(b,{k:()=>e});let d=[];for(let a=0;a<256;++a)d.push((a+256).toString(16).slice(1));function e(a,b=0){return(d[a[b+0]]+d[a[b+1]]+d[a[b+2]]+d[a[b+3]]+"-"+d[a[b+4]]+d[a[b+5]]+"-"+d[a[b+6]]+d[a[b+7]]+"-"+d[a[b+8]]+d[a[b+9]]+"-"+d[a[b+10]]+d[a[b+11]]+d[a[b+12]]+d[a[b+13]]+d[a[b+14]]+d[a[b+15]]).toLowerCase()}},89718:(a,b,c)=>{c.d(b,{CG:()=>d.CG,Dj:()=>d.Dj,Fh:()=>d.Fh,Iq:()=>d.Iq,K1:()=>d.K1,KJ:()=>d.KJ,L:()=>d.L,L9:()=>d.L9,Lh:()=>d.Lh,Mc:()=>d.Mc,OM:()=>d.OM,PX:()=>d.PX,Pc:()=>d.Pc,Q_:()=>d.Q_,Qu:()=>d.Qu,S8:()=>d.S8,Uv:()=>d.Uv,VT:()=>d.VT,XW:()=>d.XW,Xx:()=>d.Xx,Yd:()=>d.Yd,ZO:()=>d.ZO,c:()=>d.c,ek:()=>d.ek,fK:()=>d.fK,fv:()=>d.fv,getProviderConnections:()=>d.P,ho:()=>d.ho,hr:()=>d.hr,i0:()=>d.i0,iE:()=>d.iE,jd:()=>d.jd,mY:()=>d.mY,mt:()=>d.mt,o5:()=>d.o5,oG:()=>d.oG,op:()=>d.op,r4:()=>d.r4,uL:()=>d.uL,ui:()=>d.ui,updateProviderConnection:()=>d.rj,uv:()=>d.uv,yF:()=>d.yF,yg:()=>d.yg,zP:()=>d.zP});var d=c(9248)},94755:(a,b,c)=>{c.d(b,{A:()=>i});var d=c(77598);let e={randomUUID:d.randomUUID},f=new Uint8Array(256),g=f.length;var h=c(50122);let i=function(a,b,c){if(e.randomUUID&&!b&&!a)return e.randomUUID();var i=a,j=c;let k=(i=i||{}).random??i.rng?.()??(g>f.length-16&&((0,d.randomFillSync)(f),g=0),f.slice(g,g+=16));if(k.length<16)throw Error("Random bytes length must be >= 16");if(k[6]=15&k[6]|64,k[8]=63&k[8]|128,b){if((j=j||0)<0||j+16>b.length)throw RangeError(`UUID byte range ${j}:${j+15} is out of buffer bounds`);for(let a=0;a<16;++a)b[j+a]=k[a];return b}return(0,h.k)(k)}}};