@wabot-dev/framework 0.1.0-beta.11 → 0.1.0-beta.13

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.
Files changed (61) hide show
  1. package/dist/src/ai/claude/ClaudeChatBotAdapter.js +115 -0
  2. package/dist/src/channels/whatsapp/WhatsApp.js +2 -2
  3. package/dist/src/channels/whatsapp/WhatsAppSender.js +10 -1
  4. package/dist/src/channels/whatsapp/WhatsAppSenderByCloudApi.js +3 -9
  5. package/dist/src/channels/whatsapp/WhatsAppSenderByDevConnection.js +6 -11
  6. package/dist/src/core/{Persistent.js → Entity.js} +24 -9
  7. package/dist/src/core/Storable.js +8 -0
  8. package/dist/src/core/chat/Chat.js +2 -2
  9. package/dist/src/core/chat/ChatItem.js +2 -2
  10. package/dist/src/core/user/User.js +2 -2
  11. package/dist/src/error/CustomError.js +1 -0
  12. package/dist/src/index.d.ts +250 -181
  13. package/dist/src/index.js +13 -9
  14. package/dist/src/node_modules/@anthropic-ai/sdk/_vendor/partial-json-parser/parser.js +221 -0
  15. package/dist/src/node_modules/@anthropic-ai/sdk/client.js +540 -0
  16. package/dist/src/node_modules/@anthropic-ai/sdk/core/api-promise.js +74 -0
  17. package/dist/src/node_modules/@anthropic-ai/sdk/core/error.js +100 -0
  18. package/dist/src/node_modules/@anthropic-ai/sdk/core/pagination.js +119 -0
  19. package/dist/src/node_modules/@anthropic-ai/sdk/core/resource.js +8 -0
  20. package/dist/src/node_modules/@anthropic-ai/sdk/core/streaming.js +283 -0
  21. package/dist/src/node_modules/@anthropic-ai/sdk/internal/constants.js +16 -0
  22. package/dist/src/node_modules/@anthropic-ai/sdk/internal/decoders/jsonl.js +37 -0
  23. package/dist/src/node_modules/@anthropic-ai/sdk/internal/decoders/line.js +110 -0
  24. package/dist/src/node_modules/@anthropic-ai/sdk/internal/detect-platform.js +159 -0
  25. package/dist/src/node_modules/@anthropic-ai/sdk/internal/errors.js +37 -0
  26. package/dist/src/node_modules/@anthropic-ai/sdk/internal/headers.js +71 -0
  27. package/dist/src/node_modules/@anthropic-ai/sdk/internal/parse.js +53 -0
  28. package/dist/src/node_modules/@anthropic-ai/sdk/internal/request-options.js +11 -0
  29. package/dist/src/node_modules/@anthropic-ai/sdk/internal/shims.js +86 -0
  30. package/dist/src/node_modules/@anthropic-ai/sdk/internal/to-file.js +94 -0
  31. package/dist/src/node_modules/@anthropic-ai/sdk/internal/tslib.js +14 -0
  32. package/dist/src/node_modules/@anthropic-ai/sdk/internal/uploads.js +113 -0
  33. package/dist/src/node_modules/@anthropic-ai/sdk/internal/utils/bytes.js +27 -0
  34. package/dist/src/node_modules/@anthropic-ai/sdk/internal/utils/env.js +19 -0
  35. package/dist/src/node_modules/@anthropic-ai/sdk/internal/utils/log.js +82 -0
  36. package/dist/src/node_modules/@anthropic-ai/sdk/internal/utils/path.js +76 -0
  37. package/dist/src/node_modules/@anthropic-ai/sdk/internal/utils/sleep.js +4 -0
  38. package/dist/src/node_modules/@anthropic-ai/sdk/internal/utils/uuid.js +16 -0
  39. package/dist/src/node_modules/@anthropic-ai/sdk/internal/utils/values.js +48 -0
  40. package/dist/src/node_modules/@anthropic-ai/sdk/lib/BetaMessageStream.js +588 -0
  41. package/dist/src/node_modules/@anthropic-ai/sdk/lib/MessageStream.js +582 -0
  42. package/dist/src/node_modules/@anthropic-ai/sdk/resources/beta/beta.js +19 -0
  43. package/dist/src/node_modules/@anthropic-ai/sdk/resources/beta/files.js +120 -0
  44. package/dist/src/node_modules/@anthropic-ai/sdk/resources/beta/messages/batches.js +202 -0
  45. package/dist/src/node_modules/@anthropic-ai/sdk/resources/beta/messages/messages.js +85 -0
  46. package/dist/src/node_modules/@anthropic-ai/sdk/resources/beta/models.js +58 -0
  47. package/dist/src/node_modules/@anthropic-ai/sdk/resources/completions.js +21 -0
  48. package/dist/src/node_modules/@anthropic-ai/sdk/resources/messages/batches.js +151 -0
  49. package/dist/src/node_modules/@anthropic-ai/sdk/resources/messages/messages.js +71 -0
  50. package/dist/src/node_modules/@anthropic-ai/sdk/resources/models.js +43 -0
  51. package/dist/src/node_modules/@anthropic-ai/sdk/version.js +3 -0
  52. package/dist/src/pre-made/repository/chat/ram/RamChatRepository.js +1 -1
  53. package/dist/src/repository/pg/PgCrudRepository.js +2 -2
  54. package/dist/src/repository/pg/PgRepositoryBase.js +2 -2
  55. package/dist/src/rest-controller/metadata/@middleware.js +16 -0
  56. package/dist/src/rest-controller/metadata/RestControllerMetadataStore.js +13 -0
  57. package/dist/src/rest-controller/runRestControllers.js +30 -22
  58. package/dist/src/server/prepareChatContainer.js +2 -2
  59. package/dist/src/validation/metadata/@isModel.js +18 -0
  60. package/dist/src/validation/validators/validateModel.js +2 -6
  61. package/package.json +2 -1
