pipeai 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +721 -0
- package/dist/index.cjs +593 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +240 -0
- package/dist/index.d.ts +240 -0
- package/dist/index.js +568 -0
- package/dist/index.js.map +1 -0
- package/package.json +55 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/agent.ts","../src/tool-provider.ts","../src/utils.ts","../src/workflow.ts"],"sourcesContent":["export { Agent } from \"./agent\";\nexport type {\n AgentConfig,\n GenerateTextResult,\n StreamTextResult,\n} from \"./agent\";\n\nexport { Workflow, WorkflowBranchError, WorkflowLoopError } from \"./workflow\";\nexport type { SealedWorkflow } from \"./workflow\";\nexport type {\n AgentStepHooks,\n StepOptions,\n BranchCase,\n BranchSelect,\n RepeatOptions,\n WorkflowResult,\n WorkflowStreamResult,\n WorkflowStreamOptions,\n} from \"./workflow\";\n\nexport { defineTool } from \"./tool-provider\";\nexport type { ToolProviderConfig, IToolProvider } from \"./tool-provider\";\n\nexport type { MaybePromise, Resolvable } from \"./utils\";\n","import {\n generateText,\n streamText,\n tool,\n Output,\n type GenerateTextResult as AIGenerateTextResult,\n type StreamTextResult as AIStreamTextResult,\n type ModelMessage,\n type LanguageModel,\n type Tool,\n type ToolSet,\n type StopCondition,\n type ToolChoice,\n type OnStepFinishEvent,\n type OnFinishEvent,\n} from \"ai\";\n\n// Extract the Output interface type from the Output.object return type\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype OutputType<T = any> = ReturnType<typeof Output.object<T>>;\nimport type { ZodType } from \"zod\";\nimport { isToolProvider, TOOL_PROVIDER_BRAND, type IToolProvider } from \"./tool-provider\";\nimport { extractOutput, resolveValue, type MaybePromise, type Resolvable } from \"./utils\";\n\n// Tools config accepts both AI SDK tools and context-aware ToolProviders\ntype AgentToolSet<TContext> = Record<string, Tool | IToolProvider<TContext>>;\n\n// ── Result type aliases ─────────────────────────────────────────────\n\nexport type GenerateTextResult<TOOLS extends ToolSet = ToolSet, OUTPUT extends OutputType = OutputType> = AIGenerateTextResult<TOOLS, OUTPUT>;\nexport type StreamTextResult<TOOLS extends ToolSet = ToolSet, OUTPUT extends OutputType = OutputType> = AIStreamTextResult<TOOLS, OUTPUT>;\n\n// ── AI SDK passthrough types ────────────────────────────────────────\n\n// Extract options types from both AI SDK entry points\ntype StreamTextOptions = Parameters<typeof streamText>[0];\ntype GenerateTextOptions = Parameters<typeof generateText>[0];\n\n// Keys we replace with resolvable or context-enriched versions\ntype ManagedKeys =\n | 'model' | 'system' | 'prompt' | 'messages'\n | 'tools' | 'activeTools' | 'toolChoice' | 'stopWhen'\n | 'output' | 'onFinish' | 'onStepFinish' | 'onError';\n\n// Combine options from both streamText and generateText.\n// Each side contributes its unique props; shared props merge naturally.\n// Stream-only props (onChunk, onAbort) are ignored by generateText.\n// Generate-only props (experimental_include.responseBody) are ignored by streamText.\ntype AIPassthroughOptions =\n Omit<StreamTextOptions, ManagedKeys> &\n Omit<GenerateTextOptions, ManagedKeys>;\n\n// ── Resolved config (output of resolveConfig / resolveConfigAsync) ──\n\ninterface ResolvedAgentConfig {\n model: LanguageModel;\n prompt: string | undefined;\n system: string | undefined;\n messages: ModelMessage[] | undefined;\n tools: Record<string, Tool>;\n activeTools: string[] | undefined;\n toolChoice: ToolChoice<ToolSet> | undefined;\n stopWhen: StopCondition<ToolSet> | Array<StopCondition<ToolSet>> | undefined;\n}\n\n// ── Agent Configuration ─────────────────────────────────────────────\n\nexport interface AgentConfig<\n TContext,\n TInput = void,\n TOutput = void,\n> extends AIPassthroughOptions {\n // ── Custom (not in AI SDK) ──\n id: string;\n description?: string;\n input?: ZodType<TInput>;\n output?: OutputType<TOutput>;\n\n // ── Resolvable (our versions of AI SDK properties) ──\n model: Resolvable<TContext, TInput, LanguageModel>;\n system?: Resolvable<TContext, TInput, string>;\n prompt?: Resolvable<TContext, TInput, string>;\n messages?: Resolvable<TContext, TInput, ModelMessage[]>;\n tools?: Resolvable<TContext, TInput, AgentToolSet<TContext>>;\n activeTools?: Resolvable<TContext, TInput, string[]>;\n toolChoice?: Resolvable<TContext, TInput, ToolChoice<ToolSet>>;\n stopWhen?: Resolvable<TContext, TInput, StopCondition<ToolSet> | Array<StopCondition<ToolSet>>>;\n\n // ── Context-enriched callbacks (replace AI SDK versions) ──\n onStepFinish?: (params: { result: OnStepFinishEvent; ctx: Readonly<TContext>; input: TInput }) => MaybePromise<void>;\n onFinish?: (params: { result: OnFinishEvent; ctx: Readonly<TContext>; input: TInput }) => MaybePromise<void>;\n onError?: (params: { error: unknown; ctx: Readonly<TContext>; input: TInput }) => MaybePromise<void>;\n}\n\n// ── Agent ───────────────────────────────────────────────────────────\n\nexport class Agent<\n TContext,\n TInput = void,\n TOutput = void,\n> {\n readonly id: string;\n readonly description: string;\n readonly hasOutput: boolean;\n private readonly config: AgentConfig<TContext, TInput, TOutput>;\n private readonly _hasDynamicConfig: boolean;\n private readonly _resolvedStaticTools: Record<string, Tool> | null = null;\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n private readonly _passthrough: Record<string, any>;\n private readonly _onStepFinish: AgentConfig<TContext, TInput, TOutput>['onStepFinish'];\n private readonly _onFinish: AgentConfig<TContext, TInput, TOutput>['onFinish'];\n\n constructor(config: AgentConfig<TContext, TInput, TOutput>) {\n this.id = config.id;\n this.description = config.description ?? \"\";\n this.hasOutput = config.output !== undefined;\n this.config = config;\n this._hasDynamicConfig = [\n config.model, config.system, config.prompt,\n config.messages, config.tools, config.activeTools,\n config.toolChoice, config.stopWhen,\n ].some(v => typeof v === \"function\");\n\n // Cache tools when config is static and contains no ToolProviders.\n // Avoids re-iterating the tools map on every generate()/stream() call.\n if (!this._hasDynamicConfig) {\n const rawTools = (config.tools as AgentToolSet<TContext> | undefined) ?? {};\n const hasProvider = Object.values(rawTools).some(v => isToolProvider(v));\n if (!hasProvider) {\n this._resolvedStaticTools = rawTools as Record<string, Tool>;\n }\n }\n\n // Pre-compute the passthrough (AI SDK options we don't manage) once,\n // rather than destructuring on every generate()/stream() call.\n const {\n id: _id, description: _desc, input: _inputSchema, output: _output,\n model: _m, system: _s, prompt: _p, messages: _msg,\n tools: _t, activeTools: _at, toolChoice: _tc, stopWhen: _sw,\n onStepFinish, onFinish, onError: _onError,\n ...passthrough\n } = config;\n this._passthrough = passthrough;\n this._onStepFinish = onStepFinish;\n this._onFinish = onFinish;\n }\n\n async generate(ctx: TContext, ...args: TInput extends void ? [input?: TInput] : [input: TInput]): Promise<GenerateTextResult> {\n const input = args[0] as TInput;\n const resolved = await this.resolveConfig(ctx, input);\n const options = this.buildCallOptions(resolved, ctx, input);\n\n try {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return await generateText(options as any);\n } catch (error: unknown) {\n if (this.config.onError) {\n await this.config.onError({ error, ctx, input });\n }\n throw error;\n }\n }\n\n async stream(ctx: TContext, ...args: TInput extends void ? [input?: TInput] : [input: TInput]): Promise<StreamTextResult> {\n const input = args[0] as TInput;\n const resolved = await this.resolveConfig(ctx, input);\n const options = this.buildCallOptions(resolved, ctx, input);\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return streamText({\n ...options,\n onError: this.config.onError\n ? ({ error }: { error: unknown }) => this.config.onError!({ error, ctx, input })\n : undefined,\n } as any);\n }\n\n asTool(ctx: TContext, options?: {\n mapOutput?: (result: GenerateTextResult) => MaybePromise<TOutput>;\n }): Tool {\n return this.createToolInstance(ctx, options);\n }\n\n asToolProvider(options?: {\n mapOutput?: (result: GenerateTextResult) => MaybePromise<TOutput>;\n }): IToolProvider<TContext> {\n if (!this.config.input) {\n throw new Error(`Agent \"${this.id}\": asToolProvider() requires an input schema`);\n }\n\n return {\n [TOOL_PROVIDER_BRAND]: true as const,\n createTool: (ctx: Readonly<TContext>) => this.createToolInstance(ctx as TContext, options),\n };\n }\n\n private createToolInstance(ctx: TContext, options?: {\n mapOutput?: (result: GenerateTextResult) => MaybePromise<TOutput>;\n }): Tool {\n if (!this.config.input) {\n throw new Error(`Agent \"${this.id}\": asTool() requires an input schema`);\n }\n\n return tool({\n description: this.description,\n parameters: this.config.input,\n execute: async (toolInput: TInput) => {\n const result = await (this.generate as (ctx: TContext, input: TInput) => Promise<GenerateTextResult>)(ctx, toolInput);\n if (options?.mapOutput) return options.mapOutput(result);\n return extractOutput(result, this.hasOutput);\n },\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n } as any);\n }\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n private buildCallOptions(resolved: ResolvedAgentConfig, ctx: TContext, input: TInput): Record<string, any> {\n return {\n ...this._passthrough,\n model: resolved.model,\n tools: resolved.tools,\n activeTools: resolved.activeTools,\n toolChoice: resolved.toolChoice,\n stopWhen: resolved.stopWhen,\n ...(resolved.messages\n ? { messages: resolved.messages }\n : { prompt: resolved.prompt ?? \"\" }),\n ...(resolved.system ? { system: resolved.system } : {}),\n ...(this.config.output ? { output: this.config.output } : {}),\n onStepFinish: this._onStepFinish\n ? (event: OnStepFinishEvent) => this._onStepFinish!({ result: event, ctx, input })\n : undefined,\n onFinish: this._onFinish\n ? (event: OnFinishEvent) => this._onFinish!({ result: event, ctx, input })\n : undefined,\n };\n }\n\n private resolveConfig(ctx: TContext, input: TInput): ResolvedAgentConfig | Promise<ResolvedAgentConfig> {\n if (!this._hasDynamicConfig) {\n return {\n model: this.config.model as LanguageModel,\n prompt: this.config.prompt as string | undefined,\n system: this.config.system as string | undefined,\n messages: this.config.messages as ModelMessage[] | undefined,\n tools: this._resolvedStaticTools ?? this.resolveTools(\n (this.config.tools as AgentToolSet<TContext> | undefined) ?? {}, ctx\n ),\n activeTools: this.config.activeTools as string[] | undefined,\n toolChoice: this.config.toolChoice as ToolChoice<ToolSet> | undefined,\n stopWhen: this.config.stopWhen as StopCondition<ToolSet> | Array<StopCondition<ToolSet>> | undefined,\n };\n }\n return this.resolveConfigAsync(ctx, input);\n }\n\n private async resolveConfigAsync(ctx: TContext, input: TInput): Promise<ResolvedAgentConfig> {\n const [model, prompt, system, messages, rawTools, activeTools, toolChoice, stopWhen] = await Promise.all([\n resolveValue(this.config.model, ctx, input),\n resolveValue(this.config.prompt, ctx, input),\n resolveValue(this.config.system, ctx, input),\n resolveValue(this.config.messages, ctx, input),\n resolveValue(this.config.tools, ctx, input),\n resolveValue(this.config.activeTools, ctx, input),\n resolveValue(this.config.toolChoice, ctx, input),\n resolveValue(this.config.stopWhen, ctx, input),\n ]);\n const tools = this.resolveTools(rawTools ?? {}, ctx);\n return { model, prompt, system, messages, tools, activeTools, toolChoice, stopWhen };\n }\n\n private resolveTools(\n tools: AgentToolSet<TContext>,\n ctx: TContext\n ): Record<string, Tool> {\n const entries = Object.entries(tools);\n if (entries.length === 0) return tools as Record<string, Tool>;\n let hasProvider = false;\n const resolved: Record<string, Tool> = {};\n for (const [key, toolOrProvider] of entries) {\n if (isToolProvider<TContext>(toolOrProvider)) {\n hasProvider = true;\n resolved[key] = toolOrProvider.createTool(ctx as Readonly<TContext>);\n } else {\n resolved[key] = toolOrProvider as Tool;\n }\n }\n return hasProvider ? resolved : (tools as Record<string, Tool>);\n }\n}\n","import { tool, type Tool, type ToolExecutionOptions, type FlexibleSchema } from \"ai\";\n\nexport const TOOL_PROVIDER_BRAND = Symbol.for(\"agent-workflow.ToolProvider\");\n\nexport type ToolProviderConfig<TContext, TInput, TOutput> = {\n description?: string;\n input: FlexibleSchema<TInput>;\n output?: FlexibleSchema<unknown>;\n providerOptions?: unknown;\n execute: (input: TInput, ctx: Readonly<TContext>, options?: ToolExecutionOptions) => Promise<TOutput>;\n};\n\nexport interface IToolProvider<TContext> {\n readonly [TOOL_PROVIDER_BRAND]: true;\n createTool(context: Readonly<TContext>): Tool;\n}\n\nexport class ToolProvider<\n TContext,\n TInput = unknown,\n TOutput = unknown,\n> implements IToolProvider<TContext> {\n readonly [TOOL_PROVIDER_BRAND] = true as const;\n private readonly config: ToolProviderConfig<TContext, TInput, TOutput>;\n\n constructor(config: ToolProviderConfig<TContext, TInput, TOutput>) {\n this.config = config;\n }\n\n createTool(context: Readonly<TContext>): Tool {\n const { execute, input: inputSchema, ...toolDef } = this.config;\n return tool({\n ...toolDef,\n parameters: inputSchema,\n execute: (input: TInput, options?: ToolExecutionOptions) => execute(input, context, options),\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n } as any);\n }\n}\n\nexport function defineTool<TContext>() {\n return <TInput, TOutput>(\n config: ToolProviderConfig<TContext, TInput, TOutput>\n ): ToolProvider<TContext, TInput, TOutput> => new ToolProvider(config);\n}\n\nexport function isToolProvider<TContext>(obj: unknown): obj is IToolProvider<TContext> {\n return (\n typeof obj === \"object\" &&\n obj !== null &&\n TOOL_PROVIDER_BRAND in obj\n );\n}\n","export type MaybePromise<T> = T | Promise<T>;\n\n/**\n * A value that can be static or derived from context and input.\n * Used for agent config fields that may need runtime resolution.\n *\n * Functions may return a Promise for async resolution; static values are always sync.\n */\nexport type Resolvable<TCtx, TInput, TValue> =\n | TValue\n | ((ctx: Readonly<TCtx>, input: TInput) => TValue | Promise<TValue>);\n\nexport function resolveValue<TCtx, TInput, TValue>(\n value: Resolvable<TCtx, TInput, TValue>,\n ctx: TCtx,\n input: TInput\n): TValue | Promise<TValue>;\nexport function resolveValue<TCtx, TInput, TValue>(\n value: Resolvable<TCtx, TInput, TValue> | undefined,\n ctx: TCtx,\n input: TInput\n): TValue | Promise<TValue> | undefined {\n if (typeof value === \"function\") {\n return (value as (ctx: TCtx, input: TInput) => TValue | Promise<TValue>)(ctx, input);\n }\n return value;\n}\n\n/**\n * Extract structured output from an AI SDK result, falling back to text.\n * Works for both generate (sync .output/.text) and stream (async .output/.text) results.\n */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport async function extractOutput(result: any, hasStructuredOutput: boolean): Promise<unknown> {\n if (hasStructuredOutput) {\n const output = await result.output;\n if (output !== undefined) return output;\n }\n return await result.text;\n}\n","import {\n createUIMessageStream,\n type UIMessageStreamWriter,\n} from \"ai\";\nimport { type Agent, type GenerateTextResult, type StreamTextResult } from \"./agent\";\nimport { extractOutput, type MaybePromise } from \"./utils\";\n\n// ── Error Types ─────────────────────────────────────────────────────\n\nexport class WorkflowBranchError extends Error {\n constructor(\n public readonly branchType: \"predicate\" | \"select\",\n message: string,\n ) {\n super(message);\n this.name = \"WorkflowBranchError\";\n }\n}\n\nexport class WorkflowLoopError extends Error {\n constructor(\n public readonly iterations: number,\n public readonly maxIterations: number,\n ) {\n super(`Loop exceeded maximum iterations (${maxIterations})`);\n this.name = \"WorkflowLoopError\";\n }\n}\n\n// ── Shared Agent Step Hooks ─────────────────────────────────────────\n\nexport interface AgentStepHooks<TContext, TOutput, TNextOutput> {\n mapGenerateResult?: (params: { result: GenerateTextResult; ctx: Readonly<TContext>; input: TOutput }) => MaybePromise<TNextOutput>;\n mapStreamResult?: (params: { result: StreamTextResult; ctx: Readonly<TContext>; input: TOutput }) => MaybePromise<TNextOutput>;\n onGenerateResult?: (params: { result: GenerateTextResult; ctx: Readonly<TContext>; input: TOutput }) => MaybePromise<void>;\n onStreamResult?: (params: { result: StreamTextResult; ctx: Readonly<TContext>; input: TOutput }) => MaybePromise<void>;\n handleStream?: (params: {\n result: StreamTextResult;\n writer: UIMessageStreamWriter;\n ctx: Readonly<TContext>;\n }) => MaybePromise<void>;\n}\n\n// ── Step Options ────────────────────────────────────────────────────\n\nexport type StepOptions<TContext, TOutput, TNextOutput> = AgentStepHooks<TContext, TOutput, TNextOutput>;\n\n// ── Branch Types ────────────────────────────────────────────────────\n\nexport interface BranchCase<TContext, TOutput, TNextOutput> extends AgentStepHooks<TContext, TOutput, TNextOutput> {\n when?: (params: { ctx: Readonly<TContext>; input: TOutput }) => MaybePromise<boolean>;\n agent: Agent<TContext, TOutput, TNextOutput>;\n}\n\nexport interface BranchSelect<TContext, TOutput, TKeys extends string, TNextOutput> extends AgentStepHooks<TContext, TOutput, TNextOutput> {\n select: (params: { ctx: Readonly<TContext>; input: TOutput }) => MaybePromise<TKeys>;\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n agents: Record<TKeys, Agent<TContext, any, TNextOutput>>;\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n fallback?: Agent<TContext, any, TNextOutput>;\n}\n\n// ── Result Types ────────────────────────────────────────────────────\n\nexport interface WorkflowResult<TOutput> {\n output: TOutput;\n}\n\nexport interface WorkflowStreamResult<TOutput> {\n stream: ReadableStream;\n output: Promise<TOutput>;\n}\n\nexport interface WorkflowStreamOptions {\n onError?: (error: unknown) => string;\n onFinish?: () => MaybePromise<void>;\n}\n\n// ── Loop Types ──────────────────────────────────────────────────────\n\ntype LoopPredicate<TContext, TOutput> = (params: {\n output: TOutput;\n ctx: Readonly<TContext>;\n iterations: number;\n}) => MaybePromise<boolean>;\n\n// Exactly one of `until` or `while` — never both.\nexport type RepeatOptions<TContext, TOutput> =\n | { until: LoopPredicate<TContext, TOutput>; while?: never; maxIterations?: number }\n | { while: LoopPredicate<TContext, TOutput>; until?: never; maxIterations?: number };\n\n// Extracts the element type from an array type. Resolves to `never` for non-arrays,\n// making foreach uncallable at compile time when the previous step doesn't produce an array.\ntype ElementOf<T> = T extends readonly (infer E)[] ? E : never;\n\n// ── Step Node ───────────────────────────────────────────────────────\n\ntype StepNode =\n | { readonly type: \"step\"; readonly id: string; readonly execute: (state: RuntimeState) => MaybePromise<void> }\n | { readonly type: \"catch\"; readonly id: string; readonly catchFn: (params: { error: unknown; ctx: unknown; lastOutput: unknown; stepId: string }) => MaybePromise<unknown> }\n | { readonly type: \"finally\"; readonly id: string; readonly execute: (state: RuntimeState) => MaybePromise<void> };\n\ninterface RuntimeState {\n ctx: unknown;\n output: unknown;\n mode: \"generate\" | \"stream\";\n writer?: UIMessageStreamWriter;\n}\n\n// ── Sealed Workflow (returned by finally — execution only) ───────────\n\nexport class SealedWorkflow<\n TContext,\n TInput = void,\n TOutput = void,\n> {\n readonly id?: string;\n protected readonly steps: ReadonlyArray<StepNode>;\n\n protected constructor(steps: ReadonlyArray<StepNode>, id?: string) {\n this.steps = steps;\n this.id = id;\n }\n\n // ── Execution ─────────────────────────────────────────────────\n\n async generate(ctx: TContext, ...args: TInput extends void ? [input?: TInput] : [input: TInput]): Promise<WorkflowResult<TOutput>> {\n const input = args[0];\n const state: RuntimeState = {\n ctx,\n output: input,\n mode: \"generate\",\n };\n\n await this.execute(state);\n\n return {\n output: state.output as TOutput,\n };\n }\n\n stream(\n ctx: TContext,\n ...args: TInput extends void\n ? [input?: TInput, options?: WorkflowStreamOptions]\n : [input: TInput, options?: WorkflowStreamOptions]\n ): WorkflowStreamResult<TOutput> {\n const input = args[0];\n const options = args[1] as WorkflowStreamOptions | undefined;\n\n let resolveOutput: (value: TOutput) => void;\n let rejectOutput: (error: unknown) => void;\n const outputPromise = new Promise<TOutput>((res, rej) => {\n resolveOutput = res;\n rejectOutput = rej;\n });\n\n // Prevent unhandled rejection warning if the consumer never awaits `output`.\n // The original promise still rejects normally when awaited.\n outputPromise.catch(() => {});\n\n const stream = createUIMessageStream({\n execute: async ({ writer }) => {\n const state: RuntimeState = {\n ctx,\n output: input,\n mode: \"stream\",\n writer,\n };\n\n try {\n await this.execute(state);\n resolveOutput(state.output as TOutput);\n } catch (error) {\n rejectOutput!(error);\n throw error;\n }\n },\n ...(options?.onError ? { onError: options.onError } : {}),\n ...(options?.onFinish ? { onFinish: options.onFinish } : {}),\n });\n\n return {\n stream,\n output: outputPromise,\n };\n }\n\n // ── Internal: execute pipeline ────────────────────────────────\n\n protected async execute(state: RuntimeState): Promise<void> {\n if (this.steps.length === 0) {\n throw new Error(\"Workflow has no steps. Add at least one step before calling generate() or stream().\");\n }\n\n let pendingError: { error: unknown; stepId: string } | null = null;\n\n for (const node of this.steps) {\n if (node.type === \"finally\") {\n await node.execute(state);\n continue;\n }\n\n if (node.type === \"catch\") {\n if (!pendingError) continue;\n try {\n state.output = await node.catchFn({\n error: pendingError.error,\n ctx: state.ctx,\n lastOutput: state.output,\n stepId: pendingError.stepId,\n });\n pendingError = null;\n } catch (catchError) {\n pendingError = { error: catchError, stepId: node.id };\n }\n continue;\n }\n\n // type === \"step\" — skip while in error state\n if (pendingError) continue;\n\n try {\n await node.execute(state);\n } catch (error) {\n pendingError = { error, stepId: node.id };\n }\n }\n\n if (pendingError) throw pendingError.error;\n }\n\n // ── Internal: execute a nested workflow within a step/loop ─────\n // Defined on SealedWorkflow (not Workflow) because TypeScript's protected\n // access rules only allow calling workflow.execute() from the same class.\n\n protected async executeNestedWorkflow(\n state: RuntimeState,\n workflow: SealedWorkflow<TContext, unknown, unknown>,\n ): Promise<void> {\n await workflow.execute(state);\n }\n\n // ── Internal: execute an agent within a step/branch ───────────\n // In stream mode, output extraction awaits the full stream before returning.\n // Streaming benefits the client (incremental output), not pipeline throughput —\n // each step still runs sequentially.\n\n protected async executeAgent<TAgentInput, TNextOutput>(\n state: RuntimeState,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n agent: Agent<TContext, any, TNextOutput>,\n ctx: TContext,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n options?: AgentStepHooks<TContext, any, TNextOutput>,\n ): Promise<void> {\n const input = state.output as TAgentInput;\n const hasStructuredOutput = agent.hasOutput;\n\n if (state.mode === \"stream\" && state.writer) {\n const result = await (agent.stream as (ctx: TContext, input: unknown) => Promise<StreamTextResult>)(ctx, state.output);\n\n if (options?.handleStream) {\n await options.handleStream({ result, writer: state.writer, ctx });\n } else {\n state.writer.merge(result.toUIMessageStream());\n }\n\n if (options?.onStreamResult) {\n await options.onStreamResult({ result, ctx, input });\n }\n\n if (options?.mapStreamResult) {\n state.output = await options.mapStreamResult({ result, ctx, input });\n } else {\n state.output = await extractOutput(result, hasStructuredOutput);\n }\n } else {\n const result = await (agent.generate as (ctx: TContext, input: unknown) => Promise<GenerateTextResult>)(ctx, state.output);\n\n if (options?.onGenerateResult) {\n await options.onGenerateResult({ result, ctx, input });\n }\n\n if (options?.mapGenerateResult) {\n state.output = await options.mapGenerateResult({ result, ctx, input });\n } else {\n state.output = await extractOutput(result, hasStructuredOutput);\n }\n }\n }\n}\n\n// ── Workflow ────────────────────────────────────────────────────────\n\nexport class Workflow<\n TContext,\n TInput = void,\n TOutput = void,\n> extends SealedWorkflow<TContext, TInput, TOutput> {\n\n private constructor(steps: ReadonlyArray<StepNode> = [], id?: string) {\n super(steps, id);\n }\n\n static create<TContext, TInput = void>(options?: { id?: string }): Workflow<TContext, TInput, TInput> {\n return new Workflow<TContext, TInput, TInput>([], options?.id);\n }\n\n static from<TContext, TInput, TOutput>(\n agent: Agent<TContext, TInput, TOutput>,\n options?: StepOptions<TContext, TInput, TOutput>\n ): Workflow<TContext, TInput, TOutput> {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return new Workflow<TContext, TInput, any>([]).step(agent, options);\n }\n\n // ── step: agent overload ──────────────────────────────────────\n\n step<TNextOutput>(\n agent: Agent<TContext, TOutput, TNextOutput>,\n options?: StepOptions<TContext, TOutput, TNextOutput>\n ): Workflow<TContext, TInput, TNextOutput>;\n\n // ── step: nested workflow overload ─────────────────────────────\n\n step<TNextOutput>(\n workflow: SealedWorkflow<TContext, TOutput, TNextOutput>,\n ): Workflow<TContext, TInput, TNextOutput>;\n\n // ── step: transform overload (replaces map + tap) ─────────────\n\n step<TNextOutput>(\n id: string,\n fn: (params: { ctx: Readonly<TContext>; input: TOutput }) => MaybePromise<TNextOutput>\n ): Workflow<TContext, TInput, TNextOutput>;\n\n // ── step: implementation ──────────────────────────────────────\n\n step<TNextOutput>(\n target: Agent<TContext, TOutput, TNextOutput> | SealedWorkflow<TContext, TOutput, TNextOutput> | string,\n optionsOrFn?: StepOptions<TContext, TOutput, TNextOutput> | ((params: { ctx: Readonly<TContext>; input: TOutput }) => MaybePromise<TNextOutput>)\n ): Workflow<TContext, TInput, TNextOutput> {\n // Nested workflow overload: step(workflow)\n if (target instanceof SealedWorkflow) {\n const workflow = target;\n const node: StepNode = {\n type: \"step\",\n id: workflow.id ?? \"nested-workflow\",\n execute: async (state) => {\n await this.executeNestedWorkflow(state, workflow as SealedWorkflow<TContext, unknown, unknown>);\n },\n };\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return new Workflow<TContext, TInput, TNextOutput>([...this.steps, node] as any, this.id);\n }\n\n // Transform overload: step(id, fn)\n if (typeof target === \"string\") {\n if (typeof optionsOrFn !== \"function\") {\n throw new Error(`Workflow step(\"${target}\"): second argument must be a function`);\n }\n const fn = optionsOrFn as (params: { ctx: Readonly<TContext>; input: TOutput }) => MaybePromise<TNextOutput>;\n const node: StepNode = {\n type: \"step\",\n id: target,\n execute: async (state) => {\n state.output = await fn({\n ctx: state.ctx as Readonly<TContext>,\n input: state.output as TOutput,\n });\n },\n };\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return new Workflow<TContext, TInput, TNextOutput>([...this.steps, node] as any, this.id);\n }\n\n // Agent overload: step(agent, options?)\n const agent = target;\n const options = optionsOrFn as StepOptions<TContext, TOutput, TNextOutput> | undefined;\n const node: StepNode = {\n type: \"step\",\n id: agent.id,\n execute: async (state) => {\n const ctx = state.ctx as TContext;\n await this.executeAgent(state, agent, ctx, options);\n },\n };\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return new Workflow<TContext, TInput, TNextOutput>([...this.steps, node] as any, this.id);\n }\n\n // ── branch: predicate routing (array) ─────────────────────────\n\n branch<TNextOutput>(\n cases: BranchCase<TContext, TOutput, TNextOutput>[]\n ): Workflow<TContext, TInput, TNextOutput>;\n\n // ── branch: key routing (select) ──────────────────────────────\n\n branch<TKeys extends string, TNextOutput>(\n config: BranchSelect<TContext, TOutput, TKeys, TNextOutput>\n ): Workflow<TContext, TInput, TNextOutput>;\n\n // ── branch: implementation ────────────────────────────────────\n\n branch<TKeys extends string, TNextOutput>(\n casesOrConfig: BranchCase<TContext, TOutput, TNextOutput>[] | BranchSelect<TContext, TOutput, TKeys, TNextOutput>\n ): Workflow<TContext, TInput, TNextOutput> {\n if (Array.isArray(casesOrConfig)) {\n return this.branchPredicate(casesOrConfig);\n }\n return this.branchSelect(casesOrConfig);\n }\n\n private branchPredicate<TNextOutput>(\n cases: BranchCase<TContext, TOutput, TNextOutput>[]\n ): Workflow<TContext, TInput, TNextOutput> {\n const node: StepNode = {\n type: \"step\",\n id: \"branch:predicate\",\n execute: async (state) => {\n const ctx = state.ctx as TContext;\n const input = state.output as TOutput;\n\n for (const branchCase of cases) {\n if (branchCase.when) {\n const match = await branchCase.when({ ctx, input });\n if (!match) continue;\n }\n\n // Matched (or no `when` = default)\n await this.executeAgent(state, branchCase.agent, ctx, branchCase);\n return;\n }\n\n throw new WorkflowBranchError(\"predicate\", `No branch matched and no default branch (a case without \\`when\\`) was provided. Input: ${JSON.stringify(input)}`);\n },\n };\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return new Workflow<TContext, TInput, TNextOutput>([...this.steps, node] as any, this.id);\n }\n\n private branchSelect<TKeys extends string, TNextOutput>(\n config: BranchSelect<TContext, TOutput, TKeys, TNextOutput>\n ): Workflow<TContext, TInput, TNextOutput> {\n const node: StepNode = {\n type: \"step\",\n id: \"branch:select\",\n execute: async (state) => {\n const ctx = state.ctx as TContext;\n const input = state.output as TOutput;\n const key = await config.select({ ctx, input });\n\n let agent = config.agents[key];\n if (!agent) {\n if (config.fallback) {\n agent = config.fallback;\n } else {\n throw new WorkflowBranchError(\"select\", `No agent found for key \"${key}\" and no fallback provided. Available keys: ${Object.keys(config.agents).join(\", \")}`);\n }\n }\n\n await this.executeAgent(state, agent, ctx, config);\n },\n };\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return new Workflow<TContext, TInput, TNextOutput>([...this.steps, node] as any, this.id);\n }\n\n // ── foreach: array iteration ─────────────────────────────────\n\n foreach<TNextOutput>(\n target: Agent<TContext, ElementOf<TOutput>, TNextOutput> | SealedWorkflow<TContext, ElementOf<TOutput>, TNextOutput>,\n options?: { concurrency?: number },\n ): Workflow<TContext, TInput, TNextOutput[]> {\n const concurrency = options?.concurrency ?? 1;\n const isWorkflow = target instanceof SealedWorkflow;\n const id = isWorkflow ? (target.id ?? \"foreach\") : `foreach:${(target as Agent<TContext, ElementOf<TOutput>, TNextOutput>).id}`;\n\n const node: StepNode = {\n type: \"step\",\n id,\n execute: async (state) => {\n const items = state.output;\n if (!Array.isArray(items)) {\n throw new Error(`foreach \"${id}\": expected array input, got ${typeof items}`);\n }\n\n const ctx = state.ctx as TContext;\n const results: unknown[] = new Array(items.length);\n\n // Streaming is intentionally not propagated to foreach items —\n // each item runs in generate mode because merging interleaved\n // streams from parallel items into a single writer is not supported.\n const executeItem = async (item: unknown, index: number) => {\n const itemState: RuntimeState = { ctx: state.ctx, output: item, mode: \"generate\" };\n if (isWorkflow) {\n await this.executeNestedWorkflow(itemState, target as SealedWorkflow<TContext, unknown, unknown>);\n } else {\n await this.executeAgent(itemState, target as Agent<TContext, unknown, TNextOutput>, ctx);\n }\n results[index] = itemState.output;\n };\n\n if (concurrency <= 1) {\n for (let i = 0; i < items.length; i++) {\n await executeItem(items[i], i);\n }\n } else {\n for (let i = 0; i < items.length; i += concurrency) {\n const batch = items.slice(i, i + concurrency);\n await Promise.all(batch.map((item, j) => executeItem(item, i + j)));\n }\n }\n\n state.output = results;\n },\n };\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return new Workflow<TContext, TInput, TNextOutput[]>([...this.steps, node] as any, this.id);\n }\n\n // ── repeat: conditional loop ─────────────────────────────────\n\n repeat(\n target: Agent<TContext, TOutput, TOutput> | SealedWorkflow<TContext, TOutput, TOutput>,\n options: RepeatOptions<TContext, TOutput>,\n ): Workflow<TContext, TInput, TOutput> {\n const maxIterations = options.maxIterations ?? 10;\n const isWorkflow = target instanceof SealedWorkflow;\n const id = isWorkflow ? (target.id ?? \"repeat\") : `repeat:${(target as Agent<TContext, TOutput, TOutput>).id}`;\n const predicate: LoopPredicate<TContext, TOutput> = options.until\n ?? (async (p) => !(await options.while!(p)));\n\n const node: StepNode = {\n type: \"step\",\n id,\n execute: async (state) => {\n const ctx = state.ctx as TContext;\n\n for (let i = 1; i <= maxIterations; i++) {\n if (isWorkflow) {\n await this.executeNestedWorkflow(state, target as SealedWorkflow<TContext, unknown, unknown>);\n } else {\n await this.executeAgent(state, target as Agent<TContext, TOutput, TOutput>, ctx);\n }\n\n const done = await predicate({\n output: state.output as TOutput,\n ctx: ctx as Readonly<TContext>,\n iterations: i,\n });\n if (done) return;\n }\n\n throw new WorkflowLoopError(maxIterations, maxIterations);\n },\n };\n return new Workflow<TContext, TInput, TOutput>([...this.steps, node], this.id);\n }\n\n // ── catch ─────────────────────────────────────────────────────\n\n catch(\n id: string,\n fn: (params: { error: unknown; ctx: Readonly<TContext>; lastOutput: TOutput; stepId: string }) => MaybePromise<TOutput>\n ): Workflow<TContext, TInput, TOutput> {\n if (!this.steps.some(s => s.type === \"step\")) {\n throw new Error(`Workflow: catch(\"${id}\") requires at least one preceding step.`);\n }\n const node: StepNode = {\n type: \"catch\",\n id,\n catchFn: fn as (params: { error: unknown; ctx: unknown; lastOutput: unknown; stepId: string }) => MaybePromise<unknown>,\n };\n return new Workflow<TContext, TInput, TOutput>([...this.steps, node], this.id);\n }\n\n // ── finally (terminal — returns sealed workflow) ──────────────\n\n finally(\n id: string,\n fn: (params: { ctx: Readonly<TContext> }) => MaybePromise<void>\n ): SealedWorkflow<TContext, TInput, TOutput> {\n const node: StepNode = {\n type: \"finally\",\n id,\n execute: async (state) => {\n await fn({ ctx: state.ctx as Readonly<TContext> });\n },\n };\n return new SealedWorkflow<TContext, TInput, TOutput>([...this.steps, node], this.id);\n }\n}\n\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,IAAAA,aAeO;;;ACfP,gBAAgF;AAEzE,IAAM,sBAAsB,uBAAO,IAAI,6BAA6B;AAepE,IAAM,eAAN,MAI8B;AAAA,EACnC,CAAU,mB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AoC,CAAC,GAAG,KAAK,OAAO,IAAI,GAAG,KAAK,EAAE;AAAA,EAC/E;AAAA;AAAA,EAIA,MACE,IACA,IACqC;AACrC,QAAI,CAAC,KAAK,MAAM,KAAK,OAAK,EAAE,SAAS,MAAM,GAAG;AAC5C,YAAM,IAAI,MAAM,oBAAoB,EAAE,0CAA0C;AAAA,IAClF;AACA,UAAM,OAAiB;AAAA,MACrB,MAAM;AAAA,MACN;AAAA,MACA,SAAS;AAAA,IACX;AACA,WAAO,IAAI,UAAoC,CAAC,GAAG,KAAK,OAAO,IAAI,GAAG,KAAK,EAAE;AAAA,EAC/E;AAAA;AAAA,EAIA,QACE,IACA,IAC2C;AAC3C,UAAM,OAAiB;AAAA,MACrB,MAAM;AAAA,MACN;AAAA,MACA,SAAS,OAAO,UAAU;AACxB,cAAM,GAAG,EAAE,KAAK,MAAM,IAA0B,CAAC;AAAA,MACnD;AAAA,IACF;AACA,WAAO,IAAI,eAA0C,CAAC,GAAG,KAAK,OAAO,IAAI,GAAG,KAAK,EAAE;AAAA,EACrF;AACF;","names":["import_ai","import_ai","node"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
import { Tool, FlexibleSchema, ToolExecutionOptions, streamText, generateText, Output, LanguageModel, ModelMessage, ToolChoice, ToolSet, StopCondition, OnStepFinishEvent, OnFinishEvent, GenerateTextResult as GenerateTextResult$1, StreamTextResult as StreamTextResult$1, UIMessageStreamWriter } from 'ai';
|
|
2
|
+
import { ZodType } from 'zod';
|
|
3
|
+
|
|
4
|
+
declare const TOOL_PROVIDER_BRAND: unique symbol;
|
|
5
|
+
type ToolProviderConfig<TContext, TInput, TOutput> = {
|
|
6
|
+
description?: string;
|
|
7
|
+
input: FlexibleSchema<TInput>;
|
|
8
|
+
output?: FlexibleSchema<unknown>;
|
|
9
|
+
providerOptions?: unknown;
|
|
10
|
+
execute: (input: TInput, ctx: Readonly<TContext>, options?: ToolExecutionOptions) => Promise<TOutput>;
|
|
11
|
+
};
|
|
12
|
+
interface IToolProvider<TContext> {
|
|
13
|
+
readonly [TOOL_PROVIDER_BRAND]: true;
|
|
14
|
+
createTool(context: Readonly<TContext>): Tool;
|
|
15
|
+
}
|
|
16
|
+
declare class ToolProvider<TContext, TInput = unknown, TOutput = unknown> implements IToolProvider<TContext> {
|
|
17
|
+
readonly [TOOL_PROVIDER_BRAND]: true;
|
|
18
|
+
private readonly config;
|
|
19
|
+
constructor(config: ToolProviderConfig<TContext, TInput, TOutput>);
|
|
20
|
+
createTool(context: Readonly<TContext>): Tool;
|
|
21
|
+
}
|
|
22
|
+
declare function defineTool<TContext>(): <TInput, TOutput>(config: ToolProviderConfig<TContext, TInput, TOutput>) => ToolProvider<TContext, TInput, TOutput>;
|
|
23
|
+
|
|
24
|
+
type MaybePromise<T> = T | Promise<T>;
|
|
25
|
+
/**
|
|
26
|
+
* A value that can be static or derived from context and input.
|
|
27
|
+
* Used for agent config fields that may need runtime resolution.
|
|
28
|
+
*
|
|
29
|
+
* Functions may return a Promise for async resolution; static values are always sync.
|
|
30
|
+
*/
|
|
31
|
+
type Resolvable<TCtx, TInput, TValue> = TValue | ((ctx: Readonly<TCtx>, input: TInput) => TValue | Promise<TValue>);
|
|
32
|
+
|
|
33
|
+
type OutputType<T = any> = ReturnType<typeof Output.object<T>>;
|
|
34
|
+
|
|
35
|
+
type AgentToolSet<TContext> = Record<string, Tool | IToolProvider<TContext>>;
|
|
36
|
+
type GenerateTextResult<TOOLS extends ToolSet = ToolSet, OUTPUT extends OutputType = OutputType> = GenerateTextResult$1<TOOLS, OUTPUT>;
|
|
37
|
+
type StreamTextResult<TOOLS extends ToolSet = ToolSet, OUTPUT extends OutputType = OutputType> = StreamTextResult$1<TOOLS, OUTPUT>;
|
|
38
|
+
type StreamTextOptions = Parameters<typeof streamText>[0];
|
|
39
|
+
type GenerateTextOptions = Parameters<typeof generateText>[0];
|
|
40
|
+
type ManagedKeys = 'model' | 'system' | 'prompt' | 'messages' | 'tools' | 'activeTools' | 'toolChoice' | 'stopWhen' | 'output' | 'onFinish' | 'onStepFinish' | 'onError';
|
|
41
|
+
type AIPassthroughOptions = Omit<StreamTextOptions, ManagedKeys> & Omit<GenerateTextOptions, ManagedKeys>;
|
|
42
|
+
interface AgentConfig<TContext, TInput = void, TOutput = void> extends AIPassthroughOptions {
|
|
43
|
+
id: string;
|
|
44
|
+
description?: string;
|
|
45
|
+
input?: ZodType<TInput>;
|
|
46
|
+
output?: OutputType<TOutput>;
|
|
47
|
+
model: Resolvable<TContext, TInput, LanguageModel>;
|
|
48
|
+
system?: Resolvable<TContext, TInput, string>;
|
|
49
|
+
prompt?: Resolvable<TContext, TInput, string>;
|
|
50
|
+
messages?: Resolvable<TContext, TInput, ModelMessage[]>;
|
|
51
|
+
tools?: Resolvable<TContext, TInput, AgentToolSet<TContext>>;
|
|
52
|
+
activeTools?: Resolvable<TContext, TInput, string[]>;
|
|
53
|
+
toolChoice?: Resolvable<TContext, TInput, ToolChoice<ToolSet>>;
|
|
54
|
+
stopWhen?: Resolvable<TContext, TInput, StopCondition<ToolSet> | Array<StopCondition<ToolSet>>>;
|
|
55
|
+
onStepFinish?: (params: {
|
|
56
|
+
result: OnStepFinishEvent;
|
|
57
|
+
ctx: Readonly<TContext>;
|
|
58
|
+
input: TInput;
|
|
59
|
+
}) => MaybePromise<void>;
|
|
60
|
+
onFinish?: (params: {
|
|
61
|
+
result: OnFinishEvent;
|
|
62
|
+
ctx: Readonly<TContext>;
|
|
63
|
+
input: TInput;
|
|
64
|
+
}) => MaybePromise<void>;
|
|
65
|
+
onError?: (params: {
|
|
66
|
+
error: unknown;
|
|
67
|
+
ctx: Readonly<TContext>;
|
|
68
|
+
input: TInput;
|
|
69
|
+
}) => MaybePromise<void>;
|
|
70
|
+
}
|
|
71
|
+
declare class Agent<TContext, TInput = void, TOutput = void> {
|
|
72
|
+
readonly id: string;
|
|
73
|
+
readonly description: string;
|
|
74
|
+
readonly hasOutput: boolean;
|
|
75
|
+
private readonly config;
|
|
76
|
+
private readonly _hasDynamicConfig;
|
|
77
|
+
private readonly _resolvedStaticTools;
|
|
78
|
+
private readonly _passthrough;
|
|
79
|
+
private readonly _onStepFinish;
|
|
80
|
+
private readonly _onFinish;
|
|
81
|
+
constructor(config: AgentConfig<TContext, TInput, TOutput>);
|
|
82
|
+
generate(ctx: TContext, ...args: TInput extends void ? [input?: TInput] : [input: TInput]): Promise<GenerateTextResult>;
|
|
83
|
+
stream(ctx: TContext, ...args: TInput extends void ? [input?: TInput] : [input: TInput]): Promise<StreamTextResult>;
|
|
84
|
+
asTool(ctx: TContext, options?: {
|
|
85
|
+
mapOutput?: (result: GenerateTextResult) => MaybePromise<TOutput>;
|
|
86
|
+
}): Tool;
|
|
87
|
+
asToolProvider(options?: {
|
|
88
|
+
mapOutput?: (result: GenerateTextResult) => MaybePromise<TOutput>;
|
|
89
|
+
}): IToolProvider<TContext>;
|
|
90
|
+
private createToolInstance;
|
|
91
|
+
private buildCallOptions;
|
|
92
|
+
private resolveConfig;
|
|
93
|
+
private resolveConfigAsync;
|
|
94
|
+
private resolveTools;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
declare class WorkflowBranchError extends Error {
|
|
98
|
+
readonly branchType: "predicate" | "select";
|
|
99
|
+
constructor(branchType: "predicate" | "select", message: string);
|
|
100
|
+
}
|
|
101
|
+
declare class WorkflowLoopError extends Error {
|
|
102
|
+
readonly iterations: number;
|
|
103
|
+
readonly maxIterations: number;
|
|
104
|
+
constructor(iterations: number, maxIterations: number);
|
|
105
|
+
}
|
|
106
|
+
interface AgentStepHooks<TContext, TOutput, TNextOutput> {
|
|
107
|
+
mapGenerateResult?: (params: {
|
|
108
|
+
result: GenerateTextResult;
|
|
109
|
+
ctx: Readonly<TContext>;
|
|
110
|
+
input: TOutput;
|
|
111
|
+
}) => MaybePromise<TNextOutput>;
|
|
112
|
+
mapStreamResult?: (params: {
|
|
113
|
+
result: StreamTextResult;
|
|
114
|
+
ctx: Readonly<TContext>;
|
|
115
|
+
input: TOutput;
|
|
116
|
+
}) => MaybePromise<TNextOutput>;
|
|
117
|
+
onGenerateResult?: (params: {
|
|
118
|
+
result: GenerateTextResult;
|
|
119
|
+
ctx: Readonly<TContext>;
|
|
120
|
+
input: TOutput;
|
|
121
|
+
}) => MaybePromise<void>;
|
|
122
|
+
onStreamResult?: (params: {
|
|
123
|
+
result: StreamTextResult;
|
|
124
|
+
ctx: Readonly<TContext>;
|
|
125
|
+
input: TOutput;
|
|
126
|
+
}) => MaybePromise<void>;
|
|
127
|
+
handleStream?: (params: {
|
|
128
|
+
result: StreamTextResult;
|
|
129
|
+
writer: UIMessageStreamWriter;
|
|
130
|
+
ctx: Readonly<TContext>;
|
|
131
|
+
}) => MaybePromise<void>;
|
|
132
|
+
}
|
|
133
|
+
type StepOptions<TContext, TOutput, TNextOutput> = AgentStepHooks<TContext, TOutput, TNextOutput>;
|
|
134
|
+
interface BranchCase<TContext, TOutput, TNextOutput> extends AgentStepHooks<TContext, TOutput, TNextOutput> {
|
|
135
|
+
when?: (params: {
|
|
136
|
+
ctx: Readonly<TContext>;
|
|
137
|
+
input: TOutput;
|
|
138
|
+
}) => MaybePromise<boolean>;
|
|
139
|
+
agent: Agent<TContext, TOutput, TNextOutput>;
|
|
140
|
+
}
|
|
141
|
+
interface BranchSelect<TContext, TOutput, TKeys extends string, TNextOutput> extends AgentStepHooks<TContext, TOutput, TNextOutput> {
|
|
142
|
+
select: (params: {
|
|
143
|
+
ctx: Readonly<TContext>;
|
|
144
|
+
input: TOutput;
|
|
145
|
+
}) => MaybePromise<TKeys>;
|
|
146
|
+
agents: Record<TKeys, Agent<TContext, any, TNextOutput>>;
|
|
147
|
+
fallback?: Agent<TContext, any, TNextOutput>;
|
|
148
|
+
}
|
|
149
|
+
interface WorkflowResult<TOutput> {
|
|
150
|
+
output: TOutput;
|
|
151
|
+
}
|
|
152
|
+
interface WorkflowStreamResult<TOutput> {
|
|
153
|
+
stream: ReadableStream;
|
|
154
|
+
output: Promise<TOutput>;
|
|
155
|
+
}
|
|
156
|
+
interface WorkflowStreamOptions {
|
|
157
|
+
onError?: (error: unknown) => string;
|
|
158
|
+
onFinish?: () => MaybePromise<void>;
|
|
159
|
+
}
|
|
160
|
+
type LoopPredicate<TContext, TOutput> = (params: {
|
|
161
|
+
output: TOutput;
|
|
162
|
+
ctx: Readonly<TContext>;
|
|
163
|
+
iterations: number;
|
|
164
|
+
}) => MaybePromise<boolean>;
|
|
165
|
+
type RepeatOptions<TContext, TOutput> = {
|
|
166
|
+
until: LoopPredicate<TContext, TOutput>;
|
|
167
|
+
while?: never;
|
|
168
|
+
maxIterations?: number;
|
|
169
|
+
} | {
|
|
170
|
+
while: LoopPredicate<TContext, TOutput>;
|
|
171
|
+
until?: never;
|
|
172
|
+
maxIterations?: number;
|
|
173
|
+
};
|
|
174
|
+
type ElementOf<T> = T extends readonly (infer E)[] ? E : never;
|
|
175
|
+
type StepNode = {
|
|
176
|
+
readonly type: "step";
|
|
177
|
+
readonly id: string;
|
|
178
|
+
readonly execute: (state: RuntimeState) => MaybePromise<void>;
|
|
179
|
+
} | {
|
|
180
|
+
readonly type: "catch";
|
|
181
|
+
readonly id: string;
|
|
182
|
+
readonly catchFn: (params: {
|
|
183
|
+
error: unknown;
|
|
184
|
+
ctx: unknown;
|
|
185
|
+
lastOutput: unknown;
|
|
186
|
+
stepId: string;
|
|
187
|
+
}) => MaybePromise<unknown>;
|
|
188
|
+
} | {
|
|
189
|
+
readonly type: "finally";
|
|
190
|
+
readonly id: string;
|
|
191
|
+
readonly execute: (state: RuntimeState) => MaybePromise<void>;
|
|
192
|
+
};
|
|
193
|
+
interface RuntimeState {
|
|
194
|
+
ctx: unknown;
|
|
195
|
+
output: unknown;
|
|
196
|
+
mode: "generate" | "stream";
|
|
197
|
+
writer?: UIMessageStreamWriter;
|
|
198
|
+
}
|
|
199
|
+
declare class SealedWorkflow<TContext, TInput = void, TOutput = void> {
|
|
200
|
+
readonly id?: string;
|
|
201
|
+
protected readonly steps: ReadonlyArray<StepNode>;
|
|
202
|
+
protected constructor(steps: ReadonlyArray<StepNode>, id?: string);
|
|
203
|
+
generate(ctx: TContext, ...args: TInput extends void ? [input?: TInput] : [input: TInput]): Promise<WorkflowResult<TOutput>>;
|
|
204
|
+
stream(ctx: TContext, ...args: TInput extends void ? [input?: TInput, options?: WorkflowStreamOptions] : [input: TInput, options?: WorkflowStreamOptions]): WorkflowStreamResult<TOutput>;
|
|
205
|
+
protected execute(state: RuntimeState): Promise<void>;
|
|
206
|
+
protected executeNestedWorkflow(state: RuntimeState, workflow: SealedWorkflow<TContext, unknown, unknown>): Promise<void>;
|
|
207
|
+
protected executeAgent<TAgentInput, TNextOutput>(state: RuntimeState, agent: Agent<TContext, any, TNextOutput>, ctx: TContext, options?: AgentStepHooks<TContext, any, TNextOutput>): Promise<void>;
|
|
208
|
+
}
|
|
209
|
+
declare class Workflow<TContext, TInput = void, TOutput = void> extends SealedWorkflow<TContext, TInput, TOutput> {
|
|
210
|
+
private constructor();
|
|
211
|
+
static create<TContext, TInput = void>(options?: {
|
|
212
|
+
id?: string;
|
|
213
|
+
}): Workflow<TContext, TInput, TInput>;
|
|
214
|
+
static from<TContext, TInput, TOutput>(agent: Agent<TContext, TInput, TOutput>, options?: StepOptions<TContext, TInput, TOutput>): Workflow<TContext, TInput, TOutput>;
|
|
215
|
+
step<TNextOutput>(agent: Agent<TContext, TOutput, TNextOutput>, options?: StepOptions<TContext, TOutput, TNextOutput>): Workflow<TContext, TInput, TNextOutput>;
|
|
216
|
+
step<TNextOutput>(workflow: SealedWorkflow<TContext, TOutput, TNextOutput>): Workflow<TContext, TInput, TNextOutput>;
|
|
217
|
+
step<TNextOutput>(id: string, fn: (params: {
|
|
218
|
+
ctx: Readonly<TContext>;
|
|
219
|
+
input: TOutput;
|
|
220
|
+
}) => MaybePromise<TNextOutput>): Workflow<TContext, TInput, TNextOutput>;
|
|
221
|
+
branch<TNextOutput>(cases: BranchCase<TContext, TOutput, TNextOutput>[]): Workflow<TContext, TInput, TNextOutput>;
|
|
222
|
+
branch<TKeys extends string, TNextOutput>(config: BranchSelect<TContext, TOutput, TKeys, TNextOutput>): Workflow<TContext, TInput, TNextOutput>;
|
|
223
|
+
private branchPredicate;
|
|
224
|
+
private branchSelect;
|
|
225
|
+
foreach<TNextOutput>(target: Agent<TContext, ElementOf<TOutput>, TNextOutput> | SealedWorkflow<TContext, ElementOf<TOutput>, TNextOutput>, options?: {
|
|
226
|
+
concurrency?: number;
|
|
227
|
+
}): Workflow<TContext, TInput, TNextOutput[]>;
|
|
228
|
+
repeat(target: Agent<TContext, TOutput, TOutput> | SealedWorkflow<TContext, TOutput, TOutput>, options: RepeatOptions<TContext, TOutput>): Workflow<TContext, TInput, TOutput>;
|
|
229
|
+
catch(id: string, fn: (params: {
|
|
230
|
+
error: unknown;
|
|
231
|
+
ctx: Readonly<TContext>;
|
|
232
|
+
lastOutput: TOutput;
|
|
233
|
+
stepId: string;
|
|
234
|
+
}) => MaybePromise<TOutput>): Workflow<TContext, TInput, TOutput>;
|
|
235
|
+
finally(id: string, fn: (params: {
|
|
236
|
+
ctx: Readonly<TContext>;
|
|
237
|
+
}) => MaybePromise<void>): SealedWorkflow<TContext, TInput, TOutput>;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
export { Agent, type AgentConfig, type AgentStepHooks, type BranchCase, type BranchSelect, type GenerateTextResult, type IToolProvider, type MaybePromise, type RepeatOptions, type Resolvable, SealedWorkflow, type StepOptions, type StreamTextResult, type ToolProviderConfig, Workflow, WorkflowBranchError, WorkflowLoopError, type WorkflowResult, type WorkflowStreamOptions, type WorkflowStreamResult, defineTool };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
import { Tool, FlexibleSchema, ToolExecutionOptions, streamText, generateText, Output, LanguageModel, ModelMessage, ToolChoice, ToolSet, StopCondition, OnStepFinishEvent, OnFinishEvent, GenerateTextResult as GenerateTextResult$1, StreamTextResult as StreamTextResult$1, UIMessageStreamWriter } from 'ai';
|
|
2
|
+
import { ZodType } from 'zod';
|
|
3
|
+
|
|
4
|
+
declare const TOOL_PROVIDER_BRAND: unique symbol;
|
|
5
|
+
type ToolProviderConfig<TContext, TInput, TOutput> = {
|
|
6
|
+
description?: string;
|
|
7
|
+
input: FlexibleSchema<TInput>;
|
|
8
|
+
output?: FlexibleSchema<unknown>;
|
|
9
|
+
providerOptions?: unknown;
|
|
10
|
+
execute: (input: TInput, ctx: Readonly<TContext>, options?: ToolExecutionOptions) => Promise<TOutput>;
|
|
11
|
+
};
|
|
12
|
+
interface IToolProvider<TContext> {
|
|
13
|
+
readonly [TOOL_PROVIDER_BRAND]: true;
|
|
14
|
+
createTool(context: Readonly<TContext>): Tool;
|
|
15
|
+
}
|
|
16
|
+
declare class ToolProvider<TContext, TInput = unknown, TOutput = unknown> implements IToolProvider<TContext> {
|
|
17
|
+
readonly [TOOL_PROVIDER_BRAND]: true;
|
|
18
|
+
private readonly config;
|
|
19
|
+
constructor(config: ToolProviderConfig<TContext, TInput, TOutput>);
|
|
20
|
+
createTool(context: Readonly<TContext>): Tool;
|
|
21
|
+
}
|
|
22
|
+
declare function defineTool<TContext>(): <TInput, TOutput>(config: ToolProviderConfig<TContext, TInput, TOutput>) => ToolProvider<TContext, TInput, TOutput>;
|
|
23
|
+
|
|
24
|
+
type MaybePromise<T> = T | Promise<T>;
|
|
25
|
+
/**
|
|
26
|
+
* A value that can be static or derived from context and input.
|
|
27
|
+
* Used for agent config fields that may need runtime resolution.
|
|
28
|
+
*
|
|
29
|
+
* Functions may return a Promise for async resolution; static values are always sync.
|
|
30
|
+
*/
|
|
31
|
+
type Resolvable<TCtx, TInput, TValue> = TValue | ((ctx: Readonly<TCtx>, input: TInput) => TValue | Promise<TValue>);
|
|
32
|
+
|
|
33
|
+
type OutputType<T = any> = ReturnType<typeof Output.object<T>>;
|
|
34
|
+
|
|
35
|
+
type AgentToolSet<TContext> = Record<string, Tool | IToolProvider<TContext>>;
|
|
36
|
+
type GenerateTextResult<TOOLS extends ToolSet = ToolSet, OUTPUT extends OutputType = OutputType> = GenerateTextResult$1<TOOLS, OUTPUT>;
|
|
37
|
+
type StreamTextResult<TOOLS extends ToolSet = ToolSet, OUTPUT extends OutputType = OutputType> = StreamTextResult$1<TOOLS, OUTPUT>;
|
|
38
|
+
type StreamTextOptions = Parameters<typeof streamText>[0];
|
|
39
|
+
type GenerateTextOptions = Parameters<typeof generateText>[0];
|
|
40
|
+
type ManagedKeys = 'model' | 'system' | 'prompt' | 'messages' | 'tools' | 'activeTools' | 'toolChoice' | 'stopWhen' | 'output' | 'onFinish' | 'onStepFinish' | 'onError';
|
|
41
|
+
type AIPassthroughOptions = Omit<StreamTextOptions, ManagedKeys> & Omit<GenerateTextOptions, ManagedKeys>;
|
|
42
|
+
interface AgentConfig<TContext, TInput = void, TOutput = void> extends AIPassthroughOptions {
|
|
43
|
+
id: string;
|
|
44
|
+
description?: string;
|
|
45
|
+
input?: ZodType<TInput>;
|
|
46
|
+
output?: OutputType<TOutput>;
|
|
47
|
+
model: Resolvable<TContext, TInput, LanguageModel>;
|
|
48
|
+
system?: Resolvable<TContext, TInput, string>;
|
|
49
|
+
prompt?: Resolvable<TContext, TInput, string>;
|
|
50
|
+
messages?: Resolvable<TContext, TInput, ModelMessage[]>;
|
|
51
|
+
tools?: Resolvable<TContext, TInput, AgentToolSet<TContext>>;
|
|
52
|
+
activeTools?: Resolvable<TContext, TInput, string[]>;
|
|
53
|
+
toolChoice?: Resolvable<TContext, TInput, ToolChoice<ToolSet>>;
|
|
54
|
+
stopWhen?: Resolvable<TContext, TInput, StopCondition<ToolSet> | Array<StopCondition<ToolSet>>>;
|
|
55
|
+
onStepFinish?: (params: {
|
|
56
|
+
result: OnStepFinishEvent;
|
|
57
|
+
ctx: Readonly<TContext>;
|
|
58
|
+
input: TInput;
|
|
59
|
+
}) => MaybePromise<void>;
|
|
60
|
+
onFinish?: (params: {
|
|
61
|
+
result: OnFinishEvent;
|
|
62
|
+
ctx: Readonly<TContext>;
|
|
63
|
+
input: TInput;
|
|
64
|
+
}) => MaybePromise<void>;
|
|
65
|
+
onError?: (params: {
|
|
66
|
+
error: unknown;
|
|
67
|
+
ctx: Readonly<TContext>;
|
|
68
|
+
input: TInput;
|
|
69
|
+
}) => MaybePromise<void>;
|
|
70
|
+
}
|
|
71
|
+
declare class Agent<TContext, TInput = void, TOutput = void> {
|
|
72
|
+
readonly id: string;
|
|
73
|
+
readonly description: string;
|
|
74
|
+
readonly hasOutput: boolean;
|
|
75
|
+
private readonly config;
|
|
76
|
+
private readonly _hasDynamicConfig;
|
|
77
|
+
private readonly _resolvedStaticTools;
|
|
78
|
+
private readonly _passthrough;
|
|
79
|
+
private readonly _onStepFinish;
|
|
80
|
+
private readonly _onFinish;
|
|
81
|
+
constructor(config: AgentConfig<TContext, TInput, TOutput>);
|
|
82
|
+
generate(ctx: TContext, ...args: TInput extends void ? [input?: TInput] : [input: TInput]): Promise<GenerateTextResult>;
|
|
83
|
+
stream(ctx: TContext, ...args: TInput extends void ? [input?: TInput] : [input: TInput]): Promise<StreamTextResult>;
|
|
84
|
+
asTool(ctx: TContext, options?: {
|
|
85
|
+
mapOutput?: (result: GenerateTextResult) => MaybePromise<TOutput>;
|
|
86
|
+
}): Tool;
|
|
87
|
+
asToolProvider(options?: {
|
|
88
|
+
mapOutput?: (result: GenerateTextResult) => MaybePromise<TOutput>;
|
|
89
|
+
}): IToolProvider<TContext>;
|
|
90
|
+
private createToolInstance;
|
|
91
|
+
private buildCallOptions;
|
|
92
|
+
private resolveConfig;
|
|
93
|
+
private resolveConfigAsync;
|
|
94
|
+
private resolveTools;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
declare class WorkflowBranchError extends Error {
|
|
98
|
+
readonly branchType: "predicate" | "select";
|
|
99
|
+
constructor(branchType: "predicate" | "select", message: string);
|
|
100
|
+
}
|
|
101
|
+
declare class WorkflowLoopError extends Error {
|
|
102
|
+
readonly iterations: number;
|
|
103
|
+
readonly maxIterations: number;
|
|
104
|
+
constructor(iterations: number, maxIterations: number);
|
|
105
|
+
}
|
|
106
|
+
interface AgentStepHooks<TContext, TOutput, TNextOutput> {
|
|
107
|
+
mapGenerateResult?: (params: {
|
|
108
|
+
result: GenerateTextResult;
|
|
109
|
+
ctx: Readonly<TContext>;
|
|
110
|
+
input: TOutput;
|
|
111
|
+
}) => MaybePromise<TNextOutput>;
|
|
112
|
+
mapStreamResult?: (params: {
|
|
113
|
+
result: StreamTextResult;
|
|
114
|
+
ctx: Readonly<TContext>;
|
|
115
|
+
input: TOutput;
|
|
116
|
+
}) => MaybePromise<TNextOutput>;
|
|
117
|
+
onGenerateResult?: (params: {
|
|
118
|
+
result: GenerateTextResult;
|
|
119
|
+
ctx: Readonly<TContext>;
|
|
120
|
+
input: TOutput;
|
|
121
|
+
}) => MaybePromise<void>;
|
|
122
|
+
onStreamResult?: (params: {
|
|
123
|
+
result: StreamTextResult;
|
|
124
|
+
ctx: Readonly<TContext>;
|
|
125
|
+
input: TOutput;
|
|
126
|
+
}) => MaybePromise<void>;
|
|
127
|
+
handleStream?: (params: {
|
|
128
|
+
result: StreamTextResult;
|
|
129
|
+
writer: UIMessageStreamWriter;
|
|
130
|
+
ctx: Readonly<TContext>;
|
|
131
|
+
}) => MaybePromise<void>;
|
|
132
|
+
}
|
|
133
|
+
type StepOptions<TContext, TOutput, TNextOutput> = AgentStepHooks<TContext, TOutput, TNextOutput>;
|
|
134
|
+
interface BranchCase<TContext, TOutput, TNextOutput> extends AgentStepHooks<TContext, TOutput, TNextOutput> {
|
|
135
|
+
when?: (params: {
|
|
136
|
+
ctx: Readonly<TContext>;
|
|
137
|
+
input: TOutput;
|
|
138
|
+
}) => MaybePromise<boolean>;
|
|
139
|
+
agent: Agent<TContext, TOutput, TNextOutput>;
|
|
140
|
+
}
|
|
141
|
+
interface BranchSelect<TContext, TOutput, TKeys extends string, TNextOutput> extends AgentStepHooks<TContext, TOutput, TNextOutput> {
|
|
142
|
+
select: (params: {
|
|
143
|
+
ctx: Readonly<TContext>;
|
|
144
|
+
input: TOutput;
|
|
145
|
+
}) => MaybePromise<TKeys>;
|
|
146
|
+
agents: Record<TKeys, Agent<TContext, any, TNextOutput>>;
|
|
147
|
+
fallback?: Agent<TContext, any, TNextOutput>;
|
|
148
|
+
}
|
|
149
|
+
interface WorkflowResult<TOutput> {
|
|
150
|
+
output: TOutput;
|
|
151
|
+
}
|
|
152
|
+
interface WorkflowStreamResult<TOutput> {
|
|
153
|
+
stream: ReadableStream;
|
|
154
|
+
output: Promise<TOutput>;
|
|
155
|
+
}
|
|
156
|
+
interface WorkflowStreamOptions {
|
|
157
|
+
onError?: (error: unknown) => string;
|
|
158
|
+
onFinish?: () => MaybePromise<void>;
|
|
159
|
+
}
|
|
160
|
+
type LoopPredicate<TContext, TOutput> = (params: {
|
|
161
|
+
output: TOutput;
|
|
162
|
+
ctx: Readonly<TContext>;
|
|
163
|
+
iterations: number;
|
|
164
|
+
}) => MaybePromise<boolean>;
|
|
165
|
+
type RepeatOptions<TContext, TOutput> = {
|
|
166
|
+
until: LoopPredicate<TContext, TOutput>;
|
|
167
|
+
while?: never;
|
|
168
|
+
maxIterations?: number;
|
|
169
|
+
} | {
|
|
170
|
+
while: LoopPredicate<TContext, TOutput>;
|
|
171
|
+
until?: never;
|
|
172
|
+
maxIterations?: number;
|
|
173
|
+
};
|
|
174
|
+
type ElementOf<T> = T extends readonly (infer E)[] ? E : never;
|
|
175
|
+
type StepNode = {
|
|
176
|
+
readonly type: "step";
|
|
177
|
+
readonly id: string;
|
|
178
|
+
readonly execute: (state: RuntimeState) => MaybePromise<void>;
|
|
179
|
+
} | {
|
|
180
|
+
readonly type: "catch";
|
|
181
|
+
readonly id: string;
|
|
182
|
+
readonly catchFn: (params: {
|
|
183
|
+
error: unknown;
|
|
184
|
+
ctx: unknown;
|
|
185
|
+
lastOutput: unknown;
|
|
186
|
+
stepId: string;
|
|
187
|
+
}) => MaybePromise<unknown>;
|
|
188
|
+
} | {
|
|
189
|
+
readonly type: "finally";
|
|
190
|
+
readonly id: string;
|
|
191
|
+
readonly execute: (state: RuntimeState) => MaybePromise<void>;
|
|
192
|
+
};
|
|
193
|
+
interface RuntimeState {
|
|
194
|
+
ctx: unknown;
|
|
195
|
+
output: unknown;
|
|
196
|
+
mode: "generate" | "stream";
|
|
197
|
+
writer?: UIMessageStreamWriter;
|
|
198
|
+
}
|
|
199
|
+
declare class SealedWorkflow<TContext, TInput = void, TOutput = void> {
|
|
200
|
+
readonly id?: string;
|
|
201
|
+
protected readonly steps: ReadonlyArray<StepNode>;
|
|
202
|
+
protected constructor(steps: ReadonlyArray<StepNode>, id?: string);
|
|
203
|
+
generate(ctx: TContext, ...args: TInput extends void ? [input?: TInput] : [input: TInput]): Promise<WorkflowResult<TOutput>>;
|
|
204
|
+
stream(ctx: TContext, ...args: TInput extends void ? [input?: TInput, options?: WorkflowStreamOptions] : [input: TInput, options?: WorkflowStreamOptions]): WorkflowStreamResult<TOutput>;
|
|
205
|
+
protected execute(state: RuntimeState): Promise<void>;
|
|
206
|
+
protected executeNestedWorkflow(state: RuntimeState, workflow: SealedWorkflow<TContext, unknown, unknown>): Promise<void>;
|
|
207
|
+
protected executeAgent<TAgentInput, TNextOutput>(state: RuntimeState, agent: Agent<TContext, any, TNextOutput>, ctx: TContext, options?: AgentStepHooks<TContext, any, TNextOutput>): Promise<void>;
|
|
208
|
+
}
|
|
209
|
+
declare class Workflow<TContext, TInput = void, TOutput = void> extends SealedWorkflow<TContext, TInput, TOutput> {
|
|
210
|
+
private constructor();
|
|
211
|
+
static create<TContext, TInput = void>(options?: {
|
|
212
|
+
id?: string;
|
|
213
|
+
}): Workflow<TContext, TInput, TInput>;
|
|
214
|
+
static from<TContext, TInput, TOutput>(agent: Agent<TContext, TInput, TOutput>, options?: StepOptions<TContext, TInput, TOutput>): Workflow<TContext, TInput, TOutput>;
|
|
215
|
+
step<TNextOutput>(agent: Agent<TContext, TOutput, TNextOutput>, options?: StepOptions<TContext, TOutput, TNextOutput>): Workflow<TContext, TInput, TNextOutput>;
|
|
216
|
+
step<TNextOutput>(workflow: SealedWorkflow<TContext, TOutput, TNextOutput>): Workflow<TContext, TInput, TNextOutput>;
|
|
217
|
+
step<TNextOutput>(id: string, fn: (params: {
|
|
218
|
+
ctx: Readonly<TContext>;
|
|
219
|
+
input: TOutput;
|
|
220
|
+
}) => MaybePromise<TNextOutput>): Workflow<TContext, TInput, TNextOutput>;
|
|
221
|
+
branch<TNextOutput>(cases: BranchCase<TContext, TOutput, TNextOutput>[]): Workflow<TContext, TInput, TNextOutput>;
|
|
222
|
+
branch<TKeys extends string, TNextOutput>(config: BranchSelect<TContext, TOutput, TKeys, TNextOutput>): Workflow<TContext, TInput, TNextOutput>;
|
|
223
|
+
private branchPredicate;
|
|
224
|
+
private branchSelect;
|
|
225
|
+
foreach<TNextOutput>(target: Agent<TContext, ElementOf<TOutput>, TNextOutput> | SealedWorkflow<TContext, ElementOf<TOutput>, TNextOutput>, options?: {
|
|
226
|
+
concurrency?: number;
|
|
227
|
+
}): Workflow<TContext, TInput, TNextOutput[]>;
|
|
228
|
+
repeat(target: Agent<TContext, TOutput, TOutput> | SealedWorkflow<TContext, TOutput, TOutput>, options: RepeatOptions<TContext, TOutput>): Workflow<TContext, TInput, TOutput>;
|
|
229
|
+
catch(id: string, fn: (params: {
|
|
230
|
+
error: unknown;
|
|
231
|
+
ctx: Readonly<TContext>;
|
|
232
|
+
lastOutput: TOutput;
|
|
233
|
+
stepId: string;
|
|
234
|
+
}) => MaybePromise<TOutput>): Workflow<TContext, TInput, TOutput>;
|
|
235
|
+
finally(id: string, fn: (params: {
|
|
236
|
+
ctx: Readonly<TContext>;
|
|
237
|
+
}) => MaybePromise<void>): SealedWorkflow<TContext, TInput, TOutput>;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
export { Agent, type AgentConfig, type AgentStepHooks, type BranchCase, type BranchSelect, type GenerateTextResult, type IToolProvider, type MaybePromise, type RepeatOptions, type Resolvable, SealedWorkflow, type StepOptions, type StreamTextResult, type ToolProviderConfig, Workflow, WorkflowBranchError, WorkflowLoopError, type WorkflowResult, type WorkflowStreamOptions, type WorkflowStreamResult, defineTool };
|