UNPKG

@tanstack/ai

Version:

Type-safe TypeScript AI SDK for streaming chat, tool calling, agents, structured outputs, and multimodal generation.

223 lines (222 loc) • 9.67 kB
import { DebugOption } from '../../logger/types.js'; import { GenerationMiddleware } from '../middleware/types.js'; import { Model3DAdapter } from './adapter.js'; import { MediaPrompt, MediaPromptFor, Model3DJobResult, Model3DResult, Model3DStatusResult, StreamChunk } from '../../types.js'; /** The adapter kind this activity handles */ export declare const kind: "3dModel"; /** * Extract provider options from a Model3DAdapter via ~types. * * @experimental 3D model generation is experimental and may change. */ export type Model3DProviderOptions<TAdapter> = TAdapter extends Model3DAdapter<any, any, any, any> ? TAdapter['~types']['providerOptions'] : object; /** * Extract the prompt type a model accepts from a Model3DAdapter via ~types. * Models in the adapter's input-modality map get a `prompt` narrowed to text * plus their supported part types. Adapters without a map get the full * MediaPrompt. * * @experimental 3D model generation is experimental and may change. */ export type Model3DPromptForAdapter<TAdapter> = TAdapter extends Model3DAdapter<infer TModel, any, any, infer ModsByName> ? string extends keyof ModsByName ? MediaPrompt : TModel extends keyof ModsByName ? MediaPromptFor<ModsByName[TModel][number]> : MediaPrompt : MediaPrompt; /** * Options shared by every 3D model call mode. * The model is read from the adapter's model property. */ interface Model3DActivityBaseOptions<TAdapter extends Model3DAdapter<string, any, any, any>> { /** The 3D model adapter to use (must be created with a model) */ adapter: TAdapter & { kind: typeof kind; }; /** * What to make: a plain string for text-to-3D, or an ordered array of text, * image, and `model3d` parts. The accepted part types are narrowed per * model by the adapter's input-modality map. */ prompt: Model3DPromptForAdapter<TAdapter>; /** Provider-specific options for 3D model generation */ modelOptions?: Model3DProviderOptions<TAdapter>; /** * Stable conversation/thread id. Required. It is the `threadId` on the * emitted `RUN_STARTED` / `RUN_FINISHED` chunks and on the middleware * context. */ threadId: string; /** Stable run id. A new one is made when you do not set it. */ runId?: string; /** Time between two status polls, in milliseconds (`stream` and `wait` only). @default 2000 */ pollInterval?: number; /** * Maximum duration of this call, in milliseconds. Composed with * {@link abortSignal}; the first abort wins. * * In `stream` and `wait` modes it bounds the full create → poll → complete * lifecycle and defaults to 10 minutes. When it fires, the activity asks * the adapter to cancel the job. In submit mode it bounds only the submit * request and has no default. */ timeout?: number; /** * Caller cancellation signal. Composed with {@link timeout} and given to * every adapter request. An abort after the job exists asks the adapter * to cancel the job. */ abortSignal?: AbortSignal; /** * Enable debug logging. Pass `true` to enable all categories, `false` to * silence everything including errors, or a `DebugConfig` object for * granular control and/or a custom `Logger`. */ debug?: DebugOption; /** * Observe-only middleware notified on start, usage, success, and error. * Pass `otelMiddleware()` to emit OpenTelemetry spans, or implement the * `GenerationMiddleware` contract for a custom backend. * * In `stream` and `wait` modes one run covers the full lifecycle. In * submit mode the run covers only the submit request. */ middleware?: Array<GenerationMiddleware>; } /** * Options for {@link generate3DModel}. * * - No `stream` and no `wait`: submit the job and return `{ jobId, model }`. * - `stream: true`: run the full job and return StreamChunks (for SSE). * - `wait: true`: run the full job and return the finished model. * * `stream` and `wait` cannot both be `true`. * * @template TAdapter - The 3D model adapter type * @template TStream - Whether to stream the job lifecycle * @template TWait - Whether to wait for the finished model * * @experimental 3D model generation is experimental and may change. */ export type Model3DActivityOptions<TAdapter extends Model3DAdapter<string, any, any, any>, TStream extends boolean = false, TWait extends boolean = false> = Model3DActivityBaseOptions<TAdapter> & { /** * Run the full job and return an AsyncIterable<StreamChunk>: RUN_STARTED, * one `generation:progress` per poll, `generation:result`, RUN_FINISHED. * * @default false */ stream?: TStream; /** * Run the full job and return a Promise of the finished model. * * @default false */ wait?: TWait; } & ([TStream, TWait] extends [true, true] ? { wait: never; } : unknown); /** * Result type for {@link generate3DModel}: * - `stream: true`: AsyncIterable<StreamChunk> * - `wait: true`: Promise<Model3DResult> * - otherwise: Promise<Model3DJobResult> * * @experimental 3D model generation is experimental and may change. */ export type Model3DActivityResult<TStream extends boolean = false, TWait extends boolean = false> = TStream extends true ? AsyncIterable<StreamChunk> : TWait extends true ? Promise<Model3DResult> : Promise<Model3DJobResult>; /** * Generate a 3D model from text, images, or an existing mesh. * * Generation is a job. With no flag, the call submits the job and returns * `{ jobId, model }`; read it later with {@link get3DModelJobStatus}. With * `wait: true`, the call polls until the job is done and returns the model. * With `stream: true`, the call returns StreamChunks for the same lifecycle, * so you can send them to the client over SSE. * * A job that fails rejects (or ends the stream with RUN_ERROR) with the * provider error message. On timeout or abort, the activity asks the adapter * to cancel the job. * * @example Wait for the model on the server * ```ts * import { generate3DModel } from '@tanstack/ai' * import { fal3DModel } from '@tanstack/ai-fal' * * const model = await generate3DModel({ * adapter: fal3DModel('fal-ai/hunyuan3d-v3/image-to-3d'), * prompt: [{ type: 'image', source: { type: 'url', value: conceptArtUrl } }], * threadId, * wait: true, * }) * * console.log(model.url, model.format) // e.g. 'https://…/model.glb', 'glb' * ``` * * @example Stream the job to the client * ```ts * import { generate3DModel, toServerSentEventsResponse } from '@tanstack/ai' * import { fal3DModel } from '@tanstack/ai-fal' * * const stream = generate3DModel({ * adapter: fal3DModel('fal-ai/hunyuan3d-v3/image-to-3d'), * prompt: [{ type: 'image', source: { type: 'url', value: conceptArtUrl } }], * threadId, * stream: true, * }) * * return toServerSentEventsResponse(stream) * ``` * * @experimental 3D model generation is experimental and may change. */ export declare function generate3DModel<TAdapter extends Model3DAdapter<string, any, any, any>, TStream extends boolean = false, TWait extends boolean = false>(options: Model3DActivityOptions<TAdapter, TStream, TWait>): Model3DActivityResult<TStream, TWait>; /** * Options for {@link get3DModelJobStatus}. * * @experimental 3D model generation is experimental and may change. */ export interface Model3DJobStatusOptions<TAdapter extends Model3DAdapter<string, any, any, any>> { /** The 3D model adapter that created the job */ adapter: TAdapter & { kind: typeof kind; }; /** The job id that {@link generate3DModel} returned */ jobId: string; /** Caller cancellation signal, given to the adapter requests */ abortSignal?: AbortSignal; } /** * The status of a 3D model job, plus the finished model once the job is * completed. * * @experimental 3D model generation is experimental and may change. */ export interface Model3DJobStatusResult extends Model3DStatusResult { /** The finished model. Set only when `status` is `'completed'`. */ result?: Model3DResult; } /** * Read the status of a 3D model job that {@link generate3DModel} submitted. * When the job is completed, it also reads the finished model. * * It runs no middleware. To observe the full lifecycle, use `wait: true` or * `stream: true` on {@link generate3DModel}. * * @example Poll a submitted job * ```ts * import { generate3DModel, get3DModelJobStatus } from '@tanstack/ai' * import { fal3DModel } from '@tanstack/ai-fal' * * const adapter = fal3DModel('fal-ai/hunyuan3d-v3/image-to-3d') * const { jobId } = await generate3DModel({ adapter, prompt, threadId }) * * const status = await get3DModelJobStatus({ adapter, jobId }) * if (status.result) console.log(status.result.url) * ``` * * @experimental 3D model generation is experimental and may change. */ export declare function get3DModelJobStatus<TAdapter extends Model3DAdapter<string, any, any, any>>(options: Model3DJobStatusOptions<TAdapter>): Promise<Model3DJobStatusResult>; /** * Create typed options for the generate3DModel() function without running it. * * @experimental 3D model generation is experimental and may change. */ export declare function create3DModelOptions<TAdapter extends Model3DAdapter<string, any, any, any>, TStream extends boolean = false, TWait extends boolean = false>(options: Model3DActivityOptions<TAdapter, TStream, TWait>): Model3DActivityOptions<TAdapter, TStream, TWait>; export type { Model3DAdapter, Model3DAdapterConfig, AnyModel3DAdapter, } from './adapter.js'; export { BaseModel3DAdapter } from './adapter.js';