@posthog/agent
Version:
TypeScript agent framework wrapping Claude Agent SDK with Git-based task execution for PostHog
136 lines • 6.17 kB
TypeScript
/**
* The claude adapter has been based on the original claude-code-acp adapter,
* and could use some cleanup.
*
* https://github.com/zed-industries/claude-code-acp
*/
import { type Agent, AgentSideConnection, type AuthenticateRequest, type CancelNotification, type ClientCapabilities, type InitializeRequest, type InitializeResponse, type LoadSessionRequest, type LoadSessionResponse, type NewSessionRequest, type NewSessionResponse, type PromptRequest, type PromptResponse, type ReadTextFileRequest, type ReadTextFileResponse, type SessionNotification, type SetSessionModelRequest, type SetSessionModeRequest, type SetSessionModeResponse, type TerminalHandle, type TerminalOutputResponse, type WriteTextFileRequest, type WriteTextFileResponse } from "@agentclientprotocol/sdk";
import { type CanUseTool, type Options, type PermissionMode, type Query, type SDKPartialAssistantMessage, type SDKUserMessage } from "@anthropic-ai/claude-agent-sdk";
import type { ContentBlockParam } from "@anthropic-ai/sdk/resources";
import type { BetaContentBlock, BetaRawContentBlockDelta } from "@anthropic-ai/sdk/resources/beta.mjs";
import type { SessionStore } from "@/session-store.js";
import { Logger } from "@/utils/logger.js";
import { Pushable, type StreamPair } from "./utils.js";
type Session = {
query: Query;
input: Pushable<SDKUserMessage>;
cancelled: boolean;
permissionMode: PermissionMode;
notificationHistory: SessionNotification[];
sdkSessionId?: string;
};
type BackgroundTerminal = {
handle: TerminalHandle;
status: "started";
lastOutput: TerminalOutputResponse | null;
} | {
status: "aborted" | "exited" | "killed" | "timedOut";
pendingOutput: TerminalOutputResponse;
};
/**
* Extra metadata that can be given to Claude Code when creating a new session.
*/
export type NewSessionMeta = {
claudeCode?: {
/**
* Options forwarded to Claude Code when starting a new session.
* Those parameters will be ignored and managed by ACP:
* - cwd
* - includePartialMessages
* - allowDangerouslySkipPermissions
* - permissionMode
* - canUseTool
* - executable
* Those parameters will be used and updated to work with ACP:
* - hooks (merged with ACP's hooks)
* - mcpServers (merged with ACP's mcpServers)
*/
options?: Options;
};
};
/**
* Extra metadata that the agent provides for each tool_call / tool_update update.
*/
export type ToolUpdateMeta = {
claudeCode?: {
toolName: string;
toolResponse?: unknown;
};
};
type ToolUseCache = {
[key: string]: {
type: "tool_use" | "server_tool_use" | "mcp_tool_use";
id: string;
name: string;
input: any;
};
};
export declare class ClaudeAcpAgent implements Agent {
sessions: {
[key: string]: Session;
};
client: AgentSideConnection;
toolUseCache: ToolUseCache;
fileContentCache: {
[key: string]: string;
};
backgroundTerminals: {
[key: string]: BackgroundTerminal;
};
clientCapabilities?: ClientCapabilities;
logger: Logger;
sessionStore?: SessionStore;
constructor(client: AgentSideConnection, sessionStore?: SessionStore);
createSession(sessionId: string, q: Query, input: Pushable<SDKUserMessage>, permissionMode: PermissionMode): Session;
appendNotification(sessionId: string, notification: SessionNotification): void;
initialize(request: InitializeRequest): Promise<InitializeResponse>;
newSession(params: NewSessionRequest): Promise<NewSessionResponse>;
authenticate(_params: AuthenticateRequest): Promise<void>;
prompt(params: PromptRequest): Promise<PromptResponse>;
cancel(params: CancelNotification): Promise<void>;
setSessionModel(params: SetSessionModelRequest): Promise<void>;
setSessionMode(params: SetSessionModeRequest): Promise<SetSessionModeResponse>;
readTextFile(params: ReadTextFileRequest): Promise<ReadTextFileResponse>;
writeTextFile(params: WriteTextFileRequest): Promise<WriteTextFileResponse>;
/**
* Load session delegates to resumeSession since we have no need to replay history.
* Client is responsible for fetching and rendering history from S3.
*/
loadSession(params: LoadSessionRequest): Promise<LoadSessionResponse>;
canUseTool(sessionId: string): CanUseTool;
/**
* Handle custom extension methods.
* Per ACP spec, extension methods start with underscore.
*/
extMethod(method: string, params: Record<string, unknown>): Promise<Record<string, unknown>>;
/**
* Resume a session without replaying history.
* Client is responsible for fetching and rendering history from S3.
* This basically implemetns the ACP session/resume RFD:
* https://agentclientprotocol.com/rfds/session-resume
*/
resumeSession(params: LoadSessionRequest): Promise<LoadSessionResponse>;
}
export declare function promptToClaude(prompt: PromptRequest): SDKUserMessage;
/**
* Convert an SDKAssistantMessage (Claude) to a SessionNotification (ACP).
* Only handles text, image, and thinking chunks for now.
*/
export declare function toAcpNotifications(content: string | ContentBlockParam[] | BetaContentBlock[] | BetaRawContentBlockDelta[], role: "assistant" | "user", sessionId: string, toolUseCache: ToolUseCache, fileContentCache: {
[key: string]: string;
}, client: AgentSideConnection, logger: Logger): SessionNotification[];
export declare function streamEventToAcpNotifications(message: SDKPartialAssistantMessage, sessionId: string, toolUseCache: ToolUseCache, fileContentCache: {
[key: string]: string;
}, client: AgentSideConnection, logger: Logger): SessionNotification[];
export type AcpConnectionConfig = {
sessionStore?: SessionStore;
sessionId?: string;
taskId?: string;
};
export type InProcessAcpConnection = {
agentConnection: AgentSideConnection;
clientStreams: StreamPair;
};
export declare function createAcpConnection(config?: AcpConnectionConfig): InProcessAcpConnection;
export {};
//# sourceMappingURL=claude.d.ts.map