feat: manually mirror opencoze's code from bytedance

Change-Id: I09a73aadda978ad9511264a756b2ce51f5761adf
This commit is contained in:
fanlv
2025-07-20 17:36:12 +08:00
commit 890153324f
14811 changed files with 1923430 additions and 0 deletions

23
backend/infra/contract/cache/cache.go vendored Normal file
View File

@@ -0,0 +1,23 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cache
import (
"github.com/redis/go-redis/v9"
)
type Cmdable = redis.Cmdable

View File

@@ -0,0 +1,35 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package chatmodel
import (
"context"
"github.com/cloudwego/eino/components/model"
)
//go:generate mockgen -destination ../../../internal/mock/infra/contract/chatmodel/base_model_mock.go -package mock -source ${GOPATH}/src/github.com/cloudwego/eino/components/model/interface.go BaseChatModel
type BaseChatModel = model.BaseChatModel
//go:generate mockgen -destination ../../../internal/mock/infra/contract/chatmodel/toolcalling_model_mock.go -package mock -source ${GOPATH}/src/github.com/cloudwego/eino/components/model/interface.go ToolCallingChatModel
type ToolCallingChatModel = model.ToolCallingChatModel
//go:generate mockgen -destination ../../../internal/mock/infra/contract/chatmodel/chat_model_factory_mock.go -package mock -source chat_model.go Factory
type Factory interface {
CreateChatModel(ctx context.Context, protocol Protocol, config *Config) (ToolCallingChatModel, error)
SupportProtocol(protocol Protocol) bool
}

View File

@@ -0,0 +1,94 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package chatmodel
import (
"time"
"github.com/cloudwego/eino-ext/components/model/deepseek"
"github.com/cloudwego/eino-ext/libs/acl/openai"
"google.golang.org/genai"
)
type Config struct {
BaseURL string `json:"base_url,omitempty" yaml:"base_url,omitempty"`
APIKey string `json:"api_key,omitempty" yaml:"api_key,omitempty"`
Timeout time.Duration `json:"timeout,omitempty" yaml:"timeout,omitempty"`
Model string `json:"model" yaml:"model"`
Temperature *float32 `json:"temperature,omitempty" yaml:"temperature,omitempty"`
FrequencyPenalty *float32 `json:"frequency_penalty,omitempty" yaml:"frequency_penalty,omitempty"`
PresencePenalty *float32 `json:"presence_penalty,omitempty" yaml:"presence_penalty,omitempty"`
MaxTokens *int `json:"max_tokens,omitempty" yaml:"max_tokens,omitempty"`
TopP *float32 `json:"top_p,omitempty" yaml:"top_p"`
TopK *int `json:"top_k,omitempty" yaml:"top_k"`
Stop []string `json:"stop,omitempty" yaml:"stop"`
EnableThinking *bool `json:"enable_thinking,omitempty" yaml:"enable_thinking,omitempty"`
OpenAI *OpenAIConfig `json:"open_ai,omitempty" yaml:"openai"`
Claude *ClaudeConfig `json:"claude,omitempty" yaml:"claude"`
Ark *ArkConfig `json:"ark,omitempty" yaml:"ark"`
Deepseek *DeepseekConfig `json:"deepseek,omitempty" yaml:"deepseek"`
Qwen *QwenConfig `json:"qwen,omitempty" yaml:"qwen"`
Gemini *GeminiConfig `json:"gemini,omitempty" yaml:"gemini"`
Custom map[string]string `json:"custom,omitempty" yaml:"custom"`
}
type OpenAIConfig struct {
ByAzure bool `json:"by_azure,omitempty" yaml:"by_azure"`
APIVersion string `json:"api_version,omitempty" yaml:"api_version"`
ResponseFormat *openai.ChatCompletionResponseFormat `json:"response_format,omitempty" yaml:"response_format"`
}
type ClaudeConfig struct {
ByBedrock bool `json:"by_bedrock" yaml:"by_bedrock"`
// bedrock config
AccessKey string `json:"access_key,omitempty" yaml:"access_key"`
SecretAccessKey string `json:"secret_access_key,omitempty" yaml:"secret_access_key"`
SessionToken string `json:"session_token,omitempty" yaml:"session_token"`
Region string `json:"region,omitempty" yaml:"region"`
}
type ArkConfig struct {
Region string `json:"region" yaml:"region"`
AccessKey string `json:"access_key,omitempty" yaml:"access_key"`
SecretKey string `json:"secret_key,omitempty" yaml:"secret_key"`
RetryTimes *int `json:"retry_times,omitempty" yaml:"retry_times"`
CustomHeader map[string]string `json:"custom_header,omitempty" yaml:"custom_header"`
}
type DeepseekConfig struct {
ResponseFormatType deepseek.ResponseFormatType `json:"response_format_type" yaml:"response_format_type"`
}
type QwenConfig struct {
ResponseFormat *openai.ChatCompletionResponseFormat `json:"response_format,omitempty" yaml:"response_format"`
}
type GeminiConfig struct {
Backend genai.Backend `json:"backend,omitempty" yaml:"backend"`
Project string `json:"project,omitempty" yaml:"project"`
Location string `json:"location,omitempty" yaml:"location"`
APIVersion string `json:"api_version,omitempty" yaml:"api_version"`
Headers map[string][]string `json:"headers,omitempty" yaml:"headers"`
TimeoutMs int64 `json:"timeout_ms,omitempty" yaml:"timeout_ms"`
IncludeThoughts *bool `json:"include_thoughts,omitempty" yaml:"include_thoughts"` // default true
ThinkingBudget *int32 `json:"thinking_budget,omitempty" yaml:"thinking_budget"` // default nil
}

View File

