Skip to content
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
4 changes: 2 additions & 2 deletions go.work
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ use (
./go
./external/go
./external/mcp
./external/process
./external/process/go
./external/store
./external/ws
./external/io
./external/log
./external/rag
./external/rag/go
)
2 changes: 1 addition & 1 deletion go/cmd/core-agent/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var applicationPrint = func(format string, args ...any) {
}

// args := startupArgs()
// _ = c.Cli().Run("version")
// result := c.Cli().Run("version")
func startupArgs() []string {
return applyLogLevel(core.FilterArgs(startupArgv()[1:]))
}
Expand Down
2 changes: 1 addition & 1 deletion go/cmd/core-agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@
}

// app := newCoreAgent()
// _ = runApp(app, []string{"version"})
// result := runApp(app, []string{"version"})
var runApp = func(coreApp *core.Core, cliArgs []string) error {
if coreApp == nil {
return core.E("main.runApp", "core is required", nil)
}

defer coreApp.ServiceShutdown(context.Background())

Check failure on line 88 in go/cmd/core-agent/main.go

View workflow job for this annotation

GitHub Actions / golangci-lint

Error return value of `coreApp.ServiceShutdown` is not checked (errcheck)

Check failure on line 88 in go/cmd/core-agent/main.go

View workflow job for this annotation

GitHub Actions / golangci-lint

Error return value of `coreApp.ServiceShutdown` is not checked (errcheck)

result := coreApp.ServiceStartup(coreApp.Context(), nil)
if !result.OK {
Expand Down
93 changes: 75 additions & 18 deletions go/pkg/agentcompat/agentcompat.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
Models map[string]int
}

func (c *ConcurrencyLimit) UnmarshalYAML(value *yaml.Node) error {
func (c *ConcurrencyLimit) UnmarshalYAMLResult(value *yaml.Node) core.Result {
var n int
if err := value.Decode(&n); err == nil {
c.Total = n
return nil
return core.Ok(nil)
}
var m map[string]int
if err := value.Decode(&m); err != nil {
return err
return core.Fail(err)
}
c.Total = m["total"]
c.Models = make(map[string]int)
Expand All @@ -32,7 +32,18 @@
c.Models[key] = entry
}
}
return nil
return core.Ok(nil)
}

func (c *ConcurrencyLimit) UnmarshalYAML(value *yaml.Node) error { // yaml contract
result := c.UnmarshalYAMLResult(value)
if result.OK {
return nil
}
if err, ok := result.Value.(error); ok {
return err
}
return core.E("ConcurrencyLimit.UnmarshalYAML", "decode failed", nil)
}

type HTTPStream struct {
Expand All @@ -43,11 +54,18 @@
Response []byte
}

