@x-tech-laboratories/x-app 0.132.1 → 0.134.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/README.md +9 -7
- package/dist/config_watch.d.ts +1 -15
- package/dist/config_watch.js +2 -75
- package/dist/cron.d.ts +1 -34
- package/dist/cron.js +2 -187
- package/dist/log.d.ts +1 -30
- package/dist/log.js +2 -85
- package/dist/plugin_folder.d.ts +1 -24
- package/dist/plugin_folder.js +2 -107
- package/dist/plugins.d.ts +1 -138
- package/dist/plugins.js +2 -346
- package/dist/python_text.d.ts +1 -19
- package/dist/python_text.js +2 -363
- package/dist/server/agents/agent.d.ts +1 -27
- package/dist/server/agents/agent.js +2 -16
- package/dist/server/agents/connection.d.ts +1 -63
- package/dist/server/agents/connection.js +2 -266
- package/dist/server/agents/hub.d.ts +1 -110
- package/dist/server/agents/hub.js +2 -327
- package/dist/sql.d.ts +1 -27
- package/dist/sql.js +2 -61
- package/dist/web/react/charts.d.ts +4 -0
- package/dist/web/react/charts.js +43 -6
- package/dist/web/react/dashboard.js +2 -2
- package/package.json +2 -4
package/dist/plugins.d.ts
CHANGED
|
@@ -1,138 +1 @@
|
|
|
1
|
-
export
|
|
2
|
-
export type SettingValues = Record<string, SettingValue>;
|
|
3
|
-
interface SettingBase {
|
|
4
|
-
key: string;
|
|
5
|
-
label: string;
|
|
6
|
-
hint?: string;
|
|
7
|
-
}
|
|
8
|
-
export type SettingField = (SettingBase & {
|
|
9
|
-
type: "text";
|
|
10
|
-
default: string;
|
|
11
|
-
placeholder?: string;
|
|
12
|
-
secret?: boolean;
|
|
13
|
-
}) | (SettingBase & {
|
|
14
|
-
type: "number";
|
|
15
|
-
default: number;
|
|
16
|
-
min?: number;
|
|
17
|
-
max?: number;
|
|
18
|
-
step?: number;
|
|
19
|
-
}) | (SettingBase & {
|
|
20
|
-
type: "switch";
|
|
21
|
-
default: boolean;
|
|
22
|
-
}) | (SettingBase & {
|
|
23
|
-
type: "choice";
|
|
24
|
-
default: string;
|
|
25
|
-
choices: {
|
|
26
|
-
value: string;
|
|
27
|
-
label: string;
|
|
28
|
-
}[];
|
|
29
|
-
});
|
|
30
|
-
export interface PluginManifest {
|
|
31
|
-
id: string;
|
|
32
|
-
name: string;
|
|
33
|
-
version: string;
|
|
34
|
-
description?: string;
|
|
35
|
-
app?: string;
|
|
36
|
-
app_versions?: string;
|
|
37
|
-
requires?: Record<string, string>;
|
|
38
|
-
enabled_by_default?: boolean;
|
|
39
|
-
settings?: SettingField[];
|
|
40
|
-
}
|
|
41
|
-
export interface PluginApi {
|
|
42
|
-
hooks?: Record<string, AnyHandler>;
|
|
43
|
-
providers?: Record<string, unknown>;
|
|
44
|
-
slots?: Record<string, unknown>;
|
|
45
|
-
services?: unknown;
|
|
46
|
-
}
|
|
47
|
-
type AnyHandler = (...args: any[]) => void | Promise<void>;
|
|
48
|
-
type HooksOf<Api extends PluginApi> = Api["hooks"] extends Record<string, unknown> ? NonNullable<Api["hooks"]> : Record<string, never>;
|
|
49
|
-
type ProvidersOf<Api extends PluginApi> = Api["providers"] extends Record<string, unknown> ? Api["providers"] : Record<string, never>;
|
|
50
|
-
type SlotsOf<Api extends PluginApi> = Api["slots"] extends Record<string, unknown> ? Api["slots"] : Record<string, never>;
|
|
51
|
-
export interface PluginCommand {
|
|
52
|
-
id: string;
|
|
53
|
-
label: string;
|
|
54
|
-
keys?: string;
|
|
55
|
-
run: () => void | Promise<void>;
|
|
56
|
-
}
|
|
57
|
-
export interface PluginContext<Api extends PluginApi = PluginApi> {
|
|
58
|
-
manifest: PluginManifest;
|
|
59
|
-
app: AppInfo;
|
|
60
|
-
services: Api["services"];
|
|
61
|
-
hook<Name extends keyof HooksOf<Api> & string>(name: Name, handler: HooksOf<Api>[Name]): void;
|
|
62
|
-
provide<Point extends keyof ProvidersOf<Api> & string>(point: Point, id: string, value: ProvidersOf<Api>[Point]): void;
|
|
63
|
-
add_command(command: PluginCommand): void;
|
|
64
|
-
fill_slot<Slot extends keyof SlotsOf<Api> & string>(slot: Slot, value: SlotsOf<Api>[Slot]): void;
|
|
65
|
-
settings(): SettingValues;
|
|
66
|
-
on_settings_change(listener: (values: SettingValues) => void): void;
|
|
67
|
-
on_stop(cleanup: () => void | Promise<void>): void;
|
|
68
|
-
}
|
|
69
|
-
export interface Plugin<Api extends PluginApi = PluginApi> extends PluginManifest {
|
|
70
|
-
start(context: PluginContext<Api>): void | Promise<void>;
|
|
71
|
-
stop?(): void | Promise<void>;
|
|
72
|
-
}
|
|
73
|
-
export declare function define_plugin<Api extends PluginApi>(plugin: Plugin<Api>): Plugin<Api>;
|
|
74
|
-
export interface AppInfo {
|
|
75
|
-
id: string;
|
|
76
|
-
version: string;
|
|
77
|
-
}
|
|
78
|
-
export type PluginRecords = Record<string, {
|
|
79
|
-
enabled?: boolean;
|
|
80
|
-
settings?: SettingValues;
|
|
81
|
-
}>;
|
|
82
|
-
export interface PluginStorage {
|
|
83
|
-
read(): PluginRecords | undefined | Promise<PluginRecords | undefined>;
|
|
84
|
-
write(records: PluginRecords): void | Promise<void>;
|
|
85
|
-
}
|
|
86
|
-
export type PluginState = "running" | "off" | "waiting" | "failed" | "unsupported";
|
|
87
|
-
export interface PluginStatus {
|
|
88
|
-
manifest: PluginManifest;
|
|
89
|
-
state: PluginState;
|
|
90
|
-
problem?: string;
|
|
91
|
-
}
|
|
92
|
-
export interface Contribution<Value> {
|
|
93
|
-
plugin_id: string;
|
|
94
|
-
value: Value;
|
|
95
|
-
}
|
|
96
|
-
export interface Provided<Value> extends Contribution<Value> {
|
|
97
|
-
id: string;
|
|
98
|
-
}
|
|
99
|
-
export interface HostCommand extends PluginCommand {
|
|
100
|
-
plugin_id: string;
|
|
101
|
-
}
|
|
102
|
-
export interface PluginHostOptions<Api extends PluginApi> {
|
|
103
|
-
app: AppInfo;
|
|
104
|
-
services?: Api["services"];
|
|
105
|
-
storage?: PluginStorage;
|
|
106
|
-
on_error?: (plugin_id: string, error: unknown) => void;
|
|
107
|
-
}
|
|
108
|
-
export interface PluginHost<Api extends PluginApi = PluginApi> {
|
|
109
|
-
add(plugin: Plugin<Api>): Promise<void>;
|
|
110
|
-
start(): Promise<void>;
|
|
111
|
-
stop(): Promise<void>;
|
|
112
|
-
enable(id: string): Promise<void>;
|
|
113
|
-
disable(id: string): Promise<void>;
|
|
114
|
-
list(): PluginStatus[];
|
|
115
|
-
call<Name extends keyof HooksOf<Api> & string>(name: Name, ...args: Parameters<HooksOf<Api>[Name]>): Promise<void>;
|
|
116
|
-
call_parallel<Name extends keyof HooksOf<Api> & string>(name: Name, ...args: Parameters<HooksOf<Api>[Name]>): Promise<void>;
|
|
117
|
-
hook<Name extends keyof HooksOf<Api> & string>(name: Name, handler: HooksOf<Api>[Name]): () => void;
|
|
118
|
-
providers<Point extends keyof ProvidersOf<Api> & string>(point: Point): Provided<ProvidersOf<Api>[Point]>[];
|
|
119
|
-
provider<Point extends keyof ProvidersOf<Api> & string>(point: Point, id?: string): Provided<ProvidersOf<Api>[Point]> | undefined;
|
|
120
|
-
commands(): HostCommand[];
|
|
121
|
-
slot<Slot extends keyof SlotsOf<Api> & string>(slot: Slot): Contribution<SlotsOf<Api>[Slot]>[];
|
|
122
|
-
settings(id: string): SettingValues;
|
|
123
|
-
set_settings(id: string, values: SettingValues): Promise<void>;
|
|
124
|
-
watch: {
|
|
125
|
-
version(): number;
|
|
126
|
-
subscribe(listener: () => void): () => void;
|
|
127
|
-
};
|
|
128
|
-
}
|
|
129
|
-
export declare function create_plugin_host<Api extends PluginApi = PluginApi>(options: PluginHostOptions<Api>): PluginHost<Api>;
|
|
130
|
-
export declare function fits(field: SettingField, value: unknown): value is SettingValue;
|
|
131
|
-
export declare const hidden_secret = "\u2022\u2022\u2022\u2022\u2022\u2022\u2022\u2022";
|
|
132
|
-
export declare function hide_secrets(manifests: PluginManifest[], records: PluginRecords): PluginRecords;
|
|
133
|
-
export declare function keep_secrets(manifests: PluginManifest[], stored: PluginRecords, incoming: PluginRecords): PluginRecords;
|
|
134
|
-
export declare function json_plugin_storage(store: {
|
|
135
|
-
get(): string | null | undefined;
|
|
136
|
-
set(text: string): void | Promise<void>;
|
|
137
|
-
}): PluginStorage;
|
|
138
|
-
export {};
|
|
1
|
+
export * from "@x-tech-laboratories/x-app-agent/plugins";
|
package/dist/plugins.js
CHANGED
|
@@ -1,346 +1,2 @@
|
|
|
1
|
-
//
|
|
2
|
-
|
|
3
|
-
import satisfies from "semver/functions/satisfies.js";
|
|
4
|
-
import valid from "semver/functions/valid.js";
|
|
5
|
-
// Types a plugin against its app's API without restating the generic at each call.
|
|
6
|
-
export function define_plugin(plugin) {
|
|
7
|
-
return plugin;
|
|
8
|
-
}
|
|
9
|
-
export function create_plugin_host(options) {
|
|
10
|
-
const hooks = createHooks();
|
|
11
|
-
const entries = new Map();
|
|
12
|
-
const providers = new Map();
|
|
13
|
-
const slots = new Map();
|
|
14
|
-
const commands = [];
|
|
15
|
-
const report = options.on_error ?? (() => { });
|
|
16
|
-
let records = {};
|
|
17
|
-
let started = false;
|
|
18
|
-
// Serializes every change so two toggles never interleave their starts and stops.
|
|
19
|
-
let queue = Promise.resolve();
|
|
20
|
-
let version = 0;
|
|
21
|
-
const listeners = new Set();
|
|
22
|
-
const bump = () => {
|
|
23
|
-
version += 1;
|
|
24
|
-
for (const listener of listeners)
|
|
25
|
-
listener();
|
|
26
|
-
};
|
|
27
|
-
function in_turn(work) {
|
|
28
|
-
const next = queue.then(work);
|
|
29
|
-
queue = next.catch(() => { });
|
|
30
|
-
return next;
|
|
31
|
-
}
|
|
32
|
-
async function save() {
|
|
33
|
-
await options.storage?.write(records);
|
|
34
|
-
}
|
|
35
|
-
function unsupported_because(plugin) {
|
|
36
|
-
if (!plugin.id || !plugin.name || typeof plugin.start !== "function")
|
|
37
|
-
return "It has no id, name or start.";
|
|
38
|
-
if (!valid(plugin.version))
|
|
39
|
-
return `Its version "${plugin.version}" is not semver.`;
|
|
40
|
-
if (plugin.app && plugin.app !== options.app.id)
|
|
41
|
-
return `It is for ${plugin.app}, not ${options.app.id}.`;
|
|
42
|
-
if (plugin.app_versions && !satisfies(options.app.version, plugin.app_versions, { includePrerelease: true })) {
|
|
43
|
-
return `It needs ${options.app.id} ${plugin.app_versions}; this is ${options.app.version}.`;
|
|
44
|
-
}
|
|
45
|
-
return undefined;
|
|
46
|
-
}
|
|
47
|
-
function missing_requirement(plugin) {
|
|
48
|
-
for (const [id, range] of Object.entries(plugin.requires ?? {})) {
|
|
49
|
-
const required = entries.get(id);
|
|
50
|
-
if (!required)
|
|
51
|
-
return `It needs ${id} ${range}, which is not installed.`;
|
|
52
|
-
if (!satisfies(required.plugin.version, range, { includePrerelease: true }))
|
|
53
|
-
return `It needs ${id} ${range}; ${required.plugin.version} is installed.`;
|
|
54
|
-
if (required.state !== "running")
|
|
55
|
-
return `It needs ${required.plugin.name} running.`;
|
|
56
|
-
}
|
|
57
|
-
return undefined;
|
|
58
|
-
}
|
|
59
|
-
function filled_settings(plugin, stored = {}) {
|
|
60
|
-
const values = {};
|
|
61
|
-
for (const field of plugin.settings ?? []) {
|
|
62
|
-
const value = stored[field.key];
|
|
63
|
-
values[field.key] = fits(field, value) ? value : field.default;
|
|
64
|
-
}
|
|
65
|
-
return values;
|
|
66
|
-
}
|
|
67
|
-
function context_for(entry) {
|
|
68
|
-
const plugin_id = entry.plugin.id;
|
|
69
|
-
const remove_from = (list, item) => {
|
|
70
|
-
const at = list.indexOf(item);
|
|
71
|
-
if (at >= 0)
|
|
72
|
-
list.splice(at, 1);
|
|
73
|
-
};
|
|
74
|
-
return {
|
|
75
|
-
manifest: manifest_of(entry.plugin),
|
|
76
|
-
app: options.app,
|
|
77
|
-
services: options.services,
|
|
78
|
-
hook(name, handler) {
|
|
79
|
-
const guarded = async (...args) => {
|
|
80
|
-
try {
|
|
81
|
-
await handler(...args);
|
|
82
|
-
}
|
|
83
|
-
catch (error) {
|
|
84
|
-
report(plugin_id, error);
|
|
85
|
-
}
|
|
86
|
-
};
|
|
87
|
-
entry.cleanups.push(hooks.hook(name, guarded));
|
|
88
|
-
},
|
|
89
|
-
provide(point, id, value) {
|
|
90
|
-
const list = providers.get(point) ?? [];
|
|
91
|
-
const item = { plugin_id, id, value };
|
|
92
|
-
list.push(item);
|
|
93
|
-
providers.set(point, list);
|
|
94
|
-
entry.cleanups.push(() => remove_from(list, item));
|
|
95
|
-
},
|
|
96
|
-
add_command(command) {
|
|
97
|
-
const item = { ...command, id: `${plugin_id}.${command.id}`, plugin_id };
|
|
98
|
-
commands.push(item);
|
|
99
|
-
entry.cleanups.push(() => remove_from(commands, item));
|
|
100
|
-
},
|
|
101
|
-
fill_slot(slot, value) {
|
|
102
|
-
const list = slots.get(slot) ?? [];
|
|
103
|
-
const item = { plugin_id, value };
|
|
104
|
-
list.push(item);
|
|
105
|
-
slots.set(slot, list);
|
|
106
|
-
entry.cleanups.push(() => remove_from(list, item));
|
|
107
|
-
},
|
|
108
|
-
settings: () => ({ ...entry.settings }),
|
|
109
|
-
on_settings_change(listener) {
|
|
110
|
-
entry.settings_listeners.push(listener);
|
|
111
|
-
},
|
|
112
|
-
on_stop(cleanup) {
|
|
113
|
-
entry.cleanups.push(cleanup);
|
|
114
|
-
},
|
|
115
|
-
};
|
|
116
|
-
}
|
|
117
|
-
async function run_cleanups(entry) {
|
|
118
|
-
for (const cleanup of entry.cleanups.splice(0).reverse()) {
|
|
119
|
-
try {
|
|
120
|
-
await cleanup();
|
|
121
|
-
}
|
|
122
|
-
catch (error) {
|
|
123
|
-
report(entry.plugin.id, error);
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
entry.settings_listeners = [];
|
|
127
|
-
}
|
|
128
|
-
async function start_one(entry) {
|
|
129
|
-
entry.state = "running";
|
|
130
|
-
entry.problem = undefined;
|
|
131
|
-
try {
|
|
132
|
-
await entry.plugin.start(context_for(entry));
|
|
133
|
-
}
|
|
134
|
-
catch (error) {
|
|
135
|
-
await run_cleanups(entry);
|
|
136
|
-
entry.state = "failed";
|
|
137
|
-
entry.problem = error instanceof Error ? error.message : String(error);
|
|
138
|
-
report(entry.plugin.id, error);
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
async function stop_one(entry) {
|
|
142
|
-
if (entry.state !== "running")
|
|
143
|
-
return;
|
|
144
|
-
try {
|
|
145
|
-
await entry.plugin.stop?.();
|
|
146
|
-
}
|
|
147
|
-
catch (error) {
|
|
148
|
-
report(entry.plugin.id, error);
|
|
149
|
-
}
|
|
150
|
-
await run_cleanups(entry);
|
|
151
|
-
entry.state = "off";
|
|
152
|
-
}
|
|
153
|
-
// Starts what can start, again and again until a pass starts nothing, so a plugin may be added before what it requires.
|
|
154
|
-
async function settle() {
|
|
155
|
-
let progressed = started;
|
|
156
|
-
while (progressed) {
|
|
157
|
-
progressed = false;
|
|
158
|
-
for (const entry of entries.values()) {
|
|
159
|
-
if (entry.state === "running" || entry.state === "unsupported")
|
|
160
|
-
continue;
|
|
161
|
-
if (!entry.enabled) {
|
|
162
|
-
entry.state = "off";
|
|
163
|
-
entry.problem = undefined;
|
|
164
|
-
continue;
|
|
165
|
-
}
|
|
166
|
-
if (entry.state === "failed")
|
|
167
|
-
continue;
|
|
168
|
-
const problem = missing_requirement(entry.plugin);
|
|
169
|
-
if (problem) {
|
|
170
|
-
entry.state = "waiting";
|
|
171
|
-
entry.problem = problem;
|
|
172
|
-
continue;
|
|
173
|
-
}
|
|
174
|
-
await start_one(entry);
|
|
175
|
-
progressed = true;
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
bump();
|
|
179
|
-
}
|
|
180
|
-
// Stops the plugin's dependents before the plugin itself.
|
|
181
|
-
async function stop_with_dependents(entry) {
|
|
182
|
-
for (const other of entries.values()) {
|
|
183
|
-
if (other.state === "running" && other.plugin.requires && entry.plugin.id in other.plugin.requires)
|
|
184
|
-
await stop_with_dependents(other);
|
|
185
|
-
}
|
|
186
|
-
await stop_one(entry);
|
|
187
|
-
}
|
|
188
|
-
function entry_of(id) {
|
|
189
|
-
const entry = entries.get(id);
|
|
190
|
-
if (!entry)
|
|
191
|
-
throw new Error(`No plugin "${id}" is installed.`);
|
|
192
|
-
return entry;
|
|
193
|
-
}
|
|
194
|
-
return {
|
|
195
|
-
add: (plugin) => in_turn(async () => {
|
|
196
|
-
if (entries.has(plugin.id))
|
|
197
|
-
throw new Error(`A plugin "${plugin.id}" is already installed.`);
|
|
198
|
-
const problem = unsupported_because(plugin);
|
|
199
|
-
const record = records[plugin.id];
|
|
200
|
-
entries.set(plugin.id, {
|
|
201
|
-
plugin,
|
|
202
|
-
state: problem ? "unsupported" : "off",
|
|
203
|
-
problem,
|
|
204
|
-
enabled: record?.enabled ?? plugin.enabled_by_default ?? true,
|
|
205
|
-
settings: filled_settings(plugin, record?.settings),
|
|
206
|
-
cleanups: [],
|
|
207
|
-
settings_listeners: [],
|
|
208
|
-
});
|
|
209
|
-
await settle();
|
|
210
|
-
}),
|
|
211
|
-
start: () => in_turn(async () => {
|
|
212
|
-
if (started)
|
|
213
|
-
return;
|
|
214
|
-
records = (await options.storage?.read()) ?? {};
|
|
215
|
-
for (const entry of entries.values()) {
|
|
216
|
-
const record = records[entry.plugin.id];
|
|
217
|
-
if (record?.enabled !== undefined)
|
|
218
|
-
entry.enabled = record.enabled;
|
|
219
|
-
entry.settings = filled_settings(entry.plugin, record?.settings);
|
|
220
|
-
}
|
|
221
|
-
started = true;
|
|
222
|
-
await settle();
|
|
223
|
-
}),
|
|
224
|
-
stop: () => in_turn(async () => {
|
|
225
|
-
for (const entry of [...entries.values()].reverse())
|
|
226
|
-
await stop_with_dependents(entry);
|
|
227
|
-
started = false;
|
|
228
|
-
bump();
|
|
229
|
-
}),
|
|
230
|
-
enable: (id) => in_turn(async () => {
|
|
231
|
-
const entry = entry_of(id);
|
|
232
|
-
entry.enabled = true;
|
|
233
|
-
if (entry.state === "failed")
|
|
234
|
-
entry.state = "off";
|
|
235
|
-
records = { ...records, [id]: { ...records[id], enabled: true } };
|
|
236
|
-
await save();
|
|
237
|
-
await settle();
|
|
238
|
-
}),
|
|
239
|
-
disable: (id) => in_turn(async () => {
|
|
240
|
-
const entry = entry_of(id);
|
|
241
|
-
entry.enabled = false;
|
|
242
|
-
records = { ...records, [id]: { ...records[id], enabled: false } };
|
|
243
|
-
await save();
|
|
244
|
-
await stop_with_dependents(entry);
|
|
245
|
-
await settle();
|
|
246
|
-
}),
|
|
247
|
-
list: () => [...entries.values()].map((entry) => ({ manifest: manifest_of(entry.plugin), state: entry.state, problem: entry.problem })),
|
|
248
|
-
call: async (name, ...args) => {
|
|
249
|
-
await hooks.callHook(name, ...args);
|
|
250
|
-
},
|
|
251
|
-
call_parallel: async (name, ...args) => {
|
|
252
|
-
await hooks.callHookParallel(name, ...args);
|
|
253
|
-
},
|
|
254
|
-
hook: (name, handler) => hooks.hook(name, handler),
|
|
255
|
-
providers: (point) => [...(providers.get(point) ?? [])],
|
|
256
|
-
provider(point, id) {
|
|
257
|
-
const list = (providers.get(point) ?? []);
|
|
258
|
-
return list.find((item) => item.id === id) ?? list[0];
|
|
259
|
-
},
|
|
260
|
-
commands: () => [...commands],
|
|
261
|
-
slot: (slot) => [...(slots.get(slot) ?? [])],
|
|
262
|
-
settings: (id) => ({ ...entry_of(id).settings }),
|
|
263
|
-
set_settings: (id, values) => in_turn(async () => {
|
|
264
|
-
const entry = entry_of(id);
|
|
265
|
-
entry.settings = filled_settings(entry.plugin, { ...entry.settings, ...values });
|
|
266
|
-
records = { ...records, [id]: { ...records[id], settings: entry.settings } };
|
|
267
|
-
await save();
|
|
268
|
-
for (const listener of entry.settings_listeners) {
|
|
269
|
-
try {
|
|
270
|
-
listener({ ...entry.settings });
|
|
271
|
-
}
|
|
272
|
-
catch (error) {
|
|
273
|
-
report(id, error);
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
bump();
|
|
277
|
-
}),
|
|
278
|
-
watch: {
|
|
279
|
-
version: () => version,
|
|
280
|
-
subscribe: (listener) => {
|
|
281
|
-
listeners.add(listener);
|
|
282
|
-
return () => void listeners.delete(listener);
|
|
283
|
-
},
|
|
284
|
-
},
|
|
285
|
-
};
|
|
286
|
-
}
|
|
287
|
-
// ── Helpers ──────────────────────────────────────────────────────────────────
|
|
288
|
-
function manifest_of(plugin) {
|
|
289
|
-
const { id, name, version, description, app, app_versions, requires, enabled_by_default, settings } = plugin;
|
|
290
|
-
return { id, name, version, description, app, app_versions, requires, enabled_by_default, settings };
|
|
291
|
-
}
|
|
292
|
-
// Whether a stored value still suits its field, so a changed manifest falls back to the default.
|
|
293
|
-
export function fits(field, value) {
|
|
294
|
-
switch (field.type) {
|
|
295
|
-
case "text":
|
|
296
|
-
return typeof value === "string";
|
|
297
|
-
case "number":
|
|
298
|
-
return typeof value === "number" && Number.isFinite(value) && (field.min === undefined || value >= field.min) && (field.max === undefined || value <= field.max);
|
|
299
|
-
case "switch":
|
|
300
|
-
return typeof value === "boolean";
|
|
301
|
-
case "choice":
|
|
302
|
-
return typeof value === "string" && field.choices.some((choice) => choice.value === value);
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
// Stands in for a secret that is set, in records handed to someone who must not read it.
|
|
306
|
-
export const hidden_secret = "\u2022\u2022\u2022\u2022\u2022\u2022\u2022\u2022";
|
|
307
|
-
function secret_keys(manifests) {
|
|
308
|
-
return new Map(manifests.map((manifest) => [manifest.id, (manifest.settings ?? []).flatMap((field) => (field.type === "text" && field.secret ? [field.key] : []))]));
|
|
309
|
-
}
|
|
310
|
-
// The records with every secret that is set replaced by hidden_secret, for a page or another program to read.
|
|
311
|
-
export function hide_secrets(manifests, records) {
|
|
312
|
-
const secrets = secret_keys(manifests);
|
|
313
|
-
return Object.fromEntries(Object.entries(records).map(([id, record]) => {
|
|
314
|
-
const keys = secrets.get(id) ?? [];
|
|
315
|
-
if (!record.settings || keys.length === 0)
|
|
316
|
-
return [id, record];
|
|
317
|
-
const settings = Object.fromEntries(Object.entries(record.settings).map(([key, value]) => [key, keys.includes(key) && value !== "" ? hidden_secret : value]));
|
|
318
|
-
return [id, { ...record, settings }];
|
|
319
|
-
}));
|
|
320
|
-
}
|
|
321
|
-
// Records written back after hide_secrets, with each secret still hidden_secret given its stored value again.
|
|
322
|
-
export function keep_secrets(manifests, stored, incoming) {
|
|
323
|
-
const secrets = secret_keys(manifests);
|
|
324
|
-
return Object.fromEntries(Object.entries(incoming).map(([id, record]) => {
|
|
325
|
-
const keys = secrets.get(id) ?? [];
|
|
326
|
-
if (!record.settings || keys.length === 0)
|
|
327
|
-
return [id, record];
|
|
328
|
-
const settings = Object.fromEntries(Object.entries(record.settings).map(([key, value]) => [key, keys.includes(key) && value === hidden_secret ? (stored[id]?.settings?.[key] ?? "") : value]));
|
|
329
|
-
return [id, { ...record, settings }];
|
|
330
|
-
}));
|
|
331
|
-
}
|
|
332
|
-
// Storage in a key of any string store, such as web/prefs' browser storage or a file the server reads and writes.
|
|
333
|
-
export function json_plugin_storage(store) {
|
|
334
|
-
return {
|
|
335
|
-
read() {
|
|
336
|
-
try {
|
|
337
|
-
const parsed = JSON.parse(store.get() ?? "{}");
|
|
338
|
-
return parsed && typeof parsed === "object" && !Array.isArray(parsed) ? parsed : {};
|
|
339
|
-
}
|
|
340
|
-
catch {
|
|
341
|
-
return {};
|
|
342
|
-
}
|
|
343
|
-
},
|
|
344
|
-
write: (records) => store.set(JSON.stringify(records)),
|
|
345
|
-
};
|
|
346
|
-
}
|
|
1
|
+
// Moved to @x-tech-laboratories/x-app-agent, which a host agent installs without the rest of X-App.
|
|
2
|
+
export * from "@x-tech-laboratories/x-app-agent/plugins";
|
package/dist/python_text.d.ts
CHANGED
|
@@ -1,19 +1 @@
|
|
|
1
|
-
export
|
|
2
|
-
export declare function format_grouped(value: number, digits: number): string;
|
|
3
|
-
export declare function round_half_even(value: number, digits?: number): number;
|
|
4
|
-
export declare function python_float_repr(value: number): string;
|
|
5
|
-
export declare function split_lines(text: string): string[];
|
|
6
|
-
export declare function split_words(text: string, max_split?: number): string[];
|
|
7
|
-
export declare function partition(text: string, separator: string): [before: string, found: string, after: string];
|
|
8
|
-
export declare function parse_int(text: unknown): number;
|
|
9
|
-
export declare function parse_float(text: unknown): number;
|
|
10
|
-
export declare function int_or_null(text: unknown): number | null;
|
|
11
|
-
export declare function float_or_null(text: unknown): number | null;
|
|
12
|
-
export declare function fnmatch(name: string, pattern: string): boolean;
|
|
13
|
-
export declare function python_regex(pattern: string, flags?: string): RegExp;
|
|
14
|
-
export declare function python_repr(value: unknown): string;
|
|
15
|
-
export declare function python_dumps(value: unknown, sort_keys?: boolean): string;
|
|
16
|
-
export declare function python_str(value: unknown): string;
|
|
17
|
-
export type FieldLookup = (name: string) => unknown;
|
|
18
|
-
export declare function apply_format_spec(value: unknown, spec: string): string;
|
|
19
|
-
export declare function python_format(template: string, positional?: readonly unknown[], keywords?: Record<string, unknown>): string;
|
|
1
|
+
export * from "@x-tech-laboratories/x-app-agent/python_text";
|