add ai feature

This commit is contained in:
2025-11-05 16:06:09 +08:00
parent 42bc9aff42
commit 13ca7ddbed
33 changed files with 2194 additions and 30 deletions

59
internal/ai/client.go Normal file
View File

@@ -0,0 +1,59 @@
package ai
import (
"context"
"errors"
"github.com/rogeecn/renamer/internal/ai/flow"
)
// Runner executes the rename flow and returns structured suggestions.
type Runner func(ctx context.Context, input *flow.RenameFlowInput) (*flow.Output, error)
// Client orchestrates flow invocation for callers such as the CLI command.
type Client struct {
runner Runner
}
// ClientOption customises the AI client behaviour.
type ClientOption func(*Client)
// WithRunner overrides the flow runner implementation (useful for tests).
func WithRunner(r Runner) ClientOption {
return func(c *Client) {
c.runner = r
}
}
// NewClient constructs a Client with the default Genkit-backed runner.
func NewClient(opts ...ClientOption) *Client {
client := &Client{}
client.runner = func(ctx context.Context, input *flow.RenameFlowInput) (*flow.Output, error) {
creds, err := LoadCredentials()
if err != nil {
return nil, err
}
return runRenameFlow(ctx, input, creds)
}
for _, opt := range opts {
opt(client)
}
return client
}
// Suggest executes the rename flow and returns structured suggestions.
func (c *Client) Suggest(ctx context.Context, input *flow.RenameFlowInput) (*flow.Output, error) {
if c == nil {
return nil, ErrClientNotInitialized
}
if c.runner == nil {
return nil, ErrRunnerNotConfigured
}
return c.runner(ctx, input)
}
// ErrClientNotInitialized indicates the client receiver was nil.
var ErrClientNotInitialized = errors.New("ai client not initialized")
// ErrRunnerNotConfigured indicates the client runner is missing.
var ErrRunnerNotConfigured = errors.New("ai client runner not configured")