-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathutils.ts
More file actions
99 lines (91 loc) · 2.86 KB
/
Copy pathutils.ts
File metadata and controls
99 lines (91 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/** Base URL for all Context.dev API endpoints. */
export const CONTEXT_DEV_BASE_URL = 'https://api.context.dev/v1'
/**
* Builds the standard Context.dev request headers with Bearer authentication.
*/
export function contextDevHeaders(apiKey: string): Record<string, string> {
return {
Authorization: `Bearer ${apiKey}`,
Accept: 'application/json',
}
}
/**
* Builds JSON request headers with Bearer authentication for POST endpoints.
*/
export function contextDevJsonHeaders(apiKey: string): Record<string, string> {
return {
...contextDevHeaders(apiKey),
'Content-Type': 'application/json',
}
}
/**
* Throws a descriptive error when a Context.dev response is not successful.
* Returns the parsed JSON body on success.
*/
export async function parseContextDevResponse(response: Response): Promise<any> {
if (!response.ok) {
const errorText = await response.text()
throw new Error(`Context.dev API error (${response.status}): ${errorText}`)
}
return response.json()
}
/** Shape of the credit accounting object present on every Context.dev response. */
interface ContextDevKeyMetadata {
credits_consumed?: number
credits_remaining?: number
}
/**
* Extracts the credit accounting fields shared by every Context.dev response.
*/
export function extractCreditMetadata(keyMetadata: ContextDevKeyMetadata | undefined): {
creditsConsumed: number | null
creditsRemaining: number | null
} {
return {
creditsConsumed: keyMetadata?.credits_consumed ?? null,
creditsRemaining: keyMetadata?.credits_remaining ?? null,
}
}
/**
* Normalizes a brand-returning Context.dev response into the shared tool output shape.
* Used by every endpoint that returns a `brand` object.
*/
export function transformBrandResponse(data: any): {
status: string
brand: Record<string, unknown> | null
creditsConsumed: number | null
creditsRemaining: number | null
} {
return {
status: data.status ?? '',
brand: data.brand ?? null,
...extractCreditMetadata(data.key_metadata),
}
}
/**
* Appends a parameter to a URLSearchParams instance only when it is defined and non-empty.
* Booleans are serialized as the literal strings 'true' / 'false'.
*/
export function appendParam(
search: URLSearchParams,
key: string,
value: string | number | boolean | undefined | null
): void {
if (value === undefined || value === null || value === '') return
const serialized = typeof value === 'string' ? value.trim() : String(value)
if (serialized === '') return
search.append(key, serialized)
}
/** Output definitions for the credit accounting fields, reused across every tool. */
export const CREDIT_OUTPUTS = {
creditsConsumed: {
type: 'number',
description: 'Credits consumed by this request',
optional: true,
},
creditsRemaining: {
type: 'number',
description: 'Credits remaining on the API key',
optional: true,
},
} as const