Skip to content
English

ScopeRegistry ​

KIT · class · 运行时值

Generic scope relationships; categories, intent policy and extra fields belong to the host.

A scope is addressable by its id or by any of its aliases; aliases resolve to the declaring scope, and a name may be claimed only once across both namespaces.

导入 ​

ts
import { ScopeRegistry } from '@jangts/agent-core/kits/domains';
js
import { ScopeRegistry } from '@jangts/agent-core/kits/domains';

其他公开入口:@jangts/agent-core、@jangts/agent-core/types、@jangts/agent-core/kits/domains、@jangts/agent-core/kits。

定义 ​

ts
class ScopeRegistry<Definition>

泛型参数 ​

类型参数约束默认类型
DefinitionScopeDefinitionScopeDefinition

构造函数 ​

ts
new ScopeRegistry<Definition extends ScopeDefinition = ScopeDefinition>(policy?: ScopeRegistryPolicy<Definition>): ScopeRegistry<Definition>
参数类型传递说明
policyScopeRegistryPolicy<Definition>可省略—
policy 的字段
字段类型必需说明
requireGoalsboolean | undefined可省略只读。Require at least one Tool id per scope.
requireSourcesboolean | undefined可省略只读。Require at least one query scope or knowledge topic per scope.
requireToolsboolean | undefined可省略只读。Require at least one tool id per scope.
idPatternRegExp | undefined可省略只读。Pattern every id and alias must match.
categoriesreadonly string[] | undefined可省略只读。Allowed category values; undefined accepts any value.
assertDefinition((definition: Definition) => void) | undefined可省略只读。Extra App assertion, called after the built-in checks; must return void synchronously.
acceptsTool((definition: Readonly<Definition>, goal: string) => boolean) | undefined可省略只读。Replaces the default intent membership test; must return boolean synchronously.
tagsreadonly string[] | undefined可省略只读。Diagnostic tags attached to registration rejections.
errors{ readonly sealed?: BreakReason; readonly invalid?: BreakReason; } | undefined可省略只读。见类型定义及调用场景。

返回: ScopeRegistry<Definition>。

实例属性 ​

字段类型必需说明
definitionsreadonly Readonly<Omit<Readonly<ScopeDefinition & RegistrationData<Definition> & Pick<Definition, Extract<keyof Definition, keyof ScopeDefinition>>>, "evidence" | ScopeListKey> & RegisteredScopeLists<Definition> & { evidence: ScopeEvidenceList<Definition>; }>[]必需只读。Registered scopes in registration order.

成员目录 ​

成员用途
create静态。Create and seal a complete scope catalog in one call.
registerValidate, freeze and add one scope together with its aliases. Every name is checked for conflicts before any of them is stored, so a rejected scope leaves no partial alias entry behind.
registerAllRegister every scope in iteration order.
sealFinalize the registry; no registration is accepted afterwards.
idsRegistered scope ids in registration order; aliases are not included.
valuesIterate the registered scopes.
entriesIterate [id, scope] pairs.
requireRegistered scope for an id or alias.
normalizeResolve an alias to its scope id; unknown names are returned unchanged.
getRegistered scope for an id or alias, or undefined for a null/unknown name.
hasWhether the name resolves to a registered scope.
acceptsToolWhether a scope accepts an intent. The App may override the declared goalIds through acceptsTool; without an override the membership check applies.
acceptsQueryWhether the scope declares this query scope; a null scope accepts nothing.
isWhether the scope belongs to the given App category.
queryScopeIdsEvery query scope declared by any registered scope, deduplicated.
topicsKnowledge topic ids of one scope; copied so callers cannot mutate the registry.
evidenceEvidence declarations of one scope; copied so callers cannot mutate the registry.
contextTierDeclared context tier of one scope, if any.
describeCopy the scopes that accept at least one of the given goals.

成员详情 ​

create ​

静态成员。

Create and seal a complete scope catalog in one call.

