basant307/AI_Governance_Project
048
1/**2 * @license3 * Copyright 2025 Qwen Team4 * SPDX-License-Identifier: Apache-2.05 */6 7export interface InsightProgressPayload {8 insight_progress: {9 stage: string;10 progress: number;11 detail?: string;12 };13}14 15export interface InsightReadyPayload {16 insight_ready: {17 path: string;18 };19}20 21export interface InsightErrorPayload {22 insight_error: {23 error: string;24 };25}26 27export type ParsedInsightMessage =28 | {29 type: 'insight_progress';30 stage: string;31 progress: number;32 detail?: string;33 }34 | {35 type: 'insight_ready';36 path: string;37 }38 | {39 type: 'insight_error';40 error: string;41 };42 43export function encodeInsightProgressMessage(44 stage: string,45 progress: number,46 detail?: string,47): string {48 const payload: InsightProgressPayload = {49 insight_progress: { stage, progress, detail },50 };51 return JSON.stringify(payload);52}53 54export function encodeInsightReadyMessage(path: string): string {55 const payload: InsightReadyPayload = {56 insight_ready: { path },57 };58 return JSON.stringify(payload);59}60 61export function encodeInsightErrorMessage(error: string): string {62 const payload: InsightErrorPayload = {63 insight_error: { error },64 };65 return JSON.stringify(payload);66}67 68export function parseInsightMessage(69 message: string,70): ParsedInsightMessage | null {71 try {72 const parsed = JSON.parse(message) as {73 insight_progress?: {74 stage?: unknown;75 progress?: unknown;76 detail?: unknown;77 };78 insight_ready?: { path?: unknown };79 };80 81 if (parsed.insight_progress) {82 const { stage, progress, detail } = parsed.insight_progress;83 if (typeof stage === 'string' && typeof progress === 'number') {84 return {85 type: 'insight_progress',86 stage,87 progress,88 detail: typeof detail === 'string' ? detail : undefined,89 };90 }91 }92 93 if (parsed.insight_ready) {94 const { path } = parsed.insight_ready;95 if (typeof path === 'string') {96 return { type: 'insight_ready', path };97 }98 }99 100 const insightError = (parsed as { insight_error?: { error?: unknown } })101 .insight_error;102 if (insightError && typeof insightError.error === 'string') {103 return { type: 'insight_error', error: insightError.error };104 }105 } catch {106 return null;107 }108 109 return null;110}111 