Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Aquaticat/Monochromatic. TypeScript and Rust monorepo: Claude Code and Pi coding-agent plugins, a m
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229// oxlint-disable typescript/no-unsafe-type-assertion, prefer-destructuring -- API response types require assertionsimport { tagged, } from '@monochromatic-dev/module-logger/ts';
import { toVoyageContentItem, } from './encoding.voyage.ts';import type { BatchEmbeddingResult, EmbeddingProvider, EmbeddingResult, ImageDiffConfig, ImageInput, VoyageModel,} from './types.ts';import type { VoyageApiRequest, } from './types.voyage-api.ts';import { callVoyageApi, resolveVoyageApiKey,} from './voyage.api.ts';
/** Logger root for image-diff after removing the package log shim. @example ```ts const rl = tagged({ tag: someFunction.name, l, },); ``` */const l = tagged({ tag: 'image-diff', },);
/** Default Voyage model; latest and highest quality. */const DEFAULT_VOYAGE_MODEL: VoyageModel = 'voyage-multimodal-3.5';
/** Compute a single image embedding via the Voyage AI API. @param input - image to embed, in any supported format @param config - client configuration @returns embedding vector and usage metadata @example ```ts const { embedding } = await voyageEmbed({ input: { path: './photo.png' }, config: {} }); ``` */async function voyageEmbed({ input, config,}: { readonly input: ImageInput; readonly config: ImageDiffConfig;},): Promise<EmbeddingResult> { /** Logger pre-tagged with this function's name so call-site context is preserved across debug lines. */ const rl = tagged({ tag: voyageEmbed.name, l, },); rl.debug('computing single image embedding via Voyage',);
/** Resolved Voyage credential; pulled here once and forwarded into the API call. */ const apiKey = resolveVoyageApiKey(config.apiKey,); /** Effective model id; user override or {@link DEFAULT_VOYAGE_MODEL}. */ const model = (config.model ?? DEFAULT_VOYAGE_MODEL) as VoyageModel; /** Voyage-shaped content payload converted from the caller's image input. */ const contentItem = await toVoyageContentItem(input,);
/** Single-input request body wrapping the content item in Voyage's nested `inputs[].content[]` shape. */ const request: VoyageApiRequest = { inputs: [{ content: [contentItem,], },], model, input_type: 'document', truncation: true, };
/** Voyage API response; contains the embedding plus usage counters. */ const response = await callVoyageApi({ requestBody: request, apiKey, },); /** First (and only) data entry; guarded by the empty-array check below before use. */ const firstData = response.data[0]; if (firstData === undefined) throw new Error('Voyage API returned empty data array',);
return { embedding: firstData.embedding, usage: { textTokens: response.usage .text_tokens, imagePixels: response.usage .image_pixels, totalTokens: response.usage .total_tokens, }, };}
/** Compute embeddings for multiple images in a single batch via the Voyage AI API. @param inputs - array of images to embed @param config - client configuration @returns embedding vectors (in input order) and aggregate usage metadata @example ```ts const { embeddings } = await voyageEmbedBatch({ inputs: [{ path: 'a.png' }, { path: 'b.png' }], config: {}, }); ``` */async function voyageEmbedBatch({ inputs, config,}: { readonly inputs: readonly ImageInput[]; readonly config: ImageDiffConfig;},): Promise<BatchEmbeddingResult> { /** Logger pre-tagged with this function's name so call-site context is preserved across debug lines. */ const rl = tagged({ tag: voyageEmbedBatch.name, l, },); rl.debug( `computing batch embeddings via Voyage for ${String(inputs.length,)} image(s)`, );
/** Resolved Voyage credential; pulled here once and forwarded into the API call. */ const apiKey = resolveVoyageApiKey(config.apiKey,); /** Effective model id; user override or {@link DEFAULT_VOYAGE_MODEL}. */ const model = (config.model ?? DEFAULT_VOYAGE_MODEL) as VoyageModel;
/** Voyage-shaped content payloads converted from each caller image, in input order. */ const contentItems = await Promise.all( inputs.map(function convertInput(input,) { return toVoyageContentItem(input,); },), );
/** Batched request body wrapping each content item in its own `inputs[]` entry. */ const request: VoyageApiRequest = { inputs: contentItems.map(function wrapContent(item,) { return { content: [item,], }; },), model, input_type: 'document', truncation: true, };
/** Voyage API response; data entries may arrive out of order and need re-sorting below. */ const response = await callVoyageApi({ requestBody: request, apiKey, },);
/** Sort by index to guarantee input order. */ const sorted = [...response.data,].toSorted(function byIndex( a, b, ) { return a.index - b .index; },);
return { embeddings: sorted.map(function extractEmbedding(d,) { return d.embedding; },), usage: { textTokens: response.usage .text_tokens, imagePixels: response.usage .image_pixels, totalTokens: response.usage .total_tokens, }, };}
/** Voyage AI embedding provider. Implements the {@link EmbeddingProvider} interface for the Voyage multimodal API. @example ```ts const result = await voyageProvider.embed({ path: 'photo.png' }, {}); ``` */export const voyageProvider: EmbeddingProvider = { embed: voyageEmbed, embedBatch: voyageEmbedBatch,};