Skip to content
中文

defineRetrieval ​

KIT · function · Runtime value

Compile a retrieval runner from its declared ports and effects.

Rejected queries stay in the caller's seen set, so a retry is reported as a duplicate instead of silently repeating the adapter call; only successful results increment the query count.

Import ​

ts
import { defineRetrieval } from '@jangts/agent-core/kits/retrieval';
js
import { defineRetrieval } from '@jangts/agent-core/kits/retrieval';

Other public entry points:@jangts/agent-core, @jangts/agent-core/types, @jangts/agent-core/kits/retrieval, @jangts/agent-core/kits.

Definition ​

Type parameters ​

Type parameterConstraintDefault type
Query——
SourceRetrievalSource—
ResultRetrievalResult<Source>—
App——

Call ​

ts
defineRetrieval<Query, Source extends RetrievalSource, Result extends RetrievalResult<Source>, App>(definition: RetrievalDefinition<Query, Source, Result, App>): RetrievalRunner<Query, Source, Result, import("../structs/common.js").OperationContext<App>>

Compile a retrieval runner from its declared ports and effects.

Rejected queries stay in the caller's seen set, so a retry is reported as a duplicate instead of silently repeating the adapter call; only successful results increment the query count.

Type parameterConstraintDefault type
Query——
SourceRetrievalSource—
ResultRetrievalResult<Source>—
App——
ParameterTypePassingDescription
definitionRetrievalDefinition<Query, Source, Result, App>Required—
Fields of definition
FieldTypeRequiredDescription
idstringRequiredRead-only. See the type definition and usage context.
descriptionstringRequiredRead-only. See the type definition and usage context.
tagsreadonly string[] | undefinedOptionalRead-only. See the type definition and usage context.
prepare((query: Query, context: OperationContext<App>) => Query) | undefinedOptionalRead-only. Normalizes the query before assertions run; must be synchronous.
assertInput(query: Query, context: OperationContext<App>) => voidRequiredRead-only. Rejects a malformed query; must be synchronous and return void.
state(context: OperationContext<App>) => RetrievalState<Source>RequiredRead-only. Returns the request-owned state that holds counters and sources.
limit(context: OperationContext<App>) => numberRequiredRead-only. Maximum accepted queries for the current state.
key((query: Query) => string) | undefinedOptionalRead-only. Deduplication key; defaults to a canonical serialization of the query.
errors{ readonly duplicate?: RetrievalReason<Query, OperationContext<App>> | undefined; readonly budget?: RetrievalReason<Query, OperationContext<App>> | undefined; } | undefinedOptionalRead-only. See the type definition and usage context.
authorize((query: Query, context: OperationContext<App>) => void) | undefinedOptionalRead-only. Resource authorization runs after duplicate/budget checks and before reserving a key.
execute(query: Query, context: OperationContext<App>) => ResultRequiredRead-only. Performs the actual read; must be synchronous. Throwing does not refund the query key.
assertResult((result: Result, query: Query, context: OperationContext<App>) => void) | undefinedOptionalRead-only. Validates the adapter result; a throw leaves the query counted as seen but not successful.
sourceId((source: Source, query: Query, sequence: number, context: OperationContext<App>) => string) | undefinedOptionalRead-only. Names a source; the assigned value replaces sourceId in the receipt result.
effectsreadonly RetrievalEffect<Query, Source, Result, OperationContext<App>>[] | undefinedOptionalRead-only. See the type definition and usage context.
now(() => number) | undefinedOptionalRead-only. Clock override for receipt timestamps; defaults to Date.now.

Returns: RetrievalRunner<Query, Source, Result, OperationContext<App>>.

Return value structure
FieldTypeRequiredDescription
idstringRequiredRead-only. See the type definition and usage context.
descriptionstringRequiredRead-only. See the type definition and usage context.
effectsreadonly RetrievalEffect<Query, Source, Result, OperationContext<App>>[]RequiredRead-only. Effects that run after every accepted query.
run(input: Query, context: OperationContext<App>) => RetrievalReceipt<Query, Source, Result>RequiredExecutes one query in the fixed lifecycle order and returns its receipt.

