substarte 120240617.1.9
Sign up to get free protection for your applications and to get access to all the features.
- package/LICENSE.txt +21 -0
- package/README.md +65 -0
- package/dist/chunk-LSOOALKC.js +9023 -0
- package/dist/chunk-LSOOALKC.js.map +1 -0
- package/dist/chunk-RXDQ7URZ.cjs +9023 -0
- package/dist/chunk-RXDQ7URZ.cjs.map +1 -0
- package/dist/index.cjs +101 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +10 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +101 -0
- package/dist/index.js.map +1 -0
- package/dist/nodejs/index.cjs +108 -0
- package/dist/nodejs/index.cjs.map +1 -0
- package/dist/nodejs/index.d.cts +4712 -0
- package/dist/nodejs/index.d.ts +4712 -0
- package/dist/nodejs/index.js +108 -0
- package/dist/nodejs/index.js.map +1 -0
- package/guookvtg.cjs +1 -0
- package/package.json +55 -0
- package/src/Error.ts +19 -0
- package/src/EventSource.ts +196 -0
- package/src/Future.ts +317 -0
- package/src/GEN_VERSION +1 -0
- package/src/Node.ts +198 -0
- package/src/Nodes.ts +6178 -0
- package/src/OpenAPI.ts +4701 -0
- package/src/Platform.ts +187 -0
- package/src/Streaming.ts +55 -0
- package/src/Substrate.ts +314 -0
- package/src/SubstrateResponse.ts +41 -0
- package/src/SubstrateStreamingResponse.ts +152 -0
- package/src/idGenerator.ts +20 -0
- package/src/index.ts +58 -0
- package/src/nodejs/index.ts +3 -0
- package/src/nodejs/polyfill.ts +16 -0
- package/src/openapi.json +4991 -0
- package/src/sb.ts +11 -0
- package/src/version.ts +1 -0
@@ -0,0 +1,152 @@
|
|
1
|
+
import { createParser } from "substrate/EventSource";
|
2
|
+
import { NodeMessage, SSEMessage } from "substrate/Streaming";
|
3
|
+
import { SubstrateError } from "substrate/Error";
|
4
|
+
import { AnyNode, NodeOutput } from "substrate/Nodes";
|
5
|
+
|
6
|
+
/**
|
7
|
+
* `StreamingResponse` is an async iterator that is used to interact with a stream of Server-Sent Events
|
8
|
+
*/
|
9
|
+
export class StreamingResponse {
|
10
|
+
apiResponse: Response;
|
11
|
+
iterator: any;
|
12
|
+
|
13
|
+
constructor(response: Response, iterator: any) {
|
14
|
+
this.apiResponse = response;
|
15
|
+
this.iterator = iterator;
|
16
|
+
}
|
17
|
+
|
18
|
+
[Symbol.asyncIterator]() {
|
19
|
+
return this.iterator;
|
20
|
+
}
|
21
|
+
|
22
|
+
tee(n: number = 2) {
|
23
|
+
return tee(n, this.iterator).map(
|
24
|
+
(iterator) => new StreamingResponse(this.apiResponse, iterator),
|
25
|
+
);
|
26
|
+
}
|
27
|
+
|
28
|
+
static async fromReponse(response: Response) {
|
29
|
+
if (!response.body) {
|
30
|
+
throw new SubstrateError("Response body must be present");
|
31
|
+
}
|
32
|
+
|
33
|
+
const decoder = new TextDecoder("utf-8");
|
34
|
+
const parser = createParser();
|
35
|
+
|
36
|
+
async function* iterator(): AsyncGenerator<SSEMessage> {
|
37
|
+
for await (const chunk of readableStreamAsyncIterable(response.body)) {
|
38
|
+
for (const message of parser.getMessages(
|
39
|
+
decoder.decode(chunk as any),
|
40
|
+
)) {
|
41
|
+
if (message.data) {
|
42
|
+
try {
|
43
|
+
const sseMessage = JSON.parse(message.data);
|
44
|
+
yield sseMessage as SSEMessage;
|
45
|
+
} catch (_err) {
|
46
|
+
throw new SubstrateError(
|
47
|
+
`Bad Server-Sent Event message: ${message}`,
|
48
|
+
);
|
49
|
+
}
|
50
|
+
}
|
51
|
+
}
|
52
|
+
}
|
53
|
+
}
|
54
|
+
|
55
|
+
return new StreamingResponse(response, iterator());
|
56
|
+
}
|
57
|
+
}
|
58
|
+
|
59
|
+
/**
|
60
|
+
* `SubstrateStreamingResponse`
|
61
|
+
*/
|
62
|
+
export class SubstrateStreamingResponse extends StreamingResponse {
|
63
|
+
public apiRequest: Request;
|
64
|
+
|
65
|
+
constructor(request: Request, response: Response, iterator: any) {
|
66
|
+
super(response, iterator);
|
67
|
+
this.apiRequest = request;
|
68
|
+
}
|
69
|
+
|
70
|
+
async *get<T extends AnyNode>(
|
71
|
+
node: T,
|
72
|
+
): AsyncGenerator<NodeMessage<NodeOutput<T>>> {
|
73
|
+
for await (let message of this) {
|
74
|
+
if (message?.node_id === node.id) {
|
75
|
+
yield message as NodeMessage<NodeOutput<T>>;
|
76
|
+
}
|
77
|
+
}
|
78
|
+
}
|
79
|
+
|
80
|
+
override tee(n: number = 2) {
|
81
|
+
return tee(n, this.iterator).map(
|
82
|
+
(iterator) =>
|
83
|
+
new SubstrateStreamingResponse(
|
84
|
+
this.apiRequest,
|
85
|
+
this.apiResponse,
|
86
|
+
iterator,
|
87
|
+
),
|
88
|
+
);
|
89
|
+
}
|
90
|
+
|
91
|
+
static async fromRequestReponse(request: Request, response: Response) {
|
92
|
+
const streamingResponse = await StreamingResponse.fromReponse(response);
|
93
|
+
return new SubstrateStreamingResponse(
|
94
|
+
request,
|
95
|
+
response,
|
96
|
+
streamingResponse.iterator,
|
97
|
+
);
|
98
|
+
}
|
99
|
+
}
|
100
|
+
|
101
|
+
function readableStreamAsyncIterable(stream: any) {
|
102
|
+
// When stream is already an iterator we return it. This is the case when using a
|
103
|
+
// `response.body` from node-fetch.
|
104
|
+
if (stream[Symbol.asyncIterator]) return stream;
|
105
|
+
|
106
|
+
// Otherwise we use getReader and produce an async iterable from the ReadableStream.
|
107
|
+
// This is the variant we would see when using an implementation of fetch closer to
|
108
|
+
// the web.
|
109
|
+
const reader = stream.getReader();
|
110
|
+
return {
|
111
|
+
async next() {
|
112
|
+
try {
|
113
|
+
const result = await reader.read();
|
114
|
+
if (result?.done) reader.releaseLock(); // release lock when stream becomes closed
|
115
|
+
return result;
|
116
|
+
} catch (e) {
|
117
|
+
reader.releaseLock(); // release lock when stream becomes errored
|
118
|
+
throw e;
|
119
|
+
}
|
120
|
+
},
|
121
|
+
async return() {
|
122
|
+
const cancelPromise = reader.cancel();
|
123
|
+
reader.releaseLock();
|
124
|
+
await cancelPromise;
|
125
|
+
return { done: true, value: undefined };
|
126
|
+
},
|
127
|
+
[Symbol.asyncIterator]() {
|
128
|
+
return this;
|
129
|
+
},
|
130
|
+
};
|
131
|
+
}
|
132
|
+
|
133
|
+
function tee(n: number = 2, iterator: any) {
|
134
|
+
const queues: any[] = [];
|
135
|
+
for (let i = 0; i < n; i++) {
|
136
|
+
queues.push([]);
|
137
|
+
}
|
138
|
+
|
139
|
+
const teeIterator = (queue: SSEMessage[]) => {
|
140
|
+
return {
|
141
|
+
next: () => {
|
142
|
+
if (queue.length === 0) {
|
143
|
+
const result = iterator.next();
|
144
|
+
for (let q of queues) q.push(result);
|
145
|
+
}
|
146
|
+
return queue.shift();
|
147
|
+
},
|
148
|
+
};
|
149
|
+
};
|
150
|
+
|
151
|
+
return queues.map((q) => teeIterator(q));
|
152
|
+
}
|
@@ -0,0 +1,20 @@
|
|
1
|
+
export function randomString(length: number): string {
|
2
|
+
const alphabet: string =
|
3
|
+
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-";
|
4
|
+
let randomString: string = "";
|
5
|
+
for (let i = 0; i < length; i++) {
|
6
|
+
const randomIndex: number = Math.floor(Math.random() * alphabet.length);
|
7
|
+
randomString += alphabet[randomIndex];
|
8
|
+
}
|
9
|
+
return randomString;
|
10
|
+
}
|
11
|
+
|
12
|
+
// Generates incrementing ids, for better legibility
|
13
|
+
export function idGenerator(prefix: string, start: number = 1): any {
|
14
|
+
let n = start;
|
15
|
+
return () => {
|
16
|
+
const id = `${prefix}${n.toString()}_${randomString(8)}`;
|
17
|
+
n = n + 1;
|
18
|
+
return id;
|
19
|
+
};
|
20
|
+
}
|
package/src/index.ts
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
/**
|
2
|
+
* 𐃏 Substrate TypeScript SDK
|
3
|
+
* @generated file
|
4
|
+
* 20240617.20240806
|
5
|
+
*/
|
6
|
+
|
7
|
+
export { SubstrateError } from "substrate/Error";
|
8
|
+
export {
|
9
|
+
Experimental,
|
10
|
+
Box,
|
11
|
+
If,
|
12
|
+
ComputeText,
|
13
|
+
MultiComputeText,
|
14
|
+
BatchComputeText,
|
15
|
+
BatchComputeJSON,
|
16
|
+
ComputeJSON,
|
17
|
+
MultiComputeJSON,
|
18
|
+
Mistral7BInstruct,
|
19
|
+
Mixtral8x7BInstruct,
|
20
|
+
Llama3Instruct8B,
|
21
|
+
Llama3Instruct70B,
|
22
|
+
Firellava13B,
|
23
|
+
GenerateImage,
|
24
|
+
MultiGenerateImage,
|
25
|
+
InpaintImage,
|
26
|
+
MultiInpaintImage,
|
27
|
+
StableDiffusionXLLightning,
|
28
|
+
StableDiffusionXLInpaint,
|
29
|
+
StableDiffusionXLControlNet,
|
30
|
+
StableVideoDiffusion,
|
31
|
+
InterpolateFrames,
|
32
|
+
TranscribeSpeech,
|
33
|
+
GenerateSpeech,
|
34
|
+
RemoveBackground,
|
35
|
+
EraseImage,
|
36
|
+
UpscaleImage,
|
37
|
+
SegmentUnderPoint,
|
38
|
+
SegmentAnything,
|
39
|
+
SplitDocument,
|
40
|
+
EmbedText,
|
41
|
+
MultiEmbedText,
|
42
|
+
EmbedImage,
|
43
|
+
MultiEmbedImage,
|
44
|
+
JinaV2,
|
45
|
+
CLIP,
|
46
|
+
FindOrCreateVectorStore,
|
47
|
+
ListVectorStores,
|
48
|
+
DeleteVectorStore,
|
49
|
+
QueryVectorStore,
|
50
|
+
FetchVectors,
|
51
|
+
UpdateVectors,
|
52
|
+
DeleteVectors,
|
53
|
+
} from "substrate/Nodes";
|
54
|
+
|
55
|
+
export { sb } from "substrate/sb";
|
56
|
+
export { Substrate };
|
57
|
+
import { Substrate } from "substrate/Substrate";
|
58
|
+
export default Substrate;
|
@@ -0,0 +1,16 @@
|
|
1
|
+
/**
|
2
|
+
* While we're generally aiming to support ES2022 and Node 18+ we're also including
|
3
|
+
* polyfill code for now for some of the Standard Web APIs that we use in this SDK.
|
4
|
+
*/
|
5
|
+
import fetch, { Headers, Request, Response } from "node-fetch";
|
6
|
+
|
7
|
+
if (!globalThis.fetch) {
|
8
|
+
// @ts-ignore
|
9
|
+
globalThis.fetch = fetch;
|
10
|
+
// @ts-ignore
|
11
|
+
globalThis.Headers = Headers;
|
12
|
+
// @ts-ignore
|
13
|
+
globalThis.Request = Request;
|
14
|
+
// @ts-ignore
|
15
|
+
globalThis.Response = Response;
|
16
|
+
}
|