From c333d4b9ada1accf384e46d453966f2f659948de Mon Sep 17 00:00:00 2001 From: Sweets Sweetman Date: Sat, 13 Dec 2025 13:33:00 -0500 Subject: [PATCH] MCP - update_account_info --- lib/artist/updateArtistProfile.ts | 98 +++++++++++++++++++ lib/mcp/tools/index.ts | 2 + .../tools/registerUpdateAccountInfoTool.ts | 80 +++++++++++++++ .../account_info/selectAccountInfo.ts | 22 +++++ .../account_info/updateAccountInfo.ts | 28 ++++++ lib/supabase/accounts/selectAccounts.ts | 18 ++++ lib/supabase/accounts/updateAccount.ts | 28 ++++++ 7 files changed, 276 insertions(+) create mode 100644 lib/artist/updateArtistProfile.ts create mode 100644 lib/mcp/tools/registerUpdateAccountInfoTool.ts create mode 100644 lib/supabase/account_info/selectAccountInfo.ts create mode 100644 lib/supabase/account_info/updateAccountInfo.ts create mode 100644 lib/supabase/accounts/selectAccounts.ts create mode 100644 lib/supabase/accounts/updateAccount.ts diff --git a/lib/artist/updateArtistProfile.ts b/lib/artist/updateArtistProfile.ts new file mode 100644 index 000000000..9c7cbd0f0 --- /dev/null +++ b/lib/artist/updateArtistProfile.ts @@ -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 { + // 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 = {}; + 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); + } 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, + }; +} diff --git a/lib/mcp/tools/index.ts b/lib/mcp/tools/index.ts index b3fa8ab3a..219bfba56 100644 --- a/lib/mcp/tools/index.ts +++ b/lib/mcp/tools/index.ts @@ -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. @@ -23,4 +24,5 @@ export const registerAllTools = (server: McpServer): void => { registerAllSora2Tools(server); registerAllSpotifyTools(server); registerContactTeamTool(server); + registerUpdateAccountInfoTool(server); }; diff --git a/lib/mcp/tools/registerUpdateAccountInfoTool.ts b/lib/mcp/tools/registerUpdateAccountInfoTool.ts new file mode 100644 index 000000000..9520ead1f --- /dev/null +++ b/lib/mcp/tools/registerUpdateAccountInfoTool.ts @@ -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; + +/** + * 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); + } + }, + ); +} diff --git a/lib/supabase/account_info/selectAccountInfo.ts b/lib/supabase/account_info/selectAccountInfo.ts new file mode 100644 index 000000000..85499020a --- /dev/null +++ b/lib/supabase/account_info/selectAccountInfo.ts @@ -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 | null> { + const { data, error } = await supabase + .from("account_info") + .select("*") + .eq("account_id", accountId) + .single(); + + if (error || !data) { + return null; + } + + return data; +} diff --git a/lib/supabase/account_info/updateAccountInfo.ts b/lib/supabase/account_info/updateAccountInfo.ts new file mode 100644 index 000000000..3b96eccdf --- /dev/null +++ b/lib/supabase/account_info/updateAccountInfo.ts @@ -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 | 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; +} diff --git a/lib/supabase/accounts/selectAccounts.ts b/lib/supabase/accounts/selectAccounts.ts new file mode 100644 index 000000000..21fa53fbb --- /dev/null +++ b/lib/supabase/accounts/selectAccounts.ts @@ -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[]> { + const { data, error } = await supabase.from("accounts").select("*").eq("id", accountId); + + if (error || !data) { + return []; + } + + return data; +} diff --git a/lib/supabase/accounts/updateAccount.ts b/lib/supabase/accounts/updateAccount.ts new file mode 100644 index 000000000..53e835e05 --- /dev/null +++ b/lib/supabase/accounts/updateAccount.ts @@ -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 | 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; +}