agentfootprint
Version:
The explainable agent framework — backtrack a wrong answer to the exact context that caused it (evidence, not guesses). Built on footprintjs.
53 lines • 2.16 kB
JavaScript
import { emitEmbedding } from './emitEmbedding.js';
/** Default: extract plaintext from any supported content shape. */
function defaultTextFrom(message) {
return message.content ?? '';
}
/**
* Build the `embedMessages` stage. Prefers `embedBatch()` when the
* embedder implements it (one round-trip for the whole turn), otherwise
* falls back to N sequential `embed()` calls.
*/
export function embedMessages(config) {
const { embedder } = config;
const textFrom = config.textFrom ?? defaultTextFrom;
return async (scope) => {
const messages = (scope.newMessages ?? []);
if (messages.length === 0) {
scope.newMessageEmbeddings = [];
return;
}
const texts = messages.map(textFrom);
const signal = scope.$getEnv?.()?.signal;
const startedAt = Date.now();
let vectors;
if (embedder.embedBatch) {
vectors = (await embedder.embedBatch({
texts,
...(signal ? { signal } : {}),
}));
}
else {
vectors = await Promise.all(texts.map((text) => embedder.embed({ text, ...(signal ? { signal } : {}) })));
}
// The INDEX-time half of the two-phase cost model (8.9.0). Embedding cost
// splits in two — this side scales with the size of what you store, and
// the query side scales with traffic — and telling them apart is the whole
// reason for a durable index. `agentfootprint.embedding.generated` has been
// declared since 2.x with an `inputKind` field and no emitter; this is one
// of its two.
emitEmbedding(scope, {
model: config.embedderId ?? embedder.id ?? 'unknown',
provider: 'custom',
inputKind: 'document',
dimension: embedder.dimensions,
count: vectors.length,
durationMs: Date.now() - startedAt,
});
scope.newMessageEmbeddings = vectors;
if (config.embedderId !== undefined) {
scope.newMessageEmbeddingModel = config.embedderId;
}
};
}
//# sourceMappingURL=embedMessages.js.map