openloops 0.2.8 → 0.2.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -12,6 +12,7 @@ export interface AgentError {
12
12
  timestamp: Date;
13
13
  }
14
14
  type CurrentActivity = 'running' | 'stopped' | (string & {});
15
+ export type ActivityListener = (activity: string) => void;
15
16
  export interface AgentStateProps {
16
17
  status?: AgentStatus;
17
18
  language?: string;
@@ -27,10 +28,12 @@ export declare class AgentState {
27
28
  activeTaskId?: string | undefined;
28
29
  stopRequested?: boolean | undefined;
29
30
  currentActivity?: CurrentActivity | undefined;
31
+ private activityListener?;
30
32
  static fromJSON(props?: AgentStateProps): AgentState;
31
33
  constructor(status?: AgentStatus | undefined, language?: string | undefined, lastError?: AgentError | undefined, activeTaskId?: string | undefined, stopRequested?: boolean | undefined, currentActivity?: CurrentActivity | undefined);
32
34
  toJSON(): AgentStateProps;
33
35
  setLanguage(language?: string): void;
36
+ onActivityChange(listener?: ActivityListener): void;
34
37
  setCurrentActivity(currentActivity: CurrentActivity): void;
35
38
  setStatus(status: AgentStatus): void;
36
39
  setActiveTaskId(taskId?: string): void;
@@ -45,6 +45,7 @@ class AgentState {
45
45
  activeTaskId;
46
46
  stopRequested;
47
47
  currentActivity;
48
+ activityListener;
48
49
  static fromJSON(props = {}) {
49
50
  return new AgentState(
50
51
  props.status,
@@ -68,8 +69,13 @@ class AgentState {
68
69
  setLanguage(language) {
69
70
  if (language) this.language = language;
70
71
  }
72
+ onActivityChange(listener) {
73
+ this.activityListener = listener;
74
+ }
71
75
  setCurrentActivity(currentActivity) {
76
+ const changed = this.currentActivity !== currentActivity;
72
77
  this.currentActivity = currentActivity;
78
+ if (changed) this.activityListener?.(currentActivity);
73
79
  }
74
80
  setStatus(status) {
75
81
  this.status = status;
@@ -22,6 +22,7 @@ class AgentState {
22
22
  activeTaskId;
23
23
  stopRequested;
24
24
  currentActivity;
25
+ activityListener;
25
26
  static fromJSON(props = {}) {
26
27
  return new AgentState(
27
28
  props.status,
@@ -45,8 +46,13 @@ class AgentState {
45
46
  setLanguage(language) {
46
47
  if (language) this.language = language;
47
48
  }
49
+ onActivityChange(listener) {
50
+ this.activityListener = listener;
51
+ }
48
52
  setCurrentActivity(currentActivity) {
53
+ const changed = this.currentActivity !== currentActivity;
49
54
  this.currentActivity = currentActivity;
55
+ if (changed) this.activityListener?.(currentActivity);
50
56
  }
51
57
  setStatus(status) {
52
58
  this.status = status;
@@ -6,6 +6,7 @@ import { ChatSettingsProps } from '../services/chats/entities/chat-settings.enti
6
6
  import { Tool } from '../tools/tool.js';
7
7
  import { Guard } from '../guardrails/guard.js';
8
8
  import { AgentLoop } from './agent-loop';
9
+ import { ActivityListener } from './agent-state';
9
10
  export interface InternalMcp {
10
11
  name: string;
11
12
  url: string;
@@ -58,6 +59,7 @@ export declare class Agent {
58
59
  private mcpListingFailed;
59
60
  private _preHooks;
60
61
  private _postHooks;
62
+ private _activityListeners;
61
63
  private _guards;
62
64
  private awaitingConfirmation;
63
65
  private currentChat;
@@ -101,6 +103,8 @@ export declare class Agent {
101
103
  addTool(tool: Tool): void;
102
104
  addInternalMcp(mcp: InternalMcp): void;
103
105
  addHook(type: HookType, fn: Function): void;
106
+ onActivity(listener: ActivityListener): () => void;
107
+ private emitActivity;
104
108
  addGuard(guard: Guard): void;
105
109
  private preRunner;
106
110
  private runInputGuards;
@@ -76,6 +76,7 @@ class Agent {
76
76
  mcpListingFailed = false;
77
77
  _preHooks = [];
78
78
  _postHooks = [];
79
+ _activityListeners = [];
79
80
  _guards = [];
80
81
  awaitingConfirmation = false;
81
82
  currentChat;
@@ -206,6 +207,21 @@ class Agent {
206
207
  if (type === "pre_execution") this._preHooks.push(fn);
207
208
  if (type === "post_execution") this._postHooks.push(fn);
208
209
  }
210
+ onActivity(listener) {
211
+ this._activityListeners.push(listener);
212
+ return () => {
213
+ this._activityListeners = this._activityListeners.filter((candidate) => candidate !== listener);
214
+ };
215
+ }
216
+ emitActivity(activity) {
217
+ for (const listener of this._activityListeners) {
218
+ try {
219
+ listener(activity);
220
+ } catch (error) {
221
+ this.logger.warn({ error: error?.message ?? error }, "[AGENT] An activity listener failed");
222
+ }
223
+ }
224
+ }
209
225
  addGuard(guard) {
210
226
  this._guards.push(guard);
211
227
  }
@@ -227,6 +243,7 @@ class Agent {
227
243
  this.currentChat.switchLoop(this._loop.name);
228
244
  await this.loadTasks();
229
245
  }
246
+ this.currentChat.state?.onActivityChange((activity) => this.emitActivity(activity));
230
247
  const userMessage = this.currentChat.pushMessage({
231
248
  role: "user",
232
249
  content: this.currentMessage,
@@ -61,6 +61,7 @@ class Agent {
61
61
  mcpListingFailed = false;
62
62
  _preHooks = [];
63
63
  _postHooks = [];
64
+ _activityListeners = [];
64
65
  _guards = [];
65
66
  awaitingConfirmation = false;
66
67
  currentChat;
@@ -191,6 +192,21 @@ class Agent {
191
192
  if (type === "pre_execution") this._preHooks.push(fn);
192
193
  if (type === "post_execution") this._postHooks.push(fn);
193
194
  }
195
+ onActivity(listener) {
196
+ this._activityListeners.push(listener);
197
+ return () => {
198
+ this._activityListeners = this._activityListeners.filter((candidate) => candidate !== listener);
199
+ };
200
+ }
201
+ emitActivity(activity) {
202
+ for (const listener of this._activityListeners) {
203
+ try {
204
+ listener(activity);
205
+ } catch (error) {
206
+ this.logger.warn({ error: error?.message ?? error }, "[AGENT] An activity listener failed");
207
+ }
208
+ }
209
+ }
194
210
  addGuard(guard) {
195
211
  this._guards.push(guard);
196
212
  }
@@ -212,6 +228,7 @@ class Agent {
212
228
  this.currentChat.switchLoop(this._loop.name);
213
229
  await this.loadTasks();
214
230
  }
231
+ this.currentChat.state?.onActivityChange((activity) => this.emitActivity(activity));
215
232
  const userMessage = this.currentChat.pushMessage({
216
233
  role: "user",
217
234
  content: this.currentMessage,
package/docs/agent.md CHANGED
@@ -186,6 +186,25 @@ agent.addHook('post_execution', async () => {
186
186
 
187
187
  Hooks do not run when a guard blocks the message, because the loop never starts.
188
188
 
189
+ ## Activity updates
190
+
191
+ ```typescript
192
+ const stop = agent.onActivity(activity => {
193
+ stream.write(`event: activity\ndata: ${JSON.stringify({ activity })}\n\n`)
194
+ })
195
+
196
+ await agent.run({ input: { chatId, message }, currentUser })
197
+
198
+ stop()
199
+ ```
200
+
201
+ The listener receives every change of `state.currentActivity` while the run is in progress, such as `Starting...`, `Thinking...` or `Running search...`, and a last `stopped` when the run ends. It does not receive a value that repeats the previous one.
202
+
203
+ - The listener belongs to that `Agent` instance and to the chat of its current run. Nothing is shared between instances, so concurrent runs in the same process, even of different users, never hear each other. As always, create one `Agent` per run and do not share it between concurrent requests.
204
+ - It is called synchronously from the loop, so keep it fast and do not await inside it. An error thrown by a listener is logged and ignored, the run goes on and the other listeners still run.
205
+ - `onActivity` returns a function that removes the listener.
206
+ - Past values are not replayed. If a client connects late or loses its connection, read `state.currentActivity` of the chat, which is saved with it.
207
+
189
208
  ## Stopping a running turn
190
209
 
191
210
  ```typescript
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "openloops",
3
- "version": "0.2.8",
3
+ "version": "0.2.9",
4
4
  "description": "Open source harness for AI agents: a TypeScript runtime with persistence, tools, MCP trust, approvals and autonomous jobs, where the loop is pluggable",
5
5
  "author": {
6
6
  "name": "Jose Barcos",