# Knowledge packs (/en/docs/js/knowledge)

Building, querying, merging and chart-scoped subsets of a KnowledgePack, and readings inside the text.



A knowledge pack is an open format mapping language-independent keys to reading texts and school attributes;
see [Knowledge packs](/en/docs/guide/guides/knowledge-pack) and `knowledge/SCHEMA.md` in the repository. Merging
happens in the core, with the same rules in every binding.

## KnowledgePack [#knowledgepack]

| Member                                                                                | Description                                                                                                                                                                                                                                                                                                           |
| ------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `KnowledgePack.builtin(language = 'zh-CN')`                                           | The built-in pack (from iztro-docs, MIT); throws `IztroError` if that language has none                                                                                                                                                                                                                               |
| `KnowledgePack.fromObject(obj)` / `fromJSON(text)`                                    | Your own pack; checked by the `check` rules, any error-level issue (bad JSON included) throws `IztroError` (`issues` holds the full list); the pack holds the core's normalized copy (fields outside the spec dropped, `null` values in maps left out), so all four languages build the same pack from the same input |
| `KnowledgePack.check(raw, { base? })`                                                 | Checks issue by issue and returns every issue at once, see below                                                                                                                                                                                                                                                      |
| `toJSON()`                                                                            | A deep copy of the pack object; `JSON.stringify(pack)` gives the pack's JSON                                                                                                                                                                                                                                          |
| `merged(...overlays)`                                                                 | This pack with overlays (instances, or plain objects checked through `fromObject`) applied in order, as a new pack                                                                                                                                                                                                    |
| `forAstrolabe(chart, config?)` / `forHoroscope(horoscope, config?)`                   | Chart-scoped subsets                                                                                                                                                                                                                                                                                                  |
| `schema` `id` `version` `language` `extends` `source`                                 | Metadata                                                                                                                                                                                                                                                                                                              |
| `star(key)` `pattern(key)` `palace(key)` `mutagen(key)` `concept(slug)` `topic(slug)` | One entry, `undefined` when missing                                                                                                                                                                                                                                                                                   |
| `stars()` `patterns()` `topics()`                                                     | All entries; topics in display order (ascending `order`, those without one last, ties and those without one by slug)                                                                                                                                                                                                  |
| `starIntro(key)` / `patternIntro(key)`                                                | The reading text                                                                                                                                                                                                                                                                                                      |

Entry types: `StarEntry` (`attributes`, `combinations` and `combinationSummaries` default to empty objects),
`PatternEntry`, `TextEntry` (palaces, mutagens), `ConceptEntry`, `TopicEntry` (`surround` and `palaces` drive the
palace selection, `order` is the display order).

```ts
const pack = KnowledgePack.builtin();
console.log(pack.id, pack.version, pack.language, pack.source.license, pack.stars().length, pack.patterns().length,
  pack.topics().map((t) => t.slug).join(','));
const ziwei = pack.star(MajorStar.ZIWEI)!;
console.log(ziwei.name, ziwei.category, ziwei.attributes.chemistry, ziwei.summary);
```

```text
iztro-docs 2026-08-19+ec2d58b zh-CN MIT 162 64 overall,personality,career,wealth,romance,health,parents,children,siblings,friends,property,travel,spirit
紫微 major 尊贵 帝王星，有王者特质：控制欲强、重体面、好为人师、大局观强；得资源能控场规划，否则眼高手低，忌被捧杀。
```

## check [#check]

Checks a pack issue by issue and returns every issue at once. The rules, levels and all issue codes are in
[Knowledge packs · Checking a pack](/en/docs/guide/guides/knowledge-pack#checking-a-pack).

```ts
KnowledgePack.check(raw: unknown, options?: { base?: KnowledgePack }): KnowledgeIssue[];

interface KnowledgeIssue {
  level: 'error' | 'warning';
  path: (string | number)[];      // object keys as strings, array indexes as numbers
  code: KnowledgeIssueCode;       // e.g. KnowledgeIssueCode.UNKNOWN_KEY ('unknown_key')
  params: Record<string, unknown>;
  message: string;                // English description with the dotted path
}
```

A `raw` that is not an object (including `undefined`, `null` and arrays) comes back as a `not_object` issue; a `raw`
holding `NaN` / `±Infinity` throws `IztroError` (`invalid_argument`: JSON cannot represent them, and they are not
silently turned into `null`); a `raw` holding a BigInt or a cycle, or a `base` not built by this library, throws `TypeError`. The `IztroError` that
`fromObject` throws on rejection carries `issues`.

```js
const raw = JSON.parse(`{"schema": 1, "id": "my-school", "version": "1", "language": "zh-CN", "extends": "iztro-docs",
 "stars": {"ziweiMjj": {"intro": "本门认为……"}, "tianjiMaj": {"intor": "……"}}}`);
for (const issue of KnowledgePack.check(raw, { base: KnowledgePack.builtin() })) {
  console.log(issue.level, issue.message);
}
```

```text
warning stars.tianjiMaj.intor: unknown field, dropped when parsed (did you mean 'intro'?)
error stars.ziweiMjj: unknown key 'ziweiMjj' (did you mean 'ziweiMaj'?)
```

## merged [#merged]

An overlay's non-empty fields replace the matching fields of the same entry; `attributes` and `combinations`
merge field by field, arrays are replaced whole. When an overlay changes a text without supplying its summary,
the merged pack drops the summary of the old text.

```ts
const mine = pack.merged({
  schema: 1, id: 'mine', version: '1', language: 'zh-CN', extends: 'iztro-docs',
  stars: { ziweiMaj: { intro: '我的紫微释义' } },
});
console.log(mine.id, mine.starIntro(MajorStar.ZIWEI), mine.star(MajorStar.ZIWEI)!.summary);
```

```text
mine 我的紫微释义 undefined
```

## forAstrolabe / forHoroscope [#forastrolabe--forhoroscope]

The natal subset holds only the stars on the chart (major, minor and adjective stars plus the four
twelve-spirit cycles; same-palace major-star pairings are kept), the patterns that hit under the given settings,
and the four mutagens — no palaces or glossary. The horoscope subset adds each layer's horoscope stars and
patterns, down to the horoscope's query precision. Both are ordinary packs.

```js
const sub = pack.forAstrolabe(chart);
console.log(sub.stars().length, sub.patterns().map((e) => e.name).join(','));
```

```text
108 府相朝垣
```

## Readings inside the text [#readings-inside-the-text]

Every `toText` takes `knowledge` and `knowledgeDepth`: `knowledge: true` uses the built-in pack of the chart
language, or pass a `KnowledgePack` (your own or a merged one); `knowledgeDepth: 'summary'` uses the summary
layer and fails, listing what is missing, when a long text has no summary. A user's own pack works straight in
the browser's chart text, without going through any server.

```js
console.log(chart.toText({ knowledge: true, knowledgeDepth: KnowledgeDepth.SUMMARY }).length,
  chart.toText({ knowledge: mine }).includes('我的紫微释义'));
```

```text
8385 true
```
