211 lines
6.8 KiB
TypeScript
211 lines
6.8 KiB
TypeScript
|
|
/**
|
||
|
|
* Knowledge API client — Wiki articles/categories/versions, knowledge graph,
|
||
|
|
* and Ask Knowledge.
|
||
|
|
*
|
||
|
|
* All requests use the shared `apiClient` (`baseURL: '/api/v1'`).
|
||
|
|
*/
|
||
|
|
|
||
|
|
import { apiDelete, apiGet, apiPatch, apiPost } from './client';
|
||
|
|
|
||
|
|
// ─── Wiki types ────────────────────────────────────────────────────────────
|
||
|
|
|
||
|
|
export type WikiArticleStatus = 'draft' | 'published' | 'archived';
|
||
|
|
|
||
|
|
export interface WikiArticle {
|
||
|
|
id: string;
|
||
|
|
title: string;
|
||
|
|
slug: string;
|
||
|
|
content: string;
|
||
|
|
content_html: string | null;
|
||
|
|
summary: string | null;
|
||
|
|
category_id: string | null;
|
||
|
|
tags: string[];
|
||
|
|
status: WikiArticleStatus;
|
||
|
|
entity_links: Record<string, unknown>[];
|
||
|
|
version: number;
|
||
|
|
published_at: string | null;
|
||
|
|
owner_id: string | null;
|
||
|
|
created_at: string | null;
|
||
|
|
updated_at: string | null;
|
||
|
|
}
|
||
|
|
|
||
|
|
export interface WikiArticleListResult {
|
||
|
|
items: WikiArticle[];
|
||
|
|
total: number;
|
||
|
|
page: number;
|
||
|
|
page_size: number;
|
||
|
|
}
|
||
|
|
|
||
|
|
export interface WikiArticleCreate {
|
||
|
|
title: string;
|
||
|
|
slug?: string;
|
||
|
|
content?: string;
|
||
|
|
summary?: string | null;
|
||
|
|
category_id?: string | null;
|
||
|
|
tags?: string[];
|
||
|
|
status?: WikiArticleStatus;
|
||
|
|
entity_links?: Record<string, unknown>[];
|
||
|
|
}
|
||
|
|
|
||
|
|
export interface WikiArticleUpdate {
|
||
|
|
title?: string;
|
||
|
|
content?: string;
|
||
|
|
summary?: string | null;
|
||
|
|
category_id?: string | null;
|
||
|
|
tags?: string[];
|
||
|
|
status?: WikiArticleStatus;
|
||
|
|
entity_links?: Record<string, unknown>[];
|
||
|
|
edit_comment?: string | null;
|
||
|
|
}
|
||
|
|
|
||
|
|
export interface WikiCategory {
|
||
|
|
id: string;
|
||
|
|
name: string;
|
||
|
|
slug: string;
|
||
|
|
description: string | null;
|
||
|
|
parent_id: string | null;
|
||
|
|
sort_order: number;
|
||
|
|
}
|
||
|
|
|
||
|
|
export interface WikiCategoryCreate {
|
||
|
|
name: string;
|
||
|
|
slug?: string;
|
||
|
|
description?: string | null;
|
||
|
|
parent_id?: string | null;
|
||
|
|
sort_order?: number;
|
||
|
|
}
|
||
|
|
|
||
|
|
export interface WikiVersion {
|
||
|
|
id: string;
|
||
|
|
version: number;
|
||
|
|
title: string;
|
||
|
|
content: string;
|
||
|
|
edited_by: string | null;
|
||
|
|
edit_comment: string | null;
|
||
|
|
created_at: string | null;
|
||
|
|
}
|
||
|
|
|
||
|
|
// ─── Graph types ───────────────────────────────────────────────────────────
|
||
|
|
|
||
|
|
export interface GraphRelationship {
|
||
|
|
id: string;
|
||
|
|
source_type: string;
|
||
|
|
source_id: string;
|
||
|
|
target_type: string;
|
||
|
|
target_id: string;
|
||
|
|
relationship_type: string;
|
||
|
|
metadata: Record<string, unknown> | null;
|
||
|
|
owner_id: string | null;
|
||
|
|
created_at: string | null;
|
||
|
|
}
|
||
|
|
|
||
|
|
export interface GraphRelationshipsResult {
|
||
|
|
items: GraphRelationship[];
|
||
|
|
total: number;
|
||
|
|
}
|
||
|
|
|
||
|
|
// ─── Ask Knowledge types ───────────────────────────────────────────────────
|
||
|
|
|
||
|
|
export interface KnowledgeAskRequest {
|
||
|
|
query: string;
|
||
|
|
source_types?: string[];
|
||
|
|
}
|
||
|
|
|
||
|
|
export interface KnowledgeEvidence {
|
||
|
|
id: string;
|
||
|
|
source_type: string;
|
||
|
|
source_id: string;
|
||
|
|
title: string;
|
||
|
|
snippet: string;
|
||
|
|
score?: number;
|
||
|
|
url?: string | null;
|
||
|
|
}
|
||
|
|
|
||
|
|
export interface KnowledgeAskResponse {
|
||
|
|
answer: string;
|
||
|
|
evidence: KnowledgeEvidence[];
|
||
|
|
sources_used?: string[];
|
||
|
|
}
|
||
|
|
|
||
|
|
// ─── Wiki API ──────────────────────────────────────────────────────────────
|
||
|
|
|
||
|
|
export async function fetchWikiArticles(params?: {
|
||
|
|
page?: number;
|
||
|
|
page_size?: number;
|
||
|
|
category_id?: string | null;
|
||
|
|
status?: string | null;
|
||
|
|
search?: string | null;
|
||
|
|
}): Promise<WikiArticleListResult> {
|
||
|
|
const query = new URLSearchParams();
|
||
|
|
if (params?.page) query.set('page', String(params.page));
|
||
|
|
if (params?.page_size) query.set('page_size', String(params.page_size));
|
||
|
|
if (params?.category_id) query.set('category_id', params.category_id);
|
||
|
|
if (params?.status) query.set('status', params.status);
|
||
|
|
if (params?.search) query.set('search', params.search);
|
||
|
|
const qs = query.toString();
|
||
|
|
return apiGet<WikiArticleListResult>(`/wiki/articles${qs ? `?${qs}` : ''}`);
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function fetchWikiArticle(articleId: string): Promise<WikiArticle> {
|
||
|
|
return apiGet<WikiArticle>(`/wiki/articles/${articleId}`);
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function createWikiArticle(data: WikiArticleCreate): Promise<WikiArticle> {
|
||
|
|
return apiPost<WikiArticle>('/wiki/articles', data);
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function updateWikiArticle(articleId: string, data: WikiArticleUpdate): Promise<WikiArticle> {
|
||
|
|
return apiPatch<WikiArticle>(`/wiki/articles/${articleId}`, data);
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function deleteWikiArticle(articleId: string): Promise<void> {
|
||
|
|
await apiDelete<void>(`/wiki/articles/${articleId}`);
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function fetchWikiVersions(articleId: string): Promise<WikiVersion[]> {
|
||
|
|
const result = await apiGet<{ items: WikiVersion[] }>(`/wiki/articles/${articleId}/versions`);
|
||
|
|
return result.items;
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function restoreWikiVersion(articleId: string, version: number): Promise<WikiArticle> {
|
||
|
|
return apiPost<WikiArticle>(`/wiki/articles/${articleId}/versions/${version}/restore`);
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function fetchWikiCategories(): Promise<WikiCategory[]> {
|
||
|
|
const result = await apiGet<{ items: WikiCategory[] }>('/wiki/categories');
|
||
|
|
return result.items;
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function createWikiCategory(data: WikiCategoryCreate): Promise<WikiCategory> {
|
||
|
|
return apiPost<WikiCategory>('/wiki/categories', data);
|
||
|
|
}
|
||
|
|
|
||
|
|
// ─── Graph API ─────────────────────────────────────────────────────────────
|
||
|
|
|
||
|
|
export async function fetchGraphRelationships(params?: {
|
||
|
|
source_type?: string | null;
|
||
|
|
source_id?: string | null;
|
||
|
|
target_type?: string | null;
|
||
|
|
target_id?: string | null;
|
||
|
|
relationship_type?: string | null;
|
||
|
|
page?: number;
|
||
|
|
page_size?: number;
|
||
|
|
}): Promise<GraphRelationshipsResult> {
|
||
|
|
const query = new URLSearchParams();
|
||
|
|
if (params?.source_type) query.set('source_type', params.source_type);
|
||
|
|
if (params?.source_id) query.set('source_id', params.source_id);
|
||
|
|
if (params?.target_type) query.set('target_type', params.target_type);
|
||
|
|
if (params?.target_id) query.set('target_id', params.target_id);
|
||
|
|
if (params?.relationship_type) query.set('relationship_type', params.relationship_type);
|
||
|
|
if (params?.page) query.set('page', String(params.page));
|
||
|
|
if (params?.page_size) query.set('page_size', String(params.page_size));
|
||
|
|
const qs = query.toString();
|
||
|
|
return apiGet<GraphRelationshipsResult>(`/graph/relationships${qs ? `?${qs}` : ''}`);
|
||
|
|
}
|
||
|
|
|
||
|
|
// ─── Ask Knowledge API ─────────────────────────────────────────────────────
|
||
|
|
|
||
|
|
export async function askKnowledge(body: KnowledgeAskRequest): Promise<KnowledgeAskResponse> {
|
||
|
|
return apiPost<KnowledgeAskResponse>('/knowledge/ask', body);
|
||
|
|
}
|