@chaoslabs/ai-sdk 0.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/LICENSE +41 -0
- package/README.md +241 -0
- package/dist/client.d.ts +77 -0
- package/dist/client.js +167 -0
- package/dist/converter.d.ts +5 -0
- package/dist/converter.js +159 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +6 -0
- package/dist/request.d.ts +48 -0
- package/dist/request.js +93 -0
- package/dist/response.d.ts +24 -0
- package/dist/response.js +111 -0
- package/dist/schemas.d.ts +547 -0
- package/dist/schemas.js +143 -0
- package/dist/types.d.ts +310 -0
- package/dist/types.js +126 -0
- package/package.json +52 -0
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { CreateResponseParams } from './types.js';
|
|
2
|
+
/** Model ID for wallet mode (v1 API) */
|
|
3
|
+
export declare const WALLET_MODEL = "WALLET_MODEL";
|
|
4
|
+
/** Model ID for ask mode (v1 API) */
|
|
5
|
+
export declare const ASK_MODEL = "ASK_MODEL";
|
|
6
|
+
/**
|
|
7
|
+
* V1 API request body for wallet mode.
|
|
8
|
+
*/
|
|
9
|
+
export interface V1WalletRequest {
|
|
10
|
+
model: string;
|
|
11
|
+
query: string;
|
|
12
|
+
user_id: string;
|
|
13
|
+
session_id: string;
|
|
14
|
+
wallet_id: string;
|
|
15
|
+
prior_messages?: {
|
|
16
|
+
role: string;
|
|
17
|
+
content: string;
|
|
18
|
+
}[];
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* V1 API request body for ask mode.
|
|
22
|
+
*/
|
|
23
|
+
export interface V1AskRequest {
|
|
24
|
+
model: string;
|
|
25
|
+
query: string;
|
|
26
|
+
user_id: string;
|
|
27
|
+
session_id: string;
|
|
28
|
+
prior_messages?: {
|
|
29
|
+
role: string;
|
|
30
|
+
content: string;
|
|
31
|
+
}[];
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Transform SDK request to v1 wallet format.
|
|
35
|
+
*/
|
|
36
|
+
export declare function toV1WalletRequest(params: CreateResponseParams, sessionId: string): V1WalletRequest;
|
|
37
|
+
/**
|
|
38
|
+
* Transform SDK request to v1 ask format.
|
|
39
|
+
*/
|
|
40
|
+
export declare function toV1AskRequest(params: CreateResponseParams, sessionId: string): V1AskRequest;
|
|
41
|
+
/**
|
|
42
|
+
* Build HTTP headers for v1 API requests (Bearer auth).
|
|
43
|
+
*/
|
|
44
|
+
export declare function buildV1Headers(apiKey: string): Record<string, string>;
|
|
45
|
+
/**
|
|
46
|
+
* Build the v1 endpoint URL.
|
|
47
|
+
*/
|
|
48
|
+
export declare function buildV1Endpoint(baseUrl: string): string;
|
package/dist/request.js
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
// V1 API Request Adapter for /v1/chat/stream
|
|
2
|
+
// ============================================================================
|
|
3
|
+
// Model Constants
|
|
4
|
+
// ============================================================================
|
|
5
|
+
/** Model ID for wallet mode (v1 API) */
|
|
6
|
+
export const WALLET_MODEL = 'WALLET_MODEL';
|
|
7
|
+
/** Model ID for ask mode (v1 API) */
|
|
8
|
+
export const ASK_MODEL = 'ASK_MODEL';
|
|
9
|
+
// ============================================================================
|
|
10
|
+
// Request Builders
|
|
11
|
+
// ============================================================================
|
|
12
|
+
/**
|
|
13
|
+
* Transform SDK request to v1 wallet format.
|
|
14
|
+
*/
|
|
15
|
+
export function toV1WalletRequest(params, sessionId) {
|
|
16
|
+
const query = extractQuery(params.input);
|
|
17
|
+
const priorMessages = extractPriorMessages(params.input);
|
|
18
|
+
return {
|
|
19
|
+
model: params.model,
|
|
20
|
+
query,
|
|
21
|
+
user_id: params.metadata.user_id,
|
|
22
|
+
session_id: sessionId,
|
|
23
|
+
wallet_id: params.metadata.wallet_id || '',
|
|
24
|
+
prior_messages: priorMessages.length > 0 ? priorMessages : undefined,
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Transform SDK request to v1 ask format.
|
|
29
|
+
*/
|
|
30
|
+
export function toV1AskRequest(params, sessionId) {
|
|
31
|
+
const query = extractQuery(params.input);
|
|
32
|
+
const priorMessages = extractPriorMessages(params.input);
|
|
33
|
+
return {
|
|
34
|
+
model: params.model,
|
|
35
|
+
query,
|
|
36
|
+
user_id: params.metadata.user_id,
|
|
37
|
+
session_id: sessionId,
|
|
38
|
+
prior_messages: priorMessages.length > 0 ? priorMessages : undefined,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
// ============================================================================
|
|
42
|
+
// Helper Functions
|
|
43
|
+
// ============================================================================
|
|
44
|
+
/**
|
|
45
|
+
* Extract the main query from input items.
|
|
46
|
+
*/
|
|
47
|
+
function extractQuery(input) {
|
|
48
|
+
for (let i = input.length - 1; i >= 0; i--) {
|
|
49
|
+
if (input[i].role === 'user') {
|
|
50
|
+
return input[i].content;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return '';
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Extract prior messages from input items (excluding the last user message).
|
|
57
|
+
*/
|
|
58
|
+
function extractPriorMessages(input) {
|
|
59
|
+
const messages = [];
|
|
60
|
+
let lastUserIndex = -1;
|
|
61
|
+
for (let i = input.length - 1; i >= 0; i--) {
|
|
62
|
+
if (input[i].role === 'user') {
|
|
63
|
+
lastUserIndex = i;
|
|
64
|
+
break;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
for (let i = 0; i < lastUserIndex; i++) {
|
|
68
|
+
const item = input[i];
|
|
69
|
+
if (item.type === 'message' && (item.role === 'user' || item.role === 'system')) {
|
|
70
|
+
messages.push({
|
|
71
|
+
role: item.role === 'system' ? 'system' : item.role,
|
|
72
|
+
content: item.content,
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return messages;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Build HTTP headers for v1 API requests (Bearer auth).
|
|
80
|
+
*/
|
|
81
|
+
export function buildV1Headers(apiKey) {
|
|
82
|
+
return {
|
|
83
|
+
'Content-Type': 'application/json',
|
|
84
|
+
'Authorization': `Bearer ${apiKey}`,
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Build the v1 endpoint URL.
|
|
89
|
+
*/
|
|
90
|
+
export function buildV1Endpoint(baseUrl) {
|
|
91
|
+
const base = baseUrl.replace(/\/$/, '');
|
|
92
|
+
return `${base}/v1/chat/stream`;
|
|
93
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { Response } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* V1 streaming event from NDJSON stream.
|
|
4
|
+
*/
|
|
5
|
+
export interface V1StreamEvent {
|
|
6
|
+
type?: string;
|
|
7
|
+
content?: Record<string, unknown>;
|
|
8
|
+
[key: string]: unknown;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Final state collected from v1 stream.
|
|
12
|
+
*/
|
|
13
|
+
export interface V1FinalState {
|
|
14
|
+
blocks: unknown[];
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Parse v1 NDJSON stream and extract blocks.
|
|
18
|
+
* Handles type: "report" events with content.data.
|
|
19
|
+
*/
|
|
20
|
+
export declare function parseV1Stream(body: ReadableStream<Uint8Array>, onEvent?: (event: V1StreamEvent) => void): Promise<V1FinalState>;
|
|
21
|
+
/**
|
|
22
|
+
* Convert v1 final state to SDK Response format.
|
|
23
|
+
*/
|
|
24
|
+
export declare function toV1Response(responseId: string, model: string, finalState: V1FinalState): Response;
|
package/dist/response.js
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
// V1 API Response Parser
|
|
2
|
+
//
|
|
3
|
+
// Parses responses from /v1/chat/stream endpoint.
|
|
4
|
+
// Handles type: "report" events with content.data blocks.
|
|
5
|
+
import { convertBlock } from './converter.js';
|
|
6
|
+
// ============================================================================
|
|
7
|
+
// Response Parser
|
|
8
|
+
// ============================================================================
|
|
9
|
+
/**
|
|
10
|
+
* Parse v1 NDJSON stream and extract blocks.
|
|
11
|
+
* Handles type: "report" events with content.data.
|
|
12
|
+
*/
|
|
13
|
+
export async function parseV1Stream(body, onEvent) {
|
|
14
|
+
const reader = body.getReader();
|
|
15
|
+
const decoder = new TextDecoder();
|
|
16
|
+
let buffer = '';
|
|
17
|
+
const blocks = [];
|
|
18
|
+
try {
|
|
19
|
+
while (true) {
|
|
20
|
+
const { done, value } = await reader.read();
|
|
21
|
+
if (done)
|
|
22
|
+
break;
|
|
23
|
+
buffer += decoder.decode(value, { stream: true });
|
|
24
|
+
const lines = buffer.split('\n');
|
|
25
|
+
buffer = lines.pop() || '';
|
|
26
|
+
for (const line of lines) {
|
|
27
|
+
if (!line.trim())
|
|
28
|
+
continue;
|
|
29
|
+
try {
|
|
30
|
+
const event = JSON.parse(line);
|
|
31
|
+
// Handle report events with blocks
|
|
32
|
+
if (event.type === 'report' && event.content) {
|
|
33
|
+
const content = event.content;
|
|
34
|
+
// Extract the data from the report
|
|
35
|
+
if (content.data) {
|
|
36
|
+
blocks.push(content.data);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
// Call event callback
|
|
40
|
+
if (onEvent) {
|
|
41
|
+
onEvent(event);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
// Skip malformed JSON
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
// Process remaining buffer
|
|
50
|
+
if (buffer.trim()) {
|
|
51
|
+
try {
|
|
52
|
+
const event = JSON.parse(buffer);
|
|
53
|
+
if (event.type === 'report' && event.content) {
|
|
54
|
+
const content = event.content;
|
|
55
|
+
if (content.data) {
|
|
56
|
+
blocks.push(content.data);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
// Skip malformed JSON
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
finally {
|
|
66
|
+
reader.releaseLock();
|
|
67
|
+
}
|
|
68
|
+
return { blocks };
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Convert v1 final state to SDK Response format.
|
|
72
|
+
*/
|
|
73
|
+
export function toV1Response(responseId, model, finalState) {
|
|
74
|
+
const content = [];
|
|
75
|
+
// Process blocks from final state
|
|
76
|
+
if (finalState.blocks && Array.isArray(finalState.blocks)) {
|
|
77
|
+
for (const rawBlock of finalState.blocks) {
|
|
78
|
+
if (typeof rawBlock === 'string') {
|
|
79
|
+
// Text content
|
|
80
|
+
content.push({
|
|
81
|
+
type: 'output_text',
|
|
82
|
+
text: rawBlock,
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
else if (rawBlock && typeof rawBlock === 'object') {
|
|
86
|
+
// Block content - use existing converter
|
|
87
|
+
const block = convertBlock(rawBlock);
|
|
88
|
+
if (block) {
|
|
89
|
+
content.push({
|
|
90
|
+
type: 'chaos.block',
|
|
91
|
+
block,
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
const output = content.length > 0
|
|
98
|
+
? [{
|
|
99
|
+
type: 'message',
|
|
100
|
+
role: 'assistant',
|
|
101
|
+
content,
|
|
102
|
+
}]
|
|
103
|
+
: [];
|
|
104
|
+
return {
|
|
105
|
+
id: responseId,
|
|
106
|
+
object: 'response',
|
|
107
|
+
model,
|
|
108
|
+
status: 'completed',
|
|
109
|
+
output,
|
|
110
|
+
};
|
|
111
|
+
}
|