@@ -0,0 +1,115 @@
1
+ import { __decorate, __metadata } from 'tslib';
2
+ import { injectable } from '../../injection/index.js';
3
+ import { Anthropic } from '../../node_modules/@anthropic-ai/sdk/client.js';
4
+ import '../../node_modules/@anthropic-ai/sdk/core/api-promise.js';
5
+ import '../../node_modules/@anthropic-ai/sdk/core/pagination.js';
6
+ import 'uuid';
7
+ import '../../chatbot/metadata/ChatBotMetadataStore.js';
8
+ import '../../chatbot/ChatBot.js';
9
+ import { ChatBotAdapter } from '../../chatbot/ChatBotAdapter.js';
10
+ import 'reflect-metadata';
11
+ import '../../mindset/metadata/MindsetMetadataStore.js';
12
+ import { MindsetOperator } from '../../mindset/MindsetOperator.js';
13
+ import { Logger } from '../../logger/Logger.js';
14
+
15
+ let ClaudeChatBotAdapter = class ClaudeChatBotAdapter extends ChatBotAdapter {
16
+ anthropic;
17
+ model;
18
+ logger = new Logger('wabot:claude-chat-bot-adapter');
19
+ constructor(mindset) {
20
+ super(mindset);
21
+ const apiKey = process.env.CLAUDE_API_KEY;
22
+ if (!apiKey) {
23
+ throw new Error(`CLAUDE_API_KEY env variable is required`);
24
+ }
25
+ const model = process.env.CLAUDE_CHAT_MODEL;
26
+ if (!model) {
27
+ throw new Error(`CLAUDE_CHAT_MODEL env variable is required`);
28
+ }
29
+ this.anthropic = new Anthropic({ apiKey });
30
+ this.model = model;
31
+ }
32
+ async generateNextChatItem(chatItems) {
33
+ const systemPrompt = await this.systemPrompt();
34
+ const tools = (await this.mindset.allFunctionsDescriptors()).map((fn) => {
35
+ return {
36
+ name: fn.name,
37
+ description: fn.description,
38
+ input_schema: {
39
+ ...fn.parameters,
40
+ type: 'object'
41
+ }
42
+ };
43
+ });
44
+ const messages = this.mapChatItems(chatItems);
45
+ const request = {
46
+ model: this.model,
47
+ max_tokens: 4096,
48
+ system: systemPrompt,
49
+ messages,
50
+ tools: tools.length > 0 ? tools : undefined,
51
+ };
52
+ this.logger.debug(`Call Claude API with Request: ${JSON.stringify(request)}`);
53
+ const response = await this.anthropic.messages.create(request);
54
+ let newChatItem;
55
+ const content = response.content[0];
56
+ if (content.type === 'text') {
57
+ newChatItem = await this.buildBotMessageItem(content.text);
58
+ }
59
+ else if (content.type === 'tool_use') {
60
+ newChatItem = await this.buildFunctionCallItem(content.id, content.name, JSON.stringify(content.input));
61
+ }
62
+ else {
63
+ throw new Error('Not supported Claude Response');
64
+ }
65
+ return newChatItem;
66
+ }
67
+ mapChatItems(chatItems) {
68
+ const messages = [];
69
+ for (const item of chatItems) {
70
+ const itemData = item.getData();
71
+ if (itemData.type === 'CONNECTION_MESSAGE') {
72
+ if (!itemData.content.text) {
73
+ throw new Error('User message content is empty');
74
+ }
75
+ messages.push({ role: 'user', content: itemData.content.text });
76
+ }
77
+ else if (itemData.type === 'BOT_MESSAGE') {
78
+ if (!itemData.content.text) {
79
+ throw new Error('Assistant message content is empty');
80
+ }
81
+ messages.push({ role: 'assistant', content: itemData.content.text });
82
+ }
83
+ else if (itemData.type === 'FUNCTION_CALL') {
84
+ messages.push({
85
+ role: 'assistant',
86
+ content: [
87
+ {
88
+ type: 'tool_use',
89
+ id: itemData.content.id,
90
+ name: itemData.content.name,
91
+ input: JSON.parse(itemData.content.arguments || '{}')
92
+ }
93
+ ]
94
+ });
95
+ messages.push({
96
+ role: 'user',
97
+ content: [
98
+ {
99
+ type: 'tool_result',
100
+ tool_use_id: itemData.content.id,
101
+ content: itemData.content.result || 'No result'
102
+ }
103
+ ]
104
+ });
105
+ }
106
+ }
107
+ return messages;
108
+ }
109
+ };
110
+ ClaudeChatBotAdapter = __decorate([
111
+ injectable(),
112
+ __metadata("design:paramtypes", [MindsetOperator])
113
+ ], ClaudeChatBotAdapter);
114
+
115
+ export { ClaudeChatBotAdapter };
@@ -1,8 +1,8 @@
1
1
  import '../../core/chat/repository/IChatRepository.js';
2
- import { Persistent } from '../../core/Persistent.js';
2
+ import { Entity } from '../../core/Entity.js';
3
3
  import '../../core/user/IUserRepository.js';
4
4
 
5
- class WhatsApp extends Persistent {
5
+ class WhatsApp extends Entity {
6
6
  getVerifyToken() {
7
7
  return this.data.verifyToken;
8
8
  }
@@ -13,6 +13,15 @@ class WhatsAppSender {
13
13
  this.chatResolver = chatResolver;
14
14
  this.whatsAppRepository = whatsAppRepository;
15
15
  }
16
+ handleSendRequest(request) {
17
+ throw new Error('Not implemented');
18
+ }
19
+ handleSendTemplateRequest(request) {
20
+ throw new Error('Not implemented');
21
+ }
22
+ handleGetWhatsAppTemplate(request) {
23
+ throw new Error('Not implemented');
24
+ }
16
25
  async sendWhatsApp(request, options) {
17
26
  try {
18
27
  await this.handleSendRequest(request);
@@ -73,7 +82,7 @@ class WhatsAppSender {
73
82
  channelName: 'WhatsAppChannel',
74
83
  };
75
84
  const chat = await this.chatResolver.resolve(chatConnection);
76
- const chatMemory = await this.chatRepository.findMemory(chat.getId());
85
+ const chatMemory = await this.chatRepository.findMemory(chat.id);
77
86
  const chatItem = new ChatItem({
78
87
  type: 'BOT_MESSAGE',
79
88
  content: message,
@@ -1,14 +1,13 @@
1
1
  import { __decorate, __metadata } from 'tslib';
2
2
  import { Logger } from '../../logger/Logger.js';
3
3
  import { WhatsAppSender } from './WhatsAppSender.js';
4
- import { ChatRepository } from '../../core/chat/repository/IChatRepository.js';
5
- import '../../core/user/IUserRepository.js';
6
4
  import { ChatResolver } from '../../controller/channel/ChatResolver.js';
7
5
  import '../../controller/channel/UserResolver.js';
8
- import { singleton, container } from '../../injection/index.js';
6
+ import { singleton } from '../../injection/index.js';
9
7
  import '../../controller/metadata/ControllerMetadataStore.js';
8
+ import { ChatRepository } from '../../core/chat/repository/IChatRepository.js';
9
+ import '../../core/user/IUserRepository.js';
10
10
  import { WhatsAppRepository } from './WhatsAppRepository.js';
11
- import { WabotDevConnection } from '../wabot/WabotDevConnection.js';
12
11
 
13
12
  let WhatsAppSenderByCloudApi = class WhatsAppSenderByCloudApi extends WhatsAppSender {
14
13
  constructor(chatRepository, chatResolver, whatsAppRepository) {
@@ -115,10 +114,5 @@ WhatsAppSenderByCloudApi = __decorate([
115
114
  ChatResolver,
116
115
  WhatsAppRepository])
117
116
  ], WhatsAppSenderByCloudApi);
118
- if (!WabotDevConnection.isTokenAvailable()) {
119
- container.register(WhatsAppSender, {
120
- useClass: WhatsAppSenderByCloudApi,
121
- });
122
- }
123
117
 
124
118
  export { WhatsAppSenderByCloudApi };
@@ -1,14 +1,14 @@
1
1
  import { __decorate, __metadata } from 'tslib';
2
2
  import { WhatsAppSender } from './WhatsAppSender.js';
3
- import { WabotDevConnection } from '../wabot/WabotDevConnection.js';
4
- import { devEmitEvent } from '../wabot/WabotDevSocketContracts.js';
5
- import { Logger } from '../../logger/Logger.js';
6
- import { ChatRepository } from '../../core/chat/repository/IChatRepository.js';
7
- import '../../core/user/IUserRepository.js';
8
- import { singleton, container } from '../../injection/index.js';
9
3
  import { ChatResolver } from '../../controller/channel/ChatResolver.js';
10
4
  import '../../controller/channel/UserResolver.js';
5
+ import { singleton } from '../../injection/index.js';
11
6
  import '../../controller/metadata/ControllerMetadataStore.js';
7
+ import { ChatRepository } from '../../core/chat/repository/IChatRepository.js';
8
+ import '../../core/user/IUserRepository.js';
9
+ import { Logger } from '../../logger/Logger.js';
10
+ import { WabotDevConnection } from '../wabot/WabotDevConnection.js';
11
+ import { devEmitEvent } from '../wabot/WabotDevSocketContracts.js';
12
12
  import { WhatsAppRepository } from './WhatsAppRepository.js';
13
13
 
14
14
  let WhatsAppSenderByDevConnection = class WhatsAppSenderByDevConnection extends WhatsAppSender {
@@ -52,10 +52,5 @@ WhatsAppSenderByDevConnection = __decorate([
52
52
  ChatResolver,
53
53
  WhatsAppRepository])
54
54
  ], WhatsAppSenderByDevConnection);
55
- if (WabotDevConnection.isTokenAvailable()) {
56
- container.register(WhatsAppSender, {
57
- useClass: WhatsAppSenderByDevConnection,
58
- });
59
- }
60
55
 
61
56
  export { WhatsAppSenderByDevConnection };
@@ -1,22 +1,32 @@
1
- class Persistent {
2
- data;
3
- constructor(data) {
4
- this.data = data;
5
- }
6
- getId() {
1
+ import { Storable } from './Storable.js';
2
+
3
+ class Entity extends Storable {
4
+ get id() {
7
5
  if (!this.data.id) {
8
6
  throw new Error('id is required');
9
7
  }
10
8
  return this.data.id;
11
9
  }
12
- getCreatedAt() {
10
+ /**
11
+ * @deprecated use id
12
+ */
13
+ getId() {
14
+ return this.id;
15
+ }
16
+ get createdAt() {
13
17
  if (!this.data.createdAt) {
14
18
  throw new Error('createdAt is required');
15
19
  }
16
20
  return new Date(this.data.createdAt);
17
21
  }
22
+ /**
23
+ * @deprecated use createdAt
24
+ */
25
+ getCreatedAt() {
26
+ return this.createdAt;
27
+ }
18
28
  update(newData) {
19
- this.data = newData;
29
+ this.data = { ...this.data, newData, updatedAt: new Date().getTime() };
20
30
  }
21
31
  wasCreated() {
22
32
  return !!this.data.createdAt || !!this.data.id;
@@ -33,5 +43,10 @@ class Persistent {
33
43
  this.data.discardedAt = new Date().getTime();
34
44
  }
35
45
  }
46
+ /**
47
+ * @deprecated Should use Entity
48
+ */
49
+ class Persistent extends Entity {
50
+ }
36
51
 
37
- export { Persistent };
52
+ export { Entity, Persistent };
@@ -0,0 +1,8 @@
1
+ class Storable {
2
+ data;
3
+ constructor(data) {
4
+ this.data = data;
5
+ }
6
+ }
7
+
8
+ export { Storable };
@@ -1,6 +1,6 @@
1
- import { Persistent } from '../Persistent.js';
1
+ import { Entity } from '../Entity.js';
2
2
 
3
- class Chat extends Persistent {
3
+ class Chat extends Entity {
4
4
  constructor(data) {
5
5
  super(data);
6
6
  }
@@ -1,6 +1,6 @@
1
- import { Persistent } from '../Persistent.js';
1
+ import { Entity } from '../Entity.js';
2
2
 
3
- class ChatItem extends Persistent {
3
+ class ChatItem extends Entity {
4
4
  getType() {
5
5
  return this.data.type;
6
6
  }
@@ -1,6 +1,6 @@
1
- import { Persistent } from '../Persistent.js';
1
+ import { Entity } from '../Entity.js';
2
2
 
3
- class User extends Persistent {
3
+ class User extends Entity {
4
4
  constructor(data) {
5
5
  super(data);
6
6
  }
@@ -2,6 +2,7 @@ class CustomError extends Error {
2
2
  humanMessage;
3
3
  code;
4
4
  httpCode;
5
+ info;
5
6
  constructor(data) {
6
7
  super(data.message, { cause: data.cause });
7
8
  this.humanMessage = data.humanMessage;