diff --git a/docs/adr/0149-ai-answers-business-questions-through-the-dataset-semantic-layer.md b/docs/adr/0149-ai-answers-business-questions-through-the-dataset-semantic-layer.md index 8ebae3485f6..2f796b3ad54 100644 --- a/docs/adr/0149-ai-answers-business-questions-through-the-dataset-semantic-layer.md +++ b/docs/adr/0149-ai-answers-business-questions-through-the-dataset-semantic-layer.md @@ -149,6 +149,16 @@ Both `DatasetMeasureSchema` and `DatasetDimensionSchema` gain two optional keys. The rule is stated once here and carried in three places: the tool descriptions (for MCP clients), cloud's planner (the product's enforcement), and the D5 evaluation (the measurement). 1. **Normalize** the question's candidate terms and every match target the same way: NFKC, case-fold, trim, collapse whitespace, fold `_` and `-` to a space. Match targets are a member's `name`, its `label` in every locale, and each `synonym`. Equality after normalization is a **hit**; a substring, a stem or an embedding neighbour is **not** a hit (it may be offered in rule 3's question, never acted on). + + > **Amendment note (2026-11-17, [#34870](https://github.com/objectstack-ai/objectstack/issues/34870) — PR #34928): "its `label` in every locale" means every locale of an inline label map, plus the one translation-bundle label resolved for the caller's locale; a word in another language hits only as an authored `synonym`.** + > Ruled by the maintainer on [#34720](https://github.com/objectstack-ai/objectstack/issues/34720), option text verbatim 「A+C 本语言标签+同义词」 (ruling record [`6946563869`](https://github.com/objectstack-ai/objectstack/issues/34720#issuecomment-6946563869)). This note qualifies the label target and changes no other word of rule 1: the normalization, equality as the only hit, and the `name` and `synonym` targets stand as written. + > + > - **An inline label map: every locale is a target.** A member whose `label` is authored as an inline locale map is matched on each locale's value, whatever locale the caller speaks, unless the bundle answers for that member (next point). + > - **A bundle label: one value, resolved for the caller's locale (option A).** A label the translation bundle carries (`datasets..dimensions..label`, `.measures..label`) is resolved for the caller's locale before any matching, along the resolver's locale chain (`packages/spec/src/system/i18n-resolver.ts#lookupDatasetMemberAttr`): first the requested locale, read through the platform locale ladder (`packages/spec/src/system/i18n-resolver.ts#resolveBundleLocale`: case, platform table language, base language, variant expansion), then the deployment's declared fallback locale (`packages/rest/src/meta-item-read-gate.ts#metaTranslateOptions`), which is skipped when the caller's locale is the one the labels are authored in. One value is served per member, and only that resolved value is a target; no other bundle value is. So a `zh-TW` caller against a bundle whose only Chinese locale is `zh-CN` is served, and matched on, the `zh-CN` label (variant expansion, no fallback declared), and a `fr` caller in a deployment authored in another language, whose declared fallback is `en`, is served, and matched on, the `en` label when the bundle has no French one. Where the bundle answers for a member, the resolved value is the label the member is served with: it takes the place of the authored label, a plain string or an inline map alike, so the authored label is then no target. The ruling records the consequence: under a translated locale, an English source label that is not the member's `name` misses. + > - **A word in another language is an authored synonym (option C).** Synonyms are never translated and match in every language at once ([D1](#d1--business-vocabulary-on-measures-and-dimensions-synonyms-and-description)'s i18n stance), so a word meant to hit across locales is written as a `synonym`. The `candidates` of the amendment note after rule 5 offer such a word for a question; they never make it a hit. + > - **The caller's locale, per transport.** The HTTP door's dataset catalogue (`packages/runtime/src/domains/mcp.ts#datasetToolMembers`) is localized by the `/meta` list translation (`packages/rest/src/meta-item-read-gate.ts#translateMetaList`, which applies `packages/spec/src/system/i18n-resolver.ts#translateDataset`) for the locale `packages/rest/src/meta-item-read-gate.ts#metaRequestLocale` reads from the request's `Accept-Language`, falling back to the i18n service's default locale. The stdio transport (`packages/mcp/src/stdio-data-bridge.ts#createStdioDataBridge`) carries no request: it runs the same translation for the i18n service's default locale, the workspace default its principal speaks. On either transport, a host whose i18n service cannot be resolved serves the catalogue untranslated, and the authored label alone is the target: a string, or every locale of an inline map. The one exception is on HTTP: an `Accept-Language` header still resolves a locale there, and a dataset that is a `.custom` pair's declared override can still take its pair's own translations (`packages/rest/src/meta-item-read-gate.ts#pairTranslationBundleOf`, read from the protocol, not from the i18n service). Any other door to the same executor, cloud's service-ai `ToolRegistry` ([D2](#d2--two-tools-one-definition-one-executor-two-transports)) among them, supplies the executor's `loadDatasets` itself (`packages/services/service-analytics/src/dataset-tools.ts#DatasetToolsDeps`) and owes the same translation step there. + > - **The matcher is unchanged.** `packages/services/service-analytics/src/dataset-term-matcher.ts#matchDatasetTerms` compares the label it is handed (a string, or each locale's value of an inline map); which bundle locale it sees is settled before it runs, by the transport's translation. Its doc comment says the same. + 2. **One hit → it wins.** The AI answers through `query_dataset` on that dataset and member, and renders the answer's source from the tool's `provenance` — 「指标:营收(数据集:销售分析)」 — never from its own recollection of what it asked. A measure's `description` is the sentence the AI uses to explain what the number includes. 3. **More than one hit** (two datasets, or two members, answer to the word) → the AI **asks**, listing each candidate's label, dataset label and description. It does not pick, and it does not silently prefer the first. (Snowflake's suggestions, Looker's clarifying question, Genie's follow-up.) 4. **No hit → fall back to the object query the AI runs today** (`query_records` / `aggregate_records`, or cloud's equivalents), and the answer's source says so in words — 「来源:对象 crm_opportunity 的 amount 字段合计(非指标口径)」 — so a reader can tell a governed number from an estimate. The posture of this rule is [M2](#m2--what-the-ai-does-when-no-dataset-covers-the-question), ruled A; the card's wording (「没有命中时,再退回按对象查询」) is that option. **Recommended practice, not a rule** (the analysis the ruling endorsed leaned toward it): when a listed dataset's `object` is the object the fallback query landed on, the answer adds one line naming that dataset and its measures — 「该对象已有数据集 X(指标:…),如需口径答案请换用这些术语」 — so the reader learns a governed path exists. It lives in cloud's planner prompt and in the tool description, is added after the object is chosen, needs no up-front object resolution, and changes nothing about the fallback itself. D5 reports how often the line fires. diff --git a/packages/services/service-analytics/src/dataset-term-matcher.ts b/packages/services/service-analytics/src/dataset-term-matcher.ts index f4e92f4503b..c5de6a835cf 100644 --- a/packages/services/service-analytics/src/dataset-term-matcher.ts +++ b/packages/services/service-analytics/src/dataset-term-matcher.ts @@ -12,8 +12,19 @@ import { normalizeDatasetTerm } from '@objectstack/spec/ui'; * dataset schema's own vocabulary validation uses (NFKC, case-fold, trim, * collapse whitespace, `_` and `-` folded to a space) — and EQUALITY is a hit. * A substring, a stem or a near neighbour is not a hit. The match targets are - * the member's `name`, its `label` in every locale of an inline map, and each - * of its `synonyms`. + * the member's `name`, its `label` in every locale of an inline map, plus the + * one translation-bundle label resolved for the caller's locale, and each of + * its `synonyms`; a word in another language hits only as an authored synonym + * (ADR-0149 D3 rule 1's amendment note, ruled 「A+C 本语言标签+同义词」). + * + * This function never reads the bundle: the transport localizes the catalogue + * before it runs (the HTTP door for the request's locale, stdio for the i18n + * service's default), and where the bundle answers for a member that value is + * the `label` handed in, in place of the authored string or inline map. The + * value is resolved along the resolver's locale chain (the requested locale by + * the platform locale ladder, variant expansion included, then the + * deployment's declared fallback locale, skipped when the caller's locale is + * the authored one); no other bundle value is a target. * * Rules 2–5 (one hit wins, several hits ask, no hit falls back to the object * query labelled 「非指标口径」, a dimension hit alone is no answer) are the @@ -24,7 +35,10 @@ import { normalizeDatasetTerm } from '@objectstack/spec/ui'; /** A member's match targets, as the dataset declares them. */ export interface MatchableMember { readonly name: string; - /** A string, or an inline locale map (every locale is a target). */ + /** + * A string, or an inline locale map (every locale is a target); a bundle + * label arrives already resolved for the caller's locale, as a string. + */ readonly label?: unknown; readonly synonyms?: readonly unknown[]; }