File size: 3,622 Bytes
e81f4e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { isRecord } from "../shared/types";
import { imageFilenameFromObject, normalizeUploadFileInput } from "./input";
import { sanitizeUploadFilename } from "./mime";
import type { AttachmentFileRef } from "./types";

type ExistingRefState = {
	out: AttachmentFileRef[];
	seen: Set<string>;
};

export function collectOpenAIRefFileIDs(
	req: unknown,
): AttachmentFileRef[] | null {
	if (!isRecord(req)) return null;
	const state: ExistingRefState = { out: [], seen: new Set() };
	for (const key of [
		"ref_file_ids",
		"file_ids",
		"attachments",
		"messages",
		"input",
	]) {
		const raw = req[key];
		if (raw == null) continue;
		if ((key === "messages" || key === "input") && typeof raw === "string")
			continue;
		appendOpenAIRefFileIDs(state, raw);
	}
	return state.out.length ? state.out : null;
}

export function appendExistingFileRefs(
	out: AttachmentFileRef[],
	refs: unknown,
): void {
	const state: ExistingRefState = {
		out,
		seen: new Set(out.map(refKey).filter(Boolean) as string[]),
	};
	appendDirectOrOpenAIRefFileIDs(state, refs);
}

function appendOpenAIRefFileIDs(state: ExistingRefState, raw: unknown): void {
	if (raw == null) return;
	if (typeof raw === "string") {
		addOpenAIRefFileID(state, raw);
		return;
	}
	if (Array.isArray(raw)) {
		for (const item of raw) appendOpenAIRefFileIDs(state, item);
		return;
	}
	if (!isRecord(raw)) return;

	const rawFilename = imageFilenameFromObject(raw);
	if (raw.file_id != null) addOpenAIRefFileID(state, raw.file_id, rawFilename);
	const typ = String(raw.type || "")
		.trim()
		.toLowerCase();
	const hasInlinePayload =
		typ.includes("file") && !!normalizeUploadFileInput(raw);
	if (typ.includes("file") && !hasInlinePayload && raw.id != null)
		addOpenAIRefFileID(state, raw.id, rawFilename);
	const file = isRecord(raw.file) ? raw.file : null;
	if (file) {
		const fileFilename = imageFilenameFromObject(file) || rawFilename;
		const nestedInlinePayload = !!normalizeUploadFileInput(file);
		if (file.file_id != null)
			addOpenAIRefFileID(state, file.file_id, fileFilename);
		if (!nestedInlinePayload && file.id != null)
			addOpenAIRefFileID(state, file.id, fileFilename);
	}
	for (const key of [
		"ref_file_ids",
		"file_ids",
		"attachments",
		"messages",
		"input",
		"content",
		"files",
		"items",
		"data",
		"source",
	]) {
		if (!(key in raw)) continue;
		if (hasInlinePayload && (key === "data" || key === "source")) continue;
		const nested = raw[key];
		if ((key === "content" || key === "input") && typeof nested === "string")
			continue;
		appendOpenAIRefFileIDs(state, nested);
	}
}

function appendDirectOrOpenAIRefFileIDs(
	state: ExistingRefState,
	refs: unknown,
): void {
	if (refs == null) return;
	if (typeof refs === "string") {
		addOpenAIRefFileID(state, refs);
		return;
	}
	if (Array.isArray(refs)) {
		for (const ref of refs) appendDirectOrOpenAIRefFileIDs(state, ref);
		return;
	}
	if (isRecord(refs)) {
		const id = refs.ref || refs.fileRef || refs.id || refs.file_id;
		const name = refs.name || refs.filename || refs.file_name;
		if (id != null) {
			addOpenAIRefFileID(state, id, name);
			return;
		}
	}
	appendOpenAIRefFileIDs(state, refs);
}

function addOpenAIRefFileID(
	state: ExistingRefState,
	fileID: unknown,
	filename: unknown = undefined,
): void {
	const id = String(fileID || "").trim();
	if (!id || state.seen.has(id)) return;
	state.seen.add(id);
	const name = sanitizeUploadFilename(filename);
	state.out.push(name ? { id, name } : id);
}

function refKey(ref: AttachmentFileRef): string {
	if (typeof ref === "string") return ref;
	return ref.ref || ref.fileRef || ref.id || "";
}