@stack-spot/portal-network 1.0.0-betaadp.2 → 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,244 +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
- conversationId: string,
32
- }
33
-
34
- export interface Document extends PersistedResource {
35
- type: 'document',
36
- hypothesisId: string,
37
- }
38
-
39
- export type Artifact = Opportunity | Hypothesis | Document
40
-
41
- export interface ChatConversionDetails extends ConversationResponse {
42
- opportunityName?: string,
43
- hypothesisCount?: number,
44
- }
45
-
46
-
47
- const key = 'mock-adp-api'
48
-
49
- function delay() {
50
- return new Promise((resolve) => {
51
- setTimeout(resolve, random(500, 1500))
52
- })
53
- }
54
-
55
- async function load(): Promise<Artifact[]> {
56
- await delay()
57
- const fromStorage = localStorage.getItem(key)
58
- return fromStorage ? JSON.parse(fromStorage) : []
59
- }
60
-
61
- async function save(data: Artifact[]) {
62
- await delay()
63
- localStorage.setItem(key, JSON.stringify(data))
64
- }
65
-
66
- function createResource(data: Resource): Omit<PersistedResource, 'type'> {
67
- const tokenData = NetworkClient.sessionManager?.getSession().getTokenData()
68
- return {
69
- ...data,
70
- id: `${Math.random()}`,
71
- createdAt: new Date().toISOString(),
72
- updatedAt: new Date().toISOString(),
73
- createdBy: {
74
- id: tokenData?.account_id_v2 ?? '',
75
- name: tokenData?.name ?? '',
76
- },
77
- }
78
- }
79
-
80
- class AdpMockClient extends ReactQueryNetworkClient {
81
- constructor() {
82
- super(getApiAddresses().discovery.url, defaults)
83
- }
84
-
85
- protected buildStackSpotError(error: any): StackspotAPIError {
86
- return new StackspotAPIError({ status: 0, message: error?.message })
87
- }
88
-
89
- opportunities = this.query({
90
- name: 'opportunities',
91
- request: async () => {
92
- const artifacts = await load()
93
- return artifacts.filter(a => a.type === 'opportunity') as Opportunity[]
94
- },
95
- })
96
-
97
- opportunity = this.query({
98
- name: 'opportunity',
99
- request: async (_abort, { id }: { id: string }) => {
100
- const artifacts = await load()
101
- return artifacts.find(a => a.type === 'opportunity' && a.id === id) as Opportunity
102
- },
103
- })
104
-
105
- hypothesis = this.query({
106
- name: 'hypothesis',
107
- request: async (_abort, { opportunityId }: { opportunityId: string }) => {
108
- const artifacts = await load()
109
- return artifacts.filter(a => a.type === 'hypothesis' && a.opportunityId === opportunityId) as Hypothesis[]
110
- },
111
- })
112
-
113
- hypothesisById = this.query({
114
- name: 'hypothesisById',
115
- request: async (_abort, { id }: { id: string }) => {
116
- const artifacts = await load()
117
- return artifacts.find(a => a.type === 'hypothesis' && a.id === id) as Hypothesis
118
- },
119
- })
120
-
121
- documents = this.query({
122
- name: 'documents',
123
- request: async (_abort, { hypothesisId }: { hypothesisId: string }) => {
124
- const artifacts = await load()
125
- return artifacts.filter(a => a.type === 'document' && a.hypothesisId === hypothesisId) as Document[]
126
- },
127
- })
128
-
129
- document = this.query({
130
- name: 'document',
131
- request: async (_abort, { id }: { id: string }) => {
132
- const artifacts = await load()
133
- return artifacts.find(a => a.type === 'document' && a.id === id) as Document
134
- },
135
- })
136
-
137
- artifacts = this.query({
138
- name: 'artifacts',
139
- request: async (_abort, { conversationId }: { conversationId: string }) => {
140
- const artifacts = await load()
141
- const opportunities = artifacts.filter(a => a.type === 'opportunity' && a.conversationId === conversationId)
142
- const hypotheses = artifacts.filter(a => a.type === 'hypothesis' && opportunities.some(o => o.id === a.opportunityId))
143
- const documents = artifacts.filter(a => a.type === 'document' && hypotheses.some(h => h.id === a.hypothesisId))
144
- return [...opportunities, ...hypotheses, ...documents]
145
- },
146
- })
147
-
148
- createOpportunity = this.mutation({
149
- name: 'createOpportunity',
150
- request: async (_abort, { conversationId, opportunity }: { opportunity: Resource, conversationId: string }) => {
151
- const artifacts = await load()
152
- const created: Opportunity = { ...createResource(opportunity), type: 'opportunity', conversationId }
153
- artifacts.push(created)
154
- await save(artifacts)
155
- return created
156
- },
157
- })
158
-
159
- createHypothesis = this.mutation({
160
- name: 'createHypothesis',
161
- request: async (
162
- _abort,
163
- { hypothesis, opportunityId, conversationId }: { hypothesis: Resource, opportunityId: string, conversationId: string },
164
- ) => {
165
- const artifacts = await load()
166
- const created: Hypothesis = { ...createResource(hypothesis), type: 'hypothesis', opportunityId, conversationId }
167
- artifacts.push(created)
168
- await save(artifacts)
169
- return created
170
- },
171
- })
172
-
173
- createDocument = this.mutation({
174
- name: 'createDocument',
175
- request: async (_abort, { document, hypothesisId }: { document: Resource, hypothesisId: string }) => {
176
- const artifacts = await load()
177
- const created: Document = { ...createResource(document), type: 'document', hypothesisId }
178
- artifacts.push(created)
179
- await save(artifacts)
180
- return created
181
- },
182
- })
183
-
184
- private deleteArtifact = async (_abort: AbortSignal, { id }: { id: string }) => {
185
- const artifacts = await load()
186
- await save(artifacts.filter(a => a.id !== id))
187
- }
188
-
189
- deleteOpportunity = this.mutation({
190
- name: 'deleteOpportunity',
191
- request: this.deleteArtifact,
192
- })
193
-
194
- deleteHypothesis = this.mutation({
195
- name: 'deleteHypothesis',
196
- request: this.deleteArtifact,
197
- })
198
-
199
- deleteDocument = this.mutation({
200
- name: 'deleteDocument',
201
- request: this.deleteArtifact,
202
- })
203
-
204
- chats = this.query({
205
- name: 'chats',
206
- request: async (_signal, variables: { filter?: string, page?: number, size?: number }) => {
207
- const artifacts = await load()
208
-
209
- const groupedByConversionId: Record<string, any[]> = artifacts.reduce(
210
- (acc, item: any) => {
211
- if (!acc[item.conversationId]) {
212
- acc[item.conversationId] = []
213
- }
214
- acc[item.conversationId].push(item)
215
- return acc
216
- },
217
- {} as Record<string, any[]>,
218
- )
219
-
220
- const chatsHistory = await aiClient.chats.query(
221
- { ...variables, page: variables.page, size: variables.size ?? 40 },
222
- )
223
-
224
- const filteredItems = variables.filter ?
225
- chatsHistory.filter((chat) => chat.title.toLowerCase().includes(variables.filter!.toLowerCase())) : chatsHistory
226
-
227
- const enrichedChats = filteredItems?.map(chat => {
228
- const relatedArtifacts = groupedByConversionId[chat.id]
229
- const opportunity = relatedArtifacts?.find(a => a.type === 'opportunity')
230
- const opportunityName = opportunity?.title ?? null
231
- const hypothesisCount = relatedArtifacts?.filter(a => a.type === 'hypothesis').length ?? 0
232
-
233
- return {
234
- ...chat,
235
- opportunityName,
236
- hypothesisCount,
237
- }
238
- })
239
- return enrichedChats as ChatConversionDetails[]
240
- },
241
- })
242
- }
243
-
244
- 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()