@quartal/hub-plugin 0.0.0-stage → 0.2.0
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/LICENSE +21 -0
- package/README.md +18 -3
- package/dist/mod.d.ts +683 -0
- package/dist/mod.js +742 -0
- package/package.json +38 -3
- package/schemas/channel-1.0.schema.json +341 -0
package/dist/mod.js
ADDED
|
@@ -0,0 +1,742 @@
|
|
|
1
|
+
//#region src/agentRef.ts
|
|
2
|
+
/** A plugin id or an agent name: lowercase letters, digits, `-`, `_` and `.`, starting with a letter or digit. */
|
|
3
|
+
const NAME = /^[a-z0-9][a-z0-9._-]*$/;
|
|
4
|
+
/**
|
|
5
|
+
* Resolves an agent reference of a channel definition.
|
|
6
|
+
* @param ref `<name>` or `<plugin id>/<name>`.
|
|
7
|
+
* @param ownPluginId The id of the plugin the definition is in.
|
|
8
|
+
* @returns The plugin and the agent, or undefined for a reference that is not well formed.
|
|
9
|
+
*/
|
|
10
|
+
function resolveAgentRef(ref, ownPluginId) {
|
|
11
|
+
const parts = ref.split("/");
|
|
12
|
+
if (parts.length > 2 || !parts.every((part) => NAME.test(part))) return void 0;
|
|
13
|
+
return parts.length === 2 ? {
|
|
14
|
+
pluginId: parts[0],
|
|
15
|
+
agentName: parts[1]
|
|
16
|
+
} : {
|
|
17
|
+
pluginId: ownPluginId,
|
|
18
|
+
agentName: parts[0]
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
//#endregion
|
|
22
|
+
//#region src/ChannelDefinition.ts
|
|
23
|
+
/** Every base status, in the order statistics show them. */
|
|
24
|
+
const CHANNEL_BASE_STATUSES = [
|
|
25
|
+
"unread",
|
|
26
|
+
"in_progress",
|
|
27
|
+
"handled",
|
|
28
|
+
"archived",
|
|
29
|
+
"error",
|
|
30
|
+
"warning"
|
|
31
|
+
];
|
|
32
|
+
/** Every trigger. */
|
|
33
|
+
const CHANNEL_AGENT_TRIGGERS = [
|
|
34
|
+
"manual",
|
|
35
|
+
"create_thread",
|
|
36
|
+
"company_message",
|
|
37
|
+
"advisor_message",
|
|
38
|
+
"assign",
|
|
39
|
+
"status_change",
|
|
40
|
+
"schedule"
|
|
41
|
+
];
|
|
42
|
+
/** Every channel kind; the first is the default. */
|
|
43
|
+
const CHANNEL_KINDS = ["multi-party", "copilot"];
|
|
44
|
+
/** Every schedule target, in the order they are offered. */
|
|
45
|
+
const CHANNEL_SCHEDULE_TARGETS = ["message", "thread"];
|
|
46
|
+
//#endregion
|
|
47
|
+
//#region src/constants.ts
|
|
48
|
+
/**
|
|
49
|
+
* Where Quartal Hub's extension lives in a plugin.
|
|
50
|
+
*
|
|
51
|
+
* @module
|
|
52
|
+
*/
|
|
53
|
+
/** The Agent Plugins extension namespace of Quartal Hub: the folder and the `plugin.json` key. */
|
|
54
|
+
const HUB_EXTENSION_NAMESPACE = "com.quartal.hub";
|
|
55
|
+
/** The channel definition's path in a plugin. */
|
|
56
|
+
const CHANNEL_DEFINITION_FILE = `${HUB_EXTENSION_NAMESPACE}/channel.json`;
|
|
57
|
+
/** The JSON Schema of the channel definition. */
|
|
58
|
+
const CHANNEL_DEFINITION_SCHEMA_URL = "https://hub.quartal.com/schemas/channel-1.0.schema.json";
|
|
59
|
+
/** The agent a new channel plugin starts with (`agents/default.md`). */
|
|
60
|
+
const DEFAULT_CHANNEL_AGENT = "default";
|
|
61
|
+
//#endregion
|
|
62
|
+
//#region src/hubTools.ts
|
|
63
|
+
/** Every setting value. */
|
|
64
|
+
const HUB_TOOL_SETTINGS = [
|
|
65
|
+
"on",
|
|
66
|
+
"allow",
|
|
67
|
+
"ask",
|
|
68
|
+
"off"
|
|
69
|
+
];
|
|
70
|
+
/** Every group. */
|
|
71
|
+
const HUB_TOOL_GROUPS = [
|
|
72
|
+
"thread",
|
|
73
|
+
"files",
|
|
74
|
+
"memory",
|
|
75
|
+
"plan",
|
|
76
|
+
"context",
|
|
77
|
+
"web"
|
|
78
|
+
];
|
|
79
|
+
const t = (name, group, level, extra = {}) => ({
|
|
80
|
+
name,
|
|
81
|
+
group,
|
|
82
|
+
level,
|
|
83
|
+
...extra
|
|
84
|
+
});
|
|
85
|
+
/** Every Hub tool, by group. */
|
|
86
|
+
const HUB_TOOLS = [
|
|
87
|
+
t("thread_set_status", "thread", "allow"),
|
|
88
|
+
t("ask_user", "thread", "allow", { readOnly: true }),
|
|
89
|
+
t("thread_assign", "thread", "allow"),
|
|
90
|
+
t("message_post", "thread", "off", { onLevel: "allow" }),
|
|
91
|
+
t("thread_create", "thread", "ask"),
|
|
92
|
+
t("thread_search", "thread", "allow", { readOnly: true }),
|
|
93
|
+
t("files_list", "files", "allow", { readOnly: true }),
|
|
94
|
+
t("files_read", "files", "allow", { readOnly: true }),
|
|
95
|
+
t("files_search", "files", "allow", { readOnly: true }),
|
|
96
|
+
t("files_write", "files", "ask"),
|
|
97
|
+
t("files_edit", "files", "ask"),
|
|
98
|
+
t("files_move", "files", "ask"),
|
|
99
|
+
t("files_copy", "files", "ask"),
|
|
100
|
+
t("files_mkdir", "files", "ask"),
|
|
101
|
+
t("files_delete", "files", "off", { onLevel: "ask" }),
|
|
102
|
+
t("memory", "memory", "allow"),
|
|
103
|
+
t("plan", "plan", "allow"),
|
|
104
|
+
t("company_context", "context", "allow", { readOnly: true }),
|
|
105
|
+
t("web_fetch", "web", "off", {
|
|
106
|
+
onLevel: "allow",
|
|
107
|
+
readOnly: true
|
|
108
|
+
})
|
|
109
|
+
];
|
|
110
|
+
/** The catalogue entry of a Hub tool; undefined for a name that is not one. */
|
|
111
|
+
function hubTool(name) {
|
|
112
|
+
return HUB_TOOLS.find((tool) => tool.name === name);
|
|
113
|
+
}
|
|
114
|
+
/** True for a Hub tool's or a group's name: a valid key of `hubTools`. */
|
|
115
|
+
function isHubToolKey(key) {
|
|
116
|
+
return !!hubTool(key) || HUB_TOOL_GROUPS.includes(key);
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* The level of every Hub tool for an agent: each tool's default, then its group's setting, then its
|
|
120
|
+
* own; a tool in `approve` asks.
|
|
121
|
+
* @param settings `hubTools` of the agent's entry in the definition.
|
|
122
|
+
* @param approve `approve` of the entry: tools that ask.
|
|
123
|
+
* @returns The level by tool name, for every tool in the catalogue.
|
|
124
|
+
*/
|
|
125
|
+
function resolveHubTools(settings, approve = []) {
|
|
126
|
+
const out = /* @__PURE__ */ new Map();
|
|
127
|
+
const apply = (tool, setting, current) => {
|
|
128
|
+
if (setting === void 0) return current;
|
|
129
|
+
if (setting !== "on") return setting;
|
|
130
|
+
return tool.onLevel ?? (tool.level === "off" ? "allow" : tool.level);
|
|
131
|
+
};
|
|
132
|
+
for (const tool of HUB_TOOLS) {
|
|
133
|
+
let level = tool.level;
|
|
134
|
+
level = apply(tool, settings?.[tool.group], level);
|
|
135
|
+
level = apply(tool, settings?.[tool.name], level);
|
|
136
|
+
if (level === "allow" && approve.includes(tool.name)) level = "ask";
|
|
137
|
+
out.set(tool.name, level);
|
|
138
|
+
}
|
|
139
|
+
return out;
|
|
140
|
+
}
|
|
141
|
+
//#endregion
|
|
142
|
+
//#region src/channelDefinitionSchema.ts
|
|
143
|
+
/**
|
|
144
|
+
* The JSON Schema of `com.quartal.hub/channel.json`, for editors. `parseChannelDefinition` checks
|
|
145
|
+
* the same rules and a few the schema cannot say (unique status keys and agents).
|
|
146
|
+
*
|
|
147
|
+
* @module
|
|
148
|
+
*/
|
|
149
|
+
const localizedText = { oneOf: [{ type: "string" }, {
|
|
150
|
+
type: "object",
|
|
151
|
+
properties: {
|
|
152
|
+
fi: { type: "string" },
|
|
153
|
+
en: { type: "string" },
|
|
154
|
+
sv: { type: "string" }
|
|
155
|
+
},
|
|
156
|
+
additionalProperties: false
|
|
157
|
+
}] };
|
|
158
|
+
/** The JSON Schema (draft 2020-12) of a channel definition. */
|
|
159
|
+
const channelDefinitionSchema = {
|
|
160
|
+
$schema: "https://json-schema.org/draft/2020-12/schema",
|
|
161
|
+
$id: CHANNEL_DEFINITION_SCHEMA_URL,
|
|
162
|
+
title: "Quartal Hub channel definition",
|
|
163
|
+
type: "object",
|
|
164
|
+
required: ["title"],
|
|
165
|
+
properties: {
|
|
166
|
+
$schema: { type: "string" },
|
|
167
|
+
title: {
|
|
168
|
+
...localizedText,
|
|
169
|
+
description: "The name a channel made from the definition gets."
|
|
170
|
+
},
|
|
171
|
+
description: localizedText,
|
|
172
|
+
kind: {
|
|
173
|
+
enum: [...CHANNEL_KINDS],
|
|
174
|
+
description: "multi-party (default): people and agents in Quartal's chat; copilot: one person and one agent per thread, open to AG-UI clients."
|
|
175
|
+
},
|
|
176
|
+
icon: {
|
|
177
|
+
type: "string",
|
|
178
|
+
description: "A Material Symbols icon name."
|
|
179
|
+
},
|
|
180
|
+
color: {
|
|
181
|
+
type: "string",
|
|
182
|
+
description: "A Bootstrap theme color name or a CSS color."
|
|
183
|
+
},
|
|
184
|
+
avatar: {
|
|
185
|
+
type: "string",
|
|
186
|
+
description: "A picture shown instead of the icon: a path in the plugin or a URL."
|
|
187
|
+
},
|
|
188
|
+
statuses: {
|
|
189
|
+
type: "array",
|
|
190
|
+
description: "The thread statuses, in view order; the first is a new thread's.",
|
|
191
|
+
items: {
|
|
192
|
+
type: "object",
|
|
193
|
+
required: [
|
|
194
|
+
"key",
|
|
195
|
+
"text",
|
|
196
|
+
"baseStatus"
|
|
197
|
+
],
|
|
198
|
+
properties: {
|
|
199
|
+
key: {
|
|
200
|
+
type: "string",
|
|
201
|
+
pattern: "^[a-z0-9][a-z0-9_-]*$"
|
|
202
|
+
},
|
|
203
|
+
text: localizedText,
|
|
204
|
+
description: localizedText,
|
|
205
|
+
baseStatus: { enum: [...CHANNEL_BASE_STATUSES] },
|
|
206
|
+
icon: { type: "string" },
|
|
207
|
+
color: { type: "string" }
|
|
208
|
+
},
|
|
209
|
+
additionalProperties: false
|
|
210
|
+
}
|
|
211
|
+
},
|
|
212
|
+
agents: {
|
|
213
|
+
type: "array",
|
|
214
|
+
items: {
|
|
215
|
+
type: "object",
|
|
216
|
+
required: ["agent", "triggers"],
|
|
217
|
+
properties: {
|
|
218
|
+
agent: {
|
|
219
|
+
type: "string",
|
|
220
|
+
pattern: "^[a-z0-9][a-z0-9._-]*(/[a-z0-9][a-z0-9._-]*)?$",
|
|
221
|
+
description: "<name> or <plugin id>/<name>"
|
|
222
|
+
},
|
|
223
|
+
triggers: {
|
|
224
|
+
type: "array",
|
|
225
|
+
items: { enum: [...CHANNEL_AGENT_TRIGGERS] },
|
|
226
|
+
uniqueItems: true
|
|
227
|
+
},
|
|
228
|
+
active: { type: "boolean" },
|
|
229
|
+
instructions: { type: "string" },
|
|
230
|
+
approve: {
|
|
231
|
+
type: "array",
|
|
232
|
+
items: {
|
|
233
|
+
type: "string",
|
|
234
|
+
minLength: 1
|
|
235
|
+
},
|
|
236
|
+
uniqueItems: true,
|
|
237
|
+
description: "Tools that run only after the person the agent answers approves the call."
|
|
238
|
+
},
|
|
239
|
+
plugins: {
|
|
240
|
+
type: "array",
|
|
241
|
+
items: {
|
|
242
|
+
type: "object",
|
|
243
|
+
required: ["id"],
|
|
244
|
+
properties: {
|
|
245
|
+
id: {
|
|
246
|
+
type: "string",
|
|
247
|
+
minLength: 1
|
|
248
|
+
},
|
|
249
|
+
tools: {
|
|
250
|
+
type: "array",
|
|
251
|
+
items: { type: "string" }
|
|
252
|
+
},
|
|
253
|
+
skills: {
|
|
254
|
+
type: "array",
|
|
255
|
+
items: { type: "string" }
|
|
256
|
+
},
|
|
257
|
+
widgets: { type: "boolean" },
|
|
258
|
+
auth: { enum: ["none", "token"] }
|
|
259
|
+
},
|
|
260
|
+
additionalProperties: false
|
|
261
|
+
}
|
|
262
|
+
},
|
|
263
|
+
hubTools: {
|
|
264
|
+
type: "object",
|
|
265
|
+
description: "The Hub's own tools for this agent, by tool or group: allow, ask (approval), off, or on (the tool's own level).",
|
|
266
|
+
propertyNames: { enum: [...HUB_TOOL_GROUPS, ...HUB_TOOLS.map((tool) => tool.name)] },
|
|
267
|
+
additionalProperties: { enum: [...HUB_TOOL_SETTINGS] }
|
|
268
|
+
},
|
|
269
|
+
schedules: {
|
|
270
|
+
type: "array",
|
|
271
|
+
description: "Time-based runs: at each time, the agent gets the message in each customer company the schedule names, acting for the company with its own agent token.",
|
|
272
|
+
items: {
|
|
273
|
+
type: "object",
|
|
274
|
+
required: [
|
|
275
|
+
"id",
|
|
276
|
+
"cron",
|
|
277
|
+
"message"
|
|
278
|
+
],
|
|
279
|
+
properties: {
|
|
280
|
+
id: {
|
|
281
|
+
type: "string",
|
|
282
|
+
pattern: "^[a-z0-9][a-z0-9_-]*$"
|
|
283
|
+
},
|
|
284
|
+
cron: {
|
|
285
|
+
type: "string",
|
|
286
|
+
description: "minute hour day-of-month month day-of-week, e.g. \"0 9 5 * *\"."
|
|
287
|
+
},
|
|
288
|
+
timeZone: {
|
|
289
|
+
type: "string",
|
|
290
|
+
description: "IANA time zone (default Europe/Helsinki)."
|
|
291
|
+
},
|
|
292
|
+
message: {
|
|
293
|
+
type: "string",
|
|
294
|
+
minLength: 1
|
|
295
|
+
},
|
|
296
|
+
threadTitle: { type: "string" },
|
|
297
|
+
companies: {
|
|
298
|
+
description: "The companies it runs for: \"all\" (default) for every customer the channel lists, or their directory ids.",
|
|
299
|
+
oneOf: [{ const: "all" }, {
|
|
300
|
+
type: "array",
|
|
301
|
+
items: {
|
|
302
|
+
type: "string",
|
|
303
|
+
pattern: "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$"
|
|
304
|
+
},
|
|
305
|
+
uniqueItems: true
|
|
306
|
+
}]
|
|
307
|
+
},
|
|
308
|
+
target: {
|
|
309
|
+
enum: [...CHANNEL_SCHEDULE_TARGETS],
|
|
310
|
+
description: "Where each run goes (default message): a new message in one thread per company, or a new thread each time."
|
|
311
|
+
}
|
|
312
|
+
},
|
|
313
|
+
additionalProperties: false
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
},
|
|
317
|
+
additionalProperties: false
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
},
|
|
321
|
+
additionalProperties: false
|
|
322
|
+
};
|
|
323
|
+
//#endregion
|
|
324
|
+
//#region src/hubSite.ts
|
|
325
|
+
/**
|
|
326
|
+
* The placeholder of the Hub's address in a plugin's settings. The plugin host replaces it with the
|
|
327
|
+
* address of the Hub it serves (`HOST_HUB_URL`), so the same plugin works from any Hub and moves
|
|
328
|
+
* between repositories as it is.
|
|
329
|
+
*/
|
|
330
|
+
const HUB_URL_PLACEHOLDER = "{hub}";
|
|
331
|
+
/**
|
|
332
|
+
* A plugin's page in the Hub, as the start page of a `hub` plugin: `{hub}` is the Hub's address
|
|
333
|
+
* (filled by the plugin host), `{name}` the plugin's id (filled by the plugin runtime).
|
|
334
|
+
*/
|
|
335
|
+
const HUB_PLUGIN_PAGE = `${HUB_URL_PLACEHOLDER}/app/plugins/hosted/{name}`;
|
|
336
|
+
/**
|
|
337
|
+
* The `auth` and `site` settings for a Hub plugin's `qrtl.config.json`.
|
|
338
|
+
* @param kind What the web site is.
|
|
339
|
+
* @param options The public pages.
|
|
340
|
+
*/
|
|
341
|
+
function hubPluginSettings(kind, options = {}) {
|
|
342
|
+
if (kind === "app") return {
|
|
343
|
+
auth: "quartal-hub",
|
|
344
|
+
site: {
|
|
345
|
+
root: "pages",
|
|
346
|
+
access: "user",
|
|
347
|
+
...options.publicPages?.length ? { public: options.publicPages } : {}
|
|
348
|
+
}
|
|
349
|
+
};
|
|
350
|
+
return {
|
|
351
|
+
auth: "quartal-hub",
|
|
352
|
+
site: { root: { redirect: HUB_PLUGIN_PAGE } }
|
|
353
|
+
};
|
|
354
|
+
}
|
|
355
|
+
//#endregion
|
|
356
|
+
//#region src/LocalizedText.ts
|
|
357
|
+
/** The languages of Quartal Hub, the fallback first. */
|
|
358
|
+
const HUB_LANGUAGES = [
|
|
359
|
+
"fi",
|
|
360
|
+
"en",
|
|
361
|
+
"sv"
|
|
362
|
+
];
|
|
363
|
+
/**
|
|
364
|
+
* The text in a language: the language's own, else Finnish, else the first one there is.
|
|
365
|
+
* @param text The text.
|
|
366
|
+
* @param language The wanted language (`fi`, `en`, `sv`; a longer tag such as `en-GB` works too).
|
|
367
|
+
* @returns The text, or an empty string when there is none.
|
|
368
|
+
*/
|
|
369
|
+
function localize(text, language) {
|
|
370
|
+
if (text === void 0) return "";
|
|
371
|
+
if (typeof text === "string") return text;
|
|
372
|
+
const lang = language?.slice(0, 2).toLowerCase();
|
|
373
|
+
return lang && text[lang] || text.fi || Object.values(text).find((value) => !!value) || "";
|
|
374
|
+
}
|
|
375
|
+
/**
|
|
376
|
+
* Whether a value is a localized text: a string, or an object of known languages with strings.
|
|
377
|
+
* @param value The value to test.
|
|
378
|
+
*/
|
|
379
|
+
function isLocalizedText(value) {
|
|
380
|
+
if (typeof value === "string") return true;
|
|
381
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return false;
|
|
382
|
+
return Object.entries(value).every(([lang, text]) => HUB_LANGUAGES.includes(lang) && typeof text === "string");
|
|
383
|
+
}
|
|
384
|
+
//#endregion
|
|
385
|
+
//#region src/channelPluginFiles.ts
|
|
386
|
+
function yamlString(value) {
|
|
387
|
+
return JSON.stringify(value);
|
|
388
|
+
}
|
|
389
|
+
/**
|
|
390
|
+
* The files of a new channel plugin, by path: `com.quartal.hub/channel.json`, `agents/default.md`,
|
|
391
|
+
* `qrtl.config.json` (Hub sign-in, the start page in the Hub) and `README.md`.
|
|
392
|
+
* @param options What the channel starts with.
|
|
393
|
+
*/
|
|
394
|
+
function channelPluginFiles(options) {
|
|
395
|
+
const title = localize(options.title, "fi");
|
|
396
|
+
const description = localize(options.description, "fi");
|
|
397
|
+
const definition = {
|
|
398
|
+
$schema: CHANNEL_DEFINITION_SCHEMA_URL,
|
|
399
|
+
title: options.title,
|
|
400
|
+
...options.description ? { description: options.description } : {},
|
|
401
|
+
...options.icon ? { icon: options.icon } : {},
|
|
402
|
+
...options.color ? { color: options.color } : {},
|
|
403
|
+
statuses: options.statuses ?? [],
|
|
404
|
+
agents: [{
|
|
405
|
+
agent: DEFAULT_CHANNEL_AGENT,
|
|
406
|
+
triggers: options.triggers?.length ? options.triggers : ["company_message"],
|
|
407
|
+
...options.instructions ? { instructions: options.instructions } : {},
|
|
408
|
+
...options.plugins?.length ? { plugins: options.plugins } : {}
|
|
409
|
+
}]
|
|
410
|
+
};
|
|
411
|
+
const prompt = options.prompt?.trim() || `You are the assistant of the channel "${title}". Answer in the language of the question.`;
|
|
412
|
+
const agent = [
|
|
413
|
+
"---",
|
|
414
|
+
`name: ${DEFAULT_CHANNEL_AGENT}`,
|
|
415
|
+
`description: ${yamlString(`Answers in the channel ${title}.`)}`,
|
|
416
|
+
...options.model ? [`model: ${options.model}`] : [],
|
|
417
|
+
"---",
|
|
418
|
+
"",
|
|
419
|
+
prompt,
|
|
420
|
+
""
|
|
421
|
+
].join("\n");
|
|
422
|
+
const config = {
|
|
423
|
+
...options.baseConfig ?? {},
|
|
424
|
+
title,
|
|
425
|
+
description: description || `The channel ${title}.`,
|
|
426
|
+
...hubPluginSettings("hub")
|
|
427
|
+
};
|
|
428
|
+
return {
|
|
429
|
+
[CHANNEL_DEFINITION_FILE]: `${JSON.stringify(definition, null, 2)}\n`,
|
|
430
|
+
[`agents/${DEFAULT_CHANNEL_AGENT}.md`]: agent,
|
|
431
|
+
"qrtl.config.json": `${JSON.stringify(config, null, 2)}\n`,
|
|
432
|
+
"README.md": `# ${title}\n\n${description || `The channel ${title}.`}\n\nA Quartal Hub channel: its definition is \`${CHANNEL_DEFINITION_FILE}\`, its agent \`agents/${DEFAULT_CHANNEL_AGENT}.md\`.\n`
|
|
433
|
+
};
|
|
434
|
+
}
|
|
435
|
+
//#endregion
|
|
436
|
+
//#region src/cron.ts
|
|
437
|
+
/**
|
|
438
|
+
* Five-field cron expressions (`minute hour day-of-month month day-of-week`) for the schedules of
|
|
439
|
+
* channel agents. Fields take `*`, numbers, ranges `a-b`, lists `a,b` and steps `* /n` or `a-b/n`
|
|
440
|
+
* (written without the space). Day of week is 0–6 from Sunday (7 is Sunday too). When both day of
|
|
441
|
+
* month and day of week are restricted, either one matching is enough (as in cron).
|
|
442
|
+
*
|
|
443
|
+
* @module
|
|
444
|
+
*/
|
|
445
|
+
/** The time zone schedules are read in when the definition names none. */
|
|
446
|
+
const DEFAULT_SCHEDULE_TIME_ZONE = "Europe/Helsinki";
|
|
447
|
+
const RANGES = [
|
|
448
|
+
[0, 59],
|
|
449
|
+
[0, 23],
|
|
450
|
+
[1, 31],
|
|
451
|
+
[1, 12],
|
|
452
|
+
[0, 7]
|
|
453
|
+
];
|
|
454
|
+
const NAMES = [
|
|
455
|
+
"minute",
|
|
456
|
+
"hour",
|
|
457
|
+
"day of month",
|
|
458
|
+
"month",
|
|
459
|
+
"day of week"
|
|
460
|
+
];
|
|
461
|
+
function parseField(text, index) {
|
|
462
|
+
const [min, max] = RANGES[index];
|
|
463
|
+
const values = /* @__PURE__ */ new Set();
|
|
464
|
+
for (const item of text.split(",")) {
|
|
465
|
+
const match = /^(\*|(\d+)(?:-(\d+))?)(?:\/(\d+))?$/.exec(item);
|
|
466
|
+
if (!match) throw new Error(`${NAMES[index]}: "${item}" is not understood.`);
|
|
467
|
+
const step = match[4] ? Number(match[4]) : 1;
|
|
468
|
+
if (step < 1) throw new Error(`${NAMES[index]}: the step must be at least 1.`);
|
|
469
|
+
const from = match[1] === "*" ? min : Number(match[2]);
|
|
470
|
+
const to = match[1] === "*" ? max : match[3] !== void 0 ? Number(match[3]) : match[4] ? max : from;
|
|
471
|
+
if (from < min || to > max || from > to) throw new Error(`${NAMES[index]}: ${min}–${max}.`);
|
|
472
|
+
for (let v = from; v <= to; v += step) values.add(index === 4 && v === 7 ? 0 : v);
|
|
473
|
+
}
|
|
474
|
+
return {
|
|
475
|
+
values,
|
|
476
|
+
any: text === "*"
|
|
477
|
+
};
|
|
478
|
+
}
|
|
479
|
+
/**
|
|
480
|
+
* Parses a five-field cron expression.
|
|
481
|
+
* @param expression E.g. `0 9 5 * *` (09:00 on the 5th of every month).
|
|
482
|
+
* @throws When the expression is not valid; the message says which field.
|
|
483
|
+
*/
|
|
484
|
+
function parseCron(expression) {
|
|
485
|
+
const parts = expression.trim().split(/\s+/);
|
|
486
|
+
if (parts.length !== 5) throw new Error("Five fields: minute hour day-of-month month day-of-week.");
|
|
487
|
+
const [minute, hour, dom, month, dow] = parts.map(parseField);
|
|
488
|
+
return { matches(date, timeZone = DEFAULT_SCHEDULE_TIME_ZONE) {
|
|
489
|
+
const local = zonedParts(date, timeZone);
|
|
490
|
+
if (!minute.values.has(local.minute) || !hour.values.has(local.hour) || !month.values.has(local.month)) return false;
|
|
491
|
+
const domOk = dom.values.has(local.day);
|
|
492
|
+
const dowOk = dow.values.has(local.weekday);
|
|
493
|
+
if (dom.any || dow.any) return domOk && dowOk;
|
|
494
|
+
return domOk || dowOk;
|
|
495
|
+
} };
|
|
496
|
+
}
|
|
497
|
+
/** The reason a cron expression is not valid; undefined when it is. */
|
|
498
|
+
function cronError(expression) {
|
|
499
|
+
try {
|
|
500
|
+
parseCron(expression);
|
|
501
|
+
return;
|
|
502
|
+
} catch (error) {
|
|
503
|
+
return error.message;
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
/** True when `timeZone` is a time zone the runtime knows. */
|
|
507
|
+
function isTimeZone(timeZone) {
|
|
508
|
+
try {
|
|
509
|
+
new Intl.DateTimeFormat("en", { timeZone });
|
|
510
|
+
return true;
|
|
511
|
+
} catch {
|
|
512
|
+
return false;
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
const WEEKDAYS = [
|
|
516
|
+
"Sun",
|
|
517
|
+
"Mon",
|
|
518
|
+
"Tue",
|
|
519
|
+
"Wed",
|
|
520
|
+
"Thu",
|
|
521
|
+
"Fri",
|
|
522
|
+
"Sat"
|
|
523
|
+
];
|
|
524
|
+
function zonedParts(date, timeZone) {
|
|
525
|
+
const parts = new Intl.DateTimeFormat("en-US", {
|
|
526
|
+
timeZone,
|
|
527
|
+
hourCycle: "h23",
|
|
528
|
+
minute: "numeric",
|
|
529
|
+
hour: "numeric",
|
|
530
|
+
day: "numeric",
|
|
531
|
+
month: "numeric",
|
|
532
|
+
weekday: "short"
|
|
533
|
+
}).formatToParts(date);
|
|
534
|
+
const get = (type) => parts.find((p) => p.type === type)?.value ?? "";
|
|
535
|
+
return {
|
|
536
|
+
minute: Number(get("minute")),
|
|
537
|
+
hour: Number(get("hour")),
|
|
538
|
+
day: Number(get("day")),
|
|
539
|
+
month: Number(get("month")),
|
|
540
|
+
weekday: WEEKDAYS.indexOf(get("weekday"))
|
|
541
|
+
};
|
|
542
|
+
}
|
|
543
|
+
//#endregion
|
|
544
|
+
//#region src/parseChannelDefinition.ts
|
|
545
|
+
/**
|
|
546
|
+
* Reads and checks a channel definition: the shape of every field, unique status keys and agent
|
|
547
|
+
* references, known triggers and base statuses. Whether the agents exist is up to the caller, who
|
|
548
|
+
* can reach the plugins.
|
|
549
|
+
*
|
|
550
|
+
* @module
|
|
551
|
+
*/
|
|
552
|
+
const STATUS_KEY = /^[a-z0-9][a-z0-9_-]*$/;
|
|
553
|
+
const SCHEDULE_ID = STATUS_KEY;
|
|
554
|
+
/** A directory company id: a lowercase UUID. */
|
|
555
|
+
const COMPANY_ID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
|
|
556
|
+
function isObject(value) {
|
|
557
|
+
return !!value && typeof value === "object" && !Array.isArray(value);
|
|
558
|
+
}
|
|
559
|
+
function isStringArray(value) {
|
|
560
|
+
return Array.isArray(value) && value.every((item) => typeof item === "string");
|
|
561
|
+
}
|
|
562
|
+
/**
|
|
563
|
+
* Parses and checks a channel definition.
|
|
564
|
+
* @param input The file's text or its parsed JSON.
|
|
565
|
+
* @param ownPluginId The id of the plugin the definition is in (resolves `<name>` agent references).
|
|
566
|
+
*/
|
|
567
|
+
function parseChannelDefinition(input, ownPluginId) {
|
|
568
|
+
const errors = [];
|
|
569
|
+
let raw = input;
|
|
570
|
+
if (typeof input === "string") try {
|
|
571
|
+
raw = JSON.parse(input);
|
|
572
|
+
} catch (error) {
|
|
573
|
+
return {
|
|
574
|
+
ok: false,
|
|
575
|
+
errors: [`Not valid JSON: ${error.message}`]
|
|
576
|
+
};
|
|
577
|
+
}
|
|
578
|
+
if (!isObject(raw)) return {
|
|
579
|
+
ok: false,
|
|
580
|
+
errors: ["The definition must be a JSON object."]
|
|
581
|
+
};
|
|
582
|
+
const text = (value, path, required) => {
|
|
583
|
+
if (value === void 0) {
|
|
584
|
+
if (required) errors.push(`${path}: required.`);
|
|
585
|
+
return;
|
|
586
|
+
}
|
|
587
|
+
if (!isLocalizedText(value)) {
|
|
588
|
+
errors.push(`${path}: a string, or an object of fi, en and sv strings.`);
|
|
589
|
+
return;
|
|
590
|
+
}
|
|
591
|
+
return value;
|
|
592
|
+
};
|
|
593
|
+
const optionalString = (value, path) => {
|
|
594
|
+
if (value === void 0) return void 0;
|
|
595
|
+
if (typeof value !== "string") {
|
|
596
|
+
errors.push(`${path}: a string.`);
|
|
597
|
+
return;
|
|
598
|
+
}
|
|
599
|
+
return value;
|
|
600
|
+
};
|
|
601
|
+
const definition = {
|
|
602
|
+
...typeof raw.$schema === "string" ? { $schema: raw.$schema } : {},
|
|
603
|
+
title: text(raw.title, "title", true) ?? "",
|
|
604
|
+
statuses: [],
|
|
605
|
+
agents: []
|
|
606
|
+
};
|
|
607
|
+
const description = text(raw.description, "description", false);
|
|
608
|
+
if (description !== void 0) definition.description = description;
|
|
609
|
+
if (raw.kind !== void 0) if (!CHANNEL_KINDS.includes(raw.kind)) errors.push(`kind: one of ${CHANNEL_KINDS.join(", ")}.`);
|
|
610
|
+
else definition.kind = raw.kind;
|
|
611
|
+
for (const field of [
|
|
612
|
+
"icon",
|
|
613
|
+
"color",
|
|
614
|
+
"avatar"
|
|
615
|
+
]) {
|
|
616
|
+
const value = optionalString(raw[field], field);
|
|
617
|
+
if (value !== void 0) definition[field] = value;
|
|
618
|
+
}
|
|
619
|
+
if (raw.statuses !== void 0 && !Array.isArray(raw.statuses)) errors.push("statuses: an array.");
|
|
620
|
+
const keys = /* @__PURE__ */ new Set();
|
|
621
|
+
(Array.isArray(raw.statuses) ? raw.statuses : []).forEach((item, i) => {
|
|
622
|
+
const path = `statuses[${i}]`;
|
|
623
|
+
if (!isObject(item)) return void errors.push(`${path}: an object.`);
|
|
624
|
+
const key = item.key;
|
|
625
|
+
if (typeof key !== "string" || !STATUS_KEY.test(key)) errors.push(`${path}.key: lowercase letters, digits, - and _.`);
|
|
626
|
+
else if (keys.has(key)) errors.push(`${path}.key: "${key}" is used twice.`);
|
|
627
|
+
else keys.add(key);
|
|
628
|
+
if (!CHANNEL_BASE_STATUSES.includes(item.baseStatus)) errors.push(`${path}.baseStatus: one of ${CHANNEL_BASE_STATUSES.join(", ")}.`);
|
|
629
|
+
const status = {
|
|
630
|
+
key: String(key ?? ""),
|
|
631
|
+
text: text(item.text, `${path}.text`, true) ?? "",
|
|
632
|
+
baseStatus: item.baseStatus
|
|
633
|
+
};
|
|
634
|
+
const statusDescription = text(item.description, `${path}.description`, false);
|
|
635
|
+
if (statusDescription !== void 0) status.description = statusDescription;
|
|
636
|
+
const icon = optionalString(item.icon, `${path}.icon`);
|
|
637
|
+
if (icon !== void 0) status.icon = icon;
|
|
638
|
+
const color = optionalString(item.color, `${path}.color`);
|
|
639
|
+
if (color !== void 0) status.color = color;
|
|
640
|
+
definition.statuses.push(status);
|
|
641
|
+
});
|
|
642
|
+
if (raw.agents !== void 0 && !Array.isArray(raw.agents)) errors.push("agents: an array.");
|
|
643
|
+
const refs = /* @__PURE__ */ new Set();
|
|
644
|
+
(Array.isArray(raw.agents) ? raw.agents : []).forEach((item, i) => {
|
|
645
|
+
const path = `agents[${i}]`;
|
|
646
|
+
if (!isObject(item)) return void errors.push(`${path}: an object.`);
|
|
647
|
+
const ref = typeof item.agent === "string" ? resolveAgentRef(item.agent, ownPluginId) : void 0;
|
|
648
|
+
if (!ref) errors.push(`${path}.agent: "<name>" or "<plugin id>/<name>".`);
|
|
649
|
+
else {
|
|
650
|
+
const id = `${ref.pluginId}/${ref.agentName}`;
|
|
651
|
+
if (refs.has(id)) errors.push(`${path}.agent: "${item.agent}" is listed twice.`);
|
|
652
|
+
refs.add(id);
|
|
653
|
+
}
|
|
654
|
+
const triggers = item.triggers ?? [];
|
|
655
|
+
if (!isStringArray(triggers) || !triggers.every((t) => CHANNEL_AGENT_TRIGGERS.includes(t))) errors.push(`${path}.triggers: an array of ${CHANNEL_AGENT_TRIGGERS.join(", ")}.`);
|
|
656
|
+
const agent = {
|
|
657
|
+
agent: String(item.agent ?? ""),
|
|
658
|
+
triggers: isStringArray(triggers) ? [...new Set(triggers)] : []
|
|
659
|
+
};
|
|
660
|
+
if (item.active !== void 0) if (typeof item.active !== "boolean") errors.push(`${path}.active: true or false.`);
|
|
661
|
+
else agent.active = item.active;
|
|
662
|
+
const instructions = optionalString(item.instructions, `${path}.instructions`);
|
|
663
|
+
if (instructions !== void 0) agent.instructions = instructions;
|
|
664
|
+
if (item.approve !== void 0) if (!isStringArray(item.approve) || item.approve.some((name) => !name)) errors.push(`${path}.approve: an array of tool names.`);
|
|
665
|
+
else agent.approve = [...new Set(item.approve)];
|
|
666
|
+
if (item.plugins !== void 0) if (!Array.isArray(item.plugins)) errors.push(`${path}.plugins: an array.`);
|
|
667
|
+
else {
|
|
668
|
+
const ids = /* @__PURE__ */ new Set();
|
|
669
|
+
agent.plugins = [];
|
|
670
|
+
item.plugins.forEach((plugin, j) => {
|
|
671
|
+
const pp = `${path}.plugins[${j}]`;
|
|
672
|
+
if (!isObject(plugin)) return void errors.push(`${pp}: an object.`);
|
|
673
|
+
if (typeof plugin.id !== "string" || !plugin.id) return void errors.push(`${pp}.id: required.`);
|
|
674
|
+
if (ids.has(plugin.id)) errors.push(`${pp}.id: "${plugin.id}" is listed twice.`);
|
|
675
|
+
ids.add(plugin.id);
|
|
676
|
+
const use = { id: plugin.id };
|
|
677
|
+
for (const list of ["tools", "skills"]) {
|
|
678
|
+
if (plugin[list] === void 0) continue;
|
|
679
|
+
if (!isStringArray(plugin[list])) errors.push(`${pp}.${list}: an array of names.`);
|
|
680
|
+
else use[list] = plugin[list];
|
|
681
|
+
}
|
|
682
|
+
if (plugin.widgets !== void 0) if (typeof plugin.widgets !== "boolean") errors.push(`${pp}.widgets: true or false.`);
|
|
683
|
+
else use.widgets = plugin.widgets;
|
|
684
|
+
if (plugin.auth !== void 0) if (plugin.auth !== "none" && plugin.auth !== "token") errors.push(`${pp}.auth: none or token.`);
|
|
685
|
+
else use.auth = plugin.auth;
|
|
686
|
+
agent.plugins.push(use);
|
|
687
|
+
});
|
|
688
|
+
}
|
|
689
|
+
if (item.hubTools !== void 0) if (!isObject(item.hubTools)) errors.push(`${path}.hubTools: an object of tool or group names.`);
|
|
690
|
+
else {
|
|
691
|
+
agent.hubTools = {};
|
|
692
|
+
for (const [key, value] of Object.entries(item.hubTools)) if (!isHubToolKey(key)) errors.push(`${path}.hubTools.${key}: not a Hub tool or group.`);
|
|
693
|
+
else if (!HUB_TOOL_SETTINGS.includes(value)) errors.push(`${path}.hubTools.${key}: one of ${HUB_TOOL_SETTINGS.join(", ")}.`);
|
|
694
|
+
else agent.hubTools[key] = value;
|
|
695
|
+
}
|
|
696
|
+
if (item.schedules !== void 0) if (!Array.isArray(item.schedules)) errors.push(`${path}.schedules: an array.`);
|
|
697
|
+
else {
|
|
698
|
+
const ids = /* @__PURE__ */ new Set();
|
|
699
|
+
agent.schedules = [];
|
|
700
|
+
item.schedules.forEach((schedule, j) => {
|
|
701
|
+
const sp = `${path}.schedules[${j}]`;
|
|
702
|
+
if (!isObject(schedule)) return void errors.push(`${sp}: an object.`);
|
|
703
|
+
const id = schedule.id;
|
|
704
|
+
if (typeof id !== "string" || !SCHEDULE_ID.test(id)) errors.push(`${sp}.id: lowercase letters, digits, - and _.`);
|
|
705
|
+
else if (ids.has(id)) errors.push(`${sp}.id: "${id}" is used twice.`);
|
|
706
|
+
else ids.add(id);
|
|
707
|
+
const cron = typeof schedule.cron === "string" ? schedule.cron : "";
|
|
708
|
+
const cronProblem = cron ? cronError(cron) : "required.";
|
|
709
|
+
if (cronProblem) errors.push(`${sp}.cron: ${cronProblem}`);
|
|
710
|
+
if (typeof schedule.message !== "string" || !schedule.message.trim()) errors.push(`${sp}.message: required.`);
|
|
711
|
+
const run = {
|
|
712
|
+
id: String(id ?? ""),
|
|
713
|
+
cron,
|
|
714
|
+
message: String(schedule.message ?? "")
|
|
715
|
+
};
|
|
716
|
+
const timeZone = optionalString(schedule.timeZone, `${sp}.timeZone`);
|
|
717
|
+
if (timeZone !== void 0) if (!isTimeZone(timeZone)) errors.push(`${sp}.timeZone: an IANA time zone, e.g. Europe/Helsinki.`);
|
|
718
|
+
else run.timeZone = timeZone;
|
|
719
|
+
const threadTitle = optionalString(schedule.threadTitle, `${sp}.threadTitle`);
|
|
720
|
+
if (threadTitle !== void 0) run.threadTitle = threadTitle;
|
|
721
|
+
if (schedule.companies !== void 0) if (schedule.companies === "all") run.companies = "all";
|
|
722
|
+
else if (Array.isArray(schedule.companies) && schedule.companies.every((c) => typeof c === "string" && COMPANY_ID.test(c))) run.companies = [...new Set(schedule.companies)];
|
|
723
|
+
else errors.push(`${sp}.companies: "all" or an array of company ids.`);
|
|
724
|
+
if (schedule.target !== void 0) if (CHANNEL_SCHEDULE_TARGETS.includes(schedule.target)) run.target = schedule.target;
|
|
725
|
+
else errors.push(`${sp}.target: one of ${CHANNEL_SCHEDULE_TARGETS.join(", ")}.`);
|
|
726
|
+
agent.schedules.push(run);
|
|
727
|
+
});
|
|
728
|
+
if (agent.schedules.length && !agent.triggers.includes("schedule")) agent.triggers.push("schedule");
|
|
729
|
+
}
|
|
730
|
+
definition.agents.push(agent);
|
|
731
|
+
});
|
|
732
|
+
if (definition.kind === "copilot" && definition.agents.filter((a) => a.active !== false).length !== 1) errors.push("agents: a copilot channel has exactly one active agent.");
|
|
733
|
+
return errors.length ? {
|
|
734
|
+
ok: false,
|
|
735
|
+
errors
|
|
736
|
+
} : {
|
|
737
|
+
ok: true,
|
|
738
|
+
definition
|
|
739
|
+
};
|
|
740
|
+
}
|
|
741
|
+
//#endregion
|
|
742
|
+
export { CHANNEL_AGENT_TRIGGERS, CHANNEL_BASE_STATUSES, CHANNEL_DEFINITION_FILE, CHANNEL_DEFINITION_SCHEMA_URL, CHANNEL_KINDS, CHANNEL_SCHEDULE_TARGETS, DEFAULT_CHANNEL_AGENT, DEFAULT_SCHEDULE_TIME_ZONE, HUB_EXTENSION_NAMESPACE, HUB_LANGUAGES, HUB_PLUGIN_PAGE, HUB_TOOLS, HUB_TOOL_GROUPS, HUB_TOOL_SETTINGS, HUB_URL_PLACEHOLDER, channelDefinitionSchema, channelPluginFiles, cronError, hubPluginSettings, hubTool, isHubToolKey, isLocalizedText, isTimeZone, localize, parseChannelDefinition, parseCron, resolveAgentRef, resolveHubTools };
|