ts
ScopeRegistry.create<Definition extends ScopeDefinition>(definitions: Iterable<Definition>, policy?: ScopeRegistryPolicy<Definition>): ScopeRegistry<Definition>

Create and seal a complete catalog; use the constructor for incremental installation.

类型参数约束默认类型
DefinitionScopeDefinition—
参数类型传递说明
definitionsIterable<Definition>必需—
policyScopeRegistryPolicy<Definition>可省略—
policy 的字段
字段类型必需说明
requireGoalsboolean | undefined可省略只读。Require at least one Tool id per scope.
requireSourcesboolean | undefined可省略只读。Require at least one query scope or knowledge topic per scope.
requireToolsboolean | undefined可省略只读。Require at least one tool id per scope.
idPatternRegExp | undefined可省略只读。Pattern every id and alias must match.
categoriesreadonly string[] | undefined可省略只读。Allowed category values; undefined accepts any value.
assertDefinition((definition: Definition) => void) | undefined可省略只读。Extra App assertion, called after the built-in checks; must return void synchronously.
acceptsTool((definition: Readonly<Definition>, goal: string) => boolean) | undefined可省略只读。Replaces the default intent membership test; must return boolean synchronously.
tagsreadonly string[] | undefined可省略只读。Diagnostic tags attached to registration rejections.
errors{ readonly sealed?: BreakReason; readonly invalid?: BreakReason; } | undefined可省略只读。见类型定义及调用场景。

返回: ScopeRegistry<Definition>。

register ​

实例方法。

Validate, freeze and add one scope together with its aliases.

Every name is checked for conflicts before any of them is stored, so a rejected scope leaves no partial alias entry behind.

ts
register(input: Definition): ScopeRegistry<Definition>

Validate, freeze and add one scope together with its aliases.

Every name is checked for conflicts before any of them is stored, so a rejected scope leaves no partial alias entry behind.

参数类型传递说明
inputDefinition必需—

返回: ScopeRegistry<Definition>。

registerAll ​

实例方法。

Register every scope in iteration order.

ts
registerAll(definitions: Iterable<Definition>): ScopeRegistry<Definition>

Register every scope in iteration order.

参数类型传递说明
definitionsIterable<Definition>必需—

返回: ScopeRegistry<Definition>。

seal ​

实例方法。

Finalize the registry; no registration is accepted afterwards.

ts
seal(): ScopeRegistry<Definition>

Finalize the registry; no registration is accepted afterwards.

无调用参数。

返回: ScopeRegistry<Definition>。

ids ​

实例方法。

Registered scope ids in registration order; aliases are not included.

ts
ids(): readonly string[]

Registered scope ids in registration order; aliases are not included.

无调用参数。

返回: readonly string[]。

values ​

实例方法。

Iterate the registered scopes.

ts
values(): MapIterator<Readonly<Omit<Readonly<ScopeDefinition & RegistrationData<Definition> & Pick<Definition, Extract<keyof Definition, keyof ScopeDefinition>>>, "evidence" | ScopeListKey> & RegisteredScopeLists<Definition> & { evidence: ScopeEvidenceList<Definition>; }>>

Iterate the registered scopes.

无调用参数。

返回: MapIterator<Readonly<Omit<Readonly<ScopeDefinition & RegistrationData<Definition> & Pick<Definition, Extract<keyof Definition, keyof ScopeDefinition>>>, "evidence" \| ScopeListKey> & RegisteredScopeLists<Definition> & { evidence: ScopeEvidenceList<Definition>; }>>。

entries ​

实例方法。

Iterate [id, scope] pairs.

ts
entries(): MapIterator<[string, Readonly<Omit<Readonly<ScopeDefinition & RegistrationData<Definition> & Pick<Definition, Extract<keyof Definition, keyof ScopeDefinition>>>, "evidence" | ScopeListKey> & RegisteredScopeLists<Definition> & { evidence: ScopeEvidenceList<Definition>; }>]>

Iterate [id, scope] pairs.

