import { requestJSON } from './http-client.mjs';
try {
const model = process.env.API_MODEL;
const mode = process.argv[2] || 'single';
if (!/^[A-Za-z0-9_-][A-Za-z0-9._-]*$/.test(model || '') || !['single', 'batch'].includes(mode)) {
throw new Error('Set a bare API_MODEL and use single or batch.');
}
// Current gateway dispatch uses a case-sensitive "embed" path check.
if (mode === 'batch' && !model.includes('embed')) throw new Error('Batch requires an embedding model ID containing lowercase embed on this gateway revision.');
const texts = mode === 'single' ? ['A guide to API integration.']
: ['A guide to API integration.', 'A guide to vector retrieval.'];
const inputs = texts.map((text) => ({
model: `models/${model}`, content: { parts: [{ text }] }, taskType: 'RETRIEVAL_DOCUMENT',
}));
const action = mode === 'single' ? 'embedContent' : 'batchEmbedContents';
const result = await requestJSON(`/v1beta/models/${encodeURIComponent(model)}:${action}`,
mode === 'single' ? inputs[0] : { requests: inputs });
const vectors = mode === 'single' ? [result.embedding] : result.embeddings;
if (!Array.isArray(vectors) || vectors.length !== texts.length) throw new Error('Missing vectors.');
let dimensions;
for (const vector of vectors) {
const values = vector?.values;
if (!Array.isArray(values) || !values.length || !values.every((value) => typeof value === 'number' && Number.isFinite(value))
|| (dimensions !== undefined && dimensions !== values.length)) throw new Error('Invalid vector.');
dimensions = values.length;
}
console.log(JSON.stringify({ count: vectors.length, dimensions, vectors: vectors.map((item) => item.values) }));
} catch {
console.error('Gemini embedding request failed or returned invalid vectors. No automatic retry.');
process.exitCode = 1;
}