prompt.ts
1 import { defineStore } from 'pinia' 2 import { useMcpStore } from '@/renderer/store/mcp' 3 import { getServers } from './mcp' 4 5 import type { Prompt as PromptType, GetPromptRequest } from '@modelcontextprotocol/sdk/types.d.ts' 6 7 import type { ChatCompletionPromptMessage } from '@/renderer/types/message' 8 9 type ParamsType = GetPromptRequest['params'] 10 11 // Extend the argument content to be sent to the MCP server 12 export type ExtendedArgument = NonNullable<PromptType['arguments']>[number] & { 13 content?: string 14 } 15 16 type ExtendedPromptType = Omit<PromptType, 'arguments'> & { 17 arguments?: ExtendedArgument[] 18 } 19 20 export const usePromptStore = defineStore('promptStore', { 21 state: () => ({ 22 promptDialog: false, 23 promptSheet: false, 24 promptList: [] as ExtendedPromptType[], 25 promptSelect: {} as ExtendedPromptType, 26 search: '', 27 loading: false 28 }), 29 actions: { 30 loadPrompts: function () { 31 this.loading = true 32 try { 33 this.fetchPrompts().then((prompts) => { 34 console.log(prompts) 35 this.promptList = prompts 36 }) 37 } catch (error) { 38 console.error('Failed to load prompts:', error) 39 } finally { 40 this.loading = false 41 } 42 }, 43 fetchPrompts: async function () { 44 const mcpStore = useMcpStore() 45 const mcpServers = mcpStore.getSelected 46 const prompts = await mcpServers.method.list() 47 return prompts.prompts.map((prompt: PromptType) => ({ 48 title: mcpServers.server, 49 ...prompt 50 })) 51 }, 52 fetchAllPrompts: async function () { 53 const mcpServers = getServers() 54 if (!mcpServers) { 55 return [] 56 } 57 const mcpKeys = Object.keys(mcpServers) 58 const allPrompts = [] as PromptType[] 59 for (const key of mcpKeys) { 60 const func = mcpServers[key]?.prompts?.list 61 62 if (func) { 63 try { 64 const obj = await func({ method: 'prompts/list' }) 65 if (obj) { 66 obj.prompts.forEach((prompt) => allPrompts.push({ title: key, ...prompt })) 67 } 68 } catch (error) { 69 console.error(`Error fetching prompts from ${key}:`, error) 70 } 71 } 72 } 73 74 return allPrompts 75 }, 76 select: function (prompt: PromptType) { 77 console.log(prompt.title, prompt.name, prompt.arguments) 78 this.promptSelect = prompt 79 this.promptSheet = true 80 this.promptDialog = false 81 }, 82 fetchSelect: async function () { 83 const mcpStore = useMcpStore() 84 const title = this.promptSelect.title 85 if (!title) { 86 return [] 87 } 88 const getFun = mcpStore.getSelected.method.get 89 if (!getFun) { 90 return [] 91 } 92 const params: ParamsType = { 93 name: this.promptSelect.name 94 } 95 if (this.promptSelect.arguments) { 96 for (const argument of this.promptSelect.arguments) { 97 if (argument.name) { 98 if (!params.arguments) { 99 params.arguments = {} 100 } 101 params.arguments[argument.name] = argument.content as string 102 } 103 } 104 } 105 106 console.log(params) 107 const prompts = await getFun({ method: 'prompts/get', params }) 108 109 const conversations = prompts.messages.map((item: ChatCompletionPromptMessage) => { 110 const content = mcpStore.convertItem(item.content) 111 const conversation = { 112 role: item.role, 113 content: item.role === 'user' ? [content] : content.text 114 } 115 return conversation 116 }) 117 118 this.promptSheet = false 119 120 return conversations 121 } 122 } 123 })