@jackle.dev/zalox-plugin 1.0.5 → 1.0.7

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": "@jackle.dev/zalox-plugin",
3
- "version": "1.0.5",
3
+ "version": "1.0.7",
4
4
  "description": "OpenClaw channel plugin for Zalo via zca-js (in-process, single login)",
5
5
  "type": "module",
6
6
  "main": "./src/index.ts",
@@ -0,0 +1,93 @@
1
+ import { existsSync, readFileSync, writeFileSync, mkdirSync, readdirSync, unlinkSync } from 'fs';
2
+ import { join } from 'path';
3
+ import { randomUUID } from 'crypto';
4
+ import { getConfigDir, ensureConfigDir } from '../utils/config.js';
5
+
6
+ export interface Template {
7
+ id: string;
8
+ name: string;
9
+ content: string;
10
+ variables: string[];
11
+ createdAt: string;
12
+ updatedAt: string;
13
+ }
14
+
15
+ function getTemplatesDir(): string {
16
+ const root = getConfigDir();
17
+ const dir = join(root, 'templates');
18
+ if (!existsSync(dir)) {
19
+ mkdirSync(dir, { recursive: true });
20
+ }
21
+ return dir;
22
+ }
23
+
24
+ function getTemplatePath(id: string): string {
25
+ return join(getTemplatesDir(), `${id}.json`);
26
+ }
27
+
28
+ function extractVariables(content: string): string[] {
29
+ const matches = content.match(/\{\{(\w+)\}\}/g);
30
+ if (!matches) return [];
31
+ const vars = matches.map((m) => m.replace(/\{\{|\}\}/g, ''));
32
+ return [...new Set(vars)];
33
+ }
34
+
35
+ export function createTemplate(name: string, content: string): Template {
36
+ ensureConfigDir();
37
+ const dir = getTemplatesDir();
38
+ const existing = listTemplates();
39
+ if (existing.find((t) => t.name.toLowerCase() === name.toLowerCase())) {
40
+ throw new Error(`Template "${name}" already exists`);
41
+ }
42
+
43
+ const now = new Date().toISOString();
44
+ const template: Template = {
45
+ id: randomUUID().split('-')[0],
46
+ name,
47
+ content,
48
+ variables: extractVariables(content),
49
+ createdAt: now,
50
+ updatedAt: now,
51
+ };
52
+
53
+ writeFileSync(getTemplatePath(template.id), JSON.stringify(template, null, 2));
54
+ return template;
55
+ }
56
+
57
+ export function listTemplates(): Template[] {
58
+ const dir = getTemplatesDir();
59
+ if (!existsSync(dir)) return [];
60
+ const files = readdirSync(dir).filter((f) => f.endsWith('.json'));
61
+ const templates: Template[] = [];
62
+ for (const file of files) {
63
+ try {
64
+ const raw = readFileSync(join(dir, file), 'utf-8');
65
+ templates.push(JSON.parse(raw));
66
+ } catch {}
67
+ }
68
+ return templates.sort((a, b) => a.name.localeCompare(b.name));
69
+ }
70
+
71
+ export function getTemplate(idOrName: string): Template | null {
72
+ const templates = listTemplates();
73
+ return templates.find((t) => t.id === idOrName || t.name.toLowerCase() === idOrName.toLowerCase()) || null;
74
+ }
75
+
76
+ export function deleteTemplate(idOrName: string): boolean {
77
+ const template = getTemplate(idOrName);
78
+ if (!template) return false;
79
+ const path = getTemplatePath(template.id);
80
+ if (existsSync(path)) {
81
+ unlinkSync(path);
82
+ return true;
83
+ }
84
+ return false;
85
+ }
86
+
87
+ export function renderTemplate(template: Template, vars: Record<string, string>): string {
88
+ let result = template.content;
89
+ for (const [key, value] of Object.entries(vars)) {
90
+ result = result.replace(new RegExp(`\\{\\{${key}\\}\\}`, 'g'), value);
91
+ }
92
+ return result;
93
+ }
package/src/index.ts CHANGED
@@ -6,21 +6,26 @@
6
6
  */
7
7
 
8
8
  import type { OpenClawPluginApi } from 'openclaw/plugin-sdk';
9
- import { emptyPluginConfigSchema } from 'openclaw/plugin-sdk';
10
9
  import { zaloxPlugin, zaloxDock } from './channel.js';
11
10
  import { setPluginRuntime } from './listener.js';
11
+ import { zaloxGroupTool } from './tools.js';
12
+ import { zaloxTemplateTool } from './tools-template.js';
12
13
 
13
14
  const plugin = {
14
15
  id: 'zalox',
15
16
  name: 'Zalo (ZaloX)',
16
17
  description: 'Zalo personal messaging — in-process, single login, no session conflicts',
17
- configSchema: undefined, // Disabled to fix schema.toJSONSchema error
18
+ configSchema: undefined,
18
19
  register(api: OpenClawPluginApi) {
19
20
  // Store runtime for listener's message processing
20
21
  setPluginRuntime(api.runtime);
21
22
 
22
23
  // Register channel plugin
23
24
  api.registerChannel({ plugin: zaloxPlugin, dock: zaloxDock });
25
+
26
+ // Register admin tool
27
+ api.registerTool(zaloxGroupTool);
28
+ api.registerTool(zaloxTemplateTool);
24
29
  },
25
30
  };
