Skip to content

feat(spec/kling): add Kling spec with typed params and restriction validation tests#5

Open
sunqirui1987 wants to merge 18 commits intogoplus:devfrom
sunqirui1987:dev
Open

feat(spec/kling): add Kling spec with typed params and restriction validation tests#5
sunqirui1987 wants to merge 18 commits intogoplus:devfrom
sunqirui1987:dev

Conversation

@sunqirui1987
Copy link
Member

@sunqirui1987 sunqirui1987 commented Mar 8, 2026

Background

This PR introduces and refines spec/kling capabilities, including image/video parameter schemas, Qiniu provider request building, and supporting docs/examples. It also adds restriction validation tests to reduce regression risk.

Key Changes

  1. Added the spec/kling implementation across image, video, internal, and provider modules.
  2. Migrated key params to strongly typed models (for example, ImageInput, VideoRef, and KeepOriginalSound) for better compile-time safety.
  3. Refactored video request construction to use a unified base path via BaseVideoParams.
  4. Updated English and Chinese READMEs and examples to match the latest parameter structures and usage patterns.
  5. Added image restriction tests covering aspect_ratio, resolution, image_reference, and schema fallback behavior.

Testing

  • go test ./spec/kling/... passed

Commits Included

  • 4494336 ADd kling
  • 3cf6b2c kling ai UPDATE
  • c722586 xai kling

@gemini-code-assist
Copy link

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly expands the project's AI capabilities by integrating Kling AI for advanced image and video generation. It introduces a modular and extensible framework for interacting with AI services, complete with a concrete implementation for the Qiniu platform. The changes also refine the core asynchronous operation handling within the xai library, making it more robust and developer-friendly for managing long-running AI tasks. The addition of comprehensive examples and documentation aims to accelerate adoption and understanding of these new features.

