pika-shared 1.4.6 → 1.4.8

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.
@@ -1,5 +1,5 @@
1
1
  import { UploadStatus } from '../upload-types.mjs';
2
- import { ShowToastFn, ChatUser, RecordOrUndef, UserAwsCredentials, WidgetRenderingContextType, ShareSessionState, UserPrefs, ChatAppMode, IUserWidgetDataStoreState, CustomDataUiRepresentation, ChatAppOverridableFeatures, TagDefinition, TagDefinitionWidget, ChatSession, UserDataOverrideSettings, ChatMessageForRendering, ChatApp, ChatAppActionMenu, ChatAppAction, WidgetInstance, WidgetMetadata, SpotlightWidgetDefinition, InvokeAgentAsComponentOptions, WidgetAction } from './chatbot-types.mjs';
2
+ import { ShowToastFn, ChatUser, RecordOrUndef, UserAwsCredentials, WidgetRenderingContextType, ShareSessionState, UserPrefs, ChatAppMode, IUserWidgetDataStoreState, CustomDataUiRepresentation, ChatAppOverridableFeatures, TagDefinition, TagDefinitionWidget, ChatSession, UserDataOverrideSettings, ChatMessageForRendering, ChatApp, ChatAppActionMenu, ChatAppAction, WidgetInstance, WidgetSizing, InvokeAgentAsComponentOptions } from './chatbot-types.mjs';
3
3
  import '@aws-sdk/client-bedrock-agent-runtime';
4
4
  import '@aws-sdk/client-bedrock-agentcore';
5
5
 
