{"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":";;;;;;;;;AA6BA,MAAMA,cAAAA,GAAiBC,MAAEC,MAAO,CAAA;AAAEC,EAAAA,IAAAA,EAAMC,UAAAA;AAAS,CAAA,EAAGC,WAAW,EAAA;AAgCxD,MAAMC,6BAA6BC,uBAAAA,CAAAA;EA7D1C;;;AA6DkF;AAE3E,MAAMC,uBAAyCC,aAAAA,CAAAA;EA/DtD;;;EAoEEC,IAAO,GAAA,YAAA;EACPC,WAAc,GAAA,8CAAA;EAEEC,OAA8DC,GAAAA,mBAAAA,CAAQC,KAAKC,KAAM,CAAA;IAC/FC,SAAW,EAAA;AAAC,MAAA,MAAA;AAAQ,MAAA;;IACpBC,OAAS,EAAA;GACX,CAAA;EAEAC,WAAc,GAAA;AACZ,IAAA,OAAOjB,MAAEC,MAAO,CAAA;AAAEiB,MAAAA,KAAAA,EAAOlB,MAAEG,MAAM,EAAA;MAAIgB,SAAWnB,EAAAA,KAAAA,CAAEoB,MAAMrB,cAAAA;KAAgB,CAAA;AAC1E;EAEA;AACE,IAAA,IAAA,CAAKsB,QAAQ,EAAA;AACf;AAEA,EAAA,MAAgBC,KACd,EAAEJ,KAAAA,EAAOC,SAAS,EAAA,EAClBI,SACAC,GACA,EAAA;AACA,IAAA,OAAOC,WACL,CAAA,MAAM,IAAKF,CAAAA,OAAAA,CAAQG,QACjB,CAAA;AACER,MAAAA,KAAAA;AACAC,MAAAA;KAEFI,EAAAA,OAAAA,EAASG,UACTF,GAAAA,CAAAA,EAEFG,WAAI,CAAC,EAAEC,KAAK,EAAA,EAAIC,GAAS,MAAA;MACvBC,aAAeD,EAAAA,GAAAA;AACfD,MAAAA;KACF,CAAA,CAAA,EACAG,aAAO,CAAA,CAAC,EAAEH,KAAAA,EAAYA,KAAAA,KAAAA,KAAUL,OAAQS,CAAAA,QAAAA,IAAY,CAAQ,QAAA,CAAA,CAAA,EAC5DC,aAAO,CAAA;AAACC,MAAAA,WAAAA,CAAK,OAAA,CAAA;AAAU,MAAA;AAAO,KAAA,CAAA,EAC9BC,YAAKZ,OAASa,EAAAA,UAAAA,IAAc,KAAKb,OAAQa,CAAAA,UAAAA,IAAcC,QAAAA,CACvD,EAAA,CAACC,IACC,KAAA,IAAIjC,qBACFiC,IAAKX,CAAAA,GAAAA,CAAI,CAAC,EAAEG,aAAAA,EAAeF,OAAa,MAAA;AACtCW,MAAAA,QAAAA,EAAUpB,UAAUW,aAAAA,CAAAA;MACpBU,KAAOV,EAAAA,aAAAA;AACPF,MAAAA;AACF,KAAA,CAAA,CAAA,CAAA,CAAA;AAGR;AACF","file":"similarity.cjs","sourcesContent":["/**\n * Copyright 2025 IBM Corp.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\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"]}