-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathctx.ts
More file actions
520 lines (486 loc) · 18.1 KB
/
Copy pathctx.ts
File metadata and controls
520 lines (486 loc) · 18.1 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
import { resolveTrajectoryRecording, type PersonaSpec } from '@agentworkforce/persona-kit';
import type {
LlmContext,
MemoryContext,
FilesContext,
CredentialsContext,
MemoryItem,
RelayContext,
RequiredRuntimeCredentials,
ScheduleContext,
SandboxContext,
WorkforceAgentContext,
WorkforceCtx,
WorkforceDeploymentContext,
WorkflowContext
} from './types.js';
import { attachTrajectoryRecorder, createTrajectoryRecorder } from './trajectory.js';
import { buildRelayContext } from './relay.js';
type AgentInputValue = string | number | boolean | null | undefined;
interface AgentRowContext extends WorkforceAgentContext {
/**
* Mirrors agents.input_values. These are agent-level values, not
* per-deployment overrides.
*/
input_values?: Record<string, AgentInputValue>;
/** Camel-case alias for local callers that do not pass a raw DB row. */
inputValues?: Record<string, AgentInputValue>;
}
/**
* Options passed to `buildCtx` when the runner cold-starts. The deploy
* package supplies these from the bundle metadata + environment.
*
* Required subsystems (`sandbox`, `harnessRunner`) must always be
* provided — there is no sensible default for spawning a harness or
* executing inside an isolated filesystem. Optional subsystems
* (`llm`, `memory`, `workflow`, `schedule`, `log`, `integrations`)
* fall back to documented defaults: `memory` becomes a cloud-backed
* adapter when the sandbox env has enough auth, otherwise a no-op (so
* `ctx.memory.save(...)` is safe to call from any handler), the rest
* throw with a single-line "not configured" message that names the
* persona-side flag a caller would set to enable them.
*/
export interface CtxBuildOptions {
persona: PersonaSpec;
workspaceId: string;
agentName?: string;
agent: AgentRowContext;
deployment: WorkforceDeploymentContext;
sandbox: SandboxContext;
files?: FilesContext;
llm?: LlmContext;
memory?: MemoryContext;
workflow?: WorkflowContext;
schedule?: ScheduleContext;
relay?: RelayContext;
integrations?: Record<string, unknown>;
log?: WorkforceCtx['log'];
harnessRunner: WorkforceCtx['harness']['run'];
/**
* Root directory for per-run trajectory contract files. Defaults to
* `env.TRAJECTORY_ROOT`; when neither resolves, trajectory recording is
* disabled (the runtime never writes to the process cwd). The cloud runtime
* sets this to the same value it passes to the injected ai-hist MCP so the
* MCP reads back exactly what the runtime wrote.
*/
trajectoryRoot?: string;
}
const NOOP_MEMORY: MemoryContext = {
async save() {
/* memory disabled (persona.memory unset) — saves silently no-op */
},
async recall() {
return [];
}
};
const DEFAULT_CLOUD_BASE_URL = 'https://agentrelay.com';
const MEMORY_HTTP_TIMEOUT_MS = 15_000;
const UNAVAILABLE_LLM: LlmContext = {
async complete() {
throw new Error(
'ctx.llm is unavailable: set persona.useSubscription:true and connect a provider, or pass a workforce-billed LlmContext to buildCtx.'
);
}
};
const UNAVAILABLE_WORKFLOW: WorkflowContext = {
async run() {
throw new Error(
'ctx.workflow is unavailable: the runner is not connected to the workforce workflows API (workspace token missing).'
);
},
async status() {
throw new Error(
'ctx.workflow is unavailable: the runner is not connected to the workforce workflows API (workspace token missing).'
);
}
};
const UNAVAILABLE_SCHEDULE: ScheduleContext = {
async at() {
throw new Error(
'ctx.schedule.at is unavailable: connect the runner to a scheduler (relaycron or workforce cloud) before scheduling follow-ups.'
);
},
async cancel() {
throw new Error(
'ctx.schedule.cancel is unavailable: connect the runner to a scheduler (relaycron or workforce cloud) before canceling schedules.'
);
}
};
function defaultLog(level: string, message: string, attrs?: Record<string, unknown>): void {
const stream = level === 'error' || level === 'warn' ? process.stderr : process.stdout;
const line = JSON.stringify({ t: new Date().toISOString(), level, message, ...(attrs ?? {}) });
stream.write(`${line}\n`);
}
/**
* Compose a WorkforceCtx from the deploy-supplied subsystems. Subsystems
* left unset fall back to the documented defaults at the top of this
* file — handlers that depend on an unavailable subsystem fail with a
* clear runtime error rather than silently dropping work.
*/
export function buildCtx(options: CtxBuildOptions): WorkforceCtx {
const { agent, deployment } = options;
// Merge per key so a partial `input_values` (DB row) does not eclipse
// keys only present in the camel-case `inputValues` alias. `input_values`
// takes precedence on collisions to match the canonical DB shape.
const mergedAgentInputValues: Record<string, AgentInputValue> = {
...(agent.inputValues ?? {}),
...(agent.input_values ?? {})
};
const log = options.log ?? defaultLog;
const files = options.files ?? filesFromSandbox(options.sandbox);
const agentName = options.agentName ?? options.persona.id;
// Per-persona trajectory recorder (the WHY). Opt-in: no-op unless the
// persona declares `memory.trajectories` AND a trajectory root resolves.
const trajectory = resolveTrajectoryRecording(options.persona.memory);
const trajectoryRecorder = createTrajectoryRecorder({
personaId: options.persona.id,
agentName,
workspaceId: options.workspaceId,
recordTrajectories: trajectory.enabled,
...(trajectory.autoCompact !== undefined ? { autoCompact: trajectory.autoCompact } : {}),
...(options.trajectoryRoot ? { trajectoryRoot: options.trajectoryRoot } : {}),
log
});
const ctx: WorkforceCtx = {
persona: buildPersonaContext(options.persona, mergedAgentInputValues),
agent: {
id: agent.id,
deployedName: agent.deployedName,
spawnedByAgentId: agent.spawnedByAgentId
},
deployment,
workspaceId: options.workspaceId,
agentName,
llm: options.llm ?? UNAVAILABLE_LLM,
harness: { run: options.harnessRunner },
sandbox: options.sandbox,
files,
credentials: credentialsFromEnv(),
memory: options.memory ?? defaultMemoryFor(options.persona.memory, options.workspaceId, log),
workflow: options.workflow ?? UNAVAILABLE_WORKFLOW,
schedule: options.schedule ?? UNAVAILABLE_SCHEDULE,
relay: options.relay ?? buildRelayContext(log),
trajectory: trajectoryRecorder.context,
log
};
// The runner drives the recorder's lifecycle (begin/complete/fail) via this
// non-enumerable handle, keeping those internals off the public ctx surface.
attachTrajectoryRecorder(ctx, trajectoryRecorder);
// Optional per-integration subsystems attach as named ctx fields. The
// cloud-default runtime does not populate this — handlers read provider
// data through the runtime's VFS helpers (listJsonFiles / readJsonFile /
// writeJsonFile) against the provider path conventions. The passthrough
// is kept so external callers can still inject custom clients when they
// build their own ctx.
//
// Reserved fields are guarded so a malformed persona that declares
// an integration named `harness` or `sandbox` cannot clobber core
// ctx subsystems — that would silently turn `ctx.harness.run(...)`
// into a call against an attacker-controlled object.
if (options.integrations) {
for (const [provider, client] of Object.entries(options.integrations)) {
if (CORE_CTX_FIELDS.has(provider)) {
throw new Error(
`runtime: integration provider "${provider}" collides with a core ctx field; rename the integration in your persona JSON`
);
}
Object.assign(ctx, { [provider]: client });
}
}
return ctx;
}
const CORE_CTX_FIELDS: ReadonlySet<string> = new Set([
'persona',
'agent',
'deployment',
'workspaceId',
'agentName',
'llm',
'harness',
'sandbox',
'files',
'credentials',
'memory',
'workflow',
'schedule',
'relay',
'trajectory',
'log'
]);
function filesFromSandbox(sandbox: SandboxContext): FilesContext {
return {
read(path) {
return sandbox.readFile(path);
},
write(path, contents) {
return sandbox.writeFile(path, contents);
}
};
}
function credentialsFromEnv(processEnv: NodeJS.ProcessEnv = process.env): CredentialsContext {
return {
get relayfile() {
return requireRuntimeCredentials(processEnv).relayfile;
},
get cloudApi() {
return requireRuntimeCredentials(processEnv).cloudApi;
},
tryRequire() {
const snapshot = readRuntimeCredentialSnapshot(processEnv);
return snapshot.missing.length > 0 ? null : snapshot.credentials;
},
require() {
return requireRuntimeCredentials(processEnv);
}
};
}
function requireRuntimeCredentials(processEnv: NodeJS.ProcessEnv): RequiredRuntimeCredentials {
const snapshot = readRuntimeCredentialSnapshot(processEnv);
if (snapshot.missing.length > 0) {
throw new Error(`Runtime credentials are required: missing ${snapshot.missing.join(', ')}`);
}
return snapshot.credentials;
}
function readRuntimeCredentialSnapshot(processEnv: NodeJS.ProcessEnv): {
credentials: RequiredRuntimeCredentials;
missing: string[];
} {
const relayfileUrl = normalizeOptionalUrl(firstNonEmpty(processEnv.RELAYFILE_URL));
const relayfileToken = firstNonEmpty(processEnv.RELAYFILE_TOKEN);
const relayfileWorkspaceId = firstNonEmpty(processEnv.RELAYFILE_WORKSPACE_ID);
const cloudApiUrl = normalizeOptionalUrl(firstNonEmpty(processEnv.CLOUD_API_URL));
const cloudApiToken = firstNonEmpty(processEnv.CLOUD_API_ACCESS_TOKEN);
const missing = [
...(!relayfileUrl ? ['relayfile.url'] : []),
...(!relayfileToken ? ['relayfile.token'] : []),
...(!relayfileWorkspaceId ? ['relayfile.workspaceId'] : []),
...(!cloudApiUrl ? ['cloudApi.url'] : []),
...(!cloudApiToken ? ['cloudApi.token'] : [])
];
return {
credentials: {
relayfile: {
url: relayfileUrl ?? '',
token: relayfileToken ?? '',
workspaceId: relayfileWorkspaceId ?? ''
},
cloudApi: {
url: cloudApiUrl ?? '',
token: cloudApiToken ?? ''
}
},
missing
};
}
function normalizeOptionalUrl(value: string | undefined): string | undefined {
return value ? normalizeBaseUrl(value) : undefined;
}
function defaultMemoryFor(
memoryConfig: PersonaSpec['memory'],
workspaceId: string,
log: WorkforceCtx['log'],
processEnv: NodeJS.ProcessEnv = process.env
): MemoryContext {
if (
memoryConfig === false ||
memoryConfig === undefined ||
(typeof memoryConfig === 'object' && memoryConfig !== null && 'enabled' in memoryConfig && memoryConfig.enabled === false)
) {
return NOOP_MEMORY;
}
const cloudBaseUrl = firstNonEmpty(
processEnv.WORKFORCE_CLOUD_URL,
processEnv.WORKFORCE_DEPLOY_CLOUD_URL,
processEnv.AGENTRELAY_CLOUD_URL
) ?? DEFAULT_CLOUD_BASE_URL;
const agentToken = resolveAgentToken(processEnv);
const resolvedWorkspaceId = firstNonEmpty(
processEnv.WORKFORCE_WORKSPACE_ID,
processEnv.RELAY_WORKSPACE_ID,
processEnv.RELAY_DEFAULT_WORKSPACE,
workspaceId
) ?? workspaceId;
if (!agentToken || !resolvedWorkspaceId) return NOOP_MEMORY;
return createCloudMemoryContext({
cloudBaseUrl,
workspaceId: resolvedWorkspaceId,
agentToken,
log
});
}
function createCloudMemoryContext(args: {
cloudBaseUrl: string;
workspaceId: string;
agentToken: string;
log: WorkforceCtx['log'];
}): MemoryContext {
const endpoint = new URL(
`${normalizeBaseUrl(args.cloudBaseUrl)}/api/v1/workspaces/${encodeURIComponent(args.workspaceId)}/memory`
);
return {
async save(content, opts) {
try {
const response = await fetchWithTimeout(endpoint, {
method: 'POST',
headers: {
authorization: `Bearer ${args.agentToken}`,
'content-type': 'application/json'
},
body: JSON.stringify({
scope: opts?.scope ?? 'workspace',
content,
...(opts?.tags ? { tags: opts.tags } : {}),
...memoryTtl(opts)
})
});
if (!response.ok) {
args.log('warn', 'memory.save.failed', { status: response.status });
return undefined;
}
const body = await response.json().catch(() => ({})) as { id?: unknown };
return typeof body.id === 'string' ? { id: body.id } : undefined;
} catch (err) {
args.log('warn', 'memory.save.failed', { error: memoryFetchErrorMessage(err) });
return undefined;
}
},
async recall(query, opts) {
try {
const url = new URL(endpoint);
url.searchParams.set('scope', opts?.scope ?? opts?.scopes?.[0] ?? 'workspace');
url.searchParams.set('query', query);
if (opts?.limit !== undefined) url.searchParams.set('limit', String(opts.limit));
if (opts?.tags?.length) url.searchParams.set('tags', opts.tags.join(','));
const response = await fetchWithTimeout(url, {
headers: { authorization: `Bearer ${args.agentToken}` }
});
if (!response.ok) {
args.log('warn', 'memory.recall.failed', { status: response.status });
return [];
}
const body = await response.json().catch(() => ({})) as { items?: unknown };
return normalizeMemoryItems(body.items);
} catch (err) {
args.log('warn', 'memory.recall.failed', { error: memoryFetchErrorMessage(err) });
return [];
}
}
};
}
async function fetchWithTimeout(input: URL | string, init: RequestInit): Promise<Response> {
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), MEMORY_HTTP_TIMEOUT_MS);
try {
return await fetch(input, { ...init, signal: controller.signal });
} finally {
clearTimeout(timer);
}
}
function memoryFetchErrorMessage(error: unknown): string {
if (isAbortError(error)) return `timeout after ${MEMORY_HTTP_TIMEOUT_MS}ms`;
return error instanceof Error ? error.message : String(error);
}
function isAbortError(error: unknown): boolean {
return error instanceof Error && error.name === 'AbortError';
}
function memoryTtl(opts: Parameters<MemoryContext['save']>[1]): { ttlSeconds?: number } {
if (typeof opts?.ttlSeconds === 'number' && Number.isFinite(opts.ttlSeconds) && opts.ttlSeconds > 0) {
return { ttlSeconds: Math.ceil(opts.ttlSeconds) };
}
if (typeof opts?.expiresInMs === 'number' && Number.isFinite(opts.expiresInMs) && opts.expiresInMs > 0) {
return { ttlSeconds: Math.ceil(opts.expiresInMs / 1000) };
}
return {};
}
function normalizeMemoryItems(value: unknown): MemoryItem[] {
if (!Array.isArray(value)) return [];
return value.flatMap((item) => {
if (typeof item !== 'object' || item === null) return [];
const record = item as Record<string, unknown>;
if (typeof record.id !== 'string' || typeof record.content !== 'string') return [];
const scope = record.scope === 'user' || record.scope === 'global' ? record.scope : 'workspace';
return [{
id: record.id,
content: record.content,
tags: Array.isArray(record.tags) ? record.tags.filter((tag): tag is string => typeof tag === 'string') : [],
scope,
createdAt: typeof record.createdAt === 'string' ? record.createdAt : ''
}];
});
}
function firstNonEmpty(...values: Array<string | undefined>): string | undefined {
for (const value of values) {
const trimmed = value?.trim();
if (trimmed) return trimmed;
}
return undefined;
}
function resolveAgentToken(processEnv: NodeJS.ProcessEnv): string | undefined {
return firstNonEmpty(
processEnv.WORKFORCE_AGENT_TOKEN,
processEnv.RELAY_AGENT_TOKEN,
processEnv.RELAYFILE_TOKEN,
tokenFromAgentTokenMap(processEnv.RELAY_AGENT_TOKENS, processEnv.RELAY_AGENT_NAME),
processEnv.RELAY_API_KEY,
processEnv.WORKFORCE_WORKSPACE_TOKEN
);
}
function tokenFromAgentTokenMap(raw: string | undefined, agentName: string | undefined): string | undefined {
const value = raw?.trim();
if (!value) return undefined;
if (!value.startsWith('{')) return value;
try {
const parsed = JSON.parse(value) as unknown;
if (typeof parsed !== 'object' || parsed === null || Array.isArray(parsed)) return undefined;
const tokens = parsed as Record<string, unknown>;
const namedToken = agentName ? tokens[agentName] : undefined;
if (typeof namedToken === 'string' && namedToken.trim()) return namedToken.trim();
const singleToken = Object.values(tokens).find((entry): entry is string => typeof entry === 'string' && entry.trim().length > 0);
return singleToken?.trim();
} catch {
return undefined;
}
}
function normalizeBaseUrl(value: string): string {
return value.replace(/\/+$/, '');
}
function buildPersonaContext(
persona: PersonaSpec,
agentInputValues: Record<string, AgentInputValue> | undefined,
processEnv: NodeJS.ProcessEnv = process.env
): WorkforceCtx['persona'] {
const inputSpecs = persona.inputs ?? {};
const inputs: Record<string, string> = {};
for (const [key, spec] of Object.entries(inputSpecs)) {
// Mirror the canonical resolution chain used by persona-kit's
// `resolvePersonaInputs`: agent-provided value → process env
// (`spec.env` or the input key) → spec default. Empty strings are
// treated as "missing" so they fall through to later tiers, matching
// persona-kit so the runtime and CLI/plan paths stay in lockstep.
const envName = spec.env ?? key;
const agentValue = stringifyInputValue(agentInputValues?.[key]);
const envValue = stringifyInputValue(processEnv[envName]);
const defaultValue = stringifyInputValue(spec.default);
const resolved = agentValue ?? envValue ?? defaultValue;
if (resolved === undefined) {
if (spec.optional) {
inputs[key] = '';
continue;
}
throw new Error(
`Required input '${key}' has no value (no deployment override, no spec default). Set it via 'workforce deploy --input <key>=<value>' or by editing the agent record.`
);
}
inputs[key] = resolved;
}
return {
...persona,
inputs,
inputSpecs
};
}
function stringifyInputValue(value: AgentInputValue): string | undefined {
if (value === null || value === undefined) return undefined;
const text = String(value);
return text === '' ? undefined : text;
}