无调用参数。

返回: MapIterator<[string, Readonly<Omit<Readonly<ScopeDefinition & RegistrationData<Definition> & Pick<Definition, Extract<keyof Definition, keyof ScopeDefinition>>>, "evidence" \| ScopeListKey> & RegisteredScopeLists<Definition> & { evidence: ScopeEvidenceList<Definition>; }>]>。

require ​

实例方法。

Registered scope for an id or alias.

ts
require(id: string): Readonly<Omit<Readonly<ScopeDefinition & RegistrationData<Definition> & Pick<Definition, Extract<keyof Definition, keyof ScopeDefinition>>>, "evidence" | ScopeListKey> & RegisteredScopeLists<Definition> & { evidence: ScopeEvidenceList<Definition>; }>

Registered scope for an id or alias.

参数类型传递说明
idstring必需—

返回: Readonly<Omit<Readonly<ScopeDefinition & RegistrationData<Definition> & Pick<Definition, Extract<keyof Definition, keyof ScopeDefinition>>>, "evidence" \| ScopeListKey> & RegisteredScopeLists<Definition> & { evidence: ScopeEvidenceList<Definition>; }>。

返回值结构
字段类型必需说明
id(Omit<Readonly<ScopeDefinition & RegistrationData<Definition> & Pick<Definition, Extract<keyof Definition, keyof ScopeDefinition>>>, "evidence" | ScopeListKey> & RegisteredScopeLists<Definition> & { evidence: ScopeEvidenceList<Definition>; })["id"]必需Unique scope id, matched against the policy's idPattern when configured.
description(Omit<Readonly<ScopeDefinition & RegistrationData<Definition> & Pick<Definition, Extract<keyof Definition, keyof ScopeDefinition>>>, "evidence" | ScopeListKey> & RegisteredScopeLists<Definition> & { evidence: ScopeEvidenceList<Definition>; })["description"]必需Non-blank description.
category(Omit<Readonly<ScopeDefinition & RegistrationData<Definition> & Pick<Definition, Extract<keyof Definition, keyof ScopeDefinition>>>, "evidence" | ScopeListKey> & RegisteredScopeLists<Definition> & { evidence: ScopeEvidenceList<Definition>; })["category"]必需Scope category; validated against policy.categories when that is set.
contextTier(Omit<Readonly<ScopeDefinition & RegistrationData<Definition> & Pick<Definition, Extract<keyof Definition, keyof ScopeDefinition>>>, "evidence" | ScopeListKey> & RegisteredScopeLists<Definition> & { evidence: ScopeEvidenceList<Definition>; })["contextTier"] | undefined可省略App-declared context tier, passed through for the App's own policy.
evidence(Omit<Readonly<ScopeDefinition & RegistrationData<Definition> & Pick<Definition, Extract<keyof Definition, keyof ScopeDefinition>>>, "evidence" | ScopeListKey> & RegisteredScopeLists<Definition> & { evidence: ScopeEvidenceList<Definition>; })["evidence"]必需见类型定义及调用场景。

normalize ​

实例方法。

Resolve an alias to its scope id; unknown names are returned unchanged.

ts
normalize(scope: string): string

Resolve an alias to its scope id; unknown names are returned unchanged.

参数类型传递说明
scopestring必需—

返回: string。

get ​

实例方法。

Registered scope for an id or alias, or undefined for a null/unknown name.

ts
get(scope?: string | null): RegisteredScope<Definition> | undefined

Registered scope for an id or alias, or undefined for a null/unknown name.

参数类型传递说明
scopestring | null | undefined可省略—

返回: Readonly<Omit<Readonly<ScopeDefinition & RegistrationData<Definition> & Pick<Definition, Extract<keyof Definition, keyof ScopeDefinition>>>, "evidence" \| ScopeListKey> & RegisteredScopeLists<Definition> & { evidence: ScopeEvidenceList<Definition>; }> \| undefined。

