@protoboxai/sdk 1.0.0
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/README.md +51 -0
- package/dist/adapters/openai.d.ts +106 -0
- package/dist/adapters/openai.js +185 -0
- package/dist/client.d.ts +60 -0
- package/dist/client.js +307 -0
- package/dist/errors/index.d.ts +179 -0
- package/dist/errors/index.js +319 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.js +62 -0
- package/dist/live/index.d.ts +5 -0
- package/dist/live/index.js +10 -0
- package/dist/live/live-chat.d.ts +71 -0
- package/dist/live/live-chat.js +95 -0
- package/dist/live/typed-emitter.d.ts +15 -0
- package/dist/live/typed-emitter.js +40 -0
- package/dist/live/types.d.ts +24 -0
- package/dist/live/types.js +6 -0
- package/dist/modules/auth.d.ts +76 -0
- package/dist/modules/auth.js +59 -0
- package/dist/modules/chat.d.ts +164 -0
- package/dist/modules/chat.js +168 -0
- package/dist/modules/health.d.ts +45 -0
- package/dist/modules/health.js +22 -0
- package/dist/modules/knowledge.d.ts +202 -0
- package/dist/modules/knowledge.js +147 -0
- package/dist/modules/mcp.d.ts +138 -0
- package/dist/modules/mcp.js +110 -0
- package/dist/modules/prompts.d.ts +128 -0
- package/dist/modules/prompts.js +93 -0
- package/dist/modules/tools.d.ts +222 -0
- package/dist/modules/tools.js +302 -0
- package/dist/modules/toolsets.d.ts +173 -0
- package/dist/modules/toolsets.js +216 -0
- package/dist/modules/workspace.d.ts +48 -0
- package/dist/modules/workspace.js +49 -0
- package/dist/types/api.d.ts +4 -0
- package/dist/types/api.js +21 -0
- package/dist/types/config.d.ts +81 -0
- package/dist/types/config.js +3 -0
- package/dist/types/tool-calls.d.ts +60 -0
- package/dist/types/tool-calls.js +8 -0
- package/dist/types/tools.d.ts +321 -0
- package/dist/types/tools.js +9 -0
- package/dist/types/toolsets.d.ts +151 -0
- package/dist/types/toolsets.js +8 -0
- package/package.json +52 -0
package/README.md
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# @protoboxai/sdk
|
|
2
|
+
|
|
3
|
+
Public TypeScript SDK for [Protobox](https://protobox.app) — managed MCP servers, tools, agents, knowledge resources, prompts, and chat.
|
|
4
|
+
|
|
5
|
+
Forked from [`@chanl/sdk`](https://www.npmjs.com/package/@chanl/sdk) (source: `../chanl-v2/chanl-sdk`). Same API surface; Protobox-branded package for npm publish.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @protoboxai/sdk
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Quick start
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import { ProtoboxSDK } from '@protoboxai/sdk';
|
|
17
|
+
|
|
18
|
+
const sdk = new ProtoboxSDK({
|
|
19
|
+
apiKey: process.env.PROTOBOX_API_KEY!,
|
|
20
|
+
baseUrl: 'https://platform.protobox.ai', // or http://localhost:3100
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
// MCP-focused surface
|
|
24
|
+
const tools = await sdk.tools.list();
|
|
25
|
+
const resources = await sdk.knowledge.list();
|
|
26
|
+
const prompts = await sdk.prompts.list();
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Modules
|
|
30
|
+
|
|
31
|
+
| Module | Purpose |
|
|
32
|
+
|--------|---------|
|
|
33
|
+
| `tools` / `toolsets` | HTTP/OpenAPI tools exposed via MCP |
|
|
34
|
+
| `knowledge` | Knowledge base (documents, URLs, files) |
|
|
35
|
+
| `prompts` | MCP prompt templates |
|
|
36
|
+
| `agents` | Workspace agents |
|
|
37
|
+
| `mcp` | MCP server helpers |
|
|
38
|
+
| `chat` | Text chat with agents |
|
|
39
|
+
|
|
40
|
+
## Sync from upstream
|
|
41
|
+
|
|
42
|
+
When `chanl-sdk` changes, refresh this package from the monorepo root:
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
make sync-public-sdk
|
|
46
|
+
make build-public-sdk
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Publish
|
|
50
|
+
|
|
51
|
+
Package is publish-ready (`publishConfig.access: public`). Bump version in `package.json` before `npm publish` from `packages/protobox-sdk`.
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OpenAI Adapter for @chanl/sdk
|
|
3
|
+
*
|
|
4
|
+
* TDD Phase: GREEN - Implementation to make tests pass
|
|
5
|
+
*
|
|
6
|
+
* Provides integration with OpenAI's function calling format:
|
|
7
|
+
* 1. Convert Chanl tools to OpenAI ChatCompletionTool format
|
|
8
|
+
* 2. Execute tool calls from OpenAI responses
|
|
9
|
+
*/
|
|
10
|
+
import { ChanlSDK } from '../client';
|
|
11
|
+
/**
|
|
12
|
+
* OpenAI function definition
|
|
13
|
+
*/
|
|
14
|
+
export interface OpenAIFunction {
|
|
15
|
+
name: string;
|
|
16
|
+
description: string;
|
|
17
|
+
parameters: Record<string, unknown>;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* OpenAI ChatCompletionTool format
|
|
21
|
+
*/
|
|
22
|
+
export interface OpenAIChatCompletionTool {
|
|
23
|
+
type: 'function';
|
|
24
|
+
function: OpenAIFunction;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* OpenAI tool call from assistant response
|
|
28
|
+
*/
|
|
29
|
+
export interface OpenAIToolCall {
|
|
30
|
+
id: string;
|
|
31
|
+
type: 'function';
|
|
32
|
+
function: {
|
|
33
|
+
name: string;
|
|
34
|
+
arguments: string;
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Tool output for OpenAI messages
|
|
39
|
+
* Matches OpenAI's ChatCompletionToolMessageParam
|
|
40
|
+
*/
|
|
41
|
+
export interface ToolOutput {
|
|
42
|
+
tool_call_id: string;
|
|
43
|
+
role: 'tool';
|
|
44
|
+
content: string;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Options for fetching tools
|
|
48
|
+
*/
|
|
49
|
+
export interface GetToolsOptions {
|
|
50
|
+
/** Filter by tag */
|
|
51
|
+
tag?: string;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Adapter to integrate Chanl tools with OpenAI's function calling
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* ```typescript
|
|
58
|
+
* const adapter = new OpenAIAdapter(sdk, 'ws_abc123');
|
|
59
|
+
*
|
|
60
|
+
* // Get tools as OpenAI functions
|
|
61
|
+
* const tools = await adapter.getToolsAsOpenAIFunctions();
|
|
62
|
+
*
|
|
63
|
+
* // Use with OpenAI
|
|
64
|
+
* const response = await openai.chat.completions.create({
|
|
65
|
+
* model: 'gpt-4',
|
|
66
|
+
* messages: [...],
|
|
67
|
+
* tools,
|
|
68
|
+
* });
|
|
69
|
+
*
|
|
70
|
+
* // Execute tool calls from response
|
|
71
|
+
* if (response.choices[0].message.tool_calls) {
|
|
72
|
+
* const results = await adapter.executeToolCalls(
|
|
73
|
+
* response.choices[0].message.tool_calls
|
|
74
|
+
* );
|
|
75
|
+
* }
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
export declare class OpenAIAdapter {
|
|
79
|
+
private sdk;
|
|
80
|
+
/** Cached tools for lookup during execution */
|
|
81
|
+
private toolsCache;
|
|
82
|
+
constructor(sdk: ChanlSDK);
|
|
83
|
+
/**
|
|
84
|
+
* Convert Chanl tools to OpenAI function calling format
|
|
85
|
+
*
|
|
86
|
+
* @param options - Optional filters for which tools to include
|
|
87
|
+
* @returns Array of OpenAI ChatCompletionTool objects
|
|
88
|
+
*/
|
|
89
|
+
getToolsAsOpenAIFunctions(options?: GetToolsOptions): Promise<OpenAIChatCompletionTool[]>;
|
|
90
|
+
/**
|
|
91
|
+
* Execute tool calls from an OpenAI response
|
|
92
|
+
*
|
|
93
|
+
* @param toolCalls - Array of tool calls from OpenAI assistant message
|
|
94
|
+
* @returns Array of tool outputs ready for OpenAI messages
|
|
95
|
+
*/
|
|
96
|
+
executeToolCalls(toolCalls: OpenAIToolCall[]): Promise<ToolOutput[]>;
|
|
97
|
+
/**
|
|
98
|
+
* Convert a single Chanl tool to OpenAI function format
|
|
99
|
+
*/
|
|
100
|
+
private convertToolToOpenAI;
|
|
101
|
+
/**
|
|
102
|
+
* Execute a single tool call
|
|
103
|
+
*/
|
|
104
|
+
private executeSingleToolCall;
|
|
105
|
+
}
|
|
106
|
+
//# sourceMappingURL=openai.d.ts.map
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* OpenAI Adapter for @chanl/sdk
|
|
4
|
+
*
|
|
5
|
+
* TDD Phase: GREEN - Implementation to make tests pass
|
|
6
|
+
*
|
|
7
|
+
* Provides integration with OpenAI's function calling format:
|
|
8
|
+
* 1. Convert Chanl tools to OpenAI ChatCompletionTool format
|
|
9
|
+
* 2. Execute tool calls from OpenAI responses
|
|
10
|
+
*/
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.OpenAIAdapter = void 0;
|
|
13
|
+
/**
|
|
14
|
+
* Adapter to integrate Chanl tools with OpenAI's function calling
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* const adapter = new OpenAIAdapter(sdk, 'ws_abc123');
|
|
19
|
+
*
|
|
20
|
+
* // Get tools as OpenAI functions
|
|
21
|
+
* const tools = await adapter.getToolsAsOpenAIFunctions();
|
|
22
|
+
*
|
|
23
|
+
* // Use with OpenAI
|
|
24
|
+
* const response = await openai.chat.completions.create({
|
|
25
|
+
* model: 'gpt-4',
|
|
26
|
+
* messages: [...],
|
|
27
|
+
* tools,
|
|
28
|
+
* });
|
|
29
|
+
*
|
|
30
|
+
* // Execute tool calls from response
|
|
31
|
+
* if (response.choices[0].message.tool_calls) {
|
|
32
|
+
* const results = await adapter.executeToolCalls(
|
|
33
|
+
* response.choices[0].message.tool_calls
|
|
34
|
+
* );
|
|
35
|
+
* }
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
class OpenAIAdapter {
|
|
39
|
+
constructor(sdk) {
|
|
40
|
+
this.sdk = sdk;
|
|
41
|
+
/** Cached tools for lookup during execution */
|
|
42
|
+
this.toolsCache = new Map();
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Convert Chanl tools to OpenAI function calling format
|
|
46
|
+
*
|
|
47
|
+
* @param options - Optional filters for which tools to include
|
|
48
|
+
* @returns Array of OpenAI ChatCompletionTool objects
|
|
49
|
+
*/
|
|
50
|
+
async getToolsAsOpenAIFunctions(options = {}) {
|
|
51
|
+
const filters = {
|
|
52
|
+
isEnabled: true,
|
|
53
|
+
};
|
|
54
|
+
if (options.tag) {
|
|
55
|
+
filters.tag = options.tag;
|
|
56
|
+
}
|
|
57
|
+
const response = await this.sdk.tools.list(filters);
|
|
58
|
+
if (!response.success || !response.data?.tools) {
|
|
59
|
+
return [];
|
|
60
|
+
}
|
|
61
|
+
// Cache tools for later execution
|
|
62
|
+
this.toolsCache.clear();
|
|
63
|
+
for (const tool of response.data.tools) {
|
|
64
|
+
this.toolsCache.set(tool.name, tool);
|
|
65
|
+
}
|
|
66
|
+
return response.data.tools.map((tool) => this.convertToolToOpenAI(tool));
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Execute tool calls from an OpenAI response
|
|
70
|
+
*
|
|
71
|
+
* @param toolCalls - Array of tool calls from OpenAI assistant message
|
|
72
|
+
* @returns Array of tool outputs ready for OpenAI messages
|
|
73
|
+
*/
|
|
74
|
+
async executeToolCalls(toolCalls) {
|
|
75
|
+
if (toolCalls.length === 0) {
|
|
76
|
+
return [];
|
|
77
|
+
}
|
|
78
|
+
// Refresh cache if empty
|
|
79
|
+
if (this.toolsCache.size === 0) {
|
|
80
|
+
await this.getToolsAsOpenAIFunctions();
|
|
81
|
+
}
|
|
82
|
+
const results = [];
|
|
83
|
+
for (const toolCall of toolCalls) {
|
|
84
|
+
const result = await this.executeSingleToolCall(toolCall);
|
|
85
|
+
results.push(result);
|
|
86
|
+
}
|
|
87
|
+
return results;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Convert a single Chanl tool to OpenAI function format
|
|
91
|
+
*/
|
|
92
|
+
convertToolToOpenAI(tool) {
|
|
93
|
+
return {
|
|
94
|
+
type: 'function',
|
|
95
|
+
function: {
|
|
96
|
+
name: tool.name,
|
|
97
|
+
description: tool.description,
|
|
98
|
+
parameters: tool.inputSchema,
|
|
99
|
+
},
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Execute a single tool call
|
|
104
|
+
*/
|
|
105
|
+
async executeSingleToolCall(toolCall) {
|
|
106
|
+
const { id, function: func } = toolCall;
|
|
107
|
+
const { name, arguments: argsString } = func;
|
|
108
|
+
// Find the tool
|
|
109
|
+
const tool = this.toolsCache.get(name);
|
|
110
|
+
if (!tool) {
|
|
111
|
+
return {
|
|
112
|
+
tool_call_id: id,
|
|
113
|
+
role: 'tool',
|
|
114
|
+
content: JSON.stringify({
|
|
115
|
+
error: `Tool '${name}' not found`,
|
|
116
|
+
}),
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
// Parse arguments
|
|
120
|
+
let args;
|
|
121
|
+
try {
|
|
122
|
+
args = JSON.parse(argsString);
|
|
123
|
+
}
|
|
124
|
+
catch {
|
|
125
|
+
return {
|
|
126
|
+
tool_call_id: id,
|
|
127
|
+
role: 'tool',
|
|
128
|
+
content: JSON.stringify({
|
|
129
|
+
error: `Invalid JSON arguments: ${argsString}`,
|
|
130
|
+
}),
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
// Execute the tool
|
|
134
|
+
try {
|
|
135
|
+
const response = await this.sdk.tools.execute(tool.id, args);
|
|
136
|
+
if (!response.success) {
|
|
137
|
+
return {
|
|
138
|
+
tool_call_id: id,
|
|
139
|
+
role: 'tool',
|
|
140
|
+
content: JSON.stringify({
|
|
141
|
+
error: response.message ?? 'Tool execution failed',
|
|
142
|
+
}),
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
const execution = response.data;
|
|
146
|
+
// Handle undefined execution data
|
|
147
|
+
if (!execution) {
|
|
148
|
+
return {
|
|
149
|
+
tool_call_id: id,
|
|
150
|
+
role: 'tool',
|
|
151
|
+
content: JSON.stringify({
|
|
152
|
+
error: 'No execution data returned',
|
|
153
|
+
}),
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
// Check if execution failed
|
|
157
|
+
if (!execution.success) {
|
|
158
|
+
return {
|
|
159
|
+
tool_call_id: id,
|
|
160
|
+
role: 'tool',
|
|
161
|
+
content: JSON.stringify({
|
|
162
|
+
error: execution.error ?? 'Execution failed',
|
|
163
|
+
}),
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
// Return successful output
|
|
167
|
+
return {
|
|
168
|
+
tool_call_id: id,
|
|
169
|
+
role: 'tool',
|
|
170
|
+
content: JSON.stringify(execution.data ?? {}),
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
catch (error) {
|
|
174
|
+
return {
|
|
175
|
+
tool_call_id: id,
|
|
176
|
+
role: 'tool',
|
|
177
|
+
content: JSON.stringify({
|
|
178
|
+
error: error instanceof Error ? error.message : 'Unknown execution error',
|
|
179
|
+
}),
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
exports.OpenAIAdapter = OpenAIAdapter;
|
|
185
|
+
//# sourceMappingURL=openai.js.map
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { ChanlConfig, RequestConfig, ApiResponse } from "./types/config";
|
|
2
|
+
import { WorkspaceModule } from "./modules/workspace";
|
|
3
|
+
import { AuthModule } from "./modules/auth";
|
|
4
|
+
import { HealthModule } from "./modules/health";
|
|
5
|
+
import { PromptsModule } from "./modules/prompts";
|
|
6
|
+
import { ToolsModule } from "./modules/tools";
|
|
7
|
+
import { ToolsetsModule } from "./modules/toolsets";
|
|
8
|
+
import { McpModule } from "./modules/mcp";
|
|
9
|
+
import { KnowledgeModule } from "./modules/knowledge";
|
|
10
|
+
import { ChatModule } from "./modules/chat";
|
|
11
|
+
export declare class ChanlSDK {
|
|
12
|
+
private config;
|
|
13
|
+
readonly workspace: WorkspaceModule;
|
|
14
|
+
readonly auth: AuthModule;
|
|
15
|
+
readonly health: HealthModule;
|
|
16
|
+
readonly prompts: PromptsModule;
|
|
17
|
+
readonly tools: ToolsModule;
|
|
18
|
+
readonly toolsets: ToolsetsModule;
|
|
19
|
+
readonly mcp: McpModule;
|
|
20
|
+
readonly knowledge: KnowledgeModule;
|
|
21
|
+
readonly chat: ChatModule;
|
|
22
|
+
constructor(config: ChanlConfig);
|
|
23
|
+
/**
|
|
24
|
+
* Make a request to the API
|
|
25
|
+
*/
|
|
26
|
+
/**
|
|
27
|
+
* Rewrite `/api/v1/...` paths for local OSS servers (flat routes).
|
|
28
|
+
*/
|
|
29
|
+
resolveRequestPath(path: string): string;
|
|
30
|
+
request<T = unknown>(method: string, url: string, data?: unknown, config?: RequestConfig): Promise<ApiResponse<T>>;
|
|
31
|
+
/**
|
|
32
|
+
* Make a streaming request that returns the raw Response.
|
|
33
|
+
*
|
|
34
|
+
* Unlike `request()`, this does NOT parse JSON or apply timeout.
|
|
35
|
+
* The caller reads `response.body` via `getReader()`.
|
|
36
|
+
* Used for SSE / chunked-transfer endpoints (e.g., chat streaming).
|
|
37
|
+
*/
|
|
38
|
+
requestStream(method: string, url: string, data?: unknown): Promise<Response>;
|
|
39
|
+
/**
|
|
40
|
+
* Recursively normalize _id → id in response data.
|
|
41
|
+
* Skips if id already exists to avoid overwriting.
|
|
42
|
+
*/
|
|
43
|
+
private normalizeIds;
|
|
44
|
+
/**
|
|
45
|
+
* Update authentication credentials
|
|
46
|
+
*/
|
|
47
|
+
updateAuth(authConfig: {
|
|
48
|
+
apiKey?: string;
|
|
49
|
+
jwtToken?: string;
|
|
50
|
+
}): void;
|
|
51
|
+
/**
|
|
52
|
+
* Get current configuration
|
|
53
|
+
*/
|
|
54
|
+
getConfig(): ChanlConfig;
|
|
55
|
+
/**
|
|
56
|
+
* Enable/disable debug logging
|
|
57
|
+
*/
|
|
58
|
+
setDebug(enabled: boolean): void;
|
|
59
|
+
}
|
|
60
|
+
//# sourceMappingURL=client.d.ts.map
|