@@ -0,0 +1,55 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package chatmodel
import "github.com/coze-dev/coze-studio/backend/api/model/ocean/cloud/developer_api"
type Protocol string
const (
ProtocolOpenAI Protocol = "openai"
ProtocolClaude Protocol = "claude"
ProtocolDeepseek Protocol = "deepseek"
ProtocolGemini Protocol = "gemini"
ProtocolArk Protocol = "ark"
ProtocolOllama Protocol = "ollama"
ProtocolQwen Protocol = "qwen"
ProtocolErnie Protocol = "ernie"
)
func (p Protocol) TOModelClass() developer_api.ModelClass {
switch p {
case ProtocolArk:
return developer_api.ModelClass_SEED
case ProtocolOpenAI:
return developer_api.ModelClass_GPT
case ProtocolDeepseek:
return developer_api.ModelClass_DeekSeek
case ProtocolClaude:
return developer_api.ModelClass_Claude
case ProtocolGemini:
return developer_api.ModelClass_Gemini
case ProtocolOllama:
return developer_api.ModelClass_Llama
case ProtocolQwen:
return developer_api.ModelClass_QWen
case ProtocolErnie:
return developer_api.ModelClass_Ernie
default:
return developer_api.ModelClass_Other
}
}

View File

@@ -0,0 +1,126 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package document
import (
"fmt"
"github.com/cloudwego/eino/schema"
)
const (
MetaDataKeyColumns = "table_columns" // val: []*Column
MetaDataKeyColumnData = "table_column_data" // val: []*ColumnData
MetaDataKeyColumnsOnly = "table_columns_only" // val: struct{}, which means table has no data, only header.
MetaDataKeyCreatorID = "creator_id" // val: int64
MetaDataKeyExternalStorage = "external_storage" // val: map[string]any
)
func GetDocumentColumns(doc *schema.Document) ([]*Column, error) {
if doc == nil || doc.MetaData == nil {
return nil, fmt.Errorf("invalid document")
}
columns, ok := doc.MetaData[MetaDataKeyColumns].([]*Column)
if !ok {
return nil, fmt.Errorf("invalid document columns")
}
return columns, nil
}
func WithDocumentColumns(doc *schema.Document, columns []*Column) *schema.Document {
doc.MetaData[MetaDataKeyColumns] = columns
return doc
}
func GetDocumentColumnData(doc *schema.Document) ([]*ColumnData, error) {
if doc == nil || doc.MetaData == nil {
return nil, fmt.Errorf("invalid document")
}
data, ok := doc.MetaData[MetaDataKeyColumnData].([]*ColumnData)
if !ok {
return nil, fmt.Errorf("invalid document column data")
}
return data, nil
}
func WithDocumentColumnData(doc *schema.Document, data []*ColumnData) *schema.Document {
doc.MetaData[MetaDataKeyColumnData] = data
return doc
}
func WithDocumentColumnsOnly(doc *schema.Document) *schema.Document {
doc.MetaData[MetaDataKeyColumnsOnly] = struct{}{}
return doc
}
func GetDocumentColumnsOnly(doc *schema.Document) (bool, error) {
if doc == nil || doc.MetaData == nil {
return false, fmt.Errorf("invalid document")
}
_, ok := doc.MetaData[MetaDataKeyColumnsOnly].(struct{})
return ok, nil
}
func GetDocumentsColumnsOnly(docs []*schema.Document) (bool, error) {
if len(docs) != 1 {
return false, nil
}
return GetDocumentColumnsOnly(docs[0])
}
func GetDocumentCreatorID(doc *schema.Document) (int64, error) {
if doc == nil || doc.MetaData == nil {
return 0, fmt.Errorf("invalid document")
}
creatorID, ok := doc.MetaData[MetaDataKeyCreatorID].(int64)
if !ok {
return 0, fmt.Errorf("invalid document creator id")
}
return creatorID, nil
}
func WithDocumentCreatorID(doc *schema.Document, creatorID int64) *schema.Document {
doc.MetaData[MetaDataKeyCreatorID] = creatorID
return doc
}
func GetDocumentExternalStorage(doc *schema.Document) (map[string]any, error) {
if doc == nil || doc.MetaData == nil {
return nil, fmt.Errorf("invalid document")
}
data, ok := doc.MetaData[MetaDataKeyExternalStorage].(map[string]any)
if !ok {
return nil, fmt.Errorf("invalid document external storage")
}
return data, nil
}
func WithDocumentExternalStorage(doc *schema.Document, externalStorage map[string]any) *schema.Document {
doc.MetaData[MetaDataKeyExternalStorage] = externalStorage
return doc
}

View File

@@ -0,0 +1,23 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package imageunderstand
import "context"
type ImageUnderstand interface {
ImageUnderstand(ctx context.Context, image []byte) (content string, err error)
}

View File

@@ -0,0 +1,29 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nl2sql
import (
"context"
"github.com/cloudwego/eino/schema"
"github.com/coze-dev/coze-studio/backend/infra/contract/document"
)
type NL2SQL interface {
NL2SQL(ctx context.Context, messages []*schema.Message, tables []*document.TableSchema, opts ...Option) (sql string, err error)
}

View File

@@ -0,0 +1,31 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nl2sql
import "github.com/coze-dev/coze-studio/backend/infra/contract/chatmodel"
type Option func(o *Options)
type Options struct {
ChatModel chatmodel.BaseChatModel
}
func WithChatModel(cm chatmodel.BaseChatModel) Option {
return func(o *Options) {
o.ChatModel = cm
}
}

View File

@@ -0,0 +1,24 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ocr
import "context"
type OCR interface {
FromBase64(ctx context.Context, b64 string) (texts []string, err error)
FromURL(ctx context.Context, url string) (texts []string, err error)
}

View File

