Skip to content

Commit 9d40c06

Browse files
committed
Adding a TextFilePrompt method to support issue #1. This method generates text file prompt data when the request is too large.
The method result logic has been changed; it is now a global variable throughout the lifespan of the all-runner, particularly within sub-runners.
1 parent 3040d33 commit 9d40c06

File tree

7 files changed

+75
-56
lines changed

7 files changed

+75
-56
lines changed

internal/api/handlers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ func (h *APIHandlers) ChatCompletions(c *gin.Context) {
153153

154154
func (h *APIHandlers) handleContextCanceled(instanceIndex int) {
155155
page := h.pages[h.appConfig.Instance[instanceIndex].Name]
156-
r, err := runner.NewRunnerManager(h.appConfig.Instance[instanceIndex].Name, h.appConfig.Instance[instanceIndex].Runner, page, h.debug)
156+
r, err := runner.NewRunnerManager(h.appConfig.Instance[instanceIndex], page, h.debug)
157157
if err != nil {
158158
log.Error(err)
159159
return

internal/api/processor.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,32 +46,32 @@ func (cp *ChatProcessor) ProcessTask(ctx context.Context, task *RequestTask) *Ta
4646
}
4747
}
4848

49-
var appConfigRunner config.AppConfigRunner
49+
var appConfigInstance config.AppConfigInstance
5050
for i := 0; i < len(cp.appConfig.Instance); i++ {
5151
if cp.appConfig.Instance[i].Name == instanceName {
52-
appConfigRunner = cp.appConfig.Instance[i].Runner
52+
appConfigInstance = cp.appConfig.Instance[i]
5353
}
5454
}
5555

5656
streamResult := gjson.Get(task.Request, "stream")
5757
if streamResult.Type == gjson.True {
58-
return cp.processStreamingTask(instanceName, appConfigRunner, ctx, task)
58+
return cp.processStreamingTask(appConfigInstance, ctx, task)
5959
} else {
60-
return cp.processNonStreamingTask(instanceName, appConfigRunner, ctx, task)
60+
return cp.processNonStreamingTask(appConfigInstance, ctx, task)
6161
}
6262
}
6363

