20 lines
1.2 KiB
JavaScript
20 lines
1.2 KiB
JavaScript
import { createHash } from 'node:crypto';
|
|
|
|
const hash = value => createHash('sha256').update(JSON.stringify(value)).digest('hex');
|
|
export const requestPath = run => run.purpose === 'diagnostic' ? { kind: 'diagnostic', key: 'diagnostic:' + run.id }
|
|
: run.workerId ? { kind: 'worker', key: 'worker:' + run.workerId } : { kind: 'main', key: 'main' };
|
|
export const providerHistory = (session, run) => run.diagnosticMessages ?? session.providerMessages;
|
|
|
|
export function observeRequest(store, session, run, history, toolSurface = null) {
|
|
const path = requestPath(run);
|
|
const inputs = [...store.core.run.values()].filter(value => value.sessionId === session.id && value.inputMessage
|
|
&& requestPath(value).key === path.key);
|
|
return { schemaVersion: 1, path, providerHistorySha256: hash(history), toolSurface,
|
|
userMessageInstances: history.flatMap((message, index) => {
|
|
if (message.role !== 'user') return [];
|
|
const input = inputs.find(value => value.inputMessage.providerIndex === index)?.inputMessage;
|
|
return [{ id: input?.id ?? session.id + ':' + path.key + ':provider:' + index, providerIndex: index,
|
|
origin: input?.origin ?? (input ? 'user' : 'unclassified_provider_user'), sha256: hash(message) }];
|
|
}) };
|
|
}
|