@@ -0,0 +1,128 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package parser
import (
"github.com/coze-dev/coze-studio/backend/infra/contract/document"
"github.com/coze-dev/coze-studio/backend/pkg/lang/sets"
)
type Manager interface {
GetParser(config *Config) (Parser, error)
}
type Config struct {
FileExtension FileExtension
ParsingStrategy *ParsingStrategy
ChunkingStrategy *ChunkingStrategy
}
// ParsingStrategy for document parse before indexing
type ParsingStrategy struct {
// Doc
ExtractImage bool `json:"extract_image"` // 提取图片元素
ExtractTable bool `json:"extract_table"` // 提取表格元素
ImageOCR bool `json:"image_ocr"` // 图片 ocr
FilterPages []int `json:"filter_pages"` // 页过滤, 第一页=1
// Sheet
SheetID *int `json:"sheet_id"` // xlsx sheet id
HeaderLine int `json:"header_line"` // 表头行
DataStartLine int `json:"data_start_line"` // 数据起始行
RowsCount int `json:"rows_count"` // 读取数据行数
IsAppend bool `json:"-"` // 行插入
Columns []*document.Column `json:"-"` // sheet 对齐表头
IgnoreColumnTypeErr bool `json:"-"` // true 时忽略 column type 与 value 未对齐的问题,此时 value 为空
// Image
ImageAnnotationType ImageAnnotationType `json:"image_annotation_type"` // 图片内容标注类型
}
type ChunkingStrategy struct {
ChunkType ChunkType `json:"chunk_type"`
// custom config
ChunkSize int64 `json:"chunk_size"` // 分段最大长度
Separator string `json:"separator"` // 分段标识符
Overlap int64 `json:"overlap"` // 分段重叠比例
TrimSpace bool `json:"trim_space"`
TrimURLAndEmail bool `json:"trim_url_and_email"`
// leveled config
MaxDepth int64 `json:"max_depth"` // 按层级分段时的最大层级
SaveTitle bool `json:"save_title"` // 保留层级标题
}
type ChunkType int64
const (
ChunkTypeDefault ChunkType = 0 // 自动分片
ChunkTypeCustom ChunkType = 1 // 自定义规则分片
ChunkTypeLeveled ChunkType = 2 // 层级分片
)
type ImageAnnotationType int64
const (
ImageAnnotationTypeModel ImageAnnotationType = 0 // 模型自动标注
ImageAnnotationTypeManual ImageAnnotationType = 1 // 人工标注
)
type FileExtension string
const (
// document
FileExtensionPDF FileExtension = "pdf"
FileExtensionTXT FileExtension = "txt"
FileExtensionDoc FileExtension = "doc"
FileExtensionDocx FileExtension = "docx"
FileExtensionMarkdown FileExtension = "md"
// sheet
FileExtensionCSV FileExtension = "csv"
FileExtensionXLSX FileExtension = "xlsx"
FileExtensionJSON FileExtension = "json"
FileExtensionJsonMaps FileExtension = "json_maps" // json of []map[string]string
// image
FileExtensionJPG FileExtension = "jpg"
FileExtensionJPEG FileExtension = "jpeg"
FileExtensionPNG FileExtension = "png"
)
func ValidateFileExtension(fileSuffix string) (ext FileExtension, support bool) {
fileExtension := FileExtension(fileSuffix)
_, ok := fileExtensionSet[fileExtension]
if !ok {
return "", false
}
return fileExtension, true
}
var fileExtensionSet = sets.Set[FileExtension]{
FileExtensionPDF: {},
FileExtensionTXT: {},
FileExtensionDoc: {},
FileExtensionDocx: {},
FileExtensionMarkdown: {},
FileExtensionCSV: {},
FileExtensionJSON: {},
FileExtensionJsonMaps: {},
FileExtensionJPG: {},
FileExtensionJPEG: {},
FileExtensionPNG: {},
}

View File

@@ -0,0 +1,21 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package parser
import "github.com/cloudwego/eino/components/document/parser"
type Parser = parser.Parser

View File

@@ -0,0 +1,26 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package progressbar
import "context"
// ProgressBar is the interface for the progress bar.
type ProgressBar interface {
AddN(n int) error
ReportError(err error) error
GetProgress(ctx context.Context) (percent int, remainSec int, errMsg string)
}

View File

@@ -0,0 +1,43 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rerank
import (
"context"
"github.com/cloudwego/eino/schema"
)
type Reranker interface {
Rerank(ctx context.Context, req *Request) (*Response, error)
}
type Request struct {
Query string
Data [][]*Data
TopN *int64
}
type Response struct {
SortedData []*Data // 高分在前
TokenUsage *int64
}
type Data struct {
Document *schema.Document
Score float64
}

View File

@@ -0,0 +1,54 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package searchstore
import "fmt"
type DSL struct {
Op Op
Field string
Value interface{} // builtin types / []*DSL
}
type Op string
const (
OpEq Op = "eq"
OpNe Op = "ne"
OpLike Op = "like"
OpIn Op = "in"
OpAnd Op = "and"
OpOr Op = "or"
)
func (d *DSL) DSL() map[string]any {
return map[string]any{"dsl": d}
}
func LoadDSL(src map[string]any) (*DSL, error) {
if src == nil {
return nil, nil
}
dsl, ok := src["dsl"].(*DSL)
if !ok {
return nil, fmt.Errorf("load dsl failed")
}
return dsl, nil
}

View File

@@ -0,0 +1,82 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package searchstore
import (
"context"
)
type Manager interface {
Create(ctx context.Context, req *CreateRequest) error
Drop(ctx context.Context, req *DropRequest) error
GetType() SearchStoreType
GetSearchStore(ctx context.Context, collectionName string) (SearchStore, error)
}
type CreateRequest struct {
CollectionName string
Fields []*Field
CollectionMeta map[string]string
}
type DropRequest struct {
CollectionName string
}
type GetSearchStoreRequest struct {
CollectionName string
}
type Field struct {
Name FieldName
Type FieldType
Description string
Nullable bool
IsPrimary bool
Indexing bool
}
type SearchStoreType string
const (
TypeVectorStore SearchStoreType = "vector"
TypeTextStore SearchStoreType = "text"
)
type FieldName = string
// 内置 field name
const (
FieldID FieldName = "id" // int64
FieldCreatorID FieldName = "creator_id" // int64
FieldTextContent FieldName = "text_content" // string
)
type FieldType int64
const (
FieldTypeUnknown FieldType = 0
FieldTypeInt64 FieldType = 1
FieldTypeText FieldType = 2
FieldTypeDenseVector FieldType = 3
FieldTypeSparseVector FieldType = 4
)

