Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
98 changes: 98 additions & 0 deletions lib/artist/updateArtistProfile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { selectAccounts } from "@/lib/supabase/accounts/selectAccounts";
import { updateAccount } from "@/lib/supabase/accounts/updateAccount";
import { selectAccountInfo } from "@/lib/supabase/account_info/selectAccountInfo";
import { updateAccountInfo } from "@/lib/supabase/account_info/updateAccountInfo";
import { insertAccountInfo } from "@/lib/supabase/account_info/insertAccountInfo";
import type { Tables } from "@/types/database.types";

export type Knowledge = {
url: string;
name: string;
type: string;
};

export type ArtistProfile = Tables<"accounts"> & Tables<"account_info">;

/**
* Updates an artist profile (account and account_info).
* All fields are optional except artistId.
*
* @param artistId - The artist account ID
* @param image - Optional profile image URL
* @param name - Optional display name
* @param instruction - Optional custom instructions
* @param label - Optional label/role
* @param knowledges - Optional array of knowledge objects
* @returns The updated artist profile
*/
export async function updateArtistProfile(
artistId: string,
image: string,
name: string,
instruction: string,
label: string,
knowledges: Knowledge[] | null,
): Promise<ArtistProfile> {
// Fetch current account
const currentAccounts = await selectAccounts(artistId);
const currentAccount = currentAccounts[0];
if (!currentAccount) {
throw new Error("Artist does not exist");
}

// Update account name if provided
if (name) {
const accountUpdate = { name };
const updatedAccount = await updateAccount(artistId, accountUpdate);
if (!updatedAccount) {
throw new Error("Failed to update account");
}
}

// Get or create account_info
const accountInfo = await selectAccountInfo(artistId);

if (accountInfo) {
// Update existing account_info
const infoUpdate: Partial<typeof accountInfo> = {};
if (image) infoUpdate.image = image;
if (instruction) infoUpdate.instruction = instruction;

if (knowledges !== null && knowledges !== undefined) {
// Deduplicate knowledges by URL
const uniqueMap = new Map(knowledges.map(k => [k.url, k]));
infoUpdate.knowledges = Array.from(uniqueMap.values());
}

if (label !== undefined) {
infoUpdate.label = label === "" ? null : label;
}

await updateAccountInfo(artistId, infoUpdate);
Comment thread
sweetmantech marked this conversation as resolved.
} else {
// Create new account_info
await insertAccountInfo({
image: image || null,
instruction: instruction || null,
knowledges: knowledges || null,
label: label === "" ? null : label || null,
account_id: artistId,
});
}

// Fetch and return the latest account and account_info
const [updatedAccounts, updatedAccountInfo] = await Promise.all([
selectAccounts(artistId),
selectAccountInfo(artistId),
]);

const account = updatedAccounts[0];
if (!account || !updatedAccountInfo) {
throw new Error("Failed to fetch updated artist profile");
}

return {
...updatedAccountInfo,
...account,
};
}
2 changes: 2 additions & 0 deletions lib/mcp/tools/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { registerAllCatalogTools } from "./catalogs";
import { registerAllSora2Tools } from "./sora2";
import { registerAllSpotifyTools } from "./spotify";
import { registerContactTeamTool } from "./registerContactTeamTool";
import { registerUpdateAccountInfoTool } from "./registerUpdateAccountInfoTool";

