@mariozechner/pi-coding-agent 0.49.3 → 0.50.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 +99 -1
- package/README.md +310 -1230
- package/dist/cli/args.d.ts +5 -0
- package/dist/cli/args.d.ts.map +1 -1
- package/dist/cli/args.js +57 -23
- package/dist/cli/args.js.map +1 -1
- package/dist/cli/config-selector.d.ts +14 -0
- package/dist/cli/config-selector.d.ts.map +1 -0
- package/dist/cli/config-selector.js +31 -0
- package/dist/cli/config-selector.js.map +1 -0
- package/dist/cli/session-picker.d.ts.map +1 -1
- package/dist/cli/session-picker.js +1 -1
- package/dist/cli/session-picker.js.map +1 -1
- package/dist/core/agent-session.d.ts +53 -34
- package/dist/core/agent-session.d.ts.map +1 -1
- package/dist/core/agent-session.js +262 -67
- package/dist/core/agent-session.js.map +1 -1
- package/dist/core/auth-storage.d.ts +8 -18
- package/dist/core/auth-storage.d.ts.map +1 -1
- package/dist/core/auth-storage.js +39 -55
- package/dist/core/auth-storage.js.map +1 -1
- package/dist/core/bash-executor.d.ts.map +1 -1
- package/dist/core/bash-executor.js +2 -1
- package/dist/core/bash-executor.js.map +1 -1
- package/dist/core/diagnostics.d.ts +15 -0
- package/dist/core/diagnostics.d.ts.map +1 -0
- package/dist/core/diagnostics.js +2 -0
- package/dist/core/diagnostics.js.map +1 -0
- package/dist/core/export-html/template.css +9 -0
- package/dist/core/export-html/template.js +6 -4
- package/dist/core/extensions/index.d.ts +1 -1
- package/dist/core/extensions/index.d.ts.map +1 -1
- package/dist/core/extensions/index.js.map +1 -1
- package/dist/core/extensions/loader.d.ts +1 -1
- package/dist/core/extensions/loader.d.ts.map +1 -1
- package/dist/core/extensions/loader.js +10 -1
- package/dist/core/extensions/loader.js.map +1 -1
- package/dist/core/extensions/runner.d.ts +9 -3
- package/dist/core/extensions/runner.d.ts.map +1 -1
- package/dist/core/extensions/runner.js +39 -12
- package/dist/core/extensions/runner.js.map +1 -1
- package/dist/core/extensions/types.d.ts +112 -1
- package/dist/core/extensions/types.d.ts.map +1 -1
- package/dist/core/extensions/types.js.map +1 -1
- package/dist/core/footer-data-provider.d.ts +9 -2
- package/dist/core/footer-data-provider.d.ts.map +1 -1
- package/dist/core/footer-data-provider.js +13 -0
- package/dist/core/footer-data-provider.js.map +1 -1
- package/dist/core/model-registry.d.ts +42 -2
- package/dist/core/model-registry.d.ts.map +1 -1
- package/dist/core/model-registry.js +154 -44
- package/dist/core/model-registry.js.map +1 -1
- package/dist/core/model-resolver.d.ts.map +1 -1
- package/dist/core/model-resolver.js +3 -2
- package/dist/core/model-resolver.js.map +1 -1
- package/dist/core/package-manager.d.ts +129 -0
- package/dist/core/package-manager.d.ts.map +1 -0
- package/dist/core/package-manager.js +1148 -0
- package/dist/core/package-manager.js.map +1 -0
- package/dist/core/prompt-templates.d.ts +6 -0
- package/dist/core/prompt-templates.d.ts.map +1 -1
- package/dist/core/prompt-templates.js +114 -54
- package/dist/core/prompt-templates.js.map +1 -1
- package/dist/core/resource-loader.d.ts +160 -0
- package/dist/core/resource-loader.d.ts.map +1 -0
- package/dist/core/resource-loader.js +604 -0
- package/dist/core/resource-loader.js.map +1 -0
- package/dist/core/sdk.d.ts +14 -105
- package/dist/core/sdk.d.ts.map +1 -1
- package/dist/core/sdk.js +52 -304
- package/dist/core/sdk.js.map +1 -1
- package/dist/core/session-manager.d.ts.map +1 -1
- package/dist/core/session-manager.js +45 -1
- package/dist/core/session-manager.js.map +1 -1
- package/dist/core/settings-manager.d.ts +34 -16
- package/dist/core/settings-manager.d.ts.map +1 -1
- package/dist/core/settings-manager.js +104 -25
- package/dist/core/settings-manager.js.map +1 -1
- package/dist/core/skills.d.ts +18 -10
- package/dist/core/skills.d.ts.map +1 -1
- package/dist/core/skills.js +126 -93
- package/dist/core/skills.js.map +1 -1
- package/dist/core/system-prompt.d.ts +3 -27
- package/dist/core/system-prompt.d.ts.map +1 -1
- package/dist/core/system-prompt.js +16 -103
- package/dist/core/system-prompt.js.map +1 -1
- package/dist/core/tools/bash.d.ts.map +1 -1
- package/dist/core/tools/bash.js +2 -1
- package/dist/core/tools/bash.js.map +1 -1
- package/dist/core/tools/read.d.ts.map +1 -1
- package/dist/core/tools/read.js +4 -4
- package/dist/core/tools/read.js.map +1 -1
- package/dist/index.d.ts +12 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -6
- package/dist/index.js.map +1 -1
- package/dist/main.d.ts.map +1 -1
- package/dist/main.js +209 -97
- package/dist/main.js.map +1 -1
- package/dist/modes/interactive/components/bordered-loader.d.ts +5 -1
- package/dist/modes/interactive/components/bordered-loader.d.ts.map +1 -1
- package/dist/modes/interactive/components/bordered-loader.js +29 -9
- package/dist/modes/interactive/components/bordered-loader.js.map +1 -1
- package/dist/modes/interactive/components/config-selector.d.ts +71 -0
- package/dist/modes/interactive/components/config-selector.d.ts.map +1 -0
- package/dist/modes/interactive/components/config-selector.js +468 -0
- package/dist/modes/interactive/components/config-selector.js.map +1 -0
- package/dist/modes/interactive/components/footer.d.ts.map +1 -1
- package/dist/modes/interactive/components/footer.js +4 -0
- package/dist/modes/interactive/components/footer.js.map +1 -1
- package/dist/modes/interactive/components/index.d.ts +1 -0
- package/dist/modes/interactive/components/index.d.ts.map +1 -1
- package/dist/modes/interactive/components/index.js +1 -0
- package/dist/modes/interactive/components/index.js.map +1 -1
- package/dist/modes/interactive/components/oauth-selector.d.ts.map +1 -1
- package/dist/modes/interactive/components/oauth-selector.js +3 -4
- package/dist/modes/interactive/components/oauth-selector.js.map +1 -1
- package/dist/modes/interactive/components/session-selector.d.ts +18 -1
- package/dist/modes/interactive/components/session-selector.d.ts.map +1 -1
- package/dist/modes/interactive/components/session-selector.js +195 -87
- package/dist/modes/interactive/components/session-selector.js.map +1 -1
- package/dist/modes/interactive/components/skill-invocation-message.d.ts +17 -0
- package/dist/modes/interactive/components/skill-invocation-message.d.ts.map +1 -0
- package/dist/modes/interactive/components/skill-invocation-message.js +47 -0
- package/dist/modes/interactive/components/skill-invocation-message.js.map +1 -0
- package/dist/modes/interactive/components/tool-execution.d.ts.map +1 -1
- package/dist/modes/interactive/components/tool-execution.js +5 -5
- package/dist/modes/interactive/components/tool-execution.js.map +1 -1
- package/dist/modes/interactive/interactive-mode.d.ts +42 -2
- package/dist/modes/interactive/interactive-mode.d.ts.map +1 -1
- package/dist/modes/interactive/interactive-mode.js +535 -200
- package/dist/modes/interactive/interactive-mode.js.map +1 -1
- package/dist/modes/interactive/theme/dark.json +1 -1
- package/dist/modes/interactive/theme/light.json +1 -1
- package/dist/modes/interactive/theme/theme-schema.json +8 -1
- package/dist/modes/interactive/theme/theme.d.ts +8 -1
- package/dist/modes/interactive/theme/theme.d.ts.map +1 -1
- package/dist/modes/interactive/theme/theme.js +72 -25
- package/dist/modes/interactive/theme/theme.js.map +1 -1
- package/dist/modes/print-mode.d.ts.map +1 -1
- package/dist/modes/print-mode.js +25 -89
- package/dist/modes/print-mode.js.map +1 -1
- package/dist/modes/rpc/rpc-mode.d.ts.map +1 -1
- package/dist/modes/rpc/rpc-mode.js +32 -92
- package/dist/modes/rpc/rpc-mode.js.map +1 -1
- package/dist/utils/git.d.ts +2 -0
- package/dist/utils/git.d.ts.map +1 -0
- package/dist/utils/git.js +6 -0
- package/dist/utils/git.js.map +1 -0
- package/dist/utils/shell.d.ts +1 -0
- package/dist/utils/shell.d.ts.map +1 -1
- package/dist/utils/shell.js +14 -1
- package/dist/utils/shell.js.map +1 -1
- package/dist/utils/sleep.d.ts +5 -0
- package/dist/utils/sleep.d.ts.map +1 -0
- package/dist/utils/sleep.js +17 -0
- package/dist/utils/sleep.js.map +1 -0
- package/docs/compaction.md +23 -21
- package/docs/custom-provider.md +538 -0
- package/docs/development.md +69 -0
- package/docs/extensions.md +180 -118
- package/docs/images/doom-extension.png +0 -0
- package/docs/images/interactive-mode.png +0 -0
- package/docs/images/tree-view.png +0 -0
- package/docs/json.md +79 -0
- package/docs/keybindings.md +162 -0
- package/docs/models.md +193 -0
- package/docs/packages.md +163 -0
- package/docs/prompt-templates.md +67 -0
- package/docs/providers.md +147 -0
- package/docs/sdk.md +111 -178
- package/docs/session.md +167 -16
- package/docs/settings.md +216 -0
- package/docs/shell-aliases.md +13 -0
- package/docs/skills.md +111 -202
- package/docs/terminal-setup.md +65 -0
- package/docs/themes.md +295 -0
- package/docs/tui.md +36 -5
- package/docs/windows.md +17 -0
- package/examples/README.md +1 -0
- package/examples/extensions/README.md +22 -2
- package/examples/extensions/bookmark.ts +50 -0
- package/examples/extensions/custom-provider-anthropic/index.ts +604 -0
- package/examples/extensions/custom-provider-anthropic/package-lock.json +24 -0
- package/examples/extensions/custom-provider-anthropic/package.json +19 -0
- package/examples/extensions/custom-provider-gitlab-duo/index.ts +349 -0
- package/examples/extensions/custom-provider-gitlab-duo/package.json +16 -0
- package/examples/extensions/custom-provider-gitlab-duo/test.ts +82 -0
- package/examples/extensions/doom-overlay/doom/build.sh +1 -1
- package/examples/extensions/event-bus.ts +43 -0
- package/examples/extensions/message-renderer.ts +59 -0
- package/examples/extensions/session-name.ts +27 -0
- package/examples/extensions/with-deps/package-lock.json +2 -2
- package/examples/extensions/with-deps/package.json +1 -1
- package/examples/sdk/02-custom-model.ts +3 -3
- package/examples/sdk/03-custom-prompt.ts +20 -9
- package/examples/sdk/04-skills.ts +26 -27
- package/examples/sdk/06-extensions.ts +15 -6
- package/examples/sdk/07-context-files.ts +22 -18
- package/examples/sdk/08-prompt-templates.ts +19 -14
- package/examples/sdk/09-api-keys-and-oauth.ts +5 -12
- package/examples/sdk/10-settings.ts +3 -3
- package/examples/sdk/12-full-control.ts +16 -7
- package/examples/sdk/README.md +24 -30
- package/package.json +4 -4
- package/docs/theme.md +0 -617
- package/examples/extensions/chalk-logger.ts +0 -26
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* GitLab Duo Provider Extension
|
|
3
|
+
*
|
|
4
|
+
* Provides access to GitLab Duo AI models (Claude and GPT) through GitLab's AI Gateway.
|
|
5
|
+
* Delegates to pi-ai's built-in Anthropic and OpenAI streaming implementations.
|
|
6
|
+
*
|
|
7
|
+
* Usage:
|
|
8
|
+
* pi -e ./packages/coding-agent/examples/extensions/custom-provider-gitlab-duo
|
|
9
|
+
* # Then /login gitlab-duo, or set GITLAB_TOKEN=glpat-...
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import {
|
|
13
|
+
type Api,
|
|
14
|
+
type AssistantMessageEventStream,
|
|
15
|
+
type Context,
|
|
16
|
+
createAssistantMessageEventStream,
|
|
17
|
+
type Model,
|
|
18
|
+
type OAuthCredentials,
|
|
19
|
+
type OAuthLoginCallbacks,
|
|
20
|
+
type SimpleStreamOptions,
|
|
21
|
+
streamSimpleAnthropic,
|
|
22
|
+
streamSimpleOpenAIResponses,
|
|
23
|
+
} from "@mariozechner/pi-ai";
|
|
24
|
+
import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
|
|
25
|
+
|
|
26
|
+
// =============================================================================
|
|
27
|
+
// Constants
|
|
28
|
+
// =============================================================================
|
|
29
|
+
|
|
30
|
+
const GITLAB_COM_URL = "https://gitlab.com";
|
|
31
|
+
const AI_GATEWAY_URL = "https://cloud.gitlab.com";
|
|
32
|
+
const ANTHROPIC_PROXY_URL = `${AI_GATEWAY_URL}/ai/v1/proxy/anthropic/`;
|
|
33
|
+
const OPENAI_PROXY_URL = `${AI_GATEWAY_URL}/ai/v1/proxy/openai/v1`;
|
|
34
|
+
|
|
35
|
+
const BUNDLED_CLIENT_ID = "da4edff2e6ebd2bc3208611e2768bc1c1dd7be791dc5ff26ca34ca9ee44f7d4b";
|
|
36
|
+
const OAUTH_SCOPES = ["api"];
|
|
37
|
+
const REDIRECT_URI = "http://127.0.0.1:8080/callback";
|
|
38
|
+
const DIRECT_ACCESS_TTL = 25 * 60 * 1000;
|
|
39
|
+
|
|
40
|
+
// =============================================================================
|
|
41
|
+
// Models - exported for use by tests
|
|
42
|
+
// =============================================================================
|
|
43
|
+
|
|
44
|
+
type Backend = "anthropic" | "openai";
|
|
45
|
+
|
|
46
|
+
interface GitLabModel {
|
|
47
|
+
id: string;
|
|
48
|
+
name: string;
|
|
49
|
+
backend: Backend;
|
|
50
|
+
baseUrl: string;
|
|
51
|
+
reasoning: boolean;
|
|
52
|
+
input: ("text" | "image")[];
|
|
53
|
+
cost: { input: number; output: number; cacheRead: number; cacheWrite: number };
|
|
54
|
+
contextWindow: number;
|
|
55
|
+
maxTokens: number;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export const MODELS: GitLabModel[] = [
|
|
59
|
+
// Anthropic
|
|
60
|
+
{
|
|
61
|
+
id: "claude-opus-4-5-20251101",
|
|
62
|
+
name: "Claude Opus 4.5",
|
|
63
|
+
backend: "anthropic",
|
|
64
|
+
baseUrl: ANTHROPIC_PROXY_URL,
|
|
65
|
+
reasoning: true,
|
|
66
|
+
input: ["text", "image"],
|
|
67
|
+
cost: { input: 15, output: 75, cacheRead: 1.5, cacheWrite: 18.75 },
|
|
68
|
+
contextWindow: 200000,
|
|
69
|
+
maxTokens: 32000,
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
id: "claude-sonnet-4-5-20250929",
|
|
73
|
+
name: "Claude Sonnet 4.5",
|
|
74
|
+
backend: "anthropic",
|
|
75
|
+
baseUrl: ANTHROPIC_PROXY_URL,
|
|
76
|
+
reasoning: true,
|
|
77
|
+
input: ["text", "image"],
|
|
78
|
+
cost: { input: 3, output: 15, cacheRead: 0.3, cacheWrite: 3.75 },
|
|
79
|
+
contextWindow: 200000,
|
|
80
|
+
maxTokens: 16384,
|
|
81
|
+
},
|
|
82
|
+
{
|
|
83
|
+
id: "claude-haiku-4-5-20251001",
|
|
84
|
+
name: "Claude Haiku 4.5",
|
|
85
|
+
backend: "anthropic",
|
|
86
|
+
baseUrl: ANTHROPIC_PROXY_URL,
|
|
87
|
+
reasoning: true,
|
|
88
|
+
input: ["text", "image"],
|
|
89
|
+
cost: { input: 1, output: 5, cacheRead: 0.1, cacheWrite: 1.25 },
|
|
90
|
+
contextWindow: 200000,
|
|
91
|
+
maxTokens: 8192,
|
|
92
|
+
},
|
|
93
|
+
// OpenAI (all use Responses API)
|
|
94
|
+
{
|
|
95
|
+
id: "gpt-5.1-2025-11-13",
|
|
96
|
+
name: "GPT-5.1",
|
|
97
|
+
backend: "openai",
|
|
98
|
+
baseUrl: OPENAI_PROXY_URL,
|
|
99
|
+
reasoning: true,
|
|
100
|
+
input: ["text", "image"],
|
|
101
|
+
cost: { input: 2.5, output: 10, cacheRead: 0, cacheWrite: 0 },
|
|
102
|
+
contextWindow: 128000,
|
|
103
|
+
maxTokens: 16384,
|
|
104
|
+
},
|
|
105
|
+
{
|
|
106
|
+
id: "gpt-5-mini-2025-08-07",
|
|
107
|
+
name: "GPT-5 Mini",
|
|
108
|
+
backend: "openai",
|
|
109
|
+
baseUrl: OPENAI_PROXY_URL,
|
|
110
|
+
reasoning: true,
|
|
111
|
+
input: ["text", "image"],
|
|
112
|
+
cost: { input: 0.15, output: 0.6, cacheRead: 0, cacheWrite: 0 },
|
|
113
|
+
contextWindow: 128000,
|
|
114
|
+
maxTokens: 16384,
|
|
115
|
+
},
|
|
116
|
+
{
|
|
117
|
+
id: "gpt-5-codex",
|
|
118
|
+
name: "GPT-5 Codex",
|
|
119
|
+
backend: "openai",
|
|
120
|
+
baseUrl: OPENAI_PROXY_URL,
|
|
121
|
+
reasoning: true,
|
|
122
|
+
input: ["text", "image"],
|
|
123
|
+
cost: { input: 2.5, output: 10, cacheRead: 0, cacheWrite: 0 },
|
|
124
|
+
contextWindow: 128000,
|
|
125
|
+
maxTokens: 16384,
|
|
126
|
+
},
|
|
127
|
+
];
|
|
128
|
+
|
|
129
|
+
const MODEL_MAP = new Map(MODELS.map((m) => [m.id, m]));
|
|
130
|
+
|
|
131
|
+
// =============================================================================
|
|
132
|
+
// Direct Access Token Cache
|
|
133
|
+
// =============================================================================
|
|
134
|
+
|
|
135
|
+
interface DirectAccessToken {
|
|
136
|
+
token: string;
|
|
137
|
+
headers: Record<string, string>;
|
|
138
|
+
expiresAt: number;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
let cachedDirectAccess: DirectAccessToken | null = null;
|
|
142
|
+
|
|
143
|
+
async function getDirectAccessToken(gitlabAccessToken: string): Promise<DirectAccessToken> {
|
|
144
|
+
const now = Date.now();
|
|
145
|
+
if (cachedDirectAccess && cachedDirectAccess.expiresAt > now) {
|
|
146
|
+
return cachedDirectAccess;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const response = await fetch(`${GITLAB_COM_URL}/api/v4/ai/third_party_agents/direct_access`, {
|
|
150
|
+
method: "POST",
|
|
151
|
+
headers: { Authorization: `Bearer ${gitlabAccessToken}`, "Content-Type": "application/json" },
|
|
152
|
+
body: JSON.stringify({ feature_flags: { DuoAgentPlatformNext: true } }),
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
if (!response.ok) {
|
|
156
|
+
const errorText = await response.text();
|
|
157
|
+
if (response.status === 403) {
|
|
158
|
+
throw new Error(
|
|
159
|
+
`GitLab Duo access denied. Ensure GitLab Duo is enabled for your account. Error: ${errorText}`,
|
|
160
|
+
);
|
|
161
|
+
}
|
|
162
|
+
throw new Error(`Failed to get direct access token: ${response.status} ${errorText}`);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const data = (await response.json()) as { token: string; headers: Record<string, string> };
|
|
166
|
+
cachedDirectAccess = { token: data.token, headers: data.headers, expiresAt: now + DIRECT_ACCESS_TTL };
|
|
167
|
+
return cachedDirectAccess;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function invalidateDirectAccessToken() {
|
|
171
|
+
cachedDirectAccess = null;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// =============================================================================
|
|
175
|
+
// OAuth
|
|
176
|
+
// =============================================================================
|
|
177
|
+
|
|
178
|
+
async function generatePKCE(): Promise<{ verifier: string; challenge: string }> {
|
|
179
|
+
const array = new Uint8Array(32);
|
|
180
|
+
crypto.getRandomValues(array);
|
|
181
|
+
const verifier = btoa(String.fromCharCode(...array))
|
|
182
|
+
.replace(/\+/g, "-")
|
|
183
|
+
.replace(/\//g, "_")
|
|
184
|
+
.replace(/=+$/, "");
|
|
185
|
+
const hash = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(verifier));
|
|
186
|
+
const challenge = btoa(String.fromCharCode(...new Uint8Array(hash)))
|
|
187
|
+
.replace(/\+/g, "-")
|
|
188
|
+
.replace(/\//g, "_")
|
|
189
|
+
.replace(/=+$/, "");
|
|
190
|
+
return { verifier, challenge };
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
async function loginGitLab(callbacks: OAuthLoginCallbacks): Promise<OAuthCredentials> {
|
|
194
|
+
const { verifier, challenge } = await generatePKCE();
|
|
195
|
+
const authParams = new URLSearchParams({
|
|
196
|
+
client_id: BUNDLED_CLIENT_ID,
|
|
197
|
+
redirect_uri: REDIRECT_URI,
|
|
198
|
+
response_type: "code",
|
|
199
|
+
scope: OAUTH_SCOPES.join(" "),
|
|
200
|
+
code_challenge: challenge,
|
|
201
|
+
code_challenge_method: "S256",
|
|
202
|
+
state: crypto.randomUUID(),
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
callbacks.onAuth({ url: `${GITLAB_COM_URL}/oauth/authorize?${authParams.toString()}` });
|
|
206
|
+
const callbackUrl = await callbacks.onPrompt({ message: "Paste the callback URL:" });
|
|
207
|
+
const code = new URL(callbackUrl).searchParams.get("code");
|
|
208
|
+
if (!code) throw new Error("No authorization code found in callback URL");
|
|
209
|
+
|
|
210
|
+
const tokenResponse = await fetch(`${GITLAB_COM_URL}/oauth/token`, {
|
|
211
|
+
method: "POST",
|
|
212
|
+
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
|
213
|
+
body: new URLSearchParams({
|
|
214
|
+
client_id: BUNDLED_CLIENT_ID,
|
|
215
|
+
grant_type: "authorization_code",
|
|
216
|
+
code,
|
|
217
|
+
code_verifier: verifier,
|
|
218
|
+
redirect_uri: REDIRECT_URI,
|
|
219
|
+
}).toString(),
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
if (!tokenResponse.ok) throw new Error(`Token exchange failed: ${await tokenResponse.text()}`);
|
|
223
|
+
const data = (await tokenResponse.json()) as {
|
|
224
|
+
access_token: string;
|
|
225
|
+
refresh_token: string;
|
|
226
|
+
expires_in: number;
|
|
227
|
+
created_at: number;
|
|
228
|
+
};
|
|
229
|
+
invalidateDirectAccessToken();
|
|
230
|
+
return {
|
|
231
|
+
refresh: data.refresh_token,
|
|
232
|
+
access: data.access_token,
|
|
233
|
+
expires: (data.created_at + data.expires_in) * 1000 - 5 * 60 * 1000,
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
async function refreshGitLabToken(credentials: OAuthCredentials): Promise<OAuthCredentials> {
|
|
238
|
+
const response = await fetch(`${GITLAB_COM_URL}/oauth/token`, {
|
|
239
|
+
method: "POST",
|
|
240
|
+
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
|
241
|
+
body: new URLSearchParams({
|
|
242
|
+
client_id: BUNDLED_CLIENT_ID,
|
|
243
|
+
grant_type: "refresh_token",
|
|
244
|
+
refresh_token: credentials.refresh,
|
|
245
|
+
}).toString(),
|
|
246
|
+
});
|
|
247
|
+
if (!response.ok) throw new Error(`Token refresh failed: ${await response.text()}`);
|
|
248
|
+
const data = (await response.json()) as {
|
|
249
|
+
access_token: string;
|
|
250
|
+
refresh_token: string;
|
|
251
|
+
expires_in: number;
|
|
252
|
+
created_at: number;
|
|
253
|
+
};
|
|
254
|
+
invalidateDirectAccessToken();
|
|
255
|
+
return {
|
|
256
|
+
refresh: data.refresh_token,
|
|
257
|
+
access: data.access_token,
|
|
258
|
+
expires: (data.created_at + data.expires_in) * 1000 - 5 * 60 * 1000,
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
// =============================================================================
|
|
263
|
+
// Stream Function
|
|
264
|
+
// =============================================================================
|
|
265
|
+
|
|
266
|
+
export function streamGitLabDuo(
|
|
267
|
+
model: Model<Api>,
|
|
268
|
+
context: Context,
|
|
269
|
+
options?: SimpleStreamOptions,
|
|
270
|
+
): AssistantMessageEventStream {
|
|
271
|
+
const stream = createAssistantMessageEventStream();
|
|
272
|
+
|
|
273
|
+
(async () => {
|
|
274
|
+
try {
|
|
275
|
+
const gitlabAccessToken = options?.apiKey;
|
|
276
|
+
if (!gitlabAccessToken) throw new Error("No GitLab access token. Run /login gitlab-duo or set GITLAB_TOKEN");
|
|
277
|
+
|
|
278
|
+
const cfg = MODEL_MAP.get(model.id);
|
|
279
|
+
if (!cfg) throw new Error(`Unknown model: ${model.id}`);
|
|
280
|
+
|
|
281
|
+
const directAccess = await getDirectAccessToken(gitlabAccessToken);
|
|
282
|
+
const modelWithBaseUrl = { ...model, baseUrl: cfg.baseUrl };
|
|
283
|
+
const headers = { ...directAccess.headers, Authorization: `Bearer ${directAccess.token}` };
|
|
284
|
+
const streamOptions = { ...options, apiKey: "gitlab-duo", headers };
|
|
285
|
+
|
|
286
|
+
const innerStream =
|
|
287
|
+
cfg.backend === "anthropic"
|
|
288
|
+
? streamSimpleAnthropic(modelWithBaseUrl as Model<"anthropic-messages">, context, streamOptions)
|
|
289
|
+
: streamSimpleOpenAIResponses(modelWithBaseUrl as Model<"openai-responses">, context, streamOptions);
|
|
290
|
+
|
|
291
|
+
for await (const event of innerStream) stream.push(event);
|
|
292
|
+
stream.end();
|
|
293
|
+
} catch (error) {
|
|
294
|
+
stream.push({
|
|
295
|
+
type: "error",
|
|
296
|
+
reason: "error",
|
|
297
|
+
error: {
|
|
298
|
+
role: "assistant",
|
|
299
|
+
content: [],
|
|
300
|
+
api: model.api,
|
|
301
|
+
provider: model.provider,
|
|
302
|
+
model: model.id,
|
|
303
|
+
usage: {
|
|
304
|
+
input: 0,
|
|
305
|
+
output: 0,
|
|
306
|
+
cacheRead: 0,
|
|
307
|
+
cacheWrite: 0,
|
|
308
|
+
totalTokens: 0,
|
|
309
|
+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
|
310
|
+
},
|
|
311
|
+
stopReason: "error",
|
|
312
|
+
errorMessage: error instanceof Error ? error.message : String(error),
|
|
313
|
+
timestamp: Date.now(),
|
|
314
|
+
},
|
|
315
|
+
});
|
|
316
|
+
stream.end();
|
|
317
|
+
}
|
|
318
|
+
})();
|
|
319
|
+
|
|
320
|
+
return stream;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
// =============================================================================
|
|
324
|
+
// Extension Entry Point
|
|
325
|
+
// =============================================================================
|
|
326
|
+
|
|
327
|
+
export default function (pi: ExtensionAPI) {
|
|
328
|
+
pi.registerProvider("gitlab-duo", {
|
|
329
|
+
baseUrl: AI_GATEWAY_URL,
|
|
330
|
+
apiKey: "GITLAB_TOKEN",
|
|
331
|
+
api: "gitlab-duo-api",
|
|
332
|
+
models: MODELS.map(({ id, name, reasoning, input, cost, contextWindow, maxTokens }) => ({
|
|
333
|
+
id,
|
|
334
|
+
name,
|
|
335
|
+
reasoning,
|
|
336
|
+
input,
|
|
337
|
+
cost,
|
|
338
|
+
contextWindow,
|
|
339
|
+
maxTokens,
|
|
340
|
+
})),
|
|
341
|
+
oauth: {
|
|
342
|
+
name: "GitLab Duo",
|
|
343
|
+
login: loginGitLab,
|
|
344
|
+
refreshToken: refreshGitLabToken,
|
|
345
|
+
getApiKey: (cred) => cred.access,
|
|
346
|
+
},
|
|
347
|
+
streamSimple: streamGitLabDuo,
|
|
348
|
+
});
|
|
349
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "pi-extension-custom-provider-gitlab-duo",
|
|
3
|
+
"private": true,
|
|
4
|
+
"version": "1.1.0",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"clean": "echo 'nothing to clean'",
|
|
8
|
+
"build": "echo 'nothing to build'",
|
|
9
|
+
"check": "echo 'nothing to check'"
|
|
10
|
+
},
|
|
11
|
+
"pi": {
|
|
12
|
+
"extensions": [
|
|
13
|
+
"./index.ts"
|
|
14
|
+
]
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Test script for GitLab Duo extension
|
|
3
|
+
* Run: npx tsx test.ts [model-id] [--thinking]
|
|
4
|
+
*
|
|
5
|
+
* Examples:
|
|
6
|
+
* npx tsx test.ts # Test default (claude-sonnet-4-5-20250929)
|
|
7
|
+
* npx tsx test.ts gpt-5-codex # Test GPT-5 Codex
|
|
8
|
+
* npx tsx test.ts claude-sonnet-4-5-20250929 --thinking
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { type Api, type Context, type Model, registerApiProvider, streamSimple } from "@mariozechner/pi-ai";
|
|
12
|
+
import { readFileSync } from "fs";
|
|
13
|
+
import { homedir } from "os";
|
|
14
|
+
import { join } from "path";
|
|
15
|
+
import { MODELS, streamGitLabDuo } from "./index.js";
|
|
16
|
+
|
|
17
|
+
const MODEL_MAP = new Map(MODELS.map((m) => [m.id, m]));
|
|
18
|
+
|
|
19
|
+
async function main() {
|
|
20
|
+
const modelId = process.argv[2] || "claude-sonnet-4-5-20250929";
|
|
21
|
+
const useThinking = process.argv.includes("--thinking");
|
|
22
|
+
|
|
23
|
+
const cfg = MODEL_MAP.get(modelId);
|
|
24
|
+
if (!cfg) {
|
|
25
|
+
console.error(`Unknown model: ${modelId}`);
|
|
26
|
+
console.error("Available:", MODELS.map((m) => m.id).join(", "));
|
|
27
|
+
process.exit(1);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// Read auth
|
|
31
|
+
const authPath = join(homedir(), ".pi", "agent", "auth.json");
|
|
32
|
+
const authData = JSON.parse(readFileSync(authPath, "utf-8"));
|
|
33
|
+
const gitlabCred = authData["gitlab-duo"];
|
|
34
|
+
if (!gitlabCred?.access) {
|
|
35
|
+
console.error("No gitlab-duo credentials. Run /login gitlab-duo first.");
|
|
36
|
+
process.exit(1);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// Register provider
|
|
40
|
+
registerApiProvider({
|
|
41
|
+
api: "gitlab-duo-api" as Api,
|
|
42
|
+
stream: streamGitLabDuo,
|
|
43
|
+
streamSimple: streamGitLabDuo,
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
// Create model
|
|
47
|
+
const model: Model<Api> = {
|
|
48
|
+
id: cfg.id,
|
|
49
|
+
name: cfg.name,
|
|
50
|
+
api: "gitlab-duo-api" as Api,
|
|
51
|
+
provider: "gitlab-duo",
|
|
52
|
+
baseUrl: cfg.baseUrl,
|
|
53
|
+
reasoning: cfg.reasoning,
|
|
54
|
+
input: cfg.input,
|
|
55
|
+
cost: cfg.cost,
|
|
56
|
+
contextWindow: cfg.contextWindow,
|
|
57
|
+
maxTokens: cfg.maxTokens,
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
const context: Context = {
|
|
61
|
+
messages: [{ role: "user", content: "Say hello in exactly 3 words.", timestamp: Date.now() }],
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
console.log(`Model: ${model.id}, Backend: ${cfg.backend}, Thinking: ${useThinking}`);
|
|
65
|
+
|
|
66
|
+
const stream = streamSimple(model, context, {
|
|
67
|
+
apiKey: gitlabCred.access,
|
|
68
|
+
maxTokens: 100,
|
|
69
|
+
reasoning: useThinking ? "low" : undefined,
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
for await (const event of stream) {
|
|
73
|
+
if (event.type === "thinking_start") console.log("[Thinking]");
|
|
74
|
+
else if (event.type === "thinking_delta") process.stdout.write(event.delta);
|
|
75
|
+
else if (event.type === "thinking_end") console.log("\n[/Thinking]\n");
|
|
76
|
+
else if (event.type === "text_delta") process.stdout.write(event.delta);
|
|
77
|
+
else if (event.type === "error") console.error("\nError:", event.error.errorMessage);
|
|
78
|
+
else if (event.type === "done") console.log("\n\nDone!", event.reason, event.message.usage);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
main().catch(console.error);
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Inter-extension event bus example.
|
|
3
|
+
*
|
|
4
|
+
* Shows pi.events for communication between extensions. One extension
|
|
5
|
+
* can emit events that other extensions listen to.
|
|
6
|
+
*
|
|
7
|
+
* Usage: /emit [event-name] [data] - emit an event on the bus
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type { ExtensionAPI, ExtensionContext } from "@mariozechner/pi-coding-agent";
|
|
11
|
+
|
|
12
|
+
export default function (pi: ExtensionAPI) {
|
|
13
|
+
// Store ctx for use in event handler
|
|
14
|
+
let currentCtx: ExtensionContext | undefined;
|
|
15
|
+
|
|
16
|
+
pi.on("session_start", async (_event, ctx) => {
|
|
17
|
+
currentCtx = ctx;
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
// Listen for events from other extensions
|
|
21
|
+
pi.events.on("my:notification", (data) => {
|
|
22
|
+
const { message, from } = data as { message: string; from: string };
|
|
23
|
+
currentCtx?.ui.notify(`Event from ${from}: ${message}`, "info");
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
// Command to emit events (emits "my:notification" which the listener above receives)
|
|
27
|
+
pi.registerCommand("emit", {
|
|
28
|
+
description: "Emit my:notification event (usage: /emit message)",
|
|
29
|
+
handler: async (args, _ctx) => {
|
|
30
|
+
const message = args.trim() || "hello";
|
|
31
|
+
pi.events.emit("my:notification", { message, from: "/emit command" });
|
|
32
|
+
// Listener above will show the notification
|
|
33
|
+
},
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
// Example: emit on session start
|
|
37
|
+
pi.on("session_start", async () => {
|
|
38
|
+
pi.events.emit("my:notification", {
|
|
39
|
+
message: "Session started",
|
|
40
|
+
from: "event-bus-example",
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Custom message rendering example.
|
|
3
|
+
*
|
|
4
|
+
* Shows how to use registerMessageRenderer to control how custom messages
|
|
5
|
+
* appear in the TUI, with colors, formatting, and expandable details.
|
|
6
|
+
*
|
|
7
|
+
* Usage: /status [message] - sends a status message with custom rendering
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
|
|
11
|
+
import { Box, Text } from "@mariozechner/pi-tui";
|
|
12
|
+
|
|
13
|
+
export default function (pi: ExtensionAPI) {
|
|
14
|
+
// Register custom renderer for "status-update" messages
|
|
15
|
+
pi.registerMessageRenderer("status-update", (message, { expanded }, theme) => {
|
|
16
|
+
const details = message.details as { level: string; timestamp: number } | undefined;
|
|
17
|
+
const level = details?.level ?? "info";
|
|
18
|
+
|
|
19
|
+
// Color based on level
|
|
20
|
+
const color = level === "error" ? "error" : level === "warn" ? "warning" : "success";
|
|
21
|
+
const prefix = theme.fg(color, `[${level.toUpperCase()}]`);
|
|
22
|
+
|
|
23
|
+
let text = `${prefix} ${message.content}`;
|
|
24
|
+
|
|
25
|
+
// Show timestamp when expanded
|
|
26
|
+
if (expanded && details?.timestamp) {
|
|
27
|
+
const time = new Date(details.timestamp).toLocaleTimeString();
|
|
28
|
+
text += `\n${theme.fg("dim", ` at ${time}`)}`;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Use Box with customMessageBg for consistent styling
|
|
32
|
+
const box = new Box(1, 1, (t) => theme.bg("customMessageBg", t));
|
|
33
|
+
box.addChild(new Text(text, 0, 0));
|
|
34
|
+
return box;
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
// Command to send status messages
|
|
38
|
+
pi.registerCommand("status", {
|
|
39
|
+
description: "Send a status message (usage: /status [warn|error] message)",
|
|
40
|
+
handler: async (args, _ctx) => {
|
|
41
|
+
const parts = args.trim().split(/\s+/);
|
|
42
|
+
let level = "info";
|
|
43
|
+
let content = args.trim();
|
|
44
|
+
|
|
45
|
+
// Check for level prefix
|
|
46
|
+
if (parts[0] === "warn" || parts[0] === "error") {
|
|
47
|
+
level = parts[0];
|
|
48
|
+
content = parts.slice(1).join(" ") || "Status update";
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
pi.sendMessage({
|
|
52
|
+
customType: "status-update",
|
|
53
|
+
content,
|
|
54
|
+
display: true,
|
|
55
|
+
details: { level, timestamp: Date.now() },
|
|
56
|
+
});
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Session naming example.
|
|
3
|
+
*
|
|
4
|
+
* Shows setSessionName/getSessionName to give sessions friendly names
|
|
5
|
+
* that appear in the session selector instead of the first message.
|
|
6
|
+
*
|
|
7
|
+
* Usage: /session-name [name] - set or show session name
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
|
|
11
|
+
|
|
12
|
+
export default function (pi: ExtensionAPI) {
|
|
13
|
+
pi.registerCommand("session-name", {
|
|
14
|
+
description: "Set or show session name (usage: /session-name [new name])",
|
|
15
|
+
handler: async (args, ctx) => {
|
|
16
|
+
const name = args.trim();
|
|
17
|
+
|
|
18
|
+
if (name) {
|
|
19
|
+
pi.setSessionName(name);
|
|
20
|
+
ctx.ui.notify(`Session named: ${name}`, "info");
|
|
21
|
+
} else {
|
|
22
|
+
const current = pi.getSessionName();
|
|
23
|
+
ctx.ui.notify(current ? `Session: ${current}` : "No session name set", "info");
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
});
|
|
27
|
+
}
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-extension-with-deps",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.14.0",
|
|
4
4
|
"lockfileVersion": 3,
|
|
5
5
|
"requires": true,
|
|
6
6
|
"packages": {
|
|
7
7
|
"": {
|
|
8
8
|
"name": "pi-extension-with-deps",
|
|
9
|
-
"version": "1.
|
|
9
|
+
"version": "1.14.0",
|
|
10
10
|
"dependencies": {
|
|
11
11
|
"ms": "^2.1.3"
|
|
12
12
|
},
|
|
@@ -5,11 +5,11 @@
|
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
import { getModel } from "@mariozechner/pi-ai";
|
|
8
|
-
import {
|
|
8
|
+
import { AuthStorage, createAgentSession, ModelRegistry } from "@mariozechner/pi-coding-agent";
|
|
9
9
|
|
|
10
10
|
// Set up auth storage and model registry
|
|
11
|
-
const authStorage =
|
|
12
|
-
const modelRegistry =
|
|
11
|
+
const authStorage = new AuthStorage();
|
|
12
|
+
const modelRegistry = new ModelRegistry(authStorage);
|
|
13
13
|
|
|
14
14
|
// Option 1: Find a specific built-in model by provider/id
|
|
15
15
|
const opus = getModel("anthropic", "claude-opus-4-5");
|
|
@@ -4,12 +4,19 @@
|
|
|
4
4
|
* Shows how to replace or modify the default system prompt.
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
import { createAgentSession, SessionManager } from "@mariozechner/pi-coding-agent";
|
|
7
|
+
import { createAgentSession, DefaultResourceLoader, SessionManager } from "@mariozechner/pi-coding-agent";
|
|
8
8
|
|
|
9
9
|
// Option 1: Replace prompt entirely
|
|
10
|
-
const
|
|
11
|
-
|
|
10
|
+
const loader1 = new DefaultResourceLoader({
|
|
11
|
+
systemPromptOverride: () => `You are a helpful assistant that speaks like a pirate.
|
|
12
12
|
Always end responses with "Arrr!"`,
|
|
13
|
+
// Needed to avoid DefaultResourceLoader appending APPEND_SYSTEM.md from ~/.pi/agent or <cwd>/.pi.
|
|
14
|
+
appendSystemPromptOverride: () => [],
|
|
15
|
+
});
|
|
16
|
+
await loader1.reload();
|
|
17
|
+
|
|
18
|
+
const { session: session1 } = await createAgentSession({
|
|
19
|
+
resourceLoader: loader1,
|
|
13
20
|
sessionManager: SessionManager.inMemory(),
|
|
14
21
|
});
|
|
15
22
|
|
|
@@ -23,13 +30,17 @@ console.log("=== Replace prompt ===");
|
|
|
23
30
|
await session1.prompt("What is 2 + 2?");
|
|
24
31
|
console.log("\n");
|
|
25
32
|
|
|
26
|
-
// Option 2:
|
|
27
|
-
const
|
|
28
|
-
|
|
33
|
+
// Option 2: Append instructions to the default prompt
|
|
34
|
+
const loader2 = new DefaultResourceLoader({
|
|
35
|
+
appendSystemPromptOverride: (base) => [
|
|
36
|
+
...base,
|
|
37
|
+
"## Additional Instructions\n- Always be concise\n- Use bullet points when listing things",
|
|
38
|
+
],
|
|
39
|
+
});
|
|
40
|
+
await loader2.reload();
|
|
29
41
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
- Use bullet points when listing things`,
|
|
42
|
+
const { session: session2 } = await createAgentSession({
|
|
43
|
+
resourceLoader: loader2,
|
|
33
44
|
sessionManager: SessionManager.inMemory(),
|
|
34
45
|
});
|
|
35
46
|
|