@blazingagents/sdk 0.19.0 → 0.21.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/README.md +17 -1
- package/dist/approvals.d.ts +4 -12
- package/dist/approvals.js +12 -13
- package/dist/chat-transport.d.ts +18 -1
- package/dist/chat-transport.js +16 -0
- package/dist/client.d.ts +28 -5
- package/dist/client.js +39 -7
- package/dist/contracts/api.d.ts +40 -6
- package/dist/contracts/api.js +30 -2
- package/dist/contracts/entities/agent-tools.d.ts +5 -2
- package/dist/contracts/entities/agent-tools.js +5 -2
- package/dist/contracts/entities/agents.d.ts +4 -0
- package/dist/contracts/entities/agents.js +9 -0
- package/dist/contracts/entities/chat.d.ts +11 -10
- package/dist/contracts/entities/chat.js +11 -7
- package/dist/contracts/entities/mcp-connections.d.ts +3 -1
- package/dist/contracts/entities/mcp-connections.js +3 -1
- package/dist/contracts/entities/prompts.d.ts +13 -0
- package/dist/contracts/entities/prompts.js +13 -0
- package/dist/contracts/entities/session-inputs.d.ts +29 -87
- package/dist/contracts/entities/session-inputs.js +21 -19
- package/dist/contracts/entities/sessions.d.ts +27 -17
- package/dist/contracts/entities/sessions.js +23 -8
- package/dist/contracts/entities/skills.d.ts +3 -0
- package/dist/contracts/entities/skills.js +3 -0
- package/dist/contracts/entities/tasks.d.ts +1 -0
- package/dist/contracts/entities/usage.d.ts +4 -1
- package/dist/contracts/entities/usage.js +7 -1
- package/dist/contracts/entities/workspaces.d.ts +17 -6
- package/dist/contracts/entities/workspaces.js +3 -7
- package/dist/contracts/ids.d.ts +31 -0
- package/dist/contracts/ids.js +34 -0
- package/dist/contracts/index.d.ts +7 -6
- package/dist/contracts/index.js +4 -4
- package/dist/contracts/utils.d.ts +10 -0
- package/dist/contracts/utils.js +10 -0
- package/dist/direct-chat-transport.d.ts +30 -3
- package/dist/direct-chat-transport.js +40 -35
- package/dist/errors.d.ts +9 -0
- package/dist/errors.js +9 -0
- package/dist/functions.d.ts +35 -2
- package/dist/functions.js +60 -4
- package/dist/generation.d.ts +34 -2
- package/dist/generation.js +97 -19
- package/dist/http.d.ts +26 -0
- package/dist/http.js +43 -0
- package/dist/index.d.ts +4 -4
- package/dist/relay.d.ts +26 -3
- package/dist/relay.js +25 -10
- package/dist/resources/agents.d.ts +5 -0
- package/dist/resources/agents.js +38 -0
- package/dist/resources/artifacts.d.ts +2 -0
- package/dist/resources/artifacts.js +14 -0
- package/dist/resources/chat-connections.d.ts +5 -0
- package/dist/resources/chat-connections.js +32 -0
- package/dist/resources/chat-deliveries.d.ts +2 -0
- package/dist/resources/chat-deliveries.js +5 -0
- package/dist/resources/mcp-connections.d.ts +5 -0
- package/dist/resources/mcp-connections.js +26 -0
- package/dist/resources/memories.d.ts +2 -0
- package/dist/resources/memories.js +17 -0
- package/dist/resources/merchant-bindings.d.ts +2 -0
- package/dist/resources/merchant-bindings.js +11 -0
- package/dist/resources/merchant-connection.d.ts +2 -0
- package/dist/resources/merchant-connection.js +14 -0
- package/dist/resources/merchant-usage-events.d.ts +2 -0
- package/dist/resources/merchant-usage-events.js +20 -0
- package/dist/resources/prompts.d.ts +5 -0
- package/dist/resources/prompts.js +20 -0
- package/dist/resources/providers.d.ts +2 -0
- package/dist/resources/providers.js +23 -0
- package/dist/resources/sessions.d.ts +2 -0
- package/dist/resources/sessions.js +42 -39
- package/dist/resources/skills.d.ts +5 -0
- package/dist/resources/skills.js +35 -0
- package/dist/resources/tasks.d.ts +2 -0
- package/dist/resources/tasks.js +32 -0
- package/dist/resources/tenant.d.ts +2 -0
- package/dist/resources/tenant.js +8 -0
- package/dist/resources/usage.d.ts +2 -0
- package/dist/resources/usage.js +14 -0
- package/dist/resources/workspaces.d.ts +5 -0
- package/dist/resources/workspaces.js +20 -0
- package/dist/types.d.ts +691 -48
- package/package.json +1 -1
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
import { DefaultChatTransport, lastAssistantMessageIsCompleteWithApprovalResponses, } from "ai";
|
|
2
|
-
import {
|
|
2
|
+
import { extractApprovalDecisions } from "./approvals.js";
|
|
3
3
|
import { sessionIdSchema } from "./contracts/ids.js";
|
|
4
4
|
/** Connects useChat directly to the SDK, including native streaming fetch clients. */
|
|
5
5
|
export class BlazingAgentsDirectChatTransport extends DefaultChatTransport {
|
|
6
6
|
#options;
|
|
7
7
|
#sessionId;
|
|
8
|
+
/**
|
|
9
|
+
* Creates a chat transport and validates any supplied Session ID.
|
|
10
|
+
* @param options - Configuration for this operation.
|
|
11
|
+
*/
|
|
8
12
|
constructor(options) {
|
|
9
13
|
super();
|
|
10
14
|
this.#options = options;
|
|
@@ -13,19 +17,28 @@ export class BlazingAgentsDirectChatTransport extends DefaultChatTransport {
|
|
|
13
17
|
? undefined
|
|
14
18
|
: sessionIdSchema.parse(options.sessionId);
|
|
15
19
|
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
+
/**
|
|
21
|
+
* Submits the supplied user messages directly to the SDK chat endpoint.
|
|
22
|
+
* @param input - Operation input and optional cancellation signal.
|
|
23
|
+
* @returns The decoded AI SDK message-chunk stream.
|
|
24
|
+
*/
|
|
25
|
+
async sendUserMessages(input) {
|
|
20
26
|
const client = await this.#options.getClient();
|
|
21
|
-
const result = await client.
|
|
27
|
+
const result = await client.chat({
|
|
22
28
|
...input,
|
|
23
29
|
agentId: this.#options.agentId,
|
|
24
30
|
sessionId: this.#sessionId,
|
|
25
31
|
functions: this.#options.functions,
|
|
32
|
+
userId: this.#options.userId,
|
|
33
|
+
metadata: this.#options.metadata,
|
|
26
34
|
});
|
|
27
|
-
return this
|
|
35
|
+
return this.#processChatResult(result);
|
|
28
36
|
}
|
|
37
|
+
/**
|
|
38
|
+
* Submits the latest user message or complete assistant approval round to the chat.
|
|
39
|
+
* @param input - Operation input and optional cancellation signal.
|
|
40
|
+
* @returns The decoded AI SDK message-chunk stream.
|
|
41
|
+
*/
|
|
29
42
|
async sendMessages(input) {
|
|
30
43
|
if (input.trigger === "regenerate-message" && !this.#sessionId) {
|
|
31
44
|
throw new Error("Regeneration requires an existing Session.");
|
|
@@ -79,6 +92,12 @@ export class BlazingAgentsDirectChatTransport extends DefaultChatTransport {
|
|
|
79
92
|
trigger: input.trigger,
|
|
80
93
|
});
|
|
81
94
|
}
|
|
95
|
+
return this.#processChatResult(result);
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Claims the chat stream and records the first Session ID before decoding chunks.
|
|
99
|
+
*/
|
|
100
|
+
async #processChatResult(result) {
|
|
82
101
|
const stream = result.toStream();
|
|
83
102
|
try {
|
|
84
103
|
const sessionId = await result.sessionId;
|
|
@@ -93,26 +112,18 @@ export class BlazingAgentsDirectChatTransport extends DefaultChatTransport {
|
|
|
93
112
|
}
|
|
94
113
|
return this.processResponseStream(stream);
|
|
95
114
|
}
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
sessionId: this.#sessionId,
|
|
104
|
-
});
|
|
105
|
-
if (continuation?.state !== "queued" && continuation?.state !== "running") {
|
|
106
|
-
return null;
|
|
107
|
-
}
|
|
108
|
-
const result = await client.resumeChat({
|
|
109
|
-
agentId: this.#options.agentId,
|
|
110
|
-
sessionId: this.#sessionId,
|
|
111
|
-
continuationId: continuation.id,
|
|
112
|
-
functions: this.#options.functions ?? {},
|
|
113
|
-
});
|
|
114
|
-
return this.processResponseStream(result.toStream());
|
|
115
|
+
/**
|
|
116
|
+
* Returns null because reconnecting to an existing stream is unsupported.
|
|
117
|
+
* @param _input - Unused reconnect request.
|
|
118
|
+
* @returns A promise resolving to null.
|
|
119
|
+
*/
|
|
120
|
+
reconnectToStream(_input) {
|
|
121
|
+
return Promise.resolve(null);
|
|
115
122
|
}
|
|
123
|
+
/**
|
|
124
|
+
* Submits the last assistant approval round for the existing Session.
|
|
125
|
+
* @param input - Operation input and optional cancellation signal.
|
|
126
|
+
*/
|
|
116
127
|
async #sendApprovalContinuation(input, last) {
|
|
117
128
|
if (this.#sessionId === undefined) {
|
|
118
129
|
throw new Error("Tool approval requires an existing Session.");
|
|
@@ -123,16 +134,10 @@ export class BlazingAgentsDirectChatTransport extends DefaultChatTransport {
|
|
|
123
134
|
sessionId: this.#sessionId,
|
|
124
135
|
abortSignal: input.abortSignal,
|
|
125
136
|
};
|
|
126
|
-
const
|
|
127
|
-
|
|
128
|
-
message: last,
|
|
129
|
-
});
|
|
130
|
-
if (continuationId === undefined) {
|
|
131
|
-
throw new Error("Tool approval response is missing an approval.");
|
|
132
|
-
}
|
|
133
|
-
const continuation = await client.resumeChat({
|
|
137
|
+
const decisions = extractApprovalDecisions(last);
|
|
138
|
+
const continuation = await client.continueChat({
|
|
134
139
|
...target,
|
|
135
|
-
|
|
140
|
+
decisions,
|
|
136
141
|
functions: this.#options.functions ?? {},
|
|
137
142
|
});
|
|
138
143
|
return this.processResponseStream(continuation.toStream());
|
package/dist/errors.d.ts
CHANGED
|
@@ -17,6 +17,10 @@ export declare class BlazingAgentsError extends Error {
|
|
|
17
17
|
* biome-ignore lint/correctness/noUnusedPrivateClassMembers: marker pattern
|
|
18
18
|
*/
|
|
19
19
|
private readonly [BLAZING_AGENTS_ERROR_SYMBOL];
|
|
20
|
+
/**
|
|
21
|
+
* Creates an SDK error with its code, diagnostics, and optional cause.
|
|
22
|
+
* @param options - Configuration for this operation.
|
|
23
|
+
*/
|
|
20
24
|
constructor({ code, details, headers, message, param, requestId, responseBody, responseBodyTruncated, status, }: {
|
|
21
25
|
code: BlazingAgentsErrorCode;
|
|
22
26
|
details?: Record<string, unknown>;
|
|
@@ -28,6 +32,11 @@ export declare class BlazingAgentsError extends Error {
|
|
|
28
32
|
responseBodyTruncated?: boolean;
|
|
29
33
|
status?: number;
|
|
30
34
|
}, options?: ErrorOptions);
|
|
35
|
+
/**
|
|
36
|
+
* Checks the shared symbol marker, including errors from duplicated SDK packages.
|
|
37
|
+
* @param error - Value to inspect or handle.
|
|
38
|
+
* @returns Whether the value carries the SDK error marker.
|
|
39
|
+
*/
|
|
31
40
|
static isInstance(error: unknown): error is BlazingAgentsError;
|
|
32
41
|
}
|
|
33
42
|
export {};
|
package/dist/errors.js
CHANGED
|
@@ -27,6 +27,10 @@ export class BlazingAgentsError extends Error {
|
|
|
27
27
|
*/
|
|
28
28
|
[BLAZING_AGENTS_ERROR_SYMBOL] = true;
|
|
29
29
|
// Stryker restore BooleanLiteral
|
|
30
|
+
/**
|
|
31
|
+
* Creates an SDK error with its code, diagnostics, and optional cause.
|
|
32
|
+
* @param options - Configuration for this operation.
|
|
33
|
+
*/
|
|
30
34
|
constructor({ code, details, headers, message, param, requestId, responseBody, responseBodyTruncated, status, }, options) {
|
|
31
35
|
super(`[${code}] ${message}`, options);
|
|
32
36
|
this.name = BLAZING_AGENTS_ERROR_NAME;
|
|
@@ -39,6 +43,11 @@ export class BlazingAgentsError extends Error {
|
|
|
39
43
|
this.responseBodyTruncated = responseBodyTruncated;
|
|
40
44
|
this.status = status;
|
|
41
45
|
}
|
|
46
|
+
/**
|
|
47
|
+
* Checks the shared symbol marker, including errors from duplicated SDK packages.
|
|
48
|
+
* @param error - Value to inspect or handle.
|
|
49
|
+
* @returns Whether the value carries the SDK error marker.
|
|
50
|
+
*/
|
|
42
51
|
static isInstance(error) {
|
|
43
52
|
if (error === null || typeof error !== "object") {
|
|
44
53
|
return false;
|
package/dist/functions.d.ts
CHANGED
|
@@ -9,12 +9,28 @@ export interface ChatFunctionContext {
|
|
|
9
9
|
}
|
|
10
10
|
export interface ChatFunction<Schema extends z.ZodType = z.ZodType, Output = unknown> {
|
|
11
11
|
description: string;
|
|
12
|
+
/**
|
|
13
|
+
* Runs a caller-local function with validated input and deadline cancellation.
|
|
14
|
+
* @param input - Input parsed by inputSchema.
|
|
15
|
+
* @param context - Function-call idempotency key and cancellation signal.
|
|
16
|
+
* @returns The function output, synchronously or as a promise.
|
|
17
|
+
*/
|
|
12
18
|
execute(input: z.output<Schema>, context: ChatFunctionContext): Output | Promise<Output>;
|
|
13
19
|
inputSchema: Schema;
|
|
14
20
|
}
|
|
15
21
|
export type ChatFunctions = Record<string, ChatFunction>;
|
|
16
|
-
/**
|
|
22
|
+
/**
|
|
23
|
+
* Declares a caller-local function whose handler input is inferred from its Zod schema.
|
|
24
|
+
* @param definition - Description, input schema, and local handler.
|
|
25
|
+
* @returns The unchanged definition with inferred handler types.
|
|
26
|
+
*/
|
|
17
27
|
export declare function defineFunction<Schema extends z.ZodType, Output>(definition: ChatFunction<Schema, Output>): ChatFunction<Schema, Output>;
|
|
28
|
+
/**
|
|
29
|
+
* Converts caller-local Zod input schemas to validated JSON Schema definitions.
|
|
30
|
+
* @param functions - Caller-local function handlers keyed by name.
|
|
31
|
+
* @returns Validated function descriptions and JSON input schemas.
|
|
32
|
+
* @throws BlazingAgentsError - If a definition cannot be represented as a valid JSON Schema.
|
|
33
|
+
*/
|
|
18
34
|
export declare function toChatFunctionDefinitions(functions: ChatFunctions): ChatFunctionDefinitions;
|
|
19
35
|
export interface FunctionDispatchTarget {
|
|
20
36
|
abortSignal?: AbortSignal;
|
|
@@ -23,16 +39,33 @@ export interface FunctionDispatchTarget {
|
|
|
23
39
|
sessionId: string;
|
|
24
40
|
}
|
|
25
41
|
interface FunctionEventHandlers {
|
|
42
|
+
/**
|
|
43
|
+
* Dispatches a function call without blocking the stream reader.
|
|
44
|
+
* @param call - Private function-call event payload.
|
|
45
|
+
* @param fail - Reports a dispatch failure to the stream reader.
|
|
46
|
+
*/
|
|
26
47
|
onCall?(call: ChatFunctionCallEvent["data"], fail: (error: unknown) => void): void;
|
|
48
|
+
/**
|
|
49
|
+
* Handles consumer cancellation.
|
|
50
|
+
*/
|
|
27
51
|
onCancel?(): void;
|
|
52
|
+
/**
|
|
53
|
+
* Stops function handlers when the upstream stream ends.
|
|
54
|
+
*/
|
|
28
55
|
onEnd?(): void;
|
|
29
56
|
}
|
|
30
57
|
/**
|
|
31
58
|
* Reads native SSE eagerly and returns it without private function events,
|
|
32
59
|
* so slow consumers never delay a call. A recognized but malformed event
|
|
33
60
|
* fails the stream.
|
|
61
|
+
* @param body - Upstream SSE byte stream.
|
|
34
62
|
*/
|
|
35
63
|
export declare function stripFunctionEvents(body: ReadableStream<Uint8Array>, { onCall, onCancel, onEnd }?: FunctionEventHandlers): ReadableStream<Uint8Array>;
|
|
36
|
-
/**
|
|
64
|
+
/**
|
|
65
|
+
* Strips private function events and executes claimed calls without blocking the read loop.
|
|
66
|
+
* @param config - Authentication, base URL, and transport configuration.
|
|
67
|
+
* @param target - Agent, Session, local handlers, and caller cancellation signal.
|
|
68
|
+
* @param body - Upstream SSE byte stream.
|
|
69
|
+
*/
|
|
37
70
|
export declare function dispatchChatFunctions(config: HttpConfig, target: FunctionDispatchTarget, body: ReadableStream<Uint8Array>): ReadableStream<Uint8Array>;
|
|
38
71
|
export {};
|
package/dist/functions.js
CHANGED
|
@@ -2,10 +2,20 @@ import { z } from "zod";
|
|
|
2
2
|
import { chatFunctionCallEventSchema, chatFunctionDefinitionsSchema, chatFunctionOutcomeSchema, claimChatFunctionResponseSchema, resolveChatFunctionResponseSchema, } from "./contracts/entities/chat.js";
|
|
3
3
|
import { BlazingAgentsError } from "./errors.js";
|
|
4
4
|
import { requestJson } from "./http.js";
|
|
5
|
-
/**
|
|
5
|
+
/**
|
|
6
|
+
* Declares a caller-local function whose handler input is inferred from its Zod schema.
|
|
7
|
+
* @param definition - Description, input schema, and local handler.
|
|
8
|
+
* @returns The unchanged definition with inferred handler types.
|
|
9
|
+
*/
|
|
6
10
|
export function defineFunction(definition) {
|
|
7
11
|
return definition;
|
|
8
12
|
}
|
|
13
|
+
/**
|
|
14
|
+
* Converts caller-local Zod input schemas to validated JSON Schema definitions.
|
|
15
|
+
* @param functions - Caller-local function handlers keyed by name.
|
|
16
|
+
* @returns Validated function descriptions and JSON input schemas.
|
|
17
|
+
* @throws BlazingAgentsError - If a definition cannot be represented as a valid JSON Schema.
|
|
18
|
+
*/
|
|
9
19
|
export function toChatFunctionDefinitions(functions) {
|
|
10
20
|
try {
|
|
11
21
|
return chatFunctionDefinitionsSchema.parse(Object.fromEntries(Object.entries(functions).map(([name, fn]) => {
|
|
@@ -54,6 +64,9 @@ function functionCallFrom(block) {
|
|
|
54
64
|
}
|
|
55
65
|
return chatFunctionCallEventSchema.safeParse(parsed).data ?? null;
|
|
56
66
|
}
|
|
67
|
+
/**
|
|
68
|
+
* Reads Retry-After as seconds or an HTTP date and clamps past dates to zero.
|
|
69
|
+
*/
|
|
57
70
|
function retryAfterMs(headers) {
|
|
58
71
|
const value = headers?.get("retry-after");
|
|
59
72
|
if (!value) {
|
|
@@ -64,6 +77,9 @@ function retryAfterMs(headers) {
|
|
|
64
77
|
: Date.parse(value) - Date.now();
|
|
65
78
|
return Number.isNaN(ms) ? undefined : Math.max(0, ms);
|
|
66
79
|
}
|
|
80
|
+
/**
|
|
81
|
+
* Returns the retry delay for transient transport or HTTP failures.
|
|
82
|
+
*/
|
|
67
83
|
function retryDelayMs({ code, headers, status }, attempt) {
|
|
68
84
|
const retryable = status === undefined
|
|
69
85
|
? code === "network_error"
|
|
@@ -74,6 +90,9 @@ function retryDelayMs({ code, headers, status }, attempt) {
|
|
|
74
90
|
return (retryAfterMs(headers) ??
|
|
75
91
|
Math.min(RETRY_BASE_MS * 2 ** (attempt - 1), RETRY_MAX_MS));
|
|
76
92
|
}
|
|
93
|
+
/**
|
|
94
|
+
* Waits for the delay or resolves early when the signal aborts.
|
|
95
|
+
*/
|
|
77
96
|
function sleep(ms, signal) {
|
|
78
97
|
return new Promise((resolve) => {
|
|
79
98
|
const timer = setTimeout(resolve, ms);
|
|
@@ -111,7 +130,9 @@ async function postWithRetry(config, path, json, schema, until, signal) {
|
|
|
111
130
|
}
|
|
112
131
|
}
|
|
113
132
|
}
|
|
114
|
-
/**
|
|
133
|
+
/**
|
|
134
|
+
* Classifies a failed attempt as a lost race, an exhausted budget, or the delay before retrying; permanent failures rethrow.
|
|
135
|
+
*/
|
|
115
136
|
function afterFailure(caught, attempt, until, signal) {
|
|
116
137
|
const error = BlazingAgentsError.isInstance(caught) ? caught : undefined;
|
|
117
138
|
if (error?.status === 409) {
|
|
@@ -130,7 +151,9 @@ const invalidResult = {
|
|
|
130
151
|
kind: "error",
|
|
131
152
|
message: "Function returned an invalid result.",
|
|
132
153
|
};
|
|
133
|
-
/**
|
|
154
|
+
/**
|
|
155
|
+
* Resolves `undefined` when the call became late or aborted before customer code could start.
|
|
156
|
+
*/
|
|
134
157
|
async function execute(functions, { id, name, input }, deadline, signal) {
|
|
135
158
|
const fn = Object.hasOwn(functions, name) ? functions[name] : undefined;
|
|
136
159
|
if (fn === undefined) {
|
|
@@ -175,6 +198,7 @@ async function execute(functions, { id, name, input }, deadline, signal) {
|
|
|
175
198
|
* Reads native SSE eagerly and returns it without private function events,
|
|
176
199
|
* so slow consumers never delay a call. A recognized but malformed event
|
|
177
200
|
* fails the stream.
|
|
201
|
+
* @param body - Upstream SSE byte stream.
|
|
178
202
|
*/
|
|
179
203
|
export function stripFunctionEvents(body, { onCall, onCancel, onEnd } = {}) {
|
|
180
204
|
const reader = body.getReader();
|
|
@@ -182,6 +206,9 @@ export function stripFunctionEvents(body, { onCall, onCancel, onEnd } = {}) {
|
|
|
182
206
|
const encoder = new TextEncoder();
|
|
183
207
|
let output;
|
|
184
208
|
let finished = false;
|
|
209
|
+
/**
|
|
210
|
+
* Fails the output stream once and cancels its upstream reader.
|
|
211
|
+
*/
|
|
185
212
|
const fail = (error) => {
|
|
186
213
|
if (finished) {
|
|
187
214
|
return;
|
|
@@ -190,6 +217,9 @@ export function stripFunctionEvents(body, { onCall, onCancel, onEnd } = {}) {
|
|
|
190
217
|
output.error(error);
|
|
191
218
|
reader.cancel(error).catch(() => undefined);
|
|
192
219
|
};
|
|
220
|
+
/**
|
|
221
|
+
* Forwards public SSE blocks and dispatches or rejects private function events.
|
|
222
|
+
*/
|
|
193
223
|
const handleBlock = (block, raw) => {
|
|
194
224
|
const call = functionCallFrom(block);
|
|
195
225
|
if (call === undefined) {
|
|
@@ -207,6 +237,9 @@ export function stripFunctionEvents(body, { onCall, onCancel, onEnd } = {}) {
|
|
|
207
237
|
onCall?.(call.data, fail);
|
|
208
238
|
}
|
|
209
239
|
};
|
|
240
|
+
/**
|
|
241
|
+
* Reads SSE bytes eagerly and splits complete event blocks for dispatch.
|
|
242
|
+
*/
|
|
210
243
|
const pump = async () => {
|
|
211
244
|
let buffer = "";
|
|
212
245
|
try {
|
|
@@ -236,10 +269,16 @@ export function stripFunctionEvents(body, { onCall, onCancel, onEnd } = {}) {
|
|
|
236
269
|
}
|
|
237
270
|
};
|
|
238
271
|
return new ReadableStream({
|
|
272
|
+
/**
|
|
273
|
+
* Initializes the stream controller.
|
|
274
|
+
*/
|
|
239
275
|
start(controller) {
|
|
240
276
|
output = controller;
|
|
241
277
|
pump();
|
|
242
278
|
},
|
|
279
|
+
/**
|
|
280
|
+
* Cancels the upstream reader with the consumer reason.
|
|
281
|
+
*/
|
|
243
282
|
async cancel(reason) {
|
|
244
283
|
finished = true;
|
|
245
284
|
onCancel?.();
|
|
@@ -247,12 +286,20 @@ export function stripFunctionEvents(body, { onCall, onCancel, onEnd } = {}) {
|
|
|
247
286
|
},
|
|
248
287
|
});
|
|
249
288
|
}
|
|
250
|
-
/**
|
|
289
|
+
/**
|
|
290
|
+
* Strips private function events and executes claimed calls without blocking the read loop.
|
|
291
|
+
* @param config - Authentication, base URL, and transport configuration.
|
|
292
|
+
* @param target - Agent, Session, local handlers, and caller cancellation signal.
|
|
293
|
+
* @param body - Upstream SSE byte stream.
|
|
294
|
+
*/
|
|
251
295
|
export function dispatchChatFunctions(config, target, body) {
|
|
252
296
|
/** Caller abort or consumer cancellation: stops every retry. */
|
|
253
297
|
const stop = new AbortController();
|
|
254
298
|
/** Also aborted when the stream ends: stops claims and handlers. */
|
|
255
299
|
const handlers = new AbortController();
|
|
300
|
+
/**
|
|
301
|
+
* Aborts pending function claims and retries.
|
|
302
|
+
*/
|
|
256
303
|
const abort = () => stop.abort();
|
|
257
304
|
stop.signal.addEventListener("abort", () => handlers.abort(), {
|
|
258
305
|
once: true,
|
|
@@ -262,6 +309,9 @@ export function dispatchChatFunctions(config, target, body) {
|
|
|
262
309
|
abort();
|
|
263
310
|
}
|
|
264
311
|
const dispatched = new Set();
|
|
312
|
+
/**
|
|
313
|
+
* Claims a function call, runs its handler, and posts the result within its retry budget.
|
|
314
|
+
*/
|
|
265
315
|
const runCall = async (call) => {
|
|
266
316
|
const path = `/v1/agents/${target.agentId}/sessions/${target.sessionId}/function-calls/${call.id}`;
|
|
267
317
|
const claimRequestId = crypto.randomUUID();
|
|
@@ -284,6 +334,9 @@ export function dispatchChatFunctions(config, target, body) {
|
|
|
284
334
|
await postWithRetry(config, `${path}/result`, { claimRequestId, outcome }, resolveChatFunctionResponseSchema, deadline + RESULT_RETRY_GRACE_MS, stop.signal);
|
|
285
335
|
};
|
|
286
336
|
return stripFunctionEvents(body, {
|
|
337
|
+
/**
|
|
338
|
+
* Dispatches a function call without blocking the stream reader.
|
|
339
|
+
*/
|
|
287
340
|
onCall(call, fail) {
|
|
288
341
|
if (!dispatched.has(call.id)) {
|
|
289
342
|
dispatched.add(call.id);
|
|
@@ -291,6 +344,9 @@ export function dispatchChatFunctions(config, target, body) {
|
|
|
291
344
|
}
|
|
292
345
|
},
|
|
293
346
|
onCancel: abort,
|
|
347
|
+
/**
|
|
348
|
+
* Stops function handlers when the upstream stream ends.
|
|
349
|
+
*/
|
|
294
350
|
onEnd() {
|
|
295
351
|
handlers.abort();
|
|
296
352
|
target.abortSignal?.removeEventListener("abort", abort);
|
package/dist/generation.d.ts
CHANGED
|
@@ -1,11 +1,43 @@
|
|
|
1
|
-
import type { ChatInput, ChatResult, CompletionInput, CompletionResult, HttpConfig, ObjectInput, ObjectResult,
|
|
1
|
+
import type { ChatInput, ChatResult, CompletionInput, CompletionResult, ContinueChatInput, HttpConfig, ObjectInput, ObjectResult, TerminalStreamResult } from "./types.ts";
|
|
2
|
+
/**
|
|
3
|
+
* Creates or resumes a Session and returns its SSE stream and Session ID.
|
|
4
|
+
* @param config - Authentication, base URL, and transport configuration.
|
|
5
|
+
* @param input - Agent, message or stored Prompt, and optional existing Session.
|
|
6
|
+
* @returns The Session ID promise and single-owner SSE accessors.
|
|
7
|
+
*/
|
|
2
8
|
export declare function chat(config: HttpConfig, input: ChatInput): Promise<ChatResult>;
|
|
3
|
-
|
|
9
|
+
/**
|
|
10
|
+
* Submits a complete Tool approval round and streams its continuation.
|
|
11
|
+
* @param config - Authentication, base URL, and transport configuration.
|
|
12
|
+
* @param input - Agent, Session, and decisions for the complete approval round.
|
|
13
|
+
* @returns The Session ID promise and continuation SSE accessors.
|
|
14
|
+
*/
|
|
15
|
+
export declare function continueChat(config: HttpConfig, input: ContinueChatInput): Promise<ChatResult>;
|
|
4
16
|
/**
|
|
5
17
|
* Keeps status and headers while filtering the SSE body. Missing or locked
|
|
6
18
|
* bodies pass through so the stream result reports them as `stream_error`.
|
|
19
|
+
* @param response - Upstream HTTP response.
|
|
20
|
+
* @param filter - Transforms upstream SSE bytes before exposing them to the caller.
|
|
7
21
|
*/
|
|
8
22
|
export declare function withFilteredBody(response: Response, filter: (body: ReadableStream<Uint8Array>) => ReadableStream<Uint8Array>): Response;
|
|
23
|
+
/**
|
|
24
|
+
* Creates single-owner SSE response and byte-stream accessors.
|
|
25
|
+
* @param response - Upstream HTTP response.
|
|
26
|
+
* @param resourceName - Resource label used in stream errors.
|
|
27
|
+
* @param filter - Transforms upstream SSE bytes before exposing them to the caller.
|
|
28
|
+
*/
|
|
9
29
|
export declare function buildTerminalStreamResult(response: Response, resourceName: string, filter?: (body: ReadableStream<Uint8Array>) => ReadableStream<Uint8Array>): TerminalStreamResult;
|
|
30
|
+
/**
|
|
31
|
+
* Starts stateless text generation with independent final, incremental, and response outputs.
|
|
32
|
+
* @param config - Authentication, base URL, and transport configuration.
|
|
33
|
+
* @param input - Agent and literal prompt or stored Prompt reference.
|
|
34
|
+
* @returns Independent text stream, final text promise, and response relay.
|
|
35
|
+
*/
|
|
10
36
|
export declare function completion(config: HttpConfig, input: CompletionInput): Promise<CompletionResult>;
|
|
37
|
+
/**
|
|
38
|
+
* Starts stateless JSON generation with partial objects and a final parsed value.
|
|
39
|
+
* @param config - Authentication, base URL, and transport configuration.
|
|
40
|
+
* @param input - Agent, prompt source, and JSON output schema.
|
|
41
|
+
* @returns Partial JSON objects, a final JSON promise, and a text response relay.
|
|
42
|
+
*/
|
|
11
43
|
export declare function objectGeneration(config: HttpConfig, input: ObjectInput): Promise<ObjectResult>;
|