@apifuse/provider-sdk 2.2.0-beta.53 → 2.2.0-beta.55

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 (43) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/dist/cli/migrate-operation-declaration.d.ts +5 -1
  3. package/dist/cli/migrate-operation-declaration.js +645 -46
  4. package/dist/runtime/auth-flow.d.ts +1 -0
  5. package/dist/runtime/auth-flow.js +2 -0
  6. package/dist/runtime/trace.d.ts +7 -0
  7. package/dist/runtime/trace.js +24 -1
  8. package/dist/server/serve-implementation.d.ts +4 -0
  9. package/dist/server/serve-implementation.js +576 -321
  10. package/dist/types.d.ts +1 -0
  11. package/package.json +1 -1
  12. package/src/cli/__tests__/fixtures/migrate-operation-declaration/discovery-ambiguous-a.ts.txt +3 -0
  13. package/src/cli/__tests__/fixtures/migrate-operation-declaration/discovery-ambiguous-b.ts.txt +3 -0
  14. package/src/cli/__tests__/fixtures/migrate-operation-declaration/discovery-ambiguous-barrel.ts.txt +2 -0
  15. package/src/cli/__tests__/fixtures/migrate-operation-declaration/discovery-ambiguous-index.ts.txt +3 -0
  16. package/src/cli/__tests__/fixtures/migrate-operation-declaration/discovery-computed.ts.txt +6 -0
  17. package/src/cli/__tests__/fixtures/migrate-operation-declaration/discovery-cycle-a.ts.txt +1 -0
  18. package/src/cli/__tests__/fixtures/migrate-operation-declaration/discovery-cycle-b.ts.txt +1 -0
  19. package/src/cli/__tests__/fixtures/migrate-operation-declaration/discovery-cycle-index.ts.txt +3 -0
  20. package/src/cli/__tests__/fixtures/migrate-operation-declaration/discovery-daiso-barrel.ts.txt +7 -0
  21. package/src/cli/__tests__/fixtures/migrate-operation-declaration/discovery-daiso-catalog.ts.txt +19 -0
  22. package/src/cli/__tests__/fixtures/migrate-operation-declaration/discovery-daiso-docs.ts.txt +6 -0
  23. package/src/cli/__tests__/fixtures/migrate-operation-declaration/discovery-daiso-index.ts.txt +3 -0
  24. package/src/cli/__tests__/fixtures/migrate-operation-declaration/discovery-daiso-stores.ts.txt +8 -0
  25. package/src/cli/__tests__/fixtures/migrate-operation-declaration/discovery-indirect-config.ts.txt +5 -0
  26. package/src/cli/__tests__/fixtures/migrate-operation-declaration/discovery-nonstatic.ts.txt +4 -0
  27. package/src/cli/__tests__/fixtures/migrate-operation-declaration/discovery-package-index.ts.txt +3 -0
  28. package/src/cli/__tests__/fixtures/migrate-operation-declaration/discovery-reexport-index.ts.txt +3 -0
  29. package/src/cli/__tests__/fixtures/migrate-operation-declaration/discovery-reexport-leaf.ts.txt +10 -0
  30. package/src/cli/__tests__/fixtures/migrate-operation-declaration/discovery-reexport-one.ts.txt +1 -0
  31. package/src/cli/__tests__/fixtures/migrate-operation-declaration/discovery-reexport-two.ts.txt +1 -0
  32. package/src/cli/__tests__/fixtures/migrate-operation-declaration/discovery-star-package-barrel.ts.txt +2 -0
  33. package/src/cli/__tests__/fixtures/migrate-operation-declaration/discovery-star-package-index.ts.txt +3 -0
  34. package/src/cli/__tests__/fixtures/migrate-operation-declaration/discovery-star-package-leaf.ts.txt +3 -0
  35. package/src/cli/__tests__/fixtures/migrate-operation-declaration/factory-duplicate-provider.ts.txt +11 -0
  36. package/src/cli/__tests__/fixtures/migrate-operation-declaration/factory-id-unresolved.ts.txt +7 -0
  37. package/src/cli/__tests__/fixtures/migrate-operation-declaration/factory-one-to-many.ts.txt +18 -0
  38. package/src/cli/__tests__/fixtures/migrate-operation-declaration/factory-one-to-one.ts.txt +13 -0
  39. package/src/cli/migrate-operation-declaration.ts +844 -49
  40. package/src/runtime/auth-flow.ts +3 -0
  41. package/src/runtime/trace.ts +37 -1
  42. package/src/server/serve-implementation.ts +759 -586
  43. package/src/types.ts +1 -0
@@ -15,4 +15,5 @@ export declare function createFlowContext(options: {
15
15
  initialContext?: Record<string, unknown>;
16
16
  ocr?: OcrContext;
17
17
  stt?: SttContext;
18
+ trace?: FlowContext["trace"];
18
19
  }): FlowContext;
@@ -3,6 +3,7 @@ import { createAuthFlowHelpers } from "../auth.js";
3
3
  import { createUnsupportedOcrClient } from "./ocr.js";
4
4
  import { createUnsupportedResolverClient } from "./resolver-shared.js";
5
5
  import { createUnsupportedSttClient } from "./stt.js";