Highlights

  • New Kling AI Integration: Introduced a comprehensive kling package for AI image and video generation, including support for various Kling models and functionalities.
  • Qiniu Provider Implementation: Added a dedicated Qiniu provider for the Kling service, enabling interaction with Qiniu's AI platform for image and video tasks.
  • Enhanced Asynchronous Task Management: Updated the core xai API with TaskID() in OperationResponse and new helper functions CallSync and GetTask to better support asynchronous operations and task persistence.
  • Extensive Examples and Documentation: Provided a rich set of runnable examples and detailed tutorials (in both English and Chinese) for Kling image and video generation, covering various models, parameters, and usage patterns.
  • Improved Parameter Validation: Implemented robust parameter validation mechanisms, including ValidateString for Restriction and Contains for StringEnum, to ensure correct API usage and prevent common errors.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • .gitignore
    • Added new entries to ignore Kling example output files such as results.txt, images, and video directories.
  • README.md
    • Updated the main README to include a new 'Examples' section for Kling image and video generation, providing quick-start commands and API key guidance.
  • examples/README.md
    • Added a new README file detailing the Kling examples, including quick start instructions, backend modes, directory structure, supported models, and testing information.
  • examples/kling/example_test.go
    • Added a new test file containing various Go example functions demonstrating Kling image and video generation, async polling, model parameter handling, and error scenarios.
  • examples/kling/images/call_sync_example.go
    • Added a new example demonstrating the CallSync and GetTask functionality for asynchronous image generation tasks.
  • examples/kling/images/example_test.go
    • Added a new test file with mock executors to demonstrate image generation for various Kling models (v1, v1.5, v2, v2-new, v2.1, image-o1).
  • examples/kling/images/kling_image_o1.go
    • Added a new example file for Kling image-o1 model, demonstrating text-to-image and image-to-image generation with reference images and resolution settings.
  • examples/kling/images/kling_v1.go
    • Added a new example file for Kling v1 image generation, covering text-to-image, image-to-image, negative prompts, and batch generation.
  • examples/kling/images/kling_v15.go
    • Added a new example file for Kling v1.5 image generation, including text-to-image, image-to-image with subject reference, and negative prompts.
  • examples/kling/images/kling_v2.go
    • Added a new example file for Kling v2 image generation, demonstrating text-to-image, image-to-image, multi-image generation, and negative prompts.
  • examples/kling/images/kling_v21.go
    • Added a new example file for Kling v2.1 image generation, showcasing text-to-image, image-to-image with reference images, and multi-image generation.
  • examples/kling/images/kling_v2_new.go
    • Added a new example file for Kling v2-new image generation, focusing on image-to-image style transfers.
  • examples/kling/images/main.go
    • Added a new main entry point for Kling image examples, allowing execution of specific model examples or all examples.
  • examples/kling/images/urls.go
    • Added a new utility file defining demo image URLs and a function to print and save image generation results.
  • examples/kling/main.go
    • Added a new main entry point for all Kling examples, dispatching to image and video sub-programs and providing model listing functionality.
  • examples/kling/models.go
    • Added a new file to demonstrate listing available Kling image and video models, their actions, and input schemas.
  • examples/kling/output/output.go
    • Added a new utility package for appending and flushing example output results to a file.
  • examples/kling/video/example_test.go
    • Added a new test file with mock executors to demonstrate video generation for various Kling models (v2.1, v2.5-turbo, v2.6, video-o1, v3, v3-omni).
  • examples/kling/video/kling_v21.go
    • Added a new example file for Kling v2.1 video generation, covering image-to-video, keyframe generation, negative prompts, and various video sizes.
  • examples/kling/video/kling_v25_turbo.go
    • Added a new example file for Kling v2.5-turbo video generation, demonstrating text-to-video, image-to-video, keyframe, and long/square video formats.
  • examples/kling/video/kling_v26.go
    • Added a new example file for Kling v2.6 video generation, showcasing text-to-video, image-to-video, keyframe, motion control, and sound-enabled video.
  • examples/kling/video/kling_v3.go
    • Added a new example file for Kling v3 video generation, covering text-to-video, image-to-video, sound-enabled video, and long video formats.
  • examples/kling/video/kling_v3_omni.go
    • Added a new example file for Kling v3-omni video generation, demonstrating text-to-video, image-to-video (with various reference types), video-to-video, sound-enabled video, and multi-shot video.
  • examples/kling/video/kling_video_o1.go
    • Added a new example file for Kling video-o1 generation, covering text-to-video, image-to-video (single/multi-ref), keyframe, and video-to-video.
  • examples/kling/video/main.go
    • Added a new main entry point for Kling video examples, allowing execution of specific model examples or all examples.
  • examples/kling/video/urls.go
    • Added a new utility file defining demo video URLs and a function to print and save video generation results.
  • examples/shared/service.go
    • Added a new shared service utility for examples, providing a common way to create Kling services with mock or real Qiniu API integration.
  • go.mod
    • Updated the Go module version from 1.24 to 1.25.5.
  • spec/gemini/go.mod
    • Updated the Go module version from 1.24 to 1.25.5.
  • spec/gemini/operation.go
    • Added a TaskID() method to simpleResp and genVideoResp structs to support asynchronous task identification.
  • spec/kling/README.md
    • Added a new detailed tutorial for the Kling package, covering quick start, image/video generation, parameters, and provider integration.
  • spec/kling/README_CN.md
    • Added a Chinese version of the Kling package tutorial.
  • spec/kling/image/build.go
    • Added a new file for building typed ImageParams from raw parameters, handling model-specific routing and validation.
  • spec/kling/image/image.go
    • Added a new file defining image-related schema fields and restriction logic for Kling image models.
  • spec/kling/image/impl.go
    • Added a new file defining internal implementations for xai.Image using raw bytes or URIs.
  • spec/kling/image/models.go
    • Added a new file for identifying and listing supported Kling image generation models.
  • spec/kling/image/o1.go
    • Added a new file defining the input schema for the Kling image-o1 model.
  • spec/kling/image/params.go
    • Added a new file defining various typed parameter structs for different Kling image models (V1, V1.5, V2, V2.1, O1, Gemini).
  • spec/kling/image/restrict_test.go
    • Added a new test file for validating parameter restrictions and schema completeness for Kling image models.
  • spec/kling/image/results.go
    • Added a new file defining outputImages and helper functions for creating xai.Results from image URLs.
  • spec/kling/image/v1.go
    • Added a new file defining the input schema for the Kling v1 image model.
  • spec/kling/image/v15.go
    • Added a new file defining the input schema for the Kling v1.5 image model.
  • spec/kling/image/v2.go
    • Added a new file defining the input schemas for Kling v2 and v2-new image models.
  • spec/kling/image/v21.go
    • Added a new file defining the input schema for the Kling v2.1 image model.
  • spec/kling/internal/model.go
    • Added a new internal file defining constants for all Kling image and video model IDs.
  • spec/kling/internal/param.go
    • Added a new internal file defining constants for all Kling parameter names.
  • spec/kling/internal/params_helpers.go
    • Added a new internal file with helper functions for safely retrieving various types of parameter values from a ParamsReader.
  • spec/kling/internal/params_reader.go
    • Added a new internal interface ParamsReader for abstracting parameter access and defining ErrPromptRequired.
  • spec/kling/internal/values.go
    • Added a new internal file defining constants for common Kling parameter values like video modes, seconds, sizes, resolutions, and aspect ratios.
  • spec/kling/kling.go
    • Added the main Kling package file, re-exporting model/param constants, defining ImageParams/VideoParams interfaces, and providing functions for model identification, schema retrieval, and service registration.
  • spec/kling/kling_test.go
    • Added a new test file for the Kling service, including mock executors and comprehensive tests for actions, operations, and parameter validation.
  • spec/kling/operation.go
    • Added a new file implementing xai.Operation for Kling image and video generation, handling input schema, parameter validation, and dispatching to executors.
  • spec/kling/params.go
    • Added a new file defining the Params struct (implementing xai.Params and internal interfaces) and re-exporting various strongly-typed parameter structures and constants for Kling.
  • spec/kling/provider/qiniu/README.md
    • Added a new README file for the Qiniu Kling provider, detailing its capabilities, supported models, quick start, usage examples, API endpoints, and configuration options.
  • spec/kling/provider/qiniu/client.go
    • Added a new file implementing the HTTP client for the Qiniu Kling API, including retry logic, debug logging, and error handling.
  • spec/kling/provider/qiniu/executor.go
    • Added a new file implementing ImageExecutor and VideoExecutor for the Qiniu provider, handling task submission, status polling, and result conversion.
  • spec/kling/provider/qiniu/image.go
    • Added a new file for building Qiniu image API requests from typed Kling image parameters, handling different endpoints and model-specific logic.
  • spec/kling/provider/qiniu/kling_image.md
    • Added a new markdown file providing detailed API reference for Kling image generation via Qiniu, including model capabilities, request/response formats, and curl examples.
  • spec/kling/provider/qiniu/kling_video.md
    • Added a new markdown file providing detailed API reference for Kling video generation via Qiniu, including model capabilities, request/response formats, and curl examples.
  • spec/kling/provider/qiniu/qiniu_test.go
    • Added a new test file for the Qiniu provider, covering client functionality, request building, response parsing, and retry mechanisms.
  • spec/kling/provider/qiniu/response.go
    • Added a new file defining response structs and status constants for Qiniu Kling image and video API calls.
  • spec/kling/provider/qiniu/video.go
    • Added a new file for building Qiniu video API requests from typed Kling video parameters, handling different endpoints and model-specific logic.
  • spec/kling/results.go
    • Added a new file defining SyncOperationResponse and AsyncOperationResponse for handling synchronous and asynchronous operation results, respectively.
  • spec/kling/video/build.go
    • Added a new file for building typed VideoParams from raw parameters, handling model-specific parsing for image lists, video lists, and multi-prompt items.
  • spec/kling/video/impl.go
    • Added a new file defining internal implementations for xai.Video using raw bytes or URIs.
  • spec/kling/video/models.go
    • Added a new file for identifying and listing supported Kling video generation models.
  • spec/kling/video/o1.go
    • Added a new file defining the input schema for the Kling video-o1 model.
  • spec/kling/video/params.go
    • Added a new file defining various typed parameter structs for different Kling video models (Base, O1, V2.6, V3, V3-Omni) and related constants/types.
  • spec/kling/video/results.go
    • Added a new file defining outputVideos and helper functions for creating xai.Results from video URLs.
  • spec/kling/video/v21.go
    • Added a new file defining the input schema for the Kling v2.1 video model.
  • spec/kling/video/v25.go
    • Added a new file defining the input schema for the Kling v2.5-turbo video model.
  • spec/kling/video/v26.go
    • Added a new file defining the input schema for Kling v2.6 through v2.9 video models.
  • spec/kling/video/v3.go
    • Added a new file defining the input schema for the Kling v3 video model.
  • spec/kling/video/v3_omni.go
    • Added a new file defining the input schema for the Kling v3-omni video model.
  • spec/kling/video/video.go
    • Added a new file defining video-related schema fields, restriction logic, and validation functions for Kling video models.
  • spec/openai/params.go
    • Modified the Tools method to correctly assign underlying tool definitions to the parameter structure.
  • spec/openai/tool.go
    • Removed the buildTools helper function as its logic was integrated directly into params.go.
  • spec/operation.go
    • Modified the OperationResponse interface to include a TaskID() method for asynchronous task identification.
    • Added CallSync helper function to initiate an operation without waiting for completion.
    • Added GetTask helper function to retrieve an OperationResponse for an existing task by its ID.
  • spec/schema.go
    • Added a Contains method to StringEnum for checking if a value is part of the allowed enumeration.
    • Added a ValidateString method to Restriction for validating string parameters against defined limits.
    • Introduced ErrValueNotAllowed error for parameter validation failures.
  • spec/xai.go
    • Added ErrNotSupported error to indicate when a requested operation is not supported by a service.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a significant and well-structured implementation of the kling AI service, including the spec, a Qiniu provider, and a comprehensive set of examples. The code is well-organized, and the inclusion of documentation and tests is excellent.

