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

View File

@@ -0,0 +1,25 @@
// Code generated by gorm.io/gen. DO NOT EDIT.
// Code generated by gorm.io/gen. DO NOT EDIT.
// Code generated by gorm.io/gen. DO NOT EDIT.
package model
const TableNamePromptResource = "prompt_resource"
// PromptResource prompt_resource
type PromptResource struct {
ID int64 `gorm:"column:id;primaryKey;autoIncrement:true;comment:主键ID" json:"id"` // 主键ID
SpaceID int64 `gorm:"column:space_id;not null;comment:空间ID" json:"space_id"` // 空间ID
Name string `gorm:"column:name;not null;comment:名称" json:"name"` // 名称
Description string `gorm:"column:description;not null;comment:描述" json:"description"` // 描述
PromptText string `gorm:"column:prompt_text;comment:prompt正文" json:"prompt_text"` // prompt正文
Status int32 `gorm:"column:status;not null;comment:状态,0无效,1有效" json:"status"` // 状态,0无效,1有效
CreatorID int64 `gorm:"column:creator_id;not null;comment:创建者ID" json:"creator_id"` // 创建者ID
CreatedAt int64 `gorm:"column:created_at;not null;autoCreateTime:milli;comment:创建时间" json:"created_at"` // 创建时间
UpdatedAt int64 `gorm:"column:updated_at;not null;autoUpdateTime:milli;comment:更新时间" json:"updated_at"` // 更新时间
}
// TableName PromptResource's table name
func (*PromptResource) TableName() string {
return TableNamePromptResource
}

View File

