@2en/clawly-plugins 1.21.1 → 1.21.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@2en/clawly-plugins",
3
- "version": "1.21.1",
3
+ "version": "1.21.2",
4
4
  "module": "index.ts",
5
5
  "type": "module",
6
6
  "repository": {
@@ -24,6 +24,7 @@
24
24
  "gateway-fetch.ts",
25
25
  "outbound.ts",
26
26
  "model-gateway-setup.ts",
27
+ "skill-command-restore.ts",
27
28
  "openclaw.plugin.json"
28
29
  ],
29
30
  "publishConfig": {
@@ -0,0 +1,94 @@
1
+ /**
2
+ * Hook: before_message_write — restore original /skill command in user messages.
3
+ *
4
+ * The OpenClaw gateway rewrites `/skill <name> <args>` into a formatted prompt:
5
+ * Use the "<name>" skill for this request.
6
+ *
7
+ * User input:
8
+ * <args>
9
+ *
10
+ * This rewrite is useful for the LLM but pollutes chat history — the mobile app
11
+ * sees the rewritten text instead of what the user actually typed.
12
+ *
13
+ * This hook intercepts user messages before they're written to the session JSONL
14
+ * and restores the original `/skill <name> <args>` format.
15
+ */
16
+
17
+ import type {PluginApi} from './index'
18
+
19
+ const SKILL_REWRITE_RE = /^Use the "(.+?)" skill for this request\.(?:\n\nUser input:\n([\s\S]+))?$/
20
+
21
+ type AgentMessage = {
22
+ role: string
23
+ content: unknown
24
+ }
25
+
26
+ type BeforeMessageWriteEvent = {
27
+ message: AgentMessage
28
+ }
29
+
30
+ type BeforeMessageWriteResult = {
31
+ message?: AgentMessage
32
+ }
33
+
34
+ function extractText(content: unknown): string {
35
+ if (typeof content === 'string') return content
36
+ if (Array.isArray(content)) {
37
+ return content
38
+ .map((part: unknown) => {
39
+ if (!part || typeof part !== 'object') return ''
40
+ const p = part as {type?: string; text?: string}
41
+ return p.type === 'text' && typeof p.text === 'string' ? p.text : ''
42
+ })
43
+ .filter(Boolean)
44
+ .join('')
45
+ }
46
+ return ''
47
+ }
48
+
49
+ function replaceText(content: unknown, newText: string): unknown {
50
+ if (typeof content === 'string') return newText
51
+ if (Array.isArray(content)) {
52
+ // Collapse all text parts into one to avoid duplicating the restored text
53
+ // when the original content was split across multiple text parts.
54
+ let replaced = false
55
+ return content.flatMap((part: unknown) => {
56
+ if (!part || typeof part !== 'object') return [part]
57
+ const p = part as {type?: string; text?: string}
58
+ if (p.type === 'text' && typeof p.text === 'string') {
59
+ if (!replaced) {
60
+ replaced = true
61
+ return [{...p, text: newText}]
62
+ }
63
+ return []
64
+ }
65
+ return [part]
66
+ })
67
+ }
68
+ return content
69
+ }
70
+
71
+ export function registerSkillCommandRestore(api: PluginApi) {
72
+ api.on(
73
+ 'before_message_write',
74
+ (event: BeforeMessageWriteEvent): BeforeMessageWriteResult | void => {
75
+ const {message} = event
76
+ if (message.role !== 'user') return
77
+
78
+ const text = extractText(message.content)
79
+ const match = text.match(SKILL_REWRITE_RE)
80
+ if (!match) return
81
+
82
+ const skillName = match[1]
83
+ const args = match[2]?.trim()
84
+ const restored = args ? `/skill ${skillName} ${args}` : `/skill ${skillName}`
85
+
86
+ return {
87
+ message: {
88
+ ...message,
89
+ content: replaceText(message.content, restored),
90
+ },
91
+ }
92
+ },
93
+ )
94
+ }