@shop-prompter/react 0.1.1

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.
@@ -0,0 +1,135 @@
1
+ import { shopPrompterApi } from './api/shop-prompter-api'
2
+ import { sessionHandler } from './api/session-handler'
3
+ import { ShopPrompterStreamEvent } from './api/shop-prompter.types'
4
+
5
+ // Configuration - you can move these to environment variables
6
+ const SHOP_PROMPTER_CONFIG = {
7
+ baseUrl: 'https://demo.dev.shop-prompter.com/touch-points-api',
8
+ apiKey:
9
+ '864c87266ef52c079c09b3248d86b4b5ef430bf9e5a4188476124c93ee2f6dda380f337986837b30b60f73e6aceefe7a0e0f778b3d21a7fa7698cf8546afc709',
10
+ }
11
+
12
+ export interface ChatMessage {
13
+ id: string
14
+ role: 'user' | 'assistant'
15
+ content: string
16
+ productSkus?: string[]
17
+ isStreaming?: boolean
18
+ }
19
+
20
+ export class ShopPrompterService {
21
+ private isLoading = false
22
+ private error: string | null = null
23
+ private streamingText = ''
24
+
25
+ constructor() {
26
+ shopPrompterApi.setApiConfig(SHOP_PROMPTER_CONFIG)
27
+ sessionHandler.initialize()
28
+ }
29
+
30
+ public async sendMessage(
31
+ prompt: string,
32
+ callbacks: {
33
+ onTextUpdate?: (text: string) => void
34
+ onProductSkus?: (skus: string[]) => void
35
+ onComplete?: () => void
36
+ onCartOperation?: (
37
+ operations: {
38
+ sku: string
39
+ action: 'add' | 'remove'
40
+ quantity: number
41
+ }[],
42
+ ) => void
43
+ onError?: (error: string) => void
44
+ onLoadingChange?: (loading: boolean) => void
45
+ },
46
+ cartContext?: { sku: string; quantity: number }[],
47
+ ): Promise<string> {
48
+ this.isLoading = true
49
+ callbacks.onLoadingChange?.(true)
50
+ this.error = null
51
+ this.streamingText = ''
52
+
53
+ const userId = sessionHandler.getUserId()
54
+ const sessionId = sessionHandler.getSessionId()
55
+
56
+ let accumulatedText = ''
57
+
58
+ try {
59
+ await shopPrompterApi.executeStream(
60
+ prompt,
61
+ userId,
62
+ sessionId,
63
+ (event: ShopPrompterStreamEvent) => {
64
+ switch (event.type) {
65
+ case 'text':
66
+ accumulatedText += event.text
67
+ this.streamingText = accumulatedText
68
+ callbacks.onTextUpdate?.(accumulatedText)
69
+ break
70
+
71
+ case 'productSkus':
72
+ callbacks.onProductSkus?.(event.skus)
73
+ break
74
+
75
+ case 'navigation':
76
+ // Handle navigation events if needed
77
+ console.log('Navigation event:', event.navigationOperations)
78
+ break
79
+
80
+ case 'cart':
81
+ callbacks.onCartOperation?.(event.operations)
82
+ break
83
+
84
+ case 'close':
85
+ this.isLoading = false
86
+ callbacks.onLoadingChange?.(false)
87
+ callbacks.onComplete?.()
88
+ break
89
+
90
+ case 'error':
91
+ this.error = event.reason
92
+ this.isLoading = false
93
+ callbacks.onLoadingChange?.(false)
94
+ callbacks.onError?.(event.reason)
95
+ break
96
+ }
97
+ },
98
+ cartContext,
99
+ )
100
+ } catch (err) {
101
+ const errorMessage = err instanceof Error ? err.message : 'Unknown error'
102
+ this.error = errorMessage
103
+ this.isLoading = false
104
+ callbacks.onLoadingChange?.(false)
105
+ callbacks.onError?.(errorMessage)
106
+ }
107
+
108
+ return accumulatedText
109
+ }
110
+
111
+ public async getAgentExperience() {
112
+ console.log(
113
+ '## ~ ShopPrompterService ~ getAgentExperience ~ getAgentExperience:',
114
+ )
115
+
116
+ try {
117
+ return await shopPrompterApi.getAgentExperience()
118
+ } catch (err) {
119
+ console.error('Failed to fetch agent experience:', err)
120
+ throw err
121
+ }
122
+ }
123
+
124
+ public resetSession(): void {
125
+ sessionHandler.regenerate()
126
+ }
127
+
128
+ public getStatus() {
129
+ return {
130
+ isLoading: this.isLoading,
131
+ error: this.error,
132
+ streamingText: this.streamingText,
133
+ }
134
+ }
135
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "extends": "../../tsconfig.json",
3
+ "compilerOptions": {
4
+ "module": "esnext",
5
+ "target": "es2019",
6
+ "jsx": "react-jsx",
7
+ "allowJs": true,
8
+ "declaration": true,
9
+ "emitDeclarationOnly": true,
10
+ "outDir": "dist"
11
+ },
12
+ "include": [
13
+ "src"
14
+ ]
15
+ }