feat: add opencode support

This commit is contained in:
Haileyesus
2026-05-13 17:43:10 +03:00
parent 10f721cf14
commit 421bdd2f0f
53 changed files with 2691 additions and 130 deletions

View File

@@ -1,52 +1,12 @@
import fs from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
import { SkillsProvider } from '@/modules/providers/shared/skills/skills.provider.js';
import type { ProviderSkillSource } from '@/shared/types.js';
const hasGitMarker = async (dirPath: string): Promise<boolean> => {
try {
const gitMarkerStats = await fs.stat(path.join(dirPath, '.git'));
return gitMarkerStats.isDirectory() || gitMarkerStats.isFile();
} catch {
return false;
}
};
const findTopmostGitRoot = async (startPath: string): Promise<string | null> => {
let currentPath = path.resolve(startPath);
let topmostGitRoot: string | null = null;
while (true) {
if (await hasGitMarker(currentPath)) {
topmostGitRoot = currentPath;
}
const parentPath = path.dirname(currentPath);
if (parentPath === currentPath) {
break;
}
currentPath = parentPath;
}
return topmostGitRoot;
};
const addUniqueSource = (
sources: ProviderSkillSource[],
seenRootDirs: Set<string>,
source: ProviderSkillSource,
): void => {
const normalizedRootDir = path.resolve(source.rootDir);
if (seenRootDirs.has(normalizedRootDir)) {
return;
}
seenRootDirs.add(normalizedRootDir);
sources.push({ ...source, rootDir: normalizedRootDir });
};
import {
addUniqueProviderSkillSource,
findTopmostGitRoot,
} from '@/shared/utils.js';
export class CodexSkillsProvider extends SkillsProvider {
constructor() {
@@ -58,7 +18,7 @@ export class CodexSkillsProvider extends SkillsProvider {
const seenRootDirs = new Set<string>();
const repoRoot = await findTopmostGitRoot(workspacePath);
addUniqueSource(sources, seenRootDirs, {
addUniqueProviderSkillSource(sources, seenRootDirs, {
scope: 'repo',
rootDir: path.join(workspacePath, '.agents', 'skills'),
commandPrefix: '$',
@@ -67,29 +27,29 @@ export class CodexSkillsProvider extends SkillsProvider {
if (repoRoot) {
// Codex checks repository skills at the launch folder, one folder above it,
// and the topmost git root; these can collapse to the same directory.
addUniqueSource(sources, seenRootDirs, {
addUniqueProviderSkillSource(sources, seenRootDirs, {
scope: 'repo',
rootDir: path.join(path.dirname(workspacePath), '.agents', 'skills'),
commandPrefix: '$',
});
addUniqueSource(sources, seenRootDirs, {
addUniqueProviderSkillSource(sources, seenRootDirs, {
scope: 'repo',
rootDir: path.join(repoRoot, '.agents', 'skills'),
commandPrefix: '$',
});
}
addUniqueSource(sources, seenRootDirs, {
addUniqueProviderSkillSource(sources, seenRootDirs, {
scope: 'user',
rootDir: path.join(os.homedir(), '.agents', 'skills'),
commandPrefix: '$',
});
addUniqueSource(sources, seenRootDirs, {
addUniqueProviderSkillSource(sources, seenRootDirs, {
scope: 'admin',
rootDir: path.join('/etc', 'codex', 'skills'),
commandPrefix: '$',
});
addUniqueSource(sources, seenRootDirs, {
addUniqueProviderSkillSource(sources, seenRootDirs, {
scope: 'system',
rootDir: path.join(os.homedir(), '.codex', 'skills', '.system'),
commandPrefix: '$',

View File

@@ -0,0 +1,111 @@
import { readFile } from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
import spawn from 'cross-spawn';
import type { IProviderAuth } from '@/shared/interfaces.js';
import type { ProviderAuthStatus } from '@/shared/types.js';
import { readObjectRecord, readOptionalString } from '@/shared/utils.js';
type OpenCodeCredentialsStatus = {
authenticated: boolean;
email: string | null;
method: string | null;
error?: string;
};
const OPENCODE_ENV_CREDENTIAL_KEYS = [
'ANTHROPIC_API_KEY',
'OPENAI_API_KEY',
'GOOGLE_GENERATIVE_AI_API_KEY',
'GEMINI_API_KEY',
'GROQ_API_KEY',
'OPENROUTER_API_KEY',
];
export class OpenCodeProviderAuth implements IProviderAuth {
/**
* Checks whether the OpenCode CLI is available to the server process.
*/
private checkInstalled(): boolean {
try {
const result = spawn.sync('opencode', ['--version'], { stdio: 'ignore', timeout: 5000 });
return !result.error && result.status === 0;
} catch {
return false;
}
}
/**
* Returns OpenCode CLI installation and credential status.
*/
async getStatus(): Promise<ProviderAuthStatus> {
const installed = this.checkInstalled();
const credentials = await this.checkCredentials();
return {
installed,
provider: 'opencode',
authenticated: credentials.authenticated,
email: credentials.email,
method: credentials.method,
error: credentials.authenticated ? undefined : credentials.error || 'Not authenticated',
};
}
/**
* Reads OpenCode's auth store or falls back to provider API key environment variables.
*/
private async checkCredentials(): Promise<OpenCodeCredentialsStatus> {
try {
const authPath = path.join(os.homedir(), '.local', 'share', 'opencode', 'auth.json');
const content = await readFile(authPath, 'utf8');
const auth = readObjectRecord(JSON.parse(content)) ?? {};
for (const [providerId, providerAuth] of Object.entries(auth)) {
const providerRecord = readObjectRecord(providerAuth);
if (!providerRecord) {
continue;
}
const hasCredential = Object.values(providerRecord).some(
(value) => readOptionalString(value) !== undefined || Boolean(readObjectRecord(value)),
);
if (hasCredential) {
return {
authenticated: true,
email: `${providerId} credentials`,
method: 'credentials_file',
};
}
}
} catch (error) {
const code = (error as NodeJS.ErrnoException).code;
if (code !== 'ENOENT') {
return {
authenticated: false,
email: null,
method: null,
error: error instanceof Error ? error.message : 'Failed to read OpenCode auth',
};
}
}
const envCredential = OPENCODE_ENV_CREDENTIAL_KEYS.find((key) => process.env[key]?.trim());
if (envCredential) {
return {
authenticated: true,
email: envCredential,
method: 'environment',
};
}
return {
authenticated: false,
email: null,
method: null,
error: 'OpenCode not configured',
};
}
}

View File

@@ -0,0 +1,228 @@
import { access, mkdir, readFile, writeFile } from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
import { McpProvider } from '@/modules/providers/shared/mcp/mcp.provider.js';
import type { McpScope, ProviderMcpServer, UpsertProviderMcpServerInput } from '@/shared/types.js';
import {
AppError,
readObjectRecord,
readOptionalString,
readStringArray,
readStringRecord,
} from '@/shared/utils.js';
type OpenCodeConfigPath = {
filePath: string;
exists: boolean;
};
const fileExists = async (filePath: string): Promise<boolean> => {
try {
await access(filePath);
return true;
} catch {
return false;
}
};
/**
* Removes JSONC comments without touching comment-like text inside strings.
*/
const stripJsonComments = (content: string): string => {
let output = '';
let inString = false;
let quote = '';
let escaped = false;
for (let index = 0; index < content.length; index += 1) {
const char = content[index];
const next = content[index + 1];
if (inString) {
output += char;
if (escaped) {
escaped = false;
} else if (char === '\\') {
escaped = true;
} else if (char === quote) {
inString = false;
quote = '';
}
continue;
}
if (char === '"' || char === '\'') {
inString = true;
quote = char;
output += char;
continue;
}
if (char === '/' && next === '/') {
while (index < content.length && content[index] !== '\n') {
index += 1;
}
output += '\n';
continue;
}
if (char === '/' && next === '*') {
index += 2;
while (index < content.length && !(content[index] === '*' && content[index + 1] === '/')) {
index += 1;
}
index += 1;
continue;
}
output += char;
}
return output;
};
const stripTrailingCommas = (content: string): string =>
content.replace(/,\s*([}\]])/g, '$1');
const readOpenCodeConfig = async (filePath: string): Promise<Record<string, unknown>> => {
try {
const content = await readFile(filePath, 'utf8');
const parsed = JSON.parse(stripTrailingCommas(stripJsonComments(content))) as unknown;
return readObjectRecord(parsed) ?? {};
} catch (error) {
const code = (error as NodeJS.ErrnoException).code;
if (code === 'ENOENT') {
return {};
}
throw error;
}
};
const writeOpenCodeConfig = async (filePath: string, data: Record<string, unknown>): Promise<void> => {
await mkdir(path.dirname(filePath), { recursive: true });
await writeFile(filePath, `${JSON.stringify(data, null, 2)}\n`, 'utf8');
};
const resolveOpenCodeConfigPath = async (scope: McpScope, workspacePath: string): Promise<OpenCodeConfigPath> => {
const root = scope === 'user'
? path.join(os.homedir(), '.config', 'opencode')
: workspacePath;
const jsonPath = path.join(root, 'opencode.json');
const jsoncPath = path.join(root, 'opencode.jsonc');
if (await fileExists(jsonPath)) {
return { filePath: jsonPath, exists: true };
}
if (await fileExists(jsoncPath)) {
return { filePath: jsoncPath, exists: true };
}
return { filePath: jsonPath, exists: false };
};
export class OpenCodeMcpProvider extends McpProvider {
constructor() {
super('opencode', ['user', 'project'], ['stdio', 'http']);
}
protected async readScopedServers(scope: McpScope, workspacePath: string): Promise<Record<string, unknown>> {
const { filePath } = await resolveOpenCodeConfigPath(scope, workspacePath);
const config = await readOpenCodeConfig(filePath);
return readObjectRecord(config.mcp) ?? {};
}
protected async writeScopedServers(
scope: McpScope,
workspacePath: string,
servers: Record<string, unknown>,
): Promise<void> {
const { filePath } = await resolveOpenCodeConfigPath(scope, workspacePath);
const config = await readOpenCodeConfig(filePath);
config.mcp = servers;
await writeOpenCodeConfig(filePath, config);
}
protected buildServerConfig(input: UpsertProviderMcpServerInput): Record<string, unknown> {
if (input.transport === 'stdio') {
if (!input.command?.trim()) {
throw new AppError('command is required for stdio MCP servers.', {
code: 'MCP_COMMAND_REQUIRED',
statusCode: 400,
});
}
return {
type: 'local',
command: [input.command, ...(input.args ?? [])],
enabled: true,
environment: input.env ?? {},
};
}
if (!input.url?.trim()) {
throw new AppError('url is required for http MCP servers.', {
code: 'MCP_URL_REQUIRED',
statusCode: 400,
});
}
return {
type: 'remote',
url: input.url,
enabled: true,
headers: input.headers ?? {},
};
}
protected normalizeServerConfig(
scope: McpScope,
name: string,
rawConfig: unknown,
): ProviderMcpServer | null {
const config = readObjectRecord(rawConfig);
if (!config) {
return null;
}
if (config.type === 'local' || config.command !== undefined) {
const commandParts = typeof config.command === 'string'
? [config.command, ...(readStringArray(config.args) ?? [])]
: readStringArray(config.command);
const command = commandParts?.[0];
if (!command) {
return null;
}
return {
provider: 'opencode',
name,
scope,
transport: 'stdio',
command,
args: commandParts.slice(1),
env: readStringRecord(config.environment) ?? readStringRecord(config.env),
};
}
if (config.type === 'remote' || typeof config.url === 'string') {
const url = readOptionalString(config.url);
if (!url) {
return null;
}
return {
provider: 'opencode',
name,
scope,
transport: 'http',
url,
headers: readStringRecord(config.headers),
};
}
return null;
}
}

View File

@@ -0,0 +1,157 @@
import fsSync from 'node:fs';
import path from 'node:path';
import Database from 'better-sqlite3';
import { sessionsDb } from '@/modules/database/index.js';
import type { IProviderSessionSynchronizer } from '@/shared/interfaces.js';
import {
getOpenCodeDatabasePath,
normalizeProviderTimestamp,
normalizeSessionName,
readJsonRecord,
readOptionalString,
} from '@/shared/utils.js';
type OpenCodeSessionRow = {
id: string;
directory: string | null;
title: string | null;
time_created: number | null;
time_updated: number | null;
worktree: string | null;
};
type SynchronizeRowsResult = {
processed: number;
firstSessionId: string | null;
};
/**
* Session indexer for OpenCode's SQLite-backed session store.
*/
export class OpenCodeSessionSynchronizer implements IProviderSessionSynchronizer {
private readonly provider = 'opencode' as const;
/**
* Scans OpenCode's shared opencode.db and upserts active sessions into DB.
*/
async synchronize(since?: Date): Promise<number> {
const result = this.synchronizeRows(since);
return result.processed;
}
/**
* Handles watcher changes for opencode.db.
*/
async synchronizeFile(filePath: string): Promise<string | null> {
if (path.basename(filePath) !== 'opencode.db') {
return null;
}
const result = this.synchronizeRows(undefined, 1);
return result.firstSessionId;
}
private synchronizeRows(since?: Date, limit?: number): SynchronizeRowsResult {
const dbPath = getOpenCodeDatabasePath();
if (!fsSync.existsSync(dbPath)) {
return { processed: 0, firstSessionId: null };
}
const db = new Database(dbPath, { readonly: true, fileMustExist: true });
try {
const sinceMillis = since?.getTime() ?? null;
const limitClause = limit ? 'LIMIT ?' : '';
const params = limit ? [sinceMillis, sinceMillis, limit] : [sinceMillis, sinceMillis];
const rows = db.prepare(`
SELECT
s.id AS id,
s.directory AS directory,
s.title AS title,
s.time_created AS time_created,
s.time_updated AS time_updated,
p.worktree AS worktree
FROM session s
LEFT JOIN project p ON p.id = s.project_id
WHERE s.time_archived IS NULL
AND (? IS NULL OR COALESCE(s.time_updated, s.time_created, 0) >= ?)
ORDER BY COALESCE(s.time_updated, s.time_created, 0) DESC, s.id DESC
${limitClause}
`).all(...params) as OpenCodeSessionRow[];
let processed = 0;
let firstSessionId: string | null = null;
for (const row of rows) {
const indexedSessionId = this.upsertSession(db, row);
if (!indexedSessionId) {
continue;
}
if (!firstSessionId) {
firstSessionId = indexedSessionId;
}
processed += 1;
}
return { processed, firstSessionId };
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
console.warn('[OpenCodeProvider] Failed to synchronize sessions:', message);
return { processed: 0, firstSessionId: null };
} finally {
db.close();
}
}
private upsertSession(db: Database.Database, row: OpenCodeSessionRow): string | null {
const sessionId = readOptionalString(row.id);
const projectPath = readOptionalString(row.directory) ?? readOptionalString(row.worktree);
if (!sessionId || !projectPath) {
return null;
}
const fallbackTitle = 'Untitled OpenCode Session';
const existingSession = sessionsDb.getSessionById(sessionId);
const existingName = existingSession?.custom_name;
const nextName = existingName && existingName !== fallbackTitle
? existingName
: readOptionalString(row.title) ?? this.readFirstUserText(db, sessionId);
// OpenCode stores every session in one shared sqlite database, so jsonl_path
// must stay null to avoid deleting opencode.db when one app session is removed.
sessionsDb.createSession(
sessionId,
this.provider,
projectPath,
normalizeSessionName(nextName, fallbackTitle),
normalizeProviderTimestamp(row.time_created),
normalizeProviderTimestamp(row.time_updated ?? row.time_created),
null,
);
return sessionId;
}
private readFirstUserText(db: Database.Database, sessionId: string): string | undefined {
try {
const row = db.prepare(`
SELECT p.data AS data
FROM message m
INNER JOIN part p
ON p.session_id = m.session_id
AND p.message_id = m.id
WHERE m.session_id = ?
AND json_extract(m.data, '$.role') = 'user'
AND json_extract(p.data, '$.type') = 'text'
ORDER BY COALESCE(m.time_created, 0), COALESCE(p.time_created, 0)
LIMIT 1
`).get(sessionId) as { data: string | null } | undefined;
const data = readJsonRecord(row?.data);
return readOptionalString(data?.text);
} catch {
return undefined;
}
}
}

View File

@@ -0,0 +1,426 @@
import fsSync from 'node:fs';
import Database from 'better-sqlite3';
import type { IProviderSessions } from '@/shared/interfaces.js';
import type { AnyRecord, FetchHistoryOptions, FetchHistoryResult, NormalizedMessage } from '@/shared/types.js';
import {
createNormalizedMessage,
generateMessageId,
getOpenCodeDatabasePath,
normalizeProviderTimestamp,
readObjectRecord,
readJsonRecord,
readOptionalString,
} from '@/shared/utils.js';
const PROVIDER = 'opencode';
type OpenCodeHistoryRow = {
message_id: string;
message_time_created: number | null;
message_data: string | null;
part_id: string | null;
part_time_created: number | null;
part_data: string | null;
};
type OpenCodeTokenTotals = {
inputTokens: number;
outputTokens: number;
cacheReadTokens: number;
cacheCreationTokens: number;
reasoningTokens: number;
};
const openOpenCodeDatabase = (): Database.Database | null => {
const dbPath = getOpenCodeDatabasePath();
if (!fsSync.existsSync(dbPath)) {
return null;
}
return new Database(dbPath, { readonly: true, fileMustExist: true });
};
const formatToolContent = (value: unknown): string => {
if (value === undefined || value === null) {
return '';
}
if (typeof value === 'string') {
return value;
}
try {
return JSON.stringify(value, null, 2);
} catch {
return String(value);
}
};
const extractText = (value: unknown): string => {
if (typeof value === 'string') {
return value;
}
const record = readObjectRecord(value);
return readOptionalString(record?.text)
?? readOptionalString(record?.content)
?? '';
};
const buildTokenUsage = (totals: OpenCodeTokenTotals | undefined): AnyRecord | undefined => {
if (!totals) {
return undefined;
}
const inputTokens = totals.inputTokens;
const outputTokens = totals.outputTokens;
const cacheReadTokens = totals.cacheReadTokens;
const cacheCreationTokens = totals.cacheCreationTokens;
const reasoningTokens = totals.reasoningTokens;
const used = inputTokens + outputTokens + cacheReadTokens + cacheCreationTokens + reasoningTokens;
if (used <= 0) {
return undefined;
}
return {
used,
total: used,
inputTokens,
outputTokens,
cacheReadTokens,
cacheCreationTokens,
};
};
/**
* OpenCode stores per-message token counts on assistant `message.data` objects
* (see MessageV2.Assistant). Older DBs also had session-level counters; this
* matches current `opencode.db` layouts that only persist message JSON.
*/
const aggregateOpenCodeSessionTokenUsage = (
db: Database.Database,
sessionId: string,
): AnyRecord | undefined => {
const rows = db.prepare('SELECT data FROM message WHERE session_id = ?').all(sessionId) as { data: string }[];
let inputTokens = 0;
let outputTokens = 0;
let cacheReadTokens = 0;
let cacheCreationTokens = 0;
let reasoningTokens = 0;
for (const row of rows) {
const info = readJsonRecord(row.data);
if (readOptionalString(info?.role) !== 'assistant') {
continue;
}
const tokens = readObjectRecord(info?.tokens);
if (!tokens) {
continue;
}
inputTokens += Number(tokens.input ?? 0);
outputTokens += Number(tokens.output ?? 0);
reasoningTokens += Number(tokens.reasoning ?? 0);
const cache = readObjectRecord(tokens.cache);
cacheReadTokens += Number(cache?.read ?? 0);
cacheCreationTokens += Number(cache?.write ?? 0);
}
return buildTokenUsage({
inputTokens,
outputTokens,
cacheReadTokens,
cacheCreationTokens,
reasoningTokens,
});
};
export class OpenCodeSessionsProvider implements IProviderSessions {
/**
* Normalizes live `opencode run --format json` events into frontend messages.
*/
normalizeMessage(rawMessage: unknown, sessionId: string | null): NormalizedMessage[] {
const raw = readObjectRecord(rawMessage);
if (!raw) {
return [];
}
const type = readOptionalString(raw.type) ?? readOptionalString(raw.event);
const eventSessionId = readOptionalString(raw.sessionID) ?? readOptionalString(raw.sessionId) ?? sessionId;
const timestamp = normalizeProviderTimestamp(raw.time ?? raw.timestamp);
const baseId = readOptionalString(raw.id)
?? readOptionalString(raw.messageID)
?? generateMessageId('opencode');
if (type === 'text') {
const content = extractText(raw.text ?? raw.delta ?? raw.message);
if (!content.trim()) {
return [];
}
return [createNormalizedMessage({
id: baseId,
sessionId: eventSessionId,
timestamp,
provider: PROVIDER,
kind: 'stream_delta',
content,
})];
}
if (type === 'reasoning') {
const content = extractText(raw.text ?? raw.delta ?? raw.message);
if (!content.trim()) {
return [];
}
return [createNormalizedMessage({
id: baseId,
sessionId: eventSessionId,
timestamp,
provider: PROVIDER,
kind: 'thinking',
content,
})];
}
if (type === 'tool_use') {
const toolName = readOptionalString(raw.tool) ?? readOptionalString(raw.name) ?? 'Tool';
const toolId = readOptionalString(raw.callID) ?? readOptionalString(raw.toolCallId) ?? baseId;
const toolMessage = createNormalizedMessage({
id: baseId,
sessionId: eventSessionId,
timestamp,
provider: PROVIDER,
kind: 'tool_use',
toolName,
toolInput: raw.input ?? raw.arguments ?? {},
toolId,
});
if (raw.output !== undefined || raw.error !== undefined) {
toolMessage.toolResult = {
content: formatToolContent(raw.output ?? raw.error),
isError: raw.error !== undefined,
};
}
return [toolMessage];
}
if (type === 'error') {
return [createNormalizedMessage({
id: baseId,
sessionId: eventSessionId,
timestamp,
provider: PROVIDER,
kind: 'error',
content: readOptionalString(raw.error) ?? readOptionalString(raw.message) ?? 'Unknown OpenCode error',
})];
}
if (type === 'step_finish') {
return [createNormalizedMessage({
id: baseId,
sessionId: eventSessionId,
timestamp,
provider: PROVIDER,
kind: 'stream_end',
})];
}
return [];
}
/**
* Loads OpenCode history from the shared SQLite session database.
*/
async fetchHistory(
sessionId: string,
options: FetchHistoryOptions = {},
): Promise<FetchHistoryResult> {
const { limit = null, offset = 0 } = options;
const db = openOpenCodeDatabase();
if (!db) {
return { messages: [], total: 0, hasMore: false, offset: 0, limit: null };
}
try {
const rows = db.prepare(`
SELECT
m.id AS message_id,
m.time_created AS message_time_created,
m.data AS message_data,
p.id AS part_id,
p.time_created AS part_time_created,
p.data AS part_data
FROM message m
LEFT JOIN part p
ON p.session_id = m.session_id
AND p.message_id = m.id
WHERE m.session_id = ?
ORDER BY
COALESCE(m.time_created, 0),
m.id,
COALESCE(p.time_created, 0),
p.id
`).all(sessionId) as OpenCodeHistoryRow[];
const normalized = this.normalizeHistoryRows(rows, sessionId);
const tokenUsage = aggregateOpenCodeSessionTokenUsage(db, sessionId);
const normalizedOffset = Math.max(0, offset);
const normalizedLimit = limit === null ? null : Math.max(0, limit);
const total = normalized.length;
const messages = normalizedLimit === null
? normalized
: normalized.slice(
Math.max(0, total - normalizedOffset - normalizedLimit),
Math.max(0, total - normalizedOffset),
);
return {
messages,
total,
hasMore: normalizedLimit === null
? false
: Math.max(0, total - normalizedOffset - normalizedLimit) > 0,
offset: normalizedOffset,
limit: normalizedLimit,
tokenUsage,
};
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
console.warn(`[OpenCodeProvider] Failed to load session ${sessionId}:`, message);
return { messages: [], total: 0, hasMore: false, offset: 0, limit: null };
} finally {
db.close();
}
}
private normalizeHistoryRows(rows: OpenCodeHistoryRow[], sessionId: string): NormalizedMessage[] {
const normalized: NormalizedMessage[] = [];
const emittedMessageErrors = new Set<string>();
for (const row of rows) {
const timestamp = normalizeProviderTimestamp(row.part_time_created ?? row.message_time_created);
const baseId = `${row.message_id}_${row.part_id ?? normalized.length}`;
const messageInfo = readJsonRecord(row.message_data);
const messageRole = readOptionalString(messageInfo?.role);
if (
messageInfo
&& messageRole === 'assistant'
&& messageInfo.error != null
&& !emittedMessageErrors.has(row.message_id)
) {
emittedMessageErrors.add(row.message_id);
normalized.push(createNormalizedMessage({
id: `${baseId}_error`,
sessionId,
timestamp,
provider: PROVIDER,
kind: 'error',
content: formatToolContent(messageInfo.error),
}));
}
if (!row.part_id) {
continue;
}
const partData = readJsonRecord(row.part_data) ?? {};
const partType = readOptionalString(partData.type);
if (!partType) {
continue;
}
if (partType === 'text') {
const content = extractText(partData);
if (content.trim()) {
normalized.push(createNormalizedMessage({
id: baseId,
sessionId,
timestamp,
provider: PROVIDER,
kind: 'text',
role: messageRole === 'user' ? 'user' : 'assistant',
content,
}));
}
continue;
}
if (partType === 'reasoning') {
const content = extractText(partData);
if (content.trim()) {
normalized.push(createNormalizedMessage({
id: baseId,
sessionId,
timestamp,
provider: PROVIDER,
kind: 'thinking',
content,
}));
}
continue;
}
if (partType === 'tool') {
const state = readObjectRecord(partData.state) ?? {};
const status = readOptionalString(state.status);
const toolMessage = createNormalizedMessage({
id: baseId,
sessionId,
timestamp,
provider: PROVIDER,
kind: 'tool_use',
toolName: readOptionalString(partData.tool) ?? 'Tool',
toolInput: state.input ?? partData.input ?? {},
toolId: readOptionalString(partData.callID) ?? row.part_id,
});
if (status === 'completed' || status === 'error') {
toolMessage.toolResult = {
content: formatToolContent(state.output ?? state.error),
isError: status === 'error',
};
}
normalized.push(toolMessage);
continue;
}
if (partType === 'step-finish') {
normalized.push(createNormalizedMessage({
id: baseId,
sessionId,
timestamp,
provider: PROVIDER,
kind: 'stream_end',
}));
continue;
}
if (partType === 'patch' || partType === 'agent') {
normalized.push(createNormalizedMessage({
id: baseId,
sessionId,
timestamp,
provider: PROVIDER,
kind: 'tool_use',
toolName: partType === 'patch' ? 'Patch' : 'Agent',
toolInput: partData,
toolId: row.part_id,
}));
}
}
return normalized;
}
}

View File

@@ -0,0 +1,78 @@
import os from 'node:os';
import path from 'node:path';
import { SkillsProvider } from '@/modules/providers/shared/skills/skills.provider.js';
import type { ProviderSkillSource } from '@/shared/types.js';
import {
addUniqueProviderSkillSource,
findTopmostGitRoot,
} from '@/shared/utils.js';
const OPENCODE_PROJECT_SKILL_DIRS = [
['.opencode', 'skills'],
['.claude', 'skills'],
['.agents', 'skills'],
];
const OPENCODE_USER_SKILL_DIRS = [
['.config', 'opencode', 'skills'],
['.claude', 'skills'],
['.agents', 'skills'],
];
export class OpenCodeSkillsProvider extends SkillsProvider {
constructor() {
super('opencode');
}
protected async getSkillSources(workspacePath: string): Promise<ProviderSkillSource[]> {
const sources: ProviderSkillSource[] = [];
const seenRootDirs = new Set<string>();
const repoRoot = await findTopmostGitRoot(workspacePath);
for (const projectRoot of this.getProjectSearchRoots(workspacePath, repoRoot)) {
for (const skillDir of OPENCODE_PROJECT_SKILL_DIRS) {
// OpenCode intentionally reads Claude and Agents skill folders so users
// can reuse the same skill libraries across compatible coding agents.
addUniqueProviderSkillSource(sources, seenRootDirs, {
scope: 'project',
rootDir: path.join(projectRoot, ...skillDir),
commandPrefix: '/',
});
}
}
for (const skillDir of OPENCODE_USER_SKILL_DIRS) {
addUniqueProviderSkillSource(sources, seenRootDirs, {
scope: 'user',
rootDir: path.join(os.homedir(), ...skillDir),
commandPrefix: '/',
});
}
return sources;
}
private getProjectSearchRoots(workspacePath: string, repoRoot: string | null): string[] {
const roots: string[] = [];
const normalizedWorkspacePath = path.resolve(workspacePath);
const normalizedRepoRoot = repoRoot ? path.resolve(repoRoot) : null;
let currentPath = normalizedWorkspacePath;
while (true) {
roots.push(currentPath);
if (!normalizedRepoRoot || currentPath === normalizedRepoRoot) {
break;
}
const parentPath = path.dirname(currentPath);
if (parentPath === currentPath) {
break;
}
currentPath = parentPath;
}
return roots;
}
}

View File

@@ -0,0 +1,24 @@
import { OpenCodeProviderAuth } from '@/modules/providers/list/opencode/opencode-auth.provider.js';
import { OpenCodeMcpProvider } from '@/modules/providers/list/opencode/opencode-mcp.provider.js';
import { OpenCodeSessionSynchronizer } from '@/modules/providers/list/opencode/opencode-session-synchronizer.provider.js';
import { OpenCodeSessionsProvider } from '@/modules/providers/list/opencode/opencode-sessions.provider.js';
import { OpenCodeSkillsProvider } from '@/modules/providers/list/opencode/opencode-skills.provider.js';
import { AbstractProvider } from '@/modules/providers/shared/base/abstract.provider.js';
import type {
IProviderAuth,
IProviderSessionSynchronizer,
IProviderSkills,
IProviderSessions,
} from '@/shared/interfaces.js';
export class OpenCodeProvider extends AbstractProvider {
readonly mcp = new OpenCodeMcpProvider();
readonly auth: IProviderAuth = new OpenCodeProviderAuth();
readonly skills: IProviderSkills = new OpenCodeSkillsProvider();
readonly sessions: IProviderSessions = new OpenCodeSessionsProvider();
readonly sessionSynchronizer: IProviderSessionSynchronizer = new OpenCodeSessionSynchronizer();
constructor() {
super('opencode');
}
}