Examples ​

Configure one read for the same product material ​

Beginner · Node.js

ts
import { Retrieval } from '@jangts/agent-core/kits/retrieval';
type Source = { sourceId: string; text: string };
const state = { queryCount: 0, seen: new Set<string>(), sources: [] as Source[] };
const documents = [{ sourceId: 'manual', text: 'Agent Core 使用指南' }];
const retrieval = Retrieval.defineRetrieval({
  id: 'manual-search', description: '读取已确认的本地演示资料',
  assertInput(query: string) { if (!query.trim()) throw new Error('查询为空'); },
  state: () => state, limit: () => 2,
  execute: (query: string) => ({ sources: documents.filter(item => item.text.includes(query)) }),
});
console.log(retrieval.run('Agent', { app: {}, signal: new AbortController().signal }).result);
js
import { Retrieval } from '@jangts/agent-core/kits/retrieval';
const state = { queryCount: 0, seen: new Set(), sources: [] };
const documents = [{ sourceId: 'manual', text: 'Agent Core 使用指南' }];
const retrieval = Retrieval.defineRetrieval({
    id: 'manual-search', description: '读取已确认的本地演示资料',
    assertInput(query) { if (!query.trim())
        throw new Error('查询为空'); },
    state: () => state, limit: () => 2,
    execute: (query) => ({ sources: documents.filter(item => item.text.includes(query)) }),
});
console.log(retrieval.run('Agent', { app: {}, signal: new AbortController().signal }).result);

Add normalization, authorization, and receipt effects to the same run ​

Configuration · Node.js

ts
import { Retrieval } from '@jangts/agent-core/kits/retrieval';
type Source = { sourceId: string; text: string };
const state = { queryCount: 0, seen: new Set<string>(), sources: [] as Source[] };
const retrieval = Retrieval.defineRetrieval<string, Source, { sources: Source[] }, { canRead: boolean }>({
  id: 'manual-search', description: '按当前服务端权限读取资料',
  prepare: query => query.trim(),
  assertInput(query) { if (!query) throw new Error('查询为空'); },
  state: () => state, limit: () => 2,
  authorize(_query, { app }) { if (!app.canRead) throw new Error('没有资料读取权限'); },
  execute: query => ({ sources: [{ sourceId: 'manual', text: 'Agent Core 使用指南' }].filter(item => item.text.includes(query)) }),
  effects: [{ id: 'source-count', description: '给真实回执附加来源数量', handle(receipt) { receipt.extensions.sourceCount = receipt.result.sources.length; } }],
});
const receipt = retrieval.run(' Agent ', { app: { canRead: true }, signal: new AbortController().signal });
console.log(receipt.sequence, receipt.extensions);
js
import { Retrieval } from '@jangts/agent-core/kits/retrieval';
const state = { queryCount: 0, seen: new Set(), sources: [] };
const retrieval = Retrieval.defineRetrieval({
    id: 'manual-search', description: '按当前服务端权限读取资料',
    prepare: query => query.trim(),
    assertInput(query) { if (!query)
        throw new Error('查询为空'); },
    state: () => state, limit: () => 2,
    authorize(_query, { app }) { if (!app.canRead)
        throw new Error('没有资料读取权限'); },
    execute: query => ({ sources: [{ sourceId: 'manual', text: 'Agent Core 使用指南' }].filter(item => item.text.includes(query)) }),
    effects: [{ id: 'source-count', description: '给真实回执附加来源数量', handle(receipt) { receipt.extensions.sourceCount = receipt.result.sources.length; } }],
});
const receipt = retrieval.run(' Agent ', { app: { canRead: true }, signal: new AbortController().signal });
console.log(receipt.sequence, receipt.extensions);

API reference, usage examples and developer guides.