@plumbus/ai-decision-laya 0.2.0 → 0.2.2
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/CHANGELOG.md +10 -0
- package/README.md +14 -5
- package/dist/.tsbuildinfo +1 -1
- package/dist/laya-adapter.d.ts.map +1 -1
- package/dist/laya-adapter.js +19 -2
- package/dist/laya-adapter.js.map +1 -1
- package/instructions/README.md +8 -3
- package/instructions/framework.md +17 -4
- package/instructions/testing.md +10 -0
- package/package.json +3 -3
- package/service/test_audit_round_two.py +10 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"laya-adapter.d.ts","sourceRoot":"","sources":["../src/laya-adapter.ts"],"names":[],"mappings":"AAAA,OAAO,EAML,KAAK,kBAAkB,EACvB,KAAK,uBAAuB,EAC7B,MAAM,sBAAsB,CAAC;AAG9B,MAAM,WAAW,yBAA0B,SAAQ,kBAAkB;IACnE,0EAA0E;IAC1E,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,8EAA8E;IAC9E,iBAAiB,CAAC,EAAE,MAAM,CAAC;CAC5B;AAED,wFAAwF;AACxF,wBAAgB,yBAAyB,CACvC,MAAM,EAAE,yBAAyB,GAChC,uBAAuB,
|
|
1
|
+
{"version":3,"file":"laya-adapter.d.ts","sourceRoot":"","sources":["../src/laya-adapter.ts"],"names":[],"mappings":"AAAA,OAAO,EAML,KAAK,kBAAkB,EACvB,KAAK,uBAAuB,EAC7B,MAAM,sBAAsB,CAAC;AAG9B,MAAM,WAAW,yBAA0B,SAAQ,kBAAkB;IACnE,0EAA0E;IAC1E,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,8EAA8E;IAC9E,iBAAiB,CAAC,EAAE,MAAM,CAAC;CAC5B;AAED,wFAAwF;AACxF,wBAAgB,yBAAyB,CACvC,MAAM,EAAE,yBAAyB,GAChC,uBAAuB,CAgEzB"}
|
package/dist/laya-adapter.js
CHANGED
|
@@ -24,9 +24,26 @@ export function createLayaDecisionAdapter(config) {
|
|
|
24
24
|
...(model === 'auto' ? {} : { model }),
|
|
25
25
|
...(settings.data.language ? { lang: settings.data.language } : {}),
|
|
26
26
|
}, input);
|
|
27
|
-
|
|
27
|
+
let result;
|
|
28
|
+
try {
|
|
29
|
+
result = parseDecisionResponse(wire, input.questions, 'laya');
|
|
30
|
+
}
|
|
31
|
+
catch (error) {
|
|
32
|
+
if (error instanceof DecisionProviderError && error.usage && error.model) {
|
|
33
|
+
throw new DecisionProviderError('laya', error.kind, error.message, {
|
|
34
|
+
model: error.model,
|
|
35
|
+
usage: error.usage,
|
|
36
|
+
cost: settings.data.costPerRequestUsd ?? null,
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
throw error;
|
|
40
|
+
}
|
|
28
41
|
if (!z.object({ routing: z.object({}) }).safeParse(result).success) {
|
|
29
|
-
throw new DecisionProviderError('laya', 'invalid_response', 'Laya response is missing checkpoint routing identity', {
|
|
42
|
+
throw new DecisionProviderError('laya', 'invalid_response', 'Laya response is missing checkpoint routing identity', {
|
|
43
|
+
model: result.model,
|
|
44
|
+
usage: result.usage,
|
|
45
|
+
cost: settings.data.costPerRequestUsd ?? null,
|
|
46
|
+
});
|
|
30
47
|
}
|
|
31
48
|
const cost = settings.data.costPerRequestUsd ?? null;
|
|
32
49
|
return {
|
package/dist/laya-adapter.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"laya-adapter.js","sourceRoot":"","sources":["../src/laya-adapter.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,2BAA2B,EAC3B,qBAAqB,EACrB,qBAAqB,EACrB,oBAAoB,EACpB,uBAAuB,GAGxB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,CAAC,EAAE,MAAM,mBAAmB,CAAC;AAUtC,wFAAwF;AACxF,MAAM,UAAU,yBAAyB,C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AAM,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,OAAO,EAAE,CAAC;gBACnE,MAAM,IAAI,qBAAqB,CAC7B,MAAM,EACN,kBAAkB,EAClB,sDAAsD,EACtD;oBACE,KAAK,EAAE,MAAM,CAAC,KAAK;oBACnB,KAAK,EAAE,MAAM,CAAC,KAAK;oBACnB,IAAI,EAAE,QAAQ,CAAC,IAAI,CAAC,iBAAiB,IAAI,IAAI;iBAC9C,CACF,CAAC;YACJ,CAAC;YACD,MAAM,IAAI,GAAG,QAAQ,CAAC,IAAI,CAAC,iBAAiB,IAAI,IAAI,CAAC;YACrD,OAAO;gBACL,GAAG,MAAM;gBACT,IAAI;gBACJ,aAAa,EAAE,IAAI,KAAK,IAAI;gBAC5B,SAAS,EAAE,WAAW,CAAC,GAAG,EAAE,GAAG,KAAK;aACrC,CAAC;QACJ,CAAC;KACF,CAAC;AACJ,CAAC"}
|
package/instructions/README.md
CHANGED
|
@@ -5,8 +5,13 @@
|
|
|
5
5
|
| [framework.md](framework.md) | Installation, public exports, adapter boundary, usage rules |
|
|
6
6
|
| [testing.md](testing.md) | Offline validation and live environment setup |
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
**Classification, categorization, provider/model selection, TypeSafe/Jev or Laya:**
|
|
9
|
+
read `node_modules/@plumbus/core/instructions/ai-classification.md` first (core
|
|
10
|
+
**0.7.4+**), then [framework.md](framework.md) for this package.
|
|
11
|
+
|
|
12
|
+
Core 0.7.3+ provides `ctx.ai.decide()`; core 0.7.4+ also routes `ctx.ai.classify()`
|
|
13
|
+
to explicitly registered decision adapters. Keep app business logic in Plumbus
|
|
14
|
+
primitives and `ctx.*`. Never route predictions around capability access checks
|
|
15
|
+
or confirmation policy. Provider registration is explicit in `app/server.ts`.
|
|
11
16
|
|
|
12
17
|
[Package README](../README.md) · [Monorepo guide](https://github.com/plumbus-framework/plumbus/blob/main/docs/ai/decision-providers.md)
|
|
@@ -14,7 +14,19 @@ Public exports: `createLayaDecisionAdapter` — construct an explicitly configur
|
|
|
14
14
|
File map: `src/index.ts` is the public barrel, `src/__tests__/` exercises the public
|
|
15
15
|
API, `instructions/` contains these recipes. Laya alone also ships `service/`.
|
|
16
16
|
|
|
17
|
-
##
|
|
17
|
+
## Classification and model selection (core 0.7.4+)
|
|
18
|
+
|
|
19
|
+
Read `node_modules/@plumbus/core/instructions/ai-classification.md` for the complete
|
|
20
|
+
registration and usage recipe. Call `ctx.ai.classify({ text, labels, provider, model,
|
|
21
|
+
threshold })` inside capabilities/flows. `provider` is your registered decision map
|
|
22
|
+
key; `model` is an optional endpoint-supported ID. Native `threshold` defaults to
|
|
23
|
+
0.5 and returns all matching labels. Use `decide()` for richer questions.
|
|
24
|
+
|
|
25
|
+
Core records one `classify` cost row, including dispatched failures, with actual
|
|
26
|
+
model/usage and identity. Omitted `provider` still uses the text default. Keep
|
|
27
|
+
decision adapters under `decisions.providers`, separate from text providers.
|
|
28
|
+
|
|
29
|
+
## Minimal usage (infrastructure / smoke testing)
|
|
18
30
|
|
|
19
31
|
```ts
|
|
20
32
|
import { createLayaDecisionAdapter } from '@plumbus/ai-decision-laya';
|
|
@@ -33,12 +45,13 @@ console.log(result.answers.refund.probability);
|
|
|
33
45
|
1. Keep capabilities, flows, access policy and `ctx.*` as the application implementation path.
|
|
34
46
|
2. Construct adapters at a server-owned infrastructure boundary and inject them. Keep keys on the server.
|
|
35
47
|
3. Treat direct adapter usage as protocol integration, not a replacement for core's security/budget/audit lifecycle.
|
|
36
|
-
4.
|
|
48
|
+
4. With core 0.7.3+, use `ctx.ai.decide()` in capabilities/flows. Export `decisions = { providers, defaultProvider }` from `app/server.ts`; API and workers share it. Do not register decision adapters as completion providers.
|
|
37
49
|
5. Preserve distributions and provider confidence separately. A high confidence value is not authorization or proof of correctness.
|
|
38
50
|
6. Validate thresholds on the actual task/model/language. Unknown cost is `null`, not free.
|
|
39
|
-
7. Pass
|
|
51
|
+
7. Pass `signal: ctx.signal` and `timeoutMs` where needed. Flow steps supply their signal by default.
|
|
40
52
|
8. Keep Python inference outside Node.js. Use the packaged Laya service or implement its documented HTTP protocol.
|
|
41
|
-
9.
|
|
53
|
+
9. Define contracts using `defineDecision` from `@plumbus/ai-decision`; CLI startup discovers `app/decisions/`. No automatic environment provider discovery or dedicated decision CLI commands are provided.
|
|
54
|
+
10. Core records one `operation: decide` row per dispatched logical call, including failed/cancelled calls, with tenant/actor and `costContext`. Persist it in the existing `onAICostRecorded` hook. Unknown cost is null; never convert it to free usage.
|
|
42
55
|
|
|
43
56
|
Chat, voice and MCP still consume Plumbus capabilities; this package does not
|
|
44
57
|
introduce a parallel runtime or transport for those surfaces.
|
package/instructions/testing.md
CHANGED
|
@@ -17,3 +17,13 @@ after building. Scripts are repository tools, not installed core CLI commands.
|
|
|
17
17
|
Validate choices, scores, probabilities, actual model/routing, usage and unknown
|
|
18
18
|
cost handling. Measure live accuracy and calibration separately from connection
|
|
19
19
|
success. Do not assert a universal confidence cutoff or guaranteed live label.
|
|
20
|
+
|
|
21
|
+
For core 0.7.4+ classification, use `mockAI({ classify: ['billing'] })` and test
|
|
22
|
+
through `runCapability()` / `simulateFlow()`. Integration tests should assert
|
|
23
|
+
provider/model forwarding, zero/multiple matches, inclusive thresholds, and one
|
|
24
|
+
`classify` cost row. Use injected fetch; routine tests do not require live Laya.
|
|
25
|
+
|
|
26
|
+
For core 0.7.3+ integration tests, configure `mockAI({ decide: result })` or a real
|
|
27
|
+
`createAIService({ decisions, costTracker, onAICostRecorded })` with injected
|
|
28
|
+
provider fetch. Assert exactly one cost row, identity, actual model/usage, unknown
|
|
29
|
+
cost handling, billed validation failures, and budget rejection before dispatch.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@plumbus/ai-decision-laya",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.2",
|
|
4
4
|
"description": "Self-hosted Laya typed decision adapter and Python reference service for Plumbus.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -43,10 +43,10 @@
|
|
|
43
43
|
"@types/node": "^25.4.0",
|
|
44
44
|
"typescript": "^5.7.0",
|
|
45
45
|
"vitest": "^3.2.4",
|
|
46
|
-
"@plumbus/core": "0.7.
|
|
46
|
+
"@plumbus/core": "0.7.4"
|
|
47
47
|
},
|
|
48
48
|
"dependencies": {
|
|
49
|
-
"@plumbus/ai-decision": "~0.2.
|
|
49
|
+
"@plumbus/ai-decision": "~0.2.2"
|
|
50
50
|
},
|
|
51
51
|
"scripts": {
|
|
52
52
|
"build": "tsc -b",
|
|
@@ -93,6 +93,14 @@ class ServiceRoundTwo(unittest.TestCase):
|
|
|
93
93
|
return original(body)
|
|
94
94
|
backend.predict = wait
|
|
95
95
|
server = create_server(("127.0.0.1", 0), backend, "test-key", max_connections=1)
|
|
96
|
+
released = threading.Event()
|
|
97
|
+
process_request_thread = server.process_request_thread
|
|
98
|
+
def observe_handler_exit(*args):
|
|
99
|
+
try:
|
|
100
|
+
process_request_thread(*args)
|
|
101
|
+
finally:
|
|
102
|
+
released.set()
|
|
103
|
+
server.process_request_thread = observe_handler_exit
|
|
96
104
|
thread = threading.Thread(target=lambda: server.serve_forever(poll_interval=0.01), daemon=True)
|
|
97
105
|
results = []
|
|
98
106
|
first = threading.Thread(target=lambda: results.append(self.send(address=server.server_address)[0]))
|
|
@@ -104,6 +112,8 @@ class ServiceRoundTwo(unittest.TestCase):
|
|
|
104
112
|
finish.set()
|
|
105
113
|
first.join(2)
|
|
106
114
|
self.assertEqual(results, [200])
|
|
115
|
+
# Receiving the body can precede the server thread's semaphore release.
|
|
116
|
+
self.assertTrue(released.wait(2))
|
|
107
117
|
self.assertEqual(self.send(address=server.server_address)[0], 200)
|
|
108
118
|
finally:
|
|
109
119
|
finish.set()
|