@clearideas/agent-runtime-server 0.0.0-stage → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/NOTICE +8 -0
- package/README.md +44 -2
- package/dist/admission.d.ts +19 -0
- package/dist/admission.js +106 -0
- package/dist/answer.d.ts +51 -0
- package/dist/answer.js +100 -0
- package/dist/auth.d.ts +49 -0
- package/dist/auth.js +89 -0
- package/dist/config.d.ts +350 -0
- package/dist/config.js +543 -0
- package/dist/convert.d.ts +46 -0
- package/dist/convert.js +180 -0
- package/dist/errors.d.ts +36 -0
- package/dist/errors.js +100 -0
- package/dist/host.d.ts +150 -0
- package/dist/host.js +359 -0
- package/dist/http.d.ts +29 -0
- package/dist/http.js +243 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +9 -0
- package/dist/kv.d.ts +51 -0
- package/dist/kv.js +144 -0
- package/dist/ledger.d.ts +59 -0
- package/dist/ledger.js +94 -0
- package/dist/media.d.ts +37 -0
- package/dist/media.js +136 -0
- package/dist/openai.d.ts +180 -0
- package/dist/openai.js +172 -0
- package/dist/serve.d.ts +61 -0
- package/dist/serve.js +201 -0
- package/dist/service.d.ts +85 -0
- package/dist/service.js +1147 -0
- package/dist/stream.d.ts +62 -0
- package/dist/stream.js +255 -0
- package/package.json +71 -4
package/dist/stream.d.ts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Server-sent events for streamed chat completions.
|
|
3
|
+
*
|
|
4
|
+
* Only the reply is ever `delta.content`. Text from the answer step streams
|
|
5
|
+
* live when that step is certain to be the reply; otherwise the committed
|
|
6
|
+
* reply is sent when the run completes. Progress (step, tool, and command
|
|
7
|
+
* names only) and, when enabled, the answer step's model reasoning go to a
|
|
8
|
+
* separate reasoning field.
|
|
9
|
+
*/
|
|
10
|
+
import type { AgentStep } from "@clearideas/agent-runtime-contracts";
|
|
11
|
+
import type { EventSink } from "@clearideas/agent-runtime-core";
|
|
12
|
+
import { ServerError } from "./errors.js";
|
|
13
|
+
import { type CompletionIdentity, type FinishReason, type OpenAiUsage } from "./openai.js";
|
|
14
|
+
/** An async iterable fed by pushes; ends when closed or when the consumer stops. */
|
|
15
|
+
export declare class PushStream implements AsyncIterable<string> {
|
|
16
|
+
#private;
|
|
17
|
+
constructor(onCancel?: () => void);
|
|
18
|
+
get closed(): boolean;
|
|
19
|
+
push(chunk: string): void;
|
|
20
|
+
close(): void;
|
|
21
|
+
[Symbol.asyncIterator](): AsyncIterator<string>;
|
|
22
|
+
}
|
|
23
|
+
export interface ChatStreamOptions {
|
|
24
|
+
identity: CompletionIdentity;
|
|
25
|
+
includeUsage: boolean;
|
|
26
|
+
progress: "none" | "status";
|
|
27
|
+
reasoning: "none" | "answer-step";
|
|
28
|
+
progressField: "reasoning_content" | "reasoning";
|
|
29
|
+
heartbeatMs: number;
|
|
30
|
+
/** Step labels for progress: name when the manifest gives one, otherwise id. */
|
|
31
|
+
stepLabels: ReadonlyMap<string, string>;
|
|
32
|
+
onCancel?: () => void;
|
|
33
|
+
onError?: (error: ServerError) => void;
|
|
34
|
+
}
|
|
35
|
+
/** Labels for every step, nested ones included. */
|
|
36
|
+
export declare const stepLabels: (steps: readonly AgentStep[]) => Map<string, string>;
|
|
37
|
+
export declare class ChatStream {
|
|
38
|
+
#private;
|
|
39
|
+
readonly body: PushStream;
|
|
40
|
+
constructor(options: ChatStreamOptions);
|
|
41
|
+
/** Text already sent as content. */
|
|
42
|
+
get forwarded(): string;
|
|
43
|
+
/** True when live text turned out not to be a prefix of the committed reply. */
|
|
44
|
+
get diverged(): boolean;
|
|
45
|
+
/** Send the role chunk and begin heartbeats. */
|
|
46
|
+
start(): void;
|
|
47
|
+
/** The step whose text may stream live as the reply. */
|
|
48
|
+
setLiveStep(stepId: string): void;
|
|
49
|
+
readonly sink: EventSink;
|
|
50
|
+
/**
|
|
51
|
+
* Complete the stream with the committed reply. Live text that is a prefix
|
|
52
|
+
* of the reply is completed with the remainder; otherwise the stream
|
|
53
|
+
* ends with an error because emitted text cannot be withdrawn.
|
|
54
|
+
*/
|
|
55
|
+
finish(input: {
|
|
56
|
+
reply: string;
|
|
57
|
+
finishReason: FinishReason;
|
|
58
|
+
usage: OpenAiUsage;
|
|
59
|
+
}): void;
|
|
60
|
+
/** End the stream with an error event. */
|
|
61
|
+
fail(error: ServerError): void;
|
|
62
|
+
}
|
package/dist/stream.js
ADDED
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
import { ServerError } from "./errors.js";
|
|
2
|
+
import { chatCompletionChunk, usageChunk, } from "./openai.js";
|
|
3
|
+
/** An async iterable fed by pushes; ends when closed or when the consumer stops. */
|
|
4
|
+
export class PushStream {
|
|
5
|
+
#queue = [];
|
|
6
|
+
#waiting;
|
|
7
|
+
#closed = false;
|
|
8
|
+
#onCancel;
|
|
9
|
+
constructor(onCancel) {
|
|
10
|
+
this.#onCancel = onCancel;
|
|
11
|
+
}
|
|
12
|
+
get closed() {
|
|
13
|
+
return this.#closed;
|
|
14
|
+
}
|
|
15
|
+
push(chunk) {
|
|
16
|
+
if (this.#closed)
|
|
17
|
+
return;
|
|
18
|
+
if (this.#waiting) {
|
|
19
|
+
const resolve = this.#waiting;
|
|
20
|
+
this.#waiting = undefined;
|
|
21
|
+
resolve({ value: chunk, done: false });
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
this.#queue.push(chunk);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
close() {
|
|
28
|
+
if (this.#closed)
|
|
29
|
+
return;
|
|
30
|
+
this.#closed = true;
|
|
31
|
+
if (this.#waiting && this.#queue.length === 0) {
|
|
32
|
+
const resolve = this.#waiting;
|
|
33
|
+
this.#waiting = undefined;
|
|
34
|
+
resolve({ value: undefined, done: true });
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
[Symbol.asyncIterator]() {
|
|
38
|
+
return {
|
|
39
|
+
next: () => {
|
|
40
|
+
const chunk = this.#queue.shift();
|
|
41
|
+
if (chunk !== undefined)
|
|
42
|
+
return Promise.resolve({ value: chunk, done: false });
|
|
43
|
+
if (this.#closed)
|
|
44
|
+
return Promise.resolve({ value: undefined, done: true });
|
|
45
|
+
return new Promise((resolve) => {
|
|
46
|
+
this.#waiting = resolve;
|
|
47
|
+
});
|
|
48
|
+
},
|
|
49
|
+
return: () => {
|
|
50
|
+
// The consumer stopped reading: the client went away.
|
|
51
|
+
const wasOpen = !this.#closed;
|
|
52
|
+
this.#closed = true;
|
|
53
|
+
this.#queue.length = 0;
|
|
54
|
+
if (wasOpen)
|
|
55
|
+
this.#onCancel?.();
|
|
56
|
+
return Promise.resolve({ value: undefined, done: true });
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
const sse = (value) => `data: ${JSON.stringify(value)}\n\n`;
|
|
62
|
+
/** Labels for every step, nested ones included. */
|
|
63
|
+
export const stepLabels = (steps) => {
|
|
64
|
+
const labels = new Map();
|
|
65
|
+
const visit = (list) => {
|
|
66
|
+
for (const step of list) {
|
|
67
|
+
labels.set(step.id, step.name ?? step.id);
|
|
68
|
+
if (step.type === "loop")
|
|
69
|
+
visit(step.steps);
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
visit(steps);
|
|
73
|
+
return labels;
|
|
74
|
+
};
|
|
75
|
+
export class ChatStream {
|
|
76
|
+
body;
|
|
77
|
+
#options;
|
|
78
|
+
#heartbeat;
|
|
79
|
+
#lastWrite = Date.now();
|
|
80
|
+
#started = false;
|
|
81
|
+
#liveStep;
|
|
82
|
+
#forwarded = "";
|
|
83
|
+
#diverged = false;
|
|
84
|
+
constructor(options) {
|
|
85
|
+
this.#options = options;
|
|
86
|
+
this.body = new PushStream(() => {
|
|
87
|
+
this.#stopHeartbeat();
|
|
88
|
+
options.onCancel?.();
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
/** Text already sent as content. */
|
|
92
|
+
get forwarded() {
|
|
93
|
+
return this.#forwarded;
|
|
94
|
+
}
|
|
95
|
+
/** True when live text turned out not to be a prefix of the committed reply. */
|
|
96
|
+
get diverged() {
|
|
97
|
+
return this.#diverged;
|
|
98
|
+
}
|
|
99
|
+
/** Send the role chunk and begin heartbeats. */
|
|
100
|
+
start() {
|
|
101
|
+
if (this.#started)
|
|
102
|
+
return;
|
|
103
|
+
this.#started = true;
|
|
104
|
+
this.#write(chatCompletionChunk(this.#options.identity, {
|
|
105
|
+
role: "assistant",
|
|
106
|
+
content: "",
|
|
107
|
+
}));
|
|
108
|
+
this.#heartbeat = setInterval(() => {
|
|
109
|
+
if (Date.now() - this.#lastWrite >= this.#options.heartbeatMs) {
|
|
110
|
+
this.body.push(": keep-alive\n\n");
|
|
111
|
+
this.#lastWrite = Date.now();
|
|
112
|
+
}
|
|
113
|
+
}, Math.max(250, Math.floor(this.#options.heartbeatMs / 3)));
|
|
114
|
+
this.#heartbeat.unref?.();
|
|
115
|
+
}
|
|
116
|
+
/** The step whose text may stream live as the reply. */
|
|
117
|
+
setLiveStep(stepId) {
|
|
118
|
+
this.#liveStep = stepId;
|
|
119
|
+
}
|
|
120
|
+
sink = {
|
|
121
|
+
emit: (event) => this.#onEvent(event),
|
|
122
|
+
};
|
|
123
|
+
#onEvent(event) {
|
|
124
|
+
if (!this.#started || this.body.closed)
|
|
125
|
+
return;
|
|
126
|
+
const topLevelLive = this.#liveStep !== undefined &&
|
|
127
|
+
event.stepId === this.#liveStep &&
|
|
128
|
+
(event.stepPath === undefined || event.stepPath === this.#liveStep);
|
|
129
|
+
if (event.type === "model.text.delta") {
|
|
130
|
+
const delta = typeof event.data?.delta === "string" ? event.data.delta : "";
|
|
131
|
+
if (topLevelLive && delta) {
|
|
132
|
+
this.#forwarded += delta;
|
|
133
|
+
this.#delta({ content: delta });
|
|
134
|
+
}
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
if (event.type === "model.reasoning.delta") {
|
|
138
|
+
const delta = typeof event.data?.delta === "string" ? event.data.delta : "";
|
|
139
|
+
if (topLevelLive && delta && this.#options.reasoning === "answer-step")
|
|
140
|
+
this.#progress(delta);
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
if (event.type === "model.retry" && topLevelLive && this.#forwarded) {
|
|
144
|
+
this.#interrupt();
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
if (this.#options.progress === "status") {
|
|
148
|
+
const line = this.#progressLine(event);
|
|
149
|
+
if (line)
|
|
150
|
+
this.#progress(`${line}\n`);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
#progressLine(event) {
|
|
154
|
+
const label = () => (event.stepId && this.#options.stepLabels.get(event.stepId)) ||
|
|
155
|
+
event.stepId ||
|
|
156
|
+
"step";
|
|
157
|
+
const name = (key) => typeof event.data?.[key] === "string" ? event.data[key] : "";
|
|
158
|
+
switch (event.type) {
|
|
159
|
+
case "step.started":
|
|
160
|
+
return `Step: ${label()}`;
|
|
161
|
+
case "model.tool.started":
|
|
162
|
+
return name("toolName") ? `Tool: ${name("toolName")}` : undefined;
|
|
163
|
+
case "sandbox.command.started":
|
|
164
|
+
return name("name") ? `Command: ${name("name")}` : undefined;
|
|
165
|
+
case "context.reduced":
|
|
166
|
+
return event.data?.scope === "messages"
|
|
167
|
+
? "Compacting the earlier conversation"
|
|
168
|
+
: undefined;
|
|
169
|
+
case "model.retry":
|
|
170
|
+
return "Retrying a model call";
|
|
171
|
+
case "approval.requested":
|
|
172
|
+
return "Waiting for approval";
|
|
173
|
+
default:
|
|
174
|
+
return undefined;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
#progress(text) {
|
|
178
|
+
this.#delta(this.#options.progressField === "reasoning"
|
|
179
|
+
? { reasoning: text }
|
|
180
|
+
: { reasoning_content: text });
|
|
181
|
+
}
|
|
182
|
+
#delta(delta) {
|
|
183
|
+
this.#write(chatCompletionChunk(this.#options.identity, delta));
|
|
184
|
+
}
|
|
185
|
+
#write(value) {
|
|
186
|
+
this.body.push(sse(value));
|
|
187
|
+
this.#lastWrite = Date.now();
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Complete the stream with the committed reply. Live text that is a prefix
|
|
191
|
+
* of the reply is completed with the remainder; otherwise the stream
|
|
192
|
+
* ends with an error because emitted text cannot be withdrawn.
|
|
193
|
+
*/
|
|
194
|
+
finish(input) {
|
|
195
|
+
if (this.body.closed)
|
|
196
|
+
return;
|
|
197
|
+
this.start();
|
|
198
|
+
if (this.#forwarded) {
|
|
199
|
+
if (input.reply.startsWith(this.#forwarded)) {
|
|
200
|
+
const rest = input.reply.slice(this.#forwarded.length);
|
|
201
|
+
if (rest)
|
|
202
|
+
this.#sendReply(rest);
|
|
203
|
+
}
|
|
204
|
+
else {
|
|
205
|
+
this.#interrupt();
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
else if (input.reply) {
|
|
210
|
+
this.#sendReply(input.reply);
|
|
211
|
+
}
|
|
212
|
+
this.#write(chatCompletionChunk(this.#options.identity, {}, input.finishReason));
|
|
213
|
+
if (this.#options.includeUsage)
|
|
214
|
+
this.#write(usageChunk(this.#options.identity, input.usage));
|
|
215
|
+
this.#end();
|
|
216
|
+
}
|
|
217
|
+
/** Send the reply in chunks, so clients render it progressively. */
|
|
218
|
+
#sendReply(text) {
|
|
219
|
+
const chunkSize = 256;
|
|
220
|
+
let index = 0;
|
|
221
|
+
while (index < text.length) {
|
|
222
|
+
let end = Math.min(index + chunkSize, text.length);
|
|
223
|
+
// Keep a surrogate pair in one chunk.
|
|
224
|
+
const last = text.charCodeAt(end - 1);
|
|
225
|
+
if (end < text.length && last >= 0xd800 && last <= 0xdbff)
|
|
226
|
+
end += 1;
|
|
227
|
+
this.#delta({ content: text.slice(index, end) });
|
|
228
|
+
index = end;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
/** End the stream with an error event. */
|
|
232
|
+
fail(error) {
|
|
233
|
+
if (this.body.closed)
|
|
234
|
+
return;
|
|
235
|
+
this.start();
|
|
236
|
+
this.#write(error.toBody());
|
|
237
|
+
this.#end();
|
|
238
|
+
}
|
|
239
|
+
#interrupt() {
|
|
240
|
+
this.#diverged = true;
|
|
241
|
+
const error = new ServerError(502, "stream_interrupted", "The answer changed after streaming began. Retry the request.");
|
|
242
|
+
this.fail(error);
|
|
243
|
+
this.#options.onError?.(error);
|
|
244
|
+
}
|
|
245
|
+
#end() {
|
|
246
|
+
this.body.push("data: [DONE]\n\n");
|
|
247
|
+
this.#stopHeartbeat();
|
|
248
|
+
this.body.close();
|
|
249
|
+
}
|
|
250
|
+
#stopHeartbeat() {
|
|
251
|
+
if (this.#heartbeat)
|
|
252
|
+
clearInterval(this.#heartbeat);
|
|
253
|
+
this.#heartbeat = undefined;
|
|
254
|
+
}
|
|
255
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,73 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@clearideas/agent-runtime-server",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.10.0",
|
|
4
|
+
"description": "OpenAI-compatible HTTP server for Agent Runtime manifests, and the host composition shared by Agent Runtime hosts.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"author": "Clear Ideas Incorporated",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/clearideas/agent-runtime.git",
|
|
10
|
+
"directory": "packages/server"
|
|
11
|
+
},
|
|
12
|
+
"publishConfig": {
|
|
13
|
+
"access": "public",
|
|
14
|
+
"provenance": true,
|
|
15
|
+
"registry": "https://registry.npmjs.org/"
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"dist",
|
|
19
|
+
"NOTICE",
|
|
20
|
+
"README.md"
|
|
21
|
+
],
|
|
22
|
+
"type": "module",
|
|
23
|
+
"main": "dist/index.js",
|
|
24
|
+
"types": "dist/index.d.ts",
|
|
25
|
+
"exports": {
|
|
26
|
+
".": {
|
|
27
|
+
"types": "./dist/index.d.ts",
|
|
28
|
+
"default": "./dist/index.js"
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
"scripts": {
|
|
32
|
+
"build": "tsc -b tsconfig.json --force",
|
|
33
|
+
"type-check": "tsc -p tsconfig.json --noEmit",
|
|
34
|
+
"test": "vitest run",
|
|
35
|
+
"prepack": "npm run build && node ../../scripts/prepare-package-license.mjs",
|
|
36
|
+
"postpack": "node ../../scripts/cleanup-package-license.mjs"
|
|
37
|
+
},
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"@clearideas/agent-runtime-condition-jexl": "0.10.0",
|
|
40
|
+
"@clearideas/agent-runtime-config": "0.10.0",
|
|
41
|
+
"@clearideas/agent-runtime-contracts": "0.10.0",
|
|
42
|
+
"@clearideas/agent-runtime-core": "0.10.0",
|
|
43
|
+
"@clearideas/agent-runtime-sandbox": "0.10.0",
|
|
44
|
+
"@clearideas/agent-runtime-step-loop": "0.10.0",
|
|
45
|
+
"@clearideas/agent-runtime-step-prompt": "0.10.0",
|
|
46
|
+
"@clearideas/agent-runtime-step-standard": "0.10.0",
|
|
47
|
+
"@clearideas/agent-runtime-store-local": "0.10.0",
|
|
48
|
+
"@clearideas/agent-runtime-store-sqlite": "0.10.0",
|
|
49
|
+
"yaml": "^2.9.1",
|
|
50
|
+
"zod": "^4.6.6"
|
|
51
|
+
},
|
|
52
|
+
"devDependencies": {
|
|
53
|
+
"@types/node": "^26.6.6",
|
|
54
|
+
"openai": "^7.48.0",
|
|
55
|
+
"typescript": "^7.0.2",
|
|
56
|
+
"vitest": "^5.1.0"
|
|
57
|
+
},
|
|
58
|
+
"engines": {
|
|
59
|
+
"node": ">=22.20.0"
|
|
60
|
+
},
|
|
61
|
+
"homepage": "https://github.com/clearideas/agent-runtime#readme",
|
|
62
|
+
"bugs": {
|
|
63
|
+
"url": "https://github.com/clearideas/agent-runtime/issues"
|
|
64
|
+
},
|
|
65
|
+
"keywords": [
|
|
66
|
+
"ai",
|
|
67
|
+
"agents",
|
|
68
|
+
"agent-runtime",
|
|
69
|
+
"workflows",
|
|
70
|
+
"openai-compatible",
|
|
71
|
+
"server"
|
|
72
|
+
]
|
|
73
|
+
}
|