/**
* Registers all MCP tools on the server.
Expand All @@ -23,4 +24,5 @@ export const registerAllTools = (server: McpServer): void => {
registerAllSora2Tools(server);
registerAllSpotifyTools(server);
registerContactTeamTool(server);
registerUpdateAccountInfoTool(server);
};
80 changes: 80 additions & 0 deletions lib/mcp/tools/registerUpdateAccountInfoTool.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { z } from "zod";
import { updateArtistProfile } from "@/lib/artist/updateArtistProfile";
import { getToolResultSuccess } from "@/lib/mcp/getToolResultSuccess";
import { getToolResultError } from "@/lib/mcp/getToolResultError";

const updateAccountInfoSchema = z.object({
artistId: z
.string()
.min(1, "Artist account ID is required")
.describe(
"The artist_account_id to update. If not provided, check system prompt for the active artist_account_id.",
),
image: z.string().optional().describe("(Optional) The new profile image URL for the artist."),
name: z.string().optional().describe("(Optional) The display name for the artist."),
instruction: z
.string()
.optional()
.describe("(Optional) Custom instructions for the artist's account."),
label: z.string().optional().describe("(Optional) The label or role for the artist."),
knowledges: z
.array(
z.object({
url: z.string(),
name: z.string(),
type: z
.string()
.describe(
'MIME type of the file, e.g., "text/plain" for TXT files, "application/pdf" for PDFs',
),
}),
)
.optional()
.describe(
"(Optional) Array of knowledge objects ({ url, name, type }) to be stored as the knowledge base or notes for the artist. The 'type' field must be a valid MIME type (e.g., 'text/plain' for TXT files).",
),
});

type UpdateAccountInfoArgs = z.infer<typeof updateAccountInfoSchema>;

/**
* Registers the "update_account_info" tool on the MCP server.
* Updates the account_info record for an artist.
*
* @param server - The MCP server instance to register the tool on.
*/
export function registerUpdateAccountInfoTool(server: McpServer): void {
server.registerTool(
"update_account_info",
{
description: `Update the account_info record for an artist. All fields are optional except for artistId. This tool is used to update the artist's profile image, name, instructions, label, and knowledge base. If artistId is not provided, use the active artist_account_id from the system prompt.`,
inputSchema: updateAccountInfoSchema,
},
async (args: UpdateAccountInfoArgs) => {
try {
const artistProfile = await updateArtistProfile(
args.artistId,
args.image || "",
args.name || "",
args.instruction || "",
args.label || "",
args.knowledges || null,
);

const response = {
success: true,
artistProfile,
message: `Account info updated successfully for account_id: ${artistProfile.id}`,
};

return getToolResultSuccess(response);
} catch (error) {
console.error("Error updating account info:", error);
const errorMessage =
error instanceof Error ? error.message : "Failed to update account info";
return getToolResultError(errorMessage);
}
},
);
}
22 changes: 22 additions & 0 deletions lib/supabase/account_info/selectAccountInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import supabase from "../serverClient";
import type { Tables } from "@/types/database.types";

/**
* Retrieves account_info by account_id.
*
* @param accountId - The account ID
* @returns The account_info record, or null if not found
*/
export async function selectAccountInfo(accountId: string): Promise<Tables<"account_info"> | null> {
const { data, error } = await supabase
.from("account_info")
.select("*")
.eq("account_id", accountId)
.single();

if (error || !data) {
return null;
}

return data;
}
28 changes: 28 additions & 0 deletions lib/supabase/account_info/updateAccountInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import supabase from "../serverClient";
import type { Tables, TablesUpdate } from "@/types/database.types";

/**
* Updates an account_info record.
*
* @param accountId - The account ID
* @param updates - Partial account_info data to update
* @returns The updated account_info record, or null if failed
*/
export async function updateAccountInfo(
accountId: string,
updates: TablesUpdate<"account_info">,
): Promise<Tables<"account_info"> | null> {
const { data, error } = await supabase
.from("account_info")
.update(updates)
.eq("account_id", accountId)
.select("*")
.single();

if (error) {
console.error("[ERROR] updateAccountInfo:", error);
return null;
}

return data || null;
}
18 changes: 18 additions & 0 deletions lib/supabase/accounts/selectAccounts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import supabase from "../serverClient";
import type { Tables } from "@/types/database.types";

/**
* Retrieves accounts by ID.
*
* @param accountId - The account ID
* @returns Array of account records (empty array if not found)
*/
export async function selectAccounts(accountId: string): Promise<Tables<"accounts">[]> {
const { data, error } = await supabase.from("accounts").select("*").eq("id", accountId);

if (error || !data) {
return [];
}

return data;
}
28 changes: 28 additions & 0 deletions lib/supabase/accounts/updateAccount.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import supabase from "../serverClient";
import type { Tables, TablesUpdate } from "@/types/database.types";

/**
* Updates an account record.
*
* @param accountId - The account ID
* @param updates - Partial account data to update
* @returns The updated account record, or null if failed
*/
export async function updateAccount(
accountId: string,
updates: TablesUpdate<"accounts">,
): Promise<Tables<"accounts"> | null> {
const { data, error } = await supabase
.from("accounts")
.update(updates)
.eq("id", accountId)
.select("*")
.single();

if (error) {
console.error("[ERROR] updateAccount:", error);
return null;
}

return data || null;
}