My review focuses on improving the robustness and clarity of the new example code. I've pointed out several instances of incomplete error handling, which could lead to panics or unexpected behavior. I've also suggested improvements to make the examples more deterministic and easier to verify. Additionally, there are a couple of minor design and correctness suggestions for the example runner and utility code.

Overall, this is a great contribution. Addressing these points will make the examples more reliable and better showcase the new features.

Note: Security Review did not run due to the size of the PR.

Comment on lines +34 to +35
fmt.Println("Error:", err)
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

After printing the error, the function continues execution. This will likely lead to a panic later when svc is used while being nil. The function should return after handling the error. This pattern of incomplete error handling appears in several places in the new example files.

Suggested change
fmt.Println("Error:", err)
}
fmt.Println("Error:", err)
return
}

op, _ := svc.Operation(xai.Model(kling.ModelKlingV1), xai.GenImage)
op.Params().Set(kling.ParamPrompt, "一只可爱的橘猫坐在窗台上看着夕阳,照片风格,高清画质")
op.Params().Set(kling.ParamAspectRatio, kling.Aspect16x9)
results, _ := xai.Call(ctx, svc, op, svc.Options(), nil)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The error returned from xai.Call is being ignored. While this is an example, it's a good practice to demonstrate proper error handling. Please check the error and handle it, for example by printing it and returning. This issue is present in multiple places across the new example files.

