import"../../errors-pXKbCKbL.js";import"../../editor-BmJI32_P.js";import{t as e}from"../../server-DZmOQ_ym.js";import{t}from"../../engine-9wDtUZun.js";import"../../fsStore-BzLA1cBc.js";import"../../base-xqced3zT.js";import"../../config-Bphz_1Sc.js";import"../../template-Cpbt9qy8.js";import"../../xling-DyktHkvX.js";import"../../providerRegistry-DXZVv1rc.js";import"../../client-CG1X3JM2.js";import"../../types-D9R5QJnK.js";import{n}from"../../router-Dq8YoWty.js";import{Command as r,Flags as i}from"@oclif/core";import*as a from"open";import*as o from"node:readline";var s=class s extends r{static description=`Start a discussion between multiple AI models`;static flags={ui:i.boolean({description:`Launch Web UI`,default:!1}),topic:i.string({char:`t`,description:`Topic to discuss`}),models:i.string({char:`m`,description:`Comma-separated list of models to participate`}),strategy:i.string({char:`s`,description:`Turn-taking strategy (random, round-robin)`,options:[`random`,`round-robin`],default:`random`}),timeout:i.integer({description:`Timeout per turn in seconds`,default:30})};async run(){let{flags:e}=await this.parse(s);e.ui?await this.#e():await this.#t(e)}async#e(){this.log(`Starting Web UI...`);let t=3e3,n=(await e(t)).address(),r=`http://localhost:${typeof n==`object`&&n?n.port:t}`;return this.log(`Server running at ${r}`),await a.default(r),this.log(`Press Ctrl+C to stop`),new Promise(()=>{})}async#t(e){let r=await n(),i=r.getRegistry().getAllModels(),a=e.topic,s=e.models?e.models.split(`,`):[];a||=await this.#n(`Enter topic: `),s.length===0&&(s=await this.#r(i)),s.length<2&&this.error(`At least 2 models are required for a discussion.`);let c=new t(r,{topic:a,strategy:e.strategy,timeoutMs:e.timeout*1e3});return s.forEach((e,t)=>{c.addParticipant({id:`model-${t}`,name:e,model:e,type:`ai`})}),c.addParticipant({id:`user`,name:`User`,type:`human`}),this.log(`\nStarting discussion on "${a}" with: ${s.join(`, `)}\n`),this.log(`Controls:`),this.log(`  [Space] Pause/Resume`),this.log(`  [m]     Toggle Auto/Manual Mode`),this.log(`  [n]     Next Turn (Manual Mode)`),this.log(`  [i]     Interrupt / Speak`),this.log(`  [s]     Summarize & Stop`),this.log(`  [q]     Quit`),this.log(`
--------------------------------------------------
`),c.on(`turn-start`,e=>{let t=c.participants.find(t=>t.id===e);process.stdout.write(`\n\n[${t?.name}]: `)}),c.on(`message-chunk`,({delta:e})=>{process.stdout.write(e)}),c.on(`error`,({participantId:e,error:t})=>{let n=c.participants.find(t=>t.id===e);console.error(`\n[Error] ${n?.name}: ${t.message}`)}),c.on(`participant-dropped`,e=>{let t=c.participants.find(t=>t.id===e);console.error(`\n[System] ${t?.name} has left the chat (too many errors).`)}),c.start(),o.emitKeypressEvents(process.stdin),process.stdin.isTTY&&(process.stdin.setRawMode(!0),process.stdin.resume()),process.on(`SIGINT`,()=>{c.stop(),process.exit()}),process.stdin.on(`keypress`,async(e,t)=>{if(t.ctrl&&t.name===`c`&&process.exit(),t.name===`q`&&(c.stop(),process.exit()),t.name===`space`&&(c.status===`paused`?c.resume():c.pause(),this.log(`\n[System] Discussion ${c.status}`)),t.name===`m`){let e=c.mode===`auto`?`manual`:`auto`;c.setMode(e),this.log(`\n[System] Mode switched to ${e.toUpperCase()}`)}if(t.name===`n`)if(c.mode===`manual`){if(c.status===`discussing`||c.status===`paused`){let e=c.participants.filter(e=>e.type===`ai`),t=e[Math.floor(Math.random()*e.length)];c.setNextSpeaker(t.id)}}else this.log(`
[System] Switch to Manual mode [m] to use Next [n]`);if(t.name===`i`){c.pause(),process.stdin.setRawMode(!1),process.stdout.write(`
[User]: `);let e=o.createInterface({input:process.stdin,output:process.stdout});e.question(``,t=>{e.close(),t.trim()&&c.injectMessage(`user`,t),process.stdin.setRawMode(!0),c.resume()})}if(t.name===`s`){c.pause(),process.stdin.setRawMode(!1);let e=c.participants.filter(e=>e.type===`ai`);this.log(`
Select summarizer:`),e.forEach((e,t)=>this.log(`${t+1}. ${e.name}`));let t=o.createInterface({input:process.stdin,output:process.stdout});t.question(`Choice: `,async n=>{t.close();let r=e[parseInt(n)-1];if(r){this.log(`\nGenerating summary with ${r.name}...\n`);try{let e=await c.generateSummary(r.id);this.log(`
=== SUMMARY ===
`),this.log(e),this.log(`
===============
`)}catch(e){this.log(`Error: ${e.message}`)}}process.exit()})}}),new Promise(()=>{})}async#n(e){let t=o.createInterface({input:process.stdin,output:process.stdout});return new Promise(n=>{t.question(e,e=>{t.close(),n(e)})})}async#r(e){if(e.length===0)return[];let t=process.stdin,n=process.stdout;if(!t.isTTY||!n.isTTY)throw Error(`Interactive selection requires a TTY. Use --models to provide values directly.`);return new Promise((r,i)=>{let a=new Set,s=0,c=0,l=``,u=!!t.isRaw,d=(e,a)=>{t.off(`keypress`,p),t.setRawMode&&t.setRawMode(u),t.pause(),n.write(`\x1B[?25h
`),e?i(e):r(a)},f=()=>{n.moveCursor(0,-c),n.clearScreenDown();let t=[`Select models (space = toggle, arrows = navigate, enter = confirm, q/Ctrl+C = cancel)`,``,...e.map((e,t)=>{let n=t===s,r=a.has(t),i=`${n?`➜`:` `} ${r?`[x]`:`[ ]`} ${e}`;return n?`\x1B[36m${i}\x1B[0m`:i}),``,`Selected: ${Array.from(a).map(t=>e[t]).join(`, `)||`none`}`];l&&t.push(`\x1B[33m${l}\x1B[0m`),n.write(t.join(`
`)+`
`),c=t.length,l=``},p=(t,n)=>{if(n){if(n.name===`c`&&n.ctrl||n.name===`q`){d(Error(`Selection cancelled.`));return}if(n.name===`up`){s=(s-1+e.length)%e.length,f();return}if(n.name===`down`){s=(s+1)%e.length,f();return}if(n.name===`space`){a.has(s)?a.delete(s):a.add(s),f();return}if(n.name===`return`){if(a.size<2){l=`Select at least two models.`,f();return}d(void 0,Array.from(a).map(t=>e[t]))}}};o.emitKeypressEvents(t),t.resume(),t.setRawMode&&t.setRawMode(!0),n.write(`\x1B[?25l`),f(),t.on(`keypress`,p)})}};export{s as default};