@try-works/extension-laya 0.0.0-stage → 1.0.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 +38 -2
- package/lib/index.d.ts +18 -0
- package/lib/index.js +172 -0
- package/lib/index.js.map +1 -0
- package/lib/pure.d.ts +35 -0
- package/lib/pure.js +71 -0
- package/lib/pure.js.map +1 -0
- package/models.lock.json +63 -0
- package/package.json +39 -4
- package/schema/decision-choose.input.json +22 -0
- package/schema/decision-choose.output.json +20 -0
- package/schema/decision-score.input.json +18 -0
- package/schema/decision-score.output.json +18 -0
- package/schema/decision-test.input.json +10 -0
- package/schema/decision-test.output.json +18 -0
- package/tool-mode.extension.json +87 -0
- package/vendor/LICENSE.laya-ts +176 -0
- package/vendor/laya-ts/agent.d.ts +216 -0
- package/vendor/laya-ts/agent.js +631 -0
- package/vendor/laya-ts/common.d.ts +167 -0
- package/vendor/laya-ts/common.js +478 -0
- package/vendor/laya-ts/dx.d.ts +17 -0
- package/vendor/laya-ts/dx.js +46 -0
- package/vendor/laya-ts/email.d.ts +2 -0
- package/vendor/laya-ts/email.js +184 -0
- package/vendor/laya-ts/hooks.d.ts +134 -0
- package/vendor/laya-ts/hooks.js +379 -0
- package/vendor/laya-ts/index.d.ts +22 -0
- package/vendor/laya-ts/index.js +13 -0
- package/vendor/laya-ts/lang.d.ts +24 -0
- package/vendor/laya-ts/lang.js +590 -0
- package/vendor/laya-ts/presets.d.ts +5 -0
- package/vendor/laya-ts/presets.js +175 -0
- package/vendor/laya-ts/providers.d.ts +67 -0
- package/vendor/laya-ts/providers.js +668 -0
- package/vendor/laya-ts/router.d.ts +186 -0
- package/vendor/laya-ts/router.js +757 -0
- package/vendor/laya-ts/shortlist.d.ts +65 -0
- package/vendor/laya-ts/shortlist.js +407 -0
- package/vendor/laya-ts/structured.d.ts +89 -0
- package/vendor/laya-ts/structured.js +256 -0
- package/vendor/laya-ts/tokenizer.d.ts +66 -0
- package/vendor/laya-ts/tokenizer.js +482 -0
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
import { checkMinConfidence, flagLowConfidence } from "./common.js";
|
|
2
|
+
export const MAX_PROPERTIES = 32;
|
|
3
|
+
export const MAX_OPTIONS = 32;
|
|
4
|
+
export const MAX_SCORE_LEVELS = 10;
|
|
5
|
+
/** A schema cannot be expressed as Laya questions; the message names the path. */
|
|
6
|
+
export class SchemaError extends Error {
|
|
7
|
+
constructor(message) {
|
|
8
|
+
super(message);
|
|
9
|
+
this.name = "SchemaError";
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
const r4 = (v) => Math.round(v * 1e4) / 1e4;
|
|
13
|
+
function pyType(v) {
|
|
14
|
+
if (v === null || v === undefined)
|
|
15
|
+
return "NoneType";
|
|
16
|
+
if (Array.isArray(v))
|
|
17
|
+
return "list";
|
|
18
|
+
switch (typeof v) {
|
|
19
|
+
case "boolean": return "bool";
|
|
20
|
+
case "number": return Number.isInteger(v) ? "int" : "float";
|
|
21
|
+
case "string": return "str";
|
|
22
|
+
case "object": return "dict";
|
|
23
|
+
default: return typeof v;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
/** Accept a JSON schema object or a model exposing toJSONSchema()/model_json_schema()/schema(). */
|
|
27
|
+
function schemaOf(model) {
|
|
28
|
+
const m = model;
|
|
29
|
+
for (const key of ["toJSONSchema", "model_json_schema", "schema"]) {
|
|
30
|
+
if (typeof m?.[key] === "function")
|
|
31
|
+
return m[key]();
|
|
32
|
+
}
|
|
33
|
+
if (m !== null && typeof m === "object" && !Array.isArray(m))
|
|
34
|
+
return m;
|
|
35
|
+
throw new SchemaError(`expected a JSON schema object or a model with toJSONSchema(), got ${pyType(model)}`);
|
|
36
|
+
}
|
|
37
|
+
function noulField(name, description) {
|
|
38
|
+
return {
|
|
39
|
+
name,
|
|
40
|
+
kind: "noul",
|
|
41
|
+
question: { type: "noul", instructions: description || `Is \`${name}\` true?` },
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
function enumField(path, name, values, description) {
|
|
45
|
+
if (values.length > MAX_OPTIONS) {
|
|
46
|
+
throw new SchemaError(`${path}: ${values.length} options exceeds MAX_OPTIONS=${MAX_OPTIONS}`);
|
|
47
|
+
}
|
|
48
|
+
if (values.length === 0)
|
|
49
|
+
throw new SchemaError(`${path}: 'enum' must not be empty`);
|
|
50
|
+
if (values.every((v) => typeof v === "boolean"))
|
|
51
|
+
return noulField(name, description);
|
|
52
|
+
const options = values.map((v) => [v == null ? "null" : String(v), v]);
|
|
53
|
+
if (new Set(options.map(([label]) => label)).size !== options.length) {
|
|
54
|
+
throw new SchemaError(`${path}: enum values produce duplicate choice labels`);
|
|
55
|
+
}
|
|
56
|
+
return {
|
|
57
|
+
name,
|
|
58
|
+
kind: "choice",
|
|
59
|
+
question: {
|
|
60
|
+
type: "choice",
|
|
61
|
+
instructions: description || `What is \`${name}\`?`,
|
|
62
|
+
criteria: Object.fromEntries(options.map(([label]) => [label, null])),
|
|
63
|
+
},
|
|
64
|
+
options,
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
function scoreField(path, name, prop, description) {
|
|
68
|
+
const lo = prop.minimum;
|
|
69
|
+
const hi = prop.maximum;
|
|
70
|
+
if (!Number.isInteger(lo) || !Number.isInteger(hi)) {
|
|
71
|
+
throw new SchemaError(`${path}: a numeric field needs integer 'minimum' and 'maximum' to become a score`);
|
|
72
|
+
}
|
|
73
|
+
const loN = lo;
|
|
74
|
+
const hiN = hi;
|
|
75
|
+
if (hiN < loN)
|
|
76
|
+
throw new SchemaError(`${path}: 'maximum' ${hiN} is below 'minimum' ${loN}`);
|
|
77
|
+
const span = hiN - loN + 1;
|
|
78
|
+
if (span > MAX_SCORE_LEVELS) {
|
|
79
|
+
throw new SchemaError(`${path}: ${span} levels exceeds MAX_SCORE_LEVELS=${MAX_SCORE_LEVELS}; narrow the range or use an enum`);
|
|
80
|
+
}
|
|
81
|
+
return {
|
|
82
|
+
name,
|
|
83
|
+
kind: "score",
|
|
84
|
+
question: {
|
|
85
|
+
type: "score",
|
|
86
|
+
instructions: description || `Score \`${name}\` from ${loN} to ${hiN}`,
|
|
87
|
+
criteria: Array.from({ length: span }, (_, i) => String(loN + i)),
|
|
88
|
+
},
|
|
89
|
+
minimum: loN,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
function fieldFor(path, name, prop) {
|
|
93
|
+
if (prop === null || typeof prop !== "object" || Array.isArray(prop)) {
|
|
94
|
+
throw new SchemaError(`${path}: property must be an object, got ${pyType(prop)}`);
|
|
95
|
+
}
|
|
96
|
+
const p = prop;
|
|
97
|
+
const description = p.description;
|
|
98
|
+
if (!("const" in p || "enum" in p || "type" in p)) {
|
|
99
|
+
const union = (p.anyOf ?? p.oneOf);
|
|
100
|
+
if (union !== undefined && Array.isArray(union)) {
|
|
101
|
+
const branches = union.filter((b) => b !== null && typeof b === "object" && !Array.isArray(b) && b.type !== "null");
|
|
102
|
+
if (branches.length !== 1) {
|
|
103
|
+
throw new SchemaError(`${path}: only 'Optional[...]' unions (one non-null branch) are supported, got ${branches.length}`);
|
|
104
|
+
}
|
|
105
|
+
const branch = { ...branches[0] };
|
|
106
|
+
if (branch.description === undefined && description !== undefined) {
|
|
107
|
+
branch.description = description;
|
|
108
|
+
}
|
|
109
|
+
return fieldFor(path, name, branch);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
if ("const" in p)
|
|
113
|
+
return enumField(path, name, [p.const], description);
|
|
114
|
+
if ("enum" in p) {
|
|
115
|
+
if (!Array.isArray(p.enum))
|
|
116
|
+
throw new SchemaError(`${path}: 'enum' must be a list, got ${pyType(p.enum)}`);
|
|
117
|
+
return enumField(path, name, p.enum, description);
|
|
118
|
+
}
|
|
119
|
+
let jtype = p.type;
|
|
120
|
+
if (Array.isArray(jtype)) {
|
|
121
|
+
const nonNullTypes = jtype.filter((t) => t !== "null"); // nullable: ["string", "null"]
|
|
122
|
+
if (nonNullTypes.length > 1) {
|
|
123
|
+
throw new SchemaError(`${path}: 'type' has multiple non-null types; unions are not supported`);
|
|
124
|
+
}
|
|
125
|
+
jtype = nonNullTypes[0];
|
|
126
|
+
}
|
|
127
|
+
if (jtype === "boolean")
|
|
128
|
+
return noulField(name, description);
|
|
129
|
+
if (jtype === "string") {
|
|
130
|
+
throw new SchemaError(`${path}: a free string cannot be a fixed option set; use 'enum' or a boolean`);
|
|
131
|
+
}
|
|
132
|
+
if (jtype === "integer" || jtype === "number")
|
|
133
|
+
return scoreField(path, name, p, description);
|
|
134
|
+
if (jtype === "array")
|
|
135
|
+
throw new SchemaError(`${path}: arrays are not supported; ask one field per element`);
|
|
136
|
+
if (jtype === "object")
|
|
137
|
+
throw new SchemaError(`${path}: nested objects are not supported; flatten the schema`);
|
|
138
|
+
if ("$ref" in p)
|
|
139
|
+
throw new SchemaError(`${path}: $ref/recursion is not supported; flatten the schema`);
|
|
140
|
+
throw new SchemaError(`${path}: unsupported schema ${JSON.stringify(prop)}`);
|
|
141
|
+
}
|
|
142
|
+
/** Validate a JSON schema and return one planned field per property. */
|
|
143
|
+
export function planFromJsonSchema(schema) {
|
|
144
|
+
if (schema === null || typeof schema !== "object" || Array.isArray(schema)) {
|
|
145
|
+
throw new SchemaError(`expected a JSON schema object, got ${pyType(schema)}`);
|
|
146
|
+
}
|
|
147
|
+
const s = schema;
|
|
148
|
+
if (!(s.type === undefined || s.type === null || s.type === "object") || !("properties" in s)) {
|
|
149
|
+
throw new SchemaError("the top level must be an object with 'properties'");
|
|
150
|
+
}
|
|
151
|
+
const properties = s.properties;
|
|
152
|
+
if (properties === null || typeof properties !== "object" || Array.isArray(properties)
|
|
153
|
+
|| Object.keys(properties).length === 0) {
|
|
154
|
+
throw new SchemaError("'properties' must be a non-empty object");
|
|
155
|
+
}
|
|
156
|
+
const entries = Object.entries(properties);
|
|
157
|
+
if (entries.length > MAX_PROPERTIES) {
|
|
158
|
+
throw new SchemaError(`${entries.length} properties exceeds MAX_PROPERTIES=${MAX_PROPERTIES}`);
|
|
159
|
+
}
|
|
160
|
+
return entries.map(([name, prop]) => fieldFor(`properties.${name}`, name, prop));
|
|
161
|
+
}
|
|
162
|
+
/** Turn a JSON schema into Laya questions (a documented subset; see the module docstring). */
|
|
163
|
+
export function questionsFromJsonSchema(schema) {
|
|
164
|
+
return Object.fromEntries(planFromJsonSchema(schema).map((f) => [f.name, f.question]));
|
|
165
|
+
}
|
|
166
|
+
function project(answers, fields) {
|
|
167
|
+
const values = {};
|
|
168
|
+
for (const f of fields) {
|
|
169
|
+
const answer = answers[f.name];
|
|
170
|
+
if (answer == null)
|
|
171
|
+
continue;
|
|
172
|
+
if (answer.low_confidence) {
|
|
173
|
+
values[f.name] = null;
|
|
174
|
+
continue;
|
|
175
|
+
}
|
|
176
|
+
if (f.kind === "noul") {
|
|
177
|
+
values[f.name] = Number(answer.noul ?? 0.0) >= 0.5;
|
|
178
|
+
}
|
|
179
|
+
else if (f.kind === "score") {
|
|
180
|
+
const probs = answer.probabilities ?? {};
|
|
181
|
+
const n = Object.keys(probs).length;
|
|
182
|
+
let idx;
|
|
183
|
+
if (n > 0) {
|
|
184
|
+
idx = 0;
|
|
185
|
+
let best = -Infinity;
|
|
186
|
+
for (let i = 0; i < n; i++) {
|
|
187
|
+
const v = Number(probs[String(i)] ?? 0.0);
|
|
188
|
+
if (v > best) {
|
|
189
|
+
best = v;
|
|
190
|
+
idx = i;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
else {
|
|
195
|
+
// `score` is always the probability-weighted 0-based level index,
|
|
196
|
+
// the same space `idx` is in above -- not an absolute field value --
|
|
197
|
+
// so it is rounded on its own, with no `minimum` subtracted first.
|
|
198
|
+
idx = Math.round(Number(answer.score ?? 0.0));
|
|
199
|
+
}
|
|
200
|
+
values[f.name] = (f.minimum ?? 0) + idx;
|
|
201
|
+
}
|
|
202
|
+
else {
|
|
203
|
+
const label = String(answer.choice);
|
|
204
|
+
const hit = (f.options ?? []).find(([l]) => l === label);
|
|
205
|
+
values[f.name] = hit ? hit[1] : label;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
return values;
|
|
209
|
+
}
|
|
210
|
+
/** Project Laya answers onto the schema values (choice value, integer level, boolean). */
|
|
211
|
+
export function answersToJson(answers, schema) {
|
|
212
|
+
return project(answers, planFromJsonSchema(schema));
|
|
213
|
+
}
|
|
214
|
+
function detailsOf(values, answers, result) {
|
|
215
|
+
const confidence = {};
|
|
216
|
+
const probabilities = {};
|
|
217
|
+
for (const [name, answer] of Object.entries(answers)) {
|
|
218
|
+
if (answer == null)
|
|
219
|
+
continue;
|
|
220
|
+
confidence[name] = Number(answer.confidence ?? 0.0);
|
|
221
|
+
if (answer.type === "noul") {
|
|
222
|
+
const p = Number(answer.noul ?? 0.0);
|
|
223
|
+
probabilities[name] = { false: r4(1.0 - p), true: r4(p) };
|
|
224
|
+
}
|
|
225
|
+
else {
|
|
226
|
+
probabilities[name] = { ...(answer.probabilities ?? {}) };
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
return { values, confidence, probabilities, answers: { ...answers }, usage: result.usage, routing: result.routing };
|
|
230
|
+
}
|
|
231
|
+
export async function decide(runner, state, schema, opts = {}) {
|
|
232
|
+
const { questions, returnDetails = false, minConfidence, min_confidence, ...predictOpts } = opts;
|
|
233
|
+
if ((schema == null) === (questions == null)) {
|
|
234
|
+
throw new Error("pass exactly one of schema= or questions=");
|
|
235
|
+
}
|
|
236
|
+
const mcOpt = minConfidence ?? min_confidence;
|
|
237
|
+
const mc = mcOpt !== undefined && mcOpt !== null ? checkMinConfidence(mcOpt) : null;
|
|
238
|
+
if (mc !== null) {
|
|
239
|
+
predictOpts.minConfidence = mc;
|
|
240
|
+
}
|
|
241
|
+
let fields = null;
|
|
242
|
+
let qs = questions;
|
|
243
|
+
if (schema != null) {
|
|
244
|
+
fields = planFromJsonSchema(schemaOf(schema));
|
|
245
|
+
qs = Object.fromEntries(fields.map((f) => [f.name, f.question]));
|
|
246
|
+
}
|
|
247
|
+
const result = await runner.predict(state, qs, predictOpts);
|
|
248
|
+
if (mc !== null && result && typeof result === "object") {
|
|
249
|
+
flagLowConfidence(result, mc);
|
|
250
|
+
}
|
|
251
|
+
const answers = result.answers ?? {};
|
|
252
|
+
const values = fields ? project(answers, fields) : { ...answers };
|
|
253
|
+
if (returnDetails)
|
|
254
|
+
return detailsOf(values, answers, result);
|
|
255
|
+
return values;
|
|
256
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
export interface TokenizerLike {
|
|
2
|
+
readonly clsId: number;
|
|
3
|
+
readonly sepId: number;
|
|
4
|
+
readonly maskId: number;
|
|
5
|
+
readonly padId: number;
|
|
6
|
+
readonly maskToken: string;
|
|
7
|
+
encode(text: string): number[];
|
|
8
|
+
}
|
|
9
|
+
/** Special ids of the laya ModernBERT checkpoint (HF added_tokens). */
|
|
10
|
+
export declare const CHECKPOINT_IDS: {
|
|
11
|
+
readonly cls: 50281;
|
|
12
|
+
readonly sep: 50282;
|
|
13
|
+
readonly mask: 50284;
|
|
14
|
+
readonly pad: 50283;
|
|
15
|
+
readonly unk: 50280;
|
|
16
|
+
};
|
|
17
|
+
/** Alias lookup order per special: ModernBERT `[X]` names first, Gemma `<x>` names after. */
|
|
18
|
+
export declare const SPECIAL_ALIASES: {
|
|
19
|
+
readonly cls: readonly ["[CLS]", "<bos>", "<s>"];
|
|
20
|
+
readonly sep: readonly ["[SEP]", "<eos>", "</s>"];
|
|
21
|
+
readonly pad: readonly ["[PAD]", "<pad>"];
|
|
22
|
+
readonly mask: readonly ["[MASK]", "<mask>"];
|
|
23
|
+
readonly unk: readonly ["[UNK]", "<unk>"];
|
|
24
|
+
};
|
|
25
|
+
export interface TokenizerIds {
|
|
26
|
+
cls: number;
|
|
27
|
+
sep: number;
|
|
28
|
+
mask: number;
|
|
29
|
+
pad: number;
|
|
30
|
+
unk: number;
|
|
31
|
+
}
|
|
32
|
+
export type PreTokenizerKind = "metaspace" | "bytelevel";
|
|
33
|
+
export interface TokenizerData {
|
|
34
|
+
vocab: Map<string, number>;
|
|
35
|
+
merges: Map<string, number>;
|
|
36
|
+
ids: TokenizerIds;
|
|
37
|
+
kind: PreTokenizerKind;
|
|
38
|
+
maskToken: string;
|
|
39
|
+
/** Normalizer Replace rules (pattern -> content) applied in order before pre-tokenizing. */
|
|
40
|
+
replaces: Array<[string, string]>;
|
|
41
|
+
/** A character the vocab lacks becomes its `<0xNN>` byte tokens instead of unk (HF byte_fallback). */
|
|
42
|
+
byteFallback?: boolean;
|
|
43
|
+
/** Tokens cut out of the text before it is tokenized (HF added_tokens). */
|
|
44
|
+
added?: AddedToken[];
|
|
45
|
+
}
|
|
46
|
+
export interface AddedToken {
|
|
47
|
+
content: string;
|
|
48
|
+
id: number;
|
|
49
|
+
normalized: boolean;
|
|
50
|
+
lstrip: boolean;
|
|
51
|
+
rstrip: boolean;
|
|
52
|
+
}
|
|
53
|
+
/** Metaspace word-boundary marker (HF SentencePiece-style replacement for ' '). */
|
|
54
|
+
export declare const METASPACE_REPLACEMENT = "\u2581";
|
|
55
|
+
export declare function bpeEncode(vocab: Map<string, number>, merges: Map<string, number>, text: string): number[];
|
|
56
|
+
/** Metaspace (SentencePiece-style) BPE encode: unicode chars, NO byte map, NO lowercasing.
|
|
57
|
+
* Normalizer replaces run first, then one marker is ensured at text start, then the text
|
|
58
|
+
* is cut into words at each marker (marker kept as word prefix) with maximal `\n` runs
|
|
59
|
+
* as their own pieces. Each piece is BPE-merged; leftover unknown pieces map to unk. */
|
|
60
|
+
export declare function metaspaceEncode(vocab: Map<string, number>, merges: Map<string, number>, text: string, unkId?: number, replaces?: ReadonlyArray<readonly [string, string]>, byteFallback?: boolean): number[];
|
|
61
|
+
/** Dispatch to the Metaspace or GPT-2/ByteLevel encoder, after cutting out added tokens as HF does. */
|
|
62
|
+
export declare function encodeWithData(data: TokenizerData, text: string): number[];
|
|
63
|
+
/** Parse an HF tokenizer.json ({model vocab/merges, normalizer, pre_tokenizer, added_tokens}). */
|
|
64
|
+
export declare function parseTokenizerJson(raw: unknown): TokenizerData | null;
|
|
65
|
+
/** Load an HF tokenizer.json from a local path (node) or URL into vocab/merges/ids. */
|
|
66
|
+
export declare function loadTokenizerJson(pathOrUrl: string): Promise<TokenizerData | null>;
|