MCPcopy Index your code
hub / github.com/docker/docker-agent / chatJS

Function chatJS

cmd/wasm/main.go:171–221  ·  view source on GitHub ↗

--------------------------------------------------------------------------- chat ---------------------------------------------------------------------------

(_ js.Value, args []js.Value)

Source from the content-addressed store, hash-verified

169// ---------------------------------------------------------------------------
170
171func chatJS(_ js.Value, args []js.Value) any {
172 if len(args) < 1 {
173 return rejectedPromise("chat: expected at least one argument (options object)")
174 }
175 opts := args[0]
176 var onEvent js.Value
177 if len(args) >= 2 && args[1].Type() == js.TypeFunction {
178 onEvent = args[1]
179 }
180
181 yamlStr := opts.Get("yaml").String()
182 agentName := ""
183 if v := opts.Get("agentName"); v.Type() == js.TypeString {
184 agentName = v.String()
185 }
186 envMap := jsObjectToStringMap(opts.Get("env"))
187 messages, err := jsToMessages(opts.Get("messages"))
188 if err != nil {
189 return rejectedPromise(fmt.Sprintf("chat: parsing messages: %v", err))
190 }
191
192 // Set up cancellable context.
193 ctx, cancel := context.WithCancel(context.Background())
194 abortState.mu.Lock()
195 if abortState.cancel != nil {
196 abortState.cancel()
197 }
198 abortState.cancel = cancel
199 abortState.mu.Unlock()
200
201 return newPromise(func(resolve, reject func(any)) {
202 go func() {
203 defer func() {
204 abortState.mu.Lock()
205 defer abortState.mu.Unlock()
206 cancel()
207 }()
208
209 result, err := runChat(ctx, yamlStr, agentName, envMap, messages, onEvent)
210 if err != nil {
211 if ctx.Err() != nil {
212 reject(jsError(fmt.Errorf("chat aborted")))
213 } else {
214 reject(jsError(err))
215 }
216 return
217 }
218 resolve(result)
219 }()
220 })
221}
222
223// runChat loads the config, builds the runtime, and runs the agentic loop.
224func runChat(ctx context.Context, yamlStr, agentName string, envMap map[string]string, messages []chat.Message, onEvent js.Value) (any, error) {

Callers

nothing calls this directly

Calls 13

rejectedPromiseFunction · 0.85
jsObjectToStringMapFunction · 0.85
jsToMessagesFunction · 0.85
newPromiseFunction · 0.85
runChatFunction · 0.85
jsErrorFunction · 0.85
TypeMethod · 0.80
GetMethod · 0.65
ErrMethod · 0.65
resolveFunction · 0.50
StringMethod · 0.45
LockMethod · 0.45

Tested by

no test coverage detected