oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package ownermode
2
3import (
4 "amadan.net/oficina/equipo/internal/equipo/kernel"
5 "amadan.net/oficina/equipo/internal/equipo/team"
6 "amadan.net/oficina/equipo/internal/runnerproto"
7 "context"
8 "database/sql"
9 "encoding/json"
10 "errors"
11 "gorm.io/gorm"
12)
13
14func Defaults(ctx context.Context, tx *gorm.DB, core *team.Store, instance string, sel runnerproto.Selection, sqlTx func(*gorm.DB) (*sql.Tx, error), audit func(*gorm.DB, kernel.Actor, string, string) error) error {
15 var row kernel.BudgetRecord
16 err := tx.Where("team_id=?", instance).Take(&row).Error
17 if errors.Is(err, gorm.ErrRecordNotFound) {
18 config := kernel.BudgetConfig{Revision: 1, Team: team.PeriodLimits{Calls: 500, InputTokens: 16000000, OutputTokens: 2000000, Seconds: 360000}}
19 raw, err := json.Marshal(config)
20 if err != nil {
21 return err
22 }
23 row = kernel.BudgetRecord{TeamID: instance, Revision: 1, Config: string(raw)}
24 if err = tx.Create(&row).Error; err != nil {
25 return err
26 }
27 sqltx, err := sqlTx(tx)
28 if err != nil {
29 return err
30 }
31 if err = core.SyncBudgetsTx(ctx, sqltx, team.AccountID(instance), team.BudgetSet{Revision: 1, Team: config.Team}); err != nil {
32 return err
33 }
34 if err = audit(tx, kernel.Actor{Address: sel.Person}, "budget.owner_defaults", instance); err != nil {
35 return err
36 }
37 } else if err != nil {
38 return err
39 }
40 _, _, err = core.CoordinatorLimits(ctx, team.AccountID(instance), sel.Person)
41 if !errors.Is(err, team.ErrBudget) {
42 return err
43 }
44 var config kernel.BudgetConfig
45 if err = json.Unmarshal([]byte(row.Config), &config); err != nil {
46 return err
47 }
48 limits := team.Limits{Calls: min(100, config.Team.Calls), InputTokens: min(3200000, config.Team.InputTokens), OutputTokens: min(400000, config.Team.OutputTokens), Seconds: min(120000, config.Team.Seconds)}
49 if err = core.ConfigureCoordinatorLimits(ctx, team.AccountID(instance), sel.Person, 0, limits); err != nil {
50 return err
51 }
52 return audit(tx, kernel.Actor{Address: sel.Person}, "coordinator.owner_defaults", sel.Person)
53}
54