@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
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { BuiltAgent } from "@nylorun/core/define";
|
|
2
|
+
import { setDefaultRuntime } from "./default.js";
|
|
3
|
+
import type { OpenSessionOptions, SessionHandle } from "./handle.js";
|
|
4
|
+
export { setDefaultRuntime };
|
|
5
|
+
export type { OpenSessionOptions, SessionHandle };
|
|
6
|
+
/** Open-or-create a local session with an agent (bound to the default Runtime). */
|
|
7
|
+
export declare function openSession(agent: BuiltAgent<any, any>, options?: OpenSessionOptions): SessionHandle;
|
|
8
|
+
export declare function listSessions(agentId: string): Promise<readonly import("../sessions/store.js").SessionSummary[]>;
|
|
9
|
+
export declare function getSession(agentId: string, sessionId: string): Promise<import("../sessions/store.js").StoredSession | undefined>;
|
|
10
|
+
export declare function deleteSession(agentId: string, sessionId: string): Promise<boolean>;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { getDefaultRuntime, setDefaultRuntime, } from "./default.js";
|
|
2
|
+
export { setDefaultRuntime };
|
|
3
|
+
/** Open-or-create a local session with an agent (bound to the default Runtime). */
|
|
4
|
+
export function openSession(agent, options) {
|
|
5
|
+
return getDefaultRuntime().openSession(agent, options);
|
|
6
|
+
}
|
|
7
|
+
export async function listSessions(agentId) {
|
|
8
|
+
return getDefaultRuntime().listSessions(agentId);
|
|
9
|
+
}
|
|
10
|
+
export async function getSession(agentId, sessionId) {
|
|
11
|
+
return getDefaultRuntime().getSession(agentId, sessionId);
|
|
12
|
+
}
|
|
13
|
+
export async function deleteSession(agentId, sessionId) {
|
|
14
|
+
return getDefaultRuntime().deleteSession(agentId, sessionId);
|
|
15
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { Runtime } from "../server/host.js";
|
|
2
|
+
/** Wired from `server/host.ts` after `Runtime` is defined (avoids init cycles). */
|
|
3
|
+
export declare function installDefaultRuntimeFactory(factory: () => Runtime): void;
|
|
4
|
+
export declare function setDefaultRuntime(runtime: Runtime | undefined): void;
|
|
5
|
+
export declare function getDefaultRuntime(): Runtime;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
let defaultRuntime;
|
|
2
|
+
let bannerShown = false;
|
|
3
|
+
let createRuntime;
|
|
4
|
+
/** Wired from `server/host.ts` after `Runtime` is defined (avoids init cycles). */
|
|
5
|
+
export function installDefaultRuntimeFactory(factory) {
|
|
6
|
+
createRuntime = factory;
|
|
7
|
+
}
|
|
8
|
+
export function setDefaultRuntime(runtime) {
|
|
9
|
+
defaultRuntime = runtime;
|
|
10
|
+
}
|
|
11
|
+
export function getDefaultRuntime() {
|
|
12
|
+
if (defaultRuntime)
|
|
13
|
+
return defaultRuntime;
|
|
14
|
+
if (!createRuntime)
|
|
15
|
+
throw new Error("Default Runtime factory is not installed");
|
|
16
|
+
defaultRuntime = createRuntime();
|
|
17
|
+
maybeBanner();
|
|
18
|
+
return defaultRuntime;
|
|
19
|
+
}
|
|
20
|
+
function maybeBanner() {
|
|
21
|
+
if (bannerShown || typeof process === "undefined")
|
|
22
|
+
return;
|
|
23
|
+
if (process.env.NYLORUN_QUIET === "1")
|
|
24
|
+
return;
|
|
25
|
+
bannerShown = true;
|
|
26
|
+
const model = process.env.MODEL_PROVIDER && process.env.MODEL
|
|
27
|
+
? `${process.env.MODEL_PROVIDER}/${process.env.MODEL}`
|
|
28
|
+
: "unset";
|
|
29
|
+
console.info(`nylorun ▸ local runtime · loop runs in this process · sessions in memory · model ${model} (your key)`);
|
|
30
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { BuiltAgent, JsonObject, JsonValue, ModelAdapter } from "@nylorun/core/define";
|
|
2
|
+
import type { Session } from "@nylorun/harness";
|
|
3
|
+
import type { RuntimeConfig } from "../config.js";
|
|
4
|
+
import type { SessionHost } from "../sessions/host.js";
|
|
5
|
+
export type OpenSessionOptions = {
|
|
6
|
+
readonly id?: string;
|
|
7
|
+
/** Trusted host bag — keep the name `info` (never `user`). */
|
|
8
|
+
readonly info?: unknown;
|
|
9
|
+
/** Initial durable session memory (`ExecutionState.state`). */
|
|
10
|
+
readonly state?: JsonObject;
|
|
11
|
+
};
|
|
12
|
+
export type SessionHandle<Output = unknown> = Session<{
|
|
13
|
+
readonly id: string;
|
|
14
|
+
}, Output> & {
|
|
15
|
+
readonly agentId: string;
|
|
16
|
+
};
|
|
17
|
+
export type SessionRuntime = {
|
|
18
|
+
readonly host: SessionHost;
|
|
19
|
+
readonly config: RuntimeConfig;
|
|
20
|
+
resolveModel(): ModelAdapter;
|
|
21
|
+
};
|
|
22
|
+
export declare function createSessionHandle(runtime: SessionRuntime, agent: BuiltAgent<any, any>, options?: OpenSessionOptions): SessionHandle;
|
|
23
|
+
/** Resolve a wait by harness waitId (H6). */
|
|
24
|
+
export declare function resolveWait(runtime: SessionRuntime, agent: BuiltAgent<any, any>, sessionId: string, waitId: string, value?: JsonValue, options?: {
|
|
25
|
+
readonly info?: unknown;
|
|
26
|
+
readonly onModelCall?: ModelAdapter;
|
|
27
|
+
}): Promise<void>;
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
export function createSessionHandle(runtime, agent, options = {}) {
|
|
2
|
+
const id = options.id ?? crypto.randomUUID();
|
|
3
|
+
let seedState = options.state;
|
|
4
|
+
const info = options.info;
|
|
5
|
+
const submitOptions = () => {
|
|
6
|
+
const sessionState = seedState;
|
|
7
|
+
seedState = undefined;
|
|
8
|
+
return {
|
|
9
|
+
info: info === undefined
|
|
10
|
+
? { sessionId: id }
|
|
11
|
+
: { ...info, sessionId: id },
|
|
12
|
+
onModelCall: runtime.resolveModel(),
|
|
13
|
+
...(sessionState ? { sessionState } : {}),
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
const handle = {
|
|
17
|
+
id,
|
|
18
|
+
agentId: agent.id,
|
|
19
|
+
async input(message) {
|
|
20
|
+
try {
|
|
21
|
+
const result = await runtime.host.submit(agent, id, message, submitOptions());
|
|
22
|
+
const cursor = String((await runtime.host.read(agent.id, id))?.events.at(-1)?.seq ?? 0);
|
|
23
|
+
return {
|
|
24
|
+
status: "accepted",
|
|
25
|
+
turnId: result.state.plan?.turnId ?? result.state.executionId,
|
|
26
|
+
cursor,
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
catch (error) {
|
|
30
|
+
return {
|
|
31
|
+
status: "rejected",
|
|
32
|
+
error: {
|
|
33
|
+
code: error && typeof error === "object" && "code" in error
|
|
34
|
+
? String(error.code)
|
|
35
|
+
: "runtime.input-rejected",
|
|
36
|
+
message: error instanceof Error ? error.message : String(error),
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
async *stream(opts) {
|
|
42
|
+
let after = opts?.after ? Number(opts.after) : 0;
|
|
43
|
+
if (!Number.isFinite(after) || after < 0)
|
|
44
|
+
after = 0;
|
|
45
|
+
const queue = [];
|
|
46
|
+
let notify;
|
|
47
|
+
const wake = () => {
|
|
48
|
+
notify?.();
|
|
49
|
+
notify = undefined;
|
|
50
|
+
};
|
|
51
|
+
const unsubscribe = runtime.host.subscribe(agent.id, id, (event) => {
|
|
52
|
+
queue.push(event);
|
|
53
|
+
wake();
|
|
54
|
+
});
|
|
55
|
+
try {
|
|
56
|
+
const existing = await runtime.host.read(agent.id, id);
|
|
57
|
+
for (const event of existing?.events ?? [])
|
|
58
|
+
if (event.seq > after)
|
|
59
|
+
queue.push(event);
|
|
60
|
+
for (;;) {
|
|
61
|
+
while (queue.length) {
|
|
62
|
+
const event = queue.shift();
|
|
63
|
+
if (event.seq <= after)
|
|
64
|
+
continue;
|
|
65
|
+
after = event.seq;
|
|
66
|
+
const mapped = mapEvent(event, id);
|
|
67
|
+
if (mapped)
|
|
68
|
+
yield mapped;
|
|
69
|
+
}
|
|
70
|
+
await new Promise((resolve) => {
|
|
71
|
+
notify = resolve;
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
finally {
|
|
76
|
+
unsubscribe();
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
async history() {
|
|
80
|
+
const document = await runtime.host.read(agent.id, id);
|
|
81
|
+
return (document?.events ?? [])
|
|
82
|
+
.filter((event) => ["final", "interaction.required", "error", "cancelled"].includes(event.type))
|
|
83
|
+
.map((event) => event.payload);
|
|
84
|
+
},
|
|
85
|
+
async approve(interactionId, approved) {
|
|
86
|
+
await runtime.host.submit(agent, id, { kind: "approve", interactionId, approved }, submitOptions());
|
|
87
|
+
},
|
|
88
|
+
async respond(interactionId, value) {
|
|
89
|
+
await runtime.host.submit(agent, id, { kind: "respond", interactionId, value }, submitOptions());
|
|
90
|
+
},
|
|
91
|
+
async cancel() {
|
|
92
|
+
await runtime.host.cancel(agent, id);
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
return handle;
|
|
96
|
+
}
|
|
97
|
+
/** Resolve a wait by harness waitId (H6). */
|
|
98
|
+
export async function resolveWait(runtime, agent, sessionId, waitId, value, options) {
|
|
99
|
+
const input = {
|
|
100
|
+
kind: "wait-resolve",
|
|
101
|
+
waitId,
|
|
102
|
+
...(value === undefined ? {} : { value }),
|
|
103
|
+
};
|
|
104
|
+
await runtime.host.submit(agent, sessionId, input, {
|
|
105
|
+
info: options?.info,
|
|
106
|
+
onModelCall: options?.onModelCall ?? runtime.resolveModel(),
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
function mapEvent(event, sessionId) {
|
|
110
|
+
const turnId = typeof event.payload.turnId === "string"
|
|
111
|
+
? event.payload.turnId
|
|
112
|
+
: typeof event.payload.executionId === "string"
|
|
113
|
+
? event.payload.executionId
|
|
114
|
+
: sessionId;
|
|
115
|
+
const cursor = String(event.seq);
|
|
116
|
+
const at = event.ts;
|
|
117
|
+
switch (event.type) {
|
|
118
|
+
case "session.run.started":
|
|
119
|
+
return { type: "turn.started", cursor, at, sessionId, turnId };
|
|
120
|
+
case "tool.started":
|
|
121
|
+
return {
|
|
122
|
+
type: "tool.started",
|
|
123
|
+
tool: String(event.payload.toolName ?? "tool"),
|
|
124
|
+
sessionId,
|
|
125
|
+
turnId,
|
|
126
|
+
};
|
|
127
|
+
case "tool.progress":
|
|
128
|
+
return {
|
|
129
|
+
type: "tool.progress",
|
|
130
|
+
tool: String(event.payload.toolName ?? "tool"),
|
|
131
|
+
sessionId,
|
|
132
|
+
turnId,
|
|
133
|
+
};
|
|
134
|
+
case "tool.completed":
|
|
135
|
+
return {
|
|
136
|
+
type: event.payload.outcome === "failed" ? "tool.failed" : "tool.completed",
|
|
137
|
+
tool: String(event.payload.toolName ?? "tool"),
|
|
138
|
+
sessionId,
|
|
139
|
+
turnId,
|
|
140
|
+
};
|
|
141
|
+
case "interaction.required": {
|
|
142
|
+
const interaction = event.payload.interaction;
|
|
143
|
+
return {
|
|
144
|
+
type: "interaction.required",
|
|
145
|
+
id: String(interaction?.id ?? ""),
|
|
146
|
+
prompt: String(interaction?.prompt ?? ""),
|
|
147
|
+
...(interaction?.metadata ? { options: interaction.metadata } : {}),
|
|
148
|
+
sessionId,
|
|
149
|
+
turnId,
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
case "final":
|
|
153
|
+
return {
|
|
154
|
+
type: "turn.settled",
|
|
155
|
+
result: {
|
|
156
|
+
status: "completed",
|
|
157
|
+
output: event.payload.output,
|
|
158
|
+
},
|
|
159
|
+
sessionId,
|
|
160
|
+
turnId,
|
|
161
|
+
};
|
|
162
|
+
case "error":
|
|
163
|
+
return {
|
|
164
|
+
type: "turn.settled",
|
|
165
|
+
result: {
|
|
166
|
+
status: "failed",
|
|
167
|
+
error: {
|
|
168
|
+
code: String(event.payload.code ?? "execution.failed"),
|
|
169
|
+
message: String(event.payload.message ?? "failed"),
|
|
170
|
+
},
|
|
171
|
+
},
|
|
172
|
+
sessionId,
|
|
173
|
+
turnId,
|
|
174
|
+
};
|
|
175
|
+
case "cancelled":
|
|
176
|
+
return {
|
|
177
|
+
type: "turn.settled",
|
|
178
|
+
result: { status: "cancelled" },
|
|
179
|
+
sessionId,
|
|
180
|
+
turnId,
|
|
181
|
+
};
|
|
182
|
+
default:
|
|
183
|
+
if (event.type === "model.requested" || event.type === "model.completed")
|
|
184
|
+
return {
|
|
185
|
+
type: "status",
|
|
186
|
+
status: "thinking",
|
|
187
|
+
sessionId,
|
|
188
|
+
turnId,
|
|
189
|
+
};
|
|
190
|
+
if (event.type.startsWith("tool."))
|
|
191
|
+
return {
|
|
192
|
+
type: "status",
|
|
193
|
+
status: "calling_tool",
|
|
194
|
+
sessionId,
|
|
195
|
+
turnId,
|
|
196
|
+
};
|
|
197
|
+
return undefined;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { BuiltAgent, JsonObject, ModelAdapter } from "@nylorun/core/define";
|
|
2
|
+
import type { ExecutionInput, ExecutionState, RunResult } from "@nylorun/harness";
|
|
3
|
+
import type { CanonicalEvent, SessionStore, StoredSession, SessionSummary } from "./store.js";
|
|
4
|
+
export interface SubmitOptions {
|
|
5
|
+
readonly runId?: string;
|
|
6
|
+
readonly info?: unknown;
|
|
7
|
+
readonly signal?: AbortSignal;
|
|
8
|
+
readonly onModelCall: ModelAdapter;
|
|
9
|
+
readonly secrets?: readonly string[];
|
|
10
|
+
readonly onEvent?: (event: CanonicalEvent) => void;
|
|
11
|
+
readonly started?: Record<string, unknown>;
|
|
12
|
+
/** Seed `ExecutionState.state` when creating a new checkpoint (openSession). */
|
|
13
|
+
readonly sessionState?: JsonObject;
|
|
14
|
+
}
|
|
15
|
+
/** The built-in single-process coordinator. Storage adapters do not implement execution. */
|
|
16
|
+
export declare class SessionHost {
|
|
17
|
+
readonly store: SessionStore;
|
|
18
|
+
private readonly queues;
|
|
19
|
+
private readonly generations;
|
|
20
|
+
private readonly active;
|
|
21
|
+
private readonly listeners;
|
|
22
|
+
private closed;
|
|
23
|
+
constructor(store: SessionStore);
|
|
24
|
+
private key;
|
|
25
|
+
read(agentId: string, sessionId: string): Promise<StoredSession | undefined>;
|
|
26
|
+
list(agentId: string): Promise<readonly SessionSummary[]>;
|
|
27
|
+
delete(agentId: string, sessionId: string): Promise<boolean>;
|
|
28
|
+
subscribe(agentId: string, sessionId: string, listener: (event: CanonicalEvent) => void): () => void;
|
|
29
|
+
submit(agent: BuiltAgent<any, any>, sessionId: string, input: ExecutionInput, options: SubmitOptions): Promise<RunResult<any>>;
|
|
30
|
+
cancel(agent: BuiltAgent<any, any>, sessionId: string): Promise<void>;
|
|
31
|
+
interrupt(agent: BuiltAgent<any, any>, sessionId: string, input: ExecutionInput, options: SubmitOptions): Promise<RunResult<any>>;
|
|
32
|
+
close(): Promise<void>;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Map DX `wait-resolve` onto harness approve/respond/settle using wait metadata.
|
|
36
|
+
* Engine `ExecutionInput` already includes `wait-resolve`; resume is wired via
|
|
37
|
+
* existing interaction/deferred kinds until full wait-id resume lands.
|
|
38
|
+
*/
|
|
39
|
+
export declare function resolveEngineInput(state: ExecutionState | undefined, input: ExecutionInput): ExecutionInput;
|