@ainetwork/adk-provider-model-gemini 0.1.1
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/dist/index.cjs +129 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +22 -0
- package/dist/index.d.ts +22 -0
- package/dist/index.js +106 -0
- package/dist/index.js.map +1 -0
- package/index.ts +139 -0
- package/package.json +35 -0
- package/tsconfig.json +9 -0
- package/tsup.config.ts +9 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
GeminiModel: () => GeminiModel
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(index_exports);
|
|
26
|
+
var import_modules = require("@ainetwork/adk/modules");
|
|
27
|
+
var import_memory = require("@ainetwork/adk/types/memory");
|
|
28
|
+
var import_tool = require("@ainetwork/adk/types/tool");
|
|
29
|
+
var import_genai = require("@google/genai");
|
|
30
|
+
var GeminiModel = class extends import_modules.BaseModel {
|
|
31
|
+
client;
|
|
32
|
+
modelName;
|
|
33
|
+
constructor(apiKey, modelName) {
|
|
34
|
+
super();
|
|
35
|
+
this.client = new import_genai.GoogleGenAI({ apiKey });
|
|
36
|
+
this.modelName = modelName;
|
|
37
|
+
}
|
|
38
|
+
getMessageRole(role) {
|
|
39
|
+
switch (role) {
|
|
40
|
+
case import_memory.ChatRole.USER:
|
|
41
|
+
return "user";
|
|
42
|
+
case import_memory.ChatRole.MODEL:
|
|
43
|
+
case import_memory.ChatRole.SYSTEM:
|
|
44
|
+
return "model";
|
|
45
|
+
default:
|
|
46
|
+
return "model";
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
generateMessages(params) {
|
|
50
|
+
const { query, sessionHistory, systemPrompt } = params;
|
|
51
|
+
const messages = !systemPrompt ? [] : [{ role: "model", parts: [{ text: systemPrompt.trim() }] }];
|
|
52
|
+
const sessionContent = !sessionHistory ? [] : Object.keys(sessionHistory.chats).map((chatId) => {
|
|
53
|
+
const chat = sessionHistory.chats[chatId];
|
|
54
|
+
return {
|
|
55
|
+
role: this.getMessageRole(chat.role),
|
|
56
|
+
parts: [{ text: chat.content.parts[0] }]
|
|
57
|
+
};
|
|
58
|
+
});
|
|
59
|
+
const userContent = { role: "user", parts: [{ text: query }] };
|
|
60
|
+
return messages.concat(sessionContent).concat(userContent);
|
|
61
|
+
}
|
|
62
|
+
appendMessages(messages, message) {
|
|
63
|
+
messages.push({
|
|
64
|
+
role: "user",
|
|
65
|
+
parts: [{ text: message }]
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
async fetch(messages) {
|
|
69
|
+
const response = await this.client.models.generateContent({
|
|
70
|
+
model: this.modelName,
|
|
71
|
+
contents: messages
|
|
72
|
+
});
|
|
73
|
+
return { content: response.text };
|
|
74
|
+
}
|
|
75
|
+
async fetchWithContextMessage(messages, functions) {
|
|
76
|
+
if (functions.length > 0) {
|
|
77
|
+
const response = await this.client.models.generateContent({
|
|
78
|
+
model: this.modelName,
|
|
79
|
+
contents: messages,
|
|
80
|
+
config: {
|
|
81
|
+
tools: [{ functionDeclarations: functions }]
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
const { text, functionCalls } = response;
|
|
85
|
+
const hasName = (value) => {
|
|
86
|
+
return value.name !== void 0;
|
|
87
|
+
};
|
|
88
|
+
const toolCalls = functionCalls?.filter(hasName).map((value) => {
|
|
89
|
+
return {
|
|
90
|
+
name: value.name,
|
|
91
|
+
arguments: value.args
|
|
92
|
+
};
|
|
93
|
+
});
|
|
94
|
+
return {
|
|
95
|
+
content: text,
|
|
96
|
+
toolCalls
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
return await this.fetch(messages);
|
|
100
|
+
}
|
|
101
|
+
convertToolsToFunctions(tools) {
|
|
102
|
+
const functions = [];
|
|
103
|
+
for (const tool of tools) {
|
|
104
|
+
if (!tool.enabled) {
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
if (tool.protocol === import_tool.TOOL_PROTOCOL_TYPE.MCP) {
|
|
108
|
+
const { mcpTool, id } = tool;
|
|
109
|
+
functions.push({
|
|
110
|
+
name: id,
|
|
111
|
+
description: mcpTool.description,
|
|
112
|
+
parametersJsonSchema: mcpTool.inputSchema
|
|
113
|
+
});
|
|
114
|
+
} else {
|
|
115
|
+
const { id, card } = tool;
|
|
116
|
+
functions.push({
|
|
117
|
+
name: id,
|
|
118
|
+
description: card.description
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return functions;
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
126
|
+
0 && (module.exports = {
|
|
127
|
+
GeminiModel
|
|
128
|
+
});
|
|
129
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../index.ts"],"sourcesContent":["import { BaseModel } from \"@ainetwork/adk/modules\";\nimport { ChatRole, type SessionObject } from \"@ainetwork/adk/types/memory\";\nimport type {\n\tFetchResponse,\n\tIA2ATool,\n\tIAgentTool,\n\tIMCPTool,\n\tToolCall,\n} from \"@ainetwork/adk/types/tool\";\nimport { TOOL_PROTOCOL_TYPE } from \"@ainetwork/adk/types/tool\";\nimport {\n\ttype Content,\n\ttype FunctionCall,\n\ttype FunctionDeclaration,\n\tGoogleGenAI,\n} from \"@google/genai\";\n\nexport class GeminiModel extends BaseModel<Content, FunctionDeclaration> {\n\tprivate client: GoogleGenAI;\n\tprivate modelName: string;\n\n\tconstructor(apiKey: string, modelName: string) {\n\t\tsuper();\n\t\tthis.client = new GoogleGenAI({ apiKey });\n\t\tthis.modelName = modelName;\n\t}\n\n\tprivate getMessageRole(role: ChatRole) {\n\t\tswitch (role) {\n\t\t\tcase ChatRole.USER:\n\t\t\t\treturn \"user\";\n\t\t\tcase ChatRole.MODEL:\n\t\t\tcase ChatRole.SYSTEM:\n\t\t\t\treturn \"model\";\n\t\t\tdefault:\n\t\t\t\treturn \"model\"; /*FIXME*/\n\t\t}\n\t}\n\n\tgenerateMessages(params: {\n\t\tquery: string;\n\t\tsessionHistory?: SessionObject;\n\t\tsystemPrompt?: string;\n\t}): Content[] {\n\t\tconst { query, sessionHistory, systemPrompt } = params;\n\t\tconst messages: Content[] = !systemPrompt\n\t\t\t? []\n\t\t\t: [{ role: \"model\", parts: [{ text: systemPrompt.trim() }] }];\n\t\tconst sessionContent: Content[] = !sessionHistory\n\t\t\t? []\n\t\t\t: Object.keys(sessionHistory.chats).map((chatId: string) => {\n\t\t\t\t\tconst chat = sessionHistory.chats[chatId];\n\t\t\t\t\t// TODO: check message.content.type\n\t\t\t\t\treturn {\n\t\t\t\t\t\trole: this.getMessageRole(chat.role),\n\t\t\t\t\t\tparts: [{ text: chat.content.parts[0] }],\n\t\t\t\t\t};\n\t\t\t\t});\n\t\tconst userContent: Content = { role: \"user\", parts: [{ text: query }] };\n\t\treturn messages.concat(sessionContent).concat(userContent);\n\t}\n\n\tappendMessages(messages: Content[], message: string): void {\n\t\tmessages.push({\n\t\t\trole: \"user\",\n\t\t\tparts: [{ text: message }],\n\t\t});\n\t}\n\n\tasync fetch(messages: Content[]): Promise<FetchResponse> {\n\t\tconst response = await this.client.models.generateContent({\n\t\t\tmodel: this.modelName,\n\t\t\tcontents: messages,\n\t\t});\n\n\t\treturn { content: response.text };\n\t}\n\n\tasync fetchWithContextMessage(\n\t\tmessages: Content[],\n\t\tfunctions: FunctionDeclaration[],\n\t): Promise<FetchResponse> {\n\t\tif (functions.length > 0) {\n\t\t\tconst response = await this.client.models.generateContent({\n\t\t\t\tmodel: this.modelName,\n\t\t\t\tcontents: messages,\n\t\t\t\tconfig: {\n\t\t\t\t\ttools: [{ functionDeclarations: functions }],\n\t\t\t\t},\n\t\t\t});\n\n\t\t\tconst { text, functionCalls } = response;\n\t\t\tconst hasName = (\n\t\t\t\tvalue: FunctionCall,\n\t\t\t): value is FunctionCall & { name: string } => {\n\t\t\t\treturn value.name !== undefined;\n\t\t\t};\n\t\t\tconst toolCalls: ToolCall[] | undefined = functionCalls\n\t\t\t\t?.filter(hasName)\n\t\t\t\t.map((value) => {\n\t\t\t\t\treturn {\n\t\t\t\t\t\tname: value.name,\n\t\t\t\t\t\targuments: value.args,\n\t\t\t\t\t};\n\t\t\t\t});\n\n\t\t\treturn {\n\t\t\t\tcontent: text,\n\t\t\t\ttoolCalls,\n\t\t\t};\n\t\t}\n\t\treturn await this.fetch(messages);\n\t}\n\n\tconvertToolsToFunctions(tools: IAgentTool[]): FunctionDeclaration[] {\n\t\tconst functions: FunctionDeclaration[] = [];\n\t\tfor (const tool of tools) {\n\t\t\tif (!tool.enabled) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif (tool.protocol === TOOL_PROTOCOL_TYPE.MCP) {\n\t\t\t\tconst { mcpTool, id } = tool as IMCPTool;\n\t\t\t\tfunctions.push({\n\t\t\t\t\tname: id,\n\t\t\t\t\tdescription: mcpTool.description,\n\t\t\t\t\tparametersJsonSchema: mcpTool.inputSchema,\n\t\t\t\t});\n\t\t\t} else {\n\t\t\t\t// PROTOCOL_TYPE.A2A\n\t\t\t\tconst { id, card } = tool as IA2ATool;\n\t\t\t\tfunctions.push({\n\t\t\t\t\tname: id,\n\t\t\t\t\tdescription: card.description,\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\t\treturn functions;\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,qBAA0B;AAC1B,oBAA6C;AAQ7C,kBAAmC;AACnC,mBAKO;AAEA,IAAM,cAAN,cAA0B,yBAAwC;AAAA,EAChE;AAAA,EACA;AAAA,EAER,YAAY,QAAgB,WAAmB;AAC9C,UAAM;AACN,SAAK,SAAS,IAAI,yBAAY,EAAE,OAAO,CAAC;AACxC,SAAK,YAAY;AAAA,EAClB;AAAA,EAEQ,eAAe,MAAgB;AACtC,YAAQ,MAAM;AAAA,MACb,KAAK,uBAAS;AACb,eAAO;AAAA,MACR,KAAK,uBAAS;AAAA,MACd,KAAK,uBAAS;AACb,eAAO;AAAA,MACR;AACC,eAAO;AAAA,IACT;AAAA,EACD;AAAA,EAEA,iBAAiB,QAIH;AACb,UAAM,EAAE,OAAO,gBAAgB,aAAa,IAAI;AAChD,UAAM,WAAsB,CAAC,eAC1B,CAAC,IACD,CAAC,EAAE,MAAM,SAAS,OAAO,CAAC,EAAE,MAAM,aAAa,KAAK,EAAE,CAAC,EAAE,CAAC;AAC7D,UAAM,iBAA4B,CAAC,iBAChC,CAAC,IACD,OAAO,KAAK,eAAe,KAAK,EAAE,IAAI,CAAC,WAAmB;AAC1D,YAAM,OAAO,eAAe,MAAM,MAAM;AAExC,aAAO;AAAA,QACN,MAAM,KAAK,eAAe,KAAK,IAAI;AAAA,QACnC,OAAO,CAAC,E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AAQ,OAAO;AACzB,UAAI,CAAC,KAAK,SAAS;AAClB;AAAA,MACD;AACA,UAAI,KAAK,aAAa,+BAAmB,KAAK;AAC7C,cAAM,EAAE,SAAS,GAAG,IAAI;AACxB,kBAAU,KAAK;AAAA,UACd,MAAM;AAAA,UACN,aAAa,QAAQ;AAAA,UACrB,sBAAsB,QAAQ;AAAA,QAC/B,CAAC;AAAA,MACF,OAAO;AAEN,cAAM,EAAE,IAAI,KAAK,IAAI;AACrB,kBAAU,KAAK;AAAA,UACd,MAAM;AAAA,UACN,aAAa,KAAK;AAAA,QACnB,CAAC;AAAA,MACF;AAAA,IACD;AACA,WAAO;AAAA,EACR;AACD;","names":[]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { BaseModel } from '@ainetwork/adk/modules';
|
|
2
|
+
import { SessionObject } from '@ainetwork/adk/types/memory';
|
|
3
|
+
import { FetchResponse, IAgentTool } from '@ainetwork/adk/types/tool';
|
|
4
|
+
import { Content, FunctionDeclaration } from '@google/genai';
|
|
5
|
+
|
|
6
|
+
declare class GeminiModel extends BaseModel<Content, FunctionDeclaration> {
|
|
7
|
+
private client;
|
|
8
|
+
private modelName;
|
|
9
|
+
constructor(apiKey: string, modelName: string);
|
|
10
|
+
private getMessageRole;
|
|
11
|
+
generateMessages(params: {
|
|
12
|
+
query: string;
|
|
13
|
+
sessionHistory?: SessionObject;
|
|
14
|
+
systemPrompt?: string;
|
|
15
|
+
}): Content[];
|
|
16
|
+
appendMessages(messages: Content[], message: string): void;
|
|
17
|
+
fetch(messages: Content[]): Promise<FetchResponse>;
|
|
18
|
+
fetchWithContextMessage(messages: Content[], functions: FunctionDeclaration[]): Promise<FetchResponse>;
|
|
19
|
+
convertToolsToFunctions(tools: IAgentTool[]): FunctionDeclaration[];
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export { GeminiModel };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { BaseModel } from '@ainetwork/adk/modules';
|
|
2
|
+
import { SessionObject } from '@ainetwork/adk/types/memory';
|
|
3
|
+
import { FetchResponse, IAgentTool } from '@ainetwork/adk/types/tool';
|
|
4
|
+
import { Content, FunctionDeclaration } from '@google/genai';
|
|
5
|
+
|
|
6
|
+
declare class GeminiModel extends BaseModel<Content, FunctionDeclaration> {
|
|
7
|
+
private client;
|
|
8
|
+
private modelName;
|
|
9
|
+
constructor(apiKey: string, modelName: string);
|
|
10
|
+
private getMessageRole;
|
|
11
|
+
generateMessages(params: {
|
|
12
|
+
query: string;
|
|
13
|
+
sessionHistory?: SessionObject;
|
|
14
|
+
systemPrompt?: string;
|
|
15
|
+
}): Content[];
|
|
16
|
+
appendMessages(messages: Content[], message: string): void;
|
|
17
|
+
fetch(messages: Content[]): Promise<FetchResponse>;
|
|
18
|
+
fetchWithContextMessage(messages: Content[], functions: FunctionDeclaration[]): Promise<FetchResponse>;
|
|
19
|
+
convertToolsToFunctions(tools: IAgentTool[]): FunctionDeclaration[];
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export { GeminiModel };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
// index.ts
|
|
2
|
+
import { BaseModel } from "@ainetwork/adk/modules";
|
|
3
|
+
import { ChatRole } from "@ainetwork/adk/types/memory";
|
|
4
|
+
import { TOOL_PROTOCOL_TYPE } from "@ainetwork/adk/types/tool";
|
|
5
|
+
import {
|
|
6
|
+
GoogleGenAI
|
|
7
|
+
} from "@google/genai";
|
|
8
|
+
var GeminiModel = class extends BaseModel {
|
|
9
|
+
client;
|
|
10
|
+
modelName;
|
|
11
|
+
constructor(apiKey, modelName) {
|
|
12
|
+
super();
|
|
13
|
+
this.client = new GoogleGenAI({ apiKey });
|
|
14
|
+
this.modelName = modelName;
|
|
15
|
+
}
|
|
16
|
+
getMessageRole(role) {
|
|
17
|
+
switch (role) {
|
|
18
|
+
case ChatRole.USER:
|
|
19
|
+
return "user";
|
|
20
|
+
case ChatRole.MODEL:
|
|
21
|
+
case ChatRole.SYSTEM:
|
|
22
|
+
return "model";
|
|
23
|
+
default:
|
|
24
|
+
return "model";
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
generateMessages(params) {
|
|
28
|
+
const { query, sessionHistory, systemPrompt } = params;
|
|
29
|
+
const messages = !systemPrompt ? [] : [{ role: "model", parts: [{ text: systemPrompt.trim() }] }];
|
|
30
|
+
const sessionContent = !sessionHistory ? [] : Object.keys(sessionHistory.chats).map((chatId) => {
|
|
31
|
+
const chat = sessionHistory.chats[chatId];
|
|
32
|
+
return {
|
|
33
|
+
role: this.getMessageRole(chat.role),
|
|
34
|
+
parts: [{ text: chat.content.parts[0] }]
|
|
35
|
+
};
|
|
36
|
+
});
|
|
37
|
+
const userContent = { role: "user", parts: [{ text: query }] };
|
|
38
|
+
return messages.concat(sessionContent).concat(userContent);
|
|
39
|
+
}
|
|
40
|
+
appendMessages(messages, message) {
|
|
41
|
+
messages.push({
|
|
42
|
+
role: "user",
|
|
43
|
+
parts: [{ text: message }]
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
async fetch(messages) {
|
|
47
|
+
const response = await this.client.models.generateContent({
|
|
48
|
+
model: this.modelName,
|
|
49
|
+
contents: messages
|
|
50
|
+
});
|
|
51
|
+
return { content: response.text };
|
|
52
|
+
}
|
|
53
|
+
async fetchWithContextMessage(messages, functions) {
|
|
54
|
+
if (functions.length > 0) {
|
|
55
|
+
const response = await this.client.models.generateContent({
|
|
56
|
+
model: this.modelName,
|
|
57
|
+
contents: messages,
|
|
58
|
+
config: {
|
|
59
|
+
tools: [{ functionDeclarations: functions }]
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
const { text, functionCalls } = response;
|
|
63
|
+
const hasName = (value) => {
|
|
64
|
+
return value.name !== void 0;
|
|
65
|
+
};
|
|
66
|
+
const toolCalls = functionCalls?.filter(hasName).map((value) => {
|
|
67
|
+
return {
|
|
68
|
+
name: value.name,
|
|
69
|
+
arguments: value.args
|
|
70
|
+
};
|
|
71
|
+
});
|
|
72
|
+
return {
|
|
73
|
+
content: text,
|
|
74
|
+
toolCalls
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
return await this.fetch(messages);
|
|
78
|
+
}
|
|
79
|
+
convertToolsToFunctions(tools) {
|
|
80
|
+
const functions = [];
|
|
81
|
+
for (const tool of tools) {
|
|
82
|
+
if (!tool.enabled) {
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
if (tool.protocol === TOOL_PROTOCOL_TYPE.MCP) {
|
|
86
|
+
const { mcpTool, id } = tool;
|
|
87
|
+
functions.push({
|
|
88
|
+
name: id,
|
|
89
|
+
description: mcpTool.description,
|
|
90
|
+
parametersJsonSchema: mcpTool.inputSchema
|
|
91
|
+
});
|
|
92
|
+
} else {
|
|
93
|
+
const { id, card } = tool;
|
|
94
|
+
functions.push({
|
|
95
|
+
name: id,
|
|
96
|
+
description: card.description
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return functions;
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
export {
|
|
104
|
+
GeminiModel
|
|
105
|
+
};
|
|
106
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../index.ts"],"sourcesContent":["import { BaseModel } from \"@ainetwork/adk/modules\";\nimport { ChatRole, type SessionObject } from \"@ainetwork/adk/types/memory\";\nimport type {\n\tFetchResponse,\n\tIA2ATool,\n\tIAgentTool,\n\tIMCPTool,\n\tToolCall,\n} from \"@ainetwork/adk/types/tool\";\nimport { TOOL_PROTOCOL_TYPE } from \"@ainetwork/adk/types/tool\";\nimport {\n\ttype Content,\n\ttype FunctionCall,\n\ttype FunctionDeclaration,\n\tGoogleGenAI,\n} from \"@google/genai\";\n\nexport class GeminiModel extends BaseModel<Content, FunctionDeclaration> {\n\tprivate client: GoogleGenAI;\n\tprivate modelName: string;\n\n\tconstructor(apiKey: string, modelName: string) {\n\t\tsuper();\n\t\tthis.client = new GoogleGenAI({ apiKey });\n\t\tthis.modelName = modelName;\n\t}\n\n\tprivate getMessageRole(role: ChatRole) {\n\t\tswitch (role) {\n\t\t\tcase ChatRole.USER:\n\t\t\t\treturn \"user\";\n\t\t\tcase ChatRole.MODEL:\n\t\t\tcase ChatRole.SYSTEM:\n\t\t\t\treturn \"model\";\n\t\t\tdefault:\n\t\t\t\treturn \"model\"; /*FIXME*/\n\t\t}\n\t}\n\n\tgenerateMessages(params: {\n\t\tquery: string;\n\t\tsessionHistory?: SessionObject;\n\t\tsystemPrompt?: string;\n\t}): Content[] {\n\t\tconst { query, sessionHistory, systemPrompt } = params;\n\t\tconst messages: Content[] = !systemPrompt\n\t\t\t? []\n\t\t\t: [{ role: \"model\", parts: [{ text: systemPrompt.trim() }] }];\n\t\tconst sessionContent: Content[] = !sessionHistory\n\t\t\t? []\n\t\t\t: Object.keys(sessionHistory.chats).map((chatId: string) => {\n\t\t\t\t\tconst chat = sessionHistory.chats[chatId];\n\t\t\t\t\t// TODO: check message.content.type\n\t\t\t\t\treturn {\n\t\t\t\t\t\trole: this.getMessageRole(chat.role),\n\t\t\t\t\t\tparts: [{ text: chat.content.parts[0] }],\n\t\t\t\t\t};\n\t\t\t\t});\n\t\tconst userContent: Content = { role: \"user\", parts: [{ text: query }] };\n\t\treturn messages.concat(sessionContent).concat(userContent);\n\t}\n\n\tappendMessages(messages: Content[], message: string): void {\n\t\tmessages.push({\n\t\t\trole: \"user\",\n\t\t\tparts: [{ text: message }],\n\t\t});\n\t}\n\n\tasync fetch(messages: Content[]): Promise<FetchResponse> {\n\t\tconst response = await this.client.models.generateContent({\n\t\t\tmodel: this.modelName,\n\t\t\tcontents: messages,\n\t\t});\n\n\t\treturn { content: response.text };\n\t}\n\n\tasync fetchWithContextMessage(\n\t\tmessages: Content[],\n\t\tfunctions: FunctionDeclaration[],\n\t): Promise<FetchResponse> {\n\t\tif (functions.length > 0) {\n\t\t\tconst response = await this.client.models.generateContent({\n\t\t\t\tmodel: this.modelName,\n\t\t\t\tcontents: messages,\n\t\t\t\tconfig: {\n\t\t\t\t\ttools: [{ functionDeclarations: functions }],\n\t\t\t\t},\n\t\t\t});\n\n\t\t\tconst { text, functionCalls } = response;\n\t\t\tconst hasName = (\n\t\t\t\tvalue: FunctionCall,\n\t\t\t): value is FunctionCall & { name: string } => {\n\t\t\t\treturn value.name !== undefined;\n\t\t\t};\n\t\t\tconst toolCalls: ToolCall[] | undefined = functionCalls\n\t\t\t\t?.filter(hasName)\n\t\t\t\t.map((value) => {\n\t\t\t\t\treturn {\n\t\t\t\t\t\tname: value.name,\n\t\t\t\t\t\targuments: value.args,\n\t\t\t\t\t};\n\t\t\t\t});\n\n\t\t\treturn {\n\t\t\t\tcontent: text,\n\t\t\t\ttoolCalls,\n\t\t\t};\n\t\t}\n\t\treturn await this.fetch(messages);\n\t}\n\n\tconvertToolsToFunctions(tools: IAgentTool[]): FunctionDeclaration[] {\n\t\tconst functions: FunctionDeclaration[] = [];\n\t\tfor (const tool of tools) {\n\t\t\tif (!tool.enabled) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif (tool.protocol === TOOL_PROTOCOL_TYPE.MCP) {\n\t\t\t\tconst { mcpTool, id } = tool as IMCPTool;\n\t\t\t\tfunctions.push({\n\t\t\t\t\tname: id,\n\t\t\t\t\tdescription: mcpTool.description,\n\t\t\t\t\tparametersJsonSchema: mcpTool.inputSchema,\n\t\t\t\t});\n\t\t\t} else {\n\t\t\t\t// PROTOCOL_TYPE.A2A\n\t\t\t\tconst { id, card } = tool as IA2ATool;\n\t\t\t\tfunctions.push({\n\t\t\t\t\tname: id,\n\t\t\t\t\tdescription: card.description,\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\t\treturn functions;\n\t}\n}\n"],"mappings":";AAAA,SAAS,iBAAiB;AAC1B,SAAS,gBAAoC;AAQ7C,SAAS,0BAA0B;AAC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ED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { BaseModel } from "@ainetwork/adk/modules";
|
|
2
|
+
import { ChatRole, type SessionObject } from "@ainetwork/adk/types/memory";
|
|
3
|
+
import type {
|
|
4
|
+
FetchResponse,
|
|
5
|
+
IA2ATool,
|
|
6
|
+
IAgentTool,
|
|
7
|
+
IMCPTool,
|
|
8
|
+
ToolCall,
|
|
9
|
+
} from "@ainetwork/adk/types/tool";
|
|
10
|
+
import { TOOL_PROTOCOL_TYPE } from "@ainetwork/adk/types/tool";
|
|
11
|
+
import {
|
|
12
|
+
type Content,
|
|
13
|
+
type FunctionCall,
|
|
14
|
+
type FunctionDeclaration,
|
|
15
|
+
GoogleGenAI,
|
|
16
|
+
} from "@google/genai";
|
|
17
|
+
|
|
18
|
+
export class GeminiModel extends BaseModel<Content, FunctionDeclaration> {
|
|
19
|
+
private client: GoogleGenAI;
|
|
20
|
+
private modelName: string;
|
|
21
|
+
|
|
22
|
+
constructor(apiKey: string, modelName: string) {
|
|
23
|
+
super();
|
|
24
|
+
this.client = new GoogleGenAI({ apiKey });
|
|
25
|
+
this.modelName = modelName;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
private getMessageRole(role: ChatRole) {
|
|
29
|
+
switch (role) {
|
|
30
|
+
case ChatRole.USER:
|
|
31
|
+
return "user";
|
|
32
|
+
case ChatRole.MODEL:
|
|
33
|
+
case ChatRole.SYSTEM:
|
|
34
|
+
return "model";
|
|
35
|
+
default:
|
|
36
|
+
return "model"; /*FIXME*/
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
generateMessages(params: {
|
|
41
|
+
query: string;
|
|
42
|
+
sessionHistory?: SessionObject;
|
|
43
|
+
systemPrompt?: string;
|
|
44
|
+
}): Content[] {
|
|
45
|
+
const { query, sessionHistory, systemPrompt } = params;
|
|
46
|
+
const messages: Content[] = !systemPrompt
|
|
47
|
+
? []
|
|
48
|
+
: [{ role: "model", parts: [{ text: systemPrompt.trim() }] }];
|
|
49
|
+
const sessionContent: Content[] = !sessionHistory
|
|
50
|
+
? []
|
|
51
|
+
: Object.keys(sessionHistory.chats).map((chatId: string) => {
|
|
52
|
+
const chat = sessionHistory.chats[chatId];
|
|
53
|
+
// TODO: check message.content.type
|
|
54
|
+
return {
|
|
55
|
+
role: this.getMessageRole(chat.role),
|
|
56
|
+
parts: [{ text: chat.content.parts[0] }],
|
|
57
|
+
};
|
|
58
|
+
});
|
|
59
|
+
const userContent: Content = { role: "user", parts: [{ text: query }] };
|
|
60
|
+
return messages.concat(sessionContent).concat(userContent);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
appendMessages(messages: Content[], message: string): void {
|
|
64
|
+
messages.push({
|
|
65
|
+
role: "user",
|
|
66
|
+
parts: [{ text: message }],
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
async fetch(messages: Content[]): Promise<FetchResponse> {
|
|
71
|
+
const response = await this.client.models.generateContent({
|
|
72
|
+
model: this.modelName,
|
|
73
|
+
contents: messages,
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
return { content: response.text };
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
async fetchWithContextMessage(
|
|
80
|
+
messages: Content[],
|
|
81
|
+
functions: FunctionDeclaration[],
|
|
82
|
+
): Promise<FetchResponse> {
|
|
83
|
+
if (functions.length > 0) {
|
|
84
|
+
const response = await this.client.models.generateContent({
|
|
85
|
+
model: this.modelName,
|
|
86
|
+
contents: messages,
|
|
87
|
+
config: {
|
|
88
|
+
tools: [{ functionDeclarations: functions }],
|
|
89
|
+
},
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
const { text, functionCalls } = response;
|
|
93
|
+
const hasName = (
|
|
94
|
+
value: FunctionCall,
|
|
95
|
+
): value is FunctionCall & { name: string } => {
|
|
96
|
+
return value.name !== undefined;
|
|
97
|
+
};
|
|
98
|
+
const toolCalls: ToolCall[] | undefined = functionCalls
|
|
99
|
+
?.filter(hasName)
|
|
100
|
+
.map((value) => {
|
|
101
|
+
return {
|
|
102
|
+
name: value.name,
|
|
103
|
+
arguments: value.args,
|
|
104
|
+
};
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
return {
|
|
108
|
+
content: text,
|
|
109
|
+
toolCalls,
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
return await this.fetch(messages);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
convertToolsToFunctions(tools: IAgentTool[]): FunctionDeclaration[] {
|
|
116
|
+
const functions: FunctionDeclaration[] = [];
|
|
117
|
+
for (const tool of tools) {
|
|
118
|
+
if (!tool.enabled) {
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
if (tool.protocol === TOOL_PROTOCOL_TYPE.MCP) {
|
|
122
|
+
const { mcpTool, id } = tool as IMCPTool;
|
|
123
|
+
functions.push({
|
|
124
|
+
name: id,
|
|
125
|
+
description: mcpTool.description,
|
|
126
|
+
parametersJsonSchema: mcpTool.inputSchema,
|
|
127
|
+
});
|
|
128
|
+
} else {
|
|
129
|
+
// PROTOCOL_TYPE.A2A
|
|
130
|
+
const { id, card } = tool as IA2ATool;
|
|
131
|
+
functions.push({
|
|
132
|
+
name: id,
|
|
133
|
+
description: card.description,
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return functions;
|
|
138
|
+
}
|
|
139
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ainetwork/adk-provider-model-gemini",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"author": "AI Network (https://ainetwork.ai)",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"engines": {
|
|
7
|
+
"node": ">=20"
|
|
8
|
+
},
|
|
9
|
+
"main": "./dist/index.cjs",
|
|
10
|
+
"module": "./dist/index.js",
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"import": "./dist/index.js",
|
|
16
|
+
"require": "./dist/index.cjs"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"scripts": {
|
|
20
|
+
"build": "tsup",
|
|
21
|
+
"clean": "rm -rf dist"
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@ainetwork/adk": "^0.1.2",
|
|
25
|
+
"@google/genai": "^1.11.0"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"typescript": "^5.0.0"
|
|
29
|
+
},
|
|
30
|
+
"license": "MIT",
|
|
31
|
+
"publishConfig": {
|
|
32
|
+
"access": "public"
|
|
33
|
+
},
|
|
34
|
+
"gitHead": "cb6205738fd84533ce56bbeeab00ea9dfe2aaf3a"
|
|
35
|
+
}
|
package/tsconfig.json
ADDED