Suggested change
results, _ := xai.Call(ctx, svc, op, svc.Options(), nil)
results, err := xai.Call(ctx, svc, op, svc.Options(), nil)
if err != nil {
fmt.Println("Error:", err)
return
}

}

func runSubprogram(subdir string, modelArgs []string) {
cwd, _ := os.Getwd()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The error returned from os.Getwd() is ignored. While it's unlikely to fail in most scenarios, it's a good practice to handle this error to make the example more robust.

Suggested change
cwd, _ := os.Getwd()
cwd, err := os.Getwd()
if err != nil {
fmt.Printf("Error getting current directory: %v\n", err)
return
}

Comment on lines +27 to +30
var (
mu sync.Mutex
results []string
)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This package uses global variables mu and results to manage state. This makes the package stateful and can be brittle. Consider refactoring this to use a struct that holds the state (the results and the mutex), which can then be instantiated by the caller. This would make the design cleaner and safer for potential concurrent use.

Comment on lines +39 to +43
FirstFrame: "https://picsum.photos/1280/720",
EndFrame: "https://picsum.photos/1280/720",
RunningMan: "https://aitoken-public.qnaigc.com/example/generate-video/running-man.jpg",
MultiRef1: "https://picsum.photos/1280/720",
MultiRef2: "https://picsum.photos/1280/720",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using picsum.photos for demo URLs (FirstFrame, EndFrame, MultiRef1, MultiRef2) will result in a different random image on each run. This can make the examples non-deterministic and hard to verify, especially for features like keyframing that rely on specific start and end images. It would be better to use static, permanent URLs for these assets, similar to how RunningMan and MotionImage are handled.

@xgopilot
Copy link

xgopilot bot commented Mar 8, 2026

Good addition of Kling AI image/video generation support with solid test coverage and clear parameter validation. A few issues worth addressing before merging: the HTTP client logs the bearer token unconditionally by default, several example Run* functions continue execution after a nil-service error (leading to nil-pointer panics), unchecked type assertions in operation.go will panic if a non-*Service is passed, and the spec/kling/README.md Quick Start tutorial references functions and paths that don't exist in this PR.


curlCmd := c.buildCurlCommand(method, url, reqBodyBytes)
if c.logger != nil {
c.logger.Printf("[qiniu] curl command:\n%s", curlCmd)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Security: Bearer token leaked unconditionally into logs

buildCurlCommand embeds the raw bearer token in the curl string, and this block logs it on every request regardless of the debugLog flag:

if c.logger != nil {
    c.logger.Printf("[qiniu] curl command:\n%s", curlCmd)
} else {
    fmt.Println(curlCmd)
}

The debugLog guard only wraps logDebug calls — it does not gate this block. Since NewClient defaults debugLog: true and logger: log.Default(), every production caller that doesn't explicitly pass WithDebugLog(false) will have its token written to the default Go logger.

The curl-print block should be gated on c.debugLog, and the token in the Authorization header should be redacted (e.g. Bearer ***) in log output.

}

func (p *genImage) Call(ctx context.Context, svc xai.Service, opts xai.OptionBuilder) (xai.OperationResponse, error) {
s := svc.(*Service)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unchecked type assertion — panic if non-*Service is passed

Both genImage.Call (here) and genVideo.Call use:

s := svc.(*Service)

If any xai.Service implementation other than *kling.Service is passed — including a test double, proxy, or wrapper — this panics with no recovery. Use the comma-ok form and return a typed error:

s, ok := svc.(*Service)
if !ok {
    return nil, fmt.Errorf("kling: expected *kling.Service, got %T", svc)
}


// RunKlingV26 runs all kling-v2-6 demos.
func RunKlingV26() {
svc, err := shared.NewService()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nil-service panic: error is printed but execution continues

This pattern appears in several Run* functions (RunKlingV26, RunKlingV21, RunKlingV15, RunKlingV2, RunKlingV21 images, etc.):

svc, err := shared.NewService()
if err != nil {
    fmt.Println("Error:", err)
    // ← missing return
}
// svc is nil here; the next line panics
op, _ := svc.Operation(...)

Add return immediately after printing the error. By contrast, RunKlingV25Turbo, RunKlingV3, RunKlingV3Omni, and RunKlingVideoO1 use svc, _ := shared.NewService() — silently discarding the error entirely. Either pattern will panic when QINIU_API_KEY is set but the service fails to initialize.

token: token,
maxRetries: 0,
baseRetryDelay: DefaultBaseRetryDelay,
debugLog: true,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

debugLog defaults to true — debug output enabled in production by default

NewClient hardcodes debugLog: true. Any caller that does not explicitly pass WithDebugLog(false) ships with full request/response logging (and token leakage per the other finding). The safe default for a production client is debugLog: false; users should opt-in to debug output explicitly.

)

// Use mock or real backend
imgExec := qiniu.NewImageGenExecutor(imgBackend)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tutorial references non-existent functions and paths

This Quick Start uses qiniu.NewImageGenExecutor(imgBackend) and qiniu.NewVideoGenExecutor(vidBackend), which do not exist. The actual exported constructors are qiniu.NewImageExecutor(client *Client) and qiniu.NewVideoExecutor(client *Client). The simpler convenience API introduced by this PR is just:

svc := qiniu.NewService("your-api-token")

The "Run Examples" section below (around line 283) also references ./spec/kling/provider/qiniu/example/ which does not exist — the actual examples live under examples/kling/. The linked files (text2image.go, service.go, etc.) inside provider/qiniu/example/ are also absent. The Tutorial 4 Provider Integration block shows qiniu.ImageSubmitResult and qiniu.NewImageGenExecutor(backend), neither of which exists anywhere in the codebase. These need to be rewritten to match the actual API.


## Prerequisites

- Go 1.21+
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prerequisites understates the required Go version

The tutorial says "Go 1.21+" but go.mod requires go 1.25.5. Update to match. Also note that go 1.25.5 is not a released Go version as of this writing (Go releases are 1.N or 1.N.P where P ≥ 0; the latest stable is 1.24.x). The go.mod directive may need to be corrected to a real release such as go 1.24.

outputPath = filepath.Join(dir, "output", "results.txt")
}

f, err := os.OpenFile(outputPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Output file created world-readable; TOCTOU between stat and open

Two issues here:

  1. Mode 0644 makes results.txt readable by all users on the system. Since this file accumulates API response URLs (potentially time-limited signed CDN URLs), 0600 is more appropriate.

  2. There is a TOCTOU race: the directory is checked with os.Stat, then os.OpenFile is called separately. Between these two calls a symlink could redirect the write. Use os.MkdirAll to ensure the directory exists atomically before opening.

if filepath.Base(cwd) == "kling" {
runPath = subdir
}
cmd := exec.Command("go", append([]string{"run", "./" + runPath}, modelArgs...)...)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

os.Args forwarded unsanitized to exec.Command

modelArgs comes directly from os.Args[1:] and is appended to the go run invocation. While there is no shell injection (no shell is invoked), flag-style arguments such as -toolexec=malicious or -overlay=attacker.json can influence how the Go toolchain subprocess executes. The model-name guard at lines 53–67 only prevents unknown model names in the dispatcher; it does not prevent extra arguments from reaching exec.Command. Consider rejecting any modelArgs element that starts with -.

}

// GetInt returns the int value for the given param name, or 1 if missing/invalid.
func GetInt(p ParamsReader, name string) int {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GetInt returns 1 for missing/zero/negative values — silently coerces invalid input

The documented default of 1 is intentional for ParamN (image count), but this function is used for all integer params. If a caller explicitly passes 0 or a negative value, the result is also 1 with no warning or error — making invalid input indistinguishable from "not set". Consider returning 0 as the neutral default and letting callers apply model-specific defaults, or add a separate GetIntWithDefault that takes an explicit default argument.


// validateParamsAgainstRestriction checks each set string param against its Restriction.
func validateParamsAgainstRestriction(schema xai.InputSchema, params *Params) error {
for name, val := range params.Export() {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Export() called on every Call() — unnecessary map copy on the hot path

validateParamsAgainstRestriction calls params.Export() which allocates and copies the entire params map on every API request. The function only reads values — it could iterate via params.Get(name) or an unexported accessor without the defensive copy. This is an avoidable allocation on each call.

}

// IsImageModel returns true if the model supports image generation.
func IsImageModel(m string) bool {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IsImageModel exclusion list is fragile — new video models require manual additions

The exclusion uses individual strings.Contains checks for v2-5 through v2-9. A future video model like kling-v2-10 or kling-v2-5-image would either be missed or incorrectly excluded. Consider an allowlist approach (only the known image models return true) rather than a blocklist, or convert the exclusion to a numeric range check.

@xushiwei xushiwei changed the title xai update model: add kling Mar 8, 2026
@sunqirui1987 sunqirui1987 changed the title model: add kling feat(spec/kling): 引入 Kling 规范并补齐类型化参数与限制校验测试 Mar 8, 2026
@sunqirui1987 sunqirui1987 changed the title feat(spec/kling): 引入 Kling 规范并补齐类型化参数与限制校验测试 feat(spec/kling): add Kling spec with typed params and restriction validation tests Mar 8, 2026
…examples

- add `multi_shot`/`shot_type`/`multi_prompt` to kling-v3 schema and params build
- pass multi-shot fields through qiniu v3 video request builder
- update kling v3 and v3-omni video examples to use `shot_type=customize`
- normalize `multi_prompt.index` to start from 1 and be consecutive
- add/update tests and docs examples for multi_prompt usage
…OpenAI examples guide

- rewrite the whole README for examples/openai
- document all demos (text/image/video/multi-video/function-call/thinking)
- explain unified block output format for response/candidate/block
- clarify stream vs non-stream behavior and function-call special flow
- add troubleshooting for auth/network/tool schema (parameters.type) errors
- include defaults, base URL override, and verification command
…and examples

OpenAI spec:
- Split response.go into response_v1.go and response_v3.go by API version
- Add response_v1_test.go and response_v3_test.go
- Add NewV1WithQiniu() and qiniuV1Provider for Qiniu API image responses
- Add WithDebugCurl option to log curl commands
- Switch provider/qiniu to use NewV1WithQiniu

Gemini spec:
- Add backend, provider/qiniu, operation and schema updates
- Add gemini.md documentation

Examples:
- Add examples/gemini (chat, image gen, tool use)
- Update examples/openai shared blocks and service
Backend abstraction:
- Add Backend interface in backend.go for pluggable transport
- Service now uses Backend instead of direct genai.Models/Operations
- Add NewWithBackend() and genAIBackend for default genai implementation
- Operations delegate to backend; Actions() and model support per-backend

Schema & operation:
- Export NewInputSchema, NewInputSchemaEx, NewParams for external use
- Add int/int8/int16 support in kindOf
- Refactor syncResponse to NewSyncResponse; operations carry model param

Provider & examples:
- Add spec/gemini/provider/qiniu for Qiniu API
- Add examples/gemini: chat-text, chat-image, chat-tool, image-generate, image-edit
- Add gemini.md documentation
spec/gemini:
- Add BackendService interface and Service.Backend()
- Move gemini.md content to README.md, keep only links
- Simplify provider/qiniu implementation

examples/gemini:
- NewService returns xai.Service so callers depend on interface
- Rename svc to service consistently
- Remove direct dependency on gemini package
- Add cmd/restrict_gen for restrict generation
- Add spec/gemini/restrict_gen.go, spec/util/util.go
- Update spec/gemini/schema, operation; spec/schema
spec/vidu:
- Add vidu-q1 and vidu-q2 model support via xai.GenVideo
- Backend abstraction for pluggable providers
- Qiniu provider with Submit/GetTaskStatus
- Param validation, async task handling, unified result encapsulation

examples/vidu:
- Runnable demos for q1/q2 text-to-video, reference-to-video
- q2 image-to-video/pro and start-end-to-video/pro
- CallSync + TaskID + GetTask polling example
- Mock backend (default) and real Qiniu API mode
Spec & implementation:
- spec/gemini/provider/qiniu/veo.md: API reference for Veo models
- spec/gemini/provider/qiniu/video.go: video generation backend
- spec/gemini/veo_opt.go: optional params (AspectRatio, Resolution,
  DurationSeconds, PersonGeneration, CompressionQuality)
- spec/gemini/veo_validate.go: parameter validation

Examples (examples/veo/):
- veo_*_generate_preview.go: text-to-video for 2.0/3.0/3.1
- veo_image_to_video.go: image-to-video
- veo_first_last_frame.go: first+last frame
- veo_video_input.go: video as input (引用视频)
- veo_reference_images.go: multi reference images (多参考图)
- veo_callback.go: callback mode with PubsubTopic

Models: veo-2.0-generate-001/exp/preview, veo-3.0/3.1-generate-preview,
veo-3.0/3.1-fast-generate-preview
- Add video operations: text-to-video, image-to-video, remix
- Add operation_video.go for video generation and polling
- Add provider_video.go and Qiniu service_video integration
- Add examples/sora with runnable demos (text-to-video, image-to-video, remix)
- Refactor: move qiniu provider to provider/qiniu/, remove provider_qiniu.go
- Update spec: message, options, params, schema for video support
- Add video operations: text-to-video, image-to-video, remix
- Add operation_video.go for video generation and polling
- Add provider_video.go and Qiniu service_video integration
- Add examples/sora with runnable demos (text-to-video, image-to-video, remix)
- Refactor: move qiniu provider to provider/qiniu/, remove provider_qiniu.go
- Update spec: message, options, params, schema for video support
- Add spec/audio: Transcribe (ASR) and Synthesize (TTS) actions
- Add Qiniu provider for voice/asr, voice/tts, voice/list APIs
- Add OutputText and OutputAudio to spec/schema Generated interface
- Add Transcribe and Synthesize actions to spec/operation
- Add examples/audio: list-voices, asr, tts demos with mock/real modes
- Update examples/README.md with audio quick start and directory structure
- unwrap audio/kling/vidu services in operation calls
- add ApiKeySetter and thread-safe SetApiKey to Qiniu providers
- expand Gemini image examples/docs and add wrapper tests
Add VideoSchema interface and VideoSchemaProvider to standardize video
generation schema across providers (OpenAI Sora, Gemini Veo, Kling, Vidu).

Changes:
- spec/operation.go: Add VideoGenMode (text_to_video, image_to_video,
  multi_ref_to_video, start_end_to_video) with implication hierarchy and
  helper functions (ExpandVideoGenModes, NormalizeVideoGenModes)
- Add video_schema.go for openai, gemini, kling, vidu implementing
  VideoSchema with SupportedModes, Fields, Restrict, FieldModes
- Extract video param constants to params.go (openai, gemini)
- Refactor openai/operation_video.go to use param constants, move
  restrictions/fields to video_schema.go
- vidu: ParamReferenceImageURLs now accepts String|List; add Prompt
  required restriction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant