@raindrop-ai/ai-sdk 0.0.19-beta.3 → 0.0.19
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 +22 -3
- package/dist/{chunk-TVOZIVJA.mjs → chunk-CNLUX2MV.mjs} +93 -767
- package/dist/index-C9H6HnZm.d.mts +313 -0
- package/dist/index-C9H6HnZm.d.ts +313 -0
- package/dist/{index.d.mts → index.browser.d.mts} +2 -290
- package/dist/{index.d.ts → index.browser.d.ts} +2 -290
- package/dist/{index.js → index.browser.js} +68 -746
- package/dist/index.browser.mjs +3314 -0
- package/dist/index.node.d.mts +1 -1
- package/dist/index.node.d.ts +1 -1
- package/dist/index.node.js +69 -746
- package/dist/index.node.mjs +1 -1
- package/dist/index.workers.d.mts +1 -1
- package/dist/index.workers.d.ts +1 -1
- package/dist/index.workers.js +69 -746
- package/dist/index.workers.mjs +1 -1
- package/package.json +15 -29
- package/dist/index.mjs +0 -1
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
type IdentifyInput = {
|
|
2
|
+
userId: string;
|
|
3
|
+
traits?: Record<string, unknown>;
|
|
4
|
+
};
|
|
5
|
+
|
|
6
|
+
type ParentSpanContext = {
|
|
7
|
+
traceIdB64: string;
|
|
8
|
+
spanIdB64: string;
|
|
9
|
+
eventId: string;
|
|
10
|
+
};
|
|
11
|
+
interface ContextSpan {
|
|
12
|
+
readonly traceIdB64: string;
|
|
13
|
+
readonly spanIdB64: string;
|
|
14
|
+
readonly eventId: string;
|
|
15
|
+
log?(data: Record<string, unknown>): void;
|
|
16
|
+
}
|
|
17
|
+
interface AsyncLocalStorageLike<T> {
|
|
18
|
+
getStore(): T | undefined;
|
|
19
|
+
run<R>(store: T, callback: () => R): R;
|
|
20
|
+
enterWith?(store: T): void;
|
|
21
|
+
}
|
|
22
|
+
declare abstract class ContextManager {
|
|
23
|
+
abstract getParentSpanIds(): ParentSpanContext | undefined;
|
|
24
|
+
abstract runInContext<R>(span: ContextSpan, callback: () => R): R;
|
|
25
|
+
abstract getCurrentSpan(): ContextSpan | undefined;
|
|
26
|
+
abstract isReady(): boolean;
|
|
27
|
+
}
|
|
28
|
+
declare global {
|
|
29
|
+
var RAINDROP_CONTEXT_MANAGER: (new () => ContextManager) | undefined;
|
|
30
|
+
var RAINDROP_ASYNC_LOCAL_STORAGE: (new <T>() => AsyncLocalStorageLike<T>) | undefined;
|
|
31
|
+
}
|
|
32
|
+
declare function getContextManager(): ContextManager;
|
|
33
|
+
declare function currentSpan(): ContextSpan;
|
|
34
|
+
declare function withCurrent<R>(span: ContextSpan, callback: () => R): R;
|
|
35
|
+
|
|
36
|
+
declare function _resetWarnedMissingUserId(): void;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Options for creating event metadata for call-time context override.
|
|
40
|
+
*/
|
|
41
|
+
type EventMetadataOptions = {
|
|
42
|
+
/** User identifier (overrides wrap-time userId) */
|
|
43
|
+
userId?: string;
|
|
44
|
+
/** Event identifier. If not provided, a unique ID is generated. */
|
|
45
|
+
eventId?: string;
|
|
46
|
+
/** Conversation identifier to group related events */
|
|
47
|
+
convoId?: string;
|
|
48
|
+
/** Custom event name (overrides wrap-time eventName) */
|
|
49
|
+
eventName?: string;
|
|
50
|
+
/** Additional properties to merge with wrap-time properties */
|
|
51
|
+
properties?: Record<string, unknown>;
|
|
52
|
+
};
|
|
53
|
+
/**
|
|
54
|
+
* Creates metadata for use with the AI SDK's `experimental_telemetry.metadata` option.
|
|
55
|
+
* This allows per-call context override when using the wrapped AI SDK.
|
|
56
|
+
*
|
|
57
|
+
* Call-time values override wrap-time defaults. Properties are merged (call-time wins on conflicts).
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```typescript
|
|
61
|
+
* import { generateText } from "ai";
|
|
62
|
+
* import { createRaindropAISDK, eventMetadata } from "@raindrop-ai/ai-sdk";
|
|
63
|
+
*
|
|
64
|
+
* const raindrop = createRaindropAISDK({ writeKey: "..." });
|
|
65
|
+
* const { generateText } = raindrop.wrap(ai, {
|
|
66
|
+
* context: { userId: "default-user" } // wrap-time defaults
|
|
67
|
+
* });
|
|
68
|
+
*
|
|
69
|
+
* // Override context per-call
|
|
70
|
+
* const result = await generateText({
|
|
71
|
+
* model: openai("gpt-4"),
|
|
72
|
+
* prompt: "Hello!",
|
|
73
|
+
* experimental_telemetry: {
|
|
74
|
+
* isEnabled: true,
|
|
75
|
+
* metadata: eventMetadata({
|
|
76
|
+
* convoId: "convo-123", // add convoId for this call
|
|
77
|
+
* eventName: "custom-chat", // override eventName
|
|
78
|
+
* }),
|
|
79
|
+
* },
|
|
80
|
+
* });
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
declare function eventMetadata(options: EventMetadataOptions): Record<string, string>;
|
|
84
|
+
type AISDKChatRequestMessageLike = {
|
|
85
|
+
id?: string;
|
|
86
|
+
role?: string;
|
|
87
|
+
[key: string]: unknown;
|
|
88
|
+
};
|
|
89
|
+
type AISDKChatRequestLike = {
|
|
90
|
+
id?: string;
|
|
91
|
+
messageId?: string;
|
|
92
|
+
trigger?: string;
|
|
93
|
+
messages?: AISDKChatRequestMessageLike[];
|
|
94
|
+
[key: string]: unknown;
|
|
95
|
+
};
|
|
96
|
+
/**
|
|
97
|
+
* Creates stable Raindrop metadata from an AI SDK UI chat request body.
|
|
98
|
+
*
|
|
99
|
+
* This is useful for `useChat` applications that require multiple server round-trips
|
|
100
|
+
* for a single user turn, e.g. when the model triggers client-side tools.
|
|
101
|
+
*
|
|
102
|
+
* The helper derives:
|
|
103
|
+
* - `convoId` from the AI SDK chat request `id`
|
|
104
|
+
* - `eventId` from the latest user message id in `messages`
|
|
105
|
+
*
|
|
106
|
+
* Reusing the same derived `eventId` across tool handoff round-trips allows the
|
|
107
|
+
* wrapper to patch one logical event instead of creating a new event per POST.
|
|
108
|
+
*/
|
|
109
|
+
declare function eventMetadataFromChatRequest(options: EventMetadataOptions & {
|
|
110
|
+
request: AISDKChatRequestLike;
|
|
111
|
+
}): Record<string, string>;
|
|
112
|
+
type AgentCallMetadata = ReturnType<typeof eventMetadata>;
|
|
113
|
+
type NormalizeUnknownAgentOptions<T> = T extends {
|
|
114
|
+
options: infer CallOptions;
|
|
115
|
+
} ? unknown extends CallOptions ? Omit<T, "options"> & {
|
|
116
|
+
options?: never;
|
|
117
|
+
} : T : T;
|
|
118
|
+
/**
|
|
119
|
+
* Wraps an agent instance type so `generate()` and `stream()` accept optional
|
|
120
|
+
* Raindrop call metadata while preserving original return types.
|
|
121
|
+
*/
|
|
122
|
+
type AgentWithMetadata<A> = A extends {
|
|
123
|
+
generate: (...args: infer GenerateArgs) => infer GenerateReturn;
|
|
124
|
+
stream: (...args: infer StreamArgs) => infer StreamReturn;
|
|
125
|
+
} ? Omit<A, "generate" | "stream"> & {
|
|
126
|
+
generate(...args: GenerateArgs extends [infer Options, ...infer Rest] ? [NormalizeUnknownAgentOptions<Options> & {
|
|
127
|
+
metadata?: AgentCallMetadata;
|
|
128
|
+
}, ...Rest] : GenerateArgs): GenerateReturn;
|
|
129
|
+
stream(...args: StreamArgs extends [infer Options, ...infer Rest] ? [NormalizeUnknownAgentOptions<Options> & {
|
|
130
|
+
metadata?: AgentCallMetadata;
|
|
131
|
+
}, ...Rest] : StreamArgs): StreamReturn;
|
|
132
|
+
} : A;
|
|
133
|
+
type AgentConstructor = abstract new (...args: any[]) => any;
|
|
134
|
+
type WrapAgentExport<T extends object, K extends PropertyKey> = K extends keyof T ? T[K] extends AgentConstructor ? Omit<T, K> & {
|
|
135
|
+
[P in K]: new (...args: ConstructorParameters<T[K]>) => AgentWithMetadata<InstanceType<T[K]>>;
|
|
136
|
+
} : T : T;
|
|
137
|
+
/**
|
|
138
|
+
* Structural wrapper type for AI SDK modules.
|
|
139
|
+
*
|
|
140
|
+
* Rewrites AI SDK agent constructor exports so their instance `generate()` and
|
|
141
|
+
* `stream()` methods accept Raindrop `metadata` while preserving original
|
|
142
|
+
* return types. Covers the current and deprecated public export names.
|
|
143
|
+
*/
|
|
144
|
+
type WrappedAISDK<T extends object> = WrapAgentExport<WrapAgentExport<WrapAgentExport<T, "ToolLoopAgent">, "Experimental_Agent">, "Agent">;
|
|
145
|
+
/**
|
|
146
|
+
* Backward-compatible alias for wrapped AI SDK module types.
|
|
147
|
+
*
|
|
148
|
+
* This alias intentionally avoids referencing `import("ai")` to satisfy type
|
|
149
|
+
* resolution without requiring `ai` to be installed.
|
|
150
|
+
*
|
|
151
|
+
* Prefer `WrappedAISDK<typeof ai>` in app code when you have an `ai` import.
|
|
152
|
+
*/
|
|
153
|
+
type WrappedAI<T extends object = any> = WrappedAISDK<T>;
|
|
154
|
+
type Attachment = {
|
|
155
|
+
attachment_id?: string;
|
|
156
|
+
name?: string;
|
|
157
|
+
value: string;
|
|
158
|
+
role: "input" | "output";
|
|
159
|
+
} & ({
|
|
160
|
+
type: "code";
|
|
161
|
+
language?: string;
|
|
162
|
+
} | {
|
|
163
|
+
type: "text" | "image" | "iframe";
|
|
164
|
+
});
|
|
165
|
+
type RaindropAISDKOptions = {
|
|
166
|
+
/**
|
|
167
|
+
* API write key. If omitted, telemetry shipping is disabled but `wrap()` is
|
|
168
|
+
* still available for a consistent integration surface.
|
|
169
|
+
*/
|
|
170
|
+
writeKey?: string;
|
|
171
|
+
endpoint?: string;
|
|
172
|
+
traces?: {
|
|
173
|
+
enabled?: boolean;
|
|
174
|
+
flushIntervalMs?: number;
|
|
175
|
+
maxBatchSize?: number;
|
|
176
|
+
maxQueueSize?: number;
|
|
177
|
+
debug?: boolean;
|
|
178
|
+
debugSpans?: boolean;
|
|
179
|
+
};
|
|
180
|
+
events?: {
|
|
181
|
+
enabled?: boolean;
|
|
182
|
+
partialFlushMs?: number;
|
|
183
|
+
debug?: boolean;
|
|
184
|
+
};
|
|
185
|
+
};
|
|
186
|
+
type RaindropAISDKContext = {
|
|
187
|
+
userId?: string;
|
|
188
|
+
eventId?: string;
|
|
189
|
+
eventName?: string;
|
|
190
|
+
convoId?: string;
|
|
191
|
+
properties?: Record<string, unknown>;
|
|
192
|
+
attachments?: Attachment[];
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
type BuildEventPatch = {
|
|
196
|
+
eventName?: string;
|
|
197
|
+
input?: string;
|
|
198
|
+
output?: string;
|
|
199
|
+
model?: string;
|
|
200
|
+
properties?: Record<string, unknown>;
|
|
201
|
+
attachments?: Attachment[];
|
|
202
|
+
};
|
|
203
|
+
/**
|
|
204
|
+
* Message format passed to `buildEvent`.
|
|
205
|
+
*
|
|
206
|
+
* This is intentionally "AI SDK shaped" but loosely typed:
|
|
207
|
+
* - works across AI SDK versions
|
|
208
|
+
* - avoids requiring users to import AI SDK types
|
|
209
|
+
*/
|
|
210
|
+
type AISDKMessage = {
|
|
211
|
+
role: "system" | "user" | "assistant" | "tool" | string;
|
|
212
|
+
content: unknown;
|
|
213
|
+
[key: string]: unknown;
|
|
214
|
+
};
|
|
215
|
+
/**
|
|
216
|
+
* Build or override the Raindrop Event payload from an AI SDK conversation transcript.
|
|
217
|
+
*
|
|
218
|
+
* The wrapper calls this twice:
|
|
219
|
+
* - early with the input messages (system/user)
|
|
220
|
+
* - at the end with appended response messages (assistant/tool, incl tool-call + tool-result parts)
|
|
221
|
+
*/
|
|
222
|
+
type EventBuilder = (messages: AISDKMessage[]) => void | BuildEventPatch;
|
|
223
|
+
type SelfDiagnosticsSignalDefinition = {
|
|
224
|
+
/**
|
|
225
|
+
* Human-readable meaning of this signal.
|
|
226
|
+
* Used in the injected tool schema and stored in signal properties.
|
|
227
|
+
*/
|
|
228
|
+
description: string;
|
|
229
|
+
/**
|
|
230
|
+
* Optional default sentiment for this signal key.
|
|
231
|
+
* This can help dashboards classify negative/positive programmatic signals.
|
|
232
|
+
*/
|
|
233
|
+
sentiment?: "POSITIVE" | "NEGATIVE";
|
|
234
|
+
};
|
|
235
|
+
type SelfDiagnosticsSignalDefinitions = Record<string, SelfDiagnosticsSignalDefinition>;
|
|
236
|
+
type SelfDiagnosticsOptions = {
|
|
237
|
+
/** Enable automatic injection of the self diagnostics tool. Default: false */
|
|
238
|
+
enabled?: boolean;
|
|
239
|
+
/**
|
|
240
|
+
* Signal keys and descriptions exposed to the model.
|
|
241
|
+
* Defaults to a built-in set when omitted.
|
|
242
|
+
*/
|
|
243
|
+
signals?: SelfDiagnosticsSignalDefinitions;
|
|
244
|
+
/**
|
|
245
|
+
* Optional extra guidance for when the model should emit self diagnostics.
|
|
246
|
+
* The SDK still generates a full tool prompt from `signals`.
|
|
247
|
+
*/
|
|
248
|
+
guidance?: string;
|
|
249
|
+
/**
|
|
250
|
+
* Optional tool name override for the injected self diagnostics tool.
|
|
251
|
+
* Defaults to "__raindrop_report".
|
|
252
|
+
*/
|
|
253
|
+
toolName?: string;
|
|
254
|
+
};
|
|
255
|
+
type WrapAISDKOptions = {
|
|
256
|
+
context: RaindropAISDKContext | ((info: {
|
|
257
|
+
operation: string;
|
|
258
|
+
args: unknown;
|
|
259
|
+
}) => RaindropAISDKContext);
|
|
260
|
+
buildEvent?: EventBuilder;
|
|
261
|
+
autoAttachment?: boolean;
|
|
262
|
+
selfDiagnostics?: SelfDiagnosticsOptions;
|
|
263
|
+
send?: {
|
|
264
|
+
events?: boolean;
|
|
265
|
+
traces?: boolean;
|
|
266
|
+
};
|
|
267
|
+
};
|
|
268
|
+
type EventPatch = {
|
|
269
|
+
eventName?: string;
|
|
270
|
+
userId?: string;
|
|
271
|
+
convoId?: string;
|
|
272
|
+
input?: string;
|
|
273
|
+
output?: string;
|
|
274
|
+
model?: string;
|
|
275
|
+
properties?: Record<string, unknown>;
|
|
276
|
+
attachments?: Attachment[];
|
|
277
|
+
isPending?: boolean;
|
|
278
|
+
timestamp?: string;
|
|
279
|
+
};
|
|
280
|
+
type RaindropAISDKClient = {
|
|
281
|
+
wrap<T extends object>(aiSDK: T, options?: WrapAISDKOptions): WrappedAISDK<T>;
|
|
282
|
+
events: {
|
|
283
|
+
patch(eventId: string, patch: EventPatch): Promise<void>;
|
|
284
|
+
addAttachments(eventId: string, attachments: Attachment[]): Promise<void>;
|
|
285
|
+
setProperties(eventId: string, properties: Record<string, unknown>): Promise<void>;
|
|
286
|
+
finish(eventId: string, patch: {
|
|
287
|
+
output?: string;
|
|
288
|
+
model?: string;
|
|
289
|
+
properties?: Record<string, unknown>;
|
|
290
|
+
}): Promise<void>;
|
|
291
|
+
};
|
|
292
|
+
users: {
|
|
293
|
+
identify(users: IdentifyInput | IdentifyInput[]): Promise<void>;
|
|
294
|
+
};
|
|
295
|
+
signals: {
|
|
296
|
+
track(signal: {
|
|
297
|
+
eventId: string;
|
|
298
|
+
name: "thumbs_up" | "thumbs_down" | string;
|
|
299
|
+
type?: "default" | "feedback" | "edit" | "standard" | "agent" | "agent_internal";
|
|
300
|
+
sentiment?: "POSITIVE" | "NEGATIVE";
|
|
301
|
+
timestamp?: string;
|
|
302
|
+
properties?: Record<string, unknown>;
|
|
303
|
+
attachmentId?: string;
|
|
304
|
+
comment?: string;
|
|
305
|
+
after?: string;
|
|
306
|
+
}): Promise<void>;
|
|
307
|
+
};
|
|
308
|
+
flush(): Promise<void>;
|
|
309
|
+
shutdown(): Promise<void>;
|
|
310
|
+
};
|
|
311
|
+
declare function createRaindropAISDK(opts: RaindropAISDKOptions): RaindropAISDKClient;
|
|
312
|
+
|
|
313
|
+
export { type AISDKChatRequestLike as A, type BuildEventPatch as B, ContextManager as C, type EventBuilder as E, type IdentifyInput as I, type RaindropAISDKClient as R, type SelfDiagnosticsOptions as S, type WrapAISDKOptions as W, _resetWarnedMissingUserId as _, type AISDKChatRequestMessageLike as a, type AISDKMessage as b, type AgentCallMetadata as c, type AgentWithMetadata as d, type Attachment as e, type ContextSpan as f, type EventMetadataOptions as g, type RaindropAISDKContext as h, type RaindropAISDKOptions as i, type SelfDiagnosticsSignalDefinition as j, type SelfDiagnosticsSignalDefinitions as k, type WrappedAI as l, type WrappedAISDK as m, createRaindropAISDK as n, currentSpan as o, eventMetadata as p, eventMetadataFromChatRequest as q, getContextManager as r, withCurrent as w };
|
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
type IdentifyInput = {
|
|
2
|
+
userId: string;
|
|
3
|
+
traits?: Record<string, unknown>;
|
|
4
|
+
};
|
|
5
|
+
|
|
6
|
+
type ParentSpanContext = {
|
|
7
|
+
traceIdB64: string;
|
|
8
|
+
spanIdB64: string;
|
|
9
|
+
eventId: string;
|
|
10
|
+
};
|
|
11
|
+
interface ContextSpan {
|
|
12
|
+
readonly traceIdB64: string;
|
|
13
|
+
readonly spanIdB64: string;
|
|
14
|
+
readonly eventId: string;
|
|
15
|
+
log?(data: Record<string, unknown>): void;
|
|
16
|
+
}
|
|
17
|
+
interface AsyncLocalStorageLike<T> {
|
|
18
|
+
getStore(): T | undefined;
|
|
19
|
+
run<R>(store: T, callback: () => R): R;
|
|
20
|
+
enterWith?(store: T): void;
|
|
21
|
+
}
|
|
22
|
+
declare abstract class ContextManager {
|
|
23
|
+
abstract getParentSpanIds(): ParentSpanContext | undefined;
|
|
24
|
+
abstract runInContext<R>(span: ContextSpan, callback: () => R): R;
|
|
25
|
+
abstract getCurrentSpan(): ContextSpan | undefined;
|
|
26
|
+
abstract isReady(): boolean;
|
|
27
|
+
}
|
|
28
|
+
declare global {
|
|
29
|
+
var RAINDROP_CONTEXT_MANAGER: (new () => ContextManager) | undefined;
|
|
30
|
+
var RAINDROP_ASYNC_LOCAL_STORAGE: (new <T>() => AsyncLocalStorageLike<T>) | undefined;
|
|
31
|
+
}
|
|
32
|
+
declare function getContextManager(): ContextManager;
|
|
33
|
+
declare function currentSpan(): ContextSpan;
|
|
34
|
+
declare function withCurrent<R>(span: ContextSpan, callback: () => R): R;
|
|
35
|
+
|
|
36
|
+
declare function _resetWarnedMissingUserId(): void;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Options for creating event metadata for call-time context override.
|
|
40
|
+
*/
|
|
41
|
+
type EventMetadataOptions = {
|
|
42
|
+
/** User identifier (overrides wrap-time userId) */
|
|
43
|
+
userId?: string;
|
|
44
|
+
/** Event identifier. If not provided, a unique ID is generated. */
|
|
45
|
+
eventId?: string;
|
|
46
|
+
/** Conversation identifier to group related events */
|
|
47
|
+
convoId?: string;
|
|
48
|
+
/** Custom event name (overrides wrap-time eventName) */
|
|
49
|
+
eventName?: string;
|
|
50
|
+
/** Additional properties to merge with wrap-time properties */
|
|
51
|
+
properties?: Record<string, unknown>;
|
|
52
|
+
};
|
|
53
|
+
/**
|
|
54
|
+
* Creates metadata for use with the AI SDK's `experimental_telemetry.metadata` option.
|
|
55
|
+
* This allows per-call context override when using the wrapped AI SDK.
|
|
56
|
+
*
|
|
57
|
+
* Call-time values override wrap-time defaults. Properties are merged (call-time wins on conflicts).
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```typescript
|
|
61
|
+
* import { generateText } from "ai";
|
|
62
|
+
* import { createRaindropAISDK, eventMetadata } from "@raindrop-ai/ai-sdk";
|
|
63
|
+
*
|
|
64
|
+
* const raindrop = createRaindropAISDK({ writeKey: "..." });
|
|
65
|
+
* const { generateText } = raindrop.wrap(ai, {
|
|
66
|
+
* context: { userId: "default-user" } // wrap-time defaults
|
|
67
|
+
* });
|
|
68
|
+
*
|
|
69
|
+
* // Override context per-call
|
|
70
|
+
* const result = await generateText({
|
|
71
|
+
* model: openai("gpt-4"),
|
|
72
|
+
* prompt: "Hello!",
|
|
73
|
+
* experimental_telemetry: {
|
|
74
|
+
* isEnabled: true,
|
|
75
|
+
* metadata: eventMetadata({
|
|
76
|
+
* convoId: "convo-123", // add convoId for this call
|
|
77
|
+
* eventName: "custom-chat", // override eventName
|
|
78
|
+
* }),
|
|
79
|
+
* },
|
|
80
|
+
* });
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
declare function eventMetadata(options: EventMetadataOptions): Record<string, string>;
|
|
84
|
+
type AISDKChatRequestMessageLike = {
|
|
85
|
+
id?: string;
|
|
86
|
+
role?: string;
|
|
87
|
+
[key: string]: unknown;
|
|
88
|
+
};
|
|
89
|
+
type AISDKChatRequestLike = {
|
|
90
|
+
id?: string;
|
|
91
|
+
messageId?: string;
|
|
92
|
+
trigger?: string;
|
|
93
|
+
messages?: AISDKChatRequestMessageLike[];
|
|
94
|
+
[key: string]: unknown;
|
|
95
|
+
};
|
|
96
|
+
/**
|
|
97
|
+
* Creates stable Raindrop metadata from an AI SDK UI chat request body.
|
|
98
|
+
*
|
|
99
|
+
* This is useful for `useChat` applications that require multiple server round-trips
|
|
100
|
+
* for a single user turn, e.g. when the model triggers client-side tools.
|
|
101
|
+
*
|
|
102
|
+
* The helper derives:
|
|
103
|
+
* - `convoId` from the AI SDK chat request `id`
|
|
104
|
+
* - `eventId` from the latest user message id in `messages`
|
|
105
|
+
*
|
|
106
|
+
* Reusing the same derived `eventId` across tool handoff round-trips allows the
|
|
107
|
+
* wrapper to patch one logical event instead of creating a new event per POST.
|
|
108
|
+
*/
|
|
109
|
+
declare function eventMetadataFromChatRequest(options: EventMetadataOptions & {
|
|
110
|
+
request: AISDKChatRequestLike;
|
|
111
|
+
}): Record<string, string>;
|
|
112
|
+
type AgentCallMetadata = ReturnType<typeof eventMetadata>;
|
|
113
|
+
type NormalizeUnknownAgentOptions<T> = T extends {
|
|
114
|
+
options: infer CallOptions;
|
|
115
|
+
} ? unknown extends CallOptions ? Omit<T, "options"> & {
|
|
116
|
+
options?: never;
|
|
117
|
+
} : T : T;
|
|
118
|
+
/**
|
|
119
|
+
* Wraps an agent instance type so `generate()` and `stream()` accept optional
|
|
120
|
+
* Raindrop call metadata while preserving original return types.
|
|
121
|
+
*/
|
|
122
|
+
type AgentWithMetadata<A> = A extends {
|
|
123
|
+
generate: (...args: infer GenerateArgs) => infer GenerateReturn;
|
|
124
|
+
stream: (...args: infer StreamArgs) => infer StreamReturn;
|
|
125
|
+
} ? Omit<A, "generate" | "stream"> & {
|
|
126
|
+
generate(...args: GenerateArgs extends [infer Options, ...infer Rest] ? [NormalizeUnknownAgentOptions<Options> & {
|
|
127
|
+
metadata?: AgentCallMetadata;
|
|
128
|
+
}, ...Rest] : GenerateArgs): GenerateReturn;
|
|
129
|
+
stream(...args: StreamArgs extends [infer Options, ...infer Rest] ? [NormalizeUnknownAgentOptions<Options> & {
|
|
130
|
+
metadata?: AgentCallMetadata;
|
|
131
|
+
}, ...Rest] : StreamArgs): StreamReturn;
|
|
132
|
+
} : A;
|
|
133
|
+
type AgentConstructor = abstract new (...args: any[]) => any;
|
|
134
|
+
type WrapAgentExport<T extends object, K extends PropertyKey> = K extends keyof T ? T[K] extends AgentConstructor ? Omit<T, K> & {
|
|
135
|
+
[P in K]: new (...args: ConstructorParameters<T[K]>) => AgentWithMetadata<InstanceType<T[K]>>;
|
|
136
|
+
} : T : T;
|
|
137
|
+
/**
|
|
138
|
+
* Structural wrapper type for AI SDK modules.
|
|
139
|
+
*
|
|
140
|
+
* Rewrites AI SDK agent constructor exports so their instance `generate()` and
|
|
141
|
+
* `stream()` methods accept Raindrop `metadata` while preserving original
|
|
142
|
+
* return types. Covers the current and deprecated public export names.
|
|
143
|
+
*/
|
|
144
|
+
type WrappedAISDK<T extends object> = WrapAgentExport<WrapAgentExport<WrapAgentExport<T, "ToolLoopAgent">, "Experimental_Agent">, "Agent">;
|
|
145
|
+
/**
|
|
146
|
+
* Backward-compatible alias for wrapped AI SDK module types.
|
|
147
|
+
*
|
|
148
|
+
* This alias intentionally avoids referencing `import("ai")` to satisfy type
|
|
149
|
+
* resolution without requiring `ai` to be installed.
|
|
150
|
+
*
|
|
151
|
+
* Prefer `WrappedAISDK<typeof ai>` in app code when you have an `ai` import.
|
|
152
|
+
*/
|
|
153
|
+
type WrappedAI<T extends object = any> = WrappedAISDK<T>;
|
|
154
|
+
type Attachment = {
|
|
155
|
+
attachment_id?: string;
|
|
156
|
+
name?: string;
|
|
157
|
+
value: string;
|
|
158
|
+
role: "input" | "output";
|
|
159
|
+
} & ({
|
|
160
|
+
type: "code";
|
|
161
|
+
language?: string;
|
|
162
|
+
} | {
|
|
163
|
+
type: "text" | "image" | "iframe";
|
|
164
|
+
});
|
|
165
|
+
type RaindropAISDKOptions = {
|
|
166
|
+
/**
|
|
167
|
+
* API write key. If omitted, telemetry shipping is disabled but `wrap()` is
|
|
168
|
+
* still available for a consistent integration surface.
|
|
169
|
+
*/
|
|
170
|
+
writeKey?: string;
|
|
171
|
+
endpoint?: string;
|
|
172
|
+
traces?: {
|
|
173
|
+
enabled?: boolean;
|
|
174
|
+
flushIntervalMs?: number;
|
|
175
|
+
maxBatchSize?: number;
|
|
176
|
+
maxQueueSize?: number;
|
|
177
|
+
debug?: boolean;
|
|
178
|
+
debugSpans?: boolean;
|
|
179
|
+
};
|
|
180
|
+
events?: {
|
|
181
|
+
enabled?: boolean;
|
|
182
|
+
partialFlushMs?: number;
|
|
183
|
+
debug?: boolean;
|
|
184
|
+
};
|
|
185
|
+
};
|
|
186
|
+
type RaindropAISDKContext = {
|
|
187
|
+
userId?: string;
|
|
188
|
+
eventId?: string;
|
|
189
|
+
eventName?: string;
|
|
190
|
+
convoId?: string;
|
|
191
|
+
properties?: Record<string, unknown>;
|
|
192
|
+
attachments?: Attachment[];
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
type BuildEventPatch = {
|
|
196
|
+
eventName?: string;
|
|
197
|
+
input?: string;
|
|
198
|
+
output?: string;
|
|
199
|
+
model?: string;
|
|
200
|
+
properties?: Record<string, unknown>;
|
|
201
|
+
attachments?: Attachment[];
|
|
202
|
+
};
|
|
203
|
+
/**
|
|
204
|
+
* Message format passed to `buildEvent`.
|
|
205
|
+
*
|
|
206
|
+
* This is intentionally "AI SDK shaped" but loosely typed:
|
|
207
|
+
* - works across AI SDK versions
|
|
208
|
+
* - avoids requiring users to import AI SDK types
|
|
209
|
+
*/
|
|
210
|
+
type AISDKMessage = {
|
|
211
|
+
role: "system" | "user" | "assistant" | "tool" | string;
|
|
212
|
+
content: unknown;
|
|
213
|
+
[key: string]: unknown;
|
|
214
|
+
};
|
|
215
|
+
/**
|
|
216
|
+
* Build or override the Raindrop Event payload from an AI SDK conversation transcript.
|
|
217
|
+
*
|
|
218
|
+
* The wrapper calls this twice:
|
|
219
|
+
* - early with the input messages (system/user)
|
|
220
|
+
* - at the end with appended response messages (assistant/tool, incl tool-call + tool-result parts)
|
|
221
|
+
*/
|
|
222
|
+
type EventBuilder = (messages: AISDKMessage[]) => void | BuildEventPatch;
|
|
223
|
+
type SelfDiagnosticsSignalDefinition = {
|
|
224
|
+
/**
|
|
225
|
+
* Human-readable meaning of this signal.
|
|
226
|
+
* Used in the injected tool schema and stored in signal properties.
|
|
227
|
+
*/
|
|
228
|
+
description: string;
|
|
229
|
+
/**
|
|
230
|
+
* Optional default sentiment for this signal key.
|
|
231
|
+
* This can help dashboards classify negative/positive programmatic signals.
|
|
232
|
+
*/
|
|
233
|
+
sentiment?: "POSITIVE" | "NEGATIVE";
|
|
234
|
+
};
|
|
235
|
+
type SelfDiagnosticsSignalDefinitions = Record<string, SelfDiagnosticsSignalDefinition>;
|
|
236
|
+
type SelfDiagnosticsOptions = {
|
|
237
|
+
/** Enable automatic injection of the self diagnostics tool. Default: false */
|
|
238
|
+
enabled?: boolean;
|
|
239
|
+
/**
|
|
240
|
+
* Signal keys and descriptions exposed to the model.
|
|
241
|
+
* Defaults to a built-in set when omitted.
|
|
242
|
+
*/
|
|
243
|
+
signals?: SelfDiagnosticsSignalDefinitions;
|
|
244
|
+
/**
|
|
245
|
+
* Optional extra guidance for when the model should emit self diagnostics.
|
|
246
|
+
* The SDK still generates a full tool prompt from `signals`.
|
|
247
|
+
*/
|
|
248
|
+
guidance?: string;
|
|
249
|
+
/**
|
|
250
|
+
* Optional tool name override for the injected self diagnostics tool.
|
|
251
|
+
* Defaults to "__raindrop_report".
|
|
252
|
+
*/
|
|
253
|
+
toolName?: string;
|
|
254
|
+
};
|
|
255
|
+
type WrapAISDKOptions = {
|
|
256
|
+
context: RaindropAISDKContext | ((info: {
|
|
257
|
+
operation: string;
|
|
258
|
+
args: unknown;
|
|
259
|
+
}) => RaindropAISDKContext);
|
|
260
|
+
buildEvent?: EventBuilder;
|
|
261
|
+
autoAttachment?: boolean;
|
|
262
|
+
selfDiagnostics?: SelfDiagnosticsOptions;
|
|
263
|
+
send?: {
|
|
264
|
+
events?: boolean;
|
|
265
|
+
traces?: boolean;
|
|
266
|
+
};
|
|
267
|
+
};
|
|
268
|
+
type EventPatch = {
|
|
269
|
+
eventName?: string;
|
|
270
|
+
userId?: string;
|
|
271
|
+
convoId?: string;
|
|
272
|
+
input?: string;
|
|
273
|
+
output?: string;
|
|
274
|
+
model?: string;
|
|
275
|
+
properties?: Record<string, unknown>;
|
|
276
|
+
attachments?: Attachment[];
|
|
277
|
+
isPending?: boolean;
|
|
278
|
+
timestamp?: string;
|
|
279
|
+
};
|
|
280
|
+
type RaindropAISDKClient = {
|
|
281
|
+
wrap<T extends object>(aiSDK: T, options?: WrapAISDKOptions): WrappedAISDK<T>;
|
|
282
|
+
events: {
|
|
283
|
+
patch(eventId: string, patch: EventPatch): Promise<void>;
|
|
284
|
+
addAttachments(eventId: string, attachments: Attachment[]): Promise<void>;
|
|
285
|
+
setProperties(eventId: string, properties: Record<string, unknown>): Promise<void>;
|
|
286
|
+
finish(eventId: string, patch: {
|
|
287
|
+
output?: string;
|
|
288
|
+
model?: string;
|
|
289
|
+
properties?: Record<string, unknown>;
|
|
290
|
+
}): Promise<void>;
|
|
291
|
+
};
|
|
292
|
+
users: {
|
|
293
|
+
identify(users: IdentifyInput | IdentifyInput[]): Promise<void>;
|
|
294
|
+
};
|
|
295
|
+
signals: {
|
|
296
|
+
track(signal: {
|
|
297
|
+
eventId: string;
|
|
298
|
+
name: "thumbs_up" | "thumbs_down" | string;
|
|
299
|
+
type?: "default" | "feedback" | "edit" | "standard" | "agent" | "agent_internal";
|
|
300
|
+
sentiment?: "POSITIVE" | "NEGATIVE";
|
|
301
|
+
timestamp?: string;
|
|
302
|
+
properties?: Record<string, unknown>;
|
|
303
|
+
attachmentId?: string;
|
|
304
|
+
comment?: string;
|
|
305
|
+
after?: string;
|
|
306
|
+
}): Promise<void>;
|
|
307
|
+
};
|
|
308
|
+
flush(): Promise<void>;
|
|
309
|
+
shutdown(): Promise<void>;
|
|
310
|
+
};
|
|
311
|
+
declare function createRaindropAISDK(opts: RaindropAISDKOptions): RaindropAISDKClient;
|
|
312
|
+
|
|
313
|
+
export { type AISDKChatRequestLike as A, type BuildEventPatch as B, ContextManager as C, type EventBuilder as E, type IdentifyInput as I, type RaindropAISDKClient as R, type SelfDiagnosticsOptions as S, type WrapAISDKOptions as W, _resetWarnedMissingUserId as _, type AISDKChatRequestMessageLike as a, type AISDKMessage as b, type AgentCallMetadata as c, type AgentWithMetadata as d, type Attachment as e, type ContextSpan as f, type EventMetadataOptions as g, type RaindropAISDKContext as h, type RaindropAISDKOptions as i, type SelfDiagnosticsSignalDefinition as j, type SelfDiagnosticsSignalDefinitions as k, type WrappedAI as l, type WrappedAISDK as m, createRaindropAISDK as n, currentSpan as o, eventMetadata as p, eventMetadataFromChatRequest as q, getContextManager as r, withCurrent as w };
|