6
+ import { createTraceContext } from "./trace.js";
6
7
  function normalizeAllowedKeys(allowedKeys) {
7
8
  return new Set(allowedKeys.filter((key) => key.trim().length > 0));
8
9
  }
@@ -39,6 +40,7 @@ export function createFlowContext(options) {
39
40
  externalRef: options.externalRef,
40
41
  tenantId: options.tenantId,
41
42
  providerId: options.providerId,
43
+ trace: options.trace ?? createTraceContext(),
42
44
  http: options.http,
43
45
  state: options.state,
44
46
  stealth: options.stealth,
@@ -10,6 +10,8 @@ export interface CreateTraceContextOptions {
10
10
  onSpan?: (span: Span) => void;
11
11
  exportOptions?: OTLPExportOptions;
12
12
  resourceAttributes?: Record<string, string>;
13
+ /** W3C-compatible 32-character lowercase hexadecimal trace id used for export. */
14
+ traceId?: string;
13
15
  /**
14
16
  * Applied to a detached copy of each span immediately before OTLP export; never touches
15
17
  * getSpans() or onSpan. Returning nothing (or throwing) drops that export batch.
@@ -25,6 +27,11 @@ export interface TraceRecorder {
25
27
  runSpan<T>(name: string, fn: () => Promise<T> | T, options?: SpanHookOptions<T>): Promise<T>;
26
28
  }
27
29
  export declare const TRACE_RECORDER: unique symbol;
30
+ /** Updates request metadata before its pending root span completes and exports. */
31
+ export declare function updateTraceContextExportMetadata(trace: BaseTraceContext, input: {
32
+ traceId?: string;
33
+ resourceAttributes?: Record<string, string>;
34
+ }): void;
28
35
  export declare function resolveTraceContextOptions(config?: TraceConfig): CreateTraceContextOptions;
29
36
  export declare function getTraceRecorder(trace: BaseTraceContext): TraceRecorder | null;
30
37
  export declare function createTraceContext(options?: CreateTraceContextOptions): TraceContext;
@@ -1,6 +1,16 @@
1
1
  import { AsyncLocalStorage } from "node:async_hooks";
2
2
  import { exportSpansOTLP } from "./otlp.js";
3
3
  export const TRACE_RECORDER = Symbol.for("@apifuse/provider-sdk/runtime/trace-recorder");
4
+ const TRACE_EXPORT_METADATA = Symbol.for("@apifuse/provider-sdk/runtime/trace-export-metadata");
5
+ function assertValidTraceId(traceId) {
6
+ if (!/^[0-9a-f]{32}$/.test(traceId) || /^0{32}$/.test(traceId)) {
7
+ throw new TypeError("traceId must be 32 lowercase hexadecimal characters and non-zero");
8
+ }
9
+ }
10
+ /** Updates request metadata before its pending root span completes and exports. */
11
+ export function updateTraceContextExportMetadata(trace, input) {
12
+ trace[TRACE_EXPORT_METADATA]?.update(input);
13
+ }
4
14
  function buildOTLPExportOptions(config) {
5
15
  if (config?.exporter !== "otlp") {
6
16
  return undefined;
@@ -67,6 +77,8 @@ export function getTraceRecorder(trace) {
67
77
  return trace[TRACE_RECORDER] ?? null;
68
78
  }
69
79
  export function createTraceContext(options = {}) {
80
+ if (options.traceId !== undefined)
81
+ assertValidTraceId(options.traceId);
70
82
  const maxSpans = options.maxSpans ?? 1000;
71
83
  const completed = [];
72
84
  const activeSpanStorage = new AsyncLocalStorage();
@@ -79,7 +91,7 @@ export function createTraceContext(options = {}) {
79
91
  : undefined;
80
92
  // One trace id per context so every export batch of this request shares it and
81
93
  // two processes can never mint the same id.
82
- const exportTraceId = crypto.randomUUID().replace(/-/g, "");
94
+ let exportTraceId = options.traceId ?? crypto.randomUUID().replace(/-/g, "");
83
95
  let exportScheduled = false;
84
96
  // One pending batch per context: roots completing before the flush share it, and a span is
85
97
  // handed to the exporter exactly once, so later roots never re-send earlier spans.
@@ -168,6 +180,17 @@ export function createTraceContext(options = {}) {
168
180
  return completed.map((entry) => ({ ...entry.span }));
169
181
  },
170
182
  [TRACE_RECORDER]: recorder,
183
+ [TRACE_EXPORT_METADATA]: {
184
+ update(input) {
185
+ if (input.traceId !== undefined) {
186
+ assertValidTraceId(input.traceId);
187
+ exportTraceId = input.traceId;
188
+ }
189
+ if (input.resourceAttributes !== undefined && exportResourceAttributes) {
190
+ Object.assign(exportResourceAttributes, input.resourceAttributes);
191
+ }
192
+ },
193
+ },
171
194
  };
172
195
  return traceContext;
173
196
  }
@@ -79,6 +79,10 @@ type ProviderServerLogEventBase = ProviderRequestCost & {
79
79
  kind: "operation" | "auth";
80
80
  route: string;
81
81
  requestId?: string;
82
+ connectionId?: string;
83
+ flowId?: string;
84
+ tenantId?: string;
85
+ requestedProviderId?: string;
82
86
  status: number;
83
87
  proxy?: ProxyTelemetryLogPayload;
84
88
  };