'use strict';

// src/chat/types.ts
function createChatId() {
  return crypto.randomUUID();
}
var UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
function toChatId(value) {
  if (!UUID_RE.test(value)) {
    throw new TypeError(`Invalid ChatId: "${value}" is not a valid UUID`);
  }
  return value;
}
function createTextMessage(text, role = "user") {
  return {
    id: createChatId(),
    role,
    parts: [{ type: "text", text, status: "complete" }],
    createdAt: (/* @__PURE__ */ new Date()).toISOString(),
    status: "complete"
  };
}
function isObservableSession(session) {
  return "subscribe" in session && typeof session.subscribe === "function" && "getSnapshot" in session && typeof session.getSnapshot === "function";
}

// src/chat/chat-utils.ts
function getMessageText(message) {
  return message.parts.filter((p) => p.type === "text").map((p) => p.text).join("");
}
function getMessageToolCalls(message) {
  return message.parts.filter((p) => p.type === "tool_call");
}
function getMessageReasoning(message) {
  return message.parts.filter((p) => p.type === "reasoning").map((p) => p.text).join("");
}

// src/chat/guards.ts
function isChatMessage(value) {
  if (typeof value !== "object" || value === null) return false;
  const obj = value;
  return typeof obj.id === "string" && typeof obj.role === "string" && (obj.role === "user" || obj.role === "assistant" || obj.role === "system") && Array.isArray(obj.parts) && typeof obj.createdAt === "string" && typeof obj.status === "string";
}
function isChatSession(value) {
  if (typeof value !== "object" || value === null) return false;
  const obj = value;
  return typeof obj.id === "string" && Array.isArray(obj.messages) && typeof obj.config === "object" && obj.config !== null && typeof obj.createdAt === "string" && typeof obj.updatedAt === "string" && typeof obj.status === "string";
}
function isMessagePart(value) {
  if (typeof value !== "object" || value === null) return false;
  const obj = value;
  return typeof obj.type === "string" && (obj.type === "text" || obj.type === "reasoning" || obj.type === "tool_call" || obj.type === "source" || obj.type === "file");
}
function isTextPart(value) {
  if (typeof value !== "object" || value === null) return false;
  const obj = value;
  return obj.type === "text" && typeof obj.text === "string";
}
function isToolCallPart(value) {
  if (typeof value !== "object" || value === null) return false;
  const obj = value;
  return obj.type === "tool_call" && typeof obj.toolCallId === "string" && typeof obj.name === "string";
}
function isReasoningPart(value) {
  if (typeof value !== "object" || value === null) return false;
  const obj = value;
  return obj.type === "reasoning" && typeof obj.text === "string";
}
function isSourcePart(value) {
  if (typeof value !== "object" || value === null) return false;
  const obj = value;
  return obj.type === "source" && typeof obj.url === "string";
}
function isFilePart(value) {
  if (typeof value !== "object" || value === null) return false;
  const obj = value;
  return obj.type === "file" && typeof obj.name === "string" && typeof obj.mimeType === "string";
}
function isChatEvent(value) {
  if (typeof value !== "object" || value === null) return false;
  const obj = value;
  const validTypes = [
    "message:start",
    "message:delta",
    "message:complete",
    "tool:start",
    "tool:complete",
    "thinking:start",
    "thinking:delta",
    "thinking:end",
    "permission:request",
    "permission:response",
    "usage",
    "session:created",
    "session:updated",
    "error",
    "typing:start",
    "typing:end",
    "heartbeat",
    "done"
  ];
  return validTypes.includes(obj.type);
}

