@ctxprotocol/sdk 0.8.3 → 0.8.4
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/README.md +13 -1
- package/dist/client/index.cjs +167 -3
- package/dist/client/index.cjs.map +1 -1
- package/dist/client/index.d.cts +79 -2
- package/dist/client/index.d.ts +79 -2
- package/dist/client/index.js +167 -3
- package/dist/client/index.js.map +1 -1
- package/dist/index.cjs +167 -3
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +167 -3
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/client/index.d.cts
CHANGED
|
@@ -331,6 +331,12 @@ interface QueryOptions {
|
|
|
331
331
|
* Useful for large payload workflows where inline JSON is not ideal.
|
|
332
332
|
*/
|
|
333
333
|
includeDataUrl?: boolean;
|
|
334
|
+
/**
|
|
335
|
+
* Include machine-readable developer trace output for this query response.
|
|
336
|
+
* When enabled, the server may return timeline data describing retries,
|
|
337
|
+
* fallbacks, loop checks, and intermediate recovery behavior.
|
|
338
|
+
*/
|
|
339
|
+
includeDeveloperTrace?: boolean;
|
|
334
340
|
/**
|
|
335
341
|
* Query orchestration depth mode:
|
|
336
342
|
* - `fast`: lower-latency path
|
|
@@ -344,6 +350,61 @@ interface QueryOptions {
|
|
|
344
350
|
*/
|
|
345
351
|
idempotencyKey?: string;
|
|
346
352
|
}
|
|
353
|
+
/**
|
|
354
|
+
* Tool reference attached to developer trace timeline steps.
|
|
355
|
+
*/
|
|
356
|
+
interface QueryDeveloperTraceToolRef {
|
|
357
|
+
id?: string;
|
|
358
|
+
name?: string;
|
|
359
|
+
method?: string;
|
|
360
|
+
[key: string]: unknown;
|
|
361
|
+
}
|
|
362
|
+
/**
|
|
363
|
+
* Loop metadata attached to developer trace timeline steps.
|
|
364
|
+
*/
|
|
365
|
+
interface QueryDeveloperTraceLoopInfo {
|
|
366
|
+
name?: string;
|
|
367
|
+
iteration?: number;
|
|
368
|
+
maxIterations?: number;
|
|
369
|
+
[key: string]: unknown;
|
|
370
|
+
}
|
|
371
|
+
/**
|
|
372
|
+
* A single developer-trace timeline step.
|
|
373
|
+
*/
|
|
374
|
+
interface QueryDeveloperTraceStep {
|
|
375
|
+
stepType?: string;
|
|
376
|
+
event?: string;
|
|
377
|
+
status?: string;
|
|
378
|
+
message?: string;
|
|
379
|
+
timestampMs?: number;
|
|
380
|
+
tool?: QueryDeveloperTraceToolRef;
|
|
381
|
+
attempt?: number;
|
|
382
|
+
loop?: QueryDeveloperTraceLoopInfo;
|
|
383
|
+
metadata?: Record<string, unknown>;
|
|
384
|
+
[key: string]: unknown;
|
|
385
|
+
}
|
|
386
|
+
/**
|
|
387
|
+
* Aggregate counters that summarize developer-trace behavior.
|
|
388
|
+
*/
|
|
389
|
+
interface QueryDeveloperTraceSummary {
|
|
390
|
+
toolCalls?: number;
|
|
391
|
+
retryCount?: number;
|
|
392
|
+
selfHealCount?: number;
|
|
393
|
+
fallbackCount?: number;
|
|
394
|
+
failureCount?: number;
|
|
395
|
+
recoveryCount?: number;
|
|
396
|
+
completionChecks?: number;
|
|
397
|
+
loopCount?: number;
|
|
398
|
+
[key: string]: unknown;
|
|
399
|
+
}
|
|
400
|
+
/**
|
|
401
|
+
* Developer Mode trace payload returned per query response (opt-in).
|
|
402
|
+
*/
|
|
403
|
+
interface QueryDeveloperTrace {
|
|
404
|
+
summary?: QueryDeveloperTraceSummary;
|
|
405
|
+
timeline?: QueryDeveloperTraceStep[];
|
|
406
|
+
[key: string]: unknown;
|
|
407
|
+
}
|
|
347
408
|
/**
|
|
348
409
|
* Information about a tool that was used during a query response
|
|
349
410
|
*/
|
|
@@ -383,6 +444,8 @@ interface QueryResult {
|
|
|
383
444
|
data?: unknown;
|
|
384
445
|
/** Optional blob URL for persisted execution data (when includeDataUrl=true) */
|
|
385
446
|
dataUrl?: string;
|
|
447
|
+
/** Optional machine-readable Developer Mode trace payload */
|
|
448
|
+
developerTrace?: QueryDeveloperTrace;
|
|
386
449
|
}
|
|
387
450
|
/**
|
|
388
451
|
* Successful response from the /api/v1/query endpoint
|
|
@@ -395,6 +458,7 @@ interface QueryApiSuccessResponse {
|
|
|
395
458
|
durationMs: number;
|
|
396
459
|
data?: unknown;
|
|
397
460
|
dataUrl?: string;
|
|
461
|
+
developerTrace?: QueryDeveloperTrace;
|
|
398
462
|
}
|
|
399
463
|
/**
|
|
400
464
|
* Raw API response from the query endpoint
|
|
@@ -414,6 +478,11 @@ interface QueryStreamTextDeltaEvent {
|
|
|
414
478
|
type: "text-delta";
|
|
415
479
|
delta: string;
|
|
416
480
|
}
|
|
481
|
+
/** Emitted when the server streams developer trace updates/chunks */
|
|
482
|
+
interface QueryStreamDeveloperTraceEvent {
|
|
483
|
+
type: "developer-trace";
|
|
484
|
+
trace: QueryDeveloperTrace;
|
|
485
|
+
}
|
|
417
486
|
/** Emitted when the full response is complete */
|
|
418
487
|
interface QueryStreamDoneEvent {
|
|
419
488
|
type: "done";
|
|
@@ -422,7 +491,7 @@ interface QueryStreamDoneEvent {
|
|
|
422
491
|
/**
|
|
423
492
|
* Union of all events emitted during a streaming query
|
|
424
493
|
*/
|
|
425
|
-
type QueryStreamEvent = QueryStreamToolStatusEvent | QueryStreamTextDeltaEvent | QueryStreamDoneEvent;
|
|
494
|
+
type QueryStreamEvent = QueryStreamToolStatusEvent | QueryStreamTextDeltaEvent | QueryStreamDeveloperTraceEvent | QueryStreamDoneEvent;
|
|
426
495
|
/**
|
|
427
496
|
* Specific error codes returned by the Context Protocol API
|
|
428
497
|
*/
|
|
@@ -534,6 +603,10 @@ declare class Tools {
|
|
|
534
603
|
declare class Query {
|
|
535
604
|
private client;
|
|
536
605
|
constructor(client: ContextClient);
|
|
606
|
+
private buildSyntheticTraceFromRunResult;
|
|
607
|
+
private buildSyntheticTraceFromStreamStatus;
|
|
608
|
+
private mergeDeveloperTrace;
|
|
609
|
+
private parseStreamEvent;
|
|
537
610
|
/**
|
|
538
611
|
* Run an agentic query and wait for the full response.
|
|
539
612
|
*
|
|
@@ -573,6 +646,7 @@ declare class Query {
|
|
|
573
646
|
* Event types:
|
|
574
647
|
* - `tool-status` — A tool started executing or changed status
|
|
575
648
|
* - `text-delta` — A chunk of the AI response text
|
|
649
|
+
* - `developer-trace` — Runtime trace metadata (when includeDeveloperTrace=true)
|
|
576
650
|
* - `done` — The full response is complete (includes final `QueryResult`)
|
|
577
651
|
*
|
|
578
652
|
* @param options - Query options or a plain string question
|
|
@@ -588,6 +662,9 @@ declare class Query {
|
|
|
588
662
|
* case "text-delta":
|
|
589
663
|
* process.stdout.write(event.delta);
|
|
590
664
|
* break;
|
|
665
|
+
* case "developer-trace":
|
|
666
|
+
* console.log("Trace summary:", event.trace.summary);
|
|
667
|
+
* break;
|
|
591
668
|
* case "done":
|
|
592
669
|
* console.log("\nCost:", event.result.cost.totalCostUsd);
|
|
593
670
|
* break;
|
|
@@ -677,4 +754,4 @@ declare class ContextClient {
|
|
|
677
754
|
_fetchRaw(endpoint: string, options?: RequestInit): Promise<Response>;
|
|
678
755
|
}
|
|
679
756
|
|
|
680
|
-
export { ContextClient, type ContextClientOptions, ContextError, type ContextErrorCode, Discovery, type ExecuteApiErrorResponse, type ExecuteApiResponse, type ExecuteApiSuccessResponse, type ExecuteOptions, type ExecuteSessionApiResponse, type ExecuteSessionApiSuccessResponse, type ExecuteSessionResult, type ExecuteSessionSpend, type ExecuteSessionStartOptions, type ExecuteSessionStatus, type ExecutionResult, type McpTool, type McpToolMeta, type McpToolRateLimitHints, Query, type QueryApiResponse, type QueryApiSuccessResponse, type QueryCost, type QueryOptions, type QueryResult, type QueryStreamDoneEvent, type QueryStreamEvent, type QueryStreamTextDeltaEvent, type QueryStreamToolStatusEvent, type QueryToolUsage, type SearchOptions, type SearchResponse, type Tool, Tools };
|
|
757
|
+
export { ContextClient, type ContextClientOptions, ContextError, type ContextErrorCode, Discovery, type ExecuteApiErrorResponse, type ExecuteApiResponse, type ExecuteApiSuccessResponse, type ExecuteOptions, type ExecuteSessionApiResponse, type ExecuteSessionApiSuccessResponse, type ExecuteSessionResult, type ExecuteSessionSpend, type ExecuteSessionStartOptions, type ExecuteSessionStatus, type ExecutionResult, type McpTool, type McpToolMeta, type McpToolRateLimitHints, Query, type QueryApiResponse, type QueryApiSuccessResponse, type QueryCost, type QueryDeveloperTrace, type QueryDeveloperTraceLoopInfo, type QueryDeveloperTraceStep, type QueryDeveloperTraceSummary, type QueryDeveloperTraceToolRef, type QueryOptions, type QueryResult, type QueryStreamDeveloperTraceEvent, type QueryStreamDoneEvent, type QueryStreamEvent, type QueryStreamTextDeltaEvent, type QueryStreamToolStatusEvent, type QueryToolUsage, type SearchOptions, type SearchResponse, type Tool, Tools };
|
package/dist/client/index.d.ts
CHANGED
|
@@ -331,6 +331,12 @@ interface QueryOptions {
|
|
|
331
331
|
* Useful for large payload workflows where inline JSON is not ideal.
|
|
332
332
|
*/
|
|
333
333
|
includeDataUrl?: boolean;
|
|
334
|
+
/**
|
|
335
|
+
* Include machine-readable developer trace output for this query response.
|
|
336
|
+
* When enabled, the server may return timeline data describing retries,
|
|
337
|
+
* fallbacks, loop checks, and intermediate recovery behavior.
|
|
338
|
+
*/
|
|
339
|
+
includeDeveloperTrace?: boolean;
|
|
334
340
|
/**
|
|
335
341
|
* Query orchestration depth mode:
|
|
336
342
|
* - `fast`: lower-latency path
|
|
@@ -344,6 +350,61 @@ interface QueryOptions {
|
|
|
344
350
|
*/
|
|
345
351
|
idempotencyKey?: string;
|
|
346
352
|
}
|
|
353
|
+
/**
|
|
354
|
+
* Tool reference attached to developer trace timeline steps.
|
|
355
|
+
*/
|
|
356
|
+
interface QueryDeveloperTraceToolRef {
|
|
357
|
+
id?: string;
|
|
358
|
+
name?: string;
|
|
359
|
+
method?: string;
|
|
360
|
+
[key: string]: unknown;
|
|
361
|
+
}
|
|
362
|
+
/**
|
|
363
|
+
* Loop metadata attached to developer trace timeline steps.
|
|
364
|
+
*/
|
|
365
|
+
interface QueryDeveloperTraceLoopInfo {
|
|
366
|
+
name?: string;
|
|
367
|
+
iteration?: number;
|
|
368
|
+
maxIterations?: number;
|
|
369
|
+
[key: string]: unknown;
|
|
370
|
+
}
|
|
371
|
+
/**
|
|
372
|
+
* A single developer-trace timeline step.
|
|
373
|
+
*/
|
|
374
|
+
interface QueryDeveloperTraceStep {
|
|
375
|
+
stepType?: string;
|
|
376
|
+
event?: string;
|
|
377
|
+
status?: string;
|
|
378
|
+
message?: string;
|
|
379
|
+
timestampMs?: number;
|
|
380
|
+
tool?: QueryDeveloperTraceToolRef;
|
|
381
|
+
attempt?: number;
|
|
382
|
+
loop?: QueryDeveloperTraceLoopInfo;
|
|
383
|
+
metadata?: Record<string, unknown>;
|
|
384
|
+
[key: string]: unknown;
|
|
385
|
+
}
|
|
386
|
+
/**
|
|
387
|
+
* Aggregate counters that summarize developer-trace behavior.
|
|
388
|
+
*/
|
|
389
|
+
interface QueryDeveloperTraceSummary {
|
|
390
|
+
toolCalls?: number;
|
|
391
|
+
retryCount?: number;
|
|
392
|
+
selfHealCount?: number;
|
|
393
|
+
fallbackCount?: number;
|
|
394
|
+
failureCount?: number;
|
|
395
|
+
recoveryCount?: number;
|
|
396
|
+
completionChecks?: number;
|
|
397
|
+
loopCount?: number;
|
|
398
|
+
[key: string]: unknown;
|
|
399
|
+
}
|
|
400
|
+
/**
|
|
401
|
+
* Developer Mode trace payload returned per query response (opt-in).
|
|
402
|
+
*/
|
|
403
|
+
interface QueryDeveloperTrace {
|
|
404
|
+
summary?: QueryDeveloperTraceSummary;
|
|
405
|
+
timeline?: QueryDeveloperTraceStep[];
|
|
406
|
+
[key: string]: unknown;
|
|
407
|
+
}
|
|
347
408
|
/**
|
|
348
409
|
* Information about a tool that was used during a query response
|
|
349
410
|
*/
|
|
@@ -383,6 +444,8 @@ interface QueryResult {
|
|
|
383
444
|
data?: unknown;
|
|
384
445
|
/** Optional blob URL for persisted execution data (when includeDataUrl=true) */
|
|
385
446
|
dataUrl?: string;
|
|
447
|
+
/** Optional machine-readable Developer Mode trace payload */
|
|
448
|
+
developerTrace?: QueryDeveloperTrace;
|
|
386
449
|
}
|
|
387
450
|
/**
|
|
388
451
|
* Successful response from the /api/v1/query endpoint
|
|
@@ -395,6 +458,7 @@ interface QueryApiSuccessResponse {
|
|
|
395
458
|
durationMs: number;
|
|
396
459
|
data?: unknown;
|
|
397
460
|
dataUrl?: string;
|
|
461
|
+
developerTrace?: QueryDeveloperTrace;
|
|
398
462
|
}
|
|
399
463
|
/**
|
|
400
464
|
* Raw API response from the query endpoint
|
|
@@ -414,6 +478,11 @@ interface QueryStreamTextDeltaEvent {
|
|
|
414
478
|
type: "text-delta";
|
|
415
479
|
delta: string;
|
|
416
480
|
}
|
|
481
|
+
/** Emitted when the server streams developer trace updates/chunks */
|
|
482
|
+
interface QueryStreamDeveloperTraceEvent {
|
|
483
|
+
type: "developer-trace";
|
|
484
|
+
trace: QueryDeveloperTrace;
|
|
485
|
+
}
|
|
417
486
|
/** Emitted when the full response is complete */
|
|
418
487
|
interface QueryStreamDoneEvent {
|
|
419
488
|
type: "done";
|
|
@@ -422,7 +491,7 @@ interface QueryStreamDoneEvent {
|
|
|
422
491
|
/**
|
|
423
492
|
* Union of all events emitted during a streaming query
|
|
424
493
|
*/
|
|
425
|
-
type QueryStreamEvent = QueryStreamToolStatusEvent | QueryStreamTextDeltaEvent | QueryStreamDoneEvent;
|
|
494
|
+
type QueryStreamEvent = QueryStreamToolStatusEvent | QueryStreamTextDeltaEvent | QueryStreamDeveloperTraceEvent | QueryStreamDoneEvent;
|
|
426
495
|
/**
|
|
427
496
|
* Specific error codes returned by the Context Protocol API
|
|
428
497
|
*/
|
|
@@ -534,6 +603,10 @@ declare class Tools {
|
|
|
534
603
|
declare class Query {
|
|
535
604
|
private client;
|
|
536
605
|
constructor(client: ContextClient);
|
|
606
|
+
private buildSyntheticTraceFromRunResult;
|
|
607
|
+
private buildSyntheticTraceFromStreamStatus;
|
|
608
|
+
private mergeDeveloperTrace;
|
|
609
|
+
private parseStreamEvent;
|
|
537
610
|
/**
|
|
538
611
|
* Run an agentic query and wait for the full response.
|
|
539
612
|
*
|
|
@@ -573,6 +646,7 @@ declare class Query {
|
|
|
573
646
|
* Event types:
|
|
574
647
|
* - `tool-status` — A tool started executing or changed status
|
|
575
648
|
* - `text-delta` — A chunk of the AI response text
|
|
649
|
+
* - `developer-trace` — Runtime trace metadata (when includeDeveloperTrace=true)
|
|
576
650
|
* - `done` — The full response is complete (includes final `QueryResult`)
|
|
577
651
|
*
|
|
578
652
|
* @param options - Query options or a plain string question
|
|
@@ -588,6 +662,9 @@ declare class Query {
|
|
|
588
662
|
* case "text-delta":
|
|
589
663
|
* process.stdout.write(event.delta);
|
|
590
664
|
* break;
|
|
665
|
+
* case "developer-trace":
|
|
666
|
+
* console.log("Trace summary:", event.trace.summary);
|
|
667
|
+
* break;
|
|
591
668
|
* case "done":
|
|
592
669
|
* console.log("\nCost:", event.result.cost.totalCostUsd);
|
|
593
670
|
* break;
|
|
@@ -677,4 +754,4 @@ declare class ContextClient {
|
|
|
677
754
|
_fetchRaw(endpoint: string, options?: RequestInit): Promise<Response>;
|
|
678
755
|
}
|
|
679
756
|
|
|
680
|
-
export { ContextClient, type ContextClientOptions, ContextError, type ContextErrorCode, Discovery, type ExecuteApiErrorResponse, type ExecuteApiResponse, type ExecuteApiSuccessResponse, type ExecuteOptions, type ExecuteSessionApiResponse, type ExecuteSessionApiSuccessResponse, type ExecuteSessionResult, type ExecuteSessionSpend, type ExecuteSessionStartOptions, type ExecuteSessionStatus, type ExecutionResult, type McpTool, type McpToolMeta, type McpToolRateLimitHints, Query, type QueryApiResponse, type QueryApiSuccessResponse, type QueryCost, type QueryOptions, type QueryResult, type QueryStreamDoneEvent, type QueryStreamEvent, type QueryStreamTextDeltaEvent, type QueryStreamToolStatusEvent, type QueryToolUsage, type SearchOptions, type SearchResponse, type Tool, Tools };
|
|
757
|
+
export { ContextClient, type ContextClientOptions, ContextError, type ContextErrorCode, Discovery, type ExecuteApiErrorResponse, type ExecuteApiResponse, type ExecuteApiSuccessResponse, type ExecuteOptions, type ExecuteSessionApiResponse, type ExecuteSessionApiSuccessResponse, type ExecuteSessionResult, type ExecuteSessionSpend, type ExecuteSessionStartOptions, type ExecuteSessionStatus, type ExecutionResult, type McpTool, type McpToolMeta, type McpToolRateLimitHints, Query, type QueryApiResponse, type QueryApiSuccessResponse, type QueryCost, type QueryDeveloperTrace, type QueryDeveloperTraceLoopInfo, type QueryDeveloperTraceStep, type QueryDeveloperTraceSummary, type QueryDeveloperTraceToolRef, type QueryOptions, type QueryResult, type QueryStreamDeveloperTraceEvent, type QueryStreamDoneEvent, type QueryStreamEvent, type QueryStreamTextDeltaEvent, type QueryStreamToolStatusEvent, type QueryToolUsage, type SearchOptions, type SearchResponse, type Tool, Tools };
|
package/dist/client/index.js
CHANGED
|
@@ -226,6 +226,114 @@ var Query = class {
|
|
|
226
226
|
constructor(client) {
|
|
227
227
|
this.client = client;
|
|
228
228
|
}
|
|
229
|
+
buildSyntheticTraceFromRunResult(params) {
|
|
230
|
+
const timeline = params.toolsUsed.map((tool, index) => ({
|
|
231
|
+
stepType: "tool-call",
|
|
232
|
+
event: "tool-call",
|
|
233
|
+
status: "success",
|
|
234
|
+
timestampMs: index,
|
|
235
|
+
tool: {
|
|
236
|
+
id: tool.id,
|
|
237
|
+
name: tool.name
|
|
238
|
+
},
|
|
239
|
+
metadata: {
|
|
240
|
+
skillCalls: tool.skillCalls,
|
|
241
|
+
synthetic: true
|
|
242
|
+
}
|
|
243
|
+
}));
|
|
244
|
+
const toolCalls = params.toolsUsed.reduce(
|
|
245
|
+
(sum, tool) => sum + Math.max(tool.skillCalls, 0),
|
|
246
|
+
0
|
|
247
|
+
);
|
|
248
|
+
return {
|
|
249
|
+
summary: {
|
|
250
|
+
toolCalls,
|
|
251
|
+
retryCount: 0,
|
|
252
|
+
selfHealCount: 0,
|
|
253
|
+
fallbackCount: 0,
|
|
254
|
+
failureCount: 0,
|
|
255
|
+
recoveryCount: 0,
|
|
256
|
+
completionChecks: 0,
|
|
257
|
+
loopCount: 0
|
|
258
|
+
},
|
|
259
|
+
timeline,
|
|
260
|
+
source: "sdk-fallback",
|
|
261
|
+
synthetic: true,
|
|
262
|
+
reason: "backend_trace_missing",
|
|
263
|
+
durationMs: params.durationMs
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
buildSyntheticTraceFromStreamStatus(params) {
|
|
267
|
+
const timeline = params.statusTimeline.map((entry, index) => ({
|
|
268
|
+
stepType: "tool-status",
|
|
269
|
+
event: "tool-status",
|
|
270
|
+
status: entry.status,
|
|
271
|
+
timestampMs: index,
|
|
272
|
+
tool: entry.tool.name || entry.tool.id ? {
|
|
273
|
+
id: entry.tool.id || void 0,
|
|
274
|
+
name: entry.tool.name || void 0
|
|
275
|
+
} : void 0,
|
|
276
|
+
metadata: { synthetic: true }
|
|
277
|
+
}));
|
|
278
|
+
const toolCallsFromUsage = params.toolsUsed.reduce(
|
|
279
|
+
(sum, tool) => sum + Math.max(tool.skillCalls, 0),
|
|
280
|
+
0
|
|
281
|
+
);
|
|
282
|
+
const toolCallsFromStatus = params.statusTimeline.filter(
|
|
283
|
+
(entry) => entry.status === "tool-complete"
|
|
284
|
+
).length;
|
|
285
|
+
const toolCalls = toolCallsFromUsage > 0 ? toolCallsFromUsage : toolCallsFromStatus;
|
|
286
|
+
const retryCount = params.statusTimeline.filter(
|
|
287
|
+
(entry) => /(retry|fix|reflect|recover)/i.test(entry.status)
|
|
288
|
+
).length;
|
|
289
|
+
const completionChecks = params.statusTimeline.filter(
|
|
290
|
+
(entry) => /complet/i.test(entry.status)
|
|
291
|
+
).length;
|
|
292
|
+
return {
|
|
293
|
+
summary: {
|
|
294
|
+
toolCalls,
|
|
295
|
+
retryCount,
|
|
296
|
+
selfHealCount: retryCount,
|
|
297
|
+
fallbackCount: 0,
|
|
298
|
+
failureCount: 0,
|
|
299
|
+
recoveryCount: 0,
|
|
300
|
+
completionChecks,
|
|
301
|
+
loopCount: retryCount
|
|
302
|
+
},
|
|
303
|
+
timeline,
|
|
304
|
+
source: "sdk-fallback",
|
|
305
|
+
synthetic: true,
|
|
306
|
+
reason: "backend_trace_missing",
|
|
307
|
+
durationMs: params.durationMs
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
mergeDeveloperTrace(first, second) {
|
|
311
|
+
if (!first) return second;
|
|
312
|
+
if (!second) return first;
|
|
313
|
+
const firstTimeline = Array.isArray(first.timeline) ? first.timeline : [];
|
|
314
|
+
const secondTimeline = Array.isArray(second.timeline) ? second.timeline : [];
|
|
315
|
+
const mergedTimeline = [...firstTimeline, ...secondTimeline];
|
|
316
|
+
return {
|
|
317
|
+
...first,
|
|
318
|
+
...second,
|
|
319
|
+
summary: {
|
|
320
|
+
...typeof first.summary === "object" && first.summary ? first.summary : {},
|
|
321
|
+
...typeof second.summary === "object" && second.summary ? second.summary : {}
|
|
322
|
+
},
|
|
323
|
+
...mergedTimeline.length > 0 ? { timeline: mergedTimeline } : {}
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
parseStreamEvent(rawData) {
|
|
327
|
+
const parsed = JSON.parse(rawData);
|
|
328
|
+
if (!parsed || typeof parsed !== "object") {
|
|
329
|
+
return void 0;
|
|
330
|
+
}
|
|
331
|
+
const event = parsed;
|
|
332
|
+
if (typeof event.type !== "string") {
|
|
333
|
+
return void 0;
|
|
334
|
+
}
|
|
335
|
+
return event;
|
|
336
|
+
}
|
|
229
337
|
/**
|
|
230
338
|
* Run an agentic query and wait for the full response.
|
|
231
339
|
*
|
|
@@ -271,6 +379,7 @@ var Query = class {
|
|
|
271
379
|
modelId: opts.modelId,
|
|
272
380
|
includeData: opts.includeData,
|
|
273
381
|
includeDataUrl: opts.includeDataUrl,
|
|
382
|
+
includeDeveloperTrace: opts.includeDeveloperTrace,
|
|
274
383
|
queryDepth: opts.queryDepth,
|
|
275
384
|
stream: false
|
|
276
385
|
})
|
|
@@ -285,13 +394,18 @@ var Query = class {
|
|
|
285
394
|
);
|
|
286
395
|
}
|
|
287
396
|
if (response.success) {
|
|
397
|
+
const developerTrace = response.developerTrace ?? (opts.includeDeveloperTrace ? this.buildSyntheticTraceFromRunResult({
|
|
398
|
+
toolsUsed: response.toolsUsed,
|
|
399
|
+
durationMs: response.durationMs
|
|
400
|
+
}) : void 0);
|
|
288
401
|
return {
|
|
289
402
|
response: response.response,
|
|
290
403
|
toolsUsed: response.toolsUsed,
|
|
291
404
|
cost: response.cost,
|
|
292
405
|
durationMs: response.durationMs,
|
|
293
406
|
data: response.data,
|
|
294
|
-
dataUrl: response.dataUrl
|
|
407
|
+
dataUrl: response.dataUrl,
|
|
408
|
+
developerTrace
|
|
295
409
|
};
|
|
296
410
|
}
|
|
297
411
|
throw new ContextError("Unexpected response format from query API");
|
|
@@ -303,6 +417,7 @@ var Query = class {
|
|
|
303
417
|
* Event types:
|
|
304
418
|
* - `tool-status` — A tool started executing or changed status
|
|
305
419
|
* - `text-delta` — A chunk of the AI response text
|
|
420
|
+
* - `developer-trace` — Runtime trace metadata (when includeDeveloperTrace=true)
|
|
306
421
|
* - `done` — The full response is complete (includes final `QueryResult`)
|
|
307
422
|
*
|
|
308
423
|
* @param options - Query options or a plain string question
|
|
@@ -318,6 +433,9 @@ var Query = class {
|
|
|
318
433
|
* case "text-delta":
|
|
319
434
|
* process.stdout.write(event.delta);
|
|
320
435
|
* break;
|
|
436
|
+
* case "developer-trace":
|
|
437
|
+
* console.log("Trace summary:", event.trace.summary);
|
|
438
|
+
* break;
|
|
321
439
|
* case "done":
|
|
322
440
|
* console.log("\nCost:", event.result.cost.totalCostUsd);
|
|
323
441
|
* break;
|
|
@@ -337,6 +455,7 @@ var Query = class {
|
|
|
337
455
|
modelId: opts.modelId,
|
|
338
456
|
includeData: opts.includeData,
|
|
339
457
|
includeDataUrl: opts.includeDataUrl,
|
|
458
|
+
includeDeveloperTrace: opts.includeDeveloperTrace,
|
|
340
459
|
queryDepth: opts.queryDepth,
|
|
341
460
|
stream: true
|
|
342
461
|
})
|
|
@@ -348,6 +467,45 @@ var Query = class {
|
|
|
348
467
|
const reader = body.getReader();
|
|
349
468
|
const decoder = new TextDecoder();
|
|
350
469
|
let buffer = "";
|
|
470
|
+
let aggregatedTrace;
|
|
471
|
+
const statusTimeline = [];
|
|
472
|
+
const parseAndHydrateEvent = (rawData) => {
|
|
473
|
+
const event = this.parseStreamEvent(rawData);
|
|
474
|
+
if (!event) {
|
|
475
|
+
return void 0;
|
|
476
|
+
}
|
|
477
|
+
if (event.type === "developer-trace") {
|
|
478
|
+
aggregatedTrace = this.mergeDeveloperTrace(aggregatedTrace, event.trace);
|
|
479
|
+
return event;
|
|
480
|
+
}
|
|
481
|
+
if (event.type === "tool-status") {
|
|
482
|
+
statusTimeline.push({
|
|
483
|
+
status: event.status,
|
|
484
|
+
tool: {
|
|
485
|
+
id: event.tool.id,
|
|
486
|
+
name: event.tool.name
|
|
487
|
+
}
|
|
488
|
+
});
|
|
489
|
+
return event;
|
|
490
|
+
}
|
|
491
|
+
if (event.type === "done") {
|
|
492
|
+
let mergedTrace = this.mergeDeveloperTrace(
|
|
493
|
+
aggregatedTrace,
|
|
494
|
+
event.result.developerTrace
|
|
495
|
+
);
|
|
496
|
+
if (!mergedTrace && opts.includeDeveloperTrace) {
|
|
497
|
+
mergedTrace = this.buildSyntheticTraceFromStreamStatus({
|
|
498
|
+
statusTimeline,
|
|
499
|
+
toolsUsed: event.result.toolsUsed,
|
|
500
|
+
durationMs: event.result.durationMs
|
|
501
|
+
});
|
|
502
|
+
}
|
|
503
|
+
if (mergedTrace) {
|
|
504
|
+
event.result.developerTrace = mergedTrace;
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
return event;
|
|
508
|
+
};
|
|
351
509
|
try {
|
|
352
510
|
while (true) {
|
|
353
511
|
const { done, value } = await reader.read();
|
|
@@ -361,7 +519,10 @@ var Query = class {
|
|
|
361
519
|
const data = trimmed.slice(6);
|
|
362
520
|
if (data === "[DONE]") return;
|
|
363
521
|
try {
|
|
364
|
-
|
|
522
|
+
const event = parseAndHydrateEvent(data);
|
|
523
|
+
if (event) {
|
|
524
|
+
yield event;
|
|
525
|
+
}
|
|
365
526
|
} catch {
|
|
366
527
|
}
|
|
367
528
|
}
|
|
@@ -371,7 +532,10 @@ var Query = class {
|
|
|
371
532
|
const data = buffer.trim().slice(6);
|
|
372
533
|
if (data !== "[DONE]") {
|
|
373
534
|
try {
|
|
374
|
-
|
|
535
|
+
const event = parseAndHydrateEvent(data);
|
|
536
|
+
if (event) {
|
|
537
|
+
yield event;
|
|
538
|
+
}
|
|
375
539
|
} catch {
|
|
376
540
|
}
|
|
377
541
|
}
|