@molecule/api-ai-decisions-laya 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/LICENSE +115 -0
- package/README.md +329 -0
- package/dist/browser-guard.d.ts +2 -0
- package/dist/browser-guard.d.ts.map +1 -0
- package/dist/browser-guard.js +19 -0
- package/dist/browser-guard.js.map +1 -0
- package/dist/index.d.ts +68 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +68 -0
- package/dist/index.js.map +1 -0
- package/dist/provider.d.ts +26 -0
- package/dist/provider.d.ts.map +1 -0
- package/dist/provider.js +86 -0
- package/dist/provider.js.map +1 -0
- package/dist/secrets.d.ts +11 -0
- package/dist/secrets.d.ts.map +1 -0
- package/dist/secrets.js +27 -0
- package/dist/secrets.js.map +1 -0
- package/dist/types.d.ts +21 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +7 -0
- package/dist/types.js.map +1 -0
- package/dist/wire.d.ts +89 -0
- package/dist/wire.d.ts.map +1 -0
- package/dist/wire.js +188 -0
- package/dist/wire.js.map +1 -0
- package/package.json +54 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work.
|
|
38
|
+
|
|
39
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
40
|
+
form, that is based on (or derived from) the Work and for which the
|
|
41
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
42
|
+
represent, as a whole, an original work of authorship.
|
|
43
|
+
|
|
44
|
+
"Contribution" shall mean any work of authorship, including the
|
|
45
|
+
original version of the Work and any modifications or additions
|
|
46
|
+
to that Work, that is intentionally submitted to the Licensor for
|
|
47
|
+
inclusion in the Work by the copyright owner or by an individual or
|
|
48
|
+
Legal Entity authorized to submit on behalf of the copyright owner.
|
|
49
|
+
|
|
50
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
51
|
+
on behalf of whom a Contribution has been received by the Licensor and
|
|
52
|
+
subsequently incorporated within the Work.
|
|
53
|
+
|
|
54
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
55
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
56
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
57
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
58
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
59
|
+
Work and such Derivative Works in Source or Object form.
|
|
60
|
+
|
|
61
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
62
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
63
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
64
|
+
patent license to make, have made, use, offer to sell, sell, import,
|
|
65
|
+
and otherwise transfer the Work.
|
|
66
|
+
|
|
67
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
68
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
69
|
+
modifications, and in Source or Object form, provided that You
|
|
70
|
+
meet the following conditions:
|
|
71
|
+
|
|
72
|
+
(a) You must give any other recipients of the Work or
|
|
73
|
+
Derivative Works a copy of this License; and
|
|
74
|
+
|
|
75
|
+
(b) You must cause any modified files to carry prominent notices
|
|
76
|
+
stating that You changed the files; and
|
|
77
|
+
|
|
78
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
79
|
+
that You distribute, all copyright, patent, trademark, and
|
|
80
|
+
attribution notices from the Source form of the Work,
|
|
81
|
+
excluding those notices that do not pertain to any part of
|
|
82
|
+
the Derivative Works; and
|
|
83
|
+
|
|
84
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
85
|
+
distribution, then any Derivative Works that You distribute must
|
|
86
|
+
include a readable copy of the attribution notices contained
|
|
87
|
+
within such NOTICE file.
|
|
88
|
+
|
|
89
|
+
5. Submission of Contributions.
|
|
90
|
+
|
|
91
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
92
|
+
names, trademarks, service marks, or product names of the Licensor.
|
|
93
|
+
|
|
94
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
95
|
+
agreed to in writing, Licensor provides the Work on an "AS IS" BASIS,
|
|
96
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND.
|
|
97
|
+
|
|
98
|
+
8. Limitation of Liability. In no event and under no legal theory shall
|
|
99
|
+
any Contributor be liable to You for damages.
|
|
100
|
+
|
|
101
|
+
9. Accepting Warranty or Additional Liability.
|
|
102
|
+
|
|
103
|
+
Copyright 2026 Molecule Dev, Inc.
|
|
104
|
+
|
|
105
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
106
|
+
you may not use this file except in compliance with the License.
|
|
107
|
+
You may obtain a copy of the License at
|
|
108
|
+
|
|
109
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
110
|
+
|
|
111
|
+
Unless required by applicable law or agreed to in writing, software
|
|
112
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
113
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
114
|
+
See the License for the specific language governing permissions and
|
|
115
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
AUTO-GENERATED — DO NOT EDIT THIS FILE.
|
|
3
|
+
Generated by `mlcl sync-docs` from the package's src/index.ts JSDoc + mlcl/registry.json.
|
|
4
|
+
Edits here are overwritten on the next commit (molecule's pre-commit hook regenerates).
|
|
5
|
+
To change this document, edit the module-level JSDoc in src/index.ts.
|
|
6
|
+
Generated: 2026-09-28T17:40:30.240Z
|
|
7
|
+
-->
|
|
8
|
+
|
|
9
|
+
# @molecule/api-ai-decisions-laya
|
|
10
|
+
|
|
11
|
+
> **Auto-generated, AI-first package reference** for the [molecule.dev](https://molecule.dev) ecosystem.
|
|
12
|
+
> It is written to be read by coding agents as much as by people, and is generated from this
|
|
13
|
+
> package's source — edit `src/index.ts` JSDoc, not this file.
|
|
14
|
+
|
|
15
|
+
Laya decisions provider for molecule.dev — typed decisions from the
|
|
16
|
+
open-weights Laya model (Apache-2.0) on a `laya-serve` host you run.
|
|
17
|
+
|
|
18
|
+
Laya is a ~421M-parameter encoder (ModernBERT-large; a 322M multilingual
|
|
19
|
+
checkpoint covers 100+ languages) that answers `choice`, `score` and yes/no
|
|
20
|
+
questions in one forward pass — about 33–40 ms per question on a T4 GPU,
|
|
21
|
+
with no text generation. This bond speaks `laya-serve`'s `/v1/systemone`
|
|
22
|
+
route, the same wire protocol as TypeSafe's Jev, so swapping to
|
|
23
|
+
`@molecule/api-ai-decisions-jev` is a one-line change.
|
|
24
|
+
|
|
25
|
+
## Quick Start
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
# Run the model server (CPU works; a GPU is ~10x faster)
|
|
29
|
+
pip install "laya[serve]"
|
|
30
|
+
LAYA_API_KEY=change-me laya-serve # http://0.0.0.0:8000
|
|
31
|
+
# or: docker compose up (compose.yaml in github.com/NandhaKishorM/laya)
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
```typescript
|
|
35
|
+
import { setProvider, requireProvider } from '@molecule/api-ai-decisions'
|
|
36
|
+
import { provider } from '@molecule/api-ai-decisions-laya'
|
|
37
|
+
|
|
38
|
+
setProvider(provider) // reads LAYA_URL + LAYA_API_KEY on first use
|
|
39
|
+
|
|
40
|
+
const { answers } = await requireProvider().decide({
|
|
41
|
+
state: 'My card was charged twice, please refund one of them',
|
|
42
|
+
questions: {
|
|
43
|
+
queue: {
|
|
44
|
+
type: 'choice',
|
|
45
|
+
instructions: 'Which team?',
|
|
46
|
+
criteria: { billing: 'charges, refunds', tech: 'bugs, login' },
|
|
47
|
+
},
|
|
48
|
+
refund: { type: 'yesNo', instructions: 'The customer wants a refund.' },
|
|
49
|
+
},
|
|
50
|
+
})
|
|
51
|
+
answers.queue.choice // 'billing'
|
|
52
|
+
answers.refund.probability // 0.96
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Type
|
|
56
|
+
|
|
57
|
+
`provider`
|
|
58
|
+
|
|
59
|
+
## Installation
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
npm install @molecule/api-ai-decisions-laya @molecule/api-ai-decisions @molecule/api-secrets
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## API
|
|
66
|
+
|
|
67
|
+
### Interfaces
|
|
68
|
+
|
|
69
|
+
#### `LayaConfig`
|
|
70
|
+
|
|
71
|
+
Configuration for the Laya decisions provider.
|
|
72
|
+
|
|
73
|
+
```typescript
|
|
74
|
+
interface LayaConfig {
|
|
75
|
+
/** Base URL of the `laya-serve` host. Defaults to `LAYA_URL`, then `http://localhost:8000`. */
|
|
76
|
+
baseUrl?: string
|
|
77
|
+
/** Bearer token, when the server sets `LAYA_API_KEY`. Defaults to the `LAYA_API_KEY` env var. */
|
|
78
|
+
apiKey?: string
|
|
79
|
+
/**
|
|
80
|
+
* Default checkpoint: `'english'`, `'multilingual'` or `'typed-decisions'`
|
|
81
|
+
* (or a Hugging Face id such as `'convaiinnovations/laya-multilingual'`).
|
|
82
|
+
* Omit to let the server route by the input's language.
|
|
83
|
+
*/
|
|
84
|
+
model?: string
|
|
85
|
+
}
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
#### `PostOptions`
|
|
89
|
+
|
|
90
|
+
Options for {@link postSystemOne}.
|
|
91
|
+
|
|
92
|
+
```typescript
|
|
93
|
+
interface PostOptions {
|
|
94
|
+
/** Full endpoint URL. */
|
|
95
|
+
url: string
|
|
96
|
+
/** Bearer token, if any. */
|
|
97
|
+
apiKey?: string
|
|
98
|
+
/** Request body. */
|
|
99
|
+
body: Record<string, unknown>
|
|
100
|
+
/** Abort signal. */
|
|
101
|
+
signal?: AbortSignal
|
|
102
|
+
/** Label for error messages (`'Laya'`, `'Jev'`). */
|
|
103
|
+
label: string
|
|
104
|
+
/** Max retries on a retryable status (default 3). */
|
|
105
|
+
maxRetries?: number
|
|
106
|
+
}
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
#### `WireAnswer`
|
|
110
|
+
|
|
111
|
+
One answer as received on the wire (only the fields we read).
|
|
112
|
+
|
|
113
|
+
```typescript
|
|
114
|
+
interface WireAnswer {
|
|
115
|
+
type?: string
|
|
116
|
+
choice?: string
|
|
117
|
+
score?: number
|
|
118
|
+
noul?: number
|
|
119
|
+
probabilities?: Record<string, number>
|
|
120
|
+
}
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
#### `WireQuestion`
|
|
124
|
+
|
|
125
|
+
One question as sent on the wire.
|
|
126
|
+
|
|
127
|
+
```typescript
|
|
128
|
+
interface WireQuestion {
|
|
129
|
+
type: 'choice' | 'score' | 'noul'
|
|
130
|
+
instructions: string
|
|
131
|
+
criteria?: Record<string, string> | string[]
|
|
132
|
+
}
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
#### `WireResponse`
|
|
136
|
+
|
|
137
|
+
The response body (only the fields we read).
|
|
138
|
+
|
|
139
|
+
```typescript
|
|
140
|
+
interface WireResponse {
|
|
141
|
+
model?: string
|
|
142
|
+
answers?: Record<string, WireAnswer>
|
|
143
|
+
usage?: { input_tokens?: number; output_tokens?: number }
|
|
144
|
+
}
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
### Functions
|
|
148
|
+
|
|
149
|
+
#### `createProvider(config)`
|
|
150
|
+
|
|
151
|
+
Creates a Laya decisions provider.
|
|
152
|
+
|
|
153
|
+
```typescript
|
|
154
|
+
function createProvider(config?: LayaConfig): AIDecisionsProvider
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
- `config` — Base URL, API key and default checkpoint.
|
|
158
|
+
|
|
159
|
+
**Returns:** An `AIDecisionsProvider` backed by a `laya-serve` host.
|
|
160
|
+
|
|
161
|
+
#### `fromWireAnswer(id, question, wire, minConfidence)`
|
|
162
|
+
|
|
163
|
+
Converts one wire answer into the core's answer for `question`.
|
|
164
|
+
|
|
165
|
+
```typescript
|
|
166
|
+
function fromWireAnswer(
|
|
167
|
+
id: string,
|
|
168
|
+
question: DecisionQuestion,
|
|
169
|
+
wire: WireAnswer | undefined,
|
|
170
|
+
minConfidence?: number,
|
|
171
|
+
): DecisionAnswer
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
- `id` — The question id (for error messages).
|
|
175
|
+
- `question` — The question that was asked.
|
|
176
|
+
- `wire` — The wire answer.
|
|
177
|
+
- `minConfidence` — Optional low-confidence threshold.
|
|
178
|
+
|
|
179
|
+
**Returns:** The typed answer.
|
|
180
|
+
|
|
181
|
+
#### `fromWireResponse(questions, body, minConfidence)`
|
|
182
|
+
|
|
183
|
+
Converts a whole wire response into the core's result.
|
|
184
|
+
|
|
185
|
+
```typescript
|
|
186
|
+
function fromWireResponse(questions: Q, body: WireResponse, minConfidence?: number): DecideResult<Q>
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
- `questions` — The questions that were asked.
|
|
190
|
+
- `body` — The parsed response body.
|
|
191
|
+
- `minConfidence` — Optional low-confidence threshold.
|
|
192
|
+
|
|
193
|
+
**Returns:** The typed result.
|
|
194
|
+
|
|
195
|
+
#### `postSystemOne(opts)`
|
|
196
|
+
|
|
197
|
+
POSTs a `/v1/systemone` request with retry on 429/5xx-busy, honouring
|
|
198
|
+
`Retry-After`. Throws an Error carrying `status` on a non-2xx response.
|
|
199
|
+
|
|
200
|
+
```typescript
|
|
201
|
+
function postSystemOne(opts: PostOptions): Promise<WireResponse>
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
- `opts` — Request options.
|
|
205
|
+
|
|
206
|
+
**Returns:** The parsed response body.
|
|
207
|
+
|
|
208
|
+
#### `toWireQuestions(questions)`
|
|
209
|
+
|
|
210
|
+
Converts the core's questions into wire questions (`yesNo` → `noul`).
|
|
211
|
+
|
|
212
|
+
```typescript
|
|
213
|
+
function toWireQuestions(questions: Record<string, DecisionQuestion>): Record<string, WireQuestion>
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
- `questions` — The questions keyed by id.
|
|
217
|
+
|
|
218
|
+
**Returns:** The wire `questions` object.
|
|
219
|
+
|
|
220
|
+
### Constants
|
|
221
|
+
|
|
222
|
+
#### `aiDecisionsLayaSecretDefinitions`
|
|
223
|
+
|
|
224
|
+
Secret definitions used by the Laya decisions bond.
|
|
225
|
+
|
|
226
|
+
```typescript
|
|
227
|
+
const aiDecisionsLayaSecretDefinitions: SecretDefinition[]
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
#### `DEFAULT_LAYA_URL`
|
|
231
|
+
|
|
232
|
+
`laya-serve`'s default bind (`LAYA_PORT` defaults to 8000).
|
|
233
|
+
|
|
234
|
+
```typescript
|
|
235
|
+
const DEFAULT_LAYA_URL: 'http://localhost:8000'
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
#### `provider`
|
|
239
|
+
|
|
240
|
+
The provider implementation — lazy, so env vars are read on first use.
|
|
241
|
+
|
|
242
|
+
```typescript
|
|
243
|
+
const provider: AIDecisionsProvider
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
## Core Interface
|
|
247
|
+
|
|
248
|
+
Implements `@molecule/api-ai-decisions` interface.
|
|
249
|
+
|
|
250
|
+
## Bond Wiring
|
|
251
|
+
|
|
252
|
+
Setup function to register this provider with the core interface:
|
|
253
|
+
|
|
254
|
+
```typescript
|
|
255
|
+
import { setProvider } from '@molecule/api-ai-decisions'
|
|
256
|
+
import { provider } from '@molecule/api-ai-decisions-laya'
|
|
257
|
+
|
|
258
|
+
export function setupAiDecisionsLaya(): void {
|
|
259
|
+
setProvider(provider)
|
|
260
|
+
}
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
## Injection Notes
|
|
264
|
+
|
|
265
|
+
### Requirements
|
|
266
|
+
|
|
267
|
+
Peer dependencies:
|
|
268
|
+
|
|
269
|
+
- `@molecule/api-ai-decisions` ^1.0.0
|
|
270
|
+
- `@molecule/api-secrets` ^1.0.1
|
|
271
|
+
|
|
272
|
+
### Environment Variables
|
|
273
|
+
|
|
274
|
+
- `LAYA_URL` _(optional)_ — Laya server URL
|
|
275
|
+
- Setup: The base URL of your laya-serve host (pip install "laya[serve]" && laya-serve, or the Docker image). Defaults to http://localhost:8000.
|
|
276
|
+
- Get it here: [https://github.com/NandhaKishorM/laya/blob/main/docs/http-api.md](https://github.com/NandhaKishorM/laya/blob/main/docs/http-api.md)
|
|
277
|
+
- Example: `http://localhost:8000`
|
|
278
|
+
- `LAYA_API_KEY` _(optional)_ — Laya server API key
|
|
279
|
+
- Setup: The bearer token your laya-serve host requires, if you set LAYA_API_KEY on the server.
|
|
280
|
+
- Get it here: [https://github.com/NandhaKishorM/laya/blob/main/docs/http-api.md](https://github.com/NandhaKishorM/laya/blob/main/docs/http-api.md)
|
|
281
|
+
- Example: `a-long-random-string`
|
|
282
|
+
|
|
283
|
+
### Runtime Dependencies
|
|
284
|
+
|
|
285
|
+
- `@molecule/api-ai-decisions`
|
|
286
|
+
- `@molecule/api-secrets`
|
|
287
|
+
|
|
288
|
+
- **You run the model.** `LAYA_URL` (default `http://localhost:8000`) points
|
|
289
|
+
at a `laya-serve` host; set `LAYA_API_KEY` on BOTH sides to require a
|
|
290
|
+
bearer token — without it the server is open to anyone who can reach it,
|
|
291
|
+
so never expose an unauthenticated one publicly.
|
|
292
|
+
- **Any `/v1/systemone` server works.** `LAYA_URL` can point at another
|
|
293
|
+
self-hosted server of the same protocol — e.g. Kev
|
|
294
|
+
(github.com/jaredpalmer/kev, Apache-2.0 LoRA adapters on Qwen, 0.8B–27B,
|
|
295
|
+
needs a GPU or Apple MLX) — with no code change.
|
|
296
|
+
- **Checkpoints:** pass `model: 'english' | 'multilingual' | 'typed-decisions'`
|
|
297
|
+
(per call or `createProvider({ model })`); omit it and the server routes by
|
|
298
|
+
the input's script/language. Any other value (e.g. a Jev id) is ignored by
|
|
299
|
+
the server, not rejected.
|
|
300
|
+
- **Server limits (each a 413):** 64 questions/request, 100 options per
|
|
301
|
+
`choice`, 32 levels per `score`, 512 options total, 50,000-char state,
|
|
302
|
+
2 MiB body. Option texts must also fit a ~192-token window (else 422) —
|
|
303
|
+
keep descriptions short. Busy servers answer 503 + `Retry-After`; this
|
|
304
|
+
bond retries 429/503/529 up to 3 times.
|
|
305
|
+
- **Accuracy is yours to measure.** Base checkpoints are near chance on
|
|
306
|
+
unfamiliar decision sets and ship over-confident; fine-tune (the repo has
|
|
307
|
+
a free Kaggle notebook) and calibrate on your own labelled data, and gate
|
|
308
|
+
on `minConfidence`. See the core's remarks.
|
|
309
|
+
- Use the core's `setProvider`, not `bond('ai-decisions', …)` directly.
|
|
310
|
+
|
|
311
|
+
## E2E Tests
|
|
312
|
+
|
|
313
|
+
Integration checklist — drive the real UI (live preview, no mocks), adapt
|
|
314
|
+
each item to this app's actual screens/flows, and check every box off one
|
|
315
|
+
by one. A box you can't check is an integration bug to fix — not a skip:
|
|
316
|
+
|
|
317
|
+
- [ ] Each flow that makes a decision (routing, triage, moderation, a
|
|
318
|
+
guardrail) runs it from the real UI, and the answer DRIVES what happens
|
|
319
|
+
next (the item lands in the chosen queue, the badge shows, the action is
|
|
320
|
+
blocked) — not just printed.
|
|
321
|
+
- [ ] Both directions: a clearly-billing input routes to billing AND a
|
|
322
|
+
clearly-technical one routes elsewhere. One label for every input is a
|
|
323
|
+
broken integration.
|
|
324
|
+
- [ ] A low-confidence answer takes the app's fallback path (human review,
|
|
325
|
+
"unsure" state) instead of being acted on.
|
|
326
|
+
- [ ] Provider errors (service down, bad key) show a visible, recoverable
|
|
327
|
+
state — never a blank screen or an unhandled rejection.
|
|
328
|
+
- [ ] The call runs server-side: no provider request or key in the browser's
|
|
329
|
+
Network tab.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"browser-guard.d.ts","sourceRoot":"","sources":["../src/browser-guard.ts"],"names":[],"mappings":"AAuBA,OAAO,EAAE,CAAA"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Browser guard — `@molecule/api-ai-decisions-laya` is SERVER-ONLY.
|
|
3
|
+
*
|
|
4
|
+
* Generated by scripts/gen-browser-guards.mjs (workspace root) — edit THAT, not this.
|
|
5
|
+
* Evaluating a server package in a browser bundle is always an import-graph mistake
|
|
6
|
+
* (node APIs, secrets); without this guard it surfaces as a cryptic downstream crash
|
|
7
|
+
* ("Buffer is not defined") far from the culprit. Throwing here names the package and
|
|
8
|
+
* the fix at the exact moment the client bundle evaluates it. jsdom tests and SSR are
|
|
9
|
+
* unaffected: the throw requires browser globals AND the absence of a node runtime.
|
|
10
|
+
*/
|
|
11
|
+
const g = globalThis;
|
|
12
|
+
if (g.window !== undefined && g.document !== undefined && !g.process?.versions?.node) {
|
|
13
|
+
throw new Error('@molecule/api-ai-decisions-laya is SERVER-ONLY: it was bundled into browser/client code. Import it only ' +
|
|
14
|
+
'from server code (a server route/function or your API), or dynamic-import it inside ' +
|
|
15
|
+
'the server handler — never from components or shared client modules, and never ' +
|
|
16
|
+
'polyfill Buffer/process to silence this.');
|
|
17
|
+
}
|
|
18
|
+
export {};
|
|
19
|
+
//# sourceMappingURL=browser-guard.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"browser-guard.js","sourceRoot":"","sources":["../src/browser-guard.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,MAAM,CAAC,GAAG,UAIT,CAAA;AACD,IAAI,CAAC,CAAC,MAAM,KAAK,SAAS,IAAI,CAAC,CAAC,QAAQ,KAAK,SAAS,IAAI,CAAC,CAAC,CAAC,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC;IACrF,MAAM,IAAI,KAAK,CACb,0GAA0G;QACxG,sFAAsF;QACtF,iFAAiF;QACjF,0CAA0C,CAC7C,CAAA;AACH,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Laya decisions provider for molecule.dev — typed decisions from the
|
|
3
|
+
* open-weights Laya model (Apache-2.0) on a `laya-serve` host you run.
|
|
4
|
+
*
|
|
5
|
+
* Laya is a ~421M-parameter encoder (ModernBERT-large; a 322M multilingual
|
|
6
|
+
* checkpoint covers 100+ languages) that answers `choice`, `score` and yes/no
|
|
7
|
+
* questions in one forward pass — about 33–40 ms per question on a T4 GPU,
|
|
8
|
+
* with no text generation. This bond speaks `laya-serve`'s `/v1/systemone`
|
|
9
|
+
* route, the same wire protocol as TypeSafe's Jev, so swapping to
|
|
10
|
+
* `@molecule/api-ai-decisions-jev` is a one-line change.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```bash
|
|
14
|
+
* # Run the model server (CPU works; a GPU is ~10x faster)
|
|
15
|
+
* pip install "laya[serve]"
|
|
16
|
+
* LAYA_API_KEY=change-me laya-serve # http://0.0.0.0:8000
|
|
17
|
+
* # or: docker compose up (compose.yaml in github.com/NandhaKishorM/laya)
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* ```typescript
|
|
21
|
+
* import { setProvider, requireProvider } from '@molecule/api-ai-decisions'
|
|
22
|
+
* import { provider } from '@molecule/api-ai-decisions-laya'
|
|
23
|
+
*
|
|
24
|
+
* setProvider(provider) // reads LAYA_URL + LAYA_API_KEY on first use
|
|
25
|
+
*
|
|
26
|
+
* const { answers } = await requireProvider().decide({
|
|
27
|
+
* state: 'My card was charged twice, please refund one of them',
|
|
28
|
+
* questions: {
|
|
29
|
+
* queue: { type: 'choice', instructions: 'Which team?', criteria: { billing: 'charges, refunds', tech: 'bugs, login' } },
|
|
30
|
+
* refund: { type: 'yesNo', instructions: 'The customer wants a refund.' },
|
|
31
|
+
* },
|
|
32
|
+
* })
|
|
33
|
+
* answers.queue.choice // 'billing'
|
|
34
|
+
* answers.refund.probability // 0.96
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* @remarks
|
|
38
|
+
* - **You run the model.** `LAYA_URL` (default `http://localhost:8000`) points
|
|
39
|
+
* at a `laya-serve` host; set `LAYA_API_KEY` on BOTH sides to require a
|
|
40
|
+
* bearer token — without it the server is open to anyone who can reach it,
|
|
41
|
+
* so never expose an unauthenticated one publicly.
|
|
42
|
+
* - **Any `/v1/systemone` server works.** `LAYA_URL` can point at another
|
|
43
|
+
* self-hosted server of the same protocol — e.g. Kev
|
|
44
|
+
* (github.com/jaredpalmer/kev, Apache-2.0 LoRA adapters on Qwen, 0.8B–27B,
|
|
45
|
+
* needs a GPU or Apple MLX) — with no code change.
|
|
46
|
+
* - **Checkpoints:** pass `model: 'english' | 'multilingual' | 'typed-decisions'`
|
|
47
|
+
* (per call or `createProvider({ model })`); omit it and the server routes by
|
|
48
|
+
* the input's script/language. Any other value (e.g. a Jev id) is ignored by
|
|
49
|
+
* the server, not rejected.
|
|
50
|
+
* - **Server limits (each a 413):** 64 questions/request, 100 options per
|
|
51
|
+
* `choice`, 32 levels per `score`, 512 options total, 50,000-char state,
|
|
52
|
+
* 2 MiB body. Option texts must also fit a ~192-token window (else 422) —
|
|
53
|
+
* keep descriptions short. Busy servers answer 503 + `Retry-After`; this
|
|
54
|
+
* bond retries 429/503/529 up to 3 times.
|
|
55
|
+
* - **Accuracy is yours to measure.** Base checkpoints are near chance on
|
|
56
|
+
* unfamiliar decision sets and ship over-confident; fine-tune (the repo has
|
|
57
|
+
* a free Kaggle notebook) and calibrate on your own labelled data, and gate
|
|
58
|
+
* on `minConfidence`. See the core's remarks.
|
|
59
|
+
* - Use the core's `setProvider`, not `bond('ai-decisions', …)` directly.
|
|
60
|
+
*
|
|
61
|
+
* @module
|
|
62
|
+
*/
|
|
63
|
+
export * from './browser-guard.js';
|
|
64
|
+
export * from './provider.js';
|
|
65
|
+
export * from './secrets.js';
|
|
66
|
+
export * from './types.js';
|
|
67
|
+
export * from './wire.js';
|
|
68
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6DG;AAEH,cAAc,oBAAoB,CAAA;AAClC,cAAc,eAAe,CAAA;AAC7B,cAAc,cAAc,CAAA;AAC5B,cAAc,YAAY,CAAA;AAC1B,cAAc,WAAW,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Laya decisions provider for molecule.dev — typed decisions from the
|
|
3
|
+
* open-weights Laya model (Apache-2.0) on a `laya-serve` host you run.
|
|
4
|
+
*
|
|
5
|
+
* Laya is a ~421M-parameter encoder (ModernBERT-large; a 322M multilingual
|
|
6
|
+
* checkpoint covers 100+ languages) that answers `choice`, `score` and yes/no
|
|
7
|
+
* questions in one forward pass — about 33–40 ms per question on a T4 GPU,
|
|
8
|
+
* with no text generation. This bond speaks `laya-serve`'s `/v1/systemone`
|
|
9
|
+
* route, the same wire protocol as TypeSafe's Jev, so swapping to
|
|
10
|
+
* `@molecule/api-ai-decisions-jev` is a one-line change.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```bash
|
|
14
|
+
* # Run the model server (CPU works; a GPU is ~10x faster)
|
|
15
|
+
* pip install "laya[serve]"
|
|
16
|
+
* LAYA_API_KEY=change-me laya-serve # http://0.0.0.0:8000
|
|
17
|
+
* # or: docker compose up (compose.yaml in github.com/NandhaKishorM/laya)
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* ```typescript
|
|
21
|
+
* import { setProvider, requireProvider } from '@molecule/api-ai-decisions'
|
|
22
|
+
* import { provider } from '@molecule/api-ai-decisions-laya'
|
|
23
|
+
*
|
|
24
|
+
* setProvider(provider) // reads LAYA_URL + LAYA_API_KEY on first use
|
|
25
|
+
*
|
|
26
|
+
* const { answers } = await requireProvider().decide({
|
|
27
|
+
* state: 'My card was charged twice, please refund one of them',
|
|
28
|
+
* questions: {
|
|
29
|
+
* queue: { type: 'choice', instructions: 'Which team?', criteria: { billing: 'charges, refunds', tech: 'bugs, login' } },
|
|
30
|
+
* refund: { type: 'yesNo', instructions: 'The customer wants a refund.' },
|
|
31
|
+
* },
|
|
32
|
+
* })
|
|
33
|
+
* answers.queue.choice // 'billing'
|
|
34
|
+
* answers.refund.probability // 0.96
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* @remarks
|
|
38
|
+
* - **You run the model.** `LAYA_URL` (default `http://localhost:8000`) points
|
|
39
|
+
* at a `laya-serve` host; set `LAYA_API_KEY` on BOTH sides to require a
|
|
40
|
+
* bearer token — without it the server is open to anyone who can reach it,
|
|
41
|
+
* so never expose an unauthenticated one publicly.
|
|
42
|
+
* - **Any `/v1/systemone` server works.** `LAYA_URL` can point at another
|
|
43
|
+
* self-hosted server of the same protocol — e.g. Kev
|
|
44
|
+
* (github.com/jaredpalmer/kev, Apache-2.0 LoRA adapters on Qwen, 0.8B–27B,
|
|
45
|
+
* needs a GPU or Apple MLX) — with no code change.
|
|
46
|
+
* - **Checkpoints:** pass `model: 'english' | 'multilingual' | 'typed-decisions'`
|
|
47
|
+
* (per call or `createProvider({ model })`); omit it and the server routes by
|
|
48
|
+
* the input's script/language. Any other value (e.g. a Jev id) is ignored by
|
|
49
|
+
* the server, not rejected.
|
|
50
|
+
* - **Server limits (each a 413):** 64 questions/request, 100 options per
|
|
51
|
+
* `choice`, 32 levels per `score`, 512 options total, 50,000-char state,
|
|
52
|
+
* 2 MiB body. Option texts must also fit a ~192-token window (else 422) —
|
|
53
|
+
* keep descriptions short. Busy servers answer 503 + `Retry-After`; this
|
|
54
|
+
* bond retries 429/503/529 up to 3 times.
|
|
55
|
+
* - **Accuracy is yours to measure.** Base checkpoints are near chance on
|
|
56
|
+
* unfamiliar decision sets and ship over-confident; fine-tune (the repo has
|
|
57
|
+
* a free Kaggle notebook) and calibrate on your own labelled data, and gate
|
|
58
|
+
* on `minConfidence`. See the core's remarks.
|
|
59
|
+
* - Use the core's `setProvider`, not `bond('ai-decisions', …)` directly.
|
|
60
|
+
*
|
|
61
|
+
* @module
|
|
62
|
+
*/
|
|
63
|
+
export * from './browser-guard.js';
|
|
64
|
+
export * from './provider.js';
|
|
65
|
+
export * from './secrets.js';
|
|
66
|
+
export * from './types.js';
|
|
67
|
+
export * from './wire.js';
|
|
68
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6DG;AAEH,cAAc,oBAAoB,CAAA;AAClC,cAAc,eAAe,CAAA;AAC7B,cAAc,cAAc,CAAA;AAC5B,cAAc,YAAY,CAAA;AAC1B,cAAc,WAAW,CAAA"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Laya implementation of `AIDecisionsProvider`.
|
|
3
|
+
*
|
|
4
|
+
* Talks to a self-hosted `laya-serve` over its `POST /v1/systemone` route
|
|
5
|
+
* (the TypeSafe Jev wire protocol), so the open-weights Laya model answers
|
|
6
|
+
* typed decisions on your own hardware.
|
|
7
|
+
*
|
|
8
|
+
* @module
|
|
9
|
+
*/
|
|
10
|
+
import './secrets.js';
|
|
11
|
+
import type { AIDecisionsProvider } from '@molecule/api-ai-decisions';
|
|
12
|
+
import type { LayaConfig } from './types.js';
|
|
13
|
+
/** `laya-serve`'s default bind (`LAYA_PORT` defaults to 8000). */
|
|
14
|
+
export declare const DEFAULT_LAYA_URL = "http://localhost:8000";
|
|
15
|
+
/**
|
|
16
|
+
* Creates a Laya decisions provider.
|
|
17
|
+
*
|
|
18
|
+
* @param config - Base URL, API key and default checkpoint.
|
|
19
|
+
* @returns An `AIDecisionsProvider` backed by a `laya-serve` host.
|
|
20
|
+
*/
|
|
21
|
+
export declare function createProvider(config?: LayaConfig): AIDecisionsProvider;
|
|
22
|
+
/**
|
|
23
|
+
* The provider implementation — lazy, so env vars are read on first use.
|
|
24
|
+
*/
|
|
25
|
+
export declare const provider: AIDecisionsProvider;
|
|
26
|
+
//# sourceMappingURL=provider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAGH,OAAO,cAAc,CAAA;AAErB,OAAO,KAAK,EACV,mBAAmB,EAIpB,MAAM,4BAA4B,CAAA;AAEnC,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAA;AAG5C,kEAAkE;AAClE,eAAO,MAAM,gBAAgB,0BAA0B,CAAA;AAqDvD;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,MAAM,CAAC,EAAE,UAAU,GAAG,mBAAmB,CAEvE;AAGD;;GAEG;AACH,eAAO,MAAM,QAAQ,EAAE,mBASrB,CAAA"}
|
package/dist/provider.js
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Laya implementation of `AIDecisionsProvider`.
|
|
3
|
+
*
|
|
4
|
+
* Talks to a self-hosted `laya-serve` over its `POST /v1/systemone` route
|
|
5
|
+
* (the TypeSafe Jev wire protocol), so the open-weights Laya model answers
|
|
6
|
+
* typed decisions on your own hardware.
|
|
7
|
+
*
|
|
8
|
+
* @module
|
|
9
|
+
*/
|
|
10
|
+
// Side-effect import: registers this bond's secret definitions.
|
|
11
|
+
import './secrets.js';
|
|
12
|
+
import { fromWireResponse, postSystemOne, toWireQuestions } from './wire.js';
|
|
13
|
+
/** `laya-serve`'s default bind (`LAYA_PORT` defaults to 8000). */
|
|
14
|
+
export const DEFAULT_LAYA_URL = 'http://localhost:8000';
|
|
15
|
+
/**
|
|
16
|
+
* Laya decisions provider over HTTP.
|
|
17
|
+
*/
|
|
18
|
+
class LayaDecisionsProvider {
|
|
19
|
+
name = 'laya';
|
|
20
|
+
baseUrl;
|
|
21
|
+
apiKey;
|
|
22
|
+
model;
|
|
23
|
+
/**
|
|
24
|
+
* Creates the provider.
|
|
25
|
+
*
|
|
26
|
+
* @param config - Provider configuration; env vars fill anything omitted.
|
|
27
|
+
*/
|
|
28
|
+
constructor(config = {}) {
|
|
29
|
+
this.baseUrl = (config.baseUrl || process.env.LAYA_URL || DEFAULT_LAYA_URL).replace(/\/+$/, '');
|
|
30
|
+
this.apiKey = config.apiKey ?? (process.env.LAYA_API_KEY || undefined);
|
|
31
|
+
this.model = config.model;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Answers every question about `input.state` in one request.
|
|
35
|
+
*
|
|
36
|
+
* @param input - The state, questions and options.
|
|
37
|
+
* @returns One typed answer per question id.
|
|
38
|
+
*/
|
|
39
|
+
async decide(input) {
|
|
40
|
+
if (input.state === undefined || input.state === null) {
|
|
41
|
+
throw new Error('ai-decisions: decide() requires a `state`');
|
|
42
|
+
}
|
|
43
|
+
if (!input.questions || Object.keys(input.questions).length === 0) {
|
|
44
|
+
throw new Error('ai-decisions: decide() requires at least one question');
|
|
45
|
+
}
|
|
46
|
+
const model = input.model ?? this.model;
|
|
47
|
+
const body = await postSystemOne({
|
|
48
|
+
url: `${this.baseUrl}/v1/systemone`,
|
|
49
|
+
apiKey: this.apiKey,
|
|
50
|
+
body: {
|
|
51
|
+
...(model ? { model } : {}),
|
|
52
|
+
state: input.state,
|
|
53
|
+
questions: toWireQuestions(input.questions),
|
|
54
|
+
},
|
|
55
|
+
signal: input.signal,
|
|
56
|
+
label: 'Laya',
|
|
57
|
+
});
|
|
58
|
+
return fromWireResponse(input.questions, body, input.minConfidence);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Creates a Laya decisions provider.
|
|
63
|
+
*
|
|
64
|
+
* @param config - Base URL, API key and default checkpoint.
|
|
65
|
+
* @returns An `AIDecisionsProvider` backed by a `laya-serve` host.
|
|
66
|
+
*/
|
|
67
|
+
export function createProvider(config) {
|
|
68
|
+
return new LayaDecisionsProvider(config);
|
|
69
|
+
}
|
|
70
|
+
let _provider = null;
|
|
71
|
+
/**
|
|
72
|
+
* The provider implementation — lazy, so env vars are read on first use.
|
|
73
|
+
*/
|
|
74
|
+
export const provider = new Proxy({}, {
|
|
75
|
+
get(_, prop, receiver) {
|
|
76
|
+
if (!_provider)
|
|
77
|
+
_provider = createProvider();
|
|
78
|
+
return Reflect.get(_provider, prop, receiver);
|
|
79
|
+
},
|
|
80
|
+
set(_, prop, value) {
|
|
81
|
+
if (!_provider)
|
|
82
|
+
_provider = createProvider();
|
|
83
|
+
return Reflect.set(_provider, prop, value);
|
|
84
|
+
},
|
|
85
|
+
});
|
|
86
|
+
//# sourceMappingURL=provider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider.js","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,gEAAgE;AAChE,OAAO,cAAc,CAAA;AAUrB,OAAO,EAAE,gBAAgB,EAAE,aAAa,EAAE,eAAe,EAAE,MAAM,WAAW,CAAA;AAE5E,kEAAkE;AAClE,MAAM,CAAC,MAAM,gBAAgB,GAAG,uBAAuB,CAAA;AAEvD;;GAEG;AACH,MAAM,qBAAqB;IAChB,IAAI,GAAG,MAAM,CAAA;IACL,OAAO,CAAQ;IACf,MAAM,CAAoB;IAC1B,KAAK,CAAoB;IAE1C;;;;OAIG;IACH,YAAY,SAAqB,EAAE;QACjC,IAAI,CAAC,OAAO,GAAG,CAAC,MAAM,CAAC,OAAO,IAAI,OAAO,CAAC,GAAG,CAAC,QAAQ,IAAI,gBAAgB,CAAC,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAA;QAC/F,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC,MAAM,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,YAAY,IAAI,SAAS,CAAC,CAAA;QACtE,IAAI,CAAC,KAAK,GAAG,MAAM,CAAC,KAAK,CAAA;IAC3B,CAAC;IAED;;;;;OAKG;IACH,KAAK,CAAC,MAAM,CACV,KAAqB;QAErB,IAAI,KAAK,CAAC,KAAK,KAAK,SAAS,IAAI,KAAK,CAAC,KAAK,KAAK,IAAI,EAAE,CAAC;YACtD,MAAM,IAAI,KAAK,CAAC,2CAA2C,CAAC,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CAAC;IACD,GAAG,CAAC,CAAC,EAAE,IAAI,EAAE,KAAK;QAChB,IAAI,CAAC,SAAS;YAAE,SAAS,GAAG,cAAc,EAAE,CAAA;QAC5C,OAAO,OAAO,CAAC,GAAG,CAAC,SAAS,EAAE,IAAI,EAAE,KAAK,CAAC,CAAA;IAC5C,CAAC;CACF,CAAC,CAAA"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Laya secret definitions — self-registered at import time so the runtime
|
|
3
|
+
* secrets registry (`@molecule/api-secrets`) can drive boot-time configuration
|
|
4
|
+
* reports and actionable "not configured" errors.
|
|
5
|
+
*
|
|
6
|
+
* @module
|
|
7
|
+
*/
|
|
8
|
+
import type { SecretDefinition } from '@molecule/api-secrets';
|
|
9
|
+
/** Secret definitions used by the Laya decisions bond. */
|
|
10
|
+
export declare const aiDecisionsLayaSecretDefinitions: SecretDefinition[];
|
|
11
|
+
//# sourceMappingURL=secrets.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"secrets.d.ts","sourceRoot":"","sources":["../src/secrets.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAA;AAG7D,0DAA0D;AAC1D,eAAO,MAAM,gCAAgC,EAAE,gBAAgB,EAiB9D,CAAA"}
|
package/dist/secrets.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Laya secret definitions — self-registered at import time so the runtime
|
|
3
|
+
* secrets registry (`@molecule/api-secrets`) can drive boot-time configuration
|
|
4
|
+
* reports and actionable "not configured" errors.
|
|
5
|
+
*
|
|
6
|
+
* @module
|
|
7
|
+
*/
|
|
8
|
+
import { registerSecrets } from '@molecule/api-secrets';
|
|
9
|
+
/** Secret definitions used by the Laya decisions bond. */
|
|
10
|
+
export const aiDecisionsLayaSecretDefinitions = [
|
|
11
|
+
{
|
|
12
|
+
key: 'LAYA_URL',
|
|
13
|
+
description: 'Laya server URL — The base URL of your laya-serve host (pip install "laya[serve]" && laya-serve, or the Docker image). Defaults to http://localhost:8000.',
|
|
14
|
+
helpUrl: 'https://github.com/NandhaKishorM/laya/blob/main/docs/http-api.md',
|
|
15
|
+
required: false,
|
|
16
|
+
example: 'http://localhost:8000',
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
key: 'LAYA_API_KEY',
|
|
20
|
+
description: 'Laya server API key — The bearer token your laya-serve host requires, if you set LAYA_API_KEY on the server.',
|
|
21
|
+
helpUrl: 'https://github.com/NandhaKishorM/laya/blob/main/docs/http-api.md',
|
|
22
|
+
required: false,
|
|
23
|
+
example: 'a-long-random-string',
|
|
24
|
+
},
|
|
25
|
+
];
|
|
26
|
+
registerSecrets(aiDecisionsLayaSecretDefinitions);
|
|
27
|
+
//# sourceMappingURL=secrets.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"secrets.js","sourceRoot":"","sources":["../src/secrets.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAGH,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAA;AAEvD,0DAA0D;AAC1D,MAAM,CAAC,MAAM,gCAAgC,GAAuB;IAClE;QACE,GAAG,EAAE,UAAU;QACf,WAAW,EACT,2JAA2J;QAC7J,OAAO,EAAE,kEAAkE;QAC3E,QAAQ,EAAE,KAAK;QACf,OAAO,EAAE,uBAAuB;KACjC;IACD;QACE,GAAG,EAAE,cAAc;QACnB,WAAW,EACT,8GAA8G;QAChH,OAAO,EAAE,kEAAkE;QAC3E,QAAQ,EAAE,KAAK;QACf,OAAO,EAAE,sBAAsB;KAChC;CACF,CAAA;AAED,eAAe,CAAC,gCAAgC,CAAC,CAAA"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Laya decisions provider configuration.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Configuration for the Laya decisions provider.
|
|
8
|
+
*/
|
|
9
|
+
export interface LayaConfig {
|
|
10
|
+
/** Base URL of the `laya-serve` host. Defaults to `LAYA_URL`, then `http://localhost:8000`. */
|
|
11
|
+
baseUrl?: string;
|
|
12
|
+
/** Bearer token, when the server sets `LAYA_API_KEY`. Defaults to the `LAYA_API_KEY` env var. */
|
|
13
|
+
apiKey?: string;
|
|
14
|
+
/**
|
|
15
|
+
* Default checkpoint: `'english'`, `'multilingual'` or `'typed-decisions'`
|
|
16
|
+
* (or a Hugging Face id such as `'convaiinnovations/laya-multilingual'`).
|
|
17
|
+
* Omit to let the server route by the input's language.
|
|
18
|
+
*/
|
|
19
|
+
model?: string;
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH;;GAEG;AACH,MAAM,WAAW,UAAU;IACzB,+FAA+F;IAC/F,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,iGAAiG;IACjG,MAAM,CAAC,EAAE,MAAM,CAAA;IACf;;;;OAIG;IACH,KAAK,CAAC,EAAE,MAAM,CAAA;CACf"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;GAIG"}
|
package/dist/wire.d.ts
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `/v1/systemone` wire protocol (TypeSafe Jev, spoken verbatim by
|
|
3
|
+
* `laya-serve`) ↔ the `@molecule/api-ai-decisions` types.
|
|
4
|
+
*
|
|
5
|
+
* Wire facts (laya `docs/http-api.md`, docs.typesafe.ai/api): request
|
|
6
|
+
* `{ model?, state, questions: { <id>: { type: 'choice'|'score'|'noul', instructions, criteria } } }`;
|
|
7
|
+
* `choice` criteria is `{ label: description }`, `score` criteria a list of
|
|
8
|
+
* levels, `noul` criteria optional `{ true, false }`. Answers carry `choice` +
|
|
9
|
+
* `probabilities` keyed by label, `score` (expected level) + `probabilities`
|
|
10
|
+
* keyed `"0".."k-1"`, or `noul` (P(true)). Vendor `confidence` fields are
|
|
11
|
+
* ignored: the core defines `confidence` as the probability of the reported
|
|
12
|
+
* answer, computed here from the distribution.
|
|
13
|
+
*
|
|
14
|
+
* @module
|
|
15
|
+
*/
|
|
16
|
+
import type { DecideResult, DecisionAnswer, DecisionQuestion } from '@molecule/api-ai-decisions';
|
|
17
|
+
/** One question as sent on the wire. */
|
|
18
|
+
export interface WireQuestion {
|
|
19
|
+
type: 'choice' | 'score' | 'noul';
|
|
20
|
+
instructions: string;
|
|
21
|
+
criteria?: Record<string, string> | string[];
|
|
22
|
+
}
|
|
23
|
+
/** One answer as received on the wire (only the fields we read). */
|
|
24
|
+
export interface WireAnswer {
|
|
25
|
+
type?: string;
|
|
26
|
+
choice?: string;
|
|
27
|
+
score?: number;
|
|
28
|
+
noul?: number;
|
|
29
|
+
probabilities?: Record<string, number>;
|
|
30
|
+
}
|
|
31
|
+
/** The response body (only the fields we read). */
|
|
32
|
+
export interface WireResponse {
|
|
33
|
+
model?: string;
|
|
34
|
+
answers?: Record<string, WireAnswer>;
|
|
35
|
+
usage?: {
|
|
36
|
+
input_tokens?: number;
|
|
37
|
+
output_tokens?: number;
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Converts the core's questions into wire questions (`yesNo` → `noul`).
|
|
42
|
+
*
|
|
43
|
+
* @param questions - The questions keyed by id.
|
|
44
|
+
* @returns The wire `questions` object.
|
|
45
|
+
*/
|
|
46
|
+
export declare function toWireQuestions(questions: Record<string, DecisionQuestion>): Record<string, WireQuestion>;
|
|
47
|
+
/**
|
|
48
|
+
* Converts one wire answer into the core's answer for `question`.
|
|
49
|
+
*
|
|
50
|
+
* @param id - The question id (for error messages).
|
|
51
|
+
* @param question - The question that was asked.
|
|
52
|
+
* @param wire - The wire answer.
|
|
53
|
+
* @param minConfidence - Optional low-confidence threshold.
|
|
54
|
+
* @returns The typed answer.
|
|
55
|
+
*/
|
|
56
|
+
export declare function fromWireAnswer(id: string, question: DecisionQuestion, wire: WireAnswer | undefined, minConfidence?: number): DecisionAnswer;
|
|
57
|
+
/**
|
|
58
|
+
* Converts a whole wire response into the core's result.
|
|
59
|
+
*
|
|
60
|
+
* @param questions - The questions that were asked.
|
|
61
|
+
* @param body - The parsed response body.
|
|
62
|
+
* @param minConfidence - Optional low-confidence threshold.
|
|
63
|
+
* @returns The typed result.
|
|
64
|
+
*/
|
|
65
|
+
export declare function fromWireResponse<Q extends Record<string, DecisionQuestion>>(questions: Q, body: WireResponse, minConfidence?: number): DecideResult<Q>;
|
|
66
|
+
/** Options for {@link postSystemOne}. */
|
|
67
|
+
export interface PostOptions {
|
|
68
|
+
/** Full endpoint URL. */
|
|
69
|
+
url: string;
|
|
70
|
+
/** Bearer token, if any. */
|
|
71
|
+
apiKey?: string;
|
|
72
|
+
/** Request body. */
|
|
73
|
+
body: Record<string, unknown>;
|
|
74
|
+
/** Abort signal. */
|
|
75
|
+
signal?: AbortSignal;
|
|
76
|
+
/** Label for error messages (`'Laya'`, `'Jev'`). */
|
|
77
|
+
label: string;
|
|
78
|
+
/** Max retries on a retryable status (default 3). */
|
|
79
|
+
maxRetries?: number;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* POSTs a `/v1/systemone` request with retry on 429/5xx-busy, honouring
|
|
83
|
+
* `Retry-After`. Throws an Error carrying `status` on a non-2xx response.
|
|
84
|
+
*
|
|
85
|
+
* @param opts - Request options.
|
|
86
|
+
* @returns The parsed response body.
|
|
87
|
+
*/
|
|
88
|
+
export declare function postSystemOne(opts: PostOptions): Promise<WireResponse>;
|
|
89
|
+
//# sourceMappingURL=wire.d.ts.map
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AAM,GACrB,cAAc,CAiDhB;AAED;;;;;;;GAOG;AACH,wBAAgB,gBAAgB,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,gBAAgB,CAAC,EACzE,SAAS,EAAE,CAAC,EACZ,IAAI,EAAE,YAAY,EAClB,aAAa,CAAC,EAAE,MAAM,GACrB,YAAY,CAAC,CAAC,CAAC,CAiBjB;AAKD,yCAAyC;AACzC,MAAM,WAAW,WAAW;IAC1B,yBAAyB;IACzB,GAAG,EAAE,MAAM,CAAA;IACX,4BAA4B;IAC5B,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,oBAAoB;IACpB,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;IAC7B,oBAAoB;IACpB,MAAM,CAAC,EAAE,WAAW,CAAA;IACpB,oDAAoD;IACpD,KAAK,EAAE,MAAM,CAAA;IACb,qDAAqD;IACrD,UAAU,CAAC,EAAE,MAAM,CAAA;CACpB;AAED;;;;;;GAMG;AACH,wBAAsB,aAAa,CAAC,IAAI,EAAE,WAAW,GAAG,OAAO,CAAC,YAAY,CAAC,CAqC5E"}
|
package/dist/wire.js
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `/v1/systemone` wire protocol (TypeSafe Jev, spoken verbatim by
|
|
3
|
+
* `laya-serve`) ↔ the `@molecule/api-ai-decisions` types.
|
|
4
|
+
*
|
|
5
|
+
* Wire facts (laya `docs/http-api.md`, docs.typesafe.ai/api): request
|
|
6
|
+
* `{ model?, state, questions: { <id>: { type: 'choice'|'score'|'noul', instructions, criteria } } }`;
|
|
7
|
+
* `choice` criteria is `{ label: description }`, `score` criteria a list of
|
|
8
|
+
* levels, `noul` criteria optional `{ true, false }`. Answers carry `choice` +
|
|
9
|
+
* `probabilities` keyed by label, `score` (expected level) + `probabilities`
|
|
10
|
+
* keyed `"0".."k-1"`, or `noul` (P(true)). Vendor `confidence` fields are
|
|
11
|
+
* ignored: the core defines `confidence` as the probability of the reported
|
|
12
|
+
* answer, computed here from the distribution.
|
|
13
|
+
*
|
|
14
|
+
* @module
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* Converts the core's questions into wire questions (`yesNo` → `noul`).
|
|
18
|
+
*
|
|
19
|
+
* @param questions - The questions keyed by id.
|
|
20
|
+
* @returns The wire `questions` object.
|
|
21
|
+
*/
|
|
22
|
+
export function toWireQuestions(questions) {
|
|
23
|
+
const out = {};
|
|
24
|
+
for (const [id, q] of Object.entries(questions)) {
|
|
25
|
+
if (q.type === 'choice') {
|
|
26
|
+
out[id] = { type: 'choice', instructions: q.instructions, criteria: q.criteria };
|
|
27
|
+
}
|
|
28
|
+
else if (q.type === 'score') {
|
|
29
|
+
out[id] = { type: 'score', instructions: q.instructions, criteria: q.criteria };
|
|
30
|
+
}
|
|
31
|
+
else if (q.type === 'yesNo') {
|
|
32
|
+
const criteria = {};
|
|
33
|
+
if (q.criteria?.yes)
|
|
34
|
+
criteria.true = q.criteria.yes;
|
|
35
|
+
if (q.criteria?.no)
|
|
36
|
+
criteria.false = q.criteria.no;
|
|
37
|
+
out[id] = {
|
|
38
|
+
type: 'noul',
|
|
39
|
+
instructions: q.instructions,
|
|
40
|
+
...(Object.keys(criteria).length ? { criteria } : {}),
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
throw new Error(`ai-decisions: question "${id}" has unknown type "${q.type}"`);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return out;
|
|
48
|
+
}
|
|
49
|
+
const num = (v) => (typeof v === 'number' && Number.isFinite(v) ? v : 0);
|
|
50
|
+
const clamp01 = (v) => Math.max(0, Math.min(1, v));
|
|
51
|
+
/**
|
|
52
|
+
* Converts one wire answer into the core's answer for `question`.
|
|
53
|
+
*
|
|
54
|
+
* @param id - The question id (for error messages).
|
|
55
|
+
* @param question - The question that was asked.
|
|
56
|
+
* @param wire - The wire answer.
|
|
57
|
+
* @param minConfidence - Optional low-confidence threshold.
|
|
58
|
+
* @returns The typed answer.
|
|
59
|
+
*/
|
|
60
|
+
export function fromWireAnswer(id, question, wire, minConfidence) {
|
|
61
|
+
if (!wire || typeof wire !== 'object') {
|
|
62
|
+
throw new Error(`ai-decisions: the provider returned no answer for question "${id}"`);
|
|
63
|
+
}
|
|
64
|
+
let answer;
|
|
65
|
+
if (question.type === 'choice') {
|
|
66
|
+
const labels = Object.keys(question.criteria);
|
|
67
|
+
const probabilities = {};
|
|
68
|
+
for (const label of labels)
|
|
69
|
+
probabilities[label] = clamp01(num(wire.probabilities?.[label]));
|
|
70
|
+
let top = labels[0] ?? '';
|
|
71
|
+
for (const label of labels)
|
|
72
|
+
if (probabilities[label] > probabilities[top])
|
|
73
|
+
top = label;
|
|
74
|
+
const choice = typeof wire.choice === 'string' && labels.includes(wire.choice) ? wire.choice : top;
|
|
75
|
+
answer = {
|
|
76
|
+
type: 'choice',
|
|
77
|
+
choice,
|
|
78
|
+
probabilities,
|
|
79
|
+
confidence: probabilities[choice] ?? 0,
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
else if (question.type === 'score') {
|
|
83
|
+
const probabilities = question.criteria.map((_, i) => clamp01(num(wire.probabilities?.[String(i)])));
|
|
84
|
+
let level = 0;
|
|
85
|
+
probabilities.forEach((p, i) => {
|
|
86
|
+
if (p > probabilities[level])
|
|
87
|
+
level = i;
|
|
88
|
+
});
|
|
89
|
+
const expected = probabilities.reduce((sum, p, i) => sum + p * i, 0);
|
|
90
|
+
answer = {
|
|
91
|
+
type: 'score',
|
|
92
|
+
score: typeof wire.score === 'number' && Number.isFinite(wire.score) ? wire.score : expected,
|
|
93
|
+
level,
|
|
94
|
+
probabilities,
|
|
95
|
+
confidence: probabilities[level] ?? 0,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
if (typeof wire.noul !== 'number' || !Number.isFinite(wire.noul)) {
|
|
100
|
+
throw new Error(`ai-decisions: the provider's answer to "${id}" has no noul probability`);
|
|
101
|
+
}
|
|
102
|
+
const probability = clamp01(wire.noul);
|
|
103
|
+
answer = {
|
|
104
|
+
type: 'yesNo',
|
|
105
|
+
probability,
|
|
106
|
+
answer: probability >= 0.5,
|
|
107
|
+
confidence: Math.max(probability, 1 - probability),
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
if (typeof minConfidence === 'number')
|
|
111
|
+
answer.lowConfidence = answer.confidence < minConfidence;
|
|
112
|
+
return answer;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Converts a whole wire response into the core's result.
|
|
116
|
+
*
|
|
117
|
+
* @param questions - The questions that were asked.
|
|
118
|
+
* @param body - The parsed response body.
|
|
119
|
+
* @param minConfidence - Optional low-confidence threshold.
|
|
120
|
+
* @returns The typed result.
|
|
121
|
+
*/
|
|
122
|
+
export function fromWireResponse(questions, body, minConfidence) {
|
|
123
|
+
const answers = {};
|
|
124
|
+
for (const [id, q] of Object.entries(questions)) {
|
|
125
|
+
answers[id] = fromWireAnswer(id, q, body.answers?.[id], minConfidence);
|
|
126
|
+
}
|
|
127
|
+
return {
|
|
128
|
+
answers: answers,
|
|
129
|
+
...(typeof body.model === 'string' ? { model: body.model } : {}),
|
|
130
|
+
...(body.usage
|
|
131
|
+
? {
|
|
132
|
+
usage: {
|
|
133
|
+
inputTokens: num(body.usage.input_tokens),
|
|
134
|
+
outputTokens: num(body.usage.output_tokens),
|
|
135
|
+
},
|
|
136
|
+
}
|
|
137
|
+
: {}),
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
/** Statuses worth retrying: rate limit, busy (`laya-serve` 503 + Retry-After), overloaded (Jev 529). */
|
|
141
|
+
const RETRYABLE = new Set([429, 502, 503, 504, 529]);
|
|
142
|
+
/**
|
|
143
|
+
* POSTs a `/v1/systemone` request with retry on 429/5xx-busy, honouring
|
|
144
|
+
* `Retry-After`. Throws an Error carrying `status` on a non-2xx response.
|
|
145
|
+
*
|
|
146
|
+
* @param opts - Request options.
|
|
147
|
+
* @returns The parsed response body.
|
|
148
|
+
*/
|
|
149
|
+
export async function postSystemOne(opts) {
|
|
150
|
+
const { url, apiKey, body, signal, label, maxRetries = 3 } = opts;
|
|
151
|
+
const headers = { 'content-type': 'application/json' };
|
|
152
|
+
if (apiKey)
|
|
153
|
+
headers.authorization = `Bearer ${apiKey}`;
|
|
154
|
+
for (let attempt = 0;; attempt++) {
|
|
155
|
+
const response = await fetch(url, {
|
|
156
|
+
method: 'POST',
|
|
157
|
+
headers,
|
|
158
|
+
body: JSON.stringify(body),
|
|
159
|
+
signal,
|
|
160
|
+
});
|
|
161
|
+
if (response.ok)
|
|
162
|
+
return (await response.json());
|
|
163
|
+
if (RETRYABLE.has(response.status) && attempt < maxRetries) {
|
|
164
|
+
const retryAfter = Number(response.headers.get('retry-after'));
|
|
165
|
+
const waitMs = Number.isFinite(retryAfter) && retryAfter > 0
|
|
166
|
+
? Math.min(retryAfter * 1000, 10_000)
|
|
167
|
+
: 250 * 2 ** attempt;
|
|
168
|
+
await new Promise((r) => setTimeout(r, waitMs));
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
const text = await response.text().catch(() => '');
|
|
172
|
+
let detail = text.length > 0 && text.length < 300 ? text : `HTTP ${response.status}`;
|
|
173
|
+
try {
|
|
174
|
+
const parsed = JSON.parse(text);
|
|
175
|
+
if (typeof parsed.detail === 'string')
|
|
176
|
+
detail = parsed.detail;
|
|
177
|
+
else if (typeof parsed.error?.message === 'string')
|
|
178
|
+
detail = parsed.error.message;
|
|
179
|
+
}
|
|
180
|
+
catch (_error) {
|
|
181
|
+
// Not JSON — keep the raw text (or the status) as the detail.
|
|
182
|
+
}
|
|
183
|
+
throw Object.assign(new Error(`${label} decision request failed (${response.status}): ${detail}`), {
|
|
184
|
+
status: response.status,
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
//# sourceMappingURL=wire.js.map
|
package/dist/wire.js.map
ADDED
|
@@ -0,0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M,MAAM,EAAE,CAAC,EAC7E;YACE,MAAM,EAAE,QAAQ,CAAC,MAAM;SACxB,CACF,CAAA;IACH,CAAC;AACH,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@molecule/api-ai-decisions-laya",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Laya decisions provider for molecule.dev — typed choice/score/yes-no answers from the open-weights Laya model on your own laya-serve host",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "tsc",
|
|
10
|
+
"test": "vitest run",
|
|
11
|
+
"test:watch": "vitest"
|
|
12
|
+
},
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"import": "./dist/index.js"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"dist",
|
|
21
|
+
"README.md"
|
|
22
|
+
],
|
|
23
|
+
"keywords": [
|
|
24
|
+
"molecule",
|
|
25
|
+
"ai",
|
|
26
|
+
"decisions",
|
|
27
|
+
"laya",
|
|
28
|
+
"self-hosted",
|
|
29
|
+
"open-weights"
|
|
30
|
+
],
|
|
31
|
+
"license": "Apache-2.0",
|
|
32
|
+
"author": "Molecule Dev, Inc. (https://molecule.dev)",
|
|
33
|
+
"peerDependencies": {
|
|
34
|
+
"@molecule/api-ai-decisions": "^1.0.0",
|
|
35
|
+
"@molecule/api-secrets": "^1.0.1"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@molecule/api-ai-decisions": "1.0.0",
|
|
39
|
+
"@molecule/api-secrets": "1.0.2",
|
|
40
|
+
"@types/node": "26.1.2",
|
|
41
|
+
"typescript": "6.0.3",
|
|
42
|
+
"vitest": "4.1.11"
|
|
43
|
+
},
|
|
44
|
+
"repository": {
|
|
45
|
+
"type": "git",
|
|
46
|
+
"url": "https://github.com/molecule-dev/molecule.git",
|
|
47
|
+
"directory": "packages/api/bonds/ai-decisions/laya"
|
|
48
|
+
},
|
|
49
|
+
"homepage": "https://www.molecule.dev/packages/api-ai-decisions-laya",
|
|
50
|
+
"bugs": "https://github.com/molecule-dev/molecule/issues",
|
|
51
|
+
"publishConfig": {
|
|
52
|
+
"access": "public"
|
|
53
|
+
}
|
|
54
|
+
}
|