@quilltap/plugin-utils 1.0.0

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.
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts","../src/tools/parsers.ts","../src/tools/converters.ts","../src/logging/plugin-logger.ts","../src/logging/index.ts"],"sourcesContent":["/**\n * @quilltap/plugin-utils\n *\n * Utility functions for Quilltap plugin development.\n *\n * This package provides runtime utilities that complement the type definitions\n * in @quilltap/plugin-types. It includes:\n *\n * - **Tool Parsing**: Parse tool calls from any LLM provider's response format\n * - **Tool Conversion**: Convert between OpenAI, Anthropic, and Google tool formats\n * - **Logger Bridge**: Logging that integrates with Quilltap's core or runs standalone\n *\n * @packageDocumentation\n * @module @quilltap/plugin-utils\n */\n\n// ============================================================================\n// Tool Utilities\n// ============================================================================\n\nexport {\n // Parsers\n parseToolCalls,\n parseOpenAIToolCalls,\n parseAnthropicToolCalls,\n parseGoogleToolCalls,\n detectToolCallFormat,\n hasToolCalls,\n\n // Converters\n convertToAnthropicFormat,\n convertToGoogleFormat,\n convertFromAnthropicFormat,\n convertFromGoogleFormat,\n convertToolTo,\n convertToolsTo,\n applyDescriptionLimit,\n} from './tools';\n\nexport type {\n // Tool types (re-exported from plugin-types)\n OpenAIToolDefinition,\n UniversalTool,\n AnthropicToolDefinition,\n GoogleToolDefinition,\n ToolCall,\n ToolCallRequest,\n ToolResult,\n ToolFormatOptions,\n\n // Utility types\n ToolCallFormat,\n ToolConvertTarget,\n} from './tools';\n\n// ============================================================================\n// Logging Utilities\n// ============================================================================\n\nexport {\n // Plugin logger factory\n createPluginLogger,\n hasCoreLogger,\n getLogLevelFromEnv,\n\n // Re-exported from plugin-types\n createConsoleLogger,\n createNoopLogger,\n\n // Internal APIs for Quilltap core\n __injectCoreLoggerFactory,\n __clearCoreLoggerFactory,\n} from './logging';\n\nexport type {\n // Logger types\n PluginLoggerWithChild,\n PluginLogger,\n LogContext,\n LogLevel,\n} from './logging';\n\n// ============================================================================\n// Version\n// ============================================================================\n\n/**\n * Version of the plugin-utils package.\n * Can be used at runtime to check compatibility.\n */\nexport const PLUGIN_UTILS_VERSION = '1.0.0';\n","/**\n * Tool Call Parsers\n *\n * Provider-specific parsers for extracting tool calls from LLM responses.\n * Each parser converts from a provider's native format to the standardized\n * ToolCallRequest format.\n *\n * @module @quilltap/plugin-utils/tools/parsers\n */\n\nimport type { ToolCallRequest } from '@quilltap/plugin-types';\n\n/**\n * Supported tool call response formats\n */\nexport type ToolCallFormat = 'openai' | 'anthropic' | 'google' | 'auto';\n\n/**\n * Parse OpenAI format tool calls from LLM response\n *\n * Extracts tool calls from OpenAI/Grok API responses which return\n * tool_calls in the message object.\n *\n * Expected response structures:\n * - `response.tool_calls` (direct)\n * - `response.choices[0].message.tool_calls` (nested)\n *\n * @param response - The raw response from provider API\n * @returns Array of parsed tool call requests\n *\n * @example\n * ```typescript\n * const response = await openai.chat.completions.create({...});\n * const toolCalls = parseOpenAIToolCalls(response);\n * // Returns: [{ name: 'search_web', arguments: { query: 'hello' } }]\n * ```\n */\nexport function parseOpenAIToolCalls(response: unknown): ToolCallRequest[] {\n const toolCalls: ToolCallRequest[] = [];\n\n try {\n const resp = response as Record<string, unknown>;\n\n // Handle direct tool_calls array\n let toolCallsArray = resp?.tool_calls as unknown[] | undefined;\n\n // Check nested structure from streaming responses\n if (!toolCallsArray) {\n const choices = resp?.choices as\n | Array<{ message?: { tool_calls?: unknown[] } }>\n | undefined;\n toolCallsArray = choices?.[0]?.message?.tool_calls;\n }\n\n if (toolCallsArray && Array.isArray(toolCallsArray) && toolCallsArray.length > 0) {\n for (const toolCall of toolCallsArray) {\n const tc = toolCall as {\n type?: string;\n function?: { name: string; arguments: string };\n };\n\n if (tc.type === 'function' && tc.function) {\n toolCalls.push({\n name: tc.function.name,\n arguments: JSON.parse(tc.function.arguments || '{}'),\n });\n }\n }\n }\n } catch (error) {\n // Log error but don't throw - return empty array\n console.error('[plugin-utils] Error parsing OpenAI tool calls:', error);\n }\n\n return toolCalls;\n}\n\n/**\n * Parse Anthropic format tool calls from LLM response\n *\n * Extracts tool calls from Anthropic API responses which return\n * tool_use blocks in the content array.\n *\n * Expected response structure:\n * - `response.content` array with `{ type: 'tool_use', name, input }`\n *\n * @param response - The raw response from provider API\n * @returns Array of parsed tool call requests\n *\n * @example\n * ```typescript\n * const response = await anthropic.messages.create({...});\n * const toolCalls = parseAnthropicToolCalls(response);\n * // Returns: [{ name: 'search_web', arguments: { query: 'hello' } }]\n * ```\n */\nexport function parseAnthropicToolCalls(response: unknown): ToolCallRequest[] {\n const toolCalls: ToolCallRequest[] = [];\n\n try {\n const resp = response as Record<string, unknown>;\n\n if (!resp?.content || !Array.isArray(resp.content)) {\n return toolCalls;\n }\n\n for (const block of resp.content) {\n const b = block as { type?: string; name?: string; input?: Record<string, unknown> };\n\n if (b.type === 'tool_use' && b.name) {\n toolCalls.push({\n name: b.name,\n arguments: b.input || {},\n });\n }\n }\n } catch (error) {\n console.error('[plugin-utils] Error parsing Anthropic tool calls:', error);\n }\n\n return toolCalls;\n}\n\n/**\n * Parse Google Gemini format tool calls from LLM response\n *\n * Extracts tool calls from Google Gemini API responses which return\n * functionCall objects in the parts array.\n *\n * Expected response structure:\n * - `response.candidates[0].content.parts` array with `{ functionCall: { name, args } }`\n *\n * @param response - The raw response from provider API\n * @returns Array of parsed tool call requests\n *\n * @example\n * ```typescript\n * const response = await gemini.generateContent({...});\n * const toolCalls = parseGoogleToolCalls(response);\n * // Returns: [{ name: 'search_web', arguments: { query: 'hello' } }]\n * ```\n */\nexport function parseGoogleToolCalls(response: unknown): ToolCallRequest[] {\n const toolCalls: ToolCallRequest[] = [];\n\n try {\n const resp = response as Record<string, unknown>;\n const candidates = resp?.candidates as\n | Array<{ content?: { parts?: unknown[] } }>\n | undefined;\n const parts = candidates?.[0]?.content?.parts;\n\n if (!parts || !Array.isArray(parts)) {\n return toolCalls;\n }\n\n for (const part of parts) {\n const p = part as {\n functionCall?: { name: string; args?: Record<string, unknown> };\n };\n\n if (p.functionCall) {\n toolCalls.push({\n name: p.functionCall.name,\n arguments: p.functionCall.args || {},\n });\n }\n }\n } catch (error) {\n console.error('[plugin-utils] Error parsing Google tool calls:', error);\n }\n\n return toolCalls;\n}\n\n/**\n * Detect the format of a tool call response\n *\n * Analyzes the response structure to determine which provider format it uses.\n *\n * @param response - The raw response from a provider API\n * @returns The detected format, or null if unrecognized\n */\nexport function detectToolCallFormat(response: unknown): ToolCallFormat | null {\n if (!response || typeof response !== 'object') {\n return null;\n }\n\n const resp = response as Record<string, unknown>;\n\n // OpenAI format: has tool_calls directly or in choices[0].message\n if (resp.tool_calls && Array.isArray(resp.tool_calls)) {\n return 'openai';\n }\n\n const choices = resp.choices as Array<{ message?: { tool_calls?: unknown[] } }> | undefined;\n if (choices?.[0]?.message?.tool_calls) {\n return 'openai';\n }\n\n // Anthropic format: has content array with tool_use type\n if (resp.content && Array.isArray(resp.content)) {\n const hasToolUse = (resp.content as Array<{ type?: string }>).some(\n (block) => block.type === 'tool_use'\n );\n if (hasToolUse) {\n return 'anthropic';\n }\n }\n\n // Google format: has candidates[0].content.parts with functionCall\n const candidates = resp.candidates as\n | Array<{ content?: { parts?: Array<{ functionCall?: unknown }> } }>\n | undefined;\n if (candidates?.[0]?.content?.parts) {\n const hasFunctionCall = candidates[0].content.parts.some((part) => part.functionCall);\n if (hasFunctionCall) {\n return 'google';\n }\n }\n\n return null;\n}\n\n/**\n * Parse tool calls with auto-detection or explicit format\n *\n * A unified parser that can either auto-detect the response format\n * or use a specified format. This is useful when you're not sure\n * which provider's response you're handling.\n *\n * @param response - The raw response from a provider API\n * @param format - The format to use: 'openai', 'anthropic', 'google', or 'auto'\n * @returns Array of parsed tool call requests\n *\n * @example\n * ```typescript\n * // Auto-detect format\n * const toolCalls = parseToolCalls(response, 'auto');\n *\n * // Or specify format explicitly\n * const toolCalls = parseToolCalls(response, 'openai');\n * ```\n */\nexport function parseToolCalls(\n response: unknown,\n format: ToolCallFormat = 'auto'\n): ToolCallRequest[] {\n let actualFormat: ToolCallFormat | null = format;\n\n if (format === 'auto') {\n actualFormat = detectToolCallFormat(response);\n if (!actualFormat) {\n return [];\n }\n }\n\n switch (actualFormat) {\n case 'openai':\n return parseOpenAIToolCalls(response);\n case 'anthropic':\n return parseAnthropicToolCalls(response);\n case 'google':\n return parseGoogleToolCalls(response);\n default:\n return [];\n }\n}\n\n/**\n * Check if a response contains tool calls\n *\n * Quick check to determine if a response has any tool calls\n * without fully parsing them.\n *\n * @param response - The raw response from a provider API\n * @returns True if the response contains tool calls\n */\nexport function hasToolCalls(response: unknown): boolean {\n const format = detectToolCallFormat(response);\n return format !== null;\n}\n","/**\n * Tool Format Converters\n *\n * Utilities for converting between different provider tool formats.\n * The universal format (OpenAI-style) serves as the baseline for all conversions.\n *\n * @module @quilltap/plugin-utils/tools/converters\n */\n\nimport type {\n UniversalTool,\n AnthropicToolDefinition,\n GoogleToolDefinition,\n} from '@quilltap/plugin-types';\n\n/**\n * Convert OpenAI/Universal format tool to Anthropic format\n *\n * Anthropic uses a tool_use format with:\n * - name: string\n * - description: string\n * - input_schema: JSON schema object\n *\n * @param tool - Universal tool in OpenAI format\n * @returns Tool formatted for Anthropic's tool_use\n *\n * @example\n * ```typescript\n * const anthropicTool = convertToAnthropicFormat(universalTool);\n * // Returns: {\n * // name: 'search_web',\n * // description: 'Search the web',\n * // input_schema: {\n * // type: 'object',\n * // properties: { query: { type: 'string' } },\n * // required: ['query']\n * // }\n * // }\n * ```\n */\nexport function convertToAnthropicFormat(tool: UniversalTool): AnthropicToolDefinition {\n return {\n name: tool.function.name,\n description: tool.function.description,\n input_schema: {\n type: 'object',\n properties: tool.function.parameters.properties,\n required: tool.function.parameters.required,\n },\n };\n}\n\n/**\n * Convert OpenAI/Universal format tool to Google Gemini format\n *\n * Google uses a function calling format with:\n * - name: string\n * - description: string\n * - parameters: JSON schema object\n *\n * @param tool - Universal tool in OpenAI format\n * @returns Tool formatted for Google's functionCall\n *\n * @example\n * ```typescript\n * const googleTool = convertToGoogleFormat(universalTool);\n * // Returns: {\n * // name: 'search_web',\n * // description: 'Search the web',\n * // parameters: {\n * // type: 'object',\n * // properties: { query: { type: 'string' } },\n * // required: ['query']\n * // }\n * // }\n * ```\n */\nexport function convertToGoogleFormat(tool: UniversalTool): GoogleToolDefinition {\n return {\n name: tool.function.name,\n description: tool.function.description,\n parameters: {\n type: 'object',\n properties: tool.function.parameters.properties,\n required: tool.function.parameters.required,\n },\n };\n}\n\n/**\n * Convert Anthropic format tool to Universal/OpenAI format\n *\n * @param tool - Anthropic format tool\n * @returns Tool in universal OpenAI format\n */\nexport function convertFromAnthropicFormat(tool: AnthropicToolDefinition): UniversalTool {\n return {\n type: 'function',\n function: {\n name: tool.name,\n description: tool.description ?? '',\n parameters: {\n type: 'object',\n properties: tool.input_schema.properties,\n required: tool.input_schema.required ?? [],\n },\n },\n };\n}\n\n/**\n * Convert Google format tool to Universal/OpenAI format\n *\n * @param tool - Google format tool\n * @returns Tool in universal OpenAI format\n */\nexport function convertFromGoogleFormat(tool: GoogleToolDefinition): UniversalTool {\n return {\n type: 'function',\n function: {\n name: tool.name,\n description: tool.description,\n parameters: {\n type: 'object',\n properties: tool.parameters.properties,\n required: tool.parameters.required,\n },\n },\n };\n}\n\n/**\n * Apply prompt/description length limit to a tool\n *\n * Modifies a tool's description if it exceeds maxBytes, appending a warning\n * that the description was truncated. This is useful for providers with\n * strict token limits.\n *\n * @param tool - Tool object (any format) with a description property\n * @param maxBytes - Maximum bytes allowed for description (including warning)\n * @returns Modified tool with truncated description if needed\n *\n * @example\n * ```typescript\n * const limitedTool = applyDescriptionLimit(tool, 500);\n * // If description > 500 bytes, truncates and adds warning\n * ```\n */\nexport function applyDescriptionLimit<T extends { description: string }>(\n tool: T,\n maxBytes: number\n): T {\n if (!tool || !tool.description) {\n return tool;\n }\n\n const warningText = ' [Note: description truncated due to length limit]';\n const maxDescBytes = maxBytes - Buffer.byteLength(warningText);\n\n if (maxDescBytes <= 0) {\n console.warn('[plugin-utils] Length limit too small for warning text:', maxBytes);\n return tool;\n }\n\n const descBytes = Buffer.byteLength(tool.description);\n\n if (descBytes > maxBytes) {\n // Truncate description to fit within the byte limit\n let truncated = tool.description;\n while (Buffer.byteLength(truncated) > maxDescBytes && truncated.length > 0) {\n truncated = truncated.slice(0, -1);\n }\n\n return {\n ...tool,\n description: truncated + warningText,\n };\n }\n\n return tool;\n}\n\n/**\n * Target format for tool conversion\n */\nexport type ToolConvertTarget = 'openai' | 'anthropic' | 'google';\n\n/**\n * Convert a universal tool to a specific provider format\n *\n * @param tool - Universal tool in OpenAI format\n * @param target - Target provider format\n * @returns Tool in the target format\n */\nexport function convertToolTo(\n tool: UniversalTool,\n target: ToolConvertTarget\n): UniversalTool | AnthropicToolDefinition | GoogleToolDefinition {\n switch (target) {\n case 'anthropic':\n return convertToAnthropicFormat(tool);\n case 'google':\n return convertToGoogleFormat(tool);\n case 'openai':\n default:\n return tool;\n }\n}\n\n/**\n * Convert multiple tools to a specific provider format\n *\n * @param tools - Array of universal tools\n * @param target - Target provider format\n * @returns Array of tools in the target format\n */\nexport function convertToolsTo(\n tools: UniversalTool[],\n target: ToolConvertTarget\n): Array<UniversalTool | AnthropicToolDefinition | GoogleToolDefinition> {\n return tools.map((tool) => convertToolTo(tool, target));\n}\n","/**\n * Plugin Logger Bridge\n *\n * Provides a logger factory for plugins that automatically bridges\n * to Quilltap's core logging when running inside the host application,\n * or falls back to console logging when running standalone.\n *\n * @module @quilltap/plugin-utils/logging/plugin-logger\n */\n\nimport type { PluginLogger, LogContext, LogLevel } from '@quilltap/plugin-types';\n\n/**\n * Extended logger interface with child logger support\n */\nexport interface PluginLoggerWithChild extends PluginLogger {\n /**\n * Create a child logger with additional context\n * @param additionalContext Context to merge with parent context\n * @returns A new logger with combined context\n */\n child(additionalContext: LogContext): PluginLoggerWithChild;\n}\n\n/**\n * Type for the global Quilltap logger bridge\n * Stored on globalThis to work across different npm package copies\n */\ndeclare global {\n // eslint-disable-next-line no-var\n var __quilltap_logger_factory:\n | ((pluginName: string) => PluginLoggerWithChild)\n | undefined;\n}\n\n/**\n * Get the core logger factory from global namespace\n *\n * @returns The injected factory or null if not in Quilltap environment\n */\nfunction getCoreLoggerFactory(): ((pluginName: string) => PluginLoggerWithChild) | null {\n return globalThis.__quilltap_logger_factory ?? null;\n}\n\n/**\n * Inject the core logger factory from Quilltap host\n *\n * This is called by Quilltap core when loading plugins to bridge\n * plugin logging into the host's logging system. Uses globalThis\n * to ensure it works even when plugins have their own copy of\n * plugin-utils in their node_modules.\n *\n * **Internal API - not for plugin use**\n *\n * @param factory A function that creates a child logger for a plugin\n */\nexport function __injectCoreLoggerFactory(\n factory: (pluginName: string) => PluginLoggerWithChild\n): void {\n globalThis.__quilltap_logger_factory = factory;\n}\n\n/**\n * Clear the injected core logger factory\n *\n * Useful for testing or when unloading the plugin system.\n *\n * **Internal API - not for plugin use**\n */\nexport function __clearCoreLoggerFactory(): void {\n globalThis.__quilltap_logger_factory = undefined;\n}\n\n/**\n * Check if a core logger has been injected\n *\n * @returns True if running inside Quilltap with core logging available\n */\nexport function hasCoreLogger(): boolean {\n return getCoreLoggerFactory() !== null;\n}\n\n/**\n * Create a console logger with child support\n *\n * @param prefix Logger prefix\n * @param minLevel Minimum log level\n * @param baseContext Base context to include in all logs\n */\nfunction createConsoleLoggerWithChild(\n prefix: string,\n minLevel: LogLevel = 'debug',\n baseContext: LogContext = {}\n): PluginLoggerWithChild {\n const levels: LogLevel[] = ['debug', 'info', 'warn', 'error'];\n const shouldLog = (level: LogLevel): boolean =>\n levels.indexOf(level) >= levels.indexOf(minLevel);\n\n const formatContext = (context?: LogContext): string => {\n const merged = { ...baseContext, ...context };\n const entries = Object.entries(merged)\n .filter(([key]) => key !== 'context')\n .map(([key, value]) => `${key}=${JSON.stringify(value)}`)\n .join(' ');\n return entries ? ` ${entries}` : '';\n };\n\n const logger: PluginLoggerWithChild = {\n debug: (message: string, context?: LogContext): void => {\n if (shouldLog('debug')) {\n console.debug(`[${prefix}] ${message}${formatContext(context)}`);\n }\n },\n\n info: (message: string, context?: LogContext): void => {\n if (shouldLog('info')) {\n console.info(`[${prefix}] ${message}${formatContext(context)}`);\n }\n },\n\n warn: (message: string, context?: LogContext): void => {\n if (shouldLog('warn')) {\n console.warn(`[${prefix}] ${message}${formatContext(context)}`);\n }\n },\n\n error: (message: string, context?: LogContext, error?: Error): void => {\n if (shouldLog('error')) {\n console.error(\n `[${prefix}] ${message}${formatContext(context)}`,\n error ? `\\n${error.stack || error.message}` : ''\n );\n }\n },\n\n child: (additionalContext: LogContext): PluginLoggerWithChild => {\n return createConsoleLoggerWithChild(prefix, minLevel, {\n ...baseContext,\n ...additionalContext,\n });\n },\n };\n\n return logger;\n}\n\n/**\n * Create a plugin logger that bridges to Quilltap core logging\n *\n * When running inside Quilltap:\n * - Routes all logs to the core logger\n * - Tags logs with `{ plugin: pluginName, module: 'plugin' }`\n * - Logs appear in Quilltap's combined.log and console\n *\n * When running standalone:\n * - Falls back to console logging with `[pluginName]` prefix\n * - Respects the specified minimum log level\n *\n * @param pluginName - The plugin identifier (e.g., 'qtap-plugin-openai')\n * @param minLevel - Minimum log level when running standalone (default: 'debug')\n * @returns A logger instance\n *\n * @example\n * ```typescript\n * // In your plugin's provider.ts\n * import { createPluginLogger } from '@quilltap/plugin-utils';\n *\n * const logger = createPluginLogger('qtap-plugin-my-provider');\n *\n * export class MyProvider implements LLMProvider {\n * async sendMessage(params: LLMParams, apiKey: string): Promise<LLMResponse> {\n * logger.debug('Sending message', { model: params.model });\n *\n * try {\n * const response = await this.client.chat({...});\n * logger.info('Received response', { tokens: response.usage?.total_tokens });\n * return response;\n * } catch (error) {\n * logger.error('Failed to send message', { model: params.model }, error as Error);\n * throw error;\n * }\n * }\n * }\n * ```\n */\nexport function createPluginLogger(\n pluginName: string,\n minLevel: LogLevel = 'debug'\n): PluginLoggerWithChild {\n // Check for core logger factory from global namespace\n const coreFactory = getCoreLoggerFactory();\n if (coreFactory) {\n return coreFactory(pluginName);\n }\n\n // Standalone mode: use enhanced console logger\n return createConsoleLoggerWithChild(pluginName, minLevel);\n}\n\n/**\n * Get the minimum log level from environment\n *\n * Checks for LOG_LEVEL or QUILTTAP_LOG_LEVEL environment variables.\n * Useful for configuring standalone plugin logging.\n *\n * @returns The configured log level, or 'info' as default\n */\nexport function getLogLevelFromEnv(): LogLevel {\n if (typeof process !== 'undefined' && process.env) {\n const envLevel = process.env.LOG_LEVEL || process.env.QUILTTAP_LOG_LEVEL;\n if (envLevel && ['debug', 'info', 'warn', 'error'].includes(envLevel)) {\n return envLevel as LogLevel;\n }\n }\n return 'info';\n}\n","/**\n * Logging Utilities\n *\n * Exports the plugin logger bridge and related utilities.\n *\n * @module @quilltap/plugin-utils/logging\n */\n\nexport {\n createPluginLogger,\n hasCoreLogger,\n getLogLevelFromEnv,\n __injectCoreLoggerFactory,\n __clearCoreLoggerFactory,\n} from './plugin-logger';\n\nexport type { PluginLoggerWithChild } from './plugin-logger';\n\n// Re-export logger types from plugin-types\nexport type { PluginLogger, LogContext, LogLevel } from '@quilltap/plugin-types';\n\n// Re-export logger utilities from plugin-types for convenience\nexport { createConsoleLogger, createNoopLogger } from '@quilltap/plugin-types';\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACqCO,SAAS,qBAAqB,UAAsC;AACzE,QAAM,YAA+B,CAAC;AAEtC,MAAI;AACF,UAAM,OAAO;AAGb,QAAI,iBAAiB,MAAM;AAG3B,QAAI,CAAC,gBAAgB;AACnB,YAAM,UAAU,MAAM;AAGtB,uBAAiB,UAAU,CAAC,GAAG,SAAS;AAAA,IAC1C;AAEA,QAAI,kBAAkB,MAAM,QAAQ,cAAc,KAA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,WAAW,QAAQ,IAAI,aAAa,QAAQ,IAAI;AACtD,QAAI,YAAY,CAAC,SAAS,QAAQ,QAAQ,OAAO,EAAE,SAAS,QAAQ,GAAG;AACrE,aAAO;AAAA,IACT;AAAA,EACF;AACA,SAAO;AACT;;;ACjMA,0BAAsD;;;AJoE/C,IAAM,uBAAuB;","names":[]}
package/dist/index.mjs ADDED
@@ -0,0 +1,313 @@
1
+ // src/tools/parsers.ts
2
+ function parseOpenAIToolCalls(response) {
3
+ const toolCalls = [];
4
+ try {
5
+ const resp = response;
6
+ let toolCallsArray = resp?.tool_calls;
7
+ if (!toolCallsArray) {
8
+ const choices = resp?.choices;
9
+ toolCallsArray = choices?.[0]?.message?.tool_calls;
10
+ }
11
+ if (toolCallsArray && Array.isArray(toolCallsArray) && toolCallsArray.length > 0) {
12
+ for (const toolCall of toolCallsArray) {
13
+ const tc = toolCall;
14
+ if (tc.type === "function" && tc.function) {
15
+ toolCalls.push({
16
+ name: tc.function.name,
17
+ arguments: JSON.parse(tc.function.arguments || "{}")
18
+ });
19
+ }
20
+ }
21
+ }
22
+ } catch (error) {
23
+ console.error("[plugin-utils] Error parsing OpenAI tool calls:", error);
24
+ }
25
+ return toolCalls;
26
+ }
27
+ function parseAnthropicToolCalls(response) {
28
+ const toolCalls = [];
29
+ try {
30
+ const resp = response;
31
+ if (!resp?.content || !Array.isArray(resp.content)) {
32
+ return toolCalls;
33
+ }
34
+ for (const block of resp.content) {
35
+ const b = block;
36
+ if (b.type === "tool_use" && b.name) {
37
+ toolCalls.push({
38
+ name: b.name,
39
+ arguments: b.input || {}
40
+ });
41
+ }
42
+ }
43
+ } catch (error) {
44
+ console.error("[plugin-utils] Error parsing Anthropic tool calls:", error);
45
+ }
46
+ return toolCalls;
47
+ }
48
+ function parseGoogleToolCalls(response) {
49
+ const toolCalls = [];
50
+ try {
51
+ const resp = response;
52
+ const candidates = resp?.candidates;
53
+ const parts = candidates?.[0]?.content?.parts;
54
+ if (!parts || !Array.isArray(parts)) {
55
+ return toolCalls;
56
+ }
57
+ for (const part of parts) {
58
+ const p = part;
59
+ if (p.functionCall) {
60
+ toolCalls.push({
61
+ name: p.functionCall.name,
62
+ arguments: p.functionCall.args || {}
63
+ });
64
+ }
65
+ }
66
+ } catch (error) {
67
+ console.error("[plugin-utils] Error parsing Google tool calls:", error);
68
+ }
69
+ return toolCalls;
70
+ }
71
+ function detectToolCallFormat(response) {
72
+ if (!response || typeof response !== "object") {
73
+ return null;
74
+ }
75
+ const resp = response;
76
+ if (resp.tool_calls && Array.isArray(resp.tool_calls)) {
77
+ return "openai";
78
+ }
79
+ const choices = resp.choices;
80
+ if (choices?.[0]?.message?.tool_calls) {
81
+ return "openai";
82
+ }
83
+ if (resp.content && Array.isArray(resp.content)) {
84
+ const hasToolUse = resp.content.some(
85
+ (block) => block.type === "tool_use"
86
+ );
87
+ if (hasToolUse) {
88
+ return "anthropic";
89
+ }
90
+ }
91
+ const candidates = resp.candidates;
92
+ if (candidates?.[0]?.content?.parts) {
93
+ const hasFunctionCall = candidates[0].content.parts.some((part) => part.functionCall);
94
+ if (hasFunctionCall) {
95
+ return "google";
96
+ }
97
+ }
98
+ return null;
99
+ }
100
+ function parseToolCalls(response, format = "auto") {
101
+ let actualFormat = format;
102
+ if (format === "auto") {
103
+ actualFormat = detectToolCallFormat(response);
104
+ if (!actualFormat) {
105
+ return [];
106
+ }
107
+ }
108
+ switch (actualFormat) {
109
+ case "openai":
110
+ return parseOpenAIToolCalls(response);
111
+ case "anthropic":
112
+ return parseAnthropicToolCalls(response);
113
+ case "google":
114
+ return parseGoogleToolCalls(response);
115
+ default:
116
+ return [];
117
+ }
118
+ }
119
+ function hasToolCalls(response) {
120
+ const format = detectToolCallFormat(response);
121
+ return format !== null;
122
+ }
123
+
124
+ // src/tools/converters.ts
125
+ function convertToAnthropicFormat(tool) {
126
+ return {
127
+ name: tool.function.name,
128
+ description: tool.function.description,
129
+ input_schema: {
130
+ type: "object",
131
+ properties: tool.function.parameters.properties,
132
+ required: tool.function.parameters.required
133
+ }
134
+ };
135
+ }
136
+ function convertToGoogleFormat(tool) {
137
+ return {
138
+ name: tool.function.name,
139
+ description: tool.function.description,
140
+ parameters: {
141
+ type: "object",
142
+ properties: tool.function.parameters.properties,
143
+ required: tool.function.parameters.required
144
+ }
145
+ };
146
+ }
147
+ function convertFromAnthropicFormat(tool) {
148
+ return {
149
+ type: "function",
150
+ function: {
151
+ name: tool.name,
152
+ description: tool.description ?? "",
153
+ parameters: {
154
+ type: "object",
155
+ properties: tool.input_schema.properties,
156
+ required: tool.input_schema.required ?? []
157
+ }
158
+ }
159
+ };
160
+ }
161
+ function convertFromGoogleFormat(tool) {
162
+ return {
163
+ type: "function",
164
+ function: {
165
+ name: tool.name,
166
+ description: tool.description,
167
+ parameters: {
168
+ type: "object",
169
+ properties: tool.parameters.properties,
170
+ required: tool.parameters.required
171
+ }
172
+ }
173
+ };
174
+ }
175
+ function applyDescriptionLimit(tool, maxBytes) {
176
+ if (!tool || !tool.description) {
177
+ return tool;
178
+ }
179
+ const warningText = " [Note: description truncated due to length limit]";
180
+ const maxDescBytes = maxBytes - Buffer.byteLength(warningText);
181
+ if (maxDescBytes <= 0) {
182
+ console.warn("[plugin-utils] Length limit too small for warning text:", maxBytes);
183
+ return tool;
184
+ }
185
+ const descBytes = Buffer.byteLength(tool.description);
186
+ if (descBytes > maxBytes) {
187
+ let truncated = tool.description;
188
+ while (Buffer.byteLength(truncated) > maxDescBytes && truncated.length > 0) {
189
+ truncated = truncated.slice(0, -1);
190
+ }
191
+ return {
192
+ ...tool,
193
+ description: truncated + warningText
194
+ };
195
+ }
196
+ return tool;
197
+ }
198
+ function convertToolTo(tool, target) {
199
+ switch (target) {
200
+ case "anthropic":
201
+ return convertToAnthropicFormat(tool);
202
+ case "google":
203
+ return convertToGoogleFormat(tool);
204
+ case "openai":
205
+ default:
206
+ return tool;
207
+ }
208
+ }
209
+ function convertToolsTo(tools, target) {
210
+ return tools.map((tool) => convertToolTo(tool, target));
211
+ }
212
+
213
+ // src/logging/plugin-logger.ts
214
+ function getCoreLoggerFactory() {
215
+ return globalThis.__quilltap_logger_factory ?? null;
216
+ }
217
+ function __injectCoreLoggerFactory(factory) {
218
+ globalThis.__quilltap_logger_factory = factory;
219
+ }
220
+ function __clearCoreLoggerFactory() {
221
+ globalThis.__quilltap_logger_factory = void 0;
222
+ }
223
+ function hasCoreLogger() {
224
+ return getCoreLoggerFactory() !== null;
225
+ }
226
+ function createConsoleLoggerWithChild(prefix, minLevel = "debug", baseContext = {}) {
227
+ const levels = ["debug", "info", "warn", "error"];
228
+ const shouldLog = (level) => levels.indexOf(level) >= levels.indexOf(minLevel);
229
+ const formatContext = (context) => {
230
+ const merged = { ...baseContext, ...context };
231
+ const entries = Object.entries(merged).filter(([key]) => key !== "context").map(([key, value]) => `${key}=${JSON.stringify(value)}`).join(" ");
232
+ return entries ? ` ${entries}` : "";
233
+ };
234
+ const logger = {
235
+ debug: (message, context) => {
236
+ if (shouldLog("debug")) {
237
+ console.debug(`[${prefix}] ${message}${formatContext(context)}`);
238
+ }
239
+ },
240
+ info: (message, context) => {
241
+ if (shouldLog("info")) {
242
+ console.info(`[${prefix}] ${message}${formatContext(context)}`);
243
+ }
244
+ },
245
+ warn: (message, context) => {
246
+ if (shouldLog("warn")) {
247
+ console.warn(`[${prefix}] ${message}${formatContext(context)}`);
248
+ }
249
+ },
250
+ error: (message, context, error) => {
251
+ if (shouldLog("error")) {
252
+ console.error(
253
+ `[${prefix}] ${message}${formatContext(context)}`,
254
+ error ? `
255
+ ${error.stack || error.message}` : ""
256
+ );
257
+ }
258
+ },
259
+ child: (additionalContext) => {
260
+ return createConsoleLoggerWithChild(prefix, minLevel, {
261
+ ...baseContext,
262
+ ...additionalContext
263
+ });
264
+ }
265
+ };
266
+ return logger;
267
+ }
268
+ function createPluginLogger(pluginName, minLevel = "debug") {
269
+ const coreFactory = getCoreLoggerFactory();
270
+ if (coreFactory) {
271
+ return coreFactory(pluginName);
272
+ }
273
+ return createConsoleLoggerWithChild(pluginName, minLevel);
274
+ }
275
+ function getLogLevelFromEnv() {
276
+ if (typeof process !== "undefined" && process.env) {
277
+ const envLevel = process.env.LOG_LEVEL || process.env.QUILTTAP_LOG_LEVEL;
278
+ if (envLevel && ["debug", "info", "warn", "error"].includes(envLevel)) {
279
+ return envLevel;
280
+ }
281
+ }
282
+ return "info";
283
+ }
284
+
285
+ // src/logging/index.ts
286
+ import { createConsoleLogger, createNoopLogger } from "@quilltap/plugin-types";
287
+
288
+ // src/index.ts
289
+ var PLUGIN_UTILS_VERSION = "1.0.0";
290
+ export {
291
+ PLUGIN_UTILS_VERSION,
292
+ __clearCoreLoggerFactory,
293
+ __injectCoreLoggerFactory,
294
+ applyDescriptionLimit,
295
+ convertFromAnthropicFormat,
296
+ convertFromGoogleFormat,
297
+ convertToAnthropicFormat,
298
+ convertToGoogleFormat,
299
+ convertToolTo,
300
+ convertToolsTo,
301
+ createConsoleLogger,
302
+ createNoopLogger,
303
+ createPluginLogger,
304
+ detectToolCallFormat,
305
+ getLogLevelFromEnv,
306
+ hasCoreLogger,
307
+ hasToolCalls,
308
+ parseAnthropicToolCalls,
309
+ parseGoogleToolCalls,
310
+ parseOpenAIToolCalls,
311
+ parseToolCalls
312
+ };
313
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/tools/parsers.ts","../src/tools/converters.ts","../src/logging/plugin-logger.ts","../src/logging/index.ts","../src/index.ts"],"sourcesContent":["/**\n * Tool Call Parsers\n *\n * Provider-specific parsers for extracting tool calls from LLM responses.\n * Each parser converts from a provider's native format to the standardized\n * ToolCallRequest format.\n *\n * @module @quilltap/plugin-utils/tools/parsers\n */\n\nimport type { ToolCallRequest } from '@quilltap/plugin-types';\n\n/**\n * Supported tool call response formats\n */\nexport type ToolCallFormat = 'openai' | 'anthropic' | 'google' | 'auto';\n\n/**\n * Parse OpenAI format tool calls from LLM response\n *\n * Extracts tool calls from OpenAI/Grok API responses which return\n * tool_calls in the message object.\n *\n * Expected response structures:\n * - `response.tool_calls` (direct)\n * - `response.choices[0].message.tool_calls` (nested)\n *\n * @param response - The raw response from provider API\n * @returns Array of parsed tool call requests\n *\n * @example\n * ```typescript\n * const response = await openai.chat.completions.create({...});\n * const toolCalls = parseOpenAIToolCalls(response);\n * // Returns: [{ name: 'search_web', arguments: { query: 'hello' } }]\n * ```\n */\nexport function parseOpenAIToolCalls(response: unknown): ToolCallRequest[] {\n const toolCalls: ToolCallRequest[] = [];\n\n try {\n const resp = response as Record<string, unknown>;\n\n // Handle direct tool_calls array\n let toolCallsArray = resp?.tool_calls as unknown[] | undefined;\n\n // Check nested structure from streaming responses\n if (!toolCallsArray) {\n const choices = resp?.choices as\n | Array<{ message?: { tool_calls?: unknown[] } }>\n | undefined;\n toolCallsArray = choices?.[0]?.message?.tool_calls;\n }\n\n if (toolCallsArray && Array.isArray(toolCallsArray) && toolCallsArray.length > 0) {\n for (const toolCall of toolCallsArray) {\n const tc = toolCall as {\n type?: string;\n function?: { name: string; arguments: string };\n };\n\n if (tc.type === 'function' && tc.function) {\n toolCalls.push({\n name: tc.function.name,\n arguments: JSON.parse(tc.function.arguments || '{}'),\n });\n }\n }\n }\n } catch (error) {\n // Log error but don't throw - return empty array\n console.error('[plugin-utils] Error parsing OpenAI tool calls:', error);\n }\n\n return toolCalls;\n}\n\n/**\n * Parse Anthropic format tool calls from LLM response\n *\n * Extracts tool calls from Anthropic API responses which return\n * tool_use blocks in the content array.\n *\n * Expected response structure:\n * - `response.content` array with `{ type: 'tool_use', name, input }`\n *\n * @param response - The raw response from provider API\n * @returns Array of parsed tool call requests\n *\n * @example\n * ```typescript\n * const response = await anthropic.messages.create({...});\n * const toolCalls = parseAnthropicToolCalls(response);\n * // Returns: [{ name: 'search_web', arguments: { query: 'hello' } }]\n * ```\n */\nexport function parseAnthropicToolCalls(response: unknown): ToolCallRequest[] {\n const toolCalls: ToolCallRequest[] = [];\n\n try {\n const resp = response as Record<string, unknown>;\n\n if (!resp?.content || !Array.isArray(resp.content)) {\n return toolCalls;\n }\n\n for (const block of resp.content) {\n const b = block as { type?: string; name?: string; input?: Record<string, unknown> };\n\n if (b.type === 'tool_use' && b.name) {\n toolCalls.push({\n name: b.name,\n arguments: b.input || {},\n });\n }\n }\n } catch (error) {\n console.error('[plugin-utils] Error parsing Anthropic tool calls:', error);\n }\n\n return toolCalls;\n}\n\n/**\n * Parse Google Gemini format tool calls from LLM response\n *\n * Extracts tool calls from Google Gemini API responses which return\n * functionCall objects in the parts array.\n *\n * Expected response structure:\n * - `response.candidates[0].content.parts` array with `{ functionCall: { name, args } }`\n *\n * @param response - The raw response from provider API\n * @returns Array of parsed tool call requests\n *\n * @example\n * ```typescript\n * const response = await gemini.generateContent({...});\n * const toolCalls = parseGoogleToolCalls(response);\n * // Returns: [{ name: 'search_web', arguments: { query: 'hello' } }]\n * ```\n */\nexport function parseGoogleToolCalls(response: unknown): ToolCallRequest[] {\n const toolCalls: ToolCallRequest[] = [];\n\n try {\n const resp = response as Record<string, unknown>;\n const candidates = resp?.candidates as\n | Array<{ content?: { parts?: unknown[] } }>\n | undefined;\n const parts = candidates?.[0]?.content?.parts;\n\n if (!parts || !Array.isArray(parts)) {\n return toolCalls;\n }\n\n for (const part of parts) {\n const p = part as {\n functionCall?: { name: string; args?: Record<string, unknown> };\n };\n\n if (p.functionCall) {\n toolCalls.push({\n name: p.functionCall.name,\n arguments: p.functionCall.args || {},\n });\n }\n }\n } catch (error) {\n console.error('[plugin-utils] Error parsing Google tool calls:', error);\n }\n\n return toolCalls;\n}\n\n/**\n * Detect the format of a tool call response\n *\n * Analyzes the response structure to determine which provider format it uses.\n *\n * @param response - The raw response from a provider API\n * @returns The detected format, or null if unrecognized\n */\nexport function detectToolCallFormat(response: unknown): ToolCallFormat | null {\n if (!response || typeof response !== 'object') {\n return null;\n }\n\n const resp = response as Record<string, unknown>;\n\n // OpenAI format: has tool_calls directly or in choices[0].message\n if (resp.tool_calls && Array.isArray(resp.tool_calls)) {\n return 'openai';\n }\n\n const choices = resp.choices as Array<{ message?: { tool_calls?: unknown[] } }> | undefined;\n if (choices?.[0]?.message?.tool_calls) {\n return 'openai';\n }\n\n // Anthropic format: has content array with tool_use type\n if (resp.content && Array.isArray(resp.content)) {\n const hasToolUse = (resp.content as Array<{ type?: string }>).some(\n (block) => block.type === 'tool_use'\n );\n if (hasToolUse) {\n return 'anthropic';\n }\n }\n\n // Google format: has candidates[0].content.parts with functionCall\n const candidates = resp.candidates as\n | Array<{ content?: { parts?: Array<{ functionCall?: unknown }> } }>\n | undefined;\n if (candidates?.[0]?.content?.parts) {\n const hasFunctionCall = candidates[0].content.parts.some((part) => part.functionCall);\n if (hasFunctionCall) {\n return 'google';\n }\n }\n\n return null;\n}\n\n/**\n * Parse tool calls with auto-detection or explicit format\n *\n * A unified parser that can either auto-detect the response format\n * or use a specified format. This is useful when you're not sure\n * which provider's response you're handling.\n *\n * @param response - The raw response from a provider API\n * @param format - The format to use: 'openai', 'anthropic', 'google', or 'auto'\n * @returns Array of parsed tool call requests\n *\n * @example\n * ```typescript\n * // Auto-detect format\n * const toolCalls = parseToolCalls(response, 'auto');\n *\n * // Or specify format explicitly\n * const toolCalls = parseToolCalls(response, 'openai');\n * ```\n */\nexport function parseToolCalls(\n response: unknown,\n format: ToolCallFormat = 'auto'\n): ToolCallRequest[] {\n let actualFormat: ToolCallFormat | null = format;\n\n if (format === 'auto') {\n actualFormat = detectToolCallFormat(response);\n if (!actualFormat) {\n return [];\n }\n }\n\n switch (actualFormat) {\n case 'openai':\n return parseOpenAIToolCalls(response);\n case 'anthropic':\n return parseAnthropicToolCalls(response);\n case 'google':\n return parseGoogleToolCalls(response);\n default:\n return [];\n }\n}\n\n/**\n * Check if a response contains tool calls\n *\n * Quick check to determine if a response has any tool calls\n * without fully parsing them.\n *\n * @param response - The raw response from a provider API\n * @returns True if the response contains tool calls\n */\nexport function hasToolCalls(response: unknown): boolean {\n const format = detectToolCallFormat(response);\n return format !== null;\n}\n","/**\n * Tool Format Converters\n *\n * Utilities for converting between different provider tool formats.\n * The universal format (OpenAI-style) serves as the baseline for all conversions.\n *\n * @module @quilltap/plugin-utils/tools/converters\n */\n\nimport type {\n UniversalTool,\n AnthropicToolDefinition,\n GoogleToolDefinition,\n} from '@quilltap/plugin-types';\n\n/**\n * Convert OpenAI/Universal format tool to Anthropic format\n *\n * Anthropic uses a tool_use format with:\n * - name: string\n * - description: string\n * - input_schema: JSON schema object\n *\n * @param tool - Universal tool in OpenAI format\n * @returns Tool formatted for Anthropic's tool_use\n *\n * @example\n * ```typescript\n * const anthropicTool = convertToAnthropicFormat(universalTool);\n * // Returns: {\n * // name: 'search_web',\n * // description: 'Search the web',\n * // input_schema: {\n * // type: 'object',\n * // properties: { query: { type: 'string' } },\n * // required: ['query']\n * // }\n * // }\n * ```\n */\nexport function convertToAnthropicFormat(tool: UniversalTool): AnthropicToolDefinition {\n return {\n name: tool.function.name,\n description: tool.function.description,\n input_schema: {\n type: 'object',\n properties: tool.function.parameters.properties,\n required: tool.function.parameters.required,\n },\n };\n}\n\n/**\n * Convert OpenAI/Universal format tool to Google Gemini format\n *\n * Google uses a function calling format with:\n * - name: string\n * - description: string\n * - parameters: JSON schema object\n *\n * @param tool - Universal tool in OpenAI format\n * @returns Tool formatted for Google's functionCall\n *\n * @example\n * ```typescript\n * const googleTool = convertToGoogleFormat(universalTool);\n * // Returns: {\n * // name: 'search_web',\n * // description: 'Search the web',\n * // parameters: {\n * // type: 'object',\n * // properties: { query: { type: 'string' } },\n * // required: ['query']\n * // }\n * // }\n * ```\n */\nexport function convertToGoogleFormat(tool: UniversalTool): GoogleToolDefinition {\n return {\n name: tool.function.name,\n description: tool.function.description,\n parameters: {\n type: 'object',\n properties: tool.function.parameters.properties,\n required: tool.function.parameters.required,\n },\n };\n}\n\n/**\n * Convert Anthropic format tool to Universal/OpenAI format\n *\n * @param tool - Anthropic format tool\n * @returns Tool in universal OpenAI format\n */\nexport function convertFromAnthropicFormat(tool: AnthropicToolDefinition): UniversalTool {\n return {\n type: 'function',\n function: {\n name: tool.name,\n description: tool.description ?? '',\n parameters: {\n type: 'object',\n properties: tool.input_schema.properties,\n required: tool.input_schema.required ?? [],\n },\n },\n };\n}\n\n/**\n * Convert Google format tool to Universal/OpenAI format\n *\n * @param tool - Google format tool\n * @returns Tool in universal OpenAI format\n */\nexport function convertFromGoogleFormat(tool: GoogleToolDefinition): UniversalTool {\n return {\n type: 'function',\n function: {\n name: tool.name,\n description: tool.description,\n parameters: {\n type: 'object',\n properties: tool.parameters.properties,\n required: tool.parameters.required,\n },\n },\n };\n}\n\n/**\n * Apply prompt/description length limit to a tool\n *\n * Modifies a tool's description if it exceeds maxBytes, appending a warning\n * that the description was truncated. This is useful for providers with\n * strict token limits.\n *\n * @param tool - Tool object (any format) with a description property\n * @param maxBytes - Maximum bytes allowed for description (including warning)\n * @returns Modified tool with truncated description if needed\n *\n * @example\n * ```typescript\n * const limitedTool = applyDescriptionLimit(tool, 500);\n * // If description > 500 bytes, truncates and adds warning\n * ```\n */\nexport function applyDescriptionLimit<T extends { description: string }>(\n tool: T,\n maxBytes: number\n): T {\n if (!tool || !tool.description) {\n return tool;\n }\n\n const warningText = ' [Note: description truncated due to length limit]';\n const maxDescBytes = maxBytes - Buffer.byteLength(warningText);\n\n if (maxDescBytes <= 0) {\n console.warn('[plugin-utils] Length limit too small for warning text:', maxBytes);\n return tool;\n }\n\n const descBytes = Buffer.byteLength(tool.description);\n\n if (descBytes > maxBytes) {\n // Truncate description to fit within the byte limit\n let truncated = tool.description;\n while (Buffer.byteLength(truncated) > maxDescBytes && truncated.length > 0) {\n truncated = truncated.slice(0, -1);\n }\n\n return {\n ...tool,\n description: truncated + warningText,\n };\n }\n\n return tool;\n}\n\n/**\n * Target format for tool conversion\n */\nexport type ToolConvertTarget = 'openai' | 'anthropic' | 'google';\n\n/**\n * Convert a universal tool to a specific provider format\n *\n * @param tool - Universal tool in OpenAI format\n * @param target - Target provider format\n * @returns Tool in the target format\n */\nexport function convertToolTo(\n tool: UniversalTool,\n target: ToolConvertTarget\n): UniversalTool | AnthropicToolDefinition | GoogleToolDefinition {\n switch (target) {\n case 'anthropic':\n return convertToAnthropicFormat(tool);\n case 'google':\n return convertToGoogleFormat(tool);\n case 'openai':\n default:\n return tool;\n }\n}\n\n/**\n * Convert multiple tools to a specific provider format\n *\n * @param tools - Array of universal tools\n * @param target - Target provider format\n * @returns Array of tools in the target format\n */\nexport function convertToolsTo(\n tools: UniversalTool[],\n target: ToolConvertTarget\n): Array<UniversalTool | AnthropicToolDefinition | GoogleToolDefinition> {\n return tools.map((tool) => convertToolTo(tool, target));\n}\n","/**\n * Plugin Logger Bridge\n *\n * Provides a logger factory for plugins that automatically bridges\n * to Quilltap's core logging when running inside the host application,\n * or falls back to console logging when running standalone.\n *\n * @module @quilltap/plugin-utils/logging/plugin-logger\n */\n\nimport type { PluginLogger, LogContext, LogLevel } from '@quilltap/plugin-types';\n\n/**\n * Extended logger interface with child logger support\n */\nexport interface PluginLoggerWithChild extends PluginLogger {\n /**\n * Create a child logger with additional context\n * @param additionalContext Context to merge with parent context\n * @returns A new logger with combined context\n */\n child(additionalContext: LogContext): PluginLoggerWithChild;\n}\n\n/**\n * Type for the global Quilltap logger bridge\n * Stored on globalThis to work across different npm package copies\n */\ndeclare global {\n // eslint-disable-next-line no-var\n var __quilltap_logger_factory:\n | ((pluginName: string) => PluginLoggerWithChild)\n | undefined;\n}\n\n/**\n * Get the core logger factory from global namespace\n *\n * @returns The injected factory or null if not in Quilltap environment\n */\nfunction getCoreLoggerFactory(): ((pluginName: string) => PluginLoggerWithChild) | null {\n return globalThis.__quilltap_logger_factory ?? null;\n}\n\n/**\n * Inject the core logger factory from Quilltap host\n *\n * This is called by Quilltap core when loading plugins to bridge\n * plugin logging into the host's logging system. Uses globalThis\n * to ensure it works even when plugins have their own copy of\n * plugin-utils in their node_modules.\n *\n * **Internal API - not for plugin use**\n *\n * @param factory A function that creates a child logger for a plugin\n */\nexport function __injectCoreLoggerFactory(\n factory: (pluginName: string) => PluginLoggerWithChild\n): void {\n globalThis.__quilltap_logger_factory = factory;\n}\n\n/**\n * Clear the injected core logger factory\n *\n * Useful for testing or when unloading the plugin system.\n *\n * **Internal API - not for plugin use**\n */\nexport function __clearCoreLoggerFactory(): void {\n globalThis.__quilltap_logger_factory = undefined;\n}\n\n/**\n * Check if a core logger has been injected\n *\n * @returns True if running inside Quilltap with core logging available\n */\nexport function hasCoreLogger(): boolean {\n return getCoreLoggerFactory() !== null;\n}\n\n/**\n * Create a console logger with child support\n *\n * @param prefix Logger prefix\n * @param minLevel Minimum log level\n * @param baseContext Base context to include in all logs\n */\nfunction createConsoleLoggerWithChild(\n prefix: string,\n minLevel: LogLevel = 'debug',\n baseContext: LogContext = {}\n): PluginLoggerWithChild {\n const levels: LogLevel[] = ['debug', 'info', 'warn', 'error'];\n const shouldLog = (level: LogLevel): boolean =>\n levels.indexOf(level) >= levels.indexOf(minLevel);\n\n const formatContext = (context?: LogContext): string => {\n const merged = { ...baseContext, ...context };\n const entries = Object.entries(merged)\n .filter(([key]) => key !== 'context')\n .map(([key, value]) => `${key}=${JSON.stringify(value)}`)\n .join(' ');\n return entries ? ` ${entries}` : '';\n };\n\n const logger: PluginLoggerWithChild = {\n debug: (message: string, context?: LogContext): void => {\n if (shouldLog('debug')) {\n console.debug(`[${prefix}] ${message}${formatContext(context)}`);\n }\n },\n\n info: (message: string, context?: LogContext): void => {\n if (shouldLog('info')) {\n console.info(`[${prefix}] ${message}${formatContext(context)}`);\n }\n },\n\n warn: (message: string, context?: LogContext): void => {\n if (shouldLog('warn')) {\n console.warn(`[${prefix}] ${message}${formatContext(context)}`);\n }\n },\n\n error: (message: string, context?: LogContext, error?: Error): void => {\n if (shouldLog('error')) {\n console.error(\n `[${prefix}] ${message}${formatContext(context)}`,\n error ? `\\n${error.stack || error.message}` : ''\n );\n }\n },\n\n child: (additionalContext: LogContext): PluginLoggerWithChild => {\n return createConsoleLoggerWithChild(prefix, minLevel, {\n ...baseContext,\n ...additionalContext,\n });\n },\n };\n\n return logger;\n}\n\n/**\n * Create a plugin logger that bridges to Quilltap core logging\n *\n * When running inside Quilltap:\n * - Routes all logs to the core logger\n * - Tags logs with `{ plugin: pluginName, module: 'plugin' }`\n * - Logs appear in Quilltap's combined.log and console\n *\n * When running standalone:\n * - Falls back to console logging with `[pluginName]` prefix\n * - Respects the specified minimum log level\n *\n * @param pluginName - The plugin identifier (e.g., 'qtap-plugin-openai')\n * @param minLevel - Minimum log level when running standalone (default: 'debug')\n * @returns A logger instance\n *\n * @example\n * ```typescript\n * // In your plugin's provider.ts\n * import { createPluginLogger } from '@quilltap/plugin-utils';\n *\n * const logger = createPluginLogger('qtap-plugin-my-provider');\n *\n * export class MyProvider implements LLMProvider {\n * async sendMessage(params: LLMParams, apiKey: string): Promise<LLMResponse> {\n * logger.debug('Sending message', { model: params.model });\n *\n * try {\n * const response = await this.client.chat({...});\n * logger.info('Received response', { tokens: response.usage?.total_tokens });\n * return response;\n * } catch (error) {\n * logger.error('Failed to send message', { model: params.model }, error as Error);\n * throw error;\n * }\n * }\n * }\n * ```\n */\nexport function createPluginLogger(\n pluginName: string,\n minLevel: LogLevel = 'debug'\n): PluginLoggerWithChild {\n // Check for core logger factory from global namespace\n const coreFactory = getCoreLoggerFactory();\n if (coreFactory) {\n return coreFactory(pluginName);\n }\n\n // Standalone mode: use enhanced console logger\n return createConsoleLoggerWithChild(pluginName, minLevel);\n}\n\n/**\n * Get the minimum log level from environment\n *\n * Checks for LOG_LEVEL or QUILTTAP_LOG_LEVEL environment variables.\n * Useful for configuring standalone plugin logging.\n *\n * @returns The configured log level, or 'info' as default\n */\nexport function getLogLevelFromEnv(): LogLevel {\n if (typeof process !== 'undefined' && process.env) {\n const envLevel = process.env.LOG_LEVEL || process.env.QUILTTAP_LOG_LEVEL;\n if (envLevel && ['debug', 'info', 'warn', 'error'].includes(envLevel)) {\n return envLevel as LogLevel;\n }\n }\n return 'info';\n}\n","/**\n * Logging Utilities\n *\n * Exports the plugin logger bridge and related utilities.\n *\n * @module @quilltap/plugin-utils/logging\n */\n\nexport {\n createPluginLogger,\n hasCoreLogger,\n getLogLevelFromEnv,\n __injectCoreLoggerFactory,\n __clearCoreLoggerFactory,\n} from './plugin-logger';\n\nexport type { PluginLoggerWithChild } from './plugin-logger';\n\n// Re-export logger types from plugin-types\nexport type { PluginLogger, LogContext, LogLevel } from '@quilltap/plugin-types';\n\n// Re-export logger utilities from plugin-types for convenience\nexport { createConsoleLogger, createNoopLogger } from '@quilltap/plugin-types';\n","/**\n * @quilltap/plugin-utils\n *\n * Utility functions for Quilltap plugin development.\n *\n * This package provides runtime utilities that complement the type definitions\n * in @quilltap/plugin-types. It includes:\n *\n * - **Tool Parsing**: Parse tool calls from any LLM provider's response format\n * - **Tool Conversion**: Convert between OpenAI, Anthropic, and Google tool formats\n * - **Logger Bridge**: Logging that integrates with Quilltap's core or runs standalone\n *\n * @packageDocumentation\n * @module @quilltap/plugin-utils\n */\n\n// ============================================================================\n// Tool Utilities\n// ============================================================================\n\nexport {\n // Parsers\n parseToolCalls,\n parseOpenAIToolCalls,\n parseAnthropicToolCalls,\n parseGoogleToolCalls,\n detectToolCallFormat,\n hasToolCalls,\n\n // Converters\n convertToAnthropicFormat,\n convertToGoogleFormat,\n convertFromAnthropicFormat,\n convertFromGoogleFormat,\n convertToolTo,\n convertToolsTo,\n applyDescriptionLimit,\n} from './tools';\n\nexport type {\n // Tool types (re-exported from plugin-types)\n OpenAIToolDefinition,\n UniversalTool,\n AnthropicToolDefinition,\n GoogleToolDefinition,\n ToolCall,\n ToolCallRequest,\n ToolResult,\n ToolFormatOptions,\n\n // Utility types\n ToolCallFormat,\n ToolConvertTarget,\n} from './tools';\n\n// ============================================================================\n// Logging Utilities\n// ============================================================================\n\nexport {\n // Plugin logger factory\n createPluginLogger,\n hasCoreLogger,\n getLogLevelFromEnv,\n\n // Re-exported from plugin-types\n createConsoleLogger,\n createNoopLogger,\n\n // Internal APIs for Quilltap core\n __injectCoreLoggerFactory,\n __clearCoreLoggerFactory,\n} from './logging';\n\nexport type {\n // Logger types\n PluginLoggerWithChild,\n PluginLogger,\n LogContext,\n LogLevel,\n} from './logging';\n\n// ============================================================================\n// Version\n// ============================================================================\n\n/**\n * Version of the plugin-utils package.\n * Can be used at runtime to check compatibility.\n */\nexport const PLUGIN_UTILS_VERSION = '1.0.0';\n"],"mappings":";AAqCO,SAAS,qBAAqB,UAAsC;AACzE,QAAM,YAA+B,CAAC;AAEtC,MAAI;AACF,UAAM,OAAO;AAGb,QAAI,iBAAiB,MAAM;AAG3B,QAAI,CAAC,gBAAgB;AACnB,YAAM,UAAU,MAAM;AAGtB,uBAAiB,UAAU,CAAC,GAAG,SAAS;AAAA,IAC1C;AAEA,QAAI,kBAAkB,MAAM,QAAQ,cAAc,KAAK,eAAe,SAAS,GAAG;AAChF,iBAAW,YAAY,gBAAgB;AACrC,cAAM,KAAK;AAKX,YAAI,GAAG,SAAS,cAAc,GAAG,UAAU;AACzC,oBAAU,KAAK;AAAA,YACb,MAAM,GAAG,SAAS;AAAA,YAClB,WAAW,KAAK,MAAM,GAAG,SAAS,aAAa,IAAI;AAAA,UACrD,CAAC;AAAA,QACH;AAAA,MACF;AAAA,IACF;AAAA,EACF,SAAS,OAAO;AAEd,YAAQ,MAAM,mDAAmD,KAAK;AAAA,EACxE;AAEA,SAAO;AACT;AAqBO,SAAS,wBAAwB,UAAsC;AAC5E,QAAM,YAA+B,CAAC;AAEtC,MAAI;AACF,UAAM,OAAO;AAEb,QAAI,CAAC,MAAM,WAAW,CAAC,MAAM,QAAQ,KAAK,OAAO,GAAG;AAClD,aAAO;AAAA,IACT;AAEA,eAAW,SAAS,KAAK,SAAS;AAChC,YAAM,IAAI;AAEV,UAAI,EAAE,SAAS,cAAc,EAAE,MAAM;AACnC,kBAAU,KAAK;AAAA,UACb,MAAM,EAAE;AAAA,UACR,WAAW,EAAE,SAAS,CAAC;AAAA,QACzB,C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AAM,KAAK,OAAO,GAAG,cAAc,OAAO,CAAC;AAAA,UAC/C,QAAQ;AAAA,EAAK,MAAM,SAAS,MAAM,OAAO,KAAK;AAAA,QAChD;AAAA,MACF;AAAA,IACF;AAAA,IAEA,OAAO,CAAC,sBAAyD;AAC/D,aAAO,6BAA6B,QAAQ,UAAU;AAAA,QACpD,GAAG;AAAA,QACH,GAAG;AAAA,MACL,CAAC;AAAA,IACH;AAAA,EACF;AAEA,SAAO;AACT;AAyCO,SAAS,mBACd,YACA,WAAqB,SACE;AAEvB,QAAM,cAAc,qBAAqB;AACzC,MAAI,aAAa;AACf,WAAO,YAAY,UAAU;AAAA,EAC/B;AAGA,SAAO,6BAA6B,YAAY,QAAQ;AAC1D;AAUO,SAAS,qBAA+B;AAC7C,MAAI,OAAO,YAAY,eAAe,QAAQ,KAAK;AACjD,UAAM,WAAW,QAAQ,IAAI,aAAa,QAAQ,IAAI;AACtD,QAAI,YAAY,CAAC,SAAS,QAAQ,QAAQ,OAAO,EAAE,SAAS,QAAQ,GAAG;AACrE,aAAO;AAAA,IACT;AAAA,EACF;AACA,SAAO;AACT;;;ACjMA,SAAS,qBAAqB,wBAAwB;;;ACoE/C,IAAM,uBAAuB;","names":[]}
@@ -0,0 +1,109 @@
1
+ import { PluginLogger, LogContext, LogLevel } from '@quilltap/plugin-types';
2
+ export { LogContext, LogLevel, PluginLogger, createConsoleLogger, createNoopLogger } from '@quilltap/plugin-types';
3
+
4
+ /**
5
+ * Plugin Logger Bridge
6
+ *
7
+ * Provides a logger factory for plugins that automatically bridges
8
+ * to Quilltap's core logging when running inside the host application,
9
+ * or falls back to console logging when running standalone.
10
+ *
11
+ * @module @quilltap/plugin-utils/logging/plugin-logger
12
+ */
13
+
14
+ /**
15
+ * Extended logger interface with child logger support
16
+ */
17
+ interface PluginLoggerWithChild extends PluginLogger {
18
+ /**
19
+ * Create a child logger with additional context
20
+ * @param additionalContext Context to merge with parent context
21
+ * @returns A new logger with combined context
22
+ */
23
+ child(additionalContext: LogContext): PluginLoggerWithChild;
24
+ }
25
+ /**
26
+ * Type for the global Quilltap logger bridge
27
+ * Stored on globalThis to work across different npm package copies
28
+ */
29
+ declare global {
30
+ var __quilltap_logger_factory: ((pluginName: string) => PluginLoggerWithChild) | undefined;
31
+ }
32
+ /**
33
+ * Inject the core logger factory from Quilltap host
34
+ *
35
+ * This is called by Quilltap core when loading plugins to bridge
36
+ * plugin logging into the host's logging system. Uses globalThis
37
+ * to ensure it works even when plugins have their own copy of
38
+ * plugin-utils in their node_modules.
39
+ *
40
+ * **Internal API - not for plugin use**
41
+ *
42
+ * @param factory A function that creates a child logger for a plugin
43
+ */
44
+ declare function __injectCoreLoggerFactory(factory: (pluginName: string) => PluginLoggerWithChild): void;
45
+ /**
46
+ * Clear the injected core logger factory
47
+ *
48
+ * Useful for testing or when unloading the plugin system.
49
+ *
50
+ * **Internal API - not for plugin use**
51
+ */
52
+ declare function __clearCoreLoggerFactory(): void;
53
+ /**
54
+ * Check if a core logger has been injected
55
+ *
56
+ * @returns True if running inside Quilltap with core logging available
57
+ */
58
+ declare function hasCoreLogger(): boolean;
59
+ /**
60
+ * Create a plugin logger that bridges to Quilltap core logging
61
+ *
62
+ * When running inside Quilltap:
63
+ * - Routes all logs to the core logger
64
+ * - Tags logs with `{ plugin: pluginName, module: 'plugin' }`
65
+ * - Logs appear in Quilltap's combined.log and console
66
+ *
67
+ * When running standalone:
68
+ * - Falls back to console logging with `[pluginName]` prefix
69
+ * - Respects the specified minimum log level
70
+ *
71
+ * @param pluginName - The plugin identifier (e.g., 'qtap-plugin-openai')
72
+ * @param minLevel - Minimum log level when running standalone (default: 'debug')
73
+ * @returns A logger instance
74
+ *
75
+ * @example
76
+ * ```typescript
77
+ * // In your plugin's provider.ts
78
+ * import { createPluginLogger } from '@quilltap/plugin-utils';
79
+ *
80
+ * const logger = createPluginLogger('qtap-plugin-my-provider');
81
+ *
82
+ * export class MyProvider implements LLMProvider {
83
+ * async sendMessage(params: LLMParams, apiKey: string): Promise<LLMResponse> {
84
+ * logger.debug('Sending message', { model: params.model });
85
+ *
86
+ * try {
87
+ * const response = await this.client.chat({...});
88
+ * logger.info('Received response', { tokens: response.usage?.total_tokens });
89
+ * return response;
90
+ * } catch (error) {
91
+ * logger.error('Failed to send message', { model: params.model }, error as Error);
92
+ * throw error;
93
+ * }
94
+ * }
95
+ * }
96
+ * ```
97
+ */
98
+ declare function createPluginLogger(pluginName: string, minLevel?: LogLevel): PluginLoggerWithChild;
99
+ /**
100
+ * Get the minimum log level from environment
101
+ *
102
+ * Checks for LOG_LEVEL or QUILTTAP_LOG_LEVEL environment variables.
103
+ * Useful for configuring standalone plugin logging.
104
+ *
105
+ * @returns The configured log level, or 'info' as default
106
+ */
107
+ declare function getLogLevelFromEnv(): LogLevel;
108
+
109
+ export { type PluginLoggerWithChild, __clearCoreLoggerFactory, __injectCoreLoggerFactory, createPluginLogger, getLogLevelFromEnv, hasCoreLogger };