mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 20:34:18 +01:00
437 lines
14 KiB
TypeScript
437 lines
14 KiB
TypeScript
import type {
|
|
SettingsResponse,
|
|
UpdateSettingsRequest,
|
|
OpenCodeConfig,
|
|
OpenCodeConfigResponse,
|
|
CreateOpenCodeConfigRequest,
|
|
UpdateOpenCodeConfigRequest,
|
|
OpenCodeImportStatus,
|
|
SyncOpenCodeImportResponse,
|
|
SkillFileInfo,
|
|
CreateSkillRequest,
|
|
UpdateSkillRequest,
|
|
SkillScope,
|
|
InstallSkillFromGithubRequest,
|
|
InstallSkillResponse,
|
|
OpenCodeDirectoryFileInfo,
|
|
ReplaceOpenCodeConfigDirectoryResponse,
|
|
} from './types/settings'
|
|
import { API_BASE_URL } from '@/config'
|
|
import { fetchWrapper, FetchError } from './fetchWrapper'
|
|
import type { DirectoryUploadItem } from '@/lib/directoryUpload'
|
|
|
|
const DEFAULT_USER_ID = 'default'
|
|
|
|
function appendUploadItemsWithManifest(formData: FormData, items: DirectoryUploadItem[]): void {
|
|
const fileManifest: Array<{ fieldName: string; relativePath: string }> = []
|
|
|
|
items.forEach((item, index) => {
|
|
const fieldName = `file${index}`
|
|
fileManifest.push({ fieldName, relativePath: item.relativePath })
|
|
formData.append(fieldName, item.file)
|
|
})
|
|
|
|
formData.append('fileManifest', JSON.stringify(fileManifest))
|
|
}
|
|
|
|
export const settingsApi = {
|
|
getSettings: async (userId = DEFAULT_USER_ID): Promise<SettingsResponse> => {
|
|
return fetchWrapper(`${API_BASE_URL}/api/settings`, {
|
|
params: { userId },
|
|
})
|
|
},
|
|
|
|
updateSettings: async (
|
|
updates: UpdateSettingsRequest,
|
|
userId = DEFAULT_USER_ID
|
|
): Promise<SettingsResponse> => {
|
|
return fetchWrapper(`${API_BASE_URL}/api/settings`, {
|
|
method: 'PATCH',
|
|
params: { userId },
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(updates),
|
|
})
|
|
},
|
|
|
|
resetSettings: async (userId = DEFAULT_USER_ID): Promise<SettingsResponse> => {
|
|
return fetchWrapper(`${API_BASE_URL}/api/settings`, {
|
|
method: 'DELETE',
|
|
params: { userId },
|
|
})
|
|
},
|
|
|
|
getOpenCodeConfigs: async (userId = DEFAULT_USER_ID): Promise<OpenCodeConfigResponse> => {
|
|
return fetchWrapper(`${API_BASE_URL}/api/settings/opencode-configs`, {
|
|
params: { userId },
|
|
})
|
|
},
|
|
|
|
createOpenCodeConfig: async (
|
|
request: CreateOpenCodeConfigRequest,
|
|
userId = DEFAULT_USER_ID
|
|
): Promise<OpenCodeConfig> => {
|
|
return fetchWrapper(`${API_BASE_URL}/api/settings/opencode-configs`, {
|
|
method: 'POST',
|
|
params: { userId },
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(request),
|
|
})
|
|
},
|
|
|
|
updateOpenCodeConfig: async (
|
|
configName: string,
|
|
request: UpdateOpenCodeConfigRequest,
|
|
userId = DEFAULT_USER_ID
|
|
): Promise<OpenCodeConfig> => {
|
|
return fetchWrapper(
|
|
`${API_BASE_URL}/api/settings/opencode-configs/${encodeURIComponent(configName)}`,
|
|
{
|
|
method: 'PUT',
|
|
params: { userId },
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(request),
|
|
}
|
|
)
|
|
},
|
|
|
|
deleteOpenCodeConfig: async (
|
|
configName: string,
|
|
userId = DEFAULT_USER_ID
|
|
): Promise<boolean> => {
|
|
await fetchWrapper(
|
|
`${API_BASE_URL}/api/settings/opencode-configs/${encodeURIComponent(configName)}`,
|
|
{
|
|
method: 'DELETE',
|
|
params: { userId },
|
|
}
|
|
)
|
|
return true
|
|
},
|
|
|
|
setDefaultOpenCodeConfig: async (
|
|
configName: string,
|
|
userId = DEFAULT_USER_ID
|
|
): Promise<OpenCodeConfig> => {
|
|
return fetchWrapper(
|
|
`${API_BASE_URL}/api/settings/opencode-configs/${encodeURIComponent(configName)}/set-default`,
|
|
{
|
|
method: 'POST',
|
|
params: { userId },
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({}),
|
|
}
|
|
)
|
|
},
|
|
|
|
getDefaultOpenCodeConfig: async (userId = DEFAULT_USER_ID): Promise<OpenCodeConfig | null> => {
|
|
try {
|
|
return fetchWrapper(`${API_BASE_URL}/api/settings/opencode-configs/default`, {
|
|
params: { userId },
|
|
})
|
|
} catch {
|
|
return null
|
|
}
|
|
},
|
|
|
|
discoverOpenCodeModels: async (
|
|
baseUrl: string,
|
|
apiKey?: string,
|
|
forceRefresh = false,
|
|
): Promise<{ models: string[]; cached: boolean }> => {
|
|
const params: Record<string, string> = { baseUrl }
|
|
if (apiKey) params.apiKey = apiKey
|
|
if (forceRefresh) params.refresh = 'true'
|
|
return fetchWrapper(`${API_BASE_URL}/api/settings/opencode-discover-models`, {
|
|
params,
|
|
})
|
|
},
|
|
|
|
restartOpenCodeServer: async (): Promise<{ success: boolean; message: string; details?: string }> => {
|
|
return fetchWrapper(`${API_BASE_URL}/api/settings/opencode-restart`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
})
|
|
},
|
|
|
|
getActiveOpenCodeSessions: async (): Promise<{ count: number; sessions: { sessionID: string; directory: string }[] }> => {
|
|
return fetchWrapper(`${API_BASE_URL}/api/settings/opencode-active-sessions`)
|
|
},
|
|
|
|
reloadOpenCodeConfig: async (): Promise<{ success: boolean; message: string; details?: string }> => {
|
|
try {
|
|
return fetchWrapper(`${API_BASE_URL}/api/settings/opencode-reload`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
})
|
|
} catch (error) {
|
|
if (error instanceof FetchError && error.statusCode === 404) {
|
|
return fetchWrapper(`${API_BASE_URL}/api/settings/opencode-restart`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
})
|
|
}
|
|
throw error
|
|
}
|
|
},
|
|
|
|
rollbackOpenCodeConfig: async (): Promise<{ success: boolean; message: string; configName?: string }> => {
|
|
return fetchWrapper(`${API_BASE_URL}/api/settings/opencode-rollback`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
})
|
|
},
|
|
|
|
getOpenCodeImportStatus: async (): Promise<OpenCodeImportStatus> => {
|
|
return fetchWrapper(`${API_BASE_URL}/api/settings/opencode-import/status`)
|
|
},
|
|
|
|
syncOpenCodeImport: async (overwriteState = false): Promise<SyncOpenCodeImportResponse> => {
|
|
return fetchWrapper(`${API_BASE_URL}/api/settings/opencode-import`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ overwriteState }),
|
|
})
|
|
},
|
|
|
|
getOpenCodeVersions: async (): Promise<{
|
|
versions: Array<{
|
|
version: string
|
|
tag: string
|
|
name: string
|
|
publishedAt: string
|
|
}>
|
|
currentVersion: string | null
|
|
}> => {
|
|
return fetchWrapper(`${API_BASE_URL}/api/settings/opencode-versions`)
|
|
},
|
|
|
|
installOpenCodeVersion: async (version: string): Promise<{
|
|
success: boolean
|
|
message: string
|
|
oldVersion?: string
|
|
newVersion?: string
|
|
recovered?: boolean
|
|
recoveryMessage?: string
|
|
}> => {
|
|
return fetchWrapper(`${API_BASE_URL}/api/settings/opencode-install-version`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ version }),
|
|
})
|
|
},
|
|
|
|
upgradeOpenCode: async (): Promise<{
|
|
success: boolean
|
|
message: string
|
|
oldVersion?: string
|
|
newVersion?: string
|
|
upgraded: boolean
|
|
recovered?: boolean
|
|
recoveryMessage?: string
|
|
}> => {
|
|
return fetchWrapper(`${API_BASE_URL}/api/settings/opencode-upgrade`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
})
|
|
},
|
|
|
|
testSSHConnection: async (host: string, sshPrivateKey: string, passphrase?: string): Promise<{ success: boolean; message: string }> => {
|
|
return fetchWrapper(`${API_BASE_URL}/api/settings/test-ssh`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ host, sshPrivateKey, passphrase }),
|
|
})
|
|
},
|
|
|
|
getAgentsMd: async (): Promise<{ content: string }> => {
|
|
return fetchWrapper(`${API_BASE_URL}/api/settings/agents-md`)
|
|
},
|
|
|
|
getDefaultAgentsMd: async (): Promise<{ content: string }> => {
|
|
return fetchWrapper(`${API_BASE_URL}/api/settings/agents-md/default`)
|
|
},
|
|
|
|
updateAgentsMd: async (content: string): Promise<{ success: boolean }> => {
|
|
return fetchWrapper(`${API_BASE_URL}/api/settings/agents-md`, {
|
|
method: 'PUT',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ content }),
|
|
})
|
|
},
|
|
|
|
getVersionInfo: async (): Promise<VersionInfo> => {
|
|
return fetchWrapper(`${API_BASE_URL}/api/health/version`)
|
|
},
|
|
|
|
listManagedSkills: async (repoId?: number, directory?: string): Promise<SkillFileInfo[]> => {
|
|
const searchParams = new URLSearchParams()
|
|
if (repoId) searchParams.set('repoId', String(repoId))
|
|
if (directory) searchParams.set('directory', directory)
|
|
const query = searchParams.toString() ? `?${searchParams.toString()}` : ''
|
|
return fetchWrapper(`${API_BASE_URL}/api/settings/skills${query}`)
|
|
},
|
|
|
|
getSkill: async (name: string, scope: SkillScope, repoId?: number): Promise<SkillFileInfo> => {
|
|
const params = new URLSearchParams({ scope })
|
|
if (repoId) params.set('repoId', String(repoId))
|
|
return fetchWrapper(`${API_BASE_URL}/api/settings/skills/${name}?${params}`)
|
|
},
|
|
|
|
createSkill: async (data: CreateSkillRequest): Promise<SkillFileInfo> => {
|
|
return fetchWrapper(`${API_BASE_URL}/api/settings/skills`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(data),
|
|
})
|
|
},
|
|
|
|
updateSkill: async (name: string, scope: SkillScope, data: UpdateSkillRequest, repoId?: number): Promise<SkillFileInfo> => {
|
|
const params = new URLSearchParams({ scope })
|
|
if (repoId) params.set('repoId', String(repoId))
|
|
return fetchWrapper(`${API_BASE_URL}/api/settings/skills/${name}?${params}`, {
|
|
method: 'PUT',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(data),
|
|
})
|
|
},
|
|
|
|
deleteSkill: async (name: string, scope: SkillScope, repoId?: number): Promise<{ success: boolean }> => {
|
|
const params = new URLSearchParams({ scope })
|
|
if (repoId) params.set('repoId', String(repoId))
|
|
return fetchWrapper(`${API_BASE_URL}/api/settings/skills/${name}?${params}`, {
|
|
method: 'DELETE',
|
|
})
|
|
},
|
|
|
|
installSkillFromGithub: async (data: InstallSkillFromGithubRequest): Promise<InstallSkillResponse> => {
|
|
return fetchWrapper(`${API_BASE_URL}/api/settings/skills/install`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(data),
|
|
})
|
|
},
|
|
|
|
installSkillFromUpload: async (data: {
|
|
items: DirectoryUploadItem[]
|
|
scope: SkillScope
|
|
repoId?: number
|
|
overwrite?: boolean
|
|
}): Promise<InstallSkillResponse> => {
|
|
const formData = new FormData()
|
|
formData.append('sourceType', 'upload')
|
|
formData.append('scope', data.scope)
|
|
if (data.repoId !== undefined) formData.append('repoId', String(data.repoId))
|
|
if (data.overwrite !== undefined) formData.append('overwrite', String(data.overwrite))
|
|
|
|
appendUploadItemsWithManifest(formData, data.items)
|
|
|
|
return fetchWrapper(`${API_BASE_URL}/api/settings/skills/install`, {
|
|
method: 'POST',
|
|
body: formData,
|
|
})
|
|
},
|
|
|
|
installOpenCodeDirectoryFiles: async (data: {
|
|
kind: 'agents' | 'commands'
|
|
items: DirectoryUploadItem[]
|
|
}): Promise<{ kind: 'agents' | 'commands'; filesInstalled: string[] }> => {
|
|
const formData = new FormData()
|
|
formData.append('kind', data.kind)
|
|
|
|
appendUploadItemsWithManifest(formData, data.items)
|
|
|
|
return fetchWrapper(`${API_BASE_URL}/api/settings/opencode-directory-files/install`, {
|
|
method: 'POST',
|
|
body: formData,
|
|
})
|
|
},
|
|
|
|
listOpenCodeDirectoryFiles: async (kind: 'agents' | 'commands'): Promise<OpenCodeDirectoryFileInfo[]> => {
|
|
return fetchWrapper(`${API_BASE_URL}/api/settings/opencode-directory-files`, {
|
|
params: { kind },
|
|
})
|
|
},
|
|
|
|
getOpenCodeDirectoryFile: async (
|
|
kind: 'agents' | 'commands',
|
|
relativePath: string,
|
|
): Promise<OpenCodeDirectoryFileInfo & { content: string }> => {
|
|
return fetchWrapper(`${API_BASE_URL}/api/settings/opencode-directory-files/content`, {
|
|
params: { kind, relativePath },
|
|
})
|
|
},
|
|
|
|
updateOpenCodeDirectoryFile: async (data: {
|
|
kind: 'agents' | 'commands'
|
|
relativePath: string
|
|
content: string
|
|
}): Promise<OpenCodeDirectoryFileInfo> => {
|
|
return fetchWrapper(`${API_BASE_URL}/api/settings/opencode-directory-files`, {
|
|
method: 'PUT',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(data),
|
|
})
|
|
},
|
|
|
|
deleteOpenCodeDirectoryFile: async (
|
|
kind: 'agents' | 'commands',
|
|
relativePath: string,
|
|
): Promise<{ kind: 'agents' | 'commands'; relativePath: string }> => {
|
|
return fetchWrapper(`${API_BASE_URL}/api/settings/opencode-directory-files`, {
|
|
method: 'DELETE',
|
|
params: { kind, relativePath },
|
|
})
|
|
},
|
|
|
|
replaceOpenCodeConfigDirectory: async (
|
|
items: DirectoryUploadItem[],
|
|
): Promise<ReplaceOpenCodeConfigDirectoryResponse> => {
|
|
const formData = new FormData()
|
|
appendUploadItemsWithManifest(formData, items)
|
|
return fetchWrapper(`${API_BASE_URL}/api/settings/opencode-config-directory/replace`, {
|
|
method: 'POST',
|
|
body: formData,
|
|
timeout: 300000,
|
|
})
|
|
},
|
|
}
|
|
|
|
export interface VersionInfo {
|
|
currentVersion: string | null
|
|
latestVersion: string | null
|
|
updateAvailable: boolean
|
|
releaseUrl: string | null
|
|
releaseName: string | null
|
|
}
|
|
|
|
export interface OpenCodeServerAuthStatus {
|
|
isSet: boolean
|
|
source: 'db' | 'env' | 'none'
|
|
}
|
|
|
|
export async function getOpenCodeServerAuth(): Promise<OpenCodeServerAuthStatus> {
|
|
return fetchWrapper(`${API_BASE_URL}/api/settings/opencode-server-auth`)
|
|
}
|
|
|
|
export async function updateOpenCodeServerAuth(password: string | null): Promise<OpenCodeServerAuthStatus> {
|
|
return fetchWrapper(`${API_BASE_URL}/api/settings/opencode-server-auth`, {
|
|
method: 'PATCH',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ password }),
|
|
})
|
|
}
|
|
|
|
export interface ManagerTokenResponse {
|
|
token: string
|
|
}
|
|
|
|
export async function getManagerToken(): Promise<ManagerTokenResponse> {
|
|
return fetchWrapper(`${API_BASE_URL}/api/settings/manager-token`)
|
|
}
|
|
|
|
export async function rotateManagerToken(): Promise<ManagerTokenResponse> {
|
|
return fetchWrapper(`${API_BASE_URL}/api/settings/manager-token/rotate`, {
|
|
method: 'POST',
|
|
})
|
|
}
|