import { createRequire } from "node:module";
var __defProp = Object.defineProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, {
      get: all[name],
      enumerable: true,
      configurable: true,
      set: (newValue) => all[name] = () => newValue
    });
};
var __esm = (fn, res) => () => (fn && (res = fn(fn = 0)), res);
var __require = /* @__PURE__ */ createRequire(import.meta.url);

// src/providers/claude-agent-sdk/session-bridge.ts
function toClaudeAgentSession(state) {
  const messages = [];
  for (const msg of state.messages) {
    if (msg.role === "user" || msg.role === "assistant") {
      const content = typeof msg.content === "string" ? msg.content : JSON.stringify(msg.content);
      messages.push({
        role: msg.role,
        content
      });
    }
  }
  return {
    sessionId: state.sessionId,
    messages,
    metadata: {
      agentId: state.agentId,
      tenantId: state.tenantId,
      actorId: state.actorId
    }
  };
}
function createClaudeAgentSession(options) {
  return {
    sessionId: options?.sessionId,
    messages: [],
    metadata: {
      tenantId: options?.tenantId,
      actorId: options?.actorId,
      ...options?.metadata
    }
  };
}
function createEmptyClaudeSession() {
  return createClaudeAgentSession();
}
function buildClaudeAgentContext(options) {
  return {
    tenantId: options?.tenantId,
    actorId: options?.actorId,
    sessionId: options?.sessionId,
    ...options?.metadata
  };
}
function appendUserMessage(session, content) {
  return {
    ...session,
    messages: [...session.messages, { role: "user", content }]
  };
}
function appendAssistantMessage(session, content) {
  return {
    ...session,
    messages: [...session.messages, { role: "assistant", content }]
  };
}
function clearSession(session) {
  return {
    ...session,
    messages: []
  };
}
function getRecentMessages(session, count) {
  return session.messages.slice(-count);
}
function extractToolUseBlocks(content) {
  if (typeof content === "string") {
    return [];
  }
  return content.filter((block) => block.type === "tool_use");
}
function createToolResultBlock(toolUseId, result, isError = false) {
  return {
    type: "tool_result",
    tool_use_id: toolUseId,
    content: typeof result === "string" ? result : JSON.stringify(result),
    is_error: isError
  };
}
function updateSessionMetadata(session, metadata) {
  return {
    ...session,
    metadata: {
      ...session.metadata,
      ...metadata
    }
  };
}
function getMessageCount(session) {
  return session.messages.length;
}
function estimateTokens(session) {
  let chars = 0;
  for (const msg of session.messages) {
    if (typeof msg.content === "string") {
      chars += msg.content.length;
    } else {
      chars += JSON.stringify(msg.content).length;
    }
  }
  return Math.ceil(chars / 4);
}
function summarizeSession(session) {
  let userMessages = 0;
  let assistantMessages = 0;
  for (const msg of session.messages) {
    if (msg.role === "user") {
      userMessages++;
    } else {
      assistantMessages++;
    }
  }
  return {
    messageCount: session.messages.length,
    userMessages,
    assistantMessages,
    estimatedTokens: estimateTokens(session)
  };
}
export {
  updateSessionMetadata,
  toClaudeAgentSession,
  summarizeSession,
  getRecentMessages,
  getMessageCount,
  extractToolUseBlocks,
  estimateTokens,
  createToolResultBlock,
  createEmptyClaudeSession,
  createClaudeAgentSession,
  clearSession,
  buildClaudeAgentContext,
  appendUserMessage,
  appendAssistantMessage
};
