koishi-plugin-chatluna-google-gemini-adapter 1.0.0-beta.13 → 1.0.0-beta.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/utils.js DELETED
@@ -1,252 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.convertDeltaToMessageChunk = exports.messageTypeToGeminiRole = exports.formatToolToGeminiAITool = exports.formatToolsToGeminiAITools = exports.partAsType = exports.langchainMessageToGeminiMessage = void 0;
4
- /* eslint-disable @typescript-eslint/no-explicit-any */
5
- const messages_1 = require("@langchain/core/messages");
6
- const zod_to_json_schema_1 = require("zod-to-json-schema");
7
- async function langchainMessageToGeminiMessage(messages, model) {
8
- const mappedMessage = await Promise.all(messages.map(async (rawMessage) => {
9
- const role = messageTypeToGeminiRole(rawMessage._getType());
10
- if (role === 'function' ||
11
- rawMessage.additional_kwargs?.function_call != null) {
12
- return {
13
- role: 'function',
14
- parts: [
15
- {
16
- functionResponse: rawMessage.additional_kwargs?.function_call !=
17
- null
18
- ? undefined
19
- : {
20
- name: rawMessage.name,
21
- response: {
22
- name: rawMessage.name,
23
- content: (() => {
24
- try {
25
- const result = JSON.parse(rawMessage.content);
26
- if (typeof result ===
27
- 'string') {
28
- return {
29
- response: result
30
- };
31
- }
32
- else {
33
- return result;
34
- }
35
- }
36
- catch (e) {
37
- return {
38
- response: rawMessage.content
39
- };
40
- }
41
- })()
42
- }
43
- },
44
- functionCall: rawMessage.additional_kwargs?.function_call !=
45
- null
46
- ? {
47
- name: rawMessage.additional_kwargs
48
- .function_call.name,
49
- args: (() => {
50
- try {
51
- const result = JSON.parse(rawMessage
52
- .additional_kwargs
53
- .function_call
54
- .arguments);
55
- if (typeof result === 'string') {
56
- return {
57
- input: result
58
- };
59
- }
60
- else {
61
- return result;
62
- }
63
- }
64
- catch (e) {
65
- return {
66
- input: rawMessage
67
- .additional_kwargs
68
- .function_call
69
- .arguments
70
- };
71
- }
72
- })()
73
- }
74
- : undefined
75
- }
76
- ]
77
- };
78
- }
79
- const images = rawMessage.additional_kwargs.images;
80
- const result = {
81
- role,
82
- parts: [
83
- {
84
- text: rawMessage.content
85
- }
86
- ]
87
- };
88
- if ((model.includes('vision') || model.includes('gemini-1.5')) &&
89
- images != null) {
90
- for (const image of images) {
91
- result.parts.push({
92
- inline_data: {
93
- // base64 image match type
94
- data: image.replace(/^data:image\/\w+;base64,/, ''),
95
- mime_type: 'image/jpeg'
96
- }
97
- });
98
- }
99
- }
100
- return result;
101
- }));
102
- const result = [];
103
- for (let i = 0; i < mappedMessage.length; i++) {
104
- const message = mappedMessage[i];
105
- if (message.role !== 'system') {
106
- result.push(message);
107
- continue;
108
- }
109
- /* if (removeSystemMessage) {
110
- continue
111
- } */
112
- result.push({
113
- role: 'user',
114
- parts: message.parts
115
- });
116
- if (mappedMessage?.[i + 1]?.role === 'model') {
117
- continue;
118
- }
119
- if (mappedMessage?.[i + 1]?.role === 'user') {
120
- result.push({
121
- role: 'model',
122
- parts: [{ text: 'Okay, what do I need to do?' }]
123
- });
124
- }
125
- }
126
- if (result[result.length - 1].role === 'model') {
127
- result.push({
128
- role: 'user',
129
- parts: [
130
- {
131
- text: 'Continue what I said to you last message. Follow these instructions.'
132
- }
133
- ]
134
- });
135
- }
136
- if (model.includes('vision')) {
137
- // format prompts
138
- const textBuffer = [];
139
- const last = result.pop();
140
- for (let i = 0; i < result.length; i++) {
141
- const message = result[i];
142
- const text = message.parts[0].text;
143
- textBuffer.push(`${message.role}: ${text}`);
144
- }
145
- const lastParts = last.parts;
146
- let lastImagesParts = lastParts.filter((part) => part.inline_data?.mime_type ===
147
- 'image/jpeg');
148
- if (lastImagesParts.length < 1) {
149
- for (let i = result.length - 1; i >= 0; i--) {
150
- const message = result[i];
151
- const images = message.parts.filter((part) => part.inline_data?.mime_type ===
152
- 'image/jpeg');
153
- if (images.length > 0) {
154
- lastImagesParts = images;
155
- break;
156
- }
157
- }
158
- }
159
- ;
160
- lastParts.filter((part) => part.text !== undefined &&
161
- part.text !== null).forEach((part) => {
162
- textBuffer.push(`${last.role}: ${part.text}`);
163
- });
164
- return [
165
- {
166
- role: 'user',
167
- parts: [
168
- {
169
- text: textBuffer.join('\n')
170
- },
171
- ...lastImagesParts
172
- ]
173
- }
174
- ];
175
- }
176
- return result;
177
- }
178
- exports.langchainMessageToGeminiMessage = langchainMessageToGeminiMessage;
179
- function partAsType(part) {
180
- return part;
181
- }
182
- exports.partAsType = partAsType;
183
- function formatToolsToGeminiAITools(tools) {
184
- if (tools.length < 1) {
185
- return undefined;
186
- }
187
- return tools.map(formatToolToGeminiAITool);
188
- }
189
- exports.formatToolsToGeminiAITools = formatToolsToGeminiAITools;
190
- function formatToolToGeminiAITool(tool) {
191
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
192
- const parameters = (0, zod_to_json_schema_1.zodToJsonSchema)(tool.schema);
193
- // remove unsupported properties
194
- delete parameters['$schema'];
195
- delete parameters['additionalProperties'];
196
- return {
197
- name: tool.name,
198
- description: tool.description,
199
- // any?
200
- parameters
201
- };
202
- }
203
- exports.formatToolToGeminiAITool = formatToolToGeminiAITool;
204
- function messageTypeToGeminiRole(type) {
205
- switch (type) {
206
- case 'system':
207
- return 'system';
208
- case 'ai':
209
- return 'model';
210
- case 'human':
211
- return 'user';
212
- case 'function':
213
- return 'function';
214
- default:
215
- throw new Error(`Unknown message type: ${type}`);
216
- }
217
- }
218
- exports.messageTypeToGeminiRole = messageTypeToGeminiRole;
219
- function convertDeltaToMessageChunk(
220
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
221
- delta, defaultRole) {
222
- const role = delta.role ?? defaultRole;
223
- const content = delta.content ?? '';
224
- // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/naming-convention
225
- let additional_kwargs;
226
- if (delta.function_call) {
227
- additional_kwargs = {
228
- function_call: delta.function_call
229
- };
230
- }
231
- else if (delta.tool_calls) {
232
- additional_kwargs = {
233
- tool_calls: delta.tool_calls
234
- };
235
- }
236
- else {
237
- additional_kwargs = {};
238
- }
239
- if (role === 'user') {
240
- return new messages_1.HumanMessageChunk({ content });
241
- }
242
- else if (role === 'assistant') {
243
- return new messages_1.AIMessageChunk({ content, additional_kwargs });
244
- }
245
- else if (role === 'system') {
246
- return new messages_1.SystemMessageChunk({ content });
247
- }
248
- else {
249
- return new messages_1.ChatMessageChunk({ content, role });
250
- }
251
- }
252
- exports.convertDeltaToMessageChunk = convertDeltaToMessageChunk;