vet-sdk-core-ts 0.4.39 → 0.4.40

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 CHANGED
@@ -228,6 +228,16 @@ The catalogue is a governed starter set, not a closed biological universe.
228
228
  Callers may retain another verified positive non-human NCBI Taxonomy identifier.
229
229
  This matters because animal keeping varies by jurisdiction and taxonomy evolves.
230
230
 
231
+ Species-language resolution is channel-neutral. Voice, WhatsApp, portal chat
232
+ and portal speech call `resolveVetChainSpeciesAlias` with the same localized
233
+ lexicon. It performs exact normalized matching only. If that fast path misses,
234
+ a server adapter implements `VetChainSpeciesTerminologyResolver` and sends text
235
+ plus locale to the authenticated terminology service. The service returns a
236
+ closed NCBI candidate set, which must pass
237
+ `validateVetChainSpeciesTerminologyCandidates`; a model such as Gemma may rank
238
+ that set but cannot add codes. `selectVetChainSpeciesTerminologyCandidate`
239
+ accepts only a code actually returned for user review and confirmation.
240
+
231
241
  Current cards are issued with `issueVetChainAnimalCard`: five jurisdiction
232
242
  digits plus `animalNumericId15 + checkDigit1`, and the matching
233
243
  `did:web:{host}:card:vetchain:{jurisdiction5}:{animal16}`. Species is separate
@@ -238,10 +248,16 @@ being repeated in the 21 printed digits.
238
248
  import {
239
249
  VetChainDomesticAnimalSpecies,
240
250
  findVetChainSpeciesByTaxonomyId,
251
+ resolveVetChainSpeciesAlias,
241
252
  } from "vet-sdk-core-ts/species";
242
253
 
243
254
  VetChainDomesticAnimalSpecies.DomesticFerret.ncbiTaxonomyId; // "9669"
244
255
  findVetChainSpeciesByTaxonomyId("9685")?.key; // "Cat"
