@turingpulse/sdk-vercel-ai 1.0.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/package.json +16 -0
- package/src/index.ts +5 -0
- package/src/wrapper.ts +49 -0
package/package.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@turingpulse/sdk-vercel-ai",
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"license": "SEE LICENSE IN LICENSE",
|
|
5
|
+
"description": "TuringPulse SDK integration for Vercel AI SDK",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "dist/index.js",
|
|
8
|
+
"types": "dist/index.d.ts",
|
|
9
|
+
"dependencies": {
|
|
10
|
+
"@turingpulse/sdk": ">=1.0.0"
|
|
11
|
+
},
|
|
12
|
+
"peerDependencies": {
|
|
13
|
+
"ai": ">=6.0.0",
|
|
14
|
+
"typescript": ">=5.0.0"
|
|
15
|
+
}
|
|
16
|
+
}
|
package/src/index.ts
ADDED
package/src/wrapper.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { withInstrumentation, GovernanceDirective, currentContext } from '@turingpulse/sdk';
|
|
2
|
+
|
|
3
|
+
interface VercelAIInstrumentOptions {
|
|
4
|
+
name: string;
|
|
5
|
+
governance?: GovernanceDirective;
|
|
6
|
+
model?: string;
|
|
7
|
+
provider?: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Wrap Vercel AI SDK's generateText/streamText for TuringPulse observability.
|
|
12
|
+
*/
|
|
13
|
+
export function instrumentVercelAI(
|
|
14
|
+
generateFn: (...args: unknown[]) => Promise<unknown>,
|
|
15
|
+
options: VercelAIInstrumentOptions,
|
|
16
|
+
): (...args: unknown[]) => Promise<unknown> {
|
|
17
|
+
const { name, governance, model, provider = 'openai' } = options;
|
|
18
|
+
|
|
19
|
+
const wrappedFn = withInstrumentation(
|
|
20
|
+
async function vercelAIGenerate(...args: unknown[]): Promise<unknown> {
|
|
21
|
+
const result = await generateFn(...args);
|
|
22
|
+
|
|
23
|
+
const ctx = currentContext();
|
|
24
|
+
if (ctx) {
|
|
25
|
+
ctx.framework = 'vercel-ai';
|
|
26
|
+
ctx.nodeType = 'llm';
|
|
27
|
+
|
|
28
|
+
const resp = result as Record<string, unknown>;
|
|
29
|
+
const usage = resp?.usage as { promptTokens?: number; completionTokens?: number } | undefined;
|
|
30
|
+
if (usage) {
|
|
31
|
+
ctx.setTokens(usage.promptTokens ?? 0, usage.completionTokens ?? 0);
|
|
32
|
+
}
|
|
33
|
+
if (model) {
|
|
34
|
+
ctx.setModel(model, provider);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const firstArg = args[0] as Record<string, unknown> | undefined;
|
|
38
|
+
const inputStr = (firstArg?.prompt as string) ?? JSON.stringify(firstArg);
|
|
39
|
+
const outputStr = (resp?.text as string) ?? JSON.stringify(result);
|
|
40
|
+
ctx.setIO(inputStr, outputStr);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return result;
|
|
44
|
+
},
|
|
45
|
+
{ name, governance },
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
return wrappedFn as (...args: unknown[]) => Promise<unknown>;
|
|
49
|
+
}
|