|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/** |
| 4 | + * Search docs for the agent |
| 5 | + * |
| 6 | + * Gathers relevant commercetools documentation as grounding context for the agent, with instrumentation headers. |
| 7 | + * Results are written to stdout for consumption by AI agents. |
| 8 | + * |
| 9 | + * Usage: |
| 10 | + * node scripts/docs-search.mjs --query "how to create payment" --app-name <name> --model <model> --skill-name <skill> [options] |
| 11 | + * |
| 12 | + * Required: |
| 13 | + * --query <string> Search query |
| 14 | + * --app-name <string> App identifier (e.g., vscode-copilot, claude-desktop) |
| 15 | + * --model <string> Model name (e.g., claude-sonnet-4.5, gpt-4) |
| 16 | + * --skill-name <string> Skill identifier (e.g., commercetools-checkout) |
| 17 | + * |
| 18 | + * Optional: |
| 19 | + * --limit <number> Number of results (default: 3) |
| 20 | + * --content-types <string> Comma-separated content types |
| 21 | + */ |
| 22 | + |
| 23 | +import { parseArgs } from 'util'; |
| 24 | + |
| 25 | +const CONTEXT_URL = 'https://docs.commercetools.com/apis/rest/tools/documentation-search'; |
| 26 | + |
| 27 | +// Parse command line arguments |
| 28 | +const { values } = parseArgs({ |
| 29 | + options: { |
| 30 | + query: { type: 'string' }, |
| 31 | + limit: { type: 'string', default: '3' }, |
| 32 | + 'app-name': { type: 'string' }, |
| 33 | + model: { type: 'string' }, |
| 34 | + 'skill-name': { type: 'string' }, |
| 35 | + 'content-types': { type: 'string' }, |
| 36 | + }, |
| 37 | +}); |
| 38 | + |
| 39 | +// Validate required parameters |
| 40 | +const missingParams = []; |
| 41 | +if (!values.query) missingParams.push('--query'); |
| 42 | +if (!values['app-name']) missingParams.push('--app-name'); |
| 43 | +if (!values.model) missingParams.push('--model'); |
| 44 | +if (!values['skill-name']) missingParams.push('--skill-name'); |
| 45 | + |
| 46 | +if (missingParams.length > 0) { |
| 47 | + console.error(`Error: Missing required parameters: ${missingParams.join(', ')}`); |
| 48 | + console.error('Usage: node scripts/docs-search.mjs --query "search" --app-name "app" --model "model" --skill-name "skill"'); |
| 49 | + process.exit(1); |
| 50 | +} |
| 51 | + |
| 52 | +const normalizedLimit = Math.min(parseInt(values.limit, 10), 20) |
| 53 | + |
| 54 | +// Build request body |
| 55 | +const requestBody = { |
| 56 | + query: values.query, |
| 57 | + limit: normalizedLimit, |
| 58 | + products: ['Composable Commerce', 'Checkout', 'Connect', 'AI Hub'] |
| 59 | +}; |
| 60 | + |
| 61 | +// Add optional content types filter |
| 62 | +if (values['content-types']) { |
| 63 | + requestBody.contentTypes = values['content-types'].split(',').map(t => t.trim()); |
| 64 | +} |
| 65 | + |
| 66 | +const requestData = JSON.stringify(requestBody); |
| 67 | + |
| 68 | +async function main() { |
| 69 | + const controller = new AbortController(); |
| 70 | + const timeoutId = setTimeout(() => controller.abort(), 60000); |
| 71 | + |
| 72 | + try { |
| 73 | + const res = await fetch(CONTEXT_URL, { |
| 74 | + method: 'POST', |
| 75 | + headers: { |
| 76 | + 'Content-Type': 'application/json', |
| 77 | + 'Content-Length': Buffer.byteLength(requestData), |
| 78 | + 'User-Agent': `${values['app-name']}/1.0 (${values.model})`, |
| 79 | + 'X-Model': values.model, |
| 80 | + 'X-Client-Type': values['app-name'], |
| 81 | + 'X-Skill-Name': values['skill-name'], |
| 82 | + }, |
| 83 | + body: requestData, |
| 84 | + signal: controller.signal, |
| 85 | + }); |
| 86 | + |
| 87 | + if (res.status !== 200) { |
| 88 | + process.exit(0); |
| 89 | + } |
| 90 | + |
| 91 | + let response; |
| 92 | + try { |
| 93 | + response = await res.json(); |
| 94 | + } catch (err) { |
| 95 | + process.exit(0); |
| 96 | + } |
| 97 | + |
| 98 | + if (response.error) { |
| 99 | + process.exit(0); |
| 100 | + } |
| 101 | + |
| 102 | + // Format results for AI agent consumption |
| 103 | + if (response.result && Array.isArray(response.result)) { |
| 104 | + const results = response.result; |
| 105 | + |
| 106 | + if (results.length === 0) { |
| 107 | + process.stdout.write('No results found. Try broadening your search query or adjusting filters.\n'); |
| 108 | + process.exit(0); |
| 109 | + } |
| 110 | + |
| 111 | + // Write results in markdown format |
| 112 | + process.stdout.write(`# Documentation Search Results\n\n`); |
| 113 | + process.stdout.write(`Query: "${values.query}"\n`); |
| 114 | + process.stdout.write(`Found ${results.length} result(s)\n\n`); |
| 115 | + process.stdout.write('---\n\n'); |
| 116 | + |
| 117 | + results.forEach((item, index) => { |
| 118 | + process.stdout.write(`## Result ${index + 1}\n\n`); |
| 119 | + |
| 120 | + if (item.metadata) { |
| 121 | + if (item.metadata.title) { |
| 122 | + process.stdout.write(`**Title:** ${item.metadata.title}\n\n`); |
| 123 | + } |
| 124 | + if (item.metadata.url) { |
| 125 | + const url = item.metadata.url.split('#')[0]; |
| 126 | + process.stdout.write(`**URL:** ${url}\n\n`); |
| 127 | + } |
| 128 | + if (item.metadata.contentType) { |
| 129 | + process.stdout.write(`**Type:** ${item.metadata.contentType}\n\n`); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + if (item.content) { |
| 134 | + process.stdout.write(`**Content:**\n\n${item.content}\n\n`); |
| 135 | + } |
| 136 | + |
| 137 | + process.stdout.write('---\n\n'); |
| 138 | + }); |
| 139 | + } else { |
| 140 | + // Fallback: output raw response |
| 141 | + process.stdout.write(JSON.stringify(response, null, 2)); |
| 142 | + process.stdout.write('\n'); |
| 143 | + } |
| 144 | + } catch (err) { |
| 145 | + process.exit(0); |
| 146 | + } finally { |
| 147 | + clearTimeout(timeoutId); |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +main(); |
0 commit comments