@agentgov/sdk 0.1.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/README.md +408 -0
- package/dist/client.d.ts +164 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +399 -0
- package/dist/client.js.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +9 -0
- package/dist/index.js.map +1 -0
- package/dist/types.d.ts +91 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +5 -0
- package/dist/types.js.map +1 -0
- package/dist/utils/fetch.d.ts +59 -0
- package/dist/utils/fetch.d.ts.map +1 -0
- package/dist/utils/fetch.js +198 -0
- package/dist/utils/fetch.js.map +1 -0
- package/dist/utils/timing.d.ts +10 -0
- package/dist/utils/timing.d.ts.map +1 -0
- package/dist/utils/timing.js +67 -0
- package/dist/utils/timing.js.map +1 -0
- package/dist/wrappers/openai.d.ts +36 -0
- package/dist/wrappers/openai.d.ts.map +1 -0
- package/dist/wrappers/openai.js +351 -0
- package/dist/wrappers/openai.js.map +1 -0
- package/dist/wrappers/vercel-ai.d.ts +101 -0
- package/dist/wrappers/vercel-ai.d.ts.map +1 -0
- package/dist/wrappers/vercel-ai.js +461 -0
- package/dist/wrappers/vercel-ai.js.map +1 -0
- package/package.json +75 -0
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
/** Error thrown when API request fails */
|
|
2
|
+
export class AgentGovAPIError extends Error {
|
|
3
|
+
statusCode;
|
|
4
|
+
retryable;
|
|
5
|
+
constructor(message, statusCode, retryable) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.statusCode = statusCode;
|
|
8
|
+
this.retryable = retryable;
|
|
9
|
+
this.name = 'AgentGovAPIError';
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
/** Safely serialize data, handling circular references */
|
|
13
|
+
export function safeStringify(data) {
|
|
14
|
+
try {
|
|
15
|
+
return JSON.stringify(data);
|
|
16
|
+
}
|
|
17
|
+
catch {
|
|
18
|
+
return JSON.stringify({ error: 'Failed to serialize data' });
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
export class FetchClient {
|
|
22
|
+
baseUrl;
|
|
23
|
+
headers;
|
|
24
|
+
projectId;
|
|
25
|
+
debug;
|
|
26
|
+
maxRetries;
|
|
27
|
+
retryDelay;
|
|
28
|
+
timeout;
|
|
29
|
+
constructor(config) {
|
|
30
|
+
this.baseUrl = config.baseUrl.replace(/\/$/, '');
|
|
31
|
+
this.projectId = config.projectId;
|
|
32
|
+
this.debug = config.debug ?? false;
|
|
33
|
+
this.maxRetries = config.maxRetries ?? 3;
|
|
34
|
+
this.retryDelay = config.retryDelay ?? 1000;
|
|
35
|
+
this.timeout = config.timeout ?? 30000;
|
|
36
|
+
this.headers = {
|
|
37
|
+
'Content-Type': 'application/json',
|
|
38
|
+
'Authorization': `Bearer ${config.apiKey}`
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Sanitize data for logging - removes sensitive fields
|
|
43
|
+
*/
|
|
44
|
+
sanitize(data) {
|
|
45
|
+
if (typeof data !== 'object' || data === null) {
|
|
46
|
+
return data;
|
|
47
|
+
}
|
|
48
|
+
const sensitiveKeys = ['apiKey', 'password', 'secret', 'token', 'authorization', 'input', 'output'];
|
|
49
|
+
const obj = data;
|
|
50
|
+
const sanitized = {};
|
|
51
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
52
|
+
if (sensitiveKeys.some(k => key.toLowerCase().includes(k))) {
|
|
53
|
+
sanitized[key] = '[REDACTED]';
|
|
54
|
+
}
|
|
55
|
+
else if (typeof value === 'object' && value !== null) {
|
|
56
|
+
sanitized[key] = this.sanitize(value);
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
sanitized[key] = value;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return sanitized;
|
|
63
|
+
}
|
|
64
|
+
log(message, data) {
|
|
65
|
+
if (this.debug) {
|
|
66
|
+
const sanitizedData = data ? this.sanitize(data) : undefined;
|
|
67
|
+
console.log('[AgentGov]', message, sanitizedData ?? '');
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Check if error is retryable based on status code
|
|
72
|
+
*/
|
|
73
|
+
isRetryable(status) {
|
|
74
|
+
// Retry on: 408 (timeout), 429 (rate limit), 5xx (server errors)
|
|
75
|
+
return status === 408 || status === 429 || status >= 500;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Calculate delay with exponential backoff and jitter
|
|
79
|
+
*/
|
|
80
|
+
calculateDelay(attempt, retryAfter) {
|
|
81
|
+
if (retryAfter !== undefined && !isNaN(retryAfter) && retryAfter > 0) {
|
|
82
|
+
return retryAfter * 1000;
|
|
83
|
+
}
|
|
84
|
+
// Exponential backoff with jitter: baseDelay * 2^attempt * (0.5 to 1.5)
|
|
85
|
+
const exponentialDelay = this.retryDelay * Math.pow(2, attempt);
|
|
86
|
+
const jitter = 0.5 + Math.random();
|
|
87
|
+
return Math.min(exponentialDelay * jitter, 30000); // Cap at 30s
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Sleep for specified duration
|
|
91
|
+
*/
|
|
92
|
+
sleep(ms) {
|
|
93
|
+
return new Promise(resolve => setTimeout(resolve, ms));
|
|
94
|
+
}
|
|
95
|
+
async request(method, path, body) {
|
|
96
|
+
const url = `${this.baseUrl}${path}`;
|
|
97
|
+
let lastError = null;
|
|
98
|
+
for (let attempt = 0; attempt <= this.maxRetries; attempt++) {
|
|
99
|
+
try {
|
|
100
|
+
this.log(`${method} ${url}${attempt > 0 ? ` (retry ${attempt})` : ''}`, body);
|
|
101
|
+
// Create abort controller for timeout
|
|
102
|
+
const controller = new AbortController();
|
|
103
|
+
const timeoutId = setTimeout(() => controller.abort(), this.timeout);
|
|
104
|
+
try {
|
|
105
|
+
const response = await fetch(url, {
|
|
106
|
+
method,
|
|
107
|
+
headers: this.headers,
|
|
108
|
+
body: body ? safeStringify(body) : undefined,
|
|
109
|
+
signal: controller.signal
|
|
110
|
+
});
|
|
111
|
+
clearTimeout(timeoutId);
|
|
112
|
+
if (!response.ok) {
|
|
113
|
+
const errorText = await response.text();
|
|
114
|
+
const retryable = this.isRetryable(response.status);
|
|
115
|
+
// If retryable and we have retries left, try again
|
|
116
|
+
if (retryable && attempt < this.maxRetries) {
|
|
117
|
+
const retryAfterRaw = response.headers.get('Retry-After');
|
|
118
|
+
const retryAfterParsed = retryAfterRaw ? parseInt(retryAfterRaw, 10) : NaN;
|
|
119
|
+
const delay = this.calculateDelay(attempt, !isNaN(retryAfterParsed) && retryAfterParsed > 0 ? retryAfterParsed : undefined);
|
|
120
|
+
this.log(`Retryable error (${response.status}), waiting ${delay}ms before retry`);
|
|
121
|
+
await this.sleep(delay);
|
|
122
|
+
lastError = new AgentGovAPIError(`AgentGov API error: ${response.status} ${errorText}`, response.status, true);
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
throw new AgentGovAPIError(`AgentGov API error: ${response.status} ${errorText}`, response.status, retryable);
|
|
126
|
+
}
|
|
127
|
+
// Handle 204 No Content
|
|
128
|
+
if (response.status === 204) {
|
|
129
|
+
return undefined;
|
|
130
|
+
}
|
|
131
|
+
const data = await response.json();
|
|
132
|
+
this.log('Response:', data);
|
|
133
|
+
return data;
|
|
134
|
+
}
|
|
135
|
+
finally {
|
|
136
|
+
clearTimeout(timeoutId);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
catch (error) {
|
|
140
|
+
// Handle abort (timeout)
|
|
141
|
+
if (error instanceof Error && error.name === 'AbortError') {
|
|
142
|
+
lastError = new AgentGovAPIError(`Request timeout after ${this.timeout}ms`, 408, true);
|
|
143
|
+
if (attempt < this.maxRetries) {
|
|
144
|
+
const delay = this.calculateDelay(attempt);
|
|
145
|
+
this.log(`Request timed out, waiting ${delay}ms before retry`);
|
|
146
|
+
await this.sleep(delay);
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
// Handle network errors
|
|
151
|
+
if (error instanceof TypeError && error.message.includes('fetch')) {
|
|
152
|
+
lastError = new AgentGovAPIError(`Network error: ${error.message}`, 0, true);
|
|
153
|
+
if (attempt < this.maxRetries) {
|
|
154
|
+
const delay = this.calculateDelay(attempt);
|
|
155
|
+
this.log(`Network error, waiting ${delay}ms before retry`);
|
|
156
|
+
await this.sleep(delay);
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
// Re-throw AgentGovAPIError or wrap other errors
|
|
161
|
+
if (error instanceof AgentGovAPIError) {
|
|
162
|
+
throw error;
|
|
163
|
+
}
|
|
164
|
+
throw error;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
// Should not reach here, but throw last error if we do
|
|
168
|
+
throw lastError || new Error('Unexpected error in request');
|
|
169
|
+
}
|
|
170
|
+
// ============================================
|
|
171
|
+
// Traces
|
|
172
|
+
// ============================================
|
|
173
|
+
async createTrace(input) {
|
|
174
|
+
return this.request('POST', '/v1/traces', {
|
|
175
|
+
...input,
|
|
176
|
+
projectId: this.projectId
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
async updateTrace(traceId, update) {
|
|
180
|
+
return this.request('PATCH', `/v1/traces/${traceId}`, update);
|
|
181
|
+
}
|
|
182
|
+
async getTrace(traceId) {
|
|
183
|
+
return this.request('GET', `/v1/traces/${traceId}`);
|
|
184
|
+
}
|
|
185
|
+
// ============================================
|
|
186
|
+
// Spans
|
|
187
|
+
// ============================================
|
|
188
|
+
async createSpan(input) {
|
|
189
|
+
return this.request('POST', '/v1/spans', input);
|
|
190
|
+
}
|
|
191
|
+
async updateSpan(spanId, update) {
|
|
192
|
+
return this.request('PATCH', `/v1/spans/${spanId}`, update);
|
|
193
|
+
}
|
|
194
|
+
async getSpan(spanId) {
|
|
195
|
+
return this.request('GET', `/v1/spans/${spanId}`);
|
|
196
|
+
}
|
|
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E,cAAc,OAAO,EAAE,EAAE,MAAM,CAAC,CAAA;IACtE,CAAC;IAED,KAAK,CAAC,QAAQ,CAAC,OAAe;QAC5B,OAAO,IAAI,CAAC,OAAO,CAAQ,KAAK,EAAE,cAAc,OAAO,EAAE,CAAC,CAAA;IAC5D,CAAC;IAED,+CAA+C;IAC/C,QAAQ;IACR,+CAA+C;IAE/C,KAAK,CAAC,UAAU,CAAC,KAAgB;QAC/B,OAAO,IAAI,CAAC,OAAO,CAAO,MAAM,EAAE,WAAW,EAAE,KAAK,CAAC,CAAA;IACvD,CAAC;IAED,KAAK,CAAC,UAAU,CAAC,MAAc,EAAE,MAAkB;QACjD,OAAO,IAAI,CAAC,OAAO,CAAO,OAAO,EAAE,aAAa,MAAM,EAAE,EAAE,MAAM,CAAC,CAAA;IACnE,CAAC;IAED,KAAK,CAAC,OAAO,CAAC,MAAc;QAC1B,OAAO,IAAI,CAAC,OAAO,CAAO,KAAK,EAAE,aAAa,MAAM,EAAE,CAAC,CAAA;IACzD,CAAC;CACF"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Calculate duration in milliseconds
|
|
3
|
+
*/
|
|
4
|
+
export declare function calculateDuration(startTime: number): number;
|
|
5
|
+
/**
|
|
6
|
+
* Estimate cost based on model and tokens
|
|
7
|
+
* Prices as of January 2026 (approximate)
|
|
8
|
+
*/
|
|
9
|
+
export declare function estimateCost(model: string, inputTokens: number, outputTokens: number): number;
|
|
10
|
+
//# sourceMappingURL=timing.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"timing.d.ts","sourceRoot":"","sources":["../../src/utils/timing.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,wBAAgB,iBAAiB,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,CAE3D;AAUD;;;GAGG;AACH,wBAAgB,YAAY,CAC1B,KAAK,EAAE,MAAM,EACb,WAAW,EAAE,MAAM,EACnB,YAAY,EAAE,MAAM,GACnB,MAAM,CAiER"}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Calculate duration in milliseconds
|
|
3
|
+
*/
|
|
4
|
+
export function calculateDuration(startTime) {
|
|
5
|
+
return Date.now() - startTime;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Estimate cost based on model and tokens
|
|
9
|
+
* Prices as of January 2026 (approximate)
|
|
10
|
+
*/
|
|
11
|
+
export function estimateCost(model, inputTokens, outputTokens) {
|
|
12
|
+
// Pricing per 1M tokens - Updated January 2026
|
|
13
|
+
const pricing = {
|
|
14
|
+
// OpenAI - GPT-5 Series (Flagship)
|
|
15
|
+
'gpt-5.2-pro': { inputPrice: 21.00, outputPrice: 168.00 },
|
|
16
|
+
'gpt-5.2': { inputPrice: 1.75, outputPrice: 14.00 },
|
|
17
|
+
'gpt-5': { inputPrice: 1.25, outputPrice: 10.00 },
|
|
18
|
+
// OpenAI - GPT-4 Series
|
|
19
|
+
'gpt-4.1': { inputPrice: 2.00, outputPrice: 8.00 },
|
|
20
|
+
'gpt-4.1-mini': { inputPrice: 0.40, outputPrice: 1.60 },
|
|
21
|
+
'gpt-4o': { inputPrice: 2.50, outputPrice: 10.00 },
|
|
22
|
+
'gpt-4o-mini': { inputPrice: 0.15, outputPrice: 0.60 },
|
|
23
|
+
// OpenAI - o-Series (Reasoning)
|
|
24
|
+
'o4-mini': { inputPrice: 1.10, outputPrice: 4.40 },
|
|
25
|
+
'o3-pro': { inputPrice: 20.00, outputPrice: 80.00 },
|
|
26
|
+
'o3': { inputPrice: 10.00, outputPrice: 40.00 },
|
|
27
|
+
'o3-mini': { inputPrice: 1.10, outputPrice: 4.40 },
|
|
28
|
+
'o1': { inputPrice: 15.00, outputPrice: 60.00 },
|
|
29
|
+
'o1-mini': { inputPrice: 3.00, outputPrice: 12.00 },
|
|
30
|
+
// OpenAI - Legacy
|
|
31
|
+
'gpt-4-turbo': { inputPrice: 10.00, outputPrice: 30.00 },
|
|
32
|
+
'gpt-4': { inputPrice: 30.00, outputPrice: 60.00 },
|
|
33
|
+
'gpt-3.5-turbo': { inputPrice: 0.50, outputPrice: 1.50 },
|
|
34
|
+
// Anthropic
|
|
35
|
+
'claude-sonnet-4': { inputPrice: 3.00, outputPrice: 15.00 },
|
|
36
|
+
'claude-3.5-sonnet': { inputPrice: 3.00, outputPrice: 15.00 },
|
|
37
|
+
'claude-3.5-haiku': { inputPrice: 0.80, outputPrice: 4.00 },
|
|
38
|
+
'claude-3-opus': { inputPrice: 15.00, outputPrice: 75.00 },
|
|
39
|
+
'claude-3-sonnet': { inputPrice: 3.00, outputPrice: 15.00 },
|
|
40
|
+
'claude-3-haiku': { inputPrice: 0.25, outputPrice: 1.25 },
|
|
41
|
+
// Embeddings (no output tokens)
|
|
42
|
+
'text-embedding-3-small': { inputPrice: 0.02, outputPrice: 0 },
|
|
43
|
+
'text-embedding-3-large': { inputPrice: 0.13, outputPrice: 0 },
|
|
44
|
+
'text-embedding-ada-002': { inputPrice: 0.10, outputPrice: 0 },
|
|
45
|
+
// Default fallback
|
|
46
|
+
'default': { inputPrice: 1.00, outputPrice: 2.00 }
|
|
47
|
+
};
|
|
48
|
+
// Find matching pricing (partial match)
|
|
49
|
+
// Sort keys by length descending so more specific matches come first
|
|
50
|
+
// e.g., "gpt-4o-mini" should match before "gpt-4o"
|
|
51
|
+
let { inputPrice, outputPrice } = pricing['default'];
|
|
52
|
+
const sortedKeys = Object.keys(pricing)
|
|
53
|
+
.filter(k => k !== 'default')
|
|
54
|
+
.sort((a, b) => b.length - a.length);
|
|
55
|
+
for (const key of sortedKeys) {
|
|
56
|
+
if (model.toLowerCase().includes(key.toLowerCase())) {
|
|
57
|
+
;
|
|
58
|
+
({ inputPrice, outputPrice } = pricing[key]);
|
|
59
|
+
break;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
// Calculate cost (prices are per 1M tokens)
|
|
63
|
+
const inputCost = (inputTokens / 1_000_000) * inputPrice;
|
|
64
|
+
const outputCost = (outputTokens / 1_000_000) * outputPrice;
|
|
65
|
+
return Number((inputCost + outputCost).toFixed(6));
|
|
66
|
+
}
|
|
67
|
+
//# sourceMappingURL=timing.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"timing.js","sourceRoot":"","sources":["../../src/utils/timing.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,UAAU,iBAAiB,CAAC,SAAiB;IACjD,OAAO,IAAI,CAAC,GAAG,EAAE,GAAG,SAAS,CAAA;AAC/B,CAAC;AAUD;;;GAGG;AACH,MAAM,UAAU,YAAY,CAC1B,KAAa,EACb,WAAmB,EACnB,YAAoB;IAEpB,+CAA+C;IAC/C,MAAM,OAAO,GAAiC;QAC5C,mCAAmC;QACnC,aAAa,EAAE,EAAE,UAAU,EAAE,KAAK,EAAE,WAAW,EAAE,MAAM,EAAE;QACzD,SAAS,EAAE,EAAE,UAAU,EAAE,IAAI,EAAE,WAAW,EAAE,KAAK,EAAE;QACnD,OAAO,EAAE,EAAE,UAAU,EAAE,IAAI,EAAE,WAAW,EAAE,KAAK,EAAE;QAEjD,wBAAwB;QACxB,SAAS,EAAE,EAAE,UAAU,EAAE,IAAI,EAAE,WAAW,EAAE,IAAI,EAAE;QAClD,cAAc,EAAE,EAAE,UAAU,EAAE,IAAI,EAAE,WAAW,EAAE,IAAI,EAAE;QACvD,QAAQ,EAAE,EAAE,UAAU,EAAE,IAAI,EAAE,WAAW,EAAE,KAAK,EAAE;QAClD,aAAa,EAAE,EAAE,UAAU,EAAE,IAAI,EAAE,WAAW,EAAE,IAAI,EAAE;QAEtD,gCAAgC;QAChC,SAAS,EAAE,EAAE,UAAU,EAAE,IAAI,EAAE,WAAW,EAAE,IAAI,EAAE;QAClD,QAAQ,EAAE,EAAE,UAAU,EAAE,KAAK,EAAE,WAAW,EAAE,KAAK,EAAE;QACnD,IAAI,EAAE,EAAE,UAAU,EAAE,KAAK,EAAE,WAAW,EAAE,KAAK,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EAAE,IAAI,EAAE;KACnD,CAAA;IAED,wCAAwC;IACxC,qEAAqE;IACrE,mDAAmD;IACnD,IAAI,EAAE,UAAU,EAAE,WAAW,EAAE,GAAG,OAAO,CAAC,SAAS,CAAC,CAAA;IAEpD,MAAM,UAAU,GAAG,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC;SACpC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,KAAK,SAAS,CAAC;SAC5B,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC,MAAM,CAAC,CAAA;IAEtC,KAAK,MAAM,GAAG,IAAI,UAAU,EAAE,CAAC;QAC7B,IAAI,KAAK,CAAC,WAAW,EAAE,CAAC,QAAQ,CAAC,GAAG,CAAC,WAAW,EAAE,CAAC,EAAE,CAAC;YACpD,CAAC;YAAA,CAAC,EAAE,UAAU,EAAE,WAAW,EAAE,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC,CAAA;YAC7C,MAAK;QACP,CAAC;IACH,CAAC;IAED,4CAA4C;IAC5C,MAAM,SAAS,GAAG,CAAC,WAAW,GAAG,SAAS,CAAC,GAAG,UAAU,CAAA;IACxD,MAAM,UAAU,GAAG,CAAC,YAAY,GAAG,SAAS,CAAC,GAAG,WAAW,CAAA;IAE3D,OAAO,MAAM,CAAC,CAAC,SAAS,GAAG,UAAU,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAA;AACpD,CAAC"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { ChatCompletion, ChatCompletionChunk, ChatCompletionCreateParamsBase } from 'openai/resources/chat/completions';
|
|
2
|
+
import type { EmbeddingCreateParams, CreateEmbeddingResponse } from 'openai/resources/embeddings';
|
|
3
|
+
import type { FetchClient } from '../utils/fetch.js';
|
|
4
|
+
import type { TraceContext } from '../types.js';
|
|
5
|
+
export type { ChatCompletion, ChatCompletionChunk };
|
|
6
|
+
export type { EmbeddingCreateParams, CreateEmbeddingResponse };
|
|
7
|
+
export interface OpenAIClient {
|
|
8
|
+
chat: {
|
|
9
|
+
completions: {
|
|
10
|
+
create(params: ChatCompletionCreateParamsBase): Promise<ChatCompletion | AsyncIterable<ChatCompletionChunk>>;
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
embeddings?: {
|
|
14
|
+
create(params: EmbeddingCreateParams): Promise<CreateEmbeddingResponse>;
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
export type WrappedStream<T> = AsyncIterable<T>;
|
|
18
|
+
export interface WrapOpenAIOptions {
|
|
19
|
+
/** Custom trace name prefix */
|
|
20
|
+
traceNamePrefix?: string;
|
|
21
|
+
/** Auto-create trace for each call (default: true) */
|
|
22
|
+
autoTrace?: boolean;
|
|
23
|
+
/** Include full prompts in trace (default: true) */
|
|
24
|
+
captureInput?: boolean;
|
|
25
|
+
/** Include full responses in trace (default: true) */
|
|
26
|
+
captureOutput?: boolean;
|
|
27
|
+
/** Create separate spans for tool calls (default: true) */
|
|
28
|
+
traceToolCalls?: boolean;
|
|
29
|
+
/** Enable debug logging for span errors (default: false) */
|
|
30
|
+
debug?: boolean;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Wrap OpenAI client to automatically trace all calls
|
|
34
|
+
*/
|
|
35
|
+
export declare function wrapOpenAI<T extends OpenAIClient>(client: T, fetchClient: FetchClient, getContext: () => TraceContext | null, options?: WrapOpenAIOptions): T;
|
|
36
|
+
//# sourceMappingURL=openai.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"openai.d.ts","sourceRoot":"","sources":["../../src/wrappers/openai.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,cAAc,EACd,mBAAmB,EACnB,8BAA8B,EAE/B,MAAM,mCAAmC,CAAA;AAC1C,OAAO,KAAK,EACV,qBAAqB,EACrB,uBAAuB,EACxB,MAAM,6BAA6B,CAAA;AACpC,OAA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