|
| 1 | +import { GMAIL_API_BASE } from '@/tools/gmail/utils' |
| 2 | +import type { ToolConfig } from '@/tools/types' |
| 3 | + |
| 4 | +interface GmailCreateLabelParams { |
| 5 | + accessToken: string |
| 6 | + name: string |
| 7 | + messageListVisibility?: string |
| 8 | + labelListVisibility?: string |
| 9 | +} |
| 10 | + |
| 11 | +interface GmailCreateLabelResponse { |
| 12 | + success: boolean |
| 13 | + output: { |
| 14 | + id: string |
| 15 | + name: string |
| 16 | + messageListVisibility?: string |
| 17 | + labelListVisibility?: string |
| 18 | + type?: string |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +export const gmailCreateLabelV2Tool: ToolConfig<GmailCreateLabelParams, GmailCreateLabelResponse> = |
| 23 | + { |
| 24 | + id: 'gmail_create_label_v2', |
| 25 | + name: 'Gmail Create Label', |
| 26 | + description: 'Create a new label in Gmail', |
| 27 | + version: '2.0.0', |
| 28 | + |
| 29 | + oauth: { |
| 30 | + required: true, |
| 31 | + provider: 'google-email', |
| 32 | + }, |
| 33 | + |
| 34 | + params: { |
| 35 | + accessToken: { |
| 36 | + type: 'string', |
| 37 | + required: true, |
| 38 | + visibility: 'hidden', |
| 39 | + description: 'Access token for Gmail API', |
| 40 | + }, |
| 41 | + name: { |
| 42 | + type: 'string', |
| 43 | + required: true, |
| 44 | + visibility: 'user-or-llm', |
| 45 | + description: 'Display name for the new label', |
| 46 | + }, |
| 47 | + messageListVisibility: { |
| 48 | + type: 'string', |
| 49 | + required: false, |
| 50 | + visibility: 'user-or-llm', |
| 51 | + description: 'Visibility of messages with this label in the message list (show or hide)', |
| 52 | + }, |
| 53 | + labelListVisibility: { |
| 54 | + type: 'string', |
| 55 | + required: false, |
| 56 | + visibility: 'user-or-llm', |
| 57 | + description: |
| 58 | + 'Visibility of the label in the label list (labelShow, labelShowIfUnread, or labelHide)', |
| 59 | + }, |
| 60 | + }, |
| 61 | + |
| 62 | + request: { |
| 63 | + url: () => `${GMAIL_API_BASE}/labels`, |
| 64 | + method: 'POST', |
| 65 | + headers: (params: GmailCreateLabelParams) => ({ |
| 66 | + Authorization: `Bearer ${params.accessToken}`, |
| 67 | + 'Content-Type': 'application/json', |
| 68 | + }), |
| 69 | + body: (params: GmailCreateLabelParams) => { |
| 70 | + const body: Record<string, string> = { name: params.name } |
| 71 | + if (params.messageListVisibility) { |
| 72 | + body.messageListVisibility = params.messageListVisibility |
| 73 | + } |
| 74 | + if (params.labelListVisibility) { |
| 75 | + body.labelListVisibility = params.labelListVisibility |
| 76 | + } |
| 77 | + return body |
| 78 | + }, |
| 79 | + }, |
| 80 | + |
| 81 | + transformResponse: async (response: Response) => { |
| 82 | + const data = await response.json() |
| 83 | + |
| 84 | + if (!response.ok) { |
| 85 | + return { |
| 86 | + success: false, |
| 87 | + output: { id: '', name: '' }, |
| 88 | + error: data.error?.message || 'Failed to create label', |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + return { |
| 93 | + success: true, |
| 94 | + output: { |
| 95 | + id: data.id, |
| 96 | + name: data.name, |
| 97 | + messageListVisibility: data.messageListVisibility ?? null, |
| 98 | + labelListVisibility: data.labelListVisibility ?? null, |
| 99 | + type: data.type ?? null, |
| 100 | + }, |
| 101 | + } |
| 102 | + }, |
| 103 | + |
| 104 | + outputs: { |
| 105 | + id: { type: 'string', description: 'Label ID' }, |
| 106 | + name: { type: 'string', description: 'Label display name' }, |
| 107 | + messageListVisibility: { |
| 108 | + type: 'string', |
| 109 | + description: 'Visibility of messages with this label', |
| 110 | + optional: true, |
| 111 | + }, |
| 112 | + labelListVisibility: { |
| 113 | + type: 'string', |
| 114 | + description: 'Visibility of the label in the label list', |
| 115 | + optional: true, |
| 116 | + }, |
| 117 | + type: { type: 'string', description: 'Label type (system or user)', optional: true }, |
| 118 | + }, |
| 119 | + } |
0 commit comments