@cef-ai/agent-sdk 1.0.0 → 1.2.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/README.md +41 -8
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/runtime/ctx.d.ts +29 -0
- package/dist/runtime/ctx.d.ts.map +1 -0
- package/dist/runtime/ctx.js +142 -0
- package/dist/runtime/ctx.js.map +1 -0
- package/dist/runtime/index.d.ts +17 -0
- package/dist/runtime/index.d.ts.map +1 -0
- package/dist/runtime/index.js +15 -0
- package/dist/runtime/index.js.map +1 -0
- package/dist/runtime/register.d.ts +55 -0
- package/dist/runtime/register.d.ts.map +1 -0
- package/dist/runtime/register.js +139 -0
- package/dist/runtime/register.js.map +1 -0
- package/dist/runtime/types.d.ts +66 -0
- package/dist/runtime/types.d.ts.map +1 -0
- package/dist/runtime/types.js +2 -0
- package/dist/runtime/types.js.map +1 -0
- package/dist/runtime/usage.d.ts +56 -0
- package/dist/runtime/usage.d.ts.map +1 -0
- package/dist/runtime/usage.js +52 -0
- package/dist/runtime/usage.js.map +1 -0
- package/dist/types.d.ts +28 -31
- package/dist/types.d.ts.map +1 -1
- package/package.json +5 -1
package/README.md
CHANGED
|
@@ -38,8 +38,9 @@ interface UserMessage {
|
|
|
38
38
|
|
|
39
39
|
export default class Echo {
|
|
40
40
|
@OnStart
|
|
41
|
-
async onStart(
|
|
42
|
-
ctx
|
|
41
|
+
async onStart() {
|
|
42
|
+
// Logging + HTTP are plain Web globals, not ctx members:
|
|
43
|
+
console.info("started");
|
|
43
44
|
}
|
|
44
45
|
|
|
45
46
|
@OnEvent("user_message")
|
|
@@ -54,12 +55,16 @@ export default class Echo {
|
|
|
54
55
|
}
|
|
55
56
|
|
|
56
57
|
@OnClose
|
|
57
|
-
async onClose(
|
|
58
|
-
|
|
58
|
+
async onClose(_ctx: Context, reason: OnCloseReason) {
|
|
59
|
+
console.info("closing", { reason });
|
|
59
60
|
}
|
|
60
61
|
}
|
|
61
62
|
```
|
|
62
63
|
|
|
64
|
+
`ctx` carries only the CEF-specific platform calls. For plain capabilities,
|
|
65
|
+
use the sandbox globals directly: **`console.*`** for logging (the runtime
|
|
66
|
+
captures it) and **`fetch`** for HTTP.
|
|
67
|
+
|
|
63
68
|
Pair the class with a `cef.config.ts`:
|
|
64
69
|
|
|
65
70
|
```ts
|
|
@@ -80,23 +85,51 @@ export default defineAgent({
|
|
|
80
85
|
| Decorator | `@OnEvent(type)` | Bind an event type (string literal) to a handler. |
|
|
81
86
|
| Decorator | `@OnStart` | Mark the start lifecycle hook. |
|
|
82
87
|
| Decorator | `@OnClose` | Mark the close lifecycle hook. |
|
|
83
|
-
| Type | `Context` |
|
|
88
|
+
| Type | `Context` | CEF-specific platform calls only — `cubby`, `models`, `publish`, `settings`, `close`. (Log/HTTP are the globals `console` / `fetch`.) |
|
|
84
89
|
| Type | `Event<P>` | `{ id, type, ts, from, payload: P }`. |
|
|
85
90
|
| Type | `CubbyHandle` | `query(sql, params)` / `exec(sql, params)` over per-alias storage. |
|
|
86
91
|
| Type | `ModelHandle<I,O>` | `infer(input)` / `stream(input)` per declared model alias. |
|
|
87
|
-
| Type | `Logger` | Leveled structured log: `info` / `warn` / `error` / `debug`. |
|
|
88
92
|
| Type | `OnCloseReason` | `"revoked" \| "idle_timeout" \| "closed_by_agent" \| "failed"`. |
|
|
89
|
-
| Type | `FetchInit` | Subset of `RequestInit` accepted by `ctx.fetch`. |
|
|
90
93
|
| Interface | `KnownEventTypes` | Declaration-merged event-type map populated by typegen. |
|
|
91
94
|
| Function | `defineAgent(config)` | Identity helper that preserves literal types of `AgentConfig`. |
|
|
92
95
|
| Type | `AgentConfig` | Top-level config: `id`, `version`, `entry`, `cubbies`, `models`, `settings`, `fetch`, `publishes`, `lifecycle`. |
|
|
93
96
|
|
|
97
|
+
## How it runs (runtime model)
|
|
98
|
+
|
|
99
|
+
Per Agent Runtime ADR-001, the Cere Network runtime is a **CEF-agnostic
|
|
100
|
+
bundle executor**: it exposes only Web-platform globals (`fetch`, `console`,
|
|
101
|
+
`URL`, `crypto`, `TextEncoder`, timers, …) plus one opaque
|
|
102
|
+
`globalThis.context` object, and dispatches invocations as
|
|
103
|
+
`globalThis.__handlers[name](event, context)`.
|
|
104
|
+
|
|
105
|
+
`ctx` is **CEF-specific add-ons only** — SDK code compiled into your bundle by
|
|
106
|
+
`cef build`, not a runtime-provided binding:
|
|
107
|
+
|
|
108
|
+
- `ctx.publish` / `ctx.cubby` / `ctx.models[…].infer` / `ctx.close` → `fetch`
|
|
109
|
+
calls to the endpoints the orchestrator places in `globalThis.context`
|
|
110
|
+
(`context.endpoints.*`, with `context.auth` headers).
|
|
111
|
+
- `ctx.settings` → `context.settings`.
|
|
112
|
+
|
|
113
|
+
Plain Web-platform capabilities are **not** mirrored on `ctx` — use the
|
|
114
|
+
sandbox globals directly: `console.*` for logging (captured out-of-band by
|
|
115
|
+
the runtime) and `fetch` for HTTP.
|
|
116
|
+
|
|
117
|
+
You never write the shim code — you author the decorated class, and the
|
|
118
|
+
shims (the `@cef-ai/agent-sdk/runtime` subpath) are bundled in automatically.
|
|
119
|
+
|
|
120
|
+
**Inference usage is reported automatically.** Each `ctx.models[…].infer`
|
|
121
|
+
call captures the gateway's usage and the SDK folds a `_modelCalls` array
|
|
122
|
+
into your handler's return value; the orchestrator reads it to record
|
|
123
|
+
per-model token/GPU usage (ADR-004). `_modelCalls` is a reserved result key —
|
|
124
|
+
don't set it yourself.
|
|
125
|
+
|
|
94
126
|
## Subpath exports
|
|
95
127
|
|
|
96
128
|
| Specifier | Purpose |
|
|
97
129
|
| --- | --- |
|
|
98
|
-
| `@cef-ai/agent-sdk` | Decorators, `Context`, `Event`, `KnownEventTypes`, `
|
|
130
|
+
| `@cef-ai/agent-sdk` | Decorators, `Context`, `Event`, `KnownEventTypes`, `ModelHandle`, `CubbyHandle`, `OnCloseReason`. |
|
|
99
131
|
| `@cef-ai/agent-sdk/config` | `defineAgent` and the `AgentConfig` shape — used in `cef.config.ts`. |
|
|
132
|
+
| `@cef-ai/agent-sdk/runtime` | Build-internal: the `ctx` shims + `registerHandlers` that `cef build` compiles into the bundle. Authors never import this directly. |
|
|
100
133
|
|
|
101
134
|
For testing, use `@cef-ai/testing` directly — `import { testAgent } from "@cef-ai/testing"`.
|
|
102
135
|
|
package/dist/index.d.ts
CHANGED
|
@@ -2,6 +2,6 @@ export { OnEvent, OnStart, OnClose } from "./decorators.js";
|
|
|
2
2
|
export { Engagement } from "./engagement.js";
|
|
3
3
|
export type { EngagementOptions } from "./engagement.js";
|
|
4
4
|
export { ENGAGEMENT_METADATA, type EngagementMeta } from "./symbols.js";
|
|
5
|
-
export type { Context, CubbyHandle, Event,
|
|
5
|
+
export type { Context, CubbyHandle, Event, KnownEventTypes, KnownModels, ModelHandle, OnCloseReason, } from "./types.js";
|
|
6
6
|
export type { AgentCard, CubbyDecl, JSONSchema, Rule, SettingDecl, Targeting, TargetingEngagement, } from "./config/define-agent.js";
|
|
7
7
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAC5D,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,YAAY,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AACzD,OAAO,EAAE,mBAAmB,EAAE,KAAK,cAAc,EAAE,MAAM,cAAc,CAAC;AACxE,YAAY,EACV,OAAO,EACP,WAAW,EACX,KAAK,EACL,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAC5D,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,YAAY,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AACzD,OAAO,EAAE,mBAAmB,EAAE,KAAK,cAAc,EAAE,MAAM,cAAc,CAAC;AACxE,YAAY,EACV,OAAO,EACP,WAAW,EACX,KAAK,EACL,eAAe,EACf,WAAW,EACX,WAAW,EACX,aAAa,GACd,MAAM,YAAY,CAAC;AAIpB,YAAY,EACV,SAAS,EACT,SAAS,EACT,UAAU,EACV,IAAI,EACJ,WAAW,EACX,SAAS,EACT,mBAAmB,GACpB,MAAM,0BAA0B,CAAC"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `buildContext` — assembles the developer-facing `ctx` object from the
|
|
3
|
+
* opaque `globalThis.context` the Agent Runtime injects.
|
|
4
|
+
*
|
|
5
|
+
* Architecture (AR ADR-001): AR exposes only Web-platform globals
|
|
6
|
+
* (`fetch`, `console`, `URL`, `crypto`, …) plus one opaque `context`
|
|
7
|
+
* object. `ctx` carries ONLY the CEF-specific platform add-ons
|
|
8
|
+
* (`ctx.cubby` / `.publish` / `.models` / `.close` / `.settings`) — each a
|
|
9
|
+
* JS shim compiled into the bundle by `cef build` that reads endpoints +
|
|
10
|
+
* auth from `context` and calls back over `fetch`. Plain Web capabilities
|
|
11
|
+
* are NOT mirrored on `ctx`: agents log with `console.*` (the runtime
|
|
12
|
+
* captures it) and make HTTP calls with the sandbox `fetch` directly.
|
|
13
|
+
*
|
|
14
|
+
* Endpoint URLs and auth come from {@link InvokeContext} (written by the
|
|
15
|
+
* orchestrator's `buildInvokeContext`). A blank/absent endpoint disables
|
|
16
|
+
* its feature: the corresponding shim throws a clear error rather than
|
|
17
|
+
* calling an empty URL.
|
|
18
|
+
*/
|
|
19
|
+
import type { Context } from "../types.js";
|
|
20
|
+
import type { InvokeContext, ModelRef } from "./types.js";
|
|
21
|
+
import { UsageAccumulator } from "./usage.js";
|
|
22
|
+
/**
|
|
23
|
+
* Build the `ctx` object for one invocation. `raw` is whatever AR exposed
|
|
24
|
+
* as `globalThis.context`; `usage` collects inference calls for the
|
|
25
|
+
* `_modelCalls` fold (see {@link mergeModelCalls}); `modelRefs` is the
|
|
26
|
+
* `alias → ModelRef` map `cef build` baked into the bundle.
|
|
27
|
+
*/
|
|
28
|
+
export declare function buildContext(raw: InvokeContext, usage: UsageAccumulator, modelRefs?: Record<string, ModelRef>): Context;
|
|
29
|
+
//# sourceMappingURL=ctx.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ctx.d.ts","sourceRoot":"","sources":["../../src/runtime/ctx.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AACH,OAAO,KAAK,EAAE,OAAO,EAA4B,MAAM,aAAa,CAAC;AACrE,OAAO,KAAK,EAAE,aAAa,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAC1D,OAAO,EAAkB,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAwI9D;;;;;GAKG;AACH,wBAAgB,YAAY,CAC1B,GAAG,EAAE,aAAa,EAClB,KAAK,EAAE,gBAAgB,EACvB,SAAS,GAAE,MAAM,CAAC,MAAM,EAAE,QAAQ,CAAM,GACvC,OAAO,CAkDT"}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
const JSON_HEADERS = { "content-type": "application/json" };
|
|
2
|
+
function authHeaders(ctx) {
|
|
3
|
+
const h = ctx.auth?.headers ?? {};
|
|
4
|
+
const token = ctx.auth?.token;
|
|
5
|
+
return token ? { ...h, authorization: `Bearer ${token}` } : { ...h };
|
|
6
|
+
}
|
|
7
|
+
/** Map a columnar cubby query response into an array of row objects. */
|
|
8
|
+
function zipRows(columns, rows) {
|
|
9
|
+
if (!columns || !rows)
|
|
10
|
+
return [];
|
|
11
|
+
return rows.map((row) => {
|
|
12
|
+
const obj = {};
|
|
13
|
+
columns.forEach((col, i) => {
|
|
14
|
+
obj[col] = row[i];
|
|
15
|
+
});
|
|
16
|
+
return obj;
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
async function postJSON(url, body, headers, label) {
|
|
20
|
+
const res = await fetch(url, {
|
|
21
|
+
method: "POST",
|
|
22
|
+
headers: { ...JSON_HEADERS, ...headers },
|
|
23
|
+
body: JSON.stringify(body),
|
|
24
|
+
});
|
|
25
|
+
if (!res.ok) {
|
|
26
|
+
const text = await res.text().catch(() => "");
|
|
27
|
+
throw new Error(`[@cef-ai/agent-sdk] ${label} failed: HTTP ${res.status} ${text}`.trim());
|
|
28
|
+
}
|
|
29
|
+
const ct = res.headers.get("content-type") ?? "";
|
|
30
|
+
return ct.includes("application/json") ? res.json() : res.text();
|
|
31
|
+
}
|
|
32
|
+
function buildCubby(ctx, headers, alias) {
|
|
33
|
+
const base = ctx.endpoints?.orchestrator;
|
|
34
|
+
const { vaultId, scope, agentId } = ctx.correlation ?? {};
|
|
35
|
+
// Endpoint absence is reported at call time (async rejection) rather than
|
|
36
|
+
// when the handle is created, matching the other shims.
|
|
37
|
+
if (!base) {
|
|
38
|
+
const fail = async () => {
|
|
39
|
+
throw new Error(`[@cef-ai/agent-sdk] ctx.cubby("${alias}"): no orchestrator endpoint in context`);
|
|
40
|
+
};
|
|
41
|
+
return { query: fail, exec: fail };
|
|
42
|
+
}
|
|
43
|
+
const root = `${base}/api/v1/vaults/${vaultId ?? ""}/scopes/${scope ?? ""}` +
|
|
44
|
+
`/agents/${agentId ?? ""}/cubbies/${encodeURIComponent(alias)}`;
|
|
45
|
+
return {
|
|
46
|
+
async query(sql, params = []) {
|
|
47
|
+
const data = (await postJSON(`${root}/query`, { sql, params }, headers, `ctx.cubby("${alias}").query`));
|
|
48
|
+
return zipRows(data.columns, data.rows);
|
|
49
|
+
},
|
|
50
|
+
async exec(sql, params = []) {
|
|
51
|
+
const data = (await postJSON(`${root}/exec`, { sql, params }, headers, `ctx.cubby("${alias}").exec`));
|
|
52
|
+
return {
|
|
53
|
+
changes: data.rowsAffected ?? 0,
|
|
54
|
+
lastInsertRowid: data.lastInsertId ?? 0,
|
|
55
|
+
};
|
|
56
|
+
},
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
function buildModelHandle(ctx, headers, usage, alias, ref) {
|
|
60
|
+
// The IG native endpoint is orchestrator-supplied; the model identity is
|
|
61
|
+
// baked into the bundle by `cef build` (the alias → ModelRef map).
|
|
62
|
+
const url = ctx.endpoints?.inference;
|
|
63
|
+
async function infer(input) {
|
|
64
|
+
if (!url) {
|
|
65
|
+
throw new Error(`[@cef-ai/agent-sdk] ctx.models.${alias}: no inference endpoint in context`);
|
|
66
|
+
}
|
|
67
|
+
if (!ref) {
|
|
68
|
+
throw new Error(`[@cef-ai/agent-sdk] ctx.models.${alias}: model not declared in cef.config.ts`);
|
|
69
|
+
}
|
|
70
|
+
const started = Date.now();
|
|
71
|
+
const data = (await postJSON(url, { model: ref, input }, headers, `ctx.models.${alias}.infer`));
|
|
72
|
+
const call = {
|
|
73
|
+
model: alias,
|
|
74
|
+
bucket: ref.bucket,
|
|
75
|
+
version: ref.version,
|
|
76
|
+
durationMs: Date.now() - started,
|
|
77
|
+
};
|
|
78
|
+
if (data.usage) {
|
|
79
|
+
call.inputTokens = data.usage.inputTokens;
|
|
80
|
+
call.outputTokens = data.usage.outputTokens;
|
|
81
|
+
call.gpuUnits = data.usage.gpuUnits;
|
|
82
|
+
}
|
|
83
|
+
usage.record(call);
|
|
84
|
+
return data.output ?? data;
|
|
85
|
+
}
|
|
86
|
+
return {
|
|
87
|
+
infer,
|
|
88
|
+
// Streaming responses are deferred in AR v1 (no ReadableStream in the
|
|
89
|
+
// sandbox; ADR-001 §Out of scope). Degrade to a single-shot yield so
|
|
90
|
+
// `for await` consumers keep working; usage is still recorded.
|
|
91
|
+
stream(input) {
|
|
92
|
+
return {
|
|
93
|
+
async *[Symbol.asyncIterator]() {
|
|
94
|
+
yield await infer(input);
|
|
95
|
+
},
|
|
96
|
+
};
|
|
97
|
+
},
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Build the `ctx` object for one invocation. `raw` is whatever AR exposed
|
|
102
|
+
* as `globalThis.context`; `usage` collects inference calls for the
|
|
103
|
+
* `_modelCalls` fold (see {@link mergeModelCalls}); `modelRefs` is the
|
|
104
|
+
* `alias → ModelRef` map `cef build` baked into the bundle.
|
|
105
|
+
*/
|
|
106
|
+
export function buildContext(raw, usage, modelRefs = {}) {
|
|
107
|
+
const headers = authHeaders(raw);
|
|
108
|
+
// Pre-build declared model handles from the baked refs; a Proxy lets agents
|
|
109
|
+
// reference any alias (the handle throws a clear error at call time if the
|
|
110
|
+
// alias wasn't declared in cef.config.ts).
|
|
111
|
+
const models = new Proxy(Object.fromEntries(Object.entries(modelRefs).map(([a, ref]) => [
|
|
112
|
+
a,
|
|
113
|
+
buildModelHandle(raw, headers, usage, a, ref),
|
|
114
|
+
])), {
|
|
115
|
+
get(target, prop) {
|
|
116
|
+
if (typeof prop !== "string" || prop in target)
|
|
117
|
+
return target[prop];
|
|
118
|
+
return buildModelHandle(raw, headers, usage, prop, modelRefs[prop]);
|
|
119
|
+
},
|
|
120
|
+
});
|
|
121
|
+
return {
|
|
122
|
+
cubby: (alias) => buildCubby(raw, headers, alias),
|
|
123
|
+
models,
|
|
124
|
+
async publish(type, payload) {
|
|
125
|
+
const url = raw.endpoints?.vault?.publish;
|
|
126
|
+
if (!url) {
|
|
127
|
+
throw new Error("[@cef-ai/agent-sdk] ctx.publish: no vault publish endpoint in context");
|
|
128
|
+
}
|
|
129
|
+
await postJSON(url, { events: [{ type, payload, role: "agent" }] }, headers, "ctx.publish");
|
|
130
|
+
},
|
|
131
|
+
settings: Object.freeze({ ...(raw.settings ?? {}) }),
|
|
132
|
+
async close(reason) {
|
|
133
|
+
const base = raw.endpoints?.orchestrator;
|
|
134
|
+
const jobId = raw.correlation?.jobId;
|
|
135
|
+
if (!base || !jobId) {
|
|
136
|
+
throw new Error("[@cef-ai/agent-sdk] ctx.close: no orchestrator endpoint or jobId in context");
|
|
137
|
+
}
|
|
138
|
+
await postJSON(`${base}/api/v1/jobs/${encodeURIComponent(jobId)}/close`, { reason: reason ?? "closed_by_agent" }, headers, "ctx.close");
|
|
139
|
+
},
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
//# sourceMappingURL=ctx.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ctx.js","sourceRoot":"","sources":["../../src/runtime/ctx.ts"],"names":[],"mappings":"AAsBA,MAAM,YAAY,GAAG,EAAE,cAAc,EAAE,kBAAkB,EAAW,CAAC;AAErE,SAAS,WAAW,CAAC,GAAkB;IACrC,MAAM,CAAC,GAAG,GAAG,CAAC,IAAI,EAAE,OAAO,IAAI,EAAE,CAAC;IAClC,MAAM,KAAK,GAAG,GAAG,CAAC,IAAI,EAAE,KAAK,CAAC;IAC9B,OAAO,KAAK,CAAC,CAAC,CAAC,EAAE,GAAG,CAAC,EAAE,aAAa,EAAE,UAAU,KAAK,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,GAAG,CAAC,EAAE,CAAC;AACvE,CAAC;AAED,wEAAwE;AACxE,SAAS,OAAO,CAAI,OAA6B,EAAE,IAA6B;IAC9E,IAAI,CAAC,OAAO,IAAI,CAAC,IAAI;QAAE,OAAO,EAAE,CAAC;IACjC,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE;QACtB,MAAM,GAAG,GAA4B,EAAE,CAAC;QACxC,OAAO,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,CAAC,EAAE,EAAE;YACzB,GAAG,CAAC,GAAG,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC,CAAC;QACpB,CAAC,CAAC,CAAC;QACH,OAAO,GAAQ,CAAC;IAClB,CAAC,CAAC,CAAC;AACL,CAAC;AAED,KAAK,UAAU,QAAQ,CACrB,GAAW,EACX,IAAa,EACb,OAA+B,EAC/B,KAA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AE,CAAC;QAEpD,KAAK,CAAC,KAAK,CAAC,MAAe;YACzB,MAAM,IAAI,GAAG,GAAG,CAAC,SAAS,EAAE,YAAY,CAAC;YACzC,MAAM,KAAK,GAAG,GAAG,CAAC,WAAW,EAAE,KAAK,CAAC;YACrC,IAAI,CAAC,IAAI,IAAI,CAAC,KAAK,EAAE,CAAC;gBACpB,MAAM,IAAI,KAAK,CAAC,6EAA6E,CAAC,CAAC;YACjG,CAAC;YACD,MAAM,QAAQ,CACZ,GAAG,IAAI,gBAAgB,kBAAkB,CAAC,KAAK,CAAC,QAAQ,EACxD,EAAE,MAAM,EAAE,MAAM,IAAI,iBAAiB,EAAE,EACvC,OAAO,EACP,WAAW,CACZ,CAAC;QACJ,CAAC;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@cef-ai/agent-sdk/runtime` — the in-bundle runtime shim layer.
|
|
3
|
+
*
|
|
4
|
+
* This subpath is compiled INTO the agent bundle by `cef build` (unlike the
|
|
5
|
+
* package root, which is author-time types + decorators). It implements the
|
|
6
|
+
* `ctx` surface on top of the Agent Runtime's Web-platform sandbox + opaque
|
|
7
|
+
* `globalThis.context`, and exposes `registerHandlers`, which the build's
|
|
8
|
+
* generated wrapper calls to install `globalThis.__handlers`.
|
|
9
|
+
*
|
|
10
|
+
* Agent authors never import this directly — `cef build` wires it in.
|
|
11
|
+
*/
|
|
12
|
+
export { handlerKey, LIFECYCLE_CLOSE_KEY, LIFECYCLE_START_KEY, registerHandlers, SYNTHETIC_CLOSE_EVENT, SYNTHETIC_START_EVENT, } from "./register.js";
|
|
13
|
+
export type { BuildEngagementMeta, RegisterOptions } from "./register.js";
|
|
14
|
+
export { buildContext } from "./ctx.js";
|
|
15
|
+
export type { InvokeContext, ModelRef } from "./types.js";
|
|
16
|
+
export { type ModelCall, RESULT_MODEL_CALLS_KEY, UsageAccumulator } from "./usage.js";
|
|
17
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/runtime/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,OAAO,EACL,UAAU,EACV,mBAAmB,EACnB,mBAAmB,EACnB,gBAAgB,EAChB,qBAAqB,EACrB,qBAAqB,GACtB,MAAM,eAAe,CAAC;AACvB,YAAY,EAAE,mBAAmB,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAC1E,OAAO,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AACxC,YAAY,EAAE,aAAa,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAC1D,OAAO,EAAE,KAAK,SAAS,EAAE,sBAAsB,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@cef-ai/agent-sdk/runtime` — the in-bundle runtime shim layer.
|
|
3
|
+
*
|
|
4
|
+
* This subpath is compiled INTO the agent bundle by `cef build` (unlike the
|
|
5
|
+
* package root, which is author-time types + decorators). It implements the
|
|
6
|
+
* `ctx` surface on top of the Agent Runtime's Web-platform sandbox + opaque
|
|
7
|
+
* `globalThis.context`, and exposes `registerHandlers`, which the build's
|
|
8
|
+
* generated wrapper calls to install `globalThis.__handlers`.
|
|
9
|
+
*
|
|
10
|
+
* Agent authors never import this directly — `cef build` wires it in.
|
|
11
|
+
*/
|
|
12
|
+
export { handlerKey, LIFECYCLE_CLOSE_KEY, LIFECYCLE_START_KEY, registerHandlers, SYNTHETIC_CLOSE_EVENT, SYNTHETIC_START_EVENT, } from "./register.js";
|
|
13
|
+
export { buildContext } from "./ctx.js";
|
|
14
|
+
export { RESULT_MODEL_CALLS_KEY, UsageAccumulator } from "./usage.js";
|
|
15
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/runtime/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,OAAO,EACL,UAAU,EACV,mBAAmB,EACnB,mBAAmB,EACnB,gBAAgB,EAChB,qBAAqB,EACrB,qBAAqB,GACtB,MAAM,eAAe,CAAC;AAEvB,OAAO,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAExC,OAAO,EAAkB,sBAAsB,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC"}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { ModelRef } from "./types.js";
|
|
2
|
+
/** Lifecycle handler keys — must match orchestrator marketplace constants. */
|
|
3
|
+
export declare const LIFECYCLE_START_KEY = "onStart";
|
|
4
|
+
export declare const LIFECYCLE_CLOSE_KEY = "onClose";
|
|
5
|
+
/**
|
|
6
|
+
* Synthetic event-type sentinels the orchestrator injects when an engagement
|
|
7
|
+
* starts / closes — must match ddc-node marketplace `EventTypeSyntheticStart`
|
|
8
|
+
* / `EventTypeSyntheticClose`. `cef build` keys lifecycle dispatch in the
|
|
9
|
+
* manifest's `handlers` map under these (mapping them to the fixed
|
|
10
|
+
* `LIFECYCLE_*_KEY` handler names), so the orchestrator resolves lifecycle via
|
|
11
|
+
* the same `handlers[eventType]` lookup as ordinary events.
|
|
12
|
+
*/
|
|
13
|
+
export declare const SYNTHETIC_START_EVENT = "__start__";
|
|
14
|
+
export declare const SYNTHETIC_CLOSE_EVENT = "__close__";
|
|
15
|
+
/**
|
|
16
|
+
* Key under which an engagement's event handler is exposed on `__handlers`
|
|
17
|
+
* (and stored as a value in the manifest's `handlers` map). Event-handler
|
|
18
|
+
* keys are namespaced by engagement id so two engagements can each declare a
|
|
19
|
+
* method of the same name without colliding in the flat map — the
|
|
20
|
+
* orchestrator returns this string verbatim from `HandlerFor`, so AR looks up
|
|
21
|
+
* exactly this key. Lifecycle handlers are NOT namespaced: the orchestrator
|
|
22
|
+
* resolves them to the fixed `onStart` / `onClose` keys, so those remain
|
|
23
|
+
* agent-global.
|
|
24
|
+
*
|
|
25
|
+
* Used by BOTH the runtime (to build `__handlers`) and `cef build` (to emit
|
|
26
|
+
* the `handlers` map); the two MUST agree, hence the single helper.
|
|
27
|
+
*/
|
|
28
|
+
export declare function handlerKey(engagementId: string, method: string): string;
|
|
29
|
+
type EngagementClass = new () => Record<string, unknown>;
|
|
30
|
+
/**
|
|
31
|
+
* Build-time metadata the CLI reads (non-enumerable, off `__handlers`) to
|
|
32
|
+
* emit the manifest. Mirrors the per-engagement decorator metadata.
|
|
33
|
+
*/
|
|
34
|
+
export interface BuildEngagementMeta {
|
|
35
|
+
id: string;
|
|
36
|
+
goal?: string;
|
|
37
|
+
events: Record<string, string>;
|
|
38
|
+
lifecycle: {
|
|
39
|
+
start?: string;
|
|
40
|
+
close?: string;
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
/** Options `cef build` passes alongside the engagement classes. */
|
|
44
|
+
export interface RegisterOptions {
|
|
45
|
+
/** `alias → ModelRef`, resolved from `defineAgent({ models })` at build. */
|
|
46
|
+
models?: Record<string, ModelRef>;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Install `globalThis.__handlers` from the engagement classes. Called once
|
|
50
|
+
* per isolate when the bundle's top-level code runs. `options.models` (the
|
|
51
|
+
* baked `alias → ModelRef` map) is captured for the `ctx.models` shims.
|
|
52
|
+
*/
|
|
53
|
+
export declare function registerHandlers(engagements: Record<string, EngagementClass>, options?: RegisterOptions): void;
|
|
54
|
+
export {};
|
|
55
|
+
//# sourceMappingURL=register.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"register.d.ts","sourceRoot":"","sources":["../../src/runtime/register.ts"],"names":[],"mappings":"AA+BA,OAAO,KAAK,EAAiB,QAAQ,EAAE,MAAM,YAAY,CAAC;AAW1D,8EAA8E;AAC9E,eAAO,MAAM,mBAAmB,YAAY,CAAC;AAC7C,eAAO,MAAM,mBAAmB,YAAY,CAAC;AAE7C;;;;;;;GAOG;AACH,eAAO,MAAM,qBAAqB,cAAc,CAAC;AACjD,eAAO,MAAM,qBAAqB,cAAc,CAAC;AAEjD;;;;;;;;;;;;GAYG;AACH,wBAAgB,UAAU,CAAC,YAAY,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAEvE;AAED,KAAK,eAAe,GAAG,UAAU,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAGzD;;;GAGG;AACH,MAAM,WAAW,mBAAmB;IAClC,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC/B,SAAS,EAAE;QAAE,KAAK,CAAC,EAAE,MAAM,CAAC;QAAC,KAAK,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;CAC/C;AA0CD,mEAAmE;AACnE,MAAM,WAAW,eAAe;IAC9B,4EAA4E;IAC5E,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;CACnC;AAED;;;;GAIG;AACH,wBAAgB,gBAAgB,CAC9B,WAAW,EAAE,MAAM,CAAC,MAAM,EAAE,eAAe,CAAC,EAC5C,OAAO,GAAE,eAAoB,GAC5B,IAAI,CAyCN"}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `registerHandlers` — the bundle's entry point. `cef build` emits a wrapper
|
|
3
|
+
* that imports each engagement class and calls this with `{ id: Class }`;
|
|
4
|
+
* running the bundle installs `globalThis.__handlers`, the flat map the
|
|
5
|
+
* Agent Runtime dispatches against (AR ADR-001: `__handlers[name](event,
|
|
6
|
+
* context)`).
|
|
7
|
+
*
|
|
8
|
+
* Handler-key scheme (must match what the manifest's `handlers` map +
|
|
9
|
+
* orchestrator `HandlerFor` expect):
|
|
10
|
+
* - Event handlers are keyed `<engagementId>::<method>` (the value the
|
|
11
|
+
* manifest's `engagements[].handlers{eventType → key}` carries; the
|
|
12
|
+
* orchestrator returns it verbatim from `HandlerFor`).
|
|
13
|
+
* - Lifecycle handlers are keyed by the fixed constants `onStart` /
|
|
14
|
+
* `onClose`; the manifest's `handlers` map points the synthetic
|
|
15
|
+
* `__start__` / `__close__` event types at these keys, so lifecycle
|
|
16
|
+
* dispatch flows through the same `handlers[eventType]` lookup as
|
|
17
|
+
* ordinary events (no separate `hooks` array).
|
|
18
|
+
*
|
|
19
|
+
* Because the key space is flat across all engagements, two engagements
|
|
20
|
+
* declaring the same handler key collide; we throw at registration with a
|
|
21
|
+
* message naming both owners. `cef build` runs this in a vm, so the
|
|
22
|
+
* collision surfaces as a build error (not a runtime fault).
|
|
23
|
+
*
|
|
24
|
+
* Instances are constructed PER INVOCATION inside each wrapper — never
|
|
25
|
+
* cached — so no instance state survives across invocations (AR isolates
|
|
26
|
+
* are reused across Jobs/Vaults with no affinity; a cached instance would
|
|
27
|
+
* leak state across tenants). Durable state belongs in cubbies.
|
|
28
|
+
*/
|
|
29
|
+
import { ENGAGEMENT_METADATA } from "../symbols.js";
|
|
30
|
+
import { buildContext } from "./ctx.js";
|
|
31
|
+
import { mergeModelCalls, UsageAccumulator } from "./usage.js";
|
|
32
|
+
/**
|
|
33
|
+
* `alias → ModelRef` map baked into the bundle by `cef build` and passed to
|
|
34
|
+
* {@link registerHandlers}. Captured here so the per-invocation `ctx.models`
|
|
35
|
+
* shims know which model each alias points at, without the orchestrator
|
|
36
|
+
* carrying model identity.
|
|
37
|
+
*/
|
|
38
|
+
let bakedModels = {};
|
|
39
|
+
/** Lifecycle handler keys — must match orchestrator marketplace constants. */
|
|
40
|
+
export const LIFECYCLE_START_KEY = "onStart";
|
|
41
|
+
export const LIFECYCLE_CLOSE_KEY = "onClose";
|
|
42
|
+
/**
|
|
43
|
+
* Synthetic event-type sentinels the orchestrator injects when an engagement
|
|
44
|
+
* starts / closes — must match ddc-node marketplace `EventTypeSyntheticStart`
|
|
45
|
+
* / `EventTypeSyntheticClose`. `cef build` keys lifecycle dispatch in the
|
|
46
|
+
* manifest's `handlers` map under these (mapping them to the fixed
|
|
47
|
+
* `LIFECYCLE_*_KEY` handler names), so the orchestrator resolves lifecycle via
|
|
48
|
+
* the same `handlers[eventType]` lookup as ordinary events.
|
|
49
|
+
*/
|
|
50
|
+
export const SYNTHETIC_START_EVENT = "__start__";
|
|
51
|
+
export const SYNTHETIC_CLOSE_EVENT = "__close__";
|
|
52
|
+
/**
|
|
53
|
+
* Key under which an engagement's event handler is exposed on `__handlers`
|
|
54
|
+
* (and stored as a value in the manifest's `handlers` map). Event-handler
|
|
55
|
+
* keys are namespaced by engagement id so two engagements can each declare a
|
|
56
|
+
* method of the same name without colliding in the flat map — the
|
|
57
|
+
* orchestrator returns this string verbatim from `HandlerFor`, so AR looks up
|
|
58
|
+
* exactly this key. Lifecycle handlers are NOT namespaced: the orchestrator
|
|
59
|
+
* resolves them to the fixed `onStart` / `onClose` keys, so those remain
|
|
60
|
+
* agent-global.
|
|
61
|
+
*
|
|
62
|
+
* Used by BOTH the runtime (to build `__handlers`) and `cef build` (to emit
|
|
63
|
+
* the `handlers` map); the two MUST agree, hence the single helper.
|
|
64
|
+
*/
|
|
65
|
+
export function handlerKey(engagementId, method) {
|
|
66
|
+
return `${engagementId}::${method}`;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Wrap a user method as an AR handler: fresh instance + ctx per call,
|
|
70
|
+
* `args` chosen by the caller, result folded with accumulated usage.
|
|
71
|
+
*/
|
|
72
|
+
function makeHandler(Cls, method, args) {
|
|
73
|
+
return async (event, rawContext) => {
|
|
74
|
+
const usage = new UsageAccumulator();
|
|
75
|
+
const ctx = buildContext(rawContext ?? {}, usage, bakedModels);
|
|
76
|
+
const instance = new Cls();
|
|
77
|
+
const fn = instance[method];
|
|
78
|
+
if (typeof fn !== "function") {
|
|
79
|
+
throw new Error(`[@cef-ai/agent-sdk] handler "${method}" is not a function`);
|
|
80
|
+
}
|
|
81
|
+
const result = await fn.apply(instance, args(event, ctx));
|
|
82
|
+
return mergeModelCalls(result, usage);
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
/** Event handler: invoked as `method(event, ctx)`. */
|
|
86
|
+
function eventHandler(Cls, method) {
|
|
87
|
+
return makeHandler(Cls, method, (event, ctx) => [event, ctx]);
|
|
88
|
+
}
|
|
89
|
+
/** Start lifecycle hook: invoked as `method(ctx)`. */
|
|
90
|
+
function startHandler(Cls, method) {
|
|
91
|
+
return makeHandler(Cls, method, (_event, ctx) => [ctx]);
|
|
92
|
+
}
|
|
93
|
+
/** Close lifecycle hook: invoked as `method(ctx, reason)`. */
|
|
94
|
+
function closeHandler(Cls, method) {
|
|
95
|
+
return makeHandler(Cls, method, (event, ctx) => [
|
|
96
|
+
ctx,
|
|
97
|
+
event?.payload?.reason ?? "closed_by_agent",
|
|
98
|
+
]);
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Install `globalThis.__handlers` from the engagement classes. Called once
|
|
102
|
+
* per isolate when the bundle's top-level code runs. `options.models` (the
|
|
103
|
+
* baked `alias → ModelRef` map) is captured for the `ctx.models` shims.
|
|
104
|
+
*/
|
|
105
|
+
export function registerHandlers(engagements, options = {}) {
|
|
106
|
+
bakedModels = options.models ?? {};
|
|
107
|
+
const handlers = {};
|
|
108
|
+
const owners = {};
|
|
109
|
+
const meta = [];
|
|
110
|
+
const claim = (key, owner, fn) => {
|
|
111
|
+
if (owners[key]) {
|
|
112
|
+
throw new Error(`[@cef-ai/agent-sdk] handler key "${key}" is claimed by both ${owners[key]} and ${owner}; ` +
|
|
113
|
+
`handler method names (and lifecycle hooks) must be unique across engagements`);
|
|
114
|
+
}
|
|
115
|
+
owners[key] = owner;
|
|
116
|
+
handlers[key] = fn;
|
|
117
|
+
};
|
|
118
|
+
for (const [id, Cls] of Object.entries(engagements)) {
|
|
119
|
+
const m = Cls[ENGAGEMENT_METADATA];
|
|
120
|
+
if (!m) {
|
|
121
|
+
throw new Error(`[@cef-ai/agent-sdk] engagement "${id}": class has no @OnEvent/@OnStart/@OnClose/@Engagement decorators`);
|
|
122
|
+
}
|
|
123
|
+
const events = m.events ?? {};
|
|
124
|
+
const lifecycle = m.lifecycle ?? {};
|
|
125
|
+
meta.push({ id, goal: m.goal, events, lifecycle });
|
|
126
|
+
for (const method of new Set(Object.values(events))) {
|
|
127
|
+
claim(handlerKey(id, method), `${id}.${method}`, eventHandler(Cls, method));
|
|
128
|
+
}
|
|
129
|
+
if (lifecycle.start) {
|
|
130
|
+
claim(LIFECYCLE_START_KEY, `${id}.${lifecycle.start}`, startHandler(Cls, lifecycle.start));
|
|
131
|
+
}
|
|
132
|
+
if (lifecycle.close) {
|
|
133
|
+
claim(LIFECYCLE_CLOSE_KEY, `${id}.${lifecycle.close}`, closeHandler(Cls, lifecycle.close));
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
Object.defineProperty(handlers, "__engagements", { value: meta, enumerable: false });
|
|
137
|
+
globalThis.__handlers = handlers;
|
|
138
|
+
}
|
|
139
|
+
//# sourceMappingURL=register.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"register.js","sourceRoot":"","sources":["../../src/runtime/register.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,OAAO,EAAE,mBAAmB,EAAuB,MAAM,eAAe,CAAC;AAEzE,OAAO,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAExC,OAAO,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAE/D;;;;;GAKG;AACH,IAAI,WAAW,GAA6B,EAAE,CAAC;AAE/C,8EAA8E;AAC9E,MAAM,CAAC,MAAM,mBAAmB,GAAG,SAAS,CAAC;AAC7C,MAAM,CAAC,MAAM,mBAAmB,GAAG,SAAS,CAAC;AAE7C;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAG,WAAW,CAAC;AACjD,MAA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B,MAAM,SAAS,GAAG,CAAC,CAAC,SAAS,IAAI,EAAE,CAAC;QACpC,IAAI,CAAC,IAAI,CAAC,EAAE,EAAE,EAAE,IAAI,EAAE,CAAC,CAAC,IAAI,EAAE,MAAM,EAAE,SAAS,EAAE,CAAC,CAAC;QAEnD,KAAK,MAAM,MAAM,IAAI,IAAI,GAAG,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC;YACpD,KAAK,CAAC,UAAU,CAAC,EAAE,EAAE,MAAM,CAAC,EAAE,GAAG,EAAE,IAAI,MAAM,EAAE,EAAE,YAAY,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC,CAAC;QAC9E,CAAC;QACD,IAAI,SAAS,CAAC,KAAK,EAAE,CAAC;YACpB,KAAK,CAAC,mBAAmB,EAAE,GAAG,EAAE,IAAI,SAAS,CAAC,KAAK,EAAE,EAAE,YAAY,CAAC,GAAG,EAAE,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC;QAC7F,CAAC;QACD,IAAI,SAAS,CAAC,KAAK,EAAE,CAAC;YACpB,KAAK,CAAC,mBAAmB,EAAE,GAAG,EAAE,IAAI,SAAS,CAAC,KAAK,EAAE,EAAE,YAAY,CAAC,GAAG,EAAE,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC;QAC7F,CAAC;IACH,CAAC;IAED,MAAM,CAAC,cAAc,CAAC,QAAQ,EAAE,eAAe,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,UAAU,EAAE,KAAK,EAAE,CAAC,CAAC;IACpF,UAAmE,CAAC,UAAU,GAAG,QAAQ,CAAC;AAC7F,CAAC"}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shape of `globalThis.context` — the single opaque object the Agent
|
|
3
|
+
* Runtime injects into the V8 isolate per invocation (AR ADR-001 §V8
|
|
4
|
+
* sandbox surface). AR treats it as pass-through JSON; this is the
|
|
5
|
+
* *convention* the orchestrator writes and the SDK shims read.
|
|
6
|
+
*
|
|
7
|
+
* Source of truth (orchestrator side):
|
|
8
|
+
* `ddc-node/internal/compute/orchestrator/job/invoke.go` →
|
|
9
|
+
* `buildInvokeContext`. Keep this in sync with that function.
|
|
10
|
+
*
|
|
11
|
+
* Every field is optional because AR performs no validation and tests /
|
|
12
|
+
* non-CEF callers may send partial contexts; the shims throw a clear
|
|
13
|
+
* error at call time when a required endpoint is missing rather than
|
|
14
|
+
* assuming structure here.
|
|
15
|
+
*/
|
|
16
|
+
export interface InvokeContext {
|
|
17
|
+
/** Externally-reachable service URLs the shims call via `fetch`. */
|
|
18
|
+
endpoints?: {
|
|
19
|
+
/** Orchestrator root — used for cubby ops and `ctx.close`. */
|
|
20
|
+
orchestrator?: string;
|
|
21
|
+
/** Vault API — `publish` is the fully-formed events URL for this vault+scope. */
|
|
22
|
+
vault?: {
|
|
23
|
+
publish?: string;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Inference Gateway native endpoint (e.g. `<ig-base>/api/v1/inference`),
|
|
27
|
+
* configured by the orchestrator. The model identity per alias is baked
|
|
28
|
+
* into the bundle by `cef build` (see {@link ModelRef}), not carried
|
|
29
|
+
* here — the orchestrator stays model-agnostic.
|
|
30
|
+
*/
|
|
31
|
+
inference?: string;
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* Trusted-identity auth the shims attach to every callback `fetch`.
|
|
35
|
+
* `headers` carries `X-Vault-Id` / `X-Agent-Id` / `X-AS-PubKey`
|
|
36
|
+
* (network-trust boundary). `token` is reserved for the ADR-020
|
|
37
|
+
* per-invocation capability token; blank until that lands.
|
|
38
|
+
*/
|
|
39
|
+
auth?: {
|
|
40
|
+
headers?: Record<string, string>;
|
|
41
|
+
token?: string;
|
|
42
|
+
};
|
|
43
|
+
/** Decrypted ConnectionSettings, surfaced as `ctx.settings`. */
|
|
44
|
+
settings?: Record<string, unknown>;
|
|
45
|
+
/** Correlation identifiers for the running Job/Task. */
|
|
46
|
+
correlation?: {
|
|
47
|
+
jobId?: string;
|
|
48
|
+
taskId?: string;
|
|
49
|
+
vaultId?: string;
|
|
50
|
+
scope?: string;
|
|
51
|
+
agentId?: string;
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* A DDC model identity, as the Inference Gateway's native `/api/v1/inference`
|
|
56
|
+
* expects it (`{ model: { bucket, name, version }, input }`). `cef build`
|
|
57
|
+
* resolves each `defineAgent({ models })` entry into one of these and bakes
|
|
58
|
+
* the `alias → ModelRef` map into the bundle, so `ctx.models.<alias>` knows
|
|
59
|
+
* which model to call without the orchestrator carrying it.
|
|
60
|
+
*/
|
|
61
|
+
export interface ModelRef {
|
|
62
|
+
bucket: number;
|
|
63
|
+
name: string;
|
|
64
|
+
version: string;
|
|
65
|
+
}
|
|
66
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/runtime/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,MAAM,WAAW,aAAa;IAC5B,oEAAoE;IACpE,SAAS,CAAC,EAAE;QACV,8DAA8D;QAC9D,YAAY,CAAC,EAAE,MAAM,CAAC;QACtB,iFAAiF;QACjF,KAAK,CAAC,EAAE;YAAE,OAAO,CAAC,EAAE,MAAM,CAAA;SAAE,CAAC;QAC7B;;;;;WAKG;QACH,SAAS,CAAC,EAAE,MAAM,CAAC;KACpB,CAAC;IACF;;;;;OAKG;IACH,IAAI,CAAC,EAAE;QACL,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QACjC,KAAK,CAAC,EAAE,MAAM,CAAC;KAChB,CAAC;IACF,gEAAgE;IAChE,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACnC,wDAAwD;IACxD,WAAW,CAAC,EAAE;QACZ,KAAK,CAAC,EAAE,MAAM,CAAC;QACf,MAAM,CAAC,EAAE,MAAM,CAAC;QAChB,OAAO,CAAC,EAAE,MAAM,CAAC;QACjB,KAAK,CAAC,EAAE,MAAM,CAAC;QACf,OAAO,CAAC,EAAE,MAAM,CAAC;KAClB,CAAC;CACH;AAED;;;;;;GAMG;AACH,MAAM,WAAW,QAAQ;IACvB,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;CACjB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/runtime/types.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Inference-usage accumulation, propagated back to the orchestrator via a
|
|
3
|
+
* reserved key on the handler's return value.
|
|
4
|
+
*
|
|
5
|
+
* AR's `/v1/invoke` response carries only V8-profiler metrics (CPU / memory
|
|
6
|
+
* / wall-clock) — it does NOT meter inference. Per ADR-002 §Resource
|
|
7
|
+
* accounting / ADR-004, the SDK accumulates per-call inference usage from
|
|
8
|
+
* `ctx.models.*` calls and folds it into the handler result under
|
|
9
|
+
* `_modelCalls`; AR passes the result through opaquely; the orchestrator
|
|
10
|
+
* parses it at task-terminal and records it on the Task/Job.
|
|
11
|
+
*
|
|
12
|
+
* Source of truth (orchestrator side):
|
|
13
|
+
* `ddc-node/internal/compute/orchestrator/job/recorder.go`
|
|
14
|
+
* (`resultModelCallsKey = "_modelCalls"`). Keep `ModelCall` in sync with the
|
|
15
|
+
* struct it unmarshals into there.
|
|
16
|
+
*/
|
|
17
|
+
/** The reserved result key the orchestrator reads inference usage from. */
|
|
18
|
+
export declare const RESULT_MODEL_CALLS_KEY = "_modelCalls";
|
|
19
|
+
/** One per-model inference-usage record (the `_modelCalls[]` entry shape). */
|
|
20
|
+
export interface ModelCall {
|
|
21
|
+
/** Model identifier the usage is attributed to (the manifest alias). */
|
|
22
|
+
model: string;
|
|
23
|
+
/** DDC bucket id, when known. */
|
|
24
|
+
bucket?: number;
|
|
25
|
+
/** Model version, when known. */
|
|
26
|
+
version?: string;
|
|
27
|
+
/** Per-call duration in milliseconds, when measured. */
|
|
28
|
+
durationMs?: number;
|
|
29
|
+
/** Normalised GPU units consumed, when reported by the gateway. */
|
|
30
|
+
gpuUnits?: number;
|
|
31
|
+
/** Input/prompt token count. */
|
|
32
|
+
inputTokens?: number;
|
|
33
|
+
/** Output/completion token count. */
|
|
34
|
+
outputTokens?: number;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Per-invocation collector. One instance is created per handler call and
|
|
38
|
+
* threaded into the `ctx.models.*` shims; whatever they record is folded
|
|
39
|
+
* into the result by {@link mergeModelCalls}.
|
|
40
|
+
*/
|
|
41
|
+
export declare class UsageAccumulator {
|
|
42
|
+
readonly calls: ModelCall[];
|
|
43
|
+
record(call: ModelCall): void;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Fold accumulated inference usage into a handler's return value under
|
|
47
|
+
* {@link RESULT_MODEL_CALLS_KEY}. No usage → the result passes through
|
|
48
|
+
* unchanged (normalising `undefined` to `null`, matching how AR serialises
|
|
49
|
+
* an empty return). The result object is shallow-copied, never mutated.
|
|
50
|
+
*
|
|
51
|
+
* Edge case: when usage exists but the handler returned a primitive or an
|
|
52
|
+
* array (can't carry a `_modelCalls` property), the value is wrapped as
|
|
53
|
+
* `{ result, _modelCalls }` so usage is never silently dropped.
|
|
54
|
+
*/
|
|
55
|
+
export declare function mergeModelCalls(result: unknown, usage: UsageAccumulator): unknown;
|
|
56
|
+
//# sourceMappingURL=usage.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"usage.d.ts","sourceRoot":"","sources":["../../src/runtime/usage.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,2EAA2E;AAC3E,eAAO,MAAM,sBAAsB,gBAAgB,CAAC;AAEpD,8EAA8E;AAC9E,MAAM,WAAW,SAAS;IACxB,wEAAwE;IACxE,KAAK,EAAE,MAAM,CAAC;IACd,iCAAiC;IACjC,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,iCAAiC;IACjC,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,wDAAwD;IACxD,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,mEAAmE;IACnE,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,gCAAgC;IAChC,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,qCAAqC;IACrC,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AAED;;;;GAIG;AACH,qBAAa,gBAAgB;IAC3B,QAAQ,CAAC,KAAK,EAAE,SAAS,EAAE,CAAM;IAEjC,MAAM,CAAC,IAAI,EAAE,SAAS,GAAG,IAAI;CAG9B;AAED;;;;;;;;;GASG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAE,OAAO,EAAE,KAAK,EAAE,gBAAgB,GAAG,OAAO,CAWjF"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Inference-usage accumulation, propagated back to the orchestrator via a
|
|
3
|
+
* reserved key on the handler's return value.
|
|
4
|
+
*
|
|
5
|
+
* AR's `/v1/invoke` response carries only V8-profiler metrics (CPU / memory
|
|
6
|
+
* / wall-clock) — it does NOT meter inference. Per ADR-002 §Resource
|
|
7
|
+
* accounting / ADR-004, the SDK accumulates per-call inference usage from
|
|
8
|
+
* `ctx.models.*` calls and folds it into the handler result under
|
|
9
|
+
* `_modelCalls`; AR passes the result through opaquely; the orchestrator
|
|
10
|
+
* parses it at task-terminal and records it on the Task/Job.
|
|
11
|
+
*
|
|
12
|
+
* Source of truth (orchestrator side):
|
|
13
|
+
* `ddc-node/internal/compute/orchestrator/job/recorder.go`
|
|
14
|
+
* (`resultModelCallsKey = "_modelCalls"`). Keep `ModelCall` in sync with the
|
|
15
|
+
* struct it unmarshals into there.
|
|
16
|
+
*/
|
|
17
|
+
/** The reserved result key the orchestrator reads inference usage from. */
|
|
18
|
+
export const RESULT_MODEL_CALLS_KEY = "_modelCalls";
|
|
19
|
+
/**
|
|
20
|
+
* Per-invocation collector. One instance is created per handler call and
|
|
21
|
+
* threaded into the `ctx.models.*` shims; whatever they record is folded
|
|
22
|
+
* into the result by {@link mergeModelCalls}.
|
|
23
|
+
*/
|
|
24
|
+
export class UsageAccumulator {
|
|
25
|
+
calls = [];
|
|
26
|
+
record(call) {
|
|
27
|
+
this.calls.push(call);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Fold accumulated inference usage into a handler's return value under
|
|
32
|
+
* {@link RESULT_MODEL_CALLS_KEY}. No usage → the result passes through
|
|
33
|
+
* unchanged (normalising `undefined` to `null`, matching how AR serialises
|
|
34
|
+
* an empty return). The result object is shallow-copied, never mutated.
|
|
35
|
+
*
|
|
36
|
+
* Edge case: when usage exists but the handler returned a primitive or an
|
|
37
|
+
* array (can't carry a `_modelCalls` property), the value is wrapped as
|
|
38
|
+
* `{ result, _modelCalls }` so usage is never silently dropped.
|
|
39
|
+
*/
|
|
40
|
+
export function mergeModelCalls(result, usage) {
|
|
41
|
+
if (usage.calls.length === 0) {
|
|
42
|
+
return result === undefined ? null : result;
|
|
43
|
+
}
|
|
44
|
+
if (result === undefined || result === null) {
|
|
45
|
+
return { [RESULT_MODEL_CALLS_KEY]: usage.calls };
|
|
46
|
+
}
|
|
47
|
+
if (typeof result === "object" && !Array.isArray(result)) {
|
|
48
|
+
return { ...result, [RESULT_MODEL_CALLS_KEY]: usage.calls };
|
|
49
|
+
}
|
|
50
|
+
return { result, [RESULT_MODEL_CALLS_KEY]: usage.calls };
|
|
51
|
+
}
|
|
52
|
+
//# sourceMappingURL=usage.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"usage.js","sourceRoot":"","sources":["../../src/runtime/usage.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,2EAA2E;AAC3E,MAAM,CAAC,MAAM,sBAAsB,GAAG,aAAa,CAAC;AAoBpD;;;;GAIG;AACH,MAAM,OAAO,gBAAgB;IAClB,KAAK,GAAgB,EAAE,CAAC;IAEjC,MAAM,CAAC,IAAe;QACpB,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACxB,CAAC;CACF;AAED;;;;;;;;;GASG;AACH,MAAM,UAAU,eAAe,CAAC,MAAe,EAAE,KAAuB;IACtE,IAAI,KAAK,CAAC,KAAK,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAC7B,OAAO,MAAM,KAAK,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,MAAM,CAAC;IAC9C,CAAC;IACD,IAAI,MAAM,KAAK,SAAS,IAAI,MAAM,KAAK,IAAI,EAAE,CAAC;QAC5C,OAAO,EAAE,CAAC,sBAAsB,CAAC,EAAE,KAAK,CAAC,KAAK,EAAE,CAAC;IACnD,CAAC;IACD,IAAI,OAAO,MAAM,KAAK,QAAQ,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC;QACzD,OAAO,EAAE,GAAI,MAAkC,EAAE,CAAC,sBAAsB,CAAC,EAAE,KAAK,CAAC,KAAK,EAAE,CAAC;IAC3F,CAAC;IACD,OAAO,EAAE,MAAM,EAAE,CAAC,sBAAsB,CAAC,EAAE,KAAK,CAAC,KAAK,EAAE,CAAC;AAC3D,CAAC"}
|
package/dist/types.d.ts
CHANGED
|
@@ -65,18 +65,21 @@ export interface ModelHandle<I = unknown, O = unknown> {
|
|
|
65
65
|
stream(input: I): AsyncIterable<O>;
|
|
66
66
|
}
|
|
67
67
|
/**
|
|
68
|
-
*
|
|
69
|
-
*
|
|
68
|
+
* Declaration-merged map of the agent's declared model aliases to typed
|
|
69
|
+
* {@link ModelHandle}s. Populated by `cef typegen` from each model's
|
|
70
|
+
* `model.json` (`schema.inputs` / `schema.outputs`), so `ctx.models.<alias>`
|
|
71
|
+
* is typed end-to-end. Empty by default.
|
|
70
72
|
*
|
|
71
|
-
*
|
|
72
|
-
*
|
|
73
|
-
*
|
|
73
|
+
* Typegen extends it via:
|
|
74
|
+
* ```ts
|
|
75
|
+
* declare module "@cef-ai/agent-sdk" {
|
|
76
|
+
* interface KnownModels {
|
|
77
|
+
* gemma: ModelHandle<GemmaInput, GemmaOutput>;
|
|
78
|
+
* }
|
|
79
|
+
* }
|
|
80
|
+
* ```
|
|
74
81
|
*/
|
|
75
|
-
export interface
|
|
76
|
-
info(msg: string, fields?: Record<string, unknown>): void;
|
|
77
|
-
warn(msg: string, fields?: Record<string, unknown>): void;
|
|
78
|
-
error(msg: string, fields?: Record<string, unknown>): void;
|
|
79
|
-
debug(msg: string, fields?: Record<string, unknown>): void;
|
|
82
|
+
export interface KnownModels {
|
|
80
83
|
}
|
|
81
84
|
/**
|
|
82
85
|
* Reason passed to the `@OnClose` handler by the runtime, per
|
|
@@ -89,20 +92,14 @@ export interface Logger {
|
|
|
89
92
|
*/
|
|
90
93
|
export type OnCloseReason = "revoked" | "idle_timeout" | "closed_by_agent" | "failed";
|
|
91
94
|
/**
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
*
|
|
95
|
-
*
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
body?: string | Uint8Array | null;
|
|
101
|
-
signal?: AbortSignal;
|
|
102
|
-
}
|
|
103
|
-
/**
|
|
104
|
-
* The single object passed to lifecycle / event handlers. Surface defined
|
|
105
|
-
* by sdk-runtime-integration-contract §3.
|
|
95
|
+
* The single object passed to event / lifecycle handlers — the CEF-specific
|
|
96
|
+
* platform surface only.
|
|
97
|
+
*
|
|
98
|
+
* Everything here genuinely needs the platform: each member is a shim that
|
|
99
|
+
* reads endpoints/auth/settings from the Agent Runtime's `globalThis.context`
|
|
100
|
+
* and calls back over `fetch`. Plain Web-platform capabilities are NOT
|
|
101
|
+
* mirrored here — agents use the sandbox globals directly: `console.*` for
|
|
102
|
+
* logging (the runtime captures it out-of-band) and `fetch` for HTTP.
|
|
106
103
|
*
|
|
107
104
|
* `publish` is overloaded so authors with typegen-populated
|
|
108
105
|
* {@link KnownEventTypes} get payload-typed publishes; the fallback
|
|
@@ -111,19 +108,19 @@ export interface FetchInit {
|
|
|
111
108
|
export interface Context {
|
|
112
109
|
/** Per-alias scoped storage handle. */
|
|
113
110
|
cubby(alias: string): CubbyHandle;
|
|
114
|
-
/**
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
111
|
+
/**
|
|
112
|
+
* Model handles keyed by manifest-declared alias. Declared aliases are
|
|
113
|
+
* typed via {@link KnownModels} (filled in by `cef typegen`); any other
|
|
114
|
+
* alias falls back to an untyped `ModelHandle`.
|
|
115
|
+
*/
|
|
116
|
+
models: KnownModels & Record<string, ModelHandle>;
|
|
118
117
|
/** Publish a typed event. */
|
|
119
118
|
publish<T extends keyof KnownEventTypes & string>(type: T, payload: KnownEventTypes[T]): Promise<void>;
|
|
120
119
|
/** Publish an event with an arbitrary string type. */
|
|
121
120
|
publish(type: string, payload: unknown): Promise<void>;
|
|
122
|
-
/** Frozen
|
|
121
|
+
/** Frozen ConnectionSettings supplied by the user at connect time. */
|
|
123
122
|
settings: Readonly<Record<string, unknown>>;
|
|
124
123
|
/** Ask the runtime to close this agent. `reason` is surfaced to telemetry. */
|
|
125
124
|
close(reason?: string): Promise<void>;
|
|
126
|
-
/** Structured logger (see {@link Logger}). */
|
|
127
|
-
log: Logger;
|
|
128
125
|
}
|
|
129
126
|
//# sourceMappingURL=types.d.ts.map
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAMH;;;;;;;;;;;;;;;;;GAiBG;AAEH,MAAM,WAAW,eAAe;CAAG;AAEnC;;;GAGG;AACH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AC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;IAE5C,8EAA8E;IAC9E,KAAK,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CACvC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cef-ai/agent-sdk",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -12,6 +12,10 @@
|
|
|
12
12
|
"./config": {
|
|
13
13
|
"types": "./dist/config/index.d.ts",
|
|
14
14
|
"default": "./dist/config/index.js"
|
|
15
|
+
},
|
|
16
|
+
"./runtime": {
|
|
17
|
+
"types": "./dist/runtime/index.d.ts",
|
|
18
|
+
"default": "./dist/runtime/index.js"
|
|
15
19
|
}
|
|
16
20
|
},
|
|
17
21
|
"files": [
|