@ziggs-ai/agent-sdk 0.45.0 → 0.45.3
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/CHANGELOG.md +12 -0
- package/dist/Agent.d.ts +19 -0
- package/dist/Agent.js +44 -6
- package/dist/platform/inboxLoop.d.ts +13 -0
- package/dist/platform/inboxLoop.js +19 -5
- package/dist/platform/tools/tier1/protocolRunner.js +11 -6
- package/dist/platform/tools/tier1/protocolTools.js +1 -1
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,18 @@
|
|
|
2
2
|
|
|
3
3
|
## Unreleased
|
|
4
4
|
|
|
5
|
+
## 0.45.3
|
|
6
|
+
|
|
7
|
+
- `sharedInbox` (or `ZIGGS_AGENT_SHARED_INBOX=1`) runs the agent as one of several host processes. Each read tells the server how many more threads this process can start, so work spreads across processes and one thread stays on one process at a time. Only the process holding the inbox lease runs the agent's declared polls and restores its saved wake-ups, and the polls move when leadership does. Every wake reloads the thread's session from `sessionStore`, since the thread may have last run elsewhere. Requires api-client 0.40.3.
|
|
8
|
+
|
|
9
|
+
## 0.45.2
|
|
10
|
+
|
|
11
|
+
- A message record without `rootMessageId` is refused: the wake fails and the message stays pending, instead of running on a lane of its own. Every message the backend writes names its thread.
|
|
12
|
+
|
|
13
|
+
## 0.45.1
|
|
14
|
+
|
|
15
|
+
- `task_set_result` says a completion with no stated charge is free on an agreement priced at completion. A charge written as text ("300", "300 points") is read as a number; a negative one is dropped. Requires api-client 0.40.2.
|
|
16
|
+
|
|
5
17
|
## 0.45.0
|
|
6
18
|
|
|
7
19
|
- A message wake runs on its thread's lane. The lane is named from the thread's first message, the work (agreement and task) and every reader, so a later message in the same thread, under the same work, read by the same people, lands on the same lane, and the agent's Claude session, saved session state and notes carry over between messages. A reader joining or leaving, or another agreement or task, starts a new lane: an agent carries over only what everyone reading can read. Notes on a thread lane are kept under `thread:<digest>`. Task wakes and messages with no known thread keep their execution lane. The default session resolver names a thread `thr-<rootMessageId>` before falling back to `msgn-<messageId>`. Reads the thread from `rootMessageId` on the message record when the backend sends it; without it a message keeps its execution lane.
|
package/dist/Agent.d.ts
CHANGED
|
@@ -202,6 +202,15 @@ export interface AgentOptions {
|
|
|
202
202
|
* to `ZIGGS_AGENT_INBOX_CONCURRENCY`, else 4.
|
|
203
203
|
*/
|
|
204
204
|
inboxConcurrency?: number;
|
|
205
|
+
/**
|
|
206
|
+
* Run as one of several processes hosting this agent at once. Each process
|
|
207
|
+
* takes its own threads (one thread stays on one process at a time); the
|
|
208
|
+
* one holding the inbox lease leads and alone runs the agent's declared
|
|
209
|
+
* polls and restores its saved wake-ups. Every wake reloads the thread's
|
|
210
|
+
* session from `sessionStore`, since the thread may have last run on
|
|
211
|
+
* another process. Defaults to `ZIGGS_AGENT_SHARED_INBOX=1`.
|
|
212
|
+
*/
|
|
213
|
+
sharedInbox?: boolean;
|
|
205
214
|
}
|
|
206
215
|
/**
|
|
207
216
|
* One wake, as the brain received it.
|
|
@@ -467,6 +476,16 @@ export declare class Agent {
|
|
|
467
476
|
private _armIfTimedParked;
|
|
468
477
|
private _resolveOwnAgentId;
|
|
469
478
|
connectAsync(_timeout?: number): Promise<this>;
|
|
479
|
+
/** Whether this process runs as one of several hosts of the agent. */
|
|
480
|
+
private get sharedInbox();
|
|
481
|
+
private leading;
|
|
482
|
+
private wakesRestored;
|
|
483
|
+
/**
|
|
484
|
+
* Shared hosts: start the agent's polls (and restore its saved wake-ups,
|
|
485
|
+
* once) when this process becomes the leader, and stop the polls when it
|
|
486
|
+
* stops leading, so exactly one process runs them.
|
|
487
|
+
*/
|
|
488
|
+
private setLeadership;
|
|
470
489
|
/**
|
|
471
490
|
* Arm the lane's timer for the agent's own `wake_at`, and save it so a
|
|
472
491
|
* restart arms it again.
|
package/dist/Agent.js
CHANGED
|
@@ -43,7 +43,7 @@ import { resolveBackendUrl } from './shared/backendUrl.js';
|
|
|
43
43
|
import { laneUnderAgreement } from '@ziggs-ai/api-client';
|
|
44
44
|
import { TaskService } from './platform/tasks/TaskService.js';
|
|
45
45
|
import { AgreementService } from './platform/agreements/AgreementService.js';
|
|
46
|
-
import { INBOX_STOP_TIMEOUT_MS, runInboxLoop } from './platform/inboxLoop.js';
|
|
46
|
+
import { INBOX_STOP_TIMEOUT_MS, runInboxLoop, sharedInboxHost } from './platform/inboxLoop.js';
|
|
47
47
|
import { requestExactMatch } from './platform/requestMatch.js';
|
|
48
48
|
import { SeenMessages } from './platform/SeenMessages.js';
|
|
49
49
|
import { runtimeLog } from './shared/runtimeLog.js';
|
|
@@ -284,6 +284,7 @@ export class Agent {
|
|
|
284
284
|
wakeTap: resolved.wakeTap,
|
|
285
285
|
inboxCatchUp: resolved.inboxCatchUp,
|
|
286
286
|
inboxConcurrency: resolved.inboxConcurrency,
|
|
287
|
+
sharedInbox: resolved.sharedInbox,
|
|
287
288
|
telemetryIngest: resolved.telemetryIngest,
|
|
288
289
|
brain: resolved.brain,
|
|
289
290
|
polls: resolved.polls,
|
|
@@ -1170,7 +1171,9 @@ export class Agent {
|
|
|
1170
1171
|
this.stateTimers.consumeForEvent(frame, laneKey);
|
|
1171
1172
|
// Cold-start recovery: restore FSM state from sessionStore when the
|
|
1172
1173
|
// in-memory lane has no prior context (e.g. after a container restart).
|
|
1173
|
-
|
|
1174
|
+
// A shared host reloads on every wake: the thread may have run on another
|
|
1175
|
+
// process since this one last held it, and its copy would be stale.
|
|
1176
|
+
if (this.sessionStore && (this.sharedInbox || (session.ctx === undefined && session.state === undefined))) {
|
|
1174
1177
|
try {
|
|
1175
1178
|
const persisted = await this.sessionStore.load(laneKey);
|
|
1176
1179
|
if (persisted) {
|
|
@@ -1707,6 +1710,8 @@ export class Agent {
|
|
|
1707
1710
|
seen: this.seenMessages,
|
|
1708
1711
|
label,
|
|
1709
1712
|
concurrency: this.options.inboxConcurrency,
|
|
1713
|
+
sharedHost: this.sharedInbox,
|
|
1714
|
+
onLeadership: (leader) => this.setLeadership(leader),
|
|
1710
1715
|
onMessage: (text, metadata) => {
|
|
1711
1716
|
return this.handleMessage(text, metadata);
|
|
1712
1717
|
},
|
|
@@ -1732,13 +1737,46 @@ export class Agent {
|
|
|
1732
1737
|
},
|
|
1733
1738
|
});
|
|
1734
1739
|
}
|
|
1735
|
-
this.pollRunner?.start();
|
|
1736
1740
|
this.started = true;
|
|
1737
|
-
//
|
|
1738
|
-
//
|
|
1739
|
-
|
|
1741
|
+
// Shared hosts: only the leader runs the agent's polls and restores its
|
|
1742
|
+
// saved wake-ups, or every process would run them. The inbox tells this
|
|
1743
|
+
// process whether it leads after each read (setLeadership).
|
|
1744
|
+
if (!this.sharedInbox || !this.inboxLoopEnabled) {
|
|
1745
|
+
this.pollRunner?.start();
|
|
1746
|
+
// Not awaited: starting stays synchronous, so a second connect in the
|
|
1747
|
+
// same turn still sees `started`. A wake re-armed a moment later is fine.
|
|
1748
|
+
void this.restoreWakes();
|
|
1749
|
+
}
|
|
1740
1750
|
return this;
|
|
1741
1751
|
}
|
|
1752
|
+
/** Whether this process runs as one of several hosts of the agent. */
|
|
1753
|
+
get sharedInbox() {
|
|
1754
|
+
return sharedInboxHost(this.options.sharedInbox);
|
|
1755
|
+
}
|
|
1756
|
+
leading = false;
|
|
1757
|
+
wakesRestored = false;
|
|
1758
|
+
/**
|
|
1759
|
+
* Shared hosts: start the agent's polls (and restore its saved wake-ups,
|
|
1760
|
+
* once) when this process becomes the leader, and stop the polls when it
|
|
1761
|
+
* stops leading, so exactly one process runs them.
|
|
1762
|
+
*/
|
|
1763
|
+
setLeadership(leader) {
|
|
1764
|
+
if (!this.sharedInbox || leader === this.leading)
|
|
1765
|
+
return;
|
|
1766
|
+
this.leading = leader;
|
|
1767
|
+
if (leader) {
|
|
1768
|
+
this.pollRunner?.start();
|
|
1769
|
+
if (!this.wakesRestored) {
|
|
1770
|
+
this.wakesRestored = true;
|
|
1771
|
+
void this.restoreWakes();
|
|
1772
|
+
}
|
|
1773
|
+
runtimeLog.info(this.options.name || 'agent', 'leads its shared inbox: running polls');
|
|
1774
|
+
}
|
|
1775
|
+
else {
|
|
1776
|
+
this.pollRunner?.stop();
|
|
1777
|
+
runtimeLog.info(this.options.name || 'agent', 'no longer leads its shared inbox: polls stopped');
|
|
1778
|
+
}
|
|
1779
|
+
}
|
|
1742
1780
|
/**
|
|
1743
1781
|
* Arm the lane's timer for the agent's own `wake_at`, and save it so a
|
|
1744
1782
|
* restart arms it again.
|
|
@@ -41,7 +41,20 @@ export interface InboxLoopDeps {
|
|
|
41
41
|
* `ZIGGS_AGENT_INBOX_CONCURRENCY`, else {@link DEFAULT_INBOX_CONCURRENCY}.
|
|
42
42
|
*/
|
|
43
43
|
concurrency?: number;
|
|
44
|
+
/**
|
|
45
|
+
* This process is one of several hosting the agent. Each read says how many
|
|
46
|
+
* more threads this host can start, so the work spreads across hosts.
|
|
47
|
+
* Defaults to `ZIGGS_AGENT_SHARED_INBOX=1`.
|
|
48
|
+
*/
|
|
49
|
+
sharedHost?: boolean;
|
|
50
|
+
/**
|
|
51
|
+
* Shared hosts: told after every read whether this host leads (holds the
|
|
52
|
+
* inbox lease). The leader runs the agent's polls and timers.
|
|
53
|
+
*/
|
|
54
|
+
onLeadership?: (leader: boolean) => void;
|
|
44
55
|
}
|
|
56
|
+
/** Whether this process runs as one of several hosts of its agents. */
|
|
57
|
+
export declare function sharedInboxHost(explicit?: boolean): boolean;
|
|
45
58
|
export interface InboxPassResult {
|
|
46
59
|
/** Delivery references this envelope carried. */
|
|
47
60
|
deliveries: number;
|
|
@@ -3,6 +3,12 @@ import { deliveryHandlingId, withWorkExecution } from '@ziggs-ai/api-client';
|
|
|
3
3
|
import { MessageRecordsClient, InboxClient, planInboxAck, planPartialInboxAck, } from '@ziggs-ai/api-client';
|
|
4
4
|
import { runtimeLog } from '../shared/runtimeLog.js';
|
|
5
5
|
import { isRateLimited } from '@ziggs-ai/api-client';
|
|
6
|
+
/** Whether this process runs as one of several hosts of its agents. */
|
|
7
|
+
export function sharedInboxHost(explicit) {
|
|
8
|
+
if (explicit !== undefined)
|
|
9
|
+
return explicit;
|
|
10
|
+
return /^(1|true|yes|shared)$/i.test(process.env.ZIGGS_AGENT_SHARED_INBOX ?? '');
|
|
11
|
+
}
|
|
6
12
|
/** How many assigned wakes one agent runs at once unless told otherwise. */
|
|
7
13
|
export const DEFAULT_INBOX_CONCURRENCY = 4;
|
|
8
14
|
/**
|
|
@@ -111,6 +117,7 @@ function inboxClient(deps) {
|
|
|
111
117
|
workspaceIds: deps.creds.workspaceIds,
|
|
112
118
|
app: deps.creds.app,
|
|
113
119
|
sessionName: deps.creds.sessionName,
|
|
120
|
+
...(sharedInboxHost(deps.sharedHost) ? { sharedHost: true } : {}),
|
|
114
121
|
});
|
|
115
122
|
}
|
|
116
123
|
async function releaseInboxHostBestEffort(deps) {
|
|
@@ -150,13 +157,13 @@ async function deliverAssignedMessage(deps, ref, reader, attempt, handledIds, tu
|
|
|
150
157
|
const execution = await prepareWorkExecution(ref.action, deps.creds.operatorKey, deps.ownAgentId, deps.baseUrl);
|
|
151
158
|
return withWorkExecution(execution, async () => {
|
|
152
159
|
const row = await reader.get(ref.resourceId);
|
|
153
|
-
//
|
|
154
|
-
const
|
|
155
|
-
|
|
160
|
+
// Every message names its thread; one that does not is refused, not guessed.
|
|
161
|
+
const threadRootId = row.rootMessageId;
|
|
162
|
+
if (typeof threadRootId !== 'string' || !threadRootId)
|
|
163
|
+
throw new Error(`Message ${row.messageId} names no thread root`);
|
|
156
164
|
const metadata = { messageId: row.messageId, ...(row.mandate ?? {}),
|
|
157
165
|
sender: { id: row.sender.ref, type: row.sender.kind }, receiver: { id: deps.ownAgentId, type: 'agent' },
|
|
158
|
-
entryType: 'message', intent: row.intent, timestamp: row.timestamp, roomless: true,
|
|
159
|
-
...(threadRootId ? { threadRootId } : {}) };
|
|
166
|
+
entryType: 'message', intent: row.intent, timestamp: row.timestamp, roomless: true, threadRootId };
|
|
160
167
|
// Hand over in dispatch order; only the read above runs ahead.
|
|
161
168
|
await turn.ready;
|
|
162
169
|
const handover = deps.onMessage(row.content.text, {
|
|
@@ -190,13 +197,20 @@ async function deliverAssignedMessage(deps, ref, reader, attempt, handledIds, tu
|
|
|
190
197
|
export async function pollInboxOnce(deps, waitSeconds = 0, opts = {}) {
|
|
191
198
|
const label = deps.label ?? 'inboxLoop';
|
|
192
199
|
const inbox = inboxClient(deps);
|
|
200
|
+
const shared = sharedInboxHost(deps.sharedHost);
|
|
201
|
+
const free = opts.dispatch
|
|
202
|
+
? Math.max(0, opts.dispatch.limit - opts.dispatch.size)
|
|
203
|
+
: inboxConcurrency(deps.concurrency);
|
|
193
204
|
const envelope = await inbox.getInbox({
|
|
194
205
|
...(waitSeconds > 0 ? { waitSeconds } : {}),
|
|
195
206
|
...(opts.signal ? { signal: opts.signal } : {}),
|
|
207
|
+
...(shared ? { capacity: free } : {}),
|
|
196
208
|
});
|
|
197
209
|
// A response may race shutdown. Do not enqueue another wake after stopping;
|
|
198
210
|
// an already-running pass keeps its lease through its final acknowledgement.
|
|
199
211
|
opts.signal?.throwIfAborted();
|
|
212
|
+
if (envelope.host)
|
|
213
|
+
deps.onLeadership?.(envelope.host.leader);
|
|
200
214
|
let messagesDelivered = 0;
|
|
201
215
|
let resourceEvents = 0;
|
|
202
216
|
let tasksDelivered = 0;
|
|
@@ -400,16 +400,21 @@ async function subcontractAgreement(services, payload, addressId, writeKeys = nu
|
|
|
400
400
|
return services.agreementService.delegate({ ...fields, addressId, idempotencyKey });
|
|
401
401
|
}
|
|
402
402
|
/**
|
|
403
|
-
* A charge statement as the model wrote it, or nothing.
|
|
404
|
-
*
|
|
405
|
-
*
|
|
403
|
+
* A charge statement as the model wrote it, or nothing. A task with no stated
|
|
404
|
+
* charge is free, so a number written as text ("300", "300 points") still
|
|
405
|
+
* counts; a fraction rounds to whole points. A negative or missing amount is
|
|
406
|
+
* no statement.
|
|
406
407
|
*/
|
|
407
408
|
function chargeOf(raw) {
|
|
408
409
|
if (!raw || typeof raw !== 'object')
|
|
409
410
|
return undefined;
|
|
410
411
|
const { amount, basis } = raw;
|
|
411
|
-
const n = typeof amount === '
|
|
412
|
-
|
|
412
|
+
const n = typeof amount === 'number'
|
|
413
|
+
? amount
|
|
414
|
+
: typeof amount === 'string'
|
|
415
|
+
? Number(/-?\d+(?:\.\d+)?/.exec(amount.replace(/,/g, ''))?.[0] ?? NaN)
|
|
416
|
+
: NaN;
|
|
417
|
+
if (!Number.isFinite(n) || n < 0)
|
|
413
418
|
return undefined;
|
|
414
|
-
return { amount: n, ...(typeof basis === 'string' && basis.trim() ? { basis: basis.trim() } : {}) };
|
|
419
|
+
return { amount: Math.round(n), ...(typeof basis === 'string' && basis.trim() ? { basis: basis.trim() } : {}) };
|
|
415
420
|
}
|
|
@@ -83,7 +83,7 @@ export const taskSetResultTool = defineTool('task_set_result', {
|
|
|
83
83
|
},
|
|
84
84
|
charge: {
|
|
85
85
|
type: 'object',
|
|
86
|
-
description: 'Only with state completed, on an agreement priced per task up to a maximum (taskCharge at_completion): { amount, basis }. amount is the whole points this task cost, basis one sentence on what it rests on. It is charged up to the agreed maximum and the rest goes back to the payer
|
|
86
|
+
description: 'Only with state completed, on an agreement priced per task up to a maximum (taskCharge at_completion): { amount, basis }. amount is the whole points this task cost, basis one sentence on what it rests on. It is charged up to the agreed maximum and the rest goes back to the payer. Left out, the task is free, so state it on every completion you charge for. Ignored on a fixed price.',
|
|
87
87
|
},
|
|
88
88
|
sources: SOURCES_PARAM,
|
|
89
89
|
}, handlerFor('task_set_result'), { description: 'Transition a task to its terminal state, completed or failed, and record the result. To cancel a task use task_cancel. Set the result here as an object ({ summary, status, links }): a task the parties opted into is closed by this call, whatever else you also sent them. Pass state (not status). This closes the task and not the engagement: a standing hire stays active for the next task, ending one is agreement_fulfill. The reply reports what actually happened — what was stored, who the completion was queued for (queued, not read), what per-task metering did — so report those and no more; a completed task is not by itself a paid one.' });
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ziggs-ai/agent-sdk",
|
|
3
|
-
"version": "0.45.
|
|
3
|
+
"version": "0.45.3",
|
|
4
4
|
"description": "Agent framework SDK for building autonomous agents on the Ziggs platform",
|
|
5
5
|
"homepage": "https://ziggsai.com/docs",
|
|
6
6
|
"bugs": {
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
"@anthropic-ai/claude-agent-sdk": "^0.3.284",
|
|
31
31
|
"@anthropic-ai/sdk": "^0.110.0",
|
|
32
32
|
"@modelcontextprotocol/sdk": "^1.29.0",
|
|
33
|
-
"@ziggs-ai/api-client": "0.40.
|
|
33
|
+
"@ziggs-ai/api-client": "0.40.3",
|
|
34
34
|
"@ziggs-ai/contracts": "0.46.0",
|
|
35
35
|
"ajv": "^8.17.1",
|
|
36
36
|
"dotenv": "^17.2.3",
|