Skip to content
This repository was archived by the owner on Sep 11, 2025. It is now read-only.
Closed
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
14 changes: 12 additions & 2 deletions runtime/actors/agents.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,17 @@ func SendAgentMessage(ctx context.Context, agentId string, msgName string, data
}
}

func PublishAgentEvent(ctx context.Context, agentId, eventName string, eventData *string) error {
func PublishAgentEvent(ctx context.Context, agentId, eventName string, eventData *string, createdAt *string) error {
var createdTime time.Time
var err error
if createdAt != nil {
createdTime, err = time.Parse(time.RFC3339Nano, *createdAt)
if err != nil {
return fmt.Errorf("error parsing created timestamp: %w", err)
}
} else {
createdTime = time.Now()
}

var data any
if eventData != nil {
Expand All @@ -246,7 +256,7 @@ func PublishAgentEvent(ctx context.Context, agentId, eventName string, eventData
event := &messages.AgentEventMessage{
Name: eventName,
Data: dataValue,
Timestamp: timestamppb.Now(),
Copy link
Contributor Author

@danstarns danstarns Jun 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem stems from this Timestamp: timestamppb.Now() - its the published to network time, not the event captured time.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be almost zero delay between capture and publish though. There's no queue involved at that level. It's just a wasm guest to host function call.

Timestamp: timestamppb.New(createdTime),
}

eventMsg, err := anypb.New(event)
Expand Down
3 changes: 2 additions & 1 deletion runtime/actors/wasmagent.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ func (a *wasmAgentActor) updateStatus(ctx context.Context, status AgentStatus) e
}

data := fmt.Sprintf(`{"status":"%s"}`, a.status)
if err := PublishAgentEvent(ctx, a.agentId, agentStatusEventName, &data); err != nil {
createdAt := time.Now().Format(time.RFC3339Nano)
if err := PublishAgentEvent(ctx, a.agentId, agentStatusEventName, &data, &createdAt); err != nil {
return fmt.Errorf("error publishing agent status event: %w", err)
}

Expand Down
2 changes: 1 addition & 1 deletion runtime/hostfunctions/agents.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func init() {

registerHostFunction(module_name, "publishEvent", actors.PublishAgentEvent,
withErrorMessage("Error publishing agent event."),
withMessageDetail(func(agentId, eventName string, eventData *string) string {
withMessageDetail(func(agentId, eventName string, eventData *string, createdAt *string) string {
return fmt.Sprintf("AgentId: %s, EventName: %s", agentId, eventName)
}))
}
12 changes: 10 additions & 2 deletions sdk/assemblyscript/src/assembly/__tests__/agent.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,16 @@ mockImport(

mockImport(
"modus_agents.publishEvent",
// eslint-disable-next-line @typescript-eslint/no-unused-vars
(agentId: string, eventName: string, eventData: string | null): void => {},
(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
agentId: string,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
eventName: string,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
eventData: string | null,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
createdAt: string,
): void => {},
);

it("should serialize an AgentStatus using type aliases", () => {
Expand Down
5 changes: 4 additions & 1 deletion sdk/assemblyscript/src/assembly/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,10 @@ export abstract class Agent {
* Publishes an event from this agent to any subscribers.
*/
publishEvent(event: AgentEvent): void {
const createdAt = new Date(Date.now()).toISOString();

const data = JSON.stringify(event);
hostPublishEvent(this.id, event.eventName, data);
hostPublishEvent(this.id, event.eventName, data, createdAt);
}
}

Expand Down Expand Up @@ -129,6 +131,7 @@ declare function hostPublishEvent(
agentId: string,
eventName: string,
eventData: string | null,
createdAt: string,
): void;

/**
Expand Down
6 changes: 4 additions & 2 deletions sdk/go/pkg/agents/agents.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,15 +290,17 @@ func (a *AgentBase) OnReceiveMessage(msgName string, data *string) (*string, err
return nil, nil
}

// Publishes an event from this agent to any subscribers.
func (a *AgentBase) PublishEvent(event AgentEvent) error {
createdAt := time.Now().Format(time.RFC3339Nano)

bytes, err := utils.JsonSerialize(event)
if err != nil {
return fmt.Errorf("failed to serialize event data: %w", err)
}
data := string(bytes)
name := event.EventName()
hostPublishEvent(activeAgentId, &name, &data)

hostPublishEvent(activeAgentId, &name, &data, &createdAt)
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions sdk/go/pkg/agents/imports_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,6 @@ func hostListAgents() *[]AgentInfo {
}
}

func hostPublishEvent(agentId, eventName, eventData *string) {
PublishEventCallStack.Push(agentId, eventName, eventData)
func hostPublishEvent(agentId, eventName, eventData, createdAt *string) {
PublishEventCallStack.Push(agentId, eventName, eventData, createdAt)
}
2 changes: 1 addition & 1 deletion sdk/go/pkg/agents/imports_wasi.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,4 @@ func hostListAgents() *[]AgentInfo {

//go:noescape
//go:wasmimport modus_agents publishEvent
func hostPublishEvent(agentId, eventName, eventData *string)
func hostPublishEvent(agentId, eventName, eventData, createdAt *string)
Loading