返回值的可选结构: 选择其中一种结构,各分支的字段要求分别列出。

分支 1

undefined

分支 2
字段类型必需说明
id(Omit<Readonly<ScopeDefinition & RegistrationData<Definition> & Pick<Definition, Extract<keyof Definition, keyof ScopeDefinition>>>, "evidence" | ScopeListKey> & RegisteredScopeLists<Definition> & { evidence: ScopeEvidenceList<Definition>; })["id"]必需Unique scope id, matched against the policy's idPattern when configured.
description(Omit<Readonly<ScopeDefinition & RegistrationData<Definition> & Pick<Definition, Extract<keyof Definition, keyof ScopeDefinition>>>, "evidence" | ScopeListKey> & RegisteredScopeLists<Definition> & { evidence: ScopeEvidenceList<Definition>; })["description"]必需Non-blank description.
category(Omit<Readonly<ScopeDefinition & RegistrationData<Definition> & Pick<Definition, Extract<keyof Definition, keyof ScopeDefinition>>>, "evidence" | ScopeListKey> & RegisteredScopeLists<Definition> & { evidence: ScopeEvidenceList<Definition>; })["category"]必需Scope category; validated against policy.categories when that is set.
contextTier(Omit<Readonly<ScopeDefinition & RegistrationData<Definition> & Pick<Definition, Extract<keyof Definition, keyof ScopeDefinition>>>, "evidence" | ScopeListKey> & RegisteredScopeLists<Definition> & { evidence: ScopeEvidenceList<Definition>; })["contextTier"] | undefined可省略App-declared context tier, passed through for the App's own policy.
evidence(Omit<Readonly<ScopeDefinition & RegistrationData<Definition> & Pick<Definition, Extract<keyof Definition, keyof ScopeDefinition>>>, "evidence" | ScopeListKey> & RegisteredScopeLists<Definition> & { evidence: ScopeEvidenceList<Definition>; })["evidence"]必需见类型定义及调用场景。

has ​

实例方法。

Whether the name resolves to a registered scope.

ts
has(scope?: string | null): boolean

Whether the name resolves to a registered scope.

参数类型传递说明
scopestring | null | undefined可省略—

返回: boolean。

acceptsTool ​

实例方法。

Whether a scope accepts an intent.

The App may override the declared goalIds through acceptsTool; without an override the membership check applies.

ts
acceptsTool(scope: string, goal: string): boolean

Whether a scope accepts an intent.

The App may override the declared goalIds through acceptsTool; without an override the membership check applies.

参数类型传递说明
scopestring必需—
goalstring必需—

返回: boolean。

acceptsQuery ​

实例方法。

Whether the scope declares this query scope; a null scope accepts nothing.

ts
acceptsQuery(scope: string | null, queryScope: string): boolean

Whether the scope declares this query scope; a null scope accepts nothing.

参数类型传递说明
scopestring | null必需—
queryScopestring必需—

返回: boolean。

is ​

实例方法。

Whether the scope belongs to the given App category.

ts
is(scope: string | null | undefined, category: Definition["category"]): boolean

Whether the scope belongs to the given App category.

参数类型传递说明
scopestring | null | undefined必需—
categoryDefinition["category"]必需—

返回: boolean。

queryScopeIds ​

实例方法。

Every query scope declared by any registered scope, deduplicated.

ts
queryScopeIds(): string[]

Every query scope declared by any registered scope, deduplicated.

无调用参数。

返回: string[]。

topics ​

实例方法。

Knowledge topic ids of one scope; copied so callers cannot mutate the registry.

ts
topics(scope?: string | null): string[]

Knowledge topic ids of one scope; copied so callers cannot mutate the registry.

参数类型传递说明
scopestring | null | undefined可省略—

返回: string[]。

evidence ​

实例方法。

Evidence declarations of one scope; copied so callers cannot mutate the registry.

ts
evidence(scope?: string | null): RegisteredScopeEvidence<Definition>[]

