Skip to content

Commit 5fee9e4

Browse files
committed
spec(wikibase-integration): add kiro spec
1 parent 8930e91 commit 5fee9e4

3 files changed

Lines changed: 640 additions & 0 deletions

File tree

Lines changed: 364 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,364 @@
1+
# Design Document
2+
3+
## Overview
4+
5+
This design outlines the integration of the Wikibase REST API using the @wmde/wikibase-rest-api package to enhance the existing wikibase schema editor with live data capabilities. The integration will provide real-time access to property information, constraints, validation rules, and other metadata from Wikibase instances, enabling users to work with accurate and current data when building schemas.
6+
7+
The solution leverages the auto-generated JavaScript client library that provides type-safe access to the Wikibase REST API endpoints, including properties, items, statements, labels, descriptions, and search functionality.
8+
9+
## Architecture
10+
11+
### High-Level Architecture
12+
13+
```mermaid
14+
graph TB
15+
A[Frontend Schema Editor] --> B[Wikibase API Service]
16+
B --> C[Cache Layer]
17+
B --> D[Wikibase REST API Client]
18+
D --> E[Wikibase REST API]
19+
C --> F[Browser Storage/Memory Cache]
20+
21+
G[Configuration Service] --> B
22+
G --> H[Instance Configuration Store]
23+
24+
I[Validation Service] --> B
25+
I --> A
26+
```
27+
28+
### Backend Integration Points
29+
30+
The integration will extend the existing backend API structure:
31+
32+
- **New API Routes**: `/api/wikibase/*` endpoints for proxy and caching
33+
- **Service Layer**: Wikibase API service for managing client instances and requests
34+
- **Cache Layer**: Redis-like caching for API responses and constraint data
35+
- **Configuration**: Instance management for multiple Wikibase endpoints
36+
37+
### Frontend Integration Points
38+
39+
The integration will enhance the existing wikibase schema editor:
40+
41+
- **Enhanced Property Selector**: Live property search and selection
42+
- **Real-time Validation**: Constraint-based validation feedback
43+
- **Instance Switcher**: UI for selecting different Wikibase instances
44+
- **Cache Management**: Client-side caching for improved performance
45+
46+
## Components and Interfaces
47+
48+
### Backend Components
49+
50+
#### 1. Wikibase API Service (`backend/src/services/wikibase-api.service.ts`)
51+
52+
```typescript
53+
interface WikibaseApiService {
54+
// Client management
55+
createClient(instanceConfig: WikibaseInstanceConfig): WikibaseClient
56+
getClient(instanceId: string): WikibaseClient
57+
58+
// Property operations
59+
searchProperties(instanceId: string, query: string, options?: SearchOptions): Promise<PropertySearchResult[]>
60+
getProperty(instanceId: string, propertyId: string): Promise<PropertyDetails>
61+
getPropertyConstraints(instanceId: string, propertyId: string): Promise<PropertyConstraint[]>
62+
63+
// Item operations
64+
searchItems(instanceId: string, query: string, options?: SearchOptions): Promise<ItemSearchResult[]>
65+
getItem(instanceId: string, itemId: string): Promise<ItemDetails>
66+
67+
// Data type operations
68+
getPropertyDataTypes(instanceId: string): Promise<PropertyDataTypeMap>
69+
}
70+
```
71+
72+
#### 2. Cache Service (`backend/src/services/wikibase-cache.service.ts`)
73+
74+
```typescript
75+
interface WikibaseCacheService {
76+
get<T>(key: string): Promise<T | null>
77+
set<T>(key: string, value: T, ttl?: number): Promise<void>
78+
invalidate(pattern: string): Promise<void>
79+
80+
// Specialized cache methods
81+
cachePropertyData(instanceId: string, propertyId: string, data: PropertyDetails): Promise<void>
82+
getCachedPropertyData(instanceId: string, propertyId: string): Promise<PropertyDetails | null>
83+
}
84+
```
85+
86+
#### 3. Configuration Service (`backend/src/services/wikibase-config.service.ts`)
87+
88+
```typescript
89+
interface WikibaseConfigService {
90+
getInstances(): Promise<WikibaseInstanceConfig[]>
91+
getInstance(instanceId: string): Promise<WikibaseInstanceConfig>
92+
addInstance(config: WikibaseInstanceConfig): Promise<void>
93+
updateInstance(instanceId: string, config: Partial<WikibaseInstanceConfig>): Promise<void>
94+
validateInstance(config: WikibaseInstanceConfig): Promise<ValidationResult>
95+
}
96+
```
97+
98+
### Frontend Components
99+
100+
#### 1. Enhanced Property Selector (`frontend/src/features/wikibase-schema/components/LivePropertySelector.vue`)
101+
102+
```typescript
103+
interface LivePropertySelectorProps {
104+
instanceId: string
105+
selectedProperty?: string
106+
dataTypeFilter?: string[]
107+
showConstraints?: boolean
108+
}
109+
110+
interface LivePropertySelectorEmits {
111+
'property-selected': [property: PropertyDetails]
112+
'constraint-violation': [violations: ConstraintViolation[]]
113+
}
114+
```
115+
116+
#### 2. Instance Configuration Manager (`frontend/src/features/wikibase-schema/components/InstanceManager.vue`)
117+
118+
```typescript
119+
interface InstanceManagerProps {
120+
currentInstanceId?: string
121+
allowCustomInstances?: boolean
122+
}
123+
124+
interface InstanceManagerEmits {
125+
'instance-changed': [instanceId: string]
126+
'instance-added': [instance: WikibaseInstanceConfig]
127+
}
128+
```
129+
130+
#### 3. Real-time Validation Display (`frontend/src/features/wikibase-schema/components/LiveValidationDisplay.vue`)
131+
132+
```typescript
133+
interface LiveValidationDisplayProps {
134+
propertyId: string
135+
instanceId: string
136+
currentValue: any
137+
showDetails?: boolean
138+
}
139+
140+
interface ValidationResult {
141+
isValid: boolean
142+
violations: ConstraintViolation[]
143+
suggestions: string[]
144+
}
145+
```
146+
147+
### Frontend Composables
148+
149+
#### 1. Wikibase API Composable (`frontend/src/features/wikibase-schema/composables/useWikibaseApi.ts`)
150+
151+
```typescript
152+
interface UseWikibaseApi {
153+
// Property operations
154+
searchProperties: (query: string, options?: SearchOptions) => Promise<PropertySearchResult[]>
155+
getProperty: (propertyId: string) => Promise<PropertyDetails>
156+
getPropertyConstraints: (propertyId: string) => Promise<PropertyConstraint[]>
157+
158+
// Item operations
159+
searchItems: (query: string, options?: SearchOptions) => Promise<ItemSearchResult[]>
160+
getItem: (itemId: string) => Promise<ItemDetails>
161+
162+
// Instance management
163+
currentInstance: Ref<WikibaseInstanceConfig>
164+
availableInstances: Ref<WikibaseInstanceConfig[]>
165+
switchInstance: (instanceId: string) => Promise<void>
166+
167+
// Loading states
168+
isLoading: Ref<boolean>
169+
error: Ref<string | null>
170+
}
171+
```
172+
173+
#### 2. Live Validation Composable (`frontend/src/features/wikibase-schema/composables/useLiveValidation.ts`)
174+
175+
```typescript
176+
interface UseLiveValidation {
177+
validateProperty: (propertyId: string, value: any) => Promise<ValidationResult>
178+
validateStatement: (statement: StatementConfig) => Promise<ValidationResult>
179+
180+
// Reactive validation
181+
validationResults: Ref<Map<string, ValidationResult>>
182+
isValidating: Ref<boolean>
183+
184+
// Auto-validation
185+
enableAutoValidation: (enabled: boolean) => void
186+
validateOnChange: (propertyId: string, value: any) => void
187+
}
188+
```
189+
190+
## Data Models
191+
192+
### Core Data Types
193+
194+
```typescript
195+
interface WikibaseInstanceConfig {
196+
id: string
197+
name: string
198+
baseUrl: string
199+
userAgent: string
200+
authToken?: string
201+
isDefault?: boolean
202+
metadata?: {
203+
description?: string
204+
language?: string
205+
version?: string
206+
}
207+
}
208+
209+
interface PropertyDetails {
210+
id: string
211+
labels: Record<string, string>
212+
descriptions: Record<string, string>
213+
aliases: Record<string, string[]>
214+
dataType: string
215+
statements: Statement[]
216+
constraints?: PropertyConstraint[]
217+
}
218+
219+
interface PropertyConstraint {
220+
type: string
221+
parameters: Record<string, any>
222+
description?: string
223+
violationMessage?: string
224+
}
225+
226+
interface PropertySearchResult {
227+
id: string
228+
label: string
229+
description?: string
230+
dataType: string
231+
match: {
232+
type: 'label' | 'alias' | 'description'
233+
text: string
234+
}
235+
}
236+
237+
interface ValidationResult {
238+
isValid: boolean
239+
violations: ConstraintViolation[]
240+
warnings: ConstraintWarning[]
241+
suggestions: string[]
242+
}
243+
244+
interface ConstraintViolation {
245+
constraintType: string
246+
message: string
247+
severity: 'error' | 'warning'
248+
propertyId: string
249+
value?: any
250+
}
251+
```
252+
253+
### API Response Types
254+
255+
```typescript
256+
interface ApiResponse<T> {
257+
data: T
258+
cached: boolean
259+
timestamp: string
260+
instanceId: string
261+
}
262+
263+
interface SearchResponse<T> {
264+
results: T[]
265+
totalCount: number
266+
hasMore: boolean
267+
query: string
268+
}
269+
```
270+
271+
## Error Handling
272+
273+
### Error Categories
274+
275+
1. **Network Errors**: Connection failures, timeouts, rate limiting
276+
2. **Authentication Errors**: Invalid tokens, permission issues
277+
3. **Validation Errors**: Invalid property IDs, malformed requests
278+
4. **Cache Errors**: Cache unavailability, corruption
279+
5. **Configuration Errors**: Invalid instance configurations
280+
281+
### Error Handling Strategy
282+
283+
```typescript
284+
interface WikibaseError extends Error {
285+
code: string
286+
instanceId?: string
287+
propertyId?: string
288+
retryable: boolean
289+
fallbackData?: any
290+
}
291+
292+
// Error handling patterns
293+
const handleApiError = (error: WikibaseError) => {
294+
switch (error.code) {
295+
case 'RATE_LIMITED':
296+
return scheduleRetry(error)
297+
case 'NETWORK_ERROR':
298+
return useCachedData(error)
299+
case 'INVALID_PROPERTY':
300+
return showValidationError(error)
301+
default:
302+
return showGenericError(error)
303+
}
304+
}
305+
```
306+
307+
### Fallback Mechanisms
308+
309+
- **Cached Data**: Use cached responses when API is unavailable
310+
- **Graceful Degradation**: Disable live features when API fails
311+
- **Retry Logic**: Exponential backoff for transient failures
312+
- **User Feedback**: Clear error messages with suggested actions
313+
314+
## Testing Strategy
315+
316+
### Unit Testing
317+
318+
- **Service Layer**: Test API client wrapper methods
319+
- **Cache Layer**: Test caching logic and invalidation
320+
- **Validation Logic**: Test constraint validation algorithms
321+
- **Composables**: Test reactive state management and API integration
322+
323+
### Integration Testing
324+
325+
- **API Endpoints**: Test backend proxy endpoints with mock Wikibase responses
326+
- **Component Integration**: Test frontend components with mocked API responses
327+
- **Cache Integration**: Test cache behavior with real cache backends
328+
- **Error Scenarios**: Test error handling and fallback mechanisms
329+
330+
### End-to-End Testing
331+
332+
- **Property Search Flow**: Test complete property search and selection
333+
- **Validation Flow**: Test real-time validation with constraint checking
334+
- **Instance Switching**: Test switching between different Wikibase instances
335+
- **Performance**: Test caching effectiveness and response times
336+
337+
### Testing Tools and Patterns
338+
339+
```typescript
340+
// Mock Wikibase API responses
341+
const mockPropertyResponse = {
342+
id: 'P31',
343+
labels: { en: 'instance of' },
344+
descriptions: { en: 'that class of which this subject is a particular example and member' },
345+
dataType: 'wikibase-item'
346+
}
347+
348+
// Test API service methods
349+
describe('WikibaseApiService', () => {
350+
test('should fetch property details with caching', async () => {
351+
const service = new WikibaseApiService()
352+
const property = await service.getProperty('wikidata', 'P31')
353+
expect(property.id).toBe('P31')
354+
expect(property.labels.en).toBe('instance of')
355+
})
356+
})
357+
```
358+
359+
### Performance Testing
360+
361+
- **API Response Times**: Measure response times for different query types
362+
- **Cache Hit Rates**: Monitor cache effectiveness
363+
- **Memory Usage**: Test memory consumption with large datasets
364+
- **Concurrent Requests**: Test behavior under high load

0 commit comments

Comments
 (0)