slot-flight 0.1.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 +21 -0
- package/README.md +242 -0
- package/dist/adapters/openai.d.ts +49 -0
- package/dist/adapters/openai.d.ts.map +1 -0
- package/dist/adapters/openai.js +93 -0
- package/dist/adapters/openai.js.map +1 -0
- package/dist/adapters/stream.d.ts +10 -0
- package/dist/adapters/stream.d.ts.map +1 -0
- package/dist/adapters/stream.js +18 -0
- package/dist/adapters/stream.js.map +1 -0
- package/dist/adapters/vercel.d.ts +20 -0
- package/dist/adapters/vercel.d.ts.map +1 -0
- package/dist/adapters/vercel.js +67 -0
- package/dist/adapters/vercel.js.map +1 -0
- package/dist/core.d.ts +4 -0
- package/dist/core.d.ts.map +1 -0
- package/dist/core.js +3 -0
- package/dist/core.js.map +1 -0
- package/dist/engine.d.ts +14 -0
- package/dist/engine.d.ts.map +1 -0
- package/dist/engine.js +43 -0
- package/dist/engine.js.map +1 -0
- package/dist/errors.d.ts +23 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +41 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/dist/slot/execution/events.d.ts +15 -0
- package/dist/slot/execution/events.d.ts.map +1 -0
- package/dist/slot/execution/events.js +114 -0
- package/dist/slot/execution/events.js.map +1 -0
- package/dist/slot/execution/failures.d.ts +14 -0
- package/dist/slot/execution/failures.d.ts.map +1 -0
- package/dist/slot/execution/failures.js +71 -0
- package/dist/slot/execution/failures.js.map +1 -0
- package/dist/slot/execution/frame-request.d.ts +13 -0
- package/dist/slot/execution/frame-request.d.ts.map +1 -0
- package/dist/slot/execution/frame-request.js +45 -0
- package/dist/slot/execution/frame-request.js.map +1 -0
- package/dist/slot/execution/index.d.ts +4 -0
- package/dist/slot/execution/index.d.ts.map +1 -0
- package/dist/slot/execution/index.js +30 -0
- package/dist/slot/execution/index.js.map +1 -0
- package/dist/slot/execution/request.d.ts +3 -0
- package/dist/slot/execution/request.d.ts.map +1 -0
- package/dist/slot/execution/request.js +10 -0
- package/dist/slot/execution/request.js.map +1 -0
- package/dist/slot/execution/scope.d.ts +10 -0
- package/dist/slot/execution/scope.d.ts.map +1 -0
- package/dist/slot/execution/scope.js +85 -0
- package/dist/slot/execution/scope.js.map +1 -0
- package/dist/slot/execution/types.d.ts +21 -0
- package/dist/slot/execution/types.d.ts.map +1 -0
- package/dist/slot/execution/types.js +2 -0
- package/dist/slot/execution/types.js.map +1 -0
- package/dist/slot/frame/parser.d.ts +27 -0
- package/dist/slot/frame/parser.d.ts.map +1 -0
- package/dist/slot/frame/parser.js +143 -0
- package/dist/slot/frame/parser.js.map +1 -0
- package/dist/slot/frame/prompt.d.ts +4 -0
- package/dist/slot/frame/prompt.d.ts.map +1 -0
- package/dist/slot/frame/prompt.js +59 -0
- package/dist/slot/frame/prompt.js.map +1 -0
- package/dist/slot/frame/request.d.ts +5 -0
- package/dist/slot/frame/request.d.ts.map +1 -0
- package/dist/slot/frame/request.js +23 -0
- package/dist/slot/frame/request.js.map +1 -0
- package/dist/slot/index.d.ts +9 -0
- package/dist/slot/index.d.ts.map +1 -0
- package/dist/slot/index.js +9 -0
- package/dist/slot/index.js.map +1 -0
- package/dist/slot/object/definition.d.ts +12 -0
- package/dist/slot/object/definition.d.ts.map +1 -0
- package/dist/slot/object/definition.js +53 -0
- package/dist/slot/object/definition.js.map +1 -0
- package/dist/slot/object/projections.d.ts +6 -0
- package/dist/slot/object/projections.d.ts.map +1 -0
- package/dist/slot/object/projections.js +37 -0
- package/dist/slot/object/projections.js.map +1 -0
- package/dist/slot/object/run.d.ts +18 -0
- package/dist/slot/object/run.d.ts.map +1 -0
- package/dist/slot/object/run.js +94 -0
- package/dist/slot/object/run.js.map +1 -0
- package/dist/slot/object/stream.d.ts +34 -0
- package/dist/slot/object/stream.d.ts.map +1 -0
- package/dist/slot/object/stream.js +25 -0
- package/dist/slot/object/stream.js.map +1 -0
- package/dist/slot/object/web.d.ts +5 -0
- package/dist/slot/object/web.d.ts.map +1 -0
- package/dist/slot/object/web.js +107 -0
- package/dist/slot/object/web.js.map +1 -0
- package/dist/slot/path.d.ts +20 -0
- package/dist/slot/path.d.ts.map +1 -0
- package/dist/slot/path.js +125 -0
- package/dist/slot/path.js.map +1 -0
- package/dist/slot/plan.d.ts +7 -0
- package/dist/slot/plan.d.ts.map +1 -0
- package/dist/slot/plan.js +17 -0
- package/dist/slot/plan.js.map +1 -0
- package/dist/types.d.ts +94 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/package.json +70 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Minho Jang
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
# slot-flight
|
|
2
|
+
|
|
3
|
+
Slot-wise LLM value streaming with server-owned JSON assembly.
|
|
4
|
+
|
|
5
|
+
`slot-flight` does not ask the model to stream a valid JSON document. The model
|
|
6
|
+
streams compact slot frames, and the server maps those frame ids to JSON paths,
|
|
7
|
+
validates each value with Zod, retries failed slots, and assembles the final
|
|
8
|
+
object itself.
|
|
9
|
+
|
|
10
|
+
```text
|
|
11
|
+
<1>Alice</1>
|
|
12
|
+
<2>Senior Engineer</2>
|
|
13
|
+
<3>Builds streaming JSON assembly engines.</3>
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Install
|
|
17
|
+
|
|
18
|
+
```sh
|
|
19
|
+
bun add slot-flight zod
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Provider SDKs stay in your application:
|
|
23
|
+
|
|
24
|
+
```sh
|
|
25
|
+
bun add openai
|
|
26
|
+
# or
|
|
27
|
+
bun add ai @ai-sdk/openai
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Quick Start
|
|
31
|
+
|
|
32
|
+
Define the output shape with Zod. Every generated field is registered through
|
|
33
|
+
`.describe()`, which becomes the model-facing slot instruction.
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
import OpenAI from "openai";
|
|
37
|
+
import { z } from "zod";
|
|
38
|
+
import { slotObject } from "slot-flight";
|
|
39
|
+
import { streamSlotObject } from "slot-flight/adapters/openai";
|
|
40
|
+
|
|
41
|
+
const openai = new OpenAI({
|
|
42
|
+
apiKey: process.env.API_KEY,
|
|
43
|
+
baseURL: "https://integrate.api.nvidia.com/v1"
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
const stream = streamSlotObject({
|
|
47
|
+
client: openai,
|
|
48
|
+
model: "openai/gpt-oss-20b",
|
|
49
|
+
messages: [
|
|
50
|
+
{
|
|
51
|
+
role: "user",
|
|
52
|
+
content: "Classify this customer support feedback for a triage queue."
|
|
53
|
+
}
|
|
54
|
+
],
|
|
55
|
+
temperature: 0.2,
|
|
56
|
+
output: slotObject({
|
|
57
|
+
schema: z.object({
|
|
58
|
+
summary: z
|
|
59
|
+
.string()
|
|
60
|
+
.min(1)
|
|
61
|
+
.describe("Write one concise operational summary."),
|
|
62
|
+
sentiment: z
|
|
63
|
+
.enum(["positive", "neutral", "negative", "mixed"])
|
|
64
|
+
.describe("Write exactly one of: positive, neutral, negative, mixed."),
|
|
65
|
+
priority: z
|
|
66
|
+
.enum(["low", "medium", "high"])
|
|
67
|
+
.describe("Write exactly one of: low, medium, high.")
|
|
68
|
+
})
|
|
69
|
+
})
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
for await (const slot of stream.completedSlotStream) {
|
|
73
|
+
console.log(slot.slot, slot.value);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const finalObject = await stream.finalObject;
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
## Schema Contract
|
|
80
|
+
|
|
81
|
+
`slotObject()` uses the Zod schema as the single source of truth.
|
|
82
|
+
|
|
83
|
+
- Fields with `.describe()` become slots.
|
|
84
|
+
- Nested objects without `.describe()` are traversed until described fields are
|
|
85
|
+
found.
|
|
86
|
+
- Described arrays and described objects become JSON slots, so coordinated
|
|
87
|
+
structured values are generated and validated as one value.
|
|
88
|
+
- Fields without `.describe()` are rejected instead of silently inventing
|
|
89
|
+
prompts.
|
|
90
|
+
|
|
91
|
+
```ts
|
|
92
|
+
const output = slotObject({
|
|
93
|
+
schema: z.object({
|
|
94
|
+
title: z.string().min(1).describe("Write a short title."),
|
|
95
|
+
metadata: z.object({
|
|
96
|
+
audience: z.string().describe("Write the intended audience.")
|
|
97
|
+
}),
|
|
98
|
+
tags: z
|
|
99
|
+
.array(z.string().min(1))
|
|
100
|
+
.length(3)
|
|
101
|
+
.describe("Write a JSON array of exactly 3 short tags.")
|
|
102
|
+
})
|
|
103
|
+
});
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
Text slots are treated as raw values. JSON slots are parsed with `JSON.parse()`
|
|
107
|
+
before Zod validation.
|
|
108
|
+
|
|
109
|
+
## Adapters
|
|
110
|
+
|
|
111
|
+
### OpenAI-Compatible
|
|
112
|
+
|
|
113
|
+
```ts
|
|
114
|
+
import { streamSlotObject } from "slot-flight/adapters/openai";
|
|
115
|
+
|
|
116
|
+
const stream = streamSlotObject({
|
|
117
|
+
client: openai,
|
|
118
|
+
model: "openai/gpt-oss-20b",
|
|
119
|
+
messages,
|
|
120
|
+
output
|
|
121
|
+
});
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
The OpenAI adapter keeps the call shaped like a normal
|
|
125
|
+
`chat.completions.create` request. It forces `stream: true`, appends the
|
|
126
|
+
generated slot-frame prompt as the final message, passes `AbortSignal` into the
|
|
127
|
+
SDK call, and extracts `choices[].delta.content`.
|
|
128
|
+
|
|
129
|
+
Optional client enhancer:
|
|
130
|
+
|
|
131
|
+
```ts
|
|
132
|
+
import { withSlotFlight } from "slot-flight/adapters/openai";
|
|
133
|
+
|
|
134
|
+
const client = withSlotFlight(openai);
|
|
135
|
+
const stream = client.chat.completions.streamSlotObject({
|
|
136
|
+
model: "openai/gpt-oss-20b",
|
|
137
|
+
messages,
|
|
138
|
+
output
|
|
139
|
+
});
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
### Vercel AI SDK
|
|
143
|
+
|
|
144
|
+
```ts
|
|
145
|
+
import { streamText } from "ai";
|
|
146
|
+
import { streamSlotObject } from "slot-flight/adapters/vercel";
|
|
147
|
+
|
|
148
|
+
const stream = streamSlotObject({
|
|
149
|
+
streamText,
|
|
150
|
+
model,
|
|
151
|
+
messages,
|
|
152
|
+
output
|
|
153
|
+
});
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
The Vercel adapter keeps `streamText` as the generation primitive and consumes
|
|
157
|
+
its `textStream`.
|
|
158
|
+
|
|
159
|
+
### Generic Streams
|
|
160
|
+
|
|
161
|
+
For any SDK that returns async chunks:
|
|
162
|
+
|
|
163
|
+
```ts
|
|
164
|
+
import { createChunkStreamGenerator } from "slot-flight/adapters/stream";
|
|
165
|
+
|
|
166
|
+
const generate = createChunkStreamGenerator({
|
|
167
|
+
stream: (request) => someSdk.stream({ prompt: request.prompt }),
|
|
168
|
+
text: (chunk) => chunk.text
|
|
169
|
+
});
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
Advanced engine usage is available from `slot-flight/core` when you want to
|
|
173
|
+
provide your own `SlotGenerator` directly.
|
|
174
|
+
|
|
175
|
+
## Stream Outputs
|
|
176
|
+
|
|
177
|
+
`streamSlotObject()` returns a `SlotObjectStream`:
|
|
178
|
+
|
|
179
|
+
- `completedSlotStream`: validated slot values, one event per completed slot.
|
|
180
|
+
- `finalObject`: final Zod-validated object.
|
|
181
|
+
- `toResponse()`: SSE or NDJSON over completed slot output.
|
|
182
|
+
- `debug`: partial snapshots and low-level slot events.
|
|
183
|
+
|
|
184
|
+
Each `SlotObjectStream` owns one live model run. Choose one live view per run:
|
|
185
|
+
`completedSlotStream`, `toResponse()`, one `debug` stream, or `finalObject` by
|
|
186
|
+
itself. After a live view finishes, `finalObject` can still be awaited for the
|
|
187
|
+
validated result.
|
|
188
|
+
|
|
189
|
+
For HTTP handlers:
|
|
190
|
+
|
|
191
|
+
```ts
|
|
192
|
+
export async function POST() {
|
|
193
|
+
const stream = streamSlotObject({
|
|
194
|
+
client: openai,
|
|
195
|
+
model: "openai/gpt-oss-20b",
|
|
196
|
+
messages,
|
|
197
|
+
output
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
return stream.toResponse();
|
|
201
|
+
}
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
`toResponse()` defaults to SSE over completed slots. Use
|
|
205
|
+
`stream.toResponse({ format: "ndjson" })` for newline-delimited JSON.
|
|
206
|
+
|
|
207
|
+
Low-level events are intentionally behind `debug`:
|
|
208
|
+
|
|
209
|
+
```ts
|
|
210
|
+
for await (const event of stream.debug.slotEventStream) {
|
|
211
|
+
console.log(event.type);
|
|
212
|
+
}
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
## Reliability Scope
|
|
216
|
+
|
|
217
|
+
`slot-flight` owns reliability only where it has slot-level information:
|
|
218
|
+
|
|
219
|
+
- retry failed or missing slots after parsing and Zod validation
|
|
220
|
+
- retry recoverable slot protocol failures such as an unfinished frame
|
|
221
|
+
- cancel one frame stream through `AbortSignal`
|
|
222
|
+
|
|
223
|
+
It does not implement provider retry policy, backoff, rate limiting, queues,
|
|
224
|
+
failover, or agent orchestration. Leave those in your LLM SDK or application
|
|
225
|
+
workflow.
|
|
226
|
+
|
|
227
|
+
## Custom Errors
|
|
228
|
+
|
|
229
|
+
Custom error classes are exported from `slot-flight/core` for consumers that
|
|
230
|
+
need to branch on engine failures:
|
|
231
|
+
|
|
232
|
+
```ts
|
|
233
|
+
import {
|
|
234
|
+
SlotFlightError,
|
|
235
|
+
SlotFlightJsonParseError,
|
|
236
|
+
SlotFlightValidationError
|
|
237
|
+
} from "slot-flight/core";
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
Use these for slot-level failures reported by the engine. Adapter shape errors
|
|
241
|
+
remain normal `TypeError`s, caller cancellation remains an `AbortError`, and
|
|
242
|
+
provider stream failures are surfaced without provider retry policy.
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { z } from "zod";
|
|
2
|
+
import { type SlotObjectOutput, type SlotObjectStream } from "../slot/index.js";
|
|
3
|
+
import type { SlotFlightRunOptions } from "../types.js";
|
|
4
|
+
export interface OpenAIChatCompletionsClient {
|
|
5
|
+
chat: {
|
|
6
|
+
completions: {
|
|
7
|
+
create: unknown;
|
|
8
|
+
};
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
export interface OpenAIChatCompletionChunk {
|
|
12
|
+
choices?: Array<{
|
|
13
|
+
delta?: {
|
|
14
|
+
content?: string | null;
|
|
15
|
+
};
|
|
16
|
+
message?: {
|
|
17
|
+
content?: string | null;
|
|
18
|
+
};
|
|
19
|
+
}>;
|
|
20
|
+
}
|
|
21
|
+
export type OpenAIChatCompletionParams = Record<string, unknown> & {
|
|
22
|
+
model: string;
|
|
23
|
+
messages?: readonly unknown[];
|
|
24
|
+
};
|
|
25
|
+
export interface OpenAIStreamSlotObjectRequestOptions {
|
|
26
|
+
signal?: AbortSignal;
|
|
27
|
+
}
|
|
28
|
+
export type OpenAIStreamSlotObjectParams<TSchema extends z.ZodTypeAny> = OpenAIChatCompletionParams & {
|
|
29
|
+
client: OpenAIChatCompletionsClient;
|
|
30
|
+
output: SlotObjectOutput<TSchema>;
|
|
31
|
+
slotPromptRole?: string;
|
|
32
|
+
run?: SlotFlightRunOptions;
|
|
33
|
+
};
|
|
34
|
+
export type OpenAIClientStreamSlotObjectParams<TSchema extends z.ZodTypeAny> = OpenAIChatCompletionParams & {
|
|
35
|
+
output: SlotObjectOutput<TSchema>;
|
|
36
|
+
slotPromptRole?: string;
|
|
37
|
+
run?: SlotFlightRunOptions;
|
|
38
|
+
};
|
|
39
|
+
export interface SlotFlightOpenAIChatCompletionsExtension {
|
|
40
|
+
streamSlotObject: <TSchema extends z.ZodTypeAny>(body: OpenAIClientStreamSlotObjectParams<TSchema>, options?: OpenAIStreamSlotObjectRequestOptions) => SlotObjectStream<z.infer<TSchema>>;
|
|
41
|
+
}
|
|
42
|
+
export type SlotFlightOpenAIClient<TClient extends OpenAIChatCompletionsClient> = TClient & {
|
|
43
|
+
chat: TClient["chat"] & {
|
|
44
|
+
completions: TClient["chat"]["completions"] & SlotFlightOpenAIChatCompletionsExtension;
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
export declare function withSlotFlight<TClient extends OpenAIChatCompletionsClient>(client: TClient): SlotFlightOpenAIClient<TClient>;
|
|
48
|
+
export declare function streamSlotObject<TSchema extends z.ZodTypeAny>({ client, output, slotPromptRole, run, ...chat }: OpenAIStreamSlotObjectParams<TSchema>): SlotObjectStream<z.infer<TSchema>>;
|
|
49
|
+
//# sourceMappingURL=openai.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"openai.d.ts","sourceRoot":"","sources":["../../src/adapters/openai.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAE7B,OAAO,EAEL,KAAK,gBAAgB,EACrB,KAAK,gBAAgB,EACtB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,KAAK,EAGV,oBAAoB,EACrB,MAAM,aAAa,CAAC;AAGrB,MAAM,WAAW,2BAA2B;IAC1C,IAAI,EAAE;QACJ,WAAW,EAAE;YACX,MAAM,EAAE,OAAO,CAAC;SACjB,CAAC;KACH,CAAC;CACH;AAOD,MAAM,WAAW,yBAAyB;IACxC,OAAO,CAAC,EAAE,KAAK,CAAC;QACd,KAAK,CAAC,EAAE;YACN,OAAO,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;SACzB,CAAC;QACF,OAAO,CAAC,EAAE;YACR,OAAO,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;SACzB,CAAC;KACH,CAAC,CAAC;CACJ;AAED,MAAM,MAAM,0BAA0B,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG;IACjE,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,CAAC,EAAE,SAAS,OAAO,EAAE,CAAC;CAC/B,CAAC;AAEF,MAAM,WAAW,oCAAoC;IACnD,MAAM,CAAC,EAAE,WAAW,CAAC;CACtB;AAED,MAAM,MAAM,4BAA4B,CAAC,OAAO,SAAS,CAAC,CAAC,UAAU,IACnE,0BAA0B,GAAG;IAC3B,MAAM,EAAE,2BAA2B,CAAC;IACpC,MAAM,EAAE,gBAAgB,CAAC,OAAO,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
|
|
3
|
+
import { createChunkStreamGenerator } from "./stream.js";
|
|
4
|
+
export function withSlotFlight(client) {
|
|
5
|
+
const completions = client.chat
|
|
6
|
+
.completions;
|
|
7
|
+
if (completions.streamSlotObject === undefined) {
|
|
8
|
+
Object.defineProperty(completions, "streamSlotObject", {
|
|
9
|
+
configurable: true,
|
|
10
|
+
enumerable: false,
|
|
11
|
+
writable: true,
|
|
12
|
+
value: (body, options = {}) => {
|
|
13
|
+
const { run, ...rest } = body;
|
|
14
|
+
return streamSlotObject({
|
|
15
|
+
client,
|
|
16
|
+
...rest,
|
|
17
|
+
run: {
|
|
18
|
+
...run,
|
|
19
|
+
signal: combineAbortSignals(run?.signal, options.signal)
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
return client;
|
|
26
|
+
}
|
|
27
|
+
export function streamSlotObject({ client, output, slotPromptRole = "user", run, ...chat }) {
|
|
28
|
+
const streamController = new AbortController();
|
|
29
|
+
const generate = createChunkStreamGenerator({
|
|
30
|
+
stream: (request) => callOpenAIChatCompletionsCreate(client, createOpenAIChatBody(request, chat, slotPromptRole), request.signal),
|
|
31
|
+
text: extractOpenAIContentDelta
|
|
32
|
+
});
|
|
33
|
+
return createSlotObjectStream(slotFlight({
|
|
34
|
+
schema: output.schema,
|
|
35
|
+
slots: output.slots,
|
|
36
|
+
prompt: output.prompt,
|
|
37
|
+
maxRetries: output.maxRetries,
|
|
38
|
+
generate
|
|
39
|
+
}).run({
|
|
40
|
+
...run,
|
|
41
|
+
signal: combineAbortSignals(run?.signal, streamController.signal)
|
|
42
|
+
}), {
|
|
43
|
+
cancel: () => streamController.abort(new DOMException("Stream cancelled", "AbortError"))
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
function createOpenAIChatBody(request, chat, slotPromptRole) {
|
|
47
|
+
const { messages = [], ...rest } = chat;
|
|
48
|
+
return {
|
|
49
|
+
...rest,
|
|
50
|
+
messages: [
|
|
51
|
+
...messages,
|
|
52
|
+
{
|
|
53
|
+
role: slotPromptRole,
|
|
54
|
+
content: request.prompt
|
|
55
|
+
}
|
|
56
|
+
],
|
|
57
|
+
stream: true
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
function callOpenAIChatCompletionsCreate(client, body, signal) {
|
|
61
|
+
const completions = client.chat.completions;
|
|
62
|
+
const create = completions.create;
|
|
63
|
+
if (typeof create !== "function") {
|
|
64
|
+
throw new TypeError("OpenAI chat completions client is missing create().");
|
|
65
|
+
}
|
|
66
|
+
return create.call(completions, body, {
|
|
67
|
+
signal
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
function extractOpenAIContentDelta(chunk) {
|
|
71
|
+
return (chunk.choices
|
|
72
|
+
?.map((choice) => choice.delta?.content ?? choice.message?.content ?? "")
|
|
73
|
+
.join("") ?? "");
|
|
74
|
+
}
|
|
75
|
+
function combineAbortSignals(first, second) {
|
|
76
|
+
if (first === undefined || first.aborted) {
|
|
77
|
+
return first ?? second;
|
|
78
|
+
}
|
|
79
|
+
if (second === undefined || second.aborted) {
|
|
80
|
+
return second ?? first;
|
|
81
|
+
}
|
|
82
|
+
const controller = new AbortController();
|
|
83
|
+
const abortFromFirst = () => controller.abort(first.reason);
|
|
84
|
+
const abortFromSecond = () => controller.abort(second.reason);
|
|
85
|
+
first.addEventListener("abort", abortFromFirst, { once: true });
|
|
86
|
+
second.addEventListener("abort", abortFromSecond, { once: true });
|
|
87
|
+
controller.signal.addEventListener("abort", () => {
|
|
88
|
+
first.removeEventListener("abort", abortFromFirst);
|
|
89
|
+
second.removeEventListener("abort", abortFromSecond);
|
|
90
|
+
}, { once: true });
|
|
91
|
+
return controller.signal;
|
|
92
|
+
}
|
|
93
|
+
//# sourceMappingURL=openai.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"openai.js","sourceRoot":"","sources":["../../src/adapters/openai.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAA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C;AAC3B,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { MaybePromise, SlotFlightRequest, SlotGenerator } from "../types.js";
|
|
2
|
+
export type ChunkStreamFactory<TChunk> = (request: SlotFlightRequest) => MaybePromise<AsyncIterable<TChunk>>;
|
|
3
|
+
export type ChunkTextExtractor<TChunk> = (chunk: TChunk, request: SlotFlightRequest) => string | null | undefined;
|
|
4
|
+
export interface ChunkStreamGeneratorOptions<TChunk> {
|
|
5
|
+
stream: ChunkStreamFactory<TChunk>;
|
|
6
|
+
text: ChunkTextExtractor<TChunk>;
|
|
7
|
+
}
|
|
8
|
+
export declare function createChunkStreamGenerator<TChunk>({ stream, text }: ChunkStreamGeneratorOptions<TChunk>): SlotGenerator;
|
|
9
|
+
export declare function createTextStreamGenerator(stream: ChunkStreamFactory<string>): SlotGenerator;
|
|
10
|
+
//# sourceMappingURL=stream.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stream.d.ts","sourceRoot":"","sources":["../../src/adapters/stream.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,YAAY,EACZ,iBAAiB,EACjB,aAAa,EACd,MAAM,aAAa,CAAC;AAErB,MAAM,MAAM,kBAAkB,CAAC,MAAM,IAAI,CACvC,OAAO,EAAE,iBAAiB,KACvB,YAAY,CAAC,aAAa,CAAC,MAAM,CAAC,CAAC,CAAC;AAEzC,MAAM,MAAM,kBAAkB,CAAC,MAAM,IAAI,CACvC,KAAK,EAAE,MAAM,EACb,OAAO,EAAE,iBAAiB,KACvB,MAAM,GAAG,IAAI,GAAG,SAAS,CAAC;AAE/B,MAAM,WAAW,2BAA2B,CAAC,MAAM;IACjD,MAAM,EAAE,kBAAkB,CAAC,MAAM,CAAC,CAAC;IACnC,IAAI,EAAE,kBAAkB,CAAC,MAAM,CAAC,CAAC;CAClC;AAED,wBAAgB,0BAA0B,CAAC,MAAM,EAAE,EACjD,MAAM,EACN,IAAI,EACL,EAAE,2BAA2B,CAAC,MAAM,CAAC,GAAG,aAAa,CAWrD;AAED,wBAAgB,yBAAyB,CACvC,MAAM,EAAE,kBAAkB,CAAC,MAAM,CAAC,GACjC,aAAa,CAKf"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export function createChunkStreamGenerator({ stream, text }) {
|
|
2
|
+
return async function* chunkStreamGenerator(request) {
|
|
3
|
+
const chunks = await stream(request);
|
|
4
|
+
for await (const chunk of chunks) {
|
|
5
|
+
const value = text(chunk, request);
|
|
6
|
+
if (value !== undefined && value !== null && value.length > 0) {
|
|
7
|
+
yield value;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
export function createTextStreamGenerator(stream) {
|
|
13
|
+
return createChunkStreamGenerator({
|
|
14
|
+
stream,
|
|
15
|
+
text: (chunk) => chunk
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=stream.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stream.js","sourceRoot":"","sources":["../../src/adapters/stream.ts"],"names":[],"mappings":"AAoBA,MAAM,UAAU,0BAA0B,CAAS,EACjD,MAAM,EACN,IAAI,EACgC;IACpC,OAAO,KAAK,SAAS,CAAC,CAAC,oBAAoB,CAAC,OAAO;QACjD,MAAM,MAAM,GAAG,MAAM,MAAM,CAAC,OAAO,CAAC,CAAC;QAErC,IAAI,KAAK,EAAE,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;YACjC,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;YACnC,IAAI,KAAK,KAAK,SAAS,IAAI,KAAK,KAAK,IAAI,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;gBAC9D,MAAM,KAAK,CAAC;YACd,CAAC;QACH,CAAC;IACH,CAAC,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,yBAAyB,CACvC,MAAkC;IAElC,OAAO,0BAA0B,CAAC;QAChC,MAAM;QACN,IAAI,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK;KACvB,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { z } from "zod";
|
|
2
|
+
import { type SlotObjectOutput, type SlotObjectStream } from "../slot/index.js";
|
|
3
|
+
import type { SlotFlightRunOptions } from "../types.js";
|
|
4
|
+
export interface VercelStreamTextResult {
|
|
5
|
+
textStream: AsyncIterable<string>;
|
|
6
|
+
}
|
|
7
|
+
export type VercelStreamText = unknown;
|
|
8
|
+
export type VercelStreamTextParams = Record<string, unknown> & {
|
|
9
|
+
messages?: readonly unknown[];
|
|
10
|
+
prompt?: string;
|
|
11
|
+
abortSignal?: AbortSignal;
|
|
12
|
+
};
|
|
13
|
+
export type VercelStreamSlotObjectParams<TSchema extends z.ZodTypeAny> = VercelStreamTextParams & {
|
|
14
|
+
streamText: VercelStreamText;
|
|
15
|
+
output: SlotObjectOutput<TSchema>;
|
|
16
|
+
slotPromptRole?: string;
|
|
17
|
+
run?: SlotFlightRunOptions;
|
|
18
|
+
};
|
|
19
|
+
export declare function streamSlotObject<TSchema extends z.ZodTypeAny>({ streamText, output, slotPromptRole, run, ...params }: VercelStreamSlotObjectParams<TSchema>): SlotObjectStream<z.infer<TSchema>>;
|
|
20
|
+
//# sourceMappingURL=vercel.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vercel.d.ts","sourceRoot":"","sources":["../../src/adapters/vercel.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAE7B,OAAO,EAEL,KAAK,gBAAgB,EACrB,KAAK,gBAAgB,EACtB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,KAAK,EAGV,oBAAoB,EACrB,MAAM,aAAa,CAAC;AAGrB,MAAM,WAAW,sBAAsB;IACrC,UAAU,EAAE,aAAa,CAAC,MAAM,CAAC,CAAC;CACnC;AAED,MAAM,MAAM,gBAAgB,GAAG,OAAO,CAAC;AAMvC,MAAM,MAAM,sBAAsB,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG;IAC7D,QAAQ,CAAC,EAAE,SAAS,OAAO,EAAE,CAAC;IAC9B,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,WAAW,CAAC,EAAE,WAAW,CAAC;CAC3B,CAAC;AAEF,MAAM,MAAM,4BAA4B,CAAC,OAAO,SAAS,CAAC,CAAC,UAAU,IACnE,sBAAsB,GAAG;IACvB,UAAU,EAAE,gBAAgB,CAAC;IAC7B,MAAM,EAAE,gBAAgB,CAAC,OAAO,CAAC,CAAC;IAClC,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,GAAG,CAAC,EAAE,oBAAoB,CAAC;CAC5B,CAAC;AAEJ,wBAAgB,gBAAgB,CAAC,OAAO,SAAS,CAAC,CAAC,UAAU,EAAE,EAC7D,UAAU,EACV,MAAM,EACN,cAAuB,EACvB,GAAG,EACH,GAAG,MAAM,EACV,EAAE,4BAA4B,CAAC,OAAO,CAAC,GAAG,gBAAgB,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CA4B5E"}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { slotFlight } from "../engine.js";
|
|
2
|
+
import { createSlotObjectStream } from "../slot/index.js";
|
|
3
|
+
import { createTextStreamGenerator } from "./stream.js";
|
|
4
|
+
export function streamSlotObject({ streamText, output, slotPromptRole = "user", run, ...params }) {
|
|
5
|
+
const streamController = new AbortController();
|
|
6
|
+
const generate = createTextStreamGenerator(async (request) => {
|
|
7
|
+
const result = await callStreamText(streamText, createVercelStreamTextBody(request, params, slotPromptRole));
|
|
8
|
+
return result.textStream;
|
|
9
|
+
});
|
|
10
|
+
return createSlotObjectStream(slotFlight({
|
|
11
|
+
schema: output.schema,
|
|
12
|
+
slots: output.slots,
|
|
13
|
+
prompt: output.prompt,
|
|
14
|
+
maxRetries: output.maxRetries,
|
|
15
|
+
generate
|
|
16
|
+
}).run({
|
|
17
|
+
...run,
|
|
18
|
+
signal: combineAbortSignals(run?.signal, streamController.signal)
|
|
19
|
+
}), {
|
|
20
|
+
cancel: () => streamController.abort(new DOMException("Stream cancelled", "AbortError"))
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
function callStreamText(streamText, body) {
|
|
24
|
+
if (typeof streamText !== "function") {
|
|
25
|
+
throw new TypeError("Vercel AI SDK streamText must be a function.");
|
|
26
|
+
}
|
|
27
|
+
return streamText(body);
|
|
28
|
+
}
|
|
29
|
+
function createVercelStreamTextBody(request, params, slotPromptRole) {
|
|
30
|
+
const { abortSignal, messages, prompt, ...rest } = params;
|
|
31
|
+
return {
|
|
32
|
+
...rest,
|
|
33
|
+
...appendSlotPrompt({ messages, prompt }, slotPromptRole, request.prompt),
|
|
34
|
+
abortSignal: combineAbortSignals(abortSignal, request.signal)
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
function appendSlotPrompt(body, role, slotPrompt) {
|
|
38
|
+
if (body.messages !== undefined) {
|
|
39
|
+
return {
|
|
40
|
+
messages: [...body.messages, { role, content: slotPrompt }]
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
if (body.prompt !== undefined) {
|
|
44
|
+
return {
|
|
45
|
+
messages: [
|
|
46
|
+
{ role: "user", content: body.prompt },
|
|
47
|
+
{ role, content: slotPrompt }
|
|
48
|
+
]
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
return {
|
|
52
|
+
messages: [{ role, content: slotPrompt }]
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
function combineAbortSignals(callerSignal, engineSignal) {
|
|
56
|
+
if (callerSignal === undefined || callerSignal.aborted) {
|
|
57
|
+
return callerSignal ?? engineSignal;
|
|
58
|
+
}
|
|
59
|
+
if (engineSignal.aborted) {
|
|
60
|
+
return engineSignal;
|
|
61
|
+
}
|
|
62
|
+
const controller = new AbortController();
|
|
63
|
+
callerSignal.addEventListener("abort", () => controller.abort(callerSignal.reason), { once: true });
|
|
64
|
+
engineSignal.addEventListener("abort", () => controller.abort(engineSignal.reason), { once: true });
|
|
65
|
+
return controller.signal;
|
|
66
|
+
}
|
|
67
|
+
//# sourceMappingURL=vercel.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vercel.js","sourceRoot":"","sources":["../../src/adapters/vercel.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAC1C,OAAO,EACL,sBAAsB,EAGvB,MAAM,kBAAkB,CAAC;AAM1B,OAAO,EAAE,yBAAyB,EAAE,MAAM,aAAa,CAAC;AA0BxD,MAAM,UAAU,gBAAgB,CAA+B,EAC7D,UAAU,EACV,MAAM,EACN,cAAc,GAAG,MAAM,EACvB,GAAG,EACH,GAAG,MAAM,EAC6B;IACtC,MAAM,gBAAgB,GAAG,IAAI,eAAe,EAAE,CAAC;IAC/C,MAAM,QAAQ,GAAG,yBAAyB,CAAC,KAAK,EAAE,OAAO,EAAE,EAAE;QAC3D,MAAM,MAAM,GAAG,MAAM,cAAc,CACjC,UAAU,EACV,0BAA0B,CAAC,OAAO,EAAE,MAAM,EAAE,cAAc,CAAC,CAC5D,CAAC;QACF,OAAO,MAAM,CAAC,UAAU,CAAC;IAC3B,CAAC,CAAC,CAAC;IAEH,OAAO,sBAAsB,CAC3B,UAAU,CAAC;QACT,MAAM,EAAE,MAAM,CAAC,MAAM;QACrB,KAAK,EAAE,MAAM,CAAC,KAAK;QACnB,MAAM,EAAE,MAAM,CAAC,MAAM;QACrB,UAAU,EAAE,MAAM,CAAC,UAAU;QAC7B,QAAQ;KACT,CAAC,CAAC,GAAG,CAAC;QACL,GAAG,GAAG;QACN,MAAM,EAAE,mBAAmB,CAAC,GAAG,EAAE,MAAM,EAAE,gBAAgB,CAAC,MAAM,CAAC;KAClE,CAAC,EACF;QACE,MAAM,EAAE,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AC,EAAE,IAAI,EAAE,OAAO,EAAE,UAAU,EAAE,CAAC;KAC1C,CAAC;AACJ,CAAC;AAED,SAAS,mBAAmB,CAC1B,YAAqC,EACrC,YAAyB;IAEzB,IAAI,YAAY,KAAK,SAAS,IAAI,YAAY,CAAC,OAAO,EAAE,CAAC;QACvD,OAAO,YAAY,IAAI,YAAY,CAAC;IACtC,CAAC;IAED,IAAI,YAAY,CAAC,OAAO,EAAE,CAAC;QACzB,OAAO,YAAY,CAAC;IACtB,CAAC;IAED,MAAM,UAAU,GAAG,IAAI,eAAe,EAAE,CAAC;IACzC,YAAY,CAAC,gBAAgB,CAC3B,OAAO,EACP,GAAG,EAAE,CAAC,UAAU,CAAC,KAAK,CAAC,YAAY,CAAC,MAAM,CAAC,EAC3C,EAAE,IAAI,EAAE,IAAI,EAAE,CACf,CAAC;IACF,YAAY,CAAC,gBAAgB,CAC3B,OAAO,EACP,GAAG,EAAE,CAAC,UAAU,CAAC,KAAK,CAAC,YAAY,CAAC,MAAM,CAAC,EAC3C,EAAE,IAAI,EAAE,IAAI,EAAE,CACf,CAAC;IAEF,OAAO,UAAU,CAAC,MAAM,CAAC;AAC3B,CAAC"}
|
package/dist/core.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { SlotFlight, slotFlight } from "./engine.js";
|
|
2
|
+
export { SlotFlightConfigurationError, SlotFlightError, SlotFlightJsonParseError, SlotFlightSlotProtocolError, SlotFlightStreamError, SlotFlightValidationError } from "./errors.js";
|
|
3
|
+
export type { SlotDefinition, SlotFlightEvent, SlotFlightOptions, SlotFlightPrompt, SlotFlightPromptRequest, SlotFlightRequest, SlotFlightResult, SlotFlightRunOptions, SlotFrameRequest, SlotGenerator } from "./types.js";
|
|
4
|
+
//# sourceMappingURL=core.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"core.d.ts","sourceRoot":"","sources":["../src/core.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,UAAU,EACV,UAAU,EACX,MAAM,aAAa,CAAC;AACrB,OAAO,EACL,4BAA4B,EAC5B,eAAe,EACf,wBAAwB,EACxB,2BAA2B,EAC3B,qBAAqB,EACrB,yBAAyB,EAC1B,MAAM,aAAa,CAAC;AACrB,YAAY,EACV,cAAc,EACd,eAAe,EACf,iBAAiB,EACjB,gBAAgB,EAChB,uBAAuB,EACvB,iBAAiB,EACjB,gBAAgB,EAChB,oBAAoB,EACpB,gBAAgB,EAChB,aAAa,EACd,MAAM,YAAY,CAAC"}
|
package/dist/core.js
ADDED
package/dist/core.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"core.js","sourceRoot":"","sources":["../src/core.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,UAAU,EACV,UAAU,EACX,MAAM,aAAa,CAAC;AACrB,OAAO,EACL,4BAA4B,EAC5B,eAAe,EACf,wBAAwB,EACxB,2BAA2B,EAC3B,qBAAqB,EACrB,yBAAyB,EAC1B,MAAM,aAAa,CAAC"}
|
package/dist/engine.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { z } from "zod";
|
|
2
|
+
import type { SlotFlightEvent, SlotFlightOptions, SlotFlightResult, SlotFlightRunOptions } from "./types.js";
|
|
3
|
+
export { SlotFlightConfigurationError, SlotFlightError, SlotFlightJsonParseError, SlotFlightSlotProtocolError, SlotFlightStreamError, SlotFlightValidationError } from "./errors.js";
|
|
4
|
+
export declare class SlotFlight<TSchema extends z.ZodTypeAny> {
|
|
5
|
+
private readonly schema;
|
|
6
|
+
private readonly slots;
|
|
7
|
+
private readonly generateSlot;
|
|
8
|
+
private readonly prompt;
|
|
9
|
+
private readonly maxRetries;
|
|
10
|
+
constructor(options: SlotFlightOptions<TSchema>);
|
|
11
|
+
run(options?: SlotFlightRunOptions): AsyncGenerator<SlotFlightEvent, SlotFlightResult<z.infer<TSchema>>>;
|
|
12
|
+
}
|
|
13
|
+
export declare function slotFlight<TSchema extends z.ZodTypeAny>(options: SlotFlightOptions<TSchema>): SlotFlight<TSchema>;
|
|
14
|
+
//# sourceMappingURL=engine.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"engine.d.ts","sourceRoot":"","sources":["../src/engine.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAM7B,OAAO,KAAK,EACV,eAAe,EACf,iBAAiB,EACjB,gBAAgB,EAChB,oBAAoB,EAErB,MAAM,YAAY,CAAC;AAEpB,OAAO,EACL,4BAA4B,EAC5B,eAAe,EACf,wBAAwB,EACxB,2BAA2B,EAC3B,qBAAqB,EACrB,yBAAyB,EAC1B,MAAM,aAAa,CAAC;AAErB,qBAAa,UAAU,CAAC,OAAO,SAAS,CAAC,CAAC,UAAU;IAClD,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAU;IACjC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAiB;IACvC,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAgB;IAC7C,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAuC;IAC9D,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAS;gBAExB,OAAO,EAAE,iBAAiB,CAAC,OAAO,CAAC;IAQxC,GAAG,CACR,OAAO,GAAE,oBAAyB,GACjC,cAAc,CAAC,eAAe,EAAE,gBAAgB,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC;CAuBvE;AAED,wBAAgB,UAAU,CAAC,OAAO,SAAS,CAAC,CAAC,UAAU,EACrD,OAAO,EAAE,iBAAiB,CAAC,OAAO,CAAC,GAClC,UAAU,CAAC,OAAO,CAAC,CAErB"}
|
package/dist/engine.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { compileSlotPlan, runSlotFrameStream } from "./slot/index.js";
|
|
2
|
+
export { SlotFlightConfigurationError, SlotFlightError, SlotFlightJsonParseError, SlotFlightSlotProtocolError, SlotFlightStreamError, SlotFlightValidationError } from "./errors.js";
|
|
3
|
+
export class SlotFlight {
|
|
4
|
+
schema;
|
|
5
|
+
slots;
|
|
6
|
+
generateSlot;
|
|
7
|
+
prompt;
|
|
8
|
+
maxRetries;
|
|
9
|
+
constructor(options) {
|
|
10
|
+
this.schema = options.schema;
|
|
11
|
+
this.generateSlot = options.generate;
|
|
12
|
+
this.prompt = options.prompt;
|
|
13
|
+
this.maxRetries = options.maxRetries ?? 1;
|
|
14
|
+
this.slots = compileSlotPlan(options.slots);
|
|
15
|
+
}
|
|
16
|
+
async *run(options = {}) {
|
|
17
|
+
const state = {};
|
|
18
|
+
for await (const event of runSlotFrameStream({
|
|
19
|
+
slots: this.slots,
|
|
20
|
+
state,
|
|
21
|
+
generate: this.generateSlot,
|
|
22
|
+
prompt: this.prompt,
|
|
23
|
+
maxRetries: this.maxRetries,
|
|
24
|
+
signal: options.signal,
|
|
25
|
+
cloneState: cloneJson
|
|
26
|
+
})) {
|
|
27
|
+
yield event;
|
|
28
|
+
}
|
|
29
|
+
const parsed = this.schema.parse(state);
|
|
30
|
+
yield {
|
|
31
|
+
type: "done",
|
|
32
|
+
state: cloneJson(parsed)
|
|
33
|
+
};
|
|
34
|
+
return { state: parsed };
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
export function slotFlight(options) {
|
|
38
|
+
return new SlotFlight(options);
|
|
39
|
+
}
|
|
40
|
+
function cloneJson(value) {
|
|
41
|
+
return JSON.parse(JSON.stringify(value));
|
|
42
|
+
}
|
|
43
|
+
//# sourceMappingURL=engine.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"engine.js","sourceRoot":"","sources":["../src/engine.ts"],"names":[],"mappings":"AACA,OAAO,EAEL,eAAe,EACf,kBAAkB,EACnB,MAAM,iBAAiB,CAAC;AASzB,OAAO,EACL,4BAA4B,EAC5B,eAAe,EACf,wBAAwB,EACxB,2BAA2B,EAC3B,qBAAqB,EACrB,yBAAyB,EAC1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