goatchain 0.0.3 → 0.0.5
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/dist/acp-adapter/ACPAgent.d.ts +63 -0
- package/dist/acp-adapter/ProtocolConverter.d.ts +48 -0
- package/dist/acp-adapter/SessionRouter.d.ts +50 -0
- package/dist/acp-adapter/index.d.ts +12 -0
- package/dist/acp-adapter/types.d.ts +95 -0
- package/dist/acp-server.d.ts +21 -0
- package/dist/agent/agent.d.ts +196 -0
- package/dist/agent/errors.d.ts +29 -0
- package/dist/agent/hooks/index.d.ts +2 -0
- package/dist/agent/hooks/manager.d.ts +8 -0
- package/dist/agent/hooks/types.d.ts +19 -0
- package/dist/agent/index.d.ts +14 -0
- package/dist/agent/middleware.d.ts +162 -0
- package/dist/agent/tokenCounter.d.ts +55 -0
- package/dist/agent/types.d.ts +247 -0
- package/dist/index.d.ts +21 -4484
- package/dist/index.js +230 -81
- package/dist/middleware/checkpointMiddleware.d.ts +29 -0
- package/dist/middleware/commitModeMiddleware.d.ts +93 -0
- package/dist/middleware/contextCompressionMiddleware.d.ts +131 -0
- package/dist/middleware/gitUtils.d.ts +119 -0
- package/dist/middleware/parallelSubagentMiddleware.d.ts +83 -0
- package/dist/middleware/planModeMiddleware.d.ts +80 -0
- package/dist/middleware/utils.d.ts +58 -0
- package/dist/model/adapter.d.ts +14 -0
- package/dist/model/createModel.d.ts +168 -0
- package/dist/model/errors.d.ts +14 -0
- package/dist/model/health.d.ts +35 -0
- package/dist/model/index.d.ts +7 -0
- package/dist/model/openai/createOpenAIAdapter.d.ts +23 -0
- package/dist/model/router.d.ts +17 -0
- package/dist/model/types.d.ts +190 -0
- package/dist/model/utils/http.d.ts +15 -0
- package/dist/model/utils/id.d.ts +1 -0
- package/dist/model/utils/retry.d.ts +142 -0
- package/dist/session/base.d.ts +1 -0
- package/dist/session/executors/ParallelTaskExecutor.d.ts +16 -0
- package/dist/session/executors/ToolExecutor.d.ts +37 -0
- package/dist/session/handlers/ApprovalHandler.d.ts +6 -0
- package/dist/session/handlers/CheckpointManager.d.ts +10 -0
- package/dist/session/index.d.ts +4 -0
- package/dist/session/manager.d.ts +35 -0
- package/dist/session/session.d.ts +159 -0
- package/dist/session/stateStoreSessionManager.d.ts +36 -0
- package/dist/session/utils/AsyncQueue.d.ts +7 -0
- package/dist/session/utils/AutoSaveManager.d.ts +6 -0
- package/dist/state/FileStateStore.d.ts +72 -0
- package/dist/state/InMemoryStateStore.d.ts +46 -0
- package/dist/state/index.d.ts +6 -0
- package/dist/state/stateStore.d.ts +181 -0
- package/dist/state/types.d.ts +85 -0
- package/dist/subagent/file-search-specialist.d.ts +16 -0
- package/dist/subagent/index.d.ts +7 -0
- package/dist/tool/FilteredToolRegistry.d.ts +82 -0
- package/dist/tool/base.d.ts +57 -0
- package/dist/tool/builtin/askUser.d.ts +90 -0
- package/dist/tool/builtin/astGrepCli.d.ts +37 -0
- package/dist/tool/builtin/astGrepFormat.d.ts +4 -0
- package/dist/tool/builtin/astGrepReplace.d.ts +71 -0
- package/dist/tool/builtin/astGrepSearch.d.ts +101 -0
- package/dist/tool/builtin/bash.d.ts +96 -0
- package/dist/tool/builtin/edit.d.ts +82 -0
- package/dist/tool/builtin/enterPlanMode.d.ts +58 -0
- package/dist/tool/builtin/exitPlanMode.d.ts +48 -0
- package/dist/tool/builtin/glob.d.ts +70 -0
- package/dist/tool/builtin/grep.d.ts +122 -0
- package/dist/tool/builtin/index.d.ts +30 -0
- package/dist/tool/builtin/read.d.ts +127 -0
- package/dist/tool/builtin/skill.d.ts +111 -0
- package/dist/tool/builtin/task.d.ts +132 -0
- package/dist/tool/builtin/todoPlan.d.ts +52 -0
- package/dist/tool/builtin/todoWrite.d.ts +94 -0
- package/dist/tool/builtin/webFetch.d.ts +62 -0
- package/dist/tool/builtin/webSearch.d.ts +89 -0
- package/dist/tool/builtin/write.d.ts +103 -0
- package/dist/tool/index.d.ts +6 -0
- package/dist/tool/registry.d.ts +55 -0
- package/dist/tool/types.d.ts +107 -0
- package/dist/types/common.d.ts +53 -0
- package/dist/types/event.d.ts +217 -0
- package/dist/types/index.d.ts +4 -0
- package/dist/types/message.d.ts +44 -0
- package/dist/types/snapshot.d.ts +329 -0
- package/package.json +29 -27
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import type { Agent } from '../agent';
|
|
2
|
+
import type { Session } from '../session';
|
|
3
|
+
import type { ToolApprovalResult } from '../tool';
|
|
4
|
+
import type { ACPAgentOptions, ACPMessage, ACPMetadata } from './types';
|
|
5
|
+
/**
|
|
6
|
+
* ACPAgent wraps a GoatChain Agent and provides ACP protocol interface
|
|
7
|
+
*
|
|
8
|
+
* Key features:
|
|
9
|
+
* - Session-based routing using session_id from metadata
|
|
10
|
+
* - Bidirectional protocol conversion (ACP ↔ GoatChain)
|
|
11
|
+
* - Transparent middleware support (plan mode, subagent, etc.)
|
|
12
|
+
* - Streaming event forwarding
|
|
13
|
+
*/
|
|
14
|
+
export declare class ACPAgent {
|
|
15
|
+
private agent;
|
|
16
|
+
private router;
|
|
17
|
+
private converter;
|
|
18
|
+
private debug;
|
|
19
|
+
constructor(agent: Agent, options?: ACPAgentOptions);
|
|
20
|
+
/**
|
|
21
|
+
* Main entry point for receiving ACP messages and returning ACP responses
|
|
22
|
+
*
|
|
23
|
+
* This method:
|
|
24
|
+
* 1. Extracts session_id from metadata
|
|
25
|
+
* 2. Gets or creates the corresponding GoatChain Session
|
|
26
|
+
* 3. Converts ACP messages to GoatChain format
|
|
27
|
+
* 4. Sends messages to the session
|
|
28
|
+
* 5. Streams back converted ACP responses
|
|
29
|
+
*
|
|
30
|
+
* @param messages - Array of ACP messages
|
|
31
|
+
* @param metadata - Optional metadata including session_id
|
|
32
|
+
* @returns AsyncGenerator yielding ACP message responses
|
|
33
|
+
*/
|
|
34
|
+
receiveMessage(messages: ACPMessage[], metadata?: ACPMetadata): AsyncGenerator<ACPMessage, void, void>;
|
|
35
|
+
/**
|
|
36
|
+
* Resume a session after tool approval decisions are provided.
|
|
37
|
+
*/
|
|
38
|
+
receiveWithApprovals(decisions: Record<string, ToolApprovalResult>, metadata?: ACPMetadata): AsyncGenerator<ACPMessage, void, void>;
|
|
39
|
+
/**
|
|
40
|
+
* Get information about active sessions
|
|
41
|
+
*/
|
|
42
|
+
getSessionInfo(): {
|
|
43
|
+
activeSessions: string[];
|
|
44
|
+
sessionCount: number;
|
|
45
|
+
};
|
|
46
|
+
getSession(sessionId: string): Session | undefined;
|
|
47
|
+
/**
|
|
48
|
+
* Manually close a specific session
|
|
49
|
+
*/
|
|
50
|
+
closeSession(sessionId: string): boolean;
|
|
51
|
+
/**
|
|
52
|
+
* Get the wrapped GoatChain agent
|
|
53
|
+
*/
|
|
54
|
+
getAgent(): Agent;
|
|
55
|
+
/**
|
|
56
|
+
* Clean up resources
|
|
57
|
+
*/
|
|
58
|
+
destroy(): void;
|
|
59
|
+
/**
|
|
60
|
+
* Generate a unique session ID
|
|
61
|
+
*/
|
|
62
|
+
private generateSessionId;
|
|
63
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { AgentEvent, Message as GoatChainMessage } from '../types';
|
|
2
|
+
import type { ACPMessage } from './types';
|
|
3
|
+
/**
|
|
4
|
+
* ProtocolConverter handles bidirectional conversion between
|
|
5
|
+
* GoatChain events/messages and ACP protocol messages
|
|
6
|
+
*/
|
|
7
|
+
export declare class ProtocolConverter {
|
|
8
|
+
private debug;
|
|
9
|
+
constructor(options?: {
|
|
10
|
+
debug?: boolean;
|
|
11
|
+
});
|
|
12
|
+
/**
|
|
13
|
+
* Convert GoatChain event stream to ACP message stream
|
|
14
|
+
*/
|
|
15
|
+
convertToACP(events: AsyncIterable<AgentEvent>): AsyncGenerator<ACPMessage, void, void>;
|
|
16
|
+
/**
|
|
17
|
+
* Convert subagent event to ACP messages
|
|
18
|
+
*/
|
|
19
|
+
private convertSubagentEventToACP;
|
|
20
|
+
/**
|
|
21
|
+
* Convert regular (non-subagent) event to ACP messages
|
|
22
|
+
*/
|
|
23
|
+
private convertRegularEvent;
|
|
24
|
+
/**
|
|
25
|
+
* Convert ACP messages to GoatChain message format
|
|
26
|
+
*/
|
|
27
|
+
convertFromACP(messages: ACPMessage[]): GoatChainMessage[];
|
|
28
|
+
/**
|
|
29
|
+
* Convert a single ACP message to GoatChain message
|
|
30
|
+
*/
|
|
31
|
+
private convertSingleMessage;
|
|
32
|
+
/**
|
|
33
|
+
* Convert GoatChain ToolCall to ACP ToolCall
|
|
34
|
+
*/
|
|
35
|
+
private convertToolCallToACP;
|
|
36
|
+
/**
|
|
37
|
+
* Convert ACP ToolCall to GoatChain ToolCall
|
|
38
|
+
*/
|
|
39
|
+
private convertToolCallFromACP;
|
|
40
|
+
/**
|
|
41
|
+
* Extract text content from ACP content
|
|
42
|
+
*/
|
|
43
|
+
private extractTextContent;
|
|
44
|
+
/**
|
|
45
|
+
* Format tool result for ACP
|
|
46
|
+
*/
|
|
47
|
+
private formatToolResult;
|
|
48
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { Agent } from '../agent';
|
|
2
|
+
import type { Session } from '../session';
|
|
3
|
+
/**
|
|
4
|
+
* SessionRouter manages session lifecycle and routing based on session_id
|
|
5
|
+
*/
|
|
6
|
+
export declare class SessionRouter {
|
|
7
|
+
private sessions;
|
|
8
|
+
private sessionContexts;
|
|
9
|
+
private sessionTimeout;
|
|
10
|
+
private maxConcurrentSessions;
|
|
11
|
+
private maxIterations?;
|
|
12
|
+
private cleanupTimer?;
|
|
13
|
+
constructor(options?: {
|
|
14
|
+
sessionTimeout?: number;
|
|
15
|
+
maxConcurrentSessions?: number;
|
|
16
|
+
maxIterations?: number;
|
|
17
|
+
});
|
|
18
|
+
/**
|
|
19
|
+
* Get or create a session for the given session_id
|
|
20
|
+
*/
|
|
21
|
+
getOrCreateSession(sessionId: string, agent: Agent): Promise<Session>;
|
|
22
|
+
/**
|
|
23
|
+
* Get an existing session (without creating)
|
|
24
|
+
*/
|
|
25
|
+
getSession(sessionId: string): Session | undefined;
|
|
26
|
+
/**
|
|
27
|
+
* Remove a specific session
|
|
28
|
+
*/
|
|
29
|
+
removeSession(sessionId: string): boolean;
|
|
30
|
+
/**
|
|
31
|
+
* Get all active session IDs
|
|
32
|
+
*/
|
|
33
|
+
getActiveSessions(): string[];
|
|
34
|
+
/**
|
|
35
|
+
* Get session count
|
|
36
|
+
*/
|
|
37
|
+
getSessionCount(): number;
|
|
38
|
+
/**
|
|
39
|
+
* Cleanup expired sessions based on timeout
|
|
40
|
+
*/
|
|
41
|
+
private cleanupExpiredSessions;
|
|
42
|
+
/**
|
|
43
|
+
* Remove the oldest session (by creation time)
|
|
44
|
+
*/
|
|
45
|
+
private removeOldestSession;
|
|
46
|
+
/**
|
|
47
|
+
* Cleanup and stop the router
|
|
48
|
+
*/
|
|
49
|
+
destroy(): void;
|
|
50
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ACP (Agent Communication Protocol) Adapter for GoatChain
|
|
3
|
+
*
|
|
4
|
+
* This module provides an adapter layer to make GoatChain agents compatible
|
|
5
|
+
* with the ACP protocol, enabling interoperability with ACP clients.
|
|
6
|
+
*
|
|
7
|
+
* @packageDocumentation
|
|
8
|
+
*/
|
|
9
|
+
export { ACPAgent } from './ACPAgent';
|
|
10
|
+
export { ProtocolConverter } from './ProtocolConverter';
|
|
11
|
+
export { SessionRouter } from './SessionRouter';
|
|
12
|
+
export type { ACPAgentOptions, ACPContent, ACPMessage, ACPMetadata, ACPRole, ACPToolCall, ExtendedAgentEvent, SessionContext, SubagentMetadata, } from './types';
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import type { AgentEvent } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* ACP message role types
|
|
4
|
+
*/
|
|
5
|
+
export type ACPRole = 'user' | 'assistant' | 'tool' | 'system';
|
|
6
|
+
/**
|
|
7
|
+
* ACP message content (simplified)
|
|
8
|
+
* In practice, this should match the ACP SDK types
|
|
9
|
+
*/
|
|
10
|
+
export type ACPContent = string | Array<{
|
|
11
|
+
type: string;
|
|
12
|
+
text?: string;
|
|
13
|
+
[key: string]: unknown;
|
|
14
|
+
}>;
|
|
15
|
+
/**
|
|
16
|
+
* ACP tool call structure
|
|
17
|
+
*/
|
|
18
|
+
export interface ACPToolCall {
|
|
19
|
+
id: string;
|
|
20
|
+
type: 'function';
|
|
21
|
+
function: {
|
|
22
|
+
name: string;
|
|
23
|
+
arguments: string | Record<string, unknown>;
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* ACP message structure
|
|
28
|
+
*/
|
|
29
|
+
export interface ACPMessage {
|
|
30
|
+
role: ACPRole;
|
|
31
|
+
content?: ACPContent;
|
|
32
|
+
tool_calls?: ACPToolCall[];
|
|
33
|
+
tool_call_id?: string;
|
|
34
|
+
name?: string;
|
|
35
|
+
metadata?: Record<string, unknown>;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* ACP metadata for session routing
|
|
39
|
+
*/
|
|
40
|
+
export interface ACPMetadata {
|
|
41
|
+
session_id?: string;
|
|
42
|
+
[key: string]: unknown;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Configuration options for ACPAgent
|
|
46
|
+
*/
|
|
47
|
+
export interface ACPAgentOptions {
|
|
48
|
+
/**
|
|
49
|
+
* Session timeout in milliseconds (default: 30 minutes)
|
|
50
|
+
*/
|
|
51
|
+
sessionTimeout?: number;
|
|
52
|
+
/**
|
|
53
|
+
* Maximum number of concurrent sessions (default: 10)
|
|
54
|
+
*/
|
|
55
|
+
maxConcurrentSessions?: number;
|
|
56
|
+
/**
|
|
57
|
+
* Enable debug logging
|
|
58
|
+
*/
|
|
59
|
+
debug?: boolean;
|
|
60
|
+
/**
|
|
61
|
+
* Maximum number of loop iterations per session (default: 30)
|
|
62
|
+
*/
|
|
63
|
+
maxIterations?: number;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Internal session context
|
|
67
|
+
*/
|
|
68
|
+
export interface SessionContext {
|
|
69
|
+
sessionId: string;
|
|
70
|
+
createdAt: number;
|
|
71
|
+
lastAccessedAt: number;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Event conversion result for internal use
|
|
75
|
+
*/
|
|
76
|
+
export interface ConversionResult {
|
|
77
|
+
messages: ACPMessage[];
|
|
78
|
+
shouldClose?: boolean;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Subagent event metadata
|
|
82
|
+
*/
|
|
83
|
+
export interface SubagentMetadata {
|
|
84
|
+
subagent: boolean;
|
|
85
|
+
subagentId: string;
|
|
86
|
+
subagentType: string;
|
|
87
|
+
originalEventType?: string;
|
|
88
|
+
[key: string]: unknown;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Extended AgentEvent with potential subagent metadata
|
|
92
|
+
*/
|
|
93
|
+
export type ExtendedAgentEvent = AgentEvent & {
|
|
94
|
+
metadata?: SubagentMetadata | Record<string, unknown>;
|
|
95
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* Simple ACP Server for GoatChain
|
|
4
|
+
*
|
|
5
|
+
* This script exposes a GoatChain agent as an ACP-compliant server via stdio,
|
|
6
|
+
* enabling integration with ACP clients like Zed editor.
|
|
7
|
+
*
|
|
8
|
+
* Usage:
|
|
9
|
+
* pnpm acp-server
|
|
10
|
+
*
|
|
11
|
+
* Or in Zed settings.json:
|
|
12
|
+
* {
|
|
13
|
+
* "agent_servers": {
|
|
14
|
+
* "goatchain": {
|
|
15
|
+
* "command": "pnpm",
|
|
16
|
+
* "args": ["--dir", "/path/to/GoatChain", "acp-server"]
|
|
17
|
+
* }
|
|
18
|
+
* }
|
|
19
|
+
* }
|
|
20
|
+
*/
|
|
21
|
+
import 'dotenv/config';
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import type { ModelClient, ModelRef } from '../model';
|
|
2
|
+
import type { BaseSessionManager } from '../session';
|
|
3
|
+
import type { StateStore } from '../state';
|
|
4
|
+
import type { ToolRegistry } from '../tool';
|
|
5
|
+
import type { Middleware, NamedMiddleware } from './middleware';
|
|
6
|
+
import type { AgentLoopState, AgentOptions, CreateSessionOptions, SessionHandleOptions } from './types';
|
|
7
|
+
import { Session } from '../session';
|
|
8
|
+
/**
|
|
9
|
+
* Agent class - the main orchestrator.
|
|
10
|
+
*
|
|
11
|
+
* Agent is a blueprint/configuration that can handle multiple sessions.
|
|
12
|
+
* It composes model, tools, state store, and session manager.
|
|
13
|
+
*
|
|
14
|
+
* Supports middleware pattern for extensible hooks at each loop iteration.
|
|
15
|
+
*/
|
|
16
|
+
export declare class Agent {
|
|
17
|
+
readonly id: string;
|
|
18
|
+
readonly name: string;
|
|
19
|
+
readonly systemPrompt: string;
|
|
20
|
+
readonly createdAt: number;
|
|
21
|
+
private _model;
|
|
22
|
+
private _modelOverride?;
|
|
23
|
+
private _tools?;
|
|
24
|
+
private _stateStore?;
|
|
25
|
+
private _sessionManager?;
|
|
26
|
+
private _middlewares;
|
|
27
|
+
private _middlewareCounter;
|
|
28
|
+
private _middlewareTools;
|
|
29
|
+
constructor(options: AgentOptions);
|
|
30
|
+
/**
|
|
31
|
+
* Get the model client
|
|
32
|
+
*/
|
|
33
|
+
get model(): ModelClient;
|
|
34
|
+
/**
|
|
35
|
+
* Get the effective default model ID.
|
|
36
|
+
*/
|
|
37
|
+
get modelId(): string;
|
|
38
|
+
/**
|
|
39
|
+
* Get the effective default model reference (provider + modelId), if available.
|
|
40
|
+
*
|
|
41
|
+
* - If `setModel(ModelRef)` was used, this returns that pinned model.
|
|
42
|
+
* - Otherwise, returns the model client's default `modelRef` (if supported).
|
|
43
|
+
*/
|
|
44
|
+
get modelRef(): ModelRef | undefined;
|
|
45
|
+
/**
|
|
46
|
+
* Get the available model references (e.g. routing fallback order), if supported by the model client.
|
|
47
|
+
*/
|
|
48
|
+
get modelRefs(): ModelRef[];
|
|
49
|
+
/**
|
|
50
|
+
* Get the tool registry
|
|
51
|
+
*/
|
|
52
|
+
get tools(): ToolRegistry | undefined;
|
|
53
|
+
/**
|
|
54
|
+
* Get the state store
|
|
55
|
+
*/
|
|
56
|
+
get stateStore(): StateStore | undefined;
|
|
57
|
+
/**
|
|
58
|
+
* Get the session manager
|
|
59
|
+
*/
|
|
60
|
+
get sessionManager(): BaseSessionManager | undefined;
|
|
61
|
+
/**
|
|
62
|
+
* Create a new session and return a Session.
|
|
63
|
+
*/
|
|
64
|
+
createSession(options?: CreateSessionOptions): Promise<Session>;
|
|
65
|
+
/**
|
|
66
|
+
* Resume an existing session and return a Session.
|
|
67
|
+
*/
|
|
68
|
+
resumeSession(sessionId: string, options?: SessionHandleOptions): Promise<Session>;
|
|
69
|
+
/**
|
|
70
|
+
* Add middleware to the agent.
|
|
71
|
+
*
|
|
72
|
+
* Middleware runs at each loop iteration and can intercept/transform the loop state.
|
|
73
|
+
* Middleware follows an immutable pattern - it receives state and returns new state via next().
|
|
74
|
+
*
|
|
75
|
+
* If the middleware provides a `__createTools` function, the tools will be automatically
|
|
76
|
+
* registered with a namespace prefix (`<middlewareName>_<toolName>`) and automatically
|
|
77
|
+
* unregistered when the middleware is removed.
|
|
78
|
+
*
|
|
79
|
+
* @param fn - Middleware function
|
|
80
|
+
* @param name - Optional name for the middleware. If not provided:
|
|
81
|
+
* - Uses fn.__middlewareName if available (set by built-in factories)
|
|
82
|
+
* - Otherwise auto-generates a unique name
|
|
83
|
+
* @returns A function to remove this middleware (unsubscribe)
|
|
84
|
+
*
|
|
85
|
+
* @example
|
|
86
|
+
* ```ts
|
|
87
|
+
* // Named middleware (recommended)
|
|
88
|
+
* agent.use(async (state, next) => {
|
|
89
|
+
* console.log('Before iteration', state.iteration);
|
|
90
|
+
* const result = await next(state);
|
|
91
|
+
* console.log('After iteration', state.iteration);
|
|
92
|
+
* return result;
|
|
93
|
+
* }, 'logging');
|
|
94
|
+
*
|
|
95
|
+
* // Built-in middleware with default name
|
|
96
|
+
* agent.use(createContextCompressionMiddleware({ ... })); // auto-named 'context-compression'
|
|
97
|
+
*
|
|
98
|
+
* // Override default name
|
|
99
|
+
* agent.use(createPlanModeMiddleware(), 'my-plan-mode');
|
|
100
|
+
*
|
|
101
|
+
* // Middleware with tools (auto-registers tools with namespace)
|
|
102
|
+
* const middleware: Middleware = async (state, next) => next(state);
|
|
103
|
+
* middleware.__middlewareName = 'plan-mode';
|
|
104
|
+
* middleware.__createTools = () => [new EnterPlanModeTool()];
|
|
105
|
+
* agent.use(middleware);
|
|
106
|
+
* // Tool registered as: 'plan-mode_EnterPlanMode'
|
|
107
|
+
*
|
|
108
|
+
* // Anonymous middleware (auto-named)
|
|
109
|
+
* const unsubscribe = agent.use(async (state, next) => {
|
|
110
|
+
* const compressed = { ...state, messages: compress(state.messages) };
|
|
111
|
+
* return next(compressed);
|
|
112
|
+
* });
|
|
113
|
+
*
|
|
114
|
+
* // Remove using unsubscribe function
|
|
115
|
+
* unsubscribe();
|
|
116
|
+
* ```
|
|
117
|
+
*/
|
|
118
|
+
use(fn: Middleware<AgentLoopState>, name?: string): () => void;
|
|
119
|
+
/**
|
|
120
|
+
* Remove a specific middleware from the agent by name or function reference.
|
|
121
|
+
*
|
|
122
|
+
* If the middleware has associated tools (registered via `__createTools`),
|
|
123
|
+
* they will be automatically unregistered.
|
|
124
|
+
*
|
|
125
|
+
* @param nameOrFn - The middleware name (string) or function to remove
|
|
126
|
+
* @returns true if the middleware was found and removed, false otherwise
|
|
127
|
+
*
|
|
128
|
+
* @example
|
|
129
|
+
* ```ts
|
|
130
|
+
* // Remove by name (recommended)
|
|
131
|
+
* agent.use(loggingMiddleware, 'logging');
|
|
132
|
+
* agent.removeMiddleware('logging');
|
|
133
|
+
*
|
|
134
|
+
* // Remove by function reference (legacy)
|
|
135
|
+
* const loggingMiddleware = async (state, next) => {
|
|
136
|
+
* console.log('Before iteration', state.iteration);
|
|
137
|
+
* return next(state);
|
|
138
|
+
* };
|
|
139
|
+
* agent.use(loggingMiddleware);
|
|
140
|
+
* agent.removeMiddleware(loggingMiddleware);
|
|
141
|
+
* ```
|
|
142
|
+
*/
|
|
143
|
+
removeMiddleware(nameOrFn: string | Middleware<AgentLoopState>): boolean;
|
|
144
|
+
/**
|
|
145
|
+
* Clear all middlewares from the agent.
|
|
146
|
+
*
|
|
147
|
+
* Also removes all tools that were registered by middlewares via `__createTools`.
|
|
148
|
+
*
|
|
149
|
+
* @example
|
|
150
|
+
* ```ts
|
|
151
|
+
* agent.clearMiddlewares();
|
|
152
|
+
* ```
|
|
153
|
+
*/
|
|
154
|
+
clearMiddlewares(): void;
|
|
155
|
+
/**
|
|
156
|
+
* Get the current list of named middlewares (read-only).
|
|
157
|
+
*
|
|
158
|
+
* @returns A copy of the middlewares array with their names
|
|
159
|
+
*/
|
|
160
|
+
get middlewares(): ReadonlyArray<NamedMiddleware<AgentLoopState>>;
|
|
161
|
+
/**
|
|
162
|
+
* Get all middleware names.
|
|
163
|
+
*
|
|
164
|
+
* @returns Array of middleware names in registration order
|
|
165
|
+
*
|
|
166
|
+
* @example
|
|
167
|
+
* ```ts
|
|
168
|
+
* agent.use(loggingMiddleware, 'logging');
|
|
169
|
+
* agent.use(compressionMiddleware, 'compression');
|
|
170
|
+
* console.log(agent.middlewareNames); // ['logging', 'compression']
|
|
171
|
+
* ```
|
|
172
|
+
*/
|
|
173
|
+
get middlewareNames(): string[];
|
|
174
|
+
/**
|
|
175
|
+
* Switch to a different model
|
|
176
|
+
*
|
|
177
|
+
* @param model - New model client to use, or a `ModelRef` to pin the default model for subsequent requests.
|
|
178
|
+
*/
|
|
179
|
+
setModel(model: ModelClient): void;
|
|
180
|
+
setModel(model: ModelRef): void;
|
|
181
|
+
/**
|
|
182
|
+
* Get namespaced tool name for a middleware's tool
|
|
183
|
+
* @private
|
|
184
|
+
*/
|
|
185
|
+
private _getNamespacedToolName;
|
|
186
|
+
/**
|
|
187
|
+
* Register tools provided by a middleware's __createTools function
|
|
188
|
+
* @private
|
|
189
|
+
*/
|
|
190
|
+
private _registerMiddlewareTools;
|
|
191
|
+
/**
|
|
192
|
+
* Unregister all tools associated with a middleware
|
|
193
|
+
* @private
|
|
194
|
+
*/
|
|
195
|
+
private _unregisterMiddlewareTools;
|
|
196
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error thrown when agent execution is aborted via AbortSignal
|
|
3
|
+
*/
|
|
4
|
+
export declare class AgentAbortError extends Error {
|
|
5
|
+
constructor(message?: string);
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Error thrown when agent exceeds maximum iterations
|
|
9
|
+
*/
|
|
10
|
+
export declare class AgentMaxIterationsError extends Error {
|
|
11
|
+
readonly iterations: number;
|
|
12
|
+
constructor(iterations: number, message?: string);
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Internal control-flow error used to stop streaming when execution is paused.
|
|
16
|
+
*
|
|
17
|
+
* This is caught by the Agent and is not intended to be shown to end users.
|
|
18
|
+
*/
|
|
19
|
+
export declare class AgentPauseError extends Error {
|
|
20
|
+
constructor(message?: string);
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Check if the abort signal has been triggered and throw if so.
|
|
24
|
+
*
|
|
25
|
+
* @param signal - AbortSignal to check
|
|
26
|
+
* @param context - Optional context for error message
|
|
27
|
+
* @throws AgentAbortError if signal is aborted
|
|
28
|
+
*/
|
|
29
|
+
export declare function ensureNotAborted(signal?: AbortSignal, context?: string): void;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { HookContext, PreToolUseResult, ToolHooks } from './types';
|
|
2
|
+
export declare class HookManager {
|
|
3
|
+
private readonly hooks;
|
|
4
|
+
constructor(hooks?: ToolHooks);
|
|
5
|
+
executePreToolUse(context: HookContext): Promise<PreToolUseResult>;
|
|
6
|
+
executePostToolUse(context: HookContext, result: unknown): Promise<void>;
|
|
7
|
+
executePostToolUseFailure(context: HookContext, error: Error): Promise<void>;
|
|
8
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { ToolExecutionContext } from '../../tool';
|
|
2
|
+
import type { ToolCall } from '../../types';
|
|
3
|
+
export interface HookContext {
|
|
4
|
+
sessionId: string;
|
|
5
|
+
toolCall: ToolCall;
|
|
6
|
+
toolContext: ToolExecutionContext;
|
|
7
|
+
}
|
|
8
|
+
export interface PreToolUseResult {
|
|
9
|
+
allow: boolean;
|
|
10
|
+
modifiedToolCall?: ToolCall;
|
|
11
|
+
}
|
|
12
|
+
export type PreToolUseHook = (ctx: HookContext) => Promise<PreToolUseResult>;
|
|
13
|
+
export type PostToolUseHook = (ctx: HookContext, result: unknown) => Promise<void>;
|
|
14
|
+
export type PostToolUseFailureHook = (ctx: HookContext, error: Error) => Promise<void>;
|
|
15
|
+
export interface ToolHooks {
|
|
16
|
+
preToolUse?: PreToolUseHook;
|
|
17
|
+
postToolUse?: PostToolUseHook;
|
|
18
|
+
postToolUseFailure?: PostToolUseFailureHook;
|
|
19
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export { fromLoopCheckpoint, toLoopCheckpoint } from '../middleware/checkpointMiddleware';
|
|
2
|
+
export type { ToCheckpointOptions } from '../middleware/checkpointMiddleware';
|
|
3
|
+
export { compressSessionManually, createContextCompressionMiddleware } from '../middleware/contextCompressionMiddleware';
|
|
4
|
+
export type { ManualCompressionOptions, ManualCompressionResult } from '../middleware/contextCompressionMiddleware';
|
|
5
|
+
export type { CompressionStats, ContextCompressionOptions } from '../middleware/contextCompressionMiddleware';
|
|
6
|
+
export { Agent } from './agent';
|
|
7
|
+
export { AgentAbortError, AgentMaxIterationsError, AgentPauseError, ensureNotAborted } from './errors';
|
|
8
|
+
export { HookManager } from './hooks';
|
|
9
|
+
export type { HookContext, PostToolUseFailureHook, PostToolUseHook, PreToolUseHook, PreToolUseResult, ToolHooks, } from './hooks';
|
|
10
|
+
export { compose } from './middleware';
|
|
11
|
+
export type { Middleware, NamedMiddleware, NextFunction } from './middleware';
|
|
12
|
+
export { countContentTokens, countMessagesTokens, countMessageTokens, countTokens, estimateTokens, } from './tokenCounter';
|
|
13
|
+
export { createInitialLoopState } from './types';
|
|
14
|
+
export type { AgentInput, AgentLoopState, AgentOptions, CreateSessionOptions, SendOptions, SessionHandleOptions, ToolCallWithResult, } from './types';
|