goatchain 0.0.29 → 0.0.31
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 +413 -116
- package/dist/acp-adapter/history-normalizer.d.ts +10 -0
- package/dist/acp-adapter/index.d.ts +2 -0
- package/dist/acp-adapter/types.d.ts +4 -0
- package/dist/agent/agent.d.ts +14 -3
- package/dist/agent/hooks/index.d.ts +1 -1
- package/dist/agent/hooks/manager.d.ts +18 -1
- package/dist/agent/hooks/prompt-executor.d.ts +28 -0
- package/dist/agent/hooks/types.d.ts +32 -6
- package/dist/agent/index.d.ts +1 -1
- package/dist/agent/middleware.d.ts +10 -2
- package/dist/agent/types.d.ts +12 -0
- package/dist/index.d.ts +10 -10
- package/dist/index.js +492 -482
- package/dist/middleware/contextCompressionMiddleware.d.ts +22 -161
- package/dist/middleware/longRunningMiddleware.d.ts +6 -0
- package/dist/middleware/parallelSubagentMiddleware.d.ts +7 -1
- package/dist/model/anthropic/createAnthropicAdapter.d.ts +5 -0
- package/dist/session/executors/ToolExecutor.d.ts +4 -0
- package/dist/session/index.d.ts +1 -0
- package/dist/session/session.d.ts +36 -3
- package/dist/session/types/index.d.ts +1 -0
- package/dist/session/types/messageQueue.d.ts +29 -0
- package/dist/session/utils/MessageQueue.d.ts +23 -0
- package/dist/state/types.d.ts +36 -26
- package/dist/types/common.d.ts +12 -0
- package/dist/types/event.d.ts +63 -2
- package/dist/types/index.d.ts +1 -0
- package/dist/types/snapshot.d.ts +11 -0
- package/package.json +22 -22
|
@@ -3,187 +3,48 @@ import type { AgentLoopState } from '../agent/types';
|
|
|
3
3
|
import type { ModelClient } from '../model';
|
|
4
4
|
import type { StateStore } from '../state/stateStore';
|
|
5
5
|
import type { Message } from '../types';
|
|
6
|
-
/**
|
|
7
|
-
* Configuration options for context compression middleware.
|
|
8
|
-
*/
|
|
9
6
|
export interface ContextCompressionOptions {
|
|
10
|
-
|
|
11
|
-
* Maximum token limit for the model (e.g., 128000 for GPT-4).
|
|
12
|
-
* - Tool compression triggers at 50% (maxTokens * 0.5)
|
|
13
|
-
* - Summary compression triggers at 80% (maxTokens * 0.8)
|
|
14
|
-
*/
|
|
15
|
-
maxTokens: number;
|
|
16
|
-
/**
|
|
17
|
-
* Number of recent conversation turns to always protect (default: 2).
|
|
18
|
-
* A turn is a user message + assistant response pair.
|
|
19
|
-
* The last N turns will always be preserved from summary compression.
|
|
20
|
-
* Tool compression ignores this setting and only preserves tool results from
|
|
21
|
-
* the most recent completed turn.
|
|
22
|
-
*/
|
|
23
|
-
protectedTurns?: number;
|
|
24
|
-
/**
|
|
25
|
-
* Model to use for generating summaries.
|
|
26
|
-
*/
|
|
27
|
-
model: ModelClient;
|
|
28
|
-
/**
|
|
29
|
-
* State store for persisting compression state.
|
|
30
|
-
*/
|
|
31
|
-
stateStore: StateStore;
|
|
32
|
-
/**
|
|
33
|
-
* Tool compression target (default: 0.45, meaning compress to 45% of maxTokens).
|
|
34
|
-
* When tool compression is triggered at 50%, it will compress until reaching this target.
|
|
35
|
-
*/
|
|
36
|
-
toolCompressionTarget?: number;
|
|
37
|
-
/**
|
|
38
|
-
* Minimum number of recent tool results to keep (default: 5).
|
|
39
|
-
* The most recent N tool results will be protected from compression.
|
|
40
|
-
*/
|
|
41
|
-
minKeepToolResults?: number;
|
|
42
|
-
/**
|
|
43
|
-
* Enable logging compression events to a file in the current working directory.
|
|
44
|
-
* If enabled, logs will be appended to 'compression-logs.jsonl' in JSONL format.
|
|
45
|
-
*/
|
|
46
|
-
enableLogging?: boolean;
|
|
47
|
-
/**
|
|
48
|
-
* Custom log file path (relative to cwd or absolute).
|
|
49
|
-
* Default: 'compression-logs.jsonl'
|
|
50
|
-
*/
|
|
51
|
-
logFilePath?: string;
|
|
52
|
-
/**
|
|
53
|
-
* Token limit of the model used for summarization.
|
|
54
|
-
* Used to determine when chunked summarization is needed.
|
|
55
|
-
* When the content to summarize exceeds this limit, the middleware
|
|
56
|
-
* automatically splits it into chunks and summarizes sequentially.
|
|
57
|
-
* Defaults to maxTokens if not specified.
|
|
58
|
-
*/
|
|
59
|
-
summaryModelMaxTokens?: number;
|
|
60
|
-
/**
|
|
61
|
-
* Maximum characters per individual message when formatting for summary.
|
|
62
|
-
* More aggressive truncation reduces the chance of needing chunked summarization.
|
|
63
|
-
* Applies to tool outputs, user messages, and assistant messages.
|
|
64
|
-
* Default: 3000
|
|
65
|
-
*/
|
|
66
|
-
maxMessageCharsForSummary?: number;
|
|
7
|
+
contextLength: number;
|
|
67
8
|
}
|
|
68
|
-
/**
|
|
69
|
-
* Options for manual compression.
|
|
70
|
-
*/
|
|
71
9
|
export interface ManualCompressionOptions {
|
|
72
|
-
/**
|
|
73
|
-
* Session ID for the compression.
|
|
74
|
-
*/
|
|
75
10
|
sessionId: string;
|
|
76
|
-
/**
|
|
77
|
-
* Full messages from the session (including all history).
|
|
78
|
-
* These should be the complete, uncompressed messages.
|
|
79
|
-
*/
|
|
80
11
|
fullMessages: Message[];
|
|
81
|
-
/**
|
|
82
|
-
* Model to use for generating summary.
|
|
83
|
-
*/
|
|
84
12
|
model: ModelClient;
|
|
85
|
-
/**
|
|
86
|
-
* State store for persisting compression state.
|
|
87
|
-
*/
|
|
88
13
|
stateStore: StateStore;
|
|
89
|
-
|
|
90
|
-
* Custom prompt for summary generation (optional).
|
|
91
|
-
* If not provided, uses DEFAULT_SUMMARY_PROMPT.
|
|
92
|
-
*/
|
|
93
|
-
summaryPrompt?: string;
|
|
94
|
-
/**
|
|
95
|
-
* Enable logging compression events to a file.
|
|
96
|
-
*/
|
|
97
|
-
enableLogging?: boolean;
|
|
98
|
-
/**
|
|
99
|
-
* Custom log file path (relative to cwd or absolute).
|
|
100
|
-
* Default: 'compression-logs.jsonl'
|
|
101
|
-
*/
|
|
102
|
-
logFilePath?: string;
|
|
103
|
-
/**
|
|
104
|
-
* Token limit of the model used for summarization.
|
|
105
|
-
* Used to determine when chunked summarization is needed.
|
|
106
|
-
* Defaults to 128000 if not specified.
|
|
107
|
-
*/
|
|
108
|
-
summaryModelMaxTokens?: number;
|
|
109
|
-
/**
|
|
110
|
-
* Maximum characters per individual message when formatting for summary.
|
|
111
|
-
* Default: 3000
|
|
112
|
-
*/
|
|
113
|
-
maxMessageCharsForSummary?: number;
|
|
14
|
+
contextLength: number;
|
|
114
15
|
}
|
|
115
|
-
/**
|
|
116
|
-
* Result of manual compression.
|
|
117
|
-
*/
|
|
118
16
|
export interface ManualCompressionResult {
|
|
119
|
-
/**
|
|
120
|
-
* Generated summary.
|
|
121
|
-
*/
|
|
122
17
|
summary: string;
|
|
123
|
-
/**
|
|
124
|
-
* Number of messages processed.
|
|
125
|
-
*/
|
|
126
18
|
messageCount: number;
|
|
127
|
-
/**
|
|
128
|
-
* Number of tool outputs found.
|
|
129
|
-
*/
|
|
130
19
|
toolOutputCount: number;
|
|
20
|
+
compressedMessages: Message[];
|
|
131
21
|
}
|
|
132
|
-
/**
|
|
133
|
-
* Statistics about the compression operation.
|
|
134
|
-
*/
|
|
135
22
|
export interface CompressionStats {
|
|
136
|
-
/** Token count before compression */
|
|
137
23
|
tokensBefore: number;
|
|
138
|
-
/** Token count after compression */
|
|
139
24
|
tokensAfter: number;
|
|
140
|
-
/** Number of tool outputs that were cleared */
|
|
141
25
|
clearedToolOutputs: number;
|
|
142
|
-
/** Number of messages that were removed by summarization */
|
|
143
26
|
removedMessages: number;
|
|
144
|
-
/** Whether a summary was generated */
|
|
145
27
|
summaryGenerated: boolean;
|
|
146
|
-
/** Timestamp when this compression occurred */
|
|
147
28
|
timestamp: number;
|
|
148
29
|
}
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
*
|
|
168
|
-
* @param options - Compression configuration options
|
|
169
|
-
* @returns Middleware function
|
|
170
|
-
*
|
|
171
|
-
* @example
|
|
172
|
-
* ```ts
|
|
173
|
-
* const agent = new Agent({ model, stateStore, ... })
|
|
174
|
-
*
|
|
175
|
-
* agent.use(createContextCompressionMiddleware({
|
|
176
|
-
* maxTokens: 128000,
|
|
177
|
-
* protectedTurns: 2,
|
|
178
|
-
* model: model,
|
|
179
|
-
* stateStore: agent.stateStore,
|
|
180
|
-
* toolCompressionTarget: 0.45, // Compress to 45% of maxTokens
|
|
181
|
-
* minKeepToolResults: 5, // Keep last 5 tool results
|
|
182
|
-
* }))
|
|
183
|
-
* ```
|
|
184
|
-
*/
|
|
30
|
+
export type CompressionTraceStage = 'stage1' | 'stage2' | 'stage3' | 'stage4';
|
|
31
|
+
export interface CompressionTraceSnapshot {
|
|
32
|
+
sessionId: string;
|
|
33
|
+
mode: CompressionMode;
|
|
34
|
+
stage: CompressionTraceStage;
|
|
35
|
+
contextLength: number;
|
|
36
|
+
triggerThreshold: number;
|
|
37
|
+
targetTokens: number;
|
|
38
|
+
tokens: number;
|
|
39
|
+
messages: Message[];
|
|
40
|
+
rawMessages: Message[];
|
|
41
|
+
summary: string;
|
|
42
|
+
coveredMessageCount: number;
|
|
43
|
+
clearedToolOutputs: number;
|
|
44
|
+
removedMessages: number;
|
|
45
|
+
summaryGenerated: boolean;
|
|
46
|
+
}
|
|
47
|
+
type CompressionMode = 'auto' | 'manual';
|
|
185
48
|
export declare function createContextCompressionMiddleware(options: ContextCompressionOptions): Middleware<AgentLoopState>;
|
|
186
|
-
/**
|
|
187
|
-
* Manually compress a session by generating a summary from full message history.
|
|
188
|
-
*/
|
|
189
49
|
export declare function compressSessionManually(options: ManualCompressionOptions): Promise<ManualCompressionResult>;
|
|
50
|
+
export {};
|
|
@@ -34,6 +34,12 @@ export interface SelfReflectionOptions {
|
|
|
34
34
|
* @default false
|
|
35
35
|
*/
|
|
36
36
|
debug?: boolean;
|
|
37
|
+
/**
|
|
38
|
+
* Critic subagent context window / compression budget.
|
|
39
|
+
*
|
|
40
|
+
* @default 128000
|
|
41
|
+
*/
|
|
42
|
+
maxTokens?: number;
|
|
37
43
|
}
|
|
38
44
|
/**
|
|
39
45
|
* Options for the long_running middleware.
|
|
@@ -31,6 +31,12 @@ export interface ParallelSubagentMiddlewareOptions {
|
|
|
31
31
|
* 是否输出调试日志(默认: false)
|
|
32
32
|
*/
|
|
33
33
|
debug?: boolean;
|
|
34
|
+
/**
|
|
35
|
+
* Subagent context window / compression budget.
|
|
36
|
+
*
|
|
37
|
+
* Defaults to 128000 when not provided.
|
|
38
|
+
*/
|
|
39
|
+
maxTokens?: number;
|
|
34
40
|
/**
|
|
35
41
|
* 可选:middleware 名称(默认: 'parallel_subagent')
|
|
36
42
|
*/
|
|
@@ -43,7 +49,7 @@ export interface ParallelSubagentMiddlewareOptions {
|
|
|
43
49
|
* 1. 自动创建并注册 TaskTool(包含 subagent 定义和 executor)
|
|
44
50
|
* 2. 启用并行执行模式:当 LLM 同时调用多个 Task 工具时自动并行执行
|
|
45
51
|
*
|
|
46
|
-
* TaskTool
|
|
52
|
+
* TaskTool 会以 `Task` 注册(本 middleware 使用无前缀注册,提高辨识度)
|
|
47
53
|
*
|
|
48
54
|
* @example 使用默认 executor(推荐)
|
|
49
55
|
* ```typescript
|
|
@@ -51,6 +51,11 @@ interface AnthropicThinkingBlock {
|
|
|
51
51
|
thinking: string;
|
|
52
52
|
}
|
|
53
53
|
type AnthropicContentBlock = AnthropicTextBlock | AnthropicImageBlock | AnthropicDocumentBlock | AnthropicToolUseBlock | AnthropicToolResultBlock | AnthropicThinkingBlock;
|
|
54
|
+
type AnthropicUsage = {
|
|
55
|
+
input_tokens?: number;
|
|
56
|
+
output_tokens?: number;
|
|
57
|
+
};
|
|
54
58
|
export declare function contentToBlocks(content: MessageContent): AnthropicContentBlock[];
|
|
59
|
+
export declare function mergeAnthropicUsage(base?: AnthropicUsage, next?: AnthropicUsage | null): AnthropicUsage | undefined;
|
|
55
60
|
export declare function createAnthropicAdapter(options: AnthropicAdapterOptions): ModelAdapter;
|
|
56
61
|
export {};
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { AgentHooks } from '../../agent/hooks';
|
|
2
2
|
import type { AgentLoopState, ToolCallWithResult } from '../../agent/types';
|
|
3
|
+
import type { ModelClient } from '../../model/types';
|
|
3
4
|
import type { ToolExecutionContext, ToolExecutionContextInput, ToolRegistry } from '../../tool';
|
|
4
5
|
import type { AgentEvent, AgentLoopCheckpoint, CheckpointModelConfig, CheckpointRequestParams, ToolCall } from '../../types';
|
|
5
6
|
import type { ApprovalHandler } from '../handlers/ApprovalHandler';
|
|
@@ -21,6 +22,8 @@ interface ToolExecutorOptions {
|
|
|
21
22
|
addAssistantMessageWithToolCalls: (state: AgentLoopState) => void;
|
|
22
23
|
addToolResultToHistory: (state: AgentLoopState, tc: ToolCallWithResult) => void;
|
|
23
24
|
persistSessionState: (state: AgentLoopState) => Promise<void>;
|
|
25
|
+
modelClient?: ModelClient;
|
|
26
|
+
getSessionMetadata?: () => Record<string, unknown>;
|
|
24
27
|
log?: (...args: unknown[]) => void;
|
|
25
28
|
}
|
|
26
29
|
export declare class ToolExecutor {
|
|
@@ -28,6 +31,7 @@ export declare class ToolExecutor {
|
|
|
28
31
|
private toolEventQueue;
|
|
29
32
|
constructor(options: ToolExecutorOptions);
|
|
30
33
|
private _log;
|
|
34
|
+
private createHookManager;
|
|
31
35
|
createToolExecutionContext(state: AgentLoopState, signal?: AbortSignal, toolCallId?: string, toolName?: string): ToolExecutionContext;
|
|
32
36
|
executeToolCall(tc: ToolCallWithResult, ctx: ToolExecutionContext, signal?: AbortSignal, hooks?: AgentHooks, permissionRequestResult?: {
|
|
33
37
|
allow: boolean;
|
package/dist/session/index.d.ts
CHANGED
|
@@ -1,10 +1,19 @@
|
|
|
1
1
|
import type { Middleware } from '../agent/middleware';
|
|
2
2
|
import type { AgentLoopState, CreateSessionOptions, SendOptions } from '../agent/types';
|
|
3
|
-
import type { ModelClient } from '../model';
|
|
3
|
+
import type { ModelClient, ModelStreamEvent } from '../model';
|
|
4
4
|
import type { StateStore } from '../state';
|
|
5
5
|
import type { ToolApprovalResult, ToolRegistry } from '../tool';
|
|
6
|
-
import type { AgentEvent, Message, MessageContent, ModelConfig, SessionConfigOverride, SessionSnapshot, SessionStatus } from '../types';
|
|
6
|
+
import type { AgentEvent, CallModelOnceOptions, CallModelOnceResult, Message, MessageContent, ModelConfig, SessionConfigOverride, SessionSnapshot, SessionStatus } from '../types';
|
|
7
|
+
import type { MessageQueueConfig, MessageQueueStatus } from './types/messageQueue';
|
|
7
8
|
import { EventEmitter } from 'node:events';
|
|
9
|
+
type QueuedSendOptions = SendOptions & {
|
|
10
|
+
priority?: number;
|
|
11
|
+
};
|
|
12
|
+
interface BatchMessageInput {
|
|
13
|
+
input: MessageContent;
|
|
14
|
+
options?: SendOptions;
|
|
15
|
+
priority?: number;
|
|
16
|
+
}
|
|
8
17
|
interface SessionRuntimeOptions extends CreateSessionOptions {
|
|
9
18
|
modelClient?: ModelClient;
|
|
10
19
|
systemPrompt?: string;
|
|
@@ -54,6 +63,7 @@ export declare class Session extends EventEmitter {
|
|
|
54
63
|
private readonly _systemPrompt?;
|
|
55
64
|
private readonly _agentName?;
|
|
56
65
|
private readonly _onUsage?;
|
|
66
|
+
private _messageQueue;
|
|
57
67
|
private _pendingInput;
|
|
58
68
|
private _isReceiving;
|
|
59
69
|
private readonly _hooks?;
|
|
@@ -69,6 +79,9 @@ export declare class Session extends EventEmitter {
|
|
|
69
79
|
private _checkpointOriginalMessages?;
|
|
70
80
|
private _checkpointNewMessages;
|
|
71
81
|
private _checkpointSkipSnapshot?;
|
|
82
|
+
private _rawMessages;
|
|
83
|
+
private _rawOriginalMessages?;
|
|
84
|
+
private _rawNewMessages;
|
|
72
85
|
private getDisabledToolNames;
|
|
73
86
|
private isToolDisabled;
|
|
74
87
|
private collectBlockedTools;
|
|
@@ -85,6 +98,7 @@ export declare class Session extends EventEmitter {
|
|
|
85
98
|
* @private
|
|
86
99
|
*/
|
|
87
100
|
private _syncCwdToTools;
|
|
101
|
+
private getOrCreateSessionMetadata;
|
|
88
102
|
/**
|
|
89
103
|
* Create a new Session or load from StateStore.
|
|
90
104
|
*
|
|
@@ -98,7 +112,14 @@ export declare class Session extends EventEmitter {
|
|
|
98
112
|
* This is an idempotent check that reads from StateStore each time.
|
|
99
113
|
*/
|
|
100
114
|
hasCheckpoint(): Promise<boolean>;
|
|
101
|
-
send(input: MessageContent, options?:
|
|
115
|
+
send(input: MessageContent, options?: QueuedSendOptions): string;
|
|
116
|
+
sendBatch(messages: BatchMessageInput[]): string[];
|
|
117
|
+
cancelQueuedMessage(messageId: string): boolean;
|
|
118
|
+
getQueueStatus(): MessageQueueStatus;
|
|
119
|
+
clearQueue(): void;
|
|
120
|
+
updateQueueConfig(config: Partial<MessageQueueConfig>): void;
|
|
121
|
+
private _tryDequeueNext;
|
|
122
|
+
private _markCheckpointQueueMessageAsConsumed;
|
|
102
123
|
/**
|
|
103
124
|
* Cancel the current agent execution.
|
|
104
125
|
*
|
|
@@ -123,6 +144,8 @@ export declare class Session extends EventEmitter {
|
|
|
123
144
|
* ```
|
|
124
145
|
*/
|
|
125
146
|
cancel(): void;
|
|
147
|
+
callModelOnceStream(options: CallModelOnceOptions): AsyncIterable<ModelStreamEvent>;
|
|
148
|
+
callModelOnce(options: CallModelOnceOptions): Promise<CallModelOnceResult>;
|
|
126
149
|
receive(options?: SendOptions): AsyncIterable<AgentEvent>;
|
|
127
150
|
/**
|
|
128
151
|
* Convenience helper for "pause → decide → resume" flows.
|
|
@@ -144,16 +167,26 @@ export declare class Session extends EventEmitter {
|
|
|
144
167
|
* This prevents the session from being stuck waiting for responses that will never come.
|
|
145
168
|
*/
|
|
146
169
|
private buildAutoRejectToolContext;
|
|
170
|
+
private mergeToolContexts;
|
|
171
|
+
private hasMeaningfulToolContext;
|
|
147
172
|
private getRuntimeModelRoute;
|
|
173
|
+
getRawMessages(): Message[];
|
|
174
|
+
getFullRawMessages(): Message[];
|
|
175
|
+
applyCommittedWorkingMessages(messages: Message[]): Promise<void>;
|
|
148
176
|
private _ensureRuntimeConfigured;
|
|
149
177
|
private extractTextFromContent;
|
|
150
178
|
private stripLeadingMarkdownLinkPrefix;
|
|
151
179
|
private createTitle;
|
|
180
|
+
private buildRawMessagesForCompression;
|
|
181
|
+
private seedCompressionMetadata;
|
|
152
182
|
private _recordUsage;
|
|
183
|
+
private normalizeUsage;
|
|
153
184
|
private executeModelStream;
|
|
154
185
|
private mergeStateResults;
|
|
155
186
|
private appendMessage;
|
|
156
187
|
private getCheckpointMessages;
|
|
188
|
+
private commitRuntimeState;
|
|
189
|
+
private resetLoopSnapshots;
|
|
157
190
|
private saveCheckpointForState;
|
|
158
191
|
private addToolResultToHistory;
|
|
159
192
|
private addAssistantMessageWithToolCalls;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './messageQueue';
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { SendOptions } from '../../agent/types';
|
|
2
|
+
import type { MessageContent } from '../../types';
|
|
3
|
+
export interface PendingMessage {
|
|
4
|
+
id: string;
|
|
5
|
+
input: MessageContent;
|
|
6
|
+
options?: SendOptions;
|
|
7
|
+
priority: number;
|
|
8
|
+
enqueuedAt: number;
|
|
9
|
+
status: 'pending' | 'cancelled';
|
|
10
|
+
}
|
|
11
|
+
export interface MessageQueueConfig {
|
|
12
|
+
autoProcessQueue: boolean;
|
|
13
|
+
maxQueueSize?: number;
|
|
14
|
+
}
|
|
15
|
+
export interface MessageQueueSnapshot {
|
|
16
|
+
messages: PendingMessage[];
|
|
17
|
+
config: MessageQueueConfig;
|
|
18
|
+
}
|
|
19
|
+
export interface MessageQueueStatus {
|
|
20
|
+
length: number;
|
|
21
|
+
messages: Array<{
|
|
22
|
+
id: string;
|
|
23
|
+
priority: number;
|
|
24
|
+
enqueuedAt: number;
|
|
25
|
+
preview: string;
|
|
26
|
+
}>;
|
|
27
|
+
isProcessing: boolean;
|
|
28
|
+
config: MessageQueueConfig;
|
|
29
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { MessageContent } from '../../types';
|
|
2
|
+
import type { SendOptions } from '../../agent/types';
|
|
3
|
+
import type { MessageQueueConfig, MessageQueueSnapshot, MessageQueueStatus, PendingMessage } from '../types/messageQueue';
|
|
4
|
+
export declare class MessageQueue {
|
|
5
|
+
private _messages;
|
|
6
|
+
private _config;
|
|
7
|
+
private _isProcessing;
|
|
8
|
+
constructor(config?: Partial<MessageQueueConfig>);
|
|
9
|
+
get config(): MessageQueueConfig;
|
|
10
|
+
updateConfig(config: Partial<MessageQueueConfig>): void;
|
|
11
|
+
setProcessing(isProcessing: boolean): void;
|
|
12
|
+
enqueue(input: MessageContent, options?: SendOptions, priority?: number): string;
|
|
13
|
+
dequeue(): PendingMessage | null;
|
|
14
|
+
peek(): PendingMessage | null;
|
|
15
|
+
cancel(messageId: string): boolean;
|
|
16
|
+
clear(): void;
|
|
17
|
+
isEmpty(): boolean;
|
|
18
|
+
size(): number;
|
|
19
|
+
getStatus(): MessageQueueStatus;
|
|
20
|
+
toSnapshot(): MessageQueueSnapshot;
|
|
21
|
+
static fromSnapshot(snapshot: MessageQueueSnapshot): MessageQueue;
|
|
22
|
+
private _sort;
|
|
23
|
+
}
|
package/dist/state/types.d.ts
CHANGED
|
@@ -18,6 +18,8 @@ export declare const StateKeys: {
|
|
|
18
18
|
readonly CHECKPOINT: "checkpoint";
|
|
19
19
|
/** CompressionState data */
|
|
20
20
|
readonly COMPRESSION: "compression";
|
|
21
|
+
/** Persisted compressed working view used for runtime resume/looping */
|
|
22
|
+
readonly COMPRESSION_VIEW: "compression-view";
|
|
21
23
|
/** SessionSnapshot data */
|
|
22
24
|
readonly SESSION: "session";
|
|
23
25
|
/**
|
|
@@ -58,41 +60,49 @@ export interface CompressedMessagesSnapshot {
|
|
|
58
60
|
* allowing the middleware to restore its state after a restart.
|
|
59
61
|
*/
|
|
60
62
|
export interface CompressionState {
|
|
63
|
+
lastStats?: CompressionStats;
|
|
64
|
+
summary?: string;
|
|
65
|
+
coveredMessageCount?: number;
|
|
66
|
+
baseRawSignature?: string;
|
|
67
|
+
baseRawMessageCount?: number;
|
|
68
|
+
updatedAt: number;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Persisted compressed working view.
|
|
72
|
+
*
|
|
73
|
+
* This is the runtime baseline used for future loop iterations, checkpointing,
|
|
74
|
+
* and resume after a successful compression commit. Raw history is stored
|
|
75
|
+
* separately by the session snapshot for audit/rebuild purposes.
|
|
76
|
+
*/
|
|
77
|
+
export interface CommittedCompressionView {
|
|
61
78
|
/**
|
|
62
|
-
*
|
|
79
|
+
* Number of full messages that were present in the raw view when this
|
|
80
|
+
* compressed working view was produced.
|
|
63
81
|
*/
|
|
64
|
-
|
|
82
|
+
baseMessageCount: number;
|
|
83
|
+
/**
|
|
84
|
+
* Deterministic signature of the raw base messages used to validate the
|
|
85
|
+
* committed view against rewrites/forks/truncation.
|
|
86
|
+
*/
|
|
87
|
+
baseMessagesSignature: string;
|
|
65
88
|
/**
|
|
66
|
-
*
|
|
89
|
+
* Compressed working view including system messages and any injected summary.
|
|
67
90
|
*/
|
|
68
|
-
|
|
91
|
+
messages: import('../types').Message[];
|
|
69
92
|
/**
|
|
70
|
-
* Rolling summary
|
|
71
|
-
* This can be used when resuming from a checkpoint to provide context.
|
|
93
|
+
* Rolling summary associated with this working view.
|
|
72
94
|
*/
|
|
73
95
|
summary?: string;
|
|
74
96
|
/**
|
|
75
|
-
*
|
|
97
|
+
* Estimated tokens of the committed working view after compression.
|
|
76
98
|
*/
|
|
77
|
-
|
|
99
|
+
tokensAfter: number;
|
|
100
|
+
/**
|
|
101
|
+
* Whether the view came from automatic or manual compression.
|
|
102
|
+
*/
|
|
103
|
+
mode: 'auto' | 'manual';
|
|
78
104
|
/**
|
|
79
|
-
*
|
|
80
|
-
* Used to remove the same messages when loading the summary on session resume.
|
|
105
|
+
* Creation timestamp (ms).
|
|
81
106
|
*/
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Index where summarization starts (first non-system message).
|
|
85
|
-
* Messages before this index (system messages) are always kept.
|
|
86
|
-
*/
|
|
87
|
-
summarizeStart: number;
|
|
88
|
-
/**
|
|
89
|
-
* Index where protected turns start (messages from this onward are kept).
|
|
90
|
-
*/
|
|
91
|
-
protectedStart: number;
|
|
92
|
-
/**
|
|
93
|
-
* Total number of messages at the time of compression.
|
|
94
|
-
* Used to validate if the range is still applicable.
|
|
95
|
-
*/
|
|
96
|
-
totalMessagesAtCompression: number;
|
|
97
|
-
};
|
|
107
|
+
createdAt: number;
|
|
98
108
|
}
|
package/dist/types/common.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { CallToolResult, ContentBlock, ImageContent, TextContent, Tool } from '@modelcontextprotocol/sdk/types.js';
|
|
2
|
+
import type { ModelStopReason } from '../model/types';
|
|
2
3
|
/**
|
|
3
4
|
* Re-export MCP types
|
|
4
5
|
*/
|
|
@@ -52,6 +53,17 @@ export interface Usage {
|
|
|
52
53
|
completionTokens: number;
|
|
53
54
|
totalTokens: number;
|
|
54
55
|
}
|
|
56
|
+
export interface CallModelOnceOptions {
|
|
57
|
+
input: string | MessageContent;
|
|
58
|
+
systemPrompt?: string;
|
|
59
|
+
temperature?: number;
|
|
60
|
+
maxTokens?: number;
|
|
61
|
+
}
|
|
62
|
+
export interface CallModelOnceResult {
|
|
63
|
+
text: string;
|
|
64
|
+
usage?: Usage;
|
|
65
|
+
stopReason: ModelStopReason;
|
|
66
|
+
}
|
|
55
67
|
/**
|
|
56
68
|
* Small reference stored in checkpoint metadata (JSON-safe).
|
|
57
69
|
*
|
package/dist/types/event.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ import type { AgentLoopCheckpoint } from './snapshot';
|
|
|
5
5
|
/**
|
|
6
6
|
* Event types for streaming responses
|
|
7
7
|
*/
|
|
8
|
-
export type AgentEventType = 'text_start' | 'text_delta' | 'text_end' | 'tool_call_start' | 'tool_call_delta' | 'tool_call_end' | 'tool_result' | 'tool_output_start' | 'tool_output_delta' | 'tool_approval_requested' | 'requires_action' | 'tool_skipped' | 'thinking_start' | 'thinking_delta' | 'thinking_end' | 'done' | 'iteration_start' | 'iteration_end' | 'session_created' | 'subagent_event' | 'completion_assessed' | 'compression_start' | 'compression_end';
|
|
8
|
+
export type AgentEventType = 'text_start' | 'text_delta' | 'text_end' | 'tool_call_start' | 'tool_call_delta' | 'tool_call_end' | 'tool_result' | 'tool_output_start' | 'tool_output_delta' | 'tool_approval_requested' | 'requires_action' | 'tool_skipped' | 'thinking_start' | 'thinking_delta' | 'thinking_end' | 'done' | 'iteration_start' | 'iteration_end' | 'session_created' | 'subagent_event' | 'completion_assessed' | 'compression_start' | 'compression_end' | 'hook_evaluation';
|
|
9
9
|
/**
|
|
10
10
|
* Base event interface
|
|
11
11
|
*/
|
|
@@ -13,6 +13,20 @@ export interface BaseEvent {
|
|
|
13
13
|
type: AgentEventType;
|
|
14
14
|
/** Session identifier for this event */
|
|
15
15
|
sessionId: string;
|
|
16
|
+
/**
|
|
17
|
+
* Stream identifier for this prompt/run.
|
|
18
|
+
* Optional for backward compatibility with existing emitters.
|
|
19
|
+
*/
|
|
20
|
+
streamId?: string;
|
|
21
|
+
/**
|
|
22
|
+
* Monotonic sequence number within a stream.
|
|
23
|
+
* Starts from 1 when present.
|
|
24
|
+
*/
|
|
25
|
+
seq?: number;
|
|
26
|
+
/**
|
|
27
|
+
* Whether this event is replayed history rather than live output.
|
|
28
|
+
*/
|
|
29
|
+
replayed?: boolean;
|
|
16
30
|
}
|
|
17
31
|
/**
|
|
18
32
|
* Text start event - marks beginning of a text response
|
|
@@ -107,6 +121,10 @@ export interface ToolApprovalRequestedEvent extends BaseEvent {
|
|
|
107
121
|
export interface RequiresActionEvent extends BaseEvent {
|
|
108
122
|
type: 'requires_action';
|
|
109
123
|
kind: 'tool_approval' | 'ask_user';
|
|
124
|
+
/**
|
|
125
|
+
* Stable action identifier used for resume routing.
|
|
126
|
+
*/
|
|
127
|
+
actionId?: string;
|
|
110
128
|
/**
|
|
111
129
|
* Checkpoint to resume from.
|
|
112
130
|
* If a checkpoint store is configured, this may be omitted in favor of `checkpointRef`.
|
|
@@ -134,6 +152,14 @@ export interface RequiresActionEvent extends BaseEvent {
|
|
|
134
152
|
}>;
|
|
135
153
|
multiSelect: boolean;
|
|
136
154
|
}>;
|
|
155
|
+
/**
|
|
156
|
+
* Structured action error.
|
|
157
|
+
*/
|
|
158
|
+
error?: {
|
|
159
|
+
code: string;
|
|
160
|
+
message: string;
|
|
161
|
+
details?: Record<string, unknown>;
|
|
162
|
+
};
|
|
137
163
|
}
|
|
138
164
|
/**
|
|
139
165
|
* Tool skipped event - emitted when a tool execution is skipped (e.g., approval denied)
|
|
@@ -266,6 +292,14 @@ export interface CompressionStartEvent extends BaseEvent {
|
|
|
266
292
|
tokensBefore: number;
|
|
267
293
|
/** Token threshold that triggered compression */
|
|
268
294
|
threshold: number;
|
|
295
|
+
lastTurnTokens?: number;
|
|
296
|
+
compressAfterLastUser?: boolean;
|
|
297
|
+
summaryProtectedStart?: number;
|
|
298
|
+
toolProtectedStart?: number;
|
|
299
|
+
protectedToolCount?: number;
|
|
300
|
+
latestToolIndex?: number;
|
|
301
|
+
latestToolTokens?: number;
|
|
302
|
+
latestToolOversized?: boolean;
|
|
269
303
|
}
|
|
270
304
|
/**
|
|
271
305
|
* Compression end event - emitted when AI-based summary compression completes.
|
|
@@ -287,7 +321,34 @@ export interface CompressionEndEvent extends BaseEvent {
|
|
|
287
321
|
/** Time taken for compression in milliseconds */
|
|
288
322
|
elapsedMs: number;
|
|
289
323
|
}
|
|
324
|
+
/**
|
|
325
|
+
* Hook evaluation event - emitted during prompt-based hook evaluation.
|
|
326
|
+
*
|
|
327
|
+
* Uses a single event type with `phase` for easier integration:
|
|
328
|
+
* - `start`: prompt evaluation request started
|
|
329
|
+
* - `stream`: incremental text delta from model
|
|
330
|
+
* - `end`: evaluation finished (success/error/timeout)
|
|
331
|
+
*/
|
|
332
|
+
export interface HookEvaluationEvent extends BaseEvent {
|
|
333
|
+
type: 'hook_evaluation';
|
|
334
|
+
evaluationId: string;
|
|
335
|
+
hookName: 'permissionRequest' | 'preToolUse' | 'postToolUse' | 'postToolUseFailure' | 'subagentStop' | 'userPromptSubmit';
|
|
336
|
+
phase: 'start' | 'stream' | 'end';
|
|
337
|
+
prompt?: string;
|
|
338
|
+
input?: unknown;
|
|
339
|
+
delta?: string;
|
|
340
|
+
rawResponse?: string;
|
|
341
|
+
result?: unknown;
|
|
342
|
+
usage?: Usage;
|
|
343
|
+
durationMs?: number;
|
|
344
|
+
status?: 'success' | 'error' | 'timeout';
|
|
345
|
+
error?: {
|
|
346
|
+
code?: string;
|
|
347
|
+
message: string;
|
|
348
|
+
};
|
|
349
|
+
toolCallId?: string;
|
|
350
|
+
}
|
|
290
351
|
/**
|
|
291
352
|
* Union type for all agent events
|
|
292
353
|
*/
|
|
293
|
-
export type AgentEvent = TextStartEvent | TextDeltaEvent | TextEndEvent | ToolCallStartEvent | ToolCallDeltaEvent | ToolCallEndEvent | ToolResultEvent | ToolOutputStartEvent | ToolOutputDeltaEvent | ToolApprovalRequestedEvent | RequiresActionEvent | ToolSkippedEvent | ThinkingStartEvent | ThinkingDeltaEvent | ThinkingEndEvent | DoneEvent | IterationStartEvent | IterationEndEvent | SessionCreatedEvent | SubagentEvent | CompletionAssessedEvent | CompressionStartEvent | CompressionEndEvent;
|
|
354
|
+
export type AgentEvent = TextStartEvent | TextDeltaEvent | TextEndEvent | ToolCallStartEvent | ToolCallDeltaEvent | ToolCallEndEvent | ToolResultEvent | ToolOutputStartEvent | ToolOutputDeltaEvent | ToolApprovalRequestedEvent | RequiresActionEvent | ToolSkippedEvent | ThinkingStartEvent | ThinkingDeltaEvent | ThinkingEndEvent | DoneEvent | IterationStartEvent | IterationEndEvent | SessionCreatedEvent | SubagentEvent | CompletionAssessedEvent | CompressionStartEvent | CompressionEndEvent | HookEvaluationEvent;
|
package/dist/types/index.d.ts
CHANGED