@lerianstudio/matcher-mcp 5.0.0-beta.60 → 5.0.0-beta.62
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -21,6 +21,16 @@ const ANTHROPIC_DEFAULT_MODEL = 'claude-opus-4-8';
|
|
|
21
21
|
// ponytail: placeholder name/input is a known ceiling — enrich LLMToolResult
|
|
22
22
|
// with the originating name/input if the model needs to see its prior args.
|
|
23
23
|
const SYNTHETIC_TOOL_USE_NAME = 'tool';
|
|
24
|
+
/**
|
|
25
|
+
* The prompt-cache breakpoint marker, at the default 5-minute TTL.
|
|
26
|
+
*
|
|
27
|
+
* 5m, not 1h: the 1-hour TTL costs 2x on the write instead of 1.25x and only
|
|
28
|
+
* pays off when requests sharing the prefix start more than five minutes
|
|
29
|
+
* apart. Every round inside an agentic turn starts seconds after the last, and
|
|
30
|
+
* a read refreshes the entry's timer for free, so a busy console keeps the
|
|
31
|
+
* 5-minute entry alive indefinitely at the cheaper write price.
|
|
32
|
+
*/
|
|
33
|
+
const CACHE_EPHEMERAL = { type: 'ephemeral' };
|
|
24
34
|
/**
|
|
25
35
|
* Build a provider backed by Anthropic. When `streamFn` is omitted the key is
|
|
26
36
|
* required (its absence throws CopilotUnconfiguredError here, at construction);
|
|
@@ -31,10 +41,31 @@ export function createAnthropicProvider(config, streamFn) {
|
|
|
31
41
|
return {
|
|
32
42
|
async streamTurn(input, handlers) {
|
|
33
43
|
const tools = input.tools.map(toAnthropicTool);
|
|
44
|
+
// Prompt caching, two of the API's four allowed breakpoints.
|
|
45
|
+
//
|
|
46
|
+
// The request renders as tools -> system -> messages, and everything up
|
|
47
|
+
// to the end of `system` is byte-identical on every round of every turn:
|
|
48
|
+
// `buildSystemPrompt` returns a frozen module constant with nothing
|
|
49
|
+
// interpolated into it (the operator's screen context is deliberately a
|
|
50
|
+
// user message, not part of the system tier), and the tool catalogue is
|
|
51
|
+
// memoized over the immutable spec index. That static floor is the whole
|
|
52
|
+
// prize (it dwarfs the conversation), so breakpoint 1 goes on the last
|
|
53
|
+
// (only) system block, which caches the tools ahead of it in the same
|
|
54
|
+
// entry. Nothing had to be moved out of it; it was already stable, just
|
|
55
|
+
// unmarked.
|
|
56
|
+
//
|
|
57
|
+
// Breakpoint 2 is the top-level marker: the API places it on the last
|
|
58
|
+
// cacheable block and walks it forward as the request grows, which is
|
|
59
|
+
// exactly the shape of the tail here: a turn accumulates tool results
|
|
60
|
+
// round over round, and the browser replays the whole conversation on
|
|
61
|
+
// the next POST. A write bills only the delta past the highest cache
|
|
62
|
+
// hit, so this second entry costs the messages delta, never the prefix
|
|
63
|
+
// breakpoint 1 already covers.
|
|
34
64
|
const params = {
|
|
35
65
|
model: config.copilotModel ?? ANTHROPIC_DEFAULT_MODEL,
|
|
36
66
|
max_tokens: ANTHROPIC_MAX_TOKENS,
|
|
37
|
-
system: input.system,
|
|
67
|
+
system: [{ type: 'text', text: input.system, cache_control: CACHE_EPHEMERAL }],
|
|
68
|
+
cache_control: CACHE_EPHEMERAL,
|
|
38
69
|
messages: buildAnthropicMessages(input),
|
|
39
70
|
...(tools.length > 0 ? { tools } : {}),
|
|
40
71
|
};
|
|
@@ -46,13 +77,25 @@ export function createAnthropicProvider(config, streamFn) {
|
|
|
46
77
|
// neither, so the loop meters this round as 0 (fail-open).
|
|
47
78
|
let inputTokens;
|
|
48
79
|
let outputTokens;
|
|
80
|
+
// Prompt-cache halves of the input, tracked separately so `usageFrom`
|
|
81
|
+
// can both fold them into the true prompt size and report them raw. Read
|
|
82
|
+
// off message_start with message_delta (also cumulative, and nullable
|
|
83
|
+
// there) as a refresh, so a stream that only populates them on the delta
|
|
84
|
+
// is still accounted rather than silently metered as an uncached turn.
|
|
85
|
+
let cacheCreationTokens;
|
|
86
|
+
let cacheReadTokens;
|
|
49
87
|
for await (const event of openStream(params)) {
|
|
50
88
|
if (event.type === 'message_start') {
|
|
51
89
|
inputTokens = event.message.usage.input_tokens;
|
|
52
90
|
outputTokens = event.message.usage.output_tokens;
|
|
91
|
+
cacheCreationTokens = event.message.usage.cache_creation_input_tokens ?? undefined;
|
|
92
|
+
cacheReadTokens = event.message.usage.cache_read_input_tokens ?? undefined;
|
|
53
93
|
}
|
|
54
94
|
else if (event.type === 'message_delta') {
|
|
55
95
|
outputTokens = event.usage.output_tokens;
|
|
96
|
+
cacheCreationTokens =
|
|
97
|
+
event.usage.cache_creation_input_tokens ?? cacheCreationTokens;
|
|
98
|
+
cacheReadTokens = event.usage.cache_read_input_tokens ?? cacheReadTokens;
|
|
56
99
|
}
|
|
57
100
|
else if (event.type === 'content_block_start' &&
|
|
58
101
|
event.content_block.type === 'tool_use') {
|
|
@@ -74,7 +117,12 @@ export function createAnthropicProvider(config, streamFn) {
|
|
|
74
117
|
}
|
|
75
118
|
}
|
|
76
119
|
}
|
|
77
|
-
const usage = usageFrom(
|
|
120
|
+
const usage = usageFrom({
|
|
121
|
+
inputTokens,
|
|
122
|
+
outputTokens,
|
|
123
|
+
cacheCreationTokens,
|
|
124
|
+
cacheReadTokens,
|
|
125
|
+
});
|
|
78
126
|
if (toolBlocks.size === 0) {
|
|
79
127
|
// ponytail: turn-end is inferred from the absence of tool_use blocks —
|
|
80
128
|
// we do NOT inspect stop_reason, so a max_tokens-truncated text answer
|
|
@@ -104,14 +152,32 @@ function defaultAnthropicStream(config) {
|
|
|
104
152
|
}
|
|
105
153
|
/**
|
|
106
154
|
* Build an LLMUsage from the tracked token counts, or `undefined` when the
|
|
107
|
-
* stream reported
|
|
108
|
-
* missing
|
|
155
|
+
* stream reported no usage at all (so the loop meters this round as 0, which
|
|
156
|
+
* is fail-open). A missing field defaults to 0 rather than dropping the whole
|
|
157
|
+
* usage.
|
|
158
|
+
*
|
|
159
|
+
* `inputTokens` is deliberately NOT the API's `input_tokens`. With caching on,
|
|
160
|
+
* that field counts only the uncached remainder. The total prompt is
|
|
161
|
+
* `input_tokens + cache_creation_input_tokens + cache_read_input_tokens`, so
|
|
162
|
+
* the cached halves are added back here. Reporting the remainder instead would
|
|
163
|
+
* make every cached round look ~25k tokens cheaper than it is to the two
|
|
164
|
+
* consumers that read this number: the per-turn ceiling in `runAgentTurn`
|
|
165
|
+
* (which would stop tripping on a runaway) and the copilot token metric (which
|
|
166
|
+
* would under-report the prompt floor by the whole cached prefix). The raw
|
|
167
|
+
* halves ride alongside, unfolded, as the cache-hit evidence.
|
|
109
168
|
*/
|
|
110
|
-
function usageFrom(
|
|
111
|
-
if (inputTokens === undefined && outputTokens === undefined) {
|
|
169
|
+
function usageFrom(raw) {
|
|
170
|
+
if (raw.inputTokens === undefined && raw.outputTokens === undefined) {
|
|
112
171
|
return undefined;
|
|
113
172
|
}
|
|
114
|
-
|
|
173
|
+
const cacheCreationTokens = raw.cacheCreationTokens ?? 0;
|
|
174
|
+
const cacheReadTokens = raw.cacheReadTokens ?? 0;
|
|
175
|
+
return {
|
|
176
|
+
inputTokens: (raw.inputTokens ?? 0) + cacheCreationTokens + cacheReadTokens,
|
|
177
|
+
outputTokens: raw.outputTokens ?? 0,
|
|
178
|
+
cacheCreationTokens,
|
|
179
|
+
cacheReadTokens,
|
|
180
|
+
};
|
|
115
181
|
}
|
|
116
182
|
function toAnthropicTool(def) {
|
|
117
183
|
return {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"provider-anthropic.js","sourceRoot":"","sources":["../../src/copilot/provider-anthropic.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,EAAE;AACF,2EAA2E;AAC3E,8EAA8E;AAC9E,wCAAwC;AAExC,OAAO,SAAS,MAAM,mBAAmB,CAAA;AAGzC,OAAO,EAAE,wBAAwB,EAAE,MAAM,uBAAuB,CAAA;AAChE,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAA;AAWrD,gFAAgF;AAChF,8BAA8B;AAC9B,oEAAoE;AACpE,MAAM,oBAAoB,GAAG,IAAI,CAAA;AAEjC,gFAAgF;AAChF,6EAA6E;AAC7E,kFAAkF;AAClF,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