@nylorun/runtime 0.4.0-beta → 0.5.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 +25 -0
- package/README.md +29 -15
- 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/contracts.d.ts +3 -166
- package/dist/index.d.ts +8 -8
- package/dist/index.js +5 -5
- package/dist/launcher.js +6 -1
- 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 +157 -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 +11 -6
- package/dist/server/host.js +236 -305
- package/dist/sessions/host.d.ts +29 -0
- package/dist/sessions/host.js +291 -0
- package/dist/sessions/store.d.ts +40 -0
- package/dist/sessions/store.js +30 -0
- package/package.json +10 -4
- package/dist/adapters/journal.d.ts +0 -35
- package/dist/adapters/journal.js +0 -130
|
@@ -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,14 +1,17 @@
|
|
|
1
1
|
import { Hono, type Context } from "hono";
|
|
2
2
|
import type { JsonValue, RuntimeAgent } from "../contracts.js";
|
|
3
|
+
import { SessionHost } from "../sessions/host.js";
|
|
3
4
|
import type { RuntimeConfig } from "../config.js";
|
|
5
|
+
import type { ModelEnvironment } from "../model/http-model.js";
|
|
4
6
|
export type RuntimeActor = Readonly<{
|
|
5
7
|
id: string;
|
|
6
8
|
context?: Record<string, JsonValue>;
|
|
7
9
|
}>;
|
|
8
10
|
export type AgentRouterOptions = Readonly<{
|
|
9
|
-
/** Override the public URL prefix; defaults to the current Hono mount path. */
|
|
10
11
|
basePath?: string;
|
|
11
12
|
getActor?: (context: Context) => RuntimeActor | undefined | Promise<RuntimeActor | undefined>;
|
|
13
|
+
getInfo?: (context: Context) => unknown | Promise<unknown>;
|
|
14
|
+
getEnvironment?: (context: Context) => ModelEnvironment | Promise<ModelEnvironment>;
|
|
12
15
|
getRequestMetadata?: (context: Context) => Record<string, JsonValue> | undefined | Promise<Record<string, JsonValue> | undefined>;
|
|
13
16
|
}>;
|
|
14
17
|
export type ServeAgentsOptions = AgentRouterOptions & {
|
|
@@ -17,11 +20,13 @@ export type ServeAgentsOptions = AgentRouterOptions & {
|
|
|
17
20
|
};
|
|
18
21
|
declare const kServe: unique symbol;
|
|
19
22
|
export declare class Runtime {
|
|
20
|
-
|
|
21
|
-
|
|
23
|
+
private readonly config;
|
|
24
|
+
readonly host: SessionHost;
|
|
25
|
+
private served;
|
|
26
|
+
private closing?;
|
|
27
|
+
constructor(config?: RuntimeConfig);
|
|
22
28
|
close: () => Promise<void>;
|
|
23
|
-
[kServe](options: Omit<ServeAgentsOptions, "runtime">): Hono
|
|
29
|
+
[kServe](options: Omit<ServeAgentsOptions, "runtime">): Hono<any>;
|
|
24
30
|
}
|
|
25
|
-
|
|
26
|
-
export declare function serveAgents(options: ServeAgentsOptions): any;
|
|
31
|
+
export declare function serveAgents(options: ServeAgentsOptions): Hono<any>;
|
|
27
32
|
export {};
|