func (s *HTTPStream) Send(data []byte) error {
request, err := http.NewRequestWithContext(context.Background(), s.Method, s.URL, core.NewReader(string(data)))
if err != nil {
return err
func (s *HTTPStream) SendResult(data []byte) core.Result {
if s == nil {
return core.Fail(core.E("HTTPStream.Send", "stream is required", nil))

Check failure on line 59 in go/pkg/agentcompat/agentcompat.go

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Define a constant instead of duplicating this literal "stream is required" 3 times.

See more on https://sonarcloud.io/project/issues?id=dAppCore_agent&issues=AZ3ggs-r2LzBVNKAmlQp&open=AZ3ggs-r2LzBVNKAmlQp&pullRequest=10

Check failure on line 59 in go/pkg/agentcompat/agentcompat.go

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Define a constant instead of duplicating this literal "HTTPStream.Send" 4 times.

See more on https://sonarcloud.io/project/issues?id=dAppCore_agent&issues=AZ3ggs-r2LzBVNKAmlQo&open=AZ3ggs-r2LzBVNKAmlQo&pullRequest=10
}
if s.Client == nil {
return core.Fail(core.E("HTTPStream.Send", "client is required", nil))
}
requestResult := core.NewHTTPRequestContext(context.Background(), s.Method, s.URL, core.NewReader(string(data)))
if !requestResult.OK {
return requestResult
}
request := requestResult.Value.(*core.Request)
request.Header.Set("Content-Type", "application/json")
request.Header.Set("Accept", "application/json")
if s.Token != "" {
Expand All @@ -56,25 +74,64 @@

response, err := s.Client.Do(request)
if err != nil {
return err
return core.Fail(err)
}
defer func() {
_ = response.Body.Close()
}()

readResult := core.ReadAll(response.Body)
if !readResult.OK {
err, _ := readResult.Value.(error)
return core.E("httpStream.Send", "failed to read response", err)
return core.Fail(core.E("httpStream.Send", "failed to read response", err))
}
s.Response = []byte(readResult.Value.(string))
return nil
return core.Ok(nil)
}

func (s *HTTPStream) Send(data []byte) error { // core.Stream contract
if s == nil || s.Client == nil {
panic(core.E("HTTPStream.Send", "stream client is required", nil))
}
result := s.SendResult(data)
if result.OK {
return nil
}
if err, ok := result.Value.(error); ok {
return err
}
return core.E("HTTPStream.Send", "send failed", nil)
}

func (s *HTTPStream) ReceiveResult() core.Result {
if s == nil {
return core.Fail(core.E("HTTPStream.Receive", "stream is required", nil))

Check failure on line 105 in go/pkg/agentcompat/agentcompat.go

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Define a constant instead of duplicating this literal "HTTPStream.Receive" 3 times.

See more on https://sonarcloud.io/project/issues?id=dAppCore_agent&issues=AZ3ggs-r2LzBVNKAmlQn&open=AZ3ggs-r2LzBVNKAmlQn&pullRequest=10
}
return core.Ok(s.Response)
}

func (s *HTTPStream) Receive() ([]byte, error) { // core.Stream contract
if s == nil {
panic(core.E("HTTPStream.Receive", "stream is required", nil))
}
result := s.ReceiveResult()
if result.OK {
return result.Value.([]byte), nil
}
if err, ok := result.Value.(error); ok {
return nil, err
}
return nil, core.E("HTTPStream.Receive", "receive failed", nil)
}

func (s *HTTPStream) Receive() ([]byte, error) {
return s.Response, nil
func (s *HTTPStream) CloseResult() core.Result {
return core.Ok(nil)
}

func (s *HTTPStream) Close() error {
return nil
func (s *HTTPStream) Close() error { // core.Stream contract
result := s.CloseResult()
if result.OK {
return nil
}
if err, ok := result.Value.(error); ok {
return err
}
return core.E("HTTPStream.Close", "close failed", nil)
}
70 changes: 70 additions & 0 deletions go/pkg/agentcompat/agentcompat_example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// SPDX-License-Identifier: EUPL-1.2

package agentcompat

import (
core "dappco.re/go"
"gopkg.in/yaml.v3"
)

func ExampleConcurrencyLimit_UnmarshalYAMLResult() {
var node yaml.Node
yaml.Unmarshal([]byte("total: 2\ncodex: 1\n"), &node)
limit := ConcurrencyLimit{}
result := limit.UnmarshalYAMLResult(node.Content[0])
core.Println(result.OK, limit.Total, limit.Models["codex"])
// Output: true 2 1
}

func ExampleConcurrencyLimit_UnmarshalYAML() {
limit := ConcurrencyLimit{}
yaml.Unmarshal([]byte("3\n"), &limit)
core.Println(limit.Total)
// Output: 3
}

func ExampleHTTPStream_SendResult() {
stream := &HTTPStream{}
result := stream.SendResult([]byte(`{"ping":1}`))
core.Println(result.OK)
// Output: false
}

func ExampleHTTPStream_Send() {
server := core.NewHTTPTestServer(core.HandlerFunc(func(w core.ResponseWriter, r *core.Request) {
core.WriteString(w, "ok")
}))
defer server.Close()
stream := &HTTPStream{Client: server.Client(), URL: server.URL, Method: "POST"}
err := stream.Send([]byte(`{"ping":1}`))
core.Println(err == nil, string(stream.Response))
// Output: true ok
}

func ExampleHTTPStream_ReceiveResult() {
stream := &HTTPStream{Response: []byte("ok")}
result := stream.ReceiveResult()
core.Println(string(result.Value.([]byte)))
// Output: ok
}

func ExampleHTTPStream_Receive() {
stream := &HTTPStream{Response: []byte("ok")}
data, err := stream.Receive()
core.Println(string(data), err == nil)
// Output: ok true
}

func ExampleHTTPStream_CloseResult() {
stream := &HTTPStream{}
result := stream.CloseResult()
core.Println(result.OK)
// Output: true
}

func ExampleHTTPStream_Close() {
stream := &HTTPStream{}
err := stream.Close()
core.Println(err == nil)
// Output: true
}
Loading
Loading