Skip to content

Commit

Permalink
Modjo using oauth end
Browse files Browse the repository at this point in the history
  • Loading branch information
albandum committed Dec 18, 2024
1 parent 7aea36a commit e1ceeeb
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 27 deletions.
3 changes: 1 addition & 2 deletions front/pages/api/w/[wId]/labs/transcripts/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { WithAPIErrorResponse } from "@dust-tt/types";
import { encrypt, OAuthAPI } from "@dust-tt/types";
import { OAuthAPI } from "@dust-tt/types";
import { isLeft } from "fp-ts/lib/Either";
import * as t from "io-ts";
import * as reporter from "io-ts-reporters";
Expand Down Expand Up @@ -37,7 +37,6 @@ const ApiKeyConfig = t.intersection([
BaseConfiguration,
t.type({
apiKey: t.string,
apiKeyIsEncrypted: t.union([t.boolean, t.undefined]),
}),
]);

Expand Down
7 changes: 1 addition & 6 deletions front/pages/w/[wId]/assistant/labs/transcripts/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,7 @@ export default function LabsTranscriptsIndex({

const getDefaultConfiguration = useGetDefaultConfiguration({ owner });

const saveApiConnection = async (
apiKey: string,
provider: string,
apiKeyIsEncrypted: boolean = false
) => {
const saveApiConnection = async (apiKey: string, provider: string) => {
const response = await fetch(`/api/w/${owner.sId}/labs/transcripts`, {
method: "POST",
headers: {
Expand All @@ -141,7 +137,6 @@ export default function LabsTranscriptsIndex({
body: JSON.stringify({
apiKey,
provider,
apiKeyIsEncrypted,
}),
});

Expand Down
60 changes: 43 additions & 17 deletions front/temporal/labs/utils/modjo.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { decrypt } from "@dust-tt/types";
import { isModjoCredentials, OAuthAPI } from "@dust-tt/types";
import { either } from "fp-ts";
import { pipe } from "fp-ts/function";
import * as t from "io-ts";

import config from "@app/lib/api/config";
import type { Authenticator } from "@app/lib/auth";
import { getFeatureFlags } from "@app/lib/auth";
import type { LabsTranscriptsConfigurationResource } from "@app/lib/resources/labs_transcripts_resource";
import type { Logger } from "@app/logger/logger";
import logger from "@app/logger/logger";

const ModjoSpeaker = t.type({
contactId: t.union([t.number, t.undefined]),
Expand Down Expand Up @@ -110,10 +112,25 @@ export async function retrieveModjoTranscripts(
}

const workspace = auth.getNonNullableWorkspace();
const modjoApiKey = decrypt(
transcriptsConfiguration.credentialId,
workspace.sId
);
const oauthApi = new OAuthAPI(config.getOAuthAPIConfig(), logger);

const modjoApiKeyRes = await oauthApi.getCredentials({
credentialsId: transcriptsConfiguration.credentialId,
});

if (modjoApiKeyRes.isErr()) {
localLogger.error(
{ error: modjoApiKeyRes.error },
"[retrieveModjoTranscripts] Error fetching API key from Modjo. Skipping."
);
return [];
}

if (!isModjoCredentials(modjoApiKeyRes.value.credential.content)) {
throw new Error("Invalid credentials type - expected modjo credentials");
}

const modjoApiKey = modjoApiKeyRes.value.credential.content.api_key;

const flags = await getFeatureFlags(workspace);
const daysOfHistory = flags.includes("labs_transcripts_gong_full_storage")
Expand Down Expand Up @@ -234,21 +251,30 @@ export async function retrieveModjoTranscriptContent(
userParticipated: boolean;
} | null> {
if (!transcriptsConfiguration || !transcriptsConfiguration.credentialId) {
localLogger.error(
{
fileId,
transcriptsConfigurationId: transcriptsConfiguration.id,
},
"[processTranscriptActivity] No API key found. Skipping."
throw new Error(
"[processTranscriptActivity]No credentialId for modjo transcriptsConfiguration found. Skipping."
);
throw new Error("No API key for transcriptsConfiguration found. Skipping.");
}

const workspace = auth.getNonNullableWorkspace();
const modjoApiKey = decrypt(
transcriptsConfiguration.credentialId,
workspace.sId
);
const oauthApi = new OAuthAPI(config.getOAuthAPIConfig(), logger);

const modjoApiKeyRes = await oauthApi.getCredentials({
credentialsId: transcriptsConfiguration.credentialId,
});

if (modjoApiKeyRes.isErr()) {
throw new Error(
"[processTranscriptActivity] Error fetching API key from Modjo. Skipping."
);
}

if (!isModjoCredentials(modjoApiKeyRes.value.credential.content)) {
throw new Error(
"[processTranscriptActivity] Invalid credentials type - expected modjo credentials"
);
}

const modjoApiKey = modjoApiKeyRes.value.credential.content.api_key;

const findModjoUser = async () => {
const user = await transcriptsConfiguration.getUser();
Expand Down
22 changes: 20 additions & 2 deletions types/src/oauth/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export function isValidZendeskSubdomain(s: unknown): s is string {

// Credentials Providers

export const CREDENTIALS_PROVIDERS = ["snowflake"] as const;
export const CREDENTIALS_PROVIDERS = ["snowflake", "modjo"] as const;
export type CredentialsProvider = (typeof CREDENTIALS_PROVIDERS)[number];

export function isCredentialProvider(obj: unknown): obj is CredentialsProvider {
Expand All @@ -73,7 +73,25 @@ export const SnowflakeCredentialsSchema = t.type({
warehouse: t.string,
});
export type SnowflakeCredentials = t.TypeOf<typeof SnowflakeCredentialsSchema>;
export type ConnectionCredentials = SnowflakeCredentials;

export const ApiKeyCredentialsSchema = t.type({
api_key: t.string,
});
export type ModjoCredentials = t.TypeOf<typeof ApiKeyCredentialsSchema>;

export type ConnectionCredentials = SnowflakeCredentials | ModjoCredentials;

export function isSnowflakeCredentials(
credentials: ConnectionCredentials
): credentials is SnowflakeCredentials {
return "username" in credentials && "password" in credentials;
}

export function isModjoCredentials(
credentials: ConnectionCredentials
): credentials is ModjoCredentials {
return "api_key" in credentials;
}

// POST Credentials

Expand Down

0 comments on commit e1ceeeb

Please sign in to comment.