@truefoundry/ai-gateway-sdk 0.0.9 → 0.0.11

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 (33) hide show
  1. package/dist/autogen/models.d.ts +5 -0
  2. package/dist/autogen/schema.json +4 -0
  3. package/dist/constants.d.ts +8 -0
  4. package/dist/constants.js +6 -0
  5. package/dist/constants.js.map +1 -1
  6. package/dist/index.d.ts +1 -1
  7. package/dist/index.js +1 -1
  8. package/dist/index.js.map +1 -1
  9. package/dist/services/buildExecutionReport.d.ts +36 -11
  10. package/dist/services/buildExecutionReport.js +876 -2
  11. package/dist/services/buildExecutionReport.js.map +1 -1
  12. package/dist/services/execute.js +4 -2
  13. package/dist/services/execute.js.map +1 -1
  14. package/dist/services/handleProviderResponse.d.ts +3 -1
  15. package/dist/services/handleProviderResponse.js +27 -5
  16. package/dist/services/handleProviderResponse.js.map +1 -1
  17. package/dist/stream/classifyAwsStreamEventKey.d.ts +30 -0
  18. package/dist/stream/classifyAwsStreamEventKey.js +67 -0
  19. package/dist/stream/classifyAwsStreamEventKey.js.map +1 -0
  20. package/dist/stream/index.d.ts +1 -0
  21. package/dist/stream/index.js +1 -0
  22. package/dist/stream/index.js.map +1 -1
  23. package/dist/stream/pumpProviderAwsEventStream.js +13 -1
  24. package/dist/stream/pumpProviderAwsEventStream.js.map +1 -1
  25. package/dist/stream/pumpProviderSseStream.js +6 -1
  26. package/dist/stream/pumpProviderSseStream.js.map +1 -1
  27. package/dist/stream/resolveStreamEndReason.d.ts +2 -0
  28. package/dist/stream/resolveStreamEndReason.js +18 -0
  29. package/dist/stream/resolveStreamEndReason.js.map +1 -1
  30. package/dist/stream/types.d.ts +4 -1
  31. package/dist/types/contracts.d.ts +2 -0
  32. package/dist/types/executionReport.d.ts +8 -0
  33. package/package.json +1 -1
@@ -21066,6 +21066,11 @@ export interface McpServerSource {
21066
21066
  * +sort=200
21067
21067
  */
21068
21068
  enabled_tools?: string[];
21069
+ /**
21070
+ * +label=Tool Slug
21071
+ * +usage=Optional suffix used in virtual MCP tool names for this source. When unset, the gateway keeps the auto-generated suffix.
21072
+ */
21073
+ tool_slug?: string;
21069
21074
  auth_data?: MCPServerHeaderOverrideAuth;
21070
21075
  }
21071
21076
  /**
@@ -16269,6 +16269,10 @@
16269
16269
  "type": "string"
16270
16270
  }
16271
16271
  },
16272
+ "tool_slug": {
16273
+ "description": "+label=Tool Slug\n+usage=Optional suffix used in virtual MCP tool names for this source. When unset, the gateway keeps the auto-generated suffix.",
16274
+ "type": "string"
16275
+ },
16272
16276
  "auth_data": {
16273
16277
  "$ref": "#/definitions/MCPServerHeaderOverrideAuth"
16274
16278
  }
@@ -25,6 +25,12 @@ export declare const TFY_ERROR_TYPES: {
25
25
  readonly UPSTREAM_HEADERS_TIMEOUT: "upstream-headers-timeout";
26
26
  readonly UPSTREAM_BODY_TIMEOUT: "upstream-body-timeout";
27
27
  readonly UPSTREAM_TLS_ERROR: "upstream-tls-error";
28
+ /** The gateway itself rejected the request. */
29
+ readonly INPUT_GUARDRAIL: "tfy-input-guardrail";
30
+ /** An output guardrail failed after the provider had responded. */
31
+ readonly OUTPUT_GUARDRAIL: "tfy-output-guardrail";
32
+ readonly VALIDATION: "tfy-validation";
33
+ readonly CREDENTIALS: "tfy-credentials";
28
34
  readonly CLIENT_DISCONNECTED: "client-disconnected";
29
35
  readonly PROVIDER_STREAMING_TERMINATED: "provider-streaming-terminated";
30
36
  /** Provider reported a failure inside an HTTP 200 stream (e.g. a Responses `response.failed` frame). */
@@ -32,5 +38,7 @@ export declare const TFY_ERROR_TYPES: {
32
38
  readonly TTFT_TIMEOUT: "ttft-timeout";
33
39
  readonly UNKNOWN_ERROR: "unknown-error";
34
40
  };
