Skip to content
English

defineArtifactCatalog ​

KIT · function · 运行时值

Compile a catalog of artifact resolvers plus a checked resolve entry point.

导入 ​

ts
import { defineArtifactCatalog } from '@jangts/agent-core/kits/artifacts';
js
import { defineArtifactCatalog } from '@jangts/agent-core/kits/artifacts';

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

定义 ​

泛型参数 ​

类型参数约束默认类型
Value——
App——

调用 ​

ts
defineArtifactCatalog<Value, App>(definitions: readonly ArtifactResolver<Value, App>[]): Readonly<{ resolve: (reference: ArtifactReference, context: OperationContext<App>) => Promise<Value>; definitions: readonly Readonly<{ id: string; resolve: (reference: ArtifactReference, context: OperationContext<App>) => MaybePromise<Value>; }>[]; ids(): readonly string[]; values(): IterableIterator<Readonly<{ id: string; resolve: (reference: ArtifactReference, context: OperationContext<App>) => MaybePromise<Value>; }>>; entries(): IterableIterator<readonly [string, Readonly<{ id: string; resolve: (reference: ArtifactReference, context: OperationContext<App>) => MaybePromise<Value>; }>]>; has(id: string): boolean; get(id: string): Readonly<{ id: string; resolve: (reference: ArtifactReference, context: OperationContext<App>) => MaybePromise<Value>; }> | undefined; require(id: string): Readonly<{ id: string; resolve: (reference: ArtifactReference, context: OperationContext<App>) => MaybePromise<Value>; }>; }>

Compile a catalog of artifact resolvers plus a checked resolve entry point.

类型参数约束默认类型
Value——
App——
参数类型传递说明
definitionsreadonly ArtifactResolver<Value, App>[]必需—

返回: Readonly<{ resolve: (reference: ArtifactReference, context: OperationContext<App>) => Promise<Value>; definitions: readonly Readonly<{ id: string; resolve: (reference: ArtifactReference, context: OperationContext<App>) => MaybePromise<Value>; }>[]; ids(): readonly string[]; values(): IterableIterator<Readonly<{ id: string; resolve: (reference: ArtifactReference, context: OperationContext<App>) => MaybePromise<Value>; }>>; entries(): IterableIterator<readonly [string, Readonly<{ id: string; resolve: (reference: ArtifactReference, context: OperationContext<App>) => MaybePromise<Value>; }>]>; has(id: string): boolean; get(id: string): Readonly<{ id: string; resolve: (reference: ArtifactReference, context: OperationContext<App>) => MaybePromise<Value>; }> \| undefined; require(id: string): Readonly<{ id: string; resolve: (reference: ArtifactReference, context: OperationContext<App>) => MaybePromise<Value>; }>; }>。

返回值结构
字段类型必需说明
resolve(reference: ArtifactReference, context: OperationContext<App>) => Promise<Value>必需见类型定义及调用场景。
definitionsreadonly Readonly<{ id: string; resolve: (reference: ArtifactReference, context: OperationContext<App>) => MaybePromise<Value>; }>[]必需只读。Definitions in registration order; each entry is frozen by the implementation.
ids() => readonly string[]必需Ids in the same order as definitions.
values() => IterableIterator<Readonly<{ id: string; resolve: (reference: ArtifactReference, context: OperationContext<App>) => MaybePromise<Value>; }>>必需见类型定义及调用场景。
entries() => IterableIterator<readonly [string, Readonly<{ id: string; resolve: (reference: ArtifactReference, context: OperationContext<App>) => MaybePromise<Value>; }>]>必需见类型定义及调用场景。
has(id: string) => boolean必需见类型定义及调用场景。
get(id: string) => Readonly<{ id: string; resolve: (reference: ArtifactReference, context: OperationContext<App>) => MaybePromise<Value>; }> | undefined必需Returns the definition or undefined; the caller decides whether absence is an error.
require(id: string) => Readonly<{ id: string; resolve: (reference: ArtifactReference, context: OperationContext<App>) => MaybePromise<Value>; }>必需Returns the definition or throws; use when the id is a required reference.

使用示例 ​

先解析一个明确的内存产物 ​

入门 · Node.js

ts
import { Artifacts } from '@jangts/agent-core/kits/artifacts';
const content = new Map([['manual', { text: 'Agent Core 使用说明', revision: '1' }]]);
const artifacts = Artifacts.defineArtifactCatalog([
  { id: 'document', resolve(reference) { const value = content.get(reference.artifactId); if (!value) throw new Error('产物不存在'); return value; } },
]);
const result = await artifacts.resolve({ resolverId: 'document', artifactId: 'manual' }, { app: {}, signal: new AbortController().signal });
console.log(result.text);
js
import { Artifacts } from '@jangts/agent-core/kits/artifacts';
const content = new Map([['manual', { text: 'Agent Core 使用说明', revision: '1' }]]);
const artifacts = Artifacts.defineArtifactCatalog([
    { id: 'document', resolve(reference) { const value = content.get(reference.artifactId); if (!value)
            throw new Error('产物不存在'); return value; } },
]);
const result = await artifacts.resolve({ resolverId: 'document', artifactId: 'manual' }, { app: {}, signal: new AbortController().signal });
console.log(result.text);

相同 resolve 增加真实权限与版本核对 ​

配置 · Node.js

ts
import { Artifacts } from '@jangts/agent-core/kits/artifacts';
type Value = { text: string; revision: string };
type App = { canRead: boolean; documents: Map<string, Value> };
const artifacts = Artifacts.defineArtifactCatalog<Value, App>([
  { id: 'document', async resolve(reference, { app, signal }) {
    signal.throwIfAborted();
    if (!app.canRead) throw new Error('没有产物读取权限');
    const value = app.documents.get(reference.artifactId);
    if (!value) throw new Error('产物不存在或已到期');
    if (reference.revision && reference.revision !== value.revision) throw new Error('产物版本已变化');
    return structuredClone(value);
  } },
]);
const app = { canRead: true, documents: new Map([['manual', { text: 'Agent Core 使用说明', revision: '1' }]]) };
console.log(await artifacts.resolve({ resolverId: 'document', artifactId: 'manual', revision: '1' }, { app, signal: new AbortController().signal }));
js
import { Artifacts } from '@jangts/agent-core/kits/artifacts';
const artifacts = Artifacts.defineArtifactCatalog([
    { id: 'document', async resolve(reference, { app, signal }) {
            signal.throwIfAborted();
            if (!app.canRead)
                throw new Error('没有产物读取权限');
            const value = app.documents.get(reference.artifactId);
            if (!value)
                throw new Error('产物不存在或已到期');
            if (reference.revision && reference.revision !== value.revision)
                throw new Error('产物版本已变化');
            return structuredClone(value);
        } },
]);
const app = { canRead: true, documents: new Map([['manual', { text: 'Agent Core 使用说明', revision: '1' }]]) };
console.log(await artifacts.resolve({ resolverId: 'document', artifactId: 'manual', revision: '1' }, { app, signal: new AbortController().signal }));

相关策略源码 ​

相关定义 ​

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