Skip to content

Commit d31a3b9

Browse files
committed
*
1 parent 4f0ac23 commit d31a3b9

File tree

5 files changed

+774
-0
lines changed

5 files changed

+774
-0
lines changed

docs.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
"group": "AssemblyScript SDK",
111111
"pages": [
112112
"modus/sdk/assemblyscript/overview",
113+
"modus/sdk/assemblyscript/agents",
113114
"modus/sdk/assemblyscript/collections",
114115
"modus/sdk/assemblyscript/console",
115116
"modus/sdk/assemblyscript/dgraph",
@@ -126,6 +127,7 @@
126127
"group": "Go SDK",
127128
"pages": [
128129
"modus/sdk/go/overview",
130+
"modus/sdk/go/agents",
129131
"modus/sdk/go/collections",
130132
"modus/sdk/go/console",
131133
"modus/sdk/go/dgraph",
Lines changed: 344 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,344 @@
1+
---
2+
title: "Agents APIs"
3+
sidebarTitle: "Agents"
4+
description:
5+
"Build stateful agents that maintain persistent memory across interactions"
6+
icon: "circle-small"
7+
iconType: "solid"
8+
---
9+
10+
import { SdkHeader } from "/snippets/sdk-header.mdx"
11+
import SdkTip from "/snippets/sdk-tip.mdx"
12+
13+
<SdkHeader language="AssemblyScript" feature="Agents" />
14+
15+
The Modus Agents APIs allow you to create stateful agents that maintain
16+
persistent memory across interactions, survive system failures, and coordinate
17+
complex multi-step operations.
18+
19+
## Import
20+
21+
To begin, import the `agents` namespace and `Agent` base class from the SDK:
22+
23+
```ts
24+
import { agents, Agent, AgentInfo } from "@hypermode/modus-sdk-as"
25+
```
26+
27+
## Agent APIs
28+
29+
{/* vale Google.Headings = NO */}
30+
31+
The APIs in the `agents` namespace are below, organized by category.
32+
33+
<SdkTip />
34+
35+
### Agent Management Functions
36+
37+
#### register
38+
39+
Register an agent class with the Modus runtime before it can be instantiated.
40+
41+
```ts
42+
function register<T extends Agent>(): void
43+
```
44+
45+
<ResponseField name="T" required>
46+
The agent class type that extends the `Agent` base class.
47+
</ResponseField>
48+
49+
<Note>
50+
Agent registration must be done at the module level, outside of any function.
51+
</Note>
52+
53+
#### start
54+
55+
Create and start a new agent instance.
56+
57+
```ts
58+
function start(agentName: string): AgentInfo
59+
```
60+
61+
<ResponseField name="agentName" type="string" required>
62+
The name of the agent class to instantiate. This must match the `name`
63+
property returned by the agent class.
64+
</ResponseField>
65+
66+
#### stop
67+
68+
Stop an agent instance. Once stopped, the agent can't be resumed.
69+
70+
```ts
71+
function stop(agentId: string): AgentInfo
72+
```
73+
74+
<ResponseField name="agentId" type="string" required>
75+
The unique identifier of the agent instance to stop.
76+
</ResponseField>
77+
78+
#### get info
79+
80+
Get information about a specific agent instance.
81+
82+
```ts
83+
function getInfo(agentId: string): AgentInfo
84+
```
85+
86+
<ResponseField name="agentId" type="string" required>
87+
The unique identifier of the agent instance.
88+
</ResponseField>
89+
90+
#### list all
91+
92+
List all active agent instances.
93+
94+
```ts
95+
function listAll(): AgentInfo[]
96+
```
97+
98+
### Communication Functions
99+
100+
#### send message
101+
102+
Send a synchronous message to an agent and wait for a response.
103+
104+
```ts
105+
function sendMessage(
106+
agentId: string,
107+
messageName: string,
108+
data: string | null = null,
109+
): string | null
110+
```
111+
112+
<ResponseField name="agentId" type="string" required>
113+
The unique identifier of the target agent instance.
114+
</ResponseField>
115+
116+
<ResponseField name="messageName" type="string" required>
117+
The name of the message to send to the agent.
118+
</ResponseField>
119+
120+
<ResponseField name="data" type="string | null">
121+
Optional data payload to send with the message.
122+
</ResponseField>
123+
124+
#### sendMessageAsync
125+
126+
Send an asynchronous message to an agent without waiting for a response.
127+
128+
```ts
129+
function sendMessageAsync(
130+
agentId: string,
131+
messageName: string,
132+
data: string | null = null,
133+
): void
134+
```
135+
136+
<ResponseField name="agentId" type="string" required>
137+
The unique identifier of the target agent instance.
138+
</ResponseField>
139+
140+
<ResponseField name="messageName" type="string" required>
141+
The name of the message to send to the agent.
142+
</ResponseField>
143+
144+
<ResponseField name="data" type="string | null">
145+
Optional data payload to send with the message.
146+
</ResponseField>
147+
148+
### Agent Base Class
149+
150+
#### Agent
151+
152+
The base class that all agents must extend.
153+
154+
```ts
155+
abstract class Agent {
156+
abstract get name(): string
157+
abstract onReceiveMessage(
158+
messageName: string,
159+
data: string | null,
160+
): string | null
161+
162+
getState(): string | null
163+
setState(data: string | null): void
164+
onInitialize(): void
165+
onSuspend(): void
166+
onResume(): void
167+
onTerminate(): void
168+
}
169+
```
170+
171+
<ResponseField name="name" type="string" required>
172+
Abstract property that must return a unique name for the agent class.
173+
</ResponseField>
174+
175+
<ResponseField
176+
name="onReceiveMessage(messageName, data)"
177+
type="method"
178+
required
179+
>
180+
Abstract method that handles incoming messages to the agent. Must be
181+
implemented by all agent classes.
182+
</ResponseField>
183+
184+
<ResponseField name="getState()" type="method">
185+
Optional method that returns the agent's current state as a string for
186+
persistence. Called automatically when the agent needs to be suspended or
187+
migrated.
188+
</ResponseField>
189+
190+
<ResponseField name="setState(data)" type="method">
191+
Optional method that restores the agent's state from a string. Called
192+
automatically when the agent is resumed or migrated.
193+
</ResponseField>
194+
195+
<ResponseField name="onInitialize()" type="method">
196+
Optional lifecycle method called when the agent is first created.
197+
</ResponseField>
198+
199+
<ResponseField name="onSuspend()" type="method">
200+
Optional lifecycle method called when the agent is about to be suspended.
201+
</ResponseField>
202+
203+
<ResponseField name="onResume()" type="method">
204+
Optional lifecycle method called when the agent is resumed from suspension.
205+
</ResponseField>
206+
207+
<ResponseField name="onTerminate()" type="method">
208+
Optional lifecycle method called when the agent is about to be terminated.
209+
</ResponseField>
210+
211+
### Types
212+
213+
#### AgentInfo
214+
215+
Information about an agent instance.
216+
217+
```ts
218+
class AgentInfo {
219+
id: string
220+
name: string
221+
status: string
222+
}
223+
```
224+
225+
<ResponseField name="id" type="string">
226+
The unique identifier of the agent instance.
227+
</ResponseField>
228+
229+
<ResponseField name="name" type="string">
230+
The name of the agent class.
231+
</ResponseField>
232+
233+
<ResponseField name="status" type="string">
234+
The current status of the agent instance.
235+
</ResponseField>
236+
237+
## Example Usage
238+
239+
Here's a complete example of a simple counter agent:
240+
241+
### Agent Implementation
242+
243+
```ts
244+
import { Agent } from "@hypermode/modus-sdk-as"
245+
246+
export class CounterAgent extends Agent {
247+
get name(): string {
248+
return "Counter"
249+
}
250+
251+
private count: i32 = 0
252+
253+
getState(): string | null {
254+
return this.count.toString()
255+
}
256+
257+
setState(data: string | null): void {
258+
if (data == null) {
259+
return
260+
}
261+
this.count = i32.parse(data)
262+
}
263+
264+
onInitialize(): void {
265+
console.info("Counter agent started")
266+
}
267+
268+
onReceiveMessage(name: string, data: string | null): string | null {
269+
if (name == "count") {
270+
return this.count.toString()
271+
}
272+
273+
if (name == "increment") {
274+
if (data != null) {
275+
this.count += i32.parse(data)
276+
} else {
277+
this.count++
278+
}
279+
return this.count.toString()
280+
}
281+
282+
return null
283+
}
284+
}
285+
```
286+
287+
### Function Integration
288+
289+
```ts
290+
import { agents, AgentInfo } from "@hypermode/modus-sdk-as"
291+
import { CounterAgent } from "./counterAgent"
292+
293+
// Register the agent
294+
agents.register<CounterAgent>()
295+
296+
export function startCounterAgent(): AgentInfo {
297+
return agents.start("Counter")
298+
}
299+
300+
export function getCount(agentId: string): i32 {
301+
const count = agents.sendMessage(agentId, "count")
302+
if (count == null) {
303+
return 0
304+
}
305+
return i32.parse(count)
306+
}
307+
308+
export function updateCount(agentId: string): i32 {
309+
const count = agents.sendMessage(agentId, "increment")
310+
if (count == null) {
311+
return 0
312+
}
313+
return i32.parse(count)
314+
}
315+
316+
export function updateCountAsync(agentId: string, qty: i32): void {
317+
agents.sendMessageAsync(agentId, "increment", qty.toString())
318+
}
319+
```
320+
321+
### GraphQL Usage
322+
323+
Once deployed, your agent functions become available via GraphQL:
324+
325+
```graphql
326+
# Start a new agent
327+
mutation {
328+
startCounterAgent {
329+
id
330+
name
331+
status
332+
}
333+
}
334+
335+
# Get the current count
336+
query {
337+
getCount(agentId: "agent_abc123")
338+
}
339+
340+
# Increment the count
341+
mutation {
342+
updateCount(agentId: "agent_abc123")
343+
}
344+
```

modus/sdk/assemblyscript/overview.mdx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ APIs.
3636
| [`http`](./http) | Provides access to external HTTP endpoints, including REST APIs and other services |
3737
| [`graphql`](./graphql) | Allows you to securely call and fetch data from any GraphQL endpoint |
3838

39+
### Agent APIs
40+
41+
| Namespace | Purpose |
42+
| :------------------- | :--------------------------------------------------------------------------------------- |
43+
| [`agents`](./agents) | Create stateful agents that maintain persistent memory and coordinate complex operations |
44+
3945
### Database APIs
4046

4147
| Namespace | Purpose |

0 commit comments

Comments
 (0)