'use strict';

var chunkNI2DJ2VW_cjs = require('./chunk-NI2DJ2VW.cjs');
var chunkF3HP5HM6_cjs = require('./chunk-F3HP5HM6.cjs');
var chunkKIDHIRVO_cjs = require('./chunk-KIDHIRVO.cjs');
var chunkEFJCOPJ3_cjs = require('./chunk-EFJCOPJ3.cjs');
var chunk42M6Y2OD_cjs = require('./chunk-42M6Y2OD.cjs');
var chunkZ7LCIYK7_cjs = require('./chunk-Z7LCIYK7.cjs');
var chunkN54Q3AQR_cjs = require('./chunk-N54Q3AQR.cjs');
var chunk64ITUOXI_cjs = require('./chunk-64ITUOXI.cjs');
var chunkO7I5CWRX_cjs = require('./chunk-O7I5CWRX.cjs');
var crypto = require('crypto');

// src/server/handlers/conversations.ts
var conversations_exports = {};
chunkO7I5CWRX_cjs.__export(conversations_exports, {
  CREATE_CONVERSATION_ROUTE: () => CREATE_CONVERSATION_ROUTE,
  DELETE_CONVERSATION_ROUTE: () => DELETE_CONVERSATION_ROUTE,
  GET_CONVERSATION_ITEMS_ROUTE: () => GET_CONVERSATION_ITEMS_ROUTE,
  GET_CONVERSATION_ROUTE: () => GET_CONVERSATION_ROUTE
});
function buildConversationObject({ thread }) {
  return {
    id: thread.id,
    object: "conversation",
    thread
  };
}
function buildConversationItemsList(items) {
  return {
    object: "list",
    data: items,
    first_id: items[0]?.id ?? null,
    last_id: items.at(-1)?.id ?? null,
    has_more: false
  };
}
function buildConversationDeleted(conversationId) {
  return {
    id: conversationId,
    object: "conversation.deleted",
    deleted: true
  };
}
var CREATE_CONVERSATION_ROUTE = chunkN54Q3AQR_cjs.createRoute({
  method: "POST",
  path: "/v1/conversations",
  responseType: "json",
  bodySchema: chunkNI2DJ2VW_cjs.createConversationBodySchema,
  responseSchema: chunkNI2DJ2VW_cjs.conversationObjectSchema,
  summary: "Create a conversation",
  description: "Creates a new thread-backed conversation for agent-backed Responses API requests",
  tags: ["Responses"],
  requiresAuth: true,
  requiresPermission: chunk42M6Y2OD_cjs.MastraFGAPermissions.AGENTS_CREATE,
  handler: async ({ mastra, requestContext, agent_id, conversation_id, resource_id, title, metadata }) => {
    try {
      if (!mastra) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Mastra instance is required for conversations" });
      }
      const agent = await chunkEFJCOPJ3_cjs.getAgentFromSystem({ mastra, agentId: agent_id });
      const memory = await agent.getMemory({ requestContext });
      if (!memory) {
        throw new chunk64ITUOXI_cjs.HTTPException(400, { message: `Agent "${agent.id}" does not have memory configured` });
      }
      if (!await chunkKIDHIRVO_cjs.getAgentMemoryStore({ agent, requestContext })) {
        throw new chunk64ITUOXI_cjs.HTTPException(400, { message: `Memory storage is not configured for agent "${agent.id}"` });
      }
      const threadId = conversation_id ?? crypto.randomUUID();
      const resourceId = chunk42M6Y2OD_cjs.getEffectiveResourceId(requestContext, resource_id) ?? threadId;
      const thread = await memory.createThread({
        threadId,
        resourceId,
        title,
        metadata
      });
      return buildConversationObject({ thread });
    } catch (error) {
      return chunkZ7LCIYK7_cjs.handleError(error, "Error creating conversation");
    }
  }
});
var GET_CONVERSATION_ROUTE = chunkN54Q3AQR_cjs.createRoute({
  method: "GET",
  path: "/v1/conversations/:conversationId",
  responseType: "json",
  pathParamSchema: chunkNI2DJ2VW_cjs.conversationIdPathParams,
  responseSchema: chunkNI2DJ2VW_cjs.conversationObjectSchema,
  summary: "Retrieve a conversation",
  description: "Returns a conversation object backed by a Mastra memory thread",
  tags: ["Responses"],
  requiresAuth: true,
  requiresPermission: chunk42M6Y2OD_cjs.MastraFGAPermissions.AGENTS_READ,
  handler: async ({ mastra, requestContext, conversationId }) => {
    try {
      const match = await chunkKIDHIRVO_cjs.findConversationThreadAcrossAgents({ mastra, conversationId, requestContext });
      if (!match) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `Conversation ${conversationId} was not found` });
      }
      return buildConversationObject({ thread: match.thread });
    } catch (error) {
      return chunkZ7LCIYK7_cjs.handleError(error, "Error retrieving conversation");
    }
  }
});
var GET_CONVERSATION_ITEMS_ROUTE = chunkN54Q3AQR_cjs.createRoute({
  method: "GET",
  path: "/v1/conversations/:conversationId/items",
  responseType: "json",
  pathParamSchema: chunkNI2DJ2VW_cjs.conversationIdPathParams,
  responseSchema: chunkNI2DJ2VW_cjs.conversationItemsListSchema,
  summary: "List conversation items",
  description: "Returns OpenAI-style conversation items derived from the stored thread messages",
  tags: ["Responses"],
  requiresAuth: true,
  requiresPermission: chunk42M6Y2OD_cjs.MastraFGAPermissions.AGENTS_READ,
  handler: async ({ mastra, requestContext, conversationId }) => {
    try {
      const match = await chunkKIDHIRVO_cjs.findConversationThreadAcrossAgents({ mastra, conversationId, requestContext });
      if (!match) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `Conversation ${conversationId} was not found` });
      }
      const { messages } = await match.memoryStore.listMessages({
        threadId: conversationId,
        page: 0,
        perPage: 1e3
      });
      return buildConversationItemsList(chunkF3HP5HM6_cjs.mapMastraMessagesToConversationItems(messages));
    } catch (error) {
      return chunkZ7LCIYK7_cjs.handleError(error, "Error retrieving conversation");
    }
  }
});
var DELETE_CONVERSATION_ROUTE = chunkN54Q3AQR_cjs.createRoute({
  method: "DELETE",
  path: "/v1/conversations/:conversationId",
  responseType: "json",
  pathParamSchema: chunkNI2DJ2VW_cjs.conversationIdPathParams,
  responseSchema: chunkNI2DJ2VW_cjs.conversationDeletedSchema,
  summary: "Delete a conversation",
  description: "Deletes a thread-backed conversation and its stored items",
  tags: ["Responses"],
  requiresAuth: true,
  requiresPermission: chunk42M6Y2OD_cjs.MastraFGAPermissions.AGENTS_DELETE,
  handler: async ({ mastra, requestContext, conversationId }) => {
    try {
      const match = await chunkKIDHIRVO_cjs.findConversationThreadAcrossAgents({ mastra, conversationId, requestContext });
      if (!match) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `Conversation ${conversationId} was not found` });
      }
      await match.memoryStore.deleteThread({ threadId: conversationId });
      return buildConversationDeleted(conversationId);
    } catch (error) {
      return chunkZ7LCIYK7_cjs.handleError(error, "Error deleting conversation");
    }
  }
});

exports.CREATE_CONVERSATION_ROUTE = CREATE_CONVERSATION_ROUTE;
exports.DELETE_CONVERSATION_ROUTE = DELETE_CONVERSATION_ROUTE;
exports.GET_CONVERSATION_ITEMS_ROUTE = GET_CONVERSATION_ITEMS_ROUTE;
exports.GET_CONVERSATION_ROUTE = GET_CONVERSATION_ROUTE;
exports.conversations_exports = conversations_exports;
//# sourceMappingURL=chunk-SEOGVPOT.cjs.map
//# sourceMappingURL=chunk-SEOGVPOT.cjs.map