@wabot-dev/framework 0.1.0-beta.55 → 0.1.0-beta.56

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.
@@ -2,122 +2,120 @@ import { __decorate, __metadata } from 'tslib';
2
2
  import { Env } from '../../../core/env/Env.js';
3
3
  import { singleton } from '../../../core/injection/index.js';
4
4
  import { Logger } from '../../../core/logger/Logger.js';
5
- import { GoogleGenAI } from '@google/genai';
5
+ import OpenAI from 'openai';
6
6
 
7
7
  let GoogleChatAdapter = class GoogleChatAdapter {
8
8
  env;
9
- genai;
9
+ openai;
10
10
  logger = new Logger('wabot:google-chat-adapter');
11
11
  constructor(env) {
12
12
  this.env = env;
13
13
  const apiKey = this.env.requireString('GOOGLE_API_KEY');
14
- this.genai = new GoogleGenAI({ apiKey });
14
+ this.openai = new OpenAI({
15
+ apiKey,
16
+ baseURL: 'https://generativelanguage.googleapis.com/v1beta/openai/',
17
+ });
15
18
  }
16
19
  async nextItem(req) {
17
- const geminiInput = [];
18
- geminiInput.push(...this.mapChatItems(req.prevItems, req.systemPrompt));
20
+ const messages = [];
21
+ // Add system prompt as system message
22
+ messages.push({ role: 'system', content: req.systemPrompt });
23
+ // Add previous chat items
24
+ messages.push(...this.mapChatItems(req.prevItems));
19
25
  const tools = req.tools.map((x) => this.mapTool(x));
20
26
  const request = {
21
27
  model: req.model,
22
- contents: geminiInput,
28
+ messages,
23
29
  tools: tools.length > 0 ? tools : undefined,
30
+ tool_choice: 'auto',
24
31
  };
25
32
  this.logger.debug(`Call Gemini API with Request: ${JSON.stringify(request)}`);
26
- const response = await this.genai.models.generateContent(request);
33
+ const response = await this.openai.chat.completions.create(request);
27
34
  return this.mapResponse(response);
28
35
  }
29
- mapChatItems(chatItems, systemPrompt) {
30
- const geminiInput = [];
31
- if (systemPrompt) {
32
- geminiInput.push({ role: 'user', parts: [{ text: 'system: ' + systemPrompt }] });
33
- geminiInput.push({ role: 'model', parts: [{ text: 'I understand.' }] });
34
- }
36
+ mapChatItems(chatItems) {
37
+ const messages = [];
35
38
  for (const chatItem of chatItems) {
36
39
  switch (chatItem.type) {
37
40
  case 'humanMessage':
38
- geminiInput.push(this.mapHumanMessage(chatItem.humanMessage));
41
+ messages.push(this.mapHumanMessage(chatItem.humanMessage));
39
42
  break;
40
43
  case 'botMessage':
41
- geminiInput.push(this.mapBotMessage(chatItem.botMessage));
44
+ messages.push(this.mapBotMessage(chatItem.botMessage));
42
45
  break;
43
46
  case 'functionCall':
44
- geminiInput.push(...this.mapFunctionCall(chatItem.functionCall));
47
+ messages.push(...this.mapFunctionCall(chatItem.functionCall));
45
48
  break;
46
49
  }
47
50
  }
48
- return geminiInput;
51
+ return messages;
49
52
  }
50
53
  mapHumanMessage(item) {
51
54
  if (!item.text) {
52
55
  throw new Error('User message content is empty');
53
56
  }
54
- return { role: 'user', parts: [{ text: item.text }] };
57
+ return { role: 'user', content: item.text };
55
58
  }
56
59
  mapBotMessage(item) {
57
60
  if (!item.text) {
58
61
  throw new Error('Bot message content is empty');
59
62
  }
60
- return { role: 'model', parts: [{ text: item.text }] };
63
+ return { role: 'assistant', content: item.text };
61
64
  }
62
65
  mapFunctionCall(item) {
63
66
  return [
64
67
  {
65
- role: 'model',
66
- parts: [
68
+ role: 'assistant',
69
+ tool_calls: [
67
70
  {
68
- functionCall: {
71
+ id: item.id,
72
+ type: 'function',
73
+ function: {
69
74
  name: item.name,
70
- args: JSON.parse(item.arguments || '{}'),
75
+ arguments: item.arguments || '{}',
71
76
  },
72
77
  },
73
78
  ],
74
79
  },
75
80
  {
76
- role: 'user',
77
- parts: [
78
- {
79
- functionResponse: {
80
- name: item.name,
81
- response: {
82
- result: item.result || 'No result',
83
- },
84
- },
85
- },
86
- ],
81
+ role: 'tool',
82
+ tool_call_id: item.id,
83
+ content: item.result ?? 'No result',
87
84
  },
88
85
  ];
89
86
  }
90
87
  mapTool(tool) {
91
88
  return {
92
- functionDeclarations: [
93
- {
94
- name: tool.name,
95
- description: tool.description,
96
- parameters: {
97
- type: 'object',
98
- properties: tool.parameters.reduce((prev, param) => ({
99
- ...prev,
100
- [param.name]: { type: param.type, description: param.description },
101
- }), {}),
102
- required: tool.parameters.map((param) => param.name),
103
- },
89
+ type: 'function',
90
+ function: {
91
+ name: tool.name,
92
+ description: tool.description,
93
+ parameters: {
94
+ type: 'object',
95
+ properties: tool.parameters.reduce((prev, param) => ({
96
+ ...prev,
97
+ [param.name]: { type: param.type, description: param.description },
98
+ }), {}),
99
+ required: tool.parameters.map((param) => param.name),
100
+ additionalProperties: false,
104
101
  },
105
- ],
102
+ strict: true,
103
+ },
106
104
  };
107
105
  }
108
106
  mapResponse(response) {
109
107
  let chatItem;
110
- const part = response.response.candidates[0].content.parts[0];
111
- if (part.text) {
112
- chatItem = { type: 'botMessage', botMessage: { text: part.text } };
108
+ const { tool_calls: responseFunctionCall, content: responseText } = response.choices?.[0]?.message ?? {};
109
+ if (responseText) {
110
+ chatItem = { type: 'botMessage', botMessage: { text: responseText } };
113
111
  }
114
- else if (part.functionCall) {
112
+ else if (responseFunctionCall && responseFunctionCall[0]?.type === 'function') {
115
113
  chatItem = {
116
114
  type: 'functionCall',
117
115
  functionCall: {
118
- id: `gemini_${Date.now()}`,
119
- name: part.functionCall.name,
120
- arguments: JSON.stringify(part.functionCall.args || {}),
116
+ id: responseFunctionCall[0].id,
117
+ name: responseFunctionCall[0].function.name,
118
+ arguments: responseFunctionCall[0].function.arguments,
121
119
  },
122
120
  };
123
121
  }
@@ -125,10 +123,10 @@ let GoogleChatAdapter = class GoogleChatAdapter {
125
123
  throw new Error('Not supported Gemini Response');
126
124
  }
127
125
  let usage;
128
- if (response.response.usageMetadata) {
126
+ if (response.usage) {
129
127
  usage = {
130
- inputTokens: response.response.usageMetadata.promptTokenCount || 0,
131
- outputTokens: response.response.usageMetadata.candidatesTokenCount || 0,
128
+ inputTokens: response.usage.prompt_tokens,
129
+ outputTokens: response.usage.completion_tokens,
132
130
  };
133
131
  }
134
132
  else {
@@ -5,6 +5,9 @@ class Job extends Entity {
5
5
  get commandName() {
6
6
  return this.data.commandName;
7
7
  }
8
+ hasFinished() {
9
+ return this.data.successAt != null || this.data.failedAt != null;
10
+ }
8
11
  setAsStarted() {
9
12
  this.data.startedAt = new Date().getTime();
10
13
  }
@@ -328,6 +328,7 @@ interface IJobData extends IEntityData {
328
328
  startedAt?: number;
329
329
  successAt?: number;
330
330
  failedAt?: number;
331
+ retryAt?: number;
331
332
  error?: {
332
333
  message: string;
333
334
  stack?: string;
@@ -336,6 +337,7 @@ interface IJobData extends IEntityData {
336
337
  }
337
338
  declare class Job extends Entity<IJobData> {
338
339
  get commandName(): string;
340
+ hasFinished(): boolean;
339
341
  setAsStarted(): void;
340
342
  setAsSuccess(): void;
341
343
  setAsFailed(error: Error): void;
@@ -1142,7 +1144,7 @@ declare class DeepSeekChatAdapter implements IChatAdapter {
1142
1144
 
1143
1145
  declare class GoogleChatAdapter implements IChatAdapter {
1144
1146
  private env;
1145
- private genai;
1147
+ private openai;
1146
1148
  private logger;
1147
1149
  constructor(env: Env);
1148
1150
  nextItem(req: IChatAdapterNextItemReq): Promise<IChatAdapterNextItemRes>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wabot-dev/framework",
3
- "version": "0.1.0-beta.55",
3
+ "version": "0.1.0-beta.56",
4
4
  "description": "Framework for IA Chat Bots",
5
5
  "type": "module",
6
6
  "main": "dist/src/index.js",