fix(chat): downgrade media prefetch cache failures
This commit is contained in:
@@ -46,7 +46,45 @@ export class ChatMediaCacheCoordinator {
|
||||
async cacheRemoteMedia(
|
||||
input: CacheRemoteChatMediaInput,
|
||||
): Promise<Result<LocalChatMediaRow>> {
|
||||
return Result.wrap(() => this._cacheRemoteMediaOrThrow(input));
|
||||
}
|
||||
|
||||
async prefetchMediaForMessages(
|
||||
messages: readonly ChatMessage[],
|
||||
): Promise<Result<void>> {
|
||||
return this._prefetchMediaTargets(
|
||||
messages.flatMap((message) => getMessageMediaTargets(message)),
|
||||
);
|
||||
}
|
||||
|
||||
async prefetchMediaForSendResponse(
|
||||
response: ChatSendResponse,
|
||||
): Promise<Result<void>> {
|
||||
return this._prefetchMediaTargets(getSendResponseMediaTargets(response));
|
||||
}
|
||||
|
||||
private async _prefetchMediaTargets(
|
||||
targets: readonly CacheRemoteChatMediaInput[],
|
||||
): Promise<Result<void>> {
|
||||
return Result.wrap(async () => {
|
||||
for (const target of uniqueMediaTargets(targets)) {
|
||||
try {
|
||||
await this._cacheRemoteMediaOrThrow(target);
|
||||
} catch (error) {
|
||||
log.warn("[chat-media] prefetch failed", {
|
||||
messageId: target.messageId,
|
||||
kind: target.kind,
|
||||
remoteUrl: target.remoteUrl,
|
||||
error,
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private async _cacheRemoteMediaOrThrow(
|
||||
input: CacheRemoteChatMediaInput,
|
||||
): Promise<LocalChatMediaRow> {
|
||||
if (!isCacheableRemoteChatMediaUrl(input.remoteUrl)) {
|
||||
throw new Error(`Unsupported media url: ${input.remoteUrl}`);
|
||||
}
|
||||
@@ -97,39 +135,6 @@ export class ChatMediaCacheCoordinator {
|
||||
throw new Error("Cached media could not be read after save.");
|
||||
}
|
||||
return savedResult.data;
|
||||
});
|
||||
}
|
||||
|
||||
async prefetchMediaForMessages(
|
||||
messages: readonly ChatMessage[],
|
||||
): Promise<Result<void>> {
|
||||
return this._prefetchMediaTargets(
|
||||
messages.flatMap((message) => getMessageMediaTargets(message)),
|
||||
);
|
||||
}
|
||||
|
||||
async prefetchMediaForSendResponse(
|
||||
response: ChatSendResponse,
|
||||
): Promise<Result<void>> {
|
||||
return this._prefetchMediaTargets(getSendResponseMediaTargets(response));
|
||||
}
|
||||
|
||||
private async _prefetchMediaTargets(
|
||||
targets: readonly CacheRemoteChatMediaInput[],
|
||||
): Promise<Result<void>> {
|
||||
return Result.wrap(async () => {
|
||||
for (const target of uniqueMediaTargets(targets)) {
|
||||
const result = await this.cacheRemoteMedia(target);
|
||||
if (Result.isErr(result)) {
|
||||
log.warn("[chat-media] prefetch failed", {
|
||||
messageId: target.messageId,
|
||||
kind: target.kind,
|
||||
remoteUrl: target.remoteUrl,
|
||||
error: result.error,
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private async _resolveMediaOwnerKeys(): Promise<string[]> {
|
||||
|
||||
+75
-2
@@ -110,8 +110,17 @@ export class Logger {
|
||||
}
|
||||
}
|
||||
|
||||
const serializableValue = Logger.toSerializableLogValue(value);
|
||||
if (serializableValue !== value) {
|
||||
try {
|
||||
return JSON.stringify(value, null, 2);
|
||||
return JSON.stringify(serializableValue, null, 2);
|
||||
} catch {
|
||||
return String(value);
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
return JSON.stringify(serializableValue, null, 2);
|
||||
} catch {
|
||||
return String(value);
|
||||
}
|
||||
@@ -206,7 +215,10 @@ export class Logger {
|
||||
return;
|
||||
}
|
||||
|
||||
console[method](`${prefix} ${message}`, ...data);
|
||||
console[method](
|
||||
`${prefix} ${message}`,
|
||||
...Logger.toBrowserConsoleData(data),
|
||||
);
|
||||
}
|
||||
|
||||
private static toBrowserConsolePayload(args: LogArgs): {
|
||||
@@ -235,6 +247,67 @@ export class Logger {
|
||||
};
|
||||
}
|
||||
|
||||
private static toBrowserConsoleData(data: unknown[]): unknown[] {
|
||||
return data.map((item) => Logger.toSerializableLogValue(item));
|
||||
}
|
||||
|
||||
private static toSerializableLogValue(
|
||||
value: unknown,
|
||||
seen = new WeakSet<object>(),
|
||||
): unknown {
|
||||
if (value instanceof Error) {
|
||||
return Logger.serializeError(value, seen);
|
||||
}
|
||||
|
||||
if (Array.isArray(value)) {
|
||||
return value.map((item) => Logger.toSerializableLogValue(item, seen));
|
||||
}
|
||||
|
||||
if (typeof value !== "object" || value === null) {
|
||||
return value;
|
||||
}
|
||||
|
||||
if (value instanceof Date) {
|
||||
return value.toISOString();
|
||||
}
|
||||
|
||||
if (seen.has(value)) {
|
||||
return "[Circular]";
|
||||
}
|
||||
seen.add(value);
|
||||
|
||||
const output: Record<string, unknown> = {};
|
||||
for (const [key, item] of Object.entries(value)) {
|
||||
output[key] = Logger.toSerializableLogValue(item, seen);
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
private static serializeError(
|
||||
error: Error,
|
||||
seen: WeakSet<object>,
|
||||
): Record<string, unknown> {
|
||||
const output: Record<string, unknown> = {
|
||||
name: error.name,
|
||||
message: error.message,
|
||||
stack: error.stack,
|
||||
};
|
||||
|
||||
if (seen.has(error)) {
|
||||
return output;
|
||||
}
|
||||
seen.add(error);
|
||||
|
||||
for (const key of Object.getOwnPropertyNames(error)) {
|
||||
if (key in output) continue;
|
||||
output[key] = Logger.toSerializableLogValue(
|
||||
(error as unknown as Record<string, unknown>)[key],
|
||||
seen,
|
||||
);
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
/** 透传 raw pino logger(高级用法:自定义 binding / child / serializers) */
|
||||
get raw(): PinoLogger {
|
||||
return this.logger;
|
||||
|
||||
Reference in New Issue
Block a user