koishi-plugin-chatluna-google-gemini-adapter 1.0.0-beta.8 → 1.0.0-rc.0
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/README.md +7 -7
- package/lib/index.cjs +684 -0
- package/lib/index.d.ts +111 -12
- package/lib/index.mjs +677 -0
- package/package.json +36 -11
- package/lib/client.d.ts +0 -17
- package/lib/client.js +0 -79
- package/lib/index.js +0 -63
- package/lib/requester.d.ts +0 -16
- package/lib/requester.js +0 -259
- package/lib/types.d.ts +0 -60
- package/lib/types.js +0 -2
- package/lib/utils.d.ts +0 -9
- package/lib/utils.js +0 -251
package/lib/utils.js
DELETED
|
@@ -1,251 +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') && images != null) {
|
|
89
|
-
for (const image of images) {
|
|
90
|
-
result.parts.push({
|
|
91
|
-
inline_data: {
|
|
92
|
-
// base64 image match type
|
|
93
|
-
data: image.replace(/^data:image\/\w+;base64,/, ''),
|
|
94
|
-
mime_type: 'image/jpeg'
|
|
95
|
-
}
|
|
96
|
-
});
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
return result;
|
|
100
|
-
}));
|
|
101
|
-
const result = [];
|
|
102
|
-
for (let i = 0; i < mappedMessage.length; i++) {
|
|
103
|
-
const message = mappedMessage[i];
|
|
104
|
-
if (message.role !== 'system') {
|
|
105
|
-
result.push(message);
|
|
106
|
-
continue;
|
|
107
|
-
}
|
|
108
|
-
/* if (removeSystemMessage) {
|
|
109
|
-
continue
|
|
110
|
-
} */
|
|
111
|
-
result.push({
|
|
112
|
-
role: 'user',
|
|
113
|
-
parts: message.parts
|
|
114
|
-
});
|
|
115
|
-
if (mappedMessage?.[i + 1]?.role === 'model') {
|
|
116
|
-
continue;
|
|
117
|
-
}
|
|
118
|
-
if (mappedMessage?.[i + 1]?.role === 'user') {
|
|
119
|
-
result.push({
|
|
120
|
-
role: 'model',
|
|
121
|
-
parts: [{ text: 'Okay, what do I need to do?' }]
|
|
122
|
-
});
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
if (result[result.length - 1].role === 'model') {
|
|
126
|
-
result.push({
|
|
127
|
-
role: 'user',
|
|
128
|
-
parts: [
|
|
129
|
-
{
|
|
130
|
-
text: 'Continue what I said to you last message. Follow these instructions.'
|
|
131
|
-
}
|
|
132
|
-
]
|
|
133
|
-
});
|
|
134
|
-
}
|
|
135
|
-
if (model.includes('vision')) {
|
|
136
|
-
// format prompts
|
|
137
|
-
const textBuffer = [];
|
|
138
|
-
const last = result.pop();
|
|
139
|
-
for (let i = 0; i < result.length; i++) {
|
|
140
|
-
const message = result[i];
|
|
141
|
-
const text = message.parts[0].text;
|
|
142
|
-
textBuffer.push(`${message.role}: ${text}`);
|
|
143
|
-
}
|
|
144
|
-
const lastParts = last.parts;
|
|
145
|
-
let lastImagesParts = lastParts.filter((part) => part.inline_data?.mime_type ===
|
|
146
|
-
'image/jpeg');
|
|
147
|
-
if (lastImagesParts.length < 1) {
|
|
148
|
-
for (let i = result.length - 1; i >= 0; i--) {
|
|
149
|
-
const message = result[i];
|
|
150
|
-
const images = message.parts.filter((part) => part.inline_data?.mime_type ===
|
|
151
|
-
'image/jpeg');
|
|
152
|
-
if (images.length > 0) {
|
|
153
|
-
lastImagesParts = images;
|
|
154
|
-
break;
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
;
|
|
159
|
-
lastParts.filter((part) => part.text !== undefined &&
|
|
160
|
-
part.text !== null).forEach((part) => {
|
|
161
|
-
textBuffer.push(`${last.role}: ${part.text}`);
|
|
162
|
-
});
|
|
163
|
-
return [
|
|
164
|
-
{
|
|
165
|
-
role: 'user',
|
|
166
|
-
parts: [
|
|
167
|
-
{
|
|
168
|
-
text: textBuffer.join('\n')
|
|
169
|
-
},
|
|
170
|
-
...lastImagesParts
|
|
171
|
-
]
|
|
172
|
-
}
|
|
173
|
-
];
|
|
174
|
-
}
|
|
175
|
-
return result;
|
|
176
|
-
}
|
|
177
|
-
exports.langchainMessageToGeminiMessage = langchainMessageToGeminiMessage;
|
|
178
|
-
function partAsType(part) {
|
|
179
|
-
return part;
|
|
180
|
-
}
|
|
181
|
-
exports.partAsType = partAsType;
|
|
182
|
-
function formatToolsToGeminiAITools(tools) {
|
|
183
|
-
if (tools.length < 1) {
|
|
184
|
-
return undefined;
|
|
185
|
-
}
|
|
186
|
-
return tools.map(formatToolToGeminiAITool);
|
|
187
|
-
}
|
|
188
|
-
exports.formatToolsToGeminiAITools = formatToolsToGeminiAITools;
|
|
189
|
-
function formatToolToGeminiAITool(tool) {
|
|
190
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
191
|
-
const parameters = (0, zod_to_json_schema_1.zodToJsonSchema)(tool.schema);
|
|
192
|
-
// remove unsupported properties
|
|
193
|
-
delete parameters['$schema'];
|
|
194
|
-
delete parameters['additionalProperties'];
|
|
195
|
-
return {
|
|
196
|
-
name: tool.name,
|
|
197
|
-
description: tool.description,
|
|
198
|
-
// any?
|
|
199
|
-
parameters
|
|
200
|
-
};
|
|
201
|
-
}
|
|
202
|
-
exports.formatToolToGeminiAITool = formatToolToGeminiAITool;
|
|
203
|
-
function messageTypeToGeminiRole(type) {
|
|
204
|
-
switch (type) {
|
|
205
|
-
case 'system':
|
|
206
|
-
return 'system';
|
|
207
|
-
case 'ai':
|
|
208
|
-
return 'model';
|
|
209
|
-
case 'human':
|
|
210
|
-
return 'user';
|
|
211
|
-
case 'function':
|
|
212
|
-
return 'function';
|
|
213
|
-
default:
|
|
214
|
-
throw new Error(`Unknown message type: ${type}`);
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
exports.messageTypeToGeminiRole = messageTypeToGeminiRole;
|
|
218
|
-
function convertDeltaToMessageChunk(
|
|
219
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
220
|
-
delta, defaultRole) {
|
|
221
|
-
const role = delta.role ?? defaultRole;
|
|
222
|
-
const content = delta.content ?? '';
|
|
223
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/naming-convention
|
|
224
|
-
let additional_kwargs;
|
|
225
|
-
if (delta.function_call) {
|
|
226
|
-
additional_kwargs = {
|
|
227
|
-
function_call: delta.function_call
|
|
228
|
-
};
|
|
229
|
-
}
|
|
230
|
-
else if (delta.tool_calls) {
|
|
231
|
-
additional_kwargs = {
|
|
232
|
-
tool_calls: delta.tool_calls
|
|
233
|
-
};
|
|
234
|
-
}
|
|
235
|
-
else {
|
|
236
|
-
additional_kwargs = {};
|
|
237
|
-
}
|
|
238
|
-
if (role === 'user') {
|
|
239
|
-
return new messages_1.HumanMessageChunk({ content });
|
|
240
|
-
}
|
|
241
|
-
else if (role === 'assistant') {
|
|
242
|
-
return new messages_1.AIMessageChunk({ content, additional_kwargs });
|
|
243
|
-
}
|
|
244
|
-
else if (role === 'system') {
|
|
245
|
-
return new messages_1.SystemMessageChunk({ content });
|
|
246
|
-
}
|
|
247
|
-
else {
|
|
248
|
-
return new messages_1.ChatMessageChunk({ content, role });
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
exports.convertDeltaToMessageChunk = convertDeltaToMessageChunk;
|