@wabot-dev/framework 0.1.0-beta.25 → 0.1.0-beta.27
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/ai/deepseek/DeepSeekChatBotAdapter.js +1 -1
- package/dist/src/ai/openia/OpenaiChatAdapter.js +101 -0
- package/dist/src/ai/openia/OpenaiChatBotAdapter.js +1 -1
- package/dist/src/index.d.ts +81 -5
- package/dist/src/index.js +4 -0
- package/dist/src/mindset/MindsetOperator.js +83 -0
- package/dist/src/repository/pg/PgCrudRepository.js +10 -0
- package/dist/src/validation/metadata/@isArray.js +18 -0
- package/dist/src/validation/metadata/ValidationMetadataStore.js +28 -0
- package/dist/src/validation/modelInfo.js +9 -0
- package/dist/src/validation/validators/validateArray.js +41 -0
- package/package.json +1 -1
|
@@ -92,7 +92,7 @@ let DeepSeekChatBotAdapter = class DeepSeekChatBotAdapter extends ChatBotAdapter
|
|
|
92
92
|
deepSeekInput.push({
|
|
93
93
|
role: 'tool',
|
|
94
94
|
tool_call_id: itemData.content.id,
|
|
95
|
-
content: itemData.content.result,
|
|
95
|
+
content: itemData.content.result ?? 'No result',
|
|
96
96
|
});
|
|
97
97
|
}
|
|
98
98
|
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { Logger } from '../../logger/Logger.js';
|
|
2
|
+
import { OpenAI } from 'openai';
|
|
3
|
+
|
|
4
|
+
class OpenaiChatAdapter {
|
|
5
|
+
openai = new OpenAI();
|
|
6
|
+
logger = new Logger('wabot:openai-chat-adapter');
|
|
7
|
+
async nextItem(req) {
|
|
8
|
+
const openIaInput = [];
|
|
9
|
+
openIaInput.push({ role: 'system', content: req.systemPrompt });
|
|
10
|
+
openIaInput.push(...this.mapChatItems(req.prevItems));
|
|
11
|
+
const tools = req.tools.map((x) => this.mapTool(x));
|
|
12
|
+
const response = await this.openai.responses.create({
|
|
13
|
+
model: req.model,
|
|
14
|
+
input: openIaInput,
|
|
15
|
+
tools,
|
|
16
|
+
});
|
|
17
|
+
return this.mapResponse(response);
|
|
18
|
+
}
|
|
19
|
+
mapChatItems(chatItems) {
|
|
20
|
+
const openIaInput = [];
|
|
21
|
+
for (const { type, content } of chatItems) {
|
|
22
|
+
switch (type) {
|
|
23
|
+
case 'CONNECTION_MESSAGE':
|
|
24
|
+
openIaInput.push(this.mapConectionMessage(content));
|
|
25
|
+
break;
|
|
26
|
+
case 'BOT_MESSAGE':
|
|
27
|
+
openIaInput.push(this.mapBotMessage(content));
|
|
28
|
+
break;
|
|
29
|
+
case 'FUNCTION_CALL':
|
|
30
|
+
openIaInput.push(...this.mapFunctionCall(content));
|
|
31
|
+
break;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return openIaInput;
|
|
35
|
+
}
|
|
36
|
+
mapConectionMessage(item) {
|
|
37
|
+
if (!item.text) {
|
|
38
|
+
throw new Error('System message content is empty');
|
|
39
|
+
}
|
|
40
|
+
return { role: 'user', content: item.text };
|
|
41
|
+
}
|
|
42
|
+
mapBotMessage(item) {
|
|
43
|
+
if (!item.text) {
|
|
44
|
+
throw new Error('System message content is empty');
|
|
45
|
+
}
|
|
46
|
+
return { role: 'assistant', content: item.text };
|
|
47
|
+
}
|
|
48
|
+
mapFunctionCall(item) {
|
|
49
|
+
return [
|
|
50
|
+
{
|
|
51
|
+
type: 'function_call',
|
|
52
|
+
call_id: item.id,
|
|
53
|
+
name: item.name,
|
|
54
|
+
arguments: JSON.stringify(item.arguments),
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
type: 'function_call_output',
|
|
58
|
+
call_id: item.id,
|
|
59
|
+
output: item.result ?? 'Not result',
|
|
60
|
+
},
|
|
61
|
+
];
|
|
62
|
+
}
|
|
63
|
+
mapTool(tool) {
|
|
64
|
+
return {
|
|
65
|
+
type: 'function',
|
|
66
|
+
name: tool.name,
|
|
67
|
+
description: tool.description,
|
|
68
|
+
parameters: {
|
|
69
|
+
type: 'object',
|
|
70
|
+
properties: tool.parameters.reduce((prev, param) => ({
|
|
71
|
+
...prev,
|
|
72
|
+
[param.name]: { type: param.type, description: param.description },
|
|
73
|
+
}), {}),
|
|
74
|
+
required: tool.parameters.map((param) => param.name),
|
|
75
|
+
},
|
|
76
|
+
strict: true,
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
mapResponse(response) {
|
|
80
|
+
let newItem;
|
|
81
|
+
if (response.output_text) {
|
|
82
|
+
newItem = { type: 'BOT_MESSAGE', content: { text: response.output_text } };
|
|
83
|
+
}
|
|
84
|
+
else if (response.output && response.output[0]?.type == 'function_call') {
|
|
85
|
+
newItem = {
|
|
86
|
+
type: 'FUNCTION_CALL',
|
|
87
|
+
content: {
|
|
88
|
+
id: response.output[0].call_id,
|
|
89
|
+
name: response.output[0].name,
|
|
90
|
+
arguments: response.output[0].arguments,
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
throw new Error('Not supported OpenIA Response');
|
|
96
|
+
}
|
|
97
|
+
return newItem;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export { OpenaiChatAdapter };
|
|
@@ -73,7 +73,7 @@ let OpenaiChatBotAdapter = class OpenaiChatBotAdapter extends ChatBotAdapter {
|
|
|
73
73
|
openIaInput.push({
|
|
74
74
|
type: 'function_call_output',
|
|
75
75
|
call_id: itemData.content.id,
|
|
76
|
-
output: itemData.content.result,
|
|
76
|
+
output: itemData.content.result ?? 'Not result',
|
|
77
77
|
});
|
|
78
78
|
}
|
|
79
79
|
}
|
package/dist/src/index.d.ts
CHANGED
|
@@ -117,7 +117,7 @@ interface IChatMessage extends IStorableData {
|
|
|
117
117
|
text?: string;
|
|
118
118
|
documents?: IChatDocument[];
|
|
119
119
|
images?: IChatImage[];
|
|
120
|
-
senderName
|
|
120
|
+
senderName?: string;
|
|
121
121
|
}
|
|
122
122
|
interface IConnectionChatMessage extends IChatMessage {
|
|
123
123
|
chatConnection: IChatConnection;
|
|
@@ -129,7 +129,7 @@ interface IChatFunctionCall {
|
|
|
129
129
|
id: string;
|
|
130
130
|
name: string;
|
|
131
131
|
arguments?: string;
|
|
132
|
-
result
|
|
132
|
+
result?: string;
|
|
133
133
|
}
|
|
134
134
|
|
|
135
135
|
type ISystemMessageItem = {
|
|
@@ -144,7 +144,8 @@ type ISystemFunctionCallItem = {
|
|
|
144
144
|
type: 'FUNCTION_CALL';
|
|
145
145
|
content: IChatFunctionCall;
|
|
146
146
|
};
|
|
147
|
-
type
|
|
147
|
+
type IChatItemRawData = ISystemMessageItem | IReceivedMessageItem | ISystemFunctionCallItem;
|
|
148
|
+
type IChatItemData = IEntityData & IChatItemRawData;
|
|
148
149
|
type IChatItemType = IChatItemData['type'];
|
|
149
150
|
declare class ChatItem extends Entity<IChatItemData> {
|
|
150
151
|
getType(): "BOT_MESSAGE" | "CONNECTION_MESSAGE" | "FUNCTION_CALL";
|
|
@@ -332,7 +333,17 @@ declare class MindsetOperator implements IMindset {
|
|
|
332
333
|
identity(): Promise<IMindsetIdentity>;
|
|
333
334
|
skills(): Promise<string>;
|
|
334
335
|
limits(): Promise<string>;
|
|
336
|
+
systemPrompt(): Promise<string>;
|
|
337
|
+
tools(): IChatTool[];
|
|
338
|
+
tool(fn: IMindsetFunctionMetadata, module: IMindsetModuleMetadata): IChatTool;
|
|
339
|
+
private toolParameter;
|
|
340
|
+
/**
|
|
341
|
+
* @deprecated use id
|
|
342
|
+
*/
|
|
335
343
|
callFunction(name: string, params: string): Promise<string>;
|
|
344
|
+
/**
|
|
345
|
+
* @deprecated use id
|
|
346
|
+
*/
|
|
336
347
|
allFunctionsDescriptors(): Promise<{
|
|
337
348
|
readonly type: "function";
|
|
338
349
|
readonly name: string;
|
|
@@ -343,7 +354,13 @@ declare class MindsetOperator implements IMindset {
|
|
|
343
354
|
readonly required: string[];
|
|
344
355
|
};
|
|
345
356
|
}[]>;
|
|
357
|
+
/**
|
|
358
|
+
* @deprecated use id
|
|
359
|
+
*/
|
|
346
360
|
private functionDescriptor;
|
|
361
|
+
/**
|
|
362
|
+
* @deprecated use id
|
|
363
|
+
*/
|
|
347
364
|
private toolParam;
|
|
348
365
|
}
|
|
349
366
|
|
|
@@ -385,6 +402,27 @@ declare class ChatBot implements IChatBot {
|
|
|
385
402
|
protected processLoop(callback: (message: IChatMessage) => void): Promise<void>;
|
|
386
403
|
}
|
|
387
404
|
|
|
405
|
+
interface IChatToolParameter {
|
|
406
|
+
type: string;
|
|
407
|
+
name: string;
|
|
408
|
+
description: string;
|
|
409
|
+
}
|
|
410
|
+
interface IChatTool {
|
|
411
|
+
language: string;
|
|
412
|
+
name: string;
|
|
413
|
+
description: string;
|
|
414
|
+
parameters: IChatToolParameter[];
|
|
415
|
+
}
|
|
416
|
+
interface IChatAdapterNextItemReq {
|
|
417
|
+
model: string;
|
|
418
|
+
systemPrompt: string;
|
|
419
|
+
tools: IChatTool[];
|
|
420
|
+
prevItems: IChatItemRawData[];
|
|
421
|
+
}
|
|
422
|
+
interface IChatAdapter {
|
|
423
|
+
nextItem(req: IChatAdapterNextItemReq): Promise<IChatItemRawData>;
|
|
424
|
+
}
|
|
425
|
+
|
|
388
426
|
declare class DeepSeekChatBotAdapter extends ChatBotAdapter {
|
|
389
427
|
private deepSeek;
|
|
390
428
|
private model;
|
|
@@ -403,6 +441,18 @@ declare class OpenaiChatBotAdapter extends ChatBotAdapter {
|
|
|
403
441
|
private mapChatItems;
|
|
404
442
|
}
|
|
405
443
|
|
|
444
|
+
declare class OpenaiChatAdapter implements IChatAdapter {
|
|
445
|
+
private openai;
|
|
446
|
+
private logger;
|
|
447
|
+
nextItem(req: IChatAdapterNextItemReq): Promise<IChatItemRawData>;
|
|
448
|
+
private mapChatItems;
|
|
449
|
+
private mapConectionMessage;
|
|
450
|
+
private mapBotMessage;
|
|
451
|
+
private mapFunctionCall;
|
|
452
|
+
private mapTool;
|
|
453
|
+
private mapResponse;
|
|
454
|
+
}
|
|
455
|
+
|
|
406
456
|
declare class ClaudeChatBotAdapter extends ChatBotAdapter {
|
|
407
457
|
private anthropic;
|
|
408
458
|
private model;
|
|
@@ -465,6 +515,7 @@ declare class Job extends Entity<IJobData> {
|
|
|
465
515
|
interface ICrudRepository<T> {
|
|
466
516
|
find(id: string): Promise<T | null>;
|
|
467
517
|
findOrThrow(id: string): Promise<T>;
|
|
518
|
+
findByIds(ids: string[]): Promise<T[]>;
|
|
468
519
|
findAll(id: string): Promise<T[]>;
|
|
469
520
|
create(item: T): Promise<void>;
|
|
470
521
|
update(item: T): Promise<void>;
|
|
@@ -515,6 +566,7 @@ declare class PgCrudRepository<P extends Entity<IEntityData>> extends PgReposito
|
|
|
515
566
|
protected readonly config: IPgRepositoryConfig<P>;
|
|
516
567
|
constructor(pool: Pool, config: IPgRepositoryConfig<P>);
|
|
517
568
|
find(id: string): Promise<P | null>;
|
|
569
|
+
findByIds(ids: string[]): Promise<P[]>;
|
|
518
570
|
findOrThrow(id: string): Promise<P>;
|
|
519
571
|
findAll(): Promise<P[]>;
|
|
520
572
|
create(item: P): Promise<void>;
|
|
@@ -1289,6 +1341,9 @@ interface IModelValidationError extends IValidationError {
|
|
|
1289
1341
|
[key: string]: string[];
|
|
1290
1342
|
};
|
|
1291
1343
|
}
|
|
1344
|
+
interface IArrayValidationError extends IValidationError {
|
|
1345
|
+
items: (IValidationError[] | null)[];
|
|
1346
|
+
}
|
|
1292
1347
|
type IValidationResult<V> = {
|
|
1293
1348
|
value: V;
|
|
1294
1349
|
error?: undefined;
|
|
@@ -1303,7 +1358,14 @@ type IModelValidationResult<V> = {
|
|
|
1303
1358
|
value?: undefined;
|
|
1304
1359
|
error: IModelValidationError;
|
|
1305
1360
|
};
|
|
1306
|
-
type
|
|
1361
|
+
type IArrayValidationResult<V> = {
|
|
1362
|
+
value: V[];
|
|
1363
|
+
error?: undefined;
|
|
1364
|
+
} | {
|
|
1365
|
+
value?: undefined;
|
|
1366
|
+
error: IArrayValidationError;
|
|
1367
|
+
};
|
|
1368
|
+
type IValidator<V = any, O = any> = (value: V, options: O) => IValidationResult<V>;
|
|
1307
1369
|
interface IPropertyValidatorInfo {
|
|
1308
1370
|
propertyName: string;
|
|
1309
1371
|
validator: IValidator;
|
|
@@ -1333,6 +1395,18 @@ declare class ValidationMetadataStore {
|
|
|
1333
1395
|
private getConstructorPropertiesValidatorsInfo;
|
|
1334
1396
|
}
|
|
1335
1397
|
|
|
1398
|
+
interface IValidateArrayOptions {
|
|
1399
|
+
}
|
|
1400
|
+
interface IValidateArrayOptionsWithItemsValidators extends IValidateArrayOptions {
|
|
1401
|
+
itemsValidator?: {
|
|
1402
|
+
validator: IValidator;
|
|
1403
|
+
options: any;
|
|
1404
|
+
}[];
|
|
1405
|
+
}
|
|
1406
|
+
declare function validateArray(value: any, options?: IValidateArrayOptionsWithItemsValidators): IArrayValidationResult<any>;
|
|
1407
|
+
|
|
1408
|
+
declare function isArray(options?: IValidateArrayOptions): (target: object, propertyKey: string | symbol) => void;
|
|
1409
|
+
|
|
1336
1410
|
declare function validateIsBoolean(value: any): IValidationResult<boolean>;
|
|
1337
1411
|
|
|
1338
1412
|
declare function validateIsDate(value: any): IValidationResult<Date>;
|
|
@@ -1359,4 +1433,6 @@ declare function validateModel<V>(value: any, info: IModelValidatorsInfo<V>): IM
|
|
|
1359
1433
|
|
|
1360
1434
|
declare function validate<V>(value: any, modelConstructor: IConstructor<V>): IModelValidationResult<V>;
|
|
1361
1435
|
|
|
1362
|
-
|
|
1436
|
+
declare function modelInfo<V>(modelConstructor: IConstructor<V>): IModelValidatorsInfo<V>;
|
|
1437
|
+
|
|
1438
|
+
export { Async, Auth, AuthenticationModule, Chat, ChatBot, ChatBotAdapter, ChatBotMetadataStore, ChatItem, ChatMemory, ChatRepository, ChatResolver, ClaudeChatBotAdapter, CmdChannel, Command, CommandMetadataStore, Container, ControllerMetadataStore, CustomError, DeepSeekChatBotAdapter, EmailService, Entity, Env, EnvWhatsAppRepository, ExpressProvider, HtmlModule, HttpServerProvider, type IArrayValidationError, type IArrayValidationResult, type IChannelMetadata, type IChatAdapter, type IChatAdapterNextItemReq, type IChatBot, type IChatBotAdapter, type IChatBotMetadata, type IChatChannel, type IChatConnection, type IChatControllerMetadata, type IChatData, type IChatFunctionCall, type IChatItemData, type IChatItemRawData, type IChatItemType, type IChatMemory, type IChatMessage, type IChatRepository, type IChatTool, type IChatToolParameter, type IChatType, type ICommandConfig, type ICommandHandler, type ICommandHandlerConfig, type IConnectionChatMessage, type IConstructor, type ICrudRepository, type ICustomErrorData, type IEmailService, type IEndPointMetadata, type IEntityData, type IEnvType, type IGetConfig, type IGetWhatsAppTemplateRequest, type IHtmlModuleOptions, type IJobData, type IJobEvent, type IJobEventListener, type IJwtRefreshTokenData, type IListenWhatsAppMessageRequest, type IMessageContext, type IMessageMetadata, type IMiddleware, type IMiddlewareMetadata, type IMindset, type IMindsetDecoration, type IMindsetFunctionConfig, type IMindsetFunctionDecoration, type IMindsetFunctionMetadata, type IMindsetFunctionParamMetadata, type IMindsetIdentity, type IMindsetMetadata, type IMindsetModuleConfig, type IMindsetModuleDecoration, type IMindsetModuleMetadata, type IModelValidationError, type IModelValidationResult, type IModelValidatorsInfo, type IOtpService, type IParamConfig, type IParamDecoration, type IPersistentData, type IPgRepositoryConfig, type IPostConfig, type IPrimitive, type IPropertyValidatorInfo, type IReceivedMessage, type IReceivedMessageItem, type IRestControllerConfig, type IRestControllerMetadata, type ISendEmailRequest, type ISendWhatsAppRequest, type ISendWhatsAppTemplateRequest, type IServerConfig, type IServerProvider, type ISocketChannelConfig, type ISocketChannelReceivedMessage, type IStorableData, type ISystemFunctionCallItem, type ISystemMessageItem, type ITelegramChannelConfig, type IUserConnection, type IUserData, type IUserRepository, type IValidateArrayOptions, type IValidateArrayOptionsWithItemsValidators, type IValidateMaxOptions, type IValidateMinOptions, type IValidationError, type IValidationResult, type IValidator, type IValidatorMetadata, type IWhatsAppBusinessAccount, type IWhatsAppBusinessNumber, type IWhatsAppContact, type IWhatsAppData, type IWhatsAppMessage, type IWhatsAppMessageListener, type IWhatsAppRepository, type IWhatsAppSenderOptions, type IWhatsAppTemplate, type IWhatsAppTemplateComponent, type IWhatsAppTemplateMessage, type IWhatsAppTemplateParameter, type IWhatsAppTemplateResponse, type IWhatsAppWebhookPayload, type IWhatsappChannelConfig, type IchatControllerConfig, type IrunChannelProps, Job, JobRepository, JobRunner, JobsEventsHub, Jwt, JwtAccessAndRefreshTokenDto, JwtConfig, JwtGuardMiddleware, JwtRefreshToken, JwtRefreshTokenRepository, JwtSigner, JwtTokenDto, Lifecycle, Logger, MINDSET_DECORATION_MINDSET, MINDSET_FUNCTION_DECORATION_FUNCTION, MINDSET_MODULE_DECORATION_MODULE, Mapper, MessageContext, Mindset, MindsetMetadataStore, MindsetOperator, OpenaiChatAdapter, OpenaiChatBotAdapter, OtpService, PARAM_DECORATION_IS_OPTIONAL, PARAM_DECORATION_PARAM, Persistent, PgChatMemory, PgChatRepository, PgCrudRepository, PgRepositoryBase, PgUserRepository, PgWhatsAppRepository, RamChatMemory, RamChatRepository, RamUserRepository, Random, RegisterUserModule, RegisterUserWithEmailRequest, RestControllerMetadataStore, SendOneTimePasswordRequest, SocketChannel, SocketChannelConfig, SocketServerProvider, Storable, TelegramChannel, TelegramChannelConfig, User, UserRepository, UserResolver, ValidateOneTimePasswordRequest, ValidationMetadataStore, WhatsApp, WhatsAppChannel, WhatsAppReceiver, WhatsAppReceiverByDevConnection, WhatsAppReceiverByWebHook, WhatsAppRepository, WhatsAppSender, WhatsAppSenderByCloudApi, WhatsAppSenderByDevConnection, WhatsappChannelConfig, chatBot, chatController, cmd, command, commandHandler, container, get, inject, injectable, isArray, isBoolean, isDate, isModel, isNotEmpty, isNumber, isOptional, isPresent, isString, jwtGuard, max, middleware, min, mindset, mindsetFunction, mindsetModule, modelInfo, param, post, prepareChatContainer, restController, runAsyncCommandHandlers, runChannel, runRestControllers, runServer, scoped, singleton, socket, telegram, validate, validateArray, validateIsBoolean, validateIsDate, validateIsNotEmpty, validateIsNumber, validateIsPresent, validateIsString, validateMax, validateMin, validateModel, whatsapp };
|
package/dist/src/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export { DeepSeekChatBotAdapter } from './ai/deepseek/DeepSeekChatBotAdapter.js';
|
|
2
2
|
export { OpenaiChatBotAdapter } from './ai/openia/OpenaiChatBotAdapter.js';
|
|
3
|
+
export { OpenaiChatAdapter } from './ai/openia/OpenaiChatAdapter.js';
|
|
3
4
|
export { ClaudeChatBotAdapter } from './ai/claude/ClaudeChatBotAdapter.js';
|
|
4
5
|
export { command } from './async/@command.js';
|
|
5
6
|
export { commandHandler } from './async/@commandHandler.js';
|
|
@@ -115,6 +116,7 @@ export { isString } from './validation/metadata/@isString.js';
|
|
|
115
116
|
export { max } from './validation/metadata/@max.js';
|
|
116
117
|
export { min } from './validation/metadata/@min.js';
|
|
117
118
|
export { ValidationMetadataStore } from './validation/metadata/ValidationMetadataStore.js';
|
|
119
|
+
export { isArray } from './validation/metadata/@isArray.js';
|
|
118
120
|
export { validateIsBoolean } from './validation/validators/validateIsBoolean.js';
|
|
119
121
|
export { validateIsDate } from './validation/validators/validateIsDate.js';
|
|
120
122
|
export { validateIsNotEmpty } from './validation/validators/validateIsNotEmpty.js';
|
|
@@ -124,5 +126,7 @@ export { validateMax } from './validation/validators/validateMax.js';
|
|
|
124
126
|
export { validateMin } from './validation/validators/validateMin.js';
|
|
125
127
|
export { validateIsPresent } from './validation/validators/validateIsPresent.js';
|
|
126
128
|
export { validateModel } from './validation/validators/validateModel.js';
|
|
129
|
+
export { validateArray } from './validation/validators/validateArray.js';
|
|
127
130
|
export { validate } from './validation/validate.js';
|
|
131
|
+
export { modelInfo } from './validation/modelInfo.js';
|
|
128
132
|
export { Container } from './injection/Container.js';
|
|
@@ -22,6 +22,80 @@ let MindsetOperator = class MindsetOperator {
|
|
|
22
22
|
limits() {
|
|
23
23
|
return this.mindset.limits();
|
|
24
24
|
}
|
|
25
|
+
async systemPrompt() {
|
|
26
|
+
let [identity, skills, limits] = await Promise.all([
|
|
27
|
+
this.identity(),
|
|
28
|
+
this.skills(),
|
|
29
|
+
this.limits(),
|
|
30
|
+
]);
|
|
31
|
+
const language = identity.language.replaceAll('#', ' ');
|
|
32
|
+
const name = identity.name.replaceAll('#', ' ');
|
|
33
|
+
const age = identity.age ? identity.age.toString().replaceAll('#', ' ') : null;
|
|
34
|
+
const personality = identity.personality ? identity.personality.replaceAll('#', ' ') : null;
|
|
35
|
+
skills = skills.replaceAll('#', ' ');
|
|
36
|
+
limits = limits.replaceAll('#', ' ');
|
|
37
|
+
const systemPrompt = `
|
|
38
|
+
# System Instructions
|
|
39
|
+
you should act as a assistant.
|
|
40
|
+
your main language is ${language}.
|
|
41
|
+
your name is ${name}.
|
|
42
|
+
${age ? 'you are ' + age + ' years old.' : ''}
|
|
43
|
+
|
|
44
|
+
${personality ? '## Personality (in your main language) \n' + personality : ''}
|
|
45
|
+
|
|
46
|
+
## Skills (in your main language)
|
|
47
|
+
${skills}
|
|
48
|
+
|
|
49
|
+
## System limitations (in your main language)
|
|
50
|
+
${limits}
|
|
51
|
+
|
|
52
|
+
## Chat memory
|
|
53
|
+
Next you will receive a chat history,
|
|
54
|
+
you should use this information to answer the user.
|
|
55
|
+
`;
|
|
56
|
+
return systemPrompt;
|
|
57
|
+
}
|
|
58
|
+
tools() {
|
|
59
|
+
return this.metadata.modules
|
|
60
|
+
.map((module) => module.functions.map((fn) => this.tool(fn, module)))
|
|
61
|
+
.flat();
|
|
62
|
+
}
|
|
63
|
+
tool(fn, module) {
|
|
64
|
+
const description = fn.config.description.replaceAll('#', ' ');
|
|
65
|
+
return {
|
|
66
|
+
language: module.config.language ?? 'english',
|
|
67
|
+
name: fn.name,
|
|
68
|
+
description,
|
|
69
|
+
parameters: fn.params.map((param) => this.toolParameter(param)),
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
toolParameter(param) {
|
|
73
|
+
let description = `
|
|
74
|
+
### description (in your main language)
|
|
75
|
+
${param.config.description.replaceAll('#', ' ')}
|
|
76
|
+
`;
|
|
77
|
+
const type = (() => {
|
|
78
|
+
if (param.type === Number)
|
|
79
|
+
return 'number';
|
|
80
|
+
if (param.type === String)
|
|
81
|
+
return 'string';
|
|
82
|
+
if (param.type === Date) {
|
|
83
|
+
description = `${description}
|
|
84
|
+
### format: ISO 8681 - YYYY-MM-DDTHH:mm:ssZ
|
|
85
|
+
`;
|
|
86
|
+
return 'string';
|
|
87
|
+
}
|
|
88
|
+
throw new Error(`Unsupported type`);
|
|
89
|
+
})();
|
|
90
|
+
return {
|
|
91
|
+
type,
|
|
92
|
+
name: param.name,
|
|
93
|
+
description,
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* @deprecated use id
|
|
98
|
+
*/
|
|
25
99
|
async callFunction(name, params) {
|
|
26
100
|
const fnMetadata = this.metadata.modules
|
|
27
101
|
.map((module) => module.functions)
|
|
@@ -43,11 +117,17 @@ let MindsetOperator = class MindsetOperator {
|
|
|
43
117
|
return `Error: ${error}`;
|
|
44
118
|
}
|
|
45
119
|
}
|
|
120
|
+
/**
|
|
121
|
+
* @deprecated use id
|
|
122
|
+
*/
|
|
46
123
|
async allFunctionsDescriptors() {
|
|
47
124
|
return this.metadata.modules
|
|
48
125
|
.map((module) => module.functions.map((fn) => this.functionDescriptor(fn)))
|
|
49
126
|
.flat();
|
|
50
127
|
}
|
|
128
|
+
/**
|
|
129
|
+
* @deprecated use id
|
|
130
|
+
*/
|
|
51
131
|
functionDescriptor(fn) {
|
|
52
132
|
const description = fn.config.description.replaceAll('#', ' ');
|
|
53
133
|
return {
|
|
@@ -64,6 +144,9 @@ let MindsetOperator = class MindsetOperator {
|
|
|
64
144
|
},
|
|
65
145
|
};
|
|
66
146
|
}
|
|
147
|
+
/**
|
|
148
|
+
* @deprecated use id
|
|
149
|
+
*/
|
|
67
150
|
toolParam(param) {
|
|
68
151
|
const addons = {
|
|
69
152
|
description: `
|
|
@@ -18,6 +18,16 @@ class PgCrudRepository extends PgRepositoryBase {
|
|
|
18
18
|
const items = await this.query(sql, [id]);
|
|
19
19
|
return items.at(0) ?? null;
|
|
20
20
|
}
|
|
21
|
+
async findByIds(ids) {
|
|
22
|
+
const sql = `
|
|
23
|
+
SELECT ${this.columns}
|
|
24
|
+
FROM ${this.table}
|
|
25
|
+
WHERE id IN (${ids.map((_, i) => '$' + (i + 1)).join(',')})
|
|
26
|
+
LIMIT 1
|
|
27
|
+
`;
|
|
28
|
+
const items = await this.query(sql, ids);
|
|
29
|
+
return items;
|
|
30
|
+
}
|
|
21
31
|
async findOrThrow(id) {
|
|
22
32
|
const item = await this.find(id);
|
|
23
33
|
if (!item) {
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { container } from '../../injection/index.js';
|
|
2
|
+
import { validateArray } from '../validators/validateArray.js';
|
|
3
|
+
import { ValidationMetadataStore } from './ValidationMetadataStore.js';
|
|
4
|
+
|
|
5
|
+
function isArray(options) {
|
|
6
|
+
return function (target, propertyKey) {
|
|
7
|
+
const propertyName = propertyKey.toString();
|
|
8
|
+
const store = container.resolve(ValidationMetadataStore);
|
|
9
|
+
store.saveValidatorMetadata({
|
|
10
|
+
modelConstructor: target.constructor,
|
|
11
|
+
propertyName,
|
|
12
|
+
validator: validateArray,
|
|
13
|
+
validatorOptions: options,
|
|
14
|
+
});
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export { isArray };
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { __decorate } from 'tslib';
|
|
2
2
|
import { singleton } from '../../injection/index.js';
|
|
3
3
|
import { _IS_OPTIONAL_DUMMY_VALIDATOR_ } from '../validators/validateIsOptional.js';
|
|
4
|
+
import { validateArray } from '../validators/validateArray.js';
|
|
4
5
|
|
|
5
6
|
function getClassHierarchy(cls) {
|
|
6
7
|
const classes = [];
|
|
@@ -24,6 +25,33 @@ let ValidationMetadataStore = class ValidationMetadataStore {
|
|
|
24
25
|
modelValidators.set(validatorMetadata.propertyName, propertyValidators);
|
|
25
26
|
}
|
|
26
27
|
propertyValidators.unshift(validatorMetadata);
|
|
28
|
+
const arrayValidatorMetadata = propertyValidators.find((x) => x.validator === validateArray);
|
|
29
|
+
if (!arrayValidatorMetadata) {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
if (!arrayValidatorMetadata.validatorOptions) {
|
|
33
|
+
arrayValidatorMetadata.validatorOptions = {};
|
|
34
|
+
}
|
|
35
|
+
const arrayValidatorOptions = arrayValidatorMetadata.validatorOptions;
|
|
36
|
+
if (!arrayValidatorOptions.itemsValidator) {
|
|
37
|
+
arrayValidatorOptions.itemsValidator = [];
|
|
38
|
+
}
|
|
39
|
+
const removeValidatorsMetadata = [];
|
|
40
|
+
for (const validatorMetadata of propertyValidators) {
|
|
41
|
+
if (validatorMetadata.validator === validateArray ||
|
|
42
|
+
validatorMetadata.validator === _IS_OPTIONAL_DUMMY_VALIDATOR_) {
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
arrayValidatorOptions.itemsValidator.push({
|
|
46
|
+
options: validatorMetadata.validatorOptions,
|
|
47
|
+
validator: validatorMetadata.validator,
|
|
48
|
+
});
|
|
49
|
+
removeValidatorsMetadata.push(validatorMetadata);
|
|
50
|
+
}
|
|
51
|
+
for (const toRemove of removeValidatorsMetadata) {
|
|
52
|
+
const indexToRemove = propertyValidators.indexOf(toRemove);
|
|
53
|
+
propertyValidators.splice(indexToRemove, 1);
|
|
54
|
+
}
|
|
27
55
|
}
|
|
28
56
|
getModelValidatorsInfo(modelConstructor) {
|
|
29
57
|
const constructors = getClassHierarchy(modelConstructor);
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { container } from '../injection/index.js';
|
|
2
|
+
import { ValidationMetadataStore } from './metadata/ValidationMetadataStore.js';
|
|
3
|
+
|
|
4
|
+
function modelInfo(modelConstructor) {
|
|
5
|
+
const metadataStore = container.resolve(ValidationMetadataStore);
|
|
6
|
+
return metadataStore.getModelValidatorsInfo(modelConstructor);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export { modelInfo };
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
function validateArray(value, options) {
|
|
2
|
+
if (!Array.isArray(value)) {
|
|
3
|
+
return {
|
|
4
|
+
error: { description: 'Should be an array', items: [] },
|
|
5
|
+
};
|
|
6
|
+
}
|
|
7
|
+
const { itemsValidator } = options ?? {};
|
|
8
|
+
const valueOut = [];
|
|
9
|
+
const errorItems = [];
|
|
10
|
+
for (const item of value) {
|
|
11
|
+
let itemOut = item;
|
|
12
|
+
const itemErrors = [];
|
|
13
|
+
for (const itemValidator of itemsValidator ?? []) {
|
|
14
|
+
const { error, value } = itemValidator.validator(itemOut, itemValidator.options);
|
|
15
|
+
if (error) {
|
|
16
|
+
itemErrors.push(error);
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
itemOut = value;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
if (itemErrors.length == 0) {
|
|
23
|
+
valueOut.push(itemOut);
|
|
24
|
+
errorItems.push(null);
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
valueOut.push(null);
|
|
28
|
+
errorItems.push(itemErrors);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
if (errorItems.some((x) => x != null)) {
|
|
32
|
+
return {
|
|
33
|
+
error: { description: 'Error on some items', items: errorItems },
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
return {
|
|
37
|
+
value: valueOut,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export { validateArray };
|