MCPcopy Create free account
hub / github.com/diillson/chatcli / ExecuteWithStream

Method ExecuteWithStream

cli/plugins/builtin_read.go:102–141  ·  view source on GitHub ↗

ExecuteWithStream parses the flat JSON args into the engine's argv form and dispatches to engine.handleRead via a fresh Engine instance. Output is streamed line-by-line through onOutput when present.

(ctx context.Context, args []string, onOutput func(string))

Source from the content-addressed store, hash-verified

100// form and dispatches to engine.handleRead via a fresh Engine instance.
101// Output is streamed line-by-line through onOutput when present.
102func (p *BuiltinReadPlugin) ExecuteWithStream(ctx context.Context, args []string, onOutput func(string)) (string, error) {
103 parsed, err := parseReadArgs(args)
104 if err != nil {
105 return "", err
106 }
107 if parsed.File == "" {
108 return "", fmt.Errorf("file required (usage: @read {\"file\":\"path\"})")
109 }
110
111 argv := buildReadArgv(parsed)
112
113 var fullOutput strings.Builder
114 var mu sync.Mutex
115 emit := func(line string, isErr bool) {
116 mu.Lock()
117 defer mu.Unlock()
118 if onOutput != nil {
119 prefix := ""
120 if isErr {
121 prefix = "ERR: "
122 }
123 onOutput(prefix + line)
124 }
125 fullOutput.WriteString(line)
126 fullOutput.WriteString("\n")
127 }
128 outWriter := engine.NewStreamWriter(func(line string) { emit(line, false) })
129 errWriter := engine.NewStreamWriter(func(line string) { emit(line, true) })
130
131 eng := engine.NewEngine(outWriter, errWriter, "")
132 execErr := eng.Execute(ctx, "read", argv)
133
134 outWriter.Flush()
135 errWriter.Flush()
136
137 if execErr != nil {
138 return fullOutput.String(), fmt.Errorf("@read failed: %w", execErr)
139 }
140 return fullOutput.String(), nil
141}
142
143// readArgs is the typed view of @read's JSON input. Keeping this
144// separate from the JSON unmarshal call makes the dispatch logic

Callers 1

ExecuteMethod · 0.95

Calls 10

ExecuteMethod · 0.95
FlushMethod · 0.95
NewStreamWriterFunction · 0.92
NewEngineFunction · 0.92
parseReadArgsFunction · 0.85
buildReadArgvFunction · 0.85
ErrorfMethod · 0.80
LockMethod · 0.80
UnlockMethod · 0.80
StringMethod · 0.45

Tested by

no test coverage detected