@2en/clawly-plugins 1.24.7-beta.0 → 1.24.7-beta.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/cron-hook.ts +8 -21
- package/gateway/cron-delivery.ts +99 -0
- package/gateway/index.ts +2 -0
- package/gateway/offline-push.test.ts +4 -1
- package/gateway/offline-push.ts +4 -1
- package/gateway/presence.test.ts +2 -2
- package/gateway/presence.ts +4 -2
- package/index.ts +1 -1
- package/package.json +1 -1
package/cron-hook.ts
CHANGED
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
* before_tool_call hook for cron (action=add) — ensures delivery fields are
|
|
3
3
|
* always set correctly, even when the LLM omits them.
|
|
4
4
|
*
|
|
5
|
-
* Forces: delivery.mode = "none" (
|
|
6
|
-
*
|
|
5
|
+
* Forces: delivery.mode = "none" (delivery is handled by the cron-delivery
|
|
6
|
+
* agent_end hook which injects results into the main session via chat.inject)
|
|
7
7
|
* Patches: payload.kind "systemEvent" → "agentTurn"
|
|
8
8
|
*
|
|
9
9
|
* The cron tool name is "cron" (not "cron.create"). The LLM passes
|
|
@@ -19,27 +19,14 @@ function isRecord(v: unknown): v is UnknownRecord {
|
|
|
19
19
|
return typeof v === 'object' && v !== null && !Array.isArray(v)
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
const DELIVERY_SUFFIX = [
|
|
23
|
-
'',
|
|
24
|
-
'---',
|
|
25
|
-
'DELIVERY INSTRUCTIONS (mandatory):',
|
|
26
|
-
'When done, you MUST deliver your result to the user:',
|
|
27
|
-
'1. Call the clawly_send_message tool with role="assistant" and a brief, natural summary of your result.',
|
|
28
|
-
].join('\n')
|
|
29
|
-
|
|
30
22
|
function patchJob(job: UnknownRecord): UnknownRecord {
|
|
31
23
|
const patched: UnknownRecord = {...job}
|
|
32
24
|
|
|
33
|
-
// Force delivery.mode = "none" —
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
patched.payload = {
|
|
39
|
-
...job.payload,
|
|
40
|
-
message: job.payload.message + DELIVERY_SUFFIX,
|
|
41
|
-
}
|
|
42
|
-
}
|
|
25
|
+
// Force delivery.mode = "none" — delivery is handled by the cron-delivery
|
|
26
|
+
// agent_end hook (injects result into main session via chat.inject)
|
|
27
|
+
// Preserve other delivery fields (e.g. channel) if the agent set them
|
|
28
|
+
const existing = isRecord(job.delivery) ? job.delivery : {}
|
|
29
|
+
patched.delivery = {...existing, mode: 'none'}
|
|
43
30
|
|
|
44
31
|
// Patch payload.kind: systemEvent → agentTurn
|
|
45
32
|
if (isRecord(patched.payload) && (patched.payload as UnknownRecord).kind === 'systemEvent') {
|
|
@@ -73,5 +60,5 @@ export function registerCronHook(api: PluginApi) {
|
|
|
73
60
|
return {params}
|
|
74
61
|
})
|
|
75
62
|
|
|
76
|
-
api.logger.info('hook: registered before_tool_call for cron add
|
|
63
|
+
api.logger.info('hook: registered before_tool_call for cron add (none + agentTurn enforcement)')
|
|
77
64
|
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cron delivery via agent_end hook — injects cron session results into the
|
|
3
|
+
* main chat session programmatically via `chat.inject`.
|
|
4
|
+
*
|
|
5
|
+
* When a cron job completes in an isolated session, this hook:
|
|
6
|
+
* 1. Detects cron sessions via `ctx.sessionKey.startsWith('agent:clawly:cron:')`
|
|
7
|
+
* 2. Extracts the last assistant message (raw, preserving formatting)
|
|
8
|
+
* 3. Filters noise (empty, NO_REPLY, heartbeat-only, system message leak)
|
|
9
|
+
* 4. Resolves the main session key via `sessions.resolve`
|
|
10
|
+
* 5. Injects the result into the main session via `chat.inject`
|
|
11
|
+
*
|
|
12
|
+
* This runs independently of offline-push — both fire on agent_end but serve
|
|
13
|
+
* different purposes. `chat.inject` does NOT trigger a new agent_end (it's a
|
|
14
|
+
* transcript injection, not an agent turn), so no infinite loop risk.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import type {PluginApi} from '../types'
|
|
18
|
+
import {injectAssistantMessage, resolveSessionKey} from './inject'
|
|
19
|
+
import {shouldSkipPushForMessage} from './offline-push'
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Extract the last assistant message's full text from a messages array.
|
|
23
|
+
* Preserves original formatting (newlines, whitespace) — unlike the
|
|
24
|
+
* collapsed version in offline-push used for notification previews.
|
|
25
|
+
*/
|
|
26
|
+
function getRawLastAssistantText(messages: unknown): string | null {
|
|
27
|
+
if (!Array.isArray(messages)) return null
|
|
28
|
+
|
|
29
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
30
|
+
const msg = messages[i]
|
|
31
|
+
if (typeof msg !== 'object' || msg === null) continue
|
|
32
|
+
if ((msg as any).role !== 'assistant') continue
|
|
33
|
+
|
|
34
|
+
const content = (msg as any).content
|
|
35
|
+
if (typeof content === 'string') return content.trim()
|
|
36
|
+
if (Array.isArray(content)) {
|
|
37
|
+
const text = content
|
|
38
|
+
.filter((p: any) => typeof p === 'object' && p !== null && p.type === 'text')
|
|
39
|
+
.map((p: any) => p.text)
|
|
40
|
+
.join('')
|
|
41
|
+
return text.trim()
|
|
42
|
+
}
|
|
43
|
+
return null
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return null
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function registerCronDelivery(api: PluginApi) {
|
|
50
|
+
api.on('agent_end', async (event: Record<string, unknown>, ctx?: Record<string, unknown>) => {
|
|
51
|
+
const sessionKey = typeof ctx?.sessionKey === 'string' ? ctx.sessionKey : undefined
|
|
52
|
+
const agentId = typeof ctx?.agentId === 'string' ? ctx.agentId : undefined
|
|
53
|
+
|
|
54
|
+
// Only fire for cron sessions
|
|
55
|
+
if (!sessionKey?.startsWith('agent:clawly:cron:')) return
|
|
56
|
+
|
|
57
|
+
try {
|
|
58
|
+
// Extract raw assistant text (preserving formatting)
|
|
59
|
+
const text = getRawLastAssistantText(event.messages)
|
|
60
|
+
if (text == null) {
|
|
61
|
+
api.logger.info('cron-delivery: skipped (no assistant message)')
|
|
62
|
+
return
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Filter noise — reuse the same logic as offline-push
|
|
66
|
+
const reason = shouldSkipPushForMessage(text)
|
|
67
|
+
if (reason) {
|
|
68
|
+
api.logger.info(`cron-delivery: skipped (filtered: ${reason})`)
|
|
69
|
+
return
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Resolve main session key for this agent
|
|
73
|
+
if (!agentId) {
|
|
74
|
+
api.logger.error('cron-delivery: skipped (no agentId on ctx)')
|
|
75
|
+
return
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const mainSessionKey = await resolveSessionKey(agentId, api)
|
|
79
|
+
|
|
80
|
+
// Inject the cron result into the main session
|
|
81
|
+
const result = await injectAssistantMessage(
|
|
82
|
+
{
|
|
83
|
+
sessionKey: mainSessionKey,
|
|
84
|
+
message: text,
|
|
85
|
+
label: 'cron',
|
|
86
|
+
},
|
|
87
|
+
api,
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
api.logger.info(
|
|
91
|
+
`cron-delivery: injected into ${mainSessionKey} (messageId=${result.messageId})`,
|
|
92
|
+
)
|
|
93
|
+
} catch (err) {
|
|
94
|
+
api.logger.error(`cron-delivery: ${err instanceof Error ? err.message : String(err)}`)
|
|
95
|
+
}
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
api.logger.info('cron-delivery: registered agent_end hook')
|
|
99
|
+
}
|
package/gateway/index.ts
CHANGED
|
@@ -2,6 +2,7 @@ import type {PluginApi} from '../types'
|
|
|
2
2
|
import {registerAgentSend} from './agent'
|
|
3
3
|
import {registerClawhub2gateway} from './clawhub2gateway'
|
|
4
4
|
import {registerConfigRepair} from './config-repair'
|
|
5
|
+
import {registerCronDelivery} from './cron-delivery'
|
|
5
6
|
import {registerPairing} from './pairing'
|
|
6
7
|
import {registerMemoryBrowser} from './memory'
|
|
7
8
|
import {registerNotification} from './notification'
|
|
@@ -18,6 +19,7 @@ export function registerGateway(api: PluginApi) {
|
|
|
18
19
|
registerClawhub2gateway(api)
|
|
19
20
|
registerPlugins(api)
|
|
20
21
|
registerOfflinePush(api)
|
|
22
|
+
registerCronDelivery(api)
|
|
21
23
|
registerConfigRepair(api)
|
|
22
24
|
registerPairing(api)
|
|
23
25
|
registerVersion(api)
|
|
@@ -92,7 +92,10 @@ describe('offline-push', () => {
|
|
|
92
92
|
await handlers.get('agent_end')!({}, {sessionKey: 'agent:clawly:main'})
|
|
93
93
|
|
|
94
94
|
expect(logs.filter((l) => l.msg.includes('notified'))).toHaveLength(0)
|
|
95
|
-
expect(logs
|
|
95
|
+
expect(logs).toContainEqual({
|
|
96
|
+
level: 'info',
|
|
97
|
+
msg: expect.stringContaining('skipped (client online)'),
|
|
98
|
+
})
|
|
96
99
|
})
|
|
97
100
|
|
|
98
101
|
test('sends push for consecutive calls on same session', async () => {
|
package/gateway/offline-push.ts
CHANGED
|
@@ -104,7 +104,10 @@ export function registerOfflinePush(api: PluginApi) {
|
|
|
104
104
|
try {
|
|
105
105
|
// Skip if client is still connected — they got the response in real-time.
|
|
106
106
|
const online = await isClientOnline()
|
|
107
|
-
if (online)
|
|
107
|
+
if (online) {
|
|
108
|
+
api.logger.info('offline-push: skipped (client online)')
|
|
109
|
+
return
|
|
110
|
+
}
|
|
108
111
|
|
|
109
112
|
// Extract full assistant text for filtering and preview.
|
|
110
113
|
const fullText = getLastAssistantText(event.messages)
|
package/gateway/presence.test.ts
CHANGED
|
@@ -6,8 +6,8 @@ describe('isOnlineEntry', () => {
|
|
|
6
6
|
expect(isOnlineEntry({host: 'openclaw-ios', reason: 'foreground'})).toBe(true)
|
|
7
7
|
})
|
|
8
8
|
|
|
9
|
-
test('returns
|
|
10
|
-
expect(isOnlineEntry({host: 'openclaw-ios', reason: 'connect'})).toBe(
|
|
9
|
+
test('returns false for reason "connect" (gateway WS state, not reliable for app foreground)', () => {
|
|
10
|
+
expect(isOnlineEntry({host: 'openclaw-ios', reason: 'connect'})).toBe(false)
|
|
11
11
|
})
|
|
12
12
|
|
|
13
13
|
test('returns false for reason "background"', () => {
|
package/gateway/presence.ts
CHANGED
|
@@ -19,10 +19,12 @@ interface PresenceEntry {
|
|
|
19
19
|
reason?: string
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
/** Returns true if the presence entry indicates the client is actively
|
|
22
|
+
/** Returns true if the presence entry indicates the client is actively in the foreground.
|
|
23
|
+
* Only trusts the explicit 'foreground' signal from the mobile app — 'connect'
|
|
24
|
+
* (gateway WS state) can linger after the app backgrounds, causing false positives. */
|
|
23
25
|
export function isOnlineEntry(entry: PresenceEntry | undefined): boolean {
|
|
24
26
|
if (!entry) return false
|
|
25
|
-
return entry.reason === 'foreground'
|
|
27
|
+
return entry.reason === 'foreground'
|
|
26
28
|
}
|
|
27
29
|
|
|
28
30
|
/**
|
package/index.ts
CHANGED
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
* - before_message_write — restores original /skill command in user messages (undoes gateway rewrite)
|
|
26
26
|
* - tool_result_persist — copies TTS audio to persistent outbound directory
|
|
27
27
|
* - before_tool_call — enforces delivery fields on cron.create
|
|
28
|
-
* - agent_end — sends push notification when client is offline
|
|
28
|
+
* - agent_end — sends push notification when client is offline; injects cron results into main session
|
|
29
29
|
* - gateway_start — auto-approves device pairing for Clawly mobile clients (clientId: openclaw-ios)
|
|
30
30
|
* - gateway_start — registers auto-update cron job (0 3 * * *) for clawly-plugins
|
|
31
31
|
*/
|