View File

@@ -0,0 +1,87 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package searchstore
import (
"github.com/cloudwego/eino/components/indexer"
"github.com/cloudwego/eino/components/retriever"
"github.com/coze-dev/coze-studio/backend/infra/contract/document/progressbar"
)
type IndexerOptions struct {
PartitionKey *string
Partition *string // 存储分片映射
IndexingFields []string
ProgressBar progressbar.ProgressBar
}
type RetrieverOptions struct {
MultiMatch *MultiMatch // 多 field 查询
PartitionKey *string
Partitions []string // 查询分片映射
}
type MultiMatch struct {
Fields []string
Query string
}
func WithIndexerPartitionKey(key string) indexer.Option {
return indexer.WrapImplSpecificOptFn(func(o *IndexerOptions) {
o.PartitionKey = &key
})
}
func WithPartition(partition string) indexer.Option {
return indexer.WrapImplSpecificOptFn(func(o *IndexerOptions) {
o.Partition = &partition
})
}
func WithIndexingFields(fields []string) indexer.Option {
return indexer.WrapImplSpecificOptFn(func(o *IndexerOptions) {
o.IndexingFields = fields
})
}
func WithProgressBar(progressBar progressbar.ProgressBar) indexer.Option {
return indexer.WrapImplSpecificOptFn(func(o *IndexerOptions) {
o.ProgressBar = progressBar
})
}
func WithMultiMatch(fields []string, query string) retriever.Option {
return retriever.WrapImplSpecificOptFn(func(o *RetrieverOptions) {
o.MultiMatch = &MultiMatch{
Fields: fields,
Query: query,
}
})
}
func WithRetrieverPartitionKey(key string) retriever.Option {
return retriever.WrapImplSpecificOptFn(func(o *RetrieverOptions) {
o.PartitionKey = &key
})
}
func WithPartitions(partitions []string) retriever.Option {
return retriever.WrapImplSpecificOptFn(func(o *RetrieverOptions) {
o.Partitions = partitions
})
}

View File

@@ -0,0 +1,32 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package searchstore
import (
"context"
"github.com/cloudwego/eino/components/indexer"
"github.com/cloudwego/eino/components/retriever"
)
type SearchStore interface {
indexer.Indexer
retriever.Retriever
Delete(ctx context.Context, ids []string) error
}

View File

@@ -0,0 +1,155 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package document
import (
"strconv"
"time"
"github.com/coze-dev/coze-studio/backend/pkg/lang/ptr"
)
type TableSchema struct {
Name string
Comment string
Columns []*Column
}
type Column struct {
ID int64
Name string
Type TableColumnType
Description string
Nullable bool
IsPrimary bool
Sequence int // 排序编号
}
type TableColumnType int
const (
TableColumnTypeUnknown TableColumnType = 0
TableColumnTypeString TableColumnType = 1
TableColumnTypeInteger TableColumnType = 2
TableColumnTypeTime TableColumnType = 3
TableColumnTypeNumber TableColumnType = 4
TableColumnTypeBoolean TableColumnType = 5
TableColumnTypeImage TableColumnType = 6
)
func (t TableColumnType) String() string {
switch t {
case TableColumnTypeString:
return "varchar"
case TableColumnTypeInteger:
return "bigint"
case TableColumnTypeTime:
return "timestamp"
case TableColumnTypeNumber:
return "double"
case TableColumnTypeBoolean:
return "boolean"
case TableColumnTypeImage:
return "image"
default:
return "unknown"
}
}
type ColumnData struct {
ColumnID int64
ColumnName string
Type TableColumnType
ValString *string
ValInteger *int64
ValTime *time.Time
ValNumber *float64
ValBoolean *bool
ValImage *string // base64 / url
}
func (d *ColumnData) GetValue() interface{} {
switch d.Type {
case TableColumnTypeString:
return d.ValString
case TableColumnTypeInteger:
return d.ValInteger
case TableColumnTypeTime:
return d.ValTime
case TableColumnTypeNumber:
return d.ValNumber
case TableColumnTypeBoolean:
return d.ValBoolean
case TableColumnTypeImage:
return d.ValImage
default:
return nil
}
}
func (d *ColumnData) GetStringValue() string {
switch d.Type {
case TableColumnTypeString:
return ptr.From(d.ValString)
case TableColumnTypeInteger:
return strconv.FormatInt(ptr.From(d.ValInteger), 10)
case TableColumnTypeTime:
return ptr.From(d.ValTime).Format(time.DateTime)
case TableColumnTypeNumber:
return strconv.FormatFloat(ptr.From(d.ValNumber), 'f', 20, 64)
case TableColumnTypeBoolean:
return strconv.FormatBool(ptr.From(d.ValBoolean))
case TableColumnTypeImage:
return ptr.From(d.ValImage)
default:
return ptr.From(d.ValString)
}
}
func (d *ColumnData) GetNullableStringValue() string {
switch d.Type {
case TableColumnTypeString:
return ptr.From(d.ValString)
case TableColumnTypeInteger:
if d.ValInteger == nil {
return ""
}
return strconv.FormatInt(ptr.From(d.ValInteger), 10)
case TableColumnTypeTime:
if d.ValTime == nil {
return ""
}
return ptr.From(d.ValTime).Format(time.DateTime)
case TableColumnTypeNumber:
if d.ValNumber == nil {
return ""
}
return strconv.FormatFloat(ptr.From(d.ValNumber), 'f', 20, 64)
case TableColumnTypeBoolean:
if d.ValBoolean == nil {
return ""
}
return strconv.FormatBool(ptr.From(d.ValBoolean))
case TableColumnTypeImage:
if d.ValImage == nil {
return ""
}
return ptr.From(d.ValImage)
default:
return ptr.From(d.ValString)
}
}

View File

