UNPKG

@sentry/core

Version:
101 lines (97 loc) • 4.25 kB
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); const attributes = require('@sentry/conventions/attributes'); const validation = require('./validation.js'); function buildAllContentItemAttributes(content, includeContent) { const attributes$1 = { [attributes.MCP_TOOL_RESULT_CONTENT_COUNT]: content.length }; for (const [i, item] of content.entries()) { if (!validation.isValidContentItem(item)) { continue; } const single = content.length === 1; const index = String(i); if (typeof item.type === "string") { const key = single ? attributes.MCP_TOOL_RESULT_CONTENT_TYPE : attributes.MCP_TOOL_RESULT_KEY_CONTENT_TYPE.replace("<key>", index); attributes$1[key] = item.type; } if (includeContent) { const safeSet = (key, value) => { if (typeof value === "string") { attributes$1[key] = value; } }; safeSet( single ? attributes.MCP_TOOL_RESULT_MIME_TYPE : attributes.MCP_TOOL_RESULT_KEY_MIME_TYPE.replace("<key>", index), item.mimeType ); safeSet(single ? attributes.MCP_TOOL_RESULT_URI : attributes.MCP_TOOL_RESULT_KEY_URI.replace("<key>", index), item.uri); safeSet(single ? attributes.MCP_TOOL_RESULT_NAME : attributes.MCP_TOOL_RESULT_KEY_NAME.replace("<key>", index), item.name); if (typeof item.text === "string") { const attrName = single ? attributes.MCP_TOOL_RESULT_CONTENT : attributes.MCP_TOOL_RESULT_KEY_CONTENT.replace("<key>", index); attributes$1[attrName] = item.text; } if (typeof item.data === "string") { const key = single ? attributes.MCP_TOOL_RESULT_DATA_SIZE : attributes.MCP_TOOL_RESULT_KEY_DATA_SIZE.replace("<key>", index); attributes$1[key] = item.data.length; } const resource = item.resource; if (validation.isValidContentItem(resource)) { safeSet( single ? attributes.MCP_TOOL_RESULT_RESOURCE_URI : attributes.MCP_TOOL_RESULT_KEY_RESOURCE_URI.replace("<key>", index), resource.uri ); safeSet( single ? attributes.MCP_TOOL_RESULT_RESOURCE_MIME_TYPE : attributes.MCP_TOOL_RESULT_KEY_RESOURCE_MIME_TYPE.replace("<key>", index), resource.mimeType ); } } } return attributes$1; } function extractToolResultAttributes(result, recordOutputs) { if (!validation.isValidContentItem(result)) { return {}; } const attributes$1 = Array.isArray(result.content) ? buildAllContentItemAttributes(result.content, recordOutputs) : {}; if (typeof result.isError === "boolean") { attributes$1[attributes.MCP_TOOL_RESULT_IS_ERROR] = result.isError; } return attributes$1; } function extractPromptResultAttributes(result, recordOutputs) { const attributes$1 = {}; if (!validation.isValidContentItem(result)) { return attributes$1; } if (recordOutputs && typeof result.description === "string") { attributes$1[attributes.MCP_PROMPT_RESULT_DESCRIPTION] = result.description; } if (Array.isArray(result.messages)) { attributes$1[attributes.MCP_PROMPT_RESULT_MESSAGE_COUNT] = result.messages.length; if (recordOutputs) { const messages = result.messages; for (const [i, message] of messages.entries()) { if (!validation.isValidContentItem(message)) { continue; } if (typeof message.role === "string") { const attrName = messages.length === 1 ? attributes.MCP_PROMPT_RESULT_MESSAGE_ROLE : attributes.MCP_PROMPT_RESULT_KEY_ROLE.replace("<key>", String(i)); attributes$1[attrName] = message.role; } if (validation.isValidContentItem(message.content)) { const content = message.content; if (typeof content.text === "string") { const attrName = messages.length === 1 ? attributes.MCP_PROMPT_RESULT_MESSAGE_CONTENT : attributes.MCP_PROMPT_RESULT_KEY_CONTENT.replace("<key>", String(i)); attributes$1[attrName] = content.text; } } } } } return attributes$1; } exports.extractPromptResultAttributes = extractPromptResultAttributes; exports.extractToolResultAttributes = extractToolResultAttributes; //# sourceMappingURL=resultExtraction.js.map