MCPcopy Create free account
hub / github.com/Noumena-Network/code / maybe

Function maybe

src/utils/attachments.ts:1007–1044  ·  view source on GitHub ↗
(label: string, f: () => Promise<A[]>)

Source from the content-addressed store, hash-verified

1005}
1006
1007async function maybe<A>(label: string, f: () => Promise<A[]>): Promise<A[]> {
1008 const startTime = Date.now()
1009 try {
1010 const result = await f()
1011 const duration = Date.now() - startTime
1012 // Log only 5% of events to reduce volume
1013 if (Math.random() < 0.05) {
1014 // jsonStringify(undefined) returns undefined, so .length would throw
1015 const attachmentSizeBytes = result
1016 .filter(a => a !== undefined && a !== null)
1017 .reduce((total, attachment) => {
1018 return total + jsonStringify(attachment).length
1019 }, 0)
1020 logEvent('ncode_attachment_compute_duration', {
1021 label,
1022 duration_ms: duration,
1023 attachment_size_bytes: attachmentSizeBytes,
1024 attachment_count: result.length,
1025 } as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS)
1026 }
1027 return result
1028 } catch (e) {
1029 const duration = Date.now() - startTime
1030 // Log only 5% of events to reduce volume
1031 if (Math.random() < 0.05) {
1032 logEvent('ncode_attachment_compute_duration', {
1033 label,
1034 duration_ms: duration,
1035 error: true,
1036 } as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS)
1037 }
1038 logError(e)
1039 // For Ant users, log the full error to help with debugging
1040 logAntError(`Attachment error in ${label}`, e)
1041
1042 return []
1043 }
1044}
1045
1046const INLINE_NOTIFICATION_MODES = new Set(['prompt', 'task-notification'])
1047

Callers 1

getAttachmentsFunction · 0.85

Calls 5

fFunction · 0.85
jsonStringifyFunction · 0.85
logAntErrorFunction · 0.85
logErrorFunction · 0.70
logEventFunction · 0.50

Tested by

no test coverage detected