#!/usr/bin/env node
import {
  SessionManager
} from "./chunk-RIVZNS3K.js";

// src/hub/persist.ts
import { join } from "path";
function discussionToMessages(state) {
  const out = [];
  const t0 = state.messages[0]?.timestamp ?? /* @__PURE__ */ new Date();
  out.push({ role: "user", content: `\u{1F3DB} Topic: ${state.topic}`, timestamp: t0 });
  for (const m of state.messages) {
    if (m.speaker === "system") {
      out.push({ role: "system", content: m.content, timestamp: m.timestamp });
      continue;
    }
    if (m.speaker === "human") {
      out.push({ role: "user", content: `\u{1F9ED} ${m.speakerName}: ${m.content}`, timestamp: m.timestamp });
      continue;
    }
    if (m.passed || !m.content.trim()) continue;
    const tag = m.converged ? " \u2713converged" : "";
    out.push({
      role: "assistant",
      content: `**${m.speakerName}** (${m.speaker})${tag}

${m.content}`,
      timestamp: m.timestamp
    });
  }
  if (state.summary && state.summary.trim()) {
    out.push({ role: "assistant", content: `\u{1F4CB} **Summary**

${state.summary}`, timestamp: /* @__PURE__ */ new Date() });
  }
  return out;
}
async function persistDiscussion(state, config, defaultProvider, defaultModel) {
  const sm = new SessionManager(config);
  const session = sm.createSession(defaultProvider, defaultModel);
  session.messages = discussionToMessages(state);
  session.title = `[Hub] ${state.topic.slice(0, 48)}`.replace(/\n/g, " ");
  session.titleAiGenerated = true;
  await sm.save();
  return { id: session.id, path: join(config.getHistoryDir(), `${session.id}.json`) };
}

export {
  discussionToMessages,
  persistDiscussion
};
