@mariozechner/pi-coding-agent 0.17.0 → 0.18.1

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.
Files changed (89) hide show
  1. package/CHANGELOG.md +26 -0
  2. package/README.md +55 -1
  3. package/dist/cli/args.d.ts +1 -0
  4. package/dist/cli/args.d.ts.map +1 -1
  5. package/dist/cli/args.js +5 -0
  6. package/dist/cli/args.js.map +1 -1
  7. package/dist/config.d.ts +2 -0
  8. package/dist/config.d.ts.map +1 -1
  9. package/dist/config.js +4 -0
  10. package/dist/config.js.map +1 -1
  11. package/dist/core/agent-session.d.ts +25 -2
  12. package/dist/core/agent-session.d.ts.map +1 -1
  13. package/dist/core/agent-session.js +116 -3
  14. package/dist/core/agent-session.js.map +1 -1
  15. package/dist/core/hooks/index.d.ts +5 -0
  16. package/dist/core/hooks/index.d.ts.map +1 -0
  17. package/dist/core/hooks/index.js +4 -0
  18. package/dist/core/hooks/index.js.map +1 -0
  19. package/dist/core/hooks/loader.d.ts +56 -0
  20. package/dist/core/hooks/loader.d.ts.map +1 -0
  21. package/dist/core/hooks/loader.js +158 -0
  22. package/dist/core/hooks/loader.js.map +1 -0
  23. package/dist/core/hooks/runner.d.ts +69 -0
  24. package/dist/core/hooks/runner.d.ts.map +1 -0
  25. package/dist/core/hooks/runner.js +203 -0
  26. package/dist/core/hooks/runner.js.map +1 -0
  27. package/dist/core/hooks/tool-wrapper.d.ts +16 -0
  28. package/dist/core/hooks/tool-wrapper.d.ts.map +1 -0
  29. package/dist/core/hooks/tool-wrapper.js +71 -0
  30. package/dist/core/hooks/tool-wrapper.js.map +1 -0
  31. package/dist/core/hooks/types.d.ts +220 -0
  32. package/dist/core/hooks/types.d.ts.map +1 -0
  33. package/dist/core/hooks/types.js +8 -0
  34. package/dist/core/hooks/types.js.map +1 -0
  35. package/dist/core/index.d.ts +1 -0
  36. package/dist/core/index.d.ts.map +1 -1
  37. package/dist/core/index.js +1 -0
  38. package/dist/core/index.js.map +1 -1
  39. package/dist/core/model-resolver.d.ts.map +1 -1
  40. package/dist/core/model-resolver.js +1 -0
  41. package/dist/core/model-resolver.js.map +1 -1
  42. package/dist/core/settings-manager.d.ts +6 -0
  43. package/dist/core/settings-manager.d.ts.map +1 -1
  44. package/dist/core/settings-manager.js +14 -0
  45. package/dist/core/settings-manager.js.map +1 -1
  46. package/dist/core/system-prompt.d.ts.map +1 -1
  47. package/dist/core/system-prompt.js +5 -3
  48. package/dist/core/system-prompt.js.map +1 -1
  49. package/dist/index.d.ts +1 -0
  50. package/dist/index.d.ts.map +1 -1
  51. package/dist/index.js.map +1 -1
  52. package/dist/main.d.ts.map +1 -1
  53. package/dist/main.js +30 -15
  54. package/dist/main.js.map +1 -1
  55. package/dist/modes/interactive/components/hook-input.d.ts +12 -0
  56. package/dist/modes/interactive/components/hook-input.d.ts.map +1 -0
  57. package/dist/modes/interactive/components/hook-input.js +46 -0
  58. package/dist/modes/interactive/components/hook-input.js.map +1 -0
  59. package/dist/modes/interactive/components/hook-selector.d.ts +16 -0
  60. package/dist/modes/interactive/components/hook-selector.d.ts.map +1 -0
  61. package/dist/modes/interactive/components/hook-selector.js +76 -0
  62. package/dist/modes/interactive/components/hook-selector.js.map +1 -0
  63. package/dist/modes/interactive/interactive-mode.d.ts +37 -0
  64. package/dist/modes/interactive/interactive-mode.d.ts.map +1 -1
  65. package/dist/modes/interactive/interactive-mode.js +174 -5
  66. package/dist/modes/interactive/interactive-mode.js.map +1 -1
  67. package/dist/modes/interactive/theme/theme.d.ts +2 -2
  68. package/dist/modes/interactive/theme/theme.d.ts.map +1 -1
  69. package/dist/modes/interactive/theme/theme.js +8 -4
  70. package/dist/modes/interactive/theme/theme.js.map +1 -1
  71. package/dist/modes/print-mode.d.ts.map +1 -1
  72. package/dist/modes/print-mode.js +15 -0
  73. package/dist/modes/print-mode.js.map +1 -1
  74. package/dist/modes/rpc/rpc-mode.d.ts +2 -1
  75. package/dist/modes/rpc/rpc-mode.d.ts.map +1 -1
  76. package/dist/modes/rpc/rpc-mode.js +117 -3
  77. package/dist/modes/rpc/rpc-mode.js.map +1 -1
  78. package/dist/modes/rpc/rpc-types.d.ts +40 -0
  79. package/dist/modes/rpc/rpc-types.d.ts.map +1 -1
  80. package/dist/modes/rpc/rpc-types.js.map +1 -1
  81. package/docs/compaction.md +519 -0
  82. package/docs/gemini.md +255 -0
  83. package/docs/hooks.md +617 -0
  84. package/docs/rpc.md +870 -0
  85. package/docs/session.md +89 -0
  86. package/docs/theme.md +586 -0
  87. package/docs/truncation.md +235 -0
  88. package/docs/undercompaction.md +313 -0
  89. package/package.json +18 -6