@@ -0,0 +1,39 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dynconf
import "context"
// zookeeper, etcd, nacos
type Provider interface {
Initialize(ctx context.Context, namespace, group string, opts ...Option) (DynamicClient, error)
}
type DynamicClient interface {
AddListener(key string, callback func(value string, err error)) error
RemoveListener(key string) error
Get(ctx context.Context, key string) (string, error)
}
type options struct{}
type Option struct {
apply func(opts *options)
implSpecificOptFn any
}

View File

@@ -0,0 +1,37 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package embedding
import (
"context"
"github.com/cloudwego/eino/components/embedding"
)
type Embedder interface {
embedding.Embedder
EmbedStringsHybrid(ctx context.Context, texts []string, opts ...embedding.Option) ([][]float64, []map[int]float64, error) // hybrid embedding
Dimensions() int64
SupportStatus() SupportStatus
}
type SupportStatus int
const (
SupportDense SupportStatus = 1
SupportDenseAndSparse SupportStatus = 3
)

View File

@@ -0,0 +1,44 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package es
import (
"context"
)
type Client interface {
Create(ctx context.Context, index, id string, document any) error
Update(ctx context.Context, index, id string, document any) error
Delete(ctx context.Context, index, id string) error
Search(ctx context.Context, index string, req *Request) (*Response, error)
Exists(ctx context.Context, index string) (bool, error)
CreateIndex(ctx context.Context, index string, properties map[string]any) error
DeleteIndex(ctx context.Context, index string) error
Types() Types
NewBulkIndexer(index string) (BulkIndexer, error)
}
type Types interface {
NewLongNumberProperty() any
NewTextProperty() any
NewUnsignedLongNumberProperty() any
}
type BulkIndexer interface {
Add(ctx context.Context, item BulkIndexerItem) error
Close(ctx context.Context) error
}

View File

@@ -0,0 +1,73 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package es
import (
"encoding/json"
"io"
"github.com/elastic/go-elasticsearch/v8/typedapi/types/enums/totalhitsrelation"
)
type BulkIndexerItem struct {
Index string
Action string
DocumentID string
Routing string
Version *int64
VersionType string
Body io.ReadSeeker
RetryOnConflict *int
}
type Request struct {
Size *int
Query *Query
MinScore *float64
Sort []SortFiled
SearchAfter []any
From *int
}
type SortFiled struct {
Field string
Asc bool
}
type Response struct {
Hits HitsMetadata `json:"hits"`
MaxScore *float64 `json:"max_score,omitempty"`
}
type HitsMetadata struct {
Hits []Hit `json:"hits"`
MaxScore *float64 `json:"max_score,omitempty"`
// Total Total hit count information, present only if `track_total_hits` wasn't
// `false` in the search request.
Total *TotalHits `json:"total,omitempty"`
}
type Hit struct {
Id_ *string `json:"_id,omitempty"`
Score_ *float64 `json:"_score,omitempty"`
Source_ json.RawMessage `json:"_source,omitempty"`
}
type TotalHits struct {
Relation totalhitsrelation.TotalHitsRelation `json:"relation"`
Value int64 `json:"value"`
}

View File

@@ -0,0 +1,111 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package es
const (
QueryTypeEqual = "equal"
QueryTypeMatch = "match"
QueryTypeMultiMatch = "multi_match"
QueryTypeNotExists = "not_exists"
QueryTypeContains = "contains"
QueryTypeIn = "in"
)
type KV struct {
Key string
Value any
}
type QueryType string
type Query struct {
KV KV
Type QueryType
MultiMatchQuery MultiMatchQuery
Bool *BoolQuery
}
type BoolQuery struct {
Filter []Query
Must []Query
MustNot []Query
Should []Query
MinimumShouldMatch *int
}
type MultiMatchQuery struct {
Fields []string
Type string // best_fields
Query string
Operator string
}
const (
Or = "or"
And = "and"
)
func NewEqualQuery(k string, v any) Query {
return Query{
KV: KV{Key: k, Value: v},
Type: QueryTypeEqual,
}
}
func NewMatchQuery(k string, v any) Query {
return Query{
KV: KV{Key: k, Value: v},
Type: QueryTypeMatch,
}
}
func NewMultiMatchQuery(fields []string, query, typeStr, operator string) Query {
return Query{
Type: QueryTypeMultiMatch,
MultiMatchQuery: MultiMatchQuery{
Fields: fields,
Query: query,
Operator: operator,
Type: typeStr,
},
}
}
func NewNotExistsQuery(k string) Query {
return Query{
KV: KV{Key: k},
Type: QueryTypeNotExists,
}
}
func NewContainsQuery(k string, v any) Query {
return Query{
KV: KV{Key: k, Value: v},
Type: QueryTypeContains,
}
}
func NewInQuery[T any](k string, v []T) Query {
arr := make([]any, 0, len(v))
for _, item := range v {
arr = append(arr, item)
}
return Query{
KV: KV{Key: k, Value: arr},
Type: QueryTypeIn,
}
}

View File

@@ -0,0 +1,30 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package eventbus
type ConsumerOpt func(option *ConsumerOption)
type ConsumerOption struct {
Orderly *bool
// ConsumeFromWhere
}
func WithConsumerOrderly(orderly bool) ConsumerOpt {
return func(option *ConsumerOption) {
option.Orderly = &orderly
}
}

View File

@@ -0,0 +1,36 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package eventbus
import "context"
//go:generate mockgen -destination ../../../internal/mock/infra/contract/eventbus/eventbus_mock.go -package mock -source eventbus.go Factory
type Producer interface {
Send(ctx context.Context, body []byte, opts ...SendOpt) error
BatchSend(ctx context.Context, bodyArr [][]byte, opts ...SendOpt) error
}
type Consumer interface{}
type ConsumerHandler interface {
HandleMessage(ctx context.Context, msg *Message) error
}
type Message struct {
Topic string
Group string
Body []byte
}

View File

@@ -0,0 +1,29 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package eventbus
type SendOpt func(option *SendOption)
type SendOption struct {
ShardingKey *string
}
func WithShardingKey(key string) SendOpt {
return func(o *SendOption) {
o.ShardingKey = &key
}
}

