mesedi 0.1.0
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 +137 -0
- package/dist/anthropic_integration.d.ts +52 -0
- package/dist/anthropic_integration.d.ts.map +1 -0
- package/dist/anthropic_integration.js +226 -0
- package/dist/anthropic_integration.js.map +1 -0
- package/dist/client.d.ts +99 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +258 -0
- package/dist/client.js.map +1 -0
- package/dist/context.d.ts +63 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +84 -0
- package/dist/context.js.map +1 -0
- package/dist/events.d.ts +95 -0
- package/dist/events.d.ts.map +1 -0
- package/dist/events.js +103 -0
- package/dist/events.js.map +1 -0
- package/dist/halt.d.ts +165 -0
- package/dist/halt.d.ts.map +1 -0
- package/dist/halt.js +229 -0
- package/dist/halt.js.map +1 -0
- package/dist/halt_stream.d.ts +77 -0
- package/dist/halt_stream.d.ts.map +1 -0
- package/dist/halt_stream.js +214 -0
- package/dist/halt_stream.js.map +1 -0
- package/dist/index.d.ts +41 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +34 -0
- package/dist/index.js.map +1 -0
- package/dist/integrations/vercel_ai.d.ts +120 -0
- package/dist/integrations/vercel_ai.d.ts.map +1 -0
- package/dist/integrations/vercel_ai.js +381 -0
- package/dist/integrations/vercel_ai.js.map +1 -0
- package/dist/observe.d.ts +50 -0
- package/dist/observe.d.ts.map +1 -0
- package/dist/observe.js +97 -0
- package/dist/observe.js.map +1 -0
- package/dist/tool.d.ts +27 -0
- package/dist/tool.d.ts.map +1 -0
- package/dist/tool.js +138 -0
- package/dist/tool.js.map +1 -0
- package/dist/wrap.d.ts +69 -0
- package/dist/wrap.d.ts.map +1 -0
- package/dist/wrap.js +168 -0
- package/dist/wrap.js.map +1 -0
- package/package.json +70 -0
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SSE halt-stream reader — sub-slice 21e (TS port of 21b.2).
|
|
3
|
+
*
|
|
4
|
+
* When `wrap()` is entered with a `budget` configured, the wrap layer
|
|
5
|
+
* spawns a HaltStreamReader that opens an HTTP stream against
|
|
6
|
+
* `GET /executions/{id}/halt-stream` (the SSE endpoint shipped in
|
|
7
|
+
* sub-slice 21b.1). When the backend pushes an `event: halt` frame,
|
|
8
|
+
* the reader parses the reason and calls `tracker.signalRemoteHalt(reason)`.
|
|
9
|
+
* The next halt-safe boundary check inside the wrapped agent then
|
|
10
|
+
* throws `MesediHalt` with `trigger='remote_signal'`.
|
|
11
|
+
*
|
|
12
|
+
* Implementation notes vs the Python version:
|
|
13
|
+
*
|
|
14
|
+
* - **No thread.** JavaScript is single-threaded; the reader is
|
|
15
|
+
* just an async function returning a Promise that we kick off
|
|
16
|
+
* without awaiting. The fetch's underlying I/O runs on Node's
|
|
17
|
+
* libuv pool — same effective concurrency as a Python daemon
|
|
18
|
+
* thread.
|
|
19
|
+
*
|
|
20
|
+
* - **AbortController for stop().** We can't "interrupt" an async
|
|
21
|
+
* iteration the way Python's `_stop` event lets us bail out of
|
|
22
|
+
* `iter_lines()`. Instead the controller's signal is wired into
|
|
23
|
+
* fetch; calling `.stop()` aborts the underlying HTTP connection,
|
|
24
|
+
* the response stream throws on its next read, and the reader
|
|
25
|
+
* exits via the catch block.
|
|
26
|
+
*
|
|
27
|
+
* - **Native fetch + ReadableStream.** Node 18+ ships fetch in
|
|
28
|
+
* stdlib. We pull bytes off `response.body.getReader()`, decode
|
|
29
|
+
* UTF-8 chunks via TextDecoder, accumulate into a buffer, and
|
|
30
|
+
* split on newlines to get SSE lines. The SSE spec uses `\n` or
|
|
31
|
+
* `\r\n` or `\r` as line separators — we normalize all three.
|
|
32
|
+
*
|
|
33
|
+
* - **Fail-open posture.** If subscription fails (connect error,
|
|
34
|
+
* non-200 response, parse error), the reader logs at debug-level
|
|
35
|
+
* via `console.debug` and exits. The wrapped agent continues
|
|
36
|
+
* running on whatever local budget it has. Remote halt is a
|
|
37
|
+
* nice-to-have on top of local budgets, never a hard requirement.
|
|
38
|
+
*
|
|
39
|
+
* - **One-shot semantics.** After dispatching a halt event, the
|
|
40
|
+
* reader exits. The backend closes the SSE connection at the
|
|
41
|
+
* same moment — both sides agree halt is one signal per
|
|
42
|
+
* subscription.
|
|
43
|
+
*
|
|
44
|
+
* No public API surface: customers don't construct HaltStreamReader.
|
|
45
|
+
* It's used only by wrap.ts.
|
|
46
|
+
*/
|
|
47
|
+
const CONNECT_TIMEOUT_MS = 5000;
|
|
48
|
+
export class HaltStreamReader {
|
|
49
|
+
executionId;
|
|
50
|
+
baseUrl;
|
|
51
|
+
apiKey;
|
|
52
|
+
onHalt;
|
|
53
|
+
schemaVersion;
|
|
54
|
+
controller;
|
|
55
|
+
started = false;
|
|
56
|
+
stopped = false;
|
|
57
|
+
constructor(opts) {
|
|
58
|
+
this.executionId = opts.executionId;
|
|
59
|
+
this.baseUrl = opts.baseUrl.replace(/\/+$/, "");
|
|
60
|
+
this.apiKey = opts.apiKey;
|
|
61
|
+
this.onHalt = opts.onHalt;
|
|
62
|
+
this.schemaVersion = opts.schemaVersion ?? "1";
|
|
63
|
+
this.controller = new AbortController();
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Kick off the async read loop. Idempotent — second call is a no-op.
|
|
67
|
+
* Does NOT await — the wrap layer fires this and continues.
|
|
68
|
+
*/
|
|
69
|
+
start() {
|
|
70
|
+
if (this.started)
|
|
71
|
+
return;
|
|
72
|
+
this.started = true;
|
|
73
|
+
// Intentionally not awaited; the Promise runs in the background.
|
|
74
|
+
// Catch is inside _run() so we never produce an unhandled rejection.
|
|
75
|
+
void this._run();
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Signal the reader to stop and abort any in-flight fetch.
|
|
79
|
+
* Safe to call multiple times.
|
|
80
|
+
*/
|
|
81
|
+
stop() {
|
|
82
|
+
if (this.stopped)
|
|
83
|
+
return;
|
|
84
|
+
this.stopped = true;
|
|
85
|
+
try {
|
|
86
|
+
this.controller.abort();
|
|
87
|
+
}
|
|
88
|
+
catch {
|
|
89
|
+
// ignore — abort can throw on some Node versions if already aborted
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
async _run() {
|
|
93
|
+
const url = `${this.baseUrl}/executions/${encodeURIComponent(this.executionId)}/halt-stream`;
|
|
94
|
+
// Plain Record rather than DOM-lib HeadersInit so the type
|
|
95
|
+
// resolves under Node typings without pulling in the DOM lib.
|
|
96
|
+
// fetch() accepts a plain string-keyed object for headers.
|
|
97
|
+
const headers = {
|
|
98
|
+
Authorization: `Bearer ${this.apiKey}`,
|
|
99
|
+
"X-Mesedi-Schema-Version": this.schemaVersion,
|
|
100
|
+
Accept: "text/event-stream",
|
|
101
|
+
};
|
|
102
|
+
// Connect-timeout is a separate signal so it doesn't kill the
|
|
103
|
+
// long-lived read after connect succeeds. AbortSignal.timeout()
|
|
104
|
+
// is Node 17.3+ / native; AbortSignal.any() is Node 20+ — we
|
|
105
|
+
// compose them manually for broader compatibility (Node 18 LTS).
|
|
106
|
+
const connectTimeout = setTimeout(() => this.controller.abort(), CONNECT_TIMEOUT_MS);
|
|
107
|
+
let resp;
|
|
108
|
+
try {
|
|
109
|
+
resp = await fetch(url, { headers, signal: this.controller.signal });
|
|
110
|
+
clearTimeout(connectTimeout);
|
|
111
|
+
}
|
|
112
|
+
catch (err) {
|
|
113
|
+
clearTimeout(connectTimeout);
|
|
114
|
+
if (!this.stopped) {
|
|
115
|
+
// Connect failed (network error, DNS, timeout). Fail-open.
|
|
116
|
+
console.debug("[mesedi.halt_stream] connect failed (fail-open)", { executionId: this.executionId, error: String(err) });
|
|
117
|
+
}
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
if (resp.status !== 200) {
|
|
121
|
+
console.debug("[mesedi.halt_stream] subscription rejected by backend", { executionId: this.executionId, status: resp.status });
|
|
122
|
+
// Drain the body so the connection can close cleanly.
|
|
123
|
+
try {
|
|
124
|
+
await resp.text();
|
|
125
|
+
}
|
|
126
|
+
catch { /* ignore */ }
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
if (!resp.body) {
|
|
130
|
+
console.debug("[mesedi.halt_stream] response has no body", { executionId: this.executionId });
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
const reader = resp.body.getReader();
|
|
134
|
+
const decoder = new TextDecoder("utf-8");
|
|
135
|
+
let buffer = "";
|
|
136
|
+
let currentEvent = "message";
|
|
137
|
+
let dataLines = [];
|
|
138
|
+
try {
|
|
139
|
+
while (true) {
|
|
140
|
+
const { value, done } = await reader.read();
|
|
141
|
+
if (done)
|
|
142
|
+
break;
|
|
143
|
+
if (this.stopped)
|
|
144
|
+
break;
|
|
145
|
+
buffer += decoder.decode(value, { stream: true });
|
|
146
|
+
// SSE lines are separated by \n, \r\n, or \r. Normalize CRLF
|
|
147
|
+
// to LF, treat remaining \r as line separators, then split
|
|
148
|
+
// on \n.
|
|
149
|
+
buffer = buffer.replace(/\r\n/g, "\n").replace(/\r/g, "\n");
|
|
150
|
+
const lines = buffer.split("\n");
|
|
151
|
+
// Last element is either '' (clean line end) or a partial
|
|
152
|
+
// line that we hold back for the next read.
|
|
153
|
+
buffer = lines.pop() ?? "";
|
|
154
|
+
for (const line of lines) {
|
|
155
|
+
if (this.stopped)
|
|
156
|
+
break;
|
|
157
|
+
if (line === "") {
|
|
158
|
+
// Blank line dispatches the accumulated event.
|
|
159
|
+
if (currentEvent === "halt" && dataLines.length > 0) {
|
|
160
|
+
this._dispatchHalt(dataLines.join("\n"));
|
|
161
|
+
return; // one-shot — done
|
|
162
|
+
}
|
|
163
|
+
currentEvent = "message";
|
|
164
|
+
dataLines = [];
|
|
165
|
+
continue;
|
|
166
|
+
}
|
|
167
|
+
if (line.startsWith(":")) {
|
|
168
|
+
// Comment frame (`: keepalive`) — ignore.
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
if (line.startsWith("event:")) {
|
|
172
|
+
currentEvent = line.slice("event:".length).trim() || "message";
|
|
173
|
+
}
|
|
174
|
+
else if (line.startsWith("data:")) {
|
|
175
|
+
let chunk = line.slice("data:".length);
|
|
176
|
+
if (chunk.startsWith(" "))
|
|
177
|
+
chunk = chunk.slice(1);
|
|
178
|
+
dataLines.push(chunk);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
catch (err) {
|
|
184
|
+
if (!this.stopped) {
|
|
185
|
+
console.debug("[mesedi.halt_stream] reader crashed (fail-open)", { executionId: this.executionId, error: String(err) });
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
finally {
|
|
189
|
+
try {
|
|
190
|
+
reader.releaseLock();
|
|
191
|
+
}
|
|
192
|
+
catch { /* ignore */ }
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
_dispatchHalt(rawData) {
|
|
196
|
+
let reason = "remote halt";
|
|
197
|
+
try {
|
|
198
|
+
const data = JSON.parse(rawData);
|
|
199
|
+
if (data && typeof data === "object" && typeof data.reason === "string") {
|
|
200
|
+
reason = data.reason;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
catch {
|
|
204
|
+
console.debug("[mesedi.halt_stream] data was not JSON, using generic reason", { executionId: this.executionId, raw: rawData.slice(0, 200) });
|
|
205
|
+
}
|
|
206
|
+
try {
|
|
207
|
+
this.onHalt(reason);
|
|
208
|
+
}
|
|
209
|
+
catch (err) {
|
|
210
|
+
console.warn("[mesedi.halt_stream] onHalt callback threw — halt may not have fired", { executionId: this.executionId, error: String(err) });
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
//# sourceMappingURL=halt_stream.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"halt_stream.js","sourceRoot":"","sources":["../src/halt_stream.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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;QACtD,CAAC;IACH,CAAC;IAEO,aAAa,CAAC,OAAe;QACnC,IAAI,MAAM,GAAG,aAAa,CAAC;QAC3B,IAAI,CAAC;YACH,MAAM,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;YACjC,IAAI,IAAI,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,OAAO,IAAI,CAAC,MAAM,KAAK,QAAQ,EAAE,CAAC;gBACxE,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC;YACvB,CAAC;QACH,CAAC;QAAC,MAAM,CAAC;YACP,OAAO,CAAC,KAAK,CACX,8DAA8D,EAC9D,EAAE,WAAW,EAAE,IAAI,CAAC,WAAW,EAAE,GAAG,EAAE,OAAO,CAAC,KAAK,CAAC,CAAC,EAAE,GAAG,CAAC,EAAE,CAC9D,CAAC;QACJ,CAAC;QACD,IAAI,CAAC;YACH,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;QACtB,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,OAAO,CAAC,IAAI,CACV,sEAAsE,EACtE,EAAE,WAAW,EAAE,IAAI,CAAC,WAAW,EAAE,KAAK,EAAE,MAAM,CAAC,GAAG,CAAC,EAAE,CACtD,CAAC;QACJ,CAAC;IACH,CAAC;CACF"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mesedi TypeScript SDK — Guardians for Autonomous AI.
|
|
3
|
+
*
|
|
4
|
+
* Public API:
|
|
5
|
+
*
|
|
6
|
+
* configure(opts) — set up the module-level default client.
|
|
7
|
+
* Reads MESEDI_API_KEY / MESEDI_BASE_URL
|
|
8
|
+
* from process.env as fallbacks.
|
|
9
|
+
* wrap(opts?, fn) — higher-order function that records the
|
|
10
|
+
* wrapped function's invocation as an
|
|
11
|
+
* agent execution.
|
|
12
|
+
* tool(opts?, fn) — higher-order function that records each
|
|
13
|
+
* call as a tool_call event linked to the
|
|
14
|
+
* surrounding wrap()'d execution.
|
|
15
|
+
* checkpoint(name, meta?) — mark a notable point in execution.
|
|
16
|
+
* validatorResult(name, passed, opts?) — report a validator outcome.
|
|
17
|
+
* instrumentAnthropic(cls?)— patch @anthropic-ai/sdk's
|
|
18
|
+
* Messages.create to emit llm_call events.
|
|
19
|
+
* Optional `cls` for testing.
|
|
20
|
+
* flush(timeoutMs?) — wait for the background shipper to
|
|
21
|
+
* drain all events submitted so far.
|
|
22
|
+
* MesediClient — explicit client for advanced use cases.
|
|
23
|
+
* Event, Execution — wire-format dataclasses.
|
|
24
|
+
* EventType, Status — enum-style constants.
|
|
25
|
+
*/
|
|
26
|
+
export { MesediClient, configure, flush, getClient } from "./client.js";
|
|
27
|
+
export type { ConfigureOptions } from "./client.js";
|
|
28
|
+
export { EventType, Status, utcNowRfc3339, } from "./events.js";
|
|
29
|
+
export type { Event, Execution } from "./events.js";
|
|
30
|
+
export { wrap } from "./wrap.js";
|
|
31
|
+
export type { WrapOptions } from "./wrap.js";
|
|
32
|
+
export { tool } from "./tool.js";
|
|
33
|
+
export type { ToolOptions } from "./tool.js";
|
|
34
|
+
export { checkpoint, validatorResult } from "./observe.js";
|
|
35
|
+
export type { ValidatorResultOptions, ValidatorSeverity, } from "./observe.js";
|
|
36
|
+
export { instrumentAnthropic } from "./anthropic_integration.js";
|
|
37
|
+
export type { MessagesClassLike } from "./anthropic_integration.js";
|
|
38
|
+
export { MesediHalt, isMesediHalt } from "./halt.js";
|
|
39
|
+
export type { Budget, HaltTrigger } from "./halt.js";
|
|
40
|
+
export declare const VERSION = "0.0.4";
|
|
41
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAEH,OAAO,EAAE,YAAY,EAAE,SAAS,EAAE,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxE,YAAY,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AACpD,OAAO,EACL,SAAS,EACT,MAAM,EACN,aAAa,GACd,MAAM,aAAa,CAAC;AACrB,YAAY,EAAE,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACpD,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,YAAY,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AAC7C,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,YAAY,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AAC7C,OAAO,EAAE,UAAU,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAC3D,YAAY,EACV,sBAAsB,EACtB,iBAAiB,GAClB,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,mBAAmB,EAAE,MAAM,4BAA4B,CAAC;AACjE,YAAY,EAAE,iBAAiB,EAAE,MAAM,4BAA4B,CAAC;AACpE,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AACrD,YAAY,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AAErD,eAAO,MAAM,OAAO,UAAU,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mesedi TypeScript SDK — Guardians for Autonomous AI.
|
|
3
|
+
*
|
|
4
|
+
* Public API:
|
|
5
|
+
*
|
|
6
|
+
* configure(opts) — set up the module-level default client.
|
|
7
|
+
* Reads MESEDI_API_KEY / MESEDI_BASE_URL
|
|
8
|
+
* from process.env as fallbacks.
|
|
9
|
+
* wrap(opts?, fn) — higher-order function that records the
|
|
10
|
+
* wrapped function's invocation as an
|
|
11
|
+
* agent execution.
|
|
12
|
+
* tool(opts?, fn) — higher-order function that records each
|
|
13
|
+
* call as a tool_call event linked to the
|
|
14
|
+
* surrounding wrap()'d execution.
|
|
15
|
+
* checkpoint(name, meta?) — mark a notable point in execution.
|
|
16
|
+
* validatorResult(name, passed, opts?) — report a validator outcome.
|
|
17
|
+
* instrumentAnthropic(cls?)— patch @anthropic-ai/sdk's
|
|
18
|
+
* Messages.create to emit llm_call events.
|
|
19
|
+
* Optional `cls` for testing.
|
|
20
|
+
* flush(timeoutMs?) — wait for the background shipper to
|
|
21
|
+
* drain all events submitted so far.
|
|
22
|
+
* MesediClient — explicit client for advanced use cases.
|
|
23
|
+
* Event, Execution — wire-format dataclasses.
|
|
24
|
+
* EventType, Status — enum-style constants.
|
|
25
|
+
*/
|
|
26
|
+
export { MesediClient, configure, flush, getClient } from "./client.js";
|
|
27
|
+
export { EventType, Status, utcNowRfc3339, } from "./events.js";
|
|
28
|
+
export { wrap } from "./wrap.js";
|
|
29
|
+
export { tool } from "./tool.js";
|
|
30
|
+
export { checkpoint, validatorResult } from "./observe.js";
|
|
31
|
+
export { instrumentAnthropic } from "./anthropic_integration.js";
|
|
32
|
+
export { MesediHalt, isMesediHalt } from "./halt.js";
|
|
33
|
+
export const VERSION = "0.0.4";
|
|
34
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAEH,OAAO,EAAE,YAAY,EAAE,SAAS,EAAE,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAExE,OAAO,EACL,SAAS,EACT,MAAM,EACN,aAAa,GACd,MAAM,aAAa,CAAC;AAErB,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAEjC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAEjC,OAAO,EAAE,UAAU,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAK3D,OAAO,EAAE,mBAAmB,EAAE,MAAM,4BAA4B,CAAC;AAEjE,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AAGrD,MAAM,CAAC,MAAM,OAAO,GAAG,OAAO,CAAC"}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Vercel AI SDK integration — wrap `generateText` to emit Mesedi
|
|
3
|
+
* telemetry.
|
|
4
|
+
*
|
|
5
|
+
* Usage:
|
|
6
|
+
*
|
|
7
|
+
* import { wrap } from "mesedi";
|
|
8
|
+
* import { wrapGenerateText } from "mesedi/integrations/vercel_ai";
|
|
9
|
+
* import { generateText } from "ai";
|
|
10
|
+
* import { openai } from "@ai-sdk/openai";
|
|
11
|
+
*
|
|
12
|
+
* const generateTextM = wrapGenerateText(generateText);
|
|
13
|
+
*
|
|
14
|
+
* export const runAgent = wrap(
|
|
15
|
+
* { agentName: "support-triage" },
|
|
16
|
+
* async (question: string) => {
|
|
17
|
+
* const result = await generateTextM({
|
|
18
|
+
* model: openai("gpt-4o"),
|
|
19
|
+
* prompt: question,
|
|
20
|
+
* tools: { lookup, search },
|
|
21
|
+
* maxSteps: 5,
|
|
22
|
+
* });
|
|
23
|
+
* return result.text;
|
|
24
|
+
* },
|
|
25
|
+
* );
|
|
26
|
+
*
|
|
27
|
+
* Design:
|
|
28
|
+
*
|
|
29
|
+
* `wrapGenerateText` is a higher-order function that takes the real
|
|
30
|
+
* Vercel `generateText` and returns a drop-in replacement with the
|
|
31
|
+
* same signature plus telemetry side effects. Customers don't have
|
|
32
|
+
* to refactor their agent code — they just swap the import.
|
|
33
|
+
*
|
|
34
|
+
* Multi-step (ReAct) generation is the common case for agent
|
|
35
|
+
* workflows: `generateText({ maxSteps: 5, tools: { ... } })` makes
|
|
36
|
+
* up to 5 LLM calls, with tool calls between each. Vercel exposes
|
|
37
|
+
* the per-step record on `result.steps`. We iterate that array and
|
|
38
|
+
* emit one `llm_call` event per step plus one `tool_call` event per
|
|
39
|
+
* tool invocation within the step. Detectors (drift, identical /
|
|
40
|
+
* similar-call loops, tool-failures, cost-velocity,
|
|
41
|
+
* prompt-injection) see the same wire format as a hand-written
|
|
42
|
+
* Mesedi instrumentation produces.
|
|
43
|
+
*
|
|
44
|
+
* Out of scope for this slice:
|
|
45
|
+
* - `streamText` / `streamObject` — emit-on-stream-end variant
|
|
46
|
+
* would need to drain the stream first or instrument the
|
|
47
|
+
* stream's transform. Deferred to a later slice.
|
|
48
|
+
* - `generateObject` — structured output. The shape is similar to
|
|
49
|
+
* `generateText`'s but the response field is `.object` not
|
|
50
|
+
* `.text`. Will compose well with the same wrapper pattern.
|
|
51
|
+
* - OpenTelemetry-based instrumentation via `experimental_telemetry`.
|
|
52
|
+
* Heavier integration, requires @opentelemetry/api as a peer
|
|
53
|
+
* dependency. Vercel's docs note it's experimental, so we use
|
|
54
|
+
* the wrapper pattern instead for now.
|
|
55
|
+
*/
|
|
56
|
+
/**
|
|
57
|
+
* Loose duck-typed shape for the Vercel `generateText` options. We
|
|
58
|
+
* only read the fields we need; everything else passes through to
|
|
59
|
+
* the underlying function untouched.
|
|
60
|
+
*/
|
|
61
|
+
interface GenerateTextOptionsLike {
|
|
62
|
+
model?: any;
|
|
63
|
+
prompt?: string;
|
|
64
|
+
system?: string;
|
|
65
|
+
messages?: any[];
|
|
66
|
+
[key: string]: unknown;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Loose duck-typed shape for the Vercel `generateText` result. We
|
|
70
|
+
* read .text, .usage, .toolCalls, .toolResults, .steps. All fields
|
|
71
|
+
* are optional — older versions, custom providers, or streaming
|
|
72
|
+
* adapters may omit them.
|
|
73
|
+
*/
|
|
74
|
+
interface GenerateTextResultLike {
|
|
75
|
+
text?: string;
|
|
76
|
+
usage?: {
|
|
77
|
+
promptTokens?: number;
|
|
78
|
+
completionTokens?: number;
|
|
79
|
+
inputTokens?: number;
|
|
80
|
+
outputTokens?: number;
|
|
81
|
+
totalTokens?: number;
|
|
82
|
+
};
|
|
83
|
+
toolCalls?: Array<ToolCallLike>;
|
|
84
|
+
toolResults?: Array<ToolResultLike>;
|
|
85
|
+
steps?: Array<StepLike>;
|
|
86
|
+
finishReason?: string;
|
|
87
|
+
}
|
|
88
|
+
interface ToolCallLike {
|
|
89
|
+
toolCallId?: string;
|
|
90
|
+
toolName?: string;
|
|
91
|
+
args?: any;
|
|
92
|
+
}
|
|
93
|
+
interface ToolResultLike {
|
|
94
|
+
toolCallId?: string;
|
|
95
|
+
toolName?: string;
|
|
96
|
+
result?: any;
|
|
97
|
+
}
|
|
98
|
+
interface StepLike {
|
|
99
|
+
text?: string;
|
|
100
|
+
toolCalls?: Array<ToolCallLike>;
|
|
101
|
+
toolResults?: Array<ToolResultLike>;
|
|
102
|
+
usage?: GenerateTextResultLike["usage"];
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Function signature for the Vercel `generateText`. Loose enough to
|
|
106
|
+
* accommodate the (intentionally-broad) typing the Vercel SDK uses
|
|
107
|
+
* and to allow wrapping test doubles.
|
|
108
|
+
*/
|
|
109
|
+
export type GenerateTextLike = (opts: GenerateTextOptionsLike) => Promise<GenerateTextResultLike>;
|
|
110
|
+
/**
|
|
111
|
+
* Wrap a Vercel `generateText` (or any function with that shape) so
|
|
112
|
+
* each invocation emits Mesedi telemetry. Returns a function with
|
|
113
|
+
* the same signature.
|
|
114
|
+
*
|
|
115
|
+
* Outside a `wrap()` execution, the events silently no-op — same
|
|
116
|
+
* fail-open pattern as every other Mesedi primitive.
|
|
117
|
+
*/
|
|
118
|
+
export declare function wrapGenerateText<F extends GenerateTextLike>(generateTextFn: F): F;
|
|
119
|
+
export {};
|
|
120
|
+
//# sourceMappingURL=vercel_ai.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vercel_ai.d.ts","sourceRoot":"","sources":["../../src/integrations/vercel_ai.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsDG;AAgBH;;;;GAIG;AACH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