/** * March Agent SDK - Conversation Helper * Port of Python march_agent/conversation.py */ import { ConversationClient } from './conversation-client.js' import { ConversationMessage } from './conversation-message.js' // Types are inlined to avoid unused import warnings export interface GetHistoryOptions { limit?: number offset?: number } export interface GetAgentHistoryOptions extends GetHistoryOptions { agentName?: string } /** * Helper class for accessing conversation history. * Provides convenient methods for fetching messages. */ export class Conversation { readonly conversationId: string private readonly client: ConversationClient private readonly agentName?: string constructor( conversationId: string, client: ConversationClient, agentName?: string ) { this.conversationId = conversationId this.client = client this.agentName = agentName } /** * Get all messages in the conversation. */ async getHistory(options: GetHistoryOptions = {}): Promise { const messages = await this.client.getMessages(this.conversationId, { limit: options.limit ?? 100, offset: options.offset ?? 0, }) return messages.map((m) => ConversationMessage.fromApiResponse(m as unknown as Record)) } /** * Get messages to/from the current agent. * Useful for getting conversation history for a specific agent. */ async getAgentHistory(options: GetAgentHistoryOptions = {}): Promise { const targetAgent = options.agentName ?? this.agentName if (!targetAgent) { // If no agent name, return all messages return this.getHistory(options) } // Get messages where the agent is either sender or receiver const [toAgent, fromAgent] = await Promise.all([ this.client.getMessages(this.conversationId, { to: targetAgent, limit: options.limit ?? 50, offset: options.offset ?? 0, }), this.client.getMessages(this.conversationId, { from: targetAgent, limit: options.limit ?? 50, offset: options.offset ?? 0, }), ]) // Combine and sort by creation time const allMessages = [...toAgent, ...fromAgent] const uniqueMessages = Array.from( new Map(allMessages.map((m) => [m.id, m])).values() ) // Sort by createdAt uniqueMessages.sort((a, b) => { const dateA = new Date(a.createdAt).getTime() const dateB = new Date(b.createdAt).getTime() return dateA - dateB }) return uniqueMessages.map((m) => ConversationMessage.fromApiResponse(m as unknown as Record) ) } /** * Get the last N messages. */ async getLastMessages(count: number): Promise { return this.getHistory({ limit: count }) } /** * Get user messages only. */ async getUserMessages(options: GetHistoryOptions = {}): Promise { const messages = await this.client.getMessages(this.conversationId, { ...options, role: 'user', }) return messages.map((m) => ConversationMessage.fromApiResponse(m as unknown as Record)) } /** * Get assistant messages only. */ async getAssistantMessages(options: GetHistoryOptions = {}): Promise { const messages = await this.client.getMessages(this.conversationId, { ...options, role: 'assistant', }) return messages.map((m) => ConversationMessage.fromApiResponse(m as unknown as Record)) } }