View File

@@ -0,0 +1,27 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package idgen
import (
"context"
)
//go:generate mockgen -destination ../../../internal/mock/infra/contract/idgen/idgen_mock.go --package mock -source idgen.go
type IDGenerator interface {
GenID(ctx context.Context) (int64, error)
GenMultiIDs(ctx context.Context, counts int) ([]int64, error) // suggest batch size <= 200
}

View File

@@ -0,0 +1,50 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package imagex
type GetResourceOpt func(option *GetResourceOption)
type GetResourceOption struct {
Format string
Template string
Proto string
Expire int
}
func WithResourceFormat(format string) GetResourceOpt {
return func(o *GetResourceOption) {
o.Format = format
}
}
func WithResourceTemplate(template string) GetResourceOpt {
return func(o *GetResourceOption) {
o.Template = template
}
}
func WithResourceProto(proto string) GetResourceOpt {
return func(o *GetResourceOption) {
o.Proto = proto
}
}
func WithResourceExpire(expire int) GetResourceOpt {
return func(o *GetResourceOption) {
o.Expire = expire
}
}

View File

@@ -0,0 +1,71 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package imagex
import (
"context"
"time"
)
//go:generate mockgen -destination ../../../internal/mock/infra/contract/imagex/imagex_mock.go --package imagex -source imagex.go
type ImageX interface {
GetUploadAuth(ctx context.Context, opt ...UploadAuthOpt) (*SecurityToken, error)
GetUploadAuthWithExpire(ctx context.Context, expire time.Duration, opt ...UploadAuthOpt) (*SecurityToken, error)
GetResourceURL(ctx context.Context, uri string, opts ...GetResourceOpt) (*ResourceURL, error)
Upload(ctx context.Context, data []byte, opts ...UploadAuthOpt) (*UploadResult, error)
GetServerID() string
GetUploadHost(ctx context.Context) string
}
type SecurityToken struct {
AccessKeyID string `thrift:"access_key_id,1" frugal:"1,default,string" json:"access_key_id"`
SecretAccessKey string `thrift:"secret_access_key,2" frugal:"2,default,string" json:"secret_access_key"`
SessionToken string `thrift:"session_token,3" frugal:"3,default,string" json:"session_token"`
ExpiredTime string `thrift:"expired_time,4" frugal:"4,default,string" json:"expired_time"`
CurrentTime string `thrift:"current_time,5" frugal:"5,default,string" json:"current_time"`
HostScheme string `thrift:"host_scheme,6" frugal:"6,default,string" json:"host_scheme"`
}
type ResourceURL struct {
// REQUIRED; 结果图访问精简地址,与默认地址相比缺少 Bucket 部分。
CompactURL string `json:"CompactURL"`
// REQUIRED; 结果图访问默认地址。
URL string `json:"URL"`
}
type UploadResult struct {
Result *Result `json:"Results"`
RequestId string `json:"RequestId"`
FileInfo *FileInfo `json:"PluginResult"`
}
type Result struct {
Uri string `json:"Uri"`
UriStatus int `json:"UriStatus"` // 2000表示上传成功
}
type FileInfo struct {
Name string `json:"FileName"`
Uri string `json:"ImageUri"`
ImageWidth int `json:"ImageWidth"`
ImageHeight int `json:"ImageHeight"`
Md5 string `json:"ImageMd5"`
ImageFormat string `json:"ImageFormat"`
ImageSize int `json:"ImageSize"`
FrameCnt int `json:"FrameCnt"`
Duration int `json:"Duration"`
}

View File

@@ -0,0 +1,72 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package imagex
type UploadAuthOpt func(option *UploadAuthOption)
type UploadAuthOption struct {
ContentTypeBlackList []string
ContentTypeWhiteList []string
FileSizeUpLimit *string
FileSizeBottomLimit *string
KeyPtn *string
UploadOverWrite *bool
conditions map[string]string
StoreKey *string
}
func WithStoreKey(key string) UploadAuthOpt {
return func(o *UploadAuthOption) {
o.StoreKey = &key
}
}
func WithUploadKeyPtn(ptn string) UploadAuthOpt {
return func(o *UploadAuthOption) {
o.KeyPtn = &ptn
}
}
func WithUploadOverwrite(overwrite bool) UploadAuthOpt {
return func(op *UploadAuthOption) {
op.UploadOverWrite = &overwrite
}
}
func WithUploadContentTypeBlackList(blackList []string) UploadAuthOpt {
return func(op *UploadAuthOption) {
op.ContentTypeBlackList = blackList
}
}
func WithUploadContentTypeWhiteList(whiteList []string) UploadAuthOpt {
return func(op *UploadAuthOption) {
op.ContentTypeWhiteList = whiteList
}
}
func WithUploadFileSizeUpLimit(limit string) UploadAuthOpt {
return func(op *UploadAuthOption) {
op.FileSizeUpLimit = &limit
}
}
func WithUploadFileSizeBottomLimit(limit string) UploadAuthOpt {
return func(op *UploadAuthOption) {
op.FileSizeBottomLimit = &limit
}
}

View File

@@ -0,0 +1,27 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package messages2query
import (
"context"
"github.com/cloudwego/eino/schema"
)
type MessagesToQuery interface {
MessagesToQuery(ctx context.Context, messages []*schema.Message, opts ...Option) (newQuery string, err error)
}

View File

@@ -0,0 +1,31 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package messages2query
import "github.com/coze-dev/coze-studio/backend/infra/contract/chatmodel"
type Option func(o *Options)
type Options struct {
ChatModel chatmodel.BaseChatModel
}
func WithChatModel(cm chatmodel.BaseChatModel) Option {
return func(o *Options) {
o.ChatModel = cm
}
}

View File

@@ -0,0 +1,23 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package orm
import (
"gorm.io/gorm"
)
type DB = gorm.DB

View File

