@wabot-dev/framework 0.1.0-beta.54 → 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.
@@ -17,7 +17,7 @@ class JwtRefreshTokenRepository {
17
17
  update(item) {
18
18
  throw new Error('Method not implemented.');
19
19
  }
20
- discard(item) {
20
+ delete(item) {
21
21
  throw new Error('Method not implemented.');
22
22
  }
23
23
  }
@@ -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 {
@@ -39,9 +39,6 @@ class Entity extends Storable {
39
39
  throw new Error('createdAt is required');
40
40
  }
41
41
  }
42
- discard() {
43
- this.data.discardedAt = new Date().getTime();
44
- }
45
42
  }
46
43
  /**
47
44
  * @deprecated Should use Entity
@@ -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
  }
@@ -20,7 +20,7 @@ let JobRepository = class JobRepository {
20
20
  update(item) {
21
21
  throw new Error('Method not implemented.');
22
22
  }
23
- discard(item) {
23
+ delete(item) {
24
24
  throw new Error('Method not implemented.');
25
25
  }
26
26
  };
@@ -71,14 +71,12 @@ class PgCrudRepository extends PgRepositoryBase {
71
71
  `;
72
72
  await this.exec(sql, [...this.values(item), item.id]);
73
73
  }
74
- async discard(item) {
75
- const _item = await this.find(item.id);
76
- if (!_item) {
77
- throw new Error('Not found');
78
- }
79
- item.discard();
80
- _item.discard();
81
- await this.update(_item);
74
+ async delete(item) {
75
+ const sql = `
76
+ DELETE FROM ${this.table}
77
+ WHERE id = $1
78
+ `;
79
+ await this.exec(sql, [item.id]);
82
80
  }
83
81
  }
84
82
 
@@ -46,7 +46,6 @@ declare class Entity<D extends IEntityData> extends Storable<D> {
46
46
  update(newData: Omit<D, 'id' | 'createdAt' | 'discardedAt'>): void;
47
47
  wasCreated(): boolean;
48
48
  validate(): void;
49
- discard(): void;
50
49
  }
51
50
  /**
52
51
  * @deprecated Should use IEntityData
@@ -165,7 +164,7 @@ interface ICrudRepository<T> {
165
164
  findAll(id: string): Promise<T[]>;
166
165
  create(item: T): Promise<void>;
167
166
  update(item: T): Promise<void>;
168
- discard(item: T): Promise<void>;
167
+ delete(item: T): Promise<void>;
169
168
  }
170
169
 
171
170
  declare function isModel(model: IConstructor<any>): (target: object, propertyKey: string | symbol) => void;
@@ -329,6 +328,7 @@ interface IJobData extends IEntityData {
329
328
  startedAt?: number;
330
329
  successAt?: number;
331
330
  failedAt?: number;
331
+ retryAt?: number;
332
332
  error?: {
333
333
  message: string;
334
334
  stack?: string;
@@ -337,6 +337,7 @@ interface IJobData extends IEntityData {
337
337
  }
338
338
  declare class Job extends Entity<IJobData> {
339
339
  get commandName(): string;
340
+ hasFinished(): boolean;
340
341
  setAsStarted(): void;
341
342
  setAsSuccess(): void;
342
343
  setAsFailed(error: Error): void;
@@ -352,7 +353,7 @@ declare class JobRepository implements IJobRepository {
352
353
  findAll(id: string): Promise<Job[]>;
353
354
  create(item: Job): Promise<void>;
354
355
  update(item: Job): Promise<void>;
355
- discard(item: Job): Promise<void>;
356
+ delete(item: Job): Promise<void>;
356
357
  }
357
358
 
358
359
  interface IJobEvent extends IStorableData {
@@ -820,7 +821,7 @@ declare class PgCrudRepository<P extends Entity<IEntityData>> extends PgReposito
820
821
  findAll(): Promise<P[]>;
821
822
  create(item: P): Promise<void>;
822
823
  update(item: P): Promise<void>;
823
- discard(item: P): Promise<void>;
824
+ delete(item: P): Promise<void>;
824
825
  }
825
826
 
826
827
  interface IEndPointConfig {
@@ -1083,7 +1084,7 @@ declare class JwtRefreshTokenRepository<D extends IStorableData> implements IJwt
1083
1084
  findAll(id: string): Promise<JwtRefreshToken<D>[]>;
1084
1085
  create(item: JwtRefreshToken<D>): Promise<void>;
1085
1086
  update(item: JwtRefreshToken<D>): Promise<void>;
1086
- discard(item: JwtRefreshToken<D>): Promise<void>;
1087
+ delete(item: JwtRefreshToken<D>): Promise<void>;
1087
1088
  }
1088
1089
 
1089
1090
  declare class Jwt {
@@ -1143,7 +1144,7 @@ declare class DeepSeekChatAdapter implements IChatAdapter {
1143
1144
 
1144
1145
  declare class GoogleChatAdapter implements IChatAdapter {
1145
1146
  private env;
1146
- private genai;
1147
+ private openai;
1147
1148
  private logger;
1148
1149
  constructor(env: Env);
1149
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.54",
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",