Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[add] sui build IgnoreDocument support & neo local model support #508

Merged
merged 3 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions neo/message/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ func NewOpenAI(data []byte) *JSON {
}

msg := makeMessage()
data = []byte(strings.TrimPrefix(string(data), "data: "))
text := string(data)
data = []byte(strings.TrimPrefix(text, "data: "))
switch {

case strings.Contains(string(data), `"delta":{"content"`):
case strings.Contains(text, `"delta":{`) && strings.Contains(text, `"content":`):
var message openai.Message
err := jsoniter.Unmarshal(data, &message)
if err != nil {
Expand All @@ -43,12 +43,12 @@ func NewOpenAI(data []byte) *JSON {
}
break

case strings.Contains(string(data), `[DONE]`):
case strings.Contains(text, `[DONE]`):
msg.Done = true
break

default:
msg.Error = string(data)
msg.Error = text
}

return &JSON{msg}
Expand Down
52 changes: 26 additions & 26 deletions neo/neo.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,32 +257,32 @@ func (neo *DSL) Answer(ctx command.Context, question string, answer Answer) erro

return !msg.IsDone()

case <-ctx.Done():
if err := ctx.Err(); err != nil {
message.New().Text(err.Error()).Write(w)
}

if len(errorMsg) > 0 {

var errData openai.ErrorMessage
err := jsoniter.Unmarshal(errorMsg, &errData)
if err == nil {
msg := errData.Error.Message
if msg == "" {
msg = fmt.Sprintf("OpenAI error: %s", errData.Error.Code)
}
message.New().Text(msg).Write(w)
message.New().Done().Write(w)
return false
}

message.New().Text(string(errorMsg)).Write(w)
message.New().Done().Write(w)
return false
}

message.New().Done().Write(w)
return false
// case <-ctx.Done():
// if err := ctx.Err(); err != nil {
// message.New().Text(err.Error()).Write(w)
// }

// if len(errorMsg) > 0 {

// var errData openai.ErrorMessage
// err := jsoniter.Unmarshal(errorMsg, &errData)
// if err == nil {
// msg := errData.Error.Message
// if msg == "" {
// msg = fmt.Sprintf("OpenAI error: %s", errData.Error.Code)
// }
// message.New().Text(msg).Write(w)
// message.New().Done().Write(w)
// return false
// }

// message.New().Text(string(errorMsg)).Write(w)
// message.New().Done().Write(w)
// return false
// }

// message.New().Done().Write(w)
// return false
}
})

Expand Down
10 changes: 7 additions & 3 deletions sui/core/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,13 @@ func (page *Page) Build(option *BuildOption) (*goquery.Document, []string, error
// BuildHTML build the html
func (page *Page) BuildHTML(option *BuildOption) (string, error) {

html := string(page.Document)
if page.Codes.HTML.Code != "" {
html = strings.Replace(html, "{{ __page }}", page.Codes.HTML.Code, 1)
html := string(page.Codes.HTML.Code)

if !option.IgnoreDocument {
html = string(page.Document)
if page.Codes.HTML.Code != "" {
html = strings.Replace(html, "{{ __page }}", page.Codes.HTML.Code, 1)
}
}

if !option.IgnoreAssetRoot {
Expand Down
1 change: 1 addition & 0 deletions sui/core/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func (page *Page) EditorRender() (*ResponseEditorRender, error) {
doc, warnings, err := page.Build(&BuildOption{
SSR: true,
IgnoreAssetRoot: true,
IgnoreDocument: true,
})

if err != nil {
Expand Down
24 changes: 21 additions & 3 deletions sui/core/page_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,27 @@ func testPage(t *testing.T) *Page {
<div>{{ idx }} {{ article.title }}</div>
<div>{{ article.desc }}</div>
<div>{{ article.type == "article" ? "article" : "others"}}</div>
<div s:if="article.type == 'article'">article</div>
<div s:elif="article.type == 'image'">image</div>
<div s:else>others</div>

<div class="mt-10">IF</div>
<div class="p-5">
<div s:if="article.type == 'article'">article</div>
<div s:elif="article.type == 'image'">image</div>
<div s:else>others</div>
</div>

<div class="mt-10">Nested</div>
<div
s:for="article.images"
s:for-item="image"
s:for-index="imgIndex"
class="p-5"
>
<div s:if="imgIndex == 1 || imgIndex == 2" class="p-5">
{{ imgIndex }} {{ image }}
</div>
</div>
</div>

</div>
<div class="mt-10">IF</div>
<div s:if="len(articles) > 0" :name="input.data">
Expand Down
3 changes: 3 additions & 0 deletions sui/core/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/yaoapp/kun/utils"
)

func TestRender(t *testing.T) {
Expand All @@ -29,6 +30,8 @@ func TestRender(t *testing.T) {
t.Fatalf("Render error: %v", err)
}

utils.Dump(html)

assert.NotEmpty(t, html)
assert.Contains(t, html, "hello space")
assert.Equal(t, 0, len(parser.errors))
Expand Down
1 change: 1 addition & 0 deletions sui/core/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ type BuildOption struct {
UpdateAll bool `json:"update_all"`
AssetRoot string `json:"asset_root,omitempty"`
IgnoreAssetRoot bool `json:"ignore_asset_root,omitempty"`
IgnoreDocument bool `json:"ignore_document,omitempty"`
}

// Request is the struct for the request
Expand Down