@ctxprotocol/sdk 0.8.5 → 0.10.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.
package/dist/index.js CHANGED
@@ -17,6 +17,14 @@ var Discovery = class {
17
17
  constructor(client) {
18
18
  this.client = client;
19
19
  }
20
+ /**
21
+ * Fetch a single marketplace tool by its unique ID.
22
+ */
23
+ async get(toolId) {
24
+ return this.client._fetch(
25
+ `/api/v1/tools/${encodeURIComponent(toolId)}`
26
+ );
27
+ }
20
28
  async search(queryOrOptions, limit) {
21
29
  const options = typeof queryOrOptions === "string" ? { query: queryOrOptions, limit } : queryOrOptions;
22
30
  const params = new URLSearchParams();
@@ -228,6 +236,47 @@ var Query = class {
228
236
  constructor(client) {
229
237
  this.client = client;
230
238
  }
239
+ normalizeResult(result) {
240
+ const candidate = result;
241
+ if (candidate.outcomeType === "clarification_required" && "clarification" in candidate && candidate.clarification) {
242
+ return candidate;
243
+ }
244
+ if (candidate.outcomeType === "capability_miss" && "capabilityMiss" in candidate && candidate.capabilityMiss) {
245
+ return candidate;
246
+ }
247
+ return {
248
+ ...candidate,
249
+ outcomeType: "answer"
250
+ };
251
+ }
252
+ buildPolicyErrorEvent(params) {
253
+ if (params.clarificationPolicy !== "error") {
254
+ return;
255
+ }
256
+ if (params.result.outcomeType === "clarification_required") {
257
+ return {
258
+ type: "error",
259
+ error: params.result.response,
260
+ code: "clarification_required",
261
+ reasonCode: "clarification_required",
262
+ outcomeType: "clarification_required",
263
+ clarification: params.result.clarification,
264
+ querySession: params.result.querySession
265
+ };
266
+ }
267
+ if (params.result.outcomeType === "capability_miss") {
268
+ return {
269
+ type: "error",
270
+ error: params.result.response,
271
+ code: "capability_miss",
272
+ reasonCode: "capability_miss",
273
+ outcomeType: "capability_miss",
274
+ capabilityMiss: params.result.capabilityMiss,
275
+ querySession: params.result.querySession
276
+ };
277
+ }
278
+ return void 0;
279
+ }
231
280
  buildSyntheticTraceFromRunResult(params) {
232
281
  const timeline = params.toolsUsed.map((tool, index) => ({
233
282
  stepType: "tool-call",
@@ -370,6 +419,7 @@ var Query = class {
370
419
  async run(options) {
371
420
  const opts = typeof options === "string" ? { query: options } : options;
372
421
  let terminalError;
422
+ let finalResult;
373
423
  for await (const event of this.stream(opts)) {
374
424
  if (event.type === "error") {
375
425
  terminalError = {
@@ -381,9 +431,12 @@ var Query = class {
381
431
  continue;
382
432
  }
383
433
  if (event.type === "done") {
384
- return event.result;
434
+ finalResult = event.result;
385
435
  }
386
436
  }
437
+ if (finalResult) {
438
+ return finalResult;
439
+ }
387
440
  if (terminalError) {
388
441
  throw new ContextError(terminalError.error, terminalError.code);
389
442
  }
@@ -435,7 +488,11 @@ var Query = class {
435
488
  body: JSON.stringify({
436
489
  query: opts.query,
437
490
  tools: opts.tools,
438
- modelId: opts.modelId,
491
+ resumeFrom: opts.resumeFrom,
492
+ forkFrom: opts.forkFrom,
493
+ clarificationPolicy: opts.clarificationPolicy,
494
+ answerModelId: opts.answerModelId,
495
+ responseShape: opts.responseShape,
439
496
  includeData: opts.includeData,
440
497
  includeDataUrl: opts.includeDataUrl,
441
498
  includeDeveloperTrace: opts.includeDeveloperTrace,
@@ -473,22 +530,31 @@ var Query = class {
473
530
  return event;
474
531
  }
475
532
  if (event.type === "done") {
533
+ const normalizedResult = this.normalizeResult(event.result);
476
534
  let mergedTrace = this.mergeDeveloperTrace(
477
535
  aggregatedTrace,
478
- event.result.developerTrace
536
+ normalizedResult.developerTrace
479
537
  );
480
538
  if (!mergedTrace && opts.includeDeveloperTrace) {
481
539
  mergedTrace = statusTimeline.length > 0 ? this.buildSyntheticTraceFromStreamStatus({
482
540
  statusTimeline,
483
- toolsUsed: event.result.toolsUsed,
484
- durationMs: event.result.durationMs
541
+ toolsUsed: normalizedResult.toolsUsed,
542
+ durationMs: normalizedResult.durationMs
485
543
  }) : this.buildSyntheticTraceFromRunResult({
486
- toolsUsed: event.result.toolsUsed,
487
- durationMs: event.result.durationMs
544
+ toolsUsed: normalizedResult.toolsUsed,
545
+ durationMs: normalizedResult.durationMs
488
546
  });
489
547
  }
490
548
  if (mergedTrace) {
491
- event.result.developerTrace = mergedTrace;
549
+ normalizedResult.developerTrace = mergedTrace;
550
+ }
551
+ event.result = normalizedResult;
552
+ const policyErrorEvent = this.buildPolicyErrorEvent({
553
+ result: normalizedResult,
554
+ clarificationPolicy: opts.clarificationPolicy
555
+ });
556
+ if (policyErrorEvent) {
557
+ return policyErrorEvent;
492
558
  }
493
559
  }
494
560
  return event;