{"version":3,"sources":["../../src/tools/llm.ts"],"names":["LLMTool","Tool","name","description","input","emitter","Emitter","root","child","namespace","toCamelCase","filter","Boolean","creator","inputSchema","z","object","task","string","min","describe","template","PromptTemplate","schema","_run","_options","run","memory","getProp","context","contextKeys","Memory","ToolError","isFatal","isRetryable","options","output","llm","create","messages","SystemMessage","render","msg","role","Role","SYSTEM","UserMessage","StringToolOutput","getTextContent"],"mappings":";;;;;;;;;;;;AA+BO,MAAMA,gBAAgBC,aAAAA,CAAAA;EA/B7B;;;;EAgCEC,IAAAA,GAAO,KAAA;EACPC,WAAAA,GACE,wHAAA;AAGF,EAAA,WAAA,CAA+BC,KAAAA,EAAqB;AAClD,IAAA,KAAA,CAAMA,KAAAA,CAAAA,EAAAA,IAAAA,CADuBA,KAAAA,GAAAA,KAAAA;AAE7B,IAAA,IAAA,CAAKF,IAAAA,GAAOE,KAAAA,EAAOF,IAAAA,IAAQ,IAAA,CAAKA,IAAAA;AAChC,IAAA,IAAA,CAAKC,WAAAA,GAAcC,KAAAA,EAAOD,WAAAA,IAAe,IAAA,CAAKA,WAAAA;AAC9C,IAAA,IAAA,CAAKE,OAAAA,GAAUC,mBAAAA,CAAQC,IAAAA,CAAKC,KAAAA,CAAM;MAChCC,SAAAA,EAAW;AAAC,QAAA,MAAA;AAAQ,QAAA,KAAA;QAAOC,kBAAAA,CAAYN,KAAAA,EAAOF,QAAQ,EAAA;AAAKS,OAAAA,CAAAA,MAAAA,CAAOC,OAAAA,CAAAA;MAClEC,OAAAA,EAAS;KACX,CAAA;AACF,EAAA;EAEAC,WAAAA,GAAc;AACZ,IAAA,OAAOC,MAAEC,MAAAA,CAAO;AACdC,MAAAA,IAAAA,EAAMF,MAAEG,MAAAA,EAAM,CAAGC,IAAI,CAAA,CAAA,CAAGC,SAAS,iDAAA;KACnC,CAAA;AACF,EAAA;EAEA,OAAgBC,QAAAA,GAAW,IAAIC,2BAAAA,CAAe;AAC5CC,IAAAA,MAAAA,EAAQR,MAAEC,MA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ier: Apache-2.0\n */\n\nimport {\n  BaseToolOptions,\n  BaseToolRunOptions,\n  StringToolOutput,\n  Tool,\n  ToolEmitter,\n  ToolError,\n  ToolInput,\n} from \"@/tools/base.js\";\nimport { z } from \"zod\";\nimport { GetRunContext } from \"@/context.js\";\nimport { Emitter } from \"@/emitter/emitter.js\";\nimport { PromptTemplate } from \"@/template.js\";\nimport { getProp } from \"@/internals/helpers/object.js\";\nimport type { BaseMemory } from \"@/memory/base.js\";\nimport { toCamelCase } from \"remeda\";\nimport { Role, SystemMessage, UserMessage } from \"@/backend/message.js\";\nimport { ChatModel } from \"@/backend/chat.js\";\n\nexport interface LLMToolInput extends BaseToolOptions {\n  llm: ChatModel;\n  name?: string;\n  description?: string;\n  template?: typeof LLMTool.template;\n}\n\nexport class LLMTool extends Tool<StringToolOutput, LLMToolInput> {\n  name = \"LLM\";\n  description =\n    \"Uses expert LLM to work with data in the existing conversation (classification, entity extraction, summarization, ...)\";\n  declare readonly emitter: ToolEmitter<ToolInput<this>, StringToolOutput>;\n\n  constructor(protected readonly input: LLMToolInput) {\n    super(input);\n    this.name = input?.name || this.name;\n    this.description = input?.description || this.description;\n    this.emitter = Emitter.root.child({\n      namespace: [\"tool\", \"llm\", toCamelCase(input?.name ?? \"\")].filter(Boolean),\n      creator: this,\n    });\n  }\n\n  inputSchema() {\n    return z.object({\n      task: z.string().min(1).describe(\"A clearly defined task for the LLM to complete.\"),\n    });\n  }\n\n  static readonly template = new PromptTemplate({\n    schema: z.object({\n      task: z.string(),\n    }),\n    template: `Use common sense and the information contained in the conversation up to this point to complete the following task. Do not follow any previously used formats or structures.\n\nThe Task: {{task}}`,\n  });\n\n  protected async _run(\n    input: ToolInput<this>,\n    _options: Partial<BaseToolRunOptions>,\n    run: GetRunContext<this>,\n  ) {\n    const memory = getProp(run.context, [Tool.contextKeys.Memory]) as BaseMemory;\n    if (!memory) {\n      throw new ToolError(`No context has been provided!`, [], {\n        isFatal: true,\n        isRetryable: false,\n      });\n    }\n\n    const template = this.options?.template ?? LLMTool.template;\n    const output = await this.input.llm.create({\n      messages: [\n        new SystemMessage(\n          template.render({\n            task: input.task,\n          }),\n        ),\n        ...memory.messages.filter((msg) => msg.role !== Role.SYSTEM),\n        new UserMessage(\n          template.render({\n            task: input.task,\n          }),\n        ),\n      ],\n    });\n\n    return new StringToolOutput(output.getTextContent());\n  }\n}\n"]}