shortcutxl 0.2.12 → 0.2.13

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 (110) hide show
  1. package/README.md +26 -26
  2. package/agent-docs/README.md +397 -397
  3. package/agent-docs/docs/compaction.md +390 -390
  4. package/agent-docs/docs/custom-provider.md +580 -580
  5. package/agent-docs/docs/extensions.md +1971 -1971
  6. package/agent-docs/docs/packages.md +209 -209
  7. package/agent-docs/docs/rpc.md +1317 -1317
  8. package/agent-docs/docs/sdk.md +962 -962
  9. package/agent-docs/docs/session.md +412 -412
  10. package/agent-docs/docs/termux.md +127 -127
  11. package/agent-docs/docs/tui.md +887 -887
  12. package/agent-docs/examples/README.md +25 -25
  13. package/agent-docs/examples/extensions/README.md +205 -205
  14. package/agent-docs/examples/extensions/antigravity-image-gen.ts +447 -447
  15. package/agent-docs/examples/extensions/auto-commit-on-exit.ts +49 -49
  16. package/agent-docs/examples/extensions/bash-spawn-hook.ts +30 -30
  17. package/agent-docs/examples/extensions/bookmark.ts +50 -50
  18. package/agent-docs/examples/extensions/built-in-tool-renderer.ts +256 -256
  19. package/agent-docs/examples/extensions/claude-rules.ts +86 -86
  20. package/agent-docs/examples/extensions/commands.ts +75 -75
  21. package/agent-docs/examples/extensions/confirm-destructive.ts +59 -59
  22. package/agent-docs/examples/extensions/custom-compaction.ts +126 -126
  23. package/agent-docs/examples/extensions/custom-footer.ts +63 -63
  24. package/agent-docs/examples/extensions/custom-header.ts +73 -73
  25. package/agent-docs/examples/extensions/custom-provider-anthropic/index.ts +660 -660
  26. package/agent-docs/examples/extensions/custom-provider-gitlab-duo/index.ts +362 -362
  27. package/agent-docs/examples/extensions/custom-provider-gitlab-duo/test.ts +88 -88
  28. package/agent-docs/examples/extensions/custom-provider-qwen-cli/index.ts +349 -349
  29. package/agent-docs/examples/extensions/dirty-repo-guard.ts +56 -56
  30. package/agent-docs/examples/extensions/doom-overlay/doom-component.ts +133 -133
  31. package/agent-docs/examples/extensions/doom-overlay/doom-keys.ts +108 -108
  32. package/agent-docs/examples/extensions/doom-overlay/index.ts +74 -74
  33. package/agent-docs/examples/extensions/dynamic-resources/index.ts +15 -15
  34. package/agent-docs/examples/extensions/dynamic-tools.ts +77 -77
  35. package/agent-docs/examples/extensions/event-bus.ts +43 -43
  36. package/agent-docs/examples/extensions/file-trigger.ts +41 -41
  37. package/agent-docs/examples/extensions/git-checkpoint.ts +53 -53
  38. package/agent-docs/examples/extensions/handoff.ts +155 -155
  39. package/agent-docs/examples/extensions/hello.ts +25 -25
  40. package/agent-docs/examples/extensions/inline-bash.ts +94 -94
  41. package/agent-docs/examples/extensions/input-transform.ts +43 -43
  42. package/agent-docs/examples/extensions/interactive-shell.ts +209 -209
  43. package/agent-docs/examples/extensions/mac-system-theme.ts +47 -47
  44. package/agent-docs/examples/extensions/message-renderer.ts +59 -59
  45. package/agent-docs/examples/extensions/minimal-mode.ts +430 -430
  46. package/agent-docs/examples/extensions/modal-editor.ts +90 -90
  47. package/agent-docs/examples/extensions/model-status.ts +31 -31
  48. package/agent-docs/examples/extensions/notify.ts +55 -55
  49. package/agent-docs/examples/extensions/overlay-qa-tests.ts +936 -936
  50. package/agent-docs/examples/extensions/overlay-test.ts +159 -159
  51. package/agent-docs/examples/extensions/permission-gate.ts +37 -37
  52. package/agent-docs/examples/extensions/pirate.ts +47 -47
  53. package/agent-docs/examples/extensions/plan-mode/index.ts +363 -363
  54. package/agent-docs/examples/extensions/preset.ts +418 -418
  55. package/agent-docs/examples/extensions/protected-paths.ts +30 -30
  56. package/agent-docs/examples/extensions/qna.ts +122 -122
  57. package/agent-docs/examples/extensions/question.ts +278 -278
  58. package/agent-docs/examples/extensions/questionnaire.ts +440 -440
  59. package/agent-docs/examples/extensions/rainbow-editor.ts +90 -90
  60. package/agent-docs/examples/extensions/reload-runtime.ts +37 -37
  61. package/agent-docs/examples/extensions/rpc-demo.ts +124 -124
  62. package/agent-docs/examples/extensions/sandbox/index.ts +324 -324
  63. package/agent-docs/examples/extensions/send-user-message.ts +97 -97
  64. package/agent-docs/examples/extensions/session-name.ts +27 -27
  65. package/agent-docs/examples/extensions/shutdown-command.ts +69 -69
  66. package/agent-docs/examples/extensions/snake.ts +343 -343
  67. package/agent-docs/examples/extensions/space-invaders.ts +566 -566
  68. package/agent-docs/examples/extensions/ssh.ts +233 -233
  69. package/agent-docs/examples/extensions/status-line.ts +40 -40
  70. package/agent-docs/examples/extensions/subagent/agents.ts +130 -130
  71. package/agent-docs/examples/extensions/subagent/index.ts +1068 -1068
  72. package/agent-docs/examples/extensions/summarize.ts +206 -206
  73. package/agent-docs/examples/extensions/system-prompt-header.ts +17 -17
  74. package/agent-docs/examples/extensions/timed-confirm.ts +72 -72
  75. package/agent-docs/examples/extensions/titlebar-spinner.ts +58 -58
  76. package/agent-docs/examples/extensions/todo.ts +314 -314
  77. package/agent-docs/examples/extensions/tool-override.ts +146 -146
  78. package/agent-docs/examples/extensions/tools.ts +145 -145
  79. package/agent-docs/examples/extensions/trigger-compact.ts +40 -40
  80. package/agent-docs/examples/extensions/truncated-tool.ts +194 -194
  81. package/agent-docs/examples/extensions/widget-placement.ts +17 -17
  82. package/agent-docs/examples/extensions/with-deps/index.ts +37 -37
  83. package/agent-docs/examples/rpc-extension-ui.ts +654 -654
  84. package/agent-docs/examples/sdk/01-minimal.ts +22 -22
  85. package/agent-docs/examples/sdk/02-custom-model.ts +48 -48
  86. package/agent-docs/examples/sdk/03-custom-prompt.ts +55 -55
  87. package/agent-docs/examples/sdk/04-skills.ts +53 -53
  88. package/agent-docs/examples/sdk/05-tools.ts +56 -56
  89. package/agent-docs/examples/sdk/06-extensions.ts +88 -88
  90. package/agent-docs/examples/sdk/07-context-files.ts +40 -40
  91. package/agent-docs/examples/sdk/08-prompt-templates.ts +47 -47
  92. package/agent-docs/examples/sdk/09-api-keys-and-oauth.ts +48 -48
  93. package/agent-docs/examples/sdk/10-settings.ts +54 -54
  94. package/agent-docs/examples/sdk/11-sessions.ts +48 -48
  95. package/agent-docs/examples/sdk/12-full-control.ts +82 -82
  96. package/agent-docs/examples/sdk/README.md +144 -144
  97. package/agent-docs/xll-spec.md +110 -110
  98. package/dist/core/auth-storage.js +21 -2
  99. package/package.json +1 -1
  100. package/xll/ShortcutXL.xll +0 -0
  101. package/xll/modules/debug_render.py +272 -272
  102. package/xll/modules/gameboy.py +241 -241
  103. package/xll/modules/pong.py +188 -188
  104. package/xll/modules/shortcut_xl/_diff_highlight.py +176 -0
  105. package/xll/modules/shortcut_xl/_log.py +12 -12
  106. package/xll/modules/shortcut_xl/_registry.py +44 -44
  107. package/xll/modules/stocks.py +100 -100
  108. /package/skills/{com-advanced-api → COM-advanced-api}/SKILL.md +0 -0
  109. /package/skills/{com-advanced-api → COM-advanced-api}/excel-type-library.py +0 -0
  110. /package/skills/{com-advanced-api → COM-advanced-api}/office-type-library.py +0 -0
