forked from tanaikech/ToolsForMCPServer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuse_gemini.js
More file actions
282 lines (268 loc) · 11.7 KB
/
use_gemini.js
File metadata and controls
282 lines (268 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/**
* Functions using Gemini
* Updated on 20250724 11:30
*/
/**
* This function generates a description of the file and set it to the file.
* @private
*/
function generate_description_on_google_drive(object = {}) {
const { fileId } = object;
let result;
try {
if (fileId) {
const file = DriveApp.getFileById(fileId);
if (file.getMimeType() != MimeType.FOLDER) {
const g = new GeminiWithFiles({ apiKey });
const fileList = g.setFileIds([fileId]).uploadFiles();
const res = g.withUploadedFilesByGenerateContent(fileList).generateContent({ q: "Describe this file." });
g.deleteFiles(fileList.map(({ name }) => name));
file.setDescription(res);
result = { content: [{ type: "text", text: `Created description is "${res}". This was set to "${file.getName()}".` }], isError: false };
} else {
result = { content: [{ type: "text", text: `The inputted file ID is the folder ID. Please set the file ID of the file.` }], isError: true };
}
} else {
result = { content: [{ type: "text", text: `No file ID.` }], isError: true };
}
} catch ({ stack }) {
result = { content: [{ type: "text", text: stack }], isError: true };
}
console.log(result); // Check response.
return { jsonrpc: "2.0", result };
}
/**
* This function generates an image file from a prompt using Gemini API.
* @private
*/
function generate_image_on_google_drive(object = {}) {
const { prompt } = object;
let result;
try {
if (prompt) {
const g = new GeminiWithFiles({
apiKey,
exportRawData: true,
model: "models/gemini-2.0-flash-exp",
generationConfig: { responseModalities: ["TEXT", "IMAGE"] },
});
const res = g.generateContent({ q: prompt });
const imageObj = res.candidates[0].content.parts.find((e) => e.inlineData);
const imageBlob = Utilities.newBlob(
Utilities.base64Decode(imageObj.inlineData.data),
imageObj.inlineData.mimeType
);
const file = DriveApp.createFile(imageBlob.setName(prompt)).setDescription(`This image file was generated from a prompt of "${prompt}".`);
result = { content: [{ type: "text", text: `An image was successfully generated from a prompt "${prompt}" as a file on Google Drive. The file URL and ID are "${file.getUrl()}" and "${file.getId()}", respectively.` }], isError: false };
} else {
result = { content: [{ type: "text", text: `No prompt for generating image.` }], isError: true };
}
} catch ({ stack }) {
result = { content: [{ type: "text", text: stack }], isError: true };
}
console.log(result); // Check response.
return { jsonrpc: "2.0", result };
}
/**
* This function summarizes a file using Gemini API.
* @private
*/
function summarize_file_on_google_drive(object = {}) {
const { fileId, prompt = "Describe this file." } = object;
let result;
try {
if (fileId) {
const file = DriveApp.getFileById(fileId);
const mimeType = file.getMimeType();
if (mimeType != MimeType.FOLDER) {
const g = new GeminiWithFiles({ apiKey });
let fileList = [];
if (mimeType.includes("application/vnd.google-apps")) {
fileList = g.setBlobs([file.getBlob()]).uploadFiles();
} else {
fileList = g.setFileIds([fileId]).uploadFiles();
}
const text = g.withUploadedFilesByGenerateContent(fileList).generateContent({ q: prompt });
g.deleteFiles(fileList.map(({ name }) => name));
result = { content: [{ type: "text", text }], isError: false };
} else {
result = { content: [{ type: "text", text: `The inputted file ID is the folder ID. Please set the file ID of the file.` }], isError: true };
}
} else {
result = { content: [{ type: "text", text: `No file ID.` }], isError: true };
}
} catch ({ stack }) {
result = { content: [{ type: "text", text: stack }], isError: true };
}
console.log(result); // Check response.
return { jsonrpc: "2.0", result };
}
/**
* This function generates a roadmap to Google Sheets.
* @private
*/
function generate_roadmap_to_google_sheets(object = {}) {
console.log(object)
const { goal = null, exportPDF = false } = object;
let result;
try {
if (goal) {
result = getSheet_(object);
if (result.toString() == "Sheet") {
const sheet = result;
const ss = sheet.getParent();
const spreadsheetId = ss.getId();
const sheetId = sheet.getSheetId();
const obj = {
...object,
apiKey,
spreadsheetId,
sheetId,
};
const g = new GenerateRoadmap(obj).generateContent();
g.putValuesToSpreadsheet();
if (exportPDF) {
SpreadsheetApp.flush();
const range = sheet.getDataRange();
const startRow = range.getRow() - 1;
const endRow = startRow + range.getNumRows();
const startCol = range.getColumn() - 1;
const endCol = startCol + range.getNumColumns();
const pdfUrl = `https://docs.google.com/spreadsheets/d/${spreadsheetId}/export?format=pdf&gid=${sheetId}&r1=${startRow}&c1=${startCol}&r2=${endRow}&c2=${endCol}&portrait=false`;
const blob = UrlFetchApp.fetch(pdfUrl, { headers: { authorization: "Bearer " + ScriptApp.getOAuthToken() } }).getBlob();
const file = DriveApp.createFile(blob.setName(`${goal}.pdf`));
result = { content: [{ type: "text", text: `Roadmap was successfully generated in Google Sheets. The spreadsheet ID and the sheet name are "${spreadsheetId}" and "${sheet.getName()}" (sheet ID: "${sheetId}"), respectively. The URL is "${ss.getUrl()}". The file ID of the converted PDF file is "${file.getId()}".` }], isError: false };
} else {
result = { content: [{ type: "text", text: `Roadmap was successfully generated in Google Sheets. The spreadsheet ID and the sheet name are "${spreadsheetId}" and "${sheet.getName()}" (sheet ID: "${sheetId}"), respectively. The URL is "${ss.getUrl()}".` }], isError: false };
}
}
} else {
result = { content: [{ type: "text", text: `The goal of the roadmap is not found.` }], isError: true };
}
} catch ({ stack }) {
result = { content: [{ type: "text", text: stack }], isError: true };
}
console.log(result); // Check response.
return { jsonrpc: "2.0", result };
}
/**
* This function generates a description of a web site using URL.
* @private
*/
function description_web_site(object = {}) {
const { urls = [] } = object;
let result;
try {
if (Array.isArray(urls) && urls.length > 0) {
const q = [
`Read and understand all the content of all sites from "URLs". Follow "Mission".`,
`<URLs>${urls.join(",")}</URLs>`,
`<Mission>`,
`- Describe the sites.`,
`</Mission>`,
].join("\n");
const g = new GeminiWithFiles({ apiKey, model: "models/gemini-2.5-flash", tools: [{ urlContext: {} }, { googleSearch: {} }] });
const text = g.generateContent({ q });
result = { content: [{ type: "text", text }], isError: false };
} else {
result = { content: [{ type: "text", text: "No URLs." }], isError: true };
}
} catch ({ stack }) {
result = { content: [{ type: "text", text: stack }], isError: true };
}
console.log(result); // Check response.
return { jsonrpc: "2.0", result };
}
/**
* This function generates a description of YouTube by URLs.
* @private
*/
function description_video_on_youtube(object = {}) {
const { url = null } = object;
let result;
try {
if (url) {
const g = new GeminiWithFiles({ apiKey });
const parts = [{ text: "Describe the video." }, { file_data: { file_uri: url } }];
const text = g.generateContent({ parts });
result = { content: [{ type: "text", text }], isError: false };
} else {
result = { content: [{ type: "text", text: "No YouTube URL." }], isError: true };
}
} catch ({ stack }) {
result = { content: [{ type: "text", text: stack }], isError: true };
}
console.log(result); // Check response.
return { jsonrpc: "2.0", result };
}
// Descriptions of the functions.
const descriptions_use_gemini = {
generate_description_on_google_drive: {
description: "Set a description to the file on Google Drive. Use this to generate the description of the file and set it to the file on Google Drive.",
parameters: {
type: "object",
properties: {
fileId: { type: "string", description: "File ID of the file on Google Drive." },
},
required: ["fileId"]
}
},
generate_image_on_google_drive: {
description: "Use this to generate an image from an inputted prompt. The generated image is saved as a file on Google Drive.",
parameters: {
type: "object",
properties: {
prompt: { type: "string", description: "Prompt (description) for generating an image using Gemini." },
},
required: ["prompt"]
}
},
summarize_file_on_google_drive: {
description: "Use this to describe and summaize a file on Google Drive.",
parameters: {
type: "object",
properties: {
fileId: { type: "string", description: "File ID of the file on Google Drive." },
prompt: { type: "string", description: "Prompt (description) for summarizing." },
},
required: ["fileId", "prompt"]
}
},
generate_roadmap_to_google_sheets: {
description: "This generates a roadmap in Google Sheets. Use this to generate a roadmap to Google Sheets. Spreadsheet ID and your goal of the roadmap are required to be provided.",
parameters: {
type: "object",
properties: {
spreadsheetId: { type: "string", description: "Spreadsheet ID of Google Sheets." },
sheetName: { type: "string", description: "Sheet name in the Google Sheets. If both sheetName, sheetId, and sheetIndex are not provided, the values are retrieved from the 1st sheet on Google Sheets." },
sheetId: { type: "string", description: "Sheet ID of the sheet in Google Sheets. If both sheetName, sheetId, and sheetIndex are not provided, the values are retrieved from the 1st sheet on Google Sheets." },
sheetIndex: { type: "number", description: "Sheet index (The 1st sheet is 0.) of the sheet in Google Sheets. If both sheetName, sheetId, and sheetIndex are not provided, the values are retrieved from the 1st sheet on Google Sheets." },
goal: { type: "string", description: "Goal of the roadmap." },
description: { type: "string", description: "Description of the roadmap." },
exportPDF: { type: "boolean", description: "The default is false. When this is true, the PDF file converted from the Google Sheets is created and the file ID of the PDF file is returned. You can download the created PDF file using this file ID." },
},
required: ["spreadsheetId", "goal"]
}
},
description_web_site: {
description: "Use this to describe sites using URLs.",
parameters: {
type: "object",
description: "URLs of the sites. This function describes the sites.",
properties: {
urls: { type: "array", items: { type: "string", description: "URL of the site." } },
},
required: ["urls"]
}
},
description_video_on_youtube: {
description: "Use this to describe and summarize a video on YouTube using the YouTube URL.",
parameters: {
type: "object",
properties: {
url: { type: "string", description: "URL of YouTube. It will be like 'https://www.youtube.com/watch?v=[videoId]'." },
},
required: ["url"]
}
},
};