@nylorun/runtime 0.4.0-beta → 0.6.0-beta
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/CHANGELOG.md +55 -0
- package/README.md +29 -47
- package/dist/adapters/media.d.ts +2 -18
- package/dist/adapters/media.js +2 -52
- package/dist/adapters/observe.js +1 -1
- package/dist/config.d.ts +17 -10
- package/dist/configuration.d.ts +3 -0
- package/dist/configuration.js +3 -0
- package/dist/contracts.d.ts +5 -166
- package/dist/core/main.js +40 -0
- package/dist/core/provider.d.ts +12 -0
- package/dist/core/provider.js +60 -0
- package/dist/core/runtime.d.ts +50 -0
- package/dist/core/runtime.js +877 -0
- package/dist/core/store.d.ts +16 -0
- package/dist/core/store.js +101 -0
- package/dist/index.d.ts +6 -11
- package/dist/index.js +4 -6
- package/dist/media.d.ts +29 -0
- package/dist/media.js +53 -0
- package/dist/model/defaults.d.ts +17 -0
- package/dist/model/defaults.js +21 -0
- package/dist/model/http-model.d.ts +12 -0
- package/dist/model/http-model.js +299 -0
- package/dist/model/pi-model.d.ts +2 -1
- package/dist/model/pi-model.js +52 -7
- package/dist/node/index.d.ts +5 -0
- package/dist/node/index.js +5 -0
- package/dist/node/local-sessions.d.ts +5 -0
- package/dist/node/local-sessions.js +174 -0
- package/dist/redact.d.ts +1 -0
- package/dist/redact.js +14 -0
- package/dist/server/ag-ui.d.ts +1 -1
- package/dist/server/delivery.d.ts +24 -0
- package/dist/server/delivery.js +107 -0
- package/dist/server/host.d.ts +50 -7
- package/dist/server/host.js +304 -307
- package/dist/session/api.d.ts +10 -0
- package/dist/session/api.js +15 -0
- package/dist/session/default.d.ts +5 -0
- package/dist/session/default.js +30 -0
- package/dist/session/handle.d.ts +27 -0
- package/dist/session/handle.js +199 -0
- package/dist/session/index.d.ts +2 -0
- package/dist/session/index.js +2 -0
- package/dist/sessions/host.d.ts +39 -0
- package/dist/sessions/host.js +359 -0
- package/dist/sessions/store.d.ts +41 -0
- package/dist/sessions/store.js +33 -0
- package/package.json +23 -12
- package/dist/adapters/journal.d.ts +0 -35
- package/dist/adapters/journal.js +0 -130
- package/dist/cli.d.ts +0 -2
- package/dist/cli.js +0 -100
- package/dist/dev-entry.js +0 -2
- package/dist/dev.d.ts +0 -2
- package/dist/dev.js +0 -126
- package/dist/environment.d.ts +0 -2
- package/dist/environment.js +0 -64
- package/dist/launcher.d.ts +0 -1
- package/dist/launcher.js +0 -28
- package/dist/model/configure.d.ts +0 -12
- package/dist/model/configure.js +0 -155
- /package/dist/{dev-entry.d.ts → core/main.d.ts} +0 -0
package/dist/model/pi-model.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { join } from "node:path";
|
|
2
|
-
import { scrub } from "../
|
|
2
|
+
import { scrub } from "../redact.js";
|
|
3
3
|
import { ProjectCredentialStore } from "./auth-store.js";
|
|
4
4
|
import { modelsFor } from "./models.js";
|
|
5
5
|
import { modelSelection, projectSecrets } from "./settings.js";
|
|
@@ -11,12 +11,19 @@ const emptyUsage = () => ({
|
|
|
11
11
|
cacheWrite: 0,
|
|
12
12
|
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
|
13
13
|
});
|
|
14
|
-
/**
|
|
14
|
+
/** Node model adapter. Local provider configuration is read only when invoked. */
|
|
15
15
|
export function piModel(options = {}) {
|
|
16
16
|
return async (call, context) => {
|
|
17
17
|
context.signal.throwIfAborted();
|
|
18
18
|
const root = options.root ?? process.cwd();
|
|
19
|
-
const
|
|
19
|
+
const configured = options.selection ?? modelSelection(root);
|
|
20
|
+
const requested = call.model?.id;
|
|
21
|
+
const selection = {
|
|
22
|
+
...configured,
|
|
23
|
+
model: requested?.startsWith(`${configured.provider}/`)
|
|
24
|
+
? requested.slice(configured.provider.length + 1)
|
|
25
|
+
: (requested ?? configured.model),
|
|
26
|
+
};
|
|
20
27
|
const registry = modelsFor(selection, new ProjectCredentialStore(join(root, ".nylorun", "auth.json"), join(root, ".env", "auth.json")));
|
|
21
28
|
const selected = registry.getModel(selection.provider, selection.model);
|
|
22
29
|
if (!selected)
|
|
@@ -65,7 +72,9 @@ export function piModel(options = {}) {
|
|
|
65
72
|
typeof ref.agentId !== "string" ||
|
|
66
73
|
typeof ref.assetId !== "string")
|
|
67
74
|
throw new Error("Expected a local media reference.");
|
|
68
|
-
const asset = await options.media?.dataUrl({ agentId: ref.agentId, assetId: ref.assetId },
|
|
75
|
+
const asset = await options.media?.dataUrl({ agentId: ref.agentId, assetId: ref.assetId }, "sessionId" in ref && typeof ref.sessionId === "string"
|
|
76
|
+
? ref.sessionId
|
|
77
|
+
: call.executionId);
|
|
69
78
|
if (!asset)
|
|
70
79
|
throw new Error("Media is unavailable; pass the shared media adapter to piModel({ media }).");
|
|
71
80
|
const comma = asset.url.indexOf(",");
|
|
@@ -156,7 +165,14 @@ export function piModel(options = {}) {
|
|
|
156
165
|
parameters: { ...tool.inputSchema },
|
|
157
166
|
}));
|
|
158
167
|
const request = {
|
|
159
|
-
systemPrompt:
|
|
168
|
+
systemPrompt: [
|
|
169
|
+
...instructions,
|
|
170
|
+
...(call.outputSchema
|
|
171
|
+
? [
|
|
172
|
+
`Return the final answer as JSON matching this schema: ${JSON.stringify(call.outputSchema)}. Use tools when needed before returning the final JSON.`,
|
|
173
|
+
]
|
|
174
|
+
: []),
|
|
175
|
+
].join("\n"),
|
|
160
176
|
messages,
|
|
161
177
|
tools,
|
|
162
178
|
};
|
|
@@ -167,14 +183,34 @@ export function piModel(options = {}) {
|
|
|
167
183
|
call: scrub(call, secrets),
|
|
168
184
|
});
|
|
169
185
|
try {
|
|
170
|
-
const
|
|
186
|
+
const invocationOptions = {
|
|
171
187
|
signal: context.signal,
|
|
172
188
|
temperature: call.model?.controls?.temperature,
|
|
173
189
|
maxTokens: call.model?.controls?.maxOutputTokens,
|
|
174
190
|
...(call.model?.config
|
|
175
191
|
? { samplingParams: { ...call.model.config } }
|
|
176
192
|
: {}),
|
|
177
|
-
}
|
|
193
|
+
};
|
|
194
|
+
let response;
|
|
195
|
+
if (options.onPreview) {
|
|
196
|
+
const stream = registry.streamSimple(selected, request, invocationOptions);
|
|
197
|
+
for await (const event of stream) {
|
|
198
|
+
if (event.type === "text_delta") {
|
|
199
|
+
try {
|
|
200
|
+
void Promise.resolve(options.onPreview({
|
|
201
|
+
invocationId: context.invocationId,
|
|
202
|
+
text: event.delta,
|
|
203
|
+
})).catch(() => { });
|
|
204
|
+
}
|
|
205
|
+
catch {
|
|
206
|
+
/* Preview delivery is independent. */
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
response = await stream.result();
|
|
211
|
+
}
|
|
212
|
+
else
|
|
213
|
+
response = await registry.complete(selected, request, invocationOptions);
|
|
178
214
|
context.signal.throwIfAborted();
|
|
179
215
|
if (response.stopReason === "error" ||
|
|
180
216
|
response.stopReason === "aborted" ||
|
|
@@ -203,6 +239,15 @@ export function piModel(options = {}) {
|
|
|
203
239
|
...metadataFor(part.thoughtSignature),
|
|
204
240
|
});
|
|
205
241
|
}
|
|
242
|
+
if (call.outputSchema &&
|
|
243
|
+
!output.some((part) => part.type === "tool-call")) {
|
|
244
|
+
const text = output
|
|
245
|
+
.filter((part) => part.type === "text")
|
|
246
|
+
.map((part) => part.text)
|
|
247
|
+
.join("");
|
|
248
|
+
const value = JSON.parse(text);
|
|
249
|
+
output.splice(0, output.length, { type: "json", value });
|
|
250
|
+
}
|
|
206
251
|
return {
|
|
207
252
|
output,
|
|
208
253
|
finishReason: response.stopReason === "toolUse"
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { localSessions } from "./local-sessions.js";
|
|
2
|
+
export { localMedia, MediaStore } from "../adapters/media.js";
|
|
3
|
+
export { jsonlObserver } from "../adapters/observe.js";
|
|
4
|
+
export { piModel, type PiModelOptions } from "../model/pi-model.js";
|
|
5
|
+
export { projectAsset } from "../assets.js";
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { localSessions } from "./local-sessions.js";
|
|
2
|
+
export { localMedia, MediaStore } from "../adapters/media.js";
|
|
3
|
+
export { jsonlObserver } from "../adapters/observe.js";
|
|
4
|
+
export { piModel } from "../model/pi-model.js";
|
|
5
|
+
export { projectAsset } from "../assets.js";
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import { mkdir, open, readFile, readdir, rename, rm, realpath, } from "node:fs/promises";
|
|
2
|
+
import { resolve, join } from "node:path";
|
|
3
|
+
import { randomUUID } from "node:crypto";
|
|
4
|
+
import { sessionSummary, } from "../sessions/store.js";
|
|
5
|
+
/** Single-owner local disk store. No lease stealing, replay, or shared-filesystem guarantee. */
|
|
6
|
+
export function localSessions(options) {
|
|
7
|
+
let closed = false;
|
|
8
|
+
let directory;
|
|
9
|
+
const token = randomUUID();
|
|
10
|
+
const acquired = (async () => {
|
|
11
|
+
await mkdir(resolve(options.root), { recursive: true, mode: 0o700 });
|
|
12
|
+
directory = await realpath(resolve(options.root));
|
|
13
|
+
const path = join(directory, ".owner.lock");
|
|
14
|
+
let handle;
|
|
15
|
+
try {
|
|
16
|
+
handle = await open(path, "wx", 0o600);
|
|
17
|
+
}
|
|
18
|
+
catch (cause) {
|
|
19
|
+
throw new Error(`Session store ${directory} already has an owner. Stop the owner before opening it again. After a crash, confirm it has stopped and manually remove .owner.lock.`, { cause });
|
|
20
|
+
}
|
|
21
|
+
try {
|
|
22
|
+
await handle.writeFile(JSON.stringify({ token, pid: process.pid }));
|
|
23
|
+
await handle.sync();
|
|
24
|
+
}
|
|
25
|
+
finally {
|
|
26
|
+
await handle.close();
|
|
27
|
+
}
|
|
28
|
+
})();
|
|
29
|
+
void acquired.catch(() => { });
|
|
30
|
+
let operations = Promise.resolve();
|
|
31
|
+
const check = async () => {
|
|
32
|
+
await acquired;
|
|
33
|
+
if (closed)
|
|
34
|
+
throw new Error("Session store is closed");
|
|
35
|
+
const owner = JSON.parse(await readFile(join(directory, ".owner.lock"), "utf8"));
|
|
36
|
+
if (owner.token !== token)
|
|
37
|
+
throw new Error("Session store ownership was lost; refusing further writes");
|
|
38
|
+
};
|
|
39
|
+
const pathFor = (agentId, sessionId) => join(directory, safe(agentId), `${safe(sessionId)}.json`);
|
|
40
|
+
const get = async (agentId, sessionId) => {
|
|
41
|
+
await check();
|
|
42
|
+
try {
|
|
43
|
+
const value = JSON.parse(await readFile(pathFor(agentId, sessionId), "utf8"));
|
|
44
|
+
if (value.version !== 1 ||
|
|
45
|
+
value.id !== sessionId ||
|
|
46
|
+
value.agentId !== agentId ||
|
|
47
|
+
!Array.isArray(value.events))
|
|
48
|
+
throw new Error("Invalid stored session document");
|
|
49
|
+
return value;
|
|
50
|
+
}
|
|
51
|
+
catch (error) {
|
|
52
|
+
if (!isMissing(error))
|
|
53
|
+
throw error;
|
|
54
|
+
try {
|
|
55
|
+
const contents = await readFile(join(directory, safe(agentId), safe(sessionId), "events.jsonl"), "utf8");
|
|
56
|
+
const events = contents
|
|
57
|
+
.split("\n")
|
|
58
|
+
.filter(Boolean)
|
|
59
|
+
.map((line) => JSON.parse(line));
|
|
60
|
+
return {
|
|
61
|
+
version: 1,
|
|
62
|
+
id: sessionId,
|
|
63
|
+
agentId,
|
|
64
|
+
status: "archived",
|
|
65
|
+
startedAt: events[0] ? Date.parse(events[0].ts) : 0,
|
|
66
|
+
updatedAt: 0,
|
|
67
|
+
events,
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
catch (legacyError) {
|
|
71
|
+
if (isMissing(legacyError))
|
|
72
|
+
return undefined;
|
|
73
|
+
throw legacyError;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
return {
|
|
78
|
+
get,
|
|
79
|
+
put(agentId, sessionId, session) {
|
|
80
|
+
const contents = JSON.stringify(session);
|
|
81
|
+
const operation = operations.then(async () => {
|
|
82
|
+
await check();
|
|
83
|
+
if (session.id !== sessionId || session.agentId !== agentId)
|
|
84
|
+
throw new Error("Session identity mismatch");
|
|
85
|
+
const target = pathFor(agentId, sessionId);
|
|
86
|
+
await mkdir(join(directory, safe(agentId)), {
|
|
87
|
+
recursive: true,
|
|
88
|
+
mode: 0o700,
|
|
89
|
+
});
|
|
90
|
+
const temporary = `${target}.${randomUUID()}.tmp`;
|
|
91
|
+
try {
|
|
92
|
+
const file = await open(temporary, "wx", 0o600);
|
|
93
|
+
try {
|
|
94
|
+
await file.writeFile(contents);
|
|
95
|
+
await file.sync();
|
|
96
|
+
}
|
|
97
|
+
finally {
|
|
98
|
+
await file.close();
|
|
99
|
+
}
|
|
100
|
+
await check();
|
|
101
|
+
await rename(temporary, target);
|
|
102
|
+
const parent = await open(join(directory, safe(agentId)), "r");
|
|
103
|
+
try {
|
|
104
|
+
await parent.sync();
|
|
105
|
+
}
|
|
106
|
+
finally {
|
|
107
|
+
await parent.close();
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
finally {
|
|
111
|
+
await rm(temporary, { force: true });
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
operations = operation.catch(() => { });
|
|
115
|
+
return operation;
|
|
116
|
+
},
|
|
117
|
+
async list(agentId) {
|
|
118
|
+
await check();
|
|
119
|
+
let names;
|
|
120
|
+
try {
|
|
121
|
+
names = await readdir(join(directory, safe(agentId)), {
|
|
122
|
+
withFileTypes: true,
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
catch (error) {
|
|
126
|
+
if (isMissing(error))
|
|
127
|
+
return [];
|
|
128
|
+
throw error;
|
|
129
|
+
}
|
|
130
|
+
const sessions = await Promise.all(names
|
|
131
|
+
.filter((item) => item.isDirectory() || item.name.endsWith(".json"))
|
|
132
|
+
.map((item) => get(agentId, item.isDirectory() ? item.name : item.name.slice(0, -5))));
|
|
133
|
+
return sessions
|
|
134
|
+
.flatMap((session) => (session ? [sessionSummary(session)] : []))
|
|
135
|
+
.sort((a, b) => b.startedAt - a.startedAt);
|
|
136
|
+
},
|
|
137
|
+
async delete(agentId, sessionId) {
|
|
138
|
+
const operation = operations.then(async () => {
|
|
139
|
+
await check();
|
|
140
|
+
const target = pathFor(agentId, sessionId);
|
|
141
|
+
try {
|
|
142
|
+
await rm(target);
|
|
143
|
+
return true;
|
|
144
|
+
}
|
|
145
|
+
catch (error) {
|
|
146
|
+
if (isMissing(error))
|
|
147
|
+
return false;
|
|
148
|
+
throw error;
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
operations = operation.then(() => { }, () => { });
|
|
152
|
+
return operation;
|
|
153
|
+
},
|
|
154
|
+
async close() {
|
|
155
|
+
await operations;
|
|
156
|
+
if (closed)
|
|
157
|
+
return;
|
|
158
|
+
await check();
|
|
159
|
+
closed = true;
|
|
160
|
+
await rm(join(directory, ".owner.lock"));
|
|
161
|
+
},
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
function safe(value) {
|
|
165
|
+
if (value === "." || value === ".." || !/^[a-zA-Z0-9._-]+$/.test(value))
|
|
166
|
+
throw new Error("Invalid session path identifier");
|
|
167
|
+
return value;
|
|
168
|
+
}
|
|
169
|
+
function isMissing(error) {
|
|
170
|
+
return (!!error &&
|
|
171
|
+
typeof error === "object" &&
|
|
172
|
+
"code" in error &&
|
|
173
|
+
error.code === "ENOENT");
|
|
174
|
+
}
|
package/dist/redact.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function scrub(value: unknown, secrets: readonly string[]): unknown;
|
package/dist/redact.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export function scrub(value, secrets) {
|
|
2
|
+
if (typeof value === "string")
|
|
3
|
+
return secrets.reduce((text, secret) => secret.length >= 8 ? text.split(secret).join("[redacted]") : text, value.replace(/data:image\/[a-z0-9.+-]+;base64,[A-Za-z0-9+/=]+/giu, "[inline image data redacted]"));
|
|
4
|
+
if (Array.isArray(value))
|
|
5
|
+
return value.map((item) => scrub(item, secrets));
|
|
6
|
+
if (value && typeof value === "object")
|
|
7
|
+
return Object.fromEntries(Object.entries(value).map(([key, item]) => [
|
|
8
|
+
key,
|
|
9
|
+
/^(authorization|api[_-]?key|(?:access|refresh|id|auth)[_-]?token|token|secret|password|cookie|credentials?)$/iu.test(key)
|
|
10
|
+
? "[redacted]"
|
|
11
|
+
: scrub(item, secrets),
|
|
12
|
+
]));
|
|
13
|
+
return value;
|
|
14
|
+
}
|
package/dist/server/ag-ui.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { CanonicalEvent } from "../
|
|
1
|
+
import type { CanonicalEvent } from "../sessions/store.js";
|
|
2
2
|
/** Minimal truthful AG-UI projection from the same canonical events Studio displays. */
|
|
3
3
|
export declare function agUiEvents(events: readonly CanonicalEvent[], threadId: string, runId: string): readonly Record<string, unknown>[];
|
|
4
4
|
/**
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/** Bounded per-request delivery. Generation never waits for a subscriber. */
|
|
2
|
+
export declare class EventDelivery {
|
|
3
|
+
private readonly abort;
|
|
4
|
+
private readonly limits;
|
|
5
|
+
readonly response: Response;
|
|
6
|
+
private controller;
|
|
7
|
+
private readonly queue;
|
|
8
|
+
private readonly suppressed;
|
|
9
|
+
private previewBytes;
|
|
10
|
+
private readonly previewTotals;
|
|
11
|
+
private eventBytes;
|
|
12
|
+
private eventCount;
|
|
13
|
+
private ended;
|
|
14
|
+
private closed;
|
|
15
|
+
constructor(abort: () => void, limits?: {
|
|
16
|
+
previewBytes?: number;
|
|
17
|
+
eventBytes?: number;
|
|
18
|
+
eventCount?: number;
|
|
19
|
+
}, inherited?: HeadersInit);
|
|
20
|
+
private waiting;
|
|
21
|
+
private flush;
|
|
22
|
+
push(event: Record<string, unknown>, preview?: string): void;
|
|
23
|
+
end(): void;
|
|
24
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/** Bounded per-request delivery. Generation never waits for a subscriber. */
|
|
2
|
+
export class EventDelivery {
|
|
3
|
+
abort;
|
|
4
|
+
limits;
|
|
5
|
+
response;
|
|
6
|
+
controller;
|
|
7
|
+
queue = [];
|
|
8
|
+
suppressed = new Set();
|
|
9
|
+
previewBytes = 0;
|
|
10
|
+
previewTotals = new Map();
|
|
11
|
+
eventBytes = 0;
|
|
12
|
+
eventCount = 0;
|
|
13
|
+
ended = false;
|
|
14
|
+
closed = false;
|
|
15
|
+
constructor(abort, limits = {}, inherited) {
|
|
16
|
+
this.abort = abort;
|
|
17
|
+
this.limits = limits;
|
|
18
|
+
for (const value of Object.values(limits))
|
|
19
|
+
if (!Number.isSafeInteger(value) || value <= 0)
|
|
20
|
+
throw new Error("Delivery limits must be positive integers");
|
|
21
|
+
const stream = new ReadableStream({
|
|
22
|
+
start: (controller) => {
|
|
23
|
+
this.controller = controller;
|
|
24
|
+
},
|
|
25
|
+
pull: () => this.flush(),
|
|
26
|
+
cancel: () => {
|
|
27
|
+
this.closed = true;
|
|
28
|
+
this.queue.length = 0;
|
|
29
|
+
abort();
|
|
30
|
+
},
|
|
31
|
+
}, { highWaterMark: 0 });
|
|
32
|
+
const headers = new Headers(inherited);
|
|
33
|
+
headers.set("content-type", "text/event-stream; charset=utf-8");
|
|
34
|
+
headers.set("cache-control", "no-cache");
|
|
35
|
+
this.response = new Response(stream, { headers });
|
|
36
|
+
}
|
|
37
|
+
waiting = false;
|
|
38
|
+
flush() {
|
|
39
|
+
if (this.closed)
|
|
40
|
+
return;
|
|
41
|
+
const item = this.queue.shift();
|
|
42
|
+
if (item) {
|
|
43
|
+
this.waiting = false;
|
|
44
|
+
if (item.preview)
|
|
45
|
+
this.previewBytes -= item.bytes.byteLength;
|
|
46
|
+
else {
|
|
47
|
+
this.eventBytes -= item.bytes.byteLength;
|
|
48
|
+
this.eventCount--;
|
|
49
|
+
}
|
|
50
|
+
this.controller.enqueue(item.bytes);
|
|
51
|
+
}
|
|
52
|
+
else if (this.ended) {
|
|
53
|
+
this.closed = true;
|
|
54
|
+
this.controller.close();
|
|
55
|
+
}
|
|
56
|
+
else
|
|
57
|
+
this.waiting = true;
|
|
58
|
+
}
|
|
59
|
+
push(event, preview) {
|
|
60
|
+
if (this.closed || this.ended || (preview && this.suppressed.has(preview)))
|
|
61
|
+
return;
|
|
62
|
+
const bytes = new TextEncoder().encode(`data: ${JSON.stringify(event)}\n\n`);
|
|
63
|
+
if (preview &&
|
|
64
|
+
Math.max(this.previewBytes, this.previewTotals.get(preview) ?? 0) +
|
|
65
|
+
bytes.byteLength >
|
|
66
|
+
(this.limits.previewBytes ?? 64 * 1024)) {
|
|
67
|
+
this.suppressed.add(preview);
|
|
68
|
+
for (let i = this.queue.length - 1; i >= 0; i--)
|
|
69
|
+
if (this.queue[i].preview === preview) {
|
|
70
|
+
this.previewBytes -= this.queue[i].bytes.byteLength;
|
|
71
|
+
this.queue.splice(i, 1);
|
|
72
|
+
}
|
|
73
|
+
this.push({
|
|
74
|
+
type: "CUSTOM",
|
|
75
|
+
name: "nylorun.preview.incomplete",
|
|
76
|
+
value: { invocationId: preview },
|
|
77
|
+
});
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
if (!preview &&
|
|
81
|
+
(this.eventCount + 1 > (this.limits.eventCount ?? 256) ||
|
|
82
|
+
this.eventBytes + bytes.byteLength >
|
|
83
|
+
(this.limits.eventBytes ?? 1024 * 1024))) {
|
|
84
|
+
this.closed = true;
|
|
85
|
+
this.queue.length = 0;
|
|
86
|
+
this.controller.error(new Error("Subscriber exceeded execution-event delivery limits"));
|
|
87
|
+
this.abort();
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
this.queue.push({ bytes, ...(preview ? { preview } : {}) });
|
|
91
|
+
if (preview) {
|
|
92
|
+
this.previewBytes += bytes.byteLength;
|
|
93
|
+
this.previewTotals.set(preview, (this.previewTotals.get(preview) ?? 0) + bytes.byteLength);
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
this.eventBytes += bytes.byteLength;
|
|
97
|
+
this.eventCount++;
|
|
98
|
+
}
|
|
99
|
+
if (this.waiting)
|
|
100
|
+
this.flush();
|
|
101
|
+
}
|
|
102
|
+
end() {
|
|
103
|
+
this.ended = true;
|
|
104
|
+
if (this.waiting)
|
|
105
|
+
this.flush();
|
|
106
|
+
}
|
|
107
|
+
}
|
package/dist/server/host.d.ts
CHANGED
|
@@ -1,27 +1,70 @@
|
|
|
1
1
|
import { Hono, type Context } from "hono";
|
|
2
2
|
import type { JsonValue, RuntimeAgent } from "../contracts.js";
|
|
3
|
+
import type { BuiltAgent, ModelAdapter } from "@nylorun/core/define";
|
|
4
|
+
import { SessionHost } from "../sessions/host.js";
|
|
3
5
|
import type { RuntimeConfig } from "../config.js";
|
|
6
|
+
import type { ModelEnvironment } from "../model/http-model.js";
|
|
7
|
+
import { type OpenSessionOptions, type SessionHandle } from "../session/handle.js";
|
|
4
8
|
export type RuntimeActor = Readonly<{
|
|
5
9
|
id: string;
|
|
6
10
|
context?: Record<string, JsonValue>;
|
|
7
11
|
}>;
|
|
8
12
|
export type AgentRouterOptions = Readonly<{
|
|
9
|
-
/** Override the public URL prefix; defaults to the current Hono mount path. */
|
|
10
13
|
basePath?: string;
|
|
11
14
|
getActor?: (context: Context) => RuntimeActor | undefined | Promise<RuntimeActor | undefined>;
|
|
15
|
+
getInfo?: (context: Context) => unknown | Promise<unknown>;
|
|
16
|
+
getEnvironment?: (context: Context) => ModelEnvironment | Promise<ModelEnvironment>;
|
|
12
17
|
getRequestMetadata?: (context: Context) => Record<string, JsonValue> | undefined | Promise<Record<string, JsonValue> | undefined>;
|
|
13
18
|
}>;
|
|
14
|
-
|
|
19
|
+
/** 1.0 compatibility: requires an explicit Runtime and returns a Hono app. */
|
|
20
|
+
export type ServeAgentsCompatOptions = AgentRouterOptions & {
|
|
15
21
|
readonly agents: readonly RuntimeAgent[];
|
|
16
22
|
readonly runtime: Runtime;
|
|
17
23
|
};
|
|
24
|
+
/** DX v5.6 local: optional Runtime; returns `{ fetch }`. */
|
|
25
|
+
export type ServeAgentsFetchOptions = AgentRouterOptions & {
|
|
26
|
+
readonly agents?: readonly RuntimeAgent[];
|
|
27
|
+
readonly runtime?: Runtime;
|
|
28
|
+
/** Alias for `getInfo` — keep the name `info` (never `user`). */
|
|
29
|
+
readonly info?: AgentRouterOptions["getInfo"];
|
|
30
|
+
readonly on?: {
|
|
31
|
+
readonly session?: {
|
|
32
|
+
readonly created?: (session: {
|
|
33
|
+
readonly id: string;
|
|
34
|
+
readonly agentId: string;
|
|
35
|
+
}) => void;
|
|
36
|
+
readonly idle?: (session: {
|
|
37
|
+
readonly id: string;
|
|
38
|
+
readonly agentId: string;
|
|
39
|
+
}) => void;
|
|
40
|
+
readonly failed?: (session: {
|
|
41
|
+
readonly id: string;
|
|
42
|
+
readonly agentId: string;
|
|
43
|
+
}) => void;
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
export type ServeAgentsOptions = ServeAgentsCompatOptions;
|
|
48
|
+
export type ServeAgentsFetch = {
|
|
49
|
+
readonly fetch: (request: Request) => Response | Promise<Response>;
|
|
50
|
+
};
|
|
18
51
|
declare const kServe: unique symbol;
|
|
19
52
|
export declare class Runtime {
|
|
20
|
-
|
|
21
|
-
|
|
53
|
+
readonly config: RuntimeConfig;
|
|
54
|
+
readonly host: SessionHost;
|
|
55
|
+
private served;
|
|
56
|
+
private closing?;
|
|
57
|
+
constructor(config?: RuntimeConfig);
|
|
22
58
|
close: () => Promise<void>;
|
|
23
|
-
|
|
59
|
+
resolveModel(onPreview?: (preview: import("../model/defaults.js").ModelPreview) => void): ModelAdapter;
|
|
60
|
+
openSession(agent: BuiltAgent<any, any>, options?: OpenSessionOptions): SessionHandle;
|
|
61
|
+
listSessions(agentId: string): Promise<readonly import("../sessions/store.js").SessionSummary[]>;
|
|
62
|
+
getSession(agentId: string, sessionId: string): Promise<import("../sessions/store.js").StoredSession | undefined>;
|
|
63
|
+
deleteSession(agentId: string, sessionId: string): Promise<boolean>;
|
|
64
|
+
[kServe](options: Omit<ServeAgentsCompatOptions, "runtime"> & {
|
|
65
|
+
readonly agents: readonly RuntimeAgent[];
|
|
66
|
+
}): Hono<any>;
|
|
24
67
|
}
|
|
25
|
-
|
|
26
|
-
export declare function serveAgents(options:
|
|
68
|
+
export declare function serveAgents(options: ServeAgentsCompatOptions): Hono<any>;
|
|
69
|
+
export declare function serveAgents(options: ServeAgentsFetchOptions): ServeAgentsFetch;
|
|
27
70
|
export {};
|