bs-agent 0.0.9 → 0.0.12
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 +496 -410
- package/dist/core/index.cjs +520 -0
- package/dist/core/index.cjs.map +1 -0
- package/dist/core/index.d.cts +133 -0
- package/dist/core/index.d.ts +133 -0
- package/dist/core/index.js +489 -0
- package/dist/core/index.js.map +1 -0
- package/dist/react/index.cjs +1318 -0
- package/dist/react/index.cjs.map +1 -0
- package/dist/react/index.d.cts +287 -0
- package/dist/react/index.d.ts +287 -0
- package/dist/react/index.js +1286 -0
- package/dist/react/index.js.map +1 -0
- package/dist/types-DryLPWU9.d.cts +160 -0
- package/dist/types-DryLPWU9.d.ts +160 -0
- package/package.json +26 -11
- package/dist/index.cjs +0 -915
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.cts +0 -196
- package/dist/index.d.ts +0 -196
- package/dist/index.js +0 -886
- package/dist/index.js.map +0 -1
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
import { T as ToolType } from '../types-DryLPWU9.js';
|
|
2
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
3
|
+
import * as react from 'react';
|
|
4
|
+
import { ReactNode } from 'react';
|
|
5
|
+
import 'zod';
|
|
6
|
+
|
|
7
|
+
type ToolExecutionItem = {
|
|
8
|
+
itemType: "tool_call";
|
|
9
|
+
toolName: string;
|
|
10
|
+
callId: string;
|
|
11
|
+
toolType: ToolType;
|
|
12
|
+
status: "progress" | "complete" | "error";
|
|
13
|
+
inputs?: unknown;
|
|
14
|
+
output?: unknown;
|
|
15
|
+
error?: string;
|
|
16
|
+
serverName?: string;
|
|
17
|
+
};
|
|
18
|
+
type ReasoningItem = {
|
|
19
|
+
itemType: "reasoning";
|
|
20
|
+
reasoning: string;
|
|
21
|
+
index?: number;
|
|
22
|
+
};
|
|
23
|
+
type HandoffItem = {
|
|
24
|
+
itemType: "handoff";
|
|
25
|
+
agentName: string;
|
|
26
|
+
};
|
|
27
|
+
type RunErrorItem = {
|
|
28
|
+
itemType: "run_error";
|
|
29
|
+
message: string;
|
|
30
|
+
code?: string;
|
|
31
|
+
};
|
|
32
|
+
type WidgetExecutionItem = {
|
|
33
|
+
toolName: string;
|
|
34
|
+
callId: string;
|
|
35
|
+
inputs: any;
|
|
36
|
+
};
|
|
37
|
+
type ClientToolDefinition = {
|
|
38
|
+
name: string;
|
|
39
|
+
description: string;
|
|
40
|
+
parameters: unknown;
|
|
41
|
+
await?: boolean;
|
|
42
|
+
};
|
|
43
|
+
type DebugDataType = Array<ToolExecutionItem | ReasoningItem | HandoffItem | RunErrorItem>;
|
|
44
|
+
type MessagePart = {
|
|
45
|
+
type: "text";
|
|
46
|
+
text: string;
|
|
47
|
+
firstSequence: number;
|
|
48
|
+
lastSequence: number;
|
|
49
|
+
} | {
|
|
50
|
+
type: "widget";
|
|
51
|
+
toolName: string;
|
|
52
|
+
callId: string;
|
|
53
|
+
inputs: any;
|
|
54
|
+
sequence: number;
|
|
55
|
+
paused?: boolean;
|
|
56
|
+
status?: "pending" | "submitted";
|
|
57
|
+
/** Persisted result from a tool submission (handler or widget submit). */
|
|
58
|
+
result?: any;
|
|
59
|
+
};
|
|
60
|
+
type Message = {
|
|
61
|
+
role: "user" | "agent";
|
|
62
|
+
content: string;
|
|
63
|
+
parts?: Array<MessagePart>;
|
|
64
|
+
executionId?: string;
|
|
65
|
+
};
|
|
66
|
+
type Session = {
|
|
67
|
+
id: string;
|
|
68
|
+
createdAt: number;
|
|
69
|
+
updatedAt: number;
|
|
70
|
+
messages: Array<Message>;
|
|
71
|
+
name?: string;
|
|
72
|
+
};
|
|
73
|
+
type ToolConfig = {
|
|
74
|
+
name: string;
|
|
75
|
+
description: string;
|
|
76
|
+
schema: unknown;
|
|
77
|
+
await?: boolean;
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Infer the output type from a Zod schema's structural shape (`_zod.output`).
|
|
82
|
+
* Falls back to `any` for raw JSON Schema objects or untyped parameters.
|
|
83
|
+
* @internal
|
|
84
|
+
*/
|
|
85
|
+
type InferInput<T> = T extends {
|
|
86
|
+
_zod: {
|
|
87
|
+
output: infer O;
|
|
88
|
+
};
|
|
89
|
+
} ? O : any;
|
|
90
|
+
interface ClientToolRenderProps<T = any> {
|
|
91
|
+
/** Parsed inputs from the agent. */
|
|
92
|
+
inputs: T;
|
|
93
|
+
/** Submit a result back to the agent (only available when `await: true`). */
|
|
94
|
+
submit: (result: any) => void;
|
|
95
|
+
/** Current status of this widget instance. */
|
|
96
|
+
status: "pending" | "submitted";
|
|
97
|
+
/** The persisted result from a previous submission (available after submit or on reload). */
|
|
98
|
+
result?: any;
|
|
99
|
+
}
|
|
100
|
+
interface ClientToolConfig<TParams = any> {
|
|
101
|
+
/** Tool name — must match the name the agent knows. */
|
|
102
|
+
name: string;
|
|
103
|
+
/** Description of what the tool does. */
|
|
104
|
+
description: string;
|
|
105
|
+
/**
|
|
106
|
+
* Tool parameters — accepts a **Zod schema** or a raw JSON Schema object.
|
|
107
|
+
* When a Zod schema is provided, `handler` and `render` inputs are
|
|
108
|
+
* automatically typed — no explicit generic needed.
|
|
109
|
+
*
|
|
110
|
+
* @example
|
|
111
|
+
* parameters: z.object({ question: z.string() })
|
|
112
|
+
*/
|
|
113
|
+
parameters: TParams;
|
|
114
|
+
/** If true, agent pauses and waits for the tool result before continuing. */
|
|
115
|
+
await?: boolean;
|
|
116
|
+
/**
|
|
117
|
+
* Handler function for headless tools (no UI).
|
|
118
|
+
* If `await: true`, the return value is sent back to the agent.
|
|
119
|
+
* If `await: false`, runs as fire-and-forget.
|
|
120
|
+
* Inputs are automatically typed when `parameters` is a Zod schema.
|
|
121
|
+
*/
|
|
122
|
+
handler?: (inputs: InferInput<TParams>) => any | Promise<any>;
|
|
123
|
+
/**
|
|
124
|
+
* Render function for widget tools (with UI).
|
|
125
|
+
* Receives `{ inputs, submit, status }`.
|
|
126
|
+
* - `submit(result)` resumes the agent when `await: true`.
|
|
127
|
+
* - `status` is `"pending"` until submitted, then `"submitted"`.
|
|
128
|
+
* Inputs are automatically typed when `parameters` is a Zod schema.
|
|
129
|
+
*/
|
|
130
|
+
render?: (props: ClientToolRenderProps<InferInput<TParams>>) => any;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Register a client tool for a specific agent.
|
|
134
|
+
*
|
|
135
|
+
* @example
|
|
136
|
+
* ```tsx
|
|
137
|
+
* // Headless tool
|
|
138
|
+
* useClientTool("agent-123", {
|
|
139
|
+
* name: "get_location",
|
|
140
|
+
* description: "Gets user location",
|
|
141
|
+
* parameters: z.object({}),
|
|
142
|
+
* handler: async () => {
|
|
143
|
+
* const pos = await getPosition();
|
|
144
|
+
* return { lat: pos.coords.latitude, lng: pos.coords.longitude };
|
|
145
|
+
* },
|
|
146
|
+
* });
|
|
147
|
+
*
|
|
148
|
+
* // Widget tool with submission
|
|
149
|
+
* useClientTool("agent-123", {
|
|
150
|
+
* name: "feedback_form",
|
|
151
|
+
* description: "Collects feedback",
|
|
152
|
+
* parameters: z.object({ question: z.string() }),
|
|
153
|
+
* await: true,
|
|
154
|
+
* render: ({ inputs, submit, status }) => (
|
|
155
|
+
* <form onSubmit={() => submit({ answer: "..." })}>
|
|
156
|
+
* <p>{inputs.question}</p>
|
|
157
|
+
* <button disabled={status !== "pending"}>Submit</button>
|
|
158
|
+
* </form>
|
|
159
|
+
* ),
|
|
160
|
+
* });
|
|
161
|
+
* ```
|
|
162
|
+
*/
|
|
163
|
+
declare function useClientTool<TParams>(agentId: string, config: ClientToolConfig<TParams>): void;
|
|
164
|
+
interface ToolRendererProps {
|
|
165
|
+
/** The agent ID this tool belongs to. */
|
|
166
|
+
agentId: string;
|
|
167
|
+
/** The widget message part to render. */
|
|
168
|
+
part: {
|
|
169
|
+
toolName: string;
|
|
170
|
+
callId: string;
|
|
171
|
+
inputs: any;
|
|
172
|
+
paused?: boolean;
|
|
173
|
+
status?: "pending" | "submitted";
|
|
174
|
+
result?: any;
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Renders a registered widget tool.
|
|
179
|
+
* Looks up the tool by name from the registry and renders it with
|
|
180
|
+
* the appropriate props (inputs, submit, status).
|
|
181
|
+
*
|
|
182
|
+
* @example
|
|
183
|
+
* ```tsx
|
|
184
|
+
* {message.parts?.map((part) => {
|
|
185
|
+
* if (part.type === "widget") {
|
|
186
|
+
* return <ToolRenderer key={part.callId} agentId="agent-123" part={part} />;
|
|
187
|
+
* }
|
|
188
|
+
* return <Markdown key={part.firstSequence}>{part.text}</Markdown>;
|
|
189
|
+
* })}
|
|
190
|
+
* ```
|
|
191
|
+
*/
|
|
192
|
+
declare function ToolRenderer({ agentId, part }: ToolRendererProps): any;
|
|
193
|
+
|
|
194
|
+
interface AgentRunner {
|
|
195
|
+
messages: Message[];
|
|
196
|
+
inProgress: boolean;
|
|
197
|
+
sessionId: string;
|
|
198
|
+
sessions: Session[];
|
|
199
|
+
debugData: Record<string, DebugDataType>;
|
|
200
|
+
handleSend: (input: string, options?: {
|
|
201
|
+
context?: Record<string, unknown>;
|
|
202
|
+
skipUserMessage?: boolean;
|
|
203
|
+
additionalHeaders?: Record<string, string>;
|
|
204
|
+
additionalBody?: Record<string, unknown>;
|
|
205
|
+
/** @deprecated Use `useClientTool` hook instead. */
|
|
206
|
+
clientTools?: Array<{
|
|
207
|
+
name: string;
|
|
208
|
+
description: string;
|
|
209
|
+
parameters: unknown;
|
|
210
|
+
await?: boolean;
|
|
211
|
+
}>;
|
|
212
|
+
}) => Promise<void>;
|
|
213
|
+
resumeTool: (callId: string, result: any) => Promise<void>;
|
|
214
|
+
switchSession: (sessionId?: string) => void;
|
|
215
|
+
deleteSession: (sessionId: string) => void;
|
|
216
|
+
addOptimisticMessage: (input: string) => void;
|
|
217
|
+
abort: () => void;
|
|
218
|
+
}
|
|
219
|
+
interface AgentToolContextValue {
|
|
220
|
+
registerTool: (agentId: string, config: ClientToolConfig) => void;
|
|
221
|
+
unregisterTool: (agentId: string, toolName: string) => void;
|
|
222
|
+
getTool: (agentId: string, toolName: string) => ClientToolConfig | undefined;
|
|
223
|
+
getToolsForAgent: (agentId: string) => ClientToolConfig[];
|
|
224
|
+
resumeTool: (agentId: string, callId: string, result: any) => void;
|
|
225
|
+
}
|
|
226
|
+
declare const AgentToolContext: react.Context<AgentToolContextValue | null>;
|
|
227
|
+
declare function AgentContextProvider({ children }: {
|
|
228
|
+
children: ReactNode;
|
|
229
|
+
}): react_jsx_runtime.JSX.Element;
|
|
230
|
+
declare function useAgentContext(agentId: string, agentUrl: string, accessKey?: string): AgentRunner;
|
|
231
|
+
declare function useAgentGlobalState(): {
|
|
232
|
+
allSessions: Record<string, Record<string, Session>>;
|
|
233
|
+
setAllSessions: (value: Record<string, Record<string, Session>> | ((prev: Record<string, Record<string, Session>>) => Record<string, Record<string, Session>>)) => void;
|
|
234
|
+
debugData: Record<string, DebugDataType>;
|
|
235
|
+
setDebugData: (value: Record<string, DebugDataType> | ((prev: Record<string, DebugDataType>) => Record<string, DebugDataType>)) => void;
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
declare function useAgent(agentId: string, agentUrl: string, accessKey?: string): {
|
|
239
|
+
inProgress: boolean;
|
|
240
|
+
messages: Message[];
|
|
241
|
+
handleSend: (input: string, options?: {
|
|
242
|
+
context?: object;
|
|
243
|
+
skipUserMessage?: boolean;
|
|
244
|
+
additionalHeaders?: Record<string, string>;
|
|
245
|
+
additionalBody?: Record<string, unknown>;
|
|
246
|
+
/** @deprecated Use `useClientTool` hook instead. */
|
|
247
|
+
clientTools?: Array<{
|
|
248
|
+
name: string;
|
|
249
|
+
description: string;
|
|
250
|
+
parameters: unknown;
|
|
251
|
+
await?: boolean;
|
|
252
|
+
}>;
|
|
253
|
+
}) => Promise<void>;
|
|
254
|
+
resumeTool: (callId: string, result: any) => Promise<void>;
|
|
255
|
+
addOptimisticMessage: (input: string) => void;
|
|
256
|
+
abort: () => void;
|
|
257
|
+
sessionId: string;
|
|
258
|
+
switchSession: (sessionId?: string) => void;
|
|
259
|
+
deleteSession: (sessionId: string) => void;
|
|
260
|
+
sessions: Session[];
|
|
261
|
+
debugData: Record<string, DebugDataType>;
|
|
262
|
+
};
|
|
263
|
+
|
|
264
|
+
declare const AGENT_SESSIONS_KEY = "buildship:agent:conversations";
|
|
265
|
+
declare const AGENT_DEBUG_DATA_KEY = "buildship:agent:debug";
|
|
266
|
+
declare const DEFAULT_SESSION_NAME = "New Chat";
|
|
267
|
+
declare const TEMPORARY_SESSION_ID = "sess_temp";
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Helper to clean JSON Schema for LLM compatibility.
|
|
271
|
+
* Replaces modern Draft 2020-12 features with older, more compatible alternatives.
|
|
272
|
+
* Specifically handles OpenAI's 'Strict Mode' requirements.
|
|
273
|
+
*/
|
|
274
|
+
declare function cleanSchema(obj: unknown): unknown;
|
|
275
|
+
/**
|
|
276
|
+
* Safely attempts to parse a value as JSON if it's a string.
|
|
277
|
+
*/
|
|
278
|
+
declare function tryParseJSON(value: unknown): any;
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* Updates a list of message parts with a new part,
|
|
282
|
+
* appending it and merging with the previous part if both are contiguous text deltas.
|
|
283
|
+
* Parts are kept in arrival order (no sorting) since events stream chronologically.
|
|
284
|
+
*/
|
|
285
|
+
declare function updateAgentMessageParts(parts: MessagePart[], newPart: MessagePart): MessagePart[];
|
|
286
|
+
|
|
287
|
+
export { AGENT_DEBUG_DATA_KEY, AGENT_SESSIONS_KEY, AgentContextProvider, type AgentRunner, AgentToolContext, type AgentToolContextValue, type ClientToolConfig, type ClientToolDefinition, type ClientToolRenderProps, DEFAULT_SESSION_NAME, type DebugDataType, type HandoffItem, type Message, type MessagePart, type ReasoningItem, type RunErrorItem, type Session, TEMPORARY_SESSION_ID, type ToolConfig, type ToolExecutionItem, ToolRenderer, ToolType, type WidgetExecutionItem, cleanSchema, tryParseJSON, updateAgentMessageParts, useAgent, useAgentContext, useAgentGlobalState, useClientTool };
|