@ainetwork/adk-provider-model-gemini 0.1.1 → 0.1.3
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 +42 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +42 -0
- package/dist/index.js.map +1 -1
- package/index.ts +69 -0
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -98,6 +98,48 @@ var GeminiModel = class extends import_modules.BaseModel {
|
|
|
98
98
|
}
|
|
99
99
|
return await this.fetch(messages);
|
|
100
100
|
}
|
|
101
|
+
async fetchStreamWithContextMessage(messages, functions) {
|
|
102
|
+
const stream = await this.client.models.generateContentStream({
|
|
103
|
+
model: this.modelName,
|
|
104
|
+
contents: messages,
|
|
105
|
+
config: { tools: [{ functionDeclarations: functions }] }
|
|
106
|
+
});
|
|
107
|
+
return await this.createGeminiStreamAdapter(stream);
|
|
108
|
+
}
|
|
109
|
+
// NOTE(yoojin): Need to switch API Stream type to LLMStream.
|
|
110
|
+
createGeminiStreamAdapter(geminiStream) {
|
|
111
|
+
const hasName = (value) => {
|
|
112
|
+
return value.name !== void 0;
|
|
113
|
+
};
|
|
114
|
+
return {
|
|
115
|
+
async *[Symbol.asyncIterator]() {
|
|
116
|
+
for await (const geminiChunk of geminiStream) {
|
|
117
|
+
yield {
|
|
118
|
+
delta: {
|
|
119
|
+
role: geminiChunk.candidates?.[0]?.content?.role,
|
|
120
|
+
content: geminiChunk.candidates?.[0]?.content?.parts?.[0]?.text || void 0,
|
|
121
|
+
tool_calls: hasName(
|
|
122
|
+
geminiChunk.candidates?.[0]?.content?.parts?.[0]?.functionCall || {}
|
|
123
|
+
) ? [
|
|
124
|
+
{
|
|
125
|
+
index: 0,
|
|
126
|
+
id: geminiChunk.candidates?.[0]?.content?.parts?.[0]?.functionCall?.id || "id",
|
|
127
|
+
function: {
|
|
128
|
+
name: geminiChunk.candidates?.[0]?.content?.parts?.[0]?.functionCall?.name,
|
|
129
|
+
arguments: geminiChunk.candidates?.[0]?.content?.parts?.[0]?.functionCall?.args
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
] : void 0
|
|
133
|
+
},
|
|
134
|
+
finish_reason: geminiChunk.candidates?.[0]?.finishReason,
|
|
135
|
+
metadata: {
|
|
136
|
+
provider: "gemini"
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
}
|
|
101
143
|
convertToolsToFunctions(tools) {
|
|
102
144
|
const functions = [];
|
|
103
145
|
for (const tool of tools) {
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +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;
|
|
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\tLLMStream,\n\tStreamChunk,\n\tToolCallDelta,\n} from \"@ainetwork/adk/types/stream\";\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\ttype GenerateContentResponse,\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\tasync fetchStreamWithContextMessage(\n\t\tmessages: Content[],\n\t\tfunctions: FunctionDeclaration[],\n\t): Promise<LLMStream> {\n\t\tconst stream = await this.client.models.generateContentStream({\n\t\t\tmodel: this.modelName,\n\t\t\tcontents: messages,\n\t\t\tconfig: { tools: [{ functionDeclarations: functions }] },\n\t\t});\n\n\t\treturn await this.createGeminiStreamAdapter(stream);\n\t}\n\n\t// NOTE(yoojin): Need to switch API Stream type to LLMStream.\n\tprivate createGeminiStreamAdapter(\n\t\tgeminiStream: AsyncIterable<GenerateContentResponse>,\n\t): LLMStream {\n\t\tconst hasName = (\n\t\t\tvalue: FunctionCall,\n\t\t): value is FunctionCall & { name: string } => {\n\t\t\treturn value.name !== undefined;\n\t\t};\n\n\t\treturn {\n\t\t\tasync *[Symbol.asyncIterator](): AsyncIterator<StreamChunk> {\n\t\t\t\tfor await (const geminiChunk of geminiStream) {\n\t\t\t\t\tyield {\n\t\t\t\t\t\tdelta: {\n\t\t\t\t\t\t\trole: geminiChunk.candidates?.[0]?.content?.role,\n\t\t\t\t\t\t\tcontent:\n\t\t\t\t\t\t\t\tgeminiChunk.candidates?.[0]?.content?.parts?.[0]?.text ||\n\t\t\t\t\t\t\t\tundefined,\n\t\t\t\t\t\t\ttool_calls: hasName(\n\t\t\t\t\t\t\t\tgeminiChunk.candidates?.[0]?.content?.parts?.[0]\n\t\t\t\t\t\t\t\t\t?.functionCall || {},\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\t? ([\n\t\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\t\tindex: 0,\n\t\t\t\t\t\t\t\t\t\t\tid:\n\t\t\t\t\t\t\t\t\t\t\t\tgeminiChunk.candidates?.[0]?.content?.parts?.[0]\n\t\t\t\t\t\t\t\t\t\t\t\t\t?.functionCall?.id || \"id\",\n\t\t\t\t\t\t\t\t\t\t\tfunction: {\n\t\t\t\t\t\t\t\t\t\t\t\tname: geminiChunk.candidates?.[0]?.content?.parts?.[0]\n\t\t\t\t\t\t\t\t\t\t\t\t\t?.functionCall?.name,\n\t\t\t\t\t\t\t\t\t\t\t\targuments:\n\t\t\t\t\t\t\t\t\t\t\t\t\tgeminiChunk.candidates?.[0]?.content?.parts?.[0]\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t?.functionCall?.args,\n\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t] as ToolCallDelta[])\n\t\t\t\t\t\t\t\t: undefined,\n\t\t\t\t\t\t},\n\t\t\t\t\t\tfinish_reason: geminiChunk.candidates?.[0]?.finishReason as any,\n\t\t\t\t\t\tmetadata: {\n\t\t\t\t\t\t\tprovider: \"gemini\",\n\t\t\t\t\t\t},\n\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t},\n\t\t};\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;AAa7C,kBAAmC;AACnC,mBAMO;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,M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;AAAA,YACJ;AAAA,YACA,eAAe,YAAY,aAAa,CAAC,GAAG;AAAA,YAC5C,UAAU;AAAA,cACT,UAAU;AAAA,YACX;AAAA,UACD;AAAA,QACD;AAAA,MACD;AAAA,IACD;AAAA,EACD;AAAA,EAEA,wBAAwB,OAA4C;AACnE,UAAM,YAAmC,CAAC;AAC1C,eAAW,QAAQ,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
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { BaseModel } from '@ainetwork/adk/modules';
|
|
2
2
|
import { SessionObject } from '@ainetwork/adk/types/memory';
|
|
3
|
+
import { LLMStream } from '@ainetwork/adk/types/stream';
|
|
3
4
|
import { FetchResponse, IAgentTool } from '@ainetwork/adk/types/tool';
|
|
4
5
|
import { Content, FunctionDeclaration } from '@google/genai';
|
|
5
6
|
|
|
@@ -16,6 +17,8 @@ declare class GeminiModel extends BaseModel<Content, FunctionDeclaration> {
|
|
|
16
17
|
appendMessages(messages: Content[], message: string): void;
|
|
17
18
|
fetch(messages: Content[]): Promise<FetchResponse>;
|
|
18
19
|
fetchWithContextMessage(messages: Content[], functions: FunctionDeclaration[]): Promise<FetchResponse>;
|
|
20
|
+
fetchStreamWithContextMessage(messages: Content[], functions: FunctionDeclaration[]): Promise<LLMStream>;
|
|
21
|
+
private createGeminiStreamAdapter;
|
|
19
22
|
convertToolsToFunctions(tools: IAgentTool[]): FunctionDeclaration[];
|
|
20
23
|
}
|
|
21
24
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { BaseModel } from '@ainetwork/adk/modules';
|
|
2
2
|
import { SessionObject } from '@ainetwork/adk/types/memory';
|
|
3
|
+
import { LLMStream } from '@ainetwork/adk/types/stream';
|
|
3
4
|
import { FetchResponse, IAgentTool } from '@ainetwork/adk/types/tool';
|
|
4
5
|
import { Content, FunctionDeclaration } from '@google/genai';
|
|
5
6
|
|
|
@@ -16,6 +17,8 @@ declare class GeminiModel extends BaseModel<Content, FunctionDeclaration> {
|
|
|
16
17
|
appendMessages(messages: Content[], message: string): void;
|
|
17
18
|
fetch(messages: Content[]): Promise<FetchResponse>;
|
|
18
19
|
fetchWithContextMessage(messages: Content[], functions: FunctionDeclaration[]): Promise<FetchResponse>;
|
|
20
|
+
fetchStreamWithContextMessage(messages: Content[], functions: FunctionDeclaration[]): Promise<LLMStream>;
|
|
21
|
+
private createGeminiStreamAdapter;
|
|
19
22
|
convertToolsToFunctions(tools: IAgentTool[]): FunctionDeclaration[];
|
|
20
23
|
}
|
|
21
24
|
|
package/dist/index.js
CHANGED
|
@@ -76,6 +76,48 @@ var GeminiModel = class extends BaseModel {
|
|
|
76
76
|
}
|
|
77
77
|
return await this.fetch(messages);
|
|
78
78
|
}
|
|
79
|
+
async fetchStreamWithContextMessage(messages, functions) {
|
|
80
|
+
const stream = await this.client.models.generateContentStream({
|
|
81
|
+
model: this.modelName,
|
|
82
|
+
contents: messages,
|
|
83
|
+
config: { tools: [{ functionDeclarations: functions }] }
|
|
84
|
+
});
|
|
85
|
+
return await this.createGeminiStreamAdapter(stream);
|
|
86
|
+
}
|
|
87
|
+
// NOTE(yoojin): Need to switch API Stream type to LLMStream.
|
|
88
|
+
createGeminiStreamAdapter(geminiStream) {
|
|
89
|
+
const hasName = (value) => {
|
|
90
|
+
return value.name !== void 0;
|
|
91
|
+
};
|
|
92
|
+
return {
|
|
93
|
+
async *[Symbol.asyncIterator]() {
|
|
94
|
+
for await (const geminiChunk of geminiStream) {
|
|
95
|
+
yield {
|
|
96
|
+
delta: {
|
|
97
|
+
role: geminiChunk.candidates?.[0]?.content?.role,
|
|
98
|
+
content: geminiChunk.candidates?.[0]?.content?.parts?.[0]?.text || void 0,
|
|
99
|
+
tool_calls: hasName(
|
|
100
|
+
geminiChunk.candidates?.[0]?.content?.parts?.[0]?.functionCall || {}
|
|
101
|
+
) ? [
|
|
102
|
+
{
|
|
103
|
+
index: 0,
|
|
104
|
+
id: geminiChunk.candidates?.[0]?.content?.parts?.[0]?.functionCall?.id || "id",
|
|
105
|
+
function: {
|
|
106
|
+
name: geminiChunk.candidates?.[0]?.content?.parts?.[0]?.functionCall?.name,
|
|
107
|
+
arguments: geminiChunk.candidates?.[0]?.content?.parts?.[0]?.functionCall?.args
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
] : void 0
|
|
111
|
+
},
|
|
112
|
+
finish_reason: geminiChunk.candidates?.[0]?.finishReason,
|
|
113
|
+
metadata: {
|
|
114
|
+
provider: "gemini"
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
}
|
|
79
121
|
convertToolsToFunctions(tools) {
|
|
80
122
|
const functions = [];
|
|
81
123
|
for (const tool of tools) {
|
package/dist/index.js.map
CHANGED
|
@@ -1 +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;
|
|
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\tLLMStream,\n\tStreamChunk,\n\tToolCallDelta,\n} from \"@ainetwork/adk/types/stream\";\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\ttype GenerateContentResponse,\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\tasync fetchStreamWithContextMessage(\n\t\tmessages: Content[],\n\t\tfunctions: FunctionDeclaration[],\n\t): Promise<LLMStream> {\n\t\tconst stream = await this.client.models.generateContentStream({\n\t\t\tmodel: this.modelName,\n\t\t\tcontents: messages,\n\t\t\tconfig: { tools: [{ functionDeclarations: functions }] },\n\t\t});\n\n\t\treturn await this.createGeminiStreamAdapter(stream);\n\t}\n\n\t// NOTE(yoojin): Need to switch API Stream type to LLMStream.\n\tprivate createGeminiStreamAdapter(\n\t\tgeminiStream: AsyncIterable<GenerateContentResponse>,\n\t): LLMStream {\n\t\tconst hasName = (\n\t\t\tvalue: FunctionCall,\n\t\t): value is FunctionCall & { name: string } => {\n\t\t\treturn value.name !== undefined;\n\t\t};\n\n\t\treturn {\n\t\t\tasync *[Symbol.asyncIterator](): AsyncIterator<StreamChunk> {\n\t\t\t\tfor await (const geminiChunk of geminiStream) {\n\t\t\t\t\tyield {\n\t\t\t\t\t\tdelta: {\n\t\t\t\t\t\t\trole: geminiChunk.candidates?.[0]?.content?.role,\n\t\t\t\t\t\t\tcontent:\n\t\t\t\t\t\t\t\tgeminiChunk.candidates?.[0]?.content?.parts?.[0]?.text ||\n\t\t\t\t\t\t\t\tundefined,\n\t\t\t\t\t\t\ttool_calls: hasName(\n\t\t\t\t\t\t\t\tgeminiChunk.candidates?.[0]?.content?.parts?.[0]\n\t\t\t\t\t\t\t\t\t?.functionCall || {},\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\t? ([\n\t\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\t\tindex: 0,\n\t\t\t\t\t\t\t\t\t\t\tid:\n\t\t\t\t\t\t\t\t\t\t\t\tgeminiChunk.candidates?.[0]?.content?.parts?.[0]\n\t\t\t\t\t\t\t\t\t\t\t\t\t?.functionCall?.id || \"id\",\n\t\t\t\t\t\t\t\t\t\t\tfunction: {\n\t\t\t\t\t\t\t\t\t\t\t\tname: geminiChunk.candidates?.[0]?.content?.parts?.[0]\n\t\t\t\t\t\t\t\t\t\t\t\t\t?.functionCall?.name,\n\t\t\t\t\t\t\t\t\t\t\t\targuments:\n\t\t\t\t\t\t\t\t\t\t\t\t\tgeminiChunk.candidates?.[0]?.content?.parts?.[0]\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t?.functionCall?.args,\n\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t] as ToolCallDelta[])\n\t\t\t\t\t\t\t\t: undefined,\n\t\t\t\t\t\t},\n\t\t\t\t\t\tfinish_reason: geminiChunk.candidates?.[0]?.finishReason as any,\n\t\t\t\t\t\tmetadata: {\n\t\t\t\t\t\t\tprovider: \"gemini\",\n\t\t\t\t\t\t},\n\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t},\n\t\t};\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;AAa7C,SAAS,0BAA0B;AACnC;AAAA,EAKC;AAAA,OACM;AAEA,IAAM,cAAN,cAA0B,UAAwC;AAAA,EAChE;AAAA,EACA;AAAA,EAER,YAAY,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AAAA,gBACX,YAAY,aAAa,CAAC,GAAG,SAAS,QAAQ,CAAC,GAC5C,gBAAgB,CAAC;AAAA,cACrB,IACI;AAAA,gBACD;AAAA,kBACC,OAAO;AAAA,kBACP,IACC,YAAY,aAAa,CAAC,GAAG,SAAS,QAAQ,CAAC,GAC5C,cAAc,MAAM;AAAA,kBACxB,UAAU;AAAA,oBACT,MAAM,YAAY,aAAa,CAAC,GAAG,SAAS,QAAQ,CAAC,GAClD,cAAc;AAAA,oBACjB,WACC,YAAY,aAAa,CAAC,GAAG,SAAS,QAAQ,CAAC,GAC5C,cAAc;AAAA,kBACnB;AAAA,gBACD;AAAA,cACD,IACC;AAAA,YACJ;AAAA,YACA,eAAe,YAAY,aAAa,CAAC,GAAG;AAAA,YAC5C,UAAU;AAAA,cACT,UAAU;AAAA,YACX;AAAA,UACD;AAAA,QACD;AAAA,MACD;AAAA,IACD;AAAA,EACD;AAAA,EAEA,wBAAwB,OAA4C;AACnE,UAAM,YAAmC,CAAC;AAC1C,eAAW,QAAQ,OAAO;AACzB,UAAI,CAAC,KAAK,SAAS;AAClB;AAAA,MACD;AACA,UAAI,KAAK,aAAa,mBAAmB,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/index.ts
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import { BaseModel } from "@ainetwork/adk/modules";
|
|
2
2
|
import { ChatRole, type SessionObject } from "@ainetwork/adk/types/memory";
|
|
3
|
+
import type {
|
|
4
|
+
LLMStream,
|
|
5
|
+
StreamChunk,
|
|
6
|
+
ToolCallDelta,
|
|
7
|
+
} from "@ainetwork/adk/types/stream";
|
|
3
8
|
import type {
|
|
4
9
|
FetchResponse,
|
|
5
10
|
IA2ATool,
|
|
@@ -12,6 +17,7 @@ import {
|
|
|
12
17
|
type Content,
|
|
13
18
|
type FunctionCall,
|
|
14
19
|
type FunctionDeclaration,
|
|
20
|
+
type GenerateContentResponse,
|
|
15
21
|
GoogleGenAI,
|
|
16
22
|
} from "@google/genai";
|
|
17
23
|
|
|
@@ -112,6 +118,69 @@ export class GeminiModel extends BaseModel<Content, FunctionDeclaration> {
|
|
|
112
118
|
return await this.fetch(messages);
|
|
113
119
|
}
|
|
114
120
|
|
|
121
|
+
async fetchStreamWithContextMessage(
|
|
122
|
+
messages: Content[],
|
|
123
|
+
functions: FunctionDeclaration[],
|
|
124
|
+
): Promise<LLMStream> {
|
|
125
|
+
const stream = await this.client.models.generateContentStream({
|
|
126
|
+
model: this.modelName,
|
|
127
|
+
contents: messages,
|
|
128
|
+
config: { tools: [{ functionDeclarations: functions }] },
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
return await this.createGeminiStreamAdapter(stream);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// NOTE(yoojin): Need to switch API Stream type to LLMStream.
|
|
135
|
+
private createGeminiStreamAdapter(
|
|
136
|
+
geminiStream: AsyncIterable<GenerateContentResponse>,
|
|
137
|
+
): LLMStream {
|
|
138
|
+
const hasName = (
|
|
139
|
+
value: FunctionCall,
|
|
140
|
+
): value is FunctionCall & { name: string } => {
|
|
141
|
+
return value.name !== undefined;
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
return {
|
|
145
|
+
async *[Symbol.asyncIterator](): AsyncIterator<StreamChunk> {
|
|
146
|
+
for await (const geminiChunk of geminiStream) {
|
|
147
|
+
yield {
|
|
148
|
+
delta: {
|
|
149
|
+
role: geminiChunk.candidates?.[0]?.content?.role,
|
|
150
|
+
content:
|
|
151
|
+
geminiChunk.candidates?.[0]?.content?.parts?.[0]?.text ||
|
|
152
|
+
undefined,
|
|
153
|
+
tool_calls: hasName(
|
|
154
|
+
geminiChunk.candidates?.[0]?.content?.parts?.[0]
|
|
155
|
+
?.functionCall || {},
|
|
156
|
+
)
|
|
157
|
+
? ([
|
|
158
|
+
{
|
|
159
|
+
index: 0,
|
|
160
|
+
id:
|
|
161
|
+
geminiChunk.candidates?.[0]?.content?.parts?.[0]
|
|
162
|
+
?.functionCall?.id || "id",
|
|
163
|
+
function: {
|
|
164
|
+
name: geminiChunk.candidates?.[0]?.content?.parts?.[0]
|
|
165
|
+
?.functionCall?.name,
|
|
166
|
+
arguments:
|
|
167
|
+
geminiChunk.candidates?.[0]?.content?.parts?.[0]
|
|
168
|
+
?.functionCall?.args,
|
|
169
|
+
},
|
|
170
|
+
},
|
|
171
|
+
] as ToolCallDelta[])
|
|
172
|
+
: undefined,
|
|
173
|
+
},
|
|
174
|
+
finish_reason: geminiChunk.candidates?.[0]?.finishReason as any,
|
|
175
|
+
metadata: {
|
|
176
|
+
provider: "gemini",
|
|
177
|
+
},
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
},
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
|
|
115
184
|
convertToolsToFunctions(tools: IAgentTool[]): FunctionDeclaration[] {
|
|
116
185
|
const functions: FunctionDeclaration[] = [];
|
|
117
186
|
for (const tool of tools) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ainetwork/adk-provider-model-gemini",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"author": "AI Network (https://ainetwork.ai)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
"clean": "rm -rf dist"
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@ainetwork/adk": "^0.1.
|
|
24
|
+
"@ainetwork/adk": "^0.1.7",
|
|
25
25
|
"@google/genai": "^1.11.0"
|
|
26
26
|
},
|
|
27
27
|
"devDependencies": {
|
|
@@ -31,5 +31,5 @@
|
|
|
31
31
|
"publishConfig": {
|
|
32
32
|
"access": "public"
|
|
33
33
|
},
|
|
34
|
-
"gitHead": "
|
|
34
|
+
"gitHead": "179c9e22bac074444e9a1ddf656eb52d6fe8d34a"
|
|
35
35
|
}
|