@almyty/agents 1.5.6 → 1.5.8

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.
package/README.md CHANGED
@@ -38,7 +38,7 @@ other one when you ask the wrong pair.
38
38
  |------|-------------|
39
39
  | `--input '<json>'` | Input payload. Parsed as JSON; a plain string is passed through as-is, which is what an autonomous agent usually wants. |
40
40
  | `--resume <conversation-id>` | Autonomous: continue a previous conversation |
41
- | `--watch` | Autonomous: stream steps until the run reaches a terminal state |
41
+ | `--watch` | Autonomous: print each step as it finishes until the run reaches a terminal state; reconnects from the last step printed if the connection drops, and stops with exit code 4 if the run is gone |
42
42
  | `--timeout <s>` | Autonomous `--watch`: stop waiting after this long (default `300`) |
43
43
  | `--max-steps <n>` | Autonomous: step ceiling |
44
44
  | `--max-cost-cents <n>` | Autonomous: cost ceiling, in cents |
package/dist/index.js CHANGED
@@ -17,6 +17,7 @@ import { parseArgs, flagNumber, flagString } from './args.js';
17
17
  import { EXIT } from './exit-codes.js';
18
18
  import { VERSION } from './version.js';
19
19
  import { formatAgentDetail, formatAgentLine, formatExecutionSummary, formatNodeResults, formatRunDetail, formatRunSummary, formatStep, formatTrace, notActiveMessage, runSucceeded, } from './format.js';
20
+ import { watchRun } from './watch.js';
20
21
  /** Flags that are switches, so they never swallow the next token. */
21
22
  const BOOLEAN_FLAGS = ['watch', 'steps'];
22
23
  function printHelp() {
@@ -42,7 +43,7 @@ Run options:
42
43
  --max-steps <n> Autonomous: step ceiling
43
44
  --max-cost-cents <n> Autonomous: cost ceiling, in cents
44
45
  --max-duration-ms <ms> Autonomous: wall-clock ceiling
45
- --watch Autonomous: stream steps until the run ends
46
+ --watch Autonomous: print each step as it lands until the run ends (reconnects if the connection drops)
46
47
  --timeout <s> Autonomous --watch: give up waiting after this long (default 300)
47
48
 
48
49
  List options (runs, executions):
@@ -222,22 +223,28 @@ async function cmdRun(args) {
222
223
  }
223
224
  return;
224
225
  }
225
- let printed = 0;
226
226
  const timeoutSeconds = flagNumber(args.flags, 'timeout') ?? 300;
227
- const final = await client.pollRun(agent.id, runStub.id, {
228
- timeoutMs: timeoutSeconds * 1000,
229
- onStep: (run) => {
230
- if (args.flags.json)
231
- return;
232
- const steps = (run.steps ?? []);
233
- for (let i = printed; i < steps.length; i++) {
234
- const line = formatStep(steps[i], i);
227
+ // Followed from the run's log, picking up where it left off when the
228
+ // connection drops (watch.ts); a run that is gone ends it at once.
229
+ let final;
230
+ try {
231
+ final = await watchRun(client, agent.id, runStub.id, {
232
+ timeoutMs: timeoutSeconds * 1000,
233
+ onStep: (step, index) => {
234
+ if (args.flags.json)
235
+ return;
236
+ const line = formatStep(step, index);
235
237
  if (line)
236
238
  console.log(line);
237
- }
238
- printed = steps.length;
239
- },
240
- });
239
+ },
240
+ });
241
+ }
242
+ catch (err) {
243
+ if (err?.status === 404 || err?.status === 410) {
244
+ fail(`Run ${runStub.id} is no longer there to watch.`, EXIT.NOT_FOUND);
245
+ }
246
+ throw err;
247
+ }
241
248
  // A failed run is a failed command.
242
249
  //
243
250
  // pollRun returns on ANY terminal status, and this printed