package/docs/gemini.md ADDED
@@ -0,0 +1,255 @@
1
+ # Gemini OAuth Integration Guide
2
+
3
+ This document provides a comprehensive analysis of how OAuth authentication could be implemented for Google Gemini in the pi coding-agent, based on the existing Anthropic OAuth implementation and the Gemini CLI's approach.
4
+
5
+ ## Table of Contents
6
+
7
+ 1. [Current Anthropic OAuth Implementation](#current-anthropic-oauth-implementation)
8
+ 2. [Gemini CLI Authentication Analysis](#gemini-cli-authentication-analysis)
9
+ 3. [Gemini API Capabilities](#gemini-api-capabilities)
10
+ 4. [Gemini API Endpoints](#gemini-api-endpoints)
11
+ 5. [Implementation Plan](#implementation-plan)
12
+
13
+ ## Current Anthropic OAuth Implementation
14
+
15
+ The pi coding-agent implements OAuth for Anthropic with the following architecture:
16
+
17
+ ### Key Components
18
+
19
+ 1. **OAuth Flow** (`packages/coding-agent/src/core/oauth/anthropic.ts`):
20
+ - Uses PKCE (Proof Key for Code Exchange) flow for security
21
+ - Client ID: `9d1c250a-e61b-44d9-88ed-5944d1962f5e`
22
+ - Authorization URL: `https://claude.ai/oauth/authorize`
23
+ - Token URL: `https://console.anthropic.com/v1/oauth/token`
24
+ - Scopes: `org:create_api_key user:profile user:inference`
25
+
26
+ 2. **Token Storage** (`packages/coding-agent/src/core/oauth/storage.ts`):
27
+ - Stores credentials in `~/.pi/agent/oauth.json`
28
+ - File permissions set to 0600 (owner read/write only)
29
+ - Format: `{ provider: { type: "oauth", refresh: string, access: string, expires: number } }`
30
+
31
+ 3. **Token Management** (`packages/coding-agent/src/core/oauth/index.ts`):
32
+ - Auto-refresh tokens when expired (with 5-minute buffer)
33
+ - Supports multiple providers through `SupportedOAuthProvider` type
34
+ - Provider info includes id, name, and availability status
35
+
36
+ 4. **Model Integration** (`packages/coding-agent/src/core/model-config.ts`):
37
+ - Checks OAuth tokens first, then environment variables
38
+ - OAuth status cached to avoid repeated file reads
39
+ - Maps providers to OAuth providers via `providerToOAuthProvider`
40
+
41
+ ### Authentication Flow
42
+
43
+ 1. User initiates login with `pi auth login`
44
+ 2. Authorization URL is generated with PKCE challenge
45
+ 3. User opens URL in browser and authorizes
46
+ 4. User copies authorization code (format: `code#state`)
47
+ 5. Code is exchanged for access/refresh tokens
48
+ 6. Tokens are saved encrypted with expiry time
49
+
50
+ ## Gemini CLI Authentication Analysis
51
+
52
+ The Gemini CLI uses a more complex OAuth implementation with several key differences:
53
+
54
+ ### Authentication Methods
55
+
56
+ Gemini supports multiple authentication types:
57
+ - `LOGIN_WITH_GOOGLE` (OAuth personal account)
58
+ - `USE_GEMINI` (API key)
59
+ - `USE_VERTEX_AI` (Vertex AI)
60
+ - `COMPUTE_ADC` (Application Default Credentials)
61
+
62
+ ### OAuth Implementation Details
63
+
64
+ 1. **OAuth Configuration**:
65
+ - Client ID and Secret: See [google-gemini/gemini-cli oauth2.ts](https://github.com/google-gemini/gemini-cli/blob/main/packages/core/src/code_assist/oauth2.ts) (public for installed apps per Google's OAuth docs)
66
+ - Scopes:
67
+ - `https://www.googleapis.com/auth/cloud-platform`
68
+ - `https://www.googleapis.com/auth/userinfo.email`
69
+ - `https://www.googleapis.com/auth/userinfo.profile`
70
+
71
+ 2. **Authentication Flows**:
72
+ - **Web Flow**: Opens browser, runs local HTTP server for callback
73
+ - **User Code Flow**: For environments without browser (NO_BROWSER=true)
74
+ - Uses Google's `google-auth-library` for OAuth handling
75
+
76
+ 3. **Token Storage**:
77
+ - Supports encrypted storage via `OAuthCredentialStorage`
78
+ - Falls back to plain JSON storage
79
+ - Stores user info (email) separately
80
+
81
+ 4. **API Integration**:
82
+ - Uses `CodeAssistServer` for API calls
83
+ - Endpoint: `https://cloudcode-pa.googleapis.com`
84
+ - Includes user tier information (FREE, STANDARD, etc.)
85
+
86
+ ## Gemini API Capabilities
87
+
88
+ Based on the Gemini CLI analysis:
89
+
90
+ ### System Prompts
91
+ ✅ **Yes, Gemini supports system prompts**
92
+ - Implemented via `getCoreSystemPrompt()` in the codebase
93
+ - System instructions are part of the `GenerateContentParameters`
94
+
95
+ ### Tools/Function Calling
96
+ ✅ **Yes, Gemini supports tools and function calling**
97
+ - Uses the `Tool` type from `@google/genai`
98
+ - Extensive tool support including:
99
+ - File system operations (read, write, edit)
100
+ - Web search and fetch
101
+ - MCP (Model Context Protocol) tools
102
+ - Custom tool registration
103
+
104
+ ### Content Generation
105
+ - Supports streaming and non-streaming generation
106
+ - Token counting capabilities
107
+ - Embedding support
108
+ - Context compression for long conversations
109
+
110
+ ## Gemini API Endpoints
111
+
112
+ When using OAuth tokens, the Gemini CLI talks to:
113
+
114
+ ### Primary Endpoint
115
+ - **Base URL**: `https://cloudcode-pa.googleapis.com`
116
+ - **API Version**: `v1internal`
117
+
118
+ ### Key Methods
119
+ - `generateContent` - Non-streaming content generation
120
+ - `streamGenerateContent` - Streaming content generation
121
+ - `countTokens` - Token counting
122
+ - `embedContent` - Text embeddings
123
+ - `loadCodeAssist` - User setup and tier information
124
+ - `onboardUser` - User onboarding
125
+
126
+ ### Authentication
127
+ - OAuth tokens are passed via `AuthClient` from `google-auth-library`
128
+ - Tokens are automatically refreshed by the library
129
+ - Project ID and session ID included in requests
130
+
131
+ ## Implementation Plan
132
+
133
+ ### 1. Add Gemini OAuth Provider Support
134
+
135
+ **File**: `packages/coding-agent/src/core/oauth/gemini.ts`
136
+
137
+ ```typescript
138
+ import { OAuth2Client } from 'google-auth-library';
139
+ import { type OAuthCredentials, saveOAuthCredentials } from "./storage.js";
140
+
141
+ // OAuth credentials from google-gemini/gemini-cli:
142
+ // https://github.com/google-gemini/gemini-cli/blob/main/packages/core/src/code_assist/oauth2.ts
143
+ const SCOPES = [
144
+ "https://www.googleapis.com/auth/cloud-platform",
145
+ "https://www.googleapis.com/auth/userinfo.email",
146
+ "https://www.googleapis.com/auth/userinfo.profile"
147
+ ];
148
+
149
+ export async function loginGemini(
150
+ onAuthUrl: (url: string) => void,
151
+ onPromptCode: () => Promise<string>,
152
+ ): Promise<void> {
153
+ // Implementation similar to Anthropic but using google-auth-library
154
+ }
155
+
156
+ export async function refreshGeminiToken(refreshToken: string): Promise<OAuthCredentials> {
157
+ // Use google-auth-library for refresh
158
+ }
159
+ ```
160
+
161
+ ### 2. Update OAuth Index
162
+
163
+ **File**: `packages/coding-agent/src/core/oauth/index.ts`
164
+
165
+ ```typescript
166
+ export type SupportedOAuthProvider = "anthropic" | "github-copilot" | "gemini";
167
+
168
+ // Add Gemini to provider list
169
+ {
170
+ id: "gemini",
171
+ name: "Google Gemini (Code Assist)",
172
+ available: true,
173
+ }
174
+
175
+ // Add cases for Gemini in login/refresh functions
176
+ ```
177
+
178
+ ### 3. Create Gemini API Client
179
+
180
+ **File**: `packages/ai/src/providers/gemini-oauth.ts`
181
+
182
+ ```typescript
183
+ export class GeminiOAuthProvider implements Provider {
184
+ // Implement Provider interface
185
+ // Use CodeAssistServer approach from Gemini CLI
186
+ // Map to standard pi-ai API format
187
+ }
188
+ ```
189
+
190
+ ### 4. Update Model Configuration
191
+
192
+ **File**: `packages/coding-agent/src/core/model-config.ts`
193
+
194
+ ```typescript
195
+ // Add to providerToOAuthProvider mapping
196
+ gemini: "gemini",
197
+
198
+ // Add Gemini OAuth token check
199
+ if (model.provider === "gemini") {
200
+ const oauthToken = await getOAuthToken("gemini");
201
+ if (oauthToken) return oauthToken;
202
+ const oauthEnv = process.env.GEMINI_OAUTH_TOKEN;
203
+ if (oauthEnv) return oauthEnv;
204
+ }
205
+ ```
206
+
207
+ ### 5. Dependencies
208
+
209
+ Add to `package.json`:
210
+ ```json
211
+ {
212
+ "dependencies": {
213
+ "google-auth-library": "^9.0.0"
214
+ }
215
+ }
216
+ ```
217
+
218
+ ### 6. Environment Variables
219
+
220
+ Support these environment variables:
221
+ - `GEMINI_OAUTH_TOKEN` - Manual OAuth token
222
+ - `GOOGLE_CLOUD_PROJECT` - For project-specific features
223
+ - `NO_BROWSER` - Force user code flow
224
+
225
+ ### Key Differences from Anthropic Implementation
226
+
227
+ 1. **Authentication Library**: Use `google-auth-library` instead of manual OAuth
228
+ 2. **Multiple Auth Types**: Support OAuth, API key, and ADC
229
+ 3. **User Info**: Fetch and cache user email/profile
230
+ 4. **Project Context**: Include project ID in API calls
231
+ 5. **Tier Management**: Handle user tier (FREE/STANDARD) responses
232
+
233
+ ### Challenges and Considerations
234
+
235
+ 1. **API Access**: The Code Assist API (`cloudcode-pa.googleapis.com`) might require special access or be in preview
236
+ 2. **Model Naming**: Need to map Gemini model names to Code Assist equivalents
237
+ 3. **Rate Limits**: Handle tier-based rate limits
238
+ 4. **Error Handling**: Map Google-specific errors to pi error types
239
+ 5. **Token Scopes**: Ensure scopes are sufficient for all operations
240
+
241
+ ### Testing Plan
242
+
243
+ 1. Test OAuth flow (browser and NO_BROWSER modes)
244
+ 2. Test token refresh
245
+ 3. Test API calls with OAuth tokens
246
+ 4. Test fallback to API keys
247
+ 5. Test error scenarios (invalid tokens, network errors)
248
+ 6. Test model switching and tier limits
249
+
250
+ ### Migration Path
251
+
252
+ 1. Users with `GEMINI_API_KEY` continue working unchanged
253
+ 2. New `pi auth login gemini` command for OAuth
254
+ 3. OAuth takes precedence over API keys when available
255
+ 4. Clear messaging about benefits (higher limits, better features)