@easynet/agent-tool 1.0.72 → 1.0.73

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.
Files changed (44) hide show
  1. package/dist/api/expose/openapi.d.ts.map +1 -1
  2. package/dist/api/expose/openapiHttp.d.ts.map +1 -1
  3. package/dist/api/main.cjs +19 -19
  4. package/dist/api/main.js +4 -4
  5. package/dist/build.cjs +8 -8
  6. package/dist/build.js +3 -3
  7. package/dist/{chunk-OG5ZSXQ5.cjs → chunk-33N4Y6IS.cjs} +13 -13
  8. package/dist/{chunk-OG5ZSXQ5.cjs.map → chunk-33N4Y6IS.cjs.map} +1 -1
  9. package/dist/{chunk-J5EPH2QO.js → chunk-4YLATI7J.js} +4 -4
  10. package/dist/{chunk-J5EPH2QO.js.map → chunk-4YLATI7J.js.map} +1 -1
  11. package/dist/{chunk-VSFIF3WJ.js → chunk-EHXA64OA.js} +252 -259
  12. package/dist/chunk-EHXA64OA.js.map +1 -0
  13. package/dist/{chunk-YPGF5Y2Y.js → chunk-GSOJCOKN.js} +3 -3
  14. package/dist/{chunk-YPGF5Y2Y.js.map → chunk-GSOJCOKN.js.map} +1 -1
  15. package/dist/{chunk-45S2HPVU.js → chunk-HEVWKBBQ.js} +87 -60
  16. package/dist/chunk-HEVWKBBQ.js.map +1 -0
  17. package/dist/{chunk-JNIWNSCQ.cjs → chunk-LHKEJNKL.cjs} +87 -60
  18. package/dist/chunk-LHKEJNKL.cjs.map +1 -0
  19. package/dist/{chunk-WO4LZKPQ.cjs → chunk-OKKBKZWK.cjs} +4 -4
  20. package/dist/{chunk-WO4LZKPQ.cjs.map → chunk-OKKBKZWK.cjs.map} +1 -1
  21. package/dist/{chunk-YRFUGA3C.js → chunk-Q6W32HAP.js} +3 -3
  22. package/dist/{chunk-YRFUGA3C.js.map → chunk-Q6W32HAP.js.map} +1 -1
  23. package/dist/{chunk-3AM4SGUY.cjs → chunk-QVXWW657.cjs} +256 -263
  24. package/dist/chunk-QVXWW657.cjs.map +1 -0
  25. package/dist/{chunk-NMZ4IMEW.cjs → chunk-TBDSFXNG.cjs} +14 -14
  26. package/dist/{chunk-NMZ4IMEW.cjs.map → chunk-TBDSFXNG.cjs.map} +1 -1
  27. package/dist/{chunk-5J27MF7S.js → chunk-VYULM6NC.js} +4 -4
  28. package/dist/{chunk-5J27MF7S.js.map → chunk-VYULM6NC.js.map} +1 -1
  29. package/dist/{chunk-DTLALP7X.cjs → chunk-ZZHS55OM.cjs} +24 -24
  30. package/dist/{chunk-DTLALP7X.cjs.map → chunk-ZZHS55OM.cjs.map} +1 -1
  31. package/dist/core/runtime/PTCRuntime.d.ts +3 -0
  32. package/dist/core/runtime/PTCRuntime.d.ts.map +1 -1
  33. package/dist/extension.cjs +14 -14
  34. package/dist/extension.js +2 -2
  35. package/dist/index.cjs +37 -37
  36. package/dist/index.js +7 -7
  37. package/dist/tools/function/scanner.d.ts.map +1 -1
  38. package/dist/utils/cli/index.cjs +18 -18
  39. package/dist/utils/cli/index.js +4 -4
  40. package/package.json +1 -1
  41. package/dist/chunk-3AM4SGUY.cjs.map +0 -1
  42. package/dist/chunk-45S2HPVU.js.map +0 -1
  43. package/dist/chunk-JNIWNSCQ.cjs.map +0 -1
  44. package/dist/chunk-VSFIF3WJ.js.map +0 -1
