qlogicagent 0.5.2 → 0.6.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/README.md +403 -402
- package/dist/agent.js +18 -0
- package/dist/cli.js +384 -0
- package/dist/contracts.js +1 -0
- package/dist/index.js +383 -0
- package/dist/orchestration.js +34 -0
- package/dist/types/agent/agent.d.ts +43 -0
- package/dist/types/agent/constants.d.ts +47 -0
- package/dist/types/agent/tool-access.d.ts +30 -0
- package/dist/types/agent/tool-loop.d.ts +94 -0
- package/dist/types/agent/types.d.ts +238 -0
- package/dist/types/cli/main.d.ts +11 -0
- package/dist/types/cli/stdio-server.d.ts +78 -0
- package/dist/types/cli/tool-bootstrap.d.ts +40 -0
- package/dist/types/cli/transport.d.ts +40 -0
- package/dist/types/config/config.d.ts +17 -0
- package/dist/types/contracts/hooks.d.ts +175 -0
- package/dist/types/contracts/index.d.ts +9 -0
- package/dist/types/contracts/planner.d.ts +35 -0
- package/dist/types/contracts/todo.d.ts +23 -0
- package/dist/types/index.d.ts +16 -0
- package/dist/types/llm/builtin-providers.d.ts +10 -0
- package/dist/types/llm/debug-transport.d.ts +12 -0
- package/dist/types/llm/index.d.ts +16 -0
- package/dist/types/llm/llm-client.d.ts +43 -0
- package/dist/types/llm/model-catalog.d.ts +53 -0
- package/dist/types/llm/provider-def.d.ts +59 -0
- package/dist/types/llm/provider-registry.d.ts +54 -0
- package/dist/types/llm/transport.d.ts +62 -0
- package/dist/types/llm/transports/anthropic-messages.d.ts +31 -0
- package/dist/types/llm/transports/openai-chat.d.ts +36 -0
- package/dist/types/orchestration/context/context-collapse.d.ts +58 -0
- package/dist/types/orchestration/context/context-compression.d.ts +301 -0
- package/dist/types/orchestration/context/reactive-compact.d.ts +73 -0
- package/dist/types/orchestration/context/turn-loop-guard.d.ts +86 -0
- package/dist/types/orchestration/error-handling/error-classification.d.ts +12 -0
- package/dist/types/orchestration/error-handling/failover-classification.d.ts +8 -0
- package/dist/types/orchestration/error-handling/failover-error.d.ts +33 -0
- package/dist/types/orchestration/error-handling/retry-loop.d.ts +69 -0
- package/dist/types/orchestration/index.d.ts +15 -0
- package/dist/types/orchestration/skill-improvement.d.ts +59 -0
- package/dist/types/orchestration/subagent/agent-registry.d.ts +46 -0
- package/dist/types/orchestration/subagent/fork-subagent.d.ts +98 -0
- package/dist/types/orchestration/subagent/task-types.d.ts +142 -0
- package/dist/types/orchestration/tool-loop/conversation-repair.d.ts +61 -0
- package/dist/types/orchestration/tool-loop/tool-choice-policy.d.ts +54 -0
- package/dist/types/orchestration/tool-loop/tool-loop-state.d.ts +50 -0
- package/dist/types/orchestration/tool-loop/tool-schema.d.ts +39 -0
- package/dist/types/runtime/execution/dream-agent.d.ts +199 -0
- package/dist/types/runtime/execution/forked-agent.d.ts +109 -0
- package/dist/types/runtime/execution/index.d.ts +6 -0
- package/dist/types/runtime/execution/progress-tracker.d.ts +78 -0
- package/dist/types/runtime/execution/remote-agent.d.ts +63 -0
- package/dist/types/runtime/execution/streaming-tool-executor.d.ts +100 -0
- package/dist/types/runtime/execution/tool-eligibility.d.ts +59 -0
- package/dist/types/runtime/execution/tool-result-storage.d.ts +87 -0
- package/dist/types/runtime/hooks/context-compression.d.ts +61 -0
- package/dist/types/runtime/hooks/hook-registry.d.ts +12 -0
- package/dist/types/runtime/hooks/index.d.ts +3 -0
- package/dist/types/runtime/hooks/memory-hooks.d.ts +49 -0
- package/dist/types/runtime/index.d.ts +5 -0
- package/dist/types/runtime/infra/agent-paths.d.ts +57 -0
- package/dist/types/runtime/infra/checkpoint-backend.d.ts +8 -0
- package/dist/types/runtime/infra/cleanup-registry.d.ts +23 -0
- package/dist/types/runtime/infra/disk-storage.d.ts +36 -0
- package/dist/types/runtime/infra/file-watcher.d.ts +72 -0
- package/dist/types/runtime/infra/index.d.ts +8 -0
- package/dist/types/runtime/infra/secure-storage.d.ts +81 -0
- package/dist/types/runtime/infra/task-runtime.d.ts +108 -0
- package/dist/types/runtime/infra/token-budget.d.ts +92 -0
- package/dist/types/runtime/infra/worktree-backend.d.ts +85 -0
- package/dist/types/runtime/prompt/environment-context.d.ts +23 -0
- package/dist/types/runtime/prompt/index.d.ts +3 -0
- package/dist/types/runtime/prompt/instruction-loader.d.ts +64 -0
- package/dist/types/runtime/prompt/system-prompt-sections.d.ts +63 -0
- package/dist/types/runtime/session/index.d.ts +2 -0
- package/dist/types/runtime/session/session-memory.d.ts +90 -0
- package/dist/types/runtime/session/session-persistence.d.ts +94 -0
- package/dist/types/runtime/session/session-state.d.ts +117 -0
- package/dist/types/skills/index.d.ts +119 -0
- package/dist/types/skills/mcp/index.d.ts +3 -0
- package/dist/types/skills/mcp/mcp-http-client.d.ts +66 -0
- package/dist/types/skills/mcp/mcp-manager.d.ts +83 -0
- package/dist/types/skills/mcp/mcp-stdio-client.d.ts +84 -0
- package/dist/types/skills/memory/memory-extractor.d.ts +64 -0
- package/dist/types/skills/memory/memory-store.d.ts +86 -0
- package/dist/types/skills/memory/memory-tool.d.ts +87 -0
- package/dist/types/skills/memory/qmemory-adapter.d.ts +42 -0
- package/dist/types/skills/permissions/bash-classifier.d.ts +30 -0
- package/dist/types/skills/permissions/classifier-cache.d.ts +51 -0
- package/dist/types/skills/permissions/denial-tracking.d.ts +42 -0
- package/dist/types/skills/permissions/hook-runner.d.ts +85 -0
- package/dist/types/skills/permissions/index.d.ts +12 -0
- package/dist/types/skills/permissions/permission-classifier.d.ts +41 -0
- package/dist/types/skills/permissions/rule-engine.d.ts +41 -0
- package/dist/types/skills/permissions/settings-watcher.d.ts +46 -0
- package/dist/types/skills/permissions/types.d.ts +113 -0
- package/dist/types/skills/plugins/index.d.ts +2 -0
- package/dist/types/skills/plugins/plugin-api.d.ts +38 -0
- package/dist/types/skills/plugins/plugin-loader.d.ts +42 -0
- package/dist/types/skills/plugins/plugin-marketplace.d.ts +61 -0
- package/dist/types/skills/portable-tool.d.ts +104 -0
- package/dist/types/skills/skill-system/skill-frontmatter.d.ts +19 -0
- package/dist/types/skills/skill-system/skill-guard.d.ts +23 -0
- package/dist/types/skills/skill-system/skill-loader.d.ts +16 -0
- package/dist/types/skills/skill-system/skill-source.d.ts +119 -0
- package/dist/types/skills/skill-system/skill-types.d.ts +199 -0
- package/dist/types/skills/think-tool.d.ts +16 -0
- package/dist/types/skills/todo-tool.d.ts +72 -0
- package/dist/types/skills/tools/agent-tool.d.ts +91 -0
- package/dist/types/skills/tools/apply-patch-tool.d.ts +29 -0
- package/dist/types/skills/tools/ask-user-tool.d.ts +80 -0
- package/dist/types/skills/tools/brief-tool.d.ts +74 -0
- package/dist/types/skills/tools/browser-tool.d.ts +114 -0
- package/dist/types/skills/tools/checkpoint-tool.d.ts +66 -0
- package/dist/types/skills/tools/config-tool.d.ts +63 -0
- package/dist/types/skills/tools/cron-tool.d.ts +116 -0
- package/dist/types/skills/tools/edit-tool.d.ts +43 -0
- package/dist/types/skills/tools/exec-tool.d.ts +97 -0
- package/dist/types/skills/tools/image-generate-tool.d.ts +62 -0
- package/dist/types/skills/tools/instructions-tool.d.ts +65 -0
- package/dist/types/skills/tools/lsp-tool.d.ts +153 -0
- package/dist/types/skills/tools/mcp-client-types.d.ts +269 -0
- package/dist/types/skills/tools/mcp-resource-tools.d.ts +14 -0
- package/dist/types/skills/tools/mcp-tool.d.ts +249 -0
- package/dist/types/skills/tools/monitor-tool.d.ts +113 -0
- package/dist/types/skills/tools/music-generate-tool.d.ts +55 -0
- package/dist/types/skills/tools/notebook-edit-tool.d.ts +15 -0
- package/dist/types/skills/tools/notify-tool.d.ts +53 -0
- package/dist/types/skills/tools/patch-tool.d.ts +45 -0
- package/dist/types/skills/tools/plan-mode-tool.d.ts +98 -0
- package/dist/types/skills/tools/read-tool.d.ts +51 -0
- package/dist/types/skills/tools/repl-tool.d.ts +70 -0
- package/dist/types/skills/tools/search-tool.d.ts +112 -0
- package/dist/types/skills/tools/send-message-tool.d.ts +51 -0
- package/dist/types/skills/tools/shell/bash-provider.d.ts +26 -0
- package/dist/types/skills/tools/shell/command-classification.d.ts +44 -0
- package/dist/types/skills/tools/shell/command-semantics.d.ts +14 -0
- package/dist/types/skills/tools/shell/destructive-command-warning.d.ts +10 -0
- package/dist/types/skills/tools/shell/exec-permissions.d.ts +52 -0
- package/dist/types/skills/tools/shell/index.d.ts +17 -0
- package/dist/types/skills/tools/shell/powershell-provider.d.ts +15 -0
- package/dist/types/skills/tools/shell/shell-command.d.ts +54 -0
- package/dist/types/skills/tools/shell/shell-exec.d.ts +33 -0
- package/dist/types/skills/tools/shell/shell-provider.d.ts +85 -0
- package/dist/types/skills/tools/shell/task-output.d.ts +45 -0
- package/dist/types/skills/tools/skill-invoke-tool.d.ts +46 -0
- package/dist/types/skills/tools/skill-list-tool.d.ts +33 -0
- package/dist/types/skills/tools/skill-manage-tool.d.ts +73 -0
- package/dist/types/skills/tools/skill-view-tool.d.ts +37 -0
- package/dist/types/skills/tools/sleep-tool.d.ts +49 -0
- package/dist/types/skills/tools/structured-output-tool.d.ts +116 -0
- package/dist/types/skills/tools/task-tool.d.ts +104 -0
- package/dist/types/skills/tools/team-tool.d.ts +89 -0
- package/dist/types/skills/tools/tool-search-tool.d.ts +51 -0
- package/dist/types/skills/tools/tts-tool.d.ts +38 -0
- package/dist/types/skills/tools/video-edit-tool.d.ts +69 -0
- package/dist/types/skills/tools/video-generate-tool.d.ts +62 -0
- package/dist/types/skills/tools/video-merge-tool.d.ts +105 -0
- package/dist/types/skills/tools/video-upscale-tool.d.ts +45 -0
- package/dist/types/skills/tools/web-fetch-tool.d.ts +78 -0
- package/dist/types/skills/tools/web-search-tool.d.ts +57 -0
- package/dist/types/skills/tools/workflow-tool.d.ts +44 -0
- package/dist/types/skills/tools/worktree-tool.d.ts +69 -0
- package/dist/types/skills/tools/write-tool.d.ts +45 -0
- package/dist/types/skills/tools.d.ts +65 -0
- package/package.json +4 -3
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ProviderRegistry — three-layer merge registry for LLM providers.
|
|
3
|
+
*
|
|
4
|
+
* Layer 1: builtin-providers.ts hardcoded (lowest priority, ~20 providers)
|
|
5
|
+
* Layer 2: model-catalog.ts remote (models.dev — enriches model metadata)
|
|
6
|
+
* Layer 3: user config override (from agent.turn.config — highest priority)
|
|
7
|
+
*
|
|
8
|
+
* Merge strategy: Layer 3 > Layer 2 > Layer 1 (later layers override same-id fields)
|
|
9
|
+
*
|
|
10
|
+
* Aligned with Hermes provider_registry.py.
|
|
11
|
+
*/
|
|
12
|
+
import type { ModelInfo, ProviderDef } from "./provider-def.js";
|
|
13
|
+
import { ModelCatalog } from "./model-catalog.js";
|
|
14
|
+
export declare class ProviderRegistry {
|
|
15
|
+
/** Layer 1: builtin hardcoded providers */
|
|
16
|
+
private builtins;
|
|
17
|
+
/** Layer 2: remote model catalog (models.dev) */
|
|
18
|
+
private catalog;
|
|
19
|
+
/** Layer 3: user overrides (from agent.turn.config) */
|
|
20
|
+
private overrides;
|
|
21
|
+
constructor(opts?: {
|
|
22
|
+
catalog?: ModelCatalog;
|
|
23
|
+
});
|
|
24
|
+
/**
|
|
25
|
+
* Apply user config override for a provider.
|
|
26
|
+
* Typically called when agent.turn.config has baseUrl/apiKey overrides.
|
|
27
|
+
*/
|
|
28
|
+
applyOverride(providerId: string, override: Partial<ProviderDef>): void;
|
|
29
|
+
/**
|
|
30
|
+
* Get merged ProviderDef by id (Layer 3 > Layer 1).
|
|
31
|
+
* Returns undefined if provider not found.
|
|
32
|
+
* Supports common aliases (e.g., "claude" → "anthropic").
|
|
33
|
+
*/
|
|
34
|
+
getProvider(id: string): ProviderDef | undefined;
|
|
35
|
+
/**
|
|
36
|
+
* List all known provider ids.
|
|
37
|
+
*/
|
|
38
|
+
listProviders(): ProviderDef[];
|
|
39
|
+
/**
|
|
40
|
+
* List models for a specific provider.
|
|
41
|
+
* Merges: Layer 3 override > Layer 1 builtin > Layer 2 catalog enrichment.
|
|
42
|
+
*/
|
|
43
|
+
listModels(providerId: string): ModelInfo[];
|
|
44
|
+
/**
|
|
45
|
+
* Trigger background refresh of the remote model catalog.
|
|
46
|
+
*/
|
|
47
|
+
refreshCatalog(): Promise<boolean>;
|
|
48
|
+
/**
|
|
49
|
+
* Resolve API key for a provider:
|
|
50
|
+
* 1. Explicit key (from agent.turn.config)
|
|
51
|
+
* 2. Environment variables (ProviderDef.apiKeyEnvVars)
|
|
52
|
+
*/
|
|
53
|
+
resolveApiKey(providerId: string, explicitKey?: string): string | undefined;
|
|
54
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LLMTransport — abstract interface for LLM inference calls.
|
|
3
|
+
*
|
|
4
|
+
* Aligned with Hermes `ProviderTransport` ABC:
|
|
5
|
+
* stream(request, apiKey, signal) → AsyncGenerator<LLMChunk>
|
|
6
|
+
*
|
|
7
|
+
* Two concrete implementations:
|
|
8
|
+
* - OpenAI Chat Completions (covers 95% of providers)
|
|
9
|
+
* - Anthropic Messages API
|
|
10
|
+
*/
|
|
11
|
+
import type { ChatMessage, ToolDefinition } from "../agent/types.js";
|
|
12
|
+
export interface LLMRequest {
|
|
13
|
+
model: string;
|
|
14
|
+
messages: ChatMessage[];
|
|
15
|
+
tools?: ToolDefinition[];
|
|
16
|
+
toolChoice?: "auto" | "none" | "required";
|
|
17
|
+
temperature?: number;
|
|
18
|
+
maxTokens?: number;
|
|
19
|
+
reasoning?: {
|
|
20
|
+
effort: "low" | "medium" | "high";
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
export type LLMChunk = {
|
|
24
|
+
type: "delta";
|
|
25
|
+
text: string;
|
|
26
|
+
} | {
|
|
27
|
+
type: "tool_call_delta";
|
|
28
|
+
index: number;
|
|
29
|
+
id?: string;
|
|
30
|
+
name?: string;
|
|
31
|
+
arguments: string;
|
|
32
|
+
} | {
|
|
33
|
+
type: "reasoning_delta";
|
|
34
|
+
text: string;
|
|
35
|
+
} | {
|
|
36
|
+
type: "usage";
|
|
37
|
+
promptTokens: number;
|
|
38
|
+
completionTokens: number;
|
|
39
|
+
reasoningTokens?: number;
|
|
40
|
+
} | {
|
|
41
|
+
type: "done";
|
|
42
|
+
finishReason: string;
|
|
43
|
+
};
|
|
44
|
+
export interface AccumulatedToolCall {
|
|
45
|
+
id: string;
|
|
46
|
+
name: string;
|
|
47
|
+
arguments: string;
|
|
48
|
+
}
|
|
49
|
+
export interface LLMTransport {
|
|
50
|
+
/**
|
|
51
|
+
* Stream an LLM inference request.
|
|
52
|
+
* apiKey is passed explicitly (from agent.turn.config, not env).
|
|
53
|
+
*/
|
|
54
|
+
stream(request: LLMRequest, apiKey: string, signal?: AbortSignal): AsyncGenerator<LLMChunk>;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Accumulate tool_call_delta chunks into complete ToolCall objects.
|
|
58
|
+
* Modeled after admin-infer-proxy-client's Map<index, toolCall> accumulator.
|
|
59
|
+
*/
|
|
60
|
+
export declare function accumulateToolCalls(accumulator: Map<number, AccumulatedToolCall>, chunk: LLMChunk & {
|
|
61
|
+
type: "tool_call_delta";
|
|
62
|
+
}): void;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Anthropic Messages Transport — SSE streaming for Claude API.
|
|
3
|
+
*
|
|
4
|
+
* POST {baseUrl}/v1/messages with stream: true
|
|
5
|
+
* Auth: x-api-key: {apiKey} + anthropic-version header
|
|
6
|
+
*
|
|
7
|
+
* SSE event types:
|
|
8
|
+
* message_start, content_block_start, content_block_delta,
|
|
9
|
+
* content_block_stop, message_delta, message_stop
|
|
10
|
+
*
|
|
11
|
+
* Tool use is via content blocks with type "tool_use" + "input_json_delta".
|
|
12
|
+
*
|
|
13
|
+
* Aligned with Hermes anthropic_messages.py transport.
|
|
14
|
+
*/
|
|
15
|
+
import type { LLMChunk, LLMRequest, LLMTransport } from "../transport.js";
|
|
16
|
+
export interface AnthropicTransportConfig {
|
|
17
|
+
baseUrl: string;
|
|
18
|
+
/** anthropic-version header (default "2023-06-01") */
|
|
19
|
+
apiVersion?: string;
|
|
20
|
+
/** Timeout in ms (default 180_000) */
|
|
21
|
+
timeoutMs?: number;
|
|
22
|
+
}
|
|
23
|
+
export declare class AnthropicMessagesTransport implements LLMTransport {
|
|
24
|
+
private baseUrl;
|
|
25
|
+
private apiVersion;
|
|
26
|
+
private timeoutMs;
|
|
27
|
+
constructor(config: AnthropicTransportConfig);
|
|
28
|
+
stream(request: LLMRequest, apiKey: string, signal?: AbortSignal): AsyncGenerator<LLMChunk>;
|
|
29
|
+
private parseSSEStream;
|
|
30
|
+
private mapEvent;
|
|
31
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OpenAI Chat Completions Transport — SSE streaming implementation.
|
|
3
|
+
*
|
|
4
|
+
* Covers all OpenAI-compatible providers:
|
|
5
|
+
* DeepSeek, Qwen, 硅基, Minimax, Moonshot, Groq, Together, OpenRouter, etc.
|
|
6
|
+
*
|
|
7
|
+
* POST {baseUrl}/v1/chat/completions with stream: true
|
|
8
|
+
* Auth: Authorization: Bearer {apiKey}
|
|
9
|
+
*
|
|
10
|
+
* SSE format: lines prefixed with "data: ", JSON parsing per event.
|
|
11
|
+
*
|
|
12
|
+
* Adapted from admin-infer-proxy-client.ts SSE logic + Hermes openai_chat.py transport.
|
|
13
|
+
*/
|
|
14
|
+
import type { LLMChunk, LLMRequest, LLMTransport } from "../transport.js";
|
|
15
|
+
export interface OpenAIChatTransportConfig {
|
|
16
|
+
baseUrl: string;
|
|
17
|
+
/** Additional headers (e.g. for specific providers) */
|
|
18
|
+
extraHeaders?: Record<string, string>;
|
|
19
|
+
/** Timeout in ms (default 180_000) */
|
|
20
|
+
timeoutMs?: number;
|
|
21
|
+
/** Whether to include stream_options (default true). Set false for providers that reject it. */
|
|
22
|
+
supportsStreamOptions?: boolean;
|
|
23
|
+
/** Whether to omit temperature when it equals 0 (e.g. Moonshot rejects 0) */
|
|
24
|
+
omitZeroTemperature?: boolean;
|
|
25
|
+
}
|
|
26
|
+
export declare class OpenAIChatTransport implements LLMTransport {
|
|
27
|
+
private baseUrl;
|
|
28
|
+
private extraHeaders;
|
|
29
|
+
private timeoutMs;
|
|
30
|
+
private supportsStreamOptions;
|
|
31
|
+
private omitZeroTemperature;
|
|
32
|
+
constructor(config: OpenAIChatTransportConfig);
|
|
33
|
+
stream(request: LLMRequest, apiKey: string, signal?: AbortSignal): AsyncGenerator<LLMChunk>;
|
|
34
|
+
private parseSSEStream;
|
|
35
|
+
private mapChunk;
|
|
36
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Context Collapse — staged lightweight context reduction that runs
|
|
3
|
+
* BEFORE autocompact and can drain on 413 BEFORE reactive compact.
|
|
4
|
+
*
|
|
5
|
+
* CC parity: services/contextCollapse/index.ts
|
|
6
|
+
*
|
|
7
|
+
* Architecture:
|
|
8
|
+
* 1. applyCollapsesIfNeeded() — runs every iteration, projects the
|
|
9
|
+
* collapsed view and maybe commits more collapses (by summarizing
|
|
10
|
+
* old tool-result groups into compact summaries).
|
|
11
|
+
* 2. recoverFromOverflow() — drain all staged collapses on a real
|
|
12
|
+
* API 413. If it can free enough context, the retry may succeed
|
|
13
|
+
* without reactive compact (which is heavier and destroys more
|
|
14
|
+
* context).
|
|
15
|
+
*
|
|
16
|
+
* Collapses are ordered "stages" — oldest first. Each stage replaces
|
|
17
|
+
* a contiguous run of old messages with a single summary line.
|
|
18
|
+
*/
|
|
19
|
+
export interface CollapseStage {
|
|
20
|
+
/** UUID of the stage (for dedup and ordering). */
|
|
21
|
+
id: string;
|
|
22
|
+
/** Index range [start, end) in the original message array. */
|
|
23
|
+
range: [number, number];
|
|
24
|
+
/** Summary text that replaces the range. */
|
|
25
|
+
summary: string;
|
|
26
|
+
/** Whether this stage has been committed (applied to messages). */
|
|
27
|
+
committed: boolean;
|
|
28
|
+
}
|
|
29
|
+
export interface CollapseStore {
|
|
30
|
+
stages: CollapseStage[];
|
|
31
|
+
}
|
|
32
|
+
export declare function createCollapseStore(): CollapseStore;
|
|
33
|
+
/**
|
|
34
|
+
* Evaluate whether new collapses should be staged.
|
|
35
|
+
*
|
|
36
|
+
* Heuristic: if the conversation has > thresholdMessages non-system messages,
|
|
37
|
+
* group old tool-result runs into collapse stages.
|
|
38
|
+
*/
|
|
39
|
+
export declare function applyCollapsesIfNeeded<T extends {
|
|
40
|
+
role: string;
|
|
41
|
+
content?: string | unknown;
|
|
42
|
+
}>(messages: T[], store: CollapseStore, opts?: {
|
|
43
|
+
thresholdMessages?: number;
|
|
44
|
+
}): {
|
|
45
|
+
messages: T[];
|
|
46
|
+
stagedCount: number;
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* Drain all staged (uncommitted) collapses — called on 413 recovery.
|
|
50
|
+
* Commits every pending stage and returns the collapsed messages.
|
|
51
|
+
*/
|
|
52
|
+
export declare function recoverFromOverflow<T extends {
|
|
53
|
+
role: string;
|
|
54
|
+
content?: string | unknown;
|
|
55
|
+
}>(messages: T[], store: CollapseStore): {
|
|
56
|
+
messages: T[];
|
|
57
|
+
committed: number;
|
|
58
|
+
};
|
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
export interface CompressibleMessage {
|
|
2
|
+
role: string;
|
|
3
|
+
content?: string | unknown;
|
|
4
|
+
tool_call_id?: string;
|
|
5
|
+
tool_calls?: unknown;
|
|
6
|
+
name?: string;
|
|
7
|
+
}
|
|
8
|
+
export interface CompressionResult {
|
|
9
|
+
messages: CompressibleMessage[];
|
|
10
|
+
droppedCount: number;
|
|
11
|
+
strategy: string;
|
|
12
|
+
/** Phase 4: optional metrics from the compression pass */
|
|
13
|
+
metrics?: CompressionMetrics;
|
|
14
|
+
}
|
|
15
|
+
export interface CompressionMetrics {
|
|
16
|
+
tokensBefore: number;
|
|
17
|
+
tokensAfter: number;
|
|
18
|
+
compressionRatio: number;
|
|
19
|
+
/** ms elapsed for this strategy pass */
|
|
20
|
+
latencyMs: number;
|
|
21
|
+
/** Whether an LLM call was made during this pass */
|
|
22
|
+
usedLlm: boolean;
|
|
23
|
+
/** Whether prompt cache was invalidated by compression */
|
|
24
|
+
cacheInvalidated?: boolean;
|
|
25
|
+
}
|
|
26
|
+
export interface CompressionStrategy {
|
|
27
|
+
compress(messages: CompressibleMessage[], budget: number): CompressionResult;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Async compression strategy — required for LLM-based summarization.
|
|
31
|
+
* Falls back to sync `compress` when the caller doesn't support async.
|
|
32
|
+
*/
|
|
33
|
+
export interface AsyncCompressionStrategy extends CompressionStrategy {
|
|
34
|
+
compressAsync(messages: CompressibleMessage[], budget: number): Promise<CompressionResult>;
|
|
35
|
+
}
|
|
36
|
+
export declare function isAsyncCompressionStrategy(s: CompressionStrategy): s is AsyncCompressionStrategy;
|
|
37
|
+
/** Caller-provided LLM summarization function (wired by Hub). */
|
|
38
|
+
export type SummarizeFn = (messages: CompressibleMessage[], instruction: string) => Promise<string>;
|
|
39
|
+
export declare class SlidingWindowStrategy implements CompressionStrategy {
|
|
40
|
+
private estimateTokens;
|
|
41
|
+
constructor(estimateTokens: (msg: CompressibleMessage) => number);
|
|
42
|
+
compress(messages: CompressibleMessage[], budget: number): CompressionResult;
|
|
43
|
+
}
|
|
44
|
+
export declare class SummarizeOldStrategy implements CompressionStrategy {
|
|
45
|
+
private recentCount;
|
|
46
|
+
private summarize;
|
|
47
|
+
constructor(recentCount: number, summarize: (messages: CompressibleMessage[]) => string);
|
|
48
|
+
compress(messages: CompressibleMessage[], _budget: number): CompressionResult;
|
|
49
|
+
}
|
|
50
|
+
export declare class ToolResultTrimStrategy implements CompressionStrategy {
|
|
51
|
+
private maxToolResultChars;
|
|
52
|
+
constructor(maxToolResultChars?: number);
|
|
53
|
+
compress(messages: CompressibleMessage[], _budget: number): CompressionResult;
|
|
54
|
+
}
|
|
55
|
+
export declare function composeStrategies(...strategies: CompressionStrategy[]): CompressionStrategy;
|
|
56
|
+
/**
|
|
57
|
+
* Compose strategies with async support — if any strategy is async,
|
|
58
|
+
* the pipeline becomes async.
|
|
59
|
+
*/
|
|
60
|
+
export declare function composeAsyncStrategies(...strategies: CompressionStrategy[]): AsyncCompressionStrategy;
|
|
61
|
+
/**
|
|
62
|
+
* Build the structured 9-section summary instruction for the LLM.
|
|
63
|
+
* Based on Claude Code's Full Compact mode, adapted for Hub.
|
|
64
|
+
*/
|
|
65
|
+
export declare function buildStructuredSummaryPrompt(messagesToSummarize: CompressibleMessage[], opts?: {
|
|
66
|
+
taskContext?: string;
|
|
67
|
+
}): string;
|
|
68
|
+
export interface HeadTailProtectionConfig {
|
|
69
|
+
/** Number of initial user-assistant exchanges to protect (default: 1) */
|
|
70
|
+
protectedHeadExchanges: number;
|
|
71
|
+
/** Number of recent messages to always keep (default: 8 = ~4 exchanges) */
|
|
72
|
+
protectedTailMessages: number;
|
|
73
|
+
/** The summarization callback */
|
|
74
|
+
summarize: SummarizeFn;
|
|
75
|
+
/** Optional token estimator */
|
|
76
|
+
estimateTokens?: (msg: CompressibleMessage) => number;
|
|
77
|
+
/** Optional extra context for the summary prompt */
|
|
78
|
+
taskContext?: string;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Phase 2.2: Head/Tail protected summarization.
|
|
82
|
+
*
|
|
83
|
+
* Protects: system messages + first exchange + last N messages.
|
|
84
|
+
* Compresses: middle section via LLM summarization.
|
|
85
|
+
*/
|
|
86
|
+
export declare class HeadTailProtectedStrategy implements AsyncCompressionStrategy {
|
|
87
|
+
private config;
|
|
88
|
+
constructor(config: HeadTailProtectionConfig);
|
|
89
|
+
compress(messages: CompressibleMessage[], _budget: number): CompressionResult;
|
|
90
|
+
compressAsync(messages: CompressibleMessage[], budget: number): Promise<CompressionResult>;
|
|
91
|
+
}
|
|
92
|
+
export interface IncrementalCompactConfig {
|
|
93
|
+
/** Messages newer than this count are never summarized (default: 12) */
|
|
94
|
+
preserveRecentCount: number;
|
|
95
|
+
/** Summarization callback */
|
|
96
|
+
summarize: SummarizeFn;
|
|
97
|
+
/** Token estimator */
|
|
98
|
+
estimateTokens?: (msg: CompressibleMessage) => number;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Phase 2.3: Incremental (partial) compaction.
|
|
102
|
+
*
|
|
103
|
+
* Only summarizes the oldest messages beyond the preserve window.
|
|
104
|
+
* Avoids repeatedly re-summarizing already-compressed content.
|
|
105
|
+
* If a previous summary marker exists, only new old messages are compressed.
|
|
106
|
+
*/
|
|
107
|
+
export declare class IncrementalCompactStrategy implements AsyncCompressionStrategy {
|
|
108
|
+
private config;
|
|
109
|
+
constructor(config: IncrementalCompactConfig);
|
|
110
|
+
compress(messages: CompressibleMessage[], _budget: number): CompressionResult;
|
|
111
|
+
compressAsync(messages: CompressibleMessage[], budget: number): Promise<CompressionResult>;
|
|
112
|
+
}
|
|
113
|
+
export interface CacheAwareCompressionConfig {
|
|
114
|
+
/** The inner strategy to delegate to */
|
|
115
|
+
inner: CompressionStrategy;
|
|
116
|
+
/** Token estimator */
|
|
117
|
+
estimateTokens?: (msg: CompressibleMessage) => number;
|
|
118
|
+
/**
|
|
119
|
+
* Callback: notify when prompt cache was invalidated by compression.
|
|
120
|
+
* Hub uses this to set pendingPostCompaction and track cache miss rate.
|
|
121
|
+
*/
|
|
122
|
+
onCacheInvalidated?: (info: {
|
|
123
|
+
droppedCount: number;
|
|
124
|
+
strategy: string;
|
|
125
|
+
}) => void;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Phase 3: Cache-aware wrapper.
|
|
129
|
+
*
|
|
130
|
+
* Wraps any strategy and tracks whether compression invalidated the
|
|
131
|
+
* provider prompt cache. System prompt prefix stability is preserved
|
|
132
|
+
* by never modifying system[0] (the original system prompt).
|
|
133
|
+
*/
|
|
134
|
+
export declare class CacheAwareCompressionStrategy implements AsyncCompressionStrategy {
|
|
135
|
+
private config;
|
|
136
|
+
constructor(config: CacheAwareCompressionConfig);
|
|
137
|
+
compress(messages: CompressibleMessage[], budget: number): CompressionResult;
|
|
138
|
+
compressAsync(messages: CompressibleMessage[], budget: number): Promise<CompressionResult>;
|
|
139
|
+
}
|
|
140
|
+
export interface AdaptiveBudgetConfig {
|
|
141
|
+
/** Model context window size in tokens */
|
|
142
|
+
modelContextWindow: number;
|
|
143
|
+
/** Target usage ratio (0.0–1.0, default 0.75) — trigger compaction at this fraction */
|
|
144
|
+
targetUsageRatio: number;
|
|
145
|
+
/** Minimum guaranteed budget in tokens */
|
|
146
|
+
minBudget: number;
|
|
147
|
+
/** Maximum budget (usually = modelContextWindow) */
|
|
148
|
+
maxBudget: number;
|
|
149
|
+
}
|
|
150
|
+
export declare const DEFAULT_ADAPTIVE_BUDGET_CONFIG: AdaptiveBudgetConfig;
|
|
151
|
+
/**
|
|
152
|
+
* Compute the adaptive token budget for a given model + message history.
|
|
153
|
+
*
|
|
154
|
+
* Adjusts based on model context window, and uses the target ratio
|
|
155
|
+
* so compression triggers before hitting the hard limit.
|
|
156
|
+
*/
|
|
157
|
+
export declare function computeAdaptiveBudget(config?: Partial<AdaptiveBudgetConfig>): number;
|
|
158
|
+
/**
|
|
159
|
+
* Decide which compression tier to use based on current token count vs budget.
|
|
160
|
+
*/
|
|
161
|
+
export type CompressionTier = "none" | "trim-only" | "sliding-window" | "llm-summarize";
|
|
162
|
+
export declare function selectCompressionTier(currentTokens: number, budget: number): CompressionTier;
|
|
163
|
+
export interface CompressionEvent {
|
|
164
|
+
timestamp: number;
|
|
165
|
+
strategy: string;
|
|
166
|
+
tokensBefore: number;
|
|
167
|
+
tokensAfter: number;
|
|
168
|
+
droppedCount: number;
|
|
169
|
+
latencyMs: number;
|
|
170
|
+
usedLlm: boolean;
|
|
171
|
+
cacheInvalidated: boolean;
|
|
172
|
+
tier: CompressionTier;
|
|
173
|
+
}
|
|
174
|
+
export interface CompressionMetricsSnapshot {
|
|
175
|
+
totalCompressions: number;
|
|
176
|
+
totalLlmCalls: number;
|
|
177
|
+
totalCacheInvalidations: number;
|
|
178
|
+
averageCompressionRatio: number;
|
|
179
|
+
averageLatencyMs: number;
|
|
180
|
+
totalTokensSaved: number;
|
|
181
|
+
recentEvents: CompressionEvent[];
|
|
182
|
+
}
|
|
183
|
+
export declare class CompressionMetricsCollector {
|
|
184
|
+
private events;
|
|
185
|
+
private maxEvents;
|
|
186
|
+
constructor(maxEvents?: number);
|
|
187
|
+
record(event: CompressionEvent): void;
|
|
188
|
+
snapshot(): CompressionMetricsSnapshot;
|
|
189
|
+
reset(): void;
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* A pluggable context engine. Only one can be active at a time.
|
|
193
|
+
* The default engine uses the 4-layer compression funnel.
|
|
194
|
+
*/
|
|
195
|
+
export interface ContextEngine {
|
|
196
|
+
/** Unique engine identifier */
|
|
197
|
+
readonly id: string;
|
|
198
|
+
/** Human-readable label */
|
|
199
|
+
readonly label: string;
|
|
200
|
+
/** Compress messages for a turn */
|
|
201
|
+
compressAsync(messages: CompressibleMessage[], budget: number, context?: {
|
|
202
|
+
model?: string;
|
|
203
|
+
sessionId?: string;
|
|
204
|
+
}): Promise<CompressionResult>;
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Registry for context engines. Enforces single-active constraint.
|
|
208
|
+
*/
|
|
209
|
+
export declare class ContextEngineRegistry {
|
|
210
|
+
private engines;
|
|
211
|
+
private activeId;
|
|
212
|
+
register(engine: ContextEngine): void;
|
|
213
|
+
activate(id: string): boolean;
|
|
214
|
+
getActive(): ContextEngine | undefined;
|
|
215
|
+
listEngines(): Array<{
|
|
216
|
+
id: string;
|
|
217
|
+
label: string;
|
|
218
|
+
active: boolean;
|
|
219
|
+
}>;
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* MicroCompact — selective clearing of old compactable tool results.
|
|
223
|
+
*
|
|
224
|
+
* CC parity: microCompact.ts
|
|
225
|
+
*
|
|
226
|
+
* For tool result messages from COMPACTABLE_TOOLS, if the message is
|
|
227
|
+
* "old enough" (not in the most recent N messages), replace its content
|
|
228
|
+
* with a compact "[result cleared]" marker. This preserves the tool call
|
|
229
|
+
* structure (so the LLM knows what was called) but reclaims token budget.
|
|
230
|
+
*
|
|
231
|
+
* This is a lighter alternative to full summarization — it runs BEFORE
|
|
232
|
+
* LLM summarization as a first-pass reduction.
|
|
233
|
+
*/
|
|
234
|
+
export declare class MicroCompactStrategy implements CompressionStrategy {
|
|
235
|
+
/** Number of recent messages to preserve (don't clear) */
|
|
236
|
+
private preserveRecentCount;
|
|
237
|
+
/** Token estimator */
|
|
238
|
+
private estimateTokens;
|
|
239
|
+
constructor(
|
|
240
|
+
/** Number of recent messages to preserve (don't clear) */
|
|
241
|
+
preserveRecentCount?: number,
|
|
242
|
+
/** Token estimator */
|
|
243
|
+
estimateTokens?: (msg: CompressibleMessage) => number);
|
|
244
|
+
compress(messages: CompressibleMessage[], _budget: number): CompressionResult;
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* After compaction, the most recently read/edited files may have been
|
|
248
|
+
* dropped from the conversation. CC re-injects up to N file contents
|
|
249
|
+
* to ensure the agent doesn't lose awareness of files it was working with.
|
|
250
|
+
*
|
|
251
|
+
* CC parity: compact.ts postCompactFileStateRecovery
|
|
252
|
+
*/
|
|
253
|
+
export interface PostCompactRecoveryConfig {
|
|
254
|
+
/** Maximum number of files to re-inject (default 5) */
|
|
255
|
+
maxFiles: number;
|
|
256
|
+
/** Maximum total tokens for re-injected files (default 50000) */
|
|
257
|
+
maxTokenBudget: number;
|
|
258
|
+
/** Function to read a file from disk (injected by caller) */
|
|
259
|
+
readFile: (path: string) => Promise<string | null>;
|
|
260
|
+
/** Token estimator */
|
|
261
|
+
estimateTokens?: (text: string) => number;
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* Extract file paths that were recently read/edited from the conversation.
|
|
265
|
+
* Looks at tool calls and results for file path arguments.
|
|
266
|
+
*/
|
|
267
|
+
export declare function extractRecentFilePaths(messages: CompressibleMessage[]): string[];
|
|
268
|
+
/**
|
|
269
|
+
* Re-inject recently-used file contents after compaction.
|
|
270
|
+
*
|
|
271
|
+
* Call this AFTER compression but BEFORE sending to the LLM.
|
|
272
|
+
* Appends file content as system messages at the end of the
|
|
273
|
+
* system message block.
|
|
274
|
+
*/
|
|
275
|
+
export declare function postCompactFileRecovery(compressedMessages: CompressibleMessage[], originalMessages: CompressibleMessage[], config: PostCompactRecoveryConfig): Promise<CompressibleMessage[]>;
|
|
276
|
+
/**
|
|
277
|
+
* Snip Compact — permanently remove specific messages by identifier.
|
|
278
|
+
*
|
|
279
|
+
* CC parity: services/compact/snipCompact.ts
|
|
280
|
+
*
|
|
281
|
+
* Unlike sliding window or summarization, snip removes messages by
|
|
282
|
+
* their unique id (tool_call_id, or index-based). This is used for:
|
|
283
|
+
* - Removing old tool results that are known to be stale
|
|
284
|
+
* - Dropping large image/media messages after they've been processed
|
|
285
|
+
* - Clearing tombstoned messages from the transcript
|
|
286
|
+
*
|
|
287
|
+
* Snip runs BEFORE microcompact and autocompact — both may fire
|
|
288
|
+
* after snip if the result is still over threshold.
|
|
289
|
+
*/
|
|
290
|
+
export interface SnipResult {
|
|
291
|
+
messages: CompressibleMessage[];
|
|
292
|
+
tokensFreed: number;
|
|
293
|
+
removedCount: number;
|
|
294
|
+
/** Optional boundary message to yield (marks snip point in transcript). */
|
|
295
|
+
boundaryMessage?: CompressibleMessage;
|
|
296
|
+
}
|
|
297
|
+
/**
|
|
298
|
+
* Remove messages whose tool_call_id or name matches the removedIds set.
|
|
299
|
+
* If no removedIds are pending, this is a no-op.
|
|
300
|
+
*/
|
|
301
|
+
export declare function snipCompactIfNeeded(messages: CompressibleMessage[], removedIds: Set<string>, estimateTokens?: (msg: CompressibleMessage) => number): SnipResult;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reactive Compact — Emergency context compression when API returns prompt-too-long.
|
|
3
|
+
*
|
|
4
|
+
* Aligned with Claude Code's reactive compact chain:
|
|
5
|
+
* 1. API 413 → withhold error → attempt reactive compact → retry
|
|
6
|
+
* 2. Only one reactive compact attempt per turn (prevent infinite loops)
|
|
7
|
+
* 3. Falls back to abort if compact fails
|
|
8
|
+
*
|
|
9
|
+
* Also handles post-compact restoration:
|
|
10
|
+
* - Todo list re-injection
|
|
11
|
+
* - Skill context re-injection
|
|
12
|
+
* - Tool delta re-injection (new/removed tools since last compact)
|
|
13
|
+
*/
|
|
14
|
+
export interface ReactiveCompactConfig {
|
|
15
|
+
/** Maximum consecutive auto-compact failures before giving up (CC: 3). */
|
|
16
|
+
maxConsecutiveFailures: number;
|
|
17
|
+
/** Minimum messages to keep after reactive compact. */
|
|
18
|
+
minMessagesAfterCompact: number;
|
|
19
|
+
/** Target token usage after compact (percentage of context window). */
|
|
20
|
+
targetUsagePercent: number;
|
|
21
|
+
}
|
|
22
|
+
export declare const DEFAULT_REACTIVE_COMPACT_CONFIG: ReactiveCompactConfig;
|
|
23
|
+
export interface PostCompactRestorationPayload {
|
|
24
|
+
/** Current active todo list to re-inject after compact. */
|
|
25
|
+
todoList?: Array<{
|
|
26
|
+
id: number;
|
|
27
|
+
title: string;
|
|
28
|
+
status: string;
|
|
29
|
+
}>;
|
|
30
|
+
/** Active skill context (if LLM is executing a skill). */
|
|
31
|
+
activeSkillContext?: {
|
|
32
|
+
name: string;
|
|
33
|
+
step: number;
|
|
34
|
+
instructions: string;
|
|
35
|
+
};
|
|
36
|
+
/** Tool delta since last compact (tools added/removed from available set). */
|
|
37
|
+
toolDelta?: {
|
|
38
|
+
added: string[];
|
|
39
|
+
removed: string[];
|
|
40
|
+
};
|
|
41
|
+
/** Custom restoration blocks from memory hooks. */
|
|
42
|
+
customBlocks?: Array<{
|
|
43
|
+
label: string;
|
|
44
|
+
content: string;
|
|
45
|
+
}>;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Build the restoration message to inject after a compact operation.
|
|
49
|
+
* This ensures critical state survives context compression.
|
|
50
|
+
*/
|
|
51
|
+
export declare function buildPostCompactRestorationMessage(payload: PostCompactRestorationPayload): string | null;
|
|
52
|
+
export interface ReactiveCompactState {
|
|
53
|
+
/** Number of consecutive compact failures. */
|
|
54
|
+
consecutiveFailures: number;
|
|
55
|
+
/** Whether reactive compact has been attempted in this turn. */
|
|
56
|
+
attemptedThisTurn: boolean;
|
|
57
|
+
/** Last compact timestamp. */
|
|
58
|
+
lastCompactAt: number | null;
|
|
59
|
+
/** Tool names at time of last compact (for delta calculation). */
|
|
60
|
+
toolsAtLastCompact: string[];
|
|
61
|
+
}
|
|
62
|
+
export declare function createReactiveCompactState(): ReactiveCompactState;
|
|
63
|
+
/**
|
|
64
|
+
* Determine if reactive compact should be attempted.
|
|
65
|
+
*/
|
|
66
|
+
export declare function shouldAttemptReactiveCompact(state: ReactiveCompactState, config?: ReactiveCompactConfig): boolean;
|
|
67
|
+
/**
|
|
68
|
+
* Calculate the tool delta since last compact.
|
|
69
|
+
*/
|
|
70
|
+
export declare function computeToolDelta(currentTools: string[], toolsAtLastCompact: string[]): {
|
|
71
|
+
added: string[];
|
|
72
|
+
removed: string[];
|
|
73
|
+
};
|