UNPKG

@posthog/wizard

Version:

The PostHog wizard helps you to configure your project

115 lines 4.71 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.query = void 0; const axios_1 = __importDefault(require("axios")); const zod_to_json_schema_1 = require("zod-to-json-schema"); const urls_1 = require("./urls"); const analytics_1 = require("./analytics"); const axios_2 = require("axios"); const debug_1 = require("./debug"); const errors_1 = require("./errors"); const crypto = __importStar(require("node:crypto")); const generateTraceId = () => { const randomBytes = crypto.randomBytes(32); return crypto.createHash('sha256').update(randomBytes).digest('hex'); }; const TRACE_ID = generateTraceId(); const query = async ({ message, model = 'o4-mini', region, schema, accessToken, projectId: _, // TODO: Use this to switch the wizard query endpoint over to the new LLM Gateway }) => { const fullSchema = (0, zod_to_json_schema_1.zodToJsonSchema)(schema, 'schema'); const jsonSchema = fullSchema.definitions; (0, debug_1.debug)('Full schema:', JSON.stringify(fullSchema, null, 2)); (0, debug_1.debug)('Query request:', { url: `${(0, urls_1.getCloudUrlFromRegion)(region)}/api/wizard/query`, accessToken, message: message.substring(0, 100) + '...', json_schema: { ...jsonSchema, name: 'schema', strict: true }, }); const response = await axios_1.default .post(`${(0, urls_1.getCloudUrlFromRegion)(region)}/api/wizard/query`, { message, model, json_schema: { ...jsonSchema, name: 'schema', strict: true }, }, { headers: { Authorization: `Bearer ${accessToken}`, 'X-PostHog-Trace-Id': TRACE_ID, ...(process.env.RECORD_FIXTURES === 'true' ? { 'X-PostHog-Wizard-Fixture-Generation': true } : {}), }, }) .catch((error) => { (0, debug_1.debug)('Query error:', error); if (error instanceof axios_2.AxiosError) { analytics_1.analytics.captureException(error, { response_status_code: error.response?.status, message, model, json_schema: jsonSchema, type: 'wizard_query_error', }); if (error.response?.status === 429) { throw new errors_1.RateLimitError(); } } throw error; }); (0, debug_1.debug)('Query response:', { status: response.status, data: response.data, }); const validation = schema.safeParse(response.data.data); if (!validation.success) { (0, debug_1.debug)('Validation error:', validation.error); throw new Error(`Invalid response from wizard: ${validation.error.message}`); } if (process.env.NODE_ENV === 'test') { const { fixtureTracker } = await import('../../e2e-tests/mocks/fixture-tracker.js'); const requestBody = JSON.stringify({ message, model, json_schema: { ...jsonSchema, name: 'schema', strict: true }, }); fixtureTracker.saveQueryFixture(requestBody, validation.data); } return validation.data; }; exports.query = query; //# sourceMappingURL=query.js.map