longer-agent 0.1.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 +227 -0
- package/README.zh-CN.md +227 -0
- package/agent_templates/executor/agent.yaml +22 -0
- package/agent_templates/executor/system_prompt.md +17 -0
- package/agent_templates/explorer/agent.yaml +13 -0
- package/agent_templates/explorer/system_prompt.md +19 -0
- package/agent_templates/main/agent.yaml +7 -0
- package/agent_templates/main/system_prompt.md +45 -0
- package/configExample.yaml +83 -0
- package/dist/agents/agent.d.ts +79 -0
- package/dist/agents/agent.d.ts.map +1 -0
- package/dist/agents/agent.js +156 -0
- package/dist/agents/agent.js.map +1 -0
- package/dist/agents/tool-loop.d.ts +140 -0
- package/dist/agents/tool-loop.d.ts.map +1 -0
- package/dist/agents/tool-loop.js +465 -0
- package/dist/agents/tool-loop.js.map +1 -0
- package/dist/ask.d.ts +81 -0
- package/dist/ask.d.ts.map +1 -0
- package/dist/ask.js +34 -0
- package/dist/ask.js.map +1 -0
- package/dist/auth/openai-oauth.d.ts +66 -0
- package/dist/auth/openai-oauth.d.ts.map +1 -0
- package/dist/auth/openai-oauth.js +640 -0
- package/dist/auth/openai-oauth.js.map +1 -0
- package/dist/cli.d.ts +14 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +254 -0
- package/dist/cli.js.map +1 -0
- package/dist/commands.d.ts +118 -0
- package/dist/commands.d.ts.map +1 -0
- package/dist/commands.js +862 -0
- package/dist/commands.js.map +1 -0
- package/dist/config.d.ts +130 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +648 -0
- package/dist/config.js.map +1 -0
- package/dist/context-rendering.d.ts +69 -0
- package/dist/context-rendering.d.ts.map +1 -0
- package/dist/context-rendering.js +250 -0
- package/dist/context-rendering.js.map +1 -0
- package/dist/document-projection.d.ts +12 -0
- package/dist/document-projection.d.ts.map +1 -0
- package/dist/document-projection.js +75 -0
- package/dist/document-projection.js.map +1 -0
- package/dist/ephemeral-log.d.ts +15 -0
- package/dist/ephemeral-log.d.ts.map +1 -0
- package/dist/ephemeral-log.js +173 -0
- package/dist/ephemeral-log.js.map +1 -0
- package/dist/file-attach.d.ts +89 -0
- package/dist/file-attach.d.ts.map +1 -0
- package/dist/file-attach.js +571 -0
- package/dist/file-attach.js.map +1 -0
- package/dist/index.d.ts +29 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +43 -0
- package/dist/index.js.map +1 -0
- package/dist/init-wizard.d.ts +13 -0
- package/dist/init-wizard.d.ts.map +1 -0
- package/dist/init-wizard.js +328 -0
- package/dist/init-wizard.js.map +1 -0
- package/dist/log-entry.d.ts +104 -0
- package/dist/log-entry.d.ts.map +1 -0
- package/dist/log-entry.js +292 -0
- package/dist/log-entry.js.map +1 -0
- package/dist/log-projection.d.ts +73 -0
- package/dist/log-projection.d.ts.map +1 -0
- package/dist/log-projection.js +651 -0
- package/dist/log-projection.js.map +1 -0
- package/dist/mcp-client.d.ts +55 -0
- package/dist/mcp-client.d.ts.map +1 -0
- package/dist/mcp-client.js +402 -0
- package/dist/mcp-client.js.map +1 -0
- package/dist/model-selection.d.ts +16 -0
- package/dist/model-selection.d.ts.map +1 -0
- package/dist/model-selection.js +181 -0
- package/dist/model-selection.js.map +1 -0
- package/dist/network-retry.d.ts +38 -0
- package/dist/network-retry.d.ts.map +1 -0
- package/dist/network-retry.js +140 -0
- package/dist/network-retry.js.map +1 -0
- package/dist/persistence.d.ts +104 -0
- package/dist/persistence.d.ts.map +1 -0
- package/dist/persistence.js +644 -0
- package/dist/persistence.js.map +1 -0
- package/dist/primitives/context.d.ts +29 -0
- package/dist/primitives/context.d.ts.map +1 -0
- package/dist/primitives/context.js +85 -0
- package/dist/primitives/context.js.map +1 -0
- package/dist/progress.d.ts +51 -0
- package/dist/progress.d.ts.map +1 -0
- package/dist/progress.js +229 -0
- package/dist/progress.js.map +1 -0
- package/dist/provider-presets.d.ts +34 -0
- package/dist/provider-presets.d.ts.map +1 -0
- package/dist/provider-presets.js +181 -0
- package/dist/provider-presets.js.map +1 -0
- package/dist/providers/anthropic.d.ts +32 -0
- package/dist/providers/anthropic.d.ts.map +1 -0
- package/dist/providers/anthropic.js +450 -0
- package/dist/providers/anthropic.js.map +1 -0
- package/dist/providers/base.d.ts +135 -0
- package/dist/providers/base.d.ts.map +1 -0
- package/dist/providers/base.js +104 -0
- package/dist/providers/base.js.map +1 -0
- package/dist/providers/glm.d.ts +18 -0
- package/dist/providers/glm.d.ts.map +1 -0
- package/dist/providers/glm.js +59 -0
- package/dist/providers/glm.js.map +1 -0
- package/dist/providers/kimi.d.ts +23 -0
- package/dist/providers/kimi.d.ts.map +1 -0
- package/dist/providers/kimi.js +89 -0
- package/dist/providers/kimi.js.map +1 -0
- package/dist/providers/minimax.d.ts +20 -0
- package/dist/providers/minimax.d.ts.map +1 -0
- package/dist/providers/minimax.js +192 -0
- package/dist/providers/minimax.js.map +1 -0
- package/dist/providers/openai-chat.d.ts +33 -0
- package/dist/providers/openai-chat.d.ts.map +1 -0
- package/dist/providers/openai-chat.js +543 -0
- package/dist/providers/openai-chat.js.map +1 -0
- package/dist/providers/openai-responses.d.ts +26 -0
- package/dist/providers/openai-responses.d.ts.map +1 -0
- package/dist/providers/openai-responses.js +443 -0
- package/dist/providers/openai-responses.js.map +1 -0
- package/dist/providers/openrouter.d.ts +24 -0
- package/dist/providers/openrouter.d.ts.map +1 -0
- package/dist/providers/openrouter.js +177 -0
- package/dist/providers/openrouter.js.map +1 -0
- package/dist/providers/registry.d.ts +7 -0
- package/dist/providers/registry.d.ts.map +1 -0
- package/dist/providers/registry.js +38 -0
- package/dist/providers/registry.js.map +1 -0
- package/dist/security/path.d.ts +51 -0
- package/dist/security/path.d.ts.map +1 -0
- package/dist/security/path.js +187 -0
- package/dist/security/path.js.map +1 -0
- package/dist/security/sensitive-files.d.ts +3 -0
- package/dist/security/sensitive-files.d.ts.map +1 -0
- package/dist/security/sensitive-files.js +41 -0
- package/dist/security/sensitive-files.js.map +1 -0
- package/dist/session.d.ts +446 -0
- package/dist/session.d.ts.map +1 -0
- package/dist/session.js +4595 -0
- package/dist/session.js.map +1 -0
- package/dist/settings.d.ts +46 -0
- package/dist/settings.d.ts.map +1 -0
- package/dist/settings.js +134 -0
- package/dist/settings.js.map +1 -0
- package/dist/show-context.d.ts +35 -0
- package/dist/show-context.d.ts.map +1 -0
- package/dist/show-context.js +320 -0
- package/dist/show-context.js.map +1 -0
- package/dist/skills/loader.d.ts +49 -0
- package/dist/skills/loader.d.ts.map +1 -0
- package/dist/skills/loader.js +166 -0
- package/dist/skills/loader.js.map +1 -0
- package/dist/summarize-context.d.ts +29 -0
- package/dist/summarize-context.d.ts.map +1 -0
- package/dist/summarize-context.js +247 -0
- package/dist/summarize-context.js.map +1 -0
- package/dist/templates/loader.d.ts +104 -0
- package/dist/templates/loader.d.ts.map +1 -0
- package/dist/templates/loader.js +514 -0
- package/dist/templates/loader.js.map +1 -0
- package/dist/tools/basic.d.ts +29 -0
- package/dist/tools/basic.d.ts.map +1 -0
- package/dist/tools/basic.js +2079 -0
- package/dist/tools/basic.js.map +1 -0
- package/dist/tools/comm.d.ts +17 -0
- package/dist/tools/comm.d.ts.map +1 -0
- package/dist/tools/comm.js +192 -0
- package/dist/tools/comm.js.map +1 -0
- package/dist/tools/web-fetch.d.ts +11 -0
- package/dist/tools/web-fetch.d.ts.map +1 -0
- package/dist/tools/web-fetch.js +237 -0
- package/dist/tools/web-fetch.js.map +1 -0
- package/dist/tools/web-search.d.ts +24 -0
- package/dist/tools/web-search.d.ts.map +1 -0
- package/dist/tools/web-search.js +51 -0
- package/dist/tools/web-search.js.map +1 -0
- package/dist/tui/app.d.ts +35 -0
- package/dist/tui/app.d.ts.map +1 -0
- package/dist/tui/app.js +1042 -0
- package/dist/tui/app.js.map +1 -0
- package/dist/tui/checkbox-picker.d.ts +35 -0
- package/dist/tui/checkbox-picker.d.ts.map +1 -0
- package/dist/tui/checkbox-picker.js +85 -0
- package/dist/tui/checkbox-picker.js.map +1 -0
- package/dist/tui/command-picker.d.ts +31 -0
- package/dist/tui/command-picker.d.ts.map +1 -0
- package/dist/tui/command-picker.js +113 -0
- package/dist/tui/command-picker.js.map +1 -0
- package/dist/tui/components/ask-panel.d.ts +21 -0
- package/dist/tui/components/ask-panel.d.ts.map +1 -0
- package/dist/tui/components/ask-panel.js +81 -0
- package/dist/tui/components/ask-panel.js.map +1 -0
- package/dist/tui/components/conversation-panel.d.ts +68 -0
- package/dist/tui/components/conversation-panel.d.ts.map +1 -0
- package/dist/tui/components/conversation-panel.js +611 -0
- package/dist/tui/components/conversation-panel.js.map +1 -0
- package/dist/tui/components/input-panel.d.ts +27 -0
- package/dist/tui/components/input-panel.d.ts.map +1 -0
- package/dist/tui/components/input-panel.js +725 -0
- package/dist/tui/components/input-panel.js.map +1 -0
- package/dist/tui/components/logo-panel.d.ts +14 -0
- package/dist/tui/components/logo-panel.d.ts.map +1 -0
- package/dist/tui/components/logo-panel.js +37 -0
- package/dist/tui/components/logo-panel.js.map +1 -0
- package/dist/tui/components/plan-panel.d.ts +10 -0
- package/dist/tui/components/plan-panel.d.ts.map +1 -0
- package/dist/tui/components/plan-panel.js +8 -0
- package/dist/tui/components/plan-panel.js.map +1 -0
- package/dist/tui/components/status-bar.d.ts +24 -0
- package/dist/tui/components/status-bar.d.ts.map +1 -0
- package/dist/tui/components/status-bar.js +80 -0
- package/dist/tui/components/status-bar.js.map +1 -0
- package/dist/tui/input/editor-state.d.ts +22 -0
- package/dist/tui/input/editor-state.d.ts.map +1 -0
- package/dist/tui/input/editor-state.js +157 -0
- package/dist/tui/input/editor-state.js.map +1 -0
- package/dist/tui/input/keymap.d.ts +3 -0
- package/dist/tui/input/keymap.d.ts.map +1 -0
- package/dist/tui/input/keymap.js +72 -0
- package/dist/tui/input/keymap.js.map +1 -0
- package/dist/tui/input/paste-slots.d.ts +17 -0
- package/dist/tui/input/paste-slots.d.ts.map +1 -0
- package/dist/tui/input/paste-slots.js +46 -0
- package/dist/tui/input/paste-slots.js.map +1 -0
- package/dist/tui/input/paste.d.ts +15 -0
- package/dist/tui/input/paste.d.ts.map +1 -0
- package/dist/tui/input/paste.js +35 -0
- package/dist/tui/input/paste.js.map +1 -0
- package/dist/tui/input/protocol.d.ts +9 -0
- package/dist/tui/input/protocol.d.ts.map +1 -0
- package/dist/tui/input/protocol.js +387 -0
- package/dist/tui/input/protocol.js.map +1 -0
- package/dist/tui/input/sanitize.d.ts +6 -0
- package/dist/tui/input/sanitize.d.ts.map +1 -0
- package/dist/tui/input/sanitize.js +20 -0
- package/dist/tui/input/sanitize.js.map +1 -0
- package/dist/tui/input/types.d.ts +18 -0
- package/dist/tui/input/types.d.ts.map +1 -0
- package/dist/tui/input/types.js +2 -0
- package/dist/tui/input/types.js.map +1 -0
- package/dist/tui/launch.d.ts +23 -0
- package/dist/tui/launch.d.ts.map +1 -0
- package/dist/tui/launch.js +104 -0
- package/dist/tui/launch.js.map +1 -0
- package/dist/tui/theme.d.ts +20 -0
- package/dist/tui/theme.d.ts.map +1 -0
- package/dist/tui/theme.js +29 -0
- package/dist/tui/theme.js.map +1 -0
- package/dist/tui/types.d.ts +136 -0
- package/dist/tui/types.d.ts.map +1 -0
- package/dist/tui/types.js +9 -0
- package/dist/tui/types.js.map +1 -0
- package/package.json +76 -0
- package/prompts/sections/agents_md.md +23 -0
- package/prompts/sections/important_log.md +16 -0
- package/prompts/sections/system_mechanisms.md +18 -0
- package/prompts/tools/apply_patch.md +31 -0
- package/prompts/tools/ask.md +18 -0
- package/prompts/tools/bash.md +13 -0
- package/prompts/tools/bash_background.md +9 -0
- package/prompts/tools/bash_output.md +9 -0
- package/prompts/tools/check_status.md +3 -0
- package/prompts/tools/diff.md +5 -0
- package/prompts/tools/edit_file.md +11 -0
- package/prompts/tools/glob.md +7 -0
- package/prompts/tools/grep.md +20 -0
- package/prompts/tools/kill_agent.md +3 -0
- package/prompts/tools/kill_shell.md +5 -0
- package/prompts/tools/list_dir.md +5 -0
- package/prompts/tools/plan.md +252 -0
- package/prompts/tools/read_file.md +9 -0
- package/prompts/tools/show_context.md +12 -0
- package/prompts/tools/skill.md +7 -0
- package/prompts/tools/spawn_agent.md +195 -0
- package/prompts/tools/summarize_context.md +122 -0
- package/prompts/tools/test.md +5 -0
- package/prompts/tools/wait.md +17 -0
- package/prompts/tools/web_fetch.md +9 -0
- package/prompts/tools/web_search.md +5 -0
- package/prompts/tools/write_file.md +11 -0
- package/skills/.staging/.gitkeep +0 -0
- package/skills/explain-code/SKILL.md +15 -0
- package/skills/skill-manager/SKILL.md +83 -0
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
# LongerAgent Configuration
|
|
2
|
+
#
|
|
3
|
+
# This file is auto-generated by `longeragent init`, but you can also
|
|
4
|
+
# create or edit it manually. Place it at: ~/.longeragent/config.yaml
|
|
5
|
+
#
|
|
6
|
+
# API keys can use ${ENV_VAR} syntax to read from environment variables,
|
|
7
|
+
# or be specified as literal strings (not recommended for security).
|
|
8
|
+
|
|
9
|
+
# Which model to use by default. Must match a name in the `models` section.
|
|
10
|
+
default_model: my-claude
|
|
11
|
+
|
|
12
|
+
models:
|
|
13
|
+
# ── Anthropic ──────────────────────────────────────────────────────
|
|
14
|
+
my-claude:
|
|
15
|
+
provider: anthropic
|
|
16
|
+
model: claude-sonnet-4-6 # or: claude-opus-4-6, claude-haiku-4-5
|
|
17
|
+
api_key: ${ANTHROPIC_API_KEY}
|
|
18
|
+
# temperature: 0.7 # default: 0.7
|
|
19
|
+
# max_tokens: 32000 # default: 32000
|
|
20
|
+
# thinking_budget: 10000 # thinking token budget (if model supports thinking)
|
|
21
|
+
|
|
22
|
+
# ── OpenAI ─────────────────────────────────────────────────────────
|
|
23
|
+
# my-openai:
|
|
24
|
+
# provider: openai # uses Responses API (web search, reasoning)
|
|
25
|
+
# model: gpt-5.2 # or: gpt-5.2-codex, gpt-5.3-codex, gpt-5.4
|
|
26
|
+
# api_key: ${OPENAI_API_KEY}
|
|
27
|
+
|
|
28
|
+
# my-openai-chat:
|
|
29
|
+
# provider: openai-chat # uses Chat Completions API
|
|
30
|
+
# model: gpt-5.2 # or: gpt-5.2-codex, gpt-5.3-codex, gpt-5.4
|
|
31
|
+
# api_key: ${OPENAI_API_KEY}
|
|
32
|
+
|
|
33
|
+
# ── Kimi (Moonshot) ────────────────────────────────────────────────
|
|
34
|
+
# my-kimi:
|
|
35
|
+
# provider: kimi-cn # or: kimi / kimi-ai (international)
|
|
36
|
+
# model: kimi-k2.5
|
|
37
|
+
# api_key: ${KIMI_CN_API_KEY} # international: ${KIMI_API_KEY}
|
|
38
|
+
|
|
39
|
+
# ── MiniMax ────────────────────────────────────────────────────────
|
|
40
|
+
# my-minimax:
|
|
41
|
+
# provider: minimax
|
|
42
|
+
# model: MiniMax-M2.5 # or: MiniMax-M2.1, MiniMax-M1-80k
|
|
43
|
+
# api_key: ${MINIMAX_API_KEY}
|
|
44
|
+
|
|
45
|
+
# ── GLM (Zhipu) ───────────────────────────────────────────────────
|
|
46
|
+
# my-glm:
|
|
47
|
+
# provider: glm
|
|
48
|
+
# model: glm-5 # or: glm-4.7
|
|
49
|
+
# api_key: ${GLM_API_KEY}
|
|
50
|
+
|
|
51
|
+
# ── OpenRouter ─────────────────────────────────────────────────────
|
|
52
|
+
# my-openrouter:
|
|
53
|
+
# provider: openrouter
|
|
54
|
+
# model: anthropic/claude-sonnet-4.6 # or: anthropic/claude-haiku-4.5, openai/gpt-5.2-codex, openai/gpt-5.3-codex, minimax/minimax-m2.1
|
|
55
|
+
# api_key: ${OPENROUTER_API_KEY}
|
|
56
|
+
# http_referer: https://myapp.example.com # optional
|
|
57
|
+
# x_title: MyApp # optional
|
|
58
|
+
|
|
59
|
+
# ── Sub-agent model override (optional) ──────────────────────────────
|
|
60
|
+
# When set, all sub-agents use this model instead of the primary agent's.
|
|
61
|
+
# Must reference a model name defined above.
|
|
62
|
+
# sub_agent_model: my-openai
|
|
63
|
+
|
|
64
|
+
# ── MCP Servers (optional) ───────────────────────────────────────────
|
|
65
|
+
# LongerAgent acts as an MCP client: connects to these servers and
|
|
66
|
+
# makes their tools available to agents.
|
|
67
|
+
#
|
|
68
|
+
# mcp_servers:
|
|
69
|
+
# filesystem:
|
|
70
|
+
# transport: stdio
|
|
71
|
+
# command: npx
|
|
72
|
+
# args: ["-y", "@modelcontextprotocol/server-filesystem", "/path/to/dir"]
|
|
73
|
+
#
|
|
74
|
+
# brave-search:
|
|
75
|
+
# transport: stdio
|
|
76
|
+
# command: python
|
|
77
|
+
# args: ["-m", "mcp_server_brave_search"]
|
|
78
|
+
# env:
|
|
79
|
+
# BRAVE_API_KEY: ${BRAVE_API_KEY}
|
|
80
|
+
#
|
|
81
|
+
# remote-tools:
|
|
82
|
+
# transport: sse
|
|
83
|
+
# url: http://localhost:8080/mcp
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Agent — the core execution unit.
|
|
3
|
+
*
|
|
4
|
+
* An Agent wraps a model + system prompt + tools into a callable unit.
|
|
5
|
+
* Supports both stateless single-shot and stateful multi-turn execution.
|
|
6
|
+
*/
|
|
7
|
+
import type { Config, ModelConfig } from "../config.js";
|
|
8
|
+
import type { MessageBlock } from "../primitives/context.js";
|
|
9
|
+
import type { ToolDef } from "../providers/base.js";
|
|
10
|
+
import type { LogEntry } from "../log-entry.js";
|
|
11
|
+
import { type BeforeToolExecuteCallback, type OnToolCallCallback, type ToolExecutor, type ToolLoopResult } from "./tool-loop.js";
|
|
12
|
+
export declare const NO_REPLY_MARKER = "<NO_REPLY>";
|
|
13
|
+
/** Check if model output text is the `<NO_REPLY>` marker. */
|
|
14
|
+
export declare function isNoReply(text: string): boolean;
|
|
15
|
+
export interface AgentResult {
|
|
16
|
+
text: string;
|
|
17
|
+
toolHistory: Array<Record<string, unknown>>;
|
|
18
|
+
totalUsage: {
|
|
19
|
+
inputTokens: number;
|
|
20
|
+
outputTokens: number;
|
|
21
|
+
};
|
|
22
|
+
noReply: boolean;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* A callable intelligent unit: model + system prompt + tools.
|
|
26
|
+
*
|
|
27
|
+
* Supports two construction styles:
|
|
28
|
+
*
|
|
29
|
+
* **Explicit:**
|
|
30
|
+
* new Agent({ name: "Coder", modelConfig, systemPrompt: "...", tools })
|
|
31
|
+
*
|
|
32
|
+
* **Simplified:**
|
|
33
|
+
* new Agent({ name: "Coder", role: "...", model: "claude-sonnet", config })
|
|
34
|
+
*/
|
|
35
|
+
export declare class Agent {
|
|
36
|
+
name: string;
|
|
37
|
+
description: string;
|
|
38
|
+
systemPrompt: string;
|
|
39
|
+
tools: ToolDef[];
|
|
40
|
+
maxToolRounds: number;
|
|
41
|
+
modelConfig: ModelConfig;
|
|
42
|
+
private _provider;
|
|
43
|
+
constructor(opts: {
|
|
44
|
+
name: string;
|
|
45
|
+
modelConfig?: ModelConfig;
|
|
46
|
+
systemPrompt?: string;
|
|
47
|
+
tools?: ToolDef[];
|
|
48
|
+
maxToolRounds?: number;
|
|
49
|
+
role?: string;
|
|
50
|
+
model?: string;
|
|
51
|
+
config?: Config;
|
|
52
|
+
description?: string;
|
|
53
|
+
});
|
|
54
|
+
/**
|
|
55
|
+
* Replace this agent's model config and recreate the provider.
|
|
56
|
+
* Used for runtime model switching (e.g., /model command).
|
|
57
|
+
* Only safe to call between turns (not while a turn is in progress).
|
|
58
|
+
*/
|
|
59
|
+
replaceModelConfig(newConfig: ModelConfig): void;
|
|
60
|
+
/**
|
|
61
|
+
* Single-shot async execution (stateless).
|
|
62
|
+
*
|
|
63
|
+
* Builds a fresh message list with system prompt + user input,
|
|
64
|
+
* runs the tool loop, and returns the result.
|
|
65
|
+
*/
|
|
66
|
+
asyncRun(userInput: string | MessageBlock, extraMessages?: Array<Record<string, unknown>>, toolExecutors?: Record<string, ToolExecutor>, onToolCall?: OnToolCallCallback, signal?: AbortSignal): Promise<AgentResult>;
|
|
67
|
+
/**
|
|
68
|
+
* Run the tool loop with callback-based message management.
|
|
69
|
+
*
|
|
70
|
+
* The caller provides getMessages/appendEntry/allocId callbacks.
|
|
71
|
+
* Main agent: backed by structured log.
|
|
72
|
+
* Sub-agents: backed by an ephemeral structured log.
|
|
73
|
+
*/
|
|
74
|
+
asyncRunWithMessages(getMessages: () => Array<Record<string, unknown>>, appendEntry: (entry: LogEntry) => void, allocId: (type: LogEntry["type"]) => string, turnIndex: number, baseRoundIndex?: number, toolExecutors?: Record<string, ToolExecutor>, onToolCall?: OnToolCallCallback, onTextChunk?: (roundIndex: number, chunk: string) => boolean | void, onReasoningChunk?: (roundIndex: number, chunk: string) => boolean | void, signal?: AbortSignal, contextIdAllocator?: (roundIndex: number) => string, compactCheck?: (inputTokens: number, outputTokens: number, hasToolCalls: boolean) => {
|
|
75
|
+
compactNeeded: boolean;
|
|
76
|
+
scenario?: "output" | "toolcall";
|
|
77
|
+
} | null, onTokenUpdate?: (inputTokens: number, usage?: import("../providers/base.js").Usage) => void, thinkingLevel?: string, cacheEnabled?: boolean, onSaveCheckpoint?: () => void, beforeToolExecute?: BeforeToolExecuteCallback, getNotification?: () => string | null, streamCallbacksOwnEntries?: boolean, onRetryAttempt?: (attempt: number, maxRetries: number, delaySec: number, errMsg: string) => void, onRetrySuccess?: (attempt: number) => void, onRetryExhausted?: (maxRetries: number, errMsg: string) => void): Promise<ToolLoopResult>;
|
|
78
|
+
}
|
|
79
|
+
//# sourceMappingURL=agent.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agent.d.ts","sourceRoot":"","sources":["../../src/agents/agent.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AACxD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AAC7D,OAAO,KAAK,EAAgB,OAAO,EAAE,MAAM,sBAAsB,CAAC;AAIlE,OAAO,KAAK,EAAE,QAAQ,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E,KAAK,KAAK,IAAI,EAC3F,aAAa,CAAC,EAAE,MAAM,EACtB,YAAY,CAAC,EAAE,OAAO,EACtB,gBAAgB,CAAC,EAAE,MAAM,IAAI,EAC7B,iBAAiB,CAAC,EAAE,yBAAyB,EAC7C,eAAe,CAAC,EAAE,MAAM,MAAM,GAAG,IAAI,EACrC,yBAAyB,CAAC,EAAE,OAAO,EACnC,cAAc,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,KAAK,IAAI,EAChG,cAAc,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,EAC1C,gBAAgB,CAAC,EAAE,CAAC,UAAU,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,KAAK,IAAI,GAC9D,OAAO,CAAC,cAAc,CAAC;CA+B3B"}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Agent — the core execution unit.
|
|
3
|
+
*
|
|
4
|
+
* An Agent wraps a model + system prompt + tools into a callable unit.
|
|
5
|
+
* Supports both stateless single-shot and stateful multi-turn execution.
|
|
6
|
+
*/
|
|
7
|
+
import { createProvider } from "../providers/registry.js";
|
|
8
|
+
import { executeTool } from "../tools/basic.js";
|
|
9
|
+
import { createEphemeralLogState } from "../ephemeral-log.js";
|
|
10
|
+
import { asyncRunToolLoop, } from "./tool-loop.js";
|
|
11
|
+
// ------------------------------------------------------------------
|
|
12
|
+
// Output prefix detection
|
|
13
|
+
// ------------------------------------------------------------------
|
|
14
|
+
export const NO_REPLY_MARKER = "<NO_REPLY>";
|
|
15
|
+
/** Check if model output text is the `<NO_REPLY>` marker. */
|
|
16
|
+
export function isNoReply(text) {
|
|
17
|
+
return text.trim() === NO_REPLY_MARKER;
|
|
18
|
+
}
|
|
19
|
+
// ------------------------------------------------------------------
|
|
20
|
+
// Agent class
|
|
21
|
+
// ------------------------------------------------------------------
|
|
22
|
+
/**
|
|
23
|
+
* A callable intelligent unit: model + system prompt + tools.
|
|
24
|
+
*
|
|
25
|
+
* Supports two construction styles:
|
|
26
|
+
*
|
|
27
|
+
* **Explicit:**
|
|
28
|
+
* new Agent({ name: "Coder", modelConfig, systemPrompt: "...", tools })
|
|
29
|
+
*
|
|
30
|
+
* **Simplified:**
|
|
31
|
+
* new Agent({ name: "Coder", role: "...", model: "claude-sonnet", config })
|
|
32
|
+
*/
|
|
33
|
+
export class Agent {
|
|
34
|
+
name;
|
|
35
|
+
description;
|
|
36
|
+
systemPrompt;
|
|
37
|
+
tools;
|
|
38
|
+
maxToolRounds;
|
|
39
|
+
modelConfig;
|
|
40
|
+
_provider;
|
|
41
|
+
constructor(opts) {
|
|
42
|
+
this.name = opts.name;
|
|
43
|
+
this.description = opts.description ?? "";
|
|
44
|
+
this.systemPrompt = opts.role && !opts.systemPrompt
|
|
45
|
+
? opts.role
|
|
46
|
+
: opts.systemPrompt ?? "";
|
|
47
|
+
this.tools = opts.tools ?? [];
|
|
48
|
+
this.maxToolRounds = opts.maxToolRounds ?? 25;
|
|
49
|
+
// Resolve modelConfig
|
|
50
|
+
if (opts.modelConfig) {
|
|
51
|
+
this.modelConfig = opts.modelConfig;
|
|
52
|
+
}
|
|
53
|
+
else if (opts.model && opts.config) {
|
|
54
|
+
this.modelConfig = opts.config.getModel(opts.model);
|
|
55
|
+
}
|
|
56
|
+
else if (opts.model) {
|
|
57
|
+
throw new Error("Agent: 'config' is required when using the 'model' shorthand.");
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
throw new Error("Agent: either 'modelConfig' or 'model'+'config' must be provided.");
|
|
61
|
+
}
|
|
62
|
+
this._provider = createProvider(this.modelConfig);
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Replace this agent's model config and recreate the provider.
|
|
66
|
+
* Used for runtime model switching (e.g., /model command).
|
|
67
|
+
* Only safe to call between turns (not while a turn is in progress).
|
|
68
|
+
*/
|
|
69
|
+
replaceModelConfig(newConfig) {
|
|
70
|
+
this.modelConfig = newConfig;
|
|
71
|
+
this._provider = createProvider(newConfig);
|
|
72
|
+
}
|
|
73
|
+
// ------------------------------------------------------------------
|
|
74
|
+
// Async methods
|
|
75
|
+
// ------------------------------------------------------------------
|
|
76
|
+
/**
|
|
77
|
+
* Single-shot async execution (stateless).
|
|
78
|
+
*
|
|
79
|
+
* Builds a fresh message list with system prompt + user input,
|
|
80
|
+
* runs the tool loop, and returns the result.
|
|
81
|
+
*/
|
|
82
|
+
async asyncRun(userInput, extraMessages, toolExecutors, onToolCall, signal) {
|
|
83
|
+
const rendered = typeof userInput === "string"
|
|
84
|
+
? userInput
|
|
85
|
+
: userInput.render();
|
|
86
|
+
const initialMessages = [
|
|
87
|
+
{ role: "system", content: this.systemPrompt },
|
|
88
|
+
];
|
|
89
|
+
if (extraMessages) {
|
|
90
|
+
initialMessages.push(...extraMessages);
|
|
91
|
+
}
|
|
92
|
+
initialMessages.push({ role: "user", content: rendered });
|
|
93
|
+
const runtime = createEphemeralLogState(initialMessages, {
|
|
94
|
+
requiresAlternatingRoles: this._provider.requiresAlternatingRoles,
|
|
95
|
+
});
|
|
96
|
+
const result = await asyncRunToolLoop({
|
|
97
|
+
provider: this._provider,
|
|
98
|
+
getMessages: runtime.getMessages,
|
|
99
|
+
appendEntry: runtime.appendEntry,
|
|
100
|
+
allocId: runtime.allocId,
|
|
101
|
+
turnIndex: 0,
|
|
102
|
+
tools: this.tools.length > 0 ? this.tools : undefined,
|
|
103
|
+
toolExecutors: toolExecutors ?? {},
|
|
104
|
+
maxRounds: this.maxToolRounds,
|
|
105
|
+
agentName: this.name,
|
|
106
|
+
onToolCall,
|
|
107
|
+
builtinExecutor: executeTool,
|
|
108
|
+
signal,
|
|
109
|
+
});
|
|
110
|
+
return {
|
|
111
|
+
text: result.text,
|
|
112
|
+
toolHistory: result.toolHistory,
|
|
113
|
+
totalUsage: result.totalUsage,
|
|
114
|
+
noReply: isNoReply(result.text),
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Run the tool loop with callback-based message management.
|
|
119
|
+
*
|
|
120
|
+
* The caller provides getMessages/appendEntry/allocId callbacks.
|
|
121
|
+
* Main agent: backed by structured log.
|
|
122
|
+
* Sub-agents: backed by an ephemeral structured log.
|
|
123
|
+
*/
|
|
124
|
+
async asyncRunWithMessages(getMessages, appendEntry, allocId, turnIndex, baseRoundIndex, toolExecutors, onToolCall, onTextChunk, onReasoningChunk, signal, contextIdAllocator, compactCheck, onTokenUpdate, thinkingLevel, cacheEnabled, onSaveCheckpoint, beforeToolExecute, getNotification, streamCallbacksOwnEntries, onRetryAttempt, onRetrySuccess, onRetryExhausted) {
|
|
125
|
+
return asyncRunToolLoop({
|
|
126
|
+
provider: this._provider,
|
|
127
|
+
getMessages,
|
|
128
|
+
appendEntry,
|
|
129
|
+
allocId,
|
|
130
|
+
turnIndex,
|
|
131
|
+
baseRoundIndex,
|
|
132
|
+
tools: this.tools.length > 0 ? this.tools : undefined,
|
|
133
|
+
toolExecutors: toolExecutors ?? {},
|
|
134
|
+
maxRounds: this.maxToolRounds,
|
|
135
|
+
agentName: this.name,
|
|
136
|
+
onToolCall,
|
|
137
|
+
onTextChunk,
|
|
138
|
+
onReasoningChunk,
|
|
139
|
+
builtinExecutor: executeTool,
|
|
140
|
+
signal,
|
|
141
|
+
contextIdAllocator,
|
|
142
|
+
onTokenUpdate,
|
|
143
|
+
compactCheck,
|
|
144
|
+
thinkingLevel,
|
|
145
|
+
cacheEnabled,
|
|
146
|
+
onSaveCheckpoint,
|
|
147
|
+
beforeToolExecute,
|
|
148
|
+
getNotification,
|
|
149
|
+
streamCallbacksOwnEntries,
|
|
150
|
+
onRetryAttempt,
|
|
151
|
+
onRetrySuccess,
|
|
152
|
+
onRetryExhausted,
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
//# sourceMappingURL=agent.js.map
|
|
@@ -0,0 +1 @@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IAAI,CAAC,SAAS;YACxB,WAAW;YACX,WAAW;YACX,OAAO;YACP,SAAS;YACT,cAAc;YACd,KAAK,EAAE,IAAI,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS;YACrD,aAAa,EAAE,aAAa,IAAI,EAAE;YAClC,SAAS,EAAE,IAAI,CAAC,aAAa;YAC7B,SAAS,EAAE,IAAI,CAAC,IAAI;YACpB,UAAU;YACV,WAAW;YACX,gBAAgB;YAChB,eAAe,EAAE,WAAW;YAC5B,MAAM;YACN,kBAAkB;YAClB,aAAa;YACb,YAAY;YACZ,aAAa;YACb,YAAY;YACZ,gBAAgB;YAChB,iBAAiB;YACjB,eAAe;YACf,yBAAyB;YACzB,cAAc;YACd,cAAc;YACd,gBAAgB;SACjB,CAAC,CAAC;IACL,CAAC;CACF"}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared tool loop logic for Agent.
|
|
3
|
+
*
|
|
4
|
+
* Provides the async LLM <-> tool round-trip cycle. Calls the provider,
|
|
5
|
+
* executes tool calls, appends results via callbacks, and repeats until
|
|
6
|
+
* the model responds without tool calls or max rounds are reached.
|
|
7
|
+
*
|
|
8
|
+
* v2: operates through callbacks (getMessages / appendEntry) instead of
|
|
9
|
+
* directly mutating provider messages. The backing store can be the
|
|
10
|
+
* structured session log (main agent) or an ephemeral structured log
|
|
11
|
+
* (sub-agents / stateless runs).
|
|
12
|
+
*/
|
|
13
|
+
import type { BaseProvider, ToolDef, ToolResult } from "../providers/base.js";
|
|
14
|
+
import type { LogEntry } from "../log-entry.js";
|
|
15
|
+
import type { AskRequest } from "../ask.js";
|
|
16
|
+
/**
|
|
17
|
+
* A tool executor receives the arguments dict and returns either
|
|
18
|
+
* a plain string or a ToolResult. May be sync or async.
|
|
19
|
+
*/
|
|
20
|
+
export type ToolExecutor = (args: Record<string, unknown>) => ToolResult | string | Promise<ToolResult | string>;
|
|
21
|
+
/** Generate a one-line summary from a ToolDef.summaryTemplate. */
|
|
22
|
+
export declare function generateToolSummary(agentName: string, toolName: string, toolArgs: Record<string, unknown>, summaryTemplate: string): string;
|
|
23
|
+
export declare function generateToolCallDisplay(toolName: string, toolArgs: Record<string, unknown>): string;
|
|
24
|
+
export interface ToolLoopResult {
|
|
25
|
+
text: string;
|
|
26
|
+
toolHistory: Array<Record<string, unknown>>;
|
|
27
|
+
totalUsage: {
|
|
28
|
+
inputTokens: number;
|
|
29
|
+
outputTokens: number;
|
|
30
|
+
};
|
|
31
|
+
intermediateText: string[];
|
|
32
|
+
lastInputTokens: number;
|
|
33
|
+
reasoningContent: string;
|
|
34
|
+
reasoningState: unknown;
|
|
35
|
+
/** Flat context_id of the last tool-call round (undefined if no tool calls). */
|
|
36
|
+
lastRoundId?: string;
|
|
37
|
+
/** Whether the tool loop detected that compact is needed. */
|
|
38
|
+
compactNeeded?: boolean;
|
|
39
|
+
/** Which scenario triggered compact: "output" (no tool calls) or "toolcall" (after tool execution). */
|
|
40
|
+
compactScenario?: "output" | "toolcall";
|
|
41
|
+
/** Total tokens (input + output) from the last provider call. */
|
|
42
|
+
lastTotalTokens?: number;
|
|
43
|
+
/** Whether the final assistant text was already materialized by stream callbacks. */
|
|
44
|
+
textHandledInLog?: boolean;
|
|
45
|
+
/** Whether the final reasoning content was already materialized by stream callbacks. */
|
|
46
|
+
reasoningHandledInLog?: boolean;
|
|
47
|
+
/** Suspended on an ask tool call that requires user input. */
|
|
48
|
+
suspendedAsk?: {
|
|
49
|
+
ask: AskRequest;
|
|
50
|
+
toolCallId: string;
|
|
51
|
+
roundIndex: number;
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
export type OnToolCallCallback = (agentName: string, toolName: string, toolArgs: Record<string, unknown>, summary: string) => void;
|
|
55
|
+
export interface ToolPreflightContext {
|
|
56
|
+
agentName: string;
|
|
57
|
+
toolName: string;
|
|
58
|
+
toolArgs: Record<string, unknown>;
|
|
59
|
+
toolCallId: string;
|
|
60
|
+
summary: string;
|
|
61
|
+
}
|
|
62
|
+
export type ToolPreflightDecision = {
|
|
63
|
+
kind: "allow";
|
|
64
|
+
} | {
|
|
65
|
+
kind: "deny";
|
|
66
|
+
message: string;
|
|
67
|
+
};
|
|
68
|
+
export type BeforeToolExecuteCallback = (ctx: ToolPreflightContext) => ToolPreflightDecision | void | Promise<ToolPreflightDecision | void>;
|
|
69
|
+
export interface ToolLoopOptions {
|
|
70
|
+
provider: BaseProvider;
|
|
71
|
+
/**
|
|
72
|
+
* Returns the current API message sequence for the provider.
|
|
73
|
+
* Called before each provider call.
|
|
74
|
+
* Main agent: projects from _log; sub-agents: returns local array.
|
|
75
|
+
*/
|
|
76
|
+
getMessages: () => Array<Record<string, unknown>>;
|
|
77
|
+
/**
|
|
78
|
+
* Append a LogEntry to the backing store.
|
|
79
|
+
* Main agent: appends to _log; sub-agents: converts to raw msg and pushes.
|
|
80
|
+
*/
|
|
81
|
+
appendEntry: (entry: LogEntry) => void;
|
|
82
|
+
/** Allocate the next entry ID. */
|
|
83
|
+
allocId: (type: LogEntry["type"]) => string;
|
|
84
|
+
/** Current turn index (for entry creation). */
|
|
85
|
+
turnIndex: number;
|
|
86
|
+
/** Base round index for this activation within the current turn. */
|
|
87
|
+
baseRoundIndex?: number;
|
|
88
|
+
tools?: ToolDef[];
|
|
89
|
+
toolExecutors: Record<string, ToolExecutor>;
|
|
90
|
+
maxRounds: number;
|
|
91
|
+
agentName?: string;
|
|
92
|
+
onToolCall?: OnToolCallCallback;
|
|
93
|
+
toolsMap?: Record<string, ToolDef>;
|
|
94
|
+
onTextChunk?: (roundIndex: number, chunk: string) => boolean | void;
|
|
95
|
+
onReasoningChunk?: (roundIndex: number, chunk: string) => boolean | void;
|
|
96
|
+
/** Fallback executor for tools not found in toolExecutors. */
|
|
97
|
+
builtinExecutor?: (name: string, args: Record<string, unknown>) => Promise<ToolResult | string>;
|
|
98
|
+
/** Abort signal for cancellation. */
|
|
99
|
+
signal?: AbortSignal;
|
|
100
|
+
/** Allocator that returns the round's context_id. Provided by Session for context ID tracking. */
|
|
101
|
+
contextIdAllocator?: (roundIndex: number) => string;
|
|
102
|
+
/** Called after each provider response with the latest input token count and full Usage. */
|
|
103
|
+
onTokenUpdate?: (inputTokens: number, usage?: import("../providers/base.js").Usage) => void;
|
|
104
|
+
/**
|
|
105
|
+
* Callback to check whether compact is needed after each provider call.
|
|
106
|
+
* Returns { compactNeeded, scenario } or null to skip.
|
|
107
|
+
* When undefined, no compact checking is performed (e.g. sub-agents).
|
|
108
|
+
*/
|
|
109
|
+
compactCheck?: (inputTokens: number, outputTokens: number, hasToolCalls: boolean) => {
|
|
110
|
+
compactNeeded: boolean;
|
|
111
|
+
scenario?: "output" | "toolcall";
|
|
112
|
+
} | null;
|
|
113
|
+
/** Unified thinking level override (passed to provider). */
|
|
114
|
+
thinkingLevel?: string;
|
|
115
|
+
/** Whether to enable provider-specific prompt caching. */
|
|
116
|
+
cacheEnabled?: boolean;
|
|
117
|
+
/** Called after each tool_result is appended, for incremental persistence. */
|
|
118
|
+
onSaveCheckpoint?: () => void;
|
|
119
|
+
/** Optional preflight gate before executing a tool call (may ask/pause/deny). */
|
|
120
|
+
beforeToolExecute?: BeforeToolExecuteCallback;
|
|
121
|
+
/** Returns a notification string to append to tool_result content, or null if none. */
|
|
122
|
+
getNotification?: () => string | null;
|
|
123
|
+
/** When true, streamed text/reasoning callbacks own the corresponding log entries. */
|
|
124
|
+
streamCallbacksOwnEntries?: boolean;
|
|
125
|
+
/** Called when a network error is detected and a retry is being attempted. */
|
|
126
|
+
onRetryAttempt?: (attempt: number, maxRetries: number, delaySec: number, errMsg: string) => void;
|
|
127
|
+
/** Called when a retried network call succeeds. */
|
|
128
|
+
onRetrySuccess?: (attempt: number) => void;
|
|
129
|
+
/** Called when all network retries have been exhausted. */
|
|
130
|
+
onRetryExhausted?: (maxRetries: number, errMsg: string) => void;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Async tool loop: call LLM, execute tools, repeat until done.
|
|
134
|
+
*
|
|
135
|
+
* Tool executors are called with their arguments dict and may be
|
|
136
|
+
* sync or async. Exceptions are caught and returned as error
|
|
137
|
+
* ToolResult content.
|
|
138
|
+
*/
|
|
139
|
+
export declare function asyncRunToolLoop(opts: ToolLoopOptions): Promise<ToolLoopResult>;
|
|
140
|
+
//# sourceMappingURL=tool-loop.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool-loop.d.ts","sourceRoot":"","sources":["../../src/agents/tool-loop.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,KAAK,EACV,YAAY,EAEZ,OAAO,EACP,UAAU,EACX,MAAM,sBAAsB,CAAC;AAQ9B,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAOhD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AAM5C;;;GAGG;AACH,MAAM,MAAM,YAAY,GAAG,CACzB,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAC1B,UAAU,GAAG,MAAM,GAAG,OAAO,CAAC,UAAU,GAAG,MAAM,CAAC,CAAC;AAMxD,kEAAkE;AAClE,wBAAgB,mBAAmB,CACjC,SAAS,EAAE,MAAM,EACjB,QAAQ,EAAE,MAAM,EAChB,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACjC,eAAe,EAAE,MAAM,GACtB,MAAM,CAiBR;AAoBD,wBAAgB,uBAAuB,CACrC,QAAQ,EAAE,MAAM,EAChB,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAChC,MAAM,CA8DR;AAeD,MAAM,WAAW,cAAc;IAC7B,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,KAAK,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;IAC5C,UAAU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