@yourgpt/copilot-sdk 0.1.0 → 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.
@@ -0,0 +1,595 @@
1
+ /**
2
+ * Tool-related types for the agentic loop
3
+ */
4
+ /**
5
+ * Supported AI providers for tool calling
6
+ */
7
+ type AIProvider = "anthropic" | "openai" | "xai" | "grok" | "gemini" | "groq" | "ollama";
8
+ /**
9
+ * Where the tool executes
10
+ */
11
+ type ToolLocation = "server" | "client";
12
+ /**
13
+ * JSON Schema property definition
14
+ */
15
+ interface JSONSchemaProperty {
16
+ type: "string" | "number" | "boolean" | "object" | "array" | "integer" | "null";
17
+ description?: string;
18
+ enum?: (string | number | boolean)[];
19
+ items?: JSONSchemaProperty;
20
+ properties?: Record<string, JSONSchemaProperty>;
21
+ required?: string[];
22
+ default?: unknown;
23
+ minLength?: number;
24
+ maxLength?: number;
25
+ minimum?: number;
26
+ maximum?: number;
27
+ pattern?: string;
28
+ }
29
+ /**
30
+ * JSON Schema for tool input
31
+ */
32
+ interface ToolInputSchema {
33
+ type: "object";
34
+ properties: Record<string, JSONSchemaProperty>;
35
+ required?: string[];
36
+ additionalProperties?: boolean;
37
+ }
38
+ /**
39
+ * Tool execution context
40
+ *
41
+ * Provides runtime information to tool handlers including cancellation signals,
42
+ * request metadata, and custom context data.
43
+ */
44
+ interface ToolContext {
45
+ /** Abort signal for cancellation */
46
+ signal?: AbortSignal;
47
+ /** Thread ID if using threads */
48
+ threadId?: string;
49
+ /** Custom context data passed from runtime config */
50
+ data?: Record<string, unknown>;
51
+ /**
52
+ * Unique ID for this specific tool call.
53
+ * Useful for logging, tracing, and correlating tool executions.
54
+ */
55
+ toolCallId?: string;
56
+ /**
57
+ * Request headers (for auth in server tools).
58
+ * Contains headers from the original HTTP request.
59
+ *
60
+ * @example
61
+ * ```typescript
62
+ * handler: async (params, context) => {
63
+ * const token = context?.headers?.authorization;
64
+ * if (!token) return failure('Authentication required');
65
+ * // ...
66
+ * }
67
+ * ```
68
+ */
69
+ headers?: Record<string, string>;
70
+ /**
71
+ * Full request metadata for server-side tools.
72
+ * Provides access to HTTP method, URL, and headers.
73
+ *
74
+ * @example
75
+ * ```typescript
76
+ * handler: async (params, context) => {
77
+ * console.log(`Tool called from: ${context?.request?.url}`);
78
+ * // Forward auth to internal service
79
+ * const authHeader = context?.request?.headers?.authorization;
80
+ * }
81
+ * ```
82
+ */
83
+ request?: {
84
+ /** HTTP method (GET, POST, etc.) */
85
+ method?: string;
86
+ /** Request URL path */
87
+ url?: string;
88
+ /** Request headers */
89
+ headers?: Record<string, string>;
90
+ };
91
+ /**
92
+ * Data passed from user's approval action.
93
+ * Only present when tool has `needsApproval: true` and user approved with extra data.
94
+ *
95
+ * @example
96
+ * ```typescript
97
+ * // In render function:
98
+ * approval.onApprove({ supervisor: selectedSupervisor });
99
+ *
100
+ * // In handler:
101
+ * handler: async (params, context) => {
102
+ * const supervisor = context?.approvalData?.supervisor;
103
+ * await assignToSupervisor(params.ticketId, supervisor);
104
+ * }
105
+ * ```
106
+ */
107
+ approvalData?: Record<string, unknown>;
108
+ }
109
+ /**
110
+ * AI response behavior for tool results.
111
+ *
112
+ * Controls what the AI sees after a tool executes and renders UI.
113
+ *
114
+ * - `'none'`: AI generates minimal response, UI component handles display
115
+ * - `'brief'`: AI gets summary context (via aiContext), gives brief acknowledgment
116
+ * - `'full'`: AI receives full data and responds accordingly (default)
117
+ */
118
+ type AIResponseMode = "none" | "brief" | "full";
119
+ /**
120
+ * Multimodal content for AI to analyze
121
+ */
122
+ type AIContent = {
123
+ type: "image";
124
+ data: string;
125
+ mediaType: string;
126
+ } | {
127
+ type: "text";
128
+ text: string;
129
+ };
130
+ /**
131
+ * Tool response format
132
+ */
133
+ interface ToolResponse<T = unknown> {
134
+ /** Whether the tool succeeded */
135
+ success: boolean;
136
+ /** Human-readable message */
137
+ message?: string;
138
+ /** Error message if failed */
139
+ error?: string;
140
+ /** Result data */
141
+ data?: T;
142
+ /**
143
+ * Override AI context for this specific result.
144
+ * Takes precedence over tool-level aiContext config.
145
+ * If set, this message is sent to AI instead of full result data.
146
+ *
147
+ * @example
148
+ * ```typescript
149
+ * return {
150
+ * success: true,
151
+ * data: sensitiveData,
152
+ * _aiContext: '[Data retrieved - contains sensitive info, displayed to user]'
153
+ * };
154
+ * ```
155
+ */
156
+ _aiContext?: string;
157
+ /**
158
+ * Override AI response mode for this specific result.
159
+ * Takes precedence over tool-level aiResponseMode config.
160
+ */
161
+ _aiResponseMode?: AIResponseMode;
162
+ /**
163
+ * Content for AI to analyze (images, documents, etc.).
164
+ * When present, these are included as multimodal content for AI analysis.
165
+ *
166
+ * @example
167
+ * ```typescript
168
+ * // Screenshot for AI to analyze
169
+ * return {
170
+ * success: true,
171
+ * message: 'Screenshot captured',
172
+ * _aiContent: [{ type: 'image', data: base64, mediaType: 'image/png' }]
173
+ * };
174
+ * ```
175
+ */
176
+ _aiContent?: AIContent[];
177
+ }
178
+ /**
179
+ * Approval callbacks passed to render when tool requires user action.
180
+ * Only present when status is "approval-required".
181
+ */
182
+ interface ToolApprovalCallbacks {
183
+ /**
184
+ * Approve execution and optionally pass extra data to the handler.
185
+ * The extraData is available in handler via `context.approvalData`.
186
+ *
187
+ * @example
188
+ * ```tsx
189
+ * // Simple approval
190
+ * approval.onApprove();
191
+ *
192
+ * // Approval with data (e.g., user selection)
193
+ * approval.onApprove({ supervisor: { name: "John", role: "Manager" } });
194
+ * ```
195
+ */
196
+ onApprove: (extraData?: Record<string, unknown>) => void;
197
+ /**
198
+ * Reject the tool execution with optional reason.
199
+ * This stops the tool from executing and returns an error to AI.
200
+ */
201
+ onReject: (reason?: string) => void;
202
+ /** Custom message from tool's approvalMessage config */
203
+ message?: string;
204
+ }
205
+ /**
206
+ * Props passed to tool render function.
207
+ *
208
+ * The render function is called for every status change, enabling
209
+ * full lifecycle rendering similar to Vercel AI SDK.
210
+ *
211
+ * @example
212
+ * ```tsx
213
+ * render: ({ status, args, approval, result }) => {
214
+ * if (status === "approval-required" && approval) {
215
+ * return <ApprovalCard onConfirm={() => approval.onApprove()} />;
216
+ * }
217
+ * if (status === "executing") {
218
+ * return <LoadingSkeleton />;
219
+ * }
220
+ * if (status === "completed") {
221
+ * return <ResultCard data={result.data} />;
222
+ * }
223
+ * return null;
224
+ * }
225
+ * ```
226
+ */
227
+ interface ToolRenderProps<TParams = Record<string, unknown>> {
228
+ /**
229
+ * Current execution status:
230
+ * - `pending`: Tool call received, waiting to start
231
+ * - `approval-required`: Waiting for user approval (when needsApproval is set)
232
+ * - `executing`: Handler is running
233
+ * - `completed`: Handler finished successfully
234
+ * - `error`: Handler failed
235
+ */
236
+ status: "pending" | "approval-required" | "executing" | "completed" | "error";
237
+ /** Arguments passed to the tool */
238
+ args: TParams;
239
+ /** Result if completed */
240
+ result?: ToolResponse;
241
+ /** Error if failed */
242
+ error?: string;
243
+ /** Tool call ID */
244
+ toolCallId: string;
245
+ /** Tool name */
246
+ toolName: string;
247
+ /**
248
+ * Approval callbacks - only present when status is "approval-required".
249
+ * Use these to create custom approval UIs that can pass extra data to the handler.
250
+ */
251
+ approval?: ToolApprovalCallbacks;
252
+ }
253
+ /**
254
+ * Tool definition with JSON Schema
255
+ */
256
+ interface ToolDefinition<TParams = Record<string, unknown>> {
257
+ /** Unique tool name */
258
+ name: string;
259
+ /** Tool description for LLM */
260
+ description: string;
261
+ /** Where the tool executes (server or client) */
262
+ location: ToolLocation;
263
+ /**
264
+ * Human-readable title for UI display.
265
+ * Can be a static string or a function that generates title from args.
266
+ *
267
+ * @example
268
+ * ```typescript
269
+ * title: "Get order details"
270
+ * // or dynamic:
271
+ * title: (args) => `Order #${args.orderId}`
272
+ * ```
273
+ */
274
+ title?: string | ((args: TParams) => string);
275
+ /**
276
+ * Title shown while executing (present tense with "...").
277
+ * If not provided, uses `title` with "..." appended.
278
+ *
279
+ * @example
280
+ * ```typescript
281
+ * executingTitle: (args) => `Fetching order #${args.orderId}...`
282
+ * ```
283
+ */
284
+ executingTitle?: string | ((args: TParams) => string);
285
+ /**
286
+ * Title shown after completion.
287
+ * If not provided, defaults to `title`.
288
+ *
289
+ * @example
290
+ * ```typescript
291
+ * completedTitle: (args) => `Retrieved order #${args.orderId}`
292
+ * ```
293
+ */
294
+ completedTitle?: string | ((args: TParams) => string);
295
+ /** JSON Schema for input parameters */
296
+ inputSchema: ToolInputSchema;
297
+ /** Handler function (optional for client tools registered on server) */
298
+ handler?: (params: TParams, context?: ToolContext) => Promise<ToolResponse> | ToolResponse;
299
+ /** Optional render function for UI */
300
+ render?: (props: ToolRenderProps<TParams>) => unknown;
301
+ /** Whether the tool is available (for conditional registration) */
302
+ available?: boolean;
303
+ /**
304
+ * Require user approval before execution.
305
+ * Can be:
306
+ * - `true`: Always require approval
307
+ * - `false` or `undefined`: No approval needed (default)
308
+ * - `(params) => boolean`: Conditional approval based on input
309
+ *
310
+ * Similar to Vercel AI SDK v6's needsApproval pattern.
311
+ */
312
+ needsApproval?: boolean | ((params: TParams) => boolean | Promise<boolean>);
313
+ /**
314
+ * Custom message shown in the approval UI.
315
+ * Can be a string or a function that generates a message from params.
316
+ * If not provided, a default message with the tool name is shown.
317
+ */
318
+ approvalMessage?: string | ((params: TParams) => string);
319
+ /**
320
+ * How the AI should respond when this tool's result is rendered as UI.
321
+ *
322
+ * - `'none'`: AI generates minimal response ("[Result displayed to user]").
323
+ * Use for tools where UI component fully handles the display (stats cards, etc.)
324
+ *
325
+ * - `'brief'`: AI receives summary context (from aiContext) and gives brief acknowledgment.
326
+ * Use for charts/visualizations where AI should acknowledge but not repeat data.
327
+ *
328
+ * - `'full'`: AI receives complete data and responds accordingly (default).
329
+ * Use for tools where AI should analyze and elaborate on results.
330
+ *
331
+ * @default 'full'
332
+ *
333
+ * @example
334
+ * ```typescript
335
+ * // Chart tool - AI acknowledges without repeating data
336
+ * const chartTool: ToolDefinition = {
337
+ * name: 'get_chart',
338
+ * aiResponseMode: 'brief',
339
+ * aiContext: (result) => `[Chart displayed: ${result.data.title}]`,
340
+ * handler: async () => ({ success: true, data: chartData })
341
+ * };
342
+ * ```
343
+ */
344
+ aiResponseMode?: AIResponseMode;
345
+ /**
346
+ * Context/summary sent to AI instead of (or along with) full result.
347
+ *
348
+ * Used when:
349
+ * - `aiResponseMode: 'brief'` - This becomes the only thing AI sees
350
+ * - `aiResponseMode: 'full'` - This is prepended to full data for context
351
+ *
352
+ * Can be:
353
+ * - `string`: Static message (e.g., "[Weather data displayed]")
354
+ * - `function`: Dynamic based on result (e.g., (result) => `[Chart: ${result.data.title}]`)
355
+ *
356
+ * @example
357
+ * ```typescript
358
+ * // Static context
359
+ * aiContext: '[Analytics chart displayed to user]'
360
+ *
361
+ * // Dynamic context based on result
362
+ * aiContext: (result, args) => {
363
+ * const { title, currentValue } = result.data;
364
+ * return `[Chart displayed: ${title}, showing ${currentValue}]`;
365
+ * }
366
+ * ```
367
+ */
368
+ aiContext?: string | ((result: ToolResponse, args: Record<string, unknown>) => string);
369
+ }
370
+ /**
371
+ * Unified tool call format (internal representation)
372
+ */
373
+ interface UnifiedToolCall {
374
+ /** Unique tool call ID */
375
+ id: string;
376
+ /** Tool name */
377
+ name: string;
378
+ /** Tool input arguments */
379
+ input: Record<string, unknown>;
380
+ }
381
+ /**
382
+ * Unified tool result format
383
+ */
384
+ interface UnifiedToolResult {
385
+ /** Tool call ID this result is for */
386
+ toolCallId: string;
387
+ /** Serialized result content (JSON string) */
388
+ content: string;
389
+ /** Whether the tool succeeded */
390
+ success: boolean;
391
+ /** Error message if failed */
392
+ error?: string;
393
+ }
394
+ /**
395
+ * Tool execution status
396
+ */
397
+ type ToolExecutionStatus = "pending" | "executing" | "completed" | "error";
398
+ /**
399
+ * Tool approval status (for human-in-the-loop)
400
+ *
401
+ * Similar to Vercel AI SDK v6's tool approval pattern.
402
+ */
403
+ type ToolApprovalStatus = "none" | "required" | "approved" | "rejected";
404
+ /**
405
+ * Permission level for tool execution
406
+ *
407
+ * Controls whether approval is needed and how the choice is remembered:
408
+ * - "ask" - Always prompt user (default)
409
+ * - "allow_always" - Auto-approve, persisted to storage
410
+ * - "deny_always" - Auto-reject, persisted to storage
411
+ * - "session" - Auto-approve for current session only
412
+ */
413
+ type PermissionLevel = "ask" | "allow_always" | "deny_always" | "session";
414
+ /**
415
+ * Stored tool permission record
416
+ */
417
+ interface ToolPermission {
418
+ /** Tool name (unique identifier) */
419
+ toolName: string;
420
+ /** Permission level */
421
+ level: PermissionLevel;
422
+ /** When permission was set */
423
+ createdAt: number;
424
+ /** Last time this permission was used */
425
+ lastUsedAt?: number;
426
+ }
427
+ /**
428
+ * Permission storage configuration
429
+ */
430
+ interface PermissionStorageConfig {
431
+ /**
432
+ * Storage type:
433
+ * - "localStorage" - Persists across browser sessions
434
+ * - "sessionStorage" - Clears when tab closes
435
+ * - "memory" - In-memory only (for SSR or testing)
436
+ */
437
+ type: "localStorage" | "sessionStorage" | "memory";
438
+ /** Storage key prefix (default: "yourgpt-permissions") */
439
+ keyPrefix?: string;
440
+ }
441
+ /**
442
+ * Permission storage adapter interface (for custom implementations)
443
+ */
444
+ interface PermissionStorageAdapter {
445
+ /** Get permission for a tool */
446
+ get(toolName: string): Promise<ToolPermission | null>;
447
+ /** Set permission for a tool */
448
+ set(permission: ToolPermission): Promise<void>;
449
+ /** Remove permission for a tool */
450
+ remove(toolName: string): Promise<void>;
451
+ /** Get all permissions */
452
+ getAll(): Promise<ToolPermission[]>;
453
+ /** Clear all permissions */
454
+ clear(): Promise<void>;
455
+ }
456
+ /**
457
+ * Tool execution record (for UI tracking)
458
+ */
459
+ interface ToolExecution {
460
+ /** Tool call ID */
461
+ id: string;
462
+ /** Tool name */
463
+ name: string;
464
+ /** Tool arguments */
465
+ args: Record<string, unknown>;
466
+ /** Execution status */
467
+ status: ToolExecutionStatus;
468
+ /** Result if completed */
469
+ result?: ToolResponse;
470
+ /** Error message if failed */
471
+ error?: string;
472
+ /** Timestamp when execution started */
473
+ timestamp: number;
474
+ /** Duration in ms (set when completed) */
475
+ duration?: number;
476
+ /** Approval status for this execution */
477
+ approvalStatus: ToolApprovalStatus;
478
+ /** Message shown in approval UI (from tool's approvalMessage) */
479
+ approvalMessage?: string;
480
+ /** Timestamp when user responded to approval request */
481
+ approvalTimestamp?: number;
482
+ }
483
+ /**
484
+ * Agentic loop configuration
485
+ */
486
+ interface AgentLoopConfig {
487
+ /** Maximum iterations before stopping (default: 20) */
488
+ maxIterations?: number;
489
+ /** Enable debug logging */
490
+ debug?: boolean;
491
+ /** Whether to enable the agentic loop (default: true) */
492
+ enabled?: boolean;
493
+ }
494
+ /**
495
+ * Agent loop state (for tracking)
496
+ */
497
+ interface AgentLoopState {
498
+ /** Current iteration number */
499
+ iteration: number;
500
+ /** Maximum iterations allowed */
501
+ maxIterations: number;
502
+ /** Whether the loop is currently running */
503
+ running: boolean;
504
+ /** Whether max iterations was reached */
505
+ maxIterationsReached: boolean;
506
+ /** Whether the loop was aborted */
507
+ aborted: boolean;
508
+ }
509
+ /**
510
+ * A set of tools, keyed by tool name
511
+ *
512
+ * @example
513
+ * ```typescript
514
+ * const myTools: ToolSet = {
515
+ * capture_screenshot: screenshotTool,
516
+ * get_weather: weatherTool,
517
+ * };
518
+ * ```
519
+ */
520
+ type ToolSet = Record<string, ToolDefinition>;
521
+ /**
522
+ * Configuration for creating a tool
523
+ */
524
+ interface ToolConfig<TParams = Record<string, unknown>> {
525
+ /** Tool description for LLM */
526
+ description: string;
527
+ /** Where the tool executes (default: 'client') */
528
+ location?: ToolLocation;
529
+ /** Human-readable title for UI display */
530
+ title?: string | ((args: TParams) => string);
531
+ /** Title shown while executing */
532
+ executingTitle?: string | ((args: TParams) => string);
533
+ /** Title shown after completion */
534
+ completedTitle?: string | ((args: TParams) => string);
535
+ /** JSON Schema for input parameters */
536
+ inputSchema?: ToolInputSchema;
537
+ /** Handler function */
538
+ handler?: (params: TParams, context?: ToolContext) => Promise<ToolResponse> | ToolResponse;
539
+ /** Optional render function for UI */
540
+ render?: (props: ToolRenderProps<TParams>) => unknown;
541
+ /** Whether the tool is available */
542
+ available?: boolean;
543
+ /** Require user approval before execution */
544
+ needsApproval?: boolean | ((params: TParams) => boolean | Promise<boolean>);
545
+ /** Custom message shown in the approval UI */
546
+ approvalMessage?: string | ((params: TParams) => string);
547
+ /** AI response mode for this tool (default: 'full') */
548
+ aiResponseMode?: AIResponseMode;
549
+ /** Context/summary sent to AI instead of full result */
550
+ aiContext?: string | ((result: ToolResponse, args: Record<string, unknown>) => string);
551
+ }
552
+ /**
553
+ * Create a tool definition (similar to Vercel AI SDK's tool())
554
+ *
555
+ * @example
556
+ * ```typescript
557
+ * const weatherTool = tool({
558
+ * description: 'Get weather for a location',
559
+ * inputSchema: {
560
+ * type: 'object',
561
+ * properties: {
562
+ * location: { type: 'string', description: 'City name' },
563
+ * },
564
+ * required: ['location'],
565
+ * },
566
+ * handler: async ({ location }) => {
567
+ * const weather = await fetchWeather(location);
568
+ * return success(weather);
569
+ * },
570
+ * });
571
+ * ```
572
+ */
573
+ declare function tool<TParams = Record<string, unknown>>(config: ToolConfig<TParams>): Omit<ToolDefinition<TParams>, "name">;
574
+ /**
575
+ * Convert ToolDefinition to OpenAI tool format
576
+ */
577
+ declare function toolToOpenAIFormat(tool: ToolDefinition): object;
578
+ /**
579
+ * Convert ToolDefinition to Anthropic tool format
580
+ */
581
+ declare function toolToAnthropicFormat(tool: ToolDefinition): object;
582
+ /**
583
+ * Create a tool result response
584
+ */
585
+ declare function createToolResult(toolCallId: string, response: ToolResponse): UnifiedToolResult;
586
+ /**
587
+ * Create a successful tool response
588
+ */
589
+ declare function success<T = unknown>(data?: T, message?: string): ToolResponse<T>;
590
+ /**
591
+ * Create a failed tool response
592
+ */
593
+ declare function failure(error: string): ToolResponse;
594
+
595
+ export { type AIProvider as A, type JSONSchemaProperty as J, type PermissionLevel as P, type ToolDefinition as T, type UnifiedToolCall as U, type ToolExecutionStatus as a, type ToolResponse as b, type ToolInputSchema as c, type ToolLocation as d, type ToolContext as e, type ToolRenderProps as f, type ToolConfig as g, type ToolSet as h, type UnifiedToolResult as i, type ToolApprovalStatus as j, type ToolExecution as k, type AgentLoopConfig as l, type AgentLoopState as m, type AIResponseMode as n, type AIContent as o, type ToolPermission as p, type PermissionStorageConfig as q, type PermissionStorageAdapter as r, toolToOpenAIFormat as s, tool as t, toolToAnthropicFormat as u, createToolResult as v, success as w, failure as x };