@dogpile/sdk 0.2.2 → 0.3.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.
- package/CHANGELOG.md +15 -0
- package/dist/browser/index.js +1044 -507
- package/dist/browser/index.js.map +1 -1
- package/dist/index.d.ts +5 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/runtime/broadcast.d.ts +1 -0
- package/dist/runtime/broadcast.d.ts.map +1 -1
- package/dist/runtime/broadcast.js +28 -19
- package/dist/runtime/broadcast.js.map +1 -1
- package/dist/runtime/coordinator.d.ts +1 -0
- package/dist/runtime/coordinator.d.ts.map +1 -1
- package/dist/runtime/coordinator.js +46 -21
- package/dist/runtime/coordinator.js.map +1 -1
- package/dist/runtime/engine.d.ts.map +1 -1
- package/dist/runtime/engine.js +5 -0
- package/dist/runtime/engine.js.map +1 -1
- package/dist/runtime/ids.d.ts +19 -0
- package/dist/runtime/ids.d.ts.map +1 -0
- package/dist/runtime/ids.js +36 -0
- package/dist/runtime/ids.js.map +1 -0
- package/dist/runtime/logger.d.ts +61 -0
- package/dist/runtime/logger.d.ts.map +1 -0
- package/dist/runtime/logger.js +114 -0
- package/dist/runtime/logger.js.map +1 -0
- package/dist/runtime/retry.d.ts +99 -0
- package/dist/runtime/retry.d.ts.map +1 -0
- package/dist/runtime/retry.js +181 -0
- package/dist/runtime/retry.js.map +1 -0
- package/dist/runtime/sequential.d.ts +1 -0
- package/dist/runtime/sequential.d.ts.map +1 -1
- package/dist/runtime/sequential.js +25 -16
- package/dist/runtime/sequential.js.map +1 -1
- package/dist/runtime/shared.d.ts +1 -0
- package/dist/runtime/shared.d.ts.map +1 -1
- package/dist/runtime/shared.js +25 -19
- package/dist/runtime/shared.js.map +1 -1
- package/dist/runtime/termination.d.ts +6 -1
- package/dist/runtime/termination.d.ts.map +1 -1
- package/dist/runtime/termination.js +75 -0
- package/dist/runtime/termination.js.map +1 -1
- package/dist/runtime/tools/built-in.d.ts +99 -0
- package/dist/runtime/tools/built-in.d.ts.map +1 -0
- package/dist/runtime/tools/built-in.js +577 -0
- package/dist/runtime/tools/built-in.js.map +1 -0
- package/dist/runtime/tools/vercel-ai.d.ts +67 -0
- package/dist/runtime/tools/vercel-ai.d.ts.map +1 -0
- package/dist/runtime/tools/vercel-ai.js +148 -0
- package/dist/runtime/tools/vercel-ai.js.map +1 -0
- package/dist/runtime/tools.d.ts +5 -268
- package/dist/runtime/tools.d.ts.map +1 -1
- package/dist/runtime/tools.js +7 -770
- package/dist/runtime/tools.js.map +1 -1
- package/dist/runtime/validation.d.ts.map +1 -1
- package/dist/runtime/validation.js +22 -0
- package/dist/runtime/validation.js.map +1 -1
- package/dist/runtime/wrap-up.d.ts +26 -0
- package/dist/runtime/wrap-up.d.ts.map +1 -0
- package/dist/runtime/wrap-up.js +178 -0
- package/dist/runtime/wrap-up.js.map +1 -0
- package/dist/types/benchmark.d.ts +276 -0
- package/dist/types/benchmark.d.ts.map +1 -0
- package/dist/types/benchmark.js +2 -0
- package/dist/types/benchmark.js.map +1 -0
- package/dist/types/events.d.ts +495 -0
- package/dist/types/events.d.ts.map +1 -0
- package/dist/types/events.js +2 -0
- package/dist/types/events.js.map +1 -0
- package/dist/types/replay.d.ts +169 -0
- package/dist/types/replay.d.ts.map +1 -0
- package/dist/types/replay.js +2 -0
- package/dist/types/replay.js.map +1 -0
- package/dist/types.d.ts +74 -935
- package/dist/types.d.ts.map +1 -1
- package/package.json +28 -1
- package/src/index.ts +7 -1
- package/src/runtime/broadcast.ts +50 -35
- package/src/runtime/coordinator.ts +84 -43
- package/src/runtime/engine.ts +6 -0
- package/src/runtime/ids.ts +41 -0
- package/src/runtime/logger.ts +152 -0
- package/src/runtime/retry.ts +270 -0
- package/src/runtime/sequential.ts +46 -31
- package/src/runtime/shared.ts +46 -35
- package/src/runtime/termination.ts +100 -0
- package/src/runtime/tools/built-in.ts +875 -0
- package/src/runtime/tools/vercel-ai.ts +269 -0
- package/src/runtime/tools.ts +60 -1255
- package/src/runtime/validation.ts +25 -0
- package/src/runtime/wrap-up.ts +257 -0
- package/src/types/benchmark.ts +300 -0
- package/src/types/events.ts +544 -0
- package/src/types/replay.ts +201 -0
- package/src/types.ts +174 -994
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
JsonObject,
|
|
3
|
+
JsonValue,
|
|
4
|
+
ModelMessage,
|
|
5
|
+
RuntimeTool,
|
|
6
|
+
RuntimeToolExecutionContext,
|
|
7
|
+
RuntimeToolIdentity,
|
|
8
|
+
RuntimeToolInputSchema,
|
|
9
|
+
RuntimeToolResult
|
|
10
|
+
} from "../../types.js";
|
|
11
|
+
import { DogpileError } from "../../types.js";
|
|
12
|
+
|
|
13
|
+
type VercelAIToolExecuteOptions = {
|
|
14
|
+
readonly toolCallId: string;
|
|
15
|
+
readonly messages: ModelMessage[];
|
|
16
|
+
readonly abortSignal?: AbortSignal;
|
|
17
|
+
readonly experimental_context: RuntimeToolExecutionContext;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
type VercelAIToolExecuteFunction<Input, Output> = (
|
|
21
|
+
input: Input,
|
|
22
|
+
options: VercelAIToolExecuteOptions
|
|
23
|
+
) => Output | PromiseLike<Output> | AsyncIterable<Output>;
|
|
24
|
+
|
|
25
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
26
|
+
type VercelAICompatibleSchema<Input> = unknown;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Vercel AI SDK tool shape accepted by Dogpile's normalization adapter.
|
|
30
|
+
*/
|
|
31
|
+
export interface VercelAITool<Input extends JsonObject = JsonObject, Output extends JsonValue = JsonValue> {
|
|
32
|
+
readonly description?: string;
|
|
33
|
+
readonly inputSchema: VercelAICompatibleSchema<Input>;
|
|
34
|
+
readonly execute?: VercelAIToolExecuteFunction<Input, Output>;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Optional identity fields callers may layer onto normalized Vercel AI tools.
|
|
39
|
+
*/
|
|
40
|
+
export interface VercelAIToolIdentityOptions {
|
|
41
|
+
readonly id?: string;
|
|
42
|
+
readonly namespace?: string;
|
|
43
|
+
readonly version?: string;
|
|
44
|
+
readonly description?: string;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Definition used to normalize one Vercel AI SDK tool into Dogpile's runtime tool interface.
|
|
49
|
+
*/
|
|
50
|
+
export interface VercelAIToolDefinition<
|
|
51
|
+
Name extends string = string,
|
|
52
|
+
Input extends JsonObject = JsonObject,
|
|
53
|
+
Output extends JsonValue = JsonValue
|
|
54
|
+
> {
|
|
55
|
+
readonly name: Name;
|
|
56
|
+
readonly tool: VercelAITool<Input, Output>;
|
|
57
|
+
readonly identity?: VercelAIToolIdentityOptions;
|
|
58
|
+
readonly inputSchema?: RuntimeToolInputSchema;
|
|
59
|
+
readonly messages?: readonly ModelMessage[];
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Caller-supplied Vercel AI SDK tool set keyed by model-visible tool name.
|
|
64
|
+
*/
|
|
65
|
+
export interface VercelAIToolSetEntry {
|
|
66
|
+
readonly description?: string;
|
|
67
|
+
readonly inputSchema: VercelAICompatibleSchema<unknown>;
|
|
68
|
+
readonly execute?: VercelAIToolExecuteFunction<never, JsonValue>;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Caller-supplied Vercel AI SDK tool set keyed by model-visible tool name.
|
|
73
|
+
*/
|
|
74
|
+
export type VercelAIToolSet = Readonly<Record<string, VercelAIToolSetEntry>>;
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Options shared while normalizing a Vercel AI SDK tool set.
|
|
78
|
+
*/
|
|
79
|
+
export interface VercelAIToolSetNormalizationOptions {
|
|
80
|
+
readonly namespace?: string;
|
|
81
|
+
readonly version?: string;
|
|
82
|
+
readonly messages?: readonly ModelMessage[];
|
|
83
|
+
readonly identity?: Readonly<Record<string, VercelAIToolIdentityOptions>>;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Normalize one Vercel AI SDK tool into Dogpile's shared runtime tool interface.
|
|
88
|
+
*/
|
|
89
|
+
export async function normalizeVercelAITool<
|
|
90
|
+
Name extends string,
|
|
91
|
+
Input extends JsonObject,
|
|
92
|
+
Output extends JsonValue
|
|
93
|
+
>(definition: VercelAIToolDefinition<Name, Input, Output>): Promise<RuntimeTool<Input, Output>> {
|
|
94
|
+
if (!definition.tool.execute) {
|
|
95
|
+
throw new DogpileError({
|
|
96
|
+
code: "invalid-configuration",
|
|
97
|
+
message: `Vercel AI tool "${definition.name}" must define execute() to run inside Dogpile.`,
|
|
98
|
+
detail: { toolName: definition.name }
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const identity = vercelAIToolIdentity(definition);
|
|
103
|
+
const inputSchema = definition.inputSchema ?? (await vercelAIInputSchema(definition.tool, definition.name));
|
|
104
|
+
const execute = definition.tool.execute;
|
|
105
|
+
|
|
106
|
+
return {
|
|
107
|
+
identity,
|
|
108
|
+
inputSchema,
|
|
109
|
+
async execute(input, context): Promise<RuntimeToolResult<Output>> {
|
|
110
|
+
try {
|
|
111
|
+
const output = await resolveVercelAIToolOutput(
|
|
112
|
+
execute(input, {
|
|
113
|
+
toolCallId: context.toolCallId,
|
|
114
|
+
messages: Array.from(definition.messages ?? []),
|
|
115
|
+
...(context.abortSignal ? { abortSignal: context.abortSignal } : {}),
|
|
116
|
+
experimental_context: context
|
|
117
|
+
})
|
|
118
|
+
);
|
|
119
|
+
|
|
120
|
+
return {
|
|
121
|
+
type: "success",
|
|
122
|
+
toolCallId: context.toolCallId,
|
|
123
|
+
tool: identity,
|
|
124
|
+
output
|
|
125
|
+
};
|
|
126
|
+
} catch (error) {
|
|
127
|
+
return {
|
|
128
|
+
type: "error",
|
|
129
|
+
toolCallId: context.toolCallId,
|
|
130
|
+
tool: identity,
|
|
131
|
+
error: {
|
|
132
|
+
code: "vercel-ai-tool-error",
|
|
133
|
+
message: error instanceof Error ? error.message : "Vercel AI tool execution failed.",
|
|
134
|
+
retryable: false,
|
|
135
|
+
detail: errorDetail(error)
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Normalize a Vercel AI SDK tool set into runtime tools in caller-defined key order.
|
|
145
|
+
*/
|
|
146
|
+
export async function normalizeVercelAITools(
|
|
147
|
+
tools: VercelAIToolSet,
|
|
148
|
+
options: VercelAIToolSetNormalizationOptions = {}
|
|
149
|
+
): Promise<readonly RuntimeTool<JsonObject, JsonValue>[]> {
|
|
150
|
+
return Promise.all(
|
|
151
|
+
Object.entries(tools).map(([name, tool]) => {
|
|
152
|
+
const identity = removeUndefinedIdentityFields({
|
|
153
|
+
...(options.namespace !== undefined ? { namespace: options.namespace } : {}),
|
|
154
|
+
...(options.version !== undefined ? { version: options.version } : {}),
|
|
155
|
+
...options.identity?.[name]
|
|
156
|
+
});
|
|
157
|
+
return normalizeVercelAITool({
|
|
158
|
+
name,
|
|
159
|
+
tool: asJsonRuntimeVercelAITool(tool),
|
|
160
|
+
...(options.messages ? { messages: options.messages } : {}),
|
|
161
|
+
...(identity ? { identity } : {})
|
|
162
|
+
});
|
|
163
|
+
})
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
async function vercelAIInputSchema<Input extends JsonObject, Output extends JsonValue>(
|
|
168
|
+
tool: VercelAITool<Input, Output>,
|
|
169
|
+
name: string
|
|
170
|
+
): Promise<RuntimeToolInputSchema> {
|
|
171
|
+
const schema = await resolveCompatibleSchema(tool.inputSchema);
|
|
172
|
+
const jsonSchema = asJsonObject(schema, `Vercel AI tool "${name}" input schema`);
|
|
173
|
+
|
|
174
|
+
return {
|
|
175
|
+
kind: "json-schema",
|
|
176
|
+
schema: jsonSchema,
|
|
177
|
+
...(tool.description ? { description: tool.description } : {})
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
async function resolveCompatibleSchema<Input>(schema: VercelAICompatibleSchema<Input>): Promise<unknown> {
|
|
182
|
+
if (isJsonSchemaWrapper(schema)) {
|
|
183
|
+
return await schema.jsonSchema;
|
|
184
|
+
}
|
|
185
|
+
return schema;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function isJsonSchemaWrapper<Input>(
|
|
189
|
+
schema: VercelAICompatibleSchema<Input>
|
|
190
|
+
): schema is { readonly jsonSchema: unknown | PromiseLike<unknown> } {
|
|
191
|
+
return typeof schema === "object" && schema !== null && "jsonSchema" in schema;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function vercelAIToolIdentity<Name extends string, Input extends JsonObject, Output extends JsonValue>(
|
|
195
|
+
definition: VercelAIToolDefinition<Name, Input, Output>
|
|
196
|
+
): RuntimeToolIdentity {
|
|
197
|
+
return {
|
|
198
|
+
id: definition.identity?.id ?? `vercel-ai.tools.${definition.name}`,
|
|
199
|
+
name: definition.name,
|
|
200
|
+
namespace: definition.identity?.namespace ?? "vercel-ai",
|
|
201
|
+
...(definition.identity?.version ? { version: definition.identity.version } : {}),
|
|
202
|
+
...(definition.identity?.description ?? definition.tool.description
|
|
203
|
+
? { description: definition.identity?.description ?? definition.tool.description }
|
|
204
|
+
: {})
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
async function resolveVercelAIToolOutput<Output extends JsonValue>(
|
|
209
|
+
output: AsyncIterable<Output> | PromiseLike<Output> | Output
|
|
210
|
+
): Promise<Output> {
|
|
211
|
+
if (isAsyncIterable(output)) {
|
|
212
|
+
let lastOutput: Output | undefined;
|
|
213
|
+
for await (const chunk of output) {
|
|
214
|
+
lastOutput = chunk;
|
|
215
|
+
}
|
|
216
|
+
if (lastOutput === undefined) {
|
|
217
|
+
throw new DogpileError({
|
|
218
|
+
code: "provider-invalid-response",
|
|
219
|
+
message: "Vercel AI tool async iterable completed without an output."
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
return lastOutput;
|
|
223
|
+
}
|
|
224
|
+
return await output;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
function isAsyncIterable<Output extends JsonValue>(
|
|
228
|
+
value: AsyncIterable<Output> | PromiseLike<Output> | Output
|
|
229
|
+
): value is AsyncIterable<Output> {
|
|
230
|
+
return typeof value === "object" && value !== null && Symbol.asyncIterator in value;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
function asJsonObject(value: unknown, label: string): JsonObject {
|
|
234
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
235
|
+
throw new DogpileError({
|
|
236
|
+
code: "provider-invalid-response",
|
|
237
|
+
message: `${label} must resolve to a JSON object.`,
|
|
238
|
+
detail: { label }
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
return value as JsonObject;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
function errorDetail(error: unknown): JsonObject {
|
|
245
|
+
if (error instanceof Error) {
|
|
246
|
+
return { name: error.name };
|
|
247
|
+
}
|
|
248
|
+
return { valueType: typeof error };
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
function removeUndefinedIdentityFields(
|
|
252
|
+
identity: VercelAIToolIdentityOptions
|
|
253
|
+
): VercelAIToolIdentityOptions | undefined {
|
|
254
|
+
const normalized: VercelAIToolIdentityOptions = {
|
|
255
|
+
...(identity.id !== undefined ? { id: identity.id } : {}),
|
|
256
|
+
...(identity.namespace !== undefined ? { namespace: identity.namespace } : {}),
|
|
257
|
+
...(identity.version !== undefined ? { version: identity.version } : {}),
|
|
258
|
+
...(identity.description !== undefined ? { description: identity.description } : {})
|
|
259
|
+
};
|
|
260
|
+
return Object.keys(normalized).length > 0 ? normalized : undefined;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
function asJsonRuntimeVercelAITool(tool: VercelAIToolSetEntry): VercelAITool<JsonObject, JsonValue> {
|
|
264
|
+
// Structural narrowing: the runtime treats every Vercel-AI tool as having a
|
|
265
|
+
// JSON-shaped (input, output) pair — the upstream `ai` generics are nominal
|
|
266
|
+
// and don't survive without an unsafe widening. Pinned here as the single
|
|
267
|
+
// bridge point so future provider migrations only touch one cast.
|
|
268
|
+
return tool as unknown as VercelAITool<JsonObject, JsonValue>;
|
|
269
|
+
}
|