100 lines
3.1 KiB
TypeScript
100 lines
3.1 KiB
TypeScript
import type { CameraModule, PhoneDevice, PhoneSpecs } from '../schema';
|
|
import { enrichHardwareSpecs } from '../hardware-catalog';
|
|
import { setValueAtPath } from '../spec-fields';
|
|
import type { AiLookupField } from './types';
|
|
|
|
const cameraPathPattern = /^specs\.(rearCamera|frontCamera)\[(\d+)\]\.(.+)$/;
|
|
|
|
/** 把 AI 返回的扁平字段合入设备,并同步写入字段级来源与置信度 */
|
|
export function applyAiFields(device: PhoneDevice, fields: AiLookupField[]): PhoneDevice {
|
|
let specs = structuredClone(device.specs);
|
|
|
|
for (const field of fields) {
|
|
const cameraMatch = cameraPathPattern.exec(field.path);
|
|
if (cameraMatch) {
|
|
const [, kind, indexText, key] = cameraMatch;
|
|
const index = Number(indexText);
|
|
const defaultPosition: CameraModule['position'] =
|
|
kind === 'frontCamera' ? 'front' : 'wide';
|
|
const list: CameraModule[] =
|
|
kind === 'frontCamera' ? specs.frontCamera : specs.rearCamera;
|
|
|
|
while (list.length <= index) {
|
|
list.push({ position: defaultPosition, extra: {} });
|
|
}
|
|
|
|
if (key === 'position') {
|
|
list[index] = {
|
|
...list[index],
|
|
position: field.value as CameraModule['position'],
|
|
};
|
|
} else {
|
|
list[index] = setValueAtPath(list[index], key, field.value);
|
|
}
|
|
specs = { ...specs, [kind]: list } as PhoneSpecs;
|
|
continue;
|
|
}
|
|
|
|
specs = setValueAtPath(specs, field.path.replace(/^specs\./, ''), field.value);
|
|
}
|
|
|
|
const capturedAt = new Date().toISOString();
|
|
const metaMap = new Map(device.meta.map((item) => [item.path, item]));
|
|
for (const field of fields) {
|
|
metaMap.set(field.path, {
|
|
path: field.path,
|
|
confidence: field.confidence,
|
|
sourceUrl: field.sourceUrl,
|
|
verified: false,
|
|
capturedAt,
|
|
});
|
|
}
|
|
|
|
return {
|
|
...device,
|
|
specs: enrichHardwareSpecs(specs),
|
|
meta: [...metaMap.values()],
|
|
};
|
|
}
|
|
|
|
/** 清空某字段的值与来源记录;摄像头位置被清空时回退到合法默认值 */
|
|
export function clearFieldValue(device: PhoneDevice, path: string): PhoneDevice {
|
|
const cameraMatch = cameraPathPattern.exec(path);
|
|
let specs: PhoneSpecs;
|
|
|
|
if (cameraMatch) {
|
|
const [, kind, indexText, key] = cameraMatch;
|
|
const index = Number(indexText);
|
|
const list = [
|
|
...(kind === 'frontCamera' ? device.specs.frontCamera : device.specs.rearCamera),
|
|
];
|
|
if (key === 'position') {
|
|
if (list[index]) {
|
|
list[index] = {
|
|
...list[index],
|
|
position: kind === 'frontCamera' ? 'front' : 'wide',
|
|
};
|
|
}
|
|
specs = { ...device.specs, [kind]: list } as PhoneSpecs;
|
|
} else {
|
|
specs = setValueAtPath(device.specs, path.replace(/^specs\./, ''), undefined);
|
|
}
|
|
} else {
|
|
specs = setValueAtPath(device.specs, path.replace(/^specs\./, ''), undefined);
|
|
}
|
|
|
|
return {
|
|
...device,
|
|
specs,
|
|
meta: device.meta.filter((item) => item.path !== path),
|
|
};
|
|
}
|
|
|
|
export function formatFieldValue(value: unknown): string {
|
|
if (value === undefined || value === null || value === '') return '—';
|
|
if (typeof value === 'boolean') return value ? '支持' : '不支持';
|
|
if (Array.isArray(value)) return value.length > 0 ? value.join(' / ') : '—';
|
|
if (typeof value === 'object') return JSON.stringify(value);
|
|
return String(value);
|
|
}
|