diff --git a/examples/agents/granite/granite_wiki_bee.ts b/examples/agents/granite/granite_wiki_bee.ts index 6a0e7abb..6424bab4 100644 --- a/examples/agents/granite/granite_wiki_bee.ts +++ b/examples/agents/granite/granite_wiki_bee.ts @@ -63,7 +63,7 @@ function wikipediaRetrivalTool(passageSize: number, overlap: number, maxResults: query: input.query, documents: output.results.flatMap((document) => Array.from( - splitString(document.fields.markdown as string, { + splitString(document.fields.markdown ?? "", { size: passageSize * charsPerToken, overlap: overlap * charsPerToken, }), diff --git a/examples/tools/custom/piping.ts b/examples/tools/custom/piping.ts index f4b7dbac..9d6bf88e 100644 --- a/examples/tools/custom/piping.ts +++ b/examples/tools/custom/piping.ts @@ -26,7 +26,7 @@ const wikipediaWithSimilarity = wikipedia .pipe(similarity, (input, output) => ({ query: input.query, documents: output.results.flatMap((document) => - Array.from(splitString(document.fields.markdown as string, { size: 1000, overlap: 50 })).map( + Array.from(splitString(document.fields.markdown ?? "", { size: 1000, overlap: 50 })).map( (chunk) => ({ text: chunk, source: document, diff --git a/src/internals/helpers/promise.ts b/src/internals/helpers/promise.ts index edc94590..41f37b27 100644 --- a/src/internals/helpers/promise.ts +++ b/src/internals/helpers/promise.ts @@ -69,7 +69,7 @@ export async function* emitterToGenerator(fn: EmitterToGeneratorFn) export async function asyncProperties>( obj: T, -): Promise<{ [K in keyof T]: T[K] extends Promise ? P : T[K] }> { +): Promise<{ [K in keyof T]: Awaited }> { return Object.fromEntries( await Promise.all(Object.entries(obj).map(async ([key, value]) => [key, await value])), ); diff --git a/src/tools/search/wikipedia.ts b/src/tools/search/wikipedia.ts index 908942bd..e2f86f0a 100644 --- a/src/tools/search/wikipedia.ts +++ b/src/tools/search/wikipedia.ts @@ -14,10 +14,11 @@ * limitations under the License. */ +import type { Page, pageFunctions, searchOptions } from "wikipedia"; import wiki from "wikipedia"; import { Cache } from "@/cache/decoratorCache.js"; import * as R from "remeda"; -import type { Page, pageFunctions, searchOptions } from "wikipedia"; +import { keys, mapValues } from "remeda"; import { ArrayKeys, Common } from "@/internals/types.js"; import { SearchToolOptions, @@ -27,11 +28,10 @@ import { } from "./base.js"; import { asyncProperties } from "@/internals/helpers/promise.js"; import { z } from "zod"; -import { ToolEmitter, Tool, ToolInput } from "@/tools/base.js"; +import { Tool, ToolEmitter, ToolInput } from "@/tools/base.js"; import Turndown from "turndown"; // @ts-expect-error missing types import turndownPlugin from "joplin-turndown-plugin-gfm"; -import { keys, mapValues } from "remeda"; import stringComparison from "string-comparison"; import { pageResult } from "wikipedia/dist/resultTypes.js"; import { Emitter } from "@/emitter/emitter.js"; @@ -80,8 +80,12 @@ export interface WikipediaToolRunOptions extends SearchToolRunOptions { output?: OutputOptions; } +type PageWithMarkdown = Page & { markdown: () => Promise }; + +type ResultFields = { [K in keyof PageFunctions]: Awaited> }; + export interface WikipediaToolResult extends SearchToolResult { - fields: Partial>; + fields: Partial; } export class WikipediaToolOutput extends SearchToolOutput { @@ -382,7 +386,7 @@ export class WikipediaTool extends Tool< mapValues(runOptions?.extraction?.fields ?? {}, (value, key) => this._mappers[key](page, runOptions) .then((response) => (value.transform ? value.transform(response) : response)) - .catch(() => null), + .catch(() => undefined), ), ), });