diff --git a/src/create-ai-chain.ts b/src/create-ai-chain.ts index cf31d98..adda7c2 100644 --- a/src/create-ai-chain.ts +++ b/src/create-ai-chain.ts @@ -1,4 +1,5 @@ import type { SetOptional } from 'type-fest' +import type { z } from 'zod' import pMap from 'p-map' import type * as types from './types.js' @@ -8,6 +9,19 @@ import { Msg } from './message.js' import { asSchema, augmentSystemMessageWithJsonSchema } from './schema.js' import { getErrorMessage } from './utils.js' +export type AIChainParams = { + chatFn: types.ChatFn + params?: types.Simplify< + Partial> + > + tools?: types.AIFunctionLike[] + schema?: z.ZodType | types.Schema + maxCalls?: number + maxRetries?: number + toolCallConcurrency?: number + injectSchemaIntoSystemMessage?: boolean +} + /** * Creates a chain of chat completion calls that can be invoked as a single * function. It is meant to simplify the process of resolving tool calls @@ -32,7 +46,7 @@ export function createAIChain({ maxRetries = 2, toolCallConcurrency = 8, injectSchemaIntoSystemMessage = true -}: types.AIChainParams): types.AIChain { +}: AIChainParams): types.AIChain { const functionSet = new AIFunctionSet(tools) const defaultParams: Partial | undefined = rawSchema && !functionSet.size diff --git a/src/extract-object.ts b/src/extract-object.ts index 458cc64..484ccd0 100644 --- a/src/extract-object.ts +++ b/src/extract-object.ts @@ -1,8 +1,18 @@ import type * as types from './types.js' -import { createAIChain } from './create-ai-chain.js' +import { type AIChainParams, createAIChain } from './create-ai-chain.js' + +export type ExtractObjectParams = + types.Simplify< + types.SetRequired< + Omit, 'tools' | 'toolCallConcurrency' | 'params'>, + 'schema' + > & { + params: types.SetRequired, 'messages'> + } + > export function extractObject( - args: types.ExtractObjectParams + args: ExtractObjectParams ): Promise { const chain = createAIChain(args) return chain() diff --git a/src/fns.ts b/src/fns.ts index 7f420da..ac0e9ac 100644 --- a/src/fns.ts +++ b/src/fns.ts @@ -39,7 +39,10 @@ export abstract class AIFunctionsProvider { get functions(): AIFunctionSet { if (!this._functions) { const metadata = this.constructor[Symbol.metadata] - assert(metadata) + assert( + metadata, + 'Your runtime does not appear to support ES decorator metadata: https://github.com/tc39/proposal-decorator-metadata/issues/14' + ) const invocables = (metadata?.invocables as PrivateAIFunctionMetadata[]) ?? [] // console.log({ metadata, invocables }) diff --git a/src/services/jina-client.ts b/src/services/jina-client.ts index e2408c8..507f3de 100644 --- a/src/services/jina-client.ts +++ b/src/services/jina-client.ts @@ -75,6 +75,12 @@ export namespace jina { } } + export interface ReaderResponseHtml extends JinaResponse { + data: { + html: string + } + } + export interface SearchResponse extends JinaResponse { data: ReaderData[] } @@ -159,7 +165,9 @@ export class JinaClient extends AIFunctionsProvider { ? T['json'] extends true ? T['returnFormat'] extends 'screenshot' ? jina.ReaderResponseScreenshot - : jina.ReaderResponse + : T['returnFormat'] extends 'html' + ? jina.ReaderResponseHtml + : jina.ReaderResponse : T['returnFormat'] extends 'screenshot' ? ArrayBuffer : string diff --git a/src/types.ts b/src/types.ts index ecf0226..f865667 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,16 +1,15 @@ -import type { Jsonifiable, SetOptional, SetRequired, Simplify } from 'type-fest' +import type { Jsonifiable, SetOptional, Simplify } from 'type-fest' import type { z } from 'zod' import type { AIFunctionSet } from './ai-function-set.js' import type { AIFunctionsProvider } from './fns.js' import type { Msg } from './message.js' -import type { Schema } from './schema.js' export type { Msg } from './message.js' export type { Schema } from './schema.js' export type { KyInstance } from 'ky' export type { ThrottledFunction } from 'p-throttle' -export type { Simplify } from 'type-fest' +export type { SetRequired, Simplify } from 'type-fest' export type Nullable = T | null @@ -136,24 +135,3 @@ export type SafeParseResult = } export type ValidatorFn = (value: unknown) => SafeParseResult - -export type AIChainParams = { - chatFn: ChatFn - params?: Simplify>> - tools?: AIFunctionLike[] - schema?: z.ZodType | Schema - maxCalls?: number - maxRetries?: number - toolCallConcurrency?: number - injectSchemaIntoSystemMessage?: boolean -} - -export type ExtractObjectParams = - Simplify< - SetRequired< - Omit, 'tools' | 'toolCallConcurrency' | 'params'>, - 'schema' - > & { - params: SetRequired, 'messages'> - } - >