-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathindex.ts
More file actions
255 lines (225 loc) · 5.48 KB
/
index.ts
File metadata and controls
255 lines (225 loc) · 5.48 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import { Result } from 'better-result';
import { hc } from 'hono/client';
import type { AppType } from 'btca-server';
export type Client = ReturnType<typeof hc<AppType>>;
/**
* Custom error class that carries hints from the server.
*/
export class BtcaError extends Error {
readonly hint?: string;
readonly tag?: string;
constructor(message: string, options?: { hint?: string; tag?: string }) {
super(message);
this.name = 'BtcaError';
this.hint = options?.hint;
this.tag = options?.tag;
}
}
/**
* Parse error response from server and create a BtcaError.
*/
async function parseErrorResponse(
res: { json: () => Promise<unknown> },
fallbackMessage: string
): Promise<BtcaError> {
const result = await Result.tryPromise(() => res.json());
return result.match({
ok: (body) => {
const parsed = body as { error?: string; hint?: string; tag?: string };
return new BtcaError(parsed.error ?? fallbackMessage, {
hint: parsed.hint,
tag: parsed.tag
});
},
err: () => new BtcaError(fallbackMessage)
});
}
/**
* Create a typed Hono RPC client for the btca server
*/
export function createClient(baseUrl: string): Client {
return hc<AppType>(baseUrl);
}
/**
* Get server configuration
*/
export async function getConfig(client: Client) {
const res = await client.config.$get();
if (!res.ok) {
throw await parseErrorResponse(res, `Failed to get config: ${res.status}`);
}
return res.json();
}
/**
* Get available resources
*/
export async function getResources(client: Client) {
const res = await client.resources.$get();
if (!res.ok) {
throw await parseErrorResponse(res, `Failed to get resources: ${res.status}`);
}
return res.json();
}
export async function getProviders(client: Client) {
const res = await client.providers.$get();
if (!res.ok) {
throw await parseErrorResponse(res, `Failed to get providers: ${res.status}`);
}
return res.json();
}
/**
* Ask a question (non-streaming)
*/
export async function askQuestion(
client: Client,
options: {
question: string;
resources?: string[];
quiet?: boolean;
}
) {
const res = await client.question.$post({
json: {
question: options.question,
resources: options.resources,
quiet: options.quiet
}
});
if (!res.ok) {
throw await parseErrorResponse(res, `Failed to ask question: ${res.status}`);
}
return res.json();
}
/**
* Ask a question (streaming) - returns the raw Response for SSE parsing
*/
export async function askQuestionStream(
baseUrl: string,
options: {
question: string;
resources?: string[];
quiet?: boolean;
signal?: AbortSignal;
}
): Promise<Response> {
// Use raw fetch for streaming since Hono client doesn't handle SSE well
const res = await fetch(`${baseUrl}/question/stream`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
question: options.question,
resources: options.resources,
quiet: options.quiet
}),
signal: options.signal
});
if (!res.ok) {
throw await parseErrorResponse(res, `Failed to ask question: ${res.status}`);
}
return res;
}
/**
* Update model configuration
*/
export type ProviderOptionsInput = {
baseURL?: string;
name?: string;
};
export async function updateModel(
baseUrl: string,
provider: string,
model: string,
providerOptions?: ProviderOptionsInput
): Promise<{ provider: string; model: string }> {
const res = await fetch(`${baseUrl}/config/model`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
provider,
model,
...(providerOptions ? { providerOptions } : {})
})
});
if (!res.ok) {
throw await parseErrorResponse(res, `Failed to update model: ${res.status}`);
}
return res.json() as Promise<{ provider: string; model: string }>;
}
export interface GitResourceInput {
type: 'git';
name: string;
url: string;
branch?: string;
searchPath?: string;
searchPaths?: string[];
specialNotes?: string;
}
export interface LocalResourceInput {
type: 'local';
name: string;
path: string;
specialNotes?: string;
}
export interface WebsiteResourceInput {
type: 'website';
name: string;
url: string;
maxPages?: number;
maxDepth?: number;
ttlHours?: number;
specialNotes?: string;
}
export type ResourceInput = GitResourceInput | LocalResourceInput | WebsiteResourceInput;
/**
* Add a new resource
*/
export async function addResource(
baseUrl: string,
resource: ResourceInput
): Promise<ResourceInput> {
const res = await fetch(`${baseUrl}/config/resources`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(resource)
});
if (!res.ok) {
throw await parseErrorResponse(res, `Failed to add resource: ${res.status}`);
}
return res.json() as Promise<ResourceInput>;
}
/**
* Remove a resource
*/
export async function removeResource(baseUrl: string, name: string): Promise<void> {
const res = await fetch(`${baseUrl}/config/resources`, {
method: 'DELETE',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ name })
});
if (!res.ok) {
throw await parseErrorResponse(res, `Failed to remove resource: ${res.status}`);
}
}
/**
* Clear all locally cloned resources
*/
export async function clearResources(baseUrl: string): Promise<{ cleared: number }> {
const res = await fetch(`${baseUrl}/clear`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
});
if (!res.ok) {
throw await parseErrorResponse(res, `Failed to clear resources: ${res.status}`);
}
return res.json() as Promise<{ cleared: number }>;
}