polymorph-sdk 0.1.0 → 0.2.2
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 +58 -84
- package/dist/index.css +2 -0
- package/dist/index.d.ts +64 -135
- package/dist/index.js +18187 -259
- package/package.json +38 -34
- package/src/ChatThread.tsx +70 -0
- package/src/PolymorphWidget.tsx +103 -0
- package/src/RoomHandler.tsx +99 -0
- package/src/VoiceOverlay.tsx +85 -0
- package/src/WidgetPanel.tsx +228 -0
- package/src/__tests__/exports.test.ts +13 -0
- package/src/env.d.ts +6 -0
- package/src/index.ts +8 -0
- package/src/styles.module.css +304 -0
- package/src/theme.ts +53 -0
- package/src/types.ts +36 -0
- package/src/usePolymorphSession.ts +203 -0
- package/dist/index.d.mts +0 -135
- package/dist/index.mjs +0 -233
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
|
-
};
|