@@ -1,5 +1,5 @@
1
1
  import { enrichSpecWithCanonicalSchema } from './chunk-NTWOVFEY.js';
2
- import { MCP_KIND, DirectoryScanner } from './chunk-YRFUGA3C.js';
2
+ import { MCP_KIND, DirectoryScanner } from './chunk-Q6W32HAP.js';
3
3
  import { setSandboxValidationEnabled } from './chunk-QXQ4477T.js';
4
4
  import { ToolRegistry } from './chunk-WUMLZERG.js';
5
5
  import { createTaggedError, withRetry } from './chunk-RZTTO5MQ.js';
@@ -1534,101 +1534,18 @@ var PTCRuntime = class {
1534
1534
  */
1535
1535
  async invoke(intent, ctx) {
1536
1536
  const startTime = Date.now();
1537
- if (this.logger.isEnabled("debug")) {
1538
- this.logger.debug("invoke.start", {
1539
- tool: intent.tool,
1540
- requestId: ctx.requestId,
1541
- taskId: ctx.taskId,
1542
- traceId: ctx.traceId,
1543
- purpose: intent.purpose,
1544
- args: this.logger.options.includeArgs ? sanitizeForLog(intent.args) : void 0
1545
- });
1546
- }
1547
- const span = this.tracing.startSpan({
1548
- name: `tool:${intent.tool}`,
1549
- traceId: ctx.traceId,
1550
- attributes: {
1551
- "tool.name": intent.tool,
1552
- "tool.purpose": intent.purpose,
1553
- requestId: ctx.requestId,
1554
- taskId: ctx.taskId
1555
- }
1556
- });
1557
- emitToolCalled(intent, ctx, this.getObservabilityDeps());
1537
+ const span = this.beginInvokeSpan(intent, ctx);
1538
+ const observability = this.getObservabilityDeps();
1539
+ emitToolCalled(intent, ctx, observability);
1558
1540
  try {
1559
- const spec = resolveTool(intent.tool, this.registry);
1560
- this.tracing.addEvent(span.spanId, "resolved", {
1561
- kind: spec.kind,
1562
- version: spec.version
1563
- });
1564
- const normalizedArgs = normalizeInputAliases(spec, intent.args);
1565
- const validatedArgs = validateInput(spec, normalizedArgs, this.validator);
1566
- const enrichedArgs = enrichDefaults(spec, validatedArgs, this.validator);
1567
- enforcePolicy(spec, enrichedArgs, ctx, {
1568
- policy: this.policy,
1569
- eventLog: this.eventLog,
1570
- metrics: this.metrics,
1571
- tracing: this.tracing
1572
- });
1573
- if (!this.budget.checkRateLimit(spec.name)) {
1574
- throw createTaggedError(
1575
- "BUDGET_EXCEEDED",
1576
- `Rate limit exceeded for tool: ${spec.name}`
1577
- );
1578
- }
1579
- await requireHumanApproval(spec, enrichedArgs, ctx, {
1580
- onApprovalRequired: this.config.onApprovalRequired,
1581
- eventLog: this.eventLog,
1582
- logger: this.logger
1583
- });
1584
- if (ctx.dryRun) {
1585
- return this.buildDryRunResult(spec, enrichedArgs, ctx, startTime, span.spanId);
1586
- }
1587
- const { result, raw } = await executeWithBudget(
1588
- spec,
1589
- enrichedArgs,
1590
- ctx,
1591
- span.spanId,
1592
- this.getPipelineDeps()
1593
- );
1594
- const toolError = asToolReturnedError(result);
1595
- if (toolError) {
1596
- const hint = buildInputSchemaHint(spec.inputSchema);
1597
- throw createTaggedError("UPSTREAM_ERROR", toolError.message, {
1598
- ...toolError.details && typeof toolError.details === "object" && !Array.isArray(toolError.details) ? toolError.details : {},
1599
- hint: hint ?? "Check the tool's input schema for required property names."
1600
- });
1541
+ const pipeline = await this.runInvokePipeline(intent, ctx, span.spanId);
1542
+ if (pipeline.dryRun) {
1543
+ return this.buildDryRunResult(pipeline.spec, pipeline.enrichedArgs, ctx, startTime, span.spanId);
1601
1544
  }
1602
- const validatedOutput = validateOutput(spec, result, this.validator);
1603
- const durationMs = Date.now() - startTime;
1604
- const builtEvidence = buildEvidence({
1605
- spec,
1606
- args: enrichedArgs,
1607
- result: validatedOutput,
1608
- raw,
1609
- ctx,
1610
- durationMs
1611
- });
1612
- const adapterEvidence = raw && typeof raw === "object" && Array.isArray(raw.evidence) ? raw.evidence : [];
1613
- const evidence = [...adapterEvidence, ...builtEvidence];
1614
- recordSuccess(spec, durationMs, evidence, span.spanId, this.getObservabilityDeps());
1615
- if (this.logger.isEnabled("debug")) {
1616
- this.logger.debug("invoke.ok", {
1617
- tool: spec.name,
1618
- durationMs,
1619
- result: this.logger.options.includeResults ? summarizeForLog(validatedOutput) : void 0,
1620
- raw: this.logger.options.includeRaw ? summarizeForLog(raw) : void 0
1621
- });
1622
- }
1623
- return {
1624
- ok: true,
1625
- result: validatedOutput,
1626
- evidence,
1627
- raw: this.config.includeRaw !== false ? raw : void 0
1628
- };
1545
+ return this.buildInvokeSuccessResult(pipeline, ctx, startTime, span.spanId, observability);
1629
1546
  } catch (error) {
1630
1547
  const durationMs = Date.now() - startTime;
1631
- return handleError(error, intent, ctx, durationMs, span.spanId, this.getObservabilityDeps());
1548
+ return handleError(error, intent, ctx, durationMs, span.spanId, observability);
1632
1549
  }
1633
1550
  }
1634
1551
  /**
@@ -1675,6 +1592,84 @@ var PTCRuntime = class {
1675
1592
  logger: this.logger
1676
1593
  };
1677
1594
  }
1595
+ beginInvokeSpan(intent, ctx) {
1596
+ if (this.logger.isEnabled("debug")) {
1597
+ this.logger.debug("invoke.start", {
1598
+ tool: intent.tool,
1599
+ requestId: ctx.requestId,
1600
+ taskId: ctx.taskId,
1601
+ traceId: ctx.traceId,
1602
+ purpose: intent.purpose,
1603
+ args: this.logger.options.includeArgs ? sanitizeForLog(intent.args) : void 0
1604
+ });
1605
+ }
1606
+ return this.tracing.startSpan({
1607
+ name: `tool:${intent.tool}`,
1608
+ traceId: ctx.traceId,
1609
+ attributes: {
1610
+ "tool.name": intent.tool,
1611
+ "tool.purpose": intent.purpose,
1612
+ requestId: ctx.requestId,
1613
+ taskId: ctx.taskId
1614
+ }
1615
+ });
1616
+ }
1617
+ async runInvokePipeline(intent, ctx, spanId) {
1618
+ const spec = resolveTool(intent.tool, this.registry);
1619
+ this.tracing.addEvent(spanId, "resolved", { kind: spec.kind, version: spec.version });
1620
+ const normalizedArgs = normalizeInputAliases(spec, intent.args);
1621
+ const validatedArgs = validateInput(spec, normalizedArgs, this.validator);
1622
+ const enrichedArgs = enrichDefaults(spec, validatedArgs, this.validator);
1623
+ enforcePolicy(spec, enrichedArgs, ctx, this.getPipelineDeps());
1624
+ if (!this.budget.checkRateLimit(spec.name)) {
1625
+ throw createTaggedError("BUDGET_EXCEEDED", `Rate limit exceeded for tool: ${spec.name}`);
1626
+ }
1627
+ await requireHumanApproval(spec, enrichedArgs, ctx, {
1628
+ onApprovalRequired: this.config.onApprovalRequired,
1629
+ eventLog: this.eventLog,
1630
+ logger: this.logger
1631
+ });
1632
+ if (ctx.dryRun) return { spec, enrichedArgs, dryRun: true };
1633
+ const execution = await executeWithBudget(spec, enrichedArgs, ctx, spanId, this.getPipelineDeps());
1634
+ return { spec, enrichedArgs, dryRun: false, result: execution.result, raw: execution.raw };
1635
+ }
1636
+ buildInvokeSuccessResult(pipeline, ctx, startTime, spanId, observability) {
1637
+ const toolError = asToolReturnedError(pipeline.result);
1638
+ if (toolError) {
1639
+ const hint = buildInputSchemaHint(pipeline.spec.inputSchema);
1640
+ throw createTaggedError("UPSTREAM_ERROR", toolError.message, {
1641
+ ...toolError.details && typeof toolError.details === "object" && !Array.isArray(toolError.details) ? toolError.details : {},
1642
+ hint: hint ?? "Check the tool's input schema for required property names."
1643
+ });
1644
+ }
1645
+ const validatedOutput = validateOutput(pipeline.spec, pipeline.result, this.validator);
1646
+ const durationMs = Date.now() - startTime;
1647
+ const builtEvidence = buildEvidence({
1648
+ spec: pipeline.spec,
1649
+ args: pipeline.enrichedArgs,
1650
+ result: validatedOutput,
1651
+ raw: pipeline.raw,
1652
+ ctx,
1653
+ durationMs
1654
+ });
1655
+ const adapterEvidence = getAdapterEvidence(pipeline.raw);
1656
+ const evidence = [...adapterEvidence, ...builtEvidence];
1657
+ recordSuccess(pipeline.spec, durationMs, evidence, spanId, observability);
1658
+ if (this.logger.isEnabled("debug")) {
1659
+ this.logger.debug("invoke.ok", {
1660
+ tool: pipeline.spec.name,
1661
+ durationMs,
1662
+ result: this.logger.options.includeResults ? summarizeForLog(validatedOutput) : void 0,
1663
+ raw: this.logger.options.includeRaw ? summarizeForLog(pipeline.raw) : void 0
1664
+ });
1665
+ }
1666
+ return {
1667
+ ok: true,
1668
+ result: validatedOutput,
1669
+ evidence,
1670
+ raw: this.config.includeRaw !== false ? pipeline.raw : void 0
1671
+ };
1672
+ }
1678
1673
  buildDryRunResult(spec, args, _ctx, startTime, spanId) {
1679
1674
  this.tracing.endSpan(spanId, "ok");
1680
1675
  return {
@@ -1697,6 +1692,11 @@ var PTCRuntime = class {
1697
1692
  };
1698
1693
  }
1699
1694
  };
1695
+ function getAdapterEvidence(raw) {
1696
+ if (!raw || typeof raw !== "object") return [];
1697
+ const value = raw.evidence;
1698
+ return Array.isArray(value) ? value : [];
1699
+ }
1700
1700
  function asToolReturnedError(result) {
1701
1701
  if (result == null || typeof result !== "object" || Array.isArray(result)) {
1702
1702
  return null;
@@ -2224,7 +2224,20 @@ function toolsToOpenAPISpec(registry, options = {}) {
2224
2224
  const version = options.version ?? "1.0.0";
2225
2225
  const basePath = (options.basePath ?? "").replace(/\/$/, "");
2226
2226
  const specs = registry.snapshot().map(enrichSpecWithCanonicalSchema);
2227
- const toolNamesSchema = {
2227
+ const prefix = basePath ? `${basePath}/` : "/";
2228
+ const paths = createBasePaths(prefix);
2229
+ const schemaEntries = addPerToolPaths(specs, prefix, paths);
2230
+ return {
2231
+ openapi: "3.0.3",
2232
+ info: { title, version },
2233
+ paths,
2234
+ components: {
2235
+ schemas: Object.fromEntries(schemaEntries)
2236
+ }
2237
+ };
2238
+ }
2239
+ function createToolNamesSchema() {
2240
+ return {
2228
2241
  type: "object",
2229
2242
  required: ["tools"],
2230
2243
  properties: {
@@ -2241,7 +2254,9 @@ function toolsToOpenAPISpec(registry, options = {}) {
2241
2254
  }
2242
2255
  }
2243
2256
  };
2244
- const invokeRequestBody = {
2257
+ }
2258
+ function createInvokeRequestBodySchema() {
2259
+ return {
2245
2260
  type: "object",
2246
2261
  required: ["tool", "args"],
2247
2262
  properties: {
@@ -2253,8 +2268,9 @@ function toolsToOpenAPISpec(registry, options = {}) {
2253
2268
  }
2254
2269
  }
2255
2270
  };
2256
- const prefix = basePath ? `${basePath}/` : "/";
2257
- const paths = {
2271
+ }
2272
+ function createBasePaths(prefix) {
2273
+ return {
2258
2274
  [`${prefix}tools`]: {
2259
2275
  get: {
2260
2276
  summary: "List tools",
@@ -2263,11 +2279,7 @@ function toolsToOpenAPISpec(registry, options = {}) {
2263
2279
  responses: {
2264
2280
  "200": {
2265
2281
  description: "List of tools",
2266
- content: {
2267
- "application/json": {
2268
- schema: toolNamesSchema
2269
- }
2270
- }
2282
+ content: { "application/json": { schema: createToolNamesSchema() } }
2271
2283
  }
2272
2284
  }
2273
2285
  }
@@ -2279,62 +2291,42 @@ function toolsToOpenAPISpec(registry, options = {}) {
2279
2291
  operationId: "invokeTool",
2280
2292
  requestBody: {
2281
2293
  required: true,
2282
- content: {
2283
- "application/json": {
2284
- schema: invokeRequestBody
2285
- }
2286
- }
2294
+ content: { "application/json": { schema: createInvokeRequestBodySchema() } }
2287
2295
  },
2288
2296
  responses: {
2289
- "200": {
2290
- description: "Tool result",
2291
- content: { "application/json": { schema: resultSchema } }
2292
- },
2293
- "400": {
2294
- description: "Bad request",
2295
- content: { "application/json": { schema: errorSchema } }
2296
- }
2297
+ "200": { description: "Tool result", content: { "application/json": { schema: resultSchema } } },
2298
+ "400": { description: "Bad request", content: { "application/json": { schema: errorSchema } } }
2297
2299
  }
2298
2300
  }
2299
2301
  }
2300
2302
  };
2303
+ }
2304
+ function addPerToolPaths(specs, prefix, paths) {
2301
2305
  const schemaEntries = [];
2302
- for (const s of specs) {
2303
- const key = toolSchemaKey(s.name);
2304
- schemaEntries.push([key, s.inputSchema]);
2305
- const slug = toolNameToSlug(s.name);
2306
- paths[`${prefix}invoke/${slug}`] = {
2307
- post: {
2308
- summary: s.description ?? s.name,
2309
- description: `Invoke tool \`${s.name}\`. Request body is the tool's arguments (JSON Schema below).`,
2310
- operationId: `invoke_${key}`,
2311
- requestBody: {
2312
- required: true,
2313
- content: {
2314
- "application/json": {
2315
- schema: { $ref: `#/components/schemas/${key}` }
2316
- }
2317
- }
2318
- },
2319
- responses: {
2320
- "200": {
2321
- description: "Tool result",
2322
- content: { "application/json": { schema: resultSchema } }
2323
- },
2324
- "400": {
2325
- description: "Bad request (invalid args or tool error)",
2326
- content: { "application/json": { schema: errorSchema } }
2327
- }
2328
- }
2329
- }
2330
- };
2306
+ for (const spec of specs) {
2307
+ const key = toolSchemaKey(spec.name);
2308
+ schemaEntries.push([key, spec.inputSchema]);
2309
+ paths[`${prefix}invoke/${toolNameToSlug(spec.name)}`] = createPerToolPathSpec(spec, key);
2331
2310
  }
2311
+ return schemaEntries;
2312
+ }
2313
+ function createPerToolPathSpec(spec, key) {
2332
2314
  return {
2333
- openapi: "3.0.3",
2334
- info: { title, version },
2335
- paths,
2336
- components: {
2337
- schemas: Object.fromEntries(schemaEntries)
2315
+ post: {
2316
+ summary: spec.description ?? spec.name,
2317
+ description: `Invoke tool \`${spec.name}\`. Request body is the tool's arguments (JSON Schema below).`,
2318
+ operationId: `invoke_${key}`,
2319
+ requestBody: {
2320
+ required: true,
2321
+ content: { "application/json": { schema: { $ref: `#/components/schemas/${key}` } } }
2322
+ },
2323
+ responses: {
2324
+ "200": { description: "Tool result", content: { "application/json": { schema: resultSchema } } },
2325
+ "400": {
2326
+ description: "Bad request (invalid args or tool error)",
2327
+ content: { "application/json": { schema: errorSchema } }
2328
+ }
2329
+ }
2338
2330
  }
2339
2331
  };
2340
2332
  }
@@ -2443,117 +2435,118 @@ function swaggerUiHtml(specUrl) {
2443
2435
  function createOpenAPIHttpServer(runtime, options = {}) {
2444
2436
  const basePath = (options.basePath ?? "").replace(/\/$/, "");
2445
2437
  const ctxFactory = options.execContextFactory ?? (() => ({ ...DEFAULT_CTX }));
2446
- const server = createServer(async (req, res) => {
2447
- const url = req.url ?? "/";
2448
- const path = url.split("?")[0] ?? "/";
2449
- const norm = basePath ? path === basePath ? "" : path.replace(basePath, "") || "/" : path;
2450
- try {
2451
- if (req.method === "GET" && (norm === "/" || norm === "/swagger")) {
2452
- const specPath = basePath ? `${basePath}/openapi.json` : "/openapi.json";
2453
- const html = swaggerUiHtml(specPath);
2454
- res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" });
2455
- res.end(html);
2456
- return;
2457
- }
2458
- if (req.method === "GET" && (norm === "/openapi.json" || norm === "/spec")) {
2459
- const spec = toolsToOpenAPISpec(runtime.getRegistry(), {
2460
- title: "Tool API",
2461
- version: "1.0.0",
2462
- basePath: basePath || void 0
2463
- });
2464
- sendJson(res, 200, spec);
2465
- return;
2466
- }
2467
- if (req.method === "GET" && norm === "/tools") {
2468
- const { enrichSpecWithCanonicalSchema: enrichSpecWithCanonicalSchema2 } = await import('./canonicalCoreSchemas-PHGTNPN5.js');
2469
- const specs = runtime.getRegistry().snapshot().map(enrichSpecWithCanonicalSchema2);
2470
- const tools = specs.map((s) => ({
2471
- name: s.name,
2472
- description: s.description,
2473
- kind: s.kind,
2474
- inputSchema: s.inputSchema
2475
- }));
2476
- sendJson(res, 200, { tools });
2477
- return;
2478
- }
2479
- if (req.method === "POST" && norm === "/invoke") {
2480
- let body;
2481
- try {
2482
- body = await parseBody(req);
2483
- } catch (err) {
2484
- const hint = err instanceof BodyParseError ? err.hint : void 0;
2485
- sendJson(res, 400, {
2486
- ok: false,
2487
- error: "Invalid JSON body",
2488
- kind: "BAD_REQUEST",
2489
- ...hint ? { hint } : {}
2490
- });
2491
- return;
2492
- }
2493
- const tool = body?.tool;
2494
- const args = body?.args ?? {};
2495
- if (typeof tool !== "string" || !tool.trim()) {
2496
- sendJson(res, 400, { error: "Missing or invalid 'tool' in body", kind: "BAD_REQUEST" });
2497
- return;
2498
- }
2499
- const ctx = ctxFactory(req);
2500
- const result = await runtime.invoke(
2501
- { tool: tool.trim(), args, purpose: "openapi" },
2502
- ctx
2503
- );
2504
- if (result.ok) {
2505
- sendJson(res, 200, { result: result.result });
2506
- return;
2507
- }
2508
- sendJson(res, 400, {
2509
- error: result.error?.message ?? "Tool failed",
2510
- kind: result.error?.kind,
2511
- details: result.error?.details
2512
- });
2513
- return;
2514
- }
2515
- if (req.method === "POST" && norm.startsWith("/invoke/") && norm.length > "/invoke/".length) {
2516
- const slug = norm.slice("/invoke/".length);
2517
- const toolName = slugToToolName(slug);
2518
- let args;
2519
- let hint;
2520
- try {
2521
- args = await parseBody(req);
2522
- } catch (err) {
2523
- hint = err instanceof BodyParseError ? err.hint : void 0;
2524
- sendJson(res, 400, {
2525
- ok: false,
2526
- error: "Invalid JSON body",
2527
- kind: "BAD_REQUEST",
2528
- ...hint ? { hint } : {}
2529
- });
2530
- return;
2531
- }
2532
- const ctx = ctxFactory(req);
2533
- const result = await runtime.invoke(
2534
- { tool: toolName, args: args ?? {}, purpose: "openapi" },
2535
- ctx
2536
- );
2537
- if (result.ok) {
2538
- sendJson(res, 200, { result: result.result });
2539
- return;
2540
- }
2541
- sendJson(res, 400, {
2542
- error: result.error?.message ?? "Tool failed",
2543
- kind: result.error?.kind,
2544
- details: result.error?.details
2545
- });
2546
- return;
2547
- }
2548
- res.writeHead(404, { "Content-Type": "application/json" });
2549
- res.end(JSON.stringify({ error: "Not found", path: norm }));
2550
- } catch (err) {
2551
- const message = err instanceof Error ? err.message : String(err);
2552
- sendJson(res, 500, { error: message, kind: "INTERNAL_ERROR" });
2553
- }
2554
- });
2438
+ const server = createServer(
2439
+ (req, res) => handleOpenApiHttpRequest({ runtime, req, res, basePath, ctxFactory })
2440
+ );
2555
2441
  return server;
2556
2442
  }
2443
+ async function handleOpenApiHttpRequest(input) {
2444
+ const norm = normalizePath(input.req.url, input.basePath);
2445
+ try {
2446
+ if (await maybeHandleDocsRoute(input, norm)) return;
2447
+ if (await maybeHandleToolsRoute(input, norm)) return;
2448
+ if (await maybeHandleInvokeRoute(input, norm)) return;
2449
+ sendJson(input.res, 404, { error: "Not found", path: norm });
2450
+ } catch (err) {
2451
+ const message = err instanceof Error ? err.message : String(err);
2452
+ sendJson(input.res, 500, { error: message, kind: "INTERNAL_ERROR" });
2453
+ }
2454
+ }
2455
+ function normalizePath(url, basePath) {
2456
+ const path = (url ?? "/").split("?")[0] ?? "/";
2457
+ return basePath ? path === basePath ? "" : path.replace(basePath, "") || "/" : path;
2458
+ }
2459
+ async function maybeHandleDocsRoute(input, norm) {
2460
+ if (input.req.method === "GET" && (norm === "/" || norm === "/swagger")) {
2461
+ const specPath = input.basePath ? `${input.basePath}/openapi.json` : "/openapi.json";
2462
+ input.res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" });
2463
+ input.res.end(swaggerUiHtml(specPath));
2464
+ return true;
2465
+ }
2466
+ if (input.req.method === "GET" && (norm === "/openapi.json" || norm === "/spec")) {
2467
+ sendJson(
2468
+ input.res,
2469
+ 200,
2470
+ toolsToOpenAPISpec(input.runtime.getRegistry(), {
2471
+ title: "Tool API",
2472
+ version: "1.0.0",
2473
+ basePath: input.basePath || void 0
2474
+ })
2475
+ );
2476
+ return true;
2477
+ }
2478
+ return false;
2479
+ }
2480
+ async function maybeHandleToolsRoute(input, norm) {
2481
+ if (input.req.method !== "GET" || norm !== "/tools") return false;
2482
+ const { enrichSpecWithCanonicalSchema: enrichSpecWithCanonicalSchema2 } = await import('./canonicalCoreSchemas-PHGTNPN5.js');
2483
+ const tools = input.runtime.getRegistry().snapshot().map(enrichSpecWithCanonicalSchema2).map((s) => ({
2484
+ name: s.name,
2485
+ description: s.description,
2486
+ kind: s.kind,
2487
+ inputSchema: s.inputSchema
2488
+ }));
2489
+ sendJson(input.res, 200, { tools });
2490
+ return true;
2491
+ }
2492
+ async function maybeHandleInvokeRoute(input, norm) {
2493
+ if (input.req.method !== "POST") return false;
2494
+ if (norm === "/invoke") {
2495
+ const body = await parseBodyOrSendError(input.req, input.res);
2496
+ if (!body) return true;
2497
+ const tool = body.tool;
2498
+ if (typeof tool !== "string" || !tool.trim()) {
2499
+ sendJson(input.res, 400, { error: "Missing or invalid 'tool' in body", kind: "BAD_REQUEST" });
2500
+ return true;
2501
+ }
2502
+ await invokeAndSend(input.runtime, input.ctxFactory(input.req), input.res, tool.trim(), body.args ?? {});
2503
+ return true;
2504
+ }
2505
+ if (norm.startsWith("/invoke/") && norm.length > "/invoke/".length) {
2506
+ const args = await parseArgsOrSendError(input.req, input.res);
2507
+ if (args === void 0) return true;
2508
+ await invokeAndSend(input.runtime, input.ctxFactory(input.req), input.res, slugToToolName(norm.slice(8)), args);
2509
+ return true;
2510
+ }
2511
+ return false;
2512
+ }
2513
+ async function parseBodyOrSendError(req, res) {
2514
+ try {
2515
+ return await parseBody(req);
2516
+ } catch (err) {
2517
+ sendBadJsonBody(res, err);
2518
+ return null;
2519
+ }
2520
+ }
2521
+ async function parseArgsOrSendError(req, res) {
2522
+ try {
2523
+ return await parseBody(req) ?? {};
2524
+ } catch (err) {
2525
+ sendBadJsonBody(res, err);
2526
+ return void 0;
2527
+ }
2528
+ }
2529
+ function sendBadJsonBody(res, err) {
2530
+ const hint = err instanceof BodyParseError ? err.hint : void 0;
2531
+ sendJson(res, 400, {
2532
+ ok: false,
2533
+ error: "Invalid JSON body",
2534
+ kind: "BAD_REQUEST",
2535
+ ...hint ? { hint } : {}
2536
+ });
2537
+ }
2538
+ async function invokeAndSend(runtime, ctx, res, tool, args) {
2539
+ const result = await runtime.invoke({ tool, args, purpose: "openapi" }, ctx);
2540
+ if (result.ok) {
2541
+ sendJson(res, 200, { result: result.result });
2542
+ return;
2543
+ }
2544
+ sendJson(res, 400, {
2545
+ error: result.error?.message ?? "Tool failed",
2546
+ kind: result.error?.kind,
2547
+ details: result.error?.details
2548
+ });
2549
+ }
2557
2550
  function listenOpenAPIHttpServer(server, options = {}) {
2558
2551
  return new Promise((resolve4, reject) => {
2559
2552
  const port = options.port ?? 0;
@@ -2713,5 +2706,5 @@ async function runMCPServerOverStdio(runtime, options = {}) {
2713
2706
  }
2714
2707
 
2715
2708
  export { PTCRuntime, createHttpService, createMCPServer, createMCPServerStreamableHttp, createMCPStreamableHttpHandler, createRuntimeFromConfig, createRuntimeFromConfigSync, expandToolDescriptorsToRegistryNames, fileDescriptorToPackagePrefix, findAndLoadToolConfig, getDisplayScope, isBarePackageDescriptor, loadToolConfig, npmDescriptorToPackagePrefixWithVersion, resolveSandboxedPath, resolveToolDescriptor, runMCPServerOverStdio };
2716
- //# sourceMappingURL=chunk-VSFIF3WJ.js.map
2717
- //# sourceMappingURL=chunk-VSFIF3WJ.js.map
2709
+ //# sourceMappingURL=chunk-EHXA64OA.js.map
2710
+ //# sourceMappingURL=chunk-EHXA64OA.js.map