1
- import type { AnyOrama , Results , SearchParams , Nullable , IAnswerSessionConfig as OSSAnswerSessionConfig } from '@orama/orama'
2
- import { search , AnswerSession as OSSAnswerSession } from '@orama/orama'
3
- import { OramaClient , ClientSearchParams , AnswerSessionParams as CloudAnswerSessionConfig , AnswerSession as CloudAnswerSession } from '@oramacloud/client'
1
+ import type {
2
+ AnyOrama ,
3
+ IAnswerSessionConfig as OSSAnswerSessionConfig ,
4
+ Nullable ,
5
+ Results ,
6
+ SearchParams ,
7
+ } from "@orama/orama" ;
8
+ import { AnswerSession as OSSAnswerSession , search } from "@orama/orama" ;
9
+ import {
10
+ AnswerSession as CloudAnswerSession ,
11
+ AnswerSessionParams as CloudAnswerSessionConfig ,
12
+ ClientSearchParams ,
13
+ OramaClient ,
14
+ } from "@oramacloud/client" ;
15
+ import type {
16
+ AnswerSession as OramaCoreAnswerSession ,
17
+ AnswerSessionConfig as OramaCoreAnswerSessionConfig ,
18
+ SearchParams as OramaCoreSearchParams ,
19
+ } from "@orama/core" ;
20
+ import { CollectionManager } from "@orama/core" ;
4
21
5
- export type OramaSwitchClient = AnyOrama | OramaClient
22
+ export type OramaSwitchClient = AnyOrama | OramaClient | CollectionManager ;
6
23
7
- export type ClientType = ' oss' | ' cloud'
24
+ export type ClientType = " oss" | " cloud" | "core" ;
8
25
9
26
export type SearchConfig = {
10
- abortController ?: AbortController
11
- fresh ?: boolean
12
- debounce ?: number
13
- }
27
+ abortController ?: AbortController ;
28
+ fresh ?: boolean ;
29
+ debounce ?: number ;
30
+ } ;
14
31
15
32
function isOramaClient ( client : any ) : client is OramaClient {
16
- return client && typeof client === 'object' && 'api_key' in client && 'endpoint' in client ;
33
+ return client && typeof client === "object" && "api_key" in client &&
34
+ "endpoint" in client ;
35
+ }
36
+
37
+ function isOramaCoreClient ( client : any ) : client is CollectionManager {
38
+ return client && client instanceof CollectionManager ;
39
+ }
40
+
41
+ function isOramaJSClient ( client : any ) : client is AnyOrama {
42
+ return client && typeof client === "object" && "id" in client &&
43
+ "tokenizer" in client ;
17
44
}
18
45
19
46
export class Switch < T = OramaSwitchClient > {
20
- private invalidClientError = 'Invalid client. Expected either an OramaClient or an Orama OSS database.'
21
- client : OramaSwitchClient
22
- clientType : ClientType
23
- isCloud : boolean = false
24
- isOSS : boolean = false
47
+ private invalidClientError =
48
+ "Invalid client. Expected either an OramaClient, CollectionManager, or an Orama JS database." ;
49
+ client : OramaSwitchClient ;
50
+ clientType : ClientType ;
51
+ isCloud : boolean = false ;
52
+ isJS : boolean = false ;
53
+ isCore : boolean = false ;
25
54
26
55
constructor ( client : OramaSwitchClient ) {
27
- this . client = client
28
-
29
- if ( isOramaClient ( client ) ) {
30
- this . clientType = 'cloud'
31
- this . isCloud = true
32
- } else if ( typeof client === 'object' && 'id' in client && 'tokenizer' in client ) {
33
- this . clientType = 'oss'
34
- this . isOSS = true
35
- } else {
36
- throw new Error ( this . invalidClientError )
56
+ this . client = client ;
57
+
58
+ switch ( true ) {
59
+ case isOramaClient ( client ) :
60
+ this . clientType = "cloud" ;
61
+ this . isCloud = true ;
62
+ break ;
63
+ case isOramaCoreClient ( client ) :
64
+ this . clientType = "core" ;
65
+ this . isCore = true ;
66
+ break ;
67
+ case isOramaJSClient ( client ) :
68
+ this . clientType = "oss" ;
69
+ this . isJS = true ;
70
+ break ;
71
+ default :
72
+ throw new Error ( this . invalidClientError ) ;
37
73
}
38
74
}
39
75
40
76
async search < R = unknown > (
41
- params : T extends OramaClient ? ClientSearchParams : SearchParams < AnyOrama > ,
42
- config ?: SearchConfig
77
+ params : T extends OramaClient ? ClientSearchParams
78
+ : T extends CollectionManager ? SearchParams < AnyOrama >
79
+ : never ,
80
+ config ?: SearchConfig ,
43
81
) : Promise < Nullable < Results < R > > > {
44
- if ( this . isCloud ) {
45
- return ( this . client as OramaClient ) . search ( params as T extends OramaClient ? ClientSearchParams : never , config )
46
- } else {
47
- return search ( this . client as AnyOrama , params as SearchParams < AnyOrama > ) as Promise < Nullable < Results < R > > >
82
+ switch ( true ) {
83
+ // OramaCloud - Old client
84
+ case this . isCloud :
85
+ return ( this . client as OramaClient ) . search (
86
+ params as T extends OramaClient ? ClientSearchParams : never ,
87
+ config ,
88
+ ) ;
89
+
90
+ // OramaCore - New client
91
+ case this . isCore : {
92
+ const results = await ( this . client as CollectionManager ) . search (
93
+ params as T extends CollectionManager ? OramaCoreSearchParams : never ,
94
+ ) ;
95
+
96
+ return results as unknown as Nullable < Results < R > > ;
97
+ }
98
+
99
+ // OramaJS - JavaScript client
100
+ case this . isJS :
101
+ return search (
102
+ this . client as AnyOrama ,
103
+ params as SearchParams < AnyOrama > ,
104
+ ) as Promise < Nullable < Results < R > > > ;
105
+ default :
106
+ throw new Error ( this . invalidClientError ) ;
48
107
}
49
108
}
50
109
51
- createAnswerSession ( params : T extends OramaClient ? CloudAnswerSessionConfig : OSSAnswerSessionConfig ) : T extends OramaClient ? CloudAnswerSession < true > : OSSAnswerSession {
52
- if ( this . isCloud ) {
53
- const p = params as CloudAnswerSessionConfig
54
- return ( this . client as OramaClient ) . createAnswerSession ( p ) as unknown as T extends OramaClient ? CloudAnswerSession < true > : OSSAnswerSession
55
- }
110
+ createAnswerSession (
111
+ params : T extends OramaClient ? CloudAnswerSessionConfig
112
+ : OSSAnswerSessionConfig ,
113
+ ) : T extends OramaClient ? CloudAnswerSession < true >
114
+ : T extends CollectionManager ? OramaCoreAnswerSession
115
+ : OSSAnswerSession {
116
+ switch ( true ) {
117
+ // OramaCloud - Old client
118
+ case this . isCloud : {
119
+ const p = params as CloudAnswerSessionConfig ;
120
+ return ( this . client as OramaClient ) . createAnswerSession (
121
+ p ,
122
+ ) as unknown as T extends OramaClient ? CloudAnswerSession < true >
123
+ : T extends CollectionManager ? OramaCoreAnswerSession
124
+ : OSSAnswerSession ;
125
+ }
126
+
127
+ // OramaCore - New client
128
+ case this . isCore : {
129
+ const p = params as OramaCoreAnswerSessionConfig ;
130
+ return ( this . client as CollectionManager ) . createAnswerSession (
131
+ p ,
132
+ ) as unknown as T extends OramaClient ? CloudAnswerSession < true >
133
+ : T extends CollectionManager ? OramaCoreAnswerSession
134
+ : OSSAnswerSession ;
135
+ }
56
136
57
- if ( this . isOSS ) {
58
- const p = params as OSSAnswerSessionConfig
59
- return new OSSAnswerSession ( this . client as AnyOrama , {
137
+ // OramaJS - JavaScript client
138
+ case this . isJS : {
139
+ const p = params as OSSAnswerSessionConfig ;
140
+ return new OSSAnswerSession ( this . client as AnyOrama , {
60
141
conversationID : p . conversationID ,
61
142
initialMessages : p . initialMessages ,
62
143
events : p . events ,
63
144
userContext : p . userContext ,
64
145
systemPrompt : p . systemPrompt ,
65
- } ) as unknown as T extends OramaClient ? CloudAnswerSession < true > : OSSAnswerSession
66
- }
146
+ } ) as unknown as T extends OramaClient ? CloudAnswerSession < true >
147
+ : T extends CollectionManager ? OramaCoreAnswerSession
148
+ : OSSAnswerSession ;
149
+ }
67
150
68
- throw new Error ( this . invalidClientError )
151
+ default :
152
+ throw new Error ( this . invalidClientError ) ;
153
+ }
69
154
}
70
- }
155
+ }
0 commit comments