@anthropic-ai/sdk
Version:
The official TypeScript library for the Anthropic API
30 lines • 995 B
JavaScript
import { partialParse } from "../_vendor/partial-json-parser/parser.mjs";
export const JSON_BUF_PROPERTY = '__json_buf';
/**
* Copies a tool-use block with an updated `__json_buf`, installing `.input` as
* a memoized getter so the partial-JSON parse happens on first read instead of
* on every delta.
*/
export function withLazyInput(prev, jsonBuf) {
const next = {};
for (const key of Object.keys(prev)) {
if (key !== 'input')
next[key] = prev[key];
}
Object.defineProperty(next, JSON_BUF_PROPERTY, { value: jsonBuf, enumerable: false, writable: true });
let input;
let parsed = false;
Object.defineProperty(next, 'input', {
enumerable: true,
configurable: true,
get() {
if (!parsed) {
input = jsonBuf ? partialParse(jsonBuf) : {};
parsed = true;
}
return input;
},
});
return next;
}
//# sourceMappingURL=message-stream-utils.mjs.map