UNPKG

openai

Version:

The official TypeScript library for the OpenAI API

145 lines • 9.43 kB
import type { RequestOptions } from "../internal/request-options.js"; import type { ChatCompletionTool } from "../resources/chat/completions.js"; import type { FunctionTool, ParsedResponse, Response, ResponseCreateParamsBase, ResponseCreateParamsNonStreaming, ResponseFormatTextJSONSchemaConfig, ResponseFunctionToolCall, ResponseInputItem, ResponseTextConfig, Tool } from "../resources/responses/responses.js"; import type { AutoParseableTextFormat } from "../lib/parser.js"; /** Response tools, a compatible chat completion tool, or no tools. */ export type ParseableToolsParams = Tool[] | ChatCompletionTool | null; /** Response-creation parameters that may include tools recognized by parsing helpers. */ export type ResponseCreateParamsWithTools = ResponseCreateParamsBase & { /** Tools available to the model while producing the response. */ tools?: ParseableToolsParams; } & ResponseParsingInput; /** Include tuples so inline input histories retain their order for parser inference. */ export type ResponseParsingInput = { input?: ResponseCreateParamsBase['input'] | [ResponseInputItem, ...ResponseInputItem[]]; }; /** Local parser settings for Responses helpers, alongside ordinary request options. */ export type ResponseParseOptions = RequestOptions & { /** Parse with this format without sending it to the API. Pass null to leave output text unparsed. */ parseAs?: ResponseFormatTextJSONSchemaConfig | null | undefined; }; /** Request fields that can select a plain-text or structured Responses API output format. */ type TextConfigParams = { /** Optional text output configuration, including its structured JSON format. */ text?: ResponseTextConfig; input?: ResponseCreateParamsBase['input']; }; /** Infers parsed output for each member of a possibly optional text-format union. */ type ParsedTextFormat<Format> = [Format] extends [never] ? null : Format extends AutoParseableTextFormat<infer ParsedT> ? ParsedT : Format extends ResponseFormatTextJSONSchemaConfig ? unknown : null; type UpdatedContent<Item> = Item extends { type: 'configuration_update'; text?: infer Text; } ? NonNullable<Text> extends { format: infer Format; } ? ParsedTextFormat<Format> : never : never; type ParsedHistory<Input, Fallback, Later = never, Seen extends 0[] = []> = [Input] extends [never] ? Fallback | Later : Input extends readonly [...infer Earlier, infer Last] ? Last extends { type: 'configuration_update'; text: { format: infer Format; }; } ? ParsedTextFormat<Format> | Later : Seen['length'] extends 256 ? Fallback | Later | UpdatedContent<Input[number]> : ParsedHistory<Earlier, Fallback, Later | UpdatedContent<Last>, [...Seen, 0]> : Input extends readonly (infer Item)[] ? Fallback | Later | UpdatedContent<Item> : Fallback | Later; type ParsedOverride<Format, Fallback> = Format extends undefined ? Fallback : ParsedTextFormat<Format>; /** An explicit local parser takes precedence over the request's inferred output type. */ export type ParsedWithOptions<Options extends ResponseParseOptions, Fallback> = Options extends unknown ? 'parseAs' extends keyof Options ? ParsedOverride<Options['parseAs'], Fallback> : Fallback : never; type ParsingHistory<Params extends TextConfigParams> = Params extends { input: infer Input; } ? Input : ResponseCreateParamsBase['input'] extends Params['input'] ? never : Params['input']; /** * Resolves the type of `output_parsed` / `content[].parsed` for the given params. * * This must stay in sync with `isParseableResponseFormat()` and * `parseResponseFormatContent()`: formats built by an SDK helper carry their parsed * type in the brand, while a raw `{ type: 'json_schema' }` format is parsed with * `JSON.parse()` and so can only be described as `unknown`. */ export type ExtractParsedContentFromParams<Params extends TextConfigParams> = ParsedHistory<ParsingHistory<Params>, ParsedTextFormat<NonNullable<Params['text']>['format']>>; /** Only the discriminator is required to select the parser carried by a format object. */ export type ResponseParserTextFormat = { readonly type: string; } | null; /** Strip local options before sending and resolve the format against the prepared request body. */ export declare function resolveResponseParseOptions(params: TextConfigParams, options?: ResponseParseOptions): { getTextFormat: (prepared: RequestOptions) => ResponseParserTextFormat; requestOptions: { method?: import("../internal/types.js").HTTPMethod; path?: string; query?: object | undefined | null; body?: unknown; headers?: import("../internal/headers.js").HeadersLike; maxRetries?: number; stream?: boolean | undefined; timeout?: number; fetchOptions?: import("../internal/types.js").MergedRequestInit; signal?: AbortSignal | undefined | null; idempotencyKey?: string; defaultBaseURL?: string | undefined; __metadata?: Record<string, unknown>; __security?: { bearerAuth?: boolean; adminAPIKeyAuth?: boolean; }; __binaryResponse?: boolean | undefined; __streamClass?: typeof import("../streaming.js").Stream; __synthesizeEventData?: boolean; __streamMode?: "legacy" | "events"; parseAs?: ResponseFormatTextJSONSchemaConfig | null | undefined; }; }; /** * Adds parsed-output fields to a response, invoking parsers only when its request * includes an auto-parseable text format or strict function tool. */ export declare function maybeParseResponse<Params extends ResponseCreateParamsBase | null, ParsedT = Params extends null ? null : ExtractParsedContentFromParams<NonNullable<Params>>>(response: Response, params: Params, format?: ResponseParserTextFormat): ParsedResponse<ParsedT>; /** * Parses completed response text and strict function-tool arguments, matching * namespaced functions by both namespace and name. * * Incomplete responses and messages with an explicit non-final phase stay unparsed. * `output_parsed` returns the first successfully parsed output-text item. */ export declare function parseResponse<Params extends ResponseCreateParamsBase, ParsedT = ExtractParsedContentFromParams<Params>>(response: Response, params: Params, format?: ResponseParserTextFormat): ParsedResponse<ParsedT>; /** Returns whether the request includes an auto-parseable text format or strict function tool. */ export declare function hasAutoParseableInput(params: ResponseCreateParamsWithTools, format?: ResponseParserTextFormat): boolean; /** Type-level details used to infer a Responses API function tool's parser and callback. */ type ToolOptions = { /** Model-visible function name used to match generated tool calls. */ name: string; /** Parsed argument value accepted by the optional execution callback. */ arguments: any; /** Optional callback metadata associated with the parsed function tool. */ function?: ((args: any) => any) | undefined; }; /** A Responses API function tool with an argument parser and optional executable callback. */ export type AutoParseableResponseTool<OptionsT extends ToolOptions, HasFunction = OptionsT['function'] extends (...args: never[]) => unknown ? true : false> = FunctionTool & { /** Type-only marker for parsed tool arguments; this property does not exist at runtime. */ __arguments: OptionsT['arguments']; /** Type-only marker for the function name; this property does not exist at runtime. */ __name: OptionsT['name']; /** Non-enumerable SDK marker identifying a tool with an attached argument parser. */ $brand: 'auto-parseable-tool'; /** Optional callback available to helpers that execute parsed function tools. */ $callback: ((args: OptionsT['arguments']) => any) | undefined; /** Parses the raw JSON argument string into the function's typed argument value. */ $parseRaw(args: string): OptionsT['arguments']; }; /** Copies a Responses API function tool and attaches non-enumerable parser and callback metadata. */ export declare function makeParseableResponseTool<OptionsT extends ToolOptions>(tool: FunctionTool, { parser, callback, }: { /** Converts the raw JSON argument string into the function's typed argument value. */ parser: (content: string) => OptionsT['arguments']; /** Optional callback available to helpers that execute the parsed function. */ callback: ((args: OptionsT['arguments']) => any) | undefined; }): AutoParseableResponseTool<OptionsT>; /** Returns whether a Responses API tool carries the SDK's argument-parser marker. */ export declare function isAutoParsableTool(tool: any): tool is AutoParseableResponseTool<any>; /** Matches a response function call to a strict or auto-parseable tool by namespace and name. */ export declare function shouldParseToolCall(params: ResponseCreateParamsNonStreaming | null | undefined, toolCall: ResponseFunctionToolCall): boolean; /** * Validates that compatible chat completion tools can be automatically parsed. * * @throws {OpenAIError} If a tool is not a function or is missing `strict: true`. */ export declare function validateInputTools(tools: ChatCompletionTool[] | undefined): void; /** Replaces `output_text` with the concatenated text from every response output message. */ export declare function addOutputText(rsp: Response): void; export {}; //# sourceMappingURL=ResponsesParser.d.ts.map