Evidence declarations of one scope; copied so callers cannot mutate the registry.

参数类型传递说明
scopestring | null | undefined可省略—

返回: Readonly<RegistrationData<NonNullable<Definition["evidence"]>[number]>>[]。

contextTier ​

实例方法。

Declared context tier of one scope, if any.

ts
contextTier(scope?: string | null): Definition["contextTier"] | undefined

Declared context tier of one scope, if any.

参数类型传递说明
scopestring | null | undefined可省略—

返回: Definition["contextTier"] \| undefined。

describe ​

实例方法。

Copy the scopes that accept at least one of the given goals.

ts
describe(goals?: readonly string[]): ScopeDescription<Definition>[]

Copy the scopes that accept at least one of the given goals.

参数类型传递说明
goalsreadonly string[] | undefined可省略—

返回: ScopeDescription<Definition>[]。

使用示例 ​

先登记一个接口说明 Tool ​

入门 · Node.js

ts
import { IntentRegistry, ScopeRegistry } from '@jangts/agent-core/kits/domains';
const tools = IntentRegistry.create([
  { id: 'explain', description: '根据资料解释 SDK', delegates: ['read', 'reply'], asIntent: true },
]);
console.log(tools.ids(), tools.allows('explain', 'read'));
const scopes = ScopeRegistry.create([
  { id: 'sdk', description: 'SDK 说明资料', category: 'knowledge' },
]);
console.log(scopes.require('sdk').knowledgeTopicIds);
js
import { IntentRegistry, ScopeRegistry } from '@jangts/agent-core/kits/domains';
const tools = IntentRegistry.create([
    { id: 'explain', description: '根据资料解释 SDK', delegates: ['read', 'reply'], asIntent: true },
]);
console.log(tools.ids(), tools.allows('explain', 'read'));
const scopes = ScopeRegistry.create([
    { id: 'sdk', description: 'SDK 说明资料', category: 'knowledge' },
]);
console.log(scopes.require('sdk').knowledgeTopicIds);

增加约束和范围关联,查询入口保持一致 ​

配置 · Node.js

ts
import { IntentRegistry, ScopeRegistry } from '@jangts/agent-core/kits/domains';
const tools = new IntentRegistry({ idPattern: /^[a-z-]+$/, delegates: ['read', 'reply'] })
  .registerAll([{ id: 'explain', description: '根据资料解释 SDK', delegates: ['read', 'reply'], asIntent: '用户希望了解 SDK 接口或用法。' }]).seal();
const scopes = ScopeRegistry.create([
  { id: 'sdk', description: 'SDK 说明资料', category: 'knowledge', aliases: ['agent-core'], goalIds: ['explain'], knowledgeTopicIds: ['manual'], toolIds: ['read-source'] },
], { categories: ['knowledge'], requireGoals: true, requireSources: true });
console.log(tools.describe(), scopes.normalize('agent-core'));
console.log(scopes.acceptsTool('agent-core', 'explain'), scopes.topics('sdk'));
js
import { IntentRegistry, ScopeRegistry } from '@jangts/agent-core/kits/domains';
const tools = new IntentRegistry({ idPattern: /^[a-z-]+$/, delegates: ['read', 'reply'] })
    .registerAll([{ id: 'explain', description: '根据资料解释 SDK', delegates: ['read', 'reply'], asIntent: '用户希望了解 SDK 接口或用法。' }]).seal();
const scopes = ScopeRegistry.create([
    { id: 'sdk', description: 'SDK 说明资料', category: 'knowledge', aliases: ['agent-core'], goalIds: ['explain'], knowledgeTopicIds: ['manual'], toolIds: ['read-source'] },
], { categories: ['knowledge'], requireGoals: true, requireSources: true });
console.log(tools.describe(), scopes.normalize('agent-core'));
console.log(scopes.acceptsTool('agent-core', 'explain'), scopes.topics('sdk'));

相关策略源码 ​

相关定义 ​

接口参考、使用示例与开发指南。