@runtypelabs/cloudflare-agents 0.1.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/LICENSE +21 -0
- package/README.md +92 -0
- package/dist/chunk-BuRpWjxB.mjs +42 -0
- package/dist/chunk-DBVUQIKL.cjs +53 -0
- package/dist/durable-object.cjs +892 -0
- package/dist/durable-object.d.cts +114 -0
- package/dist/durable-object.d.ts +114 -0
- package/dist/durable-object.mjs +891 -0
- package/dist/index.cjs +344 -0
- package/dist/index.d.cts +13 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.mjs +339 -0
- package/dist/observer-DnEMHl8z.d.cts +121 -0
- package/dist/observer-DnEMHl8z.d.ts +121 -0
- package/package.json +56 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,344 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_identity = require("./chunk-DBVUQIKL.cjs");
|
|
3
|
+
//#region src/content.ts
|
|
4
|
+
const MAX_CONTENT_BYTES = 32768;
|
|
5
|
+
function diagnose$1(policy, code) {
|
|
6
|
+
try {
|
|
7
|
+
policy.onDiagnostic?.(code);
|
|
8
|
+
} catch {
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
function isEnabled(policy, kind) {
|
|
13
|
+
if (kind === "instructions") return policy.instructions ?? policy.messages ?? false;
|
|
14
|
+
if (kind === "error") return policy.errors ?? false;
|
|
15
|
+
return policy[kind] ?? false;
|
|
16
|
+
}
|
|
17
|
+
/** Applies privacy policy before a content value can enter telemetry storage. */
|
|
18
|
+
function captureContent(policy, kind, value) {
|
|
19
|
+
if (!isEnabled(policy, kind) || value === void 0) return void 0;
|
|
20
|
+
const limit = policy.maxBytes ?? MAX_CONTENT_BYTES;
|
|
21
|
+
if (!Number.isSafeInteger(limit) || limit < 1 || limit > MAX_CONTENT_BYTES) {
|
|
22
|
+
diagnose$1(policy, "content_invalid");
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
let detached;
|
|
26
|
+
try {
|
|
27
|
+
detached = JSON.parse(JSON.stringify(value));
|
|
28
|
+
} catch {
|
|
29
|
+
diagnose$1(policy, "content_invalid");
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
let redacted;
|
|
33
|
+
try {
|
|
34
|
+
redacted = policy.redact ? policy.redact(kind, detached) : detached;
|
|
35
|
+
} catch {
|
|
36
|
+
diagnose$1(policy, "content_redaction_failed");
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
if (redacted === void 0) return void 0;
|
|
40
|
+
try {
|
|
41
|
+
const serialized = JSON.stringify(redacted);
|
|
42
|
+
if (serialized === void 0) {
|
|
43
|
+
diagnose$1(policy, "content_invalid");
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
if (new TextEncoder().encode(serialized).byteLength > limit) {
|
|
47
|
+
diagnose$1(policy, "content_too_large");
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
return JSON.parse(serialized);
|
|
51
|
+
} catch {
|
|
52
|
+
diagnose$1(policy, "content_invalid");
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
//#endregion
|
|
57
|
+
//#region src/observer.ts
|
|
58
|
+
/** Adds bounded observation callbacks without reading or replacing the response stream. */
|
|
59
|
+
async function observeStreamTextOptions(options, observation) {
|
|
60
|
+
const destination = observation.observer;
|
|
61
|
+
let deliveryFailed = false;
|
|
62
|
+
observation = {
|
|
63
|
+
...observation,
|
|
64
|
+
turn: { ...observation.turn },
|
|
65
|
+
observer: {
|
|
66
|
+
onDiagnostic: destination.onDiagnostic,
|
|
67
|
+
async emit(event) {
|
|
68
|
+
const terminal = event.kind === "finished" || event.kind === "aborted" || event.kind === "errored";
|
|
69
|
+
const outgoing = deliveryFailed && terminal ? {
|
|
70
|
+
kind: "incomplete",
|
|
71
|
+
turn: event.turn,
|
|
72
|
+
reason: "observation_delivery_failed"
|
|
73
|
+
} : event;
|
|
74
|
+
try {
|
|
75
|
+
await destination.emit(outgoing);
|
|
76
|
+
} catch (error) {
|
|
77
|
+
deliveryFailed = true;
|
|
78
|
+
throw error;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
await emit(observation.observer, {
|
|
84
|
+
kind: "accepted",
|
|
85
|
+
turn: observation.turn
|
|
86
|
+
});
|
|
87
|
+
if (observation.turn.continuation) await emit(observation.observer, {
|
|
88
|
+
kind: "unsupported",
|
|
89
|
+
turn: observation.turn,
|
|
90
|
+
reason: "continuation"
|
|
91
|
+
});
|
|
92
|
+
const pendingToolCalls = /* @__PURE__ */ new Set();
|
|
93
|
+
const completedServerToolCalls = /* @__PURE__ */ new Set();
|
|
94
|
+
let lastToolError;
|
|
95
|
+
return {
|
|
96
|
+
...options,
|
|
97
|
+
experimental_onStart: options.experimental_onStart,
|
|
98
|
+
experimental_onStepStart: wrap(options.experimental_onStepStart, async (event) => {
|
|
99
|
+
await emitModelInput(observation, event, event.stepNumber);
|
|
100
|
+
}),
|
|
101
|
+
experimental_onToolCallStart: wrap(options.experimental_onToolCallStart, async (event) => {
|
|
102
|
+
await emit(observation.observer, {
|
|
103
|
+
kind: "tool_started",
|
|
104
|
+
turn: observation.turn,
|
|
105
|
+
stepNumber: event.stepNumber,
|
|
106
|
+
...model(event.model) ? { model: model(event.model) } : {},
|
|
107
|
+
toolCall: toolCall(observation, event.toolCall)
|
|
108
|
+
});
|
|
109
|
+
}),
|
|
110
|
+
experimental_onToolCallFinish: wrap(options.experimental_onToolCallFinish, async (event) => {
|
|
111
|
+
const id = toolCallId(event.toolCall);
|
|
112
|
+
if (id) completedServerToolCalls.add(id);
|
|
113
|
+
const output = event.success ? captureContent(observation.content, "tools", event.output) : void 0;
|
|
114
|
+
const error = event.success ? void 0 : captureError(observation.content, event.error);
|
|
115
|
+
if (!event.success) lastToolError = error;
|
|
116
|
+
const outcome = event.success ? output === void 0 ? {} : { output } : error === void 0 ? {} : { error };
|
|
117
|
+
await emit(observation.observer, {
|
|
118
|
+
kind: "tool_finished",
|
|
119
|
+
turn: observation.turn,
|
|
120
|
+
stepNumber: event.stepNumber,
|
|
121
|
+
...model(event.model) ? { model: model(event.model) } : {},
|
|
122
|
+
toolCall: toolCall(observation, event.toolCall),
|
|
123
|
+
durationMs: boundedDuration(event.durationMs),
|
|
124
|
+
success: event.success,
|
|
125
|
+
...outcome
|
|
126
|
+
});
|
|
127
|
+
}),
|
|
128
|
+
onStepFinish: wrap(options.onStepFinish, async (event) => {
|
|
129
|
+
updatePendingToolCalls(pendingToolCalls, event);
|
|
130
|
+
for (const id of completedServerToolCalls) pendingToolCalls.delete(id);
|
|
131
|
+
await emit(observation.observer, modelStep(observation, event));
|
|
132
|
+
}),
|
|
133
|
+
onFinish: wrap(options.onFinish, async (event) => {
|
|
134
|
+
if (observation.turn.continuation) return;
|
|
135
|
+
for (const id of completedServerToolCalls) pendingToolCalls.delete(id);
|
|
136
|
+
const terminalToolError = event.finishReason === "tool-calls" && event.content.some((part) => part.type === "tool-error");
|
|
137
|
+
if (event.finishReason === "error" || terminalToolError) {
|
|
138
|
+
await emit(observation.observer, {
|
|
139
|
+
kind: "errored",
|
|
140
|
+
turn: observation.turn,
|
|
141
|
+
...lastToolError === void 0 ? {} : { error: lastToolError }
|
|
142
|
+
});
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
if (pendingToolCalls.size > 0) {
|
|
146
|
+
await emit(observation.observer, {
|
|
147
|
+
kind: "unsupported",
|
|
148
|
+
turn: observation.turn,
|
|
149
|
+
reason: "unresolved_tool_calls"
|
|
150
|
+
});
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
await emit(observation.observer, {
|
|
154
|
+
kind: "finished",
|
|
155
|
+
turn: observation.turn
|
|
156
|
+
});
|
|
157
|
+
}),
|
|
158
|
+
onAbort: wrap(options.onAbort, async () => {
|
|
159
|
+
await emit(observation.observer, {
|
|
160
|
+
kind: "aborted",
|
|
161
|
+
turn: observation.turn
|
|
162
|
+
});
|
|
163
|
+
}),
|
|
164
|
+
onError: wrap(options.onError, async (event) => {
|
|
165
|
+
const error = captureError(observation.content, event.error);
|
|
166
|
+
await emit(observation.observer, {
|
|
167
|
+
kind: "errored",
|
|
168
|
+
turn: observation.turn,
|
|
169
|
+
...error === void 0 ? {} : { error }
|
|
170
|
+
});
|
|
171
|
+
})
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
function wrap(existing, observe) {
|
|
175
|
+
return async (event) => {
|
|
176
|
+
await existing?.(event);
|
|
177
|
+
try {
|
|
178
|
+
await observe(event);
|
|
179
|
+
} catch {
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
async function emit(observer, event) {
|
|
185
|
+
try {
|
|
186
|
+
await observer.emit(event);
|
|
187
|
+
} catch {
|
|
188
|
+
diagnose(observer, "observer_emit_failed");
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
function diagnose(observer, code) {
|
|
192
|
+
try {
|
|
193
|
+
observer.onDiagnostic?.(code);
|
|
194
|
+
} catch {
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
function emitModelInput(observation, event, stepNumber) {
|
|
199
|
+
const system = captureContent(observation.content, "instructions", event.system);
|
|
200
|
+
const messages = captureMessages(observation.content, event.messages);
|
|
201
|
+
return emit(observation.observer, {
|
|
202
|
+
kind: "model_input",
|
|
203
|
+
turn: observation.turn,
|
|
204
|
+
stepNumber,
|
|
205
|
+
model: model(event.model) ?? {
|
|
206
|
+
provider: "",
|
|
207
|
+
modelId: ""
|
|
208
|
+
},
|
|
209
|
+
...system === void 0 ? {} : { system },
|
|
210
|
+
...messages === void 0 ? {} : { messages }
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
function modelStep(observation, event) {
|
|
214
|
+
const output = captureContent(observation.content, "messages", event.text);
|
|
215
|
+
const toolCalls = toolsEnabled(observation.content) ? captureContent(observation.content, "tools", event.toolCalls) : void 0;
|
|
216
|
+
const toolResults = toolsEnabled(observation.content) ? captureContent(observation.content, "tools", event.toolResults) : void 0;
|
|
217
|
+
const finishReason = boundedString(event.finishReason);
|
|
218
|
+
return {
|
|
219
|
+
kind: "model_step",
|
|
220
|
+
turn: observation.turn,
|
|
221
|
+
stepNumber: event.stepNumber,
|
|
222
|
+
model: model(event.model) ?? {
|
|
223
|
+
provider: "",
|
|
224
|
+
modelId: ""
|
|
225
|
+
},
|
|
226
|
+
...output === void 0 ? {} : { output },
|
|
227
|
+
...toolCalls === void 0 ? {} : { toolCalls },
|
|
228
|
+
...toolResults === void 0 ? {} : { toolResults },
|
|
229
|
+
...finishReason === void 0 ? {} : { finishReason },
|
|
230
|
+
usage: usage(event.usage)
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
function updatePendingToolCalls(pendingToolCalls, event) {
|
|
234
|
+
for (const call of event.toolCalls) {
|
|
235
|
+
const id = toolCallId(call);
|
|
236
|
+
if (id) pendingToolCalls.add(id);
|
|
237
|
+
}
|
|
238
|
+
for (const result of event.toolResults) {
|
|
239
|
+
const id = toolCallId(result);
|
|
240
|
+
if (id) pendingToolCalls.delete(id);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
function toolCallId(value) {
|
|
244
|
+
if (!value || typeof value !== "object") return void 0;
|
|
245
|
+
return boundedString(value.toolCallId);
|
|
246
|
+
}
|
|
247
|
+
function captureMessages(policy, messages) {
|
|
248
|
+
if (!Array.isArray(messages)) return captureContent(policy, "messages", messages);
|
|
249
|
+
const captured = messages.flatMap((message) => {
|
|
250
|
+
const value = captureMessage(policy, message);
|
|
251
|
+
return value === void 0 ? [] : [value];
|
|
252
|
+
});
|
|
253
|
+
return captured.length === 0 ? void 0 : captured;
|
|
254
|
+
}
|
|
255
|
+
function captureMessage(policy, message) {
|
|
256
|
+
const kind = messageKind(message);
|
|
257
|
+
if (kind !== "messages") return captureContent(policy, kind, message);
|
|
258
|
+
if (!message || typeof message !== "object") return captureContent(policy, "messages", message);
|
|
259
|
+
const content = message.content;
|
|
260
|
+
if (!Array.isArray(content)) return captureContent(policy, "messages", message);
|
|
261
|
+
const capturedContent = content.flatMap((part) => {
|
|
262
|
+
const value = captureContent(policy, partKind(part), part);
|
|
263
|
+
return value === void 0 ? [] : [value];
|
|
264
|
+
});
|
|
265
|
+
if (capturedContent.length === 0) return void 0;
|
|
266
|
+
return captureContent(policy, "messages", {
|
|
267
|
+
...message,
|
|
268
|
+
content: capturedContent
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
function messageKind(message) {
|
|
272
|
+
if (!message || typeof message !== "object") return "messages";
|
|
273
|
+
const role = message.role;
|
|
274
|
+
if (role === "system" || role === "developer") return "instructions";
|
|
275
|
+
return role === "tool" ? "tools" : "messages";
|
|
276
|
+
}
|
|
277
|
+
function partKind(part) {
|
|
278
|
+
if (!part || typeof part !== "object") return "messages";
|
|
279
|
+
const type = part.type;
|
|
280
|
+
return typeof type === "string" && type.startsWith("tool-") ? "tools" : "messages";
|
|
281
|
+
}
|
|
282
|
+
function toolsEnabled(policy) {
|
|
283
|
+
return policy.tools === true;
|
|
284
|
+
}
|
|
285
|
+
function toolCall(observation, value) {
|
|
286
|
+
if (!value || typeof value !== "object") {
|
|
287
|
+
diagnose(observation.observer, "invalid_sdk_event");
|
|
288
|
+
return {};
|
|
289
|
+
}
|
|
290
|
+
const call = value;
|
|
291
|
+
const toolCallId = boundedString(call.toolCallId);
|
|
292
|
+
const toolName = boundedString(call.toolName);
|
|
293
|
+
const input = captureContent(observation.content, "tools", call.input);
|
|
294
|
+
return {
|
|
295
|
+
...toolCallId === void 0 ? {} : { toolCallId },
|
|
296
|
+
...toolName === void 0 ? {} : { toolName },
|
|
297
|
+
...input === void 0 ? {} : { input }
|
|
298
|
+
};
|
|
299
|
+
}
|
|
300
|
+
function model(value) {
|
|
301
|
+
if (!value || typeof value !== "object") return void 0;
|
|
302
|
+
const candidate = value;
|
|
303
|
+
const provider = boundedString(candidate.provider);
|
|
304
|
+
const modelId = boundedString(candidate.modelId);
|
|
305
|
+
return provider && modelId ? {
|
|
306
|
+
provider,
|
|
307
|
+
modelId
|
|
308
|
+
} : void 0;
|
|
309
|
+
}
|
|
310
|
+
function captureError(policy, error) {
|
|
311
|
+
if (error instanceof Error) return captureContent(policy, "error", {
|
|
312
|
+
name: error.name,
|
|
313
|
+
message: error.message
|
|
314
|
+
});
|
|
315
|
+
return captureContent(policy, "error", error);
|
|
316
|
+
}
|
|
317
|
+
function boundedString(value) {
|
|
318
|
+
return typeof value === "string" && value.length > 0 && value.length <= 512 ? value : void 0;
|
|
319
|
+
}
|
|
320
|
+
function boundedDuration(value) {
|
|
321
|
+
return typeof value === "number" && Number.isFinite(value) && value >= 0 ? value : 0;
|
|
322
|
+
}
|
|
323
|
+
function usage(value) {
|
|
324
|
+
if (!value || typeof value !== "object") return {};
|
|
325
|
+
return Object.fromEntries(Object.entries(value).filter(([, amount]) => typeof amount === "number" && Number.isFinite(amount) && amount >= 0));
|
|
326
|
+
}
|
|
327
|
+
//#endregion
|
|
328
|
+
//#region src/durable-observer.ts
|
|
329
|
+
/** Bridges a companion Durable Object receipt to the observer's delivery diagnostic. */
|
|
330
|
+
function createDurableObserver(sink, onDiagnostic) {
|
|
331
|
+
return {
|
|
332
|
+
onDiagnostic,
|
|
333
|
+
async emit(observation) {
|
|
334
|
+
if (!(await sink.observe(observation)).accepted) throw new Error("Durable adapter observation was not accepted");
|
|
335
|
+
}
|
|
336
|
+
};
|
|
337
|
+
}
|
|
338
|
+
//#endregion
|
|
339
|
+
exports.captureContent = captureContent;
|
|
340
|
+
exports.createDurableObserver = createDurableObserver;
|
|
341
|
+
exports.createRunIdentity = require_identity.createRunIdentity;
|
|
342
|
+
exports.observeStreamTextOptions = observeStreamTextOptions;
|
|
343
|
+
exports.operationSpanId = require_identity.operationSpanId;
|
|
344
|
+
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { _ as operationSpanId, c as observeStreamTextOptions, d as ContentKind, f as ContentPolicy, g as createRunIdentity, h as SubmissionIdentity, i as AdapterObserver, l as CapturedJson, m as RunIdentity, o as AdapterTurn, p as captureContent, r as AdapterObservation, s as ObserveStreamTextOptions, u as ContentDiagnostic } from "./observer-DnEMHl8z.cjs";
|
|
2
|
+
//#region src/durable-observer.d.ts
|
|
3
|
+
interface DurableObservationSink {
|
|
4
|
+
observe(observation: AdapterObservation): Promise<{
|
|
5
|
+
accepted: boolean;
|
|
6
|
+
diagnostic?: string;
|
|
7
|
+
}>;
|
|
8
|
+
}
|
|
9
|
+
/** Bridges a companion Durable Object receipt to the observer's delivery diagnostic. */
|
|
10
|
+
export declare function createDurableObserver(sink: DurableObservationSink, onDiagnostic?: AdapterObserver['onDiagnostic']): AdapterObserver;
|
|
11
|
+
//#endregion
|
|
12
|
+
export { type AdapterObservation, type AdapterObserver, type AdapterTurn, type CapturedJson, type ContentDiagnostic, type ContentKind, type ContentPolicy, type DurableObservationSink, type ObserveStreamTextOptions, type RunIdentity, type SubmissionIdentity, captureContent, createRunIdentity, observeStreamTextOptions, operationSpanId };
|
|
13
|
+
export {}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { _ as operationSpanId, c as observeStreamTextOptions, d as ContentKind, f as ContentPolicy, g as createRunIdentity, h as SubmissionIdentity, i as AdapterObserver, l as CapturedJson, m as RunIdentity, o as AdapterTurn, p as captureContent, r as AdapterObservation, s as ObserveStreamTextOptions, u as ContentDiagnostic } from "./observer-DnEMHl8z.js";
|
|
2
|
+
//#region src/durable-observer.d.ts
|
|
3
|
+
interface DurableObservationSink {
|
|
4
|
+
observe(observation: AdapterObservation): Promise<{
|
|
5
|
+
accepted: boolean;
|
|
6
|
+
diagnostic?: string;
|
|
7
|
+
}>;
|
|
8
|
+
}
|
|
9
|
+
/** Bridges a companion Durable Object receipt to the observer's delivery diagnostic. */
|
|
10
|
+
export declare function createDurableObserver(sink: DurableObservationSink, onDiagnostic?: AdapterObserver['onDiagnostic']): AdapterObserver;
|
|
11
|
+
//#endregion
|
|
12
|
+
export { type AdapterObservation, type AdapterObserver, type AdapterTurn, type CapturedJson, type ContentDiagnostic, type ContentKind, type ContentPolicy, type DurableObservationSink, type ObserveStreamTextOptions, type RunIdentity, type SubmissionIdentity, captureContent, createRunIdentity, observeStreamTextOptions, operationSpanId };
|
|
13
|
+
export {}
|