@stack-spot/portal-network 1.0.0-betaadp.1 → 1.0.0-betaadp.3

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,240 +0,0 @@
1
- import { random } from 'lodash'
2
- import { getApiAddresses } from '../api-addresses'
3
- import { ConversationResponse } from '../api/ai'
4
- import { defaults } from '../api/discovery'
5
- import { StackspotAPIError } from '../error/StackspotAPIError'
6
- import { NetworkClient } from '../network/NetworkClient'
7
- import { ReactQueryNetworkClient } from '../network/ReactQueryNetworkClient'
8
- import { aiClient } from './ai'
9
-
10
- interface Resource {
11
- title: string,
12
- description: string,
13
- }
14
-
15
- interface PersistedResource extends Resource {
16
- id: string,
17
- createdAt: string,
18
- updatedAt: string,
19
- createdBy: { id: string, name: string },
20
- type: 'opportunity' | 'hypothesis' | 'document',
21
- }
22
-
23
- export interface Opportunity extends PersistedResource {
24
- type: 'opportunity',
25
- conversationId: string,
26
- }
27
-
28
- export interface Hypothesis extends PersistedResource {
29
- type: 'hypothesis',
30
- opportunityId: string,
31
- }
32
-
33
- export interface Document extends PersistedResource {
34
- type: 'document',
35
- hypothesisId: string,
36
- }
37
-
38
- export type Artifact = Opportunity | Hypothesis | Document
39
-
40
- export interface ChatConversionDetails extends ConversationResponse {
41
- opportunityName?: string,
42
- hypothesisCount?: number,
43
- }
44
-
45
-
46
- const key = 'mock-adp-api'
47
-
48
- function delay() {
49
- return new Promise((resolve) => {
50
- setTimeout(resolve, random(500, 1500))
51
- })
52
- }
53
-
54
- async function load(): Promise<Artifact[]> {
55
- await delay()
56
- const fromStorage = localStorage.getItem(key)
57
- return fromStorage ? JSON.parse(fromStorage) : []
58
- }
59
-
60
- async function save(data: Artifact[]) {
61
- await delay()
62
- localStorage.setItem(key, JSON.stringify(data))
63
- }
64
-
65
- function createResource(data: Resource): Omit<PersistedResource, 'type'> {
66
- const tokenData = NetworkClient.sessionManager?.getSession().getTokenData()
67
- return {
68
- ...data,
69
- id: `${Math.random()}`,
70
- createdAt: new Date().toISOString(),
71
- updatedAt: new Date().toISOString(),
72
- createdBy: {
73
- id: tokenData?.account_id_v2 ?? '',
74
- name: tokenData?.name ?? '',
75
- },
76
- }
77
- }
78
-
79
- class AdpMockClient extends ReactQueryNetworkClient {
80
- constructor() {
81
- super(getApiAddresses().discovery.url, defaults)
82
- }
83
-
84
- protected buildStackSpotError(error: any): StackspotAPIError {
85
- return new StackspotAPIError({ status: 0, message: error?.message })
86
- }
87
-
88
- opportunities = this.query({
89
- name: 'opportunities',
90
- request: async () => {
91
- const artifacts = await load()
92
- return artifacts.filter(a => a.type === 'opportunity') as Opportunity[]
93
- },
94
- })
95
-
96
- opportunity = this.query({
97
- name: 'opportunity',
98
- request: async (_abort, { id }: { id: string }) => {
99
- const artifacts = await load()
100
- return artifacts.find(a => a.type === 'opportunity' && a.id === id) as Opportunity
101
- },
102
- })
103
-
104
- hypothesis = this.query({
105
- name: 'hypothesis',
106
- request: async (_abort, { opportunityId }: { opportunityId: string }) => {
107
- const artifacts = await load()
108
- return artifacts.filter(a => a.type === 'hypothesis' && a.opportunityId === opportunityId) as Hypothesis[]
109
- },
110
- })
111
-
112
- hypothesisById = this.query({
113
- name: 'hypothesisById',
114
- request: async (_abort, { id }: { id: string }) => {
115
- const artifacts = await load()
116
- return artifacts.find(a => a.type === 'hypothesis' && a.id === id) as Hypothesis
117
- },
118
- })
119
-
120
- documents = this.query({
121
- name: 'documents',
122
- request: async (_abort, { hypothesisId }: { hypothesisId: string }) => {
123
- const artifacts = await load()
124
- return artifacts.filter(a => a.type === 'document' && a.hypothesisId === hypothesisId) as Document[]
125
- },
126
- })
127
-
128
- document = this.query({
129
- name: 'document',
130
- request: async (_abort, { id }: { id: string }) => {
131
- const artifacts = await load()
132
- return artifacts.find(a => a.type === 'document' && a.id === id) as Document
133
- },
134
- })
135
-
136
- artifacts = this.query({
137
- name: 'artifacts',
138
- request: async (_abort, { conversationId }: { conversationId: string }) => {
139
- const artifacts = await load()
140
- const opportunities = artifacts.filter(a => a.type === 'opportunity' && a.conversationId === conversationId)
141
- const hypotheses = artifacts.filter(a => a.type === 'hypothesis' && opportunities.some(o => o.id === a.opportunityId))
142
- const documents = artifacts.filter(a => a.type === 'document' && hypotheses.some(h => h.id === a.hypothesisId))
143
- return [...opportunities, ...hypotheses, ...documents]
144
- },
145
- })
146
-
147
- createOpportunity = this.mutation({
148
- name: 'createOpportunity',
149
- request: async (_abort, { conversationId, opportunity }: { opportunity: Resource, conversationId: string }) => {
150
- const artifacts = await load()
151
- const created: Opportunity = { ...createResource(opportunity), type: 'opportunity', conversationId }
152
- artifacts.push(created)
153
- await save(artifacts)
154
- return created
155
- },
156
- })
157
-
158
- createHypothesis = this.mutation({
159
- name: 'createHypothesis',
160
- request: async (_abort, { hypothesis, opportunityId }: { hypothesis: Resource, opportunityId: string }) => {
161
- const artifacts = await load()
162
- const created: Hypothesis = { ...createResource(hypothesis), type: 'hypothesis', opportunityId }
163
- artifacts.push(created)
164
- await save(artifacts)
165
- return created
166
- },
167
- })
168
-
169
- createDocument = this.mutation({
170
- name: 'createDocument',
171
- request: async (_abort, { document, hypothesisId }: { document: Resource, hypothesisId: string }) => {
172
- const artifacts = await load()
173
- const created: Document = { ...createResource(document), type: 'document', hypothesisId }
174
- artifacts.push(created)
175
- await save(artifacts)
176
- return created
177
- },
178
- })
179
-
180
- private deleteArtifact = async (_abort: AbortSignal, { id }: { id: string }) => {
181
- const artifacts = await load()
182
- await save(artifacts.filter(a => a.id !== id))
183
- }
184
-
185
- deleteOpportunity = this.mutation({
186
- name: 'deleteOpportunity',
187
- request: this.deleteArtifact,
188
- })
189
-
190
- deleteHypothesis = this.mutation({
191
- name: 'deleteHypothesis',
192
- request: this.deleteArtifact,
193
- })
194
-
195
- deleteDocument = this.mutation({
196
- name: 'deleteDocument',
197
- request: this.deleteArtifact,
198
- })
199
-
200
- chats = this.query({
201
- name: 'chats',
202
- request: async (_signal, variables: { filter?: string, page?: number, size?: number }) => {
203
- const artifacts = await load()
204
-
205
- const groupedByConversionId: Record<string, any[]> = artifacts.reduce(
206
- (acc, item: any) => {
207
- if (!acc[item.conversationId]) {
208
- acc[item.conversationId] = []
209
- }
210
- acc[item.conversationId].push(item)
211
- return acc
212
- },
213
- {} as Record<string, any[]>,
214
- )
215
-
216
- const chatsHistory = await aiClient.chats.query(
217
- { ...variables, page: variables.page, size: variables.size ?? 40 },
218
- )
219
-
220
- const filteredItems = variables.filter ?
221
- chatsHistory.filter((chat) => chat.title.toLowerCase().includes(variables.filter!.toLowerCase())) : chatsHistory
222
-
223
- const enrichedChats = filteredItems?.map(chat => {
224
- const relatedArtifacts = groupedByConversionId[chat.id]
225
- const opportunity = relatedArtifacts?.find(a => a.type === 'opportunity')
226
- const opportunityName = opportunity?.title ?? null
227
- const hypothesisCount = relatedArtifacts?.filter(a => a.type === 'hypothesis').length ?? 0
228
-
229
- return {
230
- ...chat,
231
- opportunityName,
232
- hypothesisCount,
233
- }
234
- })
235
- return enrichedChats as ChatConversionDetails[]
236
- },
237
- })
238
- }
239
-
240
- export const adpMockClient = new AdpMockClient()
@@ -1,73 +0,0 @@
1
- import { HttpError } from '@oazapfts/runtime'
2
- import { attach, createHypothesis, defaults, deleteV1DocumentsByDocumentIdHypothesesAndHypothesisNumber, download, findHypothesisByNumber, getAll, getById, list1, list2, listAllDocumentsWithHypotheses, listHypothesis, listVersions, upload } from '../api/discovery'
3
- import { DefaultAPIError } from '../error/DefaultAPIError'
4
- import { baseDictionary } from '../error/dictionary/base'
5
- import { StackspotAPIError } from '../error/StackspotAPIError'
6
- import { ReactQueryNetworkClient } from '../network/ReactQueryNetworkClient'
7
- import { removeAuthorizationParam } from '../utils/remove-authorization-param'
8
- import { getApiAddresses } from '../api-addresses'
9
-
10
- class DiscoveryClient extends ReactQueryNetworkClient {
11
-
12
- constructor() {
13
- super(getApiAddresses().discovery.url, defaults)
14
- }
15
-
16
- protected buildStackSpotError(error: HttpError): StackspotAPIError {
17
- return new DefaultAPIError(error.data, error.status, baseDictionary, error.headers)
18
- }
19
- /**
20
- * Get list of hypotheses
21
- */
22
- listHypotheses = this.query(listHypothesis)
23
- /**
24
- * Get details of an hypothesis by id
25
- */
26
- getHypothesisById = this.query(findHypothesisByNumber)
27
- /**
28
- * Get list of hypothesis documents
29
- */
30
- listDocumentsByHypothesis = this.query(list2)
31
- /**
32
- * Get list of all documents
33
- */
34
- listAllDocuments = this.query(listAllDocumentsWithHypotheses)
35
- /**
36
- * Get document details by id
37
- */
38
- getDocumentById = this.query(getById)
39
- /**
40
- * Get list of opportunities
41
- */
42
- listOpportunities = this.query(list1)
43
- /**
44
- * Get list of document types
45
- */
46
- listDocumentTypes = this.query(getAll)
47
- /**
48
- * Create an hypothesis
49
- */
50
- createHypothesis = this.mutation(removeAuthorizationParam(createHypothesis))
51
- /**
52
- * Create an document
53
- */
54
- createDocument = this.mutation(removeAuthorizationParam(upload))
55
- /**
56
- * Attach a existing document in an hypothesis
57
- */
58
- attachExistingDocument = this.mutation(removeAuthorizationParam(attach))
59
- /**
60
- * Delete an document
61
- */
62
- deleteDocument = this.mutation(removeAuthorizationParam(deleteV1DocumentsByDocumentIdHypothesesAndHypothesisNumber))
63
- /**
64
- * Download an document
65
- */
66
- downloadDocument = this.query(removeAuthorizationParam(download))
67
- /**
68
- * List versions of a document
69
- */
70
- listDocumentVersions = this.query(removeAuthorizationParam(listVersions))
71
- }
72
-
73
- export const discoveryClient = new DiscoveryClient()