@nixet/sdk 0.0.0-stage → 0.1.1
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 +50 -0
- package/LICENSE +21 -0
- package/README.md +870 -2
- package/SECURITY.md +30 -0
- package/api.md +485 -0
- package/dist/cjs/errors.d.ts +37 -0
- package/dist/cjs/errors.js +79 -0
- package/dist/cjs/index.d.ts +89 -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 +189 -0
- package/dist/cjs/resources/knowledge.js +218 -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 +212 -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 +59 -4
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tasks: work that outlives the request that asked for it.
|
|
3
|
+
*
|
|
4
|
+
* A run has to finish inside its HTTP request. A task does not: it is queued,
|
|
5
|
+
* executed by a worker, and polled for. Use it for anything that would
|
|
6
|
+
* otherwise time out — research, a long report, a batch of lookups.
|
|
7
|
+
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* const task = await nixet.tasks.create({
|
|
10
|
+
* agentId: agent.id,
|
|
11
|
+
* input: "Research this company and produce a report.",
|
|
12
|
+
* });
|
|
13
|
+
*
|
|
14
|
+
* const finished = await nixet.tasks.wait(task.id);
|
|
15
|
+
* console.log(finished.output);
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* The work runs as a normal run, so `task.run_id` is where the trace, the
|
|
19
|
+
* events and the cost live.
|
|
20
|
+
*/
|
|
21
|
+
export class Tasks {
|
|
22
|
+
transport;
|
|
23
|
+
constructor(transport) {
|
|
24
|
+
this.transport = transport;
|
|
25
|
+
}
|
|
26
|
+
/** Queue a task. Returns immediately, before the work starts. */
|
|
27
|
+
create(params, options = {}) {
|
|
28
|
+
return this.transport.request({
|
|
29
|
+
method: "POST",
|
|
30
|
+
path: "/tasks",
|
|
31
|
+
body: {
|
|
32
|
+
agent_id: params.agentId,
|
|
33
|
+
input: params.input,
|
|
34
|
+
...(params.version ? { agent_version: params.version } : {}),
|
|
35
|
+
...(params.conversationId ? { conversation_id: params.conversationId } : {}),
|
|
36
|
+
...(params.userId ? { user_id: params.userId } : {}),
|
|
37
|
+
...(params.provider ? { provider: params.provider } : {}),
|
|
38
|
+
...(params.memory ? { memory: params.memory } : {}),
|
|
39
|
+
...(params.maxAttempts ? { max_attempts: params.maxAttempts } : {}),
|
|
40
|
+
},
|
|
41
|
+
signal: options.signal,
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
retrieve(taskId, options = {}) {
|
|
45
|
+
return this.transport.request({
|
|
46
|
+
method: "GET",
|
|
47
|
+
path: `/tasks/${encodeURIComponent(taskId)}`,
|
|
48
|
+
signal: options.signal,
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
list(params = {}, options = {}) {
|
|
52
|
+
return this.transport.requestPage({
|
|
53
|
+
method: "GET",
|
|
54
|
+
path: "/tasks",
|
|
55
|
+
query: {
|
|
56
|
+
...(params.limit ? { $limit: params.limit } : {}),
|
|
57
|
+
...(params.offset ? { $offset: params.offset } : {}),
|
|
58
|
+
...(params.status ? { status: params.status } : {}),
|
|
59
|
+
...(params.agentId ? { agent_id: params.agentId } : {}),
|
|
60
|
+
...(params.conversationId ? { conversation_id: params.conversationId } : {}),
|
|
61
|
+
},
|
|
62
|
+
signal: options.signal,
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Stop a task.
|
|
67
|
+
*
|
|
68
|
+
* A queued task stops immediately. A running one is marked cancelled and the
|
|
69
|
+
* worker notices when its run finishes, because interrupting a model call
|
|
70
|
+
* mid-flight would still be billed for.
|
|
71
|
+
*/
|
|
72
|
+
cancel(taskId, options = {}) {
|
|
73
|
+
return this.transport.request({
|
|
74
|
+
method: "POST",
|
|
75
|
+
path: `/tasks/${encodeURIComponent(taskId)}/cancel`,
|
|
76
|
+
signal: options.signal,
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
/** Try a failed or cancelled task again, with its attempts reset. */
|
|
80
|
+
retry(taskId, options = {}) {
|
|
81
|
+
return this.transport.request({
|
|
82
|
+
method: "POST",
|
|
83
|
+
path: `/tasks/${encodeURIComponent(taskId)}/retry`,
|
|
84
|
+
signal: options.signal,
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Continue a paused task.
|
|
89
|
+
*
|
|
90
|
+
* Nothing pauses a task yet — that arrives with tools and approvals — so
|
|
91
|
+
* this currently answers that there is nothing to resume.
|
|
92
|
+
*/
|
|
93
|
+
resume(taskId, options = {}) {
|
|
94
|
+
return this.transport.request({
|
|
95
|
+
method: "POST",
|
|
96
|
+
path: `/tasks/${encodeURIComponent(taskId)}/resume`,
|
|
97
|
+
signal: options.signal,
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Poll until the task stops, and return it.
|
|
102
|
+
*
|
|
103
|
+
* Convenience over `retrieve`, not a different mechanism: there is no push
|
|
104
|
+
* for tasks yet, so this is the loop everyone would otherwise write. The
|
|
105
|
+
* timeout exists so a stuck task fails the caller rather than hanging.
|
|
106
|
+
*/
|
|
107
|
+
async wait(taskId, options = {}) {
|
|
108
|
+
const intervalMs = options.intervalMs ?? 2_000;
|
|
109
|
+
const deadline = Date.now() + (options.timeoutMs ?? 600_000);
|
|
110
|
+
for (;;) {
|
|
111
|
+
const task = await this.retrieve(taskId, { signal: options.signal });
|
|
112
|
+
options.onUpdate?.(task);
|
|
113
|
+
if (task.status === "completed" ||
|
|
114
|
+
task.status === "failed" ||
|
|
115
|
+
task.status === "cancelled") {
|
|
116
|
+
return task;
|
|
117
|
+
}
|
|
118
|
+
if (Date.now() >= deadline) {
|
|
119
|
+
throw new Error(`The task ${taskId} was still ${task.status} after the timeout. ` +
|
|
120
|
+
"It has not been cancelled; retrieve it later to see how it ended.");
|
|
121
|
+
}
|
|
122
|
+
await new Promise((resolve) => setTimeout(resolve, intervalMs));
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tools: what your servers offer, and what agents may do with each.
|
|
3
|
+
*
|
|
4
|
+
* ```ts
|
|
5
|
+
* const tools = await nixet.tools.list({ serverName: "github" });
|
|
6
|
+
*
|
|
7
|
+
* await nixet.agents.versions.create(agent.id, {
|
|
8
|
+
* instructions: "Use the GitHub tools to answer questions about the repo.",
|
|
9
|
+
* mcpServers: ["github"],
|
|
10
|
+
* toolPolicies: { "github.delete_repo": "deny" },
|
|
11
|
+
* });
|
|
12
|
+
* ```
|
|
13
|
+
*
|
|
14
|
+
* Read-only: a tool exists because a server advertised it, so there is nothing
|
|
15
|
+
* here to create. Discovery runs when a server is registered and again when its
|
|
16
|
+
* URL moves, which is why a server that could not be reached has none listed.
|
|
17
|
+
*
|
|
18
|
+
* Each tool carries `tags` and a `default_policy`, and the two are not the same
|
|
19
|
+
* kind of thing. A tag is a claim — taken from the server's own annotations
|
|
20
|
+
* where it published any, and from our reading of the name where it did not —
|
|
21
|
+
* and the MCP specification is explicit that a server can claim `readOnlyHint`
|
|
22
|
+
* and delete your files regardless. So tags decide what you are shown and which
|
|
23
|
+
* default applies. What actually happens is the policy on the version, which is
|
|
24
|
+
* yours to set.
|
|
25
|
+
*/
|
|
26
|
+
export class Tools {
|
|
27
|
+
transport;
|
|
28
|
+
constructor(transport) {
|
|
29
|
+
this.transport = transport;
|
|
30
|
+
}
|
|
31
|
+
/** One tool, by id. */
|
|
32
|
+
retrieve(toolId, options = {}) {
|
|
33
|
+
return this.transport.request({
|
|
34
|
+
method: "GET",
|
|
35
|
+
path: `/mcp/tools/${encodeURIComponent(toolId)}`,
|
|
36
|
+
signal: options.signal,
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
/** Every tool on record, or those of one server. */
|
|
40
|
+
list(params = {}, options = {}) {
|
|
41
|
+
return this.transport.requestPage({
|
|
42
|
+
method: "GET",
|
|
43
|
+
path: "/mcp/tools",
|
|
44
|
+
query: {
|
|
45
|
+
$limit: params.limit,
|
|
46
|
+
$offset: params.offset,
|
|
47
|
+
server_name: params.serverName,
|
|
48
|
+
name: params.name,
|
|
49
|
+
},
|
|
50
|
+
signal: options.signal,
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Users: the people your agent serves.
|
|
3
|
+
*
|
|
4
|
+
* Not the people who sign into the Nixet console — those are members. A user
|
|
5
|
+
* is yours: you supply the reference, and memory is isolated per user so one
|
|
6
|
+
* of your customers can never read another's.
|
|
7
|
+
*/
|
|
8
|
+
export class Users {
|
|
9
|
+
transport;
|
|
10
|
+
constructor(transport) {
|
|
11
|
+
this.transport = transport;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Register a user.
|
|
15
|
+
*
|
|
16
|
+
* ```ts
|
|
17
|
+
* const user = await nixet.users.create({ reference: "acme_user_8821" });
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
create(params, options = {}) {
|
|
21
|
+
return this.transport.request({
|
|
22
|
+
method: "POST",
|
|
23
|
+
path: "/users",
|
|
24
|
+
body: {
|
|
25
|
+
reference: params.reference,
|
|
26
|
+
...(params.email ? { email: params.email } : {}),
|
|
27
|
+
...(params.name ? { name: params.name } : {}),
|
|
28
|
+
...(params.metadata ? { metadata: params.metadata } : {}),
|
|
29
|
+
},
|
|
30
|
+
signal: options.signal,
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
retrieve(userId, options = {}) {
|
|
34
|
+
return this.transport.request({
|
|
35
|
+
method: "GET",
|
|
36
|
+
path: `/users/${encodeURIComponent(userId)}`,
|
|
37
|
+
signal: options.signal,
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
/** Filter by `reference` to find a person by your own identifier. */
|
|
41
|
+
list(params = {}, options = {}) {
|
|
42
|
+
return this.transport.requestPage({
|
|
43
|
+
method: "GET",
|
|
44
|
+
path: "/users",
|
|
45
|
+
query: {
|
|
46
|
+
$limit: params.limit,
|
|
47
|
+
$offset: params.offset,
|
|
48
|
+
reference: params.reference,
|
|
49
|
+
},
|
|
50
|
+
signal: options.signal,
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Immutable snapshots of an agent's configuration.
|
|
3
|
+
*
|
|
4
|
+
* A published version is never edited, which is what lets a past run be
|
|
5
|
+
* explained by the configuration it recorded.
|
|
6
|
+
*/
|
|
7
|
+
export class Versions {
|
|
8
|
+
transport;
|
|
9
|
+
constructor(transport) {
|
|
10
|
+
this.transport = transport;
|
|
11
|
+
}
|
|
12
|
+
/** Publish a new version. Returns it, numbered in order: v1, v2, v3. */
|
|
13
|
+
async create(agentId, params, options = {}) {
|
|
14
|
+
return this.transport.request({
|
|
15
|
+
method: "POST",
|
|
16
|
+
path: `/agents/${encodeURIComponent(agentId)}/versions`,
|
|
17
|
+
body: {
|
|
18
|
+
instructions: params.instructions,
|
|
19
|
+
...(params.model ? { model: params.model } : {}),
|
|
20
|
+
// A bare string is the common case, so accept it as shorthand.
|
|
21
|
+
...(params.tools ? { tools: params.tools.map(toToolRef) } : {}),
|
|
22
|
+
...(params.skills ? { skills: params.skills } : {}),
|
|
23
|
+
// Named in the SDK's own casing, so a caller is not asked to know
|
|
24
|
+
// which fields happen to reach the wire untouched.
|
|
25
|
+
...(params.redaction
|
|
26
|
+
? {
|
|
27
|
+
redaction: {
|
|
28
|
+
mode: params.redaction.mode,
|
|
29
|
+
...(params.redaction.reversible === undefined
|
|
30
|
+
? {}
|
|
31
|
+
: { reversible: params.redaction.reversible }),
|
|
32
|
+
...(params.redaction.minimumConfidence === undefined
|
|
33
|
+
? {}
|
|
34
|
+
: { minimum_confidence: params.redaction.minimumConfidence }),
|
|
35
|
+
...(params.redaction.labels ? { labels: params.redaction.labels } : {}),
|
|
36
|
+
...(params.redaction.retainFor
|
|
37
|
+
? { retain_for: params.redaction.retainFor }
|
|
38
|
+
: {}),
|
|
39
|
+
},
|
|
40
|
+
}
|
|
41
|
+
: {}),
|
|
42
|
+
...(params.mcpServers ? { mcp_servers: params.mcpServers } : {}),
|
|
43
|
+
...(params.toolPolicies ? { tool_policies: params.toolPolicies } : {}),
|
|
44
|
+
...(params.knowledgeCollections
|
|
45
|
+
? { knowledge_collections: params.knowledgeCollections }
|
|
46
|
+
: {}),
|
|
47
|
+
...(params.variables ? { variables: params.variables } : {}),
|
|
48
|
+
// Sent whole rather than field by field: the server defaults `mode`
|
|
49
|
+
// and `on_block`, and a partial object here would be a second place
|
|
50
|
+
// those defaults are decided.
|
|
51
|
+
...(params.guardrails ? { guardrails: params.guardrails } : {}),
|
|
52
|
+
...(params.publish === undefined ? {} : { publish: params.publish }),
|
|
53
|
+
},
|
|
54
|
+
signal: options.signal,
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
async list(agentId, params = {}, options = {}) {
|
|
58
|
+
return this.transport.requestPage({
|
|
59
|
+
method: "GET",
|
|
60
|
+
path: `/agents/${encodeURIComponent(agentId)}/versions`,
|
|
61
|
+
query: { $limit: params.limit, $offset: params.offset },
|
|
62
|
+
signal: options.signal,
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
/** Retrieve one version by its label, such as `v1`. */
|
|
66
|
+
async retrieve(agentId, version, options = {}) {
|
|
67
|
+
return this.transport.request({
|
|
68
|
+
method: "GET",
|
|
69
|
+
path: `/agents/${encodeURIComponent(agentId)}/versions/${encodeURIComponent(version)}`,
|
|
70
|
+
signal: options.signal,
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
const toToolRef = (tool) => typeof tool === "string" ? { name: tool } : tool;
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A live run.
|
|
3
|
+
*
|
|
4
|
+
* Iterate it for events, await `text()` for the finished output, or attach
|
|
5
|
+
* handlers. All three read the same underlying stream, so choose one style per
|
|
6
|
+
* run rather than mixing them.
|
|
7
|
+
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* for await (const event of stream) {
|
|
10
|
+
* if (event.type === "run.output") process.stdout.write(event.data ?? "");
|
|
11
|
+
* }
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
export class RunStream {
|
|
15
|
+
/**
|
|
16
|
+
* Resolves once the server acknowledges the run, before it finishes.
|
|
17
|
+
*
|
|
18
|
+
* Awaiting this starts reading the stream, so it is safe to call before
|
|
19
|
+
* iterating: the acceptance frame is consumed here and the events that follow
|
|
20
|
+
* are buffered for whichever reader comes next.
|
|
21
|
+
*/
|
|
22
|
+
runId;
|
|
23
|
+
response;
|
|
24
|
+
handlers = new Map();
|
|
25
|
+
resolveRunId;
|
|
26
|
+
rejectRunId;
|
|
27
|
+
consumed = false;
|
|
28
|
+
/** Events read while resolving `runId`, replayed to the first real reader. */
|
|
29
|
+
buffered = [];
|
|
30
|
+
reading = null;
|
|
31
|
+
reader = null;
|
|
32
|
+
pending = "";
|
|
33
|
+
decoder = new TextDecoder();
|
|
34
|
+
constructor(response) {
|
|
35
|
+
this.response = response;
|
|
36
|
+
this.runId = new Promise((resolve, reject) => {
|
|
37
|
+
this.resolveRunId = resolve;
|
|
38
|
+
this.rejectRunId = reject;
|
|
39
|
+
// The acceptance frame arrives first, so start reading rather than
|
|
40
|
+
// waiting for a consumer that may only appear after this is awaited.
|
|
41
|
+
queueMicrotask(() => this.pump());
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Subscribe to one event type, or to `"*"` for all of them.
|
|
46
|
+
* Returns an unsubscribe function.
|
|
47
|
+
*/
|
|
48
|
+
on(type, handler) {
|
|
49
|
+
const existing = this.handlers.get(type) ?? new Set();
|
|
50
|
+
existing.add(handler);
|
|
51
|
+
this.handlers.set(type, existing);
|
|
52
|
+
return () => existing.delete(handler);
|
|
53
|
+
}
|
|
54
|
+
/** The concatenated output, once the run finishes. */
|
|
55
|
+
async text() {
|
|
56
|
+
let output = "";
|
|
57
|
+
for await (const event of this) {
|
|
58
|
+
if (event.type === "run.output" && event.data)
|
|
59
|
+
output += event.data;
|
|
60
|
+
}
|
|
61
|
+
return output;
|
|
62
|
+
}
|
|
63
|
+
/** Only the text deltas, for writing straight to a terminal or a socket. */
|
|
64
|
+
async *chunks() {
|
|
65
|
+
for await (const event of this) {
|
|
66
|
+
if (event.type === "run.output" && event.data)
|
|
67
|
+
yield event.data;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
async *[Symbol.asyncIterator]() {
|
|
71
|
+
if (this.consumed) {
|
|
72
|
+
throw new Error("This run stream has already been read.");
|
|
73
|
+
}
|
|
74
|
+
this.consumed = true;
|
|
75
|
+
// Anything the run-id pump already read is replayed first, so no event is
|
|
76
|
+
// lost between acknowledgement and the first consumer.
|
|
77
|
+
await this.reading;
|
|
78
|
+
for (const event of this.buffered)
|
|
79
|
+
yield event;
|
|
80
|
+
this.buffered = [];
|
|
81
|
+
for (;;) {
|
|
82
|
+
const event = await this.next();
|
|
83
|
+
if (!event)
|
|
84
|
+
break;
|
|
85
|
+
this.emit(event);
|
|
86
|
+
yield event;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Read until the acceptance frame arrives, then stop.
|
|
91
|
+
*
|
|
92
|
+
* Runs once, so `runId` is available without a consumer while the events
|
|
93
|
+
* after it stay queued for the first reader.
|
|
94
|
+
*/
|
|
95
|
+
pump() {
|
|
96
|
+
if (this.reading)
|
|
97
|
+
return;
|
|
98
|
+
this.reading = (async () => {
|
|
99
|
+
try {
|
|
100
|
+
for (;;) {
|
|
101
|
+
const event = await this.next();
|
|
102
|
+
if (!event)
|
|
103
|
+
break;
|
|
104
|
+
this.buffered.push(event);
|
|
105
|
+
break;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
catch (error) {
|
|
109
|
+
this.rejectRunId(error);
|
|
110
|
+
}
|
|
111
|
+
})();
|
|
112
|
+
}
|
|
113
|
+
/** One event from the shared cursor, or null at end of stream. */
|
|
114
|
+
async next() {
|
|
115
|
+
if (!this.reader) {
|
|
116
|
+
const body = this.response.body;
|
|
117
|
+
if (!body)
|
|
118
|
+
throw new Error("The response carried no stream.");
|
|
119
|
+
this.reader = body.getReader();
|
|
120
|
+
}
|
|
121
|
+
for (;;) {
|
|
122
|
+
// A frame may already be buffered from a previous chunk.
|
|
123
|
+
let boundary = this.pending.indexOf("\n\n");
|
|
124
|
+
while (boundary !== -1) {
|
|
125
|
+
const frame = this.pending.slice(0, boundary).trim();
|
|
126
|
+
this.pending = this.pending.slice(boundary + 2);
|
|
127
|
+
boundary = this.pending.indexOf("\n\n");
|
|
128
|
+
const event = parse(frame);
|
|
129
|
+
if (!event)
|
|
130
|
+
continue;
|
|
131
|
+
if (isAcceptance(event)) {
|
|
132
|
+
this.resolveRunId(event.run_id);
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
return event;
|
|
136
|
+
}
|
|
137
|
+
const { done, value } = await this.reader.read();
|
|
138
|
+
if (done) {
|
|
139
|
+
// A stream that ends without acknowledging leaves `runId` pending.
|
|
140
|
+
this.rejectRunId(new Error("The run was never acknowledged."));
|
|
141
|
+
return null;
|
|
142
|
+
}
|
|
143
|
+
this.pending += this.decoder.decode(value, { stream: true });
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
emit(event) {
|
|
147
|
+
for (const handler of this.handlers.get(event.type) ?? [])
|
|
148
|
+
handler(event);
|
|
149
|
+
for (const handler of this.handlers.get("*") ?? [])
|
|
150
|
+
handler(event);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
function isAcceptance(event) {
|
|
154
|
+
return event.type === "run.accepted";
|
|
155
|
+
}
|
|
156
|
+
function parse(frame) {
|
|
157
|
+
if (!frame.startsWith("data:"))
|
|
158
|
+
return null;
|
|
159
|
+
try {
|
|
160
|
+
return JSON.parse(frame.slice(5).trim());
|
|
161
|
+
}
|
|
162
|
+
catch {
|
|
163
|
+
// A malformed frame is dropped rather than failing an otherwise good run.
|
|
164
|
+
return null;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { APIError, ConnectionError, RateLimitError, ServerError } from "./errors.js";
|
|
2
|
+
/**
|
|
3
|
+
* HTTP transport.
|
|
4
|
+
*
|
|
5
|
+
* Retries only what is safe to retry: a connection failure, a 429, or a 5xx.
|
|
6
|
+
* A 4xx means the request itself was wrong, so repeating it cannot help.
|
|
7
|
+
*/
|
|
8
|
+
export class Transport {
|
|
9
|
+
options;
|
|
10
|
+
constructor(options) {
|
|
11
|
+
this.options = options;
|
|
12
|
+
}
|
|
13
|
+
async request(request) {
|
|
14
|
+
const response = await this.send(request);
|
|
15
|
+
const text = await response.text();
|
|
16
|
+
const body = text ? JSON.parse(text) : undefined;
|
|
17
|
+
return body?.data;
|
|
18
|
+
}
|
|
19
|
+
/** List responses carry a total alongside the rows. */
|
|
20
|
+
async requestPage(request) {
|
|
21
|
+
const response = await this.send(request);
|
|
22
|
+
const body = (await response.json());
|
|
23
|
+
return { data: body.data, count: body.count };
|
|
24
|
+
}
|
|
25
|
+
async stream(request) {
|
|
26
|
+
return this.send({ ...request, stream: true });
|
|
27
|
+
}
|
|
28
|
+
async send(request) {
|
|
29
|
+
let lastError;
|
|
30
|
+
for (let attempt = 0; attempt <= this.options.maxRetries; attempt += 1) {
|
|
31
|
+
try {
|
|
32
|
+
const response = await this.attempt(request);
|
|
33
|
+
if (response.ok)
|
|
34
|
+
return response;
|
|
35
|
+
const error = await this.toError(response);
|
|
36
|
+
// A client mistake is not worth repeating; only pressure and outages are.
|
|
37
|
+
const retryable = error instanceof RateLimitError || error instanceof ServerError;
|
|
38
|
+
if (!retryable || attempt === this.options.maxRetries)
|
|
39
|
+
throw error;
|
|
40
|
+
lastError = error;
|
|
41
|
+
}
|
|
42
|
+
catch (error) {
|
|
43
|
+
if (error instanceof APIError) {
|
|
44
|
+
const retryable = error instanceof RateLimitError || error instanceof ServerError;
|
|
45
|
+
if (!retryable || attempt === this.options.maxRetries)
|
|
46
|
+
throw error;
|
|
47
|
+
}
|
|
48
|
+
else if (error?.name === "AbortError") {
|
|
49
|
+
throw error;
|
|
50
|
+
}
|
|
51
|
+
else if (attempt === this.options.maxRetries) {
|
|
52
|
+
throw new ConnectionError("Could not reach the Nixet API.", error);
|
|
53
|
+
}
|
|
54
|
+
lastError = error;
|
|
55
|
+
}
|
|
56
|
+
// Exponential backoff with jitter, so retries from many clients do not
|
|
57
|
+
// arrive together and repeat the overload that caused them.
|
|
58
|
+
const backoff = Math.min(500 * 2 ** attempt, 8000);
|
|
59
|
+
await sleep(backoff * (0.5 + Math.random() * 0.5));
|
|
60
|
+
}
|
|
61
|
+
throw lastError instanceof Error
|
|
62
|
+
? lastError
|
|
63
|
+
: new ConnectionError("Could not reach the Nixet API.");
|
|
64
|
+
}
|
|
65
|
+
async attempt(request) {
|
|
66
|
+
const url = new URL(this.options.baseURL.replace(/\/$/, "") + request.path);
|
|
67
|
+
for (const [key, value] of Object.entries(request.query ?? {})) {
|
|
68
|
+
if (value !== undefined)
|
|
69
|
+
url.searchParams.set(key, String(value));
|
|
70
|
+
}
|
|
71
|
+
const timeout = AbortSignal.timeout(this.options.timeout);
|
|
72
|
+
const signal = request.signal
|
|
73
|
+
? AbortSignal.any([request.signal, timeout])
|
|
74
|
+
: timeout;
|
|
75
|
+
return this.options.fetch(url.toString(), {
|
|
76
|
+
method: request.method,
|
|
77
|
+
headers: {
|
|
78
|
+
Authorization: `Bearer ${this.options.apiKey}`,
|
|
79
|
+
...(request.form ? {} : { "Content-Type": "application/json" }),
|
|
80
|
+
Accept: request.stream ? "text/event-stream" : "application/json",
|
|
81
|
+
},
|
|
82
|
+
...(request.body === undefined
|
|
83
|
+
? {}
|
|
84
|
+
: { body: request.form ? request.body : JSON.stringify(request.body) }),
|
|
85
|
+
signal,
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
async toError(response) {
|
|
89
|
+
const requestId = response.headers.get("x-request-id");
|
|
90
|
+
let message = `The request failed with status ${response.status}.`;
|
|
91
|
+
try {
|
|
92
|
+
const body = (await response.json());
|
|
93
|
+
if (body?.message)
|
|
94
|
+
message = body.message;
|
|
95
|
+
}
|
|
96
|
+
catch {
|
|
97
|
+
// A body that is not JSON leaves the status-derived message in place.
|
|
98
|
+
}
|
|
99
|
+
return APIError.from(response.status, message, requestId);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
package/package.json
CHANGED
|
@@ -1,6 +1,61 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nixet/sdk",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Official TypeScript SDK for the Nixet agent API: agents, versions, runs, streaming, tasks, memory, knowledge, MCP tools and guardrails.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"main": "dist/cjs/index.js",
|
|
7
|
+
"module": "dist/esm/index.js",
|
|
8
|
+
"types": "dist/cjs/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/cjs/index.d.ts",
|
|
12
|
+
"import": "./dist/esm/index.js",
|
|
13
|
+
"require": "./dist/cjs/index.js"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist",
|
|
18
|
+
"README.md",
|
|
19
|
+
"api.md",
|
|
20
|
+
"CHANGELOG.md",
|
|
21
|
+
"SECURITY.md",
|
|
22
|
+
"LICENSE"
|
|
23
|
+
],
|
|
24
|
+
"scripts": {
|
|
25
|
+
"build": "rm -rf dist && tsc -p tsconfig.json && tsc -p tsconfig.esm.json && node ../../scripts/build-esm.mjs",
|
|
26
|
+
"clean": "rm -rf dist"
|
|
27
|
+
},
|
|
28
|
+
"keywords": [
|
|
29
|
+
"nixet",
|
|
30
|
+
"agents",
|
|
31
|
+
"ai",
|
|
32
|
+
"ai-agents",
|
|
33
|
+
"llm",
|
|
34
|
+
"sdk",
|
|
35
|
+
"mcp",
|
|
36
|
+
"rag",
|
|
37
|
+
"streaming",
|
|
38
|
+
"typescript"
|
|
39
|
+
],
|
|
40
|
+
"engines": {
|
|
41
|
+
"node": ">=18"
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"typescript": "6.0.3"
|
|
45
|
+
},
|
|
46
|
+
"homepage": "https://nixet.ai",
|
|
47
|
+
"repository": {
|
|
48
|
+
"type": "git",
|
|
49
|
+
"url": "git+https://github.com/nixetio/nixet-sdk.git",
|
|
50
|
+
"directory": "packages/sdk"
|
|
51
|
+
},
|
|
52
|
+
"bugs": {
|
|
53
|
+
"url": "https://github.com/nixetio/nixet-sdk/issues"
|
|
54
|
+
},
|
|
55
|
+
"dependencies": {
|
|
56
|
+
"@nixet/contracts": "^0.1.1"
|
|
57
|
+
},
|
|
58
|
+
"publishConfig": {
|
|
59
|
+
"access": "public"
|
|
60
|
+
}
|
|
61
|
+
}
|