@2en/clawly-plugins 1.1.0 → 1.1.2
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/agent-send.ts +9 -4
- package/notification.ts +13 -24
- package/package.json +4 -1
package/agent-send.ts
CHANGED
|
@@ -18,6 +18,7 @@ $.verbose = false
|
|
|
18
18
|
|
|
19
19
|
interface AgentSendParams {
|
|
20
20
|
message: string
|
|
21
|
+
agent?: string
|
|
21
22
|
notificationMessage?: string
|
|
22
23
|
}
|
|
23
24
|
|
|
@@ -29,10 +30,11 @@ interface AgentSendResult {
|
|
|
29
30
|
|
|
30
31
|
async function runAgentMessage(
|
|
31
32
|
message: string,
|
|
33
|
+
agent: string,
|
|
32
34
|
api: PluginApi,
|
|
33
35
|
): Promise<{ok: boolean; error?: string}> {
|
|
34
36
|
try {
|
|
35
|
-
await $`openclaw agent --agent
|
|
37
|
+
await $`openclaw agent --agent ${agent} --message ${message}`
|
|
36
38
|
return {ok: true}
|
|
37
39
|
} catch (err) {
|
|
38
40
|
const msg = err instanceof Error ? err.message : String(err)
|
|
@@ -46,7 +48,8 @@ async function handleAgentSend(
|
|
|
46
48
|
params: AgentSendParams,
|
|
47
49
|
api: PluginApi,
|
|
48
50
|
): Promise<AgentSendResult> {
|
|
49
|
-
const
|
|
51
|
+
const agent = params.agent || 'clawly'
|
|
52
|
+
const {ok} = await runAgentMessage(rawMessage, agent, api)
|
|
50
53
|
const online = await isClientOnline()
|
|
51
54
|
|
|
52
55
|
let pushSent = false
|
|
@@ -66,10 +69,11 @@ export function registerAgentSend(api: PluginApi) {
|
|
|
66
69
|
return
|
|
67
70
|
}
|
|
68
71
|
|
|
72
|
+
const agent = typeof params.agent === 'string' ? params.agent.trim() : undefined
|
|
69
73
|
const notificationMessage =
|
|
70
74
|
typeof params.notificationMessage === 'string' ? params.notificationMessage : undefined
|
|
71
75
|
|
|
72
|
-
const result = await handleAgentSend(message, {message, notificationMessage}, api)
|
|
76
|
+
const result = await handleAgentSend(message, {message, agent, notificationMessage}, api)
|
|
73
77
|
respond(result.ok, result)
|
|
74
78
|
})
|
|
75
79
|
|
|
@@ -81,11 +85,12 @@ export function registerAgentSend(api: PluginApi) {
|
|
|
81
85
|
return
|
|
82
86
|
}
|
|
83
87
|
|
|
88
|
+
const agent = typeof params.agent === 'string' ? params.agent.trim() : undefined
|
|
84
89
|
const notificationMessage =
|
|
85
90
|
typeof params.notificationMessage === 'string' ? params.notificationMessage : undefined
|
|
86
91
|
|
|
87
92
|
const echoMessage = `/clawly_echo ${message}`
|
|
88
|
-
const result = await handleAgentSend(echoMessage, {message, notificationMessage}, api)
|
|
93
|
+
const result = await handleAgentSend(echoMessage, {message, agent, notificationMessage}, api)
|
|
89
94
|
respond(result.ok, result)
|
|
90
95
|
})
|
|
91
96
|
|
package/notification.ts
CHANGED
|
@@ -19,24 +19,6 @@ const TOKEN_DIR = path.join(os.homedir(), '.openclaw', 'clawly')
|
|
|
19
19
|
const TOKEN_FILE = path.join(TOKEN_DIR, 'expo-push-token.json')
|
|
20
20
|
const EXPO_PUSH_URL = 'https://exp.host/--/api/v2/push/send'
|
|
21
21
|
|
|
22
|
-
let pushToken: string | null = null
|
|
23
|
-
|
|
24
|
-
function loadPersistedToken(api: PluginApi): void {
|
|
25
|
-
try {
|
|
26
|
-
if (fs.existsSync(TOKEN_FILE)) {
|
|
27
|
-
const data = JSON.parse(fs.readFileSync(TOKEN_FILE, 'utf-8'))
|
|
28
|
-
if (typeof data.token === 'string' && data.token) {
|
|
29
|
-
pushToken = data.token
|
|
30
|
-
api.logger.info(`notification: loaded persisted push token`)
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
} catch (err) {
|
|
34
|
-
api.logger.warn(
|
|
35
|
-
`notification: failed to load persisted token: ${err instanceof Error ? err.message : String(err)}`,
|
|
36
|
-
)
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
22
|
function persistToken(token: string, api: PluginApi): void {
|
|
41
23
|
try {
|
|
42
24
|
fs.mkdirSync(TOKEN_DIR, {recursive: true})
|
|
@@ -49,14 +31,23 @@ function persistToken(token: string, api: PluginApi): void {
|
|
|
49
31
|
}
|
|
50
32
|
|
|
51
33
|
export function getPushToken(): string | null {
|
|
52
|
-
|
|
34
|
+
try {
|
|
35
|
+
if (fs.existsSync(TOKEN_FILE)) {
|
|
36
|
+
const data = JSON.parse(fs.readFileSync(TOKEN_FILE, 'utf-8'))
|
|
37
|
+
if (typeof data.token === 'string' && data.token) {
|
|
38
|
+
return data.token
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
} catch {}
|
|
42
|
+
return null
|
|
53
43
|
}
|
|
54
44
|
|
|
55
45
|
export async function sendPushNotification(
|
|
56
46
|
opts: {body: string; title?: string; data?: Record<string, unknown>},
|
|
57
47
|
api: PluginApi,
|
|
58
48
|
): Promise<boolean> {
|
|
59
|
-
|
|
49
|
+
const token = getPushToken()
|
|
50
|
+
if (!token) {
|
|
60
51
|
api.logger.warn('notification: no push token registered, skipping notification')
|
|
61
52
|
return false
|
|
62
53
|
}
|
|
@@ -66,13 +57,14 @@ export async function sendPushNotification(
|
|
|
66
57
|
method: 'POST',
|
|
67
58
|
headers: {'Content-Type': 'application/json'},
|
|
68
59
|
body: JSON.stringify({
|
|
69
|
-
to:
|
|
60
|
+
to: token,
|
|
70
61
|
sound: 'default',
|
|
71
62
|
title: opts.title ?? 'Clawly',
|
|
72
63
|
body: opts.body,
|
|
73
64
|
data: opts.data,
|
|
74
65
|
}),
|
|
75
66
|
})
|
|
67
|
+
const json = await res.json()
|
|
76
68
|
|
|
77
69
|
if (!res.ok) {
|
|
78
70
|
api.logger.error(`notification: push failed — ${res.status} ${res.statusText}`)
|
|
@@ -90,8 +82,6 @@ export async function sendPushNotification(
|
|
|
90
82
|
}
|
|
91
83
|
|
|
92
84
|
export function registerNotification(api: PluginApi) {
|
|
93
|
-
loadPersistedToken(api)
|
|
94
|
-
|
|
95
85
|
api.registerGatewayMethod('clawly.notification.setToken', async ({params, respond}) => {
|
|
96
86
|
const token = typeof params.token === 'string' ? params.token.trim() : ''
|
|
97
87
|
|
|
@@ -100,7 +90,6 @@ export function registerNotification(api: PluginApi) {
|
|
|
100
90
|
return
|
|
101
91
|
}
|
|
102
92
|
|
|
103
|
-
pushToken = token
|
|
104
93
|
persistToken(token, api)
|
|
105
94
|
api.logger.info('notification: push token registered')
|
|
106
95
|
respond(true, {registered: true})
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@2en/clawly-plugins",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.2",
|
|
4
4
|
"module": "index.ts",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"repository": {
|
|
@@ -20,6 +20,9 @@
|
|
|
20
20
|
"agent-send.ts",
|
|
21
21
|
"openclaw.plugin.json"
|
|
22
22
|
],
|
|
23
|
+
"publishConfig": {
|
|
24
|
+
"access": "public"
|
|
25
|
+
},
|
|
23
26
|
"openclaw": {
|
|
24
27
|
"extensions": [
|
|
25
28
|
"./index.ts"
|