256
+ resolveVetChainSpeciesAlias({
257
+ text: "Dog.",
258
+ locale: "en-CA",
259
+ lexicon: [{ ncbiTaxonomyId: "9615", acceptedTexts: ["dog", "it is a dog"] }],
260
+ })?.ncbiTaxonomyId; // "9615"
245
261
  ```
246
262
 
247
263
  ## Digital-twin search
package/dist/species.d.ts CHANGED
@@ -9,6 +9,27 @@ export type VetChainSpeciesDefinition = Readonly<{
9
9
  scientificName: string;
10
10
  group: VetChainAnimalGroup;
11
11
  }>;
12
+ export declare const VETCHAIN_NCBI_TAXONOMY_CODE_SYSTEM: "https://www.ncbi.nlm.nih.gov/Taxonomy";
13
+ /** Localized exact texts supplied by a VetChain product, independently of its channel adapter. */
14
+ export type VetChainSpeciesLexiconEntry = Readonly<{
15
+ ncbiTaxonomyId: NcbiTaxonomyId;
16
+ acceptedTexts: readonly string[];
17
+ }>;
18
+ /** One reviewable result returned by the authenticated terminology service. */
19
+ export type VetChainSpeciesTerminologyCandidate = Readonly<{
20
+ codeSystem: typeof VETCHAIN_NCBI_TAXONOMY_CODE_SYSTEM;
21
+ codeValue: NcbiTaxonomyId;
22
+ display: string;
23
+ score?: number;
24
+ }>;
25
+ /** Channel-neutral boundary implemented by a server-side VetChain terminology adapter. */
26
+ export interface VetChainSpeciesTerminologyResolver {
27
+ search(input: Readonly<{
28
+ text: string;
29
+ locale: string;
30
+ maxCandidates: number;
31
+ }>): Promise<readonly VetChainSpeciesTerminologyCandidate[]>;
32
+ }
12
33
  /**
13
34
  * Governed starter catalogue for domesticated and commonly kept animals.
14
35
  *
@@ -482,3 +503,27 @@ export declare const VetChainDomesticAnimalSpeciesList: readonly Readonly<{
482
503
  export declare function findVetChainSpeciesByTaxonomyId(value: string): VetChainSpeciesDefinition | undefined;
483
504
  /** The current printed card layout can embed only NCBI Taxonomy IDs of at most five digits. */
484
505
  export declare function isVetChainSpeciesCode5Compatible(value: string): boolean;
506
+ /** Normalizes human text consistently before an exact localized alias lookup. */
507
+ export declare function normalizeVetChainSpeciesText(value: string, locale?: string): string;
508
+ /**
509
+ * Resolves only an exact normalized text configured by the product locale.
510
+ *
511
+ * Voice, WhatsApp, portal chat and portal speech must call this same helper.
512
+ * Unmatched free text belongs at the terminology boundary, not in an LLM-only
513
+ * inference path.
514
+ */
515
+ export declare function resolveVetChainSpeciesAlias(input: Readonly<{
516
+ text: string;
517
+ locale: string;
518
+ lexicon: readonly VetChainSpeciesLexiconEntry[];
519
+ }>): Readonly<{
520
+ ncbiTaxonomyId: NcbiTaxonomyId;
521
+ matchedText: string;
522
+ }> | undefined;
523
+ /** Validates the closed candidate set before it can be presented or ranked. */
524
+ export declare function validateVetChainSpeciesTerminologyCandidates(values: readonly VetChainSpeciesTerminologyCandidate[]): readonly VetChainSpeciesTerminologyCandidate[];
525
+ /**
526
+ * Selects only a code in the terminology service's reviewed candidate set.
527
+ * A Gemma/ranking service may reorder this set but cannot add a code.
528
+ */
529
+ export declare function selectVetChainSpeciesTerminologyCandidate(candidates: readonly VetChainSpeciesTerminologyCandidate[], selectedCodeValue: string): VetChainSpeciesTerminologyCandidate;
package/dist/species.js CHANGED
@@ -1,4 +1,5 @@
1
1
  // Copyright 2026 Connecting Solution & Applications Ltd under the Apache License, Version 2.0.
2
+ export const VETCHAIN_NCBI_TAXONOMY_CODE_SYSTEM = "https://www.ncbi.nlm.nih.gov/Taxonomy";
2
3
  function species(key, ncbiTaxonomyId, scientificName, group) {
3
4
  if (!/^[1-9]\d*$/.test(ncbiTaxonomyId) || ncbiTaxonomyId === "9606") {
4
5
  throw new TypeError("A species requires a positive, non-human NCBI Taxonomy identifier.");
@@ -74,3 +75,71 @@ export function findVetChainSpeciesByTaxonomyId(value) {
74
75
  export function isVetChainSpeciesCode5Compatible(value) {
75
76
  return /^[1-9]\d{0,4}$/.test(value) && value !== "9606";
76
77
  }
78
+ /** Normalizes human text consistently before an exact localized alias lookup. */
79
+ export function normalizeVetChainSpeciesText(value, locale = "en") {
80
+ const normalizedLocale = String(locale || "en").trim() || "en";
81
+ return String(value || "")
82
+ .normalize("NFD")
83
+ .replace(/[\u0300-\u036f]/g, "")
84
+ .toLocaleLowerCase(normalizedLocale)
85
+ .replace(/[^\p{L}\p{N}\s]/gu, " ")
86
+ .replace(/\s+/g, " ")
87
+ .trim();
88
+ }
89
+ /**
90
+ * Resolves only an exact normalized text configured by the product locale.
91
+ *
92
+ * Voice, WhatsApp, portal chat and portal speech must call this same helper.
93
+ * Unmatched free text belongs at the terminology boundary, not in an LLM-only
94
+ * inference path.
95
+ */
96
+ export function resolveVetChainSpeciesAlias(input) {
97
+ const normalizedInput = normalizeVetChainSpeciesText(input.text, input.locale);
98
+ if (!normalizedInput)
99
+ return undefined;
100
+ for (const entry of input.lexicon) {
101
+ assertNonHumanTaxonomyId(entry.ncbiTaxonomyId);
102
+ if (entry.acceptedTexts.some((text) => normalizeVetChainSpeciesText(text, input.locale) === normalizedInput)) {
103
+ return Object.freeze({ ncbiTaxonomyId: entry.ncbiTaxonomyId, matchedText: normalizedInput });
104
+ }
105
+ }
106
+ return undefined;
107
+ }
108
+ /** Validates the closed candidate set before it can be presented or ranked. */
109
+ export function validateVetChainSpeciesTerminologyCandidates(values) {
110
+ if (!Array.isArray(values) || values.length === 0 || values.length > 20) {
111
+ throw new TypeError("Species terminology must return between one and twenty candidates.");
112
+ }
113
+ const seen = new Set();
114
+ return Object.freeze(values.map((candidate) => {
115
+ if (candidate.codeSystem !== VETCHAIN_NCBI_TAXONOMY_CODE_SYSTEM) {
116
+ throw new TypeError("Species terminology candidates must use the canonical NCBI Taxonomy code system.");
117
+ }
118
+ assertNonHumanTaxonomyId(candidate.codeValue);
119
+ if (!candidate.display.trim())
120
+ throw new TypeError("A species terminology candidate requires a display label.");
121
+ if (seen.has(candidate.codeValue))
122
+ throw new TypeError("Species terminology candidates must be unique.");
123
+ if (candidate.score !== undefined && (!Number.isFinite(candidate.score) || candidate.score < 0 || candidate.score > 1)) {
124
+ throw new TypeError("A species terminology candidate score must be between zero and one.");
125
+ }
126
+ seen.add(candidate.codeValue);
127
+ return Object.freeze({ ...candidate, display: candidate.display.trim() });
128
+ }));
129
+ }
130
+ /**
131
+ * Selects only a code in the terminology service's reviewed candidate set.
132
+ * A Gemma/ranking service may reorder this set but cannot add a code.
133
+ */
134
+ export function selectVetChainSpeciesTerminologyCandidate(candidates, selectedCodeValue) {
135
+ const validated = validateVetChainSpeciesTerminologyCandidates(candidates);
136
+ const selected = validated.find((candidate) => candidate.codeValue === selectedCodeValue.trim());
137
+ if (!selected)
138
+ throw new TypeError("The selected species was not returned by the terminology service.");
139
+ return selected;
140
+ }
141
+ function assertNonHumanTaxonomyId(value) {
142
+ if (!/^[1-9]\d*$/.test(value) || value === "9606") {
143
+ throw new TypeError("A species requires a positive, non-human NCBI Taxonomy identifier.");
144
+ }
145
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vet-sdk-core-ts",
3
- "version": "0.4.39",
3
+ "version": "0.4.40",
4
4
  "description": "Browser-safe VetChain core contracts and governed animal species identifiers",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Connecting Solution & Applications Ltd",