@blazingagents/sdk 0.15.0 → 0.16.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/dist/relay.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { safeValidateUIMessages } from "ai";
2
2
  import { z } from "zod";
3
+ import { decideApprovalResponses } from "./approvals.js";
3
4
  import { sessionIdSchema } from "./contracts/ids.js";
4
5
  import { BlazingAgentsError } from "./errors.js";
5
6
  const chatBodySchema = z.object({
@@ -11,6 +12,10 @@ const chatBodySchema = z.object({
11
12
  .default("submit-message"),
12
13
  });
13
14
  const completionBodySchema = z.object({ prompt: z.string().trim().min(1) });
15
+ /**
16
+ * Relays useChat submissions. An assistant message carrying approval
17
+ * responses records each decision and then streams the continuation.
18
+ */
14
19
  export function createChatRelay(options) {
15
20
  return async (request) => {
16
21
  try {
@@ -29,9 +34,20 @@ export function createChatRelay(options) {
29
34
  (await options.sessions.ownerOf(body.sessionId)) !== context.userId) {
30
35
  return errorResponse(403, "forbidden", "Session is not available.");
31
36
  }
37
+ const message = validated.data[0];
38
+ if (message.role === "assistant") {
39
+ return await relayApprovalResponses(options.client, {
40
+ agentId: context.agentId,
41
+ functions: context.functions,
42
+ message,
43
+ request,
44
+ sessionId: body.sessionId,
45
+ });
46
+ }
32
47
  const chatInput = {
33
48
  agentId: context.agentId,
34
- message: validated.data[0],
49
+ functions: context.functions,
50
+ message,
35
51
  messageId: body.messageId,
36
52
  metadata: context.metadata,
37
53
  abortSignal: request.signal,
@@ -68,6 +84,30 @@ export function createChatRelay(options) {
68
84
  }
69
85
  };
70
86
  }
87
+ async function relayApprovalResponses(client, { functions, message, request, ...input }) {
88
+ if (input.sessionId === undefined) {
89
+ return errorResponse(400, "invalid_request", "Tool approval requires an existing Session.");
90
+ }
91
+ const target = {
92
+ agentId: input.agentId,
93
+ sessionId: input.sessionId,
94
+ abortSignal: request.signal,
95
+ };
96
+ const continuationId = await decideApprovalResponses(client, {
97
+ ...target,
98
+ message,
99
+ });
100
+ if (continuationId === undefined) {
101
+ return errorResponse(400, "invalid_request", "The message has no tool approval responses.");
102
+ }
103
+ /** An absent registry still resumes, so missing handlers become tool errors instead of a stalled observer. */
104
+ const continuation = await client.resumeChat({
105
+ ...target,
106
+ continuationId,
107
+ functions: functions ?? {},
108
+ });
109
+ return continuation.toResponse();
110
+ }
71
111
  export function createCompletionRelay(options) {
72
112
  return async (request) => {
73
113
  try {
@@ -1,5 +1,6 @@
1
1
  import { latestSessionsListResponseSchema, sessionMessagesResponseSchema, sessionsListResponseSchema, toolApprovalDecisionResponseSchema, toolApprovalsResponseSchema, } from "../contracts/entities/sessions.js";
2
- import { buildTerminalStreamResult } from "../generation.js";
2
+ import { stripFunctionEvents } from "../functions.js";
3
+ import { buildTerminalStreamResult, withFilteredBody } from "../generation.js";
3
4
  import { requestJson, requestStream } from "../http.js";
4
5
  /**
5
6
  * `client.sessions` — list/get-messages/delete over
@@ -53,7 +54,8 @@ export function createSessionsResource(config) {
53
54
  },
54
55
  async joinToolApprovalContinuation({ agentId, sessionId, continuationId, abortSignal, }) {
55
56
  const response = await requestStream(config, `/v1/agents/${agentId}/sessions/${sessionId}/tool-approval-continuations/${continuationId}`, { signal: abortSignal });
56
- return buildTerminalStreamResult(response, "continuation");
57
+ /** Observers never execute calls; private events are removed unclaimed. */
58
+ return buildTerminalStreamResult(withFilteredBody(response, stripFunctionEvents), "continuation");
57
59
  },
58
60
  async toolApprovals({ agentId, sessionId, abortSignal }) {
59
61
  return await requestJson(config, `/v1/agents/${agentId}/sessions/${sessionId}/tool-approvals`, { signal: abortSignal }, toolApprovalsResponseSchema);
package/dist/types.d.ts CHANGED
@@ -15,6 +15,7 @@ import type { CreateTaskBody, CreateTaskResponse, CreateTaskRunBody, CreateTaskR
15
15
  import type { TenantSettingsResponse, UpdateTenantSettingsBody } from "./contracts/entities/tenants.ts";
16
16
  import type { SessionUsageQuery, SessionUsageResponse, UsageOverviewQuery, UsageOverviewResponse, UsageQuery, UsageResponse } from "./contracts/entities/usage.ts";
17
17
  import type { CreateWorkspaceBody, UpdateWorkspaceBody, Workspace, WorkspacesListResponse } from "./contracts/entities/workspaces.ts";
18
+ import type { ChatFunctions } from "./functions.ts";
18
19
  export type KnownBlazingAgentsErrorCode = ApiErrorCode | "invalid_response" | "network_error" | "request_aborted" | "stream_error";
19
20
  export type BlazingAgentsErrorCode = KnownBlazingAgentsErrorCode | (string & {});
20
21
  export type BlazingAgentsFetch = (input: string, init?: BlazingAgentsRequestInit) => Promise<Response>;
@@ -69,6 +70,7 @@ export interface UserClient {
69
70
  readonly memories: MemoriesResource;
70
71
  object(input: ObjectInput): Promise<ObjectResult>;
71
72
  readonly prompts: PromptsResource;
73
+ resumeChat(input: ResumeChatInput): Promise<ChatResult>;
72
74
  readonly sessions: SessionsResource;
73
75
  readonly tasks: TasksResource;
74
76
  readonly usage: Pick<UsageResource, "get" | "sessions">;
@@ -112,6 +114,8 @@ type ChatSessionInput = NewSessionInput | ExistingSessionInput;
112
114
  interface ChatMessageContentInput extends AttributionInput, CorrelatedRequestInput {
113
115
  abortSignal?: AbortSignal;
114
116
  agentId: string;
117
+ /** Caller-local functions for this invocation; handlers run in this process. */
118
+ functions?: ChatFunctions;
115
119
  message: UIMessage;
116
120
  messageId?: string;
117
121
  promptId?: never;
@@ -120,6 +124,8 @@ interface ChatMessageContentInput extends AttributionInput, CorrelatedRequestInp
120
124
  interface ChatPromptContentInput extends AttributionInput, CorrelatedRequestInput {
121
125
  abortSignal?: AbortSignal;
122
126
  agentId: string;
127
+ /** Caller-local functions for this invocation; handlers run in this process. */
128
+ functions?: ChatFunctions;
123
129
  message?: never;
124
130
  messageId?: string;
125
131
  promptId: string;
@@ -128,6 +134,15 @@ interface ChatPromptContentInput extends AttributionInput, CorrelatedRequestInpu
128
134
  export type ChatMessageInput = ChatMessageContentInput & ChatSessionInput;
129
135
  export type ChatPromptInput = ChatPromptContentInput & ChatSessionInput;
130
136
  export type ChatInput = ChatMessageInput | ChatPromptInput;
137
+ /** Reattaches handlers to a Session's tool-approval continuation. */
138
+ export interface ResumeChatInput extends CorrelatedRequestInput {
139
+ abortSignal?: AbortSignal;
140
+ agentId: string;
141
+ /** The decided continuation; omitted, the Session's queued or running one is resumed. */
142
+ continuationId?: string;
143
+ functions: ChatFunctions;
144
+ sessionId: string;
145
+ }
131
146
  export interface ChatResult {
132
147
  requestId?: string;
133
148
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blazingagents/sdk",
3
- "version": "0.15.0",
3
+ "version": "0.16.0",
4
4
  "description": "TypeScript SDK for the Blazing Agents API",
5
5
  "homepage": "https://docs.blazingagents.com",
6
6
  "bugs": {