@kaitranntt/ccs 7.71.0 → 7.72.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +39 -2
- package/config/base-codebuddy.settings.json +10 -0
- package/config/base-codex.settings.json +4 -4
- package/config/base-cursor.settings.json +10 -0
- package/config/base-gitlab.settings.json +10 -0
- package/config/base-kilo.settings.json +10 -0
- package/dist/auth/profile-detector.d.ts +2 -3
- package/dist/auth/profile-detector.d.ts.map +1 -1
- package/dist/auth/profile-detector.js +19 -19
- package/dist/auth/profile-detector.js.map +1 -1
- package/dist/ccs.js +84 -16
- package/dist/ccs.js.map +1 -1
- package/dist/cliproxy/accounts/email-account-identity.d.ts.map +1 -1
- package/dist/cliproxy/accounts/email-account-identity.js +22 -2
- package/dist/cliproxy/accounts/email-account-identity.js.map +1 -1
- package/dist/cliproxy/auth/auth-types.d.ts +10 -0
- package/dist/cliproxy/auth/auth-types.d.ts.map +1 -1
- package/dist/cliproxy/auth/auth-types.js +32 -0
- package/dist/cliproxy/auth/auth-types.js.map +1 -1
- package/dist/cliproxy/auth/gitlab-pat-response.d.ts +9 -0
- package/dist/cliproxy/auth/gitlab-pat-response.d.ts.map +1 -0
- package/dist/cliproxy/auth/gitlab-pat-response.js +64 -0
- package/dist/cliproxy/auth/gitlab-pat-response.js.map +1 -0
- package/dist/cliproxy/auth/kiro-import.js +1 -1
- package/dist/cliproxy/auth/kiro-import.js.map +1 -1
- package/dist/cliproxy/auth/oauth-handler.d.ts +3 -0
- package/dist/cliproxy/auth/oauth-handler.d.ts.map +1 -1
- package/dist/cliproxy/auth/oauth-handler.js +120 -4
- package/dist/cliproxy/auth/oauth-handler.js.map +1 -1
- package/dist/cliproxy/auth/provider-refreshers/index.d.ts +1 -2
- package/dist/cliproxy/auth/provider-refreshers/index.d.ts.map +1 -1
- package/dist/cliproxy/auth/provider-refreshers/index.js +6 -25
- package/dist/cliproxy/auth/provider-refreshers/index.js.map +1 -1
- package/dist/cliproxy/auth/token-manager.d.ts +1 -1
- package/dist/cliproxy/auth/token-manager.d.ts.map +1 -1
- package/dist/cliproxy/auth/token-manager.js +9 -6
- package/dist/cliproxy/auth/token-manager.js.map +1 -1
- package/dist/cliproxy/binary/lifecycle.d.ts.map +1 -1
- package/dist/cliproxy/binary/lifecycle.js +12 -1
- package/dist/cliproxy/binary/lifecycle.js.map +1 -1
- package/dist/cliproxy/binary-manager.d.ts +5 -1
- package/dist/cliproxy/binary-manager.d.ts.map +1 -1
- package/dist/cliproxy/binary-manager.js +10 -2
- package/dist/cliproxy/binary-manager.js.map +1 -1
- package/dist/cliproxy/codex-plan-compatibility.d.ts.map +1 -1
- package/dist/cliproxy/codex-plan-compatibility.js +6 -6
- package/dist/cliproxy/codex-plan-compatibility.js.map +1 -1
- package/dist/cliproxy/config/env-builder.d.ts +5 -0
- package/dist/cliproxy/config/env-builder.d.ts.map +1 -1
- package/dist/cliproxy/config/env-builder.js +56 -4
- package/dist/cliproxy/config/env-builder.js.map +1 -1
- package/dist/cliproxy/config/path-resolver.d.ts +21 -0
- package/dist/cliproxy/config/path-resolver.d.ts.map +1 -1
- package/dist/cliproxy/config/path-resolver.js +42 -2
- package/dist/cliproxy/config/path-resolver.js.map +1 -1
- package/dist/cliproxy/executor/index.d.ts +1 -0
- package/dist/cliproxy/executor/index.d.ts.map +1 -1
- package/dist/cliproxy/executor/index.js +40 -7
- package/dist/cliproxy/executor/index.js.map +1 -1
- package/dist/cliproxy/model-catalog.d.ts.map +1 -1
- package/dist/cliproxy/model-catalog.js +14 -59
- package/dist/cliproxy/model-catalog.js.map +1 -1
- package/dist/cliproxy/model-config.js +4 -4
- package/dist/cliproxy/model-config.js.map +1 -1
- package/dist/cliproxy/model-id-normalizer.d.ts +2 -0
- package/dist/cliproxy/model-id-normalizer.d.ts.map +1 -1
- package/dist/cliproxy/model-id-normalizer.js +25 -1
- package/dist/cliproxy/model-id-normalizer.js.map +1 -1
- package/dist/cliproxy/provider-capabilities.d.ts.map +1 -1
- package/dist/cliproxy/provider-capabilities.js +49 -1
- package/dist/cliproxy/provider-capabilities.js.map +1 -1
- package/dist/cliproxy/quota-fetcher-claude-normalizer.d.ts +1 -5
- package/dist/cliproxy/quota-fetcher-claude-normalizer.d.ts.map +1 -1
- package/dist/cliproxy/quota-fetcher-claude-normalizer.js +33 -6
- package/dist/cliproxy/quota-fetcher-claude-normalizer.js.map +1 -1
- package/dist/cliproxy/quota-fetcher-claude.d.ts +2 -2
- package/dist/cliproxy/quota-fetcher-claude.d.ts.map +1 -1
- package/dist/cliproxy/quota-fetcher-claude.js +18 -32
- package/dist/cliproxy/quota-fetcher-claude.js.map +1 -1
- package/dist/cliproxy/quota-fetcher-codex.d.ts.map +1 -1
- package/dist/cliproxy/quota-fetcher-codex.js +2 -0
- package/dist/cliproxy/quota-fetcher-codex.js.map +1 -1
- package/dist/cliproxy/quota-fetcher-gemini-cli.d.ts.map +1 -1
- package/dist/cliproxy/quota-fetcher-gemini-cli.js +221 -99
- package/dist/cliproxy/quota-fetcher-gemini-cli.js.map +1 -1
- package/dist/cliproxy/quota-types.d.ts +2 -2
- package/dist/cliproxy/quota-types.d.ts.map +1 -1
- package/dist/cliproxy/service-manager.d.ts.map +1 -1
- package/dist/cliproxy/service-manager.js +4 -1
- package/dist/cliproxy/service-manager.js.map +1 -1
- package/dist/cliproxy/types.d.ts +11 -3
- package/dist/cliproxy/types.d.ts.map +1 -1
- package/dist/cliproxy/types.js +8 -1
- package/dist/cliproxy/types.js.map +1 -1
- package/dist/commands/browser-command.d.ts +5 -0
- package/dist/commands/browser-command.d.ts.map +1 -0
- package/dist/commands/browser-command.js +118 -0
- package/dist/commands/browser-command.js.map +1 -0
- package/dist/commands/command-catalog.d.ts +3 -2
- package/dist/commands/command-catalog.d.ts.map +1 -1
- package/dist/commands/command-catalog.js +29 -4
- package/dist/commands/command-catalog.js.map +1 -1
- package/dist/commands/completion-backend.d.ts.map +1 -1
- package/dist/commands/completion-backend.js +4 -0
- package/dist/commands/completion-backend.js.map +1 -1
- package/dist/commands/cursor-command-display.d.ts.map +1 -1
- package/dist/commands/cursor-command-display.js +43 -30
- package/dist/commands/cursor-command-display.js.map +1 -1
- package/dist/commands/cursor-command.d.ts.map +1 -1
- package/dist/commands/cursor-command.js +30 -13
- package/dist/commands/cursor-command.js.map +1 -1
- package/dist/commands/help-command.d.ts +2 -0
- package/dist/commands/help-command.d.ts.map +1 -1
- package/dist/commands/help-command.js +62 -3
- package/dist/commands/help-command.js.map +1 -1
- package/dist/commands/index.d.ts +1 -0
- package/dist/commands/index.d.ts.map +1 -1
- package/dist/commands/index.js +3 -1
- package/dist/commands/index.js.map +1 -1
- package/dist/commands/proxy-command.d.ts +2 -0
- package/dist/commands/proxy-command.d.ts.map +1 -0
- package/dist/commands/proxy-command.js +156 -0
- package/dist/commands/proxy-command.js.map +1 -0
- package/dist/commands/root-command-router.d.ts.map +1 -1
- package/dist/commands/root-command-router.js +14 -0
- package/dist/commands/root-command-router.js.map +1 -1
- package/dist/config/reserved-names.d.ts +1 -1
- package/dist/config/reserved-names.d.ts.map +1 -1
- package/dist/config/reserved-names.js +10 -0
- package/dist/config/reserved-names.js.map +1 -1
- package/dist/config/unified-config-loader.d.ts +6 -1
- package/dist/config/unified-config-loader.d.ts.map +1 -1
- package/dist/config/unified-config-loader.js +67 -1
- package/dist/config/unified-config-loader.js.map +1 -1
- package/dist/config/unified-config-types.d.ts +37 -0
- package/dist/config/unified-config-types.d.ts.map +1 -1
- package/dist/config/unified-config-types.js +25 -1
- package/dist/config/unified-config-types.js.map +1 -1
- package/dist/cursor/constants.d.ts +3 -0
- package/dist/cursor/constants.d.ts.map +1 -1
- package/dist/cursor/constants.js +26 -1
- package/dist/cursor/constants.js.map +1 -1
- package/dist/cursor/cursor-anthropic-response.d.ts +1 -6
- package/dist/cursor/cursor-anthropic-response.d.ts.map +1 -1
- package/dist/cursor/cursor-anthropic-response.js +3 -186
- package/dist/cursor/cursor-anthropic-response.js.map +1 -1
- package/dist/cursor/cursor-daemon-entry.js +2 -2
- package/dist/cursor/cursor-daemon-entry.js.map +1 -1
- package/dist/cursor/cursor-profile-executor.js +4 -4
- package/dist/cursor/cursor-profile-executor.js.map +1 -1
- package/dist/cursor/cursor-runtime-probe.js +3 -3
- package/dist/cursor/cursor-runtime-probe.js.map +1 -1
- package/dist/glmt/sse-parser.d.ts.map +1 -1
- package/dist/glmt/sse-parser.js +46 -41
- package/dist/glmt/sse-parser.js.map +1 -1
- package/dist/management/shared-manager.d.ts +2 -0
- package/dist/management/shared-manager.d.ts.map +1 -1
- package/dist/management/shared-manager.js +25 -10
- package/dist/management/shared-manager.js.map +1 -1
- package/dist/proxy/index.d.ts +8 -0
- package/dist/proxy/index.d.ts.map +1 -0
- package/dist/proxy/index.js +24 -0
- package/dist/proxy/index.js.map +1 -0
- package/dist/proxy/profile-router.d.ts +28 -0
- package/dist/proxy/profile-router.d.ts.map +1 -0
- package/dist/proxy/profile-router.js +43 -0
- package/dist/proxy/profile-router.js.map +1 -0
- package/dist/proxy/proxy-daemon-entry.d.ts +2 -0
- package/dist/proxy/proxy-daemon-entry.d.ts.map +1 -0
- package/dist/proxy/proxy-daemon-entry.js +68 -0
- package/dist/proxy/proxy-daemon-entry.js.map +1 -0
- package/dist/proxy/proxy-daemon-paths.d.ts +6 -0
- package/dist/proxy/proxy-daemon-paths.d.ts.map +1 -0
- package/dist/proxy/proxy-daemon-paths.js +43 -0
- package/dist/proxy/proxy-daemon-paths.js.map +1 -0
- package/dist/proxy/proxy-daemon-state.d.ts +18 -0
- package/dist/proxy/proxy-daemon-state.d.ts.map +1 -0
- package/dist/proxy/proxy-daemon-state.js +92 -0
- package/dist/proxy/proxy-daemon-state.js.map +1 -0
- package/dist/proxy/proxy-daemon.d.ts +26 -0
- package/dist/proxy/proxy-daemon.d.ts.map +1 -0
- package/dist/proxy/proxy-daemon.js +350 -0
- package/dist/proxy/proxy-daemon.js.map +1 -0
- package/dist/proxy/proxy-env.d.ts +3 -0
- package/dist/proxy/proxy-env.d.ts.map +1 -0
- package/dist/proxy/proxy-env.js +26 -0
- package/dist/proxy/proxy-env.js.map +1 -0
- package/dist/proxy/request-router.d.ts +12 -0
- package/dist/proxy/request-router.d.ts.map +1 -0
- package/dist/proxy/request-router.js +163 -0
- package/dist/proxy/request-router.js.map +1 -0
- package/dist/proxy/routing-config.d.ts +10 -0
- package/dist/proxy/routing-config.d.ts.map +1 -0
- package/dist/proxy/routing-config.js +79 -0
- package/dist/proxy/routing-config.js.map +1 -0
- package/dist/proxy/server/http-helpers.d.ts +8 -0
- package/dist/proxy/server/http-helpers.d.ts.map +1 -0
- package/dist/proxy/server/http-helpers.js +73 -0
- package/dist/proxy/server/http-helpers.js.map +1 -0
- package/dist/proxy/server/messages-route.d.ts +12 -0
- package/dist/proxy/server/messages-route.d.ts.map +1 -0
- package/dist/proxy/server/messages-route.js +208 -0
- package/dist/proxy/server/messages-route.js.map +1 -0
- package/dist/proxy/server/proxy-server.d.ts +12 -0
- package/dist/proxy/server/proxy-server.d.ts.map +1 -0
- package/dist/proxy/server/proxy-server.js +110 -0
- package/dist/proxy/server/proxy-server.js.map +1 -0
- package/dist/proxy/transformers/request-transformer.d.ts +52 -0
- package/dist/proxy/transformers/request-transformer.d.ts.map +1 -0
- package/dist/proxy/transformers/request-transformer.js +254 -0
- package/dist/proxy/transformers/request-transformer.js.map +1 -0
- package/dist/proxy/transformers/sse-stream-transformer.d.ts +11 -0
- package/dist/proxy/transformers/sse-stream-transformer.d.ts.map +1 -0
- package/dist/proxy/transformers/sse-stream-transformer.js +199 -0
- package/dist/proxy/transformers/sse-stream-transformer.js.map +1 -0
- package/dist/proxy/upstream-url.d.ts +3 -0
- package/dist/proxy/upstream-url.d.ts.map +1 -0
- package/dist/proxy/upstream-url.js +35 -0
- package/dist/proxy/upstream-url.js.map +1 -0
- package/dist/shared/claude-extension-setup.js +1 -1
- package/dist/shared/claude-extension-setup.js.map +1 -1
- package/dist/targets/codex-detector.d.ts.map +1 -1
- package/dist/targets/codex-detector.js +17 -8
- package/dist/targets/codex-detector.js.map +1 -1
- package/dist/targets/droid-provider.d.ts.map +1 -1
- package/dist/targets/droid-provider.js +2 -0
- package/dist/targets/droid-provider.js.map +1 -1
- package/dist/types/config.d.ts +13 -0
- package/dist/types/config.d.ts.map +1 -1
- package/dist/types/config.js.map +1 -1
- package/dist/types/index.d.ts +1 -1
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/index.js.map +1 -1
- package/dist/ui/assets/{accounts-B8EmUjf7.js → accounts-DfkqFx14.js} +1 -1
- package/dist/ui/assets/{alert-dialog-zvnFuSZG.js → alert-dialog-BJ9GwHLZ.js} +1 -1
- package/dist/ui/assets/{api-vTHvDFg5.js → api-BVo8Vm1B.js} +1 -1
- package/dist/ui/assets/{auth-section-Dg3AdWQR.js → auth-section-B-Ms1O_M.js} +1 -1
- package/dist/ui/assets/{backups-section-CuRlCk7U.js → backups-section-f8hvkPfH.js} +1 -1
- package/dist/ui/assets/{channels-ByIXpMal.js → channels-Bgvry5fT.js} +1 -1
- package/dist/ui/assets/{checkbox-DoV2JinM.js → checkbox-CbI3Vbww.js} +1 -1
- package/dist/ui/assets/{claude-extension-B-Ofp87Z.js → claude-extension-lDT7lp8C.js} +1 -1
- package/dist/ui/assets/cliproxy-D50Owb9u.js +3 -0
- package/dist/ui/assets/{cliproxy-ai-providers-BVOtMT7b.js → cliproxy-ai-providers-Cjrk90aJ.js} +8 -8
- package/dist/ui/assets/{cliproxy-control-panel-qL0R878z.js → cliproxy-control-panel-DL8JoyKU.js} +1 -1
- package/dist/ui/assets/codex-B3bW7lzx.js +27 -0
- package/dist/ui/assets/{confirm-dialog-BcfzKHj3.js → confirm-dialog-Bm2Y3ArL.js} +1 -1
- package/dist/ui/assets/{copilot-Dti40Uv-.js → copilot-CPLBRpFA.js} +1 -1
- package/dist/ui/assets/cursor-D1xhqEEF.js +1 -0
- package/dist/ui/assets/{droid-BmuIeDFh.js → droid-DiFtSJkl.js} +2 -2
- package/dist/ui/assets/{globalenv-section-CGbpkbpW.js → globalenv-section-BadlR8mm.js} +1 -1
- package/dist/ui/assets/{health-DryU1jzv.js → health-Bxol1p1X.js} +1 -1
- package/dist/ui/assets/{icons-KVCk4_U8.js → icons-Dmi9qMuE.js} +1 -1
- package/dist/ui/assets/{index-Dgu-LYP_.js → index-BPCsEKJ8.js} +1 -1
- package/dist/ui/assets/{index-B_vdNMI1.js → index-BZYBDV6A.js} +1 -1
- package/dist/ui/assets/index-C2-J9AKi.js +1 -0
- package/dist/ui/assets/{index-DUCkRc_D.js → index-CA4r5qY8.js} +1 -1
- package/dist/ui/assets/index-CXRoMyUA.js +9 -0
- package/dist/ui/assets/index-DM7Qb4kd.js +72 -0
- package/dist/ui/assets/index-DuTB1_9r.css +1 -0
- package/dist/ui/assets/{index-Cu--MLG0.js → index-x07V45Iw.js} +1 -1
- package/dist/ui/assets/{logs-CtIgaS7y.js → logs-kvpMF-pw.js} +1 -1
- package/dist/ui/assets/{masked-input-Dm8T-Vtr.js → masked-input-CFQ05toP.js} +1 -1
- package/dist/ui/assets/providers/codebuddy.png +0 -0
- package/dist/ui/assets/providers/cursor.svg +5 -0
- package/dist/ui/assets/providers/gitlab.svg +22 -0
- package/dist/ui/assets/providers/kilo.png +0 -0
- package/dist/ui/assets/{proxy-status-widget-BXghaDz7.js → proxy-status-widget-Df_-wCR0.js} +1 -1
- package/dist/ui/assets/{raw-json-settings-editor-panel-DhhcWn5Q.js → raw-json-settings-editor-panel-DeWgWDSP.js} +1 -1
- package/dist/ui/assets/{searchable-select-DiGUiefs.js → searchable-select-CPbVBSoE.js} +1 -1
- package/dist/ui/assets/{separator-DnQqlaL0.js → separator--T-CzItG.js} +1 -1
- package/dist/ui/assets/{shared-BPuiJsYO.js → shared-Dx_5pa5N.js} +1 -1
- package/dist/ui/assets/{table-BDsTJlxT.js → table-wE_LUUUQ.js} +1 -1
- package/dist/ui/assets/{updates-DQxYQBCf.js → updates-mWDqf-fm.js} +1 -1
- package/dist/ui/index.html +3 -3
- package/dist/utils/browser/browser-settings.d.ts +21 -0
- package/dist/utils/browser/browser-settings.d.ts.map +1 -0
- package/dist/utils/browser/browser-settings.js +101 -0
- package/dist/utils/browser/browser-settings.js.map +1 -0
- package/dist/utils/browser/browser-status.d.ts +39 -0
- package/dist/utils/browser/browser-status.d.ts.map +1 -0
- package/dist/utils/browser/browser-status.js +134 -0
- package/dist/utils/browser/browser-status.js.map +1 -0
- package/dist/utils/browser/index.d.ts +3 -0
- package/dist/utils/browser/index.d.ts.map +1 -1
- package/dist/utils/browser/index.js +7 -1
- package/dist/utils/browser/index.js.map +1 -1
- package/dist/utils/browser/platform.d.ts +4 -0
- package/dist/utils/browser/platform.d.ts.map +1 -0
- package/dist/utils/browser/platform.js +12 -0
- package/dist/utils/browser/platform.js.map +1 -0
- package/dist/utils/hooks/image-analysis-backend-resolver.js +1 -1
- package/dist/utils/hooks/image-analysis-backend-resolver.js.map +1 -1
- package/dist/web-server/routes/browser-routes.d.ts +3 -0
- package/dist/web-server/routes/browser-routes.d.ts.map +1 -0
- package/dist/web-server/routes/browser-routes.js +116 -0
- package/dist/web-server/routes/browser-routes.js.map +1 -0
- package/dist/web-server/routes/cliproxy-auth-routes.d.ts.map +1 -1
- package/dist/web-server/routes/cliproxy-auth-routes.js +124 -1
- package/dist/web-server/routes/cliproxy-auth-routes.js.map +1 -1
- package/dist/web-server/routes/config-routes.d.ts.map +1 -1
- package/dist/web-server/routes/config-routes.js +3 -0
- package/dist/web-server/routes/config-routes.js.map +1 -1
- package/dist/web-server/routes/index.d.ts.map +1 -1
- package/dist/web-server/routes/index.js +3 -0
- package/dist/web-server/routes/index.js.map +1 -1
- package/dist/web-server/routes/settings-routes.d.ts.map +1 -1
- package/dist/web-server/routes/settings-routes.js +8 -0
- package/dist/web-server/routes/settings-routes.js.map +1 -1
- package/dist/web-server/services/compatible-cli-docs-registry.d.ts.map +1 -1
- package/dist/web-server/services/compatible-cli-docs-registry.js +1 -0
- package/dist/web-server/services/compatible-cli-docs-registry.js.map +1 -1
- package/package.json +1 -1
- package/dist/cliproxy/auth/gemini-token-refresh.d.ts +0 -40
- package/dist/cliproxy/auth/gemini-token-refresh.d.ts.map +0 -1
- package/dist/cliproxy/auth/gemini-token-refresh.js +0 -371
- package/dist/cliproxy/auth/gemini-token-refresh.js.map +0 -1
- package/dist/ui/assets/cliproxy-Bhu0JvIb.js +0 -3
- package/dist/ui/assets/codex-BCHZ-0dx.js +0 -27
- package/dist/ui/assets/cursor-BG0Y8Z5F.js +0 -1
- package/dist/ui/assets/index-B1mN7ZR-.js +0 -72
- package/dist/ui/assets/index-BBVoDJnl.js +0 -1
- package/dist/ui/assets/index-BMC5Tudw.css +0 -1
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.startOpenAICompatProxyServer = void 0;
|
|
27
|
+
const http = __importStar(require("http"));
|
|
28
|
+
const undici_1 = require("undici");
|
|
29
|
+
const proxy_daemon_paths_1 = require("../proxy-daemon-paths");
|
|
30
|
+
const logging_1 = require("../../services/logging");
|
|
31
|
+
const messages_route_1 = require("./messages-route");
|
|
32
|
+
const http_helpers_1 = require("./http-helpers");
|
|
33
|
+
function startOpenAICompatProxyServer(options) {
|
|
34
|
+
const host = options.host?.trim() || '127.0.0.1';
|
|
35
|
+
const logger = (0, logging_1.createLogger)('proxy:openai-compat', {
|
|
36
|
+
profileName: options.profile.profileName,
|
|
37
|
+
host,
|
|
38
|
+
port: options.port,
|
|
39
|
+
});
|
|
40
|
+
const insecureDispatcher = options.insecure
|
|
41
|
+
? new undici_1.Agent({ connect: { rejectUnauthorized: false } })
|
|
42
|
+
: undefined;
|
|
43
|
+
const server = http.createServer(async (req, res) => {
|
|
44
|
+
const method = req.method || 'GET';
|
|
45
|
+
const requestUrl = req.url || '/';
|
|
46
|
+
const parsedUrl = new URL(requestUrl, 'http://127.0.0.1');
|
|
47
|
+
const pathname = parsedUrl.pathname.length > 1 ? parsedUrl.pathname.replace(/\/+$/, '') : parsedUrl.pathname;
|
|
48
|
+
if (method === 'GET' && pathname === '/health') {
|
|
49
|
+
(0, http_helpers_1.writeJson)(res, 200, {
|
|
50
|
+
ok: true,
|
|
51
|
+
service: proxy_daemon_paths_1.OPENAI_COMPAT_PROXY_SERVICE_NAME,
|
|
52
|
+
host,
|
|
53
|
+
profile: options.profile.profileName,
|
|
54
|
+
port: options.port,
|
|
55
|
+
});
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
if (method === 'GET' && pathname === '/') {
|
|
59
|
+
(0, http_helpers_1.writeJson)(res, 200, {
|
|
60
|
+
ok: true,
|
|
61
|
+
service: proxy_daemon_paths_1.OPENAI_COMPAT_PROXY_SERVICE_NAME,
|
|
62
|
+
bind: {
|
|
63
|
+
host,
|
|
64
|
+
port: options.port,
|
|
65
|
+
},
|
|
66
|
+
profile: {
|
|
67
|
+
name: options.profile.profileName,
|
|
68
|
+
provider: options.profile.provider,
|
|
69
|
+
model: options.profile.model || null,
|
|
70
|
+
},
|
|
71
|
+
endpoints: ['/health', '/v1/messages', '/v1/models'],
|
|
72
|
+
});
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
if (method === 'GET' && pathname === '/v1/models') {
|
|
76
|
+
if (!(0, messages_route_1.validateIncomingProxyAuth)(req.headers, options.authToken)) {
|
|
77
|
+
(0, http_helpers_1.writeJson)(res, 401, {
|
|
78
|
+
type: 'error',
|
|
79
|
+
error: {
|
|
80
|
+
type: 'authentication_error',
|
|
81
|
+
message: 'Missing or invalid local proxy token',
|
|
82
|
+
},
|
|
83
|
+
});
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
(0, messages_route_1.handleProxyModelsRequest)(res, options.profile);
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
if (method === 'POST' && pathname === '/v1/messages') {
|
|
90
|
+
await (0, messages_route_1.handleProxyMessagesRequest)(req, res, options.profile, options.authToken, insecureDispatcher);
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
logger.warn('http.not_found', 'Rejected unknown proxy route', {
|
|
94
|
+
method,
|
|
95
|
+
pathname,
|
|
96
|
+
});
|
|
97
|
+
(0, http_helpers_1.writeJson)(res, 404, { error: 'Not found' });
|
|
98
|
+
});
|
|
99
|
+
logger.info('server.start', 'OpenAI-compatible proxy server listening', {
|
|
100
|
+
baseUrl: `http://${host}:${options.port}`,
|
|
101
|
+
});
|
|
102
|
+
server.on('close', () => {
|
|
103
|
+
logger.info('server.stop', 'OpenAI-compatible proxy server stopped');
|
|
104
|
+
void insecureDispatcher?.close();
|
|
105
|
+
});
|
|
106
|
+
server.listen(options.port, host);
|
|
107
|
+
return server;
|
|
108
|
+
}
|
|
109
|
+
exports.startOpenAICompatProxyServer = startOpenAICompatProxyServer;
|
|
110
|
+
//# sourceMappingURL=proxy-server.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"proxy-server.js","sourceRoot":"","sources":["../../../src/proxy/server/proxy-server.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,2CAA6B;AAC7B,mCAA+B;AAE/B,8DAAyE;AACzE,oDAAsD;AACtD,qDAI0B;AAC1B,iDAA2C;AAU3C,SAAgB,4BAA4B,CAAC,OAAuC;IAClF,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,WAAW,CAAC;IACjD,MAAM,MAAM,GAAG,IAAA,sBAAY,EAAC,qBAAqB,EAAE;QACjD,WAAW,EAAE,OAAO,CAAC,OAAO,CAAC,WAAW;QACxC,IAAI;QACJ,IAAI,EAAE,OAAO,CAAC,IAAI;KACnB,CAAC,CAAC;IACH,MAAM,kBAAkB,GAAG,OAAO,CAAC,QAAQ;QACzC,CAAC,CAAC,IAAI,cAAK,CAAC,EAAE,OAAO,EAAE,EAAE,kBAAkB,EAAE,KAAK,EAAE,EAAE,CAAC;QACvD,CAAC,CAAC,SAAS,CAAC;IACd,MAAM,MAAM,GAAG,IAAI,CAAC,YAAY,CAAC,KAAK,EAAE,GAAG,EAAE,GAAG,EAAE,EAAE;QAClD,MAAM,MAAM,GAAG,GAAG,CAAC,MAAM,IAAI,KAAK,CAAC;QACnC,MAAM,UAAU,GAAG,GAAG,CAAC,GAAG,IAAI,GAAG,CAAC;QAClC,MAAM,SAAS,GAAG,IAAI,GAAG,CAAC,UAAU,EAAE,kBAAkB,CAAC,CAAC;QAC1D,MAAM,QAAQ,GACZ,SAAS,CAAC,QAAQ,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,QAAQ,CAA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
|
+
}
|
|
11
|
+
type OpenAIContentPart = OpenAITextPart | OpenAIImagePart;
|
|
12
|
+
interface OpenAIMessage {
|
|
13
|
+
role: 'system' | 'user' | 'assistant' | 'tool';
|
|
14
|
+
content: string | OpenAIContentPart[] | null;
|
|
15
|
+
tool_call_id?: string;
|
|
16
|
+
tool_calls?: Array<{
|
|
17
|
+
id: string;
|
|
18
|
+
type: 'function';
|
|
19
|
+
function: {
|
|
20
|
+
name: string;
|
|
21
|
+
arguments: string;
|
|
22
|
+
};
|
|
23
|
+
}>;
|
|
24
|
+
}
|
|
25
|
+
export interface ProxyOpenAIRequest {
|
|
26
|
+
model?: string;
|
|
27
|
+
stream: boolean;
|
|
28
|
+
reasoning_effort?: string;
|
|
29
|
+
reasoning?: {
|
|
30
|
+
enabled: boolean;
|
|
31
|
+
effort: string;
|
|
32
|
+
};
|
|
33
|
+
tools?: Array<{
|
|
34
|
+
type: 'function';
|
|
35
|
+
function: {
|
|
36
|
+
name: string;
|
|
37
|
+
description?: string;
|
|
38
|
+
parameters: Record<string, unknown>;
|
|
39
|
+
};
|
|
40
|
+
}>;
|
|
41
|
+
messages: OpenAIMessage[];
|
|
42
|
+
max_tokens?: number;
|
|
43
|
+
temperature?: number;
|
|
44
|
+
top_p?: number;
|
|
45
|
+
stop?: string[];
|
|
46
|
+
metadata?: Record<string, unknown>;
|
|
47
|
+
}
|
|
48
|
+
export declare class ProxyRequestTransformer {
|
|
49
|
+
transform(raw: unknown): ProxyOpenAIRequest;
|
|
50
|
+
}
|
|
51
|
+
export {};
|
|
52
|
+
//# sourceMappingURL=request-transformer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"request-transformer.d.ts","sourceRoot":"","sources":["../../../src/proxy/transformers/request-transformer.ts"],"names":[],"mappings":"AA0DA,UAAU,cAAc;IACtB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;CACd;AAED,UAAU,eAAe;IACvB,IAAI,EAA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AAC;CACpC;AAiSD,qBAAa,uBAAuB;IAClC,SAAS,CAAC,GAAG,EAAE,OAAO,GAAG,kBAAkB;CA4B5C"}
|
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ProxyRequestTransformer = void 0;
|
|
4
|
+
const TOOL_RESULT_SERIALIZATION_FALLBACK = '[unserializable content]';
|
|
5
|
+
const TOOL_USE_ARGUMENTS_FALLBACK = '{}';
|
|
6
|
+
function assertObject(value, label) {
|
|
7
|
+
if (typeof value !== 'object' || value === null) {
|
|
8
|
+
throw new Error(`${label} must be an object`);
|
|
9
|
+
}
|
|
10
|
+
return value;
|
|
11
|
+
}
|
|
12
|
+
function asNumber(value) {
|
|
13
|
+
return typeof value === 'number' && Number.isFinite(value) ? value : undefined;
|
|
14
|
+
}
|
|
15
|
+
function asStringArray(value) {
|
|
16
|
+
if (!Array.isArray(value)) {
|
|
17
|
+
return undefined;
|
|
18
|
+
}
|
|
19
|
+
const result = value.filter((entry) => typeof entry === 'string' && entry.length > 0);
|
|
20
|
+
return result.length > 0 ? result : undefined;
|
|
21
|
+
}
|
|
22
|
+
function asMetadata(value) {
|
|
23
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value)
|
|
24
|
+
? value
|
|
25
|
+
: undefined;
|
|
26
|
+
}
|
|
27
|
+
function safeJsonStringify(value, fallback) {
|
|
28
|
+
try {
|
|
29
|
+
const serialized = JSON.stringify(value);
|
|
30
|
+
return typeof serialized === 'string' ? serialized : fallback;
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
return fallback;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
function flattenTextContent(content, label) {
|
|
37
|
+
if (typeof content === 'string') {
|
|
38
|
+
return content;
|
|
39
|
+
}
|
|
40
|
+
if (!Array.isArray(content)) {
|
|
41
|
+
throw new Error(`${label} must be a string or content block array`);
|
|
42
|
+
}
|
|
43
|
+
return content
|
|
44
|
+
.map((block, index) => {
|
|
45
|
+
const parsed = assertObject(block, `${label}[${index}]`);
|
|
46
|
+
if (parsed.type !== 'text') {
|
|
47
|
+
throw new Error(`${label}[${index}].type "${String(parsed.type)}" is not supported`);
|
|
48
|
+
}
|
|
49
|
+
return typeof parsed.text === 'string' ? parsed.text : '';
|
|
50
|
+
})
|
|
51
|
+
.join('\n');
|
|
52
|
+
}
|
|
53
|
+
function toToolResultContent(content, label) {
|
|
54
|
+
if (content === undefined) {
|
|
55
|
+
return '';
|
|
56
|
+
}
|
|
57
|
+
if (typeof content === 'string') {
|
|
58
|
+
return content;
|
|
59
|
+
}
|
|
60
|
+
if (Array.isArray(content)) {
|
|
61
|
+
return flattenTextContent(content, label);
|
|
62
|
+
}
|
|
63
|
+
return safeJsonStringify(content, TOOL_RESULT_SERIALIZATION_FALLBACK);
|
|
64
|
+
}
|
|
65
|
+
function createFallbackToolId(messageIndex, blockIndex) {
|
|
66
|
+
return `toolu_proxy_fallback_${messageIndex}_${blockIndex}`;
|
|
67
|
+
}
|
|
68
|
+
function toImagePart(block, label) {
|
|
69
|
+
const source = block.source;
|
|
70
|
+
if (!source || source.type !== 'base64' || !source.media_type || !source.data) {
|
|
71
|
+
throw new Error(`${label}.source must be a base64 image payload`);
|
|
72
|
+
}
|
|
73
|
+
return {
|
|
74
|
+
type: 'image_url',
|
|
75
|
+
image_url: {
|
|
76
|
+
url: `data:${source.media_type};base64,${source.data}`,
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
function isImageBlock(block) {
|
|
81
|
+
return block.type === 'image';
|
|
82
|
+
}
|
|
83
|
+
function isToolUseBlock(block) {
|
|
84
|
+
return block.type === 'tool_use';
|
|
85
|
+
}
|
|
86
|
+
function isToolResultBlock(block) {
|
|
87
|
+
return block.type === 'tool_result';
|
|
88
|
+
}
|
|
89
|
+
function flushUserContent(messages, parts) {
|
|
90
|
+
if (parts.length === 0) {
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
const onlyText = parts.every((part) => part.type === 'text');
|
|
94
|
+
messages.push({
|
|
95
|
+
role: 'user',
|
|
96
|
+
content: onlyText ? parts.map((part) => part.text).join('\n') : [...parts],
|
|
97
|
+
});
|
|
98
|
+
parts.length = 0;
|
|
99
|
+
}
|
|
100
|
+
function transformTools(value) {
|
|
101
|
+
if (!Array.isArray(value)) {
|
|
102
|
+
return undefined;
|
|
103
|
+
}
|
|
104
|
+
const tools = value
|
|
105
|
+
.filter((entry) => typeof entry === 'object' && entry !== null)
|
|
106
|
+
.map((entry) => ({
|
|
107
|
+
type: 'function',
|
|
108
|
+
function: {
|
|
109
|
+
name: typeof entry.name === 'string' ? entry.name : 'tool',
|
|
110
|
+
...(typeof entry.description === 'string' ? { description: entry.description } : {}),
|
|
111
|
+
parameters: typeof entry.input_schema === 'object' && entry.input_schema !== null
|
|
112
|
+
? entry.input_schema
|
|
113
|
+
: { type: 'object', properties: {} },
|
|
114
|
+
},
|
|
115
|
+
}));
|
|
116
|
+
return tools.length > 0 ? tools : undefined;
|
|
117
|
+
}
|
|
118
|
+
function mapThinkingToReasoning(thinking) {
|
|
119
|
+
if (!thinking || thinking.type === 'disabled') {
|
|
120
|
+
return {};
|
|
121
|
+
}
|
|
122
|
+
if (thinking.type !== 'enabled') {
|
|
123
|
+
throw new Error('thinking.type must be "enabled" or "disabled"');
|
|
124
|
+
}
|
|
125
|
+
const effort = typeof thinking.budget_tokens === 'number' && thinking.budget_tokens >= 8192
|
|
126
|
+
? 'high'
|
|
127
|
+
: 'medium';
|
|
128
|
+
return {
|
|
129
|
+
reasoning_effort: effort,
|
|
130
|
+
reasoning: {
|
|
131
|
+
enabled: true,
|
|
132
|
+
effort,
|
|
133
|
+
},
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
function transformMessages(messagesValue) {
|
|
137
|
+
if (!Array.isArray(messagesValue)) {
|
|
138
|
+
throw new Error('messages must be an array');
|
|
139
|
+
}
|
|
140
|
+
const translatedMessages = [];
|
|
141
|
+
messagesValue.forEach((message, messageIndex) => {
|
|
142
|
+
const parsedMessage = assertObject(message, `messages[${messageIndex}]`);
|
|
143
|
+
const role = parsedMessage.role;
|
|
144
|
+
if (role !== 'user' && role !== 'assistant') {
|
|
145
|
+
throw new Error(`messages[${messageIndex}].role must be "user" or "assistant"`);
|
|
146
|
+
}
|
|
147
|
+
const content = parsedMessage.content;
|
|
148
|
+
if (typeof content === 'string') {
|
|
149
|
+
translatedMessages.push({ role, content });
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
if (!Array.isArray(content)) {
|
|
153
|
+
throw new Error(`messages[${messageIndex}].content must be a string or array`);
|
|
154
|
+
}
|
|
155
|
+
const userParts = [];
|
|
156
|
+
const assistantTextParts = [];
|
|
157
|
+
const toolCalls = [];
|
|
158
|
+
let sawToolResult = false;
|
|
159
|
+
content.forEach((block, blockIndex) => {
|
|
160
|
+
const parsed = assertObject(block, `messages[${messageIndex}].content[${blockIndex}]`);
|
|
161
|
+
if (parsed.type === 'text') {
|
|
162
|
+
const text = typeof parsed.text === 'string' ? parsed.text : '';
|
|
163
|
+
if (role === 'user') {
|
|
164
|
+
userParts.push({ type: 'text', text });
|
|
165
|
+
}
|
|
166
|
+
else {
|
|
167
|
+
assistantTextParts.push(text);
|
|
168
|
+
}
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
if (isImageBlock(parsed)) {
|
|
172
|
+
if (role !== 'user') {
|
|
173
|
+
throw new Error(`messages[${messageIndex}].content[${blockIndex}] image requires user role`);
|
|
174
|
+
}
|
|
175
|
+
userParts.push(toImagePart(parsed, `messages[${messageIndex}].content[${blockIndex}]`));
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
if (isToolUseBlock(parsed)) {
|
|
179
|
+
if (role !== 'assistant') {
|
|
180
|
+
throw new Error(`messages[${messageIndex}].content[${blockIndex}] tool_use requires assistant role`);
|
|
181
|
+
}
|
|
182
|
+
toolCalls.push({
|
|
183
|
+
id: typeof parsed.id === 'string' && parsed.id.length > 0
|
|
184
|
+
? parsed.id
|
|
185
|
+
: createFallbackToolId(messageIndex, blockIndex),
|
|
186
|
+
type: 'function',
|
|
187
|
+
function: {
|
|
188
|
+
name: typeof parsed.name === 'string' ? parsed.name : 'tool',
|
|
189
|
+
arguments: safeJsonStringify(parsed.input ?? {}, TOOL_USE_ARGUMENTS_FALLBACK),
|
|
190
|
+
},
|
|
191
|
+
});
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
if (isToolResultBlock(parsed)) {
|
|
195
|
+
if (role !== 'user') {
|
|
196
|
+
throw new Error(`messages[${messageIndex}].content[${blockIndex}] tool_result requires user role`);
|
|
197
|
+
}
|
|
198
|
+
if (typeof parsed.tool_use_id !== 'string' || parsed.tool_use_id.trim().length === 0) {
|
|
199
|
+
throw new Error(`messages[${messageIndex}].content[${blockIndex}].tool_use_id must be a non-empty string`);
|
|
200
|
+
}
|
|
201
|
+
sawToolResult = true;
|
|
202
|
+
flushUserContent(translatedMessages, userParts);
|
|
203
|
+
translatedMessages.push({
|
|
204
|
+
role: 'tool',
|
|
205
|
+
tool_call_id: parsed.tool_use_id,
|
|
206
|
+
content: toToolResultContent(parsed.content, `messages[${messageIndex}].content[${blockIndex}].content`),
|
|
207
|
+
});
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
throw new Error(`messages[${messageIndex}].content[${blockIndex}].type "${String(parsed.type)}" is not supported`);
|
|
211
|
+
});
|
|
212
|
+
if (role === 'assistant') {
|
|
213
|
+
translatedMessages.push({
|
|
214
|
+
role: 'assistant',
|
|
215
|
+
content: assistantTextParts.join('\n'),
|
|
216
|
+
tool_calls: toolCalls.length > 0 ? toolCalls : undefined,
|
|
217
|
+
});
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
if (userParts.length > 0 || !sawToolResult) {
|
|
221
|
+
flushUserContent(translatedMessages, userParts);
|
|
222
|
+
}
|
|
223
|
+
});
|
|
224
|
+
return translatedMessages;
|
|
225
|
+
}
|
|
226
|
+
class ProxyRequestTransformer {
|
|
227
|
+
transform(raw) {
|
|
228
|
+
const source = assertObject(raw || {}, 'request');
|
|
229
|
+
const messages = transformMessages(source.messages);
|
|
230
|
+
const system = source.system;
|
|
231
|
+
const allMessages = system !== undefined
|
|
232
|
+
? [
|
|
233
|
+
{ role: 'system', content: flattenTextContent(system, 'system') },
|
|
234
|
+
...messages,
|
|
235
|
+
]
|
|
236
|
+
: messages;
|
|
237
|
+
return {
|
|
238
|
+
model: typeof source.model === 'string' && source.model.trim().length > 0
|
|
239
|
+
? source.model.trim()
|
|
240
|
+
: undefined,
|
|
241
|
+
stream: source.stream === true,
|
|
242
|
+
messages: allMessages,
|
|
243
|
+
max_tokens: asNumber(source.max_tokens),
|
|
244
|
+
temperature: asNumber(source.temperature),
|
|
245
|
+
top_p: asNumber(source.top_p),
|
|
246
|
+
stop: asStringArray(source.stop_sequences),
|
|
247
|
+
metadata: asMetadata(source.metadata),
|
|
248
|
+
tools: transformTools(source.tools),
|
|
249
|
+
...mapThinkingToReasoning(source.thinking),
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
exports.ProxyRequestTransformer = ProxyRequestTransformer;
|
|
254
|
+
//# sourceMappingURL=request-transformer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"request-transformer.js","sourceRoot":"","sources":["../../../src/proxy/transformers/request-transformer.ts"],"names":[],"mappings":";;;AA8GA,MAAM,kCAAkC,GAAG,0BAA0B,CAAC;AACtE,MAAM,2BAA2B,GAAG,IAAI,CAAC;AAEzC,SAAS,YAAY,CAAC,KAAc,EAAE,KAAa;IACjD,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;QAChD,MAAM,IAAI,KAAK,CAAC,GAAG,KAAK,oBAAoB,CAAC,CAAC;IAChD,CAAC;IAED,OAAO,KAAgC,CAAC;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MAAM,KAAK,IAAI;YAC9B,QAAQ,EAAE,WAAW;YACrB,UAAU,EAAE,QAAQ,CAAC,MAAM,CAAC,UAAU,CAAC;YACvC,WAAW,EAAE,QAAQ,CAAC,MAAM,CAAC,WAAW,CAAC;YACzC,KAAK,EAAE,QAAQ,CAAC,MAAM,CAAC,KAAK,CAAC;YAC7B,IAAI,EAAE,aAAa,CAAC,MAAM,CAAC,cAAc,CAAC;YAC1C,QAAQ,EAAE,UAAU,CAAC,MAAM,CAAC,QAAQ,CAAC;YACrC,KAAK,EAAE,cAAc,CAAC,MAAM,CAAC,KAAK,CAAC;YACnC,GAAG,sBAAsB,CAAC,MAAM,CAAC,QAAQ,CAAC;SAC3C,CAAC;IACJ,CAAC;CACF;AA7BD,0DA6BC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/// <reference types="bun-types" />
|
|
2
|
+
/// <reference types="node" />
|
|
3
|
+
type ResponseHeaders = Headers | Record<string, string> | Array<[string, string]>;
|
|
4
|
+
export declare function createAnthropicErrorResponse(status: number, type: string, message: string, headers?: ResponseHeaders): Response;
|
|
5
|
+
export declare function createAnthropicProxyResponse(response: Response): Promise<Response>;
|
|
6
|
+
export declare class ProxySseStreamTransformer {
|
|
7
|
+
transform(response: Response): Promise<Response>;
|
|
8
|
+
error(status: number, type: string, message: string): Response;
|
|
9
|
+
}
|
|
10
|
+
export {};
|
|
11
|
+
//# sourceMappingURL=sse-stream-transformer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sse-stream-transformer.d.ts","sourceRoot":"","sources":["../../../src/proxy/transformers/sse-stream-transformer.ts"],"names":[],"mappings":";;AAQA,KAAK,eAAe,GAAG,OAAO,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,KAAK,CAAC,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;AAoClF,wBAAgB,4BAA4B,CAC1C,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,MAAM,EACf,OAAO,CAAC,EAAE,eAAe,GACxB,QAAQ,CASV;AAmLD,wBAAsB,4BAA4B,CAAC,QAAQ,EAAE,QAAQ,GAAG,OAAO,CAAC,QAAQ,CAAC,CAYxF;AAED,qBAAa,yBAAyB;IAC9B,SAAS,CAAC,QAAQ,EAAE,QAAQ,GAAG,OAAO,CAAC,QAAQ,CAAC;IAItD,KAAK,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,QAAQ;CAG/D"}
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ProxySseStreamTransformer = exports.createAnthropicProxyResponse = exports.createAnthropicErrorResponse = void 0;
|
|
4
|
+
const delta_accumulator_1 = require("../../glmt/delta-accumulator");
|
|
5
|
+
const glmt_transformer_1 = require("../../glmt/glmt-transformer");
|
|
6
|
+
const sse_parser_1 = require("../../glmt/sse-parser");
|
|
7
|
+
const JSON_TRANSLATION_ERROR_MESSAGE = 'Failed to translate OpenAI-compatible JSON response';
|
|
8
|
+
const STREAM_TRANSLATION_ERROR_MESSAGE = 'Failed to translate OpenAI-compatible SSE response';
|
|
9
|
+
function createAnthropicErrorPayload(type, message) {
|
|
10
|
+
return {
|
|
11
|
+
type: 'error',
|
|
12
|
+
error: {
|
|
13
|
+
type,
|
|
14
|
+
message,
|
|
15
|
+
},
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
function formatErrorForLog(error) {
|
|
19
|
+
if (error instanceof Error) {
|
|
20
|
+
return error.message;
|
|
21
|
+
}
|
|
22
|
+
try {
|
|
23
|
+
return JSON.stringify(error);
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
return String(error);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
function logTranslationError(context, error) {
|
|
30
|
+
console.error(`[proxy-sse-transformer] ${context}: ${formatErrorForLog(error)}`);
|
|
31
|
+
}
|
|
32
|
+
function createAnthropicErrorResponse(status, type, message, headers) {
|
|
33
|
+
const responseHeaders = new Headers(headers);
|
|
34
|
+
responseHeaders.set('Content-Type', 'application/json');
|
|
35
|
+
responseHeaders.delete('Content-Length');
|
|
36
|
+
return new Response(JSON.stringify(createAnthropicErrorPayload(type, message)), {
|
|
37
|
+
status,
|
|
38
|
+
headers: responseHeaders,
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
exports.createAnthropicErrorResponse = createAnthropicErrorResponse;
|
|
42
|
+
function formatSseEvent(event, data) {
|
|
43
|
+
return `event: ${event}\ndata: ${JSON.stringify(data)}\n\n`;
|
|
44
|
+
}
|
|
45
|
+
function hasTranslatableChoices(value) {
|
|
46
|
+
if (typeof value !== 'object' || value === null) {
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
const { choices } = value;
|
|
50
|
+
if (!Array.isArray(choices) || choices.length === 0) {
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
const firstChoice = choices[0];
|
|
54
|
+
if (typeof firstChoice !== 'object' || firstChoice === null) {
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
const message = firstChoice.message;
|
|
58
|
+
return typeof message === 'object' && message !== null;
|
|
59
|
+
}
|
|
60
|
+
function isSyntheticTransformationFallback(value) {
|
|
61
|
+
return (typeof value === 'object' &&
|
|
62
|
+
value !== null &&
|
|
63
|
+
typeof value.id === 'string' &&
|
|
64
|
+
value.id.startsWith('msg_error_'));
|
|
65
|
+
}
|
|
66
|
+
async function createAnthropicErrorProxyResponse(response) {
|
|
67
|
+
const headers = new Headers(response.headers);
|
|
68
|
+
headers.delete('Content-Type');
|
|
69
|
+
headers.delete('Content-Length');
|
|
70
|
+
let type = response.status === 401
|
|
71
|
+
? 'authentication_error'
|
|
72
|
+
: response.status === 429
|
|
73
|
+
? 'rate_limit_error'
|
|
74
|
+
: response.status >= 400 && response.status < 500
|
|
75
|
+
? 'invalid_request_error'
|
|
76
|
+
: 'api_error';
|
|
77
|
+
let message = `Upstream request failed with status ${response.status}`;
|
|
78
|
+
try {
|
|
79
|
+
const contentType = (response.headers.get('content-type') || '').toLowerCase();
|
|
80
|
+
if (contentType.includes('application/json')) {
|
|
81
|
+
const payload = (await response.json());
|
|
82
|
+
if (typeof payload?.error?.type === 'string' && payload.error.type.trim().length > 0) {
|
|
83
|
+
type = payload.error.type;
|
|
84
|
+
}
|
|
85
|
+
if (typeof payload?.error?.message === 'string' && payload.error.message.trim().length > 0) {
|
|
86
|
+
message = payload.error.message;
|
|
87
|
+
}
|
|
88
|
+
else if (typeof payload?.message === 'string' && payload.message.trim().length > 0) {
|
|
89
|
+
message = payload.message;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
const text = (await response.text()).trim();
|
|
94
|
+
if (text.length > 0) {
|
|
95
|
+
message = text;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
catch (error) {
|
|
100
|
+
logTranslationError('Failed to parse upstream error response', error);
|
|
101
|
+
}
|
|
102
|
+
return createAnthropicErrorResponse(response.status, type, message, headers);
|
|
103
|
+
}
|
|
104
|
+
async function createAnthropicJsonResponse(response) {
|
|
105
|
+
try {
|
|
106
|
+
const openAIResponse = await response.json();
|
|
107
|
+
if (!hasTranslatableChoices(openAIResponse)) {
|
|
108
|
+
return createAnthropicErrorResponse(502, 'api_error', JSON_TRANSLATION_ERROR_MESSAGE);
|
|
109
|
+
}
|
|
110
|
+
const anthropicResponse = new glmt_transformer_1.GlmtTransformer().transformResponse(openAIResponse);
|
|
111
|
+
if (isSyntheticTransformationFallback(anthropicResponse)) {
|
|
112
|
+
logTranslationError('OpenAI-compatible JSON translation produced synthetic fallback response', anthropicResponse);
|
|
113
|
+
return createAnthropicErrorResponse(502, 'api_error', JSON_TRANSLATION_ERROR_MESSAGE);
|
|
114
|
+
}
|
|
115
|
+
return new Response(JSON.stringify(anthropicResponse), {
|
|
116
|
+
status: response.status,
|
|
117
|
+
headers: { 'Content-Type': 'application/json' },
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
catch (error) {
|
|
121
|
+
logTranslationError('OpenAI-compatible JSON translation failed', error);
|
|
122
|
+
return createAnthropicErrorResponse(502, 'api_error', JSON_TRANSLATION_ERROR_MESSAGE);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
function createAnthropicStreamingResponse(response) {
|
|
126
|
+
const body = response.body;
|
|
127
|
+
if (!body) {
|
|
128
|
+
return createAnthropicErrorResponse(502, 'api_error', 'Upstream stream ended before a response body was available');
|
|
129
|
+
}
|
|
130
|
+
const parser = new sse_parser_1.SSEParser({ throwOnMalformedJson: true });
|
|
131
|
+
const transformer = new glmt_transformer_1.GlmtTransformer();
|
|
132
|
+
const accumulator = new delta_accumulator_1.DeltaAccumulator({});
|
|
133
|
+
const encoder = new TextEncoder();
|
|
134
|
+
const readable = new ReadableStream({
|
|
135
|
+
async start(controller) {
|
|
136
|
+
const reader = body.getReader();
|
|
137
|
+
try {
|
|
138
|
+
while (true) {
|
|
139
|
+
const { done, value } = await reader.read();
|
|
140
|
+
if (done) {
|
|
141
|
+
break;
|
|
142
|
+
}
|
|
143
|
+
if (!value) {
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
const events = parser.parse(Buffer.from(value));
|
|
147
|
+
for (const event of events) {
|
|
148
|
+
const anthropicEvents = transformer.transformDelta(event, accumulator);
|
|
149
|
+
for (const anthropicEvent of anthropicEvents) {
|
|
150
|
+
controller.enqueue(encoder.encode(formatSseEvent(anthropicEvent.event, anthropicEvent.data)));
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
if (!accumulator.isFinalized() && accumulator.isMessageStarted()) {
|
|
155
|
+
for (const anthropicEvent of transformer.finalizeDelta(accumulator)) {
|
|
156
|
+
controller.enqueue(encoder.encode(formatSseEvent(anthropicEvent.event, anthropicEvent.data)));
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
catch (error) {
|
|
161
|
+
logTranslationError('OpenAI-compatible SSE translation failed', error);
|
|
162
|
+
controller.enqueue(encoder.encode(formatSseEvent('error', createAnthropicErrorPayload('api_error', STREAM_TRANSLATION_ERROR_MESSAGE))));
|
|
163
|
+
}
|
|
164
|
+
finally {
|
|
165
|
+
reader.releaseLock();
|
|
166
|
+
controller.close();
|
|
167
|
+
}
|
|
168
|
+
},
|
|
169
|
+
});
|
|
170
|
+
return new Response(readable, {
|
|
171
|
+
status: response.status,
|
|
172
|
+
headers: {
|
|
173
|
+
'Content-Type': 'text/event-stream',
|
|
174
|
+
'Cache-Control': 'no-cache',
|
|
175
|
+
Connection: 'keep-alive',
|
|
176
|
+
},
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
async function createAnthropicProxyResponse(response) {
|
|
180
|
+
if (!response.ok) {
|
|
181
|
+
return createAnthropicErrorProxyResponse(response);
|
|
182
|
+
}
|
|
183
|
+
const contentType = (response.headers.get('content-type') || '').toLowerCase();
|
|
184
|
+
const isEventStream = contentType === 'text/event-stream' || contentType.startsWith('text/event-stream;');
|
|
185
|
+
return isEventStream
|
|
186
|
+
? createAnthropicStreamingResponse(response)
|
|
187
|
+
: createAnthropicJsonResponse(response);
|
|
188
|
+
}
|
|
189
|
+
exports.createAnthropicProxyResponse = createAnthropicProxyResponse;
|
|
190
|
+
class ProxySseStreamTransformer {
|
|
191
|
+
async transform(response) {
|
|
192
|
+
return createAnthropicProxyResponse(response);
|
|
193
|
+
}
|
|
194
|
+
error(status, type, message) {
|
|
195
|
+
return createAnthropicErrorResponse(status, type, message);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
exports.ProxySseStreamTransformer = ProxySseStreamTransformer;
|
|
199
|
+
//# sourceMappingURL=sse-stream-transformer.js.map
|