Skip to content

Commit

Permalink
chore: add api to return image stream
Browse files Browse the repository at this point in the history
  • Loading branch information
0xff-dev committed Apr 8, 2024
1 parent 397bcaf commit 4976704
Show file tree
Hide file tree
Showing 8 changed files with 386 additions and 195 deletions.
57 changes: 57 additions & 0 deletions apiserver/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,63 @@ const docTemplate = `{
"host": "{{.Host}}",
"basePath": "{{.BasePath}}",
"paths": {
"/bff/icon": {
"get": {
"description": "Get app image",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"MinioAPI"
],
"summary": "Get app image",
"parameters": [
{
"type": "string",
"description": "application name",
"name": "application",
"in": "query",
"required": true
},
{
"type": "string",
"description": "Name of the bucket",
"name": "namespace",
"in": "query",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "string"
}
},
"400": {
"description": "Bad Request",
"schema": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
}
}
}
},
"/bff/model/files": {
"delete": {
"description": "Delete files",
Expand Down
57 changes: 57 additions & 0 deletions apiserver/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,63 @@
"host": "localhost:8081",
"basePath": "/",
"paths": {
"/bff/icon": {
"get": {
"description": "Get app image",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"MinioAPI"
],
"summary": "Get app image",
"parameters": [
{
"type": "string",
"description": "application name",
"name": "application",
"in": "query",
"required": true
},
{
"type": "string",
"description": "Name of the bucket",
"name": "namespace",
"in": "query",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "string"
}
},
"400": {
"description": "Bad Request",
"schema": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
}
}
}
},
"/bff/model/files": {
"delete": {
"description": "Delete files",
Expand Down
38 changes: 38 additions & 0 deletions apiserver/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,44 @@ info:
title: ApiServer Swagger API
version: "1.0"
paths:
/bff/icon:
get:
consumes:
- application/json
description: Get app image
parameters:
- description: application name
in: query
name: application
required: true
type: string
- description: Name of the bucket
in: query
name: namespace
required: true
type: string
produces:
- application/json
responses:
"200":
description: OK
schema:
type: string
"400":
description: Bad Request
schema:
additionalProperties:
type: string
type: object
"500":
description: Internal Server Error
schema:
additionalProperties:
type: string
type: object
summary: Get app image
tags:
- MinioAPI
/bff/model/files:
delete:
consumes:
Expand Down
31 changes: 15 additions & 16 deletions apiserver/pkg/application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
apiprompt "github.com/kubeagi/arcadia/api/app-node/prompt/v1alpha1"
apiretriever "github.com/kubeagi/arcadia/api/app-node/retriever/v1alpha1"
"github.com/kubeagi/arcadia/api/base/v1alpha1"
pkgconf "github.com/kubeagi/arcadia/apiserver/config"
"github.com/kubeagi/arcadia/apiserver/graph/generated"
"github.com/kubeagi/arcadia/apiserver/pkg/common"
"github.com/kubeagi/arcadia/apiserver/pkg/utils"
Expand Down Expand Up @@ -73,15 +74,15 @@ func addDefaultValue(gApp *generated.Application, app *v1alpha1.Application) {
gApp.ConversionWindowSize = pointer.Int(5)
}

func cr2app(ctx context.Context, c client.Client, prompt *apiprompt.Prompt, chainConfig *apichain.CommonChainConfig, retriever *apiretriever.CommonRetrieverConfig, app *v1alpha1.Application, agent *apiagent.Agent, doc *apidocumentloader.DocumentLoader, enableRerank, enableMultiQuery *bool, rerankModel *string) (*generated.Application, error) {
func cr2app(prompt *apiprompt.Prompt, chainConfig *apichain.CommonChainConfig, retriever *apiretriever.CommonRetrieverConfig, app *v1alpha1.Application, agent *apiagent.Agent, doc *apidocumentloader.DocumentLoader, enableRerank, enableMultiQuery *bool, rerankModel *string) (*generated.Application, error) {
if app == nil {
return nil, errors.New("no app found")
}
condition := app.Status.GetCondition(v1alpha1.TypeReady)
UpdateTimestamp := &condition.LastTransitionTime.Time
status := common.GetObjStatus(app)

icon, _ := common.AppIconLink(ctx, app, c)
icon := common.AppIconLink(app, pkgconf.GetConfig().PlaygroundEndpointPrefix)
gApp := &generated.Application{
Metadata: &generated.ApplicationMetadata{
Name: app.Name,
Expand Down Expand Up @@ -153,22 +154,20 @@ func cr2app(ctx context.Context, c client.Client, prompt *apiprompt.Prompt, chai
return gApp, nil
}

func appConverterHelper(ctx context.Context, c client.Client) common.ResourceConverter {
return func(objApp client.Object) (generated.PageNode, error) {
app, ok := objApp.(*v1alpha1.Application)
if !ok {
return nil, errors.New("can't convert client.Object to Application")
}
return app2metadata(ctx, c, app)
func app2metadataConverter(objApp client.Object) (generated.PageNode, error) {
app, ok := objApp.(*v1alpha1.Application)
if !ok {
return nil, errors.New("can't convert client.Object to Application")
}
return app2metadata(app)
}

func app2metadata(ctx context.Context, c client.Client, app *v1alpha1.Application) (*generated.ApplicationMetadata, error) {
func app2metadata(app *v1alpha1.Application) (*generated.ApplicationMetadata, error) {
condition := app.Status.GetCondition(v1alpha1.TypeReady)
UpdateTimestamp := &condition.LastTransitionTime.Time
status := common.GetObjStatus(app)

icon, _ := common.AppIconLink(ctx, app, c)
icon := common.AppIconLink(app, pkgconf.GetConfig().PlaygroundEndpointPrefix)
return &generated.ApplicationMetadata{
Name: app.Name,
Namespace: app.Namespace,
Expand Down Expand Up @@ -219,7 +218,7 @@ func CreateApplication(ctx context.Context, c client.Client, input generated.Cre
if err := c.Create(ctx, app); err != nil {
return nil, err
}
return app2metadata(ctx, c, app)
return app2metadata(app)
}

func UpdateApplication(ctx context.Context, c client.Client, input generated.UpdateApplicationMetadataInput) (*generated.ApplicationMetadata, error) {
Expand Down Expand Up @@ -250,7 +249,7 @@ func UpdateApplication(ctx context.Context, c client.Client, input generated.Upd
return nil, err
}
}
return app2metadata(ctx, c, app)
return app2metadata(app)
}

func DeleteApplication(ctx context.Context, c client.Client, input generated.DeleteCommonInput) (*string, error) {
Expand Down Expand Up @@ -402,7 +401,7 @@ func GetApplication(ctx context.Context, c client.Client, name, namespace string
return nil, err
}

return cr2app(ctx, c, prompt, chainConfig, retriever, app, agent, doc, pointer.Bool(enableRerankRetriever), pointer.Bool(enableMultiQueryRetriever), pointer.String(rerankModel))
return cr2app(prompt, chainConfig, retriever, app, agent, doc, pointer.Bool(enableRerankRetriever), pointer.Bool(enableMultiQueryRetriever), pointer.String(rerankModel))
}

func ListApplicationMeatadatas(ctx context.Context, c client.Client, input generated.ListCommonInput) (*generated.PaginatedResult, error) {
Expand All @@ -425,7 +424,7 @@ func ListApplicationMeatadatas(ctx context.Context, c client.Client, input gener
for i := range res.Items {
items[i] = &res.Items[i]
}
return common.ListReources(items, page, pageSize, appConverterHelper(ctx, c), filter...)
return common.ListReources(items, page, pageSize, app2metadataConverter, filter...)
}

func UpdateApplicationConfig(ctx context.Context, c client.Client, input generated.UpdateApplicationConfigInput) (*generated.Application, error) {
Expand Down Expand Up @@ -759,7 +758,7 @@ func UpdateApplicationConfig(ctx context.Context, c client.Client, input generat
}
}

return cr2app(ctx, c, prompt, chainConfig, retriever, app, agent, documentLoader, pointer.Bool(hasRerankRetriever), pointer.Bool(hasMultiQueryRetriever), pointer.String(rerankModel))
return cr2app(prompt, chainConfig, retriever, app, agent, documentLoader, pointer.Bool(hasRerankRetriever), pointer.Bool(hasMultiQueryRetriever), pointer.String(rerankModel))
}

func mutateApp(app *v1alpha1.Application, input generated.UpdateApplicationConfigInput, hasMultiQueryRetriever, hasRerankRetriever bool) error {
Expand Down
11 changes: 3 additions & 8 deletions apiserver/pkg/chat/chat_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (

apiretriever "github.com/kubeagi/arcadia/api/app-node/retriever/v1alpha1"
"github.com/kubeagi/arcadia/api/base/v1alpha1"
"github.com/kubeagi/arcadia/apiserver/config"
"github.com/kubeagi/arcadia/apiserver/pkg/auth"
"github.com/kubeagi/arcadia/apiserver/pkg/chat/storage"
"github.com/kubeagi/arcadia/apiserver/pkg/client"
Expand Down Expand Up @@ -429,7 +430,7 @@ func (cs *ChatServer) FillAppIconToConversations(ctx context.Context, conversati
i++
}
result := make([]string, len(appMap))
g, ctx := errgroup.WithContext(ctx)
g, _ := errgroup.WithContext(ctx)
g.SetLimit(10)
for key, index := range appMap {
key, index := key, index
Expand All @@ -441,13 +442,7 @@ func (cs *ChatServer) FillAppIconToConversations(ctx context.Context, conversati
}
app.Name = name
app.Namespace = ns
link, err := common.AppIconLink(ctx, app, cs.cli)
if err != nil {
// FIXME: Currently, there is a request for an application that cannot be found in a conversation in the database,
// causing other conversations to be unable to add icons, so an error is encountered here and no error is returned.
klog.Errorf("failed to get application %s in namespace %s, error %s", name, ns, err)
return nil
}
link := common.AppIconLink(app, config.GetConfig().PlaygroundEndpointPrefix)
result[index] = link
return nil
})
Expand Down
17 changes: 5 additions & 12 deletions apiserver/pkg/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ import (
"context"
"errors"
"fmt"
"net/url"
"strings"
"time"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
Expand Down Expand Up @@ -309,15 +307,10 @@ func NewListOptions(input generated.ListCommonInput) ([]client.ListOption, error
return opts, nil
}

func AppIconLink(ctx context.Context, app *v1alpha1.Application, client client.Client) (string, error) {
ds, err := SystemDatasourceOSS(ctx, client)
if err != nil {
return "", err
}
name := fmt.Sprintf("application/%s/icon/%s", app.Name, app.Name)
u, err := ds.Client.PresignedGetObject(ctx, app.Namespace, name, 24*time.Hour, url.Values{})
if err != nil {
return "", err
func AppIconLink(app *v1alpha1.Application, endpointPrefix string) string {
base := fmt.Sprintf("/bff/icon?namespace=%s&application=%s", app.Namespace, app.Name)
if endpointPrefix != "" {
base = "/" + endpointPrefix + base
}
return u.String(), nil
return base
}
3 changes: 2 additions & 1 deletion apiserver/pkg/gpt/gpt.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/kubeagi/arcadia/api/base/v1alpha1"
pkgconf "github.com/kubeagi/arcadia/apiserver/config"
"github.com/kubeagi/arcadia/apiserver/graph/generated"
"github.com/kubeagi/arcadia/apiserver/pkg/chat"
"github.com/kubeagi/arcadia/apiserver/pkg/chat/storage"
Expand All @@ -46,7 +47,7 @@ func app2gpt(ctx context.Context, app *v1alpha1.Application, c client.Client) (*
return nil, errors.New("no app found")
}

icon, _ := common.AppIconLink(ctx, app, c)
icon := common.AppIconLink(app, pkgconf.GetConfig().PlaygroundEndpointPrefix)
gpt := &generated.Gpt{
Name: pointer.String(strings.Join([]string{app.Namespace, app.Name}, "/")),
DisplayName: pointer.String(app.Spec.DisplayName),
Expand Down
Loading

0 comments on commit 4976704

Please sign in to comment.