@ziggs-ai/api-client 0.17.0 → 0.17.1
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 +3 -0
- package/dist/http/InboxClient.d.ts +6 -1
- package/dist/http/InboxClient.js +10 -4
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/instanceIdentity.d.ts +37 -10
- package/dist/instanceIdentity.js +130 -24
- package/dist/types.d.ts +8 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -90,6 +90,9 @@ await artifacts.write({
|
|
|
90
90
|
});
|
|
91
91
|
|
|
92
92
|
// Inbox loop: references since last ack (inbox → read → act → ack)
|
|
93
|
+
// One host owns an agent's inbox. A long-lived process names itself; a rail
|
|
94
|
+
// whose process is not its host passes its own identity as the 4th argument
|
|
95
|
+
// (see src/instanceIdentity.ts for what each one sends).
|
|
93
96
|
const inbox = new InboxClient(operatorKey, agentId);
|
|
94
97
|
const env = await inbox.getInbox();
|
|
95
98
|
await inbox.ack([{ kind: 'chat', id: '<chatId>', upTo: env.asOf }]);
|
|
@@ -8,11 +8,16 @@ export declare class InboxClient {
|
|
|
8
8
|
private readonly operatorKey;
|
|
9
9
|
private readonly agentId?;
|
|
10
10
|
private readonly baseUrl;
|
|
11
|
+
private readonly instanceId;
|
|
11
12
|
/**
|
|
12
13
|
* @param operatorKey Agent-scoped or fleet operator key.
|
|
13
14
|
* @param agentId Required for fleet keys (sent as X-Agent-Id). Omit for agent-scoped keys.
|
|
15
|
+
* @param instanceId This host's stable identity, when the caller knows it
|
|
16
|
+
* better than the process does — see {@link hostIdentity}. A rail whose
|
|
17
|
+
* process is not its host (the CLI, one hosted-MCP connection among many)
|
|
18
|
+
* must pass its own; everything long-lived omits it.
|
|
14
19
|
*/
|
|
15
|
-
constructor(operatorKey: string, agentId?: string, baseUrl?: string);
|
|
20
|
+
constructor(operatorKey: string, agentId?: string, baseUrl?: string, instanceId?: string);
|
|
16
21
|
private headers;
|
|
17
22
|
private inboxUrl;
|
|
18
23
|
getInbox(opts?: InboxReadOptions): Promise<InboxEnvelope>;
|
package/dist/http/InboxClient.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { getBackendUrl } from '../utils/urlUtils.js';
|
|
2
2
|
import { pollSurfaceError } from '../shared/rateLimit.js';
|
|
3
|
-
import { INBOX_HOST_CLAIMANT_HEADER,
|
|
3
|
+
import { INBOX_HOST_CLAIMANT_HEADER, hostIdentity, } from '../instanceIdentity.js';
|
|
4
4
|
/**
|
|
5
5
|
* The doorbell, not the door: references addressed to this agent
|
|
6
6
|
* since its last ack — never content. Flow: inbox → read → act → ack
|
|
@@ -10,24 +10,30 @@ export class InboxClient {
|
|
|
10
10
|
operatorKey;
|
|
11
11
|
agentId;
|
|
12
12
|
baseUrl;
|
|
13
|
+
instanceId;
|
|
13
14
|
/**
|
|
14
15
|
* @param operatorKey Agent-scoped or fleet operator key.
|
|
15
16
|
* @param agentId Required for fleet keys (sent as X-Agent-Id). Omit for agent-scoped keys.
|
|
17
|
+
* @param instanceId This host's stable identity, when the caller knows it
|
|
18
|
+
* better than the process does — see {@link hostIdentity}. A rail whose
|
|
19
|
+
* process is not its host (the CLI, one hosted-MCP connection among many)
|
|
20
|
+
* must pass its own; everything long-lived omits it.
|
|
16
21
|
*/
|
|
17
|
-
constructor(operatorKey, agentId, baseUrl) {
|
|
22
|
+
constructor(operatorKey, agentId, baseUrl, instanceId) {
|
|
18
23
|
if (!operatorKey)
|
|
19
24
|
throw new Error('InboxClient: operatorKey is required');
|
|
20
25
|
this.operatorKey = operatorKey;
|
|
21
26
|
this.agentId = agentId;
|
|
22
27
|
this.baseUrl = baseUrl || getBackendUrl();
|
|
28
|
+
this.instanceId = hostIdentity(instanceId);
|
|
23
29
|
}
|
|
24
30
|
headers() {
|
|
25
31
|
const headers = {
|
|
26
32
|
Authorization: `Bearer ${this.operatorKey}`,
|
|
27
33
|
'Content-Type': 'application/json',
|
|
28
|
-
// This
|
|
34
|
+
// This host, not the API. Two fleets of the same agent share
|
|
29
35
|
// one backend task; without this they both wake.
|
|
30
|
-
[INBOX_HOST_CLAIMANT_HEADER]:
|
|
36
|
+
[INBOX_HOST_CLAIMANT_HEADER]: this.instanceId,
|
|
31
37
|
};
|
|
32
38
|
if (this.agentId)
|
|
33
39
|
headers['X-Agent-Id'] = this.agentId;
|
package/dist/index.d.ts
CHANGED
|
@@ -14,4 +14,5 @@ export type { Creds, Task, TaskState, PlanStep, PlanStepStatus, Agreement, Agree
|
|
|
14
14
|
export type { ProposeTerms, ProposeDirectInput, ProposeBroadcastInput, ProposeAgreementData, ClaimOptions, } from './http/AgreementClient.js';
|
|
15
15
|
export { agreementLaneId, agreementIdFromLane, isAgreementLaneId, isAgreementId } from '@ziggs-ai/contracts';
|
|
16
16
|
export * from './shared/operatorKey.js';
|
|
17
|
+
export { INBOX_HOST_CLAIMANT_HEADER, INSTANCE_IDENTITY_MAX_LENGTH, cliHostIdentity, hostIdentity, instanceIdentity, mcpConnectionIdentity, } from './instanceIdentity.js';
|
|
17
18
|
export * from './utils/appUrls.js';
|
package/dist/index.js
CHANGED
|
@@ -15,4 +15,5 @@ export { parseErrorMessage, parseErrorCode, throwApiError, } from './shared/apiE
|
|
|
15
15
|
export { ApiError } from './types.js';
|
|
16
16
|
export { agreementLaneId, agreementIdFromLane, isAgreementLaneId, isAgreementId } from '@ziggs-ai/contracts';
|
|
17
17
|
export * from './shared/operatorKey.js';
|
|
18
|
+
export { INBOX_HOST_CLAIMANT_HEADER, INSTANCE_IDENTITY_MAX_LENGTH, cliHostIdentity, hostIdentity, instanceIdentity, mcpConnectionIdentity, } from './instanceIdentity.js';
|
|
18
19
|
export * from './utils/appUrls.js';
|
|
@@ -1,14 +1,41 @@
|
|
|
1
|
-
/**
|
|
2
|
-
|
|
3
|
-
*
|
|
4
|
-
* The exclusive-read claimant must be the *host* (this fleet task, a
|
|
5
|
-
* laptop fleet, an MCP process), not the API process. Two fleets share
|
|
6
|
-
* one backend task, so stamping the API's identity would let both wake.
|
|
7
|
-
*
|
|
8
|
-
* `ecs:<id>` on Fargate, `local:<host>:<pid>` otherwise. Env-derived
|
|
9
|
-
* and memoized.
|
|
10
|
-
*/
|
|
1
|
+
/** The backend refuses anything longer; it truncates nothing itself. */
|
|
2
|
+
export declare const INSTANCE_IDENTITY_MAX_LENGTH = 64;
|
|
11
3
|
export declare const INBOX_HOST_CLAIMANT_HEADER = "X-Ziggs-Instance";
|
|
4
|
+
/** This process: `ZIGGS_INSTANCE_ID`, else `ecs:<id>`, else `local:<host>:<pid>`. */
|
|
12
5
|
export declare function instanceIdentity(): string;
|
|
13
6
|
/** Test seam: forget the memoized value so a case can set different env. */
|
|
14
7
|
export declare function resetInstanceIdentityForTests(): void;
|
|
8
|
+
/**
|
|
9
|
+
* The identity to send: a rail's own choice when it has one, this process
|
|
10
|
+
* otherwise. Every caller-supplied value goes through the same length rule, so
|
|
11
|
+
* no rail can put the header over the limit the backend enforces.
|
|
12
|
+
*/
|
|
13
|
+
export declare function hostIdentity(preferred?: string): string;
|
|
14
|
+
/**
|
|
15
|
+
* `ziggs-cli`: one identity per machine and user.
|
|
16
|
+
*
|
|
17
|
+
* Every command is its own process, so pid would make `ziggs inbox --ack` a
|
|
18
|
+
* host that never read the inbox — refused, always — and a second
|
|
19
|
+
* `ziggs inbox` inside the lease window a second host. A person at one
|
|
20
|
+
* terminal is one host; consecutive commands share this.
|
|
21
|
+
*/
|
|
22
|
+
export declare function cliHostIdentity(): string;
|
|
23
|
+
/**
|
|
24
|
+
* The hosted MCP server: one identity per CONNECTION, off the key it serves.
|
|
25
|
+
*
|
|
26
|
+
* Not the backend task's identity. That process serves every connected
|
|
27
|
+
* assistant at once, so its own identity makes them one host and changes under
|
|
28
|
+
* all of them on every deploy — a hosted read answering 409 for up to 330
|
|
29
|
+
* seconds after each restart, and replicas refusing each other's agents.
|
|
30
|
+
*
|
|
31
|
+
* The key survives both. A token refresh re-signs the same `keyId`, so an
|
|
32
|
+
* assistant keeps its inbox across a backend restart and across its own
|
|
33
|
+
* refresh, and two assistants holding different keys are two hosts, which is
|
|
34
|
+
* what the ownership law says they are.
|
|
35
|
+
*
|
|
36
|
+
* Undefined when the credential carries no `keyId`. Every operator token the
|
|
37
|
+
* backend mints has one, so this is the shape of a credential that cannot name
|
|
38
|
+
* a stable host at all; the caller falls back to the process default rather
|
|
39
|
+
* than send one value every such connection would share.
|
|
40
|
+
*/
|
|
41
|
+
export declare function mcpConnectionIdentity(operatorKey: string): string | undefined;
|
package/dist/instanceIdentity.js
CHANGED
|
@@ -1,27 +1,97 @@
|
|
|
1
|
+
import { decodeOperatorKeyClaims } from './shared/operatorKey.js';
|
|
1
2
|
/**
|
|
2
|
-
* Which
|
|
3
|
+
* Which HOST is calling `GET /inbox` and `POST /inbox/ack`.
|
|
3
4
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
5
|
+
* `X-Ziggs-Instance` decides inbox ownership: the first host to read acquires
|
|
6
|
+
* an agent's whole inbox, a second host is refused with
|
|
7
|
+
* `409 INBOX_HOST_CONFLICT` until the owner has stopped renewing for 330
|
|
8
|
+
* seconds, and an ack renews an owner but never acquires. A missing or
|
|
9
|
+
* overlong value is `400 INBOX_HOST_REQUIRED`.
|
|
7
10
|
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
11
|
+
* So the value has to be stable for as long as a rail is one logical host, and
|
|
12
|
+
* distinct between rails that are not. What each rail sends:
|
|
13
|
+
*
|
|
14
|
+
* | rail | identity |
|
|
15
|
+
* | -------------------------------------- | --------------------- |
|
|
16
|
+
* | fleet task on Fargate | `ecs:<task id>` |
|
|
17
|
+
* | any other long-lived process, stdio MCP included | `local:<host>:<pid>` |
|
|
18
|
+
* | `ziggs-cli` | `cli:<host>:<user>` |
|
|
19
|
+
* | the hosted MCP server, per connection | `mcp:<keyId>` |
|
|
20
|
+
* | anything a scheduler pins | `ZIGGS_INSTANCE_ID` |
|
|
21
|
+
*
|
|
22
|
+
* A process identity is right for a host that outlives its inbox reads and
|
|
23
|
+
* wrong for one that does not. `ziggs-cli` is a new process per command, so
|
|
24
|
+
* pid would refuse its own `--ack`; a cron that respawns
|
|
25
|
+
* `npx @ziggs-ai/ziggs-mcp` every minute is a new host every minute, which is
|
|
26
|
+
* what `ZIGGS_INSTANCE_ID` is for.
|
|
27
|
+
*
|
|
28
|
+
* `ZIGGS_INSTANCE_ID` beats the process default wherever it is read. It is NOT
|
|
29
|
+
* read on the hosted rail: that one process serves every connected assistant,
|
|
30
|
+
* and a single env var would collapse them back into the one host this exists
|
|
31
|
+
* to separate.
|
|
10
32
|
*/
|
|
11
33
|
let cached = null;
|
|
12
|
-
|
|
34
|
+
/** The backend refuses anything longer; it truncates nothing itself. */
|
|
35
|
+
export const INSTANCE_IDENTITY_MAX_LENGTH = 64;
|
|
13
36
|
export const INBOX_HOST_CLAIMANT_HEADER = 'X-Ziggs-Instance';
|
|
37
|
+
/** This process: `ZIGGS_INSTANCE_ID`, else `ecs:<id>`, else `local:<host>:<pid>`. */
|
|
14
38
|
export function instanceIdentity() {
|
|
15
39
|
if (cached)
|
|
16
40
|
return cached;
|
|
17
|
-
cached = resolve()
|
|
41
|
+
cached = fit(resolve());
|
|
18
42
|
return cached;
|
|
19
43
|
}
|
|
20
44
|
/** Test seam: forget the memoized value so a case can set different env. */
|
|
21
45
|
export function resetInstanceIdentityForTests() {
|
|
22
46
|
cached = null;
|
|
23
47
|
}
|
|
48
|
+
/**
|
|
49
|
+
* The identity to send: a rail's own choice when it has one, this process
|
|
50
|
+
* otherwise. Every caller-supplied value goes through the same length rule, so
|
|
51
|
+
* no rail can put the header over the limit the backend enforces.
|
|
52
|
+
*/
|
|
53
|
+
export function hostIdentity(preferred) {
|
|
54
|
+
const chosen = preferred?.trim();
|
|
55
|
+
return chosen ? fit(chosen) : instanceIdentity();
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* `ziggs-cli`: one identity per machine and user.
|
|
59
|
+
*
|
|
60
|
+
* Every command is its own process, so pid would make `ziggs inbox --ack` a
|
|
61
|
+
* host that never read the inbox — refused, always — and a second
|
|
62
|
+
* `ziggs inbox` inside the lease window a second host. A person at one
|
|
63
|
+
* terminal is one host; consecutive commands share this.
|
|
64
|
+
*/
|
|
65
|
+
export function cliHostIdentity() {
|
|
66
|
+
const pinned = process.env.ZIGGS_INSTANCE_ID?.trim();
|
|
67
|
+
return fit(pinned || `cli:${hostLabel()}:${safeUsername()}`);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* The hosted MCP server: one identity per CONNECTION, off the key it serves.
|
|
71
|
+
*
|
|
72
|
+
* Not the backend task's identity. That process serves every connected
|
|
73
|
+
* assistant at once, so its own identity makes them one host and changes under
|
|
74
|
+
* all of them on every deploy — a hosted read answering 409 for up to 330
|
|
75
|
+
* seconds after each restart, and replicas refusing each other's agents.
|
|
76
|
+
*
|
|
77
|
+
* The key survives both. A token refresh re-signs the same `keyId`, so an
|
|
78
|
+
* assistant keeps its inbox across a backend restart and across its own
|
|
79
|
+
* refresh, and two assistants holding different keys are two hosts, which is
|
|
80
|
+
* what the ownership law says they are.
|
|
81
|
+
*
|
|
82
|
+
* Undefined when the credential carries no `keyId`. Every operator token the
|
|
83
|
+
* backend mints has one, so this is the shape of a credential that cannot name
|
|
84
|
+
* a stable host at all; the caller falls back to the process default rather
|
|
85
|
+
* than send one value every such connection would share.
|
|
86
|
+
*/
|
|
87
|
+
export function mcpConnectionIdentity(operatorKey) {
|
|
88
|
+
const keyId = decodeOperatorKeyClaims(operatorKey)?.keyId?.trim();
|
|
89
|
+
return keyId ? fit(`mcp:${keyId}`) : undefined;
|
|
90
|
+
}
|
|
24
91
|
function resolve() {
|
|
92
|
+
const pinned = process.env.ZIGGS_INSTANCE_ID?.trim();
|
|
93
|
+
if (pinned)
|
|
94
|
+
return pinned;
|
|
25
95
|
const metadataUri = process.env.ECS_CONTAINER_METADATA_URI_V4 ??
|
|
26
96
|
process.env.ECS_CONTAINER_METADATA_URI;
|
|
27
97
|
if (metadataUri) {
|
|
@@ -30,26 +100,62 @@ function resolve() {
|
|
|
30
100
|
if (short.length >= 8)
|
|
31
101
|
return `ecs:${short}`;
|
|
32
102
|
}
|
|
33
|
-
|
|
34
|
-
|
|
103
|
+
return `local:${hostLabel()}:${process.pid}`;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Keep an overlong identity unique.
|
|
107
|
+
*
|
|
108
|
+
* Plain truncation is what the backend stopped doing on purpose: two long
|
|
109
|
+
* values cut to one is exactly the collision the header exists to prevent. A
|
|
110
|
+
* readable head plus a hash of the whole stays inside the limit and stays
|
|
111
|
+
* distinct.
|
|
112
|
+
*/
|
|
113
|
+
function fit(value) {
|
|
114
|
+
if (value.length <= INSTANCE_IDENTITY_MAX_LENGTH)
|
|
115
|
+
return value;
|
|
116
|
+
const head = value.slice(0, INSTANCE_IDENTITY_MAX_LENGTH - 9);
|
|
117
|
+
return `${head}:${fnv1a(value)}`;
|
|
118
|
+
}
|
|
119
|
+
/** FNV-1a/32 in hex. Distinguishes hosts; it is not a checksum for anything. */
|
|
120
|
+
function fnv1a(value) {
|
|
121
|
+
let hash = 0x811c9dc5;
|
|
122
|
+
for (let i = 0; i < value.length; i++) {
|
|
123
|
+
hash ^= value.charCodeAt(i);
|
|
124
|
+
hash = Math.imul(hash, 0x01000193);
|
|
125
|
+
}
|
|
126
|
+
return (hash >>> 0).toString(16).padStart(8, '0');
|
|
127
|
+
}
|
|
128
|
+
function hostLabel() {
|
|
129
|
+
return process.env.HOSTNAME || safeHostname();
|
|
35
130
|
}
|
|
36
131
|
function safeHostname() {
|
|
132
|
+
return builtinOs()?.hostname?.() || 'unknown-host';
|
|
133
|
+
}
|
|
134
|
+
function safeUsername() {
|
|
135
|
+
const fromEnv = process.env.USER || process.env.USERNAME;
|
|
136
|
+
if (fromEnv)
|
|
137
|
+
return fromEnv;
|
|
138
|
+
return (builtinOs()?.userInfo?.()
|
|
139
|
+
?.username || 'unknown-user');
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Reached for at call time, not imported at the top of the file. A static
|
|
143
|
+
* `node:os` specifier makes this module — and through the inbox client, the
|
|
144
|
+
* package entry — unresolvable to a bundler with no node builtins, which is
|
|
145
|
+
* every mobile bundler. Nothing here needs the host name in order to load.
|
|
146
|
+
*
|
|
147
|
+
* `getBuiltinModule` arrived in Node 20.16 / 22.3. On anything older the
|
|
148
|
+
* labels fall back to their `unknown-` forms, which costs a readable name on a
|
|
149
|
+
* dev machine and nothing in a deployed task: those resolve through the
|
|
150
|
+
* container metadata path above.
|
|
151
|
+
*/
|
|
152
|
+
function builtinOs() {
|
|
37
153
|
try {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
// which is every mobile bundler. Nothing here needs the host name in order
|
|
42
|
-
// to load.
|
|
43
|
-
//
|
|
44
|
-
// `getBuiltinModule` arrived in Node 20.16 / 22.3. On anything older the
|
|
45
|
-
// name falls back below, which costs a host label on a dev machine and
|
|
46
|
-
// nothing in a deployed task: those resolve through the container
|
|
47
|
-
// metadata path above.
|
|
48
|
-
const load = process.getBuiltinModule;
|
|
49
|
-
const os = load?.('node:os');
|
|
50
|
-
return os?.hostname?.() || 'unknown-host';
|
|
154
|
+
const load = process
|
|
155
|
+
.getBuiltinModule;
|
|
156
|
+
return load?.('node:os');
|
|
51
157
|
}
|
|
52
158
|
catch {
|
|
53
|
-
return
|
|
159
|
+
return undefined;
|
|
54
160
|
}
|
|
55
161
|
}
|
package/dist/types.d.ts
CHANGED
|
@@ -37,6 +37,14 @@ export interface Creds {
|
|
|
37
37
|
* agent is not in.
|
|
38
38
|
*/
|
|
39
39
|
laneId?: string;
|
|
40
|
+
/**
|
|
41
|
+
* This host's stable identity, sent as `X-Ziggs-Instance` on inbox reads and
|
|
42
|
+
* acks. Set it only on a rail whose PROCESS is not its host — the CLI, one
|
|
43
|
+
* hosted-MCP connection among the many a backend task serves. Everything
|
|
44
|
+
* long-lived leaves it unset and the process names itself. See
|
|
45
|
+
* `src/instanceIdentity.ts` for what each rail sends.
|
|
46
|
+
*/
|
|
47
|
+
instanceId?: string;
|
|
40
48
|
}
|
|
41
49
|
export type TaskState = 'active' | 'proposal' | 'completed' | 'failed' | 'cancelled' | 'ledger_open';
|
|
42
50
|
export type PlanStepStatus = 'pending' | 'in_progress' | 'completed' | 'skipped';
|