@nixet/sdk 0.0.0-stage → 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 +164 -2
- package/dist/cjs/errors.d.ts +37 -0
- package/dist/cjs/errors.js +79 -0
- package/dist/cjs/index.d.ts +88 -0
- package/dist/cjs/index.js +118 -0
- package/dist/cjs/resources/agents.d.ts +67 -0
- package/dist/cjs/resources/agents.js +74 -0
- package/dist/cjs/resources/connections.d.ts +94 -0
- package/dist/cjs/resources/connections.js +106 -0
- package/dist/cjs/resources/conversations.d.ts +62 -0
- package/dist/cjs/resources/conversations.js +86 -0
- package/dist/cjs/resources/documents.d.ts +65 -0
- package/dist/cjs/resources/documents.js +61 -0
- package/dist/cjs/resources/guardrails.d.ts +56 -0
- package/dist/cjs/resources/guardrails.js +76 -0
- package/dist/cjs/resources/knowledge.d.ts +192 -0
- package/dist/cjs/resources/knowledge.js +221 -0
- package/dist/cjs/resources/mcp-servers.d.ts +83 -0
- package/dist/cjs/resources/mcp-servers.js +107 -0
- package/dist/cjs/resources/memory.d.ts +95 -0
- package/dist/cjs/resources/memory.js +127 -0
- package/dist/cjs/resources/runs.d.ts +78 -0
- package/dist/cjs/resources/runs.js +87 -0
- package/dist/cjs/resources/secrets.d.ts +60 -0
- package/dist/cjs/resources/secrets.js +85 -0
- package/dist/cjs/resources/skills.d.ts +57 -0
- package/dist/cjs/resources/skills.js +78 -0
- package/dist/cjs/resources/tasks.d.ts +91 -0
- package/dist/cjs/resources/tasks.js +129 -0
- package/dist/cjs/resources/tools.d.ts +42 -0
- package/dist/cjs/resources/tools.js +57 -0
- package/dist/cjs/resources/users.d.ts +39 -0
- package/dist/cjs/resources/users.js +57 -0
- package/dist/cjs/resources/versions.d.ts +105 -0
- package/dist/cjs/resources/versions.js +78 -0
- package/dist/cjs/stream.d.ts +57 -0
- package/dist/cjs/stream.js +170 -0
- package/dist/cjs/transport.d.ts +45 -0
- package/dist/cjs/transport.js +106 -0
- package/dist/cjs/types.d.ts +537 -0
- package/dist/cjs/types.js +3 -0
- package/dist/esm/errors.js +66 -0
- package/dist/esm/index.js +103 -0
- package/dist/esm/package.json +1 -0
- package/dist/esm/resources/agents.js +70 -0
- package/dist/esm/resources/connections.js +102 -0
- package/dist/esm/resources/conversations.js +82 -0
- package/dist/esm/resources/documents.js +57 -0
- package/dist/esm/resources/guardrails.js +72 -0
- package/dist/esm/resources/knowledge.js +215 -0
- package/dist/esm/resources/mcp-servers.js +103 -0
- package/dist/esm/resources/memory.js +123 -0
- package/dist/esm/resources/runs.js +83 -0
- package/dist/esm/resources/secrets.js +81 -0
- package/dist/esm/resources/skills.js +74 -0
- package/dist/esm/resources/tasks.js +125 -0
- package/dist/esm/resources/tools.js +53 -0
- package/dist/esm/resources/users.js +53 -0
- package/dist/esm/resources/versions.js +74 -0
- package/dist/esm/stream.js +166 -0
- package/dist/esm/transport.js +102 -0
- package/dist/esm/types.js +2 -0
- package/package.json +42 -4
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RunStream = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* A live run.
|
|
6
|
+
*
|
|
7
|
+
* Iterate it for events, await `text()` for the finished output, or attach
|
|
8
|
+
* handlers. All three read the same underlying stream, so choose one style per
|
|
9
|
+
* run rather than mixing them.
|
|
10
|
+
*
|
|
11
|
+
* ```ts
|
|
12
|
+
* for await (const event of stream) {
|
|
13
|
+
* if (event.type === "run.output") process.stdout.write(event.data ?? "");
|
|
14
|
+
* }
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
class RunStream {
|
|
18
|
+
/**
|
|
19
|
+
* Resolves once the server acknowledges the run, before it finishes.
|
|
20
|
+
*
|
|
21
|
+
* Awaiting this starts reading the stream, so it is safe to call before
|
|
22
|
+
* iterating: the acceptance frame is consumed here and the events that follow
|
|
23
|
+
* are buffered for whichever reader comes next.
|
|
24
|
+
*/
|
|
25
|
+
runId;
|
|
26
|
+
response;
|
|
27
|
+
handlers = new Map();
|
|
28
|
+
resolveRunId;
|
|
29
|
+
rejectRunId;
|
|
30
|
+
consumed = false;
|
|
31
|
+
/** Events read while resolving `runId`, replayed to the first real reader. */
|
|
32
|
+
buffered = [];
|
|
33
|
+
reading = null;
|
|
34
|
+
reader = null;
|
|
35
|
+
pending = "";
|
|
36
|
+
decoder = new TextDecoder();
|
|
37
|
+
constructor(response) {
|
|
38
|
+
this.response = response;
|
|
39
|
+
this.runId = new Promise((resolve, reject) => {
|
|
40
|
+
this.resolveRunId = resolve;
|
|
41
|
+
this.rejectRunId = reject;
|
|
42
|
+
// The acceptance frame arrives first, so start reading rather than
|
|
43
|
+
// waiting for a consumer that may only appear after this is awaited.
|
|
44
|
+
queueMicrotask(() => this.pump());
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Subscribe to one event type, or to `"*"` for all of them.
|
|
49
|
+
* Returns an unsubscribe function.
|
|
50
|
+
*/
|
|
51
|
+
on(type, handler) {
|
|
52
|
+
const existing = this.handlers.get(type) ?? new Set();
|
|
53
|
+
existing.add(handler);
|
|
54
|
+
this.handlers.set(type, existing);
|
|
55
|
+
return () => existing.delete(handler);
|
|
56
|
+
}
|
|
57
|
+
/** The concatenated output, once the run finishes. */
|
|
58
|
+
async text() {
|
|
59
|
+
let output = "";
|
|
60
|
+
for await (const event of this) {
|
|
61
|
+
if (event.type === "run.output" && event.data)
|
|
62
|
+
output += event.data;
|
|
63
|
+
}
|
|
64
|
+
return output;
|
|
65
|
+
}
|
|
66
|
+
/** Only the text deltas, for writing straight to a terminal or a socket. */
|
|
67
|
+
async *chunks() {
|
|
68
|
+
for await (const event of this) {
|
|
69
|
+
if (event.type === "run.output" && event.data)
|
|
70
|
+
yield event.data;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
async *[Symbol.asyncIterator]() {
|
|
74
|
+
if (this.consumed) {
|
|
75
|
+
throw new Error("This run stream has already been read.");
|
|
76
|
+
}
|
|
77
|
+
this.consumed = true;
|
|
78
|
+
// Anything the run-id pump already read is replayed first, so no event is
|
|
79
|
+
// lost between acknowledgement and the first consumer.
|
|
80
|
+
await this.reading;
|
|
81
|
+
for (const event of this.buffered)
|
|
82
|
+
yield event;
|
|
83
|
+
this.buffered = [];
|
|
84
|
+
for (;;) {
|
|
85
|
+
const event = await this.next();
|
|
86
|
+
if (!event)
|
|
87
|
+
break;
|
|
88
|
+
this.emit(event);
|
|
89
|
+
yield event;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Read until the acceptance frame arrives, then stop.
|
|
94
|
+
*
|
|
95
|
+
* Runs once, so `runId` is available without a consumer while the events
|
|
96
|
+
* after it stay queued for the first reader.
|
|
97
|
+
*/
|
|
98
|
+
pump() {
|
|
99
|
+
if (this.reading)
|
|
100
|
+
return;
|
|
101
|
+
this.reading = (async () => {
|
|
102
|
+
try {
|
|
103
|
+
for (;;) {
|
|
104
|
+
const event = await this.next();
|
|
105
|
+
if (!event)
|
|
106
|
+
break;
|
|
107
|
+
this.buffered.push(event);
|
|
108
|
+
break;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
catch (error) {
|
|
112
|
+
this.rejectRunId(error);
|
|
113
|
+
}
|
|
114
|
+
})();
|
|
115
|
+
}
|
|
116
|
+
/** One event from the shared cursor, or null at end of stream. */
|
|
117
|
+
async next() {
|
|
118
|
+
if (!this.reader) {
|
|
119
|
+
const body = this.response.body;
|
|
120
|
+
if (!body)
|
|
121
|
+
throw new Error("The response carried no stream.");
|
|
122
|
+
this.reader = body.getReader();
|
|
123
|
+
}
|
|
124
|
+
for (;;) {
|
|
125
|
+
// A frame may already be buffered from a previous chunk.
|
|
126
|
+
let boundary = this.pending.indexOf("\n\n");
|
|
127
|
+
while (boundary !== -1) {
|
|
128
|
+
const frame = this.pending.slice(0, boundary).trim();
|
|
129
|
+
this.pending = this.pending.slice(boundary + 2);
|
|
130
|
+
boundary = this.pending.indexOf("\n\n");
|
|
131
|
+
const event = parse(frame);
|
|
132
|
+
if (!event)
|
|
133
|
+
continue;
|
|
134
|
+
if (isAcceptance(event)) {
|
|
135
|
+
this.resolveRunId(event.run_id);
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
return event;
|
|
139
|
+
}
|
|
140
|
+
const { done, value } = await this.reader.read();
|
|
141
|
+
if (done) {
|
|
142
|
+
// A stream that ends without acknowledging leaves `runId` pending.
|
|
143
|
+
this.rejectRunId(new Error("The run was never acknowledged."));
|
|
144
|
+
return null;
|
|
145
|
+
}
|
|
146
|
+
this.pending += this.decoder.decode(value, { stream: true });
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
emit(event) {
|
|
150
|
+
for (const handler of this.handlers.get(event.type) ?? [])
|
|
151
|
+
handler(event);
|
|
152
|
+
for (const handler of this.handlers.get("*") ?? [])
|
|
153
|
+
handler(event);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
exports.RunStream = RunStream;
|
|
157
|
+
function isAcceptance(event) {
|
|
158
|
+
return event.type === "run.accepted";
|
|
159
|
+
}
|
|
160
|
+
function parse(frame) {
|
|
161
|
+
if (!frame.startsWith("data:"))
|
|
162
|
+
return null;
|
|
163
|
+
try {
|
|
164
|
+
return JSON.parse(frame.slice(5).trim());
|
|
165
|
+
}
|
|
166
|
+
catch {
|
|
167
|
+
// A malformed frame is dropped rather than failing an otherwise good run.
|
|
168
|
+
return null;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
export interface TransportOptions {
|
|
2
|
+
apiKey: string;
|
|
3
|
+
baseURL: string;
|
|
4
|
+
timeout: number;
|
|
5
|
+
maxRetries: number;
|
|
6
|
+
fetch: typeof fetch;
|
|
7
|
+
}
|
|
8
|
+
export interface RequestOptions {
|
|
9
|
+
method: "GET" | "POST" | "PATCH" | "DELETE";
|
|
10
|
+
path: string;
|
|
11
|
+
query?: Record<string, unknown>;
|
|
12
|
+
body?: unknown;
|
|
13
|
+
/**
|
|
14
|
+
* Send the body as multipart rather than JSON.
|
|
15
|
+
*
|
|
16
|
+
* One route takes a file, and bytes do not belong in JSON. When set, the
|
|
17
|
+
* body is handed to fetch untouched and the Content-Type header is omitted:
|
|
18
|
+
* the runtime generates a boundary and sets it, and a header we wrote would
|
|
19
|
+
* be the wrong one.
|
|
20
|
+
*/
|
|
21
|
+
form?: boolean;
|
|
22
|
+
signal?: AbortSignal;
|
|
23
|
+
/** Ask for an event stream rather than JSON. */
|
|
24
|
+
stream?: boolean;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* HTTP transport.
|
|
28
|
+
*
|
|
29
|
+
* Retries only what is safe to retry: a connection failure, a 429, or a 5xx.
|
|
30
|
+
* A 4xx means the request itself was wrong, so repeating it cannot help.
|
|
31
|
+
*/
|
|
32
|
+
export declare class Transport {
|
|
33
|
+
private readonly options;
|
|
34
|
+
constructor(options: TransportOptions);
|
|
35
|
+
request<T>(request: RequestOptions): Promise<T>;
|
|
36
|
+
/** List responses carry a total alongside the rows. */
|
|
37
|
+
requestPage<T>(request: RequestOptions): Promise<{
|
|
38
|
+
data: T[];
|
|
39
|
+
count: number;
|
|
40
|
+
}>;
|
|
41
|
+
stream(request: RequestOptions): Promise<Response>;
|
|
42
|
+
private send;
|
|
43
|
+
private attempt;
|
|
44
|
+
private toError;
|
|
45
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Transport = void 0;
|
|
4
|
+
const errors_js_1 = require("./errors.js");
|
|
5
|
+
/**
|
|
6
|
+
* HTTP transport.
|
|
7
|
+
*
|
|
8
|
+
* Retries only what is safe to retry: a connection failure, a 429, or a 5xx.
|
|
9
|
+
* A 4xx means the request itself was wrong, so repeating it cannot help.
|
|
10
|
+
*/
|
|
11
|
+
class Transport {
|
|
12
|
+
options;
|
|
13
|
+
constructor(options) {
|
|
14
|
+
this.options = options;
|
|
15
|
+
}
|
|
16
|
+
async request(request) {
|
|
17
|
+
const response = await this.send(request);
|
|
18
|
+
const text = await response.text();
|
|
19
|
+
const body = text ? JSON.parse(text) : undefined;
|
|
20
|
+
return body?.data;
|
|
21
|
+
}
|
|
22
|
+
/** List responses carry a total alongside the rows. */
|
|
23
|
+
async requestPage(request) {
|
|
24
|
+
const response = await this.send(request);
|
|
25
|
+
const body = (await response.json());
|
|
26
|
+
return { data: body.data, count: body.count };
|
|
27
|
+
}
|
|
28
|
+
async stream(request) {
|
|
29
|
+
return this.send({ ...request, stream: true });
|
|
30
|
+
}
|
|
31
|
+
async send(request) {
|
|
32
|
+
let lastError;
|
|
33
|
+
for (let attempt = 0; attempt <= this.options.maxRetries; attempt += 1) {
|
|
34
|
+
try {
|
|
35
|
+
const response = await this.attempt(request);
|
|
36
|
+
if (response.ok)
|
|
37
|
+
return response;
|
|
38
|
+
const error = await this.toError(response);
|
|
39
|
+
// A client mistake is not worth repeating; only pressure and outages are.
|
|
40
|
+
const retryable = error instanceof errors_js_1.RateLimitError || error instanceof errors_js_1.ServerError;
|
|
41
|
+
if (!retryable || attempt === this.options.maxRetries)
|
|
42
|
+
throw error;
|
|
43
|
+
lastError = error;
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
if (error instanceof errors_js_1.APIError) {
|
|
47
|
+
const retryable = error instanceof errors_js_1.RateLimitError || error instanceof errors_js_1.ServerError;
|
|
48
|
+
if (!retryable || attempt === this.options.maxRetries)
|
|
49
|
+
throw error;
|
|
50
|
+
}
|
|
51
|
+
else if (error?.name === "AbortError") {
|
|
52
|
+
throw error;
|
|
53
|
+
}
|
|
54
|
+
else if (attempt === this.options.maxRetries) {
|
|
55
|
+
throw new errors_js_1.ConnectionError("Could not reach the Nixet API.", error);
|
|
56
|
+
}
|
|
57
|
+
lastError = error;
|
|
58
|
+
}
|
|
59
|
+
// Exponential backoff with jitter, so retries from many clients do not
|
|
60
|
+
// arrive together and repeat the overload that caused them.
|
|
61
|
+
const backoff = Math.min(500 * 2 ** attempt, 8000);
|
|
62
|
+
await sleep(backoff * (0.5 + Math.random() * 0.5));
|
|
63
|
+
}
|
|
64
|
+
throw lastError instanceof Error
|
|
65
|
+
? lastError
|
|
66
|
+
: new errors_js_1.ConnectionError("Could not reach the Nixet API.");
|
|
67
|
+
}
|
|
68
|
+
async attempt(request) {
|
|
69
|
+
const url = new URL(this.options.baseURL.replace(/\/$/, "") + request.path);
|
|
70
|
+
for (const [key, value] of Object.entries(request.query ?? {})) {
|
|
71
|
+
if (value !== undefined)
|
|
72
|
+
url.searchParams.set(key, String(value));
|
|
73
|
+
}
|
|
74
|
+
const timeout = AbortSignal.timeout(this.options.timeout);
|
|
75
|
+
const signal = request.signal
|
|
76
|
+
? AbortSignal.any([request.signal, timeout])
|
|
77
|
+
: timeout;
|
|
78
|
+
return this.options.fetch(url.toString(), {
|
|
79
|
+
method: request.method,
|
|
80
|
+
headers: {
|
|
81
|
+
Authorization: `Bearer ${this.options.apiKey}`,
|
|
82
|
+
...(request.form ? {} : { "Content-Type": "application/json" }),
|
|
83
|
+
Accept: request.stream ? "text/event-stream" : "application/json",
|
|
84
|
+
},
|
|
85
|
+
...(request.body === undefined
|
|
86
|
+
? {}
|
|
87
|
+
: { body: request.form ? request.body : JSON.stringify(request.body) }),
|
|
88
|
+
signal,
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
async toError(response) {
|
|
92
|
+
const requestId = response.headers.get("x-request-id");
|
|
93
|
+
let message = `The request failed with status ${response.status}.`;
|
|
94
|
+
try {
|
|
95
|
+
const body = (await response.json());
|
|
96
|
+
if (body?.message)
|
|
97
|
+
message = body.message;
|
|
98
|
+
}
|
|
99
|
+
catch {
|
|
100
|
+
// A body that is not JSON leaves the status-derived message in place.
|
|
101
|
+
}
|
|
102
|
+
return errors_js_1.APIError.from(response.status, message, requestId);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
exports.Transport = Transport;
|
|
106
|
+
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|