{"version":3,"file":"title.cjs","sources":["../../../src/utils/title.ts"],"sourcesContent":["import { ChatPromptTemplate } from '@langchain/core/prompts';\nimport { RunnableLambda, RunnableSequence } from '@langchain/core/runnables';\nimport type { Runnable, RunnableConfig } from '@langchain/core/runnables';\nimport type { AIMessage } from '@langchain/core/messages';\nimport type * as t from '@/types';\nimport { ContentTypes } from '@/common';\n\nconst defaultTitlePrompt = `Analyze this conversation and provide:\n1. The detected language of the conversation\n2. A concise title in the detected language (5 words or less, no punctuation or quotation)\n\n{convo}`;\n\nconst titleSchema = {\n  type: 'object',\n  properties: {\n    title: {\n      type: 'string',\n      description:\n        'A concise title for the conversation in 5 words or less, without punctuation or quotation',\n    },\n  },\n  required: ['title'],\n} as const;\n\nconst combinedSchema = {\n  type: 'object',\n  properties: {\n    language: {\n      type: 'string',\n      description: 'The detected language of the conversation',\n    },\n    title: {\n      type: 'string',\n      description:\n        'A concise title for the conversation in 5 words or less, without punctuation or quotation',\n    },\n  },\n  required: ['language', 'title'],\n} as const;\n\nexport const createTitleRunnable = async (\n  model: t.ChatModelInstance,\n  _titlePrompt?: string\n): Promise<Runnable> => {\n  // Disabled since this works fine\n  // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n  /* @ts-ignore */\n  const titleLLM = model.withStructuredOutput(titleSchema);\n  // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n  /* @ts-ignore */\n  const combinedLLM = model.withStructuredOutput(combinedSchema);\n\n  const titlePrompt = ChatPromptTemplate.fromTemplate(\n    _titlePrompt ?? defaultTitlePrompt\n  ).withConfig({ runName: 'TitlePrompt' });\n\n  const titleOnlyInnerChain = RunnableSequence.from([titlePrompt, titleLLM]);\n  const combinedInnerChain = RunnableSequence.from([titlePrompt, combinedLLM]);\n\n  /** Wrap titleOnlyChain in RunnableLambda to create parent span */\n  const titleOnlyChain = new RunnableLambda({\n    func: async (\n      input: { convo: string },\n      config?: Partial<RunnableConfig>\n    ): Promise<{ title: string }> => {\n      const result = await titleOnlyInnerChain.invoke(input, config);\n      return result as { title: string };\n    },\n  }).withConfig({ runName: 'TitleOnlyChain' });\n\n  /** Wrap combinedChain in RunnableLambda to create parent span */\n  const combinedChain = new RunnableLambda({\n    func: async (\n      input: { convo: string },\n      config?: Partial<RunnableConfig>\n    ): Promise<{ language: string; title: string }> => {\n      const result = await combinedInnerChain.invoke(input, config);\n      return result as { language: string; title: string };\n    },\n  }).withConfig({ runName: 'TitleLanguageChain' });\n\n  /** Runnable to add default values if needed */\n  const addDefaults = new RunnableLambda({\n    func: (\n      result: { language: string; title: string } | undefined\n    ): { language: string; title: string } => ({\n      language: result?.language ?? 'English',\n      title: result?.title ?? '',\n    }),\n  }).withConfig({ runName: 'AddDefaults' });\n\n  const combinedChainInner = RunnableSequence.from([\n    combinedChain,\n    addDefaults,\n  ]);\n\n  /** Wrap combinedChainWithDefaults in RunnableLambda to create parent span */\n  const combinedChainWithDefaults = new RunnableLambda({\n    func: async (\n      input: { convo: string },\n      config?: Partial<RunnableConfig>\n    ): Promise<{ language: string; title: string }> => {\n      return await combinedChainInner.invoke(input, config);\n    },\n  }).withConfig({ runName: 'CombinedChainWithDefaults' });\n\n  return new RunnableLambda({\n    func: async (\n      input: {\n        convo: string;\n        inputText: string;\n        skipLanguage: boolean;\n      },\n      config?: Partial<RunnableConfig>\n    ): Promise<{ language: string; title: string } | { title: string }> => {\n      const invokeInput = { convo: input.convo };\n\n      if (input.skipLanguage) {\n        return (await titleOnlyChain.invoke(invokeInput, config)) as {\n          title: string;\n        };\n      }\n\n      return await combinedChainWithDefaults.invoke(invokeInput, config);\n    },\n  }).withConfig({ runName: 'TitleGenerator' });\n};\n\nconst defaultCompletionPrompt = `Provide a concise, 5-word-or-less title for the conversation, using title case conventions. Only return the title itself.\n\nConversation:\n{convo}`;\n\nexport const createCompletionTitleRunnable = async (\n  model: t.ChatModelInstance,\n  titlePrompt?: string\n): Promise<Runnable> => {\n  const completionPrompt = ChatPromptTemplate.fromTemplate(\n    titlePrompt ?? defaultCompletionPrompt\n  ).withConfig({ runName: 'CompletionTitlePrompt' });\n\n  /** Runnable to extract content from model response */\n  const extractContent = new RunnableLambda({\n    func: (response: AIMessage): { title: string } => {\n      let content = '';\n      if (typeof response.content === 'string') {\n        content = response.content;\n      } else if (Array.isArray(response.content)) {\n        content = response.content\n          .filter(\n            (part): part is { type: ContentTypes.TEXT; text: string } =>\n              part.type === ContentTypes.TEXT\n          )\n          .map((part) => part.text)\n          .join('');\n      }\n      return { title: content.trim() };\n    },\n  }).withConfig({ runName: 'ExtractTitle' });\n\n  const innerChain = RunnableSequence.from([\n    completionPrompt,\n    model,\n    extractContent,\n  ]);\n\n  /** Wrap in RunnableLambda to create a parent span for LangFuse */\n  return new RunnableLambda({\n    func: async (\n      input: { convo: string },\n      config?: Partial<RunnableConfig>\n    ): Promise<{ title: string }> => {\n      return await innerChain.invoke(input, config);\n    },\n  }).withConfig({ runName: 'CompletionTitleChain' });\n};\n"],"names":["ChatPromptTemplate","RunnableSequence","RunnableLambda","ContentTypes"],"mappings":";;;;;;;AAOA,MAAM,kBAAkB,GAAG,CAAA;;;;QAInB;AAER,MAAM,WAAW,GAAG;AAClB,IAAA,IAAI,EAAE,QAAQ;AACd,IAAA,UAAU,EAAE;AACV,QAAA,KAAK,EAAE;AACL,YAAA,IAAI,EAAE,QAAQ;AACd,YAAA,WAAW,EACT,2FAA2F;AAC9F,SAAA;AACF,KAAA;IACD,QAAQ,EAAE,CAAC,OAAO,CAAC;CACX;AAEV,MAAM,cAAc,GAAG;AACrB,IAAA,IAAI,EAAE,QAAQ;AACd,IAAA,UAAU,EAAE;AACV,QAAA,QAAQ,EAAE;AACR,YAAA,IAAI,EAAE,QAAQ;AACd,YAAA,WAAW,EAAE,2CAA2C;AACzD,SAAA;AACD,QAAA,KAAK,EAAE;AACL,YAAA,IAAI,EAAE,QAAQ;AACd,YAAA,WAAW,EACT,2FAA2F;AAC9F,SAAA;AACF,KAAA;AACD,IAAA,QAAQ,EAAE,CAAC,UAAU,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