@jsonstudio/llms 0.4.2
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 +265 -0
- package/dist/api/index.d.ts +10 -0
- package/dist/api/index.js +11 -0
- package/dist/api/llmswitch-config.d.ts +15 -0
- package/dist/api/llmswitch-config.js +415 -0
- package/dist/api/llmswitch-types.d.ts +412 -0
- package/dist/api/llmswitch-types.js +60 -0
- package/dist/bridge/routecodex-adapter.d.ts +74 -0
- package/dist/bridge/routecodex-adapter.js +226 -0
- package/dist/config/index.d.ts +3 -0
- package/dist/config/index.js +5 -0
- package/dist/config-unified/enhanced-path-resolver.d.ts +5 -0
- package/dist/config-unified/enhanced-path-resolver.js +38 -0
- package/dist/config-unified/unified-config.d.ts +26 -0
- package/dist/config-unified/unified-config.js +187 -0
- package/dist/conversion/codec-registry.d.ts +10 -0
- package/dist/conversion/codec-registry.js +25 -0
- package/dist/conversion/codecs/anthropic-openai-codec.d.ts +23 -0
- package/dist/conversion/codecs/anthropic-openai-codec.js +546 -0
- package/dist/conversion/codecs/gemini-openai-codec.d.ts +16 -0
- package/dist/conversion/codecs/gemini-openai-codec.js +437 -0
- package/dist/conversion/codecs/openai-openai-codec.d.ts +12 -0
- package/dist/conversion/codecs/openai-openai-codec.js +125 -0
- package/dist/conversion/codecs/responses-openai-codec.d.ts +12 -0
- package/dist/conversion/codecs/responses-openai-codec.js +154 -0
- package/dist/conversion/config/config-manager.d.ts +212 -0
- package/dist/conversion/config/config-manager.js +310 -0
- package/dist/conversion/conversion-v3/adapters/index.d.ts +2 -0
- package/dist/conversion/conversion-v3/adapters/index.js +1 -0
- package/dist/conversion/conversion-v3/adapters/v3-to-standard-adapter.d.ts +38 -0
- package/dist/conversion/conversion-v3/adapters/v3-to-standard-adapter.js +55 -0
- package/dist/conversion/conversion-v3/compatibility/compatibility-profiles.d.ts +22 -0
- package/dist/conversion/conversion-v3/compatibility/compatibility-profiles.js +84 -0
- package/dist/conversion/conversion-v3/compatibility/compatibility-stages.d.ts +4 -0
- package/dist/conversion/conversion-v3/compatibility/compatibility-stages.js +285 -0
- package/dist/conversion/conversion-v3/compatibility/index.d.ts +3 -0
- package/dist/conversion/conversion-v3/compatibility/index.js +2 -0
- package/dist/conversion/conversion-v3/config/default-configs.d.ts +48 -0
- package/dist/conversion/conversion-v3/config/default-configs.js +1083 -0
- package/dist/conversion/conversion-v3/config/index.d.ts +5 -0
- package/dist/conversion/conversion-v3/config/index.js +5 -0
- package/dist/conversion/conversion-v3/config/pipeline-config-manager.d.ts +49 -0
- package/dist/conversion/conversion-v3/config/pipeline-config-manager.js +65 -0
- package/dist/conversion/conversion-v3/index.d.ts +11 -0
- package/dist/conversion/conversion-v3/index.js +17 -0
- package/dist/conversion/conversion-v3/nodes/index.d.ts +56 -0
- package/dist/conversion/conversion-v3/nodes/index.js +149 -0
- package/dist/conversion/conversion-v3/nodes/input/anthropic-input-node.d.ts +25 -0
- package/dist/conversion/conversion-v3/nodes/input/anthropic-input-node.js +56 -0
- package/dist/conversion/conversion-v3/nodes/input/base-input-node.d.ts +75 -0
- package/dist/conversion/conversion-v3/nodes/input/base-input-node.js +237 -0
- package/dist/conversion/conversion-v3/nodes/input/chat-input-node.d.ts +25 -0
- package/dist/conversion/conversion-v3/nodes/input/chat-input-node.js +56 -0
- package/dist/conversion/conversion-v3/nodes/input/chat-shape-helpers.d.ts +4 -0
- package/dist/conversion/conversion-v3/nodes/input/chat-shape-helpers.js +197 -0
- package/dist/conversion/conversion-v3/nodes/input/gemini-input-node.d.ts +16 -0
- package/dist/conversion/conversion-v3/nodes/input/gemini-input-node.js +125 -0
- package/dist/conversion/conversion-v3/nodes/input/index.d.ts +8 -0
- package/dist/conversion/conversion-v3/nodes/input/index.js +8 -0
- package/dist/conversion/conversion-v3/nodes/input/input-decision.d.ts +46 -0
- package/dist/conversion/conversion-v3/nodes/input/input-decision.js +125 -0
- package/dist/conversion/conversion-v3/nodes/input/responses-input-node.d.ts +20 -0
- package/dist/conversion/conversion-v3/nodes/input/responses-input-node.js +51 -0
- package/dist/conversion/conversion-v3/nodes/output/anthropic-output-node.d.ts +71 -0
- package/dist/conversion/conversion-v3/nodes/output/anthropic-output-node.js +320 -0
- package/dist/conversion/conversion-v3/nodes/output/base-output-node.d.ts +116 -0
- package/dist/conversion/conversion-v3/nodes/output/base-output-node.js +512 -0
- package/dist/conversion/conversion-v3/nodes/output/index.d.ts +7 -0
- package/dist/conversion/conversion-v3/nodes/output/index.js +7 -0
- package/dist/conversion/conversion-v3/nodes/output/openai-output-node.d.ts +71 -0
- package/dist/conversion/conversion-v3/nodes/output/openai-output-node.js +293 -0
- package/dist/conversion/conversion-v3/nodes/output/output-decision.d.ts +68 -0
- package/dist/conversion/conversion-v3/nodes/output/output-decision.js +161 -0
- package/dist/conversion/conversion-v3/nodes/output/responses-output-node.d.ts +64 -0
- package/dist/conversion/conversion-v3/nodes/output/responses-output-node.js +323 -0
- package/dist/conversion/conversion-v3/nodes/process/base-process-node.d.ts +72 -0
- package/dist/conversion/conversion-v3/nodes/process/base-process-node.js +246 -0
- package/dist/conversion/conversion-v3/nodes/process/chat-process-node.d.ts +135 -0
- package/dist/conversion/conversion-v3/nodes/process/chat-process-node.js +854 -0
- package/dist/conversion/conversion-v3/nodes/process/compatibility-process-node.d.ts +29 -0
- package/dist/conversion/conversion-v3/nodes/process/compatibility-process-node.js +181 -0
- package/dist/conversion/conversion-v3/nodes/process/index.d.ts +10 -0
- package/dist/conversion/conversion-v3/nodes/process/index.js +10 -0
- package/dist/conversion/conversion-v3/nodes/process/passthrough-process-node.d.ts +59 -0
- package/dist/conversion/conversion-v3/nodes/process/passthrough-process-node.js +243 -0
- package/dist/conversion/conversion-v3/nodes/process/response-process-node.d.ts +17 -0
- package/dist/conversion/conversion-v3/nodes/process/response-process-node.js +213 -0
- package/dist/conversion/conversion-v3/nodes/process/tool-process-node.d.ts +26 -0
- package/dist/conversion/conversion-v3/nodes/process/tool-process-node.js +299 -0
- package/dist/conversion/conversion-v3/nodes/process/virtual-router-process-node.d.ts +40 -0
- package/dist/conversion/conversion-v3/nodes/process/virtual-router-process-node.js +430 -0
- package/dist/conversion/conversion-v3/nodes/response/anthropic-response-input-node.d.ts +12 -0
- package/dist/conversion/conversion-v3/nodes/response/anthropic-response-input-node.js +129 -0
- package/dist/conversion/conversion-v3/nodes/response/chat-to-responses-response-node.d.ts +15 -0
- package/dist/conversion/conversion-v3/nodes/response/chat-to-responses-response-node.js +57 -0
- package/dist/conversion/conversion-v3/nodes/response/index.d.ts +4 -0
- package/dist/conversion/conversion-v3/nodes/response/index.js +4 -0
- package/dist/conversion/conversion-v3/nodes/response/openai-response-input-node.d.ts +15 -0
- package/dist/conversion/conversion-v3/nodes/response/openai-response-input-node.js +87 -0
- package/dist/conversion/conversion-v3/nodes/response/response-utils.d.ts +1 -0
- package/dist/conversion/conversion-v3/nodes/response/response-utils.js +1 -0
- package/dist/conversion/conversion-v3/nodes/response/responses-response-input-node.d.ts +12 -0
- package/dist/conversion/conversion-v3/nodes/response/responses-response-input-node.js +67 -0
- package/dist/conversion/conversion-v3/nodes/sse/index.d.ts +3 -0
- package/dist/conversion/conversion-v3/nodes/sse/index.js +3 -0
- package/dist/conversion/conversion-v3/nodes/sse/sse-input-node.d.ts +27 -0
- package/dist/conversion/conversion-v3/nodes/sse/sse-input-node.js +165 -0
- package/dist/conversion/conversion-v3/nodes/sse/sse-output-node.d.ts +19 -0
- package/dist/conversion/conversion-v3/nodes/sse/sse-output-node.js +267 -0
- package/dist/conversion/conversion-v3/nodes/sse/sse-passthrough-node.d.ts +18 -0
- package/dist/conversion/conversion-v3/nodes/sse/sse-passthrough-node.js +48 -0
- package/dist/conversion/conversion-v3/pipelines/index.d.ts +6 -0
- package/dist/conversion/conversion-v3/pipelines/index.js +6 -0
- package/dist/conversion/conversion-v3/pipelines/super-pipeline.d.ts +112 -0
- package/dist/conversion/conversion-v3/pipelines/super-pipeline.js +566 -0
- package/dist/conversion/conversion-v3/response/provider-response-converter.d.ts +28 -0
- package/dist/conversion/conversion-v3/response/provider-response-converter.js +249 -0
- package/dist/conversion/conversion-v3/sse/index.d.ts +140 -0
- package/dist/conversion/conversion-v3/sse/index.js +115 -0
- package/dist/conversion/conversion-v3/sse/json-to-sse/anthropic-json-to-sse-converter.d.ts +11 -0
- package/dist/conversion/conversion-v3/sse/json-to-sse/anthropic-json-to-sse-converter.js +79 -0
- package/dist/conversion/conversion-v3/sse/json-to-sse/chat-json-to-sse-converter.d.ts +79 -0
- package/dist/conversion/conversion-v3/sse/json-to-sse/chat-json-to-sse-converter.js +296 -0
- package/dist/conversion/conversion-v3/sse/json-to-sse/event-generators/chat.d.ts +55 -0
- package/dist/conversion/conversion-v3/sse/json-to-sse/event-generators/chat.js +233 -0
- package/dist/conversion/conversion-v3/sse/json-to-sse/event-generators/responses.d.ts +99 -0
- package/dist/conversion/conversion-v3/sse/json-to-sse/event-generators/responses.js +599 -0
- package/dist/conversion/conversion-v3/sse/json-to-sse/index.d.ts +6 -0
- package/dist/conversion/conversion-v3/sse/json-to-sse/index.js +7 -0
- package/dist/conversion/conversion-v3/sse/json-to-sse/responses-json-to-sse-converter.d.ts +79 -0
- package/dist/conversion/conversion-v3/sse/json-to-sse/responses-json-to-sse-converter.js +322 -0
- package/dist/conversion/conversion-v3/sse/json-to-sse/sequencers/chat-sequencer.d.ts +37 -0
- package/dist/conversion/conversion-v3/sse/json-to-sse/sequencers/chat-sequencer.js +218 -0
- package/dist/conversion/conversion-v3/sse/json-to-sse/sequencers/responses-sequencer.d.ts +40 -0
- package/dist/conversion/conversion-v3/sse/json-to-sse/sequencers/responses-sequencer.js +263 -0
- package/dist/conversion/conversion-v3/sse/registry/sse-codec-registry.d.ts +32 -0
- package/dist/conversion/conversion-v3/sse/registry/sse-codec-registry.js +106 -0
- package/dist/conversion/conversion-v3/sse/shared/chat-serializer.d.ts +4 -0
- package/dist/conversion/conversion-v3/sse/shared/chat-serializer.js +40 -0
- package/dist/conversion/conversion-v3/sse/shared/constants.d.ts +272 -0
- package/dist/conversion/conversion-v3/sse/shared/constants.js +321 -0
- package/dist/conversion/conversion-v3/sse/shared/serializers/base-serializer.d.ts +158 -0
- package/dist/conversion/conversion-v3/sse/shared/serializers/base-serializer.js +210 -0
- package/dist/conversion/conversion-v3/sse/shared/serializers/chat-event-serializer.d.ts +82 -0
- package/dist/conversion/conversion-v3/sse/shared/serializers/chat-event-serializer.js +275 -0
- package/dist/conversion/conversion-v3/sse/shared/serializers/index.d.ts +39 -0
- package/dist/conversion/conversion-v3/sse/shared/serializers/index.js +54 -0
- package/dist/conversion/conversion-v3/sse/shared/serializers/responses-event-serializer.d.ts +123 -0
- package/dist/conversion/conversion-v3/sse/shared/serializers/responses-event-serializer.js +356 -0
- package/dist/conversion/conversion-v3/sse/shared/serializers/types.d.ts +51 -0
- package/dist/conversion/conversion-v3/sse/shared/serializers/types.js +4 -0
- package/dist/conversion/conversion-v3/sse/shared/utils.d.ts +254 -0
- package/dist/conversion/conversion-v3/sse/shared/utils.js +543 -0
- package/dist/conversion/conversion-v3/sse/shared/writer.d.ts +111 -0
- package/dist/conversion/conversion-v3/sse/shared/writer.js +268 -0
- package/dist/conversion/conversion-v3/sse/sse-to-json/anthropic-sse-to-json-converter.d.ts +13 -0
- package/dist/conversion/conversion-v3/sse/sse-to-json/anthropic-sse-to-json-converter.js +139 -0
- package/dist/conversion/conversion-v3/sse/sse-to-json/builders/response-builder.d.ts +165 -0
- package/dist/conversion/conversion-v3/sse/sse-to-json/builders/response-builder.js +776 -0
- package/dist/conversion/conversion-v3/sse/sse-to-json/chat-sse-to-json-converter.d.ts +106 -0
- package/dist/conversion/conversion-v3/sse/sse-to-json/chat-sse-to-json-converter.js +540 -0
- package/dist/conversion/conversion-v3/sse/sse-to-json/index.d.ts +6 -0
- package/dist/conversion/conversion-v3/sse/sse-to-json/index.js +7 -0
- package/dist/conversion/conversion-v3/sse/sse-to-json/parsers/sse-parser.d.ts +73 -0
- package/dist/conversion/conversion-v3/sse/sse-to-json/parsers/sse-parser.js +332 -0
- package/dist/conversion/conversion-v3/sse/sse-to-json/responses-sse-to-json-converter.d.ts +59 -0
- package/dist/conversion/conversion-v3/sse/sse-to-json/responses-sse-to-json-converter.js +270 -0
- package/dist/conversion/conversion-v3/sse/types/anthropic-types.d.ts +87 -0
- package/dist/conversion/conversion-v3/sse/types/anthropic-types.js +5 -0
- package/dist/conversion/conversion-v3/sse/types/chat-types.d.ts +308 -0
- package/dist/conversion/conversion-v3/sse/types/chat-types.js +32 -0
- package/dist/conversion/conversion-v3/sse/types/conversion-context.d.ts +211 -0
- package/dist/conversion/conversion-v3/sse/types/conversion-context.js +20 -0
- package/dist/conversion/conversion-v3/sse/types/core-interfaces.d.ts +90 -0
- package/dist/conversion/conversion-v3/sse/types/core-interfaces.js +5 -0
- package/dist/conversion/conversion-v3/sse/types/index.d.ts +14 -0
- package/dist/conversion/conversion-v3/sse/types/index.js +7 -0
- package/dist/conversion/conversion-v3/sse/types/responses-types.d.ts +328 -0
- package/dist/conversion/conversion-v3/sse/types/responses-types.js +38 -0
- package/dist/conversion/conversion-v3/sse/types/sse-events.d.ts +180 -0
- package/dist/conversion/conversion-v3/sse/types/sse-events.js +150 -0
- package/dist/conversion/conversion-v3/sse/types/stream-state.d.ts +204 -0
- package/dist/conversion/conversion-v3/sse/types/stream-state.js +44 -0
- package/dist/conversion/conversion-v3/sse/types/utility-types.d.ts +230 -0
- package/dist/conversion/conversion-v3/sse/types/utility-types.js +49 -0
- package/dist/conversion/conversion-v3/standards/context-adapter.d.ts +146 -0
- package/dist/conversion/conversion-v3/standards/context-adapter.js +438 -0
- package/dist/conversion/conversion-v3/standards/errors.d.ts +38 -0
- package/dist/conversion/conversion-v3/standards/errors.js +27 -0
- package/dist/conversion/conversion-v3/standards/index.d.ts +15 -0
- package/dist/conversion/conversion-v3/standards/index.js +11 -0
- package/dist/conversion/conversion-v3/standards/node-capabilities.d.ts +123 -0
- package/dist/conversion/conversion-v3/standards/node-capabilities.js +330 -0
- package/dist/conversion/conversion-v3/standards/pipeline-context.d.ts +237 -0
- package/dist/conversion/conversion-v3/standards/pipeline-context.js +84 -0
- package/dist/conversion/conversion-v3/standards/pipeline-node.d.ts +196 -0
- package/dist/conversion/conversion-v3/standards/pipeline-node.js +316 -0
- package/dist/conversion/conversion-v3/standards/snapshot-handles.d.ts +24 -0
- package/dist/conversion/conversion-v3/standards/snapshot-handles.js +49 -0
- package/dist/conversion/conversion-v3/types/config-types.d.ts +242 -0
- package/dist/conversion/conversion-v3/types/config-types.js +13 -0
- package/dist/conversion/conversion-v3/types/index.d.ts +8 -0
- package/dist/conversion/conversion-v3/types/index.js +11 -0
- package/dist/conversion/conversion-v3/types/node-types.d.ts +211 -0
- package/dist/conversion/conversion-v3/types/node-types.js +20 -0
- package/dist/conversion/conversion-v3/types/pipeline-types.d.ts +246 -0
- package/dist/conversion/conversion-v3/types/pipeline-types.js +26 -0
- package/dist/conversion/conversion-v3/types/request-types.d.ts +293 -0
- package/dist/conversion/conversion-v3/types/request-types.js +26 -0
- package/dist/conversion/conversion-v3/utils/provider-payload-sanitizer.d.ts +10 -0
- package/dist/conversion/conversion-v3/utils/provider-payload-sanitizer.js +103 -0
- package/dist/conversion/conversion-v3/validations/index.d.ts +8 -0
- package/dist/conversion/conversion-v3/validations/index.js +7 -0
- package/dist/conversion/conversion-v3/validations/node-graph-validator.d.ts +116 -0
- package/dist/conversion/conversion-v3/validations/node-graph-validator.js +505 -0
- package/dist/conversion/conversion-v3/validations/tool-governance-enforcer.d.ts +139 -0
- package/dist/conversion/conversion-v3/validations/tool-governance-enforcer.js +338 -0
- package/dist/conversion/hub/config/types.d.ts +26 -0
- package/dist/conversion/hub/config/types.js +1 -0
- package/dist/conversion/hub/core/detour-registry.d.ts +9 -0
- package/dist/conversion/hub/core/detour-registry.js +23 -0
- package/dist/conversion/hub/core/hub-context.d.ts +21 -0
- package/dist/conversion/hub/core/hub-context.js +1 -0
- package/dist/conversion/hub/core/index.d.ts +3 -0
- package/dist/conversion/hub/core/index.js +3 -0
- package/dist/conversion/hub/core/stage-driver.d.ts +30 -0
- package/dist/conversion/hub/core/stage-driver.js +31 -0
- package/dist/conversion/hub/format-adapters/anthropic-format-adapter.d.ts +16 -0
- package/dist/conversion/hub/format-adapters/anthropic-format-adapter.js +23 -0
- package/dist/conversion/hub/format-adapters/chat-format-adapter.d.ts +17 -0
- package/dist/conversion/hub/format-adapters/chat-format-adapter.js +23 -0
- package/dist/conversion/hub/format-adapters/gemini-format-adapter.d.ts +16 -0
- package/dist/conversion/hub/format-adapters/gemini-format-adapter.js +23 -0
- package/dist/conversion/hub/format-adapters/index.d.ts +21 -0
- package/dist/conversion/hub/format-adapters/index.js +4 -0
- package/dist/conversion/hub/format-adapters/responses-format-adapter.d.ts +17 -0
- package/dist/conversion/hub/format-adapters/responses-format-adapter.js +23 -0
- package/dist/conversion/hub/hub-feature.d.ts +1 -0
- package/dist/conversion/hub/hub-feature.js +25 -0
- package/dist/conversion/hub/node-support.d.ts +19 -0
- package/dist/conversion/hub/node-support.js +115 -0
- package/dist/conversion/hub/pipeline/hub-pipeline.d.ts +68 -0
- package/dist/conversion/hub/pipeline/hub-pipeline.js +421 -0
- package/dist/conversion/hub/pipelines/inbound.d.ts +22 -0
- package/dist/conversion/hub/pipelines/inbound.js +64 -0
- package/dist/conversion/hub/pipelines/outbound.d.ts +22 -0
- package/dist/conversion/hub/pipelines/outbound.js +57 -0
- package/dist/conversion/hub/process/chat-process.d.ts +13 -0
- package/dist/conversion/hub/process/chat-process.js +50 -0
- package/dist/conversion/hub/registry.d.ts +28 -0
- package/dist/conversion/hub/registry.js +98 -0
- package/dist/conversion/hub/response/chat-response-utils.d.ts +6 -0
- package/dist/conversion/hub/response/chat-response-utils.js +89 -0
- package/dist/conversion/hub/response/provider-response.d.ts +20 -0
- package/dist/conversion/hub/response/provider-response.js +196 -0
- package/dist/conversion/hub/response/response-mappers.d.ts +19 -0
- package/dist/conversion/hub/response/response-mappers.js +23 -0
- package/dist/conversion/hub/response/response-runtime.d.ts +3 -0
- package/dist/conversion/hub/response/response-runtime.js +206 -0
- package/dist/conversion/hub/semantic-mappers/anthropic-mapper.d.ts +7 -0
- package/dist/conversion/hub/semantic-mappers/anthropic-mapper.js +308 -0
- package/dist/conversion/hub/semantic-mappers/chat-mapper.d.ts +7 -0
- package/dist/conversion/hub/semantic-mappers/chat-mapper.js +304 -0
- package/dist/conversion/hub/semantic-mappers/gemini-mapper.d.ts +7 -0
- package/dist/conversion/hub/semantic-mappers/gemini-mapper.js +477 -0
- package/dist/conversion/hub/semantic-mappers/index.d.ts +4 -0
- package/dist/conversion/hub/semantic-mappers/index.js +4 -0
- package/dist/conversion/hub/semantic-mappers/responses-mapper.d.ts +21 -0
- package/dist/conversion/hub/semantic-mappers/responses-mapper.js +379 -0
- package/dist/conversion/hub/snapshot-recorder.d.ts +12 -0
- package/dist/conversion/hub/snapshot-recorder.js +69 -0
- package/dist/conversion/hub/standardized-bridge.d.ts +12 -0
- package/dist/conversion/hub/standardized-bridge.js +248 -0
- package/dist/conversion/hub/types/chat-envelope.d.ts +67 -0
- package/dist/conversion/hub/types/chat-envelope.js +1 -0
- package/dist/conversion/hub/types/chat-schema.d.ts +112 -0
- package/dist/conversion/hub/types/chat-schema.js +1 -0
- package/dist/conversion/hub/types/errors.d.ts +5 -0
- package/dist/conversion/hub/types/errors.js +13 -0
- package/dist/conversion/hub/types/format-envelope.d.ts +7 -0
- package/dist/conversion/hub/types/format-envelope.js +1 -0
- package/dist/conversion/hub/types/index.d.ts +6 -0
- package/dist/conversion/hub/types/index.js +6 -0
- package/dist/conversion/hub/types/json.d.ts +9 -0
- package/dist/conversion/hub/types/json.js +9 -0
- package/dist/conversion/hub/types/node.d.ts +31 -0
- package/dist/conversion/hub/types/node.js +1 -0
- package/dist/conversion/hub/types/standardized.d.ts +94 -0
- package/dist/conversion/hub/types/standardized.js +1 -0
- package/dist/conversion/index.d.ts +28 -0
- package/dist/conversion/index.js +28 -0
- package/dist/conversion/responses/responses-openai-bridge.d.ts +95 -0
- package/dist/conversion/responses/responses-openai-bridge.js +1330 -0
- package/dist/conversion/schema-validator.d.ts +7 -0
- package/dist/conversion/schema-validator.js +67 -0
- package/dist/conversion/shared/args-mapping.d.ts +18 -0
- package/dist/conversion/shared/args-mapping.js +213 -0
- package/dist/conversion/shared/chat-request-filters.d.ts +9 -0
- package/dist/conversion/shared/chat-request-filters.js +74 -0
- package/dist/conversion/shared/jsonish.d.ts +3 -0
- package/dist/conversion/shared/jsonish.js +177 -0
- package/dist/conversion/shared/mcp-injection.d.ts +2 -0
- package/dist/conversion/shared/mcp-injection.js +89 -0
- package/dist/conversion/shared/media.d.ts +1 -0
- package/dist/conversion/shared/media.js +9 -0
- package/dist/conversion/shared/openai-finalizer.d.ts +9 -0
- package/dist/conversion/shared/openai-finalizer.js +104 -0
- package/dist/conversion/shared/openai-message-normalize.d.ts +1 -0
- package/dist/conversion/shared/openai-message-normalize.js +442 -0
- package/dist/conversion/shared/payload-budget.d.ts +13 -0
- package/dist/conversion/shared/payload-budget.js +248 -0
- package/dist/conversion/shared/reasoning-mapping.d.ts +5 -0
- package/dist/conversion/shared/reasoning-mapping.js +8 -0
- package/dist/conversion/shared/responses-conversation-store.d.ts +41 -0
- package/dist/conversion/shared/responses-conversation-store.js +285 -0
- package/dist/conversion/shared/responses-id-utils.d.ts +7 -0
- package/dist/conversion/shared/responses-id-utils.js +29 -0
- package/dist/conversion/shared/responses-instructions.d.ts +1 -0
- package/dist/conversion/shared/responses-instructions.js +113 -0
- package/dist/conversion/shared/responses-metadata.d.ts +1 -0
- package/dist/conversion/shared/responses-metadata.js +1 -0
- package/dist/conversion/shared/responses-request-adapter.d.ts +66 -0
- package/dist/conversion/shared/responses-request-adapter.js +461 -0
- package/dist/conversion/shared/snapshot-hooks.d.ts +12 -0
- package/dist/conversion/shared/snapshot-hooks.js +7 -0
- package/dist/conversion/shared/streaming-text-extractor.d.ts +26 -0
- package/dist/conversion/shared/streaming-text-extractor.js +145 -0
- package/dist/conversion/shared/text-markup-normalizer.d.ts +9 -0
- package/dist/conversion/shared/text-markup-normalizer.js +319 -0
- package/dist/conversion/shared/tool-canonicalizer.d.ts +2 -0
- package/dist/conversion/shared/tool-canonicalizer.js +50 -0
- package/dist/conversion/shared/tool-filter-pipeline.d.ts +17 -0
- package/dist/conversion/shared/tool-filter-pipeline.js +79 -0
- package/dist/conversion/shared/tool-governor.d.ts +27 -0
- package/dist/conversion/shared/tool-governor.js +269 -0
- package/dist/conversion/shared/tool-harvester.d.ts +31 -0
- package/dist/conversion/shared/tool-harvester.js +321 -0
- package/dist/conversion/shared/tool-mapping.d.ts +10 -0
- package/dist/conversion/shared/tool-mapping.js +10 -0
- package/dist/conversion/shared/tooling.d.ts +26 -0
- package/dist/conversion/shared/tooling.js +143 -0
- package/dist/conversion/types.d.ts +33 -0
- package/dist/conversion/types.js +1 -0
- package/dist/core/index.d.ts +6 -0
- package/dist/core/index.js +9 -0
- package/dist/core/llmswitch-engine-v2.d.ts +132 -0
- package/dist/core/llmswitch-engine-v2.js +649 -0
- package/dist/core/llmswitch-factory-v2.d.ts +123 -0
- package/dist/core/llmswitch-factory-v2.js +521 -0
- package/dist/endpoints/base-endpoint-handler.d.ts +128 -0
- package/dist/endpoints/base-endpoint-handler.js +545 -0
- package/dist/endpoints/chat-endpoint-handler.d.ts +115 -0
- package/dist/endpoints/chat-endpoint-handler.js +218 -0
- package/dist/endpoints/endpoint-router.d.ts +127 -0
- package/dist/endpoints/endpoint-router.js +438 -0
- package/dist/endpoints/index.d.ts +13 -0
- package/dist/endpoints/index.js +11 -0
- package/dist/endpoints/messages-endpoint-handler.d.ts +171 -0
- package/dist/endpoints/messages-endpoint-handler.js +238 -0
- package/dist/endpoints/responses-endpoint-handler.d.ts +147 -0
- package/dist/endpoints/responses-endpoint-handler.js +233 -0
- package/dist/filters/builtin/add-fields-filter.d.ts +8 -0
- package/dist/filters/builtin/add-fields-filter.js +16 -0
- package/dist/filters/builtin/blacklist-filter.d.ts +8 -0
- package/dist/filters/builtin/blacklist-filter.js +19 -0
- package/dist/filters/builtin/whitelist-filter.d.ts +8 -0
- package/dist/filters/builtin/whitelist-filter.js +19 -0
- package/dist/filters/engine.d.ts +16 -0
- package/dist/filters/engine.js +167 -0
- package/dist/filters/index.d.ts +19 -0
- package/dist/filters/index.js +24 -0
- package/dist/filters/special/request-streaming-to-nonstreaming.d.ts +12 -0
- package/dist/filters/special/request-streaming-to-nonstreaming.js +27 -0
- package/dist/filters/special/request-tool-choice-policy.d.ts +11 -0
- package/dist/filters/special/request-tool-choice-policy.js +27 -0
- package/dist/filters/special/request-tool-list-filter.d.ts +11 -0
- package/dist/filters/special/request-tool-list-filter.js +167 -0
- package/dist/filters/special/request-toolcalls-stringify.d.ts +10 -0
- package/dist/filters/special/request-toolcalls-stringify.js +44 -0
- package/dist/filters/special/request-tools-normalize.d.ts +12 -0
- package/dist/filters/special/request-tools-normalize.js +140 -0
- package/dist/filters/special/response-finish-invariants.d.ts +11 -0
- package/dist/filters/special/response-finish-invariants.js +30 -0
- package/dist/filters/special/response-openai-to-responses-bridge.d.ts +13 -0
- package/dist/filters/special/response-openai-to-responses-bridge.js +24 -0
- package/dist/filters/special/response-tool-arguments-blacklist.d.ts +12 -0
- package/dist/filters/special/response-tool-arguments-blacklist.js +63 -0
- package/dist/filters/special/response-tool-arguments-schema-converge.d.ts +13 -0
- package/dist/filters/special/response-tool-arguments-schema-converge.js +63 -0
- package/dist/filters/special/response-tool-arguments-stringify.d.ts +9 -0
- package/dist/filters/special/response-tool-arguments-stringify.js +134 -0
- package/dist/filters/special/response-tool-arguments-toon-decode.d.ts +10 -0
- package/dist/filters/special/response-tool-arguments-toon-decode.js +94 -0
- package/dist/filters/special/response-tool-arguments-whitelist.d.ts +11 -0
- package/dist/filters/special/response-tool-arguments-whitelist.js +56 -0
- package/dist/filters/special/response-tool-text-canonicalize.d.ts +10 -0
- package/dist/filters/special/response-tool-text-canonicalize.js +20 -0
- package/dist/filters/special/tool-filter-hooks.d.ts +19 -0
- package/dist/filters/special/tool-filter-hooks.js +341 -0
- package/dist/filters/special/tool-post-constraints.d.ts +31 -0
- package/dist/filters/special/tool-post-constraints.js +120 -0
- package/dist/filters/types.d.ts +68 -0
- package/dist/filters/types.js +3 -0
- package/dist/filters/utils/fieldmap-loader.d.ts +2 -0
- package/dist/filters/utils/fieldmap-loader.js +16 -0
- package/dist/filters/utils/snapshot-writer.d.ts +10 -0
- package/dist/filters/utils/snapshot-writer.js +47 -0
- package/dist/guidance/index.d.ts +3 -0
- package/dist/guidance/index.js +228 -0
- package/dist/hooks/hooks-integration.d.ts +161 -0
- package/dist/hooks/hooks-integration.js +519 -0
- package/dist/hooks/index.d.ts +10 -0
- package/dist/hooks/index.js +13 -0
- package/dist/html-to-markdown.d.ts +5 -0
- package/dist/html-to-markdown.js +66 -0
- package/dist/http/sse-response.d.ts +22 -0
- package/dist/http/sse-response.js +103 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +10 -0
- package/dist/router/virtual-router/bootstrap.d.ts +6 -0
- package/dist/router/virtual-router/bootstrap.js +620 -0
- package/dist/router/virtual-router/classifier.d.ts +10 -0
- package/dist/router/virtual-router/classifier.js +99 -0
- package/dist/router/virtual-router/engine.d.ts +35 -0
- package/dist/router/virtual-router/engine.js +260 -0
- package/dist/router/virtual-router/error-center.d.ts +10 -0
- package/dist/router/virtual-router/error-center.js +39 -0
- package/dist/router/virtual-router/features.d.ts +3 -0
- package/dist/router/virtual-router/features.js +162 -0
- package/dist/router/virtual-router/health-manager.d.ts +18 -0
- package/dist/router/virtual-router/health-manager.js +98 -0
- package/dist/router/virtual-router/load-balancer.d.ts +18 -0
- package/dist/router/virtual-router/load-balancer.js +67 -0
- package/dist/router/virtual-router/provider-registry.d.ts +11 -0
- package/dist/router/virtual-router/provider-registry.js +74 -0
- package/dist/router/virtual-router/types.d.ts +206 -0
- package/dist/router/virtual-router/types.js +31 -0
- package/dist/sse/index.d.ts +140 -0
- package/dist/sse/index.js +115 -0
- package/dist/sse/json-to-sse/anthropic-json-to-sse-converter.d.ts +11 -0
- package/dist/sse/json-to-sse/anthropic-json-to-sse-converter.js +79 -0
- package/dist/sse/json-to-sse/chat-json-to-sse-converter.d.ts +79 -0
- package/dist/sse/json-to-sse/chat-json-to-sse-converter.js +296 -0
- package/dist/sse/json-to-sse/event-generators/chat.d.ts +55 -0
- package/dist/sse/json-to-sse/event-generators/chat.js +233 -0
- package/dist/sse/json-to-sse/event-generators/responses.d.ts +99 -0
- package/dist/sse/json-to-sse/event-generators/responses.js +599 -0
- package/dist/sse/json-to-sse/index.d.ts +6 -0
- package/dist/sse/json-to-sse/index.js +7 -0
- package/dist/sse/json-to-sse/responses-json-to-sse-converter.d.ts +79 -0
- package/dist/sse/json-to-sse/responses-json-to-sse-converter.js +322 -0
- package/dist/sse/json-to-sse/sequencers/chat-sequencer.d.ts +37 -0
- package/dist/sse/json-to-sse/sequencers/chat-sequencer.js +218 -0
- package/dist/sse/json-to-sse/sequencers/responses-sequencer.d.ts +40 -0
- package/dist/sse/json-to-sse/sequencers/responses-sequencer.js +263 -0
- package/dist/sse/registry/sse-codec-registry.d.ts +32 -0
- package/dist/sse/registry/sse-codec-registry.js +106 -0
- package/dist/sse/shared/chat-serializer.d.ts +4 -0
- package/dist/sse/shared/chat-serializer.js +40 -0
- package/dist/sse/shared/constants.d.ts +272 -0
- package/dist/sse/shared/constants.js +321 -0
- package/dist/sse/shared/serializers/base-serializer.d.ts +158 -0
- package/dist/sse/shared/serializers/base-serializer.js +210 -0
- package/dist/sse/shared/serializers/chat-event-serializer.d.ts +82 -0
- package/dist/sse/shared/serializers/chat-event-serializer.js +275 -0
- package/dist/sse/shared/serializers/index.d.ts +39 -0
- package/dist/sse/shared/serializers/index.js +54 -0
- package/dist/sse/shared/serializers/responses-event-serializer.d.ts +123 -0
- package/dist/sse/shared/serializers/responses-event-serializer.js +356 -0
- package/dist/sse/shared/serializers/types.d.ts +51 -0
- package/dist/sse/shared/serializers/types.js +4 -0
- package/dist/sse/shared/utils.d.ts +254 -0
- package/dist/sse/shared/utils.js +543 -0
- package/dist/sse/shared/writer.d.ts +111 -0
- package/dist/sse/shared/writer.js +268 -0
- package/dist/sse/sse-to-json/anthropic-sse-to-json-converter.d.ts +13 -0
- package/dist/sse/sse-to-json/anthropic-sse-to-json-converter.js +139 -0
- package/dist/sse/sse-to-json/builders/response-builder.d.ts +165 -0
- package/dist/sse/sse-to-json/builders/response-builder.js +776 -0
- package/dist/sse/sse-to-json/chat-sse-to-json-converter.d.ts +106 -0
- package/dist/sse/sse-to-json/chat-sse-to-json-converter.js +540 -0
- package/dist/sse/sse-to-json/index.d.ts +6 -0
- package/dist/sse/sse-to-json/index.js +7 -0
- package/dist/sse/sse-to-json/parsers/sse-parser.d.ts +73 -0
- package/dist/sse/sse-to-json/parsers/sse-parser.js +340 -0
- package/dist/sse/sse-to-json/responses-sse-to-json-converter.d.ts +59 -0
- package/dist/sse/sse-to-json/responses-sse-to-json-converter.js +270 -0
- package/dist/sse/types/anthropic-types.d.ts +87 -0
- package/dist/sse/types/anthropic-types.js +5 -0
- package/dist/sse/types/chat-types.d.ts +308 -0
- package/dist/sse/types/chat-types.js +32 -0
- package/dist/sse/types/conversion-context.d.ts +211 -0
- package/dist/sse/types/conversion-context.js +20 -0
- package/dist/sse/types/core-interfaces.d.ts +90 -0
- package/dist/sse/types/core-interfaces.js +5 -0
- package/dist/sse/types/index.d.ts +14 -0
- package/dist/sse/types/index.js +7 -0
- package/dist/sse/types/responses-types.d.ts +328 -0
- package/dist/sse/types/responses-types.js +38 -0
- package/dist/sse/types/sse-events.d.ts +180 -0
- package/dist/sse/types/sse-events.js +150 -0
- package/dist/sse/types/stream-state.d.ts +204 -0
- package/dist/sse/types/stream-state.js +44 -0
- package/dist/sse/types/utility-types.d.ts +230 -0
- package/dist/sse/types/utility-types.js +49 -0
- package/dist/tools/proxy/anthropic-tool-proxy.d.ts +19 -0
- package/dist/tools/proxy/anthropic-tool-proxy.js +57 -0
- package/dist/tools/proxy/executors/web-fetch.d.ts +6 -0
- package/dist/tools/proxy/executors/web-fetch.js +39 -0
- package/dist/tools/proxy/registry.d.ts +8 -0
- package/dist/tools/proxy/registry.js +14 -0
- package/dist/tools/proxy/types.d.ts +29 -0
- package/dist/tools/proxy/types.js +1 -0
- package/dist/tools/tool-registry.d.ts +8 -0
- package/dist/tools/tool-registry.js +154 -0
- package/dist/tools/web-fetch-html.d.ts +15 -0
- package/dist/tools/web-fetch-html.js +83 -0
- package/dist/utils/conversion-adapter.d.ts +65 -0
- package/dist/utils/conversion-adapter.js +183 -0
- package/dist/utils/error-factory.d.ts +88 -0
- package/dist/utils/error-factory.js +138 -0
- package/dist/utils/error-utils.d.ts +334 -0
- package/dist/utils/error-utils.js +782 -0
- package/dist/utils/index.d.ts +8 -0
- package/dist/utils/index.js +13 -0
- package/dist/utils/performance-utils.d.ts +205 -0
- package/dist/utils/performance-utils.js +440 -0
- package/dist/utils/profile-mapper.d.ts +32 -0
- package/dist/utils/profile-mapper.js +269 -0
- package/dist/utils/request-processor.d.ts +52 -0
- package/dist/utils/request-processor.js +210 -0
- package/dist/utils/request-utils.d.ts +117 -0
- package/dist/utils/request-utils.js +333 -0
- package/dist/utils/response-utils.d.ts +127 -0
- package/dist/utils/response-utils.js +516 -0
- package/dist/utils/token-counter.d.ts +89 -0
- package/dist/utils/token-counter.js +541 -0
- package/package.json +65 -0
|
@@ -0,0 +1,782 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 错误处理工具函数
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* LLMSwitch错误类型
|
|
6
|
+
*/
|
|
7
|
+
export var LLMSwitchErrorType;
|
|
8
|
+
(function (LLMSwitchErrorType) {
|
|
9
|
+
// 请求相关错误
|
|
10
|
+
LLMSwitchErrorType["VALIDATION_ERROR"] = "validation_error";
|
|
11
|
+
LLMSwitchErrorType["INVALID_REQUEST_FORMAT"] = "invalid_request_format";
|
|
12
|
+
LLMSwitchErrorType["MISSING_REQUIRED_FIELD"] = "missing_required_field";
|
|
13
|
+
// 处理相关错误
|
|
14
|
+
LLMSwitchErrorType["PROCESSING_ERROR"] = "processing_error";
|
|
15
|
+
LLMSwitchErrorType["HOOK_EXECUTION_ERROR"] = "hook_execution_error";
|
|
16
|
+
LLMSwitchErrorType["TRANSFORMATION_ERROR"] = "transformation_error";
|
|
17
|
+
// 网络相关错误
|
|
18
|
+
LLMSwitchErrorType["NETWORK_ERROR"] = "network_error";
|
|
19
|
+
LLMSwitchErrorType["TIMEOUT_ERROR"] = "timeout_error";
|
|
20
|
+
LLMSwitchErrorType["CONNECTION_ERROR"] = "connection_error";
|
|
21
|
+
// 端点相关错误
|
|
22
|
+
LLMSwitchErrorType["ENDPOINT_NOT_FOUND"] = "endpoint_not_found";
|
|
23
|
+
LLMSwitchErrorType["ENDPOINT_UNAVAILABLE"] = "endpoint_unavailable";
|
|
24
|
+
LLMSwitchErrorType["UNSUPPORTED_PROTOCOL"] = "unsupported_protocol";
|
|
25
|
+
// 系统相关错误
|
|
26
|
+
LLMSwitchErrorType["MEMORY_ERROR"] = "memory_error";
|
|
27
|
+
LLMSwitchErrorType["CONFIGURATION_ERROR"] = "configuration_error";
|
|
28
|
+
LLMSwitchErrorType["INTERNAL_ERROR"] = "internal_error";
|
|
29
|
+
// 流式相关错误
|
|
30
|
+
LLMSwitchErrorType["STREAM_ERROR"] = "stream_error";
|
|
31
|
+
LLMSwitchErrorType["SSE_ERROR"] = "sse_error";
|
|
32
|
+
})(LLMSwitchErrorType || (LLMSwitchErrorType = {}));
|
|
33
|
+
/**
|
|
34
|
+
* 错误严重程度
|
|
35
|
+
*/
|
|
36
|
+
export var ErrorSeverity;
|
|
37
|
+
(function (ErrorSeverity) {
|
|
38
|
+
ErrorSeverity["LOW"] = "low";
|
|
39
|
+
ErrorSeverity["MEDIUM"] = "medium";
|
|
40
|
+
ErrorSeverity["HIGH"] = "high";
|
|
41
|
+
ErrorSeverity["CRITICAL"] = "critical";
|
|
42
|
+
})(ErrorSeverity || (ErrorSeverity = {}));
|
|
43
|
+
/**
|
|
44
|
+
* 错误类型配置映射
|
|
45
|
+
*/
|
|
46
|
+
export const ERROR_TYPE_CONFIG = new Map([
|
|
47
|
+
// 请求相关错误 - 客户端问题
|
|
48
|
+
[LLMSwitchErrorType.VALIDATION_ERROR, {
|
|
49
|
+
severity: ErrorSeverity.LOW,
|
|
50
|
+
httpStatus: 400,
|
|
51
|
+
isRetryable: false,
|
|
52
|
+
defaultMessage: 'Invalid request data'
|
|
53
|
+
}],
|
|
54
|
+
[LLMSwitchErrorType.INVALID_REQUEST_FORMAT, {
|
|
55
|
+
severity: ErrorSeverity.LOW,
|
|
56
|
+
httpStatus: 400,
|
|
57
|
+
isRetryable: false,
|
|
58
|
+
defaultMessage: 'Invalid request format'
|
|
59
|
+
}],
|
|
60
|
+
[LLMSwitchErrorType.MISSING_REQUIRED_FIELD, {
|
|
61
|
+
severity: ErrorSeverity.LOW,
|
|
62
|
+
httpStatus: 400,
|
|
63
|
+
isRetryable: false,
|
|
64
|
+
defaultMessage: 'Missing required field'
|
|
65
|
+
}],
|
|
66
|
+
// 处理相关错误 - 服务端问题
|
|
67
|
+
[LLMSwitchErrorType.PROCESSING_ERROR, {
|
|
68
|
+
severity: ErrorSeverity.HIGH,
|
|
69
|
+
httpStatus: 500,
|
|
70
|
+
isRetryable: true,
|
|
71
|
+
defaultMessage: 'Request processing failed'
|
|
72
|
+
}],
|
|
73
|
+
[LLMSwitchErrorType.HOOK_EXECUTION_ERROR, {
|
|
74
|
+
severity: ErrorSeverity.MEDIUM,
|
|
75
|
+
httpStatus: 500,
|
|
76
|
+
isRetryable: true,
|
|
77
|
+
defaultMessage: 'Hook execution failed'
|
|
78
|
+
}],
|
|
79
|
+
[LLMSwitchErrorType.TRANSFORMATION_ERROR, {
|
|
80
|
+
severity: ErrorSeverity.HIGH,
|
|
81
|
+
httpStatus: 500,
|
|
82
|
+
isRetryable: true,
|
|
83
|
+
defaultMessage: 'Data transformation failed'
|
|
84
|
+
}],
|
|
85
|
+
// 网络相关错误 - 可能重试
|
|
86
|
+
[LLMSwitchErrorType.NETWORK_ERROR, {
|
|
87
|
+
severity: ErrorSeverity.MEDIUM,
|
|
88
|
+
httpStatus: 503,
|
|
89
|
+
isRetryable: true,
|
|
90
|
+
defaultMessage: 'Network connection failed'
|
|
91
|
+
}],
|
|
92
|
+
[LLMSwitchErrorType.TIMEOUT_ERROR, {
|
|
93
|
+
severity: ErrorSeverity.MEDIUM,
|
|
94
|
+
httpStatus: 408,
|
|
95
|
+
isRetryable: true,
|
|
96
|
+
defaultMessage: 'Request timeout'
|
|
97
|
+
}],
|
|
98
|
+
[LLMSwitchErrorType.CONNECTION_ERROR, {
|
|
99
|
+
severity: ErrorSeverity.MEDIUM,
|
|
100
|
+
httpStatus: 503,
|
|
101
|
+
isRetryable: true,
|
|
102
|
+
defaultMessage: 'Connection failed'
|
|
103
|
+
}],
|
|
104
|
+
// 端点相关错误
|
|
105
|
+
[LLMSwitchErrorType.ENDPOINT_NOT_FOUND, {
|
|
106
|
+
severity: ErrorSeverity.MEDIUM,
|
|
107
|
+
httpStatus: 404,
|
|
108
|
+
isRetryable: false,
|
|
109
|
+
defaultMessage: 'Endpoint not found'
|
|
110
|
+
}],
|
|
111
|
+
[LLMSwitchErrorType.ENDPOINT_UNAVAILABLE, {
|
|
112
|
+
severity: ErrorSeverity.HIGH,
|
|
113
|
+
httpStatus: 503,
|
|
114
|
+
isRetryable: true,
|
|
115
|
+
defaultMessage: 'Endpoint unavailable'
|
|
116
|
+
}],
|
|
117
|
+
[LLMSwitchErrorType.UNSUPPORTED_PROTOCOL, {
|
|
118
|
+
severity: ErrorSeverity.LOW,
|
|
119
|
+
httpStatus: 400,
|
|
120
|
+
isRetryable: false,
|
|
121
|
+
defaultMessage: 'Unsupported protocol'
|
|
122
|
+
}],
|
|
123
|
+
// 系统相关错误 - 严重问题
|
|
124
|
+
[LLMSwitchErrorType.MEMORY_ERROR, {
|
|
125
|
+
severity: ErrorSeverity.CRITICAL,
|
|
126
|
+
httpStatus: 503,
|
|
127
|
+
isRetryable: true,
|
|
128
|
+
defaultMessage: 'Insufficient memory'
|
|
129
|
+
}],
|
|
130
|
+
[LLMSwitchErrorType.CONFIGURATION_ERROR, {
|
|
131
|
+
severity: ErrorSeverity.CRITICAL,
|
|
132
|
+
httpStatus: 500,
|
|
133
|
+
isRetryable: false,
|
|
134
|
+
defaultMessage: 'Configuration error'
|
|
135
|
+
}],
|
|
136
|
+
[LLMSwitchErrorType.INTERNAL_ERROR, {
|
|
137
|
+
severity: ErrorSeverity.CRITICAL,
|
|
138
|
+
httpStatus: 500,
|
|
139
|
+
isRetryable: true,
|
|
140
|
+
defaultMessage: 'Internal server error'
|
|
141
|
+
}],
|
|
142
|
+
// 流式相关错误
|
|
143
|
+
[LLMSwitchErrorType.STREAM_ERROR, {
|
|
144
|
+
severity: ErrorSeverity.HIGH,
|
|
145
|
+
httpStatus: 500,
|
|
146
|
+
isRetryable: true,
|
|
147
|
+
defaultMessage: 'Stream processing error'
|
|
148
|
+
}],
|
|
149
|
+
[LLMSwitchErrorType.SSE_ERROR, {
|
|
150
|
+
severity: ErrorSeverity.MEDIUM,
|
|
151
|
+
httpStatus: 500,
|
|
152
|
+
isRetryable: true,
|
|
153
|
+
defaultMessage: 'Server-sent events error'
|
|
154
|
+
}]
|
|
155
|
+
]);
|
|
156
|
+
/**
|
|
157
|
+
* LLMSwitch自定义错误类
|
|
158
|
+
*/
|
|
159
|
+
export class LLMSwitchError extends Error {
|
|
160
|
+
type;
|
|
161
|
+
code;
|
|
162
|
+
requestId;
|
|
163
|
+
endpoint;
|
|
164
|
+
stage;
|
|
165
|
+
originalError;
|
|
166
|
+
context;
|
|
167
|
+
timestamp;
|
|
168
|
+
severity;
|
|
169
|
+
httpStatus;
|
|
170
|
+
isRetryable;
|
|
171
|
+
constructor(type, message, options = {}) {
|
|
172
|
+
super(message);
|
|
173
|
+
this.name = 'LLMSwitchError';
|
|
174
|
+
this.type = type;
|
|
175
|
+
this.code = options.code || type;
|
|
176
|
+
this.requestId = options.requestId;
|
|
177
|
+
this.endpoint = options.endpoint;
|
|
178
|
+
this.stage = options.stage;
|
|
179
|
+
this.originalError = options.originalError;
|
|
180
|
+
this.context = options.context;
|
|
181
|
+
this.timestamp = Date.now();
|
|
182
|
+
// 从配置映射获取错误属性
|
|
183
|
+
const config = ERROR_TYPE_CONFIG.get(type);
|
|
184
|
+
if (config) {
|
|
185
|
+
this.severity = config.severity;
|
|
186
|
+
this.httpStatus = config.httpStatus;
|
|
187
|
+
this.isRetryable = config.isRetryable;
|
|
188
|
+
}
|
|
189
|
+
else {
|
|
190
|
+
// 默认配置
|
|
191
|
+
this.severity = ErrorSeverity.MEDIUM;
|
|
192
|
+
this.httpStatus = 500;
|
|
193
|
+
this.isRetryable = false;
|
|
194
|
+
}
|
|
195
|
+
// 保持堆栈跟踪
|
|
196
|
+
if (Error.captureStackTrace) {
|
|
197
|
+
Error.captureStackTrace(this, LLMSwitchError);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* 转换为JSON格式
|
|
202
|
+
*/
|
|
203
|
+
toJSON() {
|
|
204
|
+
return {
|
|
205
|
+
name: this.name,
|
|
206
|
+
type: this.type,
|
|
207
|
+
code: this.code,
|
|
208
|
+
message: this.message,
|
|
209
|
+
requestId: this.requestId,
|
|
210
|
+
endpoint: this.endpoint,
|
|
211
|
+
stage: this.stage,
|
|
212
|
+
context: this.context,
|
|
213
|
+
timestamp: this.timestamp,
|
|
214
|
+
severity: this.severity,
|
|
215
|
+
httpStatus: this.httpStatus,
|
|
216
|
+
isRetryable: this.isRetryable,
|
|
217
|
+
stack: this.stack,
|
|
218
|
+
originalError: this.originalError ? {
|
|
219
|
+
name: this.originalError.name,
|
|
220
|
+
message: this.originalError.message,
|
|
221
|
+
stack: this.originalError.stack
|
|
222
|
+
} : undefined
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* 转换为标准HTTP错误响应格式
|
|
227
|
+
*/
|
|
228
|
+
toErrorResponse() {
|
|
229
|
+
return {
|
|
230
|
+
success: false,
|
|
231
|
+
error: {
|
|
232
|
+
type: this.type,
|
|
233
|
+
code: this.code,
|
|
234
|
+
message: this.message,
|
|
235
|
+
severity: this.severity,
|
|
236
|
+
requestId: this.requestId,
|
|
237
|
+
endpoint: this.endpoint,
|
|
238
|
+
stage: this.stage,
|
|
239
|
+
timestamp: this.timestamp,
|
|
240
|
+
context: this.context
|
|
241
|
+
}
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* 转换为API错误响应格式(兼容旧版本)
|
|
246
|
+
*/
|
|
247
|
+
toAPIResponse() {
|
|
248
|
+
return this.toErrorResponse();
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* 错误工厂类
|
|
253
|
+
*/
|
|
254
|
+
export class ErrorFactory {
|
|
255
|
+
/**
|
|
256
|
+
* 创建验证错误
|
|
257
|
+
*/
|
|
258
|
+
static validationError(message, field, context) {
|
|
259
|
+
return new LLMSwitchError(LLMSwitchErrorType.VALIDATION_ERROR, message, {
|
|
260
|
+
code: 'validation_failed',
|
|
261
|
+
context: { field, ...context }
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* 创建请求格式错误
|
|
266
|
+
*/
|
|
267
|
+
static invalidRequestFormat(message, context) {
|
|
268
|
+
return new LLMSwitchError(LLMSwitchErrorType.INVALID_REQUEST_FORMAT, message, {
|
|
269
|
+
code: 'invalid_format',
|
|
270
|
+
context
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* 创建缺失字段错误
|
|
275
|
+
*/
|
|
276
|
+
static missingRequiredField(field, context) {
|
|
277
|
+
return new LLMSwitchError(LLMSwitchErrorType.MISSING_REQUIRED_FIELD, `Missing required field: ${field}`, {
|
|
278
|
+
code: 'missing_field',
|
|
279
|
+
context: { field, ...context }
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* 创建处理错误
|
|
284
|
+
*/
|
|
285
|
+
static processingError(message, originalError, context) {
|
|
286
|
+
return new LLMSwitchError(LLMSwitchErrorType.PROCESSING_ERROR, message, {
|
|
287
|
+
code: 'processing_failed',
|
|
288
|
+
originalError,
|
|
289
|
+
context
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* 创建Hook执行错误
|
|
294
|
+
*/
|
|
295
|
+
static hookExecutionError(stage, message, originalError, context) {
|
|
296
|
+
return new LLMSwitchError(LLMSwitchErrorType.HOOK_EXECUTION_ERROR, `Hook execution failed at ${stage}: ${message}`, {
|
|
297
|
+
code: 'hook_failed',
|
|
298
|
+
stage,
|
|
299
|
+
originalError,
|
|
300
|
+
context
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
/**
|
|
304
|
+
* 创建转换错误
|
|
305
|
+
*/
|
|
306
|
+
static transformationError(message, fromProtocol, toProtocol, context) {
|
|
307
|
+
return new LLMSwitchError(LLMSwitchErrorType.TRANSFORMATION_ERROR, message, {
|
|
308
|
+
code: 'transformation_failed',
|
|
309
|
+
context: { fromProtocol, toProtocol, ...context }
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
/**
|
|
313
|
+
* 创建网络错误
|
|
314
|
+
*/
|
|
315
|
+
static networkError(message, url, statusCode, context) {
|
|
316
|
+
return new LLMSwitchError(LLMSwitchErrorType.NETWORK_ERROR, message, {
|
|
317
|
+
code: 'network_failed',
|
|
318
|
+
context: { url, statusCode, ...context }
|
|
319
|
+
});
|
|
320
|
+
}
|
|
321
|
+
/**
|
|
322
|
+
* 创建超时错误
|
|
323
|
+
*/
|
|
324
|
+
static timeoutError(operation, timeout, context) {
|
|
325
|
+
return new LLMSwitchError(LLMSwitchErrorType.TIMEOUT_ERROR, `Operation ${operation} timed out after ${timeout}ms`, {
|
|
326
|
+
code: 'timeout',
|
|
327
|
+
context: { operation, timeout, ...context }
|
|
328
|
+
});
|
|
329
|
+
}
|
|
330
|
+
/**
|
|
331
|
+
* 创建连接错误
|
|
332
|
+
*/
|
|
333
|
+
static connectionError(message, endpoint, context) {
|
|
334
|
+
return new LLMSwitchError(LLMSwitchErrorType.CONNECTION_ERROR, message, {
|
|
335
|
+
code: 'connection_failed',
|
|
336
|
+
context: { endpoint, ...context }
|
|
337
|
+
});
|
|
338
|
+
}
|
|
339
|
+
/**
|
|
340
|
+
* 创建端点未找到错误
|
|
341
|
+
*/
|
|
342
|
+
static endpointNotFound(endpoint, context) {
|
|
343
|
+
return new LLMSwitchError(LLMSwitchErrorType.ENDPOINT_NOT_FOUND, `Endpoint not found: ${endpoint}`, {
|
|
344
|
+
code: 'endpoint_not_found',
|
|
345
|
+
context: { endpoint, ...context }
|
|
346
|
+
});
|
|
347
|
+
}
|
|
348
|
+
/**
|
|
349
|
+
* 创建端点不可用错误
|
|
350
|
+
*/
|
|
351
|
+
static endpointUnavailable(endpoint, reason, context) {
|
|
352
|
+
return new LLMSwitchError(LLMSwitchErrorType.ENDPOINT_UNAVAILABLE, `Endpoint ${endpoint} unavailable${reason ? `: ${reason}` : ''}`, {
|
|
353
|
+
code: 'endpoint_unavailable',
|
|
354
|
+
context: { endpoint, reason, ...context }
|
|
355
|
+
});
|
|
356
|
+
}
|
|
357
|
+
/**
|
|
358
|
+
* 创建不支持的协议错误
|
|
359
|
+
*/
|
|
360
|
+
static unsupportedProtocol(protocol, supportedProtocols, context) {
|
|
361
|
+
return new LLMSwitchError(LLMSwitchErrorType.UNSUPPORTED_PROTOCOL, `Unsupported protocol: ${protocol}`, {
|
|
362
|
+
code: 'unsupported_protocol',
|
|
363
|
+
context: { protocol, supportedProtocols, ...context }
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
/**
|
|
367
|
+
* 创建内存错误
|
|
368
|
+
*/
|
|
369
|
+
static memoryError(message, usage, context) {
|
|
370
|
+
return new LLMSwitchError(LLMSwitchErrorType.MEMORY_ERROR, message, {
|
|
371
|
+
code: 'memory_error',
|
|
372
|
+
context: { usage, ...context }
|
|
373
|
+
});
|
|
374
|
+
}
|
|
375
|
+
/**
|
|
376
|
+
* 创建配置错误
|
|
377
|
+
*/
|
|
378
|
+
static configurationError(message, configPath, context) {
|
|
379
|
+
return new LLMSwitchError(LLMSwitchErrorType.CONFIGURATION_ERROR, message, {
|
|
380
|
+
code: 'configuration_error',
|
|
381
|
+
context: { configPath, ...context }
|
|
382
|
+
});
|
|
383
|
+
}
|
|
384
|
+
/**
|
|
385
|
+
* 创建内部错误
|
|
386
|
+
*/
|
|
387
|
+
static internalError(message, originalError, context) {
|
|
388
|
+
return new LLMSwitchError(LLMSwitchErrorType.INTERNAL_ERROR, `Internal error: ${message}`, {
|
|
389
|
+
code: 'internal_error',
|
|
390
|
+
originalError,
|
|
391
|
+
context
|
|
392
|
+
});
|
|
393
|
+
}
|
|
394
|
+
/**
|
|
395
|
+
* 创建流式错误
|
|
396
|
+
*/
|
|
397
|
+
static streamError(message, context) {
|
|
398
|
+
return new LLMSwitchError(LLMSwitchErrorType.STREAM_ERROR, message, {
|
|
399
|
+
code: 'stream_failed',
|
|
400
|
+
context
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
/**
|
|
404
|
+
* 创建SSE错误
|
|
405
|
+
*/
|
|
406
|
+
static sseError(message, context) {
|
|
407
|
+
return new LLMSwitchError(LLMSwitchErrorType.SSE_ERROR, message, {
|
|
408
|
+
code: 'sse_failed',
|
|
409
|
+
context
|
|
410
|
+
});
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
/**
|
|
414
|
+
* 错误处理器
|
|
415
|
+
*/
|
|
416
|
+
export class ErrorHandler {
|
|
417
|
+
static errorHandlers = new Map();
|
|
418
|
+
static errorLog = [];
|
|
419
|
+
/**
|
|
420
|
+
* 注册错误处理器
|
|
421
|
+
*/
|
|
422
|
+
static registerHandler(errorType, handler) {
|
|
423
|
+
this.errorHandlers.set(errorType, handler);
|
|
424
|
+
}
|
|
425
|
+
/**
|
|
426
|
+
* 处理错误
|
|
427
|
+
*/
|
|
428
|
+
static handle(error, context) {
|
|
429
|
+
let llmswitchError;
|
|
430
|
+
if (error instanceof LLMSwitchError) {
|
|
431
|
+
llmswitchError = error;
|
|
432
|
+
}
|
|
433
|
+
else {
|
|
434
|
+
// 转换普通错误为LLMSwitchError
|
|
435
|
+
llmswitchError = new LLMSwitchError(LLMSwitchErrorType.INTERNAL_ERROR, error.message, {
|
|
436
|
+
originalError: error,
|
|
437
|
+
...context
|
|
438
|
+
});
|
|
439
|
+
}
|
|
440
|
+
// 添加上下文信息
|
|
441
|
+
if (context) {
|
|
442
|
+
if (context.requestId && !llmswitchError.requestId) {
|
|
443
|
+
llmswitchError.requestId = context.requestId;
|
|
444
|
+
}
|
|
445
|
+
if (context.endpoint && !llmswitchError.endpoint) {
|
|
446
|
+
llmswitchError.endpoint = context.endpoint;
|
|
447
|
+
}
|
|
448
|
+
if (context.stage && !llmswitchError.stage) {
|
|
449
|
+
llmswitchError.stage = context.stage;
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
// 记录错误
|
|
453
|
+
this.logError(llmswitchError);
|
|
454
|
+
// 执行注册的错误处理器
|
|
455
|
+
const handler = this.errorHandlers.get(llmswitchError.type);
|
|
456
|
+
if (handler) {
|
|
457
|
+
try {
|
|
458
|
+
handler(llmswitchError);
|
|
459
|
+
}
|
|
460
|
+
catch (handlerError) {
|
|
461
|
+
console.error('Error handler failed:', handlerError);
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
return llmswitchError;
|
|
465
|
+
}
|
|
466
|
+
/**
|
|
467
|
+
* 记录错误
|
|
468
|
+
*/
|
|
469
|
+
static logError(error) {
|
|
470
|
+
this.errorLog.push({
|
|
471
|
+
error,
|
|
472
|
+
timestamp: Date.now()
|
|
473
|
+
});
|
|
474
|
+
// 保持最近1000个错误记录
|
|
475
|
+
if (this.errorLog.length > 1000) {
|
|
476
|
+
this.errorLog = this.errorLog.slice(-1000);
|
|
477
|
+
}
|
|
478
|
+
// 输出到控制台
|
|
479
|
+
console.error(`[${new Date().toISOString()}] LLMSwitchError:`, {
|
|
480
|
+
type: error.type,
|
|
481
|
+
code: error.code,
|
|
482
|
+
message: error.message,
|
|
483
|
+
requestId: error.requestId,
|
|
484
|
+
endpoint: error.endpoint,
|
|
485
|
+
stage: error.stage,
|
|
486
|
+
context: error.context
|
|
487
|
+
});
|
|
488
|
+
}
|
|
489
|
+
/**
|
|
490
|
+
* 获取错误日志
|
|
491
|
+
*/
|
|
492
|
+
static getErrorLog(filter) {
|
|
493
|
+
let log = this.errorLog;
|
|
494
|
+
if (filter) {
|
|
495
|
+
if (filter.errorType) {
|
|
496
|
+
log = log.filter(entry => entry.error.type === filter.errorType);
|
|
497
|
+
}
|
|
498
|
+
if (filter.since) {
|
|
499
|
+
log = log.filter(entry => entry.timestamp >= filter.since);
|
|
500
|
+
}
|
|
501
|
+
if (filter.requestId) {
|
|
502
|
+
log = log.filter(entry => entry.error.requestId === filter.requestId);
|
|
503
|
+
}
|
|
504
|
+
if (filter.endpoint) {
|
|
505
|
+
log = log.filter(entry => entry.error.endpoint === filter.endpoint);
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
return log;
|
|
509
|
+
}
|
|
510
|
+
/**
|
|
511
|
+
* 获取错误统计
|
|
512
|
+
*/
|
|
513
|
+
static getErrorStats() {
|
|
514
|
+
const byType = {};
|
|
515
|
+
const byEndpoint = {};
|
|
516
|
+
const byStage = {};
|
|
517
|
+
for (const entry of this.errorLog) {
|
|
518
|
+
// 按类型统计
|
|
519
|
+
byType[entry.error.type] = (byType[entry.error.type] || 0) + 1;
|
|
520
|
+
// 按端点统计
|
|
521
|
+
if (entry.error.endpoint) {
|
|
522
|
+
byEndpoint[entry.error.endpoint] = (byEndpoint[entry.error.endpoint] || 0) + 1;
|
|
523
|
+
}
|
|
524
|
+
// 按阶段统计
|
|
525
|
+
if (entry.error.stage) {
|
|
526
|
+
byStage[entry.error.stage] = (byStage[entry.error.stage] || 0) + 1;
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
return {
|
|
530
|
+
total: this.errorLog.length,
|
|
531
|
+
byType,
|
|
532
|
+
byEndpoint,
|
|
533
|
+
byStage,
|
|
534
|
+
recentErrors: this.errorLog.slice(-10) // 最近10个错误
|
|
535
|
+
};
|
|
536
|
+
}
|
|
537
|
+
/**
|
|
538
|
+
* 清理错误日志
|
|
539
|
+
*/
|
|
540
|
+
static clearErrorLog() {
|
|
541
|
+
this.errorLog = [];
|
|
542
|
+
}
|
|
543
|
+
/**
|
|
544
|
+
* 清理过期错误记录
|
|
545
|
+
*/
|
|
546
|
+
static cleanup(maxAge = 3600000) {
|
|
547
|
+
const cutoff = Date.now() - maxAge;
|
|
548
|
+
this.errorLog = this.errorLog.filter(entry => entry.timestamp >= cutoff);
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
/**
|
|
552
|
+
* 错误恢复工具
|
|
553
|
+
*/
|
|
554
|
+
export class ErrorRecovery {
|
|
555
|
+
static recoveryStrategies = new Map();
|
|
556
|
+
/**
|
|
557
|
+
* 注册恢复策略
|
|
558
|
+
*/
|
|
559
|
+
static registerStrategy(errorType, strategy) {
|
|
560
|
+
this.recoveryStrategies.set(errorType, strategy);
|
|
561
|
+
}
|
|
562
|
+
/**
|
|
563
|
+
* 尝试错误恢复
|
|
564
|
+
*/
|
|
565
|
+
static async attemptRecovery(error, context) {
|
|
566
|
+
const strategy = this.recoveryStrategies.get(error.type);
|
|
567
|
+
if (!strategy) {
|
|
568
|
+
return { success: false, action: 'no_strategy' };
|
|
569
|
+
}
|
|
570
|
+
try {
|
|
571
|
+
return await strategy.recover(error, context);
|
|
572
|
+
}
|
|
573
|
+
catch (recoveryError) {
|
|
574
|
+
return {
|
|
575
|
+
success: false,
|
|
576
|
+
action: 'recovery_failed',
|
|
577
|
+
error: recoveryError instanceof Error ? recoveryError : new Error('Unknown recovery error')
|
|
578
|
+
};
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
/**
|
|
582
|
+
* 获取所有恢复策略
|
|
583
|
+
*/
|
|
584
|
+
static getStrategies() {
|
|
585
|
+
return new Map(this.recoveryStrategies);
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
/**
|
|
589
|
+
* 默认恢复策略
|
|
590
|
+
*/
|
|
591
|
+
export class DefaultRecoveryStrategies {
|
|
592
|
+
/**
|
|
593
|
+
* 网络错误恢复策略
|
|
594
|
+
*/
|
|
595
|
+
static networkErrorStrategy = {
|
|
596
|
+
maxAttempts: 3,
|
|
597
|
+
canRecover: (error) => error.type === LLMSwitchErrorType.NETWORK_ERROR,
|
|
598
|
+
recover: async (error, context) => {
|
|
599
|
+
const attempt = context?.attempt || 1;
|
|
600
|
+
if (attempt > 3) {
|
|
601
|
+
return { success: false, action: 'max_attempts_reached' };
|
|
602
|
+
}
|
|
603
|
+
// 等待一段时间后重试
|
|
604
|
+
await new Promise(resolve => setTimeout(resolve, Math.pow(2, attempt) * 1000));
|
|
605
|
+
return {
|
|
606
|
+
success: true,
|
|
607
|
+
action: 'retry',
|
|
608
|
+
data: { attempt: attempt + 1 }
|
|
609
|
+
};
|
|
610
|
+
}
|
|
611
|
+
};
|
|
612
|
+
/**
|
|
613
|
+
* 超时错误恢复策略
|
|
614
|
+
*/
|
|
615
|
+
static timeoutErrorStrategy = {
|
|
616
|
+
maxAttempts: 2,
|
|
617
|
+
canRecover: (error) => error.type === LLMSwitchErrorType.TIMEOUT_ERROR,
|
|
618
|
+
recover: async (error, context) => {
|
|
619
|
+
const attempt = context?.attempt || 1;
|
|
620
|
+
if (attempt > 2) {
|
|
621
|
+
return { success: false, action: 'max_attempts_reached' };
|
|
622
|
+
}
|
|
623
|
+
// 增加超时时间后重试
|
|
624
|
+
const newTimeout = (context?.timeout || 30000) * 1.5;
|
|
625
|
+
return {
|
|
626
|
+
success: true,
|
|
627
|
+
action: 'retry_with_timeout',
|
|
628
|
+
data: { attempt: attempt + 1, timeout: newTimeout }
|
|
629
|
+
};
|
|
630
|
+
}
|
|
631
|
+
};
|
|
632
|
+
/**
|
|
633
|
+
* 内存错误恢复策略
|
|
634
|
+
*/
|
|
635
|
+
static memoryErrorStrategy = {
|
|
636
|
+
canRecover: (error) => error.type === LLMSwitchErrorType.MEMORY_ERROR,
|
|
637
|
+
recover: async (error, context) => {
|
|
638
|
+
// 强制垃圾回收
|
|
639
|
+
if (global.gc) {
|
|
640
|
+
global.gc();
|
|
641
|
+
}
|
|
642
|
+
// 等待一段时间让内存释放
|
|
643
|
+
await new Promise(resolve => setTimeout(resolve, 1000));
|
|
644
|
+
return {
|
|
645
|
+
success: true,
|
|
646
|
+
action: 'garbage_collection'
|
|
647
|
+
};
|
|
648
|
+
}
|
|
649
|
+
};
|
|
650
|
+
/**
|
|
651
|
+
* 注册所有默认策略
|
|
652
|
+
*/
|
|
653
|
+
static registerAll() {
|
|
654
|
+
ErrorRecovery.registerStrategy(LLMSwitchErrorType.NETWORK_ERROR, this.networkErrorStrategy);
|
|
655
|
+
ErrorRecovery.registerStrategy(LLMSwitchErrorType.TIMEOUT_ERROR, this.timeoutErrorStrategy);
|
|
656
|
+
ErrorRecovery.registerStrategy(LLMSwitchErrorType.MEMORY_ERROR, this.memoryErrorStrategy);
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
// 注册默认恢复策略
|
|
660
|
+
DefaultRecoveryStrategies.registerAll();
|
|
661
|
+
/**
|
|
662
|
+
* 全局错误响应助手类
|
|
663
|
+
*/
|
|
664
|
+
export class ErrorMapper {
|
|
665
|
+
/**
|
|
666
|
+
* 将错误转换为标准HTTP响应
|
|
667
|
+
*/
|
|
668
|
+
static toHTTPResponse(error, requestId) {
|
|
669
|
+
let llmswitchError;
|
|
670
|
+
if (error instanceof LLMSwitchError) {
|
|
671
|
+
llmswitchError = error;
|
|
672
|
+
}
|
|
673
|
+
else {
|
|
674
|
+
llmswitchError = ErrorHandler.handle(error, { requestId });
|
|
675
|
+
}
|
|
676
|
+
const errorResponse = llmswitchError.toErrorResponse();
|
|
677
|
+
return {
|
|
678
|
+
statusCode: llmswitchError.httpStatus,
|
|
679
|
+
body: errorResponse,
|
|
680
|
+
headers: {
|
|
681
|
+
'Content-Type': 'application/json',
|
|
682
|
+
'X-Error-Type': llmswitchError.type,
|
|
683
|
+
'X-Error-Severity': llmswitchError.severity,
|
|
684
|
+
'X-Request-ID': llmswitchError.requestId || requestId || 'unknown'
|
|
685
|
+
}
|
|
686
|
+
};
|
|
687
|
+
}
|
|
688
|
+
/**
|
|
689
|
+
* 发送HTTP错误响应
|
|
690
|
+
*/
|
|
691
|
+
static sendErrorResponse(res, error, requestId) {
|
|
692
|
+
const { statusCode, body, headers } = this.toHTTPResponse(error, requestId);
|
|
693
|
+
// 设置响应头
|
|
694
|
+
Object.entries(headers).forEach(([key, value]) => {
|
|
695
|
+
res.setHeader(key, value);
|
|
696
|
+
});
|
|
697
|
+
res.writeHead(statusCode);
|
|
698
|
+
res.end(JSON.stringify(body));
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
/**
|
|
702
|
+
* 流式错误处理助手
|
|
703
|
+
*/
|
|
704
|
+
export class StreamErrorHandler {
|
|
705
|
+
/**
|
|
706
|
+
* 处理流式过程中的错误
|
|
707
|
+
*/
|
|
708
|
+
static handleStreamError(error, stream, context) {
|
|
709
|
+
const llmswitchError = ErrorHandler.handle(error, context);
|
|
710
|
+
// 尝试写入错误事件到流
|
|
711
|
+
try {
|
|
712
|
+
if (stream && !stream.destroyed) {
|
|
713
|
+
const errorEvent = {
|
|
714
|
+
type: 'error',
|
|
715
|
+
error: llmswitchError.toErrorResponse().error
|
|
716
|
+
};
|
|
717
|
+
stream.write(`data: ${JSON.stringify(errorEvent)}\n\n`);
|
|
718
|
+
// 关闭流
|
|
719
|
+
stream.end();
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
catch (writeError) {
|
|
723
|
+
console.error('Failed to write error to stream:', writeError);
|
|
724
|
+
}
|
|
725
|
+
// 记录错误
|
|
726
|
+
console.error(`Stream error [${context.requestId}]:`, {
|
|
727
|
+
error: llmswitchError,
|
|
728
|
+
context
|
|
729
|
+
});
|
|
730
|
+
}
|
|
731
|
+
/**
|
|
732
|
+
* 安全地包装流式操作
|
|
733
|
+
*/
|
|
734
|
+
static async wrapStreamOperation(operation, stream, context) {
|
|
735
|
+
try {
|
|
736
|
+
return await operation();
|
|
737
|
+
}
|
|
738
|
+
catch (error) {
|
|
739
|
+
this.handleStreamError(error, stream, context);
|
|
740
|
+
throw error; // 重新抛出以供上层处理
|
|
741
|
+
}
|
|
742
|
+
}
|
|
743
|
+
}
|
|
744
|
+
/**
|
|
745
|
+
* 安全JSON解析助手
|
|
746
|
+
*/
|
|
747
|
+
export class SafeJSONParser {
|
|
748
|
+
/**
|
|
749
|
+
* 安全解析JSON,失败时抛出结构化错误
|
|
750
|
+
*/
|
|
751
|
+
static parse(jsonString, context) {
|
|
752
|
+
try {
|
|
753
|
+
return JSON.parse(jsonString);
|
|
754
|
+
}
|
|
755
|
+
catch (error) {
|
|
756
|
+
const errorMessage = context.field
|
|
757
|
+
? `Invalid JSON in field '${context.field}': ${error.message}`
|
|
758
|
+
: `Invalid JSON: ${error.message}`;
|
|
759
|
+
throw ErrorHandler.handle(error, {
|
|
760
|
+
requestId: context.requestId,
|
|
761
|
+
stage: context.stage || 'json_parsing'
|
|
762
|
+
});
|
|
763
|
+
}
|
|
764
|
+
}
|
|
765
|
+
/**
|
|
766
|
+
* 安全字符串化JSON,失败时抛出结构化错误
|
|
767
|
+
*/
|
|
768
|
+
static stringify(obj, context) {
|
|
769
|
+
try {
|
|
770
|
+
return JSON.stringify(obj);
|
|
771
|
+
}
|
|
772
|
+
catch (error) {
|
|
773
|
+
const errorMessage = context.field
|
|
774
|
+
? `Failed to serialize field '${context.field}': ${error.message}`
|
|
775
|
+
: `Failed to serialize object: ${error.message}`;
|
|
776
|
+
throw ErrorHandler.handle(error, {
|
|
777
|
+
requestId: context.requestId,
|
|
778
|
+
stage: context.stage || 'json_stringification'
|
|
779
|
+
});
|
|
780
|
+
}
|
|
781
|
+
}
|
|
782
|
+
}
|