@@ -0,0 +1,24 @@
1
+ /**
2
+ * `run --watch`: follow an autonomous run from its event log.
3
+ *
4
+ * It used to poll the run every 1.5 s and print the steps the row had
5
+ * grown. It now reads the run's stream (AlmytyClient.followRun), which
6
+ * sends the run's log event by event and picks up after the last one it
7
+ * had when the connection drops, so a step is printed once, as its commit
8
+ * lands, whatever the network does. A run that is gone (404, 410) or not
9
+ * yours to see ends the watch at once; a stream that keeps failing
10
+ * otherwise falls back to polling, as before.
11
+ */
12
+ import { type AgentRun, type AlmytyClient, type RunLogEvent } from '@almyty/client';
13
+ export interface WatchOptions {
14
+ /** Give up after this long, as `--timeout` says. */
15
+ timeoutMs: number;
16
+ /** Each step once, in order, with its index. */
17
+ onStep: (step: unknown, index: number) => void;
18
+ /** Waits before each retry of the stream, in ms (the client's own by default). */
19
+ retryDelaysMs?: number[];
20
+ }
21
+ /** A run's steps with one more logged event applied: the fold the backend checks its rows against, for steps. */
22
+ export declare function applyLogToSteps(steps: unknown[], event: RunLogEvent): unknown[];
23
+ /** Watch a run until it ends or waits on a person; resolves with the run as it stood then. */
24
+ export declare function watchRun(client: AlmytyClient, agentId: string, runId: string, options: WatchOptions): Promise<AgentRun>;
package/dist/watch.js ADDED
@@ -0,0 +1,86 @@
1
+ /**
2
+ * `run --watch`: follow an autonomous run from its event log.
3
+ *
4
+ * It used to poll the run every 1.5 s and print the steps the row had
5
+ * grown. It now reads the run's stream (AlmytyClient.followRun), which
6
+ * sends the run's log event by event and picks up after the last one it
7
+ * had when the connection drops, so a step is printed once, as its commit
8
+ * lands, whatever the network does. A run that is gone (404, 410) or not
9
+ * yours to see ends the watch at once; a stream that keeps failing
10
+ * otherwise falls back to polling, as before.
11
+ */
12
+ import { isPermanentStreamError } from '@almyty/client';
13
+ /** A run's steps with one more logged event applied: the fold the backend checks its rows against, for steps. */
14
+ export function applyLogToSteps(steps, event) {
15
+ const p = event.payload ?? {};
16
+ switch (event.type) {
17
+ case 'run_started':
18
+ return Array.isArray(p.steps) ? [...p.steps] : [];
19
+ case 'model_call':
20
+ case 'step_recorded':
21
+ case 'tool_call':
22
+ case 'step_replaced': {
23
+ if (typeof p.index !== 'number')
24
+ return steps;
25
+ const next = [...steps];
26
+ next[p.index] = p.step;
27
+ return next;
28
+ }
29
+ case 'tool_result': {
30
+ if (typeof p.index !== 'number')
31
+ return steps;
32
+ const next = [...steps];
33
+ next[p.index] = { ...next[p.index], ...(p.result ?? {}) };
34
+ return next;
35
+ }
36
+ case 'steps_reset':
37
+ return Array.isArray(p.steps) ? [...p.steps] : [];
38
+ default:
39
+ return steps;
40
+ }
41
+ }
42
+ /** Watch a run until it ends or waits on a person; resolves with the run as it stood then. */
43
+ export async function watchRun(client, agentId, runId, options) {
44
+ let steps = [];
45
+ let printed = 0;
46
+ // A step goes out once its commit lands, as the row would have had it.
47
+ const flush = (all) => {
48
+ for (let i = printed; i < all.length; i++)
49
+ options.onStep(all[i], i);
50
+ printed = Math.max(printed, all.length);
51
+ };
52
+ const deadline = Date.now() + options.timeoutMs;
53
+ const timeUp = new AbortController();
54
+ const timer = setTimeout(() => timeUp.abort(), options.timeoutMs);
55
+ timer.unref?.();
56
+ const tooLong = () => new Error(`Run ${runId} did not finish within ${Math.round(options.timeoutMs / 1000)}s`);
57
+ let final;
58
+ try {
59
+ final = await client.followRun(agentId, runId, () => undefined, timeUp.signal, {
60
+ onLog: (event) => {
61
+ steps = applyLogToSteps(steps, event);
62
+ if (event.type === 'step_committed')
63
+ flush(steps);
64
+ },
65
+ stopOnWaitingInput: true,
66
+ ...(options.retryDelaysMs ? { retryDelaysMs: options.retryDelaysMs } : {}),
67
+ });
68
+ }
69
+ catch (err) {
70
+ if (timeUp.signal.aborted)
71
+ throw tooLong();
72
+ if (isPermanentStreamError(err))
73
+ throw err;
74
+ // The stream would not stay up: watch the row instead, for what is left of the time.
75
+ final = await client.pollRun(agentId, runId, {
76
+ timeoutMs: Math.max(0, deadline - Date.now()),
77
+ onStep: (run) => flush((run.steps ?? [])),
78
+ });
79
+ }
80
+ finally {
81
+ clearTimeout(timer);
82
+ }
83
+ // Whatever the stream did not carry (a server without the run log).
84
+ flush((final.steps ?? []));
85
+ return final;
86
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@almyty/agents",
3
- "version": "1.5.6",
3
+ "version": "1.5.8",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -28,13 +28,13 @@
28
28
  "author": "almyty",
29
29
  "license": "Apache-2.0",
30
30
  "dependencies": {
31
- "@almyty/client": "^1.5.5"
31
+ "@almyty/client": "^1.5.7"
32
32
  },
33
33
  "devDependencies": {
34
- "@types/node": "^25.4.0",
34
+ "@types/node": "^26.6.6",
35
35
  "tsx": "^4.7.0",
36
36
  "typescript": "^5.3.0",
37
- "vitest": "^4.1.0"
37
+ "vitest": "^5.0.5"
38
38
  },
39
39
  "homepage": "https://almyty.com",
40
40
  "repository": {