mirror of
https://github.com/tvytlx/ai-agent-deep-dive.git
synced 2026-04-05 08:34:47 +08:00
Add extracted source directory and README navigation
This commit is contained in:
75
extracted-source/node_modules/@anthropic-ai/sdk/lib/beta-parser.mjs
generated
vendored
Normal file
75
extracted-source/node_modules/@anthropic-ai/sdk/lib/beta-parser.mjs
generated
vendored
Normal file
@@ -0,0 +1,75 @@
|
||||
import { AnthropicError } from "../core/error.mjs";
|
||||
function getOutputFormat(params) {
|
||||
// Prefer output_format (deprecated) over output_config.format for backward compatibility
|
||||
return params?.output_format ?? params?.output_config?.format;
|
||||
}
|
||||
export function maybeParseBetaMessage(message, params, opts) {
|
||||
const outputFormat = getOutputFormat(params);
|
||||
if (!params || !('parse' in (outputFormat ?? {}))) {
|
||||
return {
|
||||
...message,
|
||||
content: message.content.map((block) => {
|
||||
if (block.type === 'text') {
|
||||
const parsedBlock = Object.defineProperty({ ...block }, 'parsed_output', {
|
||||
value: null,
|
||||
enumerable: false,
|
||||
});
|
||||
return Object.defineProperty(parsedBlock, 'parsed', {
|
||||
get() {
|
||||
opts.logger.warn('The `parsed` property on `text` blocks is deprecated, please use `parsed_output` instead.');
|
||||
return null;
|
||||
},
|
||||
enumerable: false,
|
||||
});
|
||||
}
|
||||
return block;
|
||||
}),
|
||||
parsed_output: null,
|
||||
};
|
||||
}
|
||||
return parseBetaMessage(message, params, opts);
|
||||
}
|
||||
export function parseBetaMessage(message, params, opts) {
|
||||
let firstParsedOutput = null;
|
||||
const content = message.content.map((block) => {
|
||||
if (block.type === 'text') {
|
||||
const parsedOutput = parseBetaOutputFormat(params, block.text);
|
||||
if (firstParsedOutput === null) {
|
||||
firstParsedOutput = parsedOutput;
|
||||
}
|
||||
const parsedBlock = Object.defineProperty({ ...block }, 'parsed_output', {
|
||||
value: parsedOutput,
|
||||
enumerable: false,
|
||||
});
|
||||
return Object.defineProperty(parsedBlock, 'parsed', {
|
||||
get() {
|
||||
opts.logger.warn('The `parsed` property on `text` blocks is deprecated, please use `parsed_output` instead.');
|
||||
return parsedOutput;
|
||||
},
|
||||
enumerable: false,
|
||||
});
|
||||
}
|
||||
return block;
|
||||
});
|
||||
return {
|
||||
...message,
|
||||
content,
|
||||
parsed_output: firstParsedOutput,
|
||||
};
|
||||
}
|
||||
function parseBetaOutputFormat(params, content) {
|
||||
const outputFormat = getOutputFormat(params);
|
||||
if (outputFormat?.type !== 'json_schema') {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
if ('parse' in outputFormat) {
|
||||
return outputFormat.parse(content);
|
||||
}
|
||||
return JSON.parse(content);
|
||||
}
|
||||
catch (error) {
|
||||
throw new AnthropicError(`Failed to parse structured output: ${error}`);
|
||||
}
|
||||
}
|
||||
//# sourceMappingURL=beta-parser.mjs.map
|
||||
Reference in New Issue
Block a user