@vite-hub/presets 0.0.0-stage → 0.1.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 +201 -0
- package/README.md +296 -2
- package/dist/admission-DBJdt-CX.d.ts +639 -0
- package/dist/babysitter/server.d.ts +46 -0
- package/dist/babysitter/server.js +2 -0
- package/dist/babysitter-B4D1zeYf.js +715 -0
- package/dist/babysitter.d.ts +246 -0
- package/dist/babysitter.js +2 -0
- package/dist/host-CH0L3VC9.js +438 -0
- package/dist/labeller.d.ts +53 -0
- package/dist/labeller.js +208 -0
- package/dist/server-DDmyZ98B.js +6745 -0
- package/dist/workspace.d.ts +6 -0
- package/dist/workspace.js +16 -0
- package/package.json +94 -3
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { AgentDefinition, AgentInvocationContextValues, AgentInvokerProfile, AgentRuntimeConfig, ConfiguredAgentDefinition } from "@vite-hub/agent";
|
|
2
|
+
import { GmailClient, GmailLabelSettings } from "@vite-hub/agent/channels";
|
|
3
|
+
//#region src/labeller.d.ts
|
|
4
|
+
export interface LabellerLabel extends GmailLabelSettings {}
|
|
5
|
+
export type LabellerPattern = string | readonly string[];
|
|
6
|
+
export interface LabellerRule {
|
|
7
|
+
from?: LabellerPattern;
|
|
8
|
+
to?: LabellerPattern;
|
|
9
|
+
subject?: LabellerPattern;
|
|
10
|
+
body?: LabellerPattern;
|
|
11
|
+
listId?: LabellerPattern;
|
|
12
|
+
hasLabel?: LabellerPattern;
|
|
13
|
+
header?: Readonly<Record<string, LabellerPattern>>;
|
|
14
|
+
not?: LabellerRule;
|
|
15
|
+
label?: string;
|
|
16
|
+
archive?: boolean;
|
|
17
|
+
read?: boolean;
|
|
18
|
+
star?: boolean;
|
|
19
|
+
trash?: boolean;
|
|
20
|
+
}
|
|
21
|
+
export interface LabellerAction {
|
|
22
|
+
archive?: boolean;
|
|
23
|
+
read?: boolean;
|
|
24
|
+
star?: boolean;
|
|
25
|
+
trash?: boolean;
|
|
26
|
+
}
|
|
27
|
+
export interface LabellerOptions {
|
|
28
|
+
labels: Readonly<Record<string, LabellerLabel>>;
|
|
29
|
+
rules: Readonly<Record<string, LabellerRule>>;
|
|
30
|
+
actions: Readonly<Record<string, LabellerAction>>;
|
|
31
|
+
bodyLimit: number;
|
|
32
|
+
dryRun: boolean;
|
|
33
|
+
minConfidence: number;
|
|
34
|
+
trashEnabled: boolean;
|
|
35
|
+
client?: GmailClient;
|
|
36
|
+
}
|
|
37
|
+
export interface LabellerDecision {
|
|
38
|
+
label: string;
|
|
39
|
+
rule?: string;
|
|
40
|
+
}
|
|
41
|
+
type LabellerResult = {
|
|
42
|
+
label: string | {
|
|
43
|
+
choice?: unknown;
|
|
44
|
+
confidence?: unknown;
|
|
45
|
+
probabilities?: Record<string, unknown>;
|
|
46
|
+
};
|
|
47
|
+
rule?: string;
|
|
48
|
+
};
|
|
49
|
+
type LabellerDefinition = AgentDefinition<AgentRuntimeConfig, unknown, AgentInvokerProfile, AgentInvocationContextValues, LabellerResult | LabellerDecision>;
|
|
50
|
+
export type LabellerAgent = ConfiguredAgentDefinition<LabellerOptions, LabellerDefinition>;
|
|
51
|
+
/** Gmail labelling preset: ordered rules first, then a constrained Jev choice. */
|
|
52
|
+
export declare const labeller: LabellerAgent;
|
|
53
|
+
//#endregion
|
package/dist/labeller.js
ADDED
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
import { defineAgent } from "@vite-hub/agent";
|
|
2
|
+
import { hasRuntimeType, isRuntimeObject } from "@vite-hub/runtime/internal/runtime-type";
|
|
3
|
+
import { channelMessageContextKey, gmail } from "@vite-hub/agent/channels";
|
|
4
|
+
import { ask } from "@vite-hub/gateways";
|
|
5
|
+
//#region src/labeller.ts
|
|
6
|
+
const maxBodyLimit = 1e5;
|
|
7
|
+
const maxPatternLength = 256;
|
|
8
|
+
const maxHeaderValueLength = 1e3;
|
|
9
|
+
const reservedLabels = /* @__PURE__ */ new Set([
|
|
10
|
+
"NONE",
|
|
11
|
+
"INBOX",
|
|
12
|
+
"TRASH",
|
|
13
|
+
"SPAM",
|
|
14
|
+
"UNREAD",
|
|
15
|
+
"STARRED",
|
|
16
|
+
"IMPORTANT",
|
|
17
|
+
"SENT",
|
|
18
|
+
"DRAFT",
|
|
19
|
+
"ALL",
|
|
20
|
+
"CATEGORY_PERSONAL",
|
|
21
|
+
"CATEGORY_SOCIAL",
|
|
22
|
+
"CATEGORY_PROMOTIONS",
|
|
23
|
+
"CATEGORY_UPDATES",
|
|
24
|
+
"CATEGORY_FORUMS"
|
|
25
|
+
]);
|
|
26
|
+
function validateLabels(labels) {
|
|
27
|
+
if (!isRuntimeObject(labels) || Array.isArray(labels)) throw new TypeError("[vitehub] Labeller labels must be an object.");
|
|
28
|
+
for (const [name, value] of Object.entries(labels)) {
|
|
29
|
+
if (!name.trim() || name.length > 225 || /\p{Cc}/u.test(name) || reservedLabels.has(name.toUpperCase())) throw new TypeError(`[vitehub] Labeller label "${name}" is reserved or invalid.`);
|
|
30
|
+
if (!isRuntimeObject(value) || Array.isArray(value)) throw new TypeError(`[vitehub] Labeller label "${name}" must be an object.`);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
function patternList(value) {
|
|
34
|
+
if (value === void 0) return;
|
|
35
|
+
const values = Array.isArray(value) ? value : [value];
|
|
36
|
+
if (!values.length || values.some((item) => !hasRuntimeType(item, "string") || item.length === 0 || item.length > maxPatternLength)) throw new TypeError(`[vitehub] Labeller patterns must be non-empty strings of at most ${maxPatternLength} characters.`);
|
|
37
|
+
return values;
|
|
38
|
+
}
|
|
39
|
+
function compilePattern(value) {
|
|
40
|
+
const regex = /^\/(.*)\/([a-z]*)$/.exec(value);
|
|
41
|
+
if (!regex) return;
|
|
42
|
+
if (regex[1].length > maxPatternLength || /[^imsu]/.test(regex[2])) throw new TypeError("[vitehub] Labeller regex patterns use only bounded syntax and the i, m, s, and u flags.");
|
|
43
|
+
const source = regex[1];
|
|
44
|
+
for (let index = 0; index < source.length; index++) {
|
|
45
|
+
const character = source[index];
|
|
46
|
+
if (character === "\\") {
|
|
47
|
+
const escaped = source[++index];
|
|
48
|
+
if (!escaped || !"\\^$.*+?()[]{}|/".includes(escaped)) throw new TypeError("[vitehub] Labeller regex patterns may only escape literal regex characters.");
|
|
49
|
+
} else if ("()[]{}*+?".includes(character)) throw new TypeError("[vitehub] Labeller regex patterns may not use repetition or grouping.");
|
|
50
|
+
}
|
|
51
|
+
try {
|
|
52
|
+
return new RegExp(source, regex[2]);
|
|
53
|
+
} catch {
|
|
54
|
+
throw new TypeError("[vitehub] Labeller regex pattern is invalid.");
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
function matchesText(value, pattern) {
|
|
58
|
+
const patterns = patternList(pattern);
|
|
59
|
+
if (!patterns) return true;
|
|
60
|
+
return (Array.isArray(value) ? value : [value]).some((item) => patterns.some((candidate) => {
|
|
61
|
+
try {
|
|
62
|
+
const regex = compilePattern(candidate);
|
|
63
|
+
return regex ? regex.test(item) : item.toLowerCase().includes(candidate.toLowerCase());
|
|
64
|
+
} catch {
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
}));
|
|
68
|
+
}
|
|
69
|
+
function matches(message, rule) {
|
|
70
|
+
return matchesText(message.from, rule.from) && matchesText(message.to, rule.to) && matchesText(message.subject, rule.subject) && matchesText(message.body || message.snippet, rule.body) && matchesText(message.headers["list-id"] || "", rule.listId) && matchesText(message.labelIds, rule.hasLabel) && Object.entries(rule.header || {}).every(([name, value]) => matchesText(message.headers[name.toLowerCase()] || "", value)) && (!rule.not || !matches(message, rule.not));
|
|
71
|
+
}
|
|
72
|
+
function explicitAction(rule) {
|
|
73
|
+
if (!rule) return {};
|
|
74
|
+
const action = {};
|
|
75
|
+
for (const key of [
|
|
76
|
+
"archive",
|
|
77
|
+
"read",
|
|
78
|
+
"star",
|
|
79
|
+
"trash"
|
|
80
|
+
]) if (rule[key] !== void 0) action[key] = rule[key];
|
|
81
|
+
return action;
|
|
82
|
+
}
|
|
83
|
+
function selectedLabel(result) {
|
|
84
|
+
const label = result?.label;
|
|
85
|
+
if (hasRuntimeType(label, "string")) return label;
|
|
86
|
+
const choice = label?.choice;
|
|
87
|
+
return hasRuntimeType(choice, "string") ? choice : void 0;
|
|
88
|
+
}
|
|
89
|
+
function selectedConfidence(result) {
|
|
90
|
+
if (hasRuntimeType(result.label, "string")) return 1;
|
|
91
|
+
const confidence = result.label.confidence;
|
|
92
|
+
return hasRuntimeType(confidence, "number") && Number.isFinite(confidence) && confidence >= 0 && confidence <= 1 ? confidence : void 0;
|
|
93
|
+
}
|
|
94
|
+
function validateAction(name, action) {
|
|
95
|
+
for (const key of [
|
|
96
|
+
"archive",
|
|
97
|
+
"read",
|
|
98
|
+
"star",
|
|
99
|
+
"trash"
|
|
100
|
+
]) if (action[key] !== void 0 && !hasRuntimeType(action[key], "boolean")) throw new TypeError(`[vitehub] Labeller ${name} ${key} must be a boolean.`);
|
|
101
|
+
}
|
|
102
|
+
function validateRule(name, rule, labels, depth = 0) {
|
|
103
|
+
if (depth > 8) throw new TypeError("[vitehub] Labeller rules may nest at most eight not clauses.");
|
|
104
|
+
if (!isRuntimeObject(rule)) throw new TypeError(`[vitehub] Labeller rule "${name}" must be an object.`);
|
|
105
|
+
validateAction(`rule "${name}"`, rule);
|
|
106
|
+
for (const field of [
|
|
107
|
+
rule.from,
|
|
108
|
+
rule.to,
|
|
109
|
+
rule.subject,
|
|
110
|
+
rule.body,
|
|
111
|
+
rule.listId,
|
|
112
|
+
rule.hasLabel
|
|
113
|
+
]) for (const pattern of patternList(field) || []) compilePattern(pattern);
|
|
114
|
+
for (const [header, value] of Object.entries(rule.header || {})) {
|
|
115
|
+
if (!header.trim() || header.length > maxHeaderValueLength) throw new TypeError(`[vitehub] Labeller rule "${name}" has an invalid header name.`);
|
|
116
|
+
for (const pattern of patternList(value) || []) compilePattern(pattern);
|
|
117
|
+
}
|
|
118
|
+
if (rule.label !== void 0 && (!Object.hasOwn(labels, rule.label) || !rule.label.trim())) throw new TypeError(`[vitehub] Labeller rule "${name}" refers to an undeclared label.`);
|
|
119
|
+
if (rule.label === void 0 && (rule.archive === true || rule.read === true || rule.star === true || rule.trash === true)) throw new TypeError(`[vitehub] Labeller rule "${name}" must declare a label before enabling actions.`);
|
|
120
|
+
if (rule.not) validateRule(`${name}.not`, rule.not, labels, depth + 1);
|
|
121
|
+
}
|
|
122
|
+
function validateOptions(options) {
|
|
123
|
+
for (const key of ["dryRun", "trashEnabled"]) if (!hasRuntimeType(options[key], "boolean")) throw new TypeError(`[vitehub] Labeller ${key} must be a boolean.`);
|
|
124
|
+
validateLabels(options.labels);
|
|
125
|
+
const labelCount = Object.keys(options.labels).length;
|
|
126
|
+
if (labelCount < 1 || labelCount > 254) throw new TypeError("[vitehub] Labeller labels must contain between 1 and 254 labels.");
|
|
127
|
+
if (!Number.isSafeInteger(options.bodyLimit) || options.bodyLimit < 1 || options.bodyLimit > maxBodyLimit) throw new TypeError(`[vitehub] Labeller bodyLimit must be an integer from 1 to ${maxBodyLimit}.`);
|
|
128
|
+
if (!Number.isFinite(options.minConfidence) || options.minConfidence < 0 || options.minConfidence > 1) throw new TypeError("[vitehub] Labeller minConfidence must be between 0 and 1.");
|
|
129
|
+
if (!isRuntimeObject(options.rules) || Array.isArray(options.rules)) throw new TypeError("[vitehub] Labeller rules must be an object.");
|
|
130
|
+
for (const [name, rule] of Object.entries(options.rules || {})) validateRule(name, rule, options.labels);
|
|
131
|
+
if (!isRuntimeObject(options.actions) || Array.isArray(options.actions)) throw new TypeError("[vitehub] Labeller actions must be an object.");
|
|
132
|
+
for (const [label, action] of Object.entries(options.actions || {})) {
|
|
133
|
+
if (!Object.hasOwn(options.labels, label)) throw new TypeError(`[vitehub] Labeller action "${label}" refers to an undeclared label.`);
|
|
134
|
+
if (!isRuntimeObject(action) || Array.isArray(action)) throw new TypeError(`[vitehub] Labeller action "${label}" must be an object.`);
|
|
135
|
+
validateAction(`action "${label}"`, action);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
function decision(message, rules) {
|
|
139
|
+
for (const [name, rule] of Object.entries(rules || {})) if (matches(message, rule)) return {
|
|
140
|
+
label: rule.label || "none",
|
|
141
|
+
rule: name
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
let materializingTemplate = true;
|
|
145
|
+
/** Gmail labelling preset: ordered rules first, then a constrained Jev choice. */
|
|
146
|
+
const labeller = defineAgent({
|
|
147
|
+
options: {
|
|
148
|
+
labels: {},
|
|
149
|
+
rules: {},
|
|
150
|
+
actions: {},
|
|
151
|
+
bodyLimit: 1e4,
|
|
152
|
+
dryRun: true,
|
|
153
|
+
minConfidence: .6,
|
|
154
|
+
trashEnabled: false,
|
|
155
|
+
client: void 0
|
|
156
|
+
},
|
|
157
|
+
configure: (options) => {
|
|
158
|
+
if (materializingTemplate) {
|
|
159
|
+
materializingTemplate = false;
|
|
160
|
+
return defineAgent({
|
|
161
|
+
driver: { run: () => {
|
|
162
|
+
throw new TypeError("[vitehub] Labeller labels must contain between 1 and 254 labels.");
|
|
163
|
+
} },
|
|
164
|
+
intercept: () => void 0
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
validateOptions(options);
|
|
168
|
+
const { labels, rules, actions, bodyLimit, minConfidence, trashEnabled, client } = options;
|
|
169
|
+
const channelOptions = {
|
|
170
|
+
labels,
|
|
171
|
+
bodyLimit,
|
|
172
|
+
client
|
|
173
|
+
};
|
|
174
|
+
if (options.dryRun) channelOptions.dryRun = true;
|
|
175
|
+
const channel = gmail(channelOptions);
|
|
176
|
+
return defineAgent({
|
|
177
|
+
description: "Label Gmail messages with ordered rules, then a constrained Jev choice.",
|
|
178
|
+
channels: { gmail: channel },
|
|
179
|
+
intercept: ({ context }) => {
|
|
180
|
+
const message = context.get(channelMessageContextKey);
|
|
181
|
+
const selected = message ? decision(message, rules) : void 0;
|
|
182
|
+
return selected ? selected : void 0;
|
|
183
|
+
},
|
|
184
|
+
driver: { ask: { label: ask.choice("Choose the Gmail label that best fits this email. Choose none when no declared label clearly fits.", {
|
|
185
|
+
...Object.fromEntries(Object.entries(labels).map(([name, value]) => [name, value.description || name])),
|
|
186
|
+
none: "No declared label clearly fits this email."
|
|
187
|
+
}) } },
|
|
188
|
+
hooks: { async "agent:finish"({ message, result }) {
|
|
189
|
+
if (message?.channel !== "gmail" || !result) return;
|
|
190
|
+
const output = result;
|
|
191
|
+
const label = selectedLabel(output);
|
|
192
|
+
if (!label || label === "none" || !Object.hasOwn(labels, label)) return;
|
|
193
|
+
const confidence = selectedConfidence(output);
|
|
194
|
+
if (confidence === void 0 || confidence < minConfidence) return;
|
|
195
|
+
const rule = output.rule ? rules[output.rule] : void 0;
|
|
196
|
+
const selectedAction = { ...actions[label] };
|
|
197
|
+
if (rule?.label === label) Object.assign(selectedAction, explicitAction(rule));
|
|
198
|
+
await message.label(label);
|
|
199
|
+
if (selectedAction.archive) await message.archive();
|
|
200
|
+
if (selectedAction.read) await message.markRead();
|
|
201
|
+
if (selectedAction.star) await message.star();
|
|
202
|
+
if (selectedAction.trash && trashEnabled) await message.trash();
|
|
203
|
+
} }
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
});
|
|
207
|
+
//#endregion
|
|
208
|
+
export { labeller };
|