-
Notifications
You must be signed in to change notification settings - Fork 130
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
feat: init node application #997
base: master
Are you sure you want to change the base?
Changes from all commits
09bb92e
80d2c9d
c7ccf9c
a5fcded
dff9ef4
a69b2dd
8888536
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,9 @@ | |
|
||
// Serverless defines the interface for serverless | ||
type Serverless interface { | ||
// Setup sets up the serverless | ||
Setup(opts *Options) error | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the difference between Should these two function be merged ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
// Init initializes the serverless | ||
Init(opts *Options) error | ||
|
||
|
@@ -69,3 +72,17 @@ | |
|
||
return nil, fmt.Errorf(`serverless: unsupport "%s" source (forgotten import?)`, ext) | ||
} | ||
|
||
// Setup sets up the serverless | ||
func Setup(opts *Options) error { | ||
ext := filepath.Ext(opts.Filename) | ||
|
||
driversMu.RLock() | ||
s, ok := drivers[ext] | ||
driversMu.RUnlock() | ||
if ok { | ||
return s.Setup(opts) | ||
} | ||
|
||
return fmt.Errorf(`serverless: unsupport "%s" source (forgotten import?)`, ext) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package main | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/yomorun/yomo/serverless/mock" | ||
) | ||
|
||
func TestHandler(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
ctx *mock.MockContext | ||
// want is the expected result written by ctx.WriteLLMResult() | ||
want string | ||
}{ | ||
{ | ||
name: "get weather", | ||
ctx: mock.NewArgumentsContext(`{"city":"New York","latitude":40.7128,"longitude":-74.0060}`, 0x33), | ||
want: "The current weather in New York (40.712800,-74.006000) is sunny", | ||
}, | ||
// TODO: add more test cases. | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
Handler(tt.ctx) | ||
|
||
records := tt.ctx.RecordsWritten() | ||
got := records[0].LLMResult | ||
|
||
if !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("TestHandler got: %v, want: %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/yomorun/yomo/serverless" | ||
) | ||
|
||
// Init is an optional function invoked during the initialization phase of the | ||
// sfn instance. It's designed for setup tasks like global variable | ||
// initialization, establishing database connections, or loading models into | ||
// GPU memory. If initialization fails, the sfn instance will halt and terminate. | ||
// This function can be omitted if no initialization tasks are needed. | ||
func Init() error { | ||
return nil | ||
} | ||
|
||
// DataTags specifies the data tags to which this serverless function | ||
// subscribes, essential for data reception. Upon receiving data with these | ||
// tags, the Handler function is triggered. | ||
func DataTags() []uint32 { | ||
return []uint32{0x33} | ||
} | ||
|
||
// Handler orchestrates the core processing logic of this function. | ||
// - ctx.Tag() identifies the tag of the incoming data. | ||
// - ctx.Data() accesses the raw data. | ||
// - ctx.Write() forwards processed data downstream. | ||
func Handler(ctx serverless.Context) { | ||
data := ctx.Data() | ||
fmt.Printf("<< sfn received[%d Bytes]: %s\n", len(data), data) | ||
output := strings.ToUpper(string(data)) | ||
err := ctx.Write(0x34, []byte(output)) | ||
if err != nil { | ||
fmt.Printf(">> sfn write error: %v\n", err) | ||
return | ||
} | ||
fmt.Printf(">> sfn written[%d Bytes]: %s\n", len(output), output) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should the
yomo build
command be deprecated? Migrate thewasi
flag toyomo run
command..