@atoms-agent/core 0.3.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/LICENSE +21 -0
- package/README.md +65 -0
- package/dist/agent.d.ts +58 -0
- package/dist/agent.d.ts.map +1 -0
- package/dist/agent.js +118 -0
- package/dist/agent.js.map +1 -0
- package/dist/context.d.ts +33 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +42 -0
- package/dist/context.js.map +1 -0
- package/dist/errors.d.ts +7 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +6 -0
- package/dist/errors.js.map +1 -0
- package/dist/events.d.ts +68 -0
- package/dist/events.d.ts.map +1 -0
- package/dist/events.js +2 -0
- package/dist/events.js.map +1 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -0
- package/dist/internal/abort.d.ts +2 -0
- package/dist/internal/abort.d.ts.map +1 -0
- package/dist/internal/abort.js +34 -0
- package/dist/internal/abort.js.map +1 -0
- package/dist/internal/loop.d.ts +45 -0
- package/dist/internal/loop.d.ts.map +1 -0
- package/dist/internal/loop.js +509 -0
- package/dist/internal/loop.js.map +1 -0
- package/dist/limits.d.ts +16 -0
- package/dist/limits.d.ts.map +1 -0
- package/dist/limits.js +7 -0
- package/dist/limits.js.map +1 -0
- package/dist/registry.d.ts +20 -0
- package/dist/registry.d.ts.map +1 -0
- package/dist/registry.js +52 -0
- package/dist/registry.js.map +1 -0
- package/dist/spec.d.ts +29 -0
- package/dist/spec.d.ts.map +1 -0
- package/dist/spec.js +141 -0
- package/dist/spec.js.map +1 -0
- package/dist/state.d.ts +42 -0
- package/dist/state.d.ts.map +1 -0
- package/dist/state.js +263 -0
- package/dist/state.js.map +1 -0
- package/dist/structured-output.d.ts +41 -0
- package/dist/structured-output.d.ts.map +1 -0
- package/dist/structured-output.js +170 -0
- package/dist/structured-output.js.map +1 -0
- package/dist/tool.d.ts +75 -0
- package/dist/tool.d.ts.map +1 -0
- package/dist/tool.js +206 -0
- package/dist/tool.js.map +1 -0
- package/package.json +50 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 atoms_agent contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# @atoms-agent/core
|
|
2
|
+
|
|
3
|
+
Agent loop, tool runtime, event stream, context strategy, limits, and
|
|
4
|
+
cancellation for `atoms_agent`.
|
|
5
|
+
|
|
6
|
+
This package depends on `@atoms-agent/llm` for model adapters and canonical
|
|
7
|
+
message types. It does not depend on concrete business tools, MCP, coding
|
|
8
|
+
toolkits, or host UI.
|
|
9
|
+
|
|
10
|
+
## Install
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
npm install @atoms-agent/core @atoms-agent/llm
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
Local tarball (publish-ready verification):
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm install ./atoms-agent-llm-0.3.0.tgz ./atoms-agent-core-0.3.0.tgz
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
```typescript
|
|
25
|
+
import { createAgent, defineTool } from "@atoms-agent/core";
|
|
26
|
+
import { openai } from "@atoms-agent/llm";
|
|
27
|
+
|
|
28
|
+
const agent = createAgent({
|
|
29
|
+
model: openai({
|
|
30
|
+
model: "gpt-5",
|
|
31
|
+
apiKey: process.env.OPENAI_API_KEY!,
|
|
32
|
+
}),
|
|
33
|
+
systemPrompt: "You are a helpful assistant.",
|
|
34
|
+
tools: [
|
|
35
|
+
defineTool({
|
|
36
|
+
name: "ping",
|
|
37
|
+
description: "Return pong",
|
|
38
|
+
inputSchema: {
|
|
39
|
+
type: "object",
|
|
40
|
+
properties: {},
|
|
41
|
+
additionalProperties: false,
|
|
42
|
+
},
|
|
43
|
+
sideEffect: "none",
|
|
44
|
+
execute: async () => ({ ok: true }),
|
|
45
|
+
}),
|
|
46
|
+
],
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
for await (const event of agent.run("hello")) {
|
|
50
|
+
if (event.type === "message_delta") {
|
|
51
|
+
process.stdout.write(event.delta);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Only import from the package root. Subpath / deep imports are not supported.
|
|
57
|
+
|
|
58
|
+
## Requirements
|
|
59
|
+
|
|
60
|
+
- Node.js >= 22.13
|
|
61
|
+
- ESM
|
|
62
|
+
|
|
63
|
+
## License
|
|
64
|
+
|
|
65
|
+
MIT
|
package/dist/agent.d.ts
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { ModelAdapter } from "@atoms-agent/llm";
|
|
2
|
+
import { type ContextStrategy } from "./context.js";
|
|
3
|
+
import type { AgentEvent } from "./events.js";
|
|
4
|
+
import type { RunLimits } from "./limits.js";
|
|
5
|
+
import type { RuntimeRegistry } from "./registry.js";
|
|
6
|
+
import { type AgentSpec } from "./spec.js";
|
|
7
|
+
import { type AgentState, type AgentStateSnapshot } from "./state.js";
|
|
8
|
+
import { type StructuredOutputConfig } from "./structured-output.js";
|
|
9
|
+
import { type AuthorizeFn, type ToolDefinition } from "./tool.js";
|
|
10
|
+
/**
|
|
11
|
+
* 内联形态:直接传入实现,用于快速上手。
|
|
12
|
+
* 内部会构建匿名 Registry 并归一为 Spec 形态(PRD §9 裁决)。
|
|
13
|
+
*/
|
|
14
|
+
export type InlineAgentOptions = {
|
|
15
|
+
model: ModelAdapter;
|
|
16
|
+
systemPrompt: string;
|
|
17
|
+
tools?: ToolDefinition[];
|
|
18
|
+
context?: ContextStrategy;
|
|
19
|
+
limits?: RunLimits;
|
|
20
|
+
structuredOutput?: StructuredOutputConfig;
|
|
21
|
+
authorize?: AuthorizeFn;
|
|
22
|
+
};
|
|
23
|
+
/** 配置化形态:可序列化 Spec + 运行时 Registry。 */
|
|
24
|
+
export type SpecAgentOptions = {
|
|
25
|
+
spec: AgentSpec;
|
|
26
|
+
registry: RuntimeRegistry;
|
|
27
|
+
authorize?: AuthorizeFn;
|
|
28
|
+
};
|
|
29
|
+
export type CreateAgentOptions = InlineAgentOptions | SpecAgentOptions;
|
|
30
|
+
export type RunOptions = {
|
|
31
|
+
/** Host 侧取消信号;与 timeoutMs 组合生效。 */
|
|
32
|
+
signal?: AbortSignal;
|
|
33
|
+
};
|
|
34
|
+
export interface Agent {
|
|
35
|
+
/**
|
|
36
|
+
* 执行一次 Run:输入 -> Loop -> Event Stream。
|
|
37
|
+
* 迭代结束(agent_end 或 error 后)Run 终止;消息历史保留在 Agent 内。
|
|
38
|
+
*/
|
|
39
|
+
run(input: string, options?: RunOptions): AsyncIterable<AgentEvent>;
|
|
40
|
+
/** 当前消息历史的只读快照。 */
|
|
41
|
+
state(): AgentState;
|
|
42
|
+
/**
|
|
43
|
+
* 导出 JSON-safe 状态(CORE-006)。
|
|
44
|
+
* Run 进行中禁止导出;应用可自行持久化,但不承诺 Crash Consistency、
|
|
45
|
+
* 并发写或副作用恢复。
|
|
46
|
+
*/
|
|
47
|
+
exportState(): AgentStateSnapshot;
|
|
48
|
+
/**
|
|
49
|
+
* 从 Snapshot 恢复消息历史。
|
|
50
|
+
* 拒绝非法/不兼容 Snapshot;Run 进行中禁止恢复。
|
|
51
|
+
*/
|
|
52
|
+
restoreState(snapshot: unknown): void;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* 创建 Agent(任务 C104 实现 Loop;两种形态共用同一个 Loop,禁止两套实现)。
|
|
56
|
+
*/
|
|
57
|
+
export declare function createAgent(options: CreateAgentOptions): Agent;
|
|
58
|
+
//# sourceMappingURL=agent.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agent.d.ts","sourceRoot":"","sources":["../src/agent.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAa,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAIhE,OAAO,EAAiB,KAAK,eAAe,EAAE,MAAM,cAAc,CAAC;AACnE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAE9C,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAE7C,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAErD,OAAO,EAAmB,KAAK,SAAS,EAAE,MAAM,WAAW,CAAC;AAC5D,OAAO,EAGL,KAAK,UAAU,EACf,KAAK,kBAAkB,EACxB,MAAM,YAAY,CAAC;AACpB,OAAO,EAAiC,KAAK,sBAAsB,EAAE,MAAM,wBAA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{ slidingWindow } from "./context.js";
|
|
4
|
+
import { runLoop } from "./internal/loop.js";
|
|
5
|
+
import { DEFAULT_LIMITS } from "./limits.js";
|
|
6
|
+
import { createRegistry } from "./registry.js";
|
|
7
|
+
import { assertAgentSpec } from "./spec.js";
|
|
8
|
+
import { exportState as exportAgentState, restoreState as restoreAgentState, } from "./state.js";
|
|
9
|
+
import { createStructuredOutputRuntime } from "./structured-output.js";
|
|
10
|
+
import { defaultAuthorize } from "./tool.js";
|
|
11
|
+
/**
|
|
12
|
+
* 创建 Agent(任务 C104 实现 Loop;两种形态共用同一个 Loop,禁止两套实现)。
|
|
13
|
+
*/
|
|
14
|
+
export function createAgent(options) {
|
|
15
|
+
if ("spec" in options) {
|
|
16
|
+
return createSpecAgent(options.spec, options.registry, options.authorize);
|
|
17
|
+
}
|
|
18
|
+
const registry = createRegistry();
|
|
19
|
+
const modelRef = "inline.model";
|
|
20
|
+
const contextRef = "inline.context";
|
|
21
|
+
registry.model(modelRef, options.model);
|
|
22
|
+
registry.context(contextRef, options.context ??
|
|
23
|
+
slidingWindow({ maxTokens: options.model.capabilities.maxContextTokens ?? 32_000 }));
|
|
24
|
+
const tools = options.tools ?? [];
|
|
25
|
+
const toolBindings = tools.map((tool, index) => {
|
|
26
|
+
const ref = `inline.tool.${index + 1}`;
|
|
27
|
+
registry.tool(ref, tool);
|
|
28
|
+
return { ref };
|
|
29
|
+
});
|
|
30
|
+
const spec = {
|
|
31
|
+
id: `inline:${options.model.modelId}`,
|
|
32
|
+
model: { ref: modelRef },
|
|
33
|
+
systemPrompt: options.systemPrompt,
|
|
34
|
+
tools: toolBindings,
|
|
35
|
+
context: { ref: contextRef },
|
|
36
|
+
...(options.limits === undefined ? {} : { limits: options.limits }),
|
|
37
|
+
...(options.structuredOutput === undefined
|
|
38
|
+
? {}
|
|
39
|
+
: { structuredOutput: options.structuredOutput }),
|
|
40
|
+
};
|
|
41
|
+
return createSpecAgent(spec, registry, options.authorize);
|
|
42
|
+
}
|
|
43
|
+
function createSpecAgent(sourceSpec, registry, authorize) {
|
|
44
|
+
assertAgentSpec(sourceSpec);
|
|
45
|
+
const spec = structuredClone(sourceSpec);
|
|
46
|
+
const model = registry.resolveModel(spec.model.ref);
|
|
47
|
+
const contextBinding = spec.context;
|
|
48
|
+
const context = contextBinding === undefined
|
|
49
|
+
? slidingWindow({ maxTokens: model.capabilities.maxContextTokens ?? 32_000 })
|
|
50
|
+
: registry.resolveContext(contextBinding.ref);
|
|
51
|
+
const contextConfig = contextBinding?.config;
|
|
52
|
+
const tools = new Map();
|
|
53
|
+
for (const binding of spec.tools) {
|
|
54
|
+
const tool = registry.resolveTool(binding.ref);
|
|
55
|
+
if (tools.has(tool.name)) {
|
|
56
|
+
throw new AtomsAgentError("CONFIG_ERROR", `Agent ${spec.id} resolves multiple Tools named ${tool.name}.`);
|
|
57
|
+
}
|
|
58
|
+
tools.set(tool.name, {
|
|
59
|
+
tool,
|
|
60
|
+
...(binding.config === undefined ? {} : { config: structuredClone(binding.config) }),
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
const limits = { ...DEFAULT_LIMITS, ...spec.limits };
|
|
64
|
+
const structuredOutput = spec.structuredOutput === undefined
|
|
65
|
+
? undefined
|
|
66
|
+
: createStructuredOutputRuntime(spec.structuredOutput);
|
|
67
|
+
const state = { messages: [] };
|
|
68
|
+
let running = false;
|
|
69
|
+
return {
|
|
70
|
+
run(input, runOptions) {
|
|
71
|
+
const runId = randomUUID();
|
|
72
|
+
return execute();
|
|
73
|
+
async function* execute() {
|
|
74
|
+
if (running) {
|
|
75
|
+
throw new AtomsAgentError("CONFIG_ERROR", `Agent ${spec.id} does not support concurrent runs on shared state.`);
|
|
76
|
+
}
|
|
77
|
+
running = true;
|
|
78
|
+
try {
|
|
79
|
+
yield* runLoop({
|
|
80
|
+
runId,
|
|
81
|
+
agentId: spec.id,
|
|
82
|
+
model,
|
|
83
|
+
systemPrompt: spec.systemPrompt,
|
|
84
|
+
tools,
|
|
85
|
+
context,
|
|
86
|
+
contextConfig,
|
|
87
|
+
limits,
|
|
88
|
+
structuredOutput,
|
|
89
|
+
authorize: authorize ?? defaultAuthorize,
|
|
90
|
+
state,
|
|
91
|
+
input,
|
|
92
|
+
signal: runOptions?.signal,
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
finally {
|
|
96
|
+
running = false;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
},
|
|
100
|
+
state() {
|
|
101
|
+
return structuredClone(state);
|
|
102
|
+
},
|
|
103
|
+
exportState() {
|
|
104
|
+
if (running) {
|
|
105
|
+
throw new AtomsAgentError("CONFIG_ERROR", `Agent ${spec.id} cannot export state while a run is in progress.`);
|
|
106
|
+
}
|
|
107
|
+
return exportAgentState(state);
|
|
108
|
+
},
|
|
109
|
+
restoreState(snapshot) {
|
|
110
|
+
if (running) {
|
|
111
|
+
throw new AtomsAgentError("CONFIG_ERROR", `Agent ${spec.id} cannot restore state while a run is in progress.`);
|
|
112
|
+
}
|
|
113
|
+
const restored = restoreAgentState(snapshot);
|
|
114
|
+
state.messages = restored.messages;
|
|
115
|
+
},
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
//# sourceMappingURL=agent.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agent.js","sourceRoot":"","sources":["../src/agent.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAC;AACnD,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEzC,OAAO,EAAE,aAAa,EAAwB,MAAM,cAAc,CAAC;AAEnE,OAAO,EAAE,OAAO,EAAwB,MAAM,oBAAoB,CAAC;AAEnE,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAE7C,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAC/C,OAAO,EAAE,eAAe,EAAkB,MAAM,WAAW,CAAC;AAC5D,OAAO,EACL,WAAW,IAAI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AAY;wBAC/B,KAAK;wBACL,OAAO;wBACP,aAAa;wBACb,MAAM;wBACN,gBAAgB;wBAChB,SAAS,EAAE,SAAS,IAAI,gBAAgB;wBACxC,KAAK;wBACL,KAAK;wBACL,MAAM,EAAE,UAAU,EAAE,MAAM;qBAC3B,CAAC,CAAC;gBACL,CAAC;wBAAS,CAAC;oBACT,OAAO,GAAG,KAAK,CAAC;gBAClB,CAAC;YACH,CAAC;QACH,CAAC;QACD,KAAK;YACH,OAAO,eAAe,CAAC,KAAK,CAAC,CAAC;QAChC,CAAC;QACD,WAAW;YACT,IAAI,OAAO,EAAE,CAAC;gBACZ,MAAM,IAAI,eAAe,CACvB,cAAc,EACd,SAAS,IAAI,CAAC,EAAE,kDAAkD,CACnE,CAAC;YACJ,CAAC;YACD,OAAO,gBAAgB,CAAC,KAAK,CAAC,CAAC;QACjC,CAAC;QACD,YAAY,CAAC,QAAiB;YAC5B,IAAI,OAAO,EAAE,CAAC;gBACZ,MAAM,IAAI,eAAe,CACvB,cAAc,EACd,SAAS,IAAI,CAAC,EAAE,mDAAmD,CACpE,CAAC;YACJ,CAAC;YACD,MAAM,QAAQ,GAAG,iBAAiB,CAAC,QAAQ,CAAC,CAAC;YAC7C,KAAK,CAAC,QAAQ,GAAG,QAAQ,CAAC,QAAQ,CAAC;QACrC,CAAC;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { AgentMessage, JsonValue } from "@atoms-agent/llm";
|
|
2
|
+
export type PrepareContextInput = {
|
|
3
|
+
/** 完整消息历史(只读)。 */
|
|
4
|
+
history: readonly AgentMessage[];
|
|
5
|
+
systemPrompt: string;
|
|
6
|
+
/** AgentSpec Context Binding 的 JSON-safe 配置。 */
|
|
7
|
+
config?: JsonValue;
|
|
8
|
+
/** Run 级取消/超时信号。 */
|
|
9
|
+
signal: AbortSignal;
|
|
10
|
+
};
|
|
11
|
+
export type PreparedContext = {
|
|
12
|
+
/** 本次 Provider 请求实际使用的消息列表。 */
|
|
13
|
+
messages: AgentMessage[];
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* ContextStrategy —— 从完整历史生成本次模型调用的消息投影(PRD S4 / CFG-003)。
|
|
17
|
+
*
|
|
18
|
+
* 约束(AGENTS.md §10):
|
|
19
|
+
* - 不修改原始历史(核心不变量 8),只返回新列表。
|
|
20
|
+
* - 不执行 Tool、不访问 Provider。
|
|
21
|
+
* - 无法满足最小上下文时抛 CONTEXT_LIMIT_ERROR,而不是静默丢弃关键消息。
|
|
22
|
+
* - v0.1 不做 LLM Summary Compaction。
|
|
23
|
+
*/
|
|
24
|
+
export interface ContextStrategy {
|
|
25
|
+
prepare(input: PrepareContextInput): Promise<PreparedContext>;
|
|
26
|
+
}
|
|
27
|
+
export type SlidingWindowOptions = {
|
|
28
|
+
/** 估算 Token 预算上限;超出时从最旧的非首条 User 消息开始丢弃。 */
|
|
29
|
+
maxTokens: number;
|
|
30
|
+
};
|
|
31
|
+
/** 默认 Sliding Window / Token Budget 实现(任务 C103)。 */
|
|
32
|
+
export declare function slidingWindow(options: SlidingWindowOptions): ContextStrategy;
|
|
33
|
+
//# sourceMappingURL=context.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../src/context.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAGhE,MAAM,MAAM,mBAAmB,GAAG;IAChC,kBAAkB;IAClB,OAAO,EAAE,SAAS,YAAY,EAAE,CAAC;IACjC,YAAY,EAAE,MAAM,CAAC;IACrB,gDAAgD;IAChD,MAAM,CAAC,EAAE,SAAS,CAAC;IACnB,oBAAoB;IACpB,MAAM,EAAE,WAAW,CAAC;CACrB,CAAC;AAEF,MAAM,MAAM,eAAe,GAAG;IAC5B,+BAA+B;IAC/B,QAAQ,EAAE,YAAY,EAAE,CAAC;CAC1B,CAAC;AAEF;;;;;;;;GAQG;AACH,MAAM,WAAW,eAAe;IAC9B,OAAO,CAAC,KAAK,EAAE,mBAAmB,GAAG,OAAO,CAAC,eAAe,CAAC,CAAC;CAC/D;AAED,MAAM,MAAM,oBAAoB,GAAG;IACjC,4CAA4C;IAC5C,SAAS,EAAE,MAAM,CAAC;CACnB,CAAC;AAEF,oDAAoD;AACpD,wBAAgB,aAAa,CAAC,OAAO,EAAE,oBAAoB,GAAG,eAAe,CA4B5E"}
|
package/dist/context.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { AtomsAgentError } from "@atoms-agent/llm";
|
|
2
|
+
/** 默认 Sliding Window / Token Budget 实现(任务 C103)。 */
|
|
3
|
+
export function slidingWindow(options) {
|
|
4
|
+
if (!Number.isInteger(options.maxTokens) || options.maxTokens <= 0) {
|
|
5
|
+
throw new AtomsAgentError("CONFIG_ERROR", "Sliding window maxTokens must be positive.");
|
|
6
|
+
}
|
|
7
|
+
return {
|
|
8
|
+
async prepare(input) {
|
|
9
|
+
const groups = groupMessages(input.history);
|
|
10
|
+
const selected = [...groups];
|
|
11
|
+
while (selected.length > 2 &&
|
|
12
|
+
estimateContextTokens(input.systemPrompt, selected.flat()) > options.maxTokens) {
|
|
13
|
+
selected.splice(1, 1);
|
|
14
|
+
}
|
|
15
|
+
const messages = selected.flat();
|
|
16
|
+
if (estimateContextTokens(input.systemPrompt, messages) > options.maxTokens) {
|
|
17
|
+
throw new AtomsAgentError("CONTEXT_LIMIT_ERROR", "The system prompt and minimum conversation context exceed maxTokens.");
|
|
18
|
+
}
|
|
19
|
+
return { messages: structuredClone(messages) };
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
function groupMessages(history) {
|
|
24
|
+
const groups = [];
|
|
25
|
+
for (const message of history) {
|
|
26
|
+
if (groups.length === 0 || message.role === "user") {
|
|
27
|
+
groups.push([]);
|
|
28
|
+
}
|
|
29
|
+
groups.at(-1).push(message);
|
|
30
|
+
}
|
|
31
|
+
return groups;
|
|
32
|
+
}
|
|
33
|
+
function estimateContextTokens(systemPrompt, messages) {
|
|
34
|
+
const encoder = new TextEncoder();
|
|
35
|
+
const systemTokens = Math.ceil(encoder.encode(systemPrompt).byteLength / 4);
|
|
36
|
+
const messageTokens = messages.reduce((total, message) => {
|
|
37
|
+
const bytes = encoder.encode(JSON.stringify(message)).byteLength;
|
|
38
|
+
return total + Math.ceil(bytes / 4) + 4;
|
|
39
|
+
}, 0);
|
|
40
|
+
return systemTokens + messageTokens;
|
|
41
|
+
}
|
|
42
|
+
//# sourceMappingURL=context.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.js","sourceRoot":"","sources":["../src/context.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAC;AAmCnD,oDAAoD;AACpD,MAAM,UAAU,aAAa,CAAC,OAA6B;IACzD,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,OAAO,CAAC,SAAS,CAAC,IAAI,OAAO,CAAC,SAAS,IAAI,CAAC,EAAE,CAAC;QACnE,MAAM,IAAI,eAAe,CAAC,cAAc,EAAE,4CAA4C,CAAC,CAAC;IAC1F,CAAC;IAED,OAAO;QACL,KAAK,CAAC,OAAO,CAAC,KAA0B;YACtC,MAAM,MAAM,GAAG,aAAa,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;YAC5C,MAAM,QAAQ,GAAG,CAAC,GAAG,MAAM,CAAC,CAAC;YAE7B,OACE,QAAQ,CAAC,MAAM,GAAG,CAAC;gBACnB,qBAAqB,CAAC,KAAK,CAAC,YAAY,EAAE,QAAQ,CAAC,IAAI,EAAE,CAAC,GAAG,OAAO,CAAC,SAAS,EAC9E,CAAC;gBACD,QAAQ,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;YACxB,CAAC;YAED,MAAM,QAAQ,GAAG,QAAQ,CAAC,IAAI,EAAE,CAAC;YACjC,IAAI,q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CAAC;AACtC,CAAC"}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAC;AACnD,YAAY,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC"}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAC"}
|
package/dist/events.d.ts
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type { AgentErrorCode, AssistantMessage, JsonValue, Usage } from "@atoms-agent/llm";
|
|
2
|
+
import type { StructuredOutputResult } from "./structured-output.js";
|
|
3
|
+
/**
|
|
4
|
+
* Agent Event Stream —— Core 与 Host 之间唯一的运行时通信通道(PRD §8.4)。
|
|
5
|
+
*
|
|
6
|
+
* Event 用于流式消费和观察,不是 Durable Event Store。
|
|
7
|
+
* 顺序不变量(AGENTS.md §11):
|
|
8
|
+
* - 一个 Run 恰好一次 `agent_start` 与 `agent_end`。
|
|
9
|
+
* - `message_delta` 可以任意多条,但 `message_end` 必须包含完整消息。
|
|
10
|
+
* - Event 顺序可测试。
|
|
11
|
+
*/
|
|
12
|
+
export type AgentEvent = {
|
|
13
|
+
type: "agent_start";
|
|
14
|
+
runId: string;
|
|
15
|
+
agentId: string;
|
|
16
|
+
} | {
|
|
17
|
+
type: "turn_start";
|
|
18
|
+
runId: string;
|
|
19
|
+
turn: number;
|
|
20
|
+
} | {
|
|
21
|
+
type: "message_start";
|
|
22
|
+
runId: string;
|
|
23
|
+
turn: number;
|
|
24
|
+
} | {
|
|
25
|
+
type: "message_delta";
|
|
26
|
+
runId: string;
|
|
27
|
+
turn: number;
|
|
28
|
+
delta: string;
|
|
29
|
+
} | {
|
|
30
|
+
type: "message_end";
|
|
31
|
+
runId: string;
|
|
32
|
+
turn: number;
|
|
33
|
+
message: AssistantMessage;
|
|
34
|
+
usage: Usage;
|
|
35
|
+
} | {
|
|
36
|
+
type: "tool_start";
|
|
37
|
+
runId: string;
|
|
38
|
+
turn: number;
|
|
39
|
+
toolCallId: string;
|
|
40
|
+
toolName: string;
|
|
41
|
+
input: JsonValue;
|
|
42
|
+
} | {
|
|
43
|
+
type: "tool_end";
|
|
44
|
+
runId: string;
|
|
45
|
+
turn: number;
|
|
46
|
+
toolCallId: string;
|
|
47
|
+
toolName: string;
|
|
48
|
+
/** 截断治理后的结果文本;isError=true 时为分类错误说明。 */
|
|
49
|
+
content: string;
|
|
50
|
+
isError: boolean;
|
|
51
|
+
} | {
|
|
52
|
+
type: "turn_end";
|
|
53
|
+
runId: string;
|
|
54
|
+
turn: number;
|
|
55
|
+
} | {
|
|
56
|
+
type: "agent_end";
|
|
57
|
+
runId: string;
|
|
58
|
+
/** 正常完成为 "completed";其余对应终止原因。 */
|
|
59
|
+
stopReason: "completed" | "cancelled" | "max_turns" | "timeout" | "error";
|
|
60
|
+
finalText?: string;
|
|
61
|
+
structuredOutput?: StructuredOutputResult;
|
|
62
|
+
} | {
|
|
63
|
+
type: "error";
|
|
64
|
+
runId: string;
|
|
65
|
+
code: AgentErrorCode;
|
|
66
|
+
message: string;
|
|
67
|
+
};
|
|
68
|
+
//# sourceMappingURL=events.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"events.d.ts","sourceRoot":"","sources":["../src/events.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,gBAAgB,EAAE,SAAS,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAC;AAE3F,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,wBAAwB,CAAC;AAErE;;;;;;;;GAQG;AACH,MAAM,MAAM,UAAU,G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AAW,GAAG,WAAW,GAAG,SAAS,GAAG,OAAO,CAAC;IAC1E,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,gBAAgB,CAAC,EAAE,sBAAsB,CAAC;CAC3C,GACD;IAAE,IAAI,EAAE,OAAO,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,cAAc,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,CAAC"}
|
package/dist/events.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"events.js","sourceRoot":"","sources":["../src/events.ts"],"names":[],"mappings":""}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export { createAgent } from "./agent.js";
|
|
2
|
+
export type { Agent, CreateAgentOptions, InlineAgentOptions, SpecAgentOptions, RunOptions, } from "./agent.js";
|
|
3
|
+
export { defineTool } from "./tool.js";
|
|
4
|
+
export type { ToolDefinition, ToolContext, ToolSideEffect, ToolCallRequest, AuthorizeFn, AuthorizeDecision, } from "./tool.js";
|
|
5
|
+
export type { AgentSpec } from "./spec.js";
|
|
6
|
+
export { createRegistry } from "./registry.js";
|
|
7
|
+
export type { RuntimeRegistry } from "./registry.js";
|
|
8
|
+
export { slidingWindow } from "./context.js";
|
|
9
|
+
export type { ContextStrategy, PrepareContextInput, PreparedContext, SlidingWindowOptions, } from "./context.js";
|
|
10
|
+
export type { AgentEvent } from "./events.js";
|
|
11
|
+
export type { RunLimits } from "./limits.js";
|
|
12
|
+
export { AGENT_STATE_SCHEMA_VERSION, assertAgentStateSnapshot, exportState, restoreState, } from "./state.js";
|
|
13
|
+
export type { AgentState, AgentStateSnapshot } from "./state.js";
|
|
14
|
+
export type { StructuredOutputConfig, StructuredOutputDiagnostic, StructuredOutputResult, } from "./structured-output.js";
|
|
15
|
+
export { AtomsAgentError } from "./errors.js";
|
|
16
|
+
export type { AgentErrorCode } from "./errors.js";
|
|
17
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AACzC,YAAY,EACV,KAAK,EACL,kBAAkB,EAClB,kBAAkB,EAClB,gBAAgB,EAChB,UAAU,GACX,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AACvC,YAAY,EACV,cAAc,EACd,WAAW,EACX,cAAc,EACd,eAAe,EACf,WAAW,EACX,iBAAiB,GAClB,MAAM,WAAW,CAAC;AACnB,YAAY,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAC3C,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAC/C,YAAY,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AACrD,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAC7C,YAAY,EACV,eAAe,EACf,mBAAmB,EACnB,eAAe,EACf,oBAAoB,GACrB,MAAM,cAAc,CAAC;AACtB,YAAY,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAC9C,YAAY,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAC7C,OAAO,EACL,0BAA0B,EAC1B,wBAAwB,EACxB,WAAW,EACX,YAAY,GACb,MAAM,YAAY,CAAC;AACpB,YAAY,EAAE,UAAU,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AACjE,YAAY,EACV,sBAAsB,EACtB,0BAA0B,EAC1B,sBAAsB,GACvB,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAC9C,YAAY,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { createAgent } from "./agent.js";
|
|
2
|
+
export { defineTool } from "./tool.js";
|
|
3
|
+
export { createRegistry } from "./registry.js";
|
|
4
|
+
export { slidingWindow } from "./context.js";
|
|
5
|
+
export { AGENT_STATE_SCHEMA_VERSION, assertAgentStateSnapshot, exportState, restoreState, } from "./state.js";
|
|
6
|
+
export { AtomsAgentError } from "./errors.js";
|
|
7
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAQzC,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AAUvC,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAE/C,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAS7C,OAAO,EACL,0BAA0B,EAC1B,wBAAwB,EACxB,WAAW,EACX,YAAY,GACb,MAAM,YAAY,CAAC;AAOpB,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"abort.d.ts","sourceRoot":"","sources":["../../src/internal/abort.ts"],"names":[],"mappings":"AAEA,wBAAgB,cAAc,CAAC,CAAC,EAC9B,SAAS,EAAE,MAAM,CAAC,GAAG,WAAW,CAAC,CAAC,CAAC,EACnC,MAAM,EAAE,WAAW,EACnB,OAAO,EAAE,MAAM,GACd,OAAO,CAAC,CAAC,CAAC,CAgCZ"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { AtomsAgentError } from "@atoms-agent/llm";
|
|
2
|
+
export function awaitWithAbort(operation, signal, message) {
|
|
3
|
+
if (signal.aborted) {
|
|
4
|
+
return Promise.reject(cancellationError(message, signal));
|
|
5
|
+
}
|
|
6
|
+
return new Promise((resolve, reject) => {
|
|
7
|
+
let settled = false;
|
|
8
|
+
const finish = (callback) => {
|
|
9
|
+
if (settled) {
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
settled = true;
|
|
13
|
+
signal.removeEventListener("abort", onAbort);
|
|
14
|
+
callback();
|
|
15
|
+
};
|
|
16
|
+
const onAbort = () => {
|
|
17
|
+
finish(() => reject(cancellationError(message, signal)));
|
|
18
|
+
};
|
|
19
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
20
|
+
let pending;
|
|
21
|
+
try {
|
|
22
|
+
pending = Promise.resolve(operation());
|
|
23
|
+
}
|
|
24
|
+
catch (cause) {
|
|
25
|
+
finish(() => reject(cause));
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
pending.then((value) => finish(() => resolve(value)), (cause) => finish(() => reject(cause)));
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
function cancellationError(message, signal) {
|
|
32
|
+
return new AtomsAgentError("CANCELLED", message, { cause: signal.reason });
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=abort.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"abort.js","sourceRoot":"","sources":["../../src/internal/abort.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAC;AAEnD,MAAM,UAAU,cAAc,CAC5B,SAAmC,EACnC,MAAmB,EACnB,OAAe;IAEf,IAAI,MAAM,CAAC,OAAO,EAAE,CAAC;QACnB,OAAO,OAAO,CAAC,MAAM,CAAC,iBAAiB,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC,CAAC;IAC5D,CAAC;IAED,OAAO,IAAI,OAAO,CAAI,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QACxC,IAAI,OAAO,GAAG,KAAK,CAAC;QACpB,MAAM,MAAM,GAAG,CAAC,QAAoB,EAAQ,EAAE;YAC5C,IAAI,OAAO,EAAE,CAAC;gBACZ,OAAO;YACT,CAAC;YACD,OAAO,GAAG,IAAI,CAAC;YACf,MAAM,CAAC,mBAAmB,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;YAC7C,QAAQ,EAAE,CAAC;QACb,CAAC,CAAC;QACF,MAAM,OAAO,GAAG,GAAS,EAAE;YACzB,MAAM,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,iBAAiB,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC;QAC3D,CAAC,CAAC;QAEF,MAAM,CAAC,gBAAgB,CAAC,OAAO,EAAE,OAAO,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC;QAC1D,IAAI,OAAuB,CAAC;QAC5B,IAAI,CAAC;YACH,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,SAAS,EAAE,CAAC,CAAC;QACzC,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,MAAM,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;YAC5B,OAAO;QACT,CAAC;QACD,OAAO,CAAC,IAAI,CACV,CAAC,KAAK,EAAE,EAAE,CAAC,MAAM,CAAC,GAAG,EAAE,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,EACvC,CAAC,KAAc,EAAE,EAAE,CAAC,MAAM,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAChD,CAAC;IACJ,CAAC,CAAC,CAAC;AACL,CAAC;AAED,SAAS,iBAAiB,CAAC,OAAe,EAAE,MAAmB;IAC7D,OAAO,IAAI,eAAe,CAAC,WAAW,EAAE,OAAO,EAAE,EAAE,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC;AAC7E,CAAC"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { JsonValue, ModelAdapter } from "@atoms-agent/llm";
|
|
2
|
+
import type { ContextStrategy } from "../context.js";
|
|
3
|
+
import type { AgentEvent } from "../events.js";
|
|
4
|
+
import type { RunLimits } from "../limits.js";
|
|
5
|
+
import { type AgentState } from "../state.js";
|
|
6
|
+
import { type StructuredOutputRuntime } from "../structured-output.js";
|
|
7
|
+
import { type AuthorizeFn, type ToolDefinition } from "../tool.js";
|
|
8
|
+
export type LoopToolBinding = {
|
|
9
|
+
tool: ToolDefinition;
|
|
10
|
+
config?: JsonValue;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Agent Loop 内部入口(不对外导出)。
|
|
14
|
+
*
|
|
15
|
+
* 标准流程(PRD §8.3):
|
|
16
|
+
* prepare context -> model stream -> assistant message
|
|
17
|
+
* -> tool calls? validate -> authorize -> execute -> truncate -> results -> next turn
|
|
18
|
+
* -> final response
|
|
19
|
+
*
|
|
20
|
+
* 必须维护的核心不变量(PRD §11 / AGENTS.md §7):
|
|
21
|
+
* 1. 每个 Tool Call 在下一次模型调用前恰好一个 Tool Result。
|
|
22
|
+
* 2. Invalid/Denied Tool 执行次数为 0。
|
|
23
|
+
* 3. Cancel 后不启动新的模型请求或 Tool。
|
|
24
|
+
* 4. 不超过 maxTurns / timeoutMs(timeoutMs 为 Run 级 Deadline,经 AbortSignal 取消在途操作)。
|
|
25
|
+
* 5. Tool Output 进入模型前经过大小治理。
|
|
26
|
+
* 6. 一个 Run 恰好一次 agent_start / agent_end。
|
|
27
|
+
*/
|
|
28
|
+
export type LoopInput = {
|
|
29
|
+
runId: string;
|
|
30
|
+
agentId: string;
|
|
31
|
+
model: ModelAdapter;
|
|
32
|
+
systemPrompt: string;
|
|
33
|
+
tools: Map<string, LoopToolBinding>;
|
|
34
|
+
context: ContextStrategy;
|
|
35
|
+
contextConfig: JsonValue | undefined;
|
|
36
|
+
limits: Required<RunLimits>;
|
|
37
|
+
structuredOutput: StructuredOutputRuntime | undefined;
|
|
38
|
+
authorize: AuthorizeFn;
|
|
39
|
+
state: AgentState;
|
|
40
|
+
input: string;
|
|
41
|
+
signal?: AbortSignal;
|
|
42
|
+
};
|
|
43
|
+
/** 任务 C104(Loop/Event)与 C105(取消/Limits/Retry)实现。 */
|
|
44
|
+
export declare function runLoop(input: LoopInput): AsyncIterable<AgentEvent>;
|
|
45
|
+
//# sourceMappingURL=lo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