@jackle.dev/zalox-plugin 1.0.6 → 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 +1 -1
- package/src/core/templates.ts +93 -0
- package/src/index.ts +2 -0
- package/src/tools-template.ts +54 -0
- package/src/utils/config.ts +14 -0
package/package.json
CHANGED
|
@@ -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
|
@@ -9,6 +9,7 @@ import type { OpenClawPluginApi } from 'openclaw/plugin-sdk';
|
|
|
9
9
|
import { zaloxPlugin, zaloxDock } from './channel.js';
|
|
10
10
|
import { setPluginRuntime } from './listener.js';
|
|
11
11
|
import { zaloxGroupTool } from './tools.js';
|
|
12
|
+
import { zaloxTemplateTool } from './tools-template.js';
|
|
12
13
|
|
|
13
14
|
const plugin = {
|
|
14
15
|
id: 'zalox',
|
|
@@ -24,6 +25,7 @@ const plugin = {
|
|
|
24
25
|
|
|
25
26
|
// Register admin tool
|
|
26
27
|
api.registerTool(zaloxGroupTool);
|
|
28
|
+
api.registerTool(zaloxTemplateTool);
|
|
27
29
|
},
|
|
28
30
|
};
|
|
29
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
|
+
};
|
|
@@ -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
|
+
}
|