{"version":3,"file":"base.cjs","names":[],"sources":["../../src/hub/base.ts"],"sourcesContent":["import type { BaseLanguageModel } from \"@langchain/core/language_models/base\";\nimport type { Runnable } from \"@langchain/core/runnables\";\n\nimport type { Client, ClientConfig } from \"langsmith\";\nimport type { PromptCommit } from \"langsmith/schemas\";\n\n/**\n * Push a prompt to the hub.\n * If the specified repo doesn't already exist, it will be created.\n * @param repoFullName The full name of the repo.\n * @param runnable The prompt to push.\n * @param options\n * @returns The URL of the newly pushed prompt in the hub.\n */\nexport async function basePush(\n  repoFullName: string,\n  runnable: Runnable,\n  options?: {\n    apiKey?: string;\n    apiUrl?: string;\n    parentCommitHash?: string;\n    /** @deprecated Use isPublic instead. */\n    newRepoIsPublic?: boolean;\n    isPublic?: boolean;\n    /** @deprecated Use description instead. */\n    newRepoDescription?: string;\n    description?: string;\n    readme?: string;\n    tags?: string[];\n  }\n): Promise<string> {\n  const Client = await loadLangSmith();\n  const client = new Client(options);\n  const payloadOptions = {\n    object: runnable,\n    parentCommitHash: options?.parentCommitHash,\n    isPublic: options?.isPublic ?? options?.newRepoIsPublic,\n    description: options?.description ?? options?.newRepoDescription,\n    readme: options?.readme,\n    tags: options?.tags,\n  };\n  return client.pushPrompt(repoFullName, payloadOptions);\n}\n\nexport async function basePull(\n  ownerRepoCommit: string,\n  options?: { apiKey?: string; apiUrl?: string; includeModel?: boolean }\n): Promise<PromptCommit> {\n  const Client = await loadLangSmith();\n  const client = new Client(options);\n\n  const promptObject = await client.pullPromptCommit(ownerRepoCommit, {\n    includeModel: options?.includeModel,\n  });\n\n  if (promptObject.manifest.kwargs?.metadata === undefined) {\n    promptObject.manifest.kwargs = {\n      ...promptObject.manifest.kwargs,\n      metadata: {},\n    };\n  }\n\n  promptObject.manifest.kwargs.metadata = {\n    ...promptObject.manifest.kwargs.metadata,\n    lc_hub_owner: promptObject.owner,\n    lc_hub_repo: promptObject.repo,\n    lc_hub_commit_hash: promptObject.commit_hash,\n  };\n\n  // Some nested mustache prompts have improperly parsed variables that include a dot.\n  if (promptObject.manifest.kwargs.template_format === \"mustache\") {\n    const stripDotNotation = (varName: string) => varName.split(\".\")[0];\n\n    const { input_variables } = promptObject.manifest.kwargs;\n    if (Array.isArray(input_variables)) {\n      promptObject.manifest.kwargs.input_variables =\n        input_variables.map(stripDotNotation);\n    }\n\n    const { messages } = promptObject.manifest.kwargs;\n    if (Array.isArray(messages)) {\n      // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n      promptObject.manifest.kwargs.messages = messages.map((message: any) => {\n        const nestedVars = message?.kwargs?.prompt?.kwargs?.input_variables;\n        if (Array.isArray(nestedVars)) {\n          message.kwargs.prompt.kwargs.input_variables =\n            nestedVars.map(stripDotNotation);\n        }\n        return message;\n      });\n    }\n  }\n  return promptObject;\n}\n\nexport function generateModelImportMap(\n  // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n  modelClass?: new (...args: any[]) => BaseLanguageModel\n) {\n  // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n  const modelImportMap: Record<string, any> = {};\n  if (modelClass !== undefined) {\n    // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n    const modelLcName = (modelClass as any)?.lc_name();\n    let importMapKey;\n    if (modelLcName === \"ChatOpenAI\") {\n      importMapKey = \"chat_models__openai\";\n    } else if (modelLcName === \"ChatAnthropic\") {\n      importMapKey = \"chat_models__anthropic\";\n    } else if (modelLcName === \"ChatAzureOpenAI\") {\n      importMapKey = \"chat_models__openai\";\n    } else if (modelLcName === \"ChatVertexAI\") {\n      importMapKey = \"chat_models__vertexai\";\n    } else if (modelLcName === \"ChatGoogleGenerativeAI\") {\n      importMapKey = \"chat_models__google_genai\";\n    } else if (modelLcName === \"ChatBedrockConverse\") {\n      importMapKey = \"chat_models__chat_bedrock_converse\";\n    } else if (modelLcName === \"ChatMistral\") {\n      importMapKey = \"chat_models__mistralai\";\n    } else if (modelLcName === \"ChatFireworks\") {\n      importMapKey = \"chat_models__fireworks\";\n    } else if (modelLcName === \"ChatGroq\") {\n      importMapKey = \"chat_models__groq\";\n    } else {\n      throw new Error(\"Received unsupported model class when pulling prompt.\");\n    }\n    modelImportMap[importMapKey] = {\n      ...modelImportMap[importMapKey],\n      [modelLcName]: modelClass,\n    };\n  }\n  return modelImportMap;\n}\n\nexport function generateOptionalImportMap(\n  // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n  modelClass?: new (...args: any[]) => BaseLanguageModel\n) {\n  // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n  const optionalImportMap: Record<string, any> = {};\n  if (modelClass !== undefined) {\n    // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n    const modelLcName = (modelClass as any)?.lc_name();\n    let optionalImportMapKey;\n    if (modelLcName === \"ChatGoogleGenerativeAI\") {\n      optionalImportMapKey = \"langchain_google_genai/chat_models\";\n    } else if (modelLcName === \"ChatBedrockConverse\") {\n      optionalImportMapKey = \"langchain_aws/chat_models\";\n    } else if (modelLcName === \"ChatGroq\") {\n      optionalImportMapKey = \"langchain_groq/chat_models\";\n    }\n    if (optionalImportMapKey !== undefined) {\n      optionalImportMap[optionalImportMapKey] = {\n        [modelLcName]: modelClass,\n      };\n    }\n  }\n  return optionalImportMap;\n}\n\nexport function bindOutputSchema<T extends Runnable>(loadedSequence: T) {\n  if (\n    \"first\" in loadedSequence &&\n    loadedSequence.first !== null &&\n    typeof loadedSequence.first === \"object\" &&\n    \"schema\" in loadedSequence.first &&\n    \"last\" in loadedSequence &&\n    loadedSequence.last !== null &&\n    typeof loadedSequence.last === \"object\"\n  ) {\n    if (\n      \"bound\" in loadedSequence.last &&\n      loadedSequence.last.bound !== null &&\n      typeof loadedSequence.last.bound === \"object\" &&\n      \"withStructuredOutput\" in loadedSequence.last.bound &&\n      typeof loadedSequence.last.bound.withStructuredOutput === \"function\"\n    ) {\n      loadedSequence.last.bound =\n        loadedSequence.last.bound.withStructuredOutput(\n          loadedSequence.first.schema\n        );\n    } else if (\n      \"withStructuredOutput\" in loadedSequence.last &&\n      typeof loadedSequence.last.withStructuredOutput === \"function\"\n    ) {\n      loadedSequence.last = loadedSequence.last.withStructuredOutput(\n        loadedSequence.first.schema\n      );\n    }\n  }\n  return loadedSequence;\n}\n\n/**\n * Dynamically load the LangSmith client.\n * @returns The LangSmith client.\n */\nasync function loadLangSmith(): Promise<new (config?: ClientConfig) => Client> {\n  try {\n    const { Client } = await import(\"langsmith\");\n    return Client;\n  } catch (error) {\n    // oxlint-disable-next-line no-instanceof/no-instanceof\n    const errorMessage = error instanceof Error ? error.message : String(error);\n    throw new Error(\n      `Error loading \"langsmith\" package, install it via \\`npm install langsmith\\` before you use this function.\\nError: ${errorMessage}`\n    );\n  }\n}\n"],"mappings":";;;;;;;;;AAcA,eAAsB,SACpB,cACA,UACA,SAaiB;CAEjB,MAAM,SAAS,KADA,OAAM,eAAe,GACV,QAAQ;CAClC,MAAM,iBAAiB;EACrB,QAAQ;EACR,kBAAkB,SAAS;EAC3B,UAAU,SAAS,YAAY,SAAS;EACxC,aAAa,SAAS,eAAe,SAAS;EAC9C,QAAQ,SAAS;EACjB,MAAM,SAAS;EAChB;AACD,QAAO,OAAO,WAAW,cAAc,eAAe;;AAGxD,eAAsB,SACpB,iBACA,SACuB;CAIvB,MAAM,eAAe,MAFN,KADA,OAAM,eAAe,GACV,QAAQ,CAEA,iBAAiB,iBAAiB,EAClE,cAAc,SAAS,cACxB,CAAC;AAEF,KAAI,aAAa,SAAS,QAAQ,aAAa,KAAA,EAC7C,cAAa,SAAS,SAAS;EAC7B,GAAG,aAAa,SAAS;EACzB,UAAU,EAAE;EACb;AAGH,cAAa,SAAS,OAAO,WAAW;EACtC,GAAG,aAAa,SAAS,OAAO;EAChC,cAAc,aAAa;EAC3B,aAAa,aAAa;EAC1B,oBAAoB,aAAa;EAClC;AAGD,KAAI,aAAa,SAAS,OAAO,oBAAoB,YAAY;EAC/D,MAAM,oBAAoB,YAAoB,QAAQ,MAAM,IAAI,CAAC;EAEjE,MAAM,EAAE,oBAAoB,aAAa,SA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