@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.
- package/dist/src/addon/auth/jwt/JwtRefreshTokenRepository.js +1 -1
- package/dist/src/addon/chat-bot/google/GoogleChatAdapter.js +55 -57
- package/dist/src/core/entity/Entity.js +0 -3
- package/dist/src/feature/async/Job.js +3 -0
- package/dist/src/feature/async/JobRepository.js +1 -1
- package/dist/src/feature/pg/PgCrudRepository.js +6 -8
- package/dist/src/index.d.ts +7 -6
- package/package.json +1 -1
|
@@ -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
|
|
5
|
+
import OpenAI from 'openai';
|
|
6
6
|
|
|
7
7
|
let GoogleChatAdapter = class GoogleChatAdapter {
|
|
8
8
|
env;
|
|
9
|
-
|
|
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.
|
|
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
|
|
18
|
-
|
|
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
|
-
|
|
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.
|
|
33
|
+
const response = await this.openai.chat.completions.create(request);
|
|
27
34
|
return this.mapResponse(response);
|
|
28
35
|
}
|
|
29
|
-
mapChatItems(chatItems
|
|
30
|
-
const
|
|
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
|
-
|
|
41
|
+
messages.push(this.mapHumanMessage(chatItem.humanMessage));
|
|
39
42
|
break;
|
|
40
43
|
case 'botMessage':
|
|
41
|
-
|
|
44
|
+
messages.push(this.mapBotMessage(chatItem.botMessage));
|
|
42
45
|
break;
|
|
43
46
|
case 'functionCall':
|
|
44
|
-
|
|
47
|
+
messages.push(...this.mapFunctionCall(chatItem.functionCall));
|
|
45
48
|
break;
|
|
46
49
|
}
|
|
47
50
|
}
|
|
48
|
-
return
|
|
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',
|
|
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: '
|
|
63
|
+
return { role: 'assistant', content: item.text };
|
|
61
64
|
}
|
|
62
65
|
mapFunctionCall(item) {
|
|
63
66
|
return [
|
|
64
67
|
{
|
|
65
|
-
role: '
|
|
66
|
-
|
|
68
|
+
role: 'assistant',
|
|
69
|
+
tool_calls: [
|
|
67
70
|
{
|
|
68
|
-
|
|
71
|
+
id: item.id,
|
|
72
|
+
type: 'function',
|
|
73
|
+
function: {
|
|
69
74
|
name: item.name,
|
|
70
|
-
|
|
75
|
+
arguments: item.arguments || '{}',
|
|
71
76
|
},
|
|
72
77
|
},
|
|
73
78
|
],
|
|
74
79
|
},
|
|
75
80
|
{
|
|
76
|
-
role: '
|
|
77
|
-
|
|
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
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
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
|
|
111
|
-
if (
|
|
112
|
-
chatItem = { type: 'botMessage', botMessage: { 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 (
|
|
112
|
+
else if (responseFunctionCall && responseFunctionCall[0]?.type === 'function') {
|
|
115
113
|
chatItem = {
|
|
116
114
|
type: 'functionCall',
|
|
117
115
|
functionCall: {
|
|
118
|
-
id:
|
|
119
|
-
name:
|
|
120
|
-
arguments:
|
|
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.
|
|
126
|
+
if (response.usage) {
|
|
129
127
|
usage = {
|
|
130
|
-
inputTokens: response.
|
|
131
|
-
outputTokens: response.
|
|
128
|
+
inputTokens: response.usage.prompt_tokens,
|
|
129
|
+
outputTokens: response.usage.completion_tokens,
|
|
132
130
|
};
|
|
133
131
|
}
|
|
134
132
|
else {
|
|
@@ -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
|
|
75
|
-
const
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
item.
|
|
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
|
|
package/dist/src/index.d.ts
CHANGED
|
@@ -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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
1147
|
+
private openai;
|
|
1147
1148
|
private logger;
|
|
1148
1149
|
constructor(env: Env);
|
|
1149
1150
|
nextItem(req: IChatAdapterNextItemReq): Promise<IChatAdapterNextItemRes>;
|