@pixelbyte-software/pixcode 1.35.0 → 1.35.2
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/dist/assets/{index-Djuh0wHV.js → index-D1-AIL_5.js} +133 -133
- package/dist/favicon.svg +8 -8
- package/dist/icons/icon-128x128.svg +9 -9
- package/dist/icons/icon-144x144.svg +9 -9
- package/dist/icons/icon-152x152.svg +9 -9
- package/dist/icons/icon-192x192.svg +9 -9
- package/dist/icons/icon-384x384.svg +9 -9
- package/dist/icons/icon-512x512.svg +9 -9
- package/dist/icons/icon-72x72.svg +9 -9
- package/dist/icons/icon-96x96.svg +9 -9
- package/dist/icons/icon-template.svg +9 -9
- package/dist/index.html +1 -1
- package/dist/logo.svg +12 -12
- package/dist-server/server/modules/orchestration/preview/preview-proxy.js +3 -3
- package/dist-server/server/modules/orchestration/preview/preview-proxy.js.map +1 -1
- package/package.json +1 -1
- package/server/database/db.js +794 -794
- package/server/modules/orchestration/a2a/agent-card.ts +55 -55
- package/server/modules/orchestration/a2a/auth.middleware.ts +29 -29
- package/server/modules/orchestration/a2a/bus.ts +46 -46
- package/server/modules/orchestration/preview/preview-proxy.ts +3 -3
- package/server/modules/providers/list/opencode/opencode-auth.provider.ts +130 -130
- package/server/modules/providers/list/opencode/opencode-mcp.provider.ts +126 -126
- package/server/modules/providers/list/opencode/opencode.provider.ts +29 -29
- package/server/modules/providers/list/qwen/qwen-auth.provider.ts +145 -145
- package/server/modules/providers/list/qwen/qwen-mcp.provider.ts +114 -114
- package/server/modules/providers/list/qwen/qwen.provider.ts +21 -21
- package/server/modules/providers/shared/provider-configs.ts +142 -142
- package/server/qwen-code-cli.js +395 -395
- package/server/qwen-response-handler.js +73 -73
- package/server/routes/qwen.js +27 -27
- package/server/services/external-access.js +171 -171
- package/server/services/provider-models.js +381 -381
- package/server/services/telegram/telegram-http-client.js +130 -130
- package/server/services/vapid-keys.js +36 -36
- package/server/utils/port-access.js +209 -209
|
@@ -1,55 +1,55 @@
|
|
|
1
|
-
// server/modules/orchestration/a2a/agent-card.ts
|
|
2
|
-
// Pixcode advertises itself as one A2A agent at /a2a/.well-known/agent-card.json.
|
|
3
|
-
// Per-CLI adapters publish their own cards under /a2a/agents/:id/agent-card.
|
|
4
|
-
|
|
5
|
-
import { readFileSync } from 'node:fs';
|
|
6
|
-
import { dirname, resolve } from 'node:path';
|
|
7
|
-
import { fileURLToPath } from 'node:url';
|
|
8
|
-
|
|
9
|
-
import { adapterRegistry } from '@/modules/orchestration/a2a/adapter-registry.js';
|
|
10
|
-
import type { AgentCard } from '@/modules/orchestration/a2a/types.js';
|
|
11
|
-
|
|
12
|
-
// Resolve <repo-root>/package.json from this file's location.
|
|
13
|
-
// Source layout: server/modules/orchestration/a2a/agent-card.ts (4 levels deep from repo root)
|
|
14
|
-
// Built layout: dist-server/server/modules/orchestration/a2a/agent-card.js (5 levels deep)
|
|
15
|
-
// Walk up until a package.json containing "name":"@pixelbyte-software/pixcode" is found.
|
|
16
|
-
function readPixcodeVersion(): string {
|
|
17
|
-
let dir = dirname(fileURLToPath(import.meta.url));
|
|
18
|
-
for (let i = 0; i < 8; i++) {
|
|
19
|
-
try {
|
|
20
|
-
const candidate = resolve(dir, 'package.json');
|
|
21
|
-
const raw = readFileSync(candidate, 'utf8');
|
|
22
|
-
const pkg = JSON.parse(raw) as { name?: string; version?: string };
|
|
23
|
-
if (pkg.name === '@pixelbyte-software/pixcode' && typeof pkg.version === 'string') {
|
|
24
|
-
return pkg.version;
|
|
25
|
-
}
|
|
26
|
-
} catch {
|
|
27
|
-
// not here, walk up
|
|
28
|
-
}
|
|
29
|
-
const parent = dirname(dir);
|
|
30
|
-
if (parent === dir) break;
|
|
31
|
-
dir = parent;
|
|
32
|
-
}
|
|
33
|
-
return '0.0.0-dev';
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
const VERSION: string = readPixcodeVersion();
|
|
37
|
-
|
|
38
|
-
export function buildPixcodeAgentCard(baseUrl: string): AgentCard {
|
|
39
|
-
const skills = adapterRegistry
|
|
40
|
-
.agentCards()
|
|
41
|
-
.flatMap((card) => card.skills)
|
|
42
|
-
.filter((skill, idx, arr) => arr.findIndex((s) => s.id === skill.id) === idx);
|
|
43
|
-
|
|
44
|
-
return {
|
|
45
|
-
name: 'pixcode',
|
|
46
|
-
description:
|
|
47
|
-
'Pixcode multi-CLI orchestration platform. Routes A2A tasks to ' +
|
|
48
|
-
'Claude Code, Codex, Cursor, Gemini, Qwen, or OpenCode adapters.',
|
|
49
|
-
url: `${baseUrl.replace(/\/$/, '')}/a2a`,
|
|
50
|
-
version: VERSION,
|
|
51
|
-
capabilities: ['streaming', 'taskRouting'],
|
|
52
|
-
skills,
|
|
53
|
-
authentication: { type: 'bearer' },
|
|
54
|
-
};
|
|
55
|
-
}
|
|
1
|
+
// server/modules/orchestration/a2a/agent-card.ts
|
|
2
|
+
// Pixcode advertises itself as one A2A agent at /a2a/.well-known/agent-card.json.
|
|
3
|
+
// Per-CLI adapters publish their own cards under /a2a/agents/:id/agent-card.
|
|
4
|
+
|
|
5
|
+
import { readFileSync } from 'node:fs';
|
|
6
|
+
import { dirname, resolve } from 'node:path';
|
|
7
|
+
import { fileURLToPath } from 'node:url';
|
|
8
|
+
|
|
9
|
+
import { adapterRegistry } from '@/modules/orchestration/a2a/adapter-registry.js';
|
|
10
|
+
import type { AgentCard } from '@/modules/orchestration/a2a/types.js';
|
|
11
|
+
|
|
12
|
+
// Resolve <repo-root>/package.json from this file's location.
|
|
13
|
+
// Source layout: server/modules/orchestration/a2a/agent-card.ts (4 levels deep from repo root)
|
|
14
|
+
// Built layout: dist-server/server/modules/orchestration/a2a/agent-card.js (5 levels deep)
|
|
15
|
+
// Walk up until a package.json containing "name":"@pixelbyte-software/pixcode" is found.
|
|
16
|
+
function readPixcodeVersion(): string {
|
|
17
|
+
let dir = dirname(fileURLToPath(import.meta.url));
|
|
18
|
+
for (let i = 0; i < 8; i++) {
|
|
19
|
+
try {
|
|
20
|
+
const candidate = resolve(dir, 'package.json');
|
|
21
|
+
const raw = readFileSync(candidate, 'utf8');
|
|
22
|
+
const pkg = JSON.parse(raw) as { name?: string; version?: string };
|
|
23
|
+
if (pkg.name === '@pixelbyte-software/pixcode' && typeof pkg.version === 'string') {
|
|
24
|
+
return pkg.version;
|
|
25
|
+
}
|
|
26
|
+
} catch {
|
|
27
|
+
// not here, walk up
|
|
28
|
+
}
|
|
29
|
+
const parent = dirname(dir);
|
|
30
|
+
if (parent === dir) break;
|
|
31
|
+
dir = parent;
|
|
32
|
+
}
|
|
33
|
+
return '0.0.0-dev';
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const VERSION: string = readPixcodeVersion();
|
|
37
|
+
|
|
38
|
+
export function buildPixcodeAgentCard(baseUrl: string): AgentCard {
|
|
39
|
+
const skills = adapterRegistry
|
|
40
|
+
.agentCards()
|
|
41
|
+
.flatMap((card) => card.skills)
|
|
42
|
+
.filter((skill, idx, arr) => arr.findIndex((s) => s.id === skill.id) === idx);
|
|
43
|
+
|
|
44
|
+
return {
|
|
45
|
+
name: 'pixcode',
|
|
46
|
+
description:
|
|
47
|
+
'Pixcode multi-CLI orchestration platform. Routes A2A tasks to ' +
|
|
48
|
+
'Claude Code, Codex, Cursor, Gemini, Qwen, or OpenCode adapters.',
|
|
49
|
+
url: `${baseUrl.replace(/\/$/, '')}/a2a`,
|
|
50
|
+
version: VERSION,
|
|
51
|
+
capabilities: ['streaming', 'taskRouting'],
|
|
52
|
+
skills,
|
|
53
|
+
authentication: { type: 'bearer' },
|
|
54
|
+
};
|
|
55
|
+
}
|
|
@@ -1,29 +1,29 @@
|
|
|
1
|
-
// server/modules/orchestration/a2a/auth.middleware.ts
|
|
2
|
-
// Localhost callers bypass auth; everyone else needs a Bearer JWT
|
|
3
|
-
// validated by pixcode's existing auth stack.
|
|
4
|
-
|
|
5
|
-
import type { NextFunction, Request, Response } from 'express';
|
|
6
|
-
|
|
7
|
-
// @ts-ignore — plain-JS module without type declarations
|
|
8
|
-
// eslint-disable-next-line boundaries/no-unknown -- server/middleware/auth.js is a top-level auth runtime not yet classified by eslint.config.js; cleanup deferred.
|
|
9
|
-
import { authenticateToken } from '@/middleware/auth.js';
|
|
10
|
-
|
|
11
|
-
const LOCAL_HOSTS = new Set(['127.0.0.1', '::1', 'localhost', '::ffff:127.0.0.1']);
|
|
12
|
-
|
|
13
|
-
function isLocalRequest(req: Request): boolean {
|
|
14
|
-
const remote = req.socket.remoteAddress ?? '';
|
|
15
|
-
if (LOCAL_HOSTS.has(remote)) return true;
|
|
16
|
-
// Trust the X-Forwarded-For header only when the inbound socket is local
|
|
17
|
-
// (i.e. the reverse proxy itself is on the same host).
|
|
18
|
-
return false;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export function a2aAuth(req: Request, res: Response, next: NextFunction): void {
|
|
22
|
-
if (isLocalRequest(req)) {
|
|
23
|
-
next();
|
|
24
|
-
return;
|
|
25
|
-
}
|
|
26
|
-
// Delegate to existing pixcode JWT middleware. authenticateToken
|
|
27
|
-
// populates req.user on success and 401s on failure.
|
|
28
|
-
authenticateToken(req, res, next);
|
|
29
|
-
}
|
|
1
|
+
// server/modules/orchestration/a2a/auth.middleware.ts
|
|
2
|
+
// Localhost callers bypass auth; everyone else needs a Bearer JWT
|
|
3
|
+
// validated by pixcode's existing auth stack.
|
|
4
|
+
|
|
5
|
+
import type { NextFunction, Request, Response } from 'express';
|
|
6
|
+
|
|
7
|
+
// @ts-ignore — plain-JS module without type declarations
|
|
8
|
+
// eslint-disable-next-line boundaries/no-unknown -- server/middleware/auth.js is a top-level auth runtime not yet classified by eslint.config.js; cleanup deferred.
|
|
9
|
+
import { authenticateToken } from '@/middleware/auth.js';
|
|
10
|
+
|
|
11
|
+
const LOCAL_HOSTS = new Set(['127.0.0.1', '::1', 'localhost', '::ffff:127.0.0.1']);
|
|
12
|
+
|
|
13
|
+
function isLocalRequest(req: Request): boolean {
|
|
14
|
+
const remote = req.socket.remoteAddress ?? '';
|
|
15
|
+
if (LOCAL_HOSTS.has(remote)) return true;
|
|
16
|
+
// Trust the X-Forwarded-For header only when the inbound socket is local
|
|
17
|
+
// (i.e. the reverse proxy itself is on the same host).
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function a2aAuth(req: Request, res: Response, next: NextFunction): void {
|
|
22
|
+
if (isLocalRequest(req)) {
|
|
23
|
+
next();
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
// Delegate to existing pixcode JWT middleware. authenticateToken
|
|
27
|
+
// populates req.user on success and 401s on failure.
|
|
28
|
+
authenticateToken(req, res, next);
|
|
29
|
+
}
|
|
@@ -1,46 +1,46 @@
|
|
|
1
|
-
// server/modules/orchestration/a2a/bus.ts
|
|
2
|
-
// In-process pub/sub on top of Node's EventEmitter.
|
|
3
|
-
// Subscribers receive every event for a given taskId; an
|
|
4
|
-
// "all" subscriber receives every event regardless of task.
|
|
5
|
-
// Note: the literal taskId "__all__" is reserved for the broadcast
|
|
6
|
-
// channel; callers must not use it as a real taskId.
|
|
7
|
-
|
|
8
|
-
import { EventEmitter } from 'node:events';
|
|
9
|
-
|
|
10
|
-
import type { BusEvent } from '@/modules/orchestration/a2a/types.js';
|
|
11
|
-
|
|
12
|
-
type Listener = (event: BusEvent) => void;
|
|
13
|
-
|
|
14
|
-
const ALL = '__all__';
|
|
15
|
-
|
|
16
|
-
class A2ABus {
|
|
17
|
-
private readonly emitter = new EventEmitter();
|
|
18
|
-
|
|
19
|
-
constructor() {
|
|
20
|
-
this.emitter.setMaxListeners(0); // SSE clients can be numerous
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/** Synchronous: listeners run before publish() returns. */
|
|
24
|
-
publish(event: BusEvent): void {
|
|
25
|
-
this.emitter.emit(event.taskId, event);
|
|
26
|
-
this.emitter.emit(ALL, event);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/** Subscribe to events for a specific taskId. Returns an unsubscribe
|
|
30
|
-
* function — caller MUST invoke it to release the listener; the bus
|
|
31
|
-
* retains a strong reference until then. */
|
|
32
|
-
subscribe(taskId: string, listener: Listener): () => void {
|
|
33
|
-
this.emitter.on(taskId, listener);
|
|
34
|
-
return () => this.emitter.off(taskId, listener);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/** Subscribe to ALL events. Returns an unsubscribe function with the
|
|
38
|
-
* same release semantics as subscribe(). */
|
|
39
|
-
subscribeAll(listener: Listener): () => void {
|
|
40
|
-
this.emitter.on(ALL, listener);
|
|
41
|
-
return () => this.emitter.off(ALL, listener);
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export const a2aBus = new A2ABus();
|
|
46
|
-
export type { A2ABus };
|
|
1
|
+
// server/modules/orchestration/a2a/bus.ts
|
|
2
|
+
// In-process pub/sub on top of Node's EventEmitter.
|
|
3
|
+
// Subscribers receive every event for a given taskId; an
|
|
4
|
+
// "all" subscriber receives every event regardless of task.
|
|
5
|
+
// Note: the literal taskId "__all__" is reserved for the broadcast
|
|
6
|
+
// channel; callers must not use it as a real taskId.
|
|
7
|
+
|
|
8
|
+
import { EventEmitter } from 'node:events';
|
|
9
|
+
|
|
10
|
+
import type { BusEvent } from '@/modules/orchestration/a2a/types.js';
|
|
11
|
+
|
|
12
|
+
type Listener = (event: BusEvent) => void;
|
|
13
|
+
|
|
14
|
+
const ALL = '__all__';
|
|
15
|
+
|
|
16
|
+
class A2ABus {
|
|
17
|
+
private readonly emitter = new EventEmitter();
|
|
18
|
+
|
|
19
|
+
constructor() {
|
|
20
|
+
this.emitter.setMaxListeners(0); // SSE clients can be numerous
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Synchronous: listeners run before publish() returns. */
|
|
24
|
+
publish(event: BusEvent): void {
|
|
25
|
+
this.emitter.emit(event.taskId, event);
|
|
26
|
+
this.emitter.emit(ALL, event);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Subscribe to events for a specific taskId. Returns an unsubscribe
|
|
30
|
+
* function — caller MUST invoke it to release the listener; the bus
|
|
31
|
+
* retains a strong reference until then. */
|
|
32
|
+
subscribe(taskId: string, listener: Listener): () => void {
|
|
33
|
+
this.emitter.on(taskId, listener);
|
|
34
|
+
return () => this.emitter.off(taskId, listener);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Subscribe to ALL events. Returns an unsubscribe function with the
|
|
38
|
+
* same release semantics as subscribe(). */
|
|
39
|
+
subscribeAll(listener: Listener): () => void {
|
|
40
|
+
this.emitter.on(ALL, listener);
|
|
41
|
+
return () => this.emitter.off(ALL, listener);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export const a2aBus = new A2ABus();
|
|
46
|
+
export type { A2ABus };
|
|
@@ -11,7 +11,8 @@ function buildTargetUrl(port: number, path: string): string {
|
|
|
11
11
|
|
|
12
12
|
function proxyHandler(): RequestHandler {
|
|
13
13
|
return async (req, res) => {
|
|
14
|
-
const
|
|
14
|
+
const portParam = req.params.port ?? req.params[0];
|
|
15
|
+
const rawPort = Array.isArray(portParam) ? portParam[0] : portParam;
|
|
15
16
|
const port = Number.parseInt(rawPort, 10);
|
|
16
17
|
if (!Number.isFinite(port) || port <= 0 || !getKnownPreviewPort(port)) {
|
|
17
18
|
res.status(404).json({
|
|
@@ -54,7 +55,6 @@ function proxyHandler(): RequestHandler {
|
|
|
54
55
|
|
|
55
56
|
export function createPreviewProxyRouter(): Router {
|
|
56
57
|
const router = express.Router();
|
|
57
|
-
router.use(
|
|
58
|
-
router.use('/:port/*', proxyHandler());
|
|
58
|
+
router.use(/^\/(\d+)(?:\/.*)?$/, proxyHandler());
|
|
59
59
|
return router;
|
|
60
60
|
}
|
|
@@ -1,130 +1,130 @@
|
|
|
1
|
-
import { readFile } from 'node:fs/promises';
|
|
2
|
-
import os from 'node:os';
|
|
3
|
-
import path from 'node:path';
|
|
4
|
-
|
|
5
|
-
import spawn from 'cross-spawn';
|
|
6
|
-
|
|
7
|
-
import type { IProviderAuth } from '@/shared/interfaces.js';
|
|
8
|
-
import type { ProviderAuthStatus } from '@/shared/types.js';
|
|
9
|
-
import { readObjectRecord, readOptionalString } from '@/shared/utils.js';
|
|
10
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
11
|
-
// @ts-ignore — plain-JS module, typed via inference
|
|
12
|
-
import { getProviderCredentials } from '@/services/provider-credentials.js';
|
|
13
|
-
|
|
14
|
-
type OpencodeCredentialsStatus = {
|
|
15
|
-
authenticated: boolean;
|
|
16
|
-
email: string | null;
|
|
17
|
-
method: string | null;
|
|
18
|
-
error?: string;
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* OpenCode auth checker.
|
|
23
|
-
*
|
|
24
|
-
* OpenCode stores credentials at `~/.local/share/opencode/auth.json` (XDG
|
|
25
|
-
* data dir — different layout from the other providers which use
|
|
26
|
-
* `~/.<name>/`). The file is a JSON map of `providerName → { type, ... }`
|
|
27
|
-
* where type is `api` (API key), `oauth` (OAuth tokens), or similar.
|
|
28
|
-
*
|
|
29
|
-
* Windows layout (as of the 2026 release): `%LOCALAPPDATA%\opencode\auth.json`.
|
|
30
|
-
*
|
|
31
|
-
* Since OpenCode is multi-provider (OpenAI, Anthropic, Google, Ollama,
|
|
32
|
-
* OpenCode Zen), "authenticated" here means "at least ONE provider in
|
|
33
|
-
* auth.json has credentials" — we don't care which.
|
|
34
|
-
*/
|
|
35
|
-
export class OpencodeProviderAuth implements IProviderAuth {
|
|
36
|
-
private checkInstalled(): boolean {
|
|
37
|
-
const cliPath = process.env.OPENCODE_CLI_PATH || 'opencode';
|
|
38
|
-
try {
|
|
39
|
-
const result = spawn.sync(cliPath, ['--version'], { stdio: 'ignore', timeout: 5000 });
|
|
40
|
-
return !result.error && result.status === 0;
|
|
41
|
-
} catch {
|
|
42
|
-
return false;
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
async getStatus(): Promise<ProviderAuthStatus> {
|
|
47
|
-
const installed = this.checkInstalled();
|
|
48
|
-
|
|
49
|
-
if (!installed) {
|
|
50
|
-
return {
|
|
51
|
-
installed,
|
|
52
|
-
provider: 'opencode',
|
|
53
|
-
authenticated: false,
|
|
54
|
-
email: null,
|
|
55
|
-
method: null,
|
|
56
|
-
error: 'OpenCode CLI is not installed',
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
const credentials = await this.checkCredentials();
|
|
61
|
-
|
|
62
|
-
return {
|
|
63
|
-
installed,
|
|
64
|
-
provider: 'opencode',
|
|
65
|
-
authenticated: credentials.authenticated,
|
|
66
|
-
email: credentials.email,
|
|
67
|
-
method: credentials.method,
|
|
68
|
-
error: credentials.authenticated ? undefined : credentials.error || 'Not authenticated',
|
|
69
|
-
};
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
private async checkCredentials(): Promise<OpencodeCredentialsStatus> {
|
|
73
|
-
// Pixcode-managed credentials come first — if the user stored an API
|
|
74
|
-
// key via Settings > Agents > API Key, trust that regardless of
|
|
75
|
-
// env vars or auth.json.
|
|
76
|
-
try {
|
|
77
|
-
const creds = await getProviderCredentials('opencode');
|
|
78
|
-
if (creds?.apiKey) {
|
|
79
|
-
return { authenticated: true, email: 'API Key Auth', method: 'pixcode_store' };
|
|
80
|
-
}
|
|
81
|
-
} catch { /* fall through */ }
|
|
82
|
-
|
|
83
|
-
// Env-var shortcut — any of the multi-provider keys OpenCode recognises.
|
|
84
|
-
const envKeys = ['ANTHROPIC_API_KEY', 'OPENAI_API_KEY', 'GOOGLE_API_KEY', 'OPENROUTER_API_KEY'];
|
|
85
|
-
for (const k of envKeys) {
|
|
86
|
-
if (process.env[k]?.trim()) {
|
|
87
|
-
return { authenticated: true, email: `${k.replace('_API_KEY', '')} env`, method: 'api_key' };
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
// auth.json — written by `opencode auth login`. On Windows the XDG
|
|
92
|
-
// data dir typically resolves to `%LOCALAPPDATA%\opencode`, but since
|
|
93
|
-
// Node's `os.homedir()` + `.local/share/opencode` covers the Linux
|
|
94
|
-
// default and most WSL cases, we check both paths.
|
|
95
|
-
const candidatePaths = [
|
|
96
|
-
path.join(os.homedir(), '.local', 'share', 'opencode', 'auth.json'),
|
|
97
|
-
path.join(os.homedir(), 'AppData', 'Local', 'opencode', 'auth.json'),
|
|
98
|
-
];
|
|
99
|
-
|
|
100
|
-
for (const credsPath of candidatePaths) {
|
|
101
|
-
try {
|
|
102
|
-
const content = await readFile(credsPath, 'utf8');
|
|
103
|
-
const creds = readObjectRecord(JSON.parse(content)) ?? {};
|
|
104
|
-
const providerNames = Object.keys(creds);
|
|
105
|
-
if (providerNames.length > 0) {
|
|
106
|
-
// Prefer a real provider label over a generic one when we can
|
|
107
|
-
// identify it.
|
|
108
|
-
const firstProvider = providerNames[0];
|
|
109
|
-
const firstConfig = readObjectRecord(creds[firstProvider]) ?? {};
|
|
110
|
-
const authType = readOptionalString(firstConfig.type) ?? 'stored';
|
|
111
|
-
const label = providerNames.length === 1
|
|
112
|
-
? `${firstProvider} (${authType})`
|
|
113
|
-
: `${providerNames.length} providers configured`;
|
|
114
|
-
return {
|
|
115
|
-
authenticated: true,
|
|
116
|
-
email: label,
|
|
117
|
-
method: authType === 'oauth' ? 'credentials_file' : 'api_key',
|
|
118
|
-
};
|
|
119
|
-
}
|
|
120
|
-
} catch { /* try next path */ }
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
return {
|
|
124
|
-
authenticated: false,
|
|
125
|
-
email: null,
|
|
126
|
-
method: null,
|
|
127
|
-
error: 'OpenCode is not configured — run `opencode auth login`.',
|
|
128
|
-
};
|
|
129
|
-
}
|
|
130
|
-
}
|
|
1
|
+
import { readFile } from 'node:fs/promises';
|
|
2
|
+
import os from 'node:os';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
|
|
5
|
+
import spawn from 'cross-spawn';
|
|
6
|
+
|
|
7
|
+
import type { IProviderAuth } from '@/shared/interfaces.js';
|
|
8
|
+
import type { ProviderAuthStatus } from '@/shared/types.js';
|
|
9
|
+
import { readObjectRecord, readOptionalString } from '@/shared/utils.js';
|
|
10
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
11
|
+
// @ts-ignore — plain-JS module, typed via inference
|
|
12
|
+
import { getProviderCredentials } from '@/services/provider-credentials.js';
|
|
13
|
+
|
|
14
|
+
type OpencodeCredentialsStatus = {
|
|
15
|
+
authenticated: boolean;
|
|
16
|
+
email: string | null;
|
|
17
|
+
method: string | null;
|
|
18
|
+
error?: string;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* OpenCode auth checker.
|
|
23
|
+
*
|
|
24
|
+
* OpenCode stores credentials at `~/.local/share/opencode/auth.json` (XDG
|
|
25
|
+
* data dir — different layout from the other providers which use
|
|
26
|
+
* `~/.<name>/`). The file is a JSON map of `providerName → { type, ... }`
|
|
27
|
+
* where type is `api` (API key), `oauth` (OAuth tokens), or similar.
|
|
28
|
+
*
|
|
29
|
+
* Windows layout (as of the 2026 release): `%LOCALAPPDATA%\opencode\auth.json`.
|
|
30
|
+
*
|
|
31
|
+
* Since OpenCode is multi-provider (OpenAI, Anthropic, Google, Ollama,
|
|
32
|
+
* OpenCode Zen), "authenticated" here means "at least ONE provider in
|
|
33
|
+
* auth.json has credentials" — we don't care which.
|
|
34
|
+
*/
|
|
35
|
+
export class OpencodeProviderAuth implements IProviderAuth {
|
|
36
|
+
private checkInstalled(): boolean {
|
|
37
|
+
const cliPath = process.env.OPENCODE_CLI_PATH || 'opencode';
|
|
38
|
+
try {
|
|
39
|
+
const result = spawn.sync(cliPath, ['--version'], { stdio: 'ignore', timeout: 5000 });
|
|
40
|
+
return !result.error && result.status === 0;
|
|
41
|
+
} catch {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async getStatus(): Promise<ProviderAuthStatus> {
|
|
47
|
+
const installed = this.checkInstalled();
|
|
48
|
+
|
|
49
|
+
if (!installed) {
|
|
50
|
+
return {
|
|
51
|
+
installed,
|
|
52
|
+
provider: 'opencode',
|
|
53
|
+
authenticated: false,
|
|
54
|
+
email: null,
|
|
55
|
+
method: null,
|
|
56
|
+
error: 'OpenCode CLI is not installed',
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const credentials = await this.checkCredentials();
|
|
61
|
+
|
|
62
|
+
return {
|
|
63
|
+
installed,
|
|
64
|
+
provider: 'opencode',
|
|
65
|
+
authenticated: credentials.authenticated,
|
|
66
|
+
email: credentials.email,
|
|
67
|
+
method: credentials.method,
|
|
68
|
+
error: credentials.authenticated ? undefined : credentials.error || 'Not authenticated',
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
private async checkCredentials(): Promise<OpencodeCredentialsStatus> {
|
|
73
|
+
// Pixcode-managed credentials come first — if the user stored an API
|
|
74
|
+
// key via Settings > Agents > API Key, trust that regardless of
|
|
75
|
+
// env vars or auth.json.
|
|
76
|
+
try {
|
|
77
|
+
const creds = await getProviderCredentials('opencode');
|
|
78
|
+
if (creds?.apiKey) {
|
|
79
|
+
return { authenticated: true, email: 'API Key Auth', method: 'pixcode_store' };
|
|
80
|
+
}
|
|
81
|
+
} catch { /* fall through */ }
|
|
82
|
+
|
|
83
|
+
// Env-var shortcut — any of the multi-provider keys OpenCode recognises.
|
|
84
|
+
const envKeys = ['ANTHROPIC_API_KEY', 'OPENAI_API_KEY', 'GOOGLE_API_KEY', 'OPENROUTER_API_KEY'];
|
|
85
|
+
for (const k of envKeys) {
|
|
86
|
+
if (process.env[k]?.trim()) {
|
|
87
|
+
return { authenticated: true, email: `${k.replace('_API_KEY', '')} env`, method: 'api_key' };
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// auth.json — written by `opencode auth login`. On Windows the XDG
|
|
92
|
+
// data dir typically resolves to `%LOCALAPPDATA%\opencode`, but since
|
|
93
|
+
// Node's `os.homedir()` + `.local/share/opencode` covers the Linux
|
|
94
|
+
// default and most WSL cases, we check both paths.
|
|
95
|
+
const candidatePaths = [
|
|
96
|
+
path.join(os.homedir(), '.local', 'share', 'opencode', 'auth.json'),
|
|
97
|
+
path.join(os.homedir(), 'AppData', 'Local', 'opencode', 'auth.json'),
|
|
98
|
+
];
|
|
99
|
+
|
|
100
|
+
for (const credsPath of candidatePaths) {
|
|
101
|
+
try {
|
|
102
|
+
const content = await readFile(credsPath, 'utf8');
|
|
103
|
+
const creds = readObjectRecord(JSON.parse(content)) ?? {};
|
|
104
|
+
const providerNames = Object.keys(creds);
|
|
105
|
+
if (providerNames.length > 0) {
|
|
106
|
+
// Prefer a real provider label over a generic one when we can
|
|
107
|
+
// identify it.
|
|
108
|
+
const firstProvider = providerNames[0];
|
|
109
|
+
const firstConfig = readObjectRecord(creds[firstProvider]) ?? {};
|
|
110
|
+
const authType = readOptionalString(firstConfig.type) ?? 'stored';
|
|
111
|
+
const label = providerNames.length === 1
|
|
112
|
+
? `${firstProvider} (${authType})`
|
|
113
|
+
: `${providerNames.length} providers configured`;
|
|
114
|
+
return {
|
|
115
|
+
authenticated: true,
|
|
116
|
+
email: label,
|
|
117
|
+
method: authType === 'oauth' ? 'credentials_file' : 'api_key',
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
} catch { /* try next path */ }
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
return {
|
|
124
|
+
authenticated: false,
|
|
125
|
+
email: null,
|
|
126
|
+
method: null,
|
|
127
|
+
error: 'OpenCode is not configured — run `opencode auth login`.',
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
}
|