Skip to content

Commit

Permalink
Fix: Switch OramaClient condition + update dependency version (#845)
Browse files Browse the repository at this point in the history
  • Loading branch information
raiindev authored Nov 22, 2024
2 parents 9051b71 + 2cc8ec8 commit 74511b6
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
2 changes: 1 addition & 1 deletion packages/switch/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
},
"peerDependencies": {
"@orama/orama": "workspace:*",
"@oramacloud/client": "1.3.15"
"@oramacloud/client": "^2.1.1"
},
"license": "Apache-2.0",
"devDependencies": {
Expand Down
12 changes: 8 additions & 4 deletions packages/switch/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ export type SearchConfig = {
debounce?: number
}

function isOramaClient(client: any): client is OramaClient {
return client && typeof client === 'object' && 'api_key' in client && 'endpoint' in client;
}

export class Switch<T = OramaSwitchClient> {
private invalidClientError = 'Invalid client. Expected either an OramaClient or an Orama OSS database.'
client: OramaSwitchClient
Expand All @@ -22,7 +26,7 @@ export class Switch<T = OramaSwitchClient> {
constructor(client: OramaSwitchClient) {
this.client = client

if (client instanceof OramaClient) {
if (isOramaClient(client)) {
this.clientType = 'cloud'
this.isCloud = true
} else if (typeof client === 'object' && 'id' in client && 'tokenizer' in client) {
Expand All @@ -44,10 +48,10 @@ export class Switch<T = OramaSwitchClient> {
}
}

createAnswerSession(params: T extends OramaClient ? CloudAnswerSessionConfig : OSSAnswerSessionConfig): T extends OramaClient ? CloudAnswerSession : OSSAnswerSession {
createAnswerSession(params: T extends OramaClient ? CloudAnswerSessionConfig : OSSAnswerSessionConfig): T extends OramaClient ? CloudAnswerSession<true> : OSSAnswerSession {
if (this.isCloud) {
const p = params as CloudAnswerSessionConfig
return (this.client as OramaClient).createAnswerSession(p) as unknown as T extends OramaClient ? CloudAnswerSession : OSSAnswerSession
return (this.client as OramaClient).createAnswerSession(p) as unknown as T extends OramaClient ? CloudAnswerSession<true> : OSSAnswerSession
}

if (this.isOSS) {
Expand All @@ -58,7 +62,7 @@ export class Switch<T = OramaSwitchClient> {
events: p.events,
userContext: p.userContext,
systemPrompt: p.systemPrompt,
}) as unknown as T extends OramaClient ? CloudAnswerSession : OSSAnswerSession
}) as unknown as T extends OramaClient ? CloudAnswerSession<true> : OSSAnswerSession
}

throw new Error(this.invalidClientError)
Expand Down
32 changes: 27 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 74511b6

Please sign in to comment.