Skip to content

Commit

Permalink
chore(assistant): only write to slack in prod
Browse files Browse the repository at this point in the history
  • Loading branch information
krisantrobus committed Jan 21, 2025
1 parent 2e22b6a commit c0306ee
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 57 deletions.
76 changes: 40 additions & 36 deletions packages/paste-website/src/pages/api/paste-assistant-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ const supabaseUrl = process.env.SUPABASE_URL;
const supabaseServiceKey = process.env.SUPABASE_KEY;
const slackChannelID = process.env.SLACK_CHANNEL_TMP_PASTE_ASSISTANT;

const isDev = process.env.NODE_ENV === "development";

const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY, // defaults to process.env["OPENAI_API_KEY"]
});
Expand Down Expand Up @@ -94,7 +96,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
return;
}

if (slackChannelID === undefined || slackChannelID === "") {
if ((slackChannelID === undefined || slackChannelID === "") && !isDev) {
logger.error(`${LOG_PREFIX} Slack channel ID is undefined`);
rollbar.error(`${LOG_PREFIX} Slack channel ID is undefined`);
res.status(500).json({
Expand Down Expand Up @@ -140,42 +142,44 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
});
}

// get the slack thread ID for the message thread being updated so that we can post the new message to the correct slack thread
const supabaseClient = createClient(supabaseUrl, supabaseServiceKey);
const { data: slackThreadID, error: slackThreadIDError } = await supabaseClient
.from("assistant_threads")
.select("slack_thread_ts")
.eq("openai_thread_id", threadId);

if (slackThreadIDError || slackThreadID == null) {
logger.error(`${LOG_PREFIX} Error getting slack thread ID for the message thread being updated`, {
slackThreadIDError,
});
} else {
try {
// Post that new message to the correct slack thread for this assistant thread
const postToSlackResponse = await fetch(`${protocol}://${host}/api/post-to-slack`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
message: `New message was added to the thread: \n\n --- \n\n${message}`,
channelID: slackChannelID,
threadID: slackThreadID,
}),
});
const slackResponseJSON = await postToSlackResponse.json();
const slackResult = slackResponseJSON.result as ChatPostMessageResponse;

logger.info(`${LOG_PREFIX} Posted to slack`, {
slackMessageCreated: slackResult.ts,
message: slackResult.message?.text,
});
} catch (error) {
logger.error(`${LOG_PREFIX} Error sending slack message for thread being updated`, {
error,
if (!isDev) {
// get the slack thread ID for the message thread being updated so that we can post the new message to the correct slack thread
const supabaseClient = createClient(supabaseUrl, supabaseServiceKey);
const { data: slackThreadID, error: slackThreadIDError } = await supabaseClient
.from("assistant_threads")
.select("slack_thread_ts")
.eq("openai_thread_id", threadId);

if (slackThreadIDError || slackThreadID == null) {
logger.error(`${LOG_PREFIX} Error getting slack thread ID for the message thread being updated`, {
slackThreadIDError,
});
} else {
try {
// Post that new message to the correct slack thread for this assistant thread
const postToSlackResponse = await fetch(`${protocol}://${host}/api/post-to-slack`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
message: `New message was added to the thread: \n\n --- \n\n${message}`,
channelID: slackChannelID,
threadID: slackThreadID,
}),
});
const slackResponseJSON = await postToSlackResponse.json();
const slackResult = slackResponseJSON.result as ChatPostMessageResponse;

logger.info(`${LOG_PREFIX} Posted to slack`, {
slackMessageCreated: slackResult.ts,
message: slackResult.message?.text,
});
} catch (error) {
logger.error(`${LOG_PREFIX} Error sending slack message for thread being updated`, {
error,
});
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const supabaseUrl = process.env.SUPABASE_URL;
const supabaseServiceKey = process.env.SUPABASE_KEY;
const slackChannelID = process.env.SLACK_CHANNEL_TMP_PASTE_ASSISTANT;

const isDev = process.env.NODE_ENV === "development";

const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY, // defaults to process.env["OPENAI_API_KEY"]
});
Expand Down Expand Up @@ -73,39 +75,43 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
throw new ApplicationError("Missing environment variable SUPABASE_KEY");
}

if (!slackChannelID) {
if (!slackChannelID && !isDev) {
throw new ApplicationError("Missing environment variable SLACK_CHANNEL_TMP_PASTE_ASSISTANT");
}

const newThread = await createThread();
logger.info(`${LOG_PREFIX} Created thread`, { newThread });

// post to slack
const postToSlackResponse = await fetch(`${protocol}://${host}/api/post-to-slack`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
message: `New Paste Assistant thread created: ${newThread.id}`,
channelID: slackChannelID,
}),
});

const slackResponseJSON = await postToSlackResponse.json();
const slackResult = slackResponseJSON.result as ChatPostMessageResponse;

logger.info(`${LOG_PREFIX} Posted to slack`, {
slackMessageCreated: slackResult.ts,
message: slackResult.message?.text,
});
let slackResult: ChatPostMessageResponse | undefined;

if (!isDev) {
// post to slack
const postToSlackResponse = await fetch(`${protocol}://${host}/api/post-to-slack`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
message: `New Paste Assistant thread created: ${newThread.id}`,
channelID: slackChannelID,
}),
});

const slackResponseJSON = await postToSlackResponse.json();
slackResult = slackResponseJSON.result as ChatPostMessageResponse;

logger.info(`${LOG_PREFIX} Posted to slack`, {
slackMessageCreated: slackResult.ts,
message: slackResult.message?.text,
});
}

// save to supabase
const supabaseClient = createClient(supabaseUrl, supabaseServiceKey);
const { data: newAssistantThreadData, error: newAssistantThreadError } = await supabaseClient
.from("assistant_threads")
// eslint-disable-next-line camelcase
.insert([{ openai_thread_id: newThread.id, slack_thread_ts: slackResult.ts }]);
.insert([{ openai_thread_id: newThread.id, slack_thread_ts: slackResult?.ts }]);

if (newAssistantThreadError) {
throw new ApplicationError("Failed to store new thread", newAssistantThreadError);
Expand Down

0 comments on commit c0306ee

Please sign in to comment.