forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatEvidence.ts
More file actions
246 lines (222 loc) · 7.37 KB
/
Copy pathchatEvidence.ts
File metadata and controls
246 lines (222 loc) · 7.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/**
* Bounded, fail-soft parsing for supplemental chat evidence.
*
* The message text is authoritative. This adapter only admits conversation
* summary, conversation segment, screen, keyframe, and request references.
* These references remain inert and are never used to navigate, fetch, or
* mutate anything in the client.
*/
export const CHAT_EVIDENCE_SCHEMA_VERSION = 1 as const;
export const CHAT_EVIDENCE_MAX_REFERENCES = 24;
export const CHAT_EVIDENCE_MAX_IDENTIFIER_CHARS = 256;
export const CHAT_EVIDENCE_MAX_TITLE_CHARS = 160;
export const CHAT_EVIDENCE_MAX_SUMMARY_CHARS = 600;
export const CHAT_EVIDENCE_MAX_ERROR_CODE_CHARS = 128;
export const CHAT_EVIDENCE_MAX_ERROR_MESSAGE_CHARS = 600;
export type ChatEvidenceKind =
'conversation_summary' | 'conversation_segment' | 'screen' | 'keyframe' | 'request';
export type ChatEvidenceState =
'available' | 'loading' | 'offline' | 'pruned' | 'failed' | 'unknown';
export interface ChatEvidenceReference {
id: string;
kind: ChatEvidenceKind;
state: ChatEvidenceState;
title?: string;
summary?: string;
conversationId?: string;
segmentId?: string;
frameId?: string;
requestId?: string;
errorCode?: string;
errorMessage?: string;
}
export interface ChatEvidenceEnvelope {
schemaVersion: number;
requestId?: string;
references: ChatEvidenceReference[];
}
type JsonRecord = Record<string, unknown>;
function isRecord(value: unknown): value is JsonRecord {
return (
typeof value === 'object' &&
value !== null &&
!Array.isArray(value) &&
!(value instanceof Map) &&
!(value instanceof Set)
);
}
function boundedString(value: unknown, maxLength: number): string | undefined {
if (typeof value !== 'string') return undefined;
const normalized = value.trim();
return normalized ? normalized.slice(0, maxLength) : undefined;
}
function readSchemaVersion(value: unknown): number | undefined {
if (typeof value === 'number') return Number.isSafeInteger(value) ? value : undefined;
if (typeof value === 'string' && /^\s*[+-]?\d+\s*$/.test(value)) {
const parsed = Number(value);
return Number.isSafeInteger(parsed) ? parsed : undefined;
}
return undefined;
}
function parseState(value: unknown): ChatEvidenceState {
const state = boundedString(value, CHAT_EVIDENCE_MAX_IDENTIFIER_CHARS)?.toLowerCase();
switch (state) {
case 'available':
case 'loading':
case 'offline':
case 'pruned':
case 'failed':
return state;
default:
return 'unknown';
}
}
function parseReference(value: unknown): ChatEvidenceReference | null {
if (!isRecord(value)) return null;
const id = boundedString(
value.id ?? value.reference_id,
CHAT_EVIDENCE_MAX_IDENTIFIER_CHARS,
);
const kind = boundedString(
value.kind ?? value.type,
CHAT_EVIDENCE_MAX_IDENTIFIER_CHARS,
)?.toLowerCase();
const conversationId = boundedString(
value.conversation_id ?? value.conversationId,
CHAT_EVIDENCE_MAX_IDENTIFIER_CHARS,
);
if (!id) return null;
if (
kind !== 'conversation_summary' &&
kind !== 'conversation_segment' &&
kind !== 'screen' &&
kind !== 'keyframe' &&
kind !== 'request'
) {
return null;
}
const segmentId = boundedString(
value.segment_id ?? value.segmentId,
CHAT_EVIDENCE_MAX_IDENTIFIER_CHARS,
);
const frameId = boundedString(
value.frame_id ?? value.frameId,
CHAT_EVIDENCE_MAX_IDENTIFIER_CHARS,
);
const requestId = boundedString(
value.request_id ?? value.requestId,
CHAT_EVIDENCE_MAX_IDENTIFIER_CHARS,
);
if (
(kind === 'conversation_summary' && !conversationId) ||
(kind === 'conversation_segment' && (!conversationId || !segmentId)) ||
((kind === 'screen' || kind === 'keyframe') && !frameId) ||
(kind === 'request' && !requestId)
) {
return null;
}
const title = boundedString(value.title, CHAT_EVIDENCE_MAX_TITLE_CHARS);
const summary = boundedString(
value.summary ?? value.preview,
CHAT_EVIDENCE_MAX_SUMMARY_CHARS,
);
const errorCode = boundedString(
value.error_code ?? value.errorCode,
CHAT_EVIDENCE_MAX_ERROR_CODE_CHARS,
);
const errorMessage = boundedString(
value.error_message ?? value.errorMessage,
CHAT_EVIDENCE_MAX_ERROR_MESSAGE_CHARS,
);
return {
id,
kind,
state: parseState(value.state ?? value.status),
...(title ? { title } : {}),
...(summary ? { summary } : {}),
...(conversationId ? { conversationId } : {}),
...(segmentId ? { segmentId } : {}),
...(frameId ? { frameId } : {}),
...(requestId ? { requestId } : {}),
...(errorCode ? { errorCode } : {}),
...(errorMessage ? { errorMessage } : {}),
};
}
/** Parse an evidence envelope. Invalid entries are dropped, never thrown. */
export function parseChatEvidenceEnvelope(value: unknown): ChatEvidenceEnvelope | null {
const raw = isRecord(value)
? value
: Array.isArray(value)
? { references: value }
: null;
if (!raw) return null;
const hasSchemaVersion =
Object.prototype.hasOwnProperty.call(raw, 'schema_version') ||
Object.prototype.hasOwnProperty.call(raw, 'schemaVersion') ||
Object.prototype.hasOwnProperty.call(raw, 'version');
const schemaValue = raw.schema_version ?? raw.schemaVersion ?? raw.version;
const schemaVersion = hasSchemaVersion
? (readSchemaVersion(schemaValue) ?? 0)
: CHAT_EVIDENCE_SCHEMA_VERSION;
const requestId = boundedString(
raw.request_id ?? raw.requestId,
CHAT_EVIDENCE_MAX_IDENTIFIER_CHARS,
);
// A future or malformed schema is preserved as metadata only. Its entries
// must not accidentally acquire current-client meaning.
if (schemaVersion !== CHAT_EVIDENCE_SCHEMA_VERSION) {
return {
schemaVersion,
...(requestId ? { requestId } : {}),
references: [],
};
}
const rawReferences = raw.references ?? raw.evidence_refs ?? raw.evidence_references;
const references: ChatEvidenceReference[] = [];
const seenReferenceIds = new Set<string>();
if (Array.isArray(rawReferences)) {
for (const rawReference of rawReferences.slice(0, CHAT_EVIDENCE_MAX_REFERENCES)) {
const reference = parseReference(rawReference);
if (!reference || seenReferenceIds.has(reference.id)) continue;
seenReferenceIds.add(reference.id);
references.push(reference);
}
}
return {
schemaVersion,
...(requestId ? { requestId } : {}),
references,
};
}
/**
* Read direct evidence or the legacy serialized metadata location. This is
* intentionally capped to one metadata hop so malformed input cannot recurse
* indefinitely or interfere with rendering the answer text.
*/
export function parseChatEvidenceFromRecord(value: unknown): ChatEvidenceEnvelope | null {
return parseChatEvidenceFromRecordAtDepth(value, 0);
}
function parseChatEvidenceFromRecordAtDepth(
value: unknown,
depth: number,
): ChatEvidenceEnvelope | null {
if (!isRecord(value)) return null;
const direct =
value.evidence ??
value.evidence_envelope ??
value.evidence_refs ??
value.evidence_references;
if (direct !== undefined) return parseChatEvidenceEnvelope(direct);
if (typeof value.metadata === 'string') {
if (depth >= 1) return null;
try {
return parseChatEvidenceFromRecordAtDepth(JSON.parse(value.metadata), depth + 1);
} catch {
return null;
}
}
if (isRecord(value.metadata) && depth < 1) {
return parseChatEvidenceFromRecordAtDepth(value.metadata, depth + 1);
}
return null;
}