6464
// processNonStreamingTask processes a non-streaming request
65-
func (cp *ChatProcessor) processNonStreamingTask(instanceName string, appConfigRunner config.AppConfigRunner, ctx context.Context, task *RequestTask) *TaskResponse {
65+
func (cp *ChatProcessor) processNonStreamingTask(appConfigInstance config.AppConfigInstance, ctx context.Context, task *RequestTask) *TaskResponse {
6666

6767
var fullResponse strings.Builder
6868
var done bool
6969
channel := make(chan *adapter.AdapterResponse)
7070
errChannel := make(chan error)
7171
streamChan := make(chan string, 100)
7272

73-
page := cp.pages[instanceName]
74-
r, errNewRunnerManager := runner.NewRunnerManager(instanceName, appConfigRunner, page, cp.debug)
73+
page := cp.pages[appConfigInstance.Name]
74+
r, errNewRunnerManager := runner.NewRunnerManager(appConfigInstance, page, cp.debug)
7575
go func() {
7676
if errNewRunnerManager != nil {
7777
log.Debug(errNewRunnerManager)
@@ -154,14 +154,14 @@ func (cp *ChatProcessor) processNonStreamingTask(instanceName string, appConfigR
154154
}
155155

156156
// processStreamingTask processes a streaming request
157-
func (cp *ChatProcessor) processStreamingTask(instanceName string, appConfigRunner config.AppConfigRunner, ctx context.Context, task *RequestTask) *TaskResponse {
157+
func (cp *ChatProcessor) processStreamingTask(appConfigInstance config.AppConfigInstance, ctx context.Context, task *RequestTask) *TaskResponse {
158158
// Create streaming channel
159159
streamChan := make(chan string, 100)
160160
channel := make(chan *adapter.AdapterResponse)
161161
errChannel := make(chan error)
162162

163-
page := cp.pages[instanceName]
164-
r, errNewRunnerManager := runner.NewRunnerManager(instanceName, appConfigRunner, page, cp.debug)
163+
page := cp.pages[appConfigInstance.Name]
164+
r, errNewRunnerManager := runner.NewRunnerManager(appConfigInstance, page, cp.debug)
165165
go func() {
166166
if errNewRunnerManager != nil {
167167
log.Debug(errNewRunnerManager)

internal/config/config.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@ type AppConfigRunner struct {
2626
ContextCanceled string `yaml:"context_canceled"`
2727
}
2828
type AppConfigInstance struct {
29-
Name string `yaml:"name"`
30-
Adapter string `yaml:"adapter"`
31-
ProxyURL string `yaml:"proxy-url"`
32-
URL string `yaml:"url"`
33-
SniffURL []string `yaml:"sniff-url"`
34-
UserAgent string `yaml:"user-agent,omitempty"`
35-
Auth AppConfigInstanceAuth `yaml:"auth"`
36-
Runner AppConfigRunner `yaml:"runner"`
29+
Name string `yaml:"name"`
30+
Adapter string `yaml:"adapter"`
31+
ProxyURL string `yaml:"proxy-url"`
32+
TextFilePromptMinSize int `yaml:"text-file-prompt-min-size"`
33+
URL string `yaml:"url"`
34+
SniffURL []string `yaml:"sniff-url"`
35+
UserAgent string `yaml:"user-agent,omitempty"`
36+
Auth AppConfigInstanceAuth `yaml:"auth"`
37+
Runner AppConfigRunner `yaml:"runner"`
3738
}
3839

3940
type AppConfigInstanceAuth struct {

internal/method/request.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package method
22

33
import (
4+
"encoding/base64"
45
"fmt"
56
log "github.com/sirupsen/logrus"
67
"github.com/tidwall/gjson"
@@ -418,3 +419,16 @@ func (m *Method) Tools(requestJson string) (bool, string, error) {
418419
}
419420
return true, "[]", nil
420421
}
422+
423+
func (m *Method) TextFilePrompt(requestJson string, maxSize int) (bool, []string, error) {
424+
if maxSize > 0 {
425+
prompt, err := m.BuildPrompt(requestJson, true)
426+
if err != nil {
427+
return false, []string{}, err
428+
}
429+
if len(prompt) > maxSize {
430+
return true, []string{fmt.Sprintf("data:text/plain;base64,%s", base64.StdEncoding.EncodeToString([]byte(prompt)))}, nil
431+
}
432+
}
433+
return false, []string{}, nil
434+
}

internal/runner/manager.go

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,32 +22,35 @@ type RunnerResult struct {
2222
}
2323

2424
type RunnerManager struct {
25-
name string
26-
page *chrome.Page
27-
configs map[string]Configuration
28-
method *method.Method
29-
results map[string]RunnerResult
30-
debug bool
31-
debugConfigs map[string]string
32-
appConfigRunner config.AppConfigRunner
33-
abort bool
25+
appConfigInstance config.AppConfigInstance
26+
page *chrome.Page
27+
configs map[string]Configuration
28+
method *method.Method
29+
results map[string]RunnerResult
30+
debug bool
31+
debugConfigs map[string]string
32+
abort bool
3433
}
3534

36-
func NewRunnerManager(name string, appConfigRunner config.AppConfigRunner, page *chrome.Page, debug bool) (*RunnerManager, error) {
35+
func NewRunnerManager(appConfigInstance config.AppConfigInstance, page *chrome.Page, debug bool, results ...map[string]RunnerResult) (*RunnerManager, error) {
36+
if len(results) == 0 {
37+
results = []map[string]RunnerResult{make(map[string]RunnerResult)}
38+
}
39+
3740
runner := &RunnerManager{
38-
name: name,
39-
page: page,
40-
method: method.NewMethod(page),
41-
configs: make(map[string]Configuration),
42-
results: make(map[string]RunnerResult),
43-
debug: debug,
44-
debugConfigs: make(map[string]string),
45-
appConfigRunner: appConfigRunner,
41+
appConfigInstance: appConfigInstance,
42+
page: page,
43+
method: method.NewMethod(page),
44+
configs: make(map[string]Configuration),
45+
results: results[0],
46+
debug: debug,
47+
debugConfigs: make(map[string]string),
4648
}
4749
err := runner.LoadConfigurations()
4850
if err != nil {
4951
return nil, err
5052
}
53+
runner.SetVariable("TextFilePromptMinSize", appConfigInstance.TextFilePromptMinSize, "int")
5154
return runner, nil
5255
}
5356

@@ -115,13 +118,13 @@ func (rm *RunnerManager) LoadConfiguration(name, path string) error {
115118
// LoadConfigurations scans all yaml files in the runner directory and calls LoadConfiguration method by filename
116119
func (rm *RunnerManager) LoadConfigurations() error {
117120
// Scan all yaml and yml files in the runner directory
118-
pattern := filepath.Join("runner", rm.name, "*.yaml")
121+
pattern := filepath.Join("runner", rm.appConfigInstance.Name, "*.yaml")
119122
yamlFiles, err := filepath.Glob(pattern)
120123
if err != nil {
121124
return fmt.Errorf("failed to scan yaml files: %v", err)
122125
}
123126

124-
pattern = filepath.Join("runner", rm.name, "*.yml")
127+
pattern = filepath.Join("runner", rm.appConfigInstance.Name, "*.yml")
125128
ymlFiles, err := filepath.Glob(pattern)
126129
if err != nil {
127130
return fmt.Errorf("failed to scan yml files: %v", err)
@@ -138,11 +141,11 @@ func (rm *RunnerManager) LoadConfigurations() error {
138141
name := strings.TrimSuffix(fileName, filepath.Ext(fileName))
139142

140143
switch name {
141-
case rm.appConfigRunner.Init:
144+
case rm.appConfigInstance.Runner.Init:
142145
name = "init"
143-
case rm.appConfigRunner.ChatCompletions:
146+
case rm.appConfigInstance.Runner.ChatCompletions:
144147
name = "chat_completions"
145-
case rm.appConfigRunner.ContextCanceled:
148+
case rm.appConfigInstance.Runner.ContextCanceled:
146149
name = "context_canceled"
147150
}
148151

@@ -219,7 +222,7 @@ outLoop:
219222
}
220223

221224
if workflows[executeIndex].Action == "DoRunner" {
222-
r, err := NewRunnerManager(rm.name, rm.appConfigRunner, rm.page, rm.debug)
225+
r, err := NewRunnerManager(rm.appConfigInstance, rm.page, rm.debug, rm.results)
223226
if err != nil {
224227
log.Error(err)
225228
return err
@@ -315,6 +318,8 @@ outLoop:
315318
doFailback = true
316319
} else if rule == "FAILED" {
317320
return fmt.Errorf("workflow failed")
321+
} else if rule == "FINISH" {
322+
return fmt.Errorf("workflow done")
318323
} else if strings.HasPrefix(rule, "DO-WORKFLOW-IDX:") {
319324
log.Debugf("DO-WORKFLOW-IDX: %s", rule[16:])
320325
// Handle DO-WORKFLOW-IDX:1,2,3 cases
@@ -386,6 +391,8 @@ outLoop:
386391
continue outLoop
387392
} else if err.Error() == "workflow failed" {
388393
return err
394+
} else if err.Error() == "workflow done" {
395+
return nil
389396
}
390397

391398
// sub workflow failed, need to retry
@@ -503,7 +510,7 @@ func (rm *RunnerManager) executeMethod(obj any, methodName string, params []inte
503510
input := strings.TrimSpace(reflect.ValueOf(params[i-1]).String())
504511
if len(input) > 2 && input[0] == '#' && input[len(input)-1] == '#' {
505512
if input == "#NEW_RUNNER#" {
506-
newRm, _ := NewRunnerManager(rm.name, rm.appConfigRunner, rm.page, rm.debug)
513+
newRm, _ := NewRunnerManager(rm.appConfigInstance, rm.page, rm.debug, rm.results)
507514
args[paramIndex] = reflect.ValueOf(newRm)
508515
} else {
509516
input = input[1 : len(input)-1]

main.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,23 @@ package main
22

33
import (
44
"bytes"
5-
"context" // Will be needed for marshalling cookies
5+
"context"
66
"encoding/json"
77
"fmt"
88
"github.com/chromedp/cdproto/cdp"
9+
"github.com/chromedp/chromedp"
10+
"github.com/luispater/anyAIProxyAPI/internal/api"
11+
"github.com/luispater/anyAIProxyAPI/internal/browser/chrome"
12+
chromedpmanager "github.com/luispater/anyAIProxyAPI/internal/browser/chrome"
13+
"github.com/luispater/anyAIProxyAPI/internal/config"
914
"github.com/luispater/anyAIProxyAPI/internal/runner"
15+
log "github.com/sirupsen/logrus"
1016
"os"
1117
"os/signal"
1218
"path"
1319
"path/filepath"
1420
"syscall"
1521
"time"
16-
17-
// For cdp.Node
18-
"github.com/chromedp/chromedp" // For chromedp actions
19-
"github.com/luispater/anyAIProxyAPI/internal/api"
20-
"github.com/luispater/anyAIProxyAPI/internal/browser/chrome"
21-
chromedpmanager "github.com/luispater/anyAIProxyAPI/internal/browser/chrome"
22-
"github.com/luispater/anyAIProxyAPI/internal/config"
23-
// "github.com/playwright-community/playwright-go" // Playwright no longer used
24-
log "github.com/sirupsen/logrus"
2522
)
2623

2724
type LogFormatter struct {
@@ -124,7 +121,7 @@ func main() {
124121

125122
pages[cfg.Instance[i].Name] = page // Store pageCtx
126123

127-
r, errNewRunnerManager := runner.NewRunnerManager(cfg.Instance[i].Name, cfg.Instance[i].Runner, page, cfg.Debug) // Pass pageCtx
124+
r, errNewRunnerManager := runner.NewRunnerManager(cfg.Instance[i], page, cfg.Debug) // Pass pageCtx
128125
if errNewRunnerManager != nil {
129126
log.Error(errNewRunnerManager)
130127
}

0 commit comments

Comments
 (0)