Skip to content
English

RetrievalEffect ​

KIT · interface · 类型合同

One effect applied after a successful query, in declaration order.

导入 ​

ts
import type { RetrievalEffect } from '@jangts/agent-core/kits/retrieval';
js
/** @typedef {import('@jangts/agent-core/kits/retrieval').RetrievalEffect} RetrievalEffect */

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

定义 ​

ts
interface RetrievalEffect<Query, Source, Result, Context>

泛型参数 ​

类型参数约束默认类型
Query——
SourceRetrievalSource—
ResultRetrievalResult<Source>—
Context——

字段结构 ​

字段类型必需说明
idstring必需只读。见类型定义及调用场景。
descriptionstring必需只读。见类型定义及调用场景。
handle(receipt: RetrievalReceipt<Query, Source, Result>, context: Context) => void必需Applies the effect synchronously; a throw stops later effects without rollback.

成员目录 ​

成员用途
handleApplies the effect synchronously; a throw stops later effects without rollback.

成员详情 ​

handle ​

Applies the effect synchronously; a throw stops later effects without rollback.

ts
RetrievalEffect.handle(receipt: RetrievalReceipt<Query, Source, Result>, context: Context): void

Applies the effect synchronously; a throw stops later effects without rollback.

参数类型传递说明
receiptRetrievalReceipt<Query, Source, Result>必需—
contextContext必需—
receipt 的字段
字段类型必需说明
queryQuery必需只读。Query after prepare, exactly as executed.
keystring必需只读。Deduplication key reserved before the adapter call.
sequencenumber必需只读。One-based position of this query within the request.
startedAtMsnumber必需只读。Adapter start instant in epoch milliseconds.
rawResultResult必需只读。Adapter output before source naming; rejected results never produce a receipt.
resultResult必需只读。Same result with every sourceId replaced by the Kit-assigned name.
extensionsRecord<string, unknown>必需只读。Domain attachments are computed from real results, not model-supplied receipts.

返回: void。

使用示例 ​

为同一份产品资料配置一次读取 ​

入门 · 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);

相同 run 增加规范化、授权和回执效果 ​

配置 · 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);

相关策略源码 ​

相关定义 ​

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