@@ -0,0 +1,158 @@
/*
* 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 dal
import (
"context"
"errors"
"time"
"gorm.io/gen"
"gorm.io/gorm"
"github.com/coze-dev/coze-studio/backend/domain/prompt/entity"
"github.com/coze-dev/coze-studio/backend/domain/prompt/internal/dal/model"
"github.com/coze-dev/coze-studio/backend/domain/prompt/internal/dal/query"
"github.com/coze-dev/coze-studio/backend/infra/contract/idgen"
"github.com/coze-dev/coze-studio/backend/pkg/errorx"
"github.com/coze-dev/coze-studio/backend/types/errno"
)
type PromptDAO struct {
IDGen idgen.IDGenerator
}
func NewPromptDAO(db *gorm.DB, generator idgen.IDGenerator) *PromptDAO {
query.SetDefault(db)
return &PromptDAO{
IDGen: generator,
}
}
func (d *PromptDAO) promptResourceDO2PO(p *entity.PromptResource) *model.PromptResource {
return &model.PromptResource{
ID: p.ID,
Name: p.Name,
SpaceID: p.SpaceID,
Description: p.Description,
PromptText: p.PromptText,
Status: p.Status,
CreatorID: p.CreatorID,
CreatedAt: p.CreatedAt,
UpdatedAt: p.UpdatedAt,
}
}
func (d *PromptDAO) promptResourcePO2DO(p *model.PromptResource) *entity.PromptResource {
return &entity.PromptResource{
ID: p.ID,
Name: p.Name,
SpaceID: p.SpaceID,
Description: p.Description,
PromptText: p.PromptText,
Status: p.Status,
CreatorID: p.CreatorID,
CreatedAt: p.CreatedAt,
UpdatedAt: p.UpdatedAt,
}
}
func (d *PromptDAO) CreatePromptResource(ctx context.Context, do *entity.PromptResource) (int64, error) {
id, err := d.IDGen.GenID(ctx)
if err != nil {
return 0, errorx.New(errno.ErrPromptIDGenFailCode, errorx.KV("msg", "CreatePromptResource"))
}
p := d.promptResourceDO2PO(do)
now := time.Now().Unix()
p.ID = id
p.Status = 1
p.CreatedAt = now
p.UpdatedAt = now
promptModel := query.PromptResource
err = promptModel.WithContext(ctx).Create(p)
if err != nil {
return 0, errorx.WrapByCode(err, errno.ErrPromptCreateCode)
}
return id, nil
}
func (d *PromptDAO) GetPromptResource(ctx context.Context, promptID int64) (*entity.PromptResource, error) {
promptModel := query.PromptResource
promptWhere := []gen.Condition{
promptModel.ID.Eq(promptID),
}
promptResource, err := promptModel.WithContext(ctx).Where(promptWhere...).First()
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, errorx.WrapByCode(err, errno.ErrPromptDataNotFoundCode)
}
if err != nil {
return nil, errorx.WrapByCode(err, errno.ErrPromptGetCode)
}
do := d.promptResourcePO2DO(promptResource)
return do, nil
}
func (d *PromptDAO) UpdatePromptResource(ctx context.Context, p *entity.PromptResource) error {
updateMap := make(map[string]any, 5)
if p.Name != "" {
updateMap["name"] = p.Name
}
if p.Description != "" {
updateMap["description"] = p.Description
}
if p.PromptText != "" {
updateMap["prompt_text"] = p.PromptText
}
promptModel := query.PromptResource
promptWhere := []gen.Condition{
promptModel.ID.Eq(p.ID),
}
_, err := promptModel.WithContext(ctx).Where(promptWhere...).Updates(updateMap)
if err != nil {
return errorx.WrapByCode(err, errno.ErrPromptUpdateCode)
}
return nil
}
func (d *PromptDAO) DeletePromptResource(ctx context.Context, ID int64) error {
promptModel := query.PromptResource
promptWhere := []gen.Condition{
promptModel.ID.Eq(ID),
}
_, err := promptModel.WithContext(ctx).Where(promptWhere...).Delete()
if err != nil {
return errorx.WrapByCode(err, errno.ErrPromptDeleteCode)
}
return nil
}

View File

@@ -0,0 +1,103 @@
// Code generated by gorm.io/gen. DO NOT EDIT.
// Code generated by gorm.io/gen. DO NOT EDIT.
// Code generated by gorm.io/gen. DO NOT EDIT.
package query
import (
"context"
"database/sql"
"gorm.io/gorm"
"gorm.io/gen"
"gorm.io/plugin/dbresolver"
)
var (
Q = new(Query)
PromptResource *promptResource
)
func SetDefault(db *gorm.DB, opts ...gen.DOOption) {
*Q = *Use(db, opts...)
PromptResource = &Q.PromptResource
}
func Use(db *gorm.DB, opts ...gen.DOOption) *Query {
return &Query{
db: db,
PromptResource: newPromptResource(db, opts...),
}
}
type Query struct {
db *gorm.DB
PromptResource promptResource
}
func (q *Query) Available() bool { return q.db != nil }
func (q *Query) clone(db *gorm.DB) *Query {
return &Query{
db: db,
PromptResource: q.PromptResource.clone(db),
}
}
func (q *Query) ReadDB() *Query {
return q.ReplaceDB(q.db.Clauses(dbresolver.Read))
}
func (q *Query) WriteDB() *Query {
return q.ReplaceDB(q.db.Clauses(dbresolver.Write))
}
func (q *Query) ReplaceDB(db *gorm.DB) *Query {
return &Query{
db: db,
PromptResource: q.PromptResource.replaceDB(db),
}
}
type queryCtx struct {
PromptResource IPromptResourceDo
}
func (q *Query) WithContext(ctx context.Context) *queryCtx {
return &queryCtx{
PromptResource: q.PromptResource.WithContext(ctx),
}
}
func (q *Query) Transaction(fc func(tx *Query) error, opts ...*sql.TxOptions) error {
return q.db.Transaction(func(tx *gorm.DB) error { return fc(q.clone(tx)) }, opts...)
}
func (q *Query) Begin(opts ...*sql.TxOptions) *QueryTx {
tx := q.db.Begin(opts...)
return &QueryTx{Query: q.clone(tx), Error: tx.Error}
}
type QueryTx struct {
*Query
Error error
}
func (q *QueryTx) Commit() error {
return q.db.Commit().Error
}
func (q *QueryTx) Rollback() error {
return q.db.Rollback().Error
}
func (q *QueryTx) SavePoint(name string) error {
return q.db.SavePoint(name).Error
}
func (q *QueryTx) RollbackTo(name string) error {
return q.db.RollbackTo(name).Error
}

View File

@@ -0,0 +1,413 @@
// Code generated by gorm.io/gen. DO NOT EDIT.
// Code generated by gorm.io/gen. DO NOT EDIT.
// Code generated by gorm.io/gen. DO NOT EDIT.
package query
import (
"context"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"gorm.io/gorm/schema"
"gorm.io/gen"
"gorm.io/gen/field"
"gorm.io/plugin/dbresolver"
"github.com/coze-dev/coze-studio/backend/domain/prompt/internal/dal/model"
)
func newPromptResource(db *gorm.DB, opts ...gen.DOOption) promptResource {
_promptResource := promptResource{}
_promptResource.promptResourceDo.UseDB(db, opts...)
_promptResource.promptResourceDo.UseModel(&model.PromptResource{})
tableName := _promptResource.promptResourceDo.TableName()
_promptResource.ALL = field.NewAsterisk(tableName)
_promptResource.ID = field.NewInt64(tableName, "id")
_promptResource.SpaceID = field.NewInt64(tableName, "space_id")
_promptResource.Name = field.NewString(tableName, "name")
_promptResource.Description = field.NewString(tableName, "description")
_promptResource.PromptText = field.NewString(tableName, "prompt_text")
_promptResource.Status = field.NewInt32(tableName, "status")
_promptResource.CreatorID = field.NewInt64(tableName, "creator_id")
_promptResource.CreatedAt = field.NewInt64(tableName, "created_at")
_promptResource.UpdatedAt = field.NewInt64(tableName, "updated_at")
_promptResource.fillFieldMap()
return _promptResource
}
// promptResource prompt_resource
type promptResource struct {
promptResourceDo
ALL field.Asterisk
ID field.Int64 // 主键ID
SpaceID field.Int64 // 空间ID
Name field.String // 名称
Description field.String // 描述
PromptText field.String // prompt正文
Status field.Int32 // 状态,0无效,1有效
CreatorID field.Int64 // 创建者ID
CreatedAt field.Int64 // 创建时间
UpdatedAt field.Int64 // 更新时间
fieldMap map[string]field.Expr
}
func (p promptResource) Table(newTableName string) *promptResource {
p.promptResourceDo.UseTable(newTableName)
return p.updateTableName(newTableName)
}
func (p promptResource) As(alias string) *promptResource {
p.promptResourceDo.DO = *(p.promptResourceDo.As(alias).(*gen.DO))
return p.updateTableName(alias)
}
func (p *promptResource) updateTableName(table string) *promptResource {
p.ALL = field.NewAsterisk(table)
p.ID = field.NewInt64(table, "id")
p.SpaceID = field.NewInt64(table, "space_id")
p.Name = field.NewString(table, "name")
p.Description = field.NewString(table, "description")
p.PromptText = field.NewString(table, "prompt_text")
p.Status = field.NewInt32(table, "status")
p.CreatorID = field.NewInt64(table, "creator_id")
p.CreatedAt = field.NewInt64(table, "created_at")
p.UpdatedAt = field.NewInt64(table, "updated_at")
p.fillFieldMap()
return p
}
func (p *promptResource) GetFieldByName(fieldName string) (field.OrderExpr, bool) {
_f, ok := p.fieldMap[fieldName]
if !ok || _f == nil {
return nil, false
}
_oe, ok := _f.(field.OrderExpr)
return _oe, ok
}
func (p *promptResource) fillFieldMap() {
p.fieldMap = make(map[string]field.Expr, 9)
p.fieldMap["id"] = p.ID
p.fieldMap["space_id"] = p.SpaceID
p.fieldMap["name"] = p.Name
p.fieldMap["description"] = p.Description
p.fieldMap["prompt_text"] = p.PromptText
p.fieldMap["status"] = p.Status
p.fieldMap["creator_id"] = p.CreatorID
p.fieldMap["created_at"] = p.CreatedAt
p.fieldMap["updated_at"] = p.UpdatedAt
}
func (p promptResource) clone(db *gorm.DB) promptResource {
p.promptResourceDo.ReplaceConnPool(db.Statement.ConnPool)
return p
}
func (p promptResource) replaceDB(db *gorm.DB) promptResource {
p.promptResourceDo.ReplaceDB(db)
return p
}
type promptResourceDo struct{ gen.DO }
type IPromptResourceDo interface {
gen.SubQuery
Debug() IPromptResourceDo
WithContext(ctx context.Context) IPromptResourceDo
WithResult(fc func(tx gen.Dao)) gen.ResultInfo
ReplaceDB(db *gorm.DB)
ReadDB() IPromptResourceDo
WriteDB() IPromptResourceDo
As(alias string) gen.Dao
Session(config *gorm.Session) IPromptResourceDo
Columns(cols ...field.Expr) gen.Columns
Clauses(conds ...clause.Expression) IPromptResourceDo
Not(conds ...gen.Condition) IPromptResourceDo
Or(conds ...gen.Condition) IPromptResourceDo
Select(conds ...field.Expr) IPromptResourceDo
Where(conds ...gen.Condition) IPromptResourceDo
Order(conds ...field.Expr) IPromptResourceDo
Distinct(cols ...field.Expr) IPromptResourceDo
Omit(cols ...field.Expr) IPromptResourceDo
Join(table schema.Tabler, on ...field.Expr) IPromptResourceDo
LeftJoin(table schema.Tabler, on ...field.Expr) IPromptResourceDo
RightJoin(table schema.Tabler, on ...field.Expr) IPromptResourceDo
Group(cols ...field.Expr) IPromptResourceDo
Having(conds ...gen.Condition) IPromptResourceDo
Limit(limit int) IPromptResourceDo
Offset(offset int) IPromptResourceDo
Count() (count int64, err error)
Scopes(funcs ...func(gen.Dao) gen.Dao) IPromptResourceDo
Unscoped() IPromptResourceDo
Create(values ...*model.PromptResource) error
CreateInBatches(values []*model.PromptResource, batchSize int) error
Save(values ...*model.PromptResource) error
First() (*model.PromptResource, error)
Take() (*model.PromptResource, error)
Last() (*model.PromptResource, error)
Find() ([]*model.PromptResource, error)
FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error) (results []*model.PromptResource, err error)
FindInBatches(result *[]*model.PromptResource, batchSize int, fc func(tx gen.Dao, batch int) error) error
Pluck(column field.Expr, dest interface{}) error
Delete(...*model.PromptResource) (info gen.ResultInfo, err error)
Update(column field.Expr, value interface{}) (info gen.ResultInfo, err error)
UpdateSimple(columns ...field.AssignExpr) (info gen.ResultInfo, err error)
Updates(value interface{}) (info gen.ResultInfo, err error)
UpdateColumn(column field.Expr, value interface{}) (info gen.ResultInfo, err error)
UpdateColumnSimple(columns ...field.AssignExpr) (info gen.ResultInfo, err error)
UpdateColumns(value interface{}) (info gen.ResultInfo, err error)
UpdateFrom(q gen.SubQuery) gen.Dao
Attrs(attrs ...field.AssignExpr) IPromptResourceDo
Assign(attrs ...field.AssignExpr) IPromptResourceDo
Joins(fields ...field.RelationField) IPromptResourceDo
Preload(fields ...field.RelationField) IPromptResourceDo
FirstOrInit() (*model.PromptResource, error)
FirstOrCreate() (*model.PromptResource, error)
FindByPage(offset int, limit int) (result []*model.PromptResource, count int64, err error)
ScanByPage(result interface{}, offset int, limit int) (count int64, err error)
Scan(result interface{}) (err error)
Returning(value interface{}, columns ...string) IPromptResourceDo
UnderlyingDB() *gorm.DB
schema.Tabler
}
func (p promptResourceDo) Debug() IPromptResourceDo {
return p.withDO(p.DO.Debug())
}
func (p promptResourceDo) WithContext(ctx context.Context) IPromptResourceDo {
return p.withDO(p.DO.WithContext(ctx))
}
func (p promptResourceDo) ReadDB() IPromptResourceDo {
return p.Clauses(dbresolver.Read)
}
func (p promptResourceDo) WriteDB() IPromptResourceDo {
return p.Clauses(dbresolver.Write)
}
func (p promptResourceDo) Session(config *gorm.Session) IPromptResourceDo {
return p.withDO(p.DO.Session(config))
}
func (p promptResourceDo) Clauses(conds ...clause.Expression) IPromptResourceDo {
return p.withDO(p.DO.Clauses(conds...))
}
func (p promptResourceDo) Returning(value interface{}, columns ...string) IPromptResourceDo {
return p.withDO(p.DO.Returning(value, columns...))
}
func (p promptResourceDo) Not(conds ...gen.Condition) IPromptResourceDo {
return p.withDO(p.DO.Not(conds...))
}
func (p promptResourceDo) Or(conds ...gen.Condition) IPromptResourceDo {
return p.withDO(p.DO.Or(conds...))
}
func (p promptResourceDo) Select(conds ...field.Expr) IPromptResourceDo {
return p.withDO(p.DO.Select(conds...))
}
func (p promptResourceDo) Where(conds ...gen.Condition) IPromptResourceDo {
return p.withDO(p.DO.Where(conds...))
}
func (p promptResourceDo) Order(conds ...field.Expr) IPromptResourceDo {
return p.withDO(p.DO.Order(conds...))
}
func (p promptResourceDo) Distinct(cols ...field.Expr) IPromptResourceDo {
return p.withDO(p.DO.Distinct(cols...))
}
func (p promptResourceDo) Omit(cols ...field.Expr) IPromptResourceDo {
return p.withDO(p.DO.Omit(cols...))
}
func (p promptResourceDo) Join(table schema.Tabler, on ...field.Expr) IPromptResourceDo {
return p.withDO(p.DO.Join(table, on...))
}
func (p promptResourceDo) LeftJoin(table schema.Tabler, on ...field.Expr) IPromptResourceDo {
return p.withDO(p.DO.LeftJoin(table, on...))
}
func (p promptResourceDo) RightJoin(table schema.Tabler, on ...field.Expr) IPromptResourceDo {
return p.withDO(p.DO.RightJoin(table, on...))
}
func (p promptResourceDo) Group(cols ...field.Expr) IPromptResourceDo {
return p.withDO(p.DO.Group(cols...))
}
func (p promptResourceDo) Having(conds ...gen.Condition) IPromptResourceDo {
return p.withDO(p.DO.Having(conds...))
}
func (p promptResourceDo) Limit(limit int) IPromptResourceDo {
return p.withDO(p.DO.Limit(limit))
}
func (p promptResourceDo) Offset(offset int) IPromptResourceDo {
return p.withDO(p.DO.Offset(offset))
}
func (p promptResourceDo) Scopes(funcs ...func(gen.Dao) gen.Dao) IPromptResourceDo {
return p.withDO(p.DO.Scopes(funcs...))
}
func (p promptResourceDo) Unscoped() IPromptResourceDo {
return p.withDO(p.DO.Unscoped())
}
func (p promptResourceDo) Create(values ...*model.PromptResource) error {
if len(values) == 0 {
return nil
}
return p.DO.Create(values)
}
func (p promptResourceDo) CreateInBatches(values []*model.PromptResource, batchSize int) error {
return p.DO.CreateInBatches(values, batchSize)
}
// Save : !!! underlying implementation is different with GORM
// The method is equivalent to executing the statement: db.Clauses(clause.OnConflict{UpdateAll: true}).Create(values)
func (p promptResourceDo) Save(values ...*model.PromptResource) error {
if len(values) == 0 {
return nil
}
return p.DO.Save(values)
}
func (p promptResourceDo) First() (*model.PromptResource, error) {
if result, err := p.DO.First(); err != nil {
return nil, err
} else {
return result.(*model.PromptResource), nil
}
}
func (p promptResourceDo) Take() (*model.PromptResource, error) {
if result, err := p.DO.Take(); err != nil {
return nil, err
} else {
return result.(*model.PromptResource), nil
}
}
func (p promptResourceDo) Last() (*model.PromptResource, error) {
if result, err := p.DO.Last(); err != nil {
return nil, err
} else {
return result.(*model.PromptResource), nil
}
}
func (p promptResourceDo) Find() ([]*model.PromptResource, error) {
result, err := p.DO.Find()
return result.([]*model.PromptResource), err
}
func (p promptResourceDo) FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error) (results []*model.PromptResource, err error) {
buf := make([]*model.PromptResource, 0, batchSize)
err = p.DO.FindInBatches(&buf, batchSize, func(tx gen.Dao, batch int) error {
defer func() { results = append(results, buf...) }()
return fc(tx, batch)
})
return results, err
}
func (p promptResourceDo) FindInBatches(result *[]*model.PromptResource, batchSize int, fc func(tx gen.Dao, batch int) error) error {
return p.DO.FindInBatches(result, batchSize, fc)
}
func (p promptResourceDo) Attrs(attrs ...field.AssignExpr) IPromptResourceDo {
return p.withDO(p.DO.Attrs(attrs...))
}
func (p promptResourceDo) Assign(attrs ...field.AssignExpr) IPromptResourceDo {
return p.withDO(p.DO.Assign(attrs...))
}
func (p promptResourceDo) Joins(fields ...field.RelationField) IPromptResourceDo {
for _, _f := range fields {
p = *p.withDO(p.DO.Joins(_f))
}
return &p
}
func (p promptResourceDo) Preload(fields ...field.RelationField) IPromptResourceDo {
for _, _f := range fields {
p = *p.withDO(p.DO.Preload(_f))
}
return &p
}
func (p promptResourceDo) FirstOrInit() (*model.PromptResource, error) {
if result, err := p.DO.FirstOrInit(); err != nil {
return nil, err
} else {
return result.(*model.PromptResource), nil
}
}
func (p promptResourceDo) FirstOrCreate() (*model.PromptResource, error) {
if result, err := p.DO.FirstOrCreate(); err != nil {
return nil, err
} else {
return result.(*model.PromptResource), nil
}
}
func (p promptResourceDo) FindByPage(offset int, limit int) (result []*model.PromptResource, count int64, err error) {
result, err = p.Offset(offset).Limit(limit).Find()
if err != nil {
return
}
if size := len(result); 0 < limit && 0 < size && size < limit {
count = int64(size + offset)
return
}
count, err = p.Offset(-1).Limit(-1).Count()
return
}
func (p promptResourceDo) ScanByPage(result interface{}, offset int, limit int) (count int64, err error) {
count, err = p.Count()
if err != nil {
return
}
err = p.Offset(offset).Limit(limit).Scan(result)
return
}
func (p promptResourceDo) Scan(result interface{}) (err error) {
return p.DO.Scan(result)
}
func (p promptResourceDo) Delete(models ...*model.PromptResource) (result gen.ResultInfo, err error) {
return p.DO.Delete(models)
}
func (p *promptResourceDo) withDO(do gen.Dao) *promptResourceDo {
p.DO = *do.(*gen.DO)
return p
}