v0-sdk 0.0.3 → 0.0.4
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.js +105 -0
- package/package.json +1 -1
package/dist/index.js
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* v0 Chats API SDK
|
|
3
|
+
* A client library for interacting with the v0 Chats API
|
|
4
|
+
*/ class V0ChatsError extends Error {
|
|
5
|
+
constructor(message, status, data){
|
|
6
|
+
super(message);
|
|
7
|
+
this.name = 'V0ChatsError';
|
|
8
|
+
this.status = status;
|
|
9
|
+
this.data = data;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Client for the v0 Chats API
|
|
14
|
+
*/ class V0Client {
|
|
15
|
+
/**
|
|
16
|
+
* Create a new v0 Chats API client
|
|
17
|
+
*
|
|
18
|
+
* @param options Client configuration options
|
|
19
|
+
*/ constructor(options){
|
|
20
|
+
this.apiKey = options.apiKey;
|
|
21
|
+
this.baseUrl = options.baseUrl || 'https://api.v0.dev/';
|
|
22
|
+
this.fetchFn = options.fetch || fetch;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Create a new chat
|
|
26
|
+
*
|
|
27
|
+
* @param options Options for creating a new chat
|
|
28
|
+
* @returns Promise with the created chat details
|
|
29
|
+
*/ async createChat(options) {
|
|
30
|
+
const response = await this.request('POST', '/chats', options);
|
|
31
|
+
return response;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Add a message to an existing chat
|
|
35
|
+
*
|
|
36
|
+
* @param chatId The ID of the chat to add a message to
|
|
37
|
+
* @param options Options for adding a message
|
|
38
|
+
* @returns Promise with the updated chat details
|
|
39
|
+
*/ async addMessage(chatId, options) {
|
|
40
|
+
const response = await this.request('POST', `/chats/${chatId}`, options);
|
|
41
|
+
return response;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Get chat details and message history
|
|
45
|
+
*
|
|
46
|
+
* @param chatId The ID of the chat to retrieve
|
|
47
|
+
* @returns Promise with the chat details and messages
|
|
48
|
+
*/ async getChat(chatId) {
|
|
49
|
+
const response = await this.request('GET', `/chats/${chatId}`);
|
|
50
|
+
return response;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Continue an existing chat (deprecated)
|
|
54
|
+
*
|
|
55
|
+
* This method is deprecated. Use addMessage() instead.
|
|
56
|
+
*
|
|
57
|
+
* @deprecated Use addMessage() instead
|
|
58
|
+
* @param chatId The ID of the chat to continue
|
|
59
|
+
* @param options Options for continuing the chat
|
|
60
|
+
* @returns Promise with the updated chat details
|
|
61
|
+
*/ async continueChat(chatId, options) {
|
|
62
|
+
console.warn('continueChat() is deprecated. Use addMessage() instead.');
|
|
63
|
+
const payload = {
|
|
64
|
+
...options,
|
|
65
|
+
chatId
|
|
66
|
+
};
|
|
67
|
+
const response = await this.request('POST', '/chats', payload);
|
|
68
|
+
return response;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Make a request to the v0 Chats API
|
|
72
|
+
*
|
|
73
|
+
* @param method HTTP method
|
|
74
|
+
* @param path API path
|
|
75
|
+
* @param data Request data
|
|
76
|
+
* @returns Promise with the response data
|
|
77
|
+
*/ async request(method, path, data) {
|
|
78
|
+
const url = `${this.baseUrl}${path}`;
|
|
79
|
+
const headers = {
|
|
80
|
+
Authorization: `Bearer ${this.apiKey}`,
|
|
81
|
+
'Content-Type': 'application/json'
|
|
82
|
+
};
|
|
83
|
+
const options = {
|
|
84
|
+
method,
|
|
85
|
+
headers,
|
|
86
|
+
body: data && method !== 'GET' ? JSON.stringify(data) : undefined
|
|
87
|
+
};
|
|
88
|
+
try {
|
|
89
|
+
const response = await this.fetchFn(url, options);
|
|
90
|
+
const responseData = await response.json();
|
|
91
|
+
if (!response.ok) {
|
|
92
|
+
throw new V0ChatsError(responseData.error || `API request failed with status ${response.status}`, response.status, responseData);
|
|
93
|
+
}
|
|
94
|
+
return responseData;
|
|
95
|
+
} catch (error) {
|
|
96
|
+
if (error instanceof V0ChatsError) {
|
|
97
|
+
throw error;
|
|
98
|
+
}
|
|
99
|
+
throw new V0ChatsError(error instanceof Error ? error.message : 'Unknown error occurred', 500);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
exports.V0ChatsError = V0ChatsError;
|
|
105
|
+
exports.V0Client = V0Client;
|