@ctxprotocol/sdk 0.9.0 → 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
@@ -236,6 +236,47 @@ var Query = class {
236
236
  constructor(client) {
237
237
  this.client = client;
238
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
+ }
239
280
  buildSyntheticTraceFromRunResult(params) {
240
281
  const timeline = params.toolsUsed.map((tool, index) => ({
241
282
  stepType: "tool-call",
@@ -378,6 +419,7 @@ var Query = class {
378
419
  async run(options) {
379
420
  const opts = typeof options === "string" ? { query: options } : options;
380
421
  let terminalError;
422
+ let finalResult;
381
423
  for await (const event of this.stream(opts)) {
382
424
  if (event.type === "error") {
383
425
  terminalError = {
@@ -389,9 +431,12 @@ var Query = class {
389
431
  continue;
390
432
  }
391
433
  if (event.type === "done") {
392
- return event.result;
434
+ finalResult = event.result;
393
435
  }
394
436
  }
437
+ if (finalResult) {
438
+ return finalResult;
439
+ }
395
440
  if (terminalError) {
396
441
  throw new ContextError(terminalError.error, terminalError.code);
397
442
  }
@@ -443,7 +488,11 @@ var Query = class {
443
488
  body: JSON.stringify({
444
489
  query: opts.query,
445
490
  tools: opts.tools,
446
- modelId: opts.modelId,
491
+ resumeFrom: opts.resumeFrom,
492
+ forkFrom: opts.forkFrom,
493
+ clarificationPolicy: opts.clarificationPolicy,
494
+ answerModelId: opts.answerModelId,
495
+ responseShape: opts.responseShape,
447
496
  includeData: opts.includeData,
448
497
  includeDataUrl: opts.includeDataUrl,
449
498
  includeDeveloperTrace: opts.includeDeveloperTrace,
@@ -481,22 +530,31 @@ var Query = class {
481
530
  return event;
482
531
  }
483
532
  if (event.type === "done") {
533
+ const normalizedResult = this.normalizeResult(event.result);
484
534
  let mergedTrace = this.mergeDeveloperTrace(
485
535
  aggregatedTrace,
486
- event.result.developerTrace
536
+ normalizedResult.developerTrace
487
537
  );
488
538
  if (!mergedTrace && opts.includeDeveloperTrace) {
489
539
  mergedTrace = statusTimeline.length > 0 ? this.buildSyntheticTraceFromStreamStatus({
490
540
  statusTimeline,
491
- toolsUsed: event.result.toolsUsed,
492
- durationMs: event.result.durationMs
541
+ toolsUsed: normalizedResult.toolsUsed,
542
+ durationMs: normalizedResult.durationMs
493
543
  }) : this.buildSyntheticTraceFromRunResult({
494
- toolsUsed: event.result.toolsUsed,
495
- durationMs: event.result.durationMs
544
+ toolsUsed: normalizedResult.toolsUsed,
545
+ durationMs: normalizedResult.durationMs
496
546
  });
497
547
  }
498
548
  if (mergedTrace) {
499
- 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;
500
558
  }
501
559
  }
502
560
  return event;