压缩请求与真实 callModel 合同
策略的具体实现如下。
ts
export function createContextCompressor(options: {
id: string;
instruction: string;
modelId?: string;
transport: string;
maxOutputTokens: number;
parse(value: unknown): ContextFragment['value'];
}): ContextCompressor {
const { id, instruction, modelId, transport, maxOutputTokens } = options;
breakWhile(typeof options.parse !== 'function', 'Context compressor requires a result parser');
const parse = options.parse.bind(options);
return Object.freeze({
id,
async compress(call: ContextCompressionCall) {
const result = await call.callModel({
// Suffix the stage ID so compressor calls are attributable in diagnostics and budgets.
stageId: `${call.stageId}:compress:${id}`,
modelId,
input: [
{ role: 'system', content: instruction },
{
role: 'user',
content: JSON.stringify({
sourceId: call.fragment.sourceId,
value: call.fragment.value,
targetChars: call.targetChars,
}),
},
],
maxOutputTokens,
transport,
});
return synchronous(parse(result.value), 'Context result parsers must be synchronous');
},
});
}