@@ -177,7 +177,11 @@ interface IChatAppState {
177
177
  readonly customDataForChatApp: Record<string, unknown> | undefined;
178
178
  readonly customTitleBarActions: (ChatAppActionMenu | ChatAppAction)[];
179
179
  readonly widgetInstances: Map<string, WidgetInstance>;
180
- readonly user: ChatUser;
180
+ /**
181
+ * Current user information
182
+ * @since 0.11.0
183
+ */
184
+ readonly user: ChatUser<RecordOrUndef>;
181
185
  setCurrentSessionById(sessionId: string): void;
182
186
  removeFile(s3Key: string): void;
183
187
  startNewChatSession(): void;
@@ -188,12 +192,22 @@ interface IChatAppState {
188
192
  getMessageByMessageId(messageId: string): ChatMessageForRendering | undefined;
189
193
  uploadFiles(files: File[]): Promise<void>;
190
194
  initializeData(): Promise<void>;
195
+ /**
196
+ * Render a tag in a specific context
197
+ * @param metadata - Optional metadata for the widget (title, actions, icon)
198
+ * @since 0.11.0 - Added metadata parameter
199
+ */
191
200
  renderTag(tagId: string, context: 'spotlight' | 'inline' | 'dialog' | 'canvas' | 'static', data?: Record<string, any>, metadata?: WidgetMetadata): Promise<void>;
192
201
  closeCanvas(): void;
193
202
  closeDialog(): void;
194
203
  setOrUpdateCustomTitleBarAction(action: ChatAppActionMenu | ChatAppAction): void;
195
204
  removeCustomTitleBarAction(actionId: string): void;
196
205
  getWidgetInstance(instanceId: string): WidgetInstance | undefined;
206
+ /**
207
+ * Get the full context for a widget instance
208
+ * @since 0.11.0
209
+ */
210
+ getWidgetContext(instanceId: string): PikaWCContext | undefined;
197
211
  /**
198
212
  * Update context for a widget. Call this when your widget's context has changed.
199
213
  *
@@ -272,6 +286,7 @@ interface IChatAppState {
272
286
  * @param dataKey - The key to store data under (default: 'data')
273
287
  * @param metadata - Optional widget metadata (title, actions, icon, etc.)
274
288
  * @returns The instance ID (UUID)
289
+ * @since 0.11.0 - Added metadata parameter
275
290
  *
276
291
  * @example
277
292
  * ```typescript
@@ -446,14 +461,52 @@ interface IUploadInstance {
446
461
  * Called after the element is created but before it's added to the DOM.
447
462
  */
448
463
  interface OnReadyCallback {
449
- (params: {
450
- /** The web component element that was created */
451
- element: HTMLElement;
452
- /** The unique instance ID assigned to this component */
453
- instanceId: string;
454
- /** The full Pika context with instanceId */
455
- context: PikaWCContext;
456
- }): void;
464
+ (params: WidgetCallbackContext): void;
465
+ }
466
+ /**
467
+ * Action button that appears in the widget's chrome (title bar, toolbar, etc.)
468
+ *
469
+ * @example
470
+ * ```js
471
+ * const action: WidgetAction = {
472
+ * id: 'refresh',
473
+ * title: 'Refresh data',
474
+ * iconSvg: '<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20">...</svg>',
475
+ * callback: async () => { await fetchData(); }
476
+ * };
477
+ * ```
478
+ *
479
+ * @since 0.11.0 - Moved from chatbot-types to webcomp-types
480
+ */
481
+ interface WidgetAction {
482
+ /** Unique identifier for this action */
483
+ id: string;
484
+ /** Tooltip/label for the action (also button text in dialog context) */
485
+ title: string;
486
+ /** SVG markup string for the icon (e.g., from extractIconSvg() helper) */
487
+ iconSvg: string;
488
+ /** Whether action is currently disabled */
489
+ disabled?: boolean;
490
+ /** If true, renders as default/prominent button (used in dialog context) */
491
+ primary?: boolean;
492
+ /**
493
+ * Handler when clicked
494
+ * @since 0.11.0 - Callback now receives WidgetCallbackContext parameter
495
+ */
496
+ callback: (context: WidgetCallbackContext) => void | Promise<void>;
497
+ }
498
+ /**
499
+ * The context object that is passed to the onReady callback and in action callbacks functions.
500
+ *
501
+ * @since 0.11.0
502
+ */
503
+ interface WidgetCallbackContext {
504
+ /** The web component element that was created */
505
+ element: HTMLElement;
506
+ /** The unique instance ID assigned to this component */
507
+ instanceId: string;
508
+ /** The full Pika context with instanceId */
509
+ context: PikaWCContext;
457
510
  }
458
511
  /**
459
512
  * Structure for data passed to web components.
@@ -513,5 +566,92 @@ interface PikaWCContextRequestDetail {
513
566
  interface PikaWCContextRequestEvent extends CustomEvent<PikaWCContextRequestDetail> {
514
567
  detail: PikaWCContextRequestDetail;
515
568
  }
569
+ /**
570
+ * Metadata that widgets register with the parent app
571
+ *
572
+ * @example
573
+ * ```js
574
+ * const metadata: WidgetMetadata = {
575
+ * title: 'My Widget',
576
+ * iconSvg: '<svg>...</svg>',
577
+ * iconColor: '#001F3F',
578
+ * actions: [
579
+ * { id: 'refresh', title: 'Refresh', iconSvg: '<svg>...</svg>', callback: () => refresh() }
580
+ * ]
581
+ * };
582
+ * ```
583
+ *
584
+ * @since 0.11.0 - Moved from chatbot-types to webcomp-types
585
+ */
586
+ interface WidgetMetadata {
587
+ /** Widget title shown in chrome */
588
+ title: string;
589
+ /**
590
+ * Optional Lucide icon name (will be fetched automatically and set as iconSvg).
591
+ *
592
+ * The name will be snake cased as in `arrow-big-down` and not `arrowBigDown`
593
+ */
594
+ lucideIconName?: string;
595
+ /** Optional icon SVG markup for the widget title */
596
+ iconSvg?: string;
597
+ /** Optional color for the widget icon (hex, rgb, or CSS color name) */
598
+ iconColor?: string;
599
+ /** Optional action buttons */
600
+ actions?: WidgetAction[];
601
+ /** Optional loading status */
602
+ loadingStatus?: {
603
+ loading: boolean;
604
+ loadingMsg?: string;
605
+ };
606
+ }
607
+ /**
608
+ * Internal state tracked for each widget instance
609
+ *
610
+ * @since 0.11.0 - Moved from chatbot-types to webcomp-types
611
+ */
612
+ interface WidgetMetadataState extends WidgetMetadata {
613
+ /** Unique instance ID for this widget */
614
+ instanceId: string;
615
+ /** Widget scope (e.g., 'weather', 'pika') */
616
+ scope: string;
617
+ /** Widget tag (e.g., 'favorite-cities') */
618
+ tag: string;
619
+ /** Rendering context (spotlight, canvas, dialog, inline) */
620
+ renderingContext: WidgetRenderingContextType;
621
+ }
622
+ /**
623
+ * This is used when manually registering a custom element as a spotlight widget by a component in the client.
624
+ *
625
+ * @since 0.11.0 - Moved from chatbot-types to webcomp-types
626
+ */
627
+ interface SpotlightWidgetDefinition {
628
+ /** @see TagDefinition.tag */
629
+ tag: string;
630
+ /** @see TagDefinition.scope */
631
+ scope: string;
632
+ /** @see TagDefinitionWidgetWebComponent.customElementName */
633
+ customElementName?: string;
634
+ /** @see TagDefinition.tagTitle */
635
+ tagTitle: string;
636
+ /** @see TagDefinitionWidgetWebComponent.sizing */
637
+ sizing?: WidgetSizing;
638
+ /** @see TagDefinition.componentAgentInstructionsMd */
639
+ componentAgentInstructionsMd?: Record<string, string>;
640
+ /**
641
+ * If true and there isn't an instance of this widget already created as a spotlight widget, then a new instance will be created.
642
+ */
643
+ autoCreateInstance?: boolean;
644
+ /** The display order of the widget in the spotlight. If not provided, is put first. */
645
+ displayOrder?: number;
646
+ /** Defaults to true. If true, then only one instance of this widget can be created. */
647
+ singleton?: boolean;
648
+ /** If false, widget won't appear in unpinned menu. Default: true. Use false for base widgets that only create instances */
649
+ showInUnpinnedMenu?: boolean;
650
+ /**
651
+ * Optional metadata (title, actions, icon) to apply to the widget when rendered
652
+ * @since 0.11.0
653
+ */
654
+ metadata?: WidgetMetadata;
655
+ }
516
656
 
517
- export type { DataForWidget, IAppState, IChatAppState, IIdentityState, IUploadInstance, IUserPrefsState, IWidgetMetadataAPI, OnReadyCallback, PikaWCContext, PikaWCContextRequestCallbackFn, PikaWCContextRequestDetail, PikaWCContextRequestEvent, PikaWCContextWithoutInstanceId, SidebarState, Snippet };
657
+ export type { DataForWidget, IAppState, IChatAppState, IIdentityState, IUploadInstance, IUserPrefsState, IWidgetMetadataAPI, OnReadyCallback, PikaWCContext, PikaWCContextRequestCallbackFn, PikaWCContextRequestDetail, PikaWCContextRequestEvent, PikaWCContextWithoutInstanceId, SidebarState, Snippet, SpotlightWidgetDefinition, WidgetAction, WidgetCallbackContext, WidgetMetadata, WidgetMetadataState };
@@ -1,5 +1,5 @@
1
1
  import { UploadStatus } from '../upload-types.js';
2
- import { ShowToastFn, ChatUser, RecordOrUndef, UserAwsCredentials, WidgetRenderingContextType, ShareSessionState, UserPrefs, ChatAppMode, IUserWidgetDataStoreState, CustomDataUiRepresentation, ChatAppOverridableFeatures, TagDefinition, TagDefinitionWidget, ChatSession, UserDataOverrideSettings, ChatMessageForRendering, ChatApp, ChatAppActionMenu, ChatAppAction, WidgetInstance, WidgetMetadata, SpotlightWidgetDefinition, InvokeAgentAsComponentOptions, WidgetAction } from './chatbot-types.js';
2
+ import { ShowToastFn, ChatUser, RecordOrUndef, UserAwsCredentials, WidgetRenderingContextType, ShareSessionState, UserPrefs, ChatAppMode, IUserWidgetDataStoreState, CustomDataUiRepresentation, ChatAppOverridableFeatures, TagDefinition, TagDefinitionWidget, ChatSession, UserDataOverrideSettings, ChatMessageForRendering, ChatApp, ChatAppActionMenu, ChatAppAction, WidgetInstance, WidgetSizing, InvokeAgentAsComponentOptions } from './chatbot-types.js';
3
3
  import '@aws-sdk/client-bedrock-agent-runtime';
4
4
  import '@aws-sdk/client-bedrock-agentcore';
5
5
 
@@ -177,7 +177,11 @@ interface IChatAppState {
177
177
  readonly customDataForChatApp: Record<string, unknown> | undefined;
178
178
  readonly customTitleBarActions: (ChatAppActionMenu | ChatAppAction)[];
179
179
  readonly widgetInstances: Map<string, WidgetInstance>;
180
- readonly user: ChatUser;
180
+ /**
181
+ * Current user information
182
+ * @since 0.11.0
183
+ */
184
+ readonly user: ChatUser<RecordOrUndef>;
181
185
  setCurrentSessionById(sessionId: string): void;
182
186
  removeFile(s3Key: string): void;
183
187
  startNewChatSession(): void;
@@ -188,12 +192,22 @@ interface IChatAppState {
188
192
  getMessageByMessageId(messageId: string): ChatMessageForRendering | undefined;
189
193
  uploadFiles(files: File[]): Promise<void>;
190
194
  initializeData(): Promise<void>;
195
+ /**
196
+ * Render a tag in a specific context
197
+ * @param metadata - Optional metadata for the widget (title, actions, icon)
198
+ * @since 0.11.0 - Added metadata parameter
199
+ */
191
200
  renderTag(tagId: string, context: 'spotlight' | 'inline' | 'dialog' | 'canvas' | 'static', data?: Record<string, any>, metadata?: WidgetMetadata): Promise<void>;
192
201
  closeCanvas(): void;
193
202
  closeDialog(): void;
194
203
  setOrUpdateCustomTitleBarAction(action: ChatAppActionMenu | ChatAppAction): void;
195
204
  removeCustomTitleBarAction(actionId: string): void;
196
205
  getWidgetInstance(instanceId: string): WidgetInstance | undefined;
206
+ /**
207
+ * Get the full context for a widget instance
208
+ * @since 0.11.0
209
+ */
210
+ getWidgetContext(instanceId: string): PikaWCContext | undefined;
197
211
  /**
198
212
  * Update context for a widget. Call this when your widget's context has changed.
199
213
  *
@@ -272,6 +286,7 @@ interface IChatAppState {
272
286
  * @param dataKey - The key to store data under (default: 'data')
273
287
  * @param metadata - Optional widget metadata (title, actions, icon, etc.)
274
288
  * @returns The instance ID (UUID)
289
+ * @since 0.11.0 - Added metadata parameter
275
290
  *
276
291
  * @example
277
292
  * ```typescript
@@ -446,14 +461,52 @@ interface IUploadInstance {
446
461
  * Called after the element is created but before it's added to the DOM.
447
462
  */
448
463
  interface OnReadyCallback {
449
- (params: {
450
- /** The web component element that was created */
451
- element: HTMLElement;
452
- /** The unique instance ID assigned to this component */
453
- instanceId: string;
454
- /** The full Pika context with instanceId */
455
- context: PikaWCContext;
456
- }): void;
464
+ (params: WidgetCallbackContext): void;
465
+ }
466
+ /**
467
+ * Action button that appears in the widget's chrome (title bar, toolbar, etc.)
468
+ *
469
+ * @example
470
+ * ```js
471
+ * const action: WidgetAction = {
472
+ * id: 'refresh',
473
+ * title: 'Refresh data',
474
+ * iconSvg: '<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20">...</svg>',
475
+ * callback: async () => { await fetchData(); }
476
+ * };
477
+ * ```
478
+ *
479
+ * @since 0.11.0 - Moved from chatbot-types to webcomp-types
480
+ */
481
+ interface WidgetAction {
482
+ /** Unique identifier for this action */
483
+ id: string;
484
+ /** Tooltip/label for the action (also button text in dialog context) */
485
+ title: string;
486
+ /** SVG markup string for the icon (e.g., from extractIconSvg() helper) */
487
+ iconSvg: string;
488
+ /** Whether action is currently disabled */
489
+ disabled?: boolean;
490
+ /** If true, renders as default/prominent button (used in dialog context) */
491
+ primary?: boolean;
492
+ /**
493
+ * Handler when clicked
494
+ * @since 0.11.0 - Callback now receives WidgetCallbackContext parameter
495
+ */
496
+ callback: (context: WidgetCallbackContext) => void | Promise<void>;
497
+ }
498
+ /**
499
+ * The context object that is passed to the onReady callback and in action callbacks functions.
500
+ *
501
+ * @since 0.11.0
502
+ */
503
+ interface WidgetCallbackContext {
504
+ /** The web component element that was created */
505
+ element: HTMLElement;
506
+ /** The unique instance ID assigned to this component */
507
+ instanceId: string;
508
+ /** The full Pika context with instanceId */
509
+ context: PikaWCContext;
457
510
  }
458
511
  /**
459
512
  * Structure for data passed to web components.
@@ -513,5 +566,92 @@ interface PikaWCContextRequestDetail {
513
566
  interface PikaWCContextRequestEvent extends CustomEvent<PikaWCContextRequestDetail> {
514
567
  detail: PikaWCContextRequestDetail;
515
568
  }
569
+ /**
570
+ * Metadata that widgets register with the parent app
571
+ *
572
+ * @example
573
+ * ```js
574
+ * const metadata: WidgetMetadata = {
575
+ * title: 'My Widget',
576
+ * iconSvg: '<svg>...</svg>',
577
+ * iconColor: '#001F3F',
578
+ * actions: [
579
+ * { id: 'refresh', title: 'Refresh', iconSvg: '<svg>...</svg>', callback: () => refresh() }
580
+ * ]
581
+ * };
582
+ * ```
583
+ *
584
+ * @since 0.11.0 - Moved from chatbot-types to webcomp-types
585
+ */
586
+ interface WidgetMetadata {
587
+ /** Widget title shown in chrome */
588
+ title: string;
589
+ /**
590
+ * Optional Lucide icon name (will be fetched automatically and set as iconSvg).
591
+ *
592
+ * The name will be snake cased as in `arrow-big-down` and not `arrowBigDown`
593
+ */
594
+ lucideIconName?: string;
595
+ /** Optional icon SVG markup for the widget title */
596
+ iconSvg?: string;
597
+ /** Optional color for the widget icon (hex, rgb, or CSS color name) */
598
+ iconColor?: string;
599
+ /** Optional action buttons */
600
+ actions?: WidgetAction[];
601
+ /** Optional loading status */
602
+ loadingStatus?: {
603
+ loading: boolean;
604
+ loadingMsg?: string;
605
+ };
606
+ }
607
+ /**
608
+ * Internal state tracked for each widget instance
609
+ *
610
+ * @since 0.11.0 - Moved from chatbot-types to webcomp-types
611
+ */
612
+ interface WidgetMetadataState extends WidgetMetadata {
613
+ /** Unique instance ID for this widget */
614
+ instanceId: string;
615
+ /** Widget scope (e.g., 'weather', 'pika') */
616
+ scope: string;
617
+ /** Widget tag (e.g., 'favorite-cities') */
618
+ tag: string;
619
+ /** Rendering context (spotlight, canvas, dialog, inline) */
620
+ renderingContext: WidgetRenderingContextType;
621
+ }
622
+ /**
623
+ * This is used when manually registering a custom element as a spotlight widget by a component in the client.
624
+ *
625
+ * @since 0.11.0 - Moved from chatbot-types to webcomp-types
626
+ */
627
+ interface SpotlightWidgetDefinition {
628
+ /** @see TagDefinition.tag */
629
+ tag: string;
630
+ /** @see TagDefinition.scope */
631
+ scope: string;
632
+ /** @see TagDefinitionWidgetWebComponent.customElementName */
633
+ customElementName?: string;
634
+ /** @see TagDefinition.tagTitle */
635
+ tagTitle: string;
636
+ /** @see TagDefinitionWidgetWebComponent.sizing */
637
+ sizing?: WidgetSizing;
638
+ /** @see TagDefinition.componentAgentInstructionsMd */
639
+ componentAgentInstructionsMd?: Record<string, string>;
640
+ /**
641
+ * If true and there isn't an instance of this widget already created as a spotlight widget, then a new instance will be created.
642
+ */
643
+ autoCreateInstance?: boolean;
644
+ /** The display order of the widget in the spotlight. If not provided, is put first. */
645
+ displayOrder?: number;
646
+ /** Defaults to true. If true, then only one instance of this widget can be created. */
647
+ singleton?: boolean;
648
+ /** If false, widget won't appear in unpinned menu. Default: true. Use false for base widgets that only create instances */
649
+ showInUnpinnedMenu?: boolean;
650
+ /**
651
+ * Optional metadata (title, actions, icon) to apply to the widget when rendered
652
+ * @since 0.11.0
653
+ */
654
+ metadata?: WidgetMetadata;
655
+ }
516
656
 
517
- export type { DataForWidget, IAppState, IChatAppState, IIdentityState, IUploadInstance, IUserPrefsState, IWidgetMetadataAPI, OnReadyCallback, PikaWCContext, PikaWCContextRequestCallbackFn, PikaWCContextRequestDetail, PikaWCContextRequestEvent, PikaWCContextWithoutInstanceId, SidebarState, Snippet };
657
+ export type { DataForWidget, IAppState, IChatAppState, IIdentityState, IUploadInstance, IUserPrefsState, IWidgetMetadataAPI, OnReadyCallback, PikaWCContext, PikaWCContextRequestCallbackFn, PikaWCContextRequestDetail, PikaWCContextRequestEvent, PikaWCContextWithoutInstanceId, SidebarState, Snippet, SpotlightWidgetDefinition, WidgetAction, WidgetCallbackContext, WidgetMetadata, WidgetMetadataState };