@@ -1,362 +1,362 @@
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 shortcutxl's built-in Anthropic and OpenAI streaming implementations.
6
- *
7
- * Usage:
8
- * shortcut -e ./packages/coding-agent/examples/extensions/custom-provider-gitlab-duo
9
- * # Then /login gitlab-duo, or set GITLAB_TOKEN=glpat-...
10
- */
11
-
12
- import type { ExtensionAPI } from 'shortcutxl';
13
- import {
14
- type Api,
15
- type AssistantMessageEventStream,
16
- type Context,
17
- createAssistantMessageEventStream,
18
- type Model,
19
- type OAuthCredentials,
20
- type OAuthLoginCallbacks,
21
- type SimpleStreamOptions,
22
- streamSimpleAnthropic,
23
- streamSimpleOpenAIResponses
24
- } from 'shortcutxl';
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 = ['.shortcut'];
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 = {
167
- token: data.token,
168
- headers: data.headers,
169
- expiresAt: now + DIRECT_ACCESS_TTL
170
- };
171
- return cachedDirectAccess;
172
- }
173
-
174
- function invalidateDirectAccessToken() {
175
- cachedDirectAccess = null;
176
- }
177
-
178
- // =============================================================================
179
- // OAuth
180
- // =============================================================================
181
-
182
- async function generatePKCE(): Promise<{ verifier: string; challenge: string }> {
183
- const array = new Uint8Array(32);
184
- crypto.getRandomValues(array);
185
- const verifier = btoa(String.fromCharCode(...array))
186
- .replace(/\+/g, '-')
187
- .replace(/\//g, '_')
188
- .replace(/=+$/, '');
189
- const hash = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(verifier));
190
- const challenge = btoa(String.fromCharCode(...new Uint8Array(hash)))
191
- .replace(/\+/g, '-')
192
- .replace(/\//g, '_')
193
- .replace(/=+$/, '');
194
- return { verifier, challenge };
195
- }
196
-
197
- async function loginGitLab(callbacks: OAuthLoginCallbacks): Promise<OAuthCredentials> {
198
- const { verifier, challenge } = await generatePKCE();
199
- const authParams = new URLSearchParams({
200
- client_id: BUNDLED_CLIENT_ID,
201
- redirect_uri: REDIRECT_URI,
202
- response_type: 'code',
203
- scope: OAUTH_SCOPES.join(' '),
204
- code_challenge: challenge,
205
- code_challenge_method: 'S256',
206
- state: crypto.randomUUID()
207
- });
208
-
209
- callbacks.onAuth({ url: `${GITLAB_COM_URL}/oauth/authorize?${authParams.toString()}` });
210
- const callbackUrl = await callbacks.onPrompt({ message: 'Paste the callback URL:' });
211
- const code = new URL(callbackUrl).searchParams.get('code');
212
- if (!code) throw new Error('No authorization code found in callback URL');
213
-
214
- const tokenResponse = await fetch(`${GITLAB_COM_URL}/oauth/token`, {
215
- method: 'POST',
216
- headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
217
- body: new URLSearchParams({
218
- client_id: BUNDLED_CLIENT_ID,
219
- grant_type: 'authorization_code',
220
- code,
221
- code_verifier: verifier,
222
- redirect_uri: REDIRECT_URI
223
- }).toString()
224
- });
225
-
226
- if (!tokenResponse.ok) throw new Error(`Token exchange failed: ${await tokenResponse.text()}`);
227
- const data = (await tokenResponse.json()) as {
228
- access_token: string;
229
- refresh_token: string;
230
- expires_in: number;
231
- created_at: number;
232
- };
233
- invalidateDirectAccessToken();
234
- return {
235
- refresh: data.refresh_token,
236
- access: data.access_token,
237
- expires: (data.created_at + data.expires_in) * 1000 - 5 * 60 * 1000
238
- };
239
- }
240
-
241
- async function refreshGitLabToken(credentials: OAuthCredentials): Promise<OAuthCredentials> {
242
- const response = await fetch(`${GITLAB_COM_URL}/oauth/token`, {
243
- method: 'POST',
244
- headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
245
- body: new URLSearchParams({
246
- client_id: BUNDLED_CLIENT_ID,
247
- grant_type: 'refresh_token',
248
- refresh_token: credentials.refresh
249
- }).toString()
250
- });
251
- if (!response.ok) throw new Error(`Token refresh failed: ${await response.text()}`);
252
- const data = (await response.json()) as {
253
- access_token: string;
254
- refresh_token: string;
255
- expires_in: number;
256
- created_at: number;
257
- };
258
- invalidateDirectAccessToken();
259
- return {
260
- refresh: data.refresh_token,
261
- access: data.access_token,
262
- expires: (data.created_at + data.expires_in) * 1000 - 5 * 60 * 1000
263
- };
264
- }
265
-
266
- // =============================================================================
267
- // Stream Function
268
- // =============================================================================
269
-
270
- export function streamGitLabDuo(
271
- model: Model<Api>,
272
- context: Context,
273
- options?: SimpleStreamOptions
274
- ): AssistantMessageEventStream {
275
- const stream = createAssistantMessageEventStream();
276
-
277
- (async () => {
278
- try {
279
- const gitlabAccessToken = options?.apiKey;
280
- if (!gitlabAccessToken)
281
- throw new Error('No GitLab access token. Run /login gitlab-duo or set GITLAB_TOKEN');
282
-
283
- const cfg = MODEL_MAP.get(model.id);
284
- if (!cfg) throw new Error(`Unknown model: ${model.id}`);
285
-
286
- const directAccess = await getDirectAccessToken(gitlabAccessToken);
287
- const modelWithBaseUrl = { ...model, baseUrl: cfg.baseUrl };
288
- const headers = { ...directAccess.headers, Authorization: `Bearer ${directAccess.token}` };
289
- const streamOptions = { ...options, apiKey: 'gitlab-duo', headers };
290
-
291
- const innerStream =
292
- cfg.backend === 'anthropic'
293
- ? streamSimpleAnthropic(
294
- modelWithBaseUrl as Model<'anthropic-messages'>,
295
- context,
296
- streamOptions
297
- )
298
- : streamSimpleOpenAIResponses(
299
- modelWithBaseUrl as Model<'openai-responses'>,
300
- context,
301
- streamOptions
302
- );
303
-
304
- for await (const event of innerStream) stream.push(event);
305
- stream.end();
306
- } catch (error) {
307
- stream.push({
308
- type: 'error',
309
- reason: 'error',
310
- error: {
311
- role: 'assistant',
312
- content: [],
313
- api: model.api,
314
- provider: model.provider,
315
- model: model.id,
316
- usage: {
317
- input: 0,
318
- output: 0,
319
- cacheRead: 0,
320
- cacheWrite: 0,
321
- totalTokens: 0,
322
- cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }
323
- },
324
- stopReason: 'error',
325
- errorMessage: error instanceof Error ? error.message : String(error),
326
- timestamp: Date.now()
327
- }
328
- });
329
- stream.end();
330
- }
331
- })();
332
-
333
- return stream;
334
- }
335
-
336
- // =============================================================================
337
- // Extension Entry Point
338
- // =============================================================================
339
-
340
- export default function (shortcut: ExtensionAPI) {
341
- shortcut.registerProvider('gitlab-duo', {
342
- baseUrl: AI_GATEWAY_URL,
343
- apiKey: 'GITLAB_TOKEN',
344
- api: 'gitlab-duo-api',
345
- models: MODELS.map(({ id, name, reasoning, input, cost, contextWindow, maxTokens }) => ({
346
- id,
347
- name,
348
- reasoning,
349
- input,
350
- cost,
351
- contextWindow,
352
- maxTokens
353
- })),
354
- oauth: {
355
- name: 'GitLab Duo',
356
- login: loginGitLab,
357
- refreshToken: refreshGitLabToken,
358
- getApiKey: (cred) => cred.access
359
- },
360
- streamSimple: streamGitLabDuo
361
- });
362
- }
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 shortcutxl's built-in Anthropic and OpenAI streaming implementations.
6
+ *
7
+ * Usage:
8
+ * shortcut -e ./packages/coding-agent/examples/extensions/custom-provider-gitlab-duo
9
+ * # Then /login gitlab-duo, or set GITLAB_TOKEN=glpat-...
10
+ */
11
+
12
+ import type { ExtensionAPI } from 'shortcutxl';
13
+ import {
14
+ type Api,
15
+ type AssistantMessageEventStream,
16
+ type Context,
17
+ createAssistantMessageEventStream,
18
+ type Model,
19
+ type OAuthCredentials,
20
+ type OAuthLoginCallbacks,
21
+ type SimpleStreamOptions,
22
+ streamSimpleAnthropic,
23
+ streamSimpleOpenAIResponses
24
+ } from 'shortcutxl';
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 = ['.shortcut'];
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 = {
167
+ token: data.token,
168
+ headers: data.headers,
169
+ expiresAt: now + DIRECT_ACCESS_TTL
170
+ };
171
+ return cachedDirectAccess;
172
+ }
173
+
174
+ function invalidateDirectAccessToken() {
175
+ cachedDirectAccess = null;
176
+ }
177
+
178
+ // =============================================================================
179
+ // OAuth
180
+ // =============================================================================
181
+
182
+ async function generatePKCE(): Promise<{ verifier: string; challenge: string }> {
183
+ const array = new Uint8Array(32);
184
+ crypto.getRandomValues(array);
185
+ const verifier = btoa(String.fromCharCode(...array))
186
+ .replace(/\+/g, '-')
187
+ .replace(/\//g, '_')
188
+ .replace(/=+$/, '');
189
+ const hash = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(verifier));
190
+ const challenge = btoa(String.fromCharCode(...new Uint8Array(hash)))
191
+ .replace(/\+/g, '-')
192
+ .replace(/\//g, '_')
193
+ .replace(/=+$/, '');
194
+ return { verifier, challenge };
195
+ }
196
+
197
+ async function loginGitLab(callbacks: OAuthLoginCallbacks): Promise<OAuthCredentials> {
198
+ const { verifier, challenge } = await generatePKCE();
199
+ const authParams = new URLSearchParams({
200
+ client_id: BUNDLED_CLIENT_ID,
201
+ redirect_uri: REDIRECT_URI,
202
+ response_type: 'code',
203
+ scope: OAUTH_SCOPES.join(' '),
204
+ code_challenge: challenge,
205
+ code_challenge_method: 'S256',
206
+ state: crypto.randomUUID()
207
+ });
208
+
209
+ callbacks.onAuth({ url: `${GITLAB_COM_URL}/oauth/authorize?${authParams.toString()}` });
210
+ const callbackUrl = await callbacks.onPrompt({ message: 'Paste the callback URL:' });
211
+ const code = new URL(callbackUrl).searchParams.get('code');
212
+ if (!code) throw new Error('No authorization code found in callback URL');
213
+
214
+ const tokenResponse = await fetch(`${GITLAB_COM_URL}/oauth/token`, {
215
+ method: 'POST',
216
+ headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
217
+ body: new URLSearchParams({
218
+ client_id: BUNDLED_CLIENT_ID,
219
+ grant_type: 'authorization_code',
220
+ code,
221
+ code_verifier: verifier,
222
+ redirect_uri: REDIRECT_URI
223
+ }).toString()
224
+ });
225
+
226
+ if (!tokenResponse.ok) throw new Error(`Token exchange failed: ${await tokenResponse.text()}`);
227
+ const data = (await tokenResponse.json()) as {
228
+ access_token: string;
229
+ refresh_token: string;
230
+ expires_in: number;
231
+ created_at: number;
232
+ };
233
+ invalidateDirectAccessToken();
234
+ return {
235
+ refresh: data.refresh_token,
236
+ access: data.access_token,
237
+ expires: (data.created_at + data.expires_in) * 1000 - 5 * 60 * 1000
238
+ };
239
+ }
240
+
241
+ async function refreshGitLabToken(credentials: OAuthCredentials): Promise<OAuthCredentials> {
242
+ const response = await fetch(`${GITLAB_COM_URL}/oauth/token`, {
243
+ method: 'POST',
244
+ headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
245
+ body: new URLSearchParams({
246
+ client_id: BUNDLED_CLIENT_ID,
247
+ grant_type: 'refresh_token',
248
+ refresh_token: credentials.refresh
249
+ }).toString()
250
+ });
251
+ if (!response.ok) throw new Error(`Token refresh failed: ${await response.text()}`);
252
+ const data = (await response.json()) as {
253
+ access_token: string;
254
+ refresh_token: string;
255
+ expires_in: number;
256
+ created_at: number;
257
+ };
258
+ invalidateDirectAccessToken();
259
+ return {
260
+ refresh: data.refresh_token,
261
+ access: data.access_token,
262
+ expires: (data.created_at + data.expires_in) * 1000 - 5 * 60 * 1000
263
+ };
264
+ }
265
+
266
+ // =============================================================================
267
+ // Stream Function
268
+ // =============================================================================
269
+
270
+ export function streamGitLabDuo(
271
+ model: Model<Api>,
272
+ context: Context,
273
+ options?: SimpleStreamOptions
274
+ ): AssistantMessageEventStream {
275
+ const stream = createAssistantMessageEventStream();
276
+
277
+ (async () => {
278
+ try {
279
+ const gitlabAccessToken = options?.apiKey;
280
+ if (!gitlabAccessToken)
281
+ throw new Error('No GitLab access token. Run /login gitlab-duo or set GITLAB_TOKEN');
282
+
283
+ const cfg = MODEL_MAP.get(model.id);
284
+ if (!cfg) throw new Error(`Unknown model: ${model.id}`);
285
+
286
+ const directAccess = await getDirectAccessToken(gitlabAccessToken);
287
+ const modelWithBaseUrl = { ...model, baseUrl: cfg.baseUrl };
288
+ const headers = { ...directAccess.headers, Authorization: `Bearer ${directAccess.token}` };
289
+ const streamOptions = { ...options, apiKey: 'gitlab-duo', headers };
290
+
291
+ const innerStream =
292
+ cfg.backend === 'anthropic'
293
+ ? streamSimpleAnthropic(
294
+ modelWithBaseUrl as Model<'anthropic-messages'>,
295
+ context,
296
+ streamOptions
297
+ )
298
+ : streamSimpleOpenAIResponses(
299
+ modelWithBaseUrl as Model<'openai-responses'>,
300
+ context,
301
+ streamOptions
302
+ );
303
+
304
+ for await (const event of innerStream) stream.push(event);
305
+ stream.end();
306
+ } catch (error) {
307
+ stream.push({
308
+ type: 'error',
309
+ reason: 'error',
310
+ error: {
311
+ role: 'assistant',
312
+ content: [],
313
+ api: model.api,
314
+ provider: model.provider,
315
+ model: model.id,
316
+ usage: {
317
+ input: 0,
318
+ output: 0,
319
+ cacheRead: 0,
320
+ cacheWrite: 0,
321
+ totalTokens: 0,
322
+ cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }
323
+ },
324
+ stopReason: 'error',
325
+ errorMessage: error instanceof Error ? error.message : String(error),
326
+ timestamp: Date.now()
327
+ }
328
+ });
329
+ stream.end();
330
+ }
331
+ })();
332
+
333
+ return stream;
334
+ }
335
+
336
+ // =============================================================================
337
+ // Extension Entry Point
338
+ // =============================================================================
339
+
340
+ export default function (shortcut: ExtensionAPI) {
341
+ shortcut.registerProvider('gitlab-duo', {
342
+ baseUrl: AI_GATEWAY_URL,
343
+ apiKey: 'GITLAB_TOKEN',
344
+ api: 'gitlab-duo-api',
345
+ models: MODELS.map(({ id, name, reasoning, input, cost, contextWindow, maxTokens }) => ({
346
+ id,
347
+ name,
348
+ reasoning,
349
+ input,
350
+ cost,
351
+ contextWindow,
352
+ maxTokens
353
+ })),
354
+ oauth: {
355
+ name: 'GitLab Duo',
356
+ login: loginGitLab,
357
+ refreshToken: refreshGitLabToken,
358
+ getApiKey: (cred) => cred.access
359
+ },
360
+ streamSimple: streamGitLabDuo
361
+ });
362
+ }