@plumbus/ai-decision-typesafe 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.
@@ -1 +1 @@
1
- {"version":3,"file":"typesafe-adapter.d.ts","sourceRoot":"","sources":["../src/typesafe-adapter.ts"],"names":[],"mappings":"AAAA,OAAO,EAOL,KAAK,kBAAkB,EACvB,KAAK,uBAAuB,EAC7B,MAAM,sBAAsB,CAAC;AAG9B,MAAM,WAAW,6BACf,SAAQ,IAAI,CAAC,kBAAkB,EAAE,SAAS,GAAG,QAAQ,CAAC;IACtD,MAAM,EAAE,MAAM,CAAC;IACf,uDAAuD;IACvD,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,yEAAyE;IACzE,UAAU,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;CAC/C;AAED,4FAA4F;AAC5F,eAAO,MAAM,0BAA0B;;EAAyC,CAAC;AAEjF,kFAAkF;AAClF,wBAAgB,6BAA6B,CAC3C,MAAM,EAAE,6BAA6B,GACpC,uBAAuB,CA2DzB"}
1
+ {"version":3,"file":"typesafe-adapter.d.ts","sourceRoot":"","sources":["../src/typesafe-adapter.ts"],"names":[],"mappings":"AAAA,OAAO,EAOL,KAAK,kBAAkB,EACvB,KAAK,uBAAuB,EAC7B,MAAM,sBAAsB,CAAC;AAG9B,MAAM,WAAW,6BACf,SAAQ,IAAI,CAAC,kBAAkB,EAAE,SAAS,GAAG,QAAQ,CAAC;IACtD,MAAM,EAAE,MAAM,CAAC;IACf,uDAAuD;IACvD,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,yEAAyE;IACzE,UAAU,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;CAC/C;AAED,4FAA4F;AAC5F,eAAO,MAAM,0BAA0B;;EAAyC,CAAC;AAEjF,kFAAkF;AAClF,wBAAgB,6BAA6B,CAC3C,MAAM,EAAE,6BAA6B,GACpC,uBAAuB,CA+EzB"}
@@ -32,7 +32,27 @@ export function createTypeSafeDecisionAdapter(config) {
32
32
  questions: toSystemOneQuestions(input.questions),
33
33
  model: input.model ?? settings.data.model,
34
34
  }, input);
35
- const result = parseDecisionResponse(wire, input.questions, 'typesafe');
35
+ let result;
36
+ try {
37
+ result = parseDecisionResponse(wire, input.questions, 'typesafe');
38
+ }
39
+ catch (error) {
40
+ if (error instanceof DecisionProviderError && error.model && error.usage) {
41
+ const rate = Object.hasOwn(rates, error.model) ? rates[error.model] : undefined;
42
+ const cost = rate === undefined ? null : (error.usage.inputTokens / 1_000_000) * rate;
43
+ const validCost = cost !== null &&
44
+ Number.isFinite(cost) &&
45
+ (cost > 0 || error.usage.inputTokens === 0 || rate === 0)
46
+ ? cost
47
+ : null;
48
+ throw new DecisionProviderError('typesafe', error.kind, error.message, {
49
+ model: error.model,
50
+ usage: error.usage,
51
+ cost: validCost,
52
+ });
53
+ }
54
+ throw error;
55
+ }
36
56
  const rate = Object.hasOwn(rates, result.model) ? rates[result.model] : undefined;
37
57
  const cost = rate === undefined ? null : (result.usage.inputTokens / 1_000_000) * rate;
38
58
  if (cost !== null &&
@@ -1 +1 @@
1
- {"version":3,"file":"typesafe-adapter.js","sourceRoot":"","sources":["../src/typesafe-adapter.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,2BAA2B,EAC3B,qBAAqB,EACrB,kBAAkB,EAClB,qBAAqB,EACrB,oBAAoB,EACpB,uBAAuB,GAGxB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,CAAC,EAAE,MAAM,mBAAmB,CAAC;AAYtC,4FAA4F;AAC5F,MAAM,CAAC,MAAM,0BAA0B,GAAG,MAAM,CAAC,MAAM,CAAC,EAAE,YAAY,EAAE,KAAK,EAAE,CAAC,CAAC;AAEjF,kFA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AG,KAAK;aACrC,CAAC;QACJ,CAAC;KACF,CAAC;AACJ,CAAC"}
@@ -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
- Keep app business logic in Plumbus primitives and `ctx.*`. These packages do not
9
- add `ctx.ai.decide()` or automatic provider discovery yet. Never route model
10
- predictions directly around capability access checks or confirmation policy.
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: `createTypeSafeDecisionAdapter` — construct an explicitly conf
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
- ## Minimal usage
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 { createTypeSafeDecisionAdapter } from '@plumbus/ai-decision-typesafe';
@@ -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. Do not monkeypatch `ctx.ai`, register these as completion providers, or advertise `ctx.ai.decide()` as available.
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 cancellation/deadlines explicitly during this package-only stage.
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. There are no core CLI commands, environment discovery, or `plumbus init` instruction wiring for these packages yet.
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.
@@ -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-typesafe",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
4
4
  "description": "TypeSafe/Jev typed decision provider adapter for Plumbus.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -42,10 +42,10 @@
42
42
  "@types/node": "^25.4.0",
43
43
  "typescript": "^5.7.0",
44
44
  "vitest": "^3.2.4",
45
- "@plumbus/core": "0.7.2"
45
+ "@plumbus/core": "0.7.4"
46
46
  },
47
47
  "dependencies": {
48
- "@plumbus/ai-decision": "~0.2.0"
48
+ "@plumbus/ai-decision": "~0.2.2"
49
49
  },
50
50
  "scripts": {
51
51
  "build": "tsc -b",