@@ -0,0 +1,91 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package entity
type DataType string
const (
TypeInt DataType = "INT"
TypeVarchar DataType = "VARCHAR"
TypeText DataType = "TEXT"
TypeBoolean DataType = "BOOLEAN"
TypeJson DataType = "JSON"
TypeTimestamp DataType = "TIMESTAMP"
TypeFloat DataType = "FLOAT"
TypeBigInt DataType = "BIGINT"
TypeDouble DataType = "DOUBLE"
)
type IndexType string
const (
PrimaryKey IndexType = "PRIMARY KEY"
UniqueKey IndexType = "UNIQUE KEY"
NormalKey IndexType = "KEY"
)
// AlterTableAction 定义修改表的动作类型
type AlterTableAction string
const (
AddColumn AlterTableAction = "ADD COLUMN"
DropColumn AlterTableAction = "DROP COLUMN"
ModifyColumn AlterTableAction = "MODIFY COLUMN"
RenameColumn AlterTableAction = "RENAME COLUMN"
AddIndex AlterTableAction = "ADD INDEX"
)
type LogicalOperator string
const (
AND LogicalOperator = "AND"
OR LogicalOperator = "OR"
)
type Operator string
const (
OperatorEqual Operator = "="
OperatorNotEqual Operator = "!="
OperatorGreater Operator = ">"
OperatorGreaterEqual Operator = ">="
OperatorLess Operator = "<"
OperatorLessEqual Operator = "<="
OperatorLike Operator = "LIKE"
OperatorNotLike Operator = "NOT LIKE"
OperatorIn Operator = "IN"
OperatorNotIn Operator = "NOT IN"
OperatorIsNull Operator = "IS NULL"
OperatorIsNotNull Operator = "IS NOT NULL"
)
type SortDirection string
const (
SortDirectionAsc SortDirection = "ASC" // 升序
SortDirectionDesc SortDirection = "DESC" // 降序
)
type SQLType int32
const (
SQLType_Parameterized SQLType = 0
SQLType_Raw SQLType = 1 // Complete/raw SQL
)

View File

@@ -0,0 +1,54 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package entity
type Column struct {
Name string // 保证唯一性
DataType DataType
Length *int
NotNull bool
DefaultValue *string
AutoIncrement bool // 表示该列是否为自动递增
Comment *string
}
type Index struct {
Name string
Type IndexType
Columns []string
}
type TableOption struct {
Collate *string
AutoIncrement *int64 // 设置表的自动递增初始值
Comment *string
}
type Table struct {
Name string // 保证唯一性
Columns []*Column
Indexes []*Index
Options *TableOption
CreatedAt int64
UpdatedAt int64
}
type ResultSet struct {
Columns []string
Rows []map[string]interface{}
AffectedRows int64
}

View File

@@ -0,0 +1,189 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rdb
import (
"context"
"github.com/coze-dev/coze-studio/backend/infra/contract/rdb/entity"
)
//go:generate mockgen -destination ../../../internal/mock/infra/contract/rdb/rdb_mock.go --package rdb -source rdb.go
type RDB interface {
CreateTable(ctx context.Context, req *CreateTableRequest) (*CreateTableResponse, error)
AlterTable(ctx context.Context, req *AlterTableRequest) (*AlterTableResponse, error)
DropTable(ctx context.Context, req *DropTableRequest) (*DropTableResponse, error)
GetTable(ctx context.Context, req *GetTableRequest) (*GetTableResponse, error)
InsertData(ctx context.Context, req *InsertDataRequest) (*InsertDataResponse, error)
UpdateData(ctx context.Context, req *UpdateDataRequest) (*UpdateDataResponse, error)
DeleteData(ctx context.Context, req *DeleteDataRequest) (*DeleteDataResponse, error)
SelectData(ctx context.Context, req *SelectDataRequest) (*SelectDataResponse, error)
UpsertData(ctx context.Context, req *UpsertDataRequest) (*UpsertDataResponse, error)
ExecuteSQL(ctx context.Context, req *ExecuteSQLRequest) (*ExecuteSQLResponse, error)
}
// CreateTableRequest 创建表请求
type CreateTableRequest struct {
Table *entity.Table
}
// CreateTableResponse 创建表响应
type CreateTableResponse struct {
Table *entity.Table
}
// AlterTableOperation 修改表操作
type AlterTableOperation struct {
Action entity.AlterTableAction
Column *entity.Column
OldName *string
Index *entity.Index
IndexName *string
NewTableName *string
}
// AlterTableRequest 修改表请求
type AlterTableRequest struct {
TableName string
Operations []*AlterTableOperation
}
// AlterTableResponse 修改表响应
type AlterTableResponse struct {
Table *entity.Table
}
// DropTableRequest 删除表请求
type DropTableRequest struct {
TableName string
IfExists bool
}
// DropTableResponse 删除表响应
type DropTableResponse struct {
Success bool
}
// GetTableRequest 获取表信息请求
type GetTableRequest struct {
TableName string
}
// GetTableResponse 获取表信息响应
type GetTableResponse struct {
Table *entity.Table
}
// InsertDataRequest 插入数据请求
type InsertDataRequest struct {
TableName string
Data []map[string]interface{}
}
// InsertDataResponse 插入数据响应
type InsertDataResponse struct {
AffectedRows int64
}
// Condition 定义查询条件
type Condition struct {
Field string
Operator entity.Operator
Value interface{}
}
// ComplexCondition 复杂条件
type ComplexCondition struct {
Conditions []*Condition
NestedConditions []*ComplexCondition // 与 Conditions互斥 example: WHERE (age >= 18 AND status = 'active') OR (age >= 21 AND status = 'pending')
Operator entity.LogicalOperator
}
// UpdateDataRequest 更新数据请求
type UpdateDataRequest struct {
TableName string
Data map[string]interface{}
Where *ComplexCondition
Limit *int
}
// UpdateDataResponse 更新数据响应
type UpdateDataResponse struct {
AffectedRows int64
}
// DeleteDataRequest 删除数据请求
type DeleteDataRequest struct {
TableName string
Where *ComplexCondition
Limit *int
}
// DeleteDataResponse 删除数据响应
type DeleteDataResponse struct {
AffectedRows int64
}
type OrderBy struct {
Field string // 排序字段
Direction entity.SortDirection // 排序方向
}
// SelectDataRequest 查询数据请求
type SelectDataRequest struct {
TableName string
Fields []string // 要查询的字段,如果为空则查询所有字段
Where *ComplexCondition
OrderBy []*OrderBy // 排序条件
Limit *int // 限制返回行数
Offset *int // 偏移量
}
// SelectDataResponse 查询数据响应
type SelectDataResponse struct {
ResultSet *entity.ResultSet
Total int64 // 符合条件的总记录数(不考虑分页)
}
type UpsertDataRequest struct {
TableName string
Data []map[string]interface{} // 要更新或插入的数据
Keys []string // 用于标识唯一记录的列名,为空的话默认使用主键
}
type UpsertDataResponse struct {
AffectedRows int64 // 受影响的行数
InsertedRows int64 // 新插入的行数
UpdatedRows int64 // 更新的行数
UnchangedRows int64 // 不变的行数(没有插入或更新的行数)
}
// ExecuteSQLRequest 执行SQL请求
type ExecuteSQLRequest struct {
SQL string
Params []interface{} // 用于参数化查询
// SQLType indicates the type of SQL: parameterized or raw SQL. It takes effect if OperateType is 0.
SQLType entity.SQLType
}
// ExecuteSQLResponse 执行SQL响应
type ExecuteSQLResponse struct {
ResultSet *entity.ResultSet
}

