@usereq/widget 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/README.md +166 -0
- package/dist/widget.css +2 -0
- package/dist/widget.js +3569 -0
- package/package.json +98 -0
- package/src/chat-widget/chat-widget-appearance.ts +55 -0
- package/src/chat-widget/chat-widget-defaults.tsx +45 -0
- package/src/chat-widget/components/chat-widget-frame.tsx +56 -0
- package/src/chat-widget/components/chat-widget-layout.ts +6 -0
- package/src/chat-widget/components/chat-widget-primitives.tsx +720 -0
- package/src/chat-widget/components/confirmation.tsx +155 -0
- package/src/chat-widget/components/conversation.tsx +110 -0
- package/src/chat-widget/components/message.tsx +325 -0
- package/src/chat-widget/index.ts +9 -0
- package/src/chat-widget/stop-confirmation.ts +288 -0
- package/src/chat-widget/styles/chat-widget-box.tsx +258 -0
- package/src/chat-widget/styles/chat-widget-bubble.tsx +238 -0
- package/src/chat-widget/styles/chat-widget-chatbar.tsx +248 -0
- package/src/custom-element/agent-widget-element.tsx +584 -0
- package/src/embed.ts +8 -0
- package/src/index.ts +10 -0
- package/src/register.ts +15 -0
- package/src/renderer/index.ts +6 -0
- package/src/renderer/widget-runtime.tsx +205 -0
- package/src/runtime/analytics.ts +327 -0
- package/src/runtime/api-origin.ts +29 -0
- package/src/runtime/api.ts +151 -0
- package/src/runtime/bootstrap.ts +309 -0
- package/src/runtime/messages.ts +12 -0
- package/src/runtime/session-storage.ts +32 -0
- package/src/runtime/token-renewal.ts +13 -0
- package/src/runtime/trigger-rule.ts +182 -0
- package/src/shared/analytics.ts +54 -0
- package/src/shared/shadow-css.ts +15 -0
- package/src/shared/shadow-theme.ts +85 -0
- package/src/shared/stop-confirmation.ts +20 -0
- package/src/shared/widget-config.ts +104 -0
- package/src/styles/widget.css.ts +27 -0
- package/src/types.ts +85 -0
- package/src/vite-env.d.ts +4 -0
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export {
|
|
2
|
+
DEFAULT_STOP_CONVERSATION_PROMPT,
|
|
3
|
+
STOP_CONFIRMATION_KIND,
|
|
4
|
+
STOP_CONFIRMATION_RESULT_KIND,
|
|
5
|
+
createStopConfirmationPromptEnvelope,
|
|
6
|
+
createStopConfirmationResultEnvelope,
|
|
7
|
+
encodeStopConfirmationEnvelope,
|
|
8
|
+
encodeStopConfirmationPrompt,
|
|
9
|
+
encodeStopConfirmationResult,
|
|
10
|
+
foldStopConfirmationMessages,
|
|
11
|
+
getPendingStopConfirmationId,
|
|
12
|
+
parseStopConfirmationEnvelope,
|
|
13
|
+
type StopConfirmationDecision,
|
|
14
|
+
type StopConfirmationEnvelope,
|
|
15
|
+
type StopConfirmationMessageLike,
|
|
16
|
+
type StopConfirmationPromptEnvelope,
|
|
17
|
+
type StopConfirmationRenderItem,
|
|
18
|
+
type StopConfirmationResultEnvelope,
|
|
19
|
+
type StopConfirmationState,
|
|
20
|
+
} from "../chat-widget/stop-confirmation";
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DEFAULT_CHAT_WIDGET_APPEARANCE,
|
|
3
|
+
resolveChatWidgetAppearance,
|
|
4
|
+
type ResolvedChatWidgetAppearance,
|
|
5
|
+
} from "../chat-widget/chat-widget-appearance";
|
|
6
|
+
|
|
7
|
+
export type WidgetPlacement =
|
|
8
|
+
| "top-left"
|
|
9
|
+
| "top-right"
|
|
10
|
+
| "top-center"
|
|
11
|
+
| "bottom-left"
|
|
12
|
+
| "bottom-right"
|
|
13
|
+
| "bottom-center";
|
|
14
|
+
|
|
15
|
+
export type WidgetVariant = "default" | "chatbar" | "box";
|
|
16
|
+
|
|
17
|
+
export type WidgetAppearance = ResolvedChatWidgetAppearance;
|
|
18
|
+
|
|
19
|
+
export const DEFAULT_WIDGET_APPEARANCE: WidgetAppearance =
|
|
20
|
+
DEFAULT_CHAT_WIDGET_APPEARANCE;
|
|
21
|
+
|
|
22
|
+
export type WidgetTriggerRule =
|
|
23
|
+
| { triggerRuleType: "time_delay"; timeDelay: number }
|
|
24
|
+
| { triggerRuleType: "scroll_to_bottom"; scrollToBottom: boolean }
|
|
25
|
+
| { triggerRuleType: "element_clicked"; elementClicked: string }
|
|
26
|
+
| { triggerRuleType: "element_displayed"; elementDisplayed: string };
|
|
27
|
+
|
|
28
|
+
export function widgetTriggerRuleKey(
|
|
29
|
+
rule: WidgetTriggerRule | null | undefined,
|
|
30
|
+
): string {
|
|
31
|
+
if (!rule) {
|
|
32
|
+
return "none";
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
switch (rule.triggerRuleType) {
|
|
36
|
+
case "time_delay":
|
|
37
|
+
return `time_delay:${rule.timeDelay}`;
|
|
38
|
+
case "scroll_to_bottom":
|
|
39
|
+
return `scroll_to_bottom:${String(rule.scrollToBottom)}`;
|
|
40
|
+
case "element_clicked":
|
|
41
|
+
return `element_clicked:${rule.elementClicked}`;
|
|
42
|
+
case "element_displayed":
|
|
43
|
+
return `element_displayed:${rule.elementDisplayed}`;
|
|
44
|
+
default:
|
|
45
|
+
return "unknown";
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function normalizeWidgetAppearance(
|
|
50
|
+
appearance: Partial<WidgetAppearance> | null | undefined,
|
|
51
|
+
): WidgetAppearance {
|
|
52
|
+
return resolveChatWidgetAppearance(appearance);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export const resolveWidgetAppearance = normalizeWidgetAppearance;
|
|
56
|
+
|
|
57
|
+
export function widgetAppearanceToAttributes(
|
|
58
|
+
appearance: WidgetAppearance,
|
|
59
|
+
): Array<[string, string]> {
|
|
60
|
+
return [
|
|
61
|
+
["avatar-orb-color-1", appearance.avatarOrbColor1 ?? ""],
|
|
62
|
+
["avatar-orb-color-2", appearance.avatarOrbColor2 ?? ""],
|
|
63
|
+
["action-text", appearance.actionText ?? ""],
|
|
64
|
+
];
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function normalizeWidgetVariant(
|
|
68
|
+
value: string | null | undefined,
|
|
69
|
+
): WidgetVariant {
|
|
70
|
+
switch (value) {
|
|
71
|
+
case "chatbar":
|
|
72
|
+
case "box":
|
|
73
|
+
case "default":
|
|
74
|
+
return value;
|
|
75
|
+
default:
|
|
76
|
+
return "chatbar";
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export function normalizeWidgetPlacement(
|
|
81
|
+
value: string | null | undefined,
|
|
82
|
+
): WidgetPlacement {
|
|
83
|
+
switch (value) {
|
|
84
|
+
case "top-left":
|
|
85
|
+
case "top-center":
|
|
86
|
+
case "top-right":
|
|
87
|
+
case "bottom-left":
|
|
88
|
+
case "bottom-center":
|
|
89
|
+
case "bottom-right":
|
|
90
|
+
return value;
|
|
91
|
+
default:
|
|
92
|
+
return "bottom-right";
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function splitWidgetPlacement(
|
|
97
|
+
placement: WidgetPlacement,
|
|
98
|
+
): { horizontal: "left" | "center" | "right"; vertical: "top" | "bottom" } {
|
|
99
|
+
const [vertical, horizontal] = placement.split("-") as [
|
|
100
|
+
"top" | "bottom",
|
|
101
|
+
"left" | "center" | "right",
|
|
102
|
+
];
|
|
103
|
+
return { horizontal, vertical };
|
|
104
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export const widgetCss = `
|
|
2
|
+
.widget-shell {
|
|
3
|
+
display: block;
|
|
4
|
+
box-sizing: border-box;
|
|
5
|
+
pointer-events: none;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
@media (max-width: 520px) {
|
|
9
|
+
.widget-shell[data-mode="embed"] {
|
|
10
|
+
top: auto !important;
|
|
11
|
+
right: 12px !important;
|
|
12
|
+
bottom: 12px !important;
|
|
13
|
+
left: 12px !important;
|
|
14
|
+
transform: none !important;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
.widget-shell[data-mode="embed"][data-open="true"] {
|
|
18
|
+
top: 0 !important;
|
|
19
|
+
right: 0 !important;
|
|
20
|
+
bottom: 0 !important;
|
|
21
|
+
left: 0 !important;
|
|
22
|
+
width: 100vw !important;
|
|
23
|
+
height: 100svh !important;
|
|
24
|
+
max-width: none !important;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
`;
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
WidgetAppearance,
|
|
3
|
+
WidgetPlacement,
|
|
4
|
+
WidgetVariant,
|
|
5
|
+
WidgetTriggerRule,
|
|
6
|
+
} from "./shared/widget-config";
|
|
7
|
+
|
|
8
|
+
export {
|
|
9
|
+
DEFAULT_WIDGET_APPEARANCE,
|
|
10
|
+
type WidgetAppearance,
|
|
11
|
+
type WidgetPlacement,
|
|
12
|
+
type WidgetVariant,
|
|
13
|
+
normalizeWidgetAppearance,
|
|
14
|
+
normalizeWidgetPlacement,
|
|
15
|
+
normalizeWidgetVariant,
|
|
16
|
+
splitWidgetPlacement,
|
|
17
|
+
widgetAppearanceToAttributes,
|
|
18
|
+
type WidgetTriggerRule,
|
|
19
|
+
} from "./shared/widget-config";
|
|
20
|
+
export type {
|
|
21
|
+
WidgetAnalyticsBatchEvent,
|
|
22
|
+
WidgetAnalyticsBatchRequest,
|
|
23
|
+
WidgetAnalyticsBatchResponse,
|
|
24
|
+
WidgetAnalyticsEventType,
|
|
25
|
+
WidgetClientAnalyticsEventType,
|
|
26
|
+
WidgetTriggerRuleType,
|
|
27
|
+
} from "./shared/analytics";
|
|
28
|
+
|
|
29
|
+
export type WidgetSessionConfig = {
|
|
30
|
+
sessionToken: string;
|
|
31
|
+
expiresAt: string;
|
|
32
|
+
widgetConfig: {
|
|
33
|
+
agentId: string;
|
|
34
|
+
name: string;
|
|
35
|
+
agentAvatarUrl: string | null;
|
|
36
|
+
welcomeMessage: string | null;
|
|
37
|
+
spotlightMessages: string[];
|
|
38
|
+
widgetAppearance: WidgetAppearance;
|
|
39
|
+
widgetVariant: WidgetVariant;
|
|
40
|
+
widgetPlacement: WidgetPlacement;
|
|
41
|
+
allowedDomains: string[];
|
|
42
|
+
triggerRule: WidgetTriggerRule | null;
|
|
43
|
+
};
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
export type WidgetConversation = {
|
|
47
|
+
id: string;
|
|
48
|
+
agentId: string;
|
|
49
|
+
sessionSid: string;
|
|
50
|
+
sessionExpiresAt: string;
|
|
51
|
+
isTest: boolean;
|
|
52
|
+
createdAt: string;
|
|
53
|
+
updatedAt: string;
|
|
54
|
+
metadata?: Record<string, unknown> | null;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
export type WidgetMessage = {
|
|
58
|
+
id: string;
|
|
59
|
+
conversationId: string;
|
|
60
|
+
role: "user" | "assistant";
|
|
61
|
+
content: string;
|
|
62
|
+
createdAt: string;
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
export type WidgetPublicChatSendChatResponse = {
|
|
66
|
+
kind: "chat";
|
|
67
|
+
userMessage: WidgetMessage;
|
|
68
|
+
assistantMessages: WidgetMessage[];
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
export type WidgetPublicChatSendStopConfirmationResponse = {
|
|
72
|
+
kind: "stop_confirmation";
|
|
73
|
+
confirmationMessage: WidgetMessage;
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
export type WidgetPublicChatSendResponse =
|
|
77
|
+
| WidgetPublicChatSendChatResponse
|
|
78
|
+
| WidgetPublicChatSendStopConfirmationResponse;
|
|
79
|
+
|
|
80
|
+
export type WidgetSessionState = {
|
|
81
|
+
sessionToken: string;
|
|
82
|
+
expiresAt: string;
|
|
83
|
+
widgetConfig: WidgetSessionConfig["widgetConfig"];
|
|
84
|
+
conversationId?: string;
|
|
85
|
+
};
|