26
31
 
@@ -0,0 +1,54 @@
1
+ import { z } from 'zod';
2
+ import { createTemplate, listTemplates, getTemplate, renderTemplate, deleteTemplate } from './core/templates.js';
3
+
4
+ export const zaloxTemplateTool = {
5
+ name: 'zalox_template',
6
+ description: 'Manage and use message templates.',
7
+ parameters: z.object({
8
+ action: z.enum(['list', 'get', 'create', 'delete', 'render']),
9
+ name: z.string().optional().describe('Template name or ID (required for get/create/delete/render)'),
10
+ content: z.string().optional().describe('Content for create (supports {{variable}})'),
11
+ vars: z.record(z.string()).optional().describe('Variables for render'),
12
+ }),
13
+ execute: async (args: any) => {
14
+ const { action, name, content, vars } = args;
15
+
16
+ try {
17
+ if (action === 'list') {
18
+ const templates = listTemplates();
19
+ if (templates.length === 0) return 'No templates found.';
20
+ return JSON.stringify(templates.map(t => ({ id: t.id, name: t.name, vars: t.variables })), null, 2);
21
+ }
22
+
23
+ if (action === 'get') {
24
+ if (!name) throw new Error('name required');
25
+ const t = getTemplate(name);
26
+ return t ? JSON.stringify(t, null, 2) : 'Template not found';
27
+ }
28
+
29
+ if (action === 'create') {
30
+ if (!name || !content) throw new Error('name and content required');
31
+ const t = createTemplate(name, content);
32
+ return `Created template "${t.name}" (vars: ${t.variables.join(', ')})`;
33
+ }
34
+
35
+ if (action === 'render') {
36
+ if (!name) throw new Error('name required');
37
+ const t = getTemplate(name);
38
+ if (!t) return 'Template not found';
39
+ const rendered = renderTemplate(t, vars || {});
40
+ return rendered;
41
+ }
42
+
43
+ if (action === 'delete') {
44
+ if (!name) throw new Error('name required');
45
+ const ok = deleteTemplate(name);
46
+ return ok ? `Deleted template "${name}"` : 'Template not found';
47
+ }
48
+ } catch (e: any) {
49
+ return `Error: ${e.message}`;
50
+ }
51
+
52
+ return 'Unknown action';
53
+ },
54
+ };
package/src/tools.ts ADDED
@@ -0,0 +1,51 @@
1
+ import { z } from 'zod';
2
+ import { getCachedApi } from './client.js';
3
+
4
+ export const zaloxGroupTool = {
5
+ name: 'zalox_group',
6
+ description: 'Manage Zalo groups: kick members, add members, or get group info.',
7
+ parameters: z.object({
8
+ action: z.enum(['kick', 'add', 'info']),
9
+ groupId: z.string(),
10
+ userId: z.string().optional(),
11
+ profile: z.string().optional().default('default'),
12
+ }),
13
+ execute: async (args: any) => {
14
+ const { action, groupId, userId, profile } = args;
15
+ const cached = getCachedApi(profile);
16
+ if (!cached) throw new Error(`Profile ${profile} not connected`);
17
+
18
+ const api = cached.api as any;
19
+
20
+ try {
21
+ if (action === 'kick') {
22
+ if (!userId) throw new Error('userId required for kick');
23
+ await api.removeUserFromGroup(userId, groupId);
24
+ return `Successfully kicked ${userId} from ${groupId}`;
25
+ }
26
+
27
+ if (action === 'add') {
28
+ if (!userId) throw new Error('userId required for add');
29
+ await api.addUserToGroup(userId, groupId);
30
+ return `Successfully added ${userId} to ${groupId}`;
31
+ }
32
+
33
+ if (action === 'info') {
34
+ const info = await api.getGroupInfo(groupId);
35
+ // Simplify info to avoid huge context
36
+ const simple = {
37
+ id: info.id,
38
+ name: info.name,
39
+ topic: info.topic,
40
+ totalMember: info.totalMember,
41
+ admins: info.admins,
42
+ };
43
+ return JSON.stringify(simple, null, 2);
44
+ }
45
+ } catch (e: any) {
46
+ return `Action ${action} failed: ${e.message || e}`;
47
+ }
48
+
49
+ return 'Unknown action';
50
+ },
51
+ };
@@ -0,0 +1,14 @@
1
+ import { join } from 'path';
2
+ import { homedir } from 'os';
3
+ import { existsSync, mkdirSync } from 'fs';
4
+
5
+ export function getConfigDir(): string {
6
+ return join(homedir(), '.openclaw', 'zalox');
7
+ }
8
+
9
+ export function ensureConfigDir(): void {
10
+ const dir = getConfigDir();
11
+ if (!existsSync(dir)) {
12
+ mkdirSync(dir, { recursive: true });
13
+ }
14
+ }