View File

@@ -0,0 +1,67 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package sqlparser
// TableColumn represents table and column name mapping
type TableColumn struct {
NewTableName *string // if nil, not replace table name
ColumnMap map[string]string // Column name mapping: key is original column name, value is new column name
}
type ColumnValue struct {
ColName string
Value interface{}
}
type PrimaryKeyValue struct {
ColName string
Values []interface{}
}
// OperationType represents the type of SQL operation
type OperationType string
// SQL operation types
const (
OperationTypeSelect OperationType = "SELECT"
OperationTypeInsert OperationType = "INSERT"
OperationTypeUpdate OperationType = "UPDATE"
OperationTypeDelete OperationType = "DELETE"
OperationTypeCreate OperationType = "CREATE"
OperationTypeAlter OperationType = "ALTER"
OperationTypeDrop OperationType = "DROP"
OperationTypeTruncate OperationType = "TRUNCATE"
OperationTypeUnknown OperationType = "UNKNOWN"
)
// SQLParser defines the interface for parsing and modifying SQL statements
type SQLParser interface {
// ParseAndModifySQL parses SQL and replaces table/column names according to the provided message
ParseAndModifySQL(sql string, tableColumns map[string]TableColumn) (string, error) // tableColumns Original table name -> new TableInfo
// GetSQLOperation identifies the operation type in the SQL statement
GetSQLOperation(sql string) (OperationType, error)
// AddColumnsToInsertSQL adds columns to the INSERT SQL statement.
AddColumnsToInsertSQL(origSQL string, addCols []ColumnValue, colVals *PrimaryKeyValue, isParam bool) (string, map[string]bool, error)
// GetTableName extracts the table name from a SQL statement. Only supports single-table select/insert/update/delete. If it has multiple tables, return first table name.
GetTableName(sql string) (string, error)
// GetInsertDataNums extracts the number of rows to be inserted from a SQL statement. Only supports single-table insert.
GetInsertDataNums(sql string) (int, error)
}

View File

@@ -0,0 +1,27 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package sse
import (
"context"
"github.com/hertz-contrib/sse"
)
type SSender interface {
Send(ctx context.Context, s *sse.Stream, event *sse.Event) error
}

View File

@@ -0,0 +1,73 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package storage
import (
"time"
)
type GetOptFn func(option *GetOption)
type GetOption struct {
Expire int64 // seconds
}
func WithExpire(expire int64) GetOptFn {
return func(o *GetOption) {
o.Expire = expire
}
}
type PutOption struct {
ContentType *string
ContentEncoding *string
ContentDisposition *string
ContentLanguage *string
Expires *time.Time
}
type PutOptFn func(option *PutOption)
func WithContentType(v string) PutOptFn {
return func(o *PutOption) {
o.ContentType = &v
}
}
func WithContentEncoding(v string) PutOptFn {
return func(o *PutOption) {
o.ContentEncoding = &v
}
}
func WithContentDisposition(v string) PutOptFn {
return func(o *PutOption) {
o.ContentDisposition = &v
}
}
func WithContentLanguage(v string) PutOptFn {
return func(o *PutOption) {
o.ContentLanguage = &v
}
}
func WithExpires(v time.Time) PutOptFn {
return func(o *PutOption) {
o.Expires = &v
}
}

View File

@@ -0,0 +1,35 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package storage
import "context"
//go:generate mockgen -destination ../../../internal/mock/infra/contract/storage/storage_mock.go -package mock -source storage.go Factory
type Storage interface {
PutObject(ctx context.Context, objectKey string, content []byte, opts ...PutOptFn) error
GetObject(ctx context.Context, objectKey string) ([]byte, error)
DeleteObject(ctx context.Context, objectKey string) error
GetObjectUrl(ctx context.Context, objectKey string, opts ...GetOptFn) (string, error)
}
type SecurityToken struct {
AccessKeyID string `thrift:"access_key_id,1" frugal:"1,default,string" json:"access_key_id"`
SecretAccessKey string `thrift:"secret_access_key,2" frugal:"2,default,string" json:"secret_access_key"`
SessionToken string `thrift:"session_token,3" frugal:"3,default,string" json:"session_token"`
ExpiredTime string `thrift:"expired_time,4" frugal:"4,default,string" json:"expired_time"`
CurrentTime string `thrift:"current_time,5" frugal:"5,default,string" json:"current_time"`
}