@oh-my-pi/pi-coding-agent 1.337.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/CHANGELOG.md +1228 -0
- package/README.md +1041 -0
- package/docs/compaction.md +403 -0
- package/docs/custom-tools.md +541 -0
- package/docs/extension-loading.md +1004 -0
- package/docs/hooks.md +867 -0
- package/docs/rpc.md +1040 -0
- package/docs/sdk.md +994 -0
- package/docs/session-tree-plan.md +441 -0
- package/docs/session.md +240 -0
- package/docs/skills.md +290 -0
- package/docs/theme.md +637 -0
- package/docs/tree.md +197 -0
- package/docs/tui.md +341 -0
- package/examples/README.md +21 -0
- package/examples/custom-tools/README.md +124 -0
- package/examples/custom-tools/hello/index.ts +20 -0
- package/examples/custom-tools/question/index.ts +84 -0
- package/examples/custom-tools/subagent/README.md +172 -0
- package/examples/custom-tools/subagent/agents/planner.md +37 -0
- package/examples/custom-tools/subagent/agents/reviewer.md +35 -0
- package/examples/custom-tools/subagent/agents/scout.md +50 -0
- package/examples/custom-tools/subagent/agents/worker.md +24 -0
- package/examples/custom-tools/subagent/agents.ts +156 -0
- package/examples/custom-tools/subagent/commands/implement-and-review.md +10 -0
- package/examples/custom-tools/subagent/commands/implement.md +10 -0
- package/examples/custom-tools/subagent/commands/scout-and-plan.md +9 -0
- package/examples/custom-tools/subagent/index.ts +1002 -0
- package/examples/custom-tools/todo/index.ts +212 -0
- package/examples/hooks/README.md +56 -0
- package/examples/hooks/auto-commit-on-exit.ts +49 -0
- package/examples/hooks/confirm-destructive.ts +59 -0
- package/examples/hooks/custom-compaction.ts +116 -0
- package/examples/hooks/dirty-repo-guard.ts +52 -0
- package/examples/hooks/file-trigger.ts +41 -0
- package/examples/hooks/git-checkpoint.ts +53 -0
- package/examples/hooks/handoff.ts +150 -0
- package/examples/hooks/permission-gate.ts +34 -0
- package/examples/hooks/protected-paths.ts +30 -0
- package/examples/hooks/qna.ts +119 -0
- package/examples/hooks/snake.ts +343 -0
- package/examples/hooks/status-line.ts +40 -0
- package/examples/sdk/01-minimal.ts +22 -0
- package/examples/sdk/02-custom-model.ts +49 -0
- package/examples/sdk/03-custom-prompt.ts +44 -0
- package/examples/sdk/04-skills.ts +44 -0
- package/examples/sdk/05-tools.ts +90 -0
- package/examples/sdk/06-hooks.ts +61 -0
- package/examples/sdk/07-context-files.ts +36 -0
- package/examples/sdk/08-slash-commands.ts +42 -0
- package/examples/sdk/09-api-keys-and-oauth.ts +55 -0
- package/examples/sdk/10-settings.ts +38 -0
- package/examples/sdk/11-sessions.ts +48 -0
- package/examples/sdk/12-full-control.ts +95 -0
- package/examples/sdk/README.md +154 -0
- package/package.json +81 -0
- package/src/cli/args.ts +246 -0
- package/src/cli/file-processor.ts +72 -0
- package/src/cli/list-models.ts +104 -0
- package/src/cli/plugin-cli.ts +650 -0
- package/src/cli/session-picker.ts +41 -0
- package/src/cli.ts +10 -0
- package/src/commands/init.md +20 -0
- package/src/config.ts +159 -0
- package/src/core/agent-session.ts +1900 -0
- package/src/core/auth-storage.ts +236 -0
- package/src/core/bash-executor.ts +196 -0
- package/src/core/compaction/branch-summarization.ts +343 -0
- package/src/core/compaction/compaction.ts +742 -0
- package/src/core/compaction/index.ts +7 -0
- package/src/core/compaction/utils.ts +154 -0
- package/src/core/custom-tools/index.ts +21 -0
- package/src/core/custom-tools/loader.ts +248 -0
- package/src/core/custom-tools/types.ts +169 -0
- package/src/core/custom-tools/wrapper.ts +28 -0
- package/src/core/exec.ts +129 -0
- package/src/core/export-html/index.ts +211 -0
- package/src/core/export-html/template.css +781 -0
- package/src/core/export-html/template.html +54 -0
- package/src/core/export-html/template.js +1185 -0
- package/src/core/export-html/vendor/highlight.min.js +1213 -0
- package/src/core/export-html/vendor/marked.min.js +6 -0
- package/src/core/hooks/index.ts +16 -0
- package/src/core/hooks/loader.ts +312 -0
- package/src/core/hooks/runner.ts +434 -0
- package/src/core/hooks/tool-wrapper.ts +99 -0
- package/src/core/hooks/types.ts +773 -0
- package/src/core/index.ts +52 -0
- package/src/core/mcp/client.ts +158 -0
- package/src/core/mcp/config.ts +154 -0
- package/src/core/mcp/index.ts +45 -0
- package/src/core/mcp/loader.ts +68 -0
- package/src/core/mcp/manager.ts +181 -0
- package/src/core/mcp/tool-bridge.ts +148 -0
- package/src/core/mcp/transports/http.ts +316 -0
- package/src/core/mcp/transports/index.ts +6 -0
- package/src/core/mcp/transports/stdio.ts +252 -0
- package/src/core/mcp/types.ts +220 -0
- package/src/core/messages.ts +189 -0
- package/src/core/model-registry.ts +317 -0
- package/src/core/model-resolver.ts +393 -0
- package/src/core/plugins/doctor.ts +59 -0
- package/src/core/plugins/index.ts +38 -0
- package/src/core/plugins/installer.ts +189 -0
- package/src/core/plugins/loader.ts +338 -0
- package/src/core/plugins/manager.ts +672 -0
- package/src/core/plugins/parser.ts +105 -0
- package/src/core/plugins/paths.ts +32 -0
- package/src/core/plugins/types.ts +190 -0
- package/src/core/sdk.ts +760 -0
- package/src/core/session-manager.ts +1128 -0
- package/src/core/settings-manager.ts +443 -0
- package/src/core/skills.ts +437 -0
- package/src/core/slash-commands.ts +248 -0
- package/src/core/system-prompt.ts +439 -0
- package/src/core/timings.ts +25 -0
- package/src/core/tools/ask.ts +211 -0
- package/src/core/tools/bash-interceptor.ts +120 -0
- package/src/core/tools/bash.ts +250 -0
- package/src/core/tools/context.ts +32 -0
- package/src/core/tools/edit-diff.ts +475 -0
- package/src/core/tools/edit.ts +208 -0
- package/src/core/tools/exa/company.ts +59 -0
- package/src/core/tools/exa/index.ts +64 -0
- package/src/core/tools/exa/linkedin.ts +59 -0
- package/src/core/tools/exa/logger.ts +56 -0
- package/src/core/tools/exa/mcp-client.ts +368 -0
- package/src/core/tools/exa/render.ts +196 -0
- package/src/core/tools/exa/researcher.ts +90 -0
- package/src/core/tools/exa/search.ts +337 -0
- package/src/core/tools/exa/types.ts +168 -0
- package/src/core/tools/exa/websets.ts +248 -0
- package/src/core/tools/find.ts +261 -0
- package/src/core/tools/grep.ts +555 -0
- package/src/core/tools/index.ts +202 -0
- package/src/core/tools/ls.ts +140 -0
- package/src/core/tools/lsp/client.ts +605 -0
- package/src/core/tools/lsp/config.ts +147 -0
- package/src/core/tools/lsp/edits.ts +101 -0
- package/src/core/tools/lsp/index.ts +804 -0
- package/src/core/tools/lsp/render.ts +447 -0
- package/src/core/tools/lsp/rust-analyzer.ts +145 -0
- package/src/core/tools/lsp/types.ts +463 -0
- package/src/core/tools/lsp/utils.ts +486 -0
- package/src/core/tools/notebook.ts +229 -0
- package/src/core/tools/path-utils.ts +61 -0
- package/src/core/tools/read.ts +240 -0
- package/src/core/tools/renderers.ts +540 -0
- package/src/core/tools/task/agents.ts +153 -0
- package/src/core/tools/task/artifacts.ts +114 -0
- package/src/core/tools/task/bundled-agents/browser.md +71 -0
- package/src/core/tools/task/bundled-agents/explore.md +82 -0
- package/src/core/tools/task/bundled-agents/plan.md +54 -0
- package/src/core/tools/task/bundled-agents/reviewer.md +59 -0
- package/src/core/tools/task/bundled-agents/task.md +53 -0
- package/src/core/tools/task/bundled-commands/architect-plan.md +10 -0
- package/src/core/tools/task/bundled-commands/implement-with-critic.md +11 -0
- package/src/core/tools/task/bundled-commands/implement.md +11 -0
- package/src/core/tools/task/commands.ts +213 -0
- package/src/core/tools/task/discovery.ts +208 -0
- package/src/core/tools/task/executor.ts +367 -0
- package/src/core/tools/task/index.ts +388 -0
- package/src/core/tools/task/model-resolver.ts +115 -0
- package/src/core/tools/task/parallel.ts +38 -0
- package/src/core/tools/task/render.ts +232 -0
- package/src/core/tools/task/types.ts +99 -0
- package/src/core/tools/truncate.ts +265 -0
- package/src/core/tools/web-fetch.ts +2370 -0
- package/src/core/tools/web-search/auth.ts +193 -0
- package/src/core/tools/web-search/index.ts +537 -0
- package/src/core/tools/web-search/providers/anthropic.ts +198 -0
- package/src/core/tools/web-search/providers/exa.ts +302 -0
- package/src/core/tools/web-search/providers/perplexity.ts +195 -0
- package/src/core/tools/web-search/render.ts +182 -0
- package/src/core/tools/web-search/types.ts +180 -0
- package/src/core/tools/write.ts +99 -0
- package/src/index.ts +176 -0
- package/src/main.ts +464 -0
- package/src/migrations.ts +135 -0
- package/src/modes/index.ts +43 -0
- package/src/modes/interactive/components/armin.ts +382 -0
- package/src/modes/interactive/components/assistant-message.ts +86 -0
- package/src/modes/interactive/components/bash-execution.ts +196 -0
- package/src/modes/interactive/components/bordered-loader.ts +41 -0
- package/src/modes/interactive/components/branch-summary-message.ts +42 -0
- package/src/modes/interactive/components/compaction-summary-message.ts +45 -0
- package/src/modes/interactive/components/custom-editor.ts +122 -0
- package/src/modes/interactive/components/diff.ts +147 -0
- package/src/modes/interactive/components/dynamic-border.ts +25 -0
- package/src/modes/interactive/components/footer.ts +381 -0
- package/src/modes/interactive/components/hook-editor.ts +117 -0
- package/src/modes/interactive/components/hook-input.ts +64 -0
- package/src/modes/interactive/components/hook-message.ts +96 -0
- package/src/modes/interactive/components/hook-selector.ts +91 -0
- package/src/modes/interactive/components/model-selector.ts +247 -0
- package/src/modes/interactive/components/oauth-selector.ts +120 -0
- package/src/modes/interactive/components/plugin-settings.ts +479 -0
- package/src/modes/interactive/components/queue-mode-selector.ts +56 -0
- package/src/modes/interactive/components/session-selector.ts +204 -0
- package/src/modes/interactive/components/settings-selector.ts +453 -0
- package/src/modes/interactive/components/show-images-selector.ts +45 -0
- package/src/modes/interactive/components/theme-selector.ts +62 -0
- package/src/modes/interactive/components/thinking-selector.ts +64 -0
- package/src/modes/interactive/components/tool-execution.ts +675 -0
- package/src/modes/interactive/components/tree-selector.ts +866 -0
- package/src/modes/interactive/components/user-message-selector.ts +159 -0
- package/src/modes/interactive/components/user-message.ts +18 -0
- package/src/modes/interactive/components/visual-truncate.ts +50 -0
- package/src/modes/interactive/components/welcome.ts +183 -0
- package/src/modes/interactive/interactive-mode.ts +2516 -0
- package/src/modes/interactive/theme/dark.json +101 -0
- package/src/modes/interactive/theme/light.json +98 -0
- package/src/modes/interactive/theme/theme-schema.json +308 -0
- package/src/modes/interactive/theme/theme.ts +998 -0
- package/src/modes/print-mode.ts +128 -0
- package/src/modes/rpc/rpc-client.ts +527 -0
- package/src/modes/rpc/rpc-mode.ts +483 -0
- package/src/modes/rpc/rpc-types.ts +203 -0
- package/src/utils/changelog.ts +99 -0
- package/src/utils/clipboard.ts +265 -0
- package/src/utils/fuzzy.ts +108 -0
- package/src/utils/mime.ts +30 -0
- package/src/utils/shell.ts +276 -0
- package/src/utils/tools-manager.ts +274 -0
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MCP (Model Context Protocol) type definitions.
|
|
3
|
+
*
|
|
4
|
+
* Based on MCP specification 2025-03-26:
|
|
5
|
+
* https://modelcontextprotocol.io/specification/2025-03-26/
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
// =============================================================================
|
|
9
|
+
// JSON-RPC 2.0 Types
|
|
10
|
+
// =============================================================================
|
|
11
|
+
|
|
12
|
+
export interface JsonRpcRequest {
|
|
13
|
+
jsonrpc: "2.0";
|
|
14
|
+
id: string | number;
|
|
15
|
+
method: string;
|
|
16
|
+
params?: Record<string, unknown>;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface JsonRpcNotification {
|
|
20
|
+
jsonrpc: "2.0";
|
|
21
|
+
method: string;
|
|
22
|
+
params?: Record<string, unknown>;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface JsonRpcResponse {
|
|
26
|
+
jsonrpc: "2.0";
|
|
27
|
+
id: string | number;
|
|
28
|
+
result?: unknown;
|
|
29
|
+
error?: JsonRpcError;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface JsonRpcError {
|
|
33
|
+
code: number;
|
|
34
|
+
message: string;
|
|
35
|
+
data?: unknown;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export type JsonRpcMessage = JsonRpcRequest | JsonRpcNotification | JsonRpcResponse;
|
|
39
|
+
|
|
40
|
+
// =============================================================================
|
|
41
|
+
// MCP Server Configuration (.mcp.json format)
|
|
42
|
+
// =============================================================================
|
|
43
|
+
|
|
44
|
+
/** Stdio server configuration */
|
|
45
|
+
export interface MCPStdioServerConfig {
|
|
46
|
+
type?: "stdio"; // Default if not specified
|
|
47
|
+
command: string;
|
|
48
|
+
args?: string[];
|
|
49
|
+
env?: Record<string, string>;
|
|
50
|
+
cwd?: string;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** HTTP server configuration (Streamable HTTP transport) */
|
|
54
|
+
export interface MCPHttpServerConfig {
|
|
55
|
+
type: "http";
|
|
56
|
+
url: string;
|
|
57
|
+
headers?: Record<string, string>;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** SSE server configuration (deprecated, use HTTP) */
|
|
61
|
+
export interface MCPSseServerConfig {
|
|
62
|
+
type: "sse";
|
|
63
|
+
url: string;
|
|
64
|
+
headers?: Record<string, string>;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export type MCPServerConfig = MCPStdioServerConfig | MCPHttpServerConfig | MCPSseServerConfig;
|
|
68
|
+
|
|
69
|
+
/** Root .mcp.json file structure */
|
|
70
|
+
export interface MCPConfigFile {
|
|
71
|
+
mcpServers?: Record<string, MCPServerConfig>;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// =============================================================================
|
|
75
|
+
// MCP Protocol Types
|
|
76
|
+
// =============================================================================
|
|
77
|
+
|
|
78
|
+
/** MCP implementation info */
|
|
79
|
+
export interface MCPImplementation {
|
|
80
|
+
name: string;
|
|
81
|
+
version: string;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** MCP client capabilities */
|
|
85
|
+
export interface MCPClientCapabilities {
|
|
86
|
+
roots?: { listChanged?: boolean };
|
|
87
|
+
sampling?: Record<string, never>;
|
|
88
|
+
experimental?: Record<string, unknown>;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** MCP server capabilities */
|
|
92
|
+
export interface MCPServerCapabilities {
|
|
93
|
+
tools?: { listChanged?: boolean };
|
|
94
|
+
resources?: { subscribe?: boolean; listChanged?: boolean };
|
|
95
|
+
prompts?: { listChanged?: boolean };
|
|
96
|
+
logging?: Record<string, never>;
|
|
97
|
+
experimental?: Record<string, unknown>;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** Initialize request params */
|
|
101
|
+
export interface MCPInitializeParams {
|
|
102
|
+
protocolVersion: string;
|
|
103
|
+
capabilities: MCPClientCapabilities;
|
|
104
|
+
clientInfo: MCPImplementation;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** Initialize response result */
|
|
108
|
+
export interface MCPInitializeResult {
|
|
109
|
+
protocolVersion: string;
|
|
110
|
+
capabilities: MCPServerCapabilities;
|
|
111
|
+
serverInfo: MCPImplementation;
|
|
112
|
+
instructions?: string;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** MCP tool definition */
|
|
116
|
+
export interface MCPToolDefinition {
|
|
117
|
+
name: string;
|
|
118
|
+
description?: string;
|
|
119
|
+
inputSchema: {
|
|
120
|
+
type: "object";
|
|
121
|
+
properties?: Record<string, unknown>;
|
|
122
|
+
required?: string[];
|
|
123
|
+
[key: string]: unknown;
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** tools/list response */
|
|
128
|
+
export interface MCPToolsListResult {
|
|
129
|
+
tools: MCPToolDefinition[];
|
|
130
|
+
nextCursor?: string;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** tools/call params */
|
|
134
|
+
export interface MCPToolCallParams {
|
|
135
|
+
name: string;
|
|
136
|
+
arguments?: Record<string, unknown>;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** Content types in tool results */
|
|
140
|
+
export interface MCPTextContent {
|
|
141
|
+
type: "text";
|
|
142
|
+
text: string;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export interface MCPImageContent {
|
|
146
|
+
type: "image";
|
|
147
|
+
data: string; // base64
|
|
148
|
+
mimeType: string;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export interface MCPResourceContent {
|
|
152
|
+
type: "resource";
|
|
153
|
+
resource: {
|
|
154
|
+
uri: string;
|
|
155
|
+
mimeType?: string;
|
|
156
|
+
text?: string;
|
|
157
|
+
blob?: string;
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
export type MCPContent = MCPTextContent | MCPImageContent | MCPResourceContent;
|
|
162
|
+
|
|
163
|
+
/** tools/call response */
|
|
164
|
+
export interface MCPToolCallResult {
|
|
165
|
+
content: MCPContent[];
|
|
166
|
+
isError?: boolean;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// =============================================================================
|
|
170
|
+
// Transport Types
|
|
171
|
+
// =============================================================================
|
|
172
|
+
|
|
173
|
+
/** Transport interface - abstracts stdio/http */
|
|
174
|
+
export interface MCPTransport {
|
|
175
|
+
/** Send a request and wait for response */
|
|
176
|
+
request<T = unknown>(method: string, params?: Record<string, unknown>): Promise<T>;
|
|
177
|
+
|
|
178
|
+
/** Send a notification (no response expected) */
|
|
179
|
+
notify(method: string, params?: Record<string, unknown>): Promise<void>;
|
|
180
|
+
|
|
181
|
+
/** Close the transport */
|
|
182
|
+
close(): Promise<void>;
|
|
183
|
+
|
|
184
|
+
/** Whether the transport is connected */
|
|
185
|
+
readonly connected: boolean;
|
|
186
|
+
|
|
187
|
+
/** Event handlers */
|
|
188
|
+
onClose?: () => void;
|
|
189
|
+
onError?: (error: Error) => void;
|
|
190
|
+
onNotification?: (method: string, params: unknown) => void;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** Transport factory function */
|
|
194
|
+
export type TransportFactory = (config: MCPServerConfig) => Promise<MCPTransport>;
|
|
195
|
+
|
|
196
|
+
// =============================================================================
|
|
197
|
+
// MCP Client Types
|
|
198
|
+
// =============================================================================
|
|
199
|
+
|
|
200
|
+
/** Connected MCP server state */
|
|
201
|
+
export interface MCPServerConnection {
|
|
202
|
+
/** Server name from config */
|
|
203
|
+
name: string;
|
|
204
|
+
/** Original config */
|
|
205
|
+
config: MCPServerConfig;
|
|
206
|
+
/** Transport instance */
|
|
207
|
+
transport: MCPTransport;
|
|
208
|
+
/** Server info from initialize */
|
|
209
|
+
serverInfo: MCPImplementation;
|
|
210
|
+
/** Server capabilities */
|
|
211
|
+
capabilities: MCPServerCapabilities;
|
|
212
|
+
/** Cached tools (populated on demand) */
|
|
213
|
+
tools?: MCPToolDefinition[];
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/** MCP tool with server context */
|
|
217
|
+
export interface MCPToolWithServer {
|
|
218
|
+
server: MCPServerConnection;
|
|
219
|
+
tool: MCPToolDefinition;
|
|
220
|
+
}
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Custom message types and transformers for the coding agent.
|
|
3
|
+
*
|
|
4
|
+
* Extends the base AgentMessage type with coding-agent specific message types,
|
|
5
|
+
* and provides a transformer to convert them to LLM-compatible messages.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { AgentMessage } from "@oh-my-pi/pi-agent-core";
|
|
9
|
+
import type { ImageContent, Message, TextContent } from "@oh-my-pi/pi-ai";
|
|
10
|
+
|
|
11
|
+
export const COMPACTION_SUMMARY_PREFIX = `The conversation history before this point was compacted into the following summary:
|
|
12
|
+
|
|
13
|
+
<summary>
|
|
14
|
+
`;
|
|
15
|
+
|
|
16
|
+
export const COMPACTION_SUMMARY_SUFFIX = `
|
|
17
|
+
</summary>`;
|
|
18
|
+
|
|
19
|
+
export const BRANCH_SUMMARY_PREFIX = `The following is a summary of a branch that this conversation came back from:
|
|
20
|
+
|
|
21
|
+
<summary>
|
|
22
|
+
`;
|
|
23
|
+
|
|
24
|
+
export const BRANCH_SUMMARY_SUFFIX = `</summary>`;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Message type for bash executions via the ! command.
|
|
28
|
+
*/
|
|
29
|
+
export interface BashExecutionMessage {
|
|
30
|
+
role: "bashExecution";
|
|
31
|
+
command: string;
|
|
32
|
+
output: string;
|
|
33
|
+
exitCode: number | undefined;
|
|
34
|
+
cancelled: boolean;
|
|
35
|
+
truncated: boolean;
|
|
36
|
+
fullOutputPath?: string;
|
|
37
|
+
timestamp: number;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Message type for hook-injected messages via sendMessage().
|
|
42
|
+
* These are custom messages that hooks can inject into the conversation.
|
|
43
|
+
*/
|
|
44
|
+
export interface HookMessage<T = unknown> {
|
|
45
|
+
role: "hookMessage";
|
|
46
|
+
customType: string;
|
|
47
|
+
content: string | (TextContent | ImageContent)[];
|
|
48
|
+
display: boolean;
|
|
49
|
+
details?: T;
|
|
50
|
+
timestamp: number;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface BranchSummaryMessage {
|
|
54
|
+
role: "branchSummary";
|
|
55
|
+
summary: string;
|
|
56
|
+
fromId: string;
|
|
57
|
+
timestamp: number;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface CompactionSummaryMessage {
|
|
61
|
+
role: "compactionSummary";
|
|
62
|
+
summary: string;
|
|
63
|
+
tokensBefore: number;
|
|
64
|
+
timestamp: number;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Extend CustomAgentMessages via declaration merging
|
|
68
|
+
declare module "@oh-my-pi/pi-agent-core" {
|
|
69
|
+
interface CustomAgentMessages {
|
|
70
|
+
bashExecution: BashExecutionMessage;
|
|
71
|
+
hookMessage: HookMessage;
|
|
72
|
+
branchSummary: BranchSummaryMessage;
|
|
73
|
+
compactionSummary: CompactionSummaryMessage;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Convert a BashExecutionMessage to user message text for LLM context.
|
|
79
|
+
*/
|
|
80
|
+
export function bashExecutionToText(msg: BashExecutionMessage): string {
|
|
81
|
+
let text = `Ran \`${msg.command}\`\n`;
|
|
82
|
+
if (msg.output) {
|
|
83
|
+
text += `\`\`\`\n${msg.output}\n\`\`\``;
|
|
84
|
+
} else {
|
|
85
|
+
text += "(no output)";
|
|
86
|
+
}
|
|
87
|
+
if (msg.cancelled) {
|
|
88
|
+
text += "\n\n(command cancelled)";
|
|
89
|
+
} else if (msg.exitCode !== null && msg.exitCode !== undefined && msg.exitCode !== 0) {
|
|
90
|
+
text += `\n\nCommand exited with code ${msg.exitCode}`;
|
|
91
|
+
}
|
|
92
|
+
if (msg.truncated && msg.fullOutputPath) {
|
|
93
|
+
text += `\n\n[Output truncated. Full output: ${msg.fullOutputPath}]`;
|
|
94
|
+
}
|
|
95
|
+
return text;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export function createBranchSummaryMessage(summary: string, fromId: string, timestamp: string): BranchSummaryMessage {
|
|
99
|
+
return {
|
|
100
|
+
role: "branchSummary",
|
|
101
|
+
summary,
|
|
102
|
+
fromId,
|
|
103
|
+
timestamp: new Date(timestamp).getTime(),
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export function createCompactionSummaryMessage(
|
|
108
|
+
summary: string,
|
|
109
|
+
tokensBefore: number,
|
|
110
|
+
timestamp: string,
|
|
111
|
+
): CompactionSummaryMessage {
|
|
112
|
+
return {
|
|
113
|
+
role: "compactionSummary",
|
|
114
|
+
summary: summary,
|
|
115
|
+
tokensBefore,
|
|
116
|
+
timestamp: new Date(timestamp).getTime(),
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** Convert CustomMessageEntry to AgentMessage format */
|
|
121
|
+
export function createHookMessage(
|
|
122
|
+
customType: string,
|
|
123
|
+
content: string | (TextContent | ImageContent)[],
|
|
124
|
+
display: boolean,
|
|
125
|
+
details: unknown | undefined,
|
|
126
|
+
timestamp: string,
|
|
127
|
+
): HookMessage {
|
|
128
|
+
return {
|
|
129
|
+
role: "hookMessage",
|
|
130
|
+
customType,
|
|
131
|
+
content,
|
|
132
|
+
display,
|
|
133
|
+
details,
|
|
134
|
+
timestamp: new Date(timestamp).getTime(),
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Transform AgentMessages (including custom types) to LLM-compatible Messages.
|
|
140
|
+
*
|
|
141
|
+
* This is used by:
|
|
142
|
+
* - Agent's transormToLlm option (for prompt calls and queued messages)
|
|
143
|
+
* - Compaction's generateSummary (for summarization)
|
|
144
|
+
* - Custom hooks and tools
|
|
145
|
+
*/
|
|
146
|
+
export function convertToLlm(messages: AgentMessage[]): Message[] {
|
|
147
|
+
return messages
|
|
148
|
+
.map((m): Message | undefined => {
|
|
149
|
+
switch (m.role) {
|
|
150
|
+
case "bashExecution":
|
|
151
|
+
return {
|
|
152
|
+
role: "user",
|
|
153
|
+
content: [{ type: "text", text: bashExecutionToText(m) }],
|
|
154
|
+
timestamp: m.timestamp,
|
|
155
|
+
};
|
|
156
|
+
case "hookMessage": {
|
|
157
|
+
const content = typeof m.content === "string" ? [{ type: "text" as const, text: m.content }] : m.content;
|
|
158
|
+
return {
|
|
159
|
+
role: "user",
|
|
160
|
+
content,
|
|
161
|
+
timestamp: m.timestamp,
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
case "branchSummary":
|
|
165
|
+
return {
|
|
166
|
+
role: "user",
|
|
167
|
+
content: [{ type: "text" as const, text: BRANCH_SUMMARY_PREFIX + m.summary + BRANCH_SUMMARY_SUFFIX }],
|
|
168
|
+
timestamp: m.timestamp,
|
|
169
|
+
};
|
|
170
|
+
case "compactionSummary":
|
|
171
|
+
return {
|
|
172
|
+
role: "user",
|
|
173
|
+
content: [
|
|
174
|
+
{ type: "text" as const, text: COMPACTION_SUMMARY_PREFIX + m.summary + COMPACTION_SUMMARY_SUFFIX },
|
|
175
|
+
],
|
|
176
|
+
timestamp: m.timestamp,
|
|
177
|
+
};
|
|
178
|
+
case "user":
|
|
179
|
+
case "assistant":
|
|
180
|
+
case "toolResult":
|
|
181
|
+
return m;
|
|
182
|
+
default:
|
|
183
|
+
// biome-ignore lint/correctness/noSwitchDeclarations: fine
|
|
184
|
+
const _exhaustiveCheck: never = m;
|
|
185
|
+
return undefined;
|
|
186
|
+
}
|
|
187
|
+
})
|
|
188
|
+
.filter((m) => m !== undefined);
|
|
189
|
+
}
|