Open
Conversation
Change-Id: I2f5c5a595681d9d2bbcefaecd2b81972341decda Co-developed-by: Codex <noreply@openai.com>
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Ⅰ. Describe what this PR did
本 PR 新增 KlingAI provider,支持 KlingAI 官方 AK/SK JWT 鉴权、第三方网关静态 Bearer token,并同时覆盖 OpenAI 兼容协议与 Kling 原始协议。
背景
KlingAI 视频生成接口按任务类型拆分为:
/v1/videos/text2video/v1/videos/image2video/v1/videos/text2video/{video_id}/v1/videos/image2video/{video_id}同时社区存在 302.ai、New API 等网关服务,通常需要自定义 host、base path 和静态 Bearer token。因此 Kling provider 需要同时支持:
klingAccessKey+klingSecretKey生成 HS256 JWTapiTokens作为 Bearer tokenproviderDomain/providerBasePath覆盖网关域名和路径前缀/v1/videos与 Kling 原始路径透传主要变更
新增 Kling provider 注册与配置
providerTypeKling = "kling"klingProviderInitializerklingAccessKeyklingSecretKeyklingTokenRefreshAhead,默认60Authorization: Bearer <jwt>apiTokens,设置Authorization: Bearer <apiToken>apiTokens同时配置时,优先官方 JWT新增 Kling capability
openai/v1/videos->/v1/videos/text2videokling/v1/image2video->/v1/videos/image2videoopenai/v1/retrievevideo->/v1/videos/text2video/{video_id}kling/v1/retrieveimagevideo->/v1/videos/image2video/{video_id}ProviderConfig.setDefaultCapabilities改为保留用户显式配置,不再覆盖自定义 capabilityOpenAI 兼容协议支持
POST /v1/videos先按 text-to-video capability 做 header 阶段路径映射kling/v1/image2videocapabilitymodel转换为 Kling 原生model_namemodel_name输入也支持按modelMapping原地映射task_id自动加前缀:kling-t2v-kling-i2v-GET /v1/videos/{task_id}可通过前缀路由到正确的 text/image retrieve endpoint任务查询路由与 query 处理
kling-i2v-<id>前缀 -> image retrieve endpointkling-t2v-<id>前缀 -> text retrieve endpointkling_task_type=image2video|text2videohint -> 指定 retrieve endpointkling_task_type在转发上游前剥离kling_task_typefallback 到默认 retrieve capability,同时也会剥离内部 hint原始协议支持
protocol: original返回基础klingProvider,不暴露RequestBodyHandlerContent-LengthGetApiName识别:/v1/videos/text2video/v1/videos/image2video/v1/videos/text2video/{task_id}/v1/videos/image2video/{task_id}通用链路调整
main.go只在 provider 实现RequestBodyHandler时才拦截 request bodyHeaderStopIterationutil.MapRequestPathByCapability修复嵌套 provider path 中{video_id}占位符替换问题修复前后对比
apiTokens静态 BearerproviderDomain/providerBasePath/v1/videos文生/v1/videos/text2video/v1/videos图生/v1/videos/image2videoContent-LengthⅡ. Does this pull request fix one issue?
本 PR 是 KlingAI provider 能力新增,同时修复接入过程中发现的几个路由与协议边界问题:
Content-LengthⅢ. Why don't you add test cases (unit test/integration test)?
已补充 provider 单测和插件链路测试,覆盖主要分支和边界:
klingAccessKey、klingSecretKey、klingTokenRefreshAhead、Kling 自定义 capabilityproviderDomain不被默认 host 覆盖Content-LengthContent-Lengthmodel->model_namemodel_name原地映射kling/v1/image2videocapabilitykling_task_typehintproviderBasePathContent-LengthproviderDomain/providerBasePathⅣ. Describe how to verify it
定向验证 Kling provider 单测
定向验证 Kling 插件链路测试
全量验证
本地验证结果:
go test ./provider -run Kling -count=1 -gcflags='all=-N -l'通过go test . -run TestKling -count=1 -gcflags='all=-N -l'通过go test ./...通过Ⅴ. Special notes for reviews
klingAccessKey+klingSecretKey,就优先使用官方 JWT;apiTokens仅作为网关模式 Bearer。openai/v1/videos仍作为 OpenAI 客户端入口;kling/v1/image2video用于 body 阶段根据图像字段切换上游 endpoint。openai/v1/retrievevideo。kling_task_type是内部 hint:只用于 raw task id 的 OpenAI-style retrieve 路由,转发上游前必须剥离。setDefaultCapabilities行为调整:改为不覆盖用户自定义 capability,避免 provider 初始化覆盖配置中的 Kling gateway path。Ⅵ. AI Coding Tool Usage Checklist (if applicable)
Please check all applicable items:
AI Coding Summary
关键决策:
/v1/videos保持单一客户端入口,body 阶段根据 image 字段切换 text/image upstream capability。kling_task_typehint 解决 Kling retrieve endpoint 按任务类型拆分的问题。主要改动范围:
provider/kling.goprovider/provider.goklingprovidersetDefaultCapabilities保留用户自定义 capabilitymain.goRequestBodyHandler时才拦截 request bodyutil/http.go{video_id}等占位符替换provider/kling_test.gotest/kling.gomain_test.go、provider/provider_test.go、test/util.go验证结果:
go test ./provider -run Kling -count=1 -gcflags='all=-N -l'通过go test . -run TestKling -count=1 -gcflags='all=-N -l'通过go test ./...通过Ⅰ. Describe what this PR did
This PR adds a new KlingAI provider, supports KlingAI official AK/SK JWT authentication, third-party gateway static Bearer token, and covers both OpenAI compatible protocols and Kling original protocols.
Background
KlingAI video generation interface is split according to task type:
/v1/videos/text2video/v1/videos/image2video/v1/videos/text2video/{video_id}/v1/videos/image2video/{video_id}At the same time, there are gateway services such as 302.ai and New API in the community, which usually require customized host, base path and static Bearer token. Therefore, Kling provider needs to support both:
klingAccessKey+klingSecretKeygenerate HS256 JWTapiTokensas Bearer tokensproviderDomain/providerBasePathoverrides the gateway domain name and path prefix/v1/videosand Kling original path transparent transmissionMajor changes
Added Kling provider registration and configuration
providerTypeKling = "kling"klingProviderInitializerklingAccessKeyklingSecretKeyklingTokenRefreshAhead, default60Authorization: Bearer <jwt>apiTokens, setAuthorization: Bearer <apiToken>apiTokensare configured at the same time, the official JWT takes precedenceNew Kling capability
openai/v1/videos->/v1/videos/text2videokling/v1/image2video->/v1/videos/image2videoopenai/v1/retrievevideo->/v1/videos/text2video/{video_id}kling/v1/retrieveimagevideo->/v1/videos/image2video/{video_id}ProviderConfig.setDefaultCapabilitiesis changed to retain the user's explicit configuration and no longer overrides the custom capabilityOpenAI compatible protocol support
POST /v1/videosfirst press text-to-video capability to do header stage path mappingkling/v1/image2videocapability after detecting the image field in the body stagemodelconverted to Kling nativemodel_namemodel_nameinput also supports in-place mapping bymodelMappingtask_idin creation response:kling-t2v-kling-i2v-GET /v1/videos/{task_id}can be routed to the correct text/image retrieve endpoint via the prefixTask query routing and query processing
kling-i2v-<id>prefix -> image retrieve endpointkling-t2v-<id>prefix -> text retrieve endpointkling_task_type=image2video|text2videohint -> specify retrieve endpointkling_task_typestripped before forwarding upstreamkling_task_typefallback to default retrieve capability, also stripping internal hintsOriginal protocol support
protocol: originalreturns the baseklingProviderand does not exposeRequestBodyHandlerContent-LengthGetApiNamerecognition of Kling’s native create/retrieve path:/v1/videos/text2video/v1/videos/image2video/v1/videos/text2video/{task_id}/v1/videos/image2video/{task_id}Universal Link Adjustment
main.goonly intercepts the request body if the provider implementsRequestBodyHandlerHeaderStopIterationutil.MapRequestPathByCapabilityfixes{video_id}placeholder replacement problem in nested provider pathComparison before and after repair
apiTokensstatic BearerproviderDomain/providerBasePath/v1/videosVincent/v1/videos/text2video/v1/videosTusheng/v1/videos/image2videoContent-LengthⅡ. Does this pull request fix one issue?
This PR is a new addition to KlingAI provider capabilities, and also fixes several routing and protocol boundary issues discovered during the access process:
Content-LengthⅢ. Why don't you add test cases (unit test/integration test)?
Provider single test and plug-in link test have been added to cover main branches and boundaries:
klingAccessKey,klingSecretKey,klingTokenRefreshAhead, Kling custom capability-Default host
providerDomainis not overridden by default hostContent-LengthContent-Lengthmodel->model_namemodel_namein-place mappingkling/v1/image2videocapabilitykling_task_typehintproviderBasePathContent-LengthproviderDomain/providerBasePathⅣ. Describe how to verify it
Directed verification Kling provider single test
Directed verification Kling plug-in link test
Full verification
Local verification results:
go test ./provider -run Kling -count=1 -gcflags='all=-N -l'passedgo test . -run TestKling -count=1 -gcflags='all=-N -l'passedgo test ./...passedⅤ. Special notes for reviews
klingAccessKey+klingSecretKeyis configured, the official JWT will be used first;apiTokensis only used as a gateway mode Bearer.openai/v1/videosis still used as the OpenAI client entry;kling/v1/image2videois used in the body stage to switch upstream endpoints based on the image field.openai/v1/retrievevideo.kling_task_typeis an internal hint: only used for OpenAI-style retrieve routing of raw task id, which must be stripped before forwarding to the upstream.setDefaultCapabilitiesbehavior adjustment: Changed to not overwriting user-defined capabilities to prevent provider initialization from overwriting the Kling gateway path in the configuration.Ⅵ. AI Coding Tool Usage Checklist (if applicable)
Please check all applicable items:
AI Coding Summary
Key Decisions:
/v1/videosmaintains a single client entry, and switches the text/image upstream capability according to the image field in the body stage.kling_task_typehint.Main scope of changes:
provider/kling.goprovider/provider.goklingprovidersetDefaultCapabilitiesretains user-defined capabilitiesmain.goRequestBodyHandlerutil/http.go{video_id}in capability pathprovider/kling_test.gotest/kling.gomain_test.go,provider/provider_test.go,test/util.goVerification result:
go test ./provider -run Kling -count=1 -gcflags='all=-N -l'passedgo test . -run TestKling -count=1 -gcflags='all=-N -l'passedgo test ./...passed