-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.ts
More file actions
247 lines (233 loc) · 8.07 KB
/
types.ts
File metadata and controls
247 lines (233 loc) · 8.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import type { EventSeq, JobId, SessionId, TraceId } from "@arcp/core";
import type { BearerVerifier } from "@arcp/core/auth";
import type { BaseEnvelope } from "@arcp/core/envelope";
import type { Logger } from "@arcp/core/logger";
import type {
ArtifactRefBody,
Capabilities,
DelegateBody,
Envelope,
Lease,
LeaseConstraints,
LogPayload,
MetricPayload,
ResultChunkBody,
RuntimeIdentity,
ToolCallBody,
ToolResultBody,
} from "@arcp/core/messages";
import type { EventLog } from "@arcp/core/store";
import type { Job } from "./job.js";
import type { SessionContext } from "./server.js";
/** Inbound-message dispatcher signature. */
export type Handler = (
env: Envelope,
ctx: SessionContext,
) => Promise<void> | void;
/** Sequence-number provider (§8.3), implemented by `SessionContext`. */
export interface EventSeqSource {
/** Increment and return the next session-scoped event_seq. */
nextEventSeq(): EventSeq;
}
/** Send hook the Job uses to flush an outbound envelope. */
export type JobSend = (env: BaseEnvelope) => Promise<void>;
/** Constructor options for `Job`. */
export interface JobOptions {
/** Pre-assigned `job_id` (used on idempotency hits to reuse an existing id). */
jobId?: JobId;
/** Owning session id. Stamped on every outbound envelope. */
sessionId: SessionId;
/** Agent name handling the job. */
agent: string;
/** v1.1 §7.5 — resolved agent version. May be null when no version is registered. */
agentVersion?: string | null;
/** Immutable effective lease (§9.1) — already a subset of the request. */
lease: Lease;
/** v1.1 §9.5 — lease constraints (currently `expires_at`). */
leaseConstraints?: LeaseConstraints | undefined;
/** v1.1 §9.6 — initial per-currency budget counters. */
initialBudget?: ReadonlyMap<string, number> | undefined;
/** Parent job id when this is a delegated child (§10). */
parentJobId?: JobId;
/** Delegate id assigned by the parent in its `delegate` event (§10). */
delegateId?: string;
/** W3C trace id propagated for OTel correlation (§11). */
traceId?: TraceId;
/** Heartbeat watchdog interval. */
heartbeatIntervalSeconds: number;
/** Heartbeats missed before HEARTBEAT_LOST. Default 2. */
missedHeartbeatsAllowed?: number;
}
/**
* v1.1 streamed-result writer (§8.4). Push chunks with `write`; call
* `finalize` to emit the terminating `job.result` payload. The runtime
* generates `result_id`/`chunk_seq` automatically.
*/
export interface ResultStream {
/** Stable identifier for the assembled result. */
readonly resultId: string;
/** Push one chunk. `more: false` is set by `finalize`. */
write(data: string, opts?: { encoding?: "utf8" | "base64" }): Promise<void>;
/**
* Emit the final chunk (if `data` is provided) and the terminating
* `job.result` carrying `result_id`. Returns the assembled byte count.
*/
finalize(
data?: string,
opts?: {
encoding?: "utf8" | "base64";
summary?: string;
resultSize?: number;
},
): Promise<void>;
}
/**
* Context surfaced to agent handlers (§7 / §8).
*
* Exposes one method per reserved event kind (§8.2), plus `emitEvent` for
* `x-vendor.*` kinds. All emit methods stamp the session-scoped `event_seq`
* automatically.
*/
export interface JobContext {
/** Server-assigned job id. */
readonly jobId: JobId;
/** Owning session id. */
readonly sessionId: SessionId;
/** Agent name handling this job (bare name). */
readonly agent: string;
/** v1.1 §7.5 — resolved agent version, or null when unversioned. */
readonly agentVersion: string | null;
/** Wire-form agent reference (`name@version` or bare `name`). */
readonly agentRef: string;
/** Immutable effective lease (§9.1). */
readonly lease: Lease;
/** v1.1 §9.5 — lease constraints (currently `expires_at`). */
readonly leaseConstraints: LeaseConstraints | undefined;
/**
* v1.1 §9.6 — read-only snapshot of remaining per-currency budget. Re-read
* after `cost.*` metrics fire to observe decrements.
*/
readonly budget: ReadonlyMap<string, number>;
/** W3C trace id (§11). */
readonly traceId: TraceId | undefined;
/** Abort signal — fires on `job.cancel` or grace-expired termination. */
readonly signal: AbortSignal;
/** Job-scoped logger pre-bound to `job_id`. */
readonly logger: Logger;
log(
level: LogPayload["level"],
message: string,
attributes?: Record<string, unknown>,
): Promise<void>;
thought(text: string): Promise<void>;
status(phase: string, message?: string): Promise<void>;
metric(metric: MetricPayload): Promise<void>;
toolCall(body: ToolCallBody): Promise<void>;
toolResult(body: ToolResultBody): Promise<void>;
artifactRef(body: ArtifactRefBody): Promise<void>;
delegate(body: DelegateBody): Promise<void>;
progress(
current: number,
opts?: { total?: number; units?: string; message?: string },
): Promise<void>;
resultChunk(body: ResultChunkBody): Promise<void>;
streamResult(opts?: { resultId?: string }): ResultStream;
emitEvent(kind: string, body: unknown): Promise<void>;
}
/**
* The handler signature for agents registered with the runtime. Agents
* receive `input` and a `JobContext`; they return the result value or throw
* an `ARCPError` to signal failure.
*/
export type AgentHandler<Input = unknown, Result = unknown> = (
input: Input,
ctx: JobContext,
) => Promise<Result>;
export type {
ArtifactRefBody,
DelegateBody,
Lease,
LeaseConstraints,
LogPayload,
MetricPayload,
ProgressBody,
ResultChunkBody,
StatusBody,
ThoughtBody,
ToolCallBody,
ToolResultBody,
} from "@arcp/core/messages";
/**
* Optional extra context surfaced to `validateLeaseOp` for v1.1 enforcement:
* lease expiration and per-currency budget counters.
*
* Both `constraints` and `budgetRemaining` are evaluated before the
* glob/pattern check.
*/
export interface LeaseOpContext {
constraints?: LeaseConstraints | undefined;
budgetRemaining?: ReadonlyMap<string, number> | undefined;
/** Clock override for tests; defaults to `Date.now()`. */
now?: number;
}
/**
* v1.1 §6.6 — authorization hook for `session.list_jobs` and
* `job.subscribe`. Returns true if `principal` may observe `job`.
*/
export type JobAuthorizationPolicy = (
job: Job,
principal: string | undefined,
) => boolean;
/**
* Per-session DoS / resource caps (§14).
*
* Defaults: 10_000 buffered events, 16 MiB buffered bytes, 100 concurrent
* jobs. Exceeding any cap closes the session with `INTERNAL_ERROR`
* (non-retryable).
*/
export interface SessionCaps {
/** Max number of outbound envelopes buffered in the event log per session. */
maxBufferedEvents?: number;
/** Max number of outbound envelope bytes buffered per session. */
maxBufferedBytes?: number;
/** Max number of concurrent jobs in a single session. */
maxConcurrentJobs?: number;
}
/** Top-level server options. */
export interface ARCPServerOptions {
/** Identity broadcast in `session.welcome`. */
runtime: RuntimeIdentity;
/** Capabilities advertised by this runtime. */
capabilities: Capabilities;
/** Bearer-token verifier. Required in v1.0. */
bearer?: BearerVerifier;
/** Event log to persist envelopes. Defaults to an in-memory log. */
eventLog?: EventLog;
/** Logger. */
logger?: Logger;
/** Heartbeat watchdog interval. Default 30 s. */
heartbeatIntervalSeconds?: number;
/** Resume buffer window. Default 600 s. */
resumeWindowSeconds?: number;
/** Cancellation grace period before forced termination. Default 30_000 ms. */
cancelGraceMs?: number;
/** Idempotency cache TTL. Default 24 h. */
idempotencyTtlMs?: number;
/** Per-session DoS caps. */
caps?: SessionCaps;
/**
* v1.1 §6.2 — feature flags this runtime advertises. Defaults to every
* v1.1 feature.
*/
features?: readonly string[];
/**
* v1.1 §6.6 — authorization hook for cross-session observation. Defaults
* to same-principal-only.
*/
jobAuthorizationPolicy?: JobAuthorizationPolicy;
/**
* v1.1 §6.5 — threshold (in unacked events) at which the runtime emits a
* `back_pressure` status event. Default 1000.
*/
backPressureThreshold?: number;
}