@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
|
|
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 {
|
package/dist/src/index.d.ts
CHANGED
|
@@ -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
|
|
1147
|
+
private openai;
|
|
1146
1148
|
private logger;
|
|
1147
1149
|
constructor(env: Env);
|
|
1148
1150
|
nextItem(req: IChatAdapterNextItemReq): Promise<IChatAdapterNextItemRes>;
|