@gensx/react
Version:
React hooks and components for GenSX AI workflows.
90 lines • 3.05 kB
TypeScript
import type { JsonValue, ToolBox, ToolImplementations, WorkflowMessage } from "@gensx/core";
export interface WorkflowRunConfig<TInputs = unknown> {
inputs: TInputs;
org?: string;
project?: string;
environment?: string;
}
export interface WorkflowConfig {
baseUrl: string;
headers?: Record<string, string>;
}
export interface UseWorkflowConfig<TOutput = unknown, TToolBox extends ToolBox = ToolBox> {
/**
* All workflow configuration in one place
*/
config: WorkflowConfig;
/**
* External tools that can be called from the workflow
*/
tools?: ToolImplementations<TToolBox>;
/**
* Callback fired when workflow starts
*/
onStart?: (message: string) => void;
/**
* Callback fired when workflow completes
*/
onComplete?: (output: TOutput | null) => void;
/**
* Callback fired on error
*/
onError?: (error: string) => void;
/**
* Callback fired for any event
*/
onEvent?: (event: WorkflowMessage) => void;
/**
* Optional transformer to convert accumulated string content to TOutput
* If not provided, attempts automatic string/JSON parsing
*/
outputTransformer?: (accumulatedContent: string) => TOutput;
}
export interface UseWorkflowResult<TInputs = unknown, TOutput = unknown> {
/** Whether the workflow is currently in progress */
inProgress: boolean;
/** Any error that occurred */
error: string | null;
/** The final output (accumulated from stream) */
output: TOutput | null;
/** All workflow message events received */
execution: WorkflowMessage[];
/** Run workflow in streaming mode */
run: (config: WorkflowRunConfig<TInputs>) => Promise<void>;
/** Run workflow in streaming mode */
start: (config: WorkflowRunConfig<TInputs>) => Promise<void>;
/** Stop the current workflow */
stop: () => void;
/** Clear all workflow state */
clear: () => void;
}
/**
* Hook for interacting with GenSX workflows via your API endpoint
*
* @example
* ```tsx
* const workflow = useWorkflow({
* config: {
* baseUrl: '/api/gensx',
* },
* onEvent: (event) => console.log('Event:', event)
* });
*
* // Run the workflow
* await workflow.run({ inputs: { userMessage: 'Hello' } });
*
* // Stream strongly-typed objects
* const currentProgress = useObject<ProgressEvent>(workflow.execution, 'progress');
*
* // Process events as they come in
* const allSteps = useEvents<StepEvent>(workflow.execution, 'step-completed', (step) => {
* console.log('Step completed:', step);
* });
*
* // Clear workflow state
* workflow.clear();
* ```
*/
export declare function useWorkflow<TInputs = unknown, TOutput = unknown, TToolBox extends ToolBox = ToolBox>(options: UseWorkflowConfig<TOutput, TToolBox>): UseWorkflowResult<TInputs, TOutput>;
export declare function useEvents<T extends Record<string, JsonValue>>(events: WorkflowMessage[], label: string, onEvent?: (event: T) => void): T[];
//# sourceMappingURL=use-gensx.d.ts.map