// src/chat/bridge.ts
function agentEventToChatEvent(event, messageId) {
  switch (event.type) {
    case "text_delta":
      return { type: "message:delta", messageId, text: event.text };
    case "thinking_start":
      return { type: "thinking:start", messageId };
    case "thinking_delta":
      return { type: "thinking:delta", messageId, text: event.text };
    case "thinking_end":
      return { type: "thinking:end", messageId };
    case "tool_call_start":
      return {
        type: "tool:start",
        messageId,
        toolCallId: event.toolCallId,
        toolName: event.toolName,
        args: event.args
      };
    case "tool_call_end":
      return {
        type: "tool:complete",
        messageId,
        toolCallId: event.toolCallId,
        toolName: event.toolName,
        result: event.result
      };
    case "permission_request":
      return {
        type: "permission:request",
        messageId,
        toolName: event.request.toolName,
        toolArgs: event.request.toolArgs
      };
    case "permission_response":
      return {
        type: "permission:response",
        messageId,
        toolName: event.toolName,
        allowed: event.decision.allowed
      };
    case "usage_update":
      return {
        type: "usage",
        promptTokens: event.promptTokens,
        completionTokens: event.completionTokens,
        model: event.model
      };
    case "error":
      return {
        type: "error",
        error: event.error,
        recoverable: event.recoverable,
        code: event.code,
        messageId
      };
    case "heartbeat":
      return { type: "heartbeat" };
    case "ask_user":
    case "ask_user_response":
    case "session_info":
    case "done":
      return null;
    default:
      return null;
  }
}
async function* adaptAgentEvents(events, messageId) {
  for await (const event of events) {
    const chatEvent = agentEventToChatEvent(event, messageId);
    if (chatEvent !== null) {
      yield chatEvent;
    }
  }
}
function chatEventToAgentEvent(event) {
  switch (event.type) {
    case "message:delta":
      return { type: "text_delta", text: event.text };
    case "thinking:start":
      return { type: "thinking_start" };
    case "thinking:delta":
      return { type: "thinking_delta", text: event.text };
    case "thinking:end":
      return { type: "thinking_end" };
    case "tool:start":
      return {
        type: "tool_call_start",
        toolCallId: event.toolCallId,
        toolName: event.toolName,
        args: event.args
      };
    case "tool:complete":
      return {
        type: "tool_call_end",
        toolCallId: event.toolCallId,
        toolName: event.toolName,
        result: event.result
      };
    case "error":
      return { type: "error", error: event.error, recoverable: event.recoverable, code: event.code };
    default:
      return null;
  }
}

// src/chat/conversion.ts
function toAgentMessage(message) {
  const textContent = getMessageText(message);
  const toolCallParts = getMessageToolCalls(message);
  switch (message.role) {
    case "user":
      return { role: "user", content: textContent };
    case "assistant": {
      const toolCalls = toolCallParts.length > 0 ? toolCallParts.map((p) => ({ id: p.toolCallId, name: p.name, args: p.args })) : void 0;
      return {
        role: "assistant",
        content: textContent,
        toolCalls
      };
    }
    case "system":
      return { role: "system", content: textContent };
  }
}
function fromAgentMessage(message, id) {
  const chatId = id ?? createChatId();
  const now = (/* @__PURE__ */ new Date()).toISOString();
  const parts = [];
  const textContent = typeof message.content === "string" ? message.content : Array.isArray(message.content) ? message.content.filter((part) => part.type === "text").map((part) => part.text).join("\n") : message.content ?? "";
  if (textContent) {
    parts.push({ type: "text", text: textContent, status: "complete" });
  }
  if (message.role === "assistant" && message.toolCalls) {
    for (const tc of message.toolCalls) {
      parts.push({
        type: "tool_call",
        toolCallId: tc.id,
        name: tc.name,
        args: tc.args,
        status: "complete"
      });
    }
  }
  if (message.role === "tool" && message.toolResults) {
    for (const tr of message.toolResults) {
      parts.push({
        type: "tool_call",
        toolCallId: tr.toolCallId,
        name: tr.name,
        args: {},
        result: tr.result,
        status: "complete"
      });
    }
  }
  if (parts.length === 0) {
    parts.push({ type: "text", text: "", status: "complete" });
  }
  const role = message.role === "tool" ? "assistant" : message.role;
  return {
    id: chatId,
    role,
    parts,
    createdAt: now,
    status: "complete"
  };
}
function extractToolResults(message) {
  return getMessageToolCalls(message).filter((p) => p.result !== void 0).map((p) => ({
    toolCallId: p.toolCallId,
    name: p.name,
    result: p.result,
    isError: p.status === "error" ? true : void 0
  }));
}

exports.adaptAgentEvents = adaptAgentEvents;
exports.agentEventToChatEvent = agentEventToChatEvent;
exports.chatEventToAgentEvent = chatEventToAgentEvent;
exports.createChatId = createChatId;
exports.createTextMessage = createTextMessage;
exports.extractToolResults = extractToolResults;
exports.fromAgentMessage = fromAgentMessage;
exports.getMessageReasoning = getMessageReasoning;
exports.getMessageText = getMessageText;
exports.getMessageToolCalls = getMessageToolCalls;
exports.isChatEvent = isChatEvent;
exports.isChatMessage = isChatMessage;
exports.isChatSession = isChatSession;
exports.isFilePart = isFilePart;
exports.isMessagePart = isMessagePart;
exports.isObservableSession = isObservableSession;
exports.isReasoningPart = isReasoningPart;
exports.isSourcePart = isSourcePart;
exports.isTextPart = isTextPart;
exports.isToolCallPart = isToolCallPart;
exports.toAgentMessage = toAgentMessage;
exports.toChatId = toChatId;
//# sourceMappingURL=core.cjs.map
//# sourceMappingURL=core.cjs.map