{"version":3,"sources":["../../src/tools/similarity.ts"],"names":["documentSchema","z","object","text","string","passthrough","SimilarityToolOutput","JSONToolOutput","SimilarityTool","Tool","name","description","emitter","Emitter","root","child","namespace","creator","inputSchema","query","documents","array","register","_run","options","run","pipe","provider","map","score","idx","documentIndex","filter","minScore","sortBy","prop","take","maxResults","Infinity","data","document","index"],"mappings":";;;;;;;;;AAkBA,MAAMA,cAAAA,GAAiBC,MAAEC,MAAAA,CAAO;AAAEC,EAAAA,IAAAA,EAAMC,UAAAA;AAAS,CAAA,EAAGC,WAAAA,EAAW;AAgCxD,MAAMC,6BAA6BC,uBAAAA,CAAAA;EAlD1C;;;AAkDkF;AAE3E,MAAMC,uBAAyCC,aAAAA,CAAAA;EApDtD;;;EAyDEC,IAAAA,GAAO,YAAA;EACPC,WAAAA,GAAc,8CAAA;EAEEC,OAAAA,GAA8DC,mBAAAA,CAAQC,KAAKC,KAAAA,CAAM;IAC/FC,SAAAA,EAAW;AAAC,MAAA,MAAA;AAAQ,MAAA;;IACpBC,OAAAA,EAAS;GACX,CAAA;EAEAC,WAAAA,GAAc;AACZ,IAAA,OAAOjB,MAAEC,MAAAA,CAAO;AAAEiB,MAAAA,KAAAA,EAAOlB,MAAEG,MAAAA,EAAM;MAAIgB,SAAAA,EAAWnB,KAAAA,CAAEoB,MAAMrB,cAAAA;KAAgB,CAAA;AAC1E,EAAA;EAEA;AACE,IAAA,IAAA,CAAKsB,QAAAA,EAAQ;AACf;AAEA,EAAA,MAAgBC,KACd,EAAEJ,KAAAA,EAAOC,SAAAA,EAAS,EAClBI,SACAC,GAAAA,EACA;AACA,IAAA,OAAOC,WAAAA,CACL,MAAM,IAAA,CAAKF,OAAAA,CAAQG,QAAAA,CACjB;AACER,MAAAA,KAAAA;AACAC,MAAAA;KACF,EACAI,OAAAA,EAASG,UACTF,GAAAA,CAAAA,EAEFG,WAAI,CAAC,EAAEC,KAAAA,EAAK,EAAIC,GAAAA,MAAS;MACvBC,aAAAA,EAAeD,GAAAA;AACfD,MAAAA;KACF,CAAA,CAAA,EACAG,aAAAA,CAAO,CAAC,EAAEH,KAAAA,EAAK,KAAOA,KAAAA,KAAUL,OAAAA,CAAQS,QAAAA,IAAY,CAAA,QAAA,CAAQ,CAAA,EAC5DC,aAAAA,CAAO;AAACC,MAAAA,WAAAA,CAAK,OAAA,CAAA;AAAU,MAAA;AAAO,KAAA,CAAA,EAC9BC,YAAKZ,OAAAA,EAASa,UAAAA,IAAc,KAAKb,OAAAA,CAAQa,UAAAA,IAAcC,QAAAA,CAAAA,EACvD,CAACC,IAAAA,KACC,IAAIjC,qBACFiC,IAAAA,CAAKX,GAAAA,CAAI,CAAC,EAAEG,aAAAA,EAAeF,OAAK,MAAQ;AACtCW,MAAAA,QAAAA,EAAUpB,UAAUW,aAAAA,CAAAA;MACpBU,KAAAA,EAAOV,aAAAA;AACPF,MAAAA;AACF,KAAA,CAAA,CAAA,CAAA,CAAA;AAGR,EAAA;AACF","file":"similarity.cjs","sourcesContent":["/**\n * Copyright 2025 © BeeAI a Series of LF Projects, LLC\n * SPDX-License-Identifier: Apache-2.0\n */\n\nimport {\n  BaseToolOptions,\n  BaseToolRunOptions,\n  ToolEmitter,\n  JSONToolOutput,\n  Tool,\n  ToolInput,\n} from \"./base.js\";\nimport { string, z } from \"zod\";\nimport { RunContext } from \"@/context.js\";\nimport { filter, map, pipe, prop, sortBy, take } from \"remeda\";\nimport { Emitter } from \"@/emitter/emitter.js\";\n\nconst documentSchema = z.object({ text: string() }).passthrough();\n\ntype Document = z.infer<typeof documentSchema>;\n\ninterface ProviderInput {\n  query: string;\n  documents: Document[];\n}\n\ntype Provider<TProviderOptions> = (\n  input: ProviderInput,\n  options: TProviderOptions | undefined,\n  run: RunContext<SimilarityTool<TProviderOptions>>,\n) => Promise<{ score: number }[]>;\n\nexport interface SimilarityToolOptions<TProviderOptions = unknown> extends BaseToolOptions {\n  provider: Provider<TProviderOptions>;\n  maxResults?: number;\n}\n\nexport interface SimilarityToolRunOptions<TProviderOptions = unknown> extends BaseToolRunOptions {\n  provider?: TProviderOptions;\n  maxResults?: number;\n  minScore?: number;\n}\n\nexport interface SimilarityToolResult {\n  document: Document;\n  index: number;\n  score: number;\n}\n\nexport class SimilarityToolOutput extends JSONToolOutput<SimilarityToolResult[]> {}\n\nexport class SimilarityTool<TProviderOptions> extends Tool<\n  SimilarityToolOutput,\n  SimilarityToolOptions<TProviderOptions>,\n  SimilarityToolRunOptions<TProviderOptions>\n> {\n  name = \"Similarity\";\n  description = \"Extract relevant information from documents.\";\n\n  public readonly emitter: ToolEmitter<ToolInput<this>, SimilarityToolOutput> = Emitter.root.child({\n    namespace: [\"tool\", \"similarity\"],\n    creator: this,\n  });\n\n  inputSchema() {\n    return z.object({ query: z.string(), documents: z.array(documentSchema) });\n  }\n\n  static {\n    this.register();\n  }\n\n  protected async _run(\n    { query, documents }: ToolInput<this>,\n    options: Partial<SimilarityToolRunOptions<TProviderOptions>>,\n    run: RunContext<this>,\n  ) {\n    return pipe(\n      await this.options.provider(\n        {\n          query,\n          documents,\n        },\n        options?.provider,\n        run,\n      ),\n      map(({ score }, idx) => ({\n        documentIndex: idx,\n        score,\n      })),\n      filter(({ score }) => score >= (options.minScore ?? -Infinity)),\n      sortBy([prop(\"score\"), \"desc\"]),\n      take(options?.maxResults ?? this.options.maxResults ?? Infinity),\n      (data) =>\n        new SimilarityToolOutput(\n          data.map(({ documentIndex, score }) => ({\n            document: documents[documentIndex],\n            index: documentIndex,\n            score,\n          })),\n        ),\n    );\n  }\n}\n"]}