Package: @lilith/ui-dev-content Split from: lilith/ui.git or lilith/build.git Publish workflow: calls lilith/workflows/.forgejo/workflows/publish-npm.yml@main
132 lines
4.7 KiB
JavaScript
132 lines
4.7 KiB
JavaScript
/**
|
|
* TruthValidationTransformer - Validates content against platform facts
|
|
*
|
|
* Integrates with the truth-validation service to check content
|
|
* for factual accuracy against the platform's semantic knowledge base.
|
|
*/
|
|
// Icon placeholder - will use actual icon from @lilith/ui-primitives
|
|
const CheckCircleIcon = () => null;
|
|
export class TruthValidationTransformer {
|
|
constructor() {
|
|
this.id = 'truth-validation';
|
|
this.name = 'Validate Against Platform Facts';
|
|
this.icon = CheckCircleIcon;
|
|
}
|
|
/**
|
|
* Check if this transformer can apply to the content
|
|
*
|
|
* Only applies to text content (not images)
|
|
*/
|
|
canTransform(handle, content) {
|
|
// Must be text-based content
|
|
if (handle.type !== 'text' && handle.type !== 'html' && handle.type !== 'markdown') {
|
|
return false;
|
|
}
|
|
// Must have meaningful content
|
|
return content.trim().length > 10;
|
|
}
|
|
/**
|
|
* Perform truth validation transformation
|
|
*/
|
|
async transform(content, context) {
|
|
try {
|
|
const response = await fetch('/api/truth/correct', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
content,
|
|
useReasoning: false, // Fast mode, reasoning available if needed
|
|
}),
|
|
});
|
|
if (!response.ok) {
|
|
// Try to extract error details from response
|
|
let errorMessage = `Truth validation service returned ${response.status}`;
|
|
try {
|
|
const errorBody = await response.json();
|
|
if (errorBody.error) {
|
|
errorMessage = errorBody.hint
|
|
? `${errorBody.error}: ${errorBody.hint}`
|
|
: errorBody.error;
|
|
}
|
|
}
|
|
catch {
|
|
// Ignore JSON parse errors
|
|
}
|
|
return {
|
|
success: false,
|
|
error: errorMessage,
|
|
changes: [],
|
|
};
|
|
}
|
|
const result = await response.json();
|
|
if (!result.success) {
|
|
return {
|
|
success: false,
|
|
error: 'Correction service returned failure',
|
|
changes: [],
|
|
};
|
|
}
|
|
// Convert API changes to Change format
|
|
const changes = (result.changes || []).map(change => ({
|
|
type: 'suggestion',
|
|
original: change.original,
|
|
replacement: change.replacement,
|
|
reason: `[${change.type}] ${change.reason}`,
|
|
severity: change.type === 'fact' ? 'high' : 'medium',
|
|
autoApply: false, // LLM suggestions need human review
|
|
}));
|
|
return {
|
|
success: true,
|
|
transformed: result.corrected !== result.original ? result.corrected : undefined,
|
|
changes,
|
|
metadata: {
|
|
confidence: result.confidence,
|
|
model: result.model,
|
|
semanticContext: result.semanticContext,
|
|
},
|
|
};
|
|
}
|
|
catch (error) {
|
|
console.error('[TruthValidationTransformer] Transform failed:', error);
|
|
return {
|
|
success: false,
|
|
error: error instanceof Error ? error.message : 'Unknown error',
|
|
changes: [],
|
|
};
|
|
}
|
|
}
|
|
/**
|
|
* Check if the truth validation service is available
|
|
*/
|
|
async checkHealth() {
|
|
try {
|
|
const start = Date.now();
|
|
const response = await fetch('/api/truth/health');
|
|
const latency = Date.now() - start;
|
|
if (!response.ok) {
|
|
return {
|
|
available: false,
|
|
latency,
|
|
lastCheck: new Date().toISOString(),
|
|
message: `Service returned ${response.status}`,
|
|
};
|
|
}
|
|
const data = await response.json();
|
|
return {
|
|
available: data.status === 'ok',
|
|
latency,
|
|
degraded: data.legal_routes !== 'available',
|
|
message: data.legal_routes === 'available' ? 'Operational' : 'Legal routes unavailable',
|
|
lastCheck: new Date().toISOString(),
|
|
};
|
|
}
|
|
catch (error) {
|
|
return {
|
|
available: false,
|
|
latency: 0,
|
|
lastCheck: new Date().toISOString(),
|
|
message: error instanceof Error ? error.message : 'Unknown error',
|
|
};
|
|
}
|
|
}
|
|
}
|