Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable locking available ships - added reduce credit at start of startProject event and in catch handling revert the available ships #28

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions server/controllers/onboardingToolController.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ const searchService = require("../services/searchService");
const { toKebabCase } = require("../utils/file");
const {
insertShip,
getUserProfile,
updateUserProfile,
// getUserProfile,
// updateUserProfile,
updateConversation,
} = require("../services/dbService");
const { TOOLS } = require("../config/tools");

const { nanoid } = require('nanoid');
const { nanoid } = require("nanoid");

const generateProjectFolderName = (projectName) => {
return toKebabCase(projectName) + "-" + nanoid(8);
Expand Down Expand Up @@ -145,12 +145,14 @@ async function handleOnboardingToolUse({
};
const { id } = await insertShip(ship);
console.log("Inserted ship", id);
if (mode === 'paid') {
const profile = await getUserProfile(userId);
const { available_ships } = profile; // current
const profilePayload = { available_ships: available_ships - 1 }; // updated
await updateUserProfile(userId, profilePayload);
}

// removing deduct available ships from here.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can remove the unused code instead of committing, rest looks good

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed commented code. Please review now.

// if (mode === 'paid') {
// const profile = await getUserProfile(userId);
// const { available_ships } = profile; // current
// const profilePayload = { available_ships: available_ships - 1 }; // updated
// await updateUserProfile(userId, profilePayload);
// }
const convPayload = {
ship_id: id,
tokens_used: client.tokensUsed,
Expand Down
22 changes: 20 additions & 2 deletions server/services/onboadingService.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ const {
handleOnboardingToolUse,
} = require("../controllers/onboardingToolController");
const { AnthropicService } = require("../services/anthropicService");
const { getUserProfile, insertMessage } = require("../services/dbService");
const {
getUserProfile,
insertMessage,
updateUserProfile,
} = require("../services/dbService");
const { SHIP_TYPES, DEFAULT_MESSAGES } = require("./constants");

async function processConversation({
Expand Down Expand Up @@ -52,7 +56,6 @@ async function processConversation({
tools.push(searchTool);
messages = [{ role: "user", content: message }];
}

}

try {
Expand Down Expand Up @@ -188,7 +191,14 @@ function handleOnboardingSocketEvents(io) {
};

abortController = new AbortController();
const mode = client.isCustomKey ? "self-key" : "paid";
try {
if (mode === "paid") {
const profile = await getUserProfile(userId);
const { available_ships } = profile; // current
const profilePayload = { available_ships: available_ships - 1 }; // updated
await updateUserProfile(userId, profilePayload);
}
await processConversation({
client,
sendEvent,
Expand All @@ -206,6 +216,14 @@ function handleOnboardingSocketEvents(io) {
sendEvent("creationAborted", {
message: "Website creation was aborted",
});

// revert available ships in case of paid mode
if (mode === "paid") {
const profile = await getUserProfile(userId);
const { available_ships } = profile;
const profilePayload = { available_ships: available_ships + 1 }; // updated
await updateUserProfile(userId, profilePayload);
}
} else {
console.error("Error in processConversation:", error);
}
Expand Down