ContextJournal
CORE · class · 运行时值
Request-local context projection of execution receipts; not a separate persistent log store.
导入
ts
import { ContextJournal } from '@jangts/agent-core/context';js
import { ContextJournal } from '@jangts/agent-core/context';其他公开入口:@jangts/agent-core、@jangts/agent-core/types、@jangts/agent-core/context、@jangts/agent-core/journal。
定义
ts
class ContextJournal继承或实现: ToolJournal。
构造函数
ts
new ContextJournal(): ContextJournal无调用参数。
返回: ContextJournal。
成员目录
| 成员 | 用途 |
|---|---|
model | Record one model-produced value. |
beginTool | Open a tool call. |
finishTool | Close an open tool call with its outcome. |
snapshot | 完整输入输出见下方重载。 |
成员详情
model
实例方法。
Record one model-produced value.
ts
model({ stageId, value, invocation, }: { stageId: string; value: unknown; invocation: InvocationMetadata; }): voidRecord one model-produced value.
| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
__0 | { stageId: string; value: unknown; invocation: InvocationMetadata; } | 必需 | — |
__0 的字段
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
stageId | string | 必需 | 见类型定义及调用场景。 |
value | unknown | 必需 | 见类型定义及调用场景。 |
invocation | InvocationMetadata | 必需 | 见类型定义及调用场景。 |
返回: void。
beginTool
实例方法。
Open a tool call.
ts
beginTool({ toolId, input, invocation }: ToolJournalStart): stringOpen a tool call.
| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
__0 | ToolJournalStart | 必需 | — |
__0 的字段
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
toolId | string | 必需 | Tool being invoked. |
input | unknown | 必需 | Parsed input; stored as-is so the host can build its own domain record. |
invocation | Partial<InvocationMetadata> | undefined | 可省略 | What the caller already knows. The journal fills gaps and generates an id when absent. |
返回: string。
finishTool
实例方法。
Close an open tool call with its outcome.
ts
finishTool({ invocationId, status, value }: ToolJournalFinish): voidClose an open tool call with its outcome.
| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
__0 | ToolJournalFinish | 必需 | — |
__0 的字段
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
invocationId | string | 必需 | Invocation opened by beginTool. |
status | "error" | "success" | "cancelled" | 必需 | Terminal status; cancelled is distinct from error. |
value | unknown | 必需 | Produced value on success; failure detail on error; ignored for cancellation. |
返回: void。
snapshot
实例方法。
ts
snapshot(): readonly ContextRecord[]无调用参数。
返回: readonly ContextRecord[]。
使用示例
- ContextJournal:回执的请求内投影:将真实调用回执变成可用于后续上下文的请求内记录,不另起持久日志系统。
读取 Runner 已记录的模型回执
入门 · Node.js
ts
import { ModelRunner } from '@jangts/agent-core';
const runner = new ModelRunner({ provider: { async *stream() { yield '服务运行正常'; } } });
const result = await runner.invoke('查询服务状态');
console.log(result.invocation.invocationId);
console.log(runner.journal.snapshot());js
import { ModelRunner } from '@jangts/agent-core';
const runner = new ModelRunner({ provider: { async *stream() { yield '服务运行正常'; } } });
const result = await runner.invoke('查询服务状态');
console.log(result.invocation.invocationId);
console.log(runner.journal.snapshot());应用适配器记录它实际执行的本地工具
配置 · Node.js
ts
import { ContextJournal } from '@jangts/agent-core/journal';
const journal = new ContextJournal();
const invocationId = journal.beginTool({ toolId: 'get_status', input: {} });
// 此处由 App 实际执行本地读取;不是模型提供的回执。
const status = '服务运行正常';
journal.finishTool({ invocationId, status: 'success', value: status });
console.log(journal.snapshot());js
import { ContextJournal } from '@jangts/agent-core/journal';
const journal = new ContextJournal();
const invocationId = journal.beginTool({ toolId: 'get_status', input: {} });
// 此处由 App 实际执行本地读取;不是模型提供的回执。
const status = '服务运行正常';
journal.finishTool({ invocationId, status: 'success', value: status });
console.log(journal.snapshot());