@qozara/ai-client 0.0.0-stage → 1.0.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/dist/index.d.ts +108 -0
- package/dist/index.js +386 -0
- package/package.json +36 -4
- package/README.md +0 -3
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
interface AgentChatMessage {
|
|
2
|
+
role: string;
|
|
3
|
+
content: string;
|
|
4
|
+
}
|
|
5
|
+
interface AgentChatTool {
|
|
6
|
+
name: string;
|
|
7
|
+
description: string;
|
|
8
|
+
parameters: any;
|
|
9
|
+
}
|
|
10
|
+
interface AgentChatRequest {
|
|
11
|
+
messages: AgentChatMessage[];
|
|
12
|
+
systemPrompt?: string;
|
|
13
|
+
tools?: AgentChatTool[] | any[];
|
|
14
|
+
}
|
|
15
|
+
interface AgentChatResponse {
|
|
16
|
+
type: 'text' | 'tool_call';
|
|
17
|
+
content?: string;
|
|
18
|
+
tool?: string;
|
|
19
|
+
arguments?: any;
|
|
20
|
+
error?: string;
|
|
21
|
+
}
|
|
22
|
+
interface AiProvider {
|
|
23
|
+
readonly id: string;
|
|
24
|
+
readonly mode: 'byok' | 'cloud' | 'local-browser' | 'local-ollama' | 'disabled';
|
|
25
|
+
chat(request: AgentChatRequest): Promise<AgentChatResponse>;
|
|
26
|
+
checkAvailability(): Promise<boolean>;
|
|
27
|
+
getSetupMessage(): string | null;
|
|
28
|
+
}
|
|
29
|
+
interface AiFactoryConfig {
|
|
30
|
+
providerPreference: 'auto' | 'byok' | 'local' | 'local-browser' | 'cloud' | 'disabled';
|
|
31
|
+
encryptedApiKey?: string;
|
|
32
|
+
baseUrl?: string;
|
|
33
|
+
proxyUrl?: string;
|
|
34
|
+
ollamaModel?: string;
|
|
35
|
+
byokProvider?: string;
|
|
36
|
+
enableLocalAi?: boolean;
|
|
37
|
+
}
|
|
38
|
+
type AuthTokenGetter = () => string | null | undefined | Promise<string | null | undefined>;
|
|
39
|
+
|
|
40
|
+
declare class ProxyAiProvider implements AiProvider {
|
|
41
|
+
private getAuthToken?;
|
|
42
|
+
private encryptedApiKey?;
|
|
43
|
+
private byokProvider?;
|
|
44
|
+
readonly id = "cloud-proxy";
|
|
45
|
+
private base;
|
|
46
|
+
get mode(): 'byok' | 'cloud';
|
|
47
|
+
constructor(proxyUrl?: string, getAuthToken?: AuthTokenGetter | undefined, encryptedApiKey?: string | undefined, byokProvider?: string | undefined);
|
|
48
|
+
chat(request: AgentChatRequest): Promise<AgentChatResponse>;
|
|
49
|
+
checkAvailability(): Promise<boolean>;
|
|
50
|
+
getSetupMessage(): string | null;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
declare class WindowAiProvider implements AiProvider {
|
|
54
|
+
readonly id = "window-ai";
|
|
55
|
+
readonly mode = "local-browser";
|
|
56
|
+
chat(request: AgentChatRequest): Promise<AgentChatResponse>;
|
|
57
|
+
checkAvailability(): Promise<boolean>;
|
|
58
|
+
getSetupMessage(): string | null;
|
|
59
|
+
private isSupported;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
declare class LocalOllamaProvider implements AiProvider {
|
|
63
|
+
readonly id = "local-ollama";
|
|
64
|
+
readonly mode = "local-ollama";
|
|
65
|
+
private model;
|
|
66
|
+
private base;
|
|
67
|
+
constructor(baseUrl?: string, model?: string);
|
|
68
|
+
chat(request: AgentChatRequest): Promise<AgentChatResponse>;
|
|
69
|
+
checkAvailability(): Promise<boolean>;
|
|
70
|
+
getSetupMessage(): string | null;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
declare class DisabledAiProvider implements AiProvider {
|
|
74
|
+
readonly id = "disabled";
|
|
75
|
+
readonly mode = "disabled";
|
|
76
|
+
chat(): Promise<{
|
|
77
|
+
type: "text";
|
|
78
|
+
error: string;
|
|
79
|
+
}>;
|
|
80
|
+
checkAvailability(): Promise<boolean>;
|
|
81
|
+
getSetupMessage(): null;
|
|
82
|
+
}
|
|
83
|
+
declare class AiProviderFactory {
|
|
84
|
+
static createProvider(config: AiFactoryConfig, getAuthToken?: AuthTokenGetter): Promise<AiProvider>;
|
|
85
|
+
static getSetupMessage(providerId: string, config?: Partial<AiFactoryConfig>): string | null;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
interface AgentClientChatOptions extends AgentChatRequest {
|
|
89
|
+
ciphertext?: string;
|
|
90
|
+
byokProvider?: string;
|
|
91
|
+
}
|
|
92
|
+
declare class AgentClient {
|
|
93
|
+
private getAuthToken?;
|
|
94
|
+
private base;
|
|
95
|
+
constructor(baseUrl?: string, getAuthToken?: AuthTokenGetter | undefined);
|
|
96
|
+
/**
|
|
97
|
+
* Encrypts a personal API key using the edge proxy's KMS_SECRET.
|
|
98
|
+
* Works completely unauthenticated (zero login required).
|
|
99
|
+
*/
|
|
100
|
+
encryptApiKey(apiKey: string): Promise<string>;
|
|
101
|
+
/**
|
|
102
|
+
* Sends a chat request to the edge proxy.
|
|
103
|
+
* Auth token is optional (unauthenticated clients can use BYOK or anonymous tier).
|
|
104
|
+
*/
|
|
105
|
+
chat(request: AgentClientChatOptions): Promise<AgentChatResponse>;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export { type AgentChatMessage, type AgentChatRequest, type AgentChatResponse, type AgentChatTool, AgentClient, type AgentClientChatOptions, type AiFactoryConfig, type AiProvider, AiProviderFactory, type AuthTokenGetter, DisabledAiProvider, LocalOllamaProvider, ProxyAiProvider, WindowAiProvider };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,386 @@
|
|
|
1
|
+
// src/providers/ProxyAiProvider.ts
|
|
2
|
+
var ProxyAiProvider = class {
|
|
3
|
+
constructor(proxyUrl = "", getAuthToken, encryptedApiKey, byokProvider) {
|
|
4
|
+
this.getAuthToken = getAuthToken;
|
|
5
|
+
this.encryptedApiKey = encryptedApiKey;
|
|
6
|
+
this.byokProvider = byokProvider;
|
|
7
|
+
this.base = (proxyUrl || "").replace(/\/$/, "");
|
|
8
|
+
}
|
|
9
|
+
getAuthToken;
|
|
10
|
+
encryptedApiKey;
|
|
11
|
+
byokProvider;
|
|
12
|
+
id = "cloud-proxy";
|
|
13
|
+
base;
|
|
14
|
+
get mode() {
|
|
15
|
+
return this.encryptedApiKey ? "byok" : "cloud";
|
|
16
|
+
}
|
|
17
|
+
async chat(request) {
|
|
18
|
+
try {
|
|
19
|
+
const headers = {
|
|
20
|
+
"Content-Type": "application/json"
|
|
21
|
+
};
|
|
22
|
+
if (this.getAuthToken) {
|
|
23
|
+
const token = await this.getAuthToken();
|
|
24
|
+
if (token) {
|
|
25
|
+
headers["Authorization"] = `Bearer ${token}`;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
const response = await fetch(`${this.base}/api/v1/agent/chat`, {
|
|
29
|
+
method: "POST",
|
|
30
|
+
headers,
|
|
31
|
+
body: JSON.stringify({
|
|
32
|
+
messages: request.messages,
|
|
33
|
+
systemPrompt: request.systemPrompt,
|
|
34
|
+
tools: request.tools,
|
|
35
|
+
ciphertext: this.encryptedApiKey,
|
|
36
|
+
byokProvider: this.byokProvider
|
|
37
|
+
})
|
|
38
|
+
});
|
|
39
|
+
if (!response.ok) {
|
|
40
|
+
const error = await response.json().catch(() => ({}));
|
|
41
|
+
return {
|
|
42
|
+
type: "text",
|
|
43
|
+
error: error.code || error.message || error.error || `Proxy request failed (${response.status})`
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
return await response.json();
|
|
47
|
+
} catch (error) {
|
|
48
|
+
return { type: "text", error: error.message || "Network error communicating with AI proxy" };
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
async checkAvailability() {
|
|
52
|
+
if (!this.base) return false;
|
|
53
|
+
try {
|
|
54
|
+
const controller = new AbortController();
|
|
55
|
+
const id = setTimeout(() => controller.abort(), 3e3);
|
|
56
|
+
const response = await fetch(`${this.base}/health`, { signal: controller.signal });
|
|
57
|
+
clearTimeout(id);
|
|
58
|
+
return response.ok;
|
|
59
|
+
} catch {
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
getSetupMessage() {
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
// src/providers/WindowAiProvider.ts
|
|
69
|
+
var WindowAiProvider = class {
|
|
70
|
+
id = "window-ai";
|
|
71
|
+
mode = "local-browser";
|
|
72
|
+
async chat(request) {
|
|
73
|
+
try {
|
|
74
|
+
if (!this.isSupported()) {
|
|
75
|
+
return { type: "text", error: "window.ai not supported" };
|
|
76
|
+
}
|
|
77
|
+
const capabilities = await globalThis.window?.ai.languageModel.capabilities();
|
|
78
|
+
if (capabilities.available !== "readily") {
|
|
79
|
+
return { type: "text", error: "window.ai model not ready" };
|
|
80
|
+
}
|
|
81
|
+
const session = await globalThis.window?.ai.languageModel.create({
|
|
82
|
+
systemPrompt: request.systemPrompt
|
|
83
|
+
});
|
|
84
|
+
const prompt = request.messages.map((m) => `${m.role === "user" ? "User" : "Assistant"}: ${m.content}`).join("\n") + "\nAssistant:";
|
|
85
|
+
const response = await session.prompt(prompt);
|
|
86
|
+
session.destroy();
|
|
87
|
+
let cleanResponse = response.trim();
|
|
88
|
+
if (cleanResponse.includes("```json")) {
|
|
89
|
+
cleanResponse = cleanResponse.split("```json")[1].split("```")[0].trim();
|
|
90
|
+
} else if (cleanResponse.includes("```")) {
|
|
91
|
+
cleanResponse = cleanResponse.split("```")[1].split("```")[0].trim();
|
|
92
|
+
}
|
|
93
|
+
try {
|
|
94
|
+
const parsed = JSON.parse(cleanResponse);
|
|
95
|
+
if (parsed.tool && parsed.arguments) {
|
|
96
|
+
return {
|
|
97
|
+
type: "tool_call",
|
|
98
|
+
tool: parsed.tool,
|
|
99
|
+
arguments: parsed.arguments
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
if (parsed.type === "tool_call") {
|
|
103
|
+
return parsed;
|
|
104
|
+
}
|
|
105
|
+
return { type: "text", content: response };
|
|
106
|
+
} catch {
|
|
107
|
+
return { type: "text", content: response };
|
|
108
|
+
}
|
|
109
|
+
} catch (error) {
|
|
110
|
+
return { type: "text", error: error.message || "Window AI error" };
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
async checkAvailability() {
|
|
114
|
+
if (!this.isSupported()) return false;
|
|
115
|
+
try {
|
|
116
|
+
const capabilities = await globalThis.window?.ai.languageModel.capabilities();
|
|
117
|
+
return capabilities.available === "readily";
|
|
118
|
+
} catch {
|
|
119
|
+
return false;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
getSetupMessage() {
|
|
123
|
+
if (this.isSupported()) return null;
|
|
124
|
+
return "To enable Local Browser AI: 1. Use Chrome Dev/Canary. 2. Enable 'Prompt API for Gemini Nano' in chrome://flags. 3. Enable 'Enables optimization guide on device' in chrome://flags. 4. Relaunch and wait for model download in chrome://components.";
|
|
125
|
+
}
|
|
126
|
+
isSupported() {
|
|
127
|
+
return typeof globalThis !== "undefined" && !!globalThis.window?.ai?.languageModel;
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
// src/providers/LocalOllamaProvider.ts
|
|
132
|
+
var LocalOllamaProvider = class {
|
|
133
|
+
id = "local-ollama";
|
|
134
|
+
mode = "local-ollama";
|
|
135
|
+
model;
|
|
136
|
+
base;
|
|
137
|
+
constructor(baseUrl = "http://localhost:11434/api", model = "qwen2.5:0.5b") {
|
|
138
|
+
this.base = (baseUrl || "http://localhost:11434/api").replace(/\/$/, "");
|
|
139
|
+
this.model = model;
|
|
140
|
+
}
|
|
141
|
+
async chat(request) {
|
|
142
|
+
try {
|
|
143
|
+
const bodyPayload = {
|
|
144
|
+
model: this.model,
|
|
145
|
+
messages: [
|
|
146
|
+
{ role: "system", content: request.systemPrompt },
|
|
147
|
+
...request.messages
|
|
148
|
+
],
|
|
149
|
+
stream: false
|
|
150
|
+
};
|
|
151
|
+
if (request.tools && request.tools.length > 0) {
|
|
152
|
+
bodyPayload.tools = request.tools.map((t) => ({
|
|
153
|
+
type: "function",
|
|
154
|
+
function: {
|
|
155
|
+
name: t.name,
|
|
156
|
+
description: t.description,
|
|
157
|
+
parameters: t.parameters
|
|
158
|
+
}
|
|
159
|
+
}));
|
|
160
|
+
} else {
|
|
161
|
+
bodyPayload.format = "json";
|
|
162
|
+
}
|
|
163
|
+
const chatUrl = this.base.endsWith("/api") ? `${this.base}/chat` : `${this.base}/api/chat`;
|
|
164
|
+
const response = await fetch(chatUrl, {
|
|
165
|
+
method: "POST",
|
|
166
|
+
headers: {
|
|
167
|
+
"Content-Type": "application/json"
|
|
168
|
+
},
|
|
169
|
+
body: JSON.stringify(bodyPayload)
|
|
170
|
+
});
|
|
171
|
+
if (!response.ok) {
|
|
172
|
+
let errorMsg = `Ollama failed (${response.status})`;
|
|
173
|
+
try {
|
|
174
|
+
const errBody = await response.json();
|
|
175
|
+
if (errBody.error) errorMsg += `: ${errBody.error}`;
|
|
176
|
+
} catch {
|
|
177
|
+
}
|
|
178
|
+
return { type: "text", error: errorMsg };
|
|
179
|
+
}
|
|
180
|
+
const data = await response.json();
|
|
181
|
+
if (data.message?.tool_calls && data.message.tool_calls.length > 0) {
|
|
182
|
+
const call = data.message.tool_calls[0].function;
|
|
183
|
+
return {
|
|
184
|
+
type: "tool_call",
|
|
185
|
+
tool: call.name,
|
|
186
|
+
arguments: call.arguments
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
const content = data.message?.content || "";
|
|
190
|
+
try {
|
|
191
|
+
const parsed = JSON.parse(content);
|
|
192
|
+
const toolName = parsed.tool || parsed.action || parsed.type || parsed.tool_used;
|
|
193
|
+
if (toolName) {
|
|
194
|
+
const args = parsed.arguments || { ...parsed };
|
|
195
|
+
if (args === parsed) {
|
|
196
|
+
delete args.tool;
|
|
197
|
+
delete args.action;
|
|
198
|
+
delete args.type;
|
|
199
|
+
delete args.tool_used;
|
|
200
|
+
}
|
|
201
|
+
return {
|
|
202
|
+
type: "tool_call",
|
|
203
|
+
tool: toolName,
|
|
204
|
+
arguments: args
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
const requestedTools = (request.tools || []).map((t) => t.name);
|
|
208
|
+
for (const tool of requestedTools) {
|
|
209
|
+
const key = Object.keys(parsed).find((k) => k.toLowerCase() === tool.toLowerCase());
|
|
210
|
+
if (key) {
|
|
211
|
+
return {
|
|
212
|
+
type: "tool_call",
|
|
213
|
+
tool,
|
|
214
|
+
arguments: parsed[key]
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
return { type: "text", content };
|
|
219
|
+
} catch {
|
|
220
|
+
return { type: "text", content };
|
|
221
|
+
}
|
|
222
|
+
} catch (error) {
|
|
223
|
+
return { type: "text", error: error.message || "Local Ollama error" };
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
async checkAvailability() {
|
|
227
|
+
try {
|
|
228
|
+
const controller = new AbortController();
|
|
229
|
+
const id = setTimeout(() => controller.abort(), 2e3);
|
|
230
|
+
const tagsUrl = this.base.endsWith("/api") ? `${this.base}/tags` : `${this.base}/api/tags`;
|
|
231
|
+
const response = await fetch(tagsUrl, { signal: controller.signal });
|
|
232
|
+
clearTimeout(id);
|
|
233
|
+
return response.ok;
|
|
234
|
+
} catch {
|
|
235
|
+
return false;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
getSetupMessage() {
|
|
239
|
+
return `Note: Start Ollama with OLLAMA_ORIGINS="*" ollama serve to allow browser connections. Make sure you have pulled the model '${this.model}'.`;
|
|
240
|
+
}
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
// src/providers/AiProviderFactory.ts
|
|
244
|
+
var DisabledAiProvider = class {
|
|
245
|
+
id = "disabled";
|
|
246
|
+
mode = "disabled";
|
|
247
|
+
async chat() {
|
|
248
|
+
return { type: "text", error: "AI is disabled" };
|
|
249
|
+
}
|
|
250
|
+
async checkAvailability() {
|
|
251
|
+
return false;
|
|
252
|
+
}
|
|
253
|
+
getSetupMessage() {
|
|
254
|
+
return null;
|
|
255
|
+
}
|
|
256
|
+
};
|
|
257
|
+
var AiProviderFactory = class {
|
|
258
|
+
static async createProvider(config, getAuthToken) {
|
|
259
|
+
const { providerPreference, encryptedApiKey, baseUrl, proxyUrl, byokProvider, enableLocalAi } = config;
|
|
260
|
+
if (providerPreference === "disabled") {
|
|
261
|
+
return new DisabledAiProvider();
|
|
262
|
+
}
|
|
263
|
+
if (providerPreference === "byok" && encryptedApiKey && proxyUrl) {
|
|
264
|
+
return new ProxyAiProvider(proxyUrl, getAuthToken, encryptedApiKey, byokProvider);
|
|
265
|
+
}
|
|
266
|
+
if (providerPreference === "local") {
|
|
267
|
+
if (enableLocalAi !== false) {
|
|
268
|
+
return new LocalOllamaProvider(baseUrl, config.ollamaModel);
|
|
269
|
+
}
|
|
270
|
+
return new DisabledAiProvider();
|
|
271
|
+
}
|
|
272
|
+
if (providerPreference === "local-browser") {
|
|
273
|
+
return new WindowAiProvider();
|
|
274
|
+
}
|
|
275
|
+
if (providerPreference === "cloud" && proxyUrl) {
|
|
276
|
+
return new ProxyAiProvider(proxyUrl, getAuthToken, encryptedApiKey, byokProvider);
|
|
277
|
+
}
|
|
278
|
+
if (providerPreference === "auto") {
|
|
279
|
+
if (encryptedApiKey && proxyUrl) {
|
|
280
|
+
return new ProxyAiProvider(proxyUrl, getAuthToken, encryptedApiKey, byokProvider);
|
|
281
|
+
}
|
|
282
|
+
const windowProvider = new WindowAiProvider();
|
|
283
|
+
if (await windowProvider.checkAvailability()) {
|
|
284
|
+
return windowProvider;
|
|
285
|
+
}
|
|
286
|
+
if (enableLocalAi !== false) {
|
|
287
|
+
const localProvider = new LocalOllamaProvider(baseUrl, config.ollamaModel);
|
|
288
|
+
if (await localProvider.checkAvailability()) {
|
|
289
|
+
return localProvider;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
if (proxyUrl) {
|
|
293
|
+
return new ProxyAiProvider(proxyUrl, getAuthToken, void 0, byokProvider);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
return new DisabledAiProvider();
|
|
297
|
+
}
|
|
298
|
+
static getSetupMessage(providerId, config) {
|
|
299
|
+
switch (providerId) {
|
|
300
|
+
case "local":
|
|
301
|
+
case "local-ollama":
|
|
302
|
+
return new LocalOllamaProvider(config?.baseUrl, config?.ollamaModel).getSetupMessage();
|
|
303
|
+
case "local-browser":
|
|
304
|
+
case "window-ai":
|
|
305
|
+
return new WindowAiProvider().getSetupMessage();
|
|
306
|
+
case "byok":
|
|
307
|
+
return "Enter your Gemini API key below. It will be encrypted and stored solely in your personal storage.";
|
|
308
|
+
case "cloud":
|
|
309
|
+
return "Using Qozara Intelligence (Requires registered Qozara Google account).";
|
|
310
|
+
default:
|
|
311
|
+
return null;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
};
|
|
315
|
+
|
|
316
|
+
// src/AgentClient.ts
|
|
317
|
+
var AgentClient = class {
|
|
318
|
+
constructor(baseUrl = "", getAuthToken) {
|
|
319
|
+
this.getAuthToken = getAuthToken;
|
|
320
|
+
this.base = (baseUrl || "").replace(/\/$/, "");
|
|
321
|
+
}
|
|
322
|
+
getAuthToken;
|
|
323
|
+
base;
|
|
324
|
+
/**
|
|
325
|
+
* Encrypts a personal API key using the edge proxy's KMS_SECRET.
|
|
326
|
+
* Works completely unauthenticated (zero login required).
|
|
327
|
+
*/
|
|
328
|
+
async encryptApiKey(apiKey) {
|
|
329
|
+
const headers = {
|
|
330
|
+
"Content-Type": "application/json"
|
|
331
|
+
};
|
|
332
|
+
if (this.getAuthToken) {
|
|
333
|
+
const token = await this.getAuthToken();
|
|
334
|
+
if (token) {
|
|
335
|
+
headers["Authorization"] = `Bearer ${token}`;
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
const response = await fetch(`${this.base}/api/v1/agent/encrypt`, {
|
|
339
|
+
method: "POST",
|
|
340
|
+
headers,
|
|
341
|
+
body: JSON.stringify({ apiKey })
|
|
342
|
+
});
|
|
343
|
+
if (!response.ok) {
|
|
344
|
+
const error = await response.json().catch(() => ({}));
|
|
345
|
+
throw new Error(error.message || error.error || `Encryption failed (${response.status})`);
|
|
346
|
+
}
|
|
347
|
+
const result = await response.json();
|
|
348
|
+
return result.ciphertext;
|
|
349
|
+
}
|
|
350
|
+
/**
|
|
351
|
+
* Sends a chat request to the edge proxy.
|
|
352
|
+
* Auth token is optional (unauthenticated clients can use BYOK or anonymous tier).
|
|
353
|
+
*/
|
|
354
|
+
async chat(request) {
|
|
355
|
+
const headers = {
|
|
356
|
+
"Content-Type": "application/json"
|
|
357
|
+
};
|
|
358
|
+
if (this.getAuthToken) {
|
|
359
|
+
const token = await this.getAuthToken();
|
|
360
|
+
if (token) {
|
|
361
|
+
headers["Authorization"] = `Bearer ${token}`;
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
const response = await fetch(`${this.base}/api/v1/agent/chat`, {
|
|
365
|
+
method: "POST",
|
|
366
|
+
headers,
|
|
367
|
+
body: JSON.stringify(request)
|
|
368
|
+
});
|
|
369
|
+
if (!response.ok) {
|
|
370
|
+
const error = await response.json().catch(() => ({}));
|
|
371
|
+
return {
|
|
372
|
+
type: "text",
|
|
373
|
+
error: error.code || error.message || error.error || `AI proxy request failed (${response.status})`
|
|
374
|
+
};
|
|
375
|
+
}
|
|
376
|
+
return await response.json();
|
|
377
|
+
}
|
|
378
|
+
};
|
|
379
|
+
export {
|
|
380
|
+
AgentClient,
|
|
381
|
+
AiProviderFactory,
|
|
382
|
+
DisabledAiProvider,
|
|
383
|
+
LocalOllamaProvider,
|
|
384
|
+
ProxyAiProvider,
|
|
385
|
+
WindowAiProvider
|
|
386
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,38 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@qozara/ai-client",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "Isomorphic AI client SDK for Qozara ecosystem with polymorphic fallback and KMS envelope encryption support",
|
|
5
|
+
"files": [
|
|
6
|
+
"dist"
|
|
7
|
+
],
|
|
8
|
+
"publishConfig": {
|
|
9
|
+
"access": "public"
|
|
10
|
+
},
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/qozara/ai-proxy.git",
|
|
14
|
+
"directory": "packages/client"
|
|
15
|
+
},
|
|
16
|
+
"type": "module",
|
|
17
|
+
"main": "./dist/index.js",
|
|
18
|
+
"module": "./dist/index.js",
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"exports": {
|
|
21
|
+
".": {
|
|
22
|
+
"types": "./dist/index.d.ts",
|
|
23
|
+
"import": "./dist/index.js",
|
|
24
|
+
"default": "./dist/index.js"
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"scripts": {
|
|
28
|
+
"build": "tsup src/index.ts --format esm --dts",
|
|
29
|
+
"dev": "tsup src/index.ts --format esm --watch --dts",
|
|
30
|
+
"test": "vitest run",
|
|
31
|
+
"check": "tsc --noEmit"
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"tsup": "^8.3.0",
|
|
35
|
+
"typescript": "5.6.3",
|
|
36
|
+
"vitest": "^4.1.11"
|
|
37
|
+
}
|
|
38
|
+
}
|
package/README.md
DELETED