@tangle-network/agent-provider-tangle 4.5.7 → 5.0.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 +13 -13
- package/dist/tangle-create-options.d.ts +5 -2
- package/dist/tangle-create-options.js +33 -28
- package/dist/tangle-environment-dispatch.d.ts +2 -1
- package/dist/tangle-environment-dispatch.js +4 -4
- package/dist/tangle-environment-session.d.ts +4 -16
- package/dist/tangle-environment-session.js +285 -307
- package/dist/tangle-environment.d.ts +1 -23
- package/dist/tangle-environment.js +198 -221
- package/dist/tangle-prompt.d.ts +6 -24
- package/dist/tangle-prompt.js +91 -89
- package/dist/tangle-provider.js +13 -11
- package/dist/tangle-proxy-secrets.d.ts +1 -3
- package/dist/tangle-proxy-secrets.js +13 -43
- package/dist/tangle-types.d.ts +6 -12
- package/package.json +4 -4
|
@@ -5,7 +5,7 @@ import { captureModelCredentials, NATIVE_RESUME_CHECKPOINT_METADATA_KEY } from "
|
|
|
5
5
|
import { AgentTurnInputSchema, deepFreeze } from "@tangle-network/agent-interface";
|
|
6
6
|
import { AgentEnvironmentCapabilitiesSchema } from "@tangle-network/agent-interface/environment-provider";
|
|
7
7
|
import { environmentEventFromSandboxEvent } from "./tangle-events.js";
|
|
8
|
-
import { executionIdFromTurnInput, promptFromTurnInput, promptOptionsFromTurnInput, preparePromptOptions, } from "./tangle-prompt.js";
|
|
8
|
+
import { executionIdFromTurnInput, promptFromTurnInput, promptOptionsFromTurnInput, preparePromptOptions, proxySecretReceiptFromSandbox, } from "./tangle-prompt.js";
|
|
9
9
|
import { creationFromSandboxCreateReceipt, placementInfoFromLoopPlacement, statusFromUnknown, } from "./tangle-environment-values.js";
|
|
10
10
|
import { execResultFromSandboxExecResult } from "./tangle-result-values.js";
|
|
11
11
|
import { capabilitiesForSandbox, frozenCapabilityDocument, sandboxCapabilitySupport, } from "./tangle-capabilities.js";
|
|
@@ -25,29 +25,7 @@ import { createTangleInteractiveAgentRegistry } from "./tangle-interactive.js";
|
|
|
25
25
|
import { confidentialVerifierOption, createTangleWorkspaceBranching, } from "./tangle-workspace-branching.js";
|
|
26
26
|
import { bindTangleEvidenceEnvironment, noteTangleSession } from "./tangle-evidence.js";
|
|
27
27
|
import { assertNativeCaptureHarness } from "./tangle-native-capture-proof.js";
|
|
28
|
-
/**
|
|
29
|
-
* Compose one concrete sandbox into an environment.
|
|
30
|
-
*
|
|
31
|
-
* This is the only stage that can read deployment truth, so it does: one
|
|
32
|
-
* `GET /capabilities` against the sandbox decides retained control, and the
|
|
33
|
-
* environment then exposes exactly the operations both the adapter surface
|
|
34
|
-
* and the deployment back. A deployment that cannot disclose a readable
|
|
35
|
-
* document yields no retained-control surface at all. The environment
|
|
36
|
-
* publishes the resulting document on `capabilities`, so a caller reads the
|
|
37
|
-
* answer for this sandbox rather than the provider's pre-sandbox claim.
|
|
38
|
-
*
|
|
39
|
-
* The document is measured once, here. A sandbox that is not yet running
|
|
40
|
-
* cannot answer, so an environment composed during provisioning claims
|
|
41
|
-
* nothing and keeps claiming nothing: the exposed operations and the document
|
|
42
|
-
* are composed together and a caller may already hold either one. This is why
|
|
43
|
-
* `provider.create()` holds until the sandbox is running before it composes
|
|
44
|
-
* (see `tangle-readiness.ts`). `provider.get(id)` composes whatever the
|
|
45
|
-
* sandbox is at that moment, so compose again once a starting sandbox runs.
|
|
46
|
-
*
|
|
47
|
-
* @param request What the create call asked for. An environment rebuilt by id
|
|
48
|
-
* carries none of it, so its observation reports the requested compute shape
|
|
49
|
-
* as absent instead of restating a request it never saw.
|
|
50
|
-
*/
|
|
28
|
+
/** Compose measured sandbox operations; metadata records intent, never grant authority. */
|
|
51
29
|
export async function sandboxInstanceAsEnvironment(box, providerName, client, declaredCapabilities, operation, request) {
|
|
52
30
|
const environmentId = boundedIdentifier(box.id, "Tangle environment id");
|
|
53
31
|
boundedIdentifier(providerName, "Tangle provider name");
|
|
@@ -100,13 +78,130 @@ export async function sandboxInstanceAsEnvironment(box, providerName, client, de
|
|
|
100
78
|
...(request?.requireNativeSessionCapture ? { requireNativeSessionCapture: true } : {}),
|
|
101
79
|
})
|
|
102
80
|
: undefined;
|
|
103
|
-
//
|
|
104
|
-
// offers, so the session surface reads its grant from there.
|
|
81
|
+
// Session operations follow the measured environment document.
|
|
105
82
|
const retainedControl = capabilities.retainedControl !== undefined;
|
|
106
83
|
const interactionResponses = capabilities.interactions !== undefined;
|
|
107
|
-
//
|
|
108
|
-
// this handle and the observation reports the newest record it holds.
|
|
84
|
+
// Observations report the newest usage measured through this handle.
|
|
109
85
|
const usageLog = createExecutionUsageLog();
|
|
86
|
+
const prepareTurn = environmentTurnPreparation(box, providerName, environmentId, request, deployment, nativeReference, credentialHarness, boundCredentialSource, proxySecrets);
|
|
87
|
+
const prepareOptions = (options) => preparePromptOptions(box, options, providerName, request?.proxySecrets ?? options.backend?.profile?.proxySecrets);
|
|
88
|
+
const dispatchRun = capabilities.streaming.detach && box.dispatchPrompt
|
|
89
|
+
? dispatchEnvironmentRun(box, providerName, environmentId, prepareOptions)
|
|
90
|
+
: undefined;
|
|
91
|
+
const dispatch = dispatchRun === undefined ? undefined : async (input) => {
|
|
92
|
+
return dispatchRun(await prepareTurn(input));
|
|
93
|
+
};
|
|
94
|
+
const exactExecutionEvents = (options) => box.streamPrompt("", {
|
|
95
|
+
sessionId: options.sessionId,
|
|
96
|
+
executionId: options.executionId,
|
|
97
|
+
// Exact replay starts at the first event when no cursor was supplied.
|
|
98
|
+
lastEventId: options.since ?? "0",
|
|
99
|
+
...(options.signal ? { signal: options.signal } : {}),
|
|
100
|
+
...(options.controlRef ? { runControlRef: options.controlRef } : {}),
|
|
101
|
+
});
|
|
102
|
+
const creation = creationFromSandboxCreateReceipt(box.createReceipt?.());
|
|
103
|
+
const environment = {
|
|
104
|
+
id: environmentId,
|
|
105
|
+
provider: providerName,
|
|
106
|
+
get proxySecretReceipt() { return proxySecretReceiptFromSandbox(box); },
|
|
107
|
+
...(creation === undefined ? {} : { creation }),
|
|
108
|
+
...(box.name ? { name: boundedString(box.name, "Tangle environment name") } : {}),
|
|
109
|
+
...(box.metadata ? { metadata: snapshotMetadata(box.metadata) } : {}),
|
|
110
|
+
capabilities,
|
|
111
|
+
...(workspaceBranching === undefined
|
|
112
|
+
? {}
|
|
113
|
+
: { workspaceBranching }),
|
|
114
|
+
async status(options) {
|
|
115
|
+
assertOptionKeys(options, ["signal"], "Tangle environment status");
|
|
116
|
+
await awaitWithSignal(box.refresh?.(options?.signal), options?.signal);
|
|
117
|
+
return statusFromUnknown(box.status);
|
|
118
|
+
},
|
|
119
|
+
stream: environmentTurnStream(box, providerName, environmentId, prepareTurn, prepareOptions, (sessionId, executionId) => noteTangleSession(environment, sessionId, executionId), usageLog),
|
|
120
|
+
...(dispatch ? { async dispatch(input) {
|
|
121
|
+
const ref = await dispatch(input);
|
|
122
|
+
noteTangleSession(environment, ref.id, input.executionId ?? input.controlRef?.executionId ?? ref.controlRef?.executionId);
|
|
123
|
+
return ref;
|
|
124
|
+
} } : {}),
|
|
125
|
+
...((capabilities.sessions.continue || capabilities.streaming.replay || capabilities.streaming.detach) &&
|
|
126
|
+
box.session
|
|
127
|
+
? {
|
|
128
|
+
session(id, options) {
|
|
129
|
+
boundedIdentifier(id, "Tangle session id");
|
|
130
|
+
assertOptionKeys(options, ["controlRef", "signal"], "Tangle session");
|
|
131
|
+
options?.signal?.throwIfAborted();
|
|
132
|
+
const session = box.session?.(id, options?.signal ? { signal: options.signal } : undefined);
|
|
133
|
+
if (!session)
|
|
134
|
+
throw new Error("sandbox session(id) returned undefined");
|
|
135
|
+
options?.signal?.throwIfAborted();
|
|
136
|
+
boundedIdentifier(session.id, "Tangle session id");
|
|
137
|
+
if (session.id !== id) {
|
|
138
|
+
throw new Error("sandbox session(id) returned an unrelated session");
|
|
139
|
+
}
|
|
140
|
+
const agentSession = sandboxSessionAsAgentSession(session, options?.controlRef, providerName, environmentId, dispatchRun, exactExecutionEvents, retainedControl, interactionResponses, usageLog, capabilities.nativeContinuation !== undefined, prepareTurn, (executionId) => noteTangleSession(environment, id, executionId), prepareOptions);
|
|
141
|
+
// Every concrete retained session must honor the advertised cancellation.
|
|
142
|
+
if (capabilities.sessions.continue &&
|
|
143
|
+
typeof agentSession.cancelRun !== "function") {
|
|
144
|
+
throw new Error("Tangle retained session support requires SandboxSession.cancelRun");
|
|
145
|
+
}
|
|
146
|
+
return agentSession;
|
|
147
|
+
},
|
|
148
|
+
}
|
|
149
|
+
: {}),
|
|
150
|
+
...(interactionResponses && box.session
|
|
151
|
+
? {
|
|
152
|
+
respondToInteraction(command, options) {
|
|
153
|
+
assertOptionKeys(options, ["signal"], "Tangle interaction response");
|
|
154
|
+
options?.signal?.throwIfAborted();
|
|
155
|
+
// Bind the command to an actual session of this environment.
|
|
156
|
+
const sessionId = boundedIdentifier(command?.binding?.sessionId, "Tangle interaction session id");
|
|
157
|
+
const session = box.session?.(sessionId, options?.signal ? { signal: options.signal } : undefined);
|
|
158
|
+
if (!session)
|
|
159
|
+
throw new Error("sandbox session(id) returned undefined");
|
|
160
|
+
if (session.id !== sessionId) {
|
|
161
|
+
throw new Error("sandbox session(id) returned an unrelated session");
|
|
162
|
+
}
|
|
163
|
+
return tangleInteractionResponder({
|
|
164
|
+
session,
|
|
165
|
+
sessionId,
|
|
166
|
+
environmentId,
|
|
167
|
+
})(command, options);
|
|
168
|
+
},
|
|
169
|
+
}
|
|
170
|
+
: {}),
|
|
171
|
+
...environmentWorkspaceOperations(box, client, capabilities),
|
|
172
|
+
...(capabilities.observation
|
|
173
|
+
? {
|
|
174
|
+
async observe(options) {
|
|
175
|
+
assertOptionKeys(options, ["signal"], "Tangle observe");
|
|
176
|
+
return await observeTangleEnvironment({
|
|
177
|
+
box,
|
|
178
|
+
client,
|
|
179
|
+
provider: providerName,
|
|
180
|
+
environmentId,
|
|
181
|
+
...(request?.resources === undefined
|
|
182
|
+
? {}
|
|
183
|
+
: { requestedResources: request.resources }),
|
|
184
|
+
usageLog,
|
|
185
|
+
}, options);
|
|
186
|
+
},
|
|
187
|
+
}
|
|
188
|
+
: {}),
|
|
189
|
+
...environmentInteractiveOperations(box, capabilities, providerName, environmentId),
|
|
190
|
+
async refresh(options) {
|
|
191
|
+
assertOptionKeys(options, ["signal"], "Tangle refresh");
|
|
192
|
+
options?.signal?.throwIfAborted();
|
|
193
|
+
await awaitWithSignal(box.refresh?.(options?.signal), options?.signal);
|
|
194
|
+
options?.signal?.throwIfAborted();
|
|
195
|
+
},
|
|
196
|
+
...(support.destroy ? { destroy: environmentDestruction(box, request?.ephemeral === true) } : {}),
|
|
197
|
+
};
|
|
198
|
+
bindTangleEvidenceEnvironment(environment, box);
|
|
199
|
+
return environment;
|
|
200
|
+
}
|
|
201
|
+
function snapshotMetadata(metadata) {
|
|
202
|
+
return deepFreeze(structuredClone(metadata));
|
|
203
|
+
}
|
|
204
|
+
function environmentTurnPreparation(box, providerName, environmentId, request, deployment, nativeReference, credentialHarness, boundCredentialSource, proxySecrets) {
|
|
110
205
|
const assertCaptureInput = (input) => {
|
|
111
206
|
const backend = promptOptionsFromTurnInput(input, { provider: providerName, environmentId }).backend;
|
|
112
207
|
if (input.nativeResume !== undefined) {
|
|
@@ -152,7 +247,6 @@ export async function sandboxInstanceAsEnvironment(box, providerName, client, de
|
|
|
152
247
|
if (nativeReference === undefined)
|
|
153
248
|
return input;
|
|
154
249
|
const backend = promptOptionsFromTurnInput(input, { provider: providerName, environmentId }).backend;
|
|
155
|
-
// A plain exact read supplies no backend override and keeps its admitted request unchanged.
|
|
156
250
|
if (replay && backend === undefined)
|
|
157
251
|
return input;
|
|
158
252
|
const harness = backend?.type ?? backend?.profile?.harness ?? credentialHarness;
|
|
@@ -167,10 +261,7 @@ export async function sandboxInstanceAsEnvironment(box, providerName, client, de
|
|
|
167
261
|
}
|
|
168
262
|
assertCliAuthReferenceSupported(harness, { ...model, cliAuth: nativeReference });
|
|
169
263
|
let selectedReference = nativeReference;
|
|
170
|
-
//
|
|
171
|
-
// account-owner binding; retries of that operation present the same key after restart.
|
|
172
|
-
// Native subscription turns can change accounts only when the serving image proves
|
|
173
|
-
// a named grant that preserves this harness's private session HOME.
|
|
264
|
+
// Fresh subscription turns need an owner-bound grant and proven HOME continuity.
|
|
174
265
|
if (!replay && request?.resolveModelCredentials !== undefined &&
|
|
175
266
|
(harness === "claude-code" || harness === "codex")) {
|
|
176
267
|
const supported = harness === "claude-code"
|
|
@@ -222,7 +313,7 @@ export async function sandboxInstanceAsEnvironment(box, providerName, client, de
|
|
|
222
313
|
}
|
|
223
314
|
const selectedModel = { ...model, cliAuth: selectedReference };
|
|
224
315
|
assertCliAuthReferenceSupported(harness, selectedModel);
|
|
225
|
-
//
|
|
316
|
+
// Replay checks substitutions without rewriting admitted identity.
|
|
226
317
|
if (replay)
|
|
227
318
|
return input;
|
|
228
319
|
const { profile: _profile, ...backendOptions } = backend ?? {};
|
|
@@ -230,163 +321,15 @@ export async function sandboxInstanceAsEnvironment(box, providerName, client, de
|
|
|
230
321
|
...input,
|
|
231
322
|
providerOptions: {
|
|
232
323
|
...input.providerOptions,
|
|
233
|
-
//
|
|
234
|
-
// the exact typed profile again for the actual SDK dispatch.
|
|
324
|
+
// Projection retains the exact typed profile for dispatch.
|
|
235
325
|
backend: { ...(input.profile === undefined ? backend : backendOptions), type: harness, model: { ...selectedModel, authMode: "oauth" } },
|
|
236
326
|
},
|
|
237
327
|
};
|
|
238
328
|
};
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
const interactiveAgents = capabilities.interactiveAgent?.start === true &&
|
|
244
|
-
capabilities.interactiveAgent.control === true
|
|
245
|
-
? createTangleInteractiveAgentRegistry(box, providerName, environmentId)
|
|
246
|
-
: undefined;
|
|
247
|
-
const dispatchRun = capabilities.streaming.detach && box.dispatchPrompt
|
|
248
|
-
? dispatchEnvironmentRun(box, providerName, environmentId)
|
|
249
|
-
: undefined;
|
|
250
|
-
const dispatch = dispatchRun === undefined ? undefined : async (input) => {
|
|
251
|
-
return dispatchRun(await prepareTurn(input));
|
|
252
|
-
};
|
|
253
|
-
const exactExecutionEvents = (options) => box.streamPrompt("", {
|
|
254
|
-
sessionId: options.sessionId,
|
|
255
|
-
executionId: options.executionId,
|
|
256
|
-
// The execution replay endpoint owns stable event IDs. A missing cursor
|
|
257
|
-
// starts at the beginning so the caller can journal a complete run.
|
|
258
|
-
lastEventId: options.since ?? "0",
|
|
259
|
-
...(options.signal ? { signal: options.signal } : {}),
|
|
260
|
-
...(options.controlRef ? { runControlRef: options.controlRef } : {}),
|
|
261
|
-
});
|
|
262
|
-
const creation = creationFromSandboxCreateReceipt(box.createReceipt?.());
|
|
263
|
-
const environment = {
|
|
264
|
-
id: environmentId,
|
|
265
|
-
provider: providerName,
|
|
266
|
-
...(creation === undefined ? {} : { creation }),
|
|
267
|
-
...(box.name ? { name: boundedString(box.name, "Tangle environment name") } : {}),
|
|
268
|
-
...(box.metadata ? { metadata: snapshotMetadata(box.metadata) } : {}),
|
|
269
|
-
capabilities,
|
|
270
|
-
...(workspaceBranching === undefined
|
|
271
|
-
? {}
|
|
272
|
-
: { workspaceBranching }),
|
|
273
|
-
async status(options) {
|
|
274
|
-
assertOptionKeys(options, ["signal"], "Tangle environment status");
|
|
275
|
-
await awaitWithSignal(box.refresh?.(options?.signal), options?.signal);
|
|
276
|
-
return statusFromUnknown(box.status);
|
|
277
|
-
},
|
|
278
|
-
async *stream(input) {
|
|
279
|
-
AgentTurnInputSchema.parse(input);
|
|
280
|
-
input.signal?.throwIfAborted();
|
|
281
|
-
input = await prepareTurn(input);
|
|
282
|
-
const expectedExecutionId = executionIdFromTurnInput(input);
|
|
283
|
-
const promptOptions = await preparePromptOptions(box, promptOptionsFromTurnInput(input, { provider: providerName, environmentId }), providerName);
|
|
284
|
-
const expectedSessionId = promptOptions.sessionId;
|
|
285
|
-
if (expectedSessionId)
|
|
286
|
-
noteTangleSession(environment, expectedSessionId, expectedExecutionId);
|
|
287
|
-
// A turn resumed after a cursor starts past its first steps, so it cannot
|
|
288
|
-
// report the execution's running total.
|
|
289
|
-
const stepUsage = input.lastEventId === undefined ? createStepUsageFold() : undefined;
|
|
290
|
-
const iterator = box.streamPrompt(promptFromTurnInput(input), promptOptions)[Symbol.asyncIterator]();
|
|
291
|
-
let completed = false;
|
|
292
|
-
try {
|
|
293
|
-
while (true) {
|
|
294
|
-
const next = await awaitWithSignal(iterator.next(), input.signal);
|
|
295
|
-
if (next.done) {
|
|
296
|
-
completed = true;
|
|
297
|
-
break;
|
|
298
|
-
}
|
|
299
|
-
input.signal?.throwIfAborted();
|
|
300
|
-
if (typeof next.value.data?.runtimeSessionId === "string") {
|
|
301
|
-
noteTangleSession(environment, next.value.data.runtimeSessionId, expectedExecutionId);
|
|
302
|
-
}
|
|
303
|
-
const converted = environmentEventFromSandboxEvent(next.value, {
|
|
304
|
-
executionId: expectedExecutionId,
|
|
305
|
-
sessionId: expectedSessionId,
|
|
306
|
-
...(expectedExecutionId !== undefined || expectedSessionId !== undefined
|
|
307
|
-
? { streamBound: true }
|
|
308
|
-
: {}),
|
|
309
|
-
});
|
|
310
|
-
// The log holds execution receipts, so it records before step usage is attached.
|
|
311
|
-
usageLog.record(expectedExecutionId, converted.usage);
|
|
312
|
-
input.signal?.throwIfAborted();
|
|
313
|
-
yield stepUsage?.observe(converted) ?? converted;
|
|
314
|
-
}
|
|
315
|
-
}
|
|
316
|
-
catch (error) {
|
|
317
|
-
if (input.signal?.aborted &&
|
|
318
|
-
input.detach !== true &&
|
|
319
|
-
expectedSessionId !== undefined &&
|
|
320
|
-
expectedExecutionId !== undefined) {
|
|
321
|
-
void interruptExecutionAfterAbort(box, expectedSessionId, expectedExecutionId);
|
|
322
|
-
}
|
|
323
|
-
throw error;
|
|
324
|
-
}
|
|
325
|
-
finally {
|
|
326
|
-
if (!completed) {
|
|
327
|
-
void Promise.resolve(iterator.return?.()).catch(() => undefined);
|
|
328
|
-
}
|
|
329
|
-
}
|
|
330
|
-
input.signal?.throwIfAborted();
|
|
331
|
-
},
|
|
332
|
-
...(dispatch ? { async dispatch(input) {
|
|
333
|
-
const ref = await dispatch(input);
|
|
334
|
-
noteTangleSession(environment, ref.id, input.executionId ?? input.controlRef?.executionId ?? ref.controlRef?.executionId);
|
|
335
|
-
return ref;
|
|
336
|
-
} } : {}),
|
|
337
|
-
...((capabilities.sessions.continue || capabilities.streaming.replay || capabilities.streaming.detach) &&
|
|
338
|
-
box.session
|
|
339
|
-
? {
|
|
340
|
-
session(id, options) {
|
|
341
|
-
boundedIdentifier(id, "Tangle session id");
|
|
342
|
-
assertOptionKeys(options, ["controlRef", "signal"], "Tangle session");
|
|
343
|
-
options?.signal?.throwIfAborted();
|
|
344
|
-
const session = box.session?.(id, options?.signal ? { signal: options.signal } : undefined);
|
|
345
|
-
if (!session)
|
|
346
|
-
throw new Error("sandbox session(id) returned undefined");
|
|
347
|
-
options?.signal?.throwIfAborted();
|
|
348
|
-
boundedIdentifier(session.id, "Tangle session id");
|
|
349
|
-
if (session.id !== id) {
|
|
350
|
-
throw new Error("sandbox session(id) returned an unrelated session");
|
|
351
|
-
}
|
|
352
|
-
const agentSession = sandboxSessionAsAgentSession(session, options?.controlRef, providerName, environmentId, dispatchRun, exactExecutionEvents, retainedControl, interactionResponses, usageLog, capabilities.nativeContinuation !== undefined, prepareTurn, (executionId) => noteTangleSession(environment, id, executionId), box);
|
|
353
|
-
// sessions.continue was granted from the probe session and the
|
|
354
|
-
// deployment document together; this backstop holds every
|
|
355
|
-
// concrete session to that grant, so a client whose sessions
|
|
356
|
-
// diverge from its probe surface fails loud here instead of
|
|
357
|
-
// failing at the first cancellation.
|
|
358
|
-
if (capabilities.sessions.continue &&
|
|
359
|
-
typeof agentSession.cancelRun !== "function") {
|
|
360
|
-
throw new Error("Tangle retained session support requires SandboxSession.cancelRun");
|
|
361
|
-
}
|
|
362
|
-
return agentSession;
|
|
363
|
-
},
|
|
364
|
-
}
|
|
365
|
-
: {}),
|
|
366
|
-
...(interactionResponses && box.session
|
|
367
|
-
? {
|
|
368
|
-
respondToInteraction(command, options) {
|
|
369
|
-
assertOptionKeys(options, ["signal"], "Tangle interaction response");
|
|
370
|
-
options?.signal?.throwIfAborted();
|
|
371
|
-
// The command names its own session, so the environment answers an
|
|
372
|
-
// ask on any session of this sandbox without the caller holding a
|
|
373
|
-
// session object. The binding is checked against this handle, so a
|
|
374
|
-
// command for another session is refused rather than delivered.
|
|
375
|
-
const sessionId = boundedIdentifier(command?.binding?.sessionId, "Tangle interaction session id");
|
|
376
|
-
const session = box.session?.(sessionId, options?.signal ? { signal: options.signal } : undefined);
|
|
377
|
-
if (!session)
|
|
378
|
-
throw new Error("sandbox session(id) returned undefined");
|
|
379
|
-
if (session.id !== sessionId) {
|
|
380
|
-
throw new Error("sandbox session(id) returned an unrelated session");
|
|
381
|
-
}
|
|
382
|
-
return tangleInteractionResponder({
|
|
383
|
-
session,
|
|
384
|
-
sessionId,
|
|
385
|
-
environmentId,
|
|
386
|
-
})(command, options);
|
|
387
|
-
},
|
|
388
|
-
}
|
|
389
|
-
: {}),
|
|
329
|
+
return prepareTurn;
|
|
330
|
+
}
|
|
331
|
+
function environmentWorkspaceOperations(box, client, capabilities) {
|
|
332
|
+
return {
|
|
390
333
|
...(capabilities.workspace.read && box.read
|
|
391
334
|
? {
|
|
392
335
|
async read(path, options) {
|
|
@@ -442,28 +385,76 @@ export async function sandboxInstanceAsEnvironment(box, providerName, client, de
|
|
|
442
385
|
},
|
|
443
386
|
}
|
|
444
387
|
: {}),
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
388
|
+
};
|
|
389
|
+
}
|
|
390
|
+
function environmentTurnStream(box, providerName, environmentId, prepareTurn, prepareOptions, onSession, usageLog) {
|
|
391
|
+
return async function* (input) {
|
|
392
|
+
AgentTurnInputSchema.parse(input);
|
|
393
|
+
input.signal?.throwIfAborted();
|
|
394
|
+
input = await prepareTurn(input);
|
|
395
|
+
const expectedExecutionId = executionIdFromTurnInput(input);
|
|
396
|
+
const promptOptions = await prepareOptions(promptOptionsFromTurnInput(input, { provider: providerName, environmentId }));
|
|
397
|
+
const expectedSessionId = promptOptions.sessionId;
|
|
398
|
+
if (expectedSessionId)
|
|
399
|
+
onSession(expectedSessionId, expectedExecutionId);
|
|
400
|
+
// A turn resumed after a cursor starts past its first steps, so it cannot
|
|
401
|
+
// report the execution's running total.
|
|
402
|
+
const stepUsage = input.lastEventId === undefined ? createStepUsageFold() : undefined;
|
|
403
|
+
const iterator = box.streamPrompt(promptFromTurnInput(input), promptOptions)[Symbol.asyncIterator]();
|
|
404
|
+
let completed = false;
|
|
405
|
+
try {
|
|
406
|
+
while (true) {
|
|
407
|
+
const next = await awaitWithSignal(iterator.next(), input.signal);
|
|
408
|
+
if (next.done) {
|
|
409
|
+
completed = true;
|
|
410
|
+
break;
|
|
411
|
+
}
|
|
412
|
+
input.signal?.throwIfAborted();
|
|
413
|
+
if (typeof next.value.data?.runtimeSessionId === "string") {
|
|
414
|
+
onSession(next.value.data.runtimeSessionId, expectedExecutionId);
|
|
415
|
+
}
|
|
416
|
+
const converted = environmentEventFromSandboxEvent(next.value, {
|
|
417
|
+
executionId: expectedExecutionId,
|
|
418
|
+
sessionId: expectedSessionId,
|
|
419
|
+
...(expectedExecutionId !== undefined || expectedSessionId !== undefined
|
|
420
|
+
? { streamBound: true }
|
|
421
|
+
: {}),
|
|
422
|
+
});
|
|
423
|
+
// The log holds execution receipts, so it records before step usage is attached.
|
|
424
|
+
usageLog.record(expectedExecutionId, converted.usage);
|
|
425
|
+
input.signal?.throwIfAborted();
|
|
426
|
+
yield stepUsage?.observe(converted) ?? converted;
|
|
460
427
|
}
|
|
461
|
-
|
|
428
|
+
}
|
|
429
|
+
catch (error) {
|
|
430
|
+
if (input.signal?.aborted &&
|
|
431
|
+
input.detach !== true &&
|
|
432
|
+
expectedSessionId !== undefined &&
|
|
433
|
+
expectedExecutionId !== undefined) {
|
|
434
|
+
void interruptExecutionAfterAbort(box, expectedSessionId, expectedExecutionId);
|
|
435
|
+
}
|
|
436
|
+
throw error;
|
|
437
|
+
}
|
|
438
|
+
finally {
|
|
439
|
+
if (!completed) {
|
|
440
|
+
void Promise.resolve(iterator.return?.()).catch(() => undefined);
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
input.signal?.throwIfAborted();
|
|
444
|
+
};
|
|
445
|
+
}
|
|
446
|
+
function environmentInteractiveOperations(box, capabilities, providerName, environmentId) {
|
|
447
|
+
const terminals = capabilities.interactiveTerminal?.attach === true
|
|
448
|
+
? createTangleTerminalRegistry(box)
|
|
449
|
+
: undefined;
|
|
450
|
+
const interactiveAgents = capabilities.interactiveAgent?.start === true &&
|
|
451
|
+
capabilities.interactiveAgent.control === true
|
|
452
|
+
? createTangleInteractiveAgentRegistry(box, providerName, environmentId)
|
|
453
|
+
: undefined;
|
|
454
|
+
return {
|
|
462
455
|
...(terminals
|
|
463
456
|
? {
|
|
464
|
-
|
|
465
|
-
return await terminals.attach(terminalRequest, options);
|
|
466
|
-
},
|
|
457
|
+
attachTerminal: terminals.attach.bind(terminals),
|
|
467
458
|
terminal(terminalSessionId, options) {
|
|
468
459
|
boundedIdentifier(terminalSessionId, "Tangle terminal session id");
|
|
469
460
|
assertOptionKeys(options, ["signal"], "Tangle terminal");
|
|
@@ -474,25 +465,11 @@ export async function sandboxInstanceAsEnvironment(box, providerName, client, de
|
|
|
474
465
|
: {}),
|
|
475
466
|
...(interactiveAgents
|
|
476
467
|
? {
|
|
477
|
-
|
|
478
|
-
return await interactiveAgents.start(interactiveRequest, options);
|
|
479
|
-
},
|
|
468
|
+
startInteractive: interactiveAgents.start.bind(interactiveAgents),
|
|
480
469
|
interactive(ref) {
|
|
481
470
|
return interactiveAgents.get(ref);
|
|
482
471
|
},
|
|
483
472
|
}
|
|
484
473
|
: {}),
|
|
485
|
-
async refresh(options) {
|
|
486
|
-
assertOptionKeys(options, ["signal"], "Tangle refresh");
|
|
487
|
-
options?.signal?.throwIfAborted();
|
|
488
|
-
await awaitWithSignal(box.refresh?.(options?.signal), options?.signal);
|
|
489
|
-
options?.signal?.throwIfAborted();
|
|
490
|
-
},
|
|
491
|
-
...(support.destroy ? { destroy: environmentDestruction(box, request?.ephemeral === true) } : {}),
|
|
492
474
|
};
|
|
493
|
-
bindTangleEvidenceEnvironment(environment, box);
|
|
494
|
-
return environment;
|
|
495
|
-
}
|
|
496
|
-
function snapshotMetadata(metadata) {
|
|
497
|
-
return deepFreeze(structuredClone(metadata));
|
|
498
475
|
}
|
package/dist/tangle-prompt.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { BackendConfig, PromptOptions, PromptResult } from "@tangle-network/sandbox";
|
|
2
2
|
import type { AgentTurnInput, AgentTurnResult } from "@tangle-network/agent-interface/environment-provider";
|
|
3
3
|
import type { AgentExactRunControlRef, InputPart } from "@tangle-network/agent-interface";
|
|
4
|
+
import { type ProxySecretReceipt, type ProxySecretBinding } from "@tangle-network/agent-interface";
|
|
4
5
|
import type { SandboxInstanceLike } from "./tangle-types.js";
|
|
5
6
|
export declare function promptFromTurnInput(input: AgentTurnInput): string | InputPart[];
|
|
6
7
|
export declare function executionIdFromTurnInput(input: AgentTurnInput): string | undefined;
|
|
@@ -9,12 +10,11 @@ export declare function promptOptionsFromTurnInput(input: AgentTurnInput, target
|
|
|
9
10
|
environmentId: string;
|
|
10
11
|
sessionId?: string;
|
|
11
12
|
}): PromptOptions;
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
export declare function proxySecretReceiptFromSandbox(box: SandboxInstanceLike): ProxySecretReceipt | undefined;
|
|
14
|
+
/** Prepare one exact launch; legacy and cursor replay retain their existing path. */
|
|
15
|
+
export declare function preparePromptOptions(box: SandboxInstanceLike, options: PromptOptions, provider: string, bindings?: readonly ProxySecretBinding[]): Promise<PromptOptions>;
|
|
15
16
|
declare const SANDBOX_BACKEND_FIELD_LIST: readonly ["runtimeAttachments", "runtimeSecrets", "type", "profile", "model", "server", "interactions", "metadata"];
|
|
16
17
|
declare const SANDBOX_BACKEND_MODEL_FIELD_LIST: readonly ["provider", "model", "apiKey", "baseUrl", "maxThinkingTokens", "mode", "apiKeyEnv", "cliAuth", "authMode", "authFiles", "routing"];
|
|
17
|
-
/** SDK canonical fields stay exhaustive; legacy aliases are explicitly projected into model. */
|
|
18
18
|
type Exhaustive<T extends never> = T;
|
|
19
19
|
type SandboxBackendModel = NonNullable<BackendConfig["model"]>;
|
|
20
20
|
declare const SANDBOX_AUTH_FILE_FIELD_LIST: readonly ["path", "content", "mode"];
|
|
@@ -22,29 +22,11 @@ type SandboxAuthFile = NonNullable<SandboxBackendModel["authFiles"]>[number];
|
|
|
22
22
|
type FieldDifference<T, Fields extends readonly PropertyKey[]> = Exclude<keyof T, Fields[number]> | Exclude<Fields[number], keyof T>;
|
|
23
23
|
export type SandboxBackendFieldCoverage = Exhaustive<FieldDifference<Omit<BackendConfig, Exclude<(typeof SANDBOX_BACKEND_MODEL_FIELD_LIST)[number], "model"> | "modelId">, typeof SANDBOX_BACKEND_FIELD_LIST> | FieldDifference<SandboxBackendModel, typeof SANDBOX_BACKEND_MODEL_FIELD_LIST> | FieldDifference<SandboxAuthFile, typeof SANDBOX_AUTH_FILE_FIELD_LIST>>;
|
|
24
24
|
type SandboxPromptBackend = NonNullable<PromptOptions["backend"]>;
|
|
25
|
-
/**
|
|
26
|
-
* Read the per-turn backend options a caller sent through `providerOptions`.
|
|
27
|
-
*
|
|
28
|
-
* `AgentTurnInput.providerOptions.backend` is the exact shape agent-runtime
|
|
29
|
-
* emits for a per-turn backend or model override, so refusing it dropped the
|
|
30
|
-
* caller's model, profile, and session credential bundle at the adapter
|
|
31
|
-
* boundary. Every field is checked against what the Sandbox prompt options
|
|
32
|
-
* declare, and the result is bounded JSON the SDK reads once.
|
|
33
|
-
*/
|
|
25
|
+
/** Validate the exact per-turn backend projection. */
|
|
34
26
|
export declare function backendFromTurnProviderOptions(providerOptions: Record<string, unknown> | undefined): SandboxPromptBackend | undefined;
|
|
35
27
|
export declare function normalizeTangleBackend(value: unknown, allowRouting?: boolean): BackendConfig | undefined;
|
|
36
|
-
/**
|
|
37
|
-
* The part of the backend options that decides which work a turn is.
|
|
38
|
-
*
|
|
39
|
-
* Bearer material is deliberately absent. A rotated seat token is the same
|
|
40
|
-
* seat running the same work, so digesting the token bytes would make an
|
|
41
|
-
* ordinary refresh conflict with the run it is continuing. The auth files are
|
|
42
|
-
* reduced to the paths and modes they install, which states that the turn
|
|
43
|
-
* carries a credential bundle and which slots it fills, without binding the
|
|
44
|
-
* identity to the secret inside.
|
|
45
|
-
*/
|
|
28
|
+
/** Request identity excludes bearer bytes; auth files contribute only paths and modes. */
|
|
46
29
|
export declare function backendRequestIdentity(backend: SandboxPromptBackend | undefined): Record<string, unknown> | undefined;
|
|
47
|
-
/** Project the exact turn profile once for both dispatch and retained identity. */
|
|
48
30
|
export declare function backendFromTurnInput(input: AgentTurnInput): SandboxPromptBackend | undefined;
|
|
49
31
|
type SandboxRunStatus = "success" | "failed" | "blocked_on_approval" | "awaiting_question" | "awaiting_interaction" | "awaiting_plan_decision";
|
|
50
32
|
type ValidatedSandboxPromptResult = Record<string, unknown> & {
|