This repository was archived by the owner on Sep 11, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathagents.go
More file actions
534 lines (434 loc) · 13.5 KB
/
agents.go
File metadata and controls
534 lines (434 loc) · 13.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
/*
* Copyright 2025 Hypermode Inc.
* Licensed under the terms of the Apache License, Version 2.0
* See the LICENSE file that accompanied this code for further details.
*
* SPDX-FileCopyrightText: 2025 Hypermode Inc. <hello@hypermode.com>
* SPDX-License-Identifier: Apache-2.0
*/
package actors
import (
"context"
"fmt"
"strings"
"time"
"github.com/hypermodeinc/modus/runtime/db"
"github.com/hypermodeinc/modus/runtime/logger"
"github.com/hypermodeinc/modus/runtime/messages"
"github.com/hypermodeinc/modus/runtime/plugins"
"github.com/hypermodeinc/modus/runtime/utils"
"github.com/hypermodeinc/modus/runtime/wasmhost"
"github.com/rs/xid"
wasm "github.com/tetratelabs/wazero/api"
goakt "github.com/tochemey/goakt/v3/actor"
)
type AgentInfo struct {
Id string `json:"id"`
Name string `json:"name"`
Status AgentStatus `json:"status"`
}
type AgentStatus = string
const (
AgentStatusStarting AgentStatus = "starting"
AgentStatusRunning AgentStatus = "running"
AgentStatusSuspending AgentStatus = "suspending"
AgentStatusSuspended AgentStatus = "suspended"
AgentStatusResuming AgentStatus = "resuming"
AgentStatusStopping AgentStatus = "stopping"
AgentStatusTerminated AgentStatus = "terminated"
)
func StartAgent(ctx context.Context, agentName string) (*AgentInfo, error) {
plugin, ok := plugins.GetPluginFromContext(ctx)
if !ok {
return nil, fmt.Errorf("no plugin found in context")
}
agentId := xid.New().String()
host := wasmhost.GetWasmHost(ctx)
spawnActorForAgent(host, plugin, agentId, agentName, false, nil)
info := &AgentInfo{
Id: agentId,
Name: agentName,
Status: AgentStatusStarting,
}
return info, nil
}
func spawnActorForAgent(host wasmhost.WasmHost, plugin *plugins.Plugin, agentId, agentName string, resuming bool, initialState *string) {
// The actor needs to spawn in its own context, so we don't pass one in to this function.
// If we did, then when the original context was cancelled or completed, the actor initialization would be cancelled too.
// We spawn the actor in a goroutine to avoid blocking while the actor is being spawned.
// This allows many agents to be spawned in parallel, if needed.
go func() {
ctx := context.Background()
ctx = context.WithValue(ctx, utils.WasmHostContextKey, host)
ctx = context.WithValue(ctx, utils.PluginContextKey, plugin)
ctx = context.WithValue(ctx, utils.AgentIdContextKey, agentId)
ctx = context.WithValue(ctx, utils.AgentNameContextKey, agentName)
actor := newWasmAgentActor(agentId, agentName, host, plugin)
actorName := getActorName(agentId)
if resuming {
actor.status = AgentStatusResuming
actor.initialState = initialState
} else {
actor.status = AgentStatusStarting
}
if _, err := _actorSystem.Spawn(ctx, actorName, actor); err != nil {
logger.Err(ctx, err).Msg("Error spawning actor for agent.")
}
}()
}
func StopAgent(ctx context.Context, agentId string) bool {
pid, err := getActorPid(ctx, agentId)
if err != nil {
logger.Err(ctx, err).Msg("Error stopping agent.")
return false
}
actor := pid.Actor().(*wasmAgentActor)
actor.status = AgentStatusStopping
if err := pid.Shutdown(ctx); err != nil {
logger.Err(ctx, err).Msg("Error stopping agent.")
return false
}
return true
}
type agentMessageResponse struct {
Data *string
}
func SendAgentMessage(ctx context.Context, agentId string, msgName string, data *string, timeout int64) (*agentMessageResponse, error) {
pid, err := getActorPid(ctx, agentId)
if err != nil {
return nil, err
}
msg := &messages.AgentRequestMessage{
Name: msgName,
Data: data,
Respond: timeout > 0,
}
if timeout == 0 {
if err := goakt.Tell(ctx, pid, msg); err != nil {
return nil, fmt.Errorf("error sending message to agent %s: %w", pid.ID(), err)
}
return &agentMessageResponse{}, nil
}
res, err := goakt.Ask(ctx, pid, msg, time.Duration(timeout))
if err != nil {
return nil, fmt.Errorf("error sending message to agent %s: %w", pid.ID(), err)
}
if response, ok := res.(*messages.AgentResponseMessage); ok {
return &agentMessageResponse{response.Data}, nil
} else {
return nil, fmt.Errorf("unexpected response type: %T", res)
}
}
func getActorName(agentId string) string {
return "agent-" + agentId
}
func getActorPid(ctx context.Context, agentId string) (*goakt.PID, error) {
addr, pid, err := _actorSystem.ActorOf(ctx, getActorName(agentId))
if err != nil {
if strings.HasSuffix(err.Error(), " not found") {
return nil, fmt.Errorf("agent %s not found", agentId)
} else {
return nil, fmt.Errorf("error getting actor for agent %s: %w", agentId, err)
}
}
_ = addr // TODO: this will be used when we implement remote actors with clustering
return pid, nil
}
func ListAgents() []AgentInfo {
if _actorSystem == nil {
return nil
}
actors := _actorSystem.Actors()
results := make([]AgentInfo, 0, len(actors))
for _, pid := range actors {
if actor, ok := pid.Actor().(*wasmAgentActor); ok {
results = append(results, AgentInfo{
Id: actor.agentId,
Name: actor.agentName,
Status: actor.status,
})
}
}
return results
}
type wasmAgentActor struct {
agentId string
agentName string
status AgentStatus
plugin *plugins.Plugin
host wasmhost.WasmHost
module wasm.Module
buffers utils.OutputBuffers
initialState *string
}
func newWasmAgentActor(agentId, agentName string, host wasmhost.WasmHost, plugin *plugins.Plugin) *wasmAgentActor {
return &wasmAgentActor{
agentId: agentId,
agentName: agentName,
host: host,
plugin: plugin,
}
}
func (a *wasmAgentActor) PreStart(ac *goakt.Context) error {
ctx := a.newContext()
switch a.status {
case AgentStatusStarting:
logger.Info(ctx).Msg("Starting agent.")
case AgentStatusResuming, AgentStatusSuspended:
a.status = AgentStatusResuming
logger.Info(ctx).Msg("Resuming agent.")
default:
return fmt.Errorf("invalid agent status for actor PreStart: %s", a.status)
}
if err := a.saveState(ctx); err != nil {
logger.Err(ctx, err).Msg("Error saving agent state.")
}
start := time.Now()
a.buffers = utils.NewOutputBuffers()
if mod, err := a.host.GetModuleInstance(ctx, a.plugin, a.buffers); err != nil {
return err
} else {
a.module = mod
}
if err := a.activateAgent(ctx); err != nil {
logger.Err(ctx, err).Msg("Error activating agent.")
return err
}
if a.status == AgentStatusResuming {
if err := a.setAgentState(ctx, a.initialState); err != nil {
logger.Err(ctx, err).Msg("Error resuming agent state.")
}
a.initialState = nil
}
duration := time.Since(start)
if a.status == AgentStatusResuming {
logger.Info(ctx).Msg("Agent resumed successfully.")
} else {
logger.Info(ctx).Dur("duration_ms", duration).Msg("Agent started successfully.")
}
a.status = AgentStatusRunning
if err := a.saveState(ctx); err != nil {
logger.Err(ctx, err).Msg("Error saving agent state.")
}
return nil
}
func (a *wasmAgentActor) PostStop(ac *goakt.Context) error {
ctx := a.newContext()
defer a.module.Close(ctx)
switch a.status {
case AgentStatusRunning, AgentStatusSuspending:
a.status = AgentStatusSuspending
logger.Info(ctx).Msg("Suspending agent.")
case AgentStatusStopping:
logger.Info(ctx).Msg("Stopping agent.")
default:
return fmt.Errorf("invalid agent status for actor PostStop: %s", a.status)
}
if err := a.saveState(ctx); err != nil {
logger.Err(ctx, err).Msg("Error saving agent state.")
}
start := time.Now()
if err := a.shutdownAgent(ctx); err != nil {
logger.Err(ctx, err).Msg("Error shutting down agent.")
return err
}
duration := time.Since(start)
switch a.status {
case AgentStatusSuspending:
a.status = AgentStatusSuspended
if err := a.saveState(ctx); err != nil {
return err
}
logger.Info(ctx).Msg("Agent suspended successfully.")
case AgentStatusStopping:
a.status = AgentStatusTerminated
if err := a.saveState(ctx); err != nil {
return err
}
logger.Info(ctx).Dur("duration_ms", duration).Msg("Agent terminated successfully.")
default:
return fmt.Errorf("invalid agent status for actor PostStop: %s", a.status)
}
return nil
}
func (a *wasmAgentActor) saveState(ctx context.Context) error {
var data string
if a.module != nil {
if d, err := a.getAgentState(ctx); err != nil {
return fmt.Errorf("error getting state from agent: %w", err)
} else if d != nil {
data = *d
}
}
if err := db.WriteAgentState(ctx, db.AgentState{
Id: a.agentId,
Name: a.agentName,
Status: a.status,
Data: data,
UpdatedAt: time.Now().UTC().Format(time.RFC3339),
}); err != nil {
return fmt.Errorf("error saving state to database: %w", err)
}
return nil
}
func (a *wasmAgentActor) Receive(rc *goakt.ReceiveContext) {
ctx := a.newContext()
switch msg := rc.Message().(type) {
case *messages.AgentRequestMessage:
logger.Info(ctx).Str("msg_name", msg.Name).Msg("Received message.")
start := time.Now()
fnInfo, err := a.host.GetFunctionInfo("_modus_agent_handle_message")
if err != nil {
rc.Err(err)
return
}
params := map[string]any{
"msgName": msg.Name,
"data": msg.Data,
}
execInfo, err := a.host.CallFunctionInModule(ctx, a.module, a.buffers, fnInfo, params)
if err != nil {
rc.Err(err)
return
}
if msg.Respond {
result := execInfo.Result()
response := &messages.AgentResponseMessage{}
if result != nil {
switch result := result.(type) {
case string:
response.Data = &result
case *string:
response.Data = result
default:
err := fmt.Errorf("unexpected result type: %T", result)
logger.Err(ctx, err).Msg("Error handling message.")
rc.Err(err)
return
}
}
rc.Response(response)
}
duration := time.Since(start)
logger.Info(ctx).Str("msg_name", msg.Name).Dur("duration_ms", duration).Msg("Message handled successfully.")
// save the state after handling the message to ensure the state is up to date in case of hard termination
if err := a.saveState(ctx); err != nil {
logger.Err(ctx, err).Msg("Error saving agent state.")
}
default:
rc.Unhandled()
}
}
func (a *wasmAgentActor) newContext() context.Context {
ctx := context.Background()
ctx = context.WithValue(ctx, utils.WasmHostContextKey, a.host)
ctx = context.WithValue(ctx, utils.PluginContextKey, a.plugin)
ctx = context.WithValue(ctx, utils.AgentIdContextKey, a.agentId)
ctx = context.WithValue(ctx, utils.AgentNameContextKey, a.agentName)
return ctx
}
func (a *wasmAgentActor) activateAgent(ctx context.Context) error {
fnInfo, err := a.host.GetFunctionInfo("_modus_agent_activate")
if err != nil {
return err
}
params := map[string]any{
"name": a.agentName,
"id": a.agentId,
"reloading": a.status == AgentStatusResuming,
}
execInfo, err := a.host.CallFunctionInModule(ctx, a.module, a.buffers, fnInfo, params)
_ = execInfo // TODO
return err
}
func (a *wasmAgentActor) shutdownAgent(ctx context.Context) error {
fnInfo, err := a.host.GetFunctionInfo("_modus_agent_shutdown")
if err != nil {
return err
}
params := map[string]any{
"suspending": a.status == AgentStatusSuspending,
}
execInfo, err := a.host.CallFunctionInModule(ctx, a.module, a.buffers, fnInfo, params)
_ = execInfo // TODO
return err
}
func (a *wasmAgentActor) getAgentState(ctx context.Context) (*string, error) {
fnInfo, err := a.host.GetFunctionInfo("_modus_agent_get_state")
if err != nil {
return nil, err
}
execInfo, err := a.host.CallFunctionInModule(ctx, a.module, a.buffers, fnInfo, nil)
if err != nil {
return nil, err
}
result := execInfo.Result()
if result == nil {
return nil, nil
}
switch state := result.(type) {
case string:
return &state, nil
case *string:
return state, nil
default:
return nil, fmt.Errorf("unexpected result type: %T", result)
}
}
func (a *wasmAgentActor) setAgentState(ctx context.Context, data *string) error {
fnInfo, err := a.host.GetFunctionInfo("_modus_agent_set_state")
if err != nil {
return err
}
params := map[string]any{
"data": data,
}
execInfo, err := a.host.CallFunctionInModule(ctx, a.module, a.buffers, fnInfo, params)
if err != nil {
return err
}
_ = execInfo // TODO
return err
}
func (a *wasmAgentActor) reloadModule(ctx context.Context, plugin *plugins.Plugin) error {
// the context may not have these values set
ctx = context.WithValue(ctx, utils.PluginContextKey, a.plugin)
ctx = context.WithValue(ctx, utils.AgentIdContextKey, a.agentId)
ctx = context.WithValue(ctx, utils.AgentNameContextKey, a.agentName)
logger.Info(ctx).Msg("Reloading module for agent.")
a.status = AgentStatusSuspending
if err := a.shutdownAgent(ctx); err != nil {
logger.Err(ctx, err).Msg("Error shutting down agent.")
return err
}
// get the current state and close the module instance
state, err := a.getAgentState(ctx)
if err != nil {
logger.Err(ctx, err).Msg("Error getting agent state.")
return err
}
a.module.Close(ctx)
a.status = AgentStatusSuspended
// create a new module instance and assign it to the actor
a.plugin = plugin
a.buffers = utils.NewOutputBuffers()
mod, err := a.host.GetModuleInstance(ctx, a.plugin, a.buffers)
if err != nil {
return err
}
a.module = mod
// activate the agent in the new module instance
a.status = AgentStatusResuming
if err := a.activateAgent(ctx); err != nil {
logger.Err(ctx, err).Msg("Error reloading agent.")
return err
}
// restore the state in the new module instance
if err := a.setAgentState(ctx, state); err != nil {
logger.Err(ctx, err).Msg("Error setting agent state.")
return err
}
a.status = AgentStatusRunning
logger.Info(ctx).Msg("Agent reloaded module successfully.")
return nil
}