41
+ /** Failures where the gateway itself rejected the request or its response. */
42
+ export type GatewayRejectionErrorType = typeof TFY_ERROR_TYPES.INPUT_GUARDRAIL | typeof TFY_ERROR_TYPES.OUTPUT_GUARDRAIL | typeof TFY_ERROR_TYPES.VALIDATION | typeof TFY_ERROR_TYPES.CREDENTIALS;
35
43
  /** Failures where the gateway never got an HTTP response from the provider. */
36
44
  export type GatewayFailureErrorType = typeof TFY_ERROR_TYPES.UPSTREAM_CONNECT_FAILURE | typeof TFY_ERROR_TYPES.UPSTREAM_HEADERS_TIMEOUT | typeof TFY_ERROR_TYPES.UPSTREAM_BODY_TIMEOUT | typeof TFY_ERROR_TYPES.UPSTREAM_TLS_ERROR;
package/dist/constants.js CHANGED
@@ -25,6 +25,12 @@ export const TFY_ERROR_TYPES = {
25
25
  UPSTREAM_HEADERS_TIMEOUT: 'upstream-headers-timeout',
26
26
  UPSTREAM_BODY_TIMEOUT: 'upstream-body-timeout',
27
27
  UPSTREAM_TLS_ERROR: 'upstream-tls-error',
28
+ /** The gateway itself rejected the request. */
29
+ INPUT_GUARDRAIL: 'tfy-input-guardrail',
30
+ /** An output guardrail failed after the provider had responded. */
31
+ OUTPUT_GUARDRAIL: 'tfy-output-guardrail',
32
+ VALIDATION: 'tfy-validation',
33
+ CREDENTIALS: 'tfy-credentials',
28
34
  CLIENT_DISCONNECTED: 'client-disconnected',
29
35
  PROVIDER_STREAMING_TERMINATED: 'provider-streaming-terminated',
30
36
  /** Provider reported a failure inside an HTTP 200 stream (e.g. a Responses `response.failed` frame). */
@@ -1 +1 @@
1
- {"version":3,"file":"constants.js","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,mBAAmB,GAAG,gBAAgB,CAAC;AACpD,MAAM,CAAC,MAAM,uBAAuB,GAAG,oBAAoB,CAAC;AAC5D,MAAM,CAAC,MAAM,0BAA0B,GAAG,uBAAuB,CAAC;AAClE,kHAAkH;AAClH,MAAM,CAAC,MAAM,6BAA6B,GAAG,0BAA0B,CAAC;AACxE,MAAM,CAAC,MAAM,mBAAmB,GAAG,CAAC,IAAI,EAAE,SAAS,CAAU,CAAC;AAC9D,MAAM,CAAC,MAAM,qBAAqB,GAAG,kBAAkB,CA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SAAS,CAAU,CAAC;AAC9D,MAAM,CAAC,MAAM,qBAAqB,GAAG,kBAAkB,CAAC;AACxD,MAAM,CAAC,MAAM,yBAAyB,GAAG,sBAAsB,CAAC;AAChE,MAAM,CAAC,MAAM,uBAAuB,GAAG,oBAAoB,CAAC;AAC5D,MAAM,CAAC,MAAM,wBAAwB,GAAG,qBAAqB,CAAC;AAC9D,MAAM,CAAC,MAAM,mCAAmC,GAAG,gCAAgC,CAAC;AACpF,MAAM,CAAC,MAAM,6BAA6B,GAAG,0BAA0B,CAAC;AACxE,MAAM,CAAC,MAAM,eAAe,GAAG,UAAU,CAAC;AAC1C,MAAM,CAAC,MAAM,sBAAsB,GAAG,mBAAmB,CAAC;AAE1D,MAAM,CAAC,MAAM,eAAe,GAAG;IAC7B,UAAU,EAAE,gBAAgB;IAC5B,YAAY,EAAE,kBAAkB;IAChC,aAAa,EAAE,eAAe;IAC9B,qGAAqG;IACrG,eAAe,EAAE,qBAAqB;IACtC,eAAe,EAAE,iBAAiB;IAClC,eAAe,EAAE,iBAAiB;IAClC,uFAAuF;IACvF,wBAAwB,EAAE,0BAA0B;IACpD,wBAAwB,EAAE,0BAA0B;IACpD,qBAAqB,EAAE,uBAAuB;IAC9C,kBAAkB,EAAE,oBAAoB;IACxC,+CAA+C;IAC/C,eAAe,EAAE,qBAAqB;IACtC,mEAAmE;IACnE,gBAAgB,EAAE,sBAAsB;IACxC,UAAU,EAAE,gBAAgB;IAC5B,WAAW,EAAE,iBAAiB;IAC9B,mBAAmB,EAAE,qBAAqB;IAC1C,6BAA6B,EAAE,+BAA+B;IAC9D,wGAAwG;IACxG,qBAAqB,EAAE,uBAAuB;IAC9C,YAAY,EAAE,cAAc;IAC5B,aAAa,EAAE,eAAe;CACtB,CAAC"}
package/dist/index.d.ts CHANGED
@@ -7,7 +7,7 @@ export type { ContentItem, ExtractedMessages, GuardrailHelpers, GuardrailsRunCon
7
7
  export { transformCapturedProviderResponse, type CapturedProviderResponse, type TransformCapturedProviderResponseInput, type TransformedClientResponse, } from './parity/transformCapturedProviderResponse.js';
8
8
  export { getSdkRequestTenantName, runWithSdkRequestContext } from './requestContext.js';
9
9
  export { LlmGatewaySdk } from './sdk.js';
10
- export { getStreamModeSplitPattern, pumpProviderAwsEventStream, pumpProviderSseStream, readSseFrames, shouldReadAwsEventStream, } from './stream/index.js';
10
+ export { classifyAwsStreamEventKey, getStreamModeSplitPattern, pumpProviderAwsEventStream, pumpProviderSseStream, readSseFrames, shouldReadAwsEventStream, } from './stream/index.js';
11
11
  export type { GetStreamModeSplitPatternInput, PumpProviderAwsEventStreamOptions, PumpProviderBaseOptions, PumpProviderSseOptions, PumpResult, ReadSseFramesOptions, ReadSseFramesResult, ShouldReadAwsEventStreamInput, SplitPattern, SseCommentMode, StreamTransformFn, } from './stream/index.js';
12
12
  export type { CachePort } from './types/cache.js';
13
13
  export type { BilledUsageOverlay, EndReason, Endpoint, ExecutionFinishReason, ExecutionModelMetadata, ExecutionReport, ExecutionTimings, ExecutionToolUsage, GatewayFailure, MidStreamError, MidStreamErrorKind, MidStreamProviderErrorToken, ProviderCallConfig, ProviderOptions, SdkCallInputs, SdkExecuteResult, SdkExecutionConfig, SdkInitConfig, SdkRequest, SdkRequestBody, SessionContext, TokenUsage, TokenUsageIteration, } from './types/contracts.js';
package/dist/index.js CHANGED
@@ -6,6 +6,6 @@ export { initAdapters } from './adapters/index.js';
6
6
  export { transformCapturedProviderResponse, } from './parity/transformCapturedProviderResponse.js';
7
7
  export { getSdkRequestTenantName, runWithSdkRequestContext } from './requestContext.js';
8
8
  export { LlmGatewaySdk } from './sdk.js';
9
- export { getStreamModeSplitPattern, pumpProviderAwsEventStream, pumpProviderSseStream, readSseFrames, shouldReadAwsEventStream, } from './stream/index.js';
9
+ export { classifyAwsStreamEventKey, getStreamModeSplitPattern, pumpProviderAwsEventStream, pumpProviderSseStream, readSseFrames, shouldReadAwsEventStream, } from './stream/index.js';
10
10
  export { deriveProviderHeaderOptions } from './utils/deriveProviderHeaderOptions.js';
11
11
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAU1C,OAAO,EACL,iCAAiC,GAIlC,MAAM,4CAA4C,CAAC;AACpD,OAAO,EAAE,uBAAuB,EAAE,wBAAwB,EAAE,MAAM,kBAAkB,CAAC;AACrF,OAAO,EAAE,aAAa,EAAE,MAAM,OAAO,CAAC;AACtC,OAAO,EACL,yBAAyB,EACzB,0BAA0B,EAC1B,qBAAqB,EACrB,aAAa,EACb,wBAAwB,GACzB,MAAM,UAAU,CAAC;AAkElB,OAAO,EAAE,2BAA2B,EAAE,MAAM,qCAAqC,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAU1C,OAAO,EACL,iCAAiC,GAIlC,MAAM,4CAA4C,CAAC;AACpD,OAAO,EAAE,uBAAuB,EAAE,wBAAwB,EAAE,MAAM,kBAAkB,CAAC;AACrF,OAAO,EAAE,aAAa,EAAE,MAAM,OAAO,CAAC;AACtC,OAAO,EACL,yBAAyB,EACzB,yBAAyB,EACzB,0BAA0B,EAC1B,qBAAqB,EACrB,aAAa,EACb,wBAAwB,GACzB,MAAM,UAAU,CAAC;AAkElB,OAAO,EAAE,2BAA2B,EAAE,MAAM,qCAAqC,CAAC"}
@@ -1,11 +1,6 @@
1
- /**
2
- * Settle {@link ExecutionReport} for SDK execute (P7.1).
3
- *
4
- * No USD / OTel. Prefer omitting usage over inventing zeros.
5
- */
6
1
  import type { Endpoint } from '../providers/types.js';
7
2
  import type { PumpResult } from '../stream/types.js';
8
- import type { ExecutionFinishReason, ExecutionModelMetadata, ExecutionReport, ExecutionTimings, TokenUsage, TokenUsageIteration } from '../types/executionReport.js';
3
+ import type { ExecutionFinishReason, ExecutionModelMetadata, ExecutionReport, ExecutionTimings, ExecutionToolUsage, TokenUsage, TokenUsageIteration } from '../types/executionReport.js';
9
4
  import { type BilledUsageOverlay } from '../utils/billedUsage.js';
10
5
  export declare function usageFromJson(json: unknown): TokenUsage | undefined;
11
6
  export declare function mergeTokenUsage(prev?: TokenUsage, next?: TokenUsage): TokenUsage | undefined;
@@ -14,6 +9,8 @@ export type UsageSnapshot = {
14
9
  finishReason?: ExecutionFinishReason;
15
10
  iterations?: TokenUsageIteration[];
16
11
  };
12
+ /** NEW: streaming settle payload (usage snapshot + CLONE LiveUsage pricing facts). */
13
+ export type StreamSettleExtra = UsageSnapshot & Omit<LiveUsage, 'usage' | 'finishReason'>;
17
14
  export declare function mergeUsageSnapshots(prev?: UsageSnapshot, next?: UsageSnapshot): UsageSnapshot | undefined;
18
15
  export declare function finalizeReportUsage(args: {
19
16
  usage?: TokenUsage;
@@ -39,10 +36,38 @@ export declare function createStreamingReportDeferred(args: {
39
36
  providerRequestId?: string;
40
37
  }): {
41
38
  promise: Promise<ExecutionReport>;
42
- settle: (pumpResult: PumpResult, extra?: {
43
- finishReason?: ExecutionFinishReason;
44
- usage?: TokenUsage;
45
- iterations?: TokenUsageIteration[];
46
- }) => void;
39
+ settle: (pumpResult: PumpResult, extra?: StreamSettleExtra) => void;
47
40
  settleWithError: (err: unknown) => void;
48
41
  };
42
+ /** NEW: SDK stand-in for the live `ctx` facts token loggers branch on. */
43
+ export interface LiveUsageScope {
44
+ /** Live `requestType` / served endpoint. */
45
+ endpoint: Endpoint;
46
+ /** Live `ctx.req.url` pathname substrings (messages, ranking, converse-stream, …). */
47
+ inboundPath: string;
48
+ isGeminiRouter?: boolean;
49
+ isBedrockRouter?: boolean;
50
+ /** Live `request` / `gatewayRequest` (post-adapter body). */
51
+ request: any;
52
+ }
53
+ /** NEW shape: what one live logger would pass into `logModelSpanAttributes` for pricing. */
54
+ export interface LiveUsage {
55
+ usage: TokenUsage;
56
+ /** CLONE: live `TOKENS_ESTIMATED` when estimateTokenCountFromText runs. */
57
+ tokensEstimated?: boolean;
58
+ finishReason?: ExecutionFinishReason;
59
+ serviceTier?: string;
60
+ toolUsage?: ExecutionToolUsage;
61
+ /** CLONE: live `providerReportedCostInUsd(usage)` (e.g. Perplexity total_cost). */
62
+ providerReportedCostInUsd?: number;
63
+ }
64
+ /**
65
+ * NEW router over CLONE parsers: non-stream counts live `handleNonStreamingMode` would price.
66
+ * `undefined` = no live token logger (caller keeps usageFromJson).
67
+ */
68
+ export declare function liveUsageFromResponse(scope: LiveUsageScope, json: unknown, headers: Headers): LiveUsage | undefined;
69
+ /**
70
+ * NEW router over CLONE parsers: stream counts live `logStreamingResponse` would price from frames.
71
+ * `undefined` = no live stream logger for this endpoint.
72
+ */
73
+ export declare function liveUsageFromStreamFrames(scope: LiveUsageScope, frames: string[], streamState: Record<string, unknown>, cancelledMidwayByClient: boolean): LiveUsage | undefined;