@jackle.dev/zalox-plugin 1.0.6 → 1.0.8
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 +4 -0
- package/src/tools-scenario.ts +48 -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,8 @@ 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';
|
|
13
|
+
import { zaloxScenarioTool } from './tools-scenario.js';
|
|
12
14
|
|
|
13
15
|
const plugin = {
|
|
14
16
|
id: 'zalox',
|
|
@@ -24,6 +26,8 @@ const plugin = {
|
|
|
24
26
|
|
|
25
27
|
// Register admin tool
|
|
26
28
|
api.registerTool(zaloxGroupTool);
|
|
29
|
+
api.registerTool(zaloxTemplateTool);
|
|
30
|
+
api.registerTool(zaloxScenarioTool);
|
|
27
31
|
},
|
|
28
32
|
};
|
|
29
33
|
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { createTemplate } from './core/templates.js';
|
|
3
|
+
|
|
4
|
+
export const zaloxScenarioTool = {
|
|
5
|
+
name: 'zalox_scenario',
|
|
6
|
+
description: 'Import scenario templates from a URL or Registry ID.',
|
|
7
|
+
parameters: z.object({
|
|
8
|
+
action: z.enum(['import']),
|
|
9
|
+
url: z.string().optional().describe('Direct URL to scenario JSON'),
|
|
10
|
+
registryId: z.string().optional().describe('Registry ID (e.g., "appointment-booking")'),
|
|
11
|
+
}),
|
|
12
|
+
execute: async (args: any) => {
|
|
13
|
+
const { action, url, registryId } = args;
|
|
14
|
+
if (action !== 'import') return 'Only import action supported';
|
|
15
|
+
|
|
16
|
+
let targetUrl = url;
|
|
17
|
+
if (!targetUrl && registryId) {
|
|
18
|
+
// Default to jackle.dev registry
|
|
19
|
+
targetUrl = `https://zalox.jackle.dev/registry/${registryId}.json`;
|
|
20
|
+
}
|
|
21
|
+
if (!targetUrl) throw new Error('url or registryId required');
|
|
22
|
+
|
|
23
|
+
try {
|
|
24
|
+
const res = await fetch(targetUrl);
|
|
25
|
+
if (!res.ok) throw new Error(`Fetch failed: ${res.statusText}`);
|
|
26
|
+
const data = await res.json();
|
|
27
|
+
|
|
28
|
+
const results = [];
|
|
29
|
+
if (Array.isArray(data.templates)) {
|
|
30
|
+
for (const t of data.templates) {
|
|
31
|
+
try {
|
|
32
|
+
createTemplate(t.name, t.content);
|
|
33
|
+
results.push(`✅ Created "${t.name}"`);
|
|
34
|
+
} catch (e: any) {
|
|
35
|
+
if (e.message.includes('exists')) {
|
|
36
|
+
results.push(`⚠️ Skipped "${t.name}" (already exists)`);
|
|
37
|
+
} else {
|
|
38
|
+
results.push(`❌ Failed "${t.name}": ${e.message}`);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return `Imported scenario "${data.name || 'Unknown'}":\n${results.join('\n')}`;
|
|
44
|
+
} catch (e: any) {
|
|
45
|
+
return `Scenario import failed: ${e.message}`;
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
};
|
|
@@ -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
|
+
}
|