-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathai-provider-gemini-native-pdf.ts
More file actions
175 lines (156 loc) · 4.85 KB
/
ai-provider-gemini-native-pdf.ts
File metadata and controls
175 lines (156 loc) · 4.85 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
import type {
File as GeminiFile,
GenerateContentParameters,
GoogleGenAI
} from '@google/genai';
import { createPartFromUri, createUserContent } from '@google/genai';
import { AiProviderRequestError } from './ai-provider-errors';
import { buildOptimizeCvUserContent } from './ai-provider-openai-compatible-request';
import type { AiProviderRuntimeMetadata, AiSourceDocument } from './ai-provider-types';
import type { TargetPositions } from './upload/types';
const FILE_READY_POLL_INTERVAL_MS = 1000;
const FILE_READY_MAX_ATTEMPTS = 60;
interface OptimizeCvWithGeminiNativePdfParams {
client: GoogleGenAI;
model: string;
systemPrompt: string;
content: string;
targetPositions: TargetPositions;
sourceDocument: AiSourceDocument;
logDevelopment: (title: string, payload: string) => void;
runtimeMetadata: AiProviderRuntimeMetadata;
}
const wait = async (ms: number): Promise<void> => {
await new Promise((resolve) => {
setTimeout(resolve, ms);
});
};
const waitForUploadedFileToBecomeActive = async (
client: GoogleGenAI,
fileName: string
): Promise<GeminiFile> => {
for (let attempt = 1; attempt <= FILE_READY_MAX_ATTEMPTS; attempt += 1) {
const file = await client.files.get({ name: fileName });
if (file.state === 'ACTIVE') {
return file;
}
if (file.state === 'FAILED') {
throw new AiProviderRequestError(
typeof file.error?.message === 'string' && file.error.message.trim().length > 0
? file.error.message
: 'El proveedor Gemini no pudo procesar el PDF subido.'
);
}
if (attempt < FILE_READY_MAX_ATTEMPTS) {
await wait(FILE_READY_POLL_INTERVAL_MS);
}
}
throw new AiProviderRequestError('El PDF tardó demasiado en estar listo para procesamiento nativo.');
};
const uploadPdfForNativeProcessing = async (
client: GoogleGenAI,
sourceDocument: AiSourceDocument
): Promise<GeminiFile> => {
let uploadedFile: GeminiFile;
try {
uploadedFile = await client.files.upload({
file: sourceDocument.file,
config: {
displayName: sourceDocument.file.name || 'cv.pdf',
mimeType: sourceDocument.mimeType || 'application/pdf'
}
});
} catch (error) {
throw new AiProviderRequestError(
'No se pudo subir el PDF al Files API de Gemini para procesamiento nativo.',
error
);
}
if (!uploadedFile.name) {
throw new AiProviderRequestError(
'La subida del PDF no devolvio un identificador de archivo valido.'
);
}
if (uploadedFile.state === 'ACTIVE') {
return uploadedFile;
}
return waitForUploadedFileToBecomeActive(client, uploadedFile.name);
};
/**
* Uses Gemini Files API for the PDF-native optimization path while keeping the public service generic.
*/
export const optimizeCvWithGeminiNativePdf = async ({
client,
model,
systemPrompt,
content,
targetPositions,
sourceDocument,
logDevelopment,
runtimeMetadata
}: OptimizeCvWithGeminiNativePdfParams): Promise<string> => {
const userContent = buildOptimizeCvUserContent(content, targetPositions, true);
let uploadedPdfName: string | null = null;
logDevelopment(
'Gemini PDF native request metadata',
JSON.stringify(
{
codingEnvironment: runtimeMetadata.codingEnvironment,
provider: runtimeMetadata.provider,
model,
rawContentLength: content.length,
normalizedContentLength: userContent.length,
maxOutputTokens: runtimeMetadata.maxOutputTokens,
temperature: runtimeMetadata.temperature,
topP: runtimeMetadata.topP,
targetPositions,
targetPositionsCount: targetPositions.length,
hasPdfAttachment: true
},
null,
2
)
);
logDevelopment('System prompt', systemPrompt);
logDevelopment('Content sent to AI', userContent);
try {
const uploadedPdf = await uploadPdfForNativeProcessing(client, sourceDocument);
uploadedPdfName = uploadedPdf.name ?? null;
if (!uploadedPdf.uri || !uploadedPdf.mimeType) {
throw new AiProviderRequestError(
'No se recibio una URI valida del PDF subido para Gemini.'
);
}
const request: GenerateContentParameters = {
model,
contents: [
createUserContent([createPartFromUri(uploadedPdf.uri, uploadedPdf.mimeType), userContent])
],
config: {
systemInstruction: systemPrompt,
maxOutputTokens: runtimeMetadata.maxOutputTokens,
temperature: runtimeMetadata.temperature,
topP: runtimeMetadata.topP
}
};
const result = await client.models.generateContent(request);
const text = result.text?.trim();
if (!text) {
throw new AiProviderRequestError('La IA no devolvio contenido util para el CV.');
}
logDevelopment('Gemini PDF native usage metadata', JSON.stringify(result.usageMetadata ?? {}, null, 2));
logDevelopment('AI response', text);
return text;
} finally {
if (uploadedPdfName) {
try {
await client.files.delete({ name: uploadedPdfName });
} catch (error) {
logDevelopment(
'Gemini PDF cleanup warning',
error instanceof Error ? error.message : 'No se pudo eliminar el archivo temporal.'
);
}
}
}
};