feat(tool-calling) Big refactor on how tool calling is handled

these commits are too big
This commit is contained in:
2025-03-22 20:46:26 +00:00
parent 13e5ed9f9e
commit 410df01b4d
8 changed files with 294 additions and 147 deletions

View File

@@ -3,11 +3,12 @@ package models
import (
"context"
"database/sql"
. "github.com/go-jet/jet/v2/postgres"
"log"
"screenmark/screenmark/.gen/haystack/haystack/model"
. "screenmark/screenmark/.gen/haystack/haystack/table"
. "github.com/go-jet/jet/v2/postgres"
"github.com/google/uuid"
)
@@ -15,60 +16,37 @@ type LocationModel struct {
dbPool *sql.DB
}
// This looks stupid
func getValues(location model.Locations) []any {
arr := make([]any, 0)
if location.Address != nil {
arr = append(arr, *location.Address)
} else {
arr = append(arr, nil)
}
if location.Coordinates != nil {
arr = append(arr, *location.Coordinates)
} else {
arr = append(arr, nil)
}
if location.Description != nil {
arr = append(arr, *location.Description)
} else {
arr = append(arr, nil)
}
return arr
}
func (m LocationModel) List(ctx context.Context, userId uuid.UUID) ([]model.Locations, error) {
listLocationsStmt := SELECT(Locations.AllColumns, ImageLocations.AllColumns, UserImages.AllColumns).
listLocationsStmt := SELECT(Locations.AllColumns).
FROM(
Locations.
INNER_JOIN(ImageLocations, ImageLocations.LocationID.EQ(Locations.ID)).
INNER_JOIN(UserImages, UserImages.ImageID.EQ(ImageLocations.ImageID)),
).WHERE(UserImages.UserID.EQ(UUID(userId)))
INNER_JOIN(UserLocations, UserLocations.LocationID.EQ(Locations.ID)),
).
WHERE(UserLocations.UserID.EQ(UUID(userId)))
locations := []model.Locations{}
err := listLocationsStmt.QueryContext(ctx, m.dbPool, &locations)
return locations, err
}
func (m LocationModel) Save(ctx context.Context, locations []model.Locations) ([]model.Locations, error) {
func (m LocationModel) Save(ctx context.Context, userId uuid.UUID, location model.Locations) (model.Locations, error) {
insertLocationStmt := Locations.
INSERT(Locations.Name, Locations.Address, Locations.Coordinates, Locations.Description)
INSERT(Locations.Name, Locations.Address, Locations.Coordinates, Locations.Description).
VALUES(location.Name, location.Address, location.Coordinates, location.Description).
RETURNING(Locations.AllColumns)
for _, location := range locations {
insertLocationStmt = insertLocationStmt.VALUES(location.Name, getValues(location)...)
insertedLocation := model.Locations{}
err := insertLocationStmt.QueryContext(ctx, m.dbPool, &insertedLocation)
if err != nil {
return model.Locations{}, err
}
insertLocationStmt = insertLocationStmt.RETURNING(Locations.AllColumns)
insertUserLocationStmt := UserLocations.
INSERT(UserLocations.UserID, UserLocations.LocationID).
VALUES(userId, insertedLocation.ID)
log.Println(insertLocationStmt.DebugSql())
insertedLocation := []model.Locations{}
err := insertLocationStmt.QueryContext(ctx, m.dbPool, &insertedLocation)
_, err = insertUserLocationStmt.ExecContext(ctx, m.dbPool)
return insertedLocation, err
}