import { conversationObjectSchema, createConversationBodySchema, conversationIdPathParams, conversationItemsListSchema, conversationDeletedSchema } from './chunk-LEKVIHSE.js';
import { mapMastraMessagesToConversationItems } from './chunk-TLLV2JP5.js';
import { getAgentMemoryStore, findConversationThreadAcrossAgents } from './chunk-L4DD3HSB.js';
import { getAgentFromSystem } from './chunk-5NBDNR2B.js';
import { MastraFGAPermissions, getEffectiveResourceId } from './chunk-JBSUT5XF.js';
import { handleError } from './chunk-7ZWJX3AN.js';
import { createRoute } from './chunk-PIKPS6Q6.js';
import { HTTPException } from './chunk-6QWQZI4Q.js';
import { __export } from './chunk-PR4QN5HX.js';
import { randomUUID } from 'crypto';

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

export { CREATE_CONVERSATION_ROUTE, DELETE_CONVERSATION_ROUTE, GET_CONVERSATION_ITEMS_ROUTE, GET_CONVERSATION_ROUTE, conversations_exports };
//# sourceMappingURL=chunk-4HSM2TU2.js.map
//# sourceMappingURL=chunk-4HSM2TU2.js.map