@tanstack/ai
Version:
Type-safe TypeScript AI SDK for streaming chat, tool calling, agents, structured outputs, and multimodal generation.
195 lines (194 loc) • 7.95 kB
TypeScript
import { DebugOption } from '../../logger/types.js';
import { GenerationMiddleware } from '../middleware/types.js';
import { AnyMotionAdapter, MotionAdapter } from './adapter.js';
import { MotionGenerationResult, MotionJobResult, MotionJobState, StreamChunk } from '../../types.js';
/** The adapter kind this activity handles */
export declare const kind: "motion";
/** Custom stream event names emitted by `generateMotion({ stream: true })`. */
export declare const MOTION_EVENTS: {
/** `{ jobId, model }` once the provider accepted the job */
readonly JOB_CREATED: "motion:job:created";
/** `{ jobId, status, progress?, queuePosition?, error? }` on every poll */
readonly STATUS: "motion:status";
};
/** Extract provider options from a MotionAdapter via ~types. */
export type MotionProviderOptions<TAdapter> = TAdapter extends {
'~types': {
providerOptions: infer P extends object;
};
} ? P : object;
/**
* Options for {@link generateMotion}.
*
* @experimental Motion generation is an experimental feature and may change.
*/
export interface MotionActivityOptions<TAdapter extends MotionAdapter<string, MotionProviderOptions<TAdapter>>, TStream extends boolean = false> {
/** The motion adapter to use (must be created with a model) */
adapter: TAdapter & {
kind: typeof kind;
};
/** Natural-language description of the motion */
prompt: string;
/**
* Requested clip length in seconds. The provider decides the accepted
* range and rejects values outside it.
*/
duration?: number;
/** Provider-specific options for motion generation */
modelOptions?: MotionProviderOptions<TAdapter>;
/**
* `false` (default): submit the job and return `{ jobId, model }`. Poll
* it later with {@link getMotionJobStatus}.
*
* `true`: return an `AsyncIterable<StreamChunk>` that submits the job,
* polls it, and ends with `generation:result` and `RUN_FINISHED`.
*/
stream?: TStream;
/** Polling interval in milliseconds (stream mode only). @default 2000 */
pollingInterval?: number;
/** Stop polling after this many milliseconds (stream mode only). @default 600000 */
maxDuration?: number;
/** Stable conversation/thread id. Also the AG-UI thread id on the stream. */
threadId: string;
/**
* Run id for the stream chunks (stream mode only). A non-streaming submit
* ignores it: that run id is derived from the provider job, so
* {@link getMotionJobStatus} can find the same run from the job id alone.
*/
runId?: string;
/** Enable debug logging. See `DebugOption`. */
debug?: DebugOption;
/**
* Observe-only middleware (`otelMiddleware()`, persistence, custom).
*
* Stream mode: one run from submit to result.
* Submit mode: the submit opens the run, and the {@link getMotionJobStatus}
* poll that sees a terminal state closes it. Pass the same `middleware`
* and `threadId` to both.
*/
middleware?: Array<GenerationMiddleware>;
/**
* Maximum time for this call in milliseconds. In stream mode it bounds the
* whole observation. Reaching it stops observation, not the provider job.
*/
timeout?: number;
/**
* Caller cancellation signal. It stops local observation only. The
* provider job keeps running. Use {@link cancelMotionJob} to stop it.
*/
abortSignal?: AbortSignal;
}
/**
* Result type for {@link generateMotion}.
* - `stream: true`: `AsyncIterable<StreamChunk>`
* - otherwise: `Promise<MotionJobResult>`
*/
export type MotionActivityResult<TStream extends boolean = false> = TStream extends true ? AsyncIterable<StreamChunk> : Promise<MotionJobResult>;
/**
* Generate a skeletal motion from a text prompt.
*
* @example Submit, then poll from a later request
* ```ts
* import { generateMotion, getMotionJobStatus } from '@tanstack/ai'
* import { falMotion } from '@tanstack/ai-fal'
*
* const adapter = falMotion('fal-ai/hunyuan-motion')
* const { jobId } = await generateMotion({
* adapter,
* threadId: 'orc-attacks',
* prompt: 'A heavy two-handed overhead axe swing with a slow wind-up',
* duration: 3,
* })
*
* const status = await getMotionJobStatus({ adapter, jobId })
* if (status.motion) console.log(status.motion.assets[0]?.url)
* ```
*
* @example Stream the whole lifecycle to a browser
* ```ts
* const stream = generateMotion({
* adapter: falMotion('fal-ai/hunyuan-motion'),
* threadId: 'orc-attacks',
* prompt: 'A heavy two-handed overhead axe swing',
* stream: true,
* })
* return toServerSentEventsResponse(stream)
* ```
*
* @experimental Motion generation is an experimental feature and may change.
*/
export declare function generateMotion<TAdapter extends MotionAdapter<string, MotionProviderOptions<TAdapter>>, TStream extends boolean = false>(options: MotionActivityOptions<TAdapter, TStream>): MotionActivityResult<TStream>;
/**
* Options for {@link getMotionJobStatus}.
*
* @experimental Motion generation is an experimental feature and may change.
*/
export interface MotionJobStatusOptions<TAdapter extends AnyMotionAdapter> {
/** The motion adapter (same provider and model as the submit) */
adapter: TAdapter & {
kind: typeof kind;
};
/** The provider job id */
jobId: string;
/** Must match the submit's `threadId` when middleware is used. */
threadId?: string;
/**
* Observe-only middleware. Hooks fire only on the poll that sees a
* terminal state: it closes the run that the submit opened.
*/
middleware?: Array<GenerationMiddleware>;
/** Maximum time for this call in milliseconds. */
timeout?: number;
/** Cancels this HTTP call only. The job keeps running. */
abortSignal?: AbortSignal;
}
/**
* Status of a motion job, plus the finished motion once it completed.
*
* @experimental Motion generation is an experimental feature and may change.
*/
export interface MotionJobStatusResult {
jobId: string;
status: MotionJobState;
progress?: number;
queuePosition?: number;
error?: string;
/** The finished motion, set when `status` is `completed` */
motion?: MotionGenerationResult;
}
/**
* Read the status of a motion job. When the job completed, the result
* includes the finished motion and its asset URLs.
*
* Safe to call from any request or process: the job id is the only state.
*
* @experimental Motion generation is an experimental feature and may change.
*/
export declare function getMotionJobStatus<TAdapter extends AnyMotionAdapter>(options: MotionJobStatusOptions<TAdapter>): Promise<MotionJobStatusResult>;
/**
* Options for {@link cancelMotionJob}.
*
* @experimental Motion generation is an experimental feature and may change.
*/
export interface CancelMotionJobOptions<TAdapter extends AnyMotionAdapter> {
adapter: TAdapter & {
kind: typeof kind;
};
jobId: string;
/** Cancels this HTTP call only. */
abortSignal?: AbortSignal;
}
/**
* Ask the provider to stop a motion job. This is a remote cancel, not a
* local stop. Providers decide what happens: a queued job is usually
* removed, a running job may still finish.
*
* @throws When the adapter's provider has no cancel API.
*
* @experimental Motion generation is an experimental feature and may change.
*/
export declare function cancelMotionJob<TAdapter extends AnyMotionAdapter>(options: CancelMotionJobOptions<TAdapter>): Promise<void>;
/** Create typed options for generateMotion() without executing. */
export declare function createMotionOptions<TAdapter extends MotionAdapter<string, MotionProviderOptions<TAdapter>>, TStream extends boolean = false>(options: MotionActivityOptions<TAdapter, TStream>): MotionActivityOptions<TAdapter, TStream>;
export type { MotionAdapter, AnyMotionAdapter } from './adapter.js';
export { BaseMotionAdapter } from './adapter.js';