vanilla-agent 1.9.0 → 1.10.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/README.md +246 -0
- package/dist/index.cjs +7 -7
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +277 -1
- package/dist/index.d.ts +277 -1
- package/dist/index.global.js +26 -26
- package/dist/index.global.js.map +1 -1
- package/dist/index.js +7 -7
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/client.ts +141 -12
- package/src/components/forms.ts +1 -0
- package/src/components/registry.ts +87 -0
- package/src/index.ts +22 -2
- package/src/plugins/registry.ts +1 -0
- package/src/plugins/types.ts +1 -0
- package/src/types.ts +150 -0
- package/src/ui.ts +69 -6
- package/src/utils/component-middleware.ts +137 -0
- package/src/utils/component-parser.ts +119 -0
- package/src/utils/constants.ts +1 -0
- package/src/utils/dom.ts +1 -0
- package/src/utils/formatting.ts +33 -8
- package/src/utils/positioning.ts +1 -0
- package/src/utils/theme.ts +1 -0
package/dist/index.d.cts
CHANGED
|
@@ -363,10 +363,58 @@ interface AgentWidgetStreamParser {
|
|
|
363
363
|
*/
|
|
364
364
|
close?(): Promise<void> | void;
|
|
365
365
|
}
|
|
366
|
+
/**
|
|
367
|
+
* Component renderer function signature for custom components
|
|
368
|
+
*/
|
|
369
|
+
type AgentWidgetComponentRenderer = (props: Record<string, unknown>, context: {
|
|
370
|
+
message: AgentWidgetMessage;
|
|
371
|
+
config: AgentWidgetConfig;
|
|
372
|
+
updateProps: (newProps: Record<string, unknown>) => void;
|
|
373
|
+
}) => HTMLElement;
|
|
374
|
+
/**
|
|
375
|
+
* Result from custom SSE event parser
|
|
376
|
+
*/
|
|
377
|
+
type AgentWidgetSSEEventResult = {
|
|
378
|
+
/** Text content to display */
|
|
379
|
+
text?: string;
|
|
380
|
+
/** Whether the stream is complete */
|
|
381
|
+
done?: boolean;
|
|
382
|
+
/** Error message if an error occurred */
|
|
383
|
+
error?: string;
|
|
384
|
+
} | null;
|
|
385
|
+
/**
|
|
386
|
+
* Custom SSE event parser function
|
|
387
|
+
* Allows transforming non-standard SSE event formats to vanilla-agent's expected format
|
|
388
|
+
*/
|
|
389
|
+
type AgentWidgetSSEEventParser = (eventData: unknown) => AgentWidgetSSEEventResult | Promise<AgentWidgetSSEEventResult>;
|
|
390
|
+
/**
|
|
391
|
+
* Custom fetch function for full control over API requests
|
|
392
|
+
* Use this for custom authentication, request transformation, etc.
|
|
393
|
+
*/
|
|
394
|
+
type AgentWidgetCustomFetch = (url: string, init: RequestInit, payload: AgentWidgetRequestPayload) => Promise<Response>;
|
|
395
|
+
/**
|
|
396
|
+
* Dynamic headers function - called before each request
|
|
397
|
+
*/
|
|
398
|
+
type AgentWidgetHeadersFunction = () => Record<string, string> | Promise<Record<string, string>>;
|
|
366
399
|
type AgentWidgetConfig = {
|
|
367
400
|
apiUrl?: string;
|
|
368
401
|
flowId?: string;
|
|
402
|
+
/**
|
|
403
|
+
* Static headers to include with each request.
|
|
404
|
+
* For dynamic headers (e.g., auth tokens), use `getHeaders` instead.
|
|
405
|
+
*/
|
|
369
406
|
headers?: Record<string, string>;
|
|
407
|
+
/**
|
|
408
|
+
* Dynamic headers function - called before each request.
|
|
409
|
+
* Useful for adding auth tokens that may change.
|
|
410
|
+
* @example
|
|
411
|
+
* ```typescript
|
|
412
|
+
* getHeaders: async () => ({
|
|
413
|
+
* 'Authorization': `Bearer ${await getAuthToken()}`
|
|
414
|
+
* })
|
|
415
|
+
* ```
|
|
416
|
+
*/
|
|
417
|
+
getHeaders?: AgentWidgetHeadersFunction;
|
|
370
418
|
copy?: {
|
|
371
419
|
welcomeTitle?: string;
|
|
372
420
|
welcomeSubtitle?: string;
|
|
@@ -398,6 +446,32 @@ type AgentWidgetConfig = {
|
|
|
398
446
|
actionParsers?: AgentWidgetActionParser[];
|
|
399
447
|
actionHandlers?: AgentWidgetActionHandler[];
|
|
400
448
|
storageAdapter?: AgentWidgetStorageAdapter;
|
|
449
|
+
/**
|
|
450
|
+
* Registry of custom components that can be rendered from JSON directives.
|
|
451
|
+
* Components are registered by name and can be invoked via JSON responses
|
|
452
|
+
* with the format: `{"component": "ComponentName", "props": {...}}`
|
|
453
|
+
*
|
|
454
|
+
* @example
|
|
455
|
+
* ```typescript
|
|
456
|
+
* config: {
|
|
457
|
+
* components: {
|
|
458
|
+
* ProductCard: (props, context) => {
|
|
459
|
+
* const card = document.createElement("div");
|
|
460
|
+
* card.innerHTML = `<h3>${props.title}</h3><p>$${props.price}</p>`;
|
|
461
|
+
* return card;
|
|
462
|
+
* }
|
|
463
|
+
* }
|
|
464
|
+
* }
|
|
465
|
+
* ```
|
|
466
|
+
*/
|
|
467
|
+
components?: Record<string, AgentWidgetComponentRenderer>;
|
|
468
|
+
/**
|
|
469
|
+
* Enable component streaming. When true, component props will be updated
|
|
470
|
+
* incrementally as they stream in from the JSON response.
|
|
471
|
+
*
|
|
472
|
+
* @default true
|
|
473
|
+
*/
|
|
474
|
+
enableComponentStreaming?: boolean;
|
|
401
475
|
/**
|
|
402
476
|
* Custom stream parser for extracting text from streaming structured responses.
|
|
403
477
|
* Handles incremental parsing of JSON, XML, or other formats.
|
|
@@ -454,6 +528,68 @@ type AgentWidgetConfig = {
|
|
|
454
528
|
* ```
|
|
455
529
|
*/
|
|
456
530
|
parserType?: "plain" | "json" | "regex-json" | "xml";
|
|
531
|
+
/**
|
|
532
|
+
* Custom fetch function for full control over API requests.
|
|
533
|
+
* Use this for custom authentication, request/response transformation, etc.
|
|
534
|
+
*
|
|
535
|
+
* When provided, this function is called instead of the default fetch.
|
|
536
|
+
* You receive the URL, RequestInit, and the payload that would be sent.
|
|
537
|
+
*
|
|
538
|
+
* @example
|
|
539
|
+
* ```typescript
|
|
540
|
+
* config: {
|
|
541
|
+
* customFetch: async (url, init, payload) => {
|
|
542
|
+
* // Transform request for your API format
|
|
543
|
+
* const myPayload = {
|
|
544
|
+
* flow: { id: 'my-flow-id' },
|
|
545
|
+
* messages: payload.messages,
|
|
546
|
+
* options: { stream_response: true }
|
|
547
|
+
* };
|
|
548
|
+
*
|
|
549
|
+
* // Add auth header
|
|
550
|
+
* const token = await getAuthToken();
|
|
551
|
+
*
|
|
552
|
+
* return fetch('/my-api/dispatch', {
|
|
553
|
+
* method: 'POST',
|
|
554
|
+
* headers: {
|
|
555
|
+
* 'Content-Type': 'application/json',
|
|
556
|
+
* 'Authorization': `Bearer ${token}`
|
|
557
|
+
* },
|
|
558
|
+
* body: JSON.stringify(myPayload),
|
|
559
|
+
* signal: init.signal
|
|
560
|
+
* });
|
|
561
|
+
* }
|
|
562
|
+
* }
|
|
563
|
+
* ```
|
|
564
|
+
*/
|
|
565
|
+
customFetch?: AgentWidgetCustomFetch;
|
|
566
|
+
/**
|
|
567
|
+
* Custom SSE event parser for non-standard streaming response formats.
|
|
568
|
+
*
|
|
569
|
+
* Use this when your API returns SSE events in a different format than expected.
|
|
570
|
+
* Return `{ text }` for text chunks, `{ done: true }` for completion,
|
|
571
|
+
* `{ error }` for errors, or `null` to ignore the event.
|
|
572
|
+
*
|
|
573
|
+
* @example
|
|
574
|
+
* ```typescript
|
|
575
|
+
* // For Travrse API format
|
|
576
|
+
* config: {
|
|
577
|
+
* parseSSEEvent: (data) => {
|
|
578
|
+
* if (data.type === 'step_chunk' && data.chunk) {
|
|
579
|
+
* return { text: data.chunk };
|
|
580
|
+
* }
|
|
581
|
+
* if (data.type === 'flow_complete') {
|
|
582
|
+
* return { done: true };
|
|
583
|
+
* }
|
|
584
|
+
* if (data.type === 'step_error') {
|
|
585
|
+
* return { error: data.error };
|
|
586
|
+
* }
|
|
587
|
+
* return null; // Ignore other events
|
|
588
|
+
* }
|
|
589
|
+
* }
|
|
590
|
+
* ```
|
|
591
|
+
*/
|
|
592
|
+
parseSSEEvent?: AgentWidgetSSEEventParser;
|
|
457
593
|
};
|
|
458
594
|
type AgentWidgetMessageRole = "user" | "assistant" | "system";
|
|
459
595
|
type AgentWidgetReasoning = {
|
|
@@ -613,9 +749,17 @@ declare class AgentWidgetClient {
|
|
|
613
749
|
private readonly createStreamParser;
|
|
614
750
|
private readonly contextProviders;
|
|
615
751
|
private readonly requestMiddleware?;
|
|
752
|
+
private readonly customFetch?;
|
|
753
|
+
private readonly parseSSEEvent?;
|
|
754
|
+
private readonly getHeaders?;
|
|
616
755
|
constructor(config?: AgentWidgetConfig);
|
|
617
756
|
dispatch(options: DispatchOptions, onEvent: SSEHandler): Promise<void>;
|
|
618
757
|
private buildPayload;
|
|
758
|
+
/**
|
|
759
|
+
* Handle custom SSE event parsing via parseSSEEvent callback
|
|
760
|
+
* Returns true if event was handled, false otherwise
|
|
761
|
+
*/
|
|
762
|
+
private handleCustomSSEEvent;
|
|
619
763
|
private streamResponse;
|
|
620
764
|
}
|
|
621
765
|
|
|
@@ -724,6 +868,138 @@ declare class PluginRegistry {
|
|
|
724
868
|
}
|
|
725
869
|
declare const pluginRegistry: PluginRegistry;
|
|
726
870
|
|
|
871
|
+
/**
|
|
872
|
+
* Context provided to component renderers
|
|
873
|
+
*/
|
|
874
|
+
interface ComponentContext {
|
|
875
|
+
message: AgentWidgetMessage;
|
|
876
|
+
config: AgentWidgetConfig;
|
|
877
|
+
/**
|
|
878
|
+
* Update component props during streaming
|
|
879
|
+
*/
|
|
880
|
+
updateProps: (newProps: Record<string, unknown>) => void;
|
|
881
|
+
}
|
|
882
|
+
/**
|
|
883
|
+
* Component renderer function signature
|
|
884
|
+
*/
|
|
885
|
+
type ComponentRenderer = (props: Record<string, unknown>, context: ComponentContext) => HTMLElement;
|
|
886
|
+
/**
|
|
887
|
+
* Component registry for managing custom components
|
|
888
|
+
*/
|
|
889
|
+
declare class ComponentRegistry {
|
|
890
|
+
private components;
|
|
891
|
+
/**
|
|
892
|
+
* Register a custom component
|
|
893
|
+
*/
|
|
894
|
+
register(name: string, renderer: ComponentRenderer): void;
|
|
895
|
+
/**
|
|
896
|
+
* Unregister a component
|
|
897
|
+
*/
|
|
898
|
+
unregister(name: string): void;
|
|
899
|
+
/**
|
|
900
|
+
* Get a component renderer by name
|
|
901
|
+
*/
|
|
902
|
+
get(name: string): ComponentRenderer | undefined;
|
|
903
|
+
/**
|
|
904
|
+
* Check if a component is registered
|
|
905
|
+
*/
|
|
906
|
+
has(name: string): boolean;
|
|
907
|
+
/**
|
|
908
|
+
* Get all registered component names
|
|
909
|
+
*/
|
|
910
|
+
getAllNames(): string[];
|
|
911
|
+
/**
|
|
912
|
+
* Clear all registered components
|
|
913
|
+
*/
|
|
914
|
+
clear(): void;
|
|
915
|
+
/**
|
|
916
|
+
* Register multiple components at once
|
|
917
|
+
*/
|
|
918
|
+
registerAll(components: Record<string, ComponentRenderer>): void;
|
|
919
|
+
}
|
|
920
|
+
/**
|
|
921
|
+
* Global component registry instance
|
|
922
|
+
*/
|
|
923
|
+
declare const componentRegistry: ComponentRegistry;
|
|
924
|
+
|
|
925
|
+
/**
|
|
926
|
+
* Represents a component directive extracted from JSON
|
|
927
|
+
*/
|
|
928
|
+
interface ComponentDirective {
|
|
929
|
+
component: string;
|
|
930
|
+
props: Record<string, unknown>;
|
|
931
|
+
raw: string;
|
|
932
|
+
}
|
|
933
|
+
/**
|
|
934
|
+
* Creates a parser that extracts component directives from JSON streams
|
|
935
|
+
* This parser looks for objects with a "component" field and extracts
|
|
936
|
+
* the component name and props incrementally as they stream in.
|
|
937
|
+
*/
|
|
938
|
+
declare function createComponentStreamParser(): {
|
|
939
|
+
/**
|
|
940
|
+
* Get the currently extracted component directive
|
|
941
|
+
*/
|
|
942
|
+
getExtractedDirective: () => ComponentDirective | null;
|
|
943
|
+
/**
|
|
944
|
+
* Process a chunk of JSON and extract component directive if present
|
|
945
|
+
*/
|
|
946
|
+
processChunk: (accumulatedContent: string) => ComponentDirective | null;
|
|
947
|
+
/**
|
|
948
|
+
* Reset the parser state
|
|
949
|
+
*/
|
|
950
|
+
reset: () => void;
|
|
951
|
+
};
|
|
952
|
+
/**
|
|
953
|
+
* Type guard to check if an object is a component directive
|
|
954
|
+
*/
|
|
955
|
+
declare function isComponentDirectiveType(obj: unknown): obj is ComponentDirective;
|
|
956
|
+
|
|
957
|
+
type MessageTransform = (context: {
|
|
958
|
+
text: string;
|
|
959
|
+
message: AgentWidgetMessage;
|
|
960
|
+
streaming: boolean;
|
|
961
|
+
raw?: string;
|
|
962
|
+
}) => string;
|
|
963
|
+
|
|
964
|
+
/**
|
|
965
|
+
* Options for component middleware
|
|
966
|
+
*/
|
|
967
|
+
interface ComponentMiddlewareOptions {
|
|
968
|
+
config: AgentWidgetConfig;
|
|
969
|
+
message: AgentWidgetMessage;
|
|
970
|
+
transform: MessageTransform;
|
|
971
|
+
onPropsUpdate?: (props: Record<string, unknown>) => void;
|
|
972
|
+
}
|
|
973
|
+
/**
|
|
974
|
+
* Renders a component directive into an HTMLElement
|
|
975
|
+
*/
|
|
976
|
+
declare function renderComponentDirective(directive: ComponentDirective, options: ComponentMiddlewareOptions): HTMLElement | null;
|
|
977
|
+
/**
|
|
978
|
+
* Creates middleware that processes component directives from streamed JSON
|
|
979
|
+
*/
|
|
980
|
+
declare function createComponentMiddleware(): {
|
|
981
|
+
/**
|
|
982
|
+
* Process accumulated content and extract component directive
|
|
983
|
+
*/
|
|
984
|
+
processChunk: (accumulatedContent: string) => ComponentDirective | null;
|
|
985
|
+
/**
|
|
986
|
+
* Get the currently extracted directive
|
|
987
|
+
*/
|
|
988
|
+
getDirective: () => ComponentDirective | null;
|
|
989
|
+
/**
|
|
990
|
+
* Reset the parser state
|
|
991
|
+
*/
|
|
992
|
+
reset: () => void;
|
|
993
|
+
};
|
|
994
|
+
/**
|
|
995
|
+
* Checks if a message contains a component directive in its raw content
|
|
996
|
+
*/
|
|
997
|
+
declare function hasComponentDirective(message: AgentWidgetMessage): boolean;
|
|
998
|
+
/**
|
|
999
|
+
* Extracts component directive from a complete message
|
|
1000
|
+
*/
|
|
1001
|
+
declare function extractComponentDirectiveFromMessage(message: AgentWidgetMessage): ComponentDirective | null;
|
|
1002
|
+
|
|
727
1003
|
/**
|
|
728
1004
|
* Default widget configuration
|
|
729
1005
|
* Single source of truth for all default values
|
|
@@ -735,4 +1011,4 @@ declare const DEFAULT_WIDGET_CONFIG: Partial<AgentWidgetConfig>;
|
|
|
735
1011
|
*/
|
|
736
1012
|
declare function mergeWithDefaults(config?: Partial<AgentWidgetConfig>): Partial<AgentWidgetConfig>;
|
|
737
1013
|
|
|
738
|
-
export { AgentWidgetClient, type AgentWidgetConfig, type AgentWidgetController, type AgentWidgetEvent, type AgentWidgetFeatureFlags, type AgentWidgetInitHandle, type AgentWidgetInitOptions, type AgentWidgetLauncherConfig, type AgentWidgetMessage, type AgentWidgetPlugin, AgentWidgetSession, type AgentWidgetSessionStatus, type AgentWidgetStreamParser, type AgentWidgetStreamParserResult, type AgentWidgetTheme, DEFAULT_WIDGET_CONFIG, createActionManager, createAgentExperience, createFlexibleJsonStreamParser, createJsonStreamParser, createLocalStorageAdapter, createPlainTextParser, createRegexJsonParser, createXmlParser, initAgentWidget as default, defaultActionHandlers, defaultJsonActionParser, directivePostprocessor, escapeHtml, initAgentWidget, markdownPostprocessor, mergeWithDefaults, pluginRegistry };
|
|
1014
|
+
export { AgentWidgetClient, type AgentWidgetConfig, type AgentWidgetController, type AgentWidgetCustomFetch, type AgentWidgetEvent, type AgentWidgetFeatureFlags, type AgentWidgetHeadersFunction, type AgentWidgetInitHandle, type AgentWidgetInitOptions, type AgentWidgetLauncherConfig, type AgentWidgetMessage, type AgentWidgetPlugin, type AgentWidgetRequestPayload, type AgentWidgetSSEEventParser, type AgentWidgetSSEEventResult, AgentWidgetSession, type AgentWidgetSessionStatus, type AgentWidgetStreamParser, type AgentWidgetStreamParserResult, type AgentWidgetTheme, type ComponentContext, type ComponentDirective, type ComponentRenderer, DEFAULT_WIDGET_CONFIG, componentRegistry, createActionManager, createAgentExperience, createComponentMiddleware, createComponentStreamParser, createFlexibleJsonStreamParser, createJsonStreamParser, createLocalStorageAdapter, createPlainTextParser, createRegexJsonParser, createXmlParser, initAgentWidget as default, defaultActionHandlers, defaultJsonActionParser, directivePostprocessor, escapeHtml, extractComponentDirectiveFromMessage, hasComponentDirective, initAgentWidget, isComponentDirectiveType, markdownPostprocessor, mergeWithDefaults, pluginRegistry, renderComponentDirective };
|
package/dist/index.d.ts
CHANGED
|
@@ -363,10 +363,58 @@ interface AgentWidgetStreamParser {
|
|
|
363
363
|
*/
|
|
364
364
|
close?(): Promise<void> | void;
|
|
365
365
|
}
|
|
366
|
+
/**
|
|
367
|
+
* Component renderer function signature for custom components
|
|
368
|
+
*/
|
|
369
|
+
type AgentWidgetComponentRenderer = (props: Record<string, unknown>, context: {
|
|
370
|
+
message: AgentWidgetMessage;
|
|
371
|
+
config: AgentWidgetConfig;
|
|
372
|
+
updateProps: (newProps: Record<string, unknown>) => void;
|
|
373
|
+
}) => HTMLElement;
|
|
374
|
+
/**
|
|
375
|
+
* Result from custom SSE event parser
|
|
376
|
+
*/
|
|
377
|
+
type AgentWidgetSSEEventResult = {
|
|
378
|
+
/** Text content to display */
|
|
379
|
+
text?: string;
|
|
380
|
+
/** Whether the stream is complete */
|
|
381
|
+
done?: boolean;
|
|
382
|
+
/** Error message if an error occurred */
|
|
383
|
+
error?: string;
|
|
384
|
+
} | null;
|
|
385
|
+
/**
|
|
386
|
+
* Custom SSE event parser function
|
|
387
|
+
* Allows transforming non-standard SSE event formats to vanilla-agent's expected format
|
|
388
|
+
*/
|
|
389
|
+
type AgentWidgetSSEEventParser = (eventData: unknown) => AgentWidgetSSEEventResult | Promise<AgentWidgetSSEEventResult>;
|
|
390
|
+
/**
|
|
391
|
+
* Custom fetch function for full control over API requests
|
|
392
|
+
* Use this for custom authentication, request transformation, etc.
|
|
393
|
+
*/
|
|
394
|
+
type AgentWidgetCustomFetch = (url: string, init: RequestInit, payload: AgentWidgetRequestPayload) => Promise<Response>;
|
|
395
|
+
/**
|
|
396
|
+
* Dynamic headers function - called before each request
|
|
397
|
+
*/
|
|
398
|
+
type AgentWidgetHeadersFunction = () => Record<string, string> | Promise<Record<string, string>>;
|
|
366
399
|
type AgentWidgetConfig = {
|
|
367
400
|
apiUrl?: string;
|
|
368
401
|
flowId?: string;
|
|
402
|
+
/**
|
|
403
|
+
* Static headers to include with each request.
|
|
404
|
+
* For dynamic headers (e.g., auth tokens), use `getHeaders` instead.
|
|
405
|
+
*/
|
|
369
406
|
headers?: Record<string, string>;
|
|
407
|
+
/**
|
|
408
|
+
* Dynamic headers function - called before each request.
|
|
409
|
+
* Useful for adding auth tokens that may change.
|
|
410
|
+
* @example
|
|
411
|
+
* ```typescript
|
|
412
|
+
* getHeaders: async () => ({
|
|
413
|
+
* 'Authorization': `Bearer ${await getAuthToken()}`
|
|
414
|
+
* })
|
|
415
|
+
* ```
|
|
416
|
+
*/
|
|
417
|
+
getHeaders?: AgentWidgetHeadersFunction;
|
|
370
418
|
copy?: {
|
|
371
419
|
welcomeTitle?: string;
|
|
372
420
|
welcomeSubtitle?: string;
|
|
@@ -398,6 +446,32 @@ type AgentWidgetConfig = {
|
|
|
398
446
|
actionParsers?: AgentWidgetActionParser[];
|
|
399
447
|
actionHandlers?: AgentWidgetActionHandler[];
|
|
400
448
|
storageAdapter?: AgentWidgetStorageAdapter;
|
|
449
|
+
/**
|
|
450
|
+
* Registry of custom components that can be rendered from JSON directives.
|
|
451
|
+
* Components are registered by name and can be invoked via JSON responses
|
|
452
|
+
* with the format: `{"component": "ComponentName", "props": {...}}`
|
|
453
|
+
*
|
|
454
|
+
* @example
|
|
455
|
+
* ```typescript
|
|
456
|
+
* config: {
|
|
457
|
+
* components: {
|
|
458
|
+
* ProductCard: (props, context) => {
|
|
459
|
+
* const card = document.createElement("div");
|
|
460
|
+
* card.innerHTML = `<h3>${props.title}</h3><p>$${props.price}</p>`;
|
|
461
|
+
* return card;
|
|
462
|
+
* }
|
|
463
|
+
* }
|
|
464
|
+
* }
|
|
465
|
+
* ```
|
|
466
|
+
*/
|
|
467
|
+
components?: Record<string, AgentWidgetComponentRenderer>;
|
|
468
|
+
/**
|
|
469
|
+
* Enable component streaming. When true, component props will be updated
|
|
470
|
+
* incrementally as they stream in from the JSON response.
|
|
471
|
+
*
|
|
472
|
+
* @default true
|
|
473
|
+
*/
|
|
474
|
+
enableComponentStreaming?: boolean;
|
|
401
475
|
/**
|
|
402
476
|
* Custom stream parser for extracting text from streaming structured responses.
|
|
403
477
|
* Handles incremental parsing of JSON, XML, or other formats.
|
|
@@ -454,6 +528,68 @@ type AgentWidgetConfig = {
|
|
|
454
528
|
* ```
|
|
455
529
|
*/
|
|
456
530
|
parserType?: "plain" | "json" | "regex-json" | "xml";
|
|
531
|
+
/**
|
|
532
|
+
* Custom fetch function for full control over API requests.
|
|
533
|
+
* Use this for custom authentication, request/response transformation, etc.
|
|
534
|
+
*
|
|
535
|
+
* When provided, this function is called instead of the default fetch.
|
|
536
|
+
* You receive the URL, RequestInit, and the payload that would be sent.
|
|
537
|
+
*
|
|
538
|
+
* @example
|
|
539
|
+
* ```typescript
|
|
540
|
+
* config: {
|
|
541
|
+
* customFetch: async (url, init, payload) => {
|
|
542
|
+
* // Transform request for your API format
|
|
543
|
+
* const myPayload = {
|
|
544
|
+
* flow: { id: 'my-flow-id' },
|
|
545
|
+
* messages: payload.messages,
|
|
546
|
+
* options: { stream_response: true }
|
|
547
|
+
* };
|
|
548
|
+
*
|
|
549
|
+
* // Add auth header
|
|
550
|
+
* const token = await getAuthToken();
|
|
551
|
+
*
|
|
552
|
+
* return fetch('/my-api/dispatch', {
|
|
553
|
+
* method: 'POST',
|
|
554
|
+
* headers: {
|
|
555
|
+
* 'Content-Type': 'application/json',
|
|
556
|
+
* 'Authorization': `Bearer ${token}`
|
|
557
|
+
* },
|
|
558
|
+
* body: JSON.stringify(myPayload),
|
|
559
|
+
* signal: init.signal
|
|
560
|
+
* });
|
|
561
|
+
* }
|
|
562
|
+
* }
|
|
563
|
+
* ```
|
|
564
|
+
*/
|
|
565
|
+
customFetch?: AgentWidgetCustomFetch;
|
|
566
|
+
/**
|
|
567
|
+
* Custom SSE event parser for non-standard streaming response formats.
|
|
568
|
+
*
|
|
569
|
+
* Use this when your API returns SSE events in a different format than expected.
|
|
570
|
+
* Return `{ text }` for text chunks, `{ done: true }` for completion,
|
|
571
|
+
* `{ error }` for errors, or `null` to ignore the event.
|
|
572
|
+
*
|
|
573
|
+
* @example
|
|
574
|
+
* ```typescript
|
|
575
|
+
* // For Travrse API format
|
|
576
|
+
* config: {
|
|
577
|
+
* parseSSEEvent: (data) => {
|
|
578
|
+
* if (data.type === 'step_chunk' && data.chunk) {
|
|
579
|
+
* return { text: data.chunk };
|
|
580
|
+
* }
|
|
581
|
+
* if (data.type === 'flow_complete') {
|
|
582
|
+
* return { done: true };
|
|
583
|
+
* }
|
|
584
|
+
* if (data.type === 'step_error') {
|
|
585
|
+
* return { error: data.error };
|
|
586
|
+
* }
|
|
587
|
+
* return null; // Ignore other events
|
|
588
|
+
* }
|
|
589
|
+
* }
|
|
590
|
+
* ```
|
|
591
|
+
*/
|
|
592
|
+
parseSSEEvent?: AgentWidgetSSEEventParser;
|
|
457
593
|
};
|
|
458
594
|
type AgentWidgetMessageRole = "user" | "assistant" | "system";
|
|
459
595
|
type AgentWidgetReasoning = {
|
|
@@ -613,9 +749,17 @@ declare class AgentWidgetClient {
|
|
|
613
749
|
private readonly createStreamParser;
|
|
614
750
|
private readonly contextProviders;
|
|
615
751
|
private readonly requestMiddleware?;
|
|
752
|
+
private readonly customFetch?;
|
|
753
|
+
private readonly parseSSEEvent?;
|
|
754
|
+
private readonly getHeaders?;
|
|
616
755
|
constructor(config?: AgentWidgetConfig);
|
|
617
756
|
dispatch(options: DispatchOptions, onEvent: SSEHandler): Promise<void>;
|
|
618
757
|
private buildPayload;
|
|
758
|
+
/**
|
|
759
|
+
* Handle custom SSE event parsing via parseSSEEvent callback
|
|
760
|
+
* Returns true if event was handled, false otherwise
|
|
761
|
+
*/
|
|
762
|
+
private handleCustomSSEEvent;
|
|
619
763
|
private streamResponse;
|
|
620
764
|
}
|
|
621
765
|
|
|
@@ -724,6 +868,138 @@ declare class PluginRegistry {
|
|
|
724
868
|
}
|
|
725
869
|
declare const pluginRegistry: PluginRegistry;
|
|
726
870
|
|
|
871
|
+
/**
|
|
872
|
+
* Context provided to component renderers
|
|
873
|
+
*/
|
|
874
|
+
interface ComponentContext {
|
|
875
|
+
message: AgentWidgetMessage;
|
|
876
|
+
config: AgentWidgetConfig;
|
|
877
|
+
/**
|
|
878
|
+
* Update component props during streaming
|
|
879
|
+
*/
|
|
880
|
+
updateProps: (newProps: Record<string, unknown>) => void;
|
|
881
|
+
}
|
|
882
|
+
/**
|
|
883
|
+
* Component renderer function signature
|
|
884
|
+
*/
|
|
885
|
+
type ComponentRenderer = (props: Record<string, unknown>, context: ComponentContext) => HTMLElement;
|
|
886
|
+
/**
|
|
887
|
+
* Component registry for managing custom components
|
|
888
|
+
*/
|
|
889
|
+
declare class ComponentRegistry {
|
|
890
|
+
private components;
|
|
891
|
+
/**
|
|
892
|
+
* Register a custom component
|
|
893
|
+
*/
|
|
894
|
+
register(name: string, renderer: ComponentRenderer): void;
|
|
895
|
+
/**
|
|
896
|
+
* Unregister a component
|
|
897
|
+
*/
|
|
898
|
+
unregister(name: string): void;
|
|
899
|
+
/**
|
|
900
|
+
* Get a component renderer by name
|
|
901
|
+
*/
|
|
902
|
+
get(name: string): ComponentRenderer | undefined;
|
|
903
|
+
/**
|
|
904
|
+
* Check if a component is registered
|
|
905
|
+
*/
|
|
906
|
+
has(name: string): boolean;
|
|
907
|
+
/**
|
|
908
|
+
* Get all registered component names
|
|
909
|
+
*/
|
|
910
|
+
getAllNames(): string[];
|
|
911
|
+
/**
|
|
912
|
+
* Clear all registered components
|
|
913
|
+
*/
|
|
914
|
+
clear(): void;
|
|
915
|
+
/**
|
|
916
|
+
* Register multiple components at once
|
|
917
|
+
*/
|
|
918
|
+
registerAll(components: Record<string, ComponentRenderer>): void;
|
|
919
|
+
}
|
|
920
|
+
/**
|
|
921
|
+
* Global component registry instance
|
|
922
|
+
*/
|
|
923
|
+
declare const componentRegistry: ComponentRegistry;
|
|
924
|
+
|
|
925
|
+
/**
|
|
926
|
+
* Represents a component directive extracted from JSON
|
|
927
|
+
*/
|
|
928
|
+
interface ComponentDirective {
|
|
929
|
+
component: string;
|
|
930
|
+
props: Record<string, unknown>;
|
|
931
|
+
raw: string;
|
|
932
|
+
}
|
|
933
|
+
/**
|
|
934
|
+
* Creates a parser that extracts component directives from JSON streams
|
|
935
|
+
* This parser looks for objects with a "component" field and extracts
|
|
936
|
+
* the component name and props incrementally as they stream in.
|
|
937
|
+
*/
|
|
938
|
+
declare function createComponentStreamParser(): {
|
|
939
|
+
/**
|
|
940
|
+
* Get the currently extracted component directive
|
|
941
|
+
*/
|
|
942
|
+
getExtractedDirective: () => ComponentDirective | null;
|
|
943
|
+
/**
|
|
944
|
+
* Process a chunk of JSON and extract component directive if present
|
|
945
|
+
*/
|
|
946
|
+
processChunk: (accumulatedContent: string) => ComponentDirective | null;
|
|
947
|
+
/**
|
|
948
|
+
* Reset the parser state
|
|
949
|
+
*/
|
|
950
|
+
reset: () => void;
|
|
951
|
+
};
|
|
952
|
+
/**
|
|
953
|
+
* Type guard to check if an object is a component directive
|
|
954
|
+
*/
|
|
955
|
+
declare function isComponentDirectiveType(obj: unknown): obj is ComponentDirective;
|
|
956
|
+
|
|
957
|
+
type MessageTransform = (context: {
|
|
958
|
+
text: string;
|
|
959
|
+
message: AgentWidgetMessage;
|
|
960
|
+
streaming: boolean;
|
|
961
|
+
raw?: string;
|
|
962
|
+
}) => string;
|
|
963
|
+
|
|
964
|
+
/**
|
|
965
|
+
* Options for component middleware
|
|
966
|
+
*/
|
|
967
|
+
interface ComponentMiddlewareOptions {
|
|
968
|
+
config: AgentWidgetConfig;
|
|
969
|
+
message: AgentWidgetMessage;
|
|
970
|
+
transform: MessageTransform;
|
|
971
|
+
onPropsUpdate?: (props: Record<string, unknown>) => void;
|
|
972
|
+
}
|
|
973
|
+
/**
|
|
974
|
+
* Renders a component directive into an HTMLElement
|
|
975
|
+
*/
|
|
976
|
+
declare function renderComponentDirective(directive: ComponentDirective, options: ComponentMiddlewareOptions): HTMLElement | null;
|
|
977
|
+
/**
|
|
978
|
+
* Creates middleware that processes component directives from streamed JSON
|
|
979
|
+
*/
|
|
980
|
+
declare function createComponentMiddleware(): {
|
|
981
|
+
/**
|
|
982
|
+
* Process accumulated content and extract component directive
|
|
983
|
+
*/
|
|
984
|
+
processChunk: (accumulatedContent: string) => ComponentDirective | null;
|
|
985
|
+
/**
|
|
986
|
+
* Get the currently extracted directive
|
|
987
|
+
*/
|
|
988
|
+
getDirective: () => ComponentDirective | null;
|
|
989
|
+
/**
|
|
990
|
+
* Reset the parser state
|
|
991
|
+
*/
|
|
992
|
+
reset: () => void;
|
|
993
|
+
};
|
|
994
|
+
/**
|
|
995
|
+
* Checks if a message contains a component directive in its raw content
|
|
996
|
+
*/
|
|
997
|
+
declare function hasComponentDirective(message: AgentWidgetMessage): boolean;
|
|
998
|
+
/**
|
|
999
|
+
* Extracts component directive from a complete message
|
|
1000
|
+
*/
|
|
1001
|
+
declare function extractComponentDirectiveFromMessage(message: AgentWidgetMessage): ComponentDirective | null;
|
|
1002
|
+
|
|
727
1003
|
/**
|
|
728
1004
|
* Default widget configuration
|
|
729
1005
|
* Single source of truth for all default values
|
|
@@ -735,4 +1011,4 @@ declare const DEFAULT_WIDGET_CONFIG: Partial<AgentWidgetConfig>;
|
|
|
735
1011
|
*/
|
|
736
1012
|
declare function mergeWithDefaults(config?: Partial<AgentWidgetConfig>): Partial<AgentWidgetConfig>;
|
|
737
1013
|
|
|
738
|
-
export { AgentWidgetClient, type AgentWidgetConfig, type AgentWidgetController, type AgentWidgetEvent, type AgentWidgetFeatureFlags, type AgentWidgetInitHandle, type AgentWidgetInitOptions, type AgentWidgetLauncherConfig, type AgentWidgetMessage, type AgentWidgetPlugin, AgentWidgetSession, type AgentWidgetSessionStatus, type AgentWidgetStreamParser, type AgentWidgetStreamParserResult, type AgentWidgetTheme, DEFAULT_WIDGET_CONFIG, createActionManager, createAgentExperience, createFlexibleJsonStreamParser, createJsonStreamParser, createLocalStorageAdapter, createPlainTextParser, createRegexJsonParser, createXmlParser, initAgentWidget as default, defaultActionHandlers, defaultJsonActionParser, directivePostprocessor, escapeHtml, initAgentWidget, markdownPostprocessor, mergeWithDefaults, pluginRegistry };
|
|
1014
|
+
export { AgentWidgetClient, type AgentWidgetConfig, type AgentWidgetController, type AgentWidgetCustomFetch, type AgentWidgetEvent, type AgentWidgetFeatureFlags, type AgentWidgetHeadersFunction, type AgentWidgetInitHandle, type AgentWidgetInitOptions, type AgentWidgetLauncherConfig, type AgentWidgetMessage, type AgentWidgetPlugin, type AgentWidgetRequestPayload, type AgentWidgetSSEEventParser, type AgentWidgetSSEEventResult, AgentWidgetSession, type AgentWidgetSessionStatus, type AgentWidgetStreamParser, type AgentWidgetStreamParserResult, type AgentWidgetTheme, type ComponentContext, type ComponentDirective, type ComponentRenderer, DEFAULT_WIDGET_CONFIG, componentRegistry, createActionManager, createAgentExperience, createComponentMiddleware, createComponentStreamParser, createFlexibleJsonStreamParser, createJsonStreamParser, createLocalStorageAdapter, createPlainTextParser, createRegexJsonParser, createXmlParser, initAgentWidget as default, defaultActionHandlers, defaultJsonActionParser, directivePostprocessor, escapeHtml, extractComponentDirectiveFromMessage, hasComponentDirective, initAgentWidget, isComponentDirectiveType, markdownPostprocessor, mergeWithDefaults, pluginRegistry, renderComponentDirective };
|