@stacksjs/chat 0.69.5 → 0.70.1
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/base.d.ts +111 -0
- package/dist/index.js +3236 -1
- package/dist/slack.d.ts +70 -1
- package/package.json +9 -6
package/dist/base.d.ts
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import type { ChatDriver, ChatDriverConfig, ChatMessage, ChatResult, RenderOptions } from '@stacksjs/types';
|
|
2
|
+
|
|
3
|
+
export declare abstract class BaseChatDriver implements ChatDriver {
|
|
4
|
+
public abstract name: string
|
|
5
|
+
protected config: Required<ChatDriverConfig>
|
|
6
|
+
|
|
7
|
+
constructor(config?: ChatDriverConfig) {
|
|
8
|
+
this.config = {
|
|
9
|
+
maxRetries: config?.maxRetries || 3,
|
|
10
|
+
retryTimeout: config?.retryTimeout || 1000,
|
|
11
|
+
...config,
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
public configure(config: ChatDriverConfig): void {
|
|
16
|
+
this.config = { ...this.config, ...config }
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
public abstract send(message: ChatMessage, options?: RenderOptions): Promise<ChatResult>
|
|
20
|
+
|
|
21
|
+
protected validateMessage(message: ChatMessage): boolean {
|
|
22
|
+
if (!message.to) {
|
|
23
|
+
throw new Error('Message recipient is required')
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (!message.content && !message.template) {
|
|
27
|
+
throw new Error('Either message content or template is required')
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return true
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
protected async handleError(error: unknown, message: ChatMessage): Promise<ChatResult> {
|
|
34
|
+
const err = error instanceof Error ? error : new Error(String(error))
|
|
35
|
+
|
|
36
|
+
log.error(`[${this.name}] Message sending failed`, {
|
|
37
|
+
error: err.message,
|
|
38
|
+
stack: err.stack,
|
|
39
|
+
to: message.to,
|
|
40
|
+
subject: message.subject,
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
let result: ChatResult = {
|
|
44
|
+
message: `Message sending failed: ${err.message}`,
|
|
45
|
+
success: false,
|
|
46
|
+
provider: this.name,
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (message.onError) {
|
|
50
|
+
const customResult = message.onError(err)
|
|
51
|
+
const handlerResult = customResult instanceof Promise
|
|
52
|
+
? await customResult
|
|
53
|
+
: customResult
|
|
54
|
+
|
|
55
|
+
result = {
|
|
56
|
+
...result,
|
|
57
|
+
...handlerResult,
|
|
58
|
+
success: false,
|
|
59
|
+
provider: this.name,
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return result
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
protected async handleSuccess(message: ChatMessage, messageId?: string): Promise<ChatResult> {
|
|
67
|
+
let result: ChatResult = {
|
|
68
|
+
message: 'Message sent successfully',
|
|
69
|
+
success: true,
|
|
70
|
+
provider: this.name,
|
|
71
|
+
messageId,
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
try {
|
|
75
|
+
if (message.handle) {
|
|
76
|
+
const customResult = message.handle()
|
|
77
|
+
const handlerResult = customResult instanceof Promise
|
|
78
|
+
? await customResult
|
|
79
|
+
: customResult
|
|
80
|
+
|
|
81
|
+
result = {
|
|
82
|
+
...result,
|
|
83
|
+
...handlerResult,
|
|
84
|
+
success: true,
|
|
85
|
+
provider: this.name,
|
|
86
|
+
messageId,
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (message.onSuccess) {
|
|
91
|
+
const successResult = message.onSuccess()
|
|
92
|
+
const handlerResult = successResult instanceof Promise
|
|
93
|
+
? await successResult
|
|
94
|
+
: successResult
|
|
95
|
+
|
|
96
|
+
result = {
|
|
97
|
+
...result,
|
|
98
|
+
...handlerResult,
|
|
99
|
+
success: true,
|
|
100
|
+
provider: this.name,
|
|
101
|
+
messageId,
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
catch (error) {
|
|
106
|
+
return this.handleError(error, message)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return result
|
|
110
|
+
}
|
|
111
|
+
}
|