@linnlabs/linnkit 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +84 -0
- package/LICENSE +21 -0
- package/README.md +178 -0
- package/README.zh-CN.md +182 -0
- package/dist/agent-invocation-BHcNfrBV.d.cts +30 -0
- package/dist/agent-invocation-BznDaXDs.d.ts +30 -0
- package/dist/agentEvents-DEB7Fy_J.d.cts +81 -0
- package/dist/agentEvents-DEB7Fy_J.d.ts +81 -0
- package/dist/agentSpec-EkmviZjy.d.cts +2621 -0
- package/dist/agentSpec-EkmviZjy.d.ts +2621 -0
- package/dist/ai-engine.types-BpeU_XQG.d.cts +158 -0
- package/dist/ai-engine.types-vZRnQcJa.d.ts +158 -0
- package/dist/audit-BaRUGaqv.d.cts +307 -0
- package/dist/audit-BaRUGaqv.d.ts +307 -0
- package/dist/audit-CtcfART1.d.ts +33 -0
- package/dist/audit-LeOrm2hX.d.cts +33 -0
- package/dist/checkpointMarker-DAI3wUQu.d.cts +8 -0
- package/dist/checkpointMarker-DAI3wUQu.d.ts +8 -0
- package/dist/cli.cjs +8028 -0
- package/dist/cli.cjs.map +1 -0
- package/dist/cli.d.cts +4 -0
- package/dist/cli.d.ts +4 -0
- package/dist/cli.js +8025 -0
- package/dist/cli.js.map +1 -0
- package/dist/context-manager.cjs +8704 -0
- package/dist/context-manager.cjs.map +1 -0
- package/dist/context-manager.d.cts +2190 -0
- package/dist/context-manager.d.ts +2190 -0
- package/dist/context-manager.js +8650 -0
- package/dist/context-manager.js.map +1 -0
- package/dist/context-trace-DRi5M4lX.d.ts +239 -0
- package/dist/context-trace-HE2qY5Q-.d.cts +239 -0
- package/dist/contracts.cjs +1333 -0
- package/dist/contracts.cjs.map +1 -0
- package/dist/contracts.d.cts +8 -0
- package/dist/contracts.d.ts +8 -0
- package/dist/contracts.js +1214 -0
- package/dist/contracts.js.map +1 -0
- package/dist/defaultGraphExecutor-BBswR8wn.d.ts +624 -0
- package/dist/defaultGraphExecutor-BIjJj7WF.d.cts +624 -0
- package/dist/execution-CAIypb41.d.cts +129 -0
- package/dist/execution-CAIypb41.d.ts +129 -0
- package/dist/index-CHqwkvGp.d.ts +149 -0
- package/dist/index-CJeWHopy.d.ts +584 -0
- package/dist/index-Cm-JbzTH.d.cts +1450 -0
- package/dist/index-Cvr23YCl.d.cts +23 -0
- package/dist/index-DDzuSb0n.d.ts +23 -0
- package/dist/index-DO4dQgf2.d.cts +584 -0
- package/dist/index-DRBWi1fy.d.ts +1450 -0
- package/dist/index-Dl5PLgAv.d.cts +149 -0
- package/dist/index.cjs +9577 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +89 -0
- package/dist/index.d.ts +89 -0
- package/dist/index.js +9563 -0
- package/dist/index.js.map +1 -0
- package/dist/messages-XthmnHZ3.d.cts +8007 -0
- package/dist/messages-XthmnHZ3.d.ts +8007 -0
- package/dist/ports-DaatKJXp.d.cts +90 -0
- package/dist/ports-DnLuKfpE.d.ts +90 -0
- package/dist/ports.cjs +4 -0
- package/dist/ports.cjs.map +1 -0
- package/dist/ports.d.cts +7 -0
- package/dist/ports.d.ts +7 -0
- package/dist/ports.js +3 -0
- package/dist/ports.js.map +1 -0
- package/dist/quickstart.cjs +7697 -0
- package/dist/quickstart.cjs.map +1 -0
- package/dist/quickstart.d.cts +24 -0
- package/dist/quickstart.d.ts +24 -0
- package/dist/quickstart.js +7691 -0
- package/dist/quickstart.js.map +1 -0
- package/dist/runAgent-CPj_9e58.d.ts +88 -0
- package/dist/runAgent-HYKlXbVr.d.cts +88 -0
- package/dist/runHandle-CyXvzgzk.d.ts +239 -0
- package/dist/runHandle-D3gPsD7B.d.cts +239 -0
- package/dist/runtime-kernel/events.cjs +1485 -0
- package/dist/runtime-kernel/events.cjs.map +1 -0
- package/dist/runtime-kernel/events.d.cts +8 -0
- package/dist/runtime-kernel/events.d.ts +8 -0
- package/dist/runtime-kernel/events.js +1475 -0
- package/dist/runtime-kernel/events.js.map +1 -0
- package/dist/runtime-kernel.cjs +8656 -0
- package/dist/runtime-kernel.cjs.map +1 -0
- package/dist/runtime-kernel.d.cts +19 -0
- package/dist/runtime-kernel.d.ts +19 -0
- package/dist/runtime-kernel.js +8568 -0
- package/dist/runtime-kernel.js.map +1 -0
- package/dist/sse-vPyrOPa0.d.cts +1687 -0
- package/dist/sse-vPyrOPa0.d.ts +1687 -0
- package/dist/testkit.cjs +10613 -0
- package/dist/testkit.cjs.map +1 -0
- package/dist/testkit.d.cts +284 -0
- package/dist/testkit.d.ts +284 -0
- package/dist/testkit.js +10593 -0
- package/dist/testkit.js.map +1 -0
- package/dist/todo-B1PmDlp3.d.cts +2253 -0
- package/dist/todo-B1PmDlp3.d.ts +2253 -0
- package/dist/tokenizer-DFL4I7-I.d.ts +28 -0
- package/dist/tokenizer-DH_JXv-H.d.cts +28 -0
- package/dist/toolContracts-Blll0241.d.ts +463 -0
- package/dist/toolContracts-CLkQmhTG.d.cts +463 -0
- package/docs/README.md +76 -0
- package/docs/integration/01-installation.md +94 -0
- package/docs/integration/02-quickstart.md +104 -0
- package/docs/integration/README.md +223 -0
- package/docs/integration/agent-registration-guide.md +330 -0
- package/docs/integration/audit.md +64 -0
- package/docs/integration/child-runs.md +87 -0
- package/docs/integration/constraints-and-pitfalls.md +87 -0
- package/docs/integration/context-engineering.md +650 -0
- package/docs/integration/context-fences.md +289 -0
- package/docs/integration/glossary.md +69 -0
- package/docs/integration/llm-provider.md +76 -0
- package/docs/integration/persistence.md +44 -0
- package/docs/integration/realtime.md +76 -0
- package/docs/integration/run-supervisor.md +69 -0
- package/docs/integration/telemetry.md +48 -0
- package/docs/integration/testing.md +95 -0
- package/docs/integration/tool-development-guide.md +362 -0
- package/docs/integration/tool-history.md +202 -0
- package/docs/integration/tools.md +188 -0
- package/package.json +115 -0
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { A as AgentAiEngine } from './audit-CtcfART1.js';
|
|
2
|
+
import { f as AgentSpecContextPolicyInput, a as AgentSpec } from './agentSpec-EkmviZjy.js';
|
|
3
|
+
import { e as RuntimeEvent } from './todo-B1PmDlp3.js';
|
|
4
|
+
import { B as BaseTool, a as ToolArgs, O as OpenAIToolSchema, T as ToolExecutionContext } from './toolContracts-Blll0241.js';
|
|
5
|
+
import { g as ToolRuntimeDefinition, f as ToolExecutionResult } from './ports-DnLuKfpE.js';
|
|
6
|
+
import { e as RunCost$1 } from './runHandle-CyXvzgzk.js';
|
|
7
|
+
|
|
8
|
+
interface DefinedAgent {
|
|
9
|
+
readonly spec: AgentSpec;
|
|
10
|
+
readonly systemPrompt: string;
|
|
11
|
+
readonly modelId?: string;
|
|
12
|
+
readonly tools: readonly BaseTool<ToolArgs, string>[];
|
|
13
|
+
}
|
|
14
|
+
interface DefineAgentInput {
|
|
15
|
+
id: string;
|
|
16
|
+
version?: string;
|
|
17
|
+
description?: string;
|
|
18
|
+
role?: string;
|
|
19
|
+
systemPrompt: string;
|
|
20
|
+
modelId?: string;
|
|
21
|
+
capabilities?: string[];
|
|
22
|
+
tools?: readonly BaseTool<ToolArgs, string>[];
|
|
23
|
+
contextPolicy?: AgentSpecContextPolicyInput;
|
|
24
|
+
metadata?: Record<string, unknown>;
|
|
25
|
+
}
|
|
26
|
+
interface LinnkitQuickstartConfig {
|
|
27
|
+
agents: readonly DefinedAgent[];
|
|
28
|
+
llm: AgentAiEngine | (() => AgentAiEngine | Promise<AgentAiEngine>);
|
|
29
|
+
defaultModelId?: string;
|
|
30
|
+
}
|
|
31
|
+
interface RunAgentOptions {
|
|
32
|
+
input: string;
|
|
33
|
+
llm: AgentAiEngine;
|
|
34
|
+
modelId?: string;
|
|
35
|
+
conversationId?: string;
|
|
36
|
+
runId?: string;
|
|
37
|
+
signal?: AbortSignal;
|
|
38
|
+
/**
|
|
39
|
+
* Quickstart 事件回调。
|
|
40
|
+
*
|
|
41
|
+
* 中文备注:CLI 用它打印实时输出;生产 host 应直接接入 EventBus / EventStore。
|
|
42
|
+
*/
|
|
43
|
+
onEvent?: (event: RuntimeEvent) => void | Promise<void>;
|
|
44
|
+
}
|
|
45
|
+
interface RunAgentResult {
|
|
46
|
+
runId: string;
|
|
47
|
+
finalAnswer: string;
|
|
48
|
+
events: RuntimeEvent[];
|
|
49
|
+
cost: RunCost;
|
|
50
|
+
contextTrace?: unknown;
|
|
51
|
+
}
|
|
52
|
+
type RunCost = RunCost$1;
|
|
53
|
+
interface QuickstartToolRuntime {
|
|
54
|
+
getToolSchemas(toolNames?: string[]): OpenAIToolSchema[];
|
|
55
|
+
getToolDefinition(toolName: string): ToolRuntimeDefinition | undefined;
|
|
56
|
+
getDisplayOptions(toolName: string): BaseTool<ToolArgs, string>['displayOptions'] | undefined;
|
|
57
|
+
executeTool(toolName: string, args: ToolArgs, context: ToolExecutionContext): Promise<ToolExecutionResult>;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Quickstart 级 Agent 构造器。
|
|
62
|
+
*
|
|
63
|
+
* 中文备注:
|
|
64
|
+
* - 这里不是新的业务 registry,只是把 AgentSpec 的必填项压成低门槛入口;
|
|
65
|
+
* - 生产 host 仍可直接维护自己的 AgentDefinition -> AgentSpec 映射。
|
|
66
|
+
*/
|
|
67
|
+
declare function defineAgent(input: DefineAgentInput): DefinedAgent;
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Quickstart 配置构造器。
|
|
71
|
+
*
|
|
72
|
+
* 中文备注:
|
|
73
|
+
* - 只做轻量运行时校验,避免 CLI 加载坏 config 后给出晦涩堆栈;
|
|
74
|
+
* - 不接管生产 host 的完整配置系统。
|
|
75
|
+
*/
|
|
76
|
+
declare function defineConfig(config: LinnkitQuickstartConfig): LinnkitQuickstartConfig;
|
|
77
|
+
declare function resolveConfiguredLlm(config: LinnkitQuickstartConfig): Promise<AgentAiEngine>;
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* 运行一个 quickstart agent。
|
|
81
|
+
*
|
|
82
|
+
* 中文备注:
|
|
83
|
+
* - 这个 helper 用于“npm install 后立即跑通”的最小体验;
|
|
84
|
+
* - 生产 host 仍应自行装配 EventStore / ToolRuntime / ContextManager / RunSupervisor。
|
|
85
|
+
*/
|
|
86
|
+
declare function runAgent(agent: DefinedAgent, options: RunAgentOptions): Promise<RunAgentResult>;
|
|
87
|
+
|
|
88
|
+
export { type DefineAgentInput as D, type LinnkitQuickstartConfig as L, type QuickstartToolRuntime as Q, type RunAgentOptions as R, type DefinedAgent as a, type RunAgentResult as b, defineConfig as c, defineAgent as d, runAgent as e, resolveConfiguredLlm as r };
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { A as AgentAiEngine } from './audit-LeOrm2hX.cjs';
|
|
2
|
+
import { f as AgentSpecContextPolicyInput, a as AgentSpec } from './agentSpec-EkmviZjy.cjs';
|
|
3
|
+
import { e as RuntimeEvent } from './todo-B1PmDlp3.cjs';
|
|
4
|
+
import { B as BaseTool, a as ToolArgs, O as OpenAIToolSchema, T as ToolExecutionContext } from './toolContracts-CLkQmhTG.cjs';
|
|
5
|
+
import { g as ToolRuntimeDefinition, f as ToolExecutionResult } from './ports-DaatKJXp.cjs';
|
|
6
|
+
import { e as RunCost$1 } from './runHandle-D3gPsD7B.cjs';
|
|
7
|
+
|
|
8
|
+
interface DefinedAgent {
|
|
9
|
+
readonly spec: AgentSpec;
|
|
10
|
+
readonly systemPrompt: string;
|
|
11
|
+
readonly modelId?: string;
|
|
12
|
+
readonly tools: readonly BaseTool<ToolArgs, string>[];
|
|
13
|
+
}
|
|
14
|
+
interface DefineAgentInput {
|
|
15
|
+
id: string;
|
|
16
|
+
version?: string;
|
|
17
|
+
description?: string;
|
|
18
|
+
role?: string;
|
|
19
|
+
systemPrompt: string;
|
|
20
|
+
modelId?: string;
|
|
21
|
+
capabilities?: string[];
|
|
22
|
+
tools?: readonly BaseTool<ToolArgs, string>[];
|
|
23
|
+
contextPolicy?: AgentSpecContextPolicyInput;
|
|
24
|
+
metadata?: Record<string, unknown>;
|
|
25
|
+
}
|
|
26
|
+
interface LinnkitQuickstartConfig {
|
|
27
|
+
agents: readonly DefinedAgent[];
|
|
28
|
+
llm: AgentAiEngine | (() => AgentAiEngine | Promise<AgentAiEngine>);
|
|
29
|
+
defaultModelId?: string;
|
|
30
|
+
}
|
|
31
|
+
interface RunAgentOptions {
|
|
32
|
+
input: string;
|
|
33
|
+
llm: AgentAiEngine;
|
|
34
|
+
modelId?: string;
|
|
35
|
+
conversationId?: string;
|
|
36
|
+
runId?: string;
|
|
37
|
+
signal?: AbortSignal;
|
|
38
|
+
/**
|
|
39
|
+
* Quickstart 事件回调。
|
|
40
|
+
*
|
|
41
|
+
* 中文备注:CLI 用它打印实时输出;生产 host 应直接接入 EventBus / EventStore。
|
|
42
|
+
*/
|
|
43
|
+
onEvent?: (event: RuntimeEvent) => void | Promise<void>;
|
|
44
|
+
}
|
|
45
|
+
interface RunAgentResult {
|
|
46
|
+
runId: string;
|
|
47
|
+
finalAnswer: string;
|
|
48
|
+
events: RuntimeEvent[];
|
|
49
|
+
cost: RunCost;
|
|
50
|
+
contextTrace?: unknown;
|
|
51
|
+
}
|
|
52
|
+
type RunCost = RunCost$1;
|
|
53
|
+
interface QuickstartToolRuntime {
|
|
54
|
+
getToolSchemas(toolNames?: string[]): OpenAIToolSchema[];
|
|
55
|
+
getToolDefinition(toolName: string): ToolRuntimeDefinition | undefined;
|
|
56
|
+
getDisplayOptions(toolName: string): BaseTool<ToolArgs, string>['displayOptions'] | undefined;
|
|
57
|
+
executeTool(toolName: string, args: ToolArgs, context: ToolExecutionContext): Promise<ToolExecutionResult>;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Quickstart 级 Agent 构造器。
|
|
62
|
+
*
|
|
63
|
+
* 中文备注:
|
|
64
|
+
* - 这里不是新的业务 registry,只是把 AgentSpec 的必填项压成低门槛入口;
|
|
65
|
+
* - 生产 host 仍可直接维护自己的 AgentDefinition -> AgentSpec 映射。
|
|
66
|
+
*/
|
|
67
|
+
declare function defineAgent(input: DefineAgentInput): DefinedAgent;
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Quickstart 配置构造器。
|
|
71
|
+
*
|
|
72
|
+
* 中文备注:
|
|
73
|
+
* - 只做轻量运行时校验,避免 CLI 加载坏 config 后给出晦涩堆栈;
|
|
74
|
+
* - 不接管生产 host 的完整配置系统。
|
|
75
|
+
*/
|
|
76
|
+
declare function defineConfig(config: LinnkitQuickstartConfig): LinnkitQuickstartConfig;
|
|
77
|
+
declare function resolveConfiguredLlm(config: LinnkitQuickstartConfig): Promise<AgentAiEngine>;
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* 运行一个 quickstart agent。
|
|
81
|
+
*
|
|
82
|
+
* 中文备注:
|
|
83
|
+
* - 这个 helper 用于“npm install 后立即跑通”的最小体验;
|
|
84
|
+
* - 生产 host 仍应自行装配 EventStore / ToolRuntime / ContextManager / RunSupervisor。
|
|
85
|
+
*/
|
|
86
|
+
declare function runAgent(agent: DefinedAgent, options: RunAgentOptions): Promise<RunAgentResult>;
|
|
87
|
+
|
|
88
|
+
export { type DefineAgentInput as D, type LinnkitQuickstartConfig as L, type QuickstartToolRuntime as Q, type RunAgentOptions as R, type DefinedAgent as a, type RunAgentResult as b, defineConfig as c, defineAgent as d, runAgent as e, resolveConfiguredLlm as r };
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
import { a as AgentSpec } from './agentSpec-EkmviZjy.js';
|
|
2
|
+
import { e as RuntimeEvent } from './todo-B1PmDlp3.js';
|
|
3
|
+
import { b as AuditPort } from './audit-CtcfART1.js';
|
|
4
|
+
import { E as EventEnvelope } from './execution-CAIypb41.js';
|
|
5
|
+
|
|
6
|
+
type PersistedEvent = {
|
|
7
|
+
eventId: string;
|
|
8
|
+
timestamp: number;
|
|
9
|
+
conversationId: string;
|
|
10
|
+
runId?: string;
|
|
11
|
+
event: RuntimeEvent;
|
|
12
|
+
};
|
|
13
|
+
type EventRangeOptions = {
|
|
14
|
+
fromEventId?: string;
|
|
15
|
+
toEventId?: string;
|
|
16
|
+
limit?: number;
|
|
17
|
+
};
|
|
18
|
+
interface EventStore {
|
|
19
|
+
append(conversationId: string, event: PersistedEvent): Promise<void>;
|
|
20
|
+
range(conversationId: string, opts?: EventRangeOptions): Promise<PersistedEvent[]>;
|
|
21
|
+
latestEventId(conversationId: string): Promise<string | null>;
|
|
22
|
+
truncate?(conversationId: string, opts: {
|
|
23
|
+
beforeEventId?: string;
|
|
24
|
+
beforeMs?: number;
|
|
25
|
+
}): Promise<void>;
|
|
26
|
+
}
|
|
27
|
+
declare function createMonotonicEventIdFactory(nowProvider?: () => number): () => string;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* @file src/agent/runtime-kernel/execution/event-bus.ts
|
|
31
|
+
* @description 内存中的事件总线
|
|
32
|
+
*
|
|
33
|
+
* 功能 (What):
|
|
34
|
+
* - 提供一个中央事件中心,用于发布和订阅 `EventEnvelope` 事件。
|
|
35
|
+
* - 解耦事件的产生方(如 Runners)和消费方(如持久化、SSE推送)。
|
|
36
|
+
* - 保证事件处理的逻辑集中化。
|
|
37
|
+
*
|
|
38
|
+
* 这是一个简单的内存实现,为每个执行流程(execution)创建一个实例。
|
|
39
|
+
*/
|
|
40
|
+
|
|
41
|
+
type EventBusEvents = {
|
|
42
|
+
'event': (envelope: EventEnvelope<RuntimeEvent>) => void;
|
|
43
|
+
'error': (error: Error) => void;
|
|
44
|
+
'close': () => void;
|
|
45
|
+
};
|
|
46
|
+
type EventListener = (...args: never[]) => void;
|
|
47
|
+
declare class TypedEventEmitter<TEvents extends Record<string, EventListener>> {
|
|
48
|
+
private readonly listeners;
|
|
49
|
+
private maxListeners;
|
|
50
|
+
on<TEvent extends keyof TEvents>(event: TEvent, listener: TEvents[TEvent]): this;
|
|
51
|
+
off<TEvent extends keyof TEvents>(event: TEvent, listener: TEvents[TEvent]): this;
|
|
52
|
+
once<TEvent extends keyof TEvents>(event: TEvent, listener: TEvents[TEvent]): this;
|
|
53
|
+
emit<TEvent extends keyof TEvents>(event: TEvent, ...args: Parameters<TEvents[TEvent]>): boolean;
|
|
54
|
+
removeAllListeners<TEvent extends keyof TEvents>(event?: TEvent): this;
|
|
55
|
+
setMaxListeners(n: number): this;
|
|
56
|
+
private getListeners;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* 事件总线类
|
|
60
|
+
*
|
|
61
|
+
* 为每一次执行(execution)实例化一次,管理该执行生命周期内的所有事件。
|
|
62
|
+
*/
|
|
63
|
+
declare class EventBus extends TypedEventEmitter<EventBusEvents> {
|
|
64
|
+
readonly executionId: string;
|
|
65
|
+
constructor(executionId: string);
|
|
66
|
+
/**
|
|
67
|
+
* 发布一个事件到总线
|
|
68
|
+
* @param envelope - 经过序列器包装的事件信封
|
|
69
|
+
*/
|
|
70
|
+
publish(envelope: EventEnvelope<RuntimeEvent>): void;
|
|
71
|
+
/**
|
|
72
|
+
* 关闭总线,清理所有监听器
|
|
73
|
+
*/
|
|
74
|
+
close(): void;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
type RunStatus = 'pending' | 'running' | 'awaiting_user' | 'paused' | 'completed' | 'failed' | 'cancelled';
|
|
78
|
+
type ListRunsFilter = {
|
|
79
|
+
status?: RunStatus | RunStatus[];
|
|
80
|
+
parentRunId?: string;
|
|
81
|
+
agentSpecId?: string;
|
|
82
|
+
startedAfter?: number;
|
|
83
|
+
startedBefore?: number;
|
|
84
|
+
limit?: number;
|
|
85
|
+
cursor?: string;
|
|
86
|
+
};
|
|
87
|
+
type RunRecord = {
|
|
88
|
+
runId: string;
|
|
89
|
+
conversationId: string;
|
|
90
|
+
parentRunId?: string;
|
|
91
|
+
agentSpecId?: string;
|
|
92
|
+
status: RunStatus;
|
|
93
|
+
currentNode?: string;
|
|
94
|
+
startedAt: number;
|
|
95
|
+
updatedAt: number;
|
|
96
|
+
pausedAt?: number;
|
|
97
|
+
pauseReason?: string;
|
|
98
|
+
iterationsUsed?: number;
|
|
99
|
+
iterationBudget?: {
|
|
100
|
+
max: number;
|
|
101
|
+
refundable: boolean;
|
|
102
|
+
};
|
|
103
|
+
errorIfAny?: {
|
|
104
|
+
errorCode: string;
|
|
105
|
+
message: string;
|
|
106
|
+
recoverable: boolean;
|
|
107
|
+
};
|
|
108
|
+
metadata?: Record<string, unknown>;
|
|
109
|
+
};
|
|
110
|
+
interface RunRegistryStore {
|
|
111
|
+
save(record: RunRecord): Promise<void>;
|
|
112
|
+
load(runId: string): Promise<RunRecord | null>;
|
|
113
|
+
list(filter?: ListRunsFilter): Promise<{
|
|
114
|
+
runs: RunRecord[];
|
|
115
|
+
nextCursor?: string;
|
|
116
|
+
}>;
|
|
117
|
+
delete(runId: string): Promise<void>;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
type RunRequestSnapshot = Readonly<object>;
|
|
121
|
+
interface CancelOpts {
|
|
122
|
+
reason: string;
|
|
123
|
+
forceCleanup?: boolean;
|
|
124
|
+
timeout?: number;
|
|
125
|
+
}
|
|
126
|
+
interface RunCost {
|
|
127
|
+
tokensInput: number;
|
|
128
|
+
tokensOutput: number;
|
|
129
|
+
totalCostUsd?: number;
|
|
130
|
+
latencyMs?: number;
|
|
131
|
+
childrenTotal?: RunCost;
|
|
132
|
+
}
|
|
133
|
+
interface RunCostCollector {
|
|
134
|
+
snapshot(runId: string): RunCost | Promise<RunCost>;
|
|
135
|
+
}
|
|
136
|
+
interface RunLifecyclePatch {
|
|
137
|
+
currentNode?: string;
|
|
138
|
+
iterationsUsed?: number;
|
|
139
|
+
}
|
|
140
|
+
interface RunAwaitingUserPatch extends RunLifecyclePatch {
|
|
141
|
+
reason?: string;
|
|
142
|
+
eventId?: string;
|
|
143
|
+
}
|
|
144
|
+
interface RunFailureInfo {
|
|
145
|
+
errorCode: string;
|
|
146
|
+
message: string;
|
|
147
|
+
recoverable: boolean;
|
|
148
|
+
}
|
|
149
|
+
interface RunMeta {
|
|
150
|
+
runId: string;
|
|
151
|
+
parentRunId?: string;
|
|
152
|
+
agentSpecId?: string;
|
|
153
|
+
conversationId: string;
|
|
154
|
+
status: RunStatus;
|
|
155
|
+
currentNode?: string;
|
|
156
|
+
startedAt: number;
|
|
157
|
+
updatedAt: number;
|
|
158
|
+
pausedAt?: number;
|
|
159
|
+
pauseReason?: string;
|
|
160
|
+
iterationsUsed?: number;
|
|
161
|
+
errorIfAny?: {
|
|
162
|
+
errorCode: string;
|
|
163
|
+
message: string;
|
|
164
|
+
recoverable: boolean;
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
interface RunObserveFilter {
|
|
168
|
+
/** RuntimeEvent 使用 type 作为主判别字段;保留 kinds 只是为了贴近 N-3 草案措辞。 */
|
|
169
|
+
types?: RuntimeEvent['type'][];
|
|
170
|
+
kinds?: RuntimeEvent['type'][];
|
|
171
|
+
includePersisted?: boolean;
|
|
172
|
+
}
|
|
173
|
+
interface RunHandle<TRequest extends RunRequestSnapshot = RunRequestSnapshot> {
|
|
174
|
+
readonly runId: string;
|
|
175
|
+
readonly parentRunId?: string;
|
|
176
|
+
readonly signal: AbortSignal;
|
|
177
|
+
spec(): Promise<AgentSpec>;
|
|
178
|
+
request(): Promise<TRequest>;
|
|
179
|
+
cancel(opts: CancelOpts): Promise<void>;
|
|
180
|
+
observe(filter?: RunObserveFilter): AsyncIterable<RuntimeEvent>;
|
|
181
|
+
cost(): Promise<RunCost>;
|
|
182
|
+
meta(): Promise<RunMeta>;
|
|
183
|
+
markRunning(patch?: RunLifecyclePatch): Promise<void>;
|
|
184
|
+
markAwaitingUser(patch?: RunAwaitingUserPatch): Promise<void>;
|
|
185
|
+
markCompleted(patch?: RunLifecyclePatch): Promise<void>;
|
|
186
|
+
markFailed(error: RunFailureInfo, patch?: RunLifecyclePatch): Promise<void>;
|
|
187
|
+
pause(reason?: string): Promise<never>;
|
|
188
|
+
resume(): Promise<never>;
|
|
189
|
+
}
|
|
190
|
+
interface DefaultRunHandleOptions<TRequest extends RunRequestSnapshot = RunRequestSnapshot> {
|
|
191
|
+
runRecord: RunRecord;
|
|
192
|
+
abortController: AbortController;
|
|
193
|
+
agentSpec: AgentSpec;
|
|
194
|
+
request: TRequest;
|
|
195
|
+
eventBus: EventBus;
|
|
196
|
+
eventStore: EventStore;
|
|
197
|
+
costCollector: RunCostCollector;
|
|
198
|
+
registryStore: RunRegistryStore;
|
|
199
|
+
auditPort?: AuditPort;
|
|
200
|
+
onCancelled?: (runId: string, opts: CancelOpts) => void;
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* RunHandle 的默认实现只编织现有机制:
|
|
204
|
+
* - cancel 只触发 AbortController,不改 GraphExecutor;
|
|
205
|
+
* - observe 只包装 EventBus/EventStore;
|
|
206
|
+
* - cost 只读取外部注入的 CostCollector。
|
|
207
|
+
*/
|
|
208
|
+
declare class DefaultRunHandle<TRequest extends RunRequestSnapshot = RunRequestSnapshot> implements RunHandle<TRequest> {
|
|
209
|
+
readonly runId: string;
|
|
210
|
+
readonly parentRunId?: string;
|
|
211
|
+
readonly signal: AbortSignal;
|
|
212
|
+
private runRecord;
|
|
213
|
+
private readonly abortController;
|
|
214
|
+
private readonly agentSpecSnapshot;
|
|
215
|
+
private readonly requestSnapshot;
|
|
216
|
+
private readonly eventBus;
|
|
217
|
+
private readonly eventStore;
|
|
218
|
+
private readonly costCollector;
|
|
219
|
+
private readonly registryStore;
|
|
220
|
+
private readonly auditPort;
|
|
221
|
+
private readonly onCancelled?;
|
|
222
|
+
constructor(options: DefaultRunHandleOptions<TRequest>);
|
|
223
|
+
spec(): Promise<AgentSpec>;
|
|
224
|
+
request(): Promise<TRequest>;
|
|
225
|
+
cancel(opts: CancelOpts): Promise<void>;
|
|
226
|
+
observe(filter?: RunObserveFilter): AsyncIterable<RuntimeEvent>;
|
|
227
|
+
cost(): Promise<RunCost>;
|
|
228
|
+
meta(): Promise<RunMeta>;
|
|
229
|
+
markRunning(patch?: RunLifecyclePatch): Promise<void>;
|
|
230
|
+
markAwaitingUser(patch?: RunAwaitingUserPatch): Promise<void>;
|
|
231
|
+
markCompleted(patch?: RunLifecyclePatch): Promise<void>;
|
|
232
|
+
markFailed(error: RunFailureInfo, patch?: RunLifecyclePatch): Promise<void>;
|
|
233
|
+
pause(_reason?: string): Promise<never>;
|
|
234
|
+
resume(): Promise<never>;
|
|
235
|
+
private saveLifecycleStatus;
|
|
236
|
+
}
|
|
237
|
+
declare function runMetaFromRecord(record: RunRecord): RunMeta;
|
|
238
|
+
|
|
239
|
+
export { type CancelOpts as C, DefaultRunHandle as D, type EventRangeOptions as E, type ListRunsFilter as L, type PersistedEvent as P, type RunAwaitingUserPatch as R, type EventStore as a, EventBus as b, createMonotonicEventIdFactory as c, type DefaultRunHandleOptions as d, type RunCost as e, type RunCostCollector as f, type RunFailureInfo as g, type RunHandle as h, type RunLifecyclePatch as i, type RunMeta as j, type RunObserveFilter as k, type RunRecord as l, type RunRegistryStore as m, type RunRequestSnapshot as n, type RunStatus as o, runMetaFromRecord as r };
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
import { a as AgentSpec } from './agentSpec-EkmviZjy.cjs';
|
|
2
|
+
import { e as RuntimeEvent } from './todo-B1PmDlp3.cjs';
|
|
3
|
+
import { b as AuditPort } from './audit-LeOrm2hX.cjs';
|
|
4
|
+
import { E as EventEnvelope } from './execution-CAIypb41.cjs';
|
|
5
|
+
|
|
6
|
+
type PersistedEvent = {
|
|
7
|
+
eventId: string;
|
|
8
|
+
timestamp: number;
|
|
9
|
+
conversationId: string;
|
|
10
|
+
runId?: string;
|
|
11
|
+
event: RuntimeEvent;
|
|
12
|
+
};
|
|
13
|
+
type EventRangeOptions = {
|
|
14
|
+
fromEventId?: string;
|
|
15
|
+
toEventId?: string;
|
|
16
|
+
limit?: number;
|
|
17
|
+
};
|
|
18
|
+
interface EventStore {
|
|
19
|
+
append(conversationId: string, event: PersistedEvent): Promise<void>;
|
|
20
|
+
range(conversationId: string, opts?: EventRangeOptions): Promise<PersistedEvent[]>;
|
|
21
|
+
latestEventId(conversationId: string): Promise<string | null>;
|
|
22
|
+
truncate?(conversationId: string, opts: {
|
|
23
|
+
beforeEventId?: string;
|
|
24
|
+
beforeMs?: number;
|
|
25
|
+
}): Promise<void>;
|
|
26
|
+
}
|
|
27
|
+
declare function createMonotonicEventIdFactory(nowProvider?: () => number): () => string;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* @file src/agent/runtime-kernel/execution/event-bus.ts
|
|
31
|
+
* @description 内存中的事件总线
|
|
32
|
+
*
|
|
33
|
+
* 功能 (What):
|
|
34
|
+
* - 提供一个中央事件中心,用于发布和订阅 `EventEnvelope` 事件。
|
|
35
|
+
* - 解耦事件的产生方(如 Runners)和消费方(如持久化、SSE推送)。
|
|
36
|
+
* - 保证事件处理的逻辑集中化。
|
|
37
|
+
*
|
|
38
|
+
* 这是一个简单的内存实现,为每个执行流程(execution)创建一个实例。
|
|
39
|
+
*/
|
|
40
|
+
|
|
41
|
+
type EventBusEvents = {
|
|
42
|
+
'event': (envelope: EventEnvelope<RuntimeEvent>) => void;
|
|
43
|
+
'error': (error: Error) => void;
|
|
44
|
+
'close': () => void;
|
|
45
|
+
};
|
|
46
|
+
type EventListener = (...args: never[]) => void;
|
|
47
|
+
declare class TypedEventEmitter<TEvents extends Record<string, EventListener>> {
|
|
48
|
+
private readonly listeners;
|
|
49
|
+
private maxListeners;
|
|
50
|
+
on<TEvent extends keyof TEvents>(event: TEvent, listener: TEvents[TEvent]): this;
|
|
51
|
+
off<TEvent extends keyof TEvents>(event: TEvent, listener: TEvents[TEvent]): this;
|
|
52
|
+
once<TEvent extends keyof TEvents>(event: TEvent, listener: TEvents[TEvent]): this;
|
|
53
|
+
emit<TEvent extends keyof TEvents>(event: TEvent, ...args: Parameters<TEvents[TEvent]>): boolean;
|
|
54
|
+
removeAllListeners<TEvent extends keyof TEvents>(event?: TEvent): this;
|
|
55
|
+
setMaxListeners(n: number): this;
|
|
56
|
+
private getListeners;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* 事件总线类
|
|
60
|
+
*
|
|
61
|
+
* 为每一次执行(execution)实例化一次,管理该执行生命周期内的所有事件。
|
|
62
|
+
*/
|
|
63
|
+
declare class EventBus extends TypedEventEmitter<EventBusEvents> {
|
|
64
|
+
readonly executionId: string;
|
|
65
|
+
constructor(executionId: string);
|
|
66
|
+
/**
|
|
67
|
+
* 发布一个事件到总线
|
|
68
|
+
* @param envelope - 经过序列器包装的事件信封
|
|
69
|
+
*/
|
|
70
|
+
publish(envelope: EventEnvelope<RuntimeEvent>): void;
|
|
71
|
+
/**
|
|
72
|
+
* 关闭总线,清理所有监听器
|
|
73
|
+
*/
|
|
74
|
+
close(): void;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
type RunStatus = 'pending' | 'running' | 'awaiting_user' | 'paused' | 'completed' | 'failed' | 'cancelled';
|
|
78
|
+
type ListRunsFilter = {
|
|
79
|
+
status?: RunStatus | RunStatus[];
|
|
80
|
+
parentRunId?: string;
|
|
81
|
+
agentSpecId?: string;
|
|
82
|
+
startedAfter?: number;
|
|
83
|
+
startedBefore?: number;
|
|
84
|
+
limit?: number;
|
|
85
|
+
cursor?: string;
|
|
86
|
+
};
|
|
87
|
+
type RunRecord = {
|
|
88
|
+
runId: string;
|
|
89
|
+
conversationId: string;
|
|
90
|
+
parentRunId?: string;
|
|
91
|
+
agentSpecId?: string;
|
|
92
|
+
status: RunStatus;
|
|
93
|
+
currentNode?: string;
|
|
94
|
+
startedAt: number;
|
|
95
|
+
updatedAt: number;
|
|
96
|
+
pausedAt?: number;
|
|
97
|
+
pauseReason?: string;
|
|
98
|
+
iterationsUsed?: number;
|
|
99
|
+
iterationBudget?: {
|
|
100
|
+
max: number;
|
|
101
|
+
refundable: boolean;
|
|
102
|
+
};
|
|
103
|
+
errorIfAny?: {
|
|
104
|
+
errorCode: string;
|
|
105
|
+
message: string;
|
|
106
|
+
recoverable: boolean;
|
|
107
|
+
};
|
|
108
|
+
metadata?: Record<string, unknown>;
|
|
109
|
+
};
|
|
110
|
+
interface RunRegistryStore {
|
|
111
|
+
save(record: RunRecord): Promise<void>;
|
|
112
|
+
load(runId: string): Promise<RunRecord | null>;
|
|
113
|
+
list(filter?: ListRunsFilter): Promise<{
|
|
114
|
+
runs: RunRecord[];
|
|
115
|
+
nextCursor?: string;
|
|
116
|
+
}>;
|
|
117
|
+
delete(runId: string): Promise<void>;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
type RunRequestSnapshot = Readonly<object>;
|
|
121
|
+
interface CancelOpts {
|
|
122
|
+
reason: string;
|
|
123
|
+
forceCleanup?: boolean;
|
|
124
|
+
timeout?: number;
|
|
125
|
+
}
|
|
126
|
+
interface RunCost {
|
|
127
|
+
tokensInput: number;
|
|
128
|
+
tokensOutput: number;
|
|
129
|
+
totalCostUsd?: number;
|
|
130
|
+
latencyMs?: number;
|
|
131
|
+
childrenTotal?: RunCost;
|
|
132
|
+
}
|
|
133
|
+
interface RunCostCollector {
|
|
134
|
+
snapshot(runId: string): RunCost | Promise<RunCost>;
|
|
135
|
+
}
|
|
136
|
+
interface RunLifecyclePatch {
|
|
137
|
+
currentNode?: string;
|
|
138
|
+
iterationsUsed?: number;
|
|
139
|
+
}
|
|
140
|
+
interface RunAwaitingUserPatch extends RunLifecyclePatch {
|
|
141
|
+
reason?: string;
|
|
142
|
+
eventId?: string;
|
|
143
|
+
}
|
|
144
|
+
interface RunFailureInfo {
|
|
145
|
+
errorCode: string;
|
|
146
|
+
message: string;
|
|
147
|
+
recoverable: boolean;
|
|
148
|
+
}
|
|
149
|
+
interface RunMeta {
|
|
150
|
+
runId: string;
|
|
151
|
+
parentRunId?: string;
|
|
152
|
+
agentSpecId?: string;
|
|
153
|
+
conversationId: string;
|
|
154
|
+
status: RunStatus;
|
|
155
|
+
currentNode?: string;
|
|
156
|
+
startedAt: number;
|
|
157
|
+
updatedAt: number;
|
|
158
|
+
pausedAt?: number;
|
|
159
|
+
pauseReason?: string;
|
|
160
|
+
iterationsUsed?: number;
|
|
161
|
+
errorIfAny?: {
|
|
162
|
+
errorCode: string;
|
|
163
|
+
message: string;
|
|
164
|
+
recoverable: boolean;
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
interface RunObserveFilter {
|
|
168
|
+
/** RuntimeEvent 使用 type 作为主判别字段;保留 kinds 只是为了贴近 N-3 草案措辞。 */
|
|
169
|
+
types?: RuntimeEvent['type'][];
|
|
170
|
+
kinds?: RuntimeEvent['type'][];
|
|
171
|
+
includePersisted?: boolean;
|
|
172
|
+
}
|
|
173
|
+
interface RunHandle<TRequest extends RunRequestSnapshot = RunRequestSnapshot> {
|
|
174
|
+
readonly runId: string;
|
|
175
|
+
readonly parentRunId?: string;
|
|
176
|
+
readonly signal: AbortSignal;
|
|
177
|
+
spec(): Promise<AgentSpec>;
|
|
178
|
+
request(): Promise<TRequest>;
|
|
179
|
+
cancel(opts: CancelOpts): Promise<void>;
|
|
180
|
+
observe(filter?: RunObserveFilter): AsyncIterable<RuntimeEvent>;
|
|
181
|
+
cost(): Promise<RunCost>;
|
|
182
|
+
meta(): Promise<RunMeta>;
|
|
183
|
+
markRunning(patch?: RunLifecyclePatch): Promise<void>;
|
|
184
|
+
markAwaitingUser(patch?: RunAwaitingUserPatch): Promise<void>;
|
|
185
|
+
markCompleted(patch?: RunLifecyclePatch): Promise<void>;
|
|
186
|
+
markFailed(error: RunFailureInfo, patch?: RunLifecyclePatch): Promise<void>;
|
|
187
|
+
pause(reason?: string): Promise<never>;
|
|
188
|
+
resume(): Promise<never>;
|
|
189
|
+
}
|
|
190
|
+
interface DefaultRunHandleOptions<TRequest extends RunRequestSnapshot = RunRequestSnapshot> {
|
|
191
|
+
runRecord: RunRecord;
|
|
192
|
+
abortController: AbortController;
|
|
193
|
+
agentSpec: AgentSpec;
|
|
194
|
+
request: TRequest;
|
|
195
|
+
eventBus: EventBus;
|
|
196
|
+
eventStore: EventStore;
|
|
197
|
+
costCollector: RunCostCollector;
|
|
198
|
+
registryStore: RunRegistryStore;
|
|
199
|
+
auditPort?: AuditPort;
|
|
200
|
+
onCancelled?: (runId: string, opts: CancelOpts) => void;
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* RunHandle 的默认实现只编织现有机制:
|
|
204
|
+
* - cancel 只触发 AbortController,不改 GraphExecutor;
|
|
205
|
+
* - observe 只包装 EventBus/EventStore;
|
|
206
|
+
* - cost 只读取外部注入的 CostCollector。
|
|
207
|
+
*/
|
|
208
|
+
declare class DefaultRunHandle<TRequest extends RunRequestSnapshot = RunRequestSnapshot> implements RunHandle<TRequest> {
|
|
209
|
+
readonly runId: string;
|
|
210
|
+
readonly parentRunId?: string;
|
|
211
|
+
readonly signal: AbortSignal;
|
|
212
|
+
private runRecord;
|
|
213
|
+
private readonly abortController;
|
|
214
|
+
private readonly agentSpecSnapshot;
|
|
215
|
+
private readonly requestSnapshot;
|
|
216
|
+
private readonly eventBus;
|
|
217
|
+
private readonly eventStore;
|
|
218
|
+
private readonly costCollector;
|
|
219
|
+
private readonly registryStore;
|
|
220
|
+
private readonly auditPort;
|
|
221
|
+
private readonly onCancelled?;
|
|
222
|
+
constructor(options: DefaultRunHandleOptions<TRequest>);
|
|
223
|
+
spec(): Promise<AgentSpec>;
|
|
224
|
+
request(): Promise<TRequest>;
|
|
225
|
+
cancel(opts: CancelOpts): Promise<void>;
|
|
226
|
+
observe(filter?: RunObserveFilter): AsyncIterable<RuntimeEvent>;
|
|
227
|
+
cost(): Promise<RunCost>;
|
|
228
|
+
meta(): Promise<RunMeta>;
|
|
229
|
+
markRunning(patch?: RunLifecyclePatch): Promise<void>;
|
|
230
|
+
markAwaitingUser(patch?: RunAwaitingUserPatch): Promise<void>;
|
|
231
|
+
markCompleted(patch?: RunLifecyclePatch): Promise<void>;
|
|
232
|
+
markFailed(error: RunFailureInfo, patch?: RunLifecyclePatch): Promise<void>;
|
|
233
|
+
pause(_reason?: string): Promise<never>;
|
|
234
|
+
resume(): Promise<never>;
|
|
235
|
+
private saveLifecycleStatus;
|
|
236
|
+
}
|
|
237
|
+
declare function runMetaFromRecord(record: RunRecord): RunMeta;
|
|
238
|
+
|
|
239
|
+
export { type CancelOpts as C, DefaultRunHandle as D, type EventRangeOptions as E, type ListRunsFilter as L, type PersistedEvent as P, type RunAwaitingUserPatch as R, type EventStore as a, EventBus as b, createMonotonicEventIdFactory as c, type DefaultRunHandleOptions as d, type RunCost as e, type RunCostCollector as f, type RunFailureInfo as g, type RunHandle as h, type RunLifecyclePatch as i, type RunMeta as j, type RunObserveFilter as k, type RunRecord as l, type RunRegistryStore as m, type RunRequestSnapshot as n, type RunStatus as o, runMetaFromRecord as r };
|