polymorph-sdk 0.1.0 → 0.2.0
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 +59 -84
- package/dist/index.css +2 -0
- package/dist/index.d.ts +64 -135
- package/dist/index.js +18116 -258
- package/package.json +35 -35
- package/src/ChatThread.tsx +26 -0
- package/src/PolymorphWidget.tsx +41 -0
- package/src/VoiceOverlay.tsx +65 -0
- package/src/WidgetPanel.tsx +194 -0
- package/src/env.d.ts +6 -0
- package/src/index.ts +3 -0
- package/src/styles.module.css +240 -0
- package/src/theme.ts +53 -0
- package/src/types.ts +36 -0
- package/src/usePolymorphSession.ts +134 -0
- package/dist/index.d.mts +0 -135
- package/dist/index.mjs +0 -233
package/src/theme.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { MantineColorsTuple, MantineThemeOverride } from "@mantine/core";
|
|
2
|
+
import { createTheme } from "@mantine/core";
|
|
3
|
+
|
|
4
|
+
function hexToMantineTuple(hex: string): MantineColorsTuple {
|
|
5
|
+
const r = Number.parseInt(hex.slice(1, 3), 16);
|
|
6
|
+
const g = Number.parseInt(hex.slice(3, 5), 16);
|
|
7
|
+
const b = Number.parseInt(hex.slice(5, 7), 16);
|
|
8
|
+
|
|
9
|
+
const lighten = (amt: number) =>
|
|
10
|
+
`#${[r, g, b]
|
|
11
|
+
.map((c) =>
|
|
12
|
+
Math.min(255, Math.round(c + (255 - c) * amt))
|
|
13
|
+
.toString(16)
|
|
14
|
+
.padStart(2, "0"),
|
|
15
|
+
)
|
|
16
|
+
.join("")}`;
|
|
17
|
+
|
|
18
|
+
const darken = (amt: number) =>
|
|
19
|
+
`#${[r, g, b]
|
|
20
|
+
.map((c) =>
|
|
21
|
+
Math.max(0, Math.round(c * (1 - amt)))
|
|
22
|
+
.toString(16)
|
|
23
|
+
.padStart(2, "0"),
|
|
24
|
+
)
|
|
25
|
+
.join("")}`;
|
|
26
|
+
|
|
27
|
+
return [
|
|
28
|
+
lighten(0.92),
|
|
29
|
+
lighten(0.84),
|
|
30
|
+
lighten(0.72),
|
|
31
|
+
lighten(0.56),
|
|
32
|
+
lighten(0.36),
|
|
33
|
+
hex,
|
|
34
|
+
darken(0.08),
|
|
35
|
+
darken(0.16),
|
|
36
|
+
darken(0.28),
|
|
37
|
+
darken(0.4),
|
|
38
|
+
];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function buildWidgetTheme(
|
|
42
|
+
primaryColor = "#171717",
|
|
43
|
+
): MantineThemeOverride {
|
|
44
|
+
return createTheme({
|
|
45
|
+
fontFamily:
|
|
46
|
+
'-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif',
|
|
47
|
+
primaryColor: "brand",
|
|
48
|
+
colors: {
|
|
49
|
+
brand: hexToMantineTuple(primaryColor),
|
|
50
|
+
},
|
|
51
|
+
defaultRadius: "md",
|
|
52
|
+
});
|
|
53
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
export interface WidgetConfig {
|
|
2
|
+
apiBaseUrl: string;
|
|
3
|
+
apiKey?: string;
|
|
4
|
+
metadata?: Record<string, string>;
|
|
5
|
+
branding?: WidgetBranding;
|
|
6
|
+
position?: "bottom-right" | "bottom-left";
|
|
7
|
+
/** Enable voice call (default: true). When false, widget is chat-only. */
|
|
8
|
+
enableVoice?: boolean;
|
|
9
|
+
/** Participant identity sent to LiveKit */
|
|
10
|
+
userIdentity?: string;
|
|
11
|
+
/** Participant display name sent to LiveKit */
|
|
12
|
+
userName?: string;
|
|
13
|
+
/** Extra options passed to fetch (e.g. { credentials: "include" }) */
|
|
14
|
+
fetchOptions?: RequestInit;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface WidgetBranding {
|
|
18
|
+
/** FAB and accent color (default: "#171717") */
|
|
19
|
+
primaryColor?: string;
|
|
20
|
+
/** Panel header title (default: "Hi there") */
|
|
21
|
+
title?: string;
|
|
22
|
+
/** Panel subheader */
|
|
23
|
+
subtitle?: string;
|
|
24
|
+
/** Initial message shown before agent connects */
|
|
25
|
+
greeting?: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface ChatMessage {
|
|
29
|
+
id: string;
|
|
30
|
+
role: "user" | "agent";
|
|
31
|
+
text: string;
|
|
32
|
+
source: "chat" | "voice";
|
|
33
|
+
timestamp: number;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export type SessionStatus = "idle" | "connecting" | "connected" | "error";
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { useCallback, useRef, useState } from "react";
|
|
2
|
+
import type { Room } from "livekit-client";
|
|
3
|
+
import type { ChatMessage, SessionStatus, WidgetConfig } from "./types";
|
|
4
|
+
|
|
5
|
+
interface RoomConnection {
|
|
6
|
+
token: string;
|
|
7
|
+
livekitUrl: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function makeGreeting(text: string): ChatMessage {
|
|
11
|
+
return {
|
|
12
|
+
id: "greeting",
|
|
13
|
+
role: "agent",
|
|
14
|
+
text,
|
|
15
|
+
source: "chat",
|
|
16
|
+
timestamp: Date.now(),
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function usePolymorphSession(config: WidgetConfig) {
|
|
21
|
+
const [status, setStatus] = useState<SessionStatus>("idle");
|
|
22
|
+
const [roomConnection, setRoomConnection] = useState<RoomConnection | null>(null);
|
|
23
|
+
const [messages, setMessages] = useState<ChatMessage[]>(() => {
|
|
24
|
+
if (config.branding?.greeting) {
|
|
25
|
+
return [makeGreeting(config.branding.greeting)];
|
|
26
|
+
}
|
|
27
|
+
return [];
|
|
28
|
+
});
|
|
29
|
+
const [isVoiceEnabled, setIsVoiceEnabled] = useState(config.enableVoice !== false);
|
|
30
|
+
const [isMicActive, setIsMicActive] = useState(config.enableVoice !== false);
|
|
31
|
+
const [error, setError] = useState<string | null>(null);
|
|
32
|
+
const roomRef = useRef<Room | null>(null);
|
|
33
|
+
|
|
34
|
+
const addMessage = useCallback((role: "user" | "agent", text: string, source: "chat" | "voice") => {
|
|
35
|
+
setMessages(prev => [...prev, {
|
|
36
|
+
id: crypto.randomUUID(),
|
|
37
|
+
role,
|
|
38
|
+
text,
|
|
39
|
+
source,
|
|
40
|
+
timestamp: Date.now(),
|
|
41
|
+
}]);
|
|
42
|
+
}, []);
|
|
43
|
+
|
|
44
|
+
const connect = useCallback(async () => {
|
|
45
|
+
if (status === "connecting" || status === "connected") return;
|
|
46
|
+
setStatus("connecting");
|
|
47
|
+
setError(null);
|
|
48
|
+
try {
|
|
49
|
+
const { headers: extraHeaders, ...restFetchOptions } = config.fetchOptions ?? {};
|
|
50
|
+
const authHeaders: Record<string, string> = {};
|
|
51
|
+
if (config.apiKey) {
|
|
52
|
+
authHeaders["Authorization"] = `Bearer ${config.apiKey}`;
|
|
53
|
+
}
|
|
54
|
+
const response = await fetch(`${config.apiBaseUrl}/voice-rooms/start`, {
|
|
55
|
+
method: "POST",
|
|
56
|
+
headers: {
|
|
57
|
+
"Content-Type": "application/json",
|
|
58
|
+
...authHeaders,
|
|
59
|
+
...(extraHeaders instanceof Headers
|
|
60
|
+
? Object.fromEntries(extraHeaders.entries())
|
|
61
|
+
: extraHeaders),
|
|
62
|
+
},
|
|
63
|
+
body: JSON.stringify({
|
|
64
|
+
agent_name: "custom-voice-agent",
|
|
65
|
+
metadata: config.metadata,
|
|
66
|
+
user_identity: config.userIdentity,
|
|
67
|
+
user_name: config.userName,
|
|
68
|
+
}),
|
|
69
|
+
...restFetchOptions,
|
|
70
|
+
});
|
|
71
|
+
if (!response.ok) {
|
|
72
|
+
throw new Error(await response.text() || "Failed to start session");
|
|
73
|
+
}
|
|
74
|
+
const data = await response.json();
|
|
75
|
+
setRoomConnection({ token: data.token, livekitUrl: data.livekit_url });
|
|
76
|
+
} catch (err) {
|
|
77
|
+
setError(err instanceof Error ? err.message : "Something went wrong");
|
|
78
|
+
setStatus("error");
|
|
79
|
+
}
|
|
80
|
+
}, [config, status]);
|
|
81
|
+
|
|
82
|
+
const disconnect = useCallback(() => {
|
|
83
|
+
roomRef.current?.disconnect();
|
|
84
|
+
roomRef.current = null;
|
|
85
|
+
setRoomConnection(null);
|
|
86
|
+
setStatus("idle");
|
|
87
|
+
}, []);
|
|
88
|
+
|
|
89
|
+
const sendMessage = useCallback((text: string) => {
|
|
90
|
+
const room = roomRef.current;
|
|
91
|
+
if (!room || !text.trim()) return;
|
|
92
|
+
addMessage("user", text.trim(), "chat");
|
|
93
|
+
const payload = new TextEncoder().encode(JSON.stringify({ text: text.trim() }));
|
|
94
|
+
room.localParticipant.publishData(payload, { reliable: true, topic: "chat_message" });
|
|
95
|
+
}, [addMessage]);
|
|
96
|
+
|
|
97
|
+
const toggleMic = useCallback(async () => {
|
|
98
|
+
const room = roomRef.current;
|
|
99
|
+
if (!room) return;
|
|
100
|
+
const newState = !isMicActive;
|
|
101
|
+
await room.localParticipant.setMicrophoneEnabled(newState);
|
|
102
|
+
setIsMicActive(newState);
|
|
103
|
+
}, [isMicActive]);
|
|
104
|
+
|
|
105
|
+
const toggleVoice = useCallback(async () => {
|
|
106
|
+
const room = roomRef.current;
|
|
107
|
+
if (!room) return;
|
|
108
|
+
const newState = !isVoiceEnabled;
|
|
109
|
+
await room.localParticipant.setMicrophoneEnabled(newState);
|
|
110
|
+
setIsVoiceEnabled(newState);
|
|
111
|
+
setIsMicActive(newState);
|
|
112
|
+
}, [isVoiceEnabled]);
|
|
113
|
+
|
|
114
|
+
const setRoom = useCallback((room: Room | null) => {
|
|
115
|
+
roomRef.current = room;
|
|
116
|
+
if (room) setStatus("connected");
|
|
117
|
+
}, []);
|
|
118
|
+
|
|
119
|
+
return {
|
|
120
|
+
status,
|
|
121
|
+
roomConnection,
|
|
122
|
+
messages,
|
|
123
|
+
isVoiceEnabled,
|
|
124
|
+
isMicActive,
|
|
125
|
+
error,
|
|
126
|
+
connect,
|
|
127
|
+
disconnect,
|
|
128
|
+
addMessage,
|
|
129
|
+
sendMessage,
|
|
130
|
+
toggleMic,
|
|
131
|
+
toggleVoice,
|
|
132
|
+
setRoom,
|
|
133
|
+
};
|
|
134
|
+
}
|
package/dist/index.d.mts
DELETED
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
import { ToolSet, streamText, generateText } from 'ai';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Configuration options for Polymorph SDK.
|
|
5
|
-
*/
|
|
6
|
-
interface PolymorphConfigOptions {
|
|
7
|
-
/**
|
|
8
|
-
* User role for potential RBAC functionality.
|
|
9
|
-
*/
|
|
10
|
-
role: string;
|
|
11
|
-
/**
|
|
12
|
-
* API key for authentication. Can also be set via POLYMORPH_API_KEY env var.
|
|
13
|
-
*/
|
|
14
|
-
apiKey?: string;
|
|
15
|
-
/**
|
|
16
|
-
* Base URL for the Polymorph API. Can also be set via POLYMORPH_BASE_URL env var.
|
|
17
|
-
*/
|
|
18
|
-
baseUrl?: string;
|
|
19
|
-
/**
|
|
20
|
-
* HTTP request timeout in milliseconds. Defaults to 10000 (10 seconds).
|
|
21
|
-
*/
|
|
22
|
-
httpTimeout?: number;
|
|
23
|
-
/**
|
|
24
|
-
* Whether HTTP logging is enabled. Defaults to true.
|
|
25
|
-
*/
|
|
26
|
-
httpEnabled?: boolean;
|
|
27
|
-
}
|
|
28
|
-
/**
|
|
29
|
-
* Configuration for tool logging and permissions.
|
|
30
|
-
*/
|
|
31
|
-
declare class PolymorphConfig {
|
|
32
|
-
readonly role: string;
|
|
33
|
-
readonly apiKey: string | undefined;
|
|
34
|
-
readonly baseUrl: string | undefined;
|
|
35
|
-
readonly httpTimeout: number;
|
|
36
|
-
readonly httpEnabled: boolean;
|
|
37
|
-
constructor(options: PolymorphConfigOptions);
|
|
38
|
-
private getEnv;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* HTTP client for Polymorph API calls.
|
|
43
|
-
*
|
|
44
|
-
* All errors are caught and logged - never crashes the SDK.
|
|
45
|
-
*/
|
|
46
|
-
declare class PolymorphHTTPClient {
|
|
47
|
-
private baseUrl;
|
|
48
|
-
private apiKey;
|
|
49
|
-
private timeout;
|
|
50
|
-
private enabled;
|
|
51
|
-
constructor(options: {
|
|
52
|
-
baseUrl?: string;
|
|
53
|
-
apiKey?: string;
|
|
54
|
-
timeout?: number;
|
|
55
|
-
enabled?: boolean;
|
|
56
|
-
});
|
|
57
|
-
/**
|
|
58
|
-
* Make a POST request to the given endpoint.
|
|
59
|
-
*
|
|
60
|
-
* Returns the response JSON if successful, undefined otherwise.
|
|
61
|
-
* All errors are caught and logged.
|
|
62
|
-
*/
|
|
63
|
-
post<T = unknown>(endpoint: string, payload: Record<string, unknown>): Promise<T | undefined>;
|
|
64
|
-
/**
|
|
65
|
-
* Fire-and-forget POST request. Does not wait for response.
|
|
66
|
-
*/
|
|
67
|
-
postFireAndForget(endpoint: string, payload: Record<string, unknown>): void;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Log an entry to configured destinations.
|
|
72
|
-
*/
|
|
73
|
-
declare function log(entry: Record<string, unknown>): void;
|
|
74
|
-
|
|
75
|
-
type StreamTextParams = Parameters<typeof streamText>[0];
|
|
76
|
-
type GenerateTextParams = Parameters<typeof generateText>[0];
|
|
77
|
-
type StreamTextResult = ReturnType<typeof streamText>;
|
|
78
|
-
type GenerateTextResult = ReturnType<typeof generateText>;
|
|
79
|
-
/**
|
|
80
|
-
* Hook context passed to pre/post tool hooks.
|
|
81
|
-
*/
|
|
82
|
-
interface ToolHookContext {
|
|
83
|
-
toolName: string;
|
|
84
|
-
toolCallId: string;
|
|
85
|
-
input: unknown;
|
|
86
|
-
}
|
|
87
|
-
/**
|
|
88
|
-
* Pre-tool hook result. Return `{ deny: true, reason: string }` to deny the tool call.
|
|
89
|
-
*/
|
|
90
|
-
interface PreToolHookResult {
|
|
91
|
-
deny?: boolean;
|
|
92
|
-
reason?: string;
|
|
93
|
-
}
|
|
94
|
-
/**
|
|
95
|
-
* Post-tool hook context with the result.
|
|
96
|
-
*/
|
|
97
|
-
interface PostToolHookContext extends ToolHookContext {
|
|
98
|
-
result: unknown;
|
|
99
|
-
error?: Error;
|
|
100
|
-
durationMs: number;
|
|
101
|
-
}
|
|
102
|
-
/**
|
|
103
|
-
* Options for creating a Polymorph AI client.
|
|
104
|
-
*/
|
|
105
|
-
interface PolymorphAIClientOptions {
|
|
106
|
-
config: PolymorphConfig;
|
|
107
|
-
}
|
|
108
|
-
/**
|
|
109
|
-
* Polymorph AI client that wraps Vercel AI SDK functions with tool logging.
|
|
110
|
-
*/
|
|
111
|
-
declare class PolymorphAIClient {
|
|
112
|
-
private config;
|
|
113
|
-
private httpClient;
|
|
114
|
-
private preToolHook;
|
|
115
|
-
private postToolHook;
|
|
116
|
-
constructor(options: PolymorphAIClientOptions);
|
|
117
|
-
/**
|
|
118
|
-
* Wraps tools with Polymorph logging hooks.
|
|
119
|
-
*/
|
|
120
|
-
wrapTools<T extends ToolSet>(tools: T): T;
|
|
121
|
-
/**
|
|
122
|
-
* Creates a wrapped streamText function that automatically wraps tools.
|
|
123
|
-
*/
|
|
124
|
-
streamText(originalStreamText: typeof streamText): (params: StreamTextParams) => StreamTextResult;
|
|
125
|
-
/**
|
|
126
|
-
* Creates a wrapped generateText function that automatically wraps tools.
|
|
127
|
-
*/
|
|
128
|
-
generateText(originalGenerateText: typeof generateText): (params: GenerateTextParams) => GenerateTextResult;
|
|
129
|
-
}
|
|
130
|
-
/**
|
|
131
|
-
* Creates a Polymorph AI client for wrapping Vercel AI SDK functions.
|
|
132
|
-
*/
|
|
133
|
-
declare function createPolymorphAIClient(options: PolymorphAIClientOptions): PolymorphAIClient;
|
|
134
|
-
|
|
135
|
-
export { PolymorphAIClient, type PolymorphAIClientOptions, PolymorphConfig, type PolymorphConfigOptions, PolymorphHTTPClient, type PostToolHookContext, type PreToolHookResult, type ToolHookContext, createPolymorphAIClient, log };
|
package/dist/index.mjs
DELETED
|
@@ -1,233 +0,0 @@
|
|
|
1
|
-
// src/config.ts
|
|
2
|
-
var PolymorphConfig = class {
|
|
3
|
-
constructor(options) {
|
|
4
|
-
this.role = options.role;
|
|
5
|
-
this.apiKey = options.apiKey ?? this.getEnv("POLYMORPH_API_KEY");
|
|
6
|
-
this.baseUrl = options.baseUrl ?? this.getEnv("POLYMORPH_BASE_URL");
|
|
7
|
-
this.httpTimeout = options.httpTimeout ?? 1e4;
|
|
8
|
-
this.httpEnabled = options.httpEnabled ?? true;
|
|
9
|
-
if (!this.apiKey) {
|
|
10
|
-
throw new Error(
|
|
11
|
-
"apiKey is required. Provide it in the constructor or set POLYMORPH_API_KEY env var."
|
|
12
|
-
);
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
getEnv(key) {
|
|
16
|
-
if (typeof process !== "undefined" && process.env) {
|
|
17
|
-
return process.env[key];
|
|
18
|
-
}
|
|
19
|
-
return void 0;
|
|
20
|
-
}
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
// src/http-client.ts
|
|
24
|
-
var PolymorphHTTPClient = class {
|
|
25
|
-
constructor(options) {
|
|
26
|
-
this.baseUrl = options.baseUrl;
|
|
27
|
-
this.apiKey = options.apiKey;
|
|
28
|
-
this.timeout = options.timeout ?? 1e4;
|
|
29
|
-
this.enabled = options.enabled ?? true;
|
|
30
|
-
}
|
|
31
|
-
/**
|
|
32
|
-
* Make a POST request to the given endpoint.
|
|
33
|
-
*
|
|
34
|
-
* Returns the response JSON if successful, undefined otherwise.
|
|
35
|
-
* All errors are caught and logged.
|
|
36
|
-
*/
|
|
37
|
-
async post(endpoint, payload) {
|
|
38
|
-
if (!this.enabled) {
|
|
39
|
-
console.debug(`[Polymorph] HTTP client disabled, skipping POST to ${endpoint}`);
|
|
40
|
-
return void 0;
|
|
41
|
-
}
|
|
42
|
-
if (!this.baseUrl) {
|
|
43
|
-
console.debug(`[Polymorph] No baseUrl configured, skipping POST to ${endpoint}`);
|
|
44
|
-
return void 0;
|
|
45
|
-
}
|
|
46
|
-
const url = `${this.baseUrl.replace(/\/$/, "")}${endpoint}`;
|
|
47
|
-
const headers = {
|
|
48
|
-
"Content-Type": "application/json"
|
|
49
|
-
};
|
|
50
|
-
if (this.apiKey) {
|
|
51
|
-
headers["Authorization"] = `Bearer ${this.apiKey}`;
|
|
52
|
-
}
|
|
53
|
-
const controller = new AbortController();
|
|
54
|
-
const timeoutId = setTimeout(() => controller.abort(), this.timeout);
|
|
55
|
-
try {
|
|
56
|
-
const response = await fetch(url, {
|
|
57
|
-
method: "POST",
|
|
58
|
-
headers,
|
|
59
|
-
body: JSON.stringify(payload),
|
|
60
|
-
signal: controller.signal
|
|
61
|
-
});
|
|
62
|
-
clearTimeout(timeoutId);
|
|
63
|
-
if (!response.ok) {
|
|
64
|
-
console.warn(
|
|
65
|
-
`[Polymorph] HTTP error ${response.status} while posting to ${url}`
|
|
66
|
-
);
|
|
67
|
-
return void 0;
|
|
68
|
-
}
|
|
69
|
-
return await response.json();
|
|
70
|
-
} catch (error) {
|
|
71
|
-
clearTimeout(timeoutId);
|
|
72
|
-
if (error instanceof Error) {
|
|
73
|
-
if (error.name === "AbortError") {
|
|
74
|
-
console.warn(`[Polymorph] Timeout while posting to ${url}`);
|
|
75
|
-
} else {
|
|
76
|
-
console.warn(`[Polymorph] Request error while posting to ${url}:`, error.message);
|
|
77
|
-
}
|
|
78
|
-
} else {
|
|
79
|
-
console.warn(`[Polymorph] Unexpected error while posting to ${url}:`, error);
|
|
80
|
-
}
|
|
81
|
-
return void 0;
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
/**
|
|
85
|
-
* Fire-and-forget POST request. Does not wait for response.
|
|
86
|
-
*/
|
|
87
|
-
postFireAndForget(endpoint, payload) {
|
|
88
|
-
this.post(endpoint, payload);
|
|
89
|
-
}
|
|
90
|
-
};
|
|
91
|
-
|
|
92
|
-
// src/logging.ts
|
|
93
|
-
function log(entry) {
|
|
94
|
-
console.log("[Polymorph]", entry);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
// src/providers/vercel-ai.ts
|
|
98
|
-
function createHooks(config, httpClient) {
|
|
99
|
-
async function preToolHook(context) {
|
|
100
|
-
const payload = {
|
|
101
|
-
event: "tool_call_start",
|
|
102
|
-
tool: context.toolName,
|
|
103
|
-
input: context.input,
|
|
104
|
-
tool_use_id: context.toolCallId,
|
|
105
|
-
role: config.role
|
|
106
|
-
};
|
|
107
|
-
log(payload);
|
|
108
|
-
httpClient.postFireAndForget("/sdk/tool/pre", payload);
|
|
109
|
-
return {};
|
|
110
|
-
}
|
|
111
|
-
async function postToolHook(context) {
|
|
112
|
-
const payload = {
|
|
113
|
-
event: "tool_call_end",
|
|
114
|
-
tool: context.toolName,
|
|
115
|
-
tool_use_id: context.toolCallId,
|
|
116
|
-
duration_ms: context.durationMs,
|
|
117
|
-
success: !context.error,
|
|
118
|
-
error: context.error?.message,
|
|
119
|
-
role: config.role
|
|
120
|
-
};
|
|
121
|
-
log(payload);
|
|
122
|
-
httpClient.postFireAndForget("/sdk/tool/post", payload);
|
|
123
|
-
}
|
|
124
|
-
return { preToolHook, postToolHook };
|
|
125
|
-
}
|
|
126
|
-
function wrapTool(toolName, tool, preToolHook, postToolHook) {
|
|
127
|
-
if (!tool.execute) {
|
|
128
|
-
return tool;
|
|
129
|
-
}
|
|
130
|
-
const originalExecute = tool.execute;
|
|
131
|
-
const wrappedExecute = async (input, options) => {
|
|
132
|
-
const toolCallId = options?.toolCallId ?? `call_${Date.now()}`;
|
|
133
|
-
const context = {
|
|
134
|
-
toolName,
|
|
135
|
-
toolCallId,
|
|
136
|
-
input
|
|
137
|
-
};
|
|
138
|
-
const preResult = await preToolHook(context);
|
|
139
|
-
if (preResult.deny) {
|
|
140
|
-
throw new Error(
|
|
141
|
-
`Tool '${toolName}' denied: ${preResult.reason ?? "Permission denied"}`
|
|
142
|
-
);
|
|
143
|
-
}
|
|
144
|
-
const startTime = Date.now();
|
|
145
|
-
let result;
|
|
146
|
-
let error;
|
|
147
|
-
try {
|
|
148
|
-
result = await originalExecute(input, options);
|
|
149
|
-
} catch (e) {
|
|
150
|
-
error = e instanceof Error ? e : new Error(String(e));
|
|
151
|
-
throw error;
|
|
152
|
-
} finally {
|
|
153
|
-
const durationMs = Date.now() - startTime;
|
|
154
|
-
await postToolHook({
|
|
155
|
-
...context,
|
|
156
|
-
result,
|
|
157
|
-
error,
|
|
158
|
-
durationMs
|
|
159
|
-
});
|
|
160
|
-
}
|
|
161
|
-
return result;
|
|
162
|
-
};
|
|
163
|
-
return {
|
|
164
|
-
...tool,
|
|
165
|
-
execute: wrappedExecute
|
|
166
|
-
};
|
|
167
|
-
}
|
|
168
|
-
function wrapTools(tools, preToolHook, postToolHook) {
|
|
169
|
-
const wrappedTools = {};
|
|
170
|
-
for (const [name, tool] of Object.entries(tools)) {
|
|
171
|
-
wrappedTools[name] = wrapTool(name, tool, preToolHook, postToolHook);
|
|
172
|
-
}
|
|
173
|
-
return wrappedTools;
|
|
174
|
-
}
|
|
175
|
-
var PolymorphAIClient = class {
|
|
176
|
-
constructor(options) {
|
|
177
|
-
this.config = options.config;
|
|
178
|
-
this.httpClient = new PolymorphHTTPClient({
|
|
179
|
-
baseUrl: this.config.baseUrl,
|
|
180
|
-
apiKey: this.config.apiKey,
|
|
181
|
-
timeout: this.config.httpTimeout,
|
|
182
|
-
enabled: this.config.httpEnabled
|
|
183
|
-
});
|
|
184
|
-
const hooks = createHooks(this.config, this.httpClient);
|
|
185
|
-
this.preToolHook = hooks.preToolHook;
|
|
186
|
-
this.postToolHook = hooks.postToolHook;
|
|
187
|
-
this.httpClient.postFireAndForget("/sdk/init", {
|
|
188
|
-
role: this.config.role,
|
|
189
|
-
sdk: "vercel-ai",
|
|
190
|
-
version: "0.1.0"
|
|
191
|
-
});
|
|
192
|
-
}
|
|
193
|
-
/**
|
|
194
|
-
* Wraps tools with Polymorph logging hooks.
|
|
195
|
-
*/
|
|
196
|
-
wrapTools(tools) {
|
|
197
|
-
return wrapTools(tools, this.preToolHook, this.postToolHook);
|
|
198
|
-
}
|
|
199
|
-
/**
|
|
200
|
-
* Creates a wrapped streamText function that automatically wraps tools.
|
|
201
|
-
*/
|
|
202
|
-
streamText(originalStreamText) {
|
|
203
|
-
return (params) => {
|
|
204
|
-
const wrappedParams = {
|
|
205
|
-
...params,
|
|
206
|
-
tools: params.tools ? this.wrapTools(params.tools) : void 0
|
|
207
|
-
};
|
|
208
|
-
return originalStreamText(wrappedParams);
|
|
209
|
-
};
|
|
210
|
-
}
|
|
211
|
-
/**
|
|
212
|
-
* Creates a wrapped generateText function that automatically wraps tools.
|
|
213
|
-
*/
|
|
214
|
-
generateText(originalGenerateText) {
|
|
215
|
-
return (params) => {
|
|
216
|
-
const wrappedParams = {
|
|
217
|
-
...params,
|
|
218
|
-
tools: params.tools ? this.wrapTools(params.tools) : void 0
|
|
219
|
-
};
|
|
220
|
-
return originalGenerateText(wrappedParams);
|
|
221
|
-
};
|
|
222
|
-
}
|
|
223
|
-
};
|
|
224
|
-
function createPolymorphAIClient(options) {
|
|
225
|
-
return new PolymorphAIClient(options);
|
|
226
|
-
}
|
|
227
|
-
export {
|
|
228
|
-
PolymorphAIClient,
|
|
229
|
-
PolymorphConfig,
|
|
230
|
-
PolymorphHTTPClient,
|
|
231
|
-
createPolymorphAIClient